random: introduce getrandom(2) system call
Subsystems (2)
One basic question... why limit this to /dev/random?
If we're trying to avoid fd exhaustion attacks, wouldn't an "atomically read a file into a buffer" system call (that could be used on /dev/urandom, or /etc/hostname, or /proc/foo, or...) be more useful?
E.g.
ssize_t readat(int dirfd, char const *path, struct stat *st,
char *buf, size_t len, int flags);
It's basically equivalent to openat(), optional fstat() (if st is non-NULL), read(), close(), but it doesn't allocate an fd number.
Is it necessary to have a system call just for entropy?
If you want a "urandom that blocks until seeded", you can always create another device node for the purpose.
Quoted 4 lines
The main argument I can see for putting in a limit is to encourage the "proper" use of the interface. In practice, anything larger than 128 probably means the interface is getting misused, either due to a bug or some other kind of oversight.
Agreed. Even 1024 bits is excessive. 32 bytes is the "real" maximum that people should be asking for with current primitives, so an interface limitation to 64 is quite defensible. (But 128 isn't *wildly* excessive.)
If you do stick with a random-specific call, specifying the entropy in bits (with some specified convention for the last fractional byte) is anothet interesting idea. Perhaps too prone to bugs, though. (People thinking it's bytes and producing low-entropy keys.)
Quoted 4 lines
In the end people would just recall getentropy in a loop and fetch 256 bytes each time. I don't think the artificial limit does make any sense. I agree that this allows a potential misuse of the interface, but doesn't a warning in dmesg suffice?
It makes their code not work, so they can are forced to think about fixing it before adding the obvious workaround.
Quoted 2 lines
It also makes it easier to port applications from
open("/dev/*random"), read(...) togetentropy()by reusing the same limits.
But such an application *is broken*. Making it easier to port is an anti-goal. The goal is to make it enough of a hassle that people will *fix* their code.
There's a *reason* that the /dev/random man page explicitly tells people not to trust software that reads more than 32 bytes at a time from /dev/random:
Quoted 7 lines
While some safety margin above that minimum is reasonable, as a guard against flaws in the CPRNG algorithm, no cryptographic primitive avail- able today can hope to promise more than 256 bits of security, so if any program reads more than 256 bits (32 bytes) from the kernel random pool per invocation, or per reasonable reseed interval (not less than one minute), that should be taken as a sign that its cryptography is *not* skillfully implemented.
("not skilfuly implemented" was the phrase chosen after some discussion to convey "either a quick hack or something you dhouldn't trust.")
To expand on what I said in my mail to Ted, 256 is too high. I'd go with OpenBSD's 128 bytes or even drop it to 64.
Theodore Ts'o
maintainerQuoted 11 lines · George Spelvin
One basic question... why limit this to
/dev/random?If we're trying to avoid fd exhaustion attacks, wouldn't an "atomically read a file into a buffer" system call (that could be used on
/dev/urandom, or/etc/hostname, or/proc/foo, or...) be more useful?E.g.
ssize_treadat(int dirfd, char const *path, struct stat *st,char *buf, size_t len, int flags);
Quoted 7 lines
It's basically equivalent to
openat(), optionalfstat()(if st is non-NULL),read(),close(), but it doesn't allocate an fd number.Is it necessary to have a system call just for entropy?
If you want a "urandom that blocks until seeded", you can always create another device node for the purpose.
I'd really rather not go down this path. Your readat(2) proposal is interesting, but it adds a whole lot of complications. For example, just simply booting a new kernel doesn't guarantee that a new device node for "blocks until seeded" will exist. So that means a lot of applications will just either continue to use /dev/urandom, or have to put in fallback code to first try the new device name, and then fall back to /dev/urandom. (And of course, they have to deal with what to do if /dev/urandom doesn't exist --- which presumably would be raise(SIGKILL), but we're now talking about a number of lines of codes that application writers would have to get right.)
Readat(2) would also have to get linked into auditing, and LSM, and honestly, it's a lot more work that I'm not all that interested in doing and trying to get right.
Quoted 4 lines
If you do stick with a random-specific call, specifying the entropy in bits (with some specified convention for the last fractional byte) is anothet interesting idea. Perhaps too prone to bugs, though. (People thinking it's bytes and producing low-entropy keys.)
Definitely not worth the complexity.
- Ted
Quoted 27 lines · George Spelvin
Quoted 4 lines
In the end people would just recall getentropy in a loop and fetch 256 bytes each time. I don't think the artificial limit does make any sense. I agree that this allows a potential misuse of the interface, but doesn't a warning in dmesg suffice?
It makes their code not work, so they can are forced to think about fixing it before adding the obvious workaround.
Quoted 2 lines
It also makes it easier to port applications from
open("/dev/*random"), read(...) togetentropy()by reusing the same limits.But such an application *is broken*. Making it easier to port is an anti-goal. The goal is to make it enough of a hassle that people will *fix* their code.
There's a *reason* that the
/dev/randomman page explicitly tells people not to trust software that reads more than 32 bytes at a time from/dev/random:Quoted 7 lines
While some safety margin above that minimum is reasonable, as a guard against flaws in the CPRNG algorithm, no cryptographic primitive avail- able today can hope to promise more than 256 bits of security, so if any program reads more than 256 bits (32 bytes) from the kernel random pool per invocation, or per reasonable reseed interval (not less than one minute), that should be taken as a sign that its cryptography is *not* skillfully implemented.
Quoted 5 lines
("not skilfuly implemented" was the phrase chosen after some discussion to convey "either a quick hack or something you dhouldn't trust.")
To expand on what I said in my mail to Ted, 256 is too high. I'd go with OpenBSD's 128 bytes or even drop it to 64.
I don't like partial reads/writes and think that a lot of people get them wrong, because they often only check for negative return values.
I thought about the following check (as replacement for the old check):
/* we will always generate a partial buffer fill */ if (flags & GRND_RANDOM && count > 512)
return -EINVAL;
We could also be more conservative and return -EINVAL in case a stray write happened if one tried to extract less than 512 by checking the return values of random_read(), but somehow this sounds dangerous to me.
In case of urandom extraction, I wouldn't actually limit the number of bytes. A lot of applications I have seen already extract more than 128 out of urandom (not for seeding a prng but just to mess around with some memory). I don't see a reason why getrandom shouldn't be used for that. It just adds one more thing to look out for if using getrandom() in urandom mode, especially during porting an application over to this new interface.
Bye,
Hannes
Quoted 2 lines
I don't like partial reads/writes and think that a lot of people get them wrong, because they often only check for negative return values.
The v1 patch, which did it right IMHO, didn't do partial reads in the case we're talking about:
+ if (count > 256)
+ return -EINVAL;
Quoted 7 lines
In case of urandom extraction, I wouldn't actually limit the number of bytes. A lot of applications I have seen already extract more than 128 out of urandom (not for seeding a prng but just to mess around with some memory). I don't see a reason why getrandom shouldn't be used for that. It just adds one more thing to look out for if using
getrandom()in urandom mode, especially during porting an application over to this new interface.
Again, I disagree. If it's "just messing around" code, use /dev/urandom. It's more portable and you don't care about the fd exhaustion attacks.
If it's actual code to be used in anger, fix it to not abuse /dev/urandom.
You're right that a quick hack might be "broken on purpose", but without exception, *all* code that I have seen which reads 64 or more bytes from /dev/*random is broken, and highlighting the brokenness is a highly desirable thing.
The sole and exclusive reason for this syscall to exist at all is to solve a security problem. Supporting broken security code does no favors to anyone.
Review
Quoted 8 lines · George Spelvin
Quoted 2 lines
I don't like partial reads/writes and think that a lot of people get them wrong, because they often only check for negative return values.
The v1 patch, which did it right IMHO, didn't do partial reads in the case we're talking about:
(quoted diff) +2
-0+ if (count > 256) + return -EINVAL;
I checked and unfortunately it does not; 256 bytes is way too large to guarantee non-existence of partial reads. On a typical older system without rdrand/rdseed you would have to limit the amount of bytes to extract to about 32. That's way too low. That said, the 512 bytes check only in case of extracting bytes from blocking pool would serve no purpose.
Quoted 13 lines
Quoted 7 lines
In case of urandom extraction, I wouldn't actually limit the number of bytes. A lot of applications I have seen already extract more than 128 out of urandom (not for seeding a prng but just to mess around with some memory). I don't see a reason why getrandom shouldn't be used for that. It just adds one more thing to look out for if using
getrandom()in urandom mode, especially during porting an application over to this new interface.Again, I disagree. If it's "just messing around" code, use
/dev/urandom. It's more portable and you don't care about the fd exhaustion attacks.If it's actual code to be used in anger, fix it to not abuse
/dev/urandom.
Quoted 8 lines
You're right that a quick hack might be "broken on purpose", but without exception, *all* code that I have seen which reads 64 or more bytes from
/dev/*randomis broken, and highlighting the brokenness is a highly desirable thing.The sole and exclusive reason for this syscall to exist at all is to solve a security problem. Supporting broken security code does no favors to anyone.
Then let's agree to disagree. :)
I think it is dangerous if application will get ported to this new interface without checking size limitations and will only notice it after the applications will be rolled out (if at all).
Bye,
Hannes
Quoted 7 lines · Hannes Frederic Sowa
Quoted 6 lines · George Spelvin
Quoted 2 lines
I don't like partial reads/writes and think that a lot of people get them wrong, because they often only check for negative return values.
The v1 patch, which did it right IMHO, didn't do partial reads in the case we're talking about:
Quoted 9 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (count > 256) + return -EINVAL;I checked and unfortunately it does not; 256 bytes is way too large to guarantee non-existence of partial reads. On a typical older system without rdrand/rdseed you would have to limit the amount of bytes to extract to about 32. That's way too low. That said, the 512 bytes check only in case of extracting bytes from blocking pool would serve no purpose.
Ted, would it make sense to specifiy a 512 byte upper bound limit for random entropy extraction (I am not yet convinced to do that for urandom) and in case the syscall should block we make sure that we extract the amount of bytes the user requested?
Having a quick look maybe something like this? Only compile tested and maybe can still be simplified. It guarantees we don't do a partial write to user space for sub 512 bytes requests.
drivers/char/random.c +13 -7
@@ -1345,8 +1345,14 @@ static int arch_random_refill(void)return n; } +enum blocking_mode { +NONBLOCKING , +SYSCALL_BLOCK , +DEV_RANDOM_BLOCK +}; +static ssize_t -_random_read (int nonblock,char __user *buf,size_t nbytes) +_random_read (enum blocking_mode mode,char __user *buf,size_t nbytes) {ssize_t n;
@@ -1361,7 +1367,7 @@ _random_read(int nonblock, char __user *buf, size_t nbytes)trace_random_read (n*8 , (nbytes-n)*8 ,ENTROPY_BITS (&blocking_pool),ENTROPY_BITS (&input_pool)); -if (n >0 ) +if (mode !=SYSCALL_BLOCK && n >0 )return n;/* Pool is (near) empty. Maybe wait and retry. */
@@ -1370,7 +1376,7 @@ _random_read(int nonblock, char __user *buf, size_t nbytes)if (arch_random_refill ())continue ; -if (nonblock) +if (mode ==NONBLOCKING )return -EAGAIN ; wait_event_interruptible(random_read_wait,
@@ -1384,7 +1390,8 @@ _random_read(int nonblock, char __user *buf, size_t nbytes)static ssize_t random_read (struct file *file,char __user *buf,size_t nbytes,loff_t *ppos) { -return _random_read (file->f_flags &O_NONBLOCK , buf, nbytes); +return _random_read (file->f_flags &O_NONBLOCK ?NONBLOCKING : +DEV_RANDOM_BLOCK , buf, nbytes); } static ssize_t
@@ -1534,8 +1541,6 @@ const struct file_operations urandom_fops = { SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int, flags) { -int r; -if (flags & ~(GRND_NONBLOCK |GRND_RANDOM ))return -EINVAL ;
@@ -1543,7 +1548,8 @@ SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, count =INT_MAX ;if (flags &GRND_RANDOM ) -return _random_read (flags &GRND_NONBLOCK , buf, count); +return _random_read (flags &GRND_NONBLOCK ?NONBLOCKING : +SYSCALL_BLOCK , buf, count);if (unlikely (nonblocking_pool.initialized ==0 )) {if (flags &GRND_NONBLOCK )
Bye,
Hannes
Theodore Ts'o
maintainerQuoted 5 lines · Hannes Frederic Sowa
Ted, would it make sense to specifiy a 512 byte upper bound limit for random entropy extraction (I am not yet convinced to do that for urandom) and in case the syscall should block we make sure that we extract the amount of bytes the user requested?
On some systems, it's still possible that with a large /dev/random extraction, you could end up blocking for hours. So either the getrandom(2) syscall needs to be uninterruptible, which has one set of problems (and not just the user typing ^C, but also things like being able to process alarms, which is highly problematic indeed), or you need to allow it to be interruptible by a signal, in which case userspace needs to check the error return for things like EINTR anyway. And if you have to check the error return, you might as well check the number of bytes returned.
Yes, one could in theory set up a new variant of "uninterruptible" signals that only exited if the signal caused the process to exit, and otherwise, forced a system call restart even if SA_INTERRUPTIBLE was not set in sigalarim, but that's add *way* more complexity than this deserves.
Basically, I view /dev/random as an advanced call, and someone who uses it should know what they are doing. It's not the default for a reason.
- Ted
Hello,
Quoted 7 lines · Theodore Ts'o
Quoted 5 lines · Hannes Frederic Sowa
Ted, would it make sense to specifiy a 512 byte upper bound limit for random entropy extraction (I am not yet convinced to do that for urandom) and in case the syscall should block we make sure that we extract the amount of bytes the user requested?
Quoted 9 lines
On some systems, it's still possible that with a large
/dev/randomextraction, you could end up blocking for hours. So either the getrandom(2) syscall needs to be uninterruptible, which has one set of problems (and not just the user typing ^C, but also things like being able to process alarms, which is highly problematic indeed), or you need to allow it to be interruptible by a signal, in which case userspace needs to check the error return for things like EINTR anyway. And if you have to check the error return, you might as well check the number of bytes returned.
I think a lot of checks are of the type "if (getrandom() < 0)", so this actually was the kind of programming errors I wanted to guard against. Also, on some systems it is very likely that we return a short write to user space, so it prevents this very common situation. Even if one would like to extract 64 bytes randomness, one would often need two calls to getrandom() on my virtualized systems. Would be great to know that in blocking mode, either I get a -1 or the buffer is always filled and I won't need to do pointer arithmetic to fill the rest of the buffer.
I still think it is a good idea even without caring about the signal interruptions.
Quoted 5 lines
Yes, one could in theory set up a new variant of "uninterruptible" signals that only exited if the signal caused the process to exit, and otherwise, forced a system call restart even if
SA_INTERRUPTIBLEwas not set in sigalarim, but that's add *way* more complexity than this deserves.
I don't want to do that. It is totally ok if we return -1 and set errno to EINTR in case of pending signals.
Quoted 3 lines
Basically, I view
/dev/randomas an advanced call, and someone who uses it should know what they are doing. It's not the default for a reason.
I agree, but I still think this would make the interface a bit more friendly to use.
Thanks,
Hannes
Theodore Ts'o
maintainerQuoted 9 lines · Hannes Frederic Sowa
I think a lot of checks are of the type "if (
getrandom()< 0)", so this actually was the kind of programming errors I wanted to guard against. Also, on some systems it is very likely that we return a short write to user space, so it prevents this very common situation. Even if one would like to extract 64 bytes randomness, one would often need two calls togetrandom()on my virtualized systems. Would be great to know that in blocking mode, either I get a -1 or the buffer is always filled and I won't need to do pointer arithmetic to fill the rest of the buffer.
But why would you need to use GRND_RANDOM in your scenario, and accept your application potentially getting stalled and stuck in amber for perhaps hours? If you are going to accept your application stalling like that, you can do the pointer arithmatic. It's really not hard, and someone who can't do that, again, shouldn't be allowd anywhere near crypto code in the first place (and if they are, they'll probably be making lots of other, equally fatal if not more so, newbie mistakes).
- Ted
Hi,
Quoted 8 lines · Theodore Ts'o
But why would you need to use
GRND_RANDOMin your scenario, and accept your application potentially getting stalled and stuck in amber for perhaps hours? If you are going to accept your application stalling like that, you can do the pointer arithmatic. It's really not hard, and someone who can't do that, again, shouldn't be allowd anywhere near crypto code in the first place (and if they are, they'll probably be making lots of other, equally fatal if not more so, newbie mistakes).
I favored the idea of having a non-failing non-partial-read getrandom syscall. But I am with you if it often causes long stalls that we should stick to the old semantics.
Thanks,
Hannes
I keep wishing for a more general solution. For example, some way to have a "spare" extra fd that could be accessed with a special O_NOFAIL flag.
That would allow any number of library functions to not fail, such as logging from nasty corner cases.
But you'd have to provide one per thread, and block non-fatal signals while it was open, so you don't get reentrancy problems. Ick.
This overly-specialized system call (and worse yet, a blocking system call that you can't put into a poll() loop) just feels ugly to me. Is it *absolutely* necessary?
For example, how about simply making getentropy() a library function that aborts if it can't open /dev/urandom? If you're suffering fd exhaustion, you're being DoSed already.
Quoted 7 lines · George Spelvin
I keep wishing for a more general solution. For example, some way to have a "spare" extra fd that could be accessed with a special
O_NOFAILflag.That would allow any number of library functions to not fail, such as logging from nasty corner cases.
Quoted 7 lines
But you'd have to provide one per thread, and block non-fatal signals while it was open, so you don't get reentrancy problems. Ick.
This overly-specialized system call (and worse yet, a blocking system call that you can't put into a
poll()loop) just feels ugly to me. Is it *absolutely* necessary?
One point that often came up besides fd exhaustion is missing /dev/u?random device nodes in chroot environments.
I also thought about a more general interface, like e.g. an opennod(dev_t device, int flags) call but all those ideas ended up being very complex changes besides having design issues. getrandom is simple and solves a real problem.
The only problem I see, that we allow access to /dev/random without checking any permission bits like we did on opening /dev/random before and we cannot restrict applications to deplete the whole entropy pool.
Quoted 3 lines
For example, how about simply making
getentropy()a library function that aborts if it can't open/dev/urandom? If you're suffering fd exhaustion, you're being DoSed already.
Maybe applications want to mitigate fd exhaustion.
Bye,
Hannes
Quoted 14 lines · Hannes Frederic Sowa
Quoted 11 lines · George Spelvin
I keep wishing for a more general solution. For example, some way to have a "spare" extra fd that could be accessed with a special
O_NOFAILflag.That would allow any number of library functions to not fail, such as logging from nasty corner cases.
But you'd have to provide one per thread, and block non-fatal signals while it was open, so you don't get reentrancy problems. Ick.
Quoted 8 lines
Quoted 3 lines
This overly-specialized system call (and worse yet, a blocking system call that you can't put into a
poll()loop) just feels ugly to me. Is it *absolutely* necessary?One point that often came up besides fd exhaustion is missing /dev/u?random device nodes in chroot environments.
From the maillist discussion, it seems you sometimes _want_
/dev/random not to be present.
For example you want to trace exactly the same path through malware every time.
Quoted 5 lines
Quoted 3 lines
For example, how about simply making
getentropy()a library function that aborts if it can't open/dev/urandom? If you're suffering fd exhaustion, you're being DoSed already.Maybe applications want to mitigate fd exhaustion.
Dunno. Will we add special read_passwd() syscall that reads just /etc/passwd, to allow uid<->name resolution without available FDs?
I like the library function suggestion, this should not need a new syscall.
And btw -- compatibility with getentropy() is _not_ going to be easy, if they have different blocking / partial read / signals policy.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html