Re: [PATCH v16 2/5] random: add vgetrandom_alloc() syscall

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Jun 07, 2024 at 04:41:26PM +0200, Jason A. Donenfeld wrote:
> On Tue, Jun 04, 2024 at 10:22:49AM -0700, Eric Biggers wrote:
> > On Sat, Jun 01, 2024 at 12:56:40PM +0200, Jason A. Donenfeld wrote:
> > > On Thu, May 30, 2024 at 08:59:17PM -0700, Eric Biggers wrote:
> > > > On Tue, May 28, 2024 at 02:19:51PM +0200, Jason A. Donenfeld wrote:
> > > > > +/**
> > > > > + * sys_vgetrandom_alloc - Allocate opaque states for use with vDSO getrandom().
> > > > > + *
> > > > > + * @num:	   On input, a pointer to a suggested hint of how many states to
> > > > > + * 		   allocate, and on return the number of states actually allocated.
> > > > > + *
> > > > > + * @size_per_each: On input, must be zero. On return, the size of each state allocated,
> > > > > + * 		   so that the caller can split up the returned allocation into
> > > > > + * 		   individual states.
> > > > > + *
> > > > > + * @addr:	   Reserved, must be zero.
> > > > > + *
> > > > > + * @flags:	   Reserved, must be zero.
> > > > > + *
> > > > > + * The getrandom() vDSO function in userspace requires an opaque state, which
> > > > > + * this function allocates by mapping a certain number of special pages into
> > > > > + * the calling process. It takes a hint as to the number of opaque states
> > > > > + * desired, and provides the caller with the number of opaque states actually
> > > > > + * allocated, the size of each one in bytes, and the address of the first
> > > > > + * state, which may be split up into @num states of @size_per_each bytes each,
> > > > > + * by adding @size_per_each to the returned first state @num times, while
> > > > > + * ensuring that no single state straddles a page boundary.
> > > > > + *
> > > > > + * Returns the address of the first state in the allocation on success, or a
> > > > > + * negative error value on failure.
> > > > > + *
> > > > > + * The returned address of the first state may be passed to munmap(2) with a
> > > > > + * length of `(size_t)num * (size_t)size_per_each`, in order to deallocate the
> > > > > + * memory, after which it is invalid to pass it to vDSO getrandom().
> > > > 
> > > > Wouldn't a munmap with '(size_t)num * (size_t)size_per_each' be potentially too
> > > > short, due to how the allocation is sized such that states don't cross page
> > > > boundaries?
> > > 
> > > You're right, I think. The calculation should instead be something like:
> > > 
> > >     DIV_ROUND_UP(num, PAGE_SIZE / size_per_each) * PAGE_SIZE
> > > 
> > > Does that seem correct to you?
> > > 
> > 
> > Yes, though I wonder if it would be better to give userspace the number of pages
> > instead of the number of states.
> 
> Or maybe just the number of total bytes allocated? That would match
> what's expected to be passed to munmap() and is maybe the easiest to
> deal with. I'll give that a shot for v+1.

Hmm, though, on second thought,

 * @num:           On input, a pointer to a suggested hint of how many states to
 *                 allocate, and on return the number of states actually allocated.

This is kind of elegant -- it's an in/out param. Changing the semantics
of the out param isn't super obvious. And bytes means it should probably
be a long too. So maybe I'll keep it as is, and fix the documentation to
have the right calculation.

Jason




[Index of Archives]     [Kernel]     [Gnu Classpath]     [Gnu Crypto]     [DM Crypt]     [Netfilter]     [Bugtraq]
  Powered by Linux