public inbox for linux-man@vger.kernel.org
 help / color / mirror / Atom feed
* Return type of getrandom(2)
@ 2016-10-08 12:28 Florian Weimer
       [not found] ` <87mvifnhxw.fsf-ZqZwdwZz9NfTBotR3TxKnbNAH6kLmebB@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: Florian Weimer @ 2016-10-08 12:28 UTC (permalink / raw)
  To: linux-api-u79uwXL29TY76Z2rM5mHXA,
	linux-man-u79uwXL29TY76Z2rM5mHXA
  Cc: Heinrich Schuchardt, Theodore Ts'o

The manual page says the return type of getrandom(2) is int, but
ssize_t would be more natural (see read(2) for comparison).  The
kernel uses ssize_t internally, which is converted to long on the
system call boundary.

The difference does not currently matter because the return value is
limited to much less than INT_MAX in the implementation.

Should we use int or ssize_t in the glibc system call wrapper?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Return type of getrandom(2)
       [not found] ` <87mvifnhxw.fsf-ZqZwdwZz9NfTBotR3TxKnbNAH6kLmebB@public.gmane.org>
@ 2016-10-08 19:40   ` Theodore Ts'o
       [not found]     ` <20161008194005.uo7uwiaukgk4y7ku-AKGzg7BKzIDYtjvyW6yDsg@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: Theodore Ts'o @ 2016-10-08 19:40 UTC (permalink / raw)
  To: Florian Weimer
  Cc: linux-api-u79uwXL29TY76Z2rM5mHXA,
	linux-man-u79uwXL29TY76Z2rM5mHXA, Heinrich Schuchardt

On Sat, Oct 08, 2016 at 02:28:27PM +0200, Florian Weimer wrote:
> The manual page says the return type of getrandom(2) is int, but
> ssize_t would be more natural (see read(2) for comparison).  The
> kernel uses ssize_t internally, which is converted to long on the
> system call boundary.
> 
> The difference does not currently matter because the return value is
> limited to much less than INT_MAX in the implementation.
> 
> Should we use int or ssize_t in the glibc system call wrapper?

I'd suggest keeping it as an int since (a) OpenBSD's getentropy(2)
returns an int, and part of the orignal design goal is to be able to
emulate OpenBSD's getentropy(2) system call via:

int getentropy(void *buf, size_t buflen)
{
	return getrandom(buf, buflen, 0);
}

and (b) the maximum number of bytes returned will *always* be well
under INT_MAX.  I can't forsee at any point in any future or alternate
universe where getrandom() would need to return anywhere near
SHORT_MAX, let alone INT_MAX.

