Skip to contentSkip to conversation

Return type of getrandom(2)

DISCUSSION Florian Weimer 3 participants 5 messages Oct 8, 2016 – Oct 12, 2016 3 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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?

#2 lore raw
In reply to a message not in the archive
Quoted 9 lines · Florian Weimer

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

#4 lore raw
In reply to a message not in the archive

* Theodore Ts'o:

Quoted 11 lines · Florian Weimer
Quoted 9 lines

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?

Quoted 8 lines

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.

Quoted 4 lines

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 [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
In reply to a message not in the archive
Quoted 11 lines · Theodore Ts'o
Quoted 9 lines · Florian Weimer

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

Quoted 1 line

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

#7 lore raw
In reply to a message not in the archive

* Andy Lutomirski:

Quoted 16 lines · Theodore Ts'o
Quoted 11 lines · Florian Weimer
Quoted 9 lines

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.

Quoted 7 lines
Quoted 2 lines

(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 [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html