Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Wed, 28 Jan 2015 09:54:10 -0500
From: Rich Felker <dalias@...c.org>
To: musl@...ts.openwall.com
Subject: Re: getrandom syscall

On Tue, Jan 27, 2015 at 11:12:46PM +0100, Daniel Cegiełka wrote:
> best regards,
> Daniel

Thanks. I've been wanting to get this added as well as a getentropy
function (the other API for the same thing).

> #include <stddef.h>
> #include <errno.h>
> #include "syscall.h"
> 
> int getrandom(void *buf, size_t len)
> {
> 	int ret, pre_errno = errno;

There's no need to save/restore errno here. errno is only meaningful
after a function returns an error code. On success it should not be
inspected and could contain junk.

> 	if (len > 256) {
> 		errno = EIO;
> 		return -1;
> 	}

Could you explain the motivation for this part?

> 	do {
> 		ret = syscall(SYS_getrandom, buf, len, 0);
> 	} while (ret == -1 && errno == EINTR);

This would be more efficient (and avoid your errno issue entirely) if
you use __syscall which returns -errcode rather than storing errcode
in errno. It allows the whole loop to be inlined with no function
call. Something like:

	while ((ret = __syscall(SYS_getrandom, buf, len, 0)) == -EINTR);

Of course there's the question of whether it should loop on EINTR
anyway; I don't know. Also if this can block there's the question of
whether it should be cancellable, but that can be decided later.

Finally, I wonder if it would make sense to use other fallbacks in the
case where the syscall is not supported -- perhaps the aux vector
AT_RANDOM or even /dev/urandom? (But I'd rather avoid doing anything
that depends on fds, which would make a function that appears to
always-work but actually fails on resource exhaustion.)

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.