Cheers,

					- Ted

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Return type of getrandom(2)
       [not found]     ` <20161008194005.uo7uwiaukgk4y7ku-AKGzg7BKzIDYtjvyW6yDsg@public.gmane.org>
@ 2016-10-09  9:47       ` Florian Weimer
  2016-10-09 16:53       ` Andy Lutomirski
  1 sibling, 0 replies; 5+ messages in thread
From: Florian Weimer @ 2016-10-09  9:47 UTC (permalink / raw)
  To: Theodore Ts'o
  Cc: linux-api-u79uwXL29TY76Z2rM5mHXA,
	linux-man-u79uwXL29TY76Z2rM5mHXA, Heinrich Schuchardt

* Theodore Ts'o:

> On Sat, Oct 08, 2016 at 02:28:27PM +0200, Florian Weimer wrote:
>> The manual page says the return type of getrandom(2) is int, but
>> ssize_t would be more natural (see read(2) for comparison).  The
>> kernel uses ssize_t internally, which is converted to long on the
>> system call boundary.
>> 
>> The difference does not currently matter because the return value is
>> limited to much less than INT_MAX in the implementation.
>> 
>> Should we use int or ssize_t in the glibc system call wrapper?
>
> I'd suggest keeping it as an int since (a) OpenBSD's getentropy(2)
> returns an int, and part of the orignal design goal is to be able to
> emulate OpenBSD's getentropy(2) system call via:
>
> int getentropy(void *buf, size_t buflen)
> {
> 	return getrandom(buf, buflen, 0);
> }

But this implementation is quite wrong.  It has to look like something
like this:

int
getentropy (void *buf, size_t buflen)
{
  ssize_t ret = getrandom (buf, buflen, 0)
  if (ret < 0)
    return -1;
  if (ret < buflen)
    {
      errno = EIO;
      return -1;
    }
  return 0;
}

The ssize_t return would hint to the fact that such a wrapper is
required because the interfaces are somewhat different.

> and (b) the maximum number of bytes returned will *always* be well
> under INT_MAX.  I can't forsee at any point in any future or alternate
> universe where getrandom() would need to return anywhere near
> SHORT_MAX, let alone INT_MAX.

Right, that's true for the Linux implementation.  The question is
whether it applies to other implementations as well.  Solaris appears
to have an even lower limit.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Return type of getrandom(2)
       [not found]     ` <20161008194005.uo7uwiaukgk4y7ku-AKGzg7BKzIDYtjvyW6yDsg@public.gmane.org>
  2016-10-09  9:47       ` Florian Weimer
@ 2016-10-09 16:53       ` Andy Lutomirski
       [not found]         ` <CALCETrWgc0Wwd5AQ8fk3qPabsXKAgFZmH5O6w0CrcXEx+Pwy-Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  1 sibling, 1 reply; 5+ messages in thread
From: Andy Lutomirski @ 2016-10-09 16:53 UTC (permalink / raw)
  To: Ted Ts'o; +Cc: Heinrich Schuchardt, Florian Weimer, linux-man, Linux API

On Oct 8, 2016 4:17 PM, "Theodore Ts'o" <tytso-3s7WtUTddSA@public.gmane.org> wrote:
>
> On Sat, Oct 08, 2016 at 02:28:27PM +0200, Florian Weimer wrote:
> > The manual page says the return type of getrandom(2) is int, but
> > ssize_t would be more natural (see read(2) for comparison).  The
> > kernel uses ssize_t internally, which is converted to long on the
> > system call boundary.
> >
> > The difference does not currently matter because the return value is
> > limited to much less than INT_MAX in the implementation.
> >
> > Should we use int or ssize_t in the glibc system call wrapper?

I think it should be ssize_t.  Having the types mismatched across the
syscall boundary is just confusing.

(b) the maximum number of bytes returned will *always* be well
> under INT_MAX.

I would argue that this particular ship sailed when the len parameter
was given the type size_t.  The door is open for requests bigger than
2GiB.  Even if Linux will never honor those requests, I see no reason
to make their return value have a nonsensical type.

--Andy

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Return type of getrandom(2)
       [not found]         ` <CALCETrWgc0Wwd5AQ8fk3qPabsXKAgFZmH5O6w0CrcXEx+Pwy-Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2016-10-12  6:30           ` Florian Weimer
  0 siblings, 0 replies; 5+ messages in thread
From: Florian Weimer @ 2016-10-12  6:30 UTC (permalink / raw)
  To: Andy Lutomirski; +Cc: Ted Ts'o, Heinrich Schuchardt, linux-man, Linux API

* Andy Lutomirski:

> On Oct 8, 2016 4:17 PM, "Theodore Ts'o" <tytso-3s7WtUTddSA@public.gmane.org> wrote:
>>
>> On Sat, Oct 08, 2016 at 02:28:27PM +0200, Florian Weimer wrote:
>> > The manual page says the return type of getrandom(2) is int, but
>> > ssize_t would be more natural (see read(2) for comparison).  The
>> > kernel uses ssize_t internally, which is converted to long on the
>> > system call boundary.
>> >
>> > The difference does not currently matter because the return value is
>> > limited to much less than INT_MAX in the implementation.
>> >
>> > Should we use int or ssize_t in the glibc system call wrapper?
>
> I think it should be ssize_t.  Having the types mismatched across the
> syscall boundary is just confusing.
>
>> (b) the maximum number of bytes returned will *always* be well
>> under INT_MAX.
>
> I would argue that this particular ship sailed when the len parameter
> was given the type size_t.  The door is open for requests bigger than
> 2GiB.  Even if Linux will never honor those requests, I see no reason
> to make their return value have a nonsensical type.

Thanks for your input.  I'm leaning towards ssize_t now as well.
Ted's getentropy example was helpful as well.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-10-12  6:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-10-08 12:28 Return type of getrandom(2) Florian Weimer
     [not found] ` <87mvifnhxw.fsf-ZqZwdwZz9NfTBotR3TxKnbNAH6kLmebB@public.gmane.org>
2016-10-08 19:40   ` Theodore Ts'o
     [not found]     ` <20161008194005.uo7uwiaukgk4y7ku-AKGzg7BKzIDYtjvyW6yDsg@public.gmane.org>
2016-10-09  9:47       ` Florian Weimer
2016-10-09 16:53       ` Andy Lutomirski
     [not found]         ` <CALCETrWgc0Wwd5AQ8fk3qPabsXKAgFZmH5O6w0CrcXEx+Pwy-Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-10-12  6:30           ` Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox