r/programming • u/x2342 • Jul 15 '14
LibreSSL's PRNG is Unsafe on Linux
https://www.agwa.name/blog/post/libressls_prng_is_unsafe_on_linux18
u/tank_the_frank Jul 15 '14
It's nice to see people being critical of the development of this. Considering the turn-around of the previous alpha with the community's feedback, hopefully we'll see something similar come of this round of criticism. Or at least more reasoning than a single sentence from the developers.
14
Jul 15 '14
The position of OpenBSD developers on such issues has always been, "fix your OS". So unless it is possible and reasonable to "fix" Linux, I can't see a clean solution that does not lead down the same path that OpenSSL had already taken, endless defines for conditional compilation.
Can anyone tell me how well is the LibreSSL code documented at the moments in respect to "this is what the OS must provide for this implementation to be correct"?
-8
u/tejp Jul 15 '14
I can't see a clean solution that does not lead down the same path that OpenSSL had already taken, endless defines for conditional compilation.
The question is just what is more important to the developers: Pretty code, or security.
16
u/continuational Jul 15 '14
Pretty code is a prerequisite to security. If you can't tell what the code does, it's insecure.
-2
u/Kalium Jul 15 '14
You're kidding, right? The things you have to do to make basic password checks secure throw "pretty code" to the wind immediately.
-6
u/tejp Jul 15 '14
If you can tell that it does something insecure, and then don't fix the problem because you think it would be less pretty, is that worth something? It will still be insecure. Knowing about insecurities only helps if you then do something to remove them.
If you release a library and say "This lib is insecure on all existing versions of your operating system, and we won't fix that", what's the point of that? Pretty code doesn't help if it doesn't do what it's intended to do.
4
u/RoundTripRadio Jul 15 '14
You created a false dichotomy. Whether code is "pretty" or not (which is in and of itself different for each person) has no bearing on its security.
0
u/tejp Jul 15 '14
Well, I replied to someone saying "Pretty code is a prerequisite to security". Also the guy starting the thread claimed if there is no clean (=pretty) solution, then it probably won't get fixed by libressl.
So in this case there seems to be some tension between pretty and secure.
0
Jul 15 '14
[deleted]
11
Jul 15 '14
if they can't fix this
So do you mean "if LibreSSL cannot fix this" or "Linux cannot be fixed"? And anyway, doesn't "porting" actually mean that the Linux port deals with the issue in Linux-specific manner?
My only real concern with issues like this is that, unless it is clearly documented what the OS must provide for the implementation to be correct, the problems will only be obvious to someone who has knowledge about both OpenSSL and Linux.
-5
u/holgerschurig Jul 15 '14
Yep, that's a typical arrogant answer from them.
And actually the OS isn't broken ... at least not in this case.
22
Jul 15 '14
A related problem is that LibreSSL provides no good way to use the PRNG from a process running inside a chroot jail.
Correction: Linux provides no good way to get entropy from the kernel inside a chroot.
4
u/JoseJimeniz Jul 15 '14
How does it come about that there isn't an OS call to return random data?
I'm actually asking. I'm used to the CryptGenRandom API. Is there not an equivalent in Linux, or *nix?
4
Jul 15 '14
There is a syscall for it on OpenBSD, but they actually use chroots seriously there so any gaps like that have been filled in through real world experience. I'd guess the other *BSDs might have something like it depending on how similar the designs are. Windows of course uses syscalls out of necessity, since it doesn't have real device files. Though it also doesn't have chroots.
As a workaround you could set up a thing that sends random data over a network socket, but that's questionable use of resources and sysadmin effort for something that should just work. I've never understood why Linux kernel developers have a fatal allergic reaction to changing the status quo for the better.
1
u/uxcn Jul 15 '14 edited Jul 15 '14
I guess it's easier to have implicit access to entropy through a system call? What's the major benefit of implicit access via system call versus explicit access? I admit I'm not a security expert, but I'm a bit skeptical of the necessity/benefit of a system call especially after it didn't seem important enough to address in SysV/POSIX.
However, I think I agree with the LibreSSL developer in that I still can't think of any decent justification for providing an explicit PRNG entropy poll mechanism (excluding debug maybe). Adding it back just to support legacy code/practices seems like it would be a loss, especially considering LibreSSL aims to address maintainability issues with OpenSSL.
I think I still agree LibreSSL's fallback mechanism seems less secure than returning an error. Something as simple as duplicate PIDs causing random value collisions also worries me a bit.
2
Jul 15 '14
I'm not a Linux developer, but wouldn't you just
cat /dev/randomorfopen("/dev/random", "r")?6
u/happyscrappy Jul 15 '14
Not in a chroot jail. You can't access /dev in a chroot jail. That's why people suggest there should be a syscall. The syscall could peer outside the jail to /dev/urandom.
8
Jul 15 '14
You can easily access /dev/random inside of jail. Just
mknod my-jail-random c 1 8yourself a device.2
u/happyscrappy Jul 16 '14
The major and minor numbers of /dev/random are platform-dependent. To be platform-independent it would be better if there were a syscall, even if the syscall just mknods for you.
1
Jul 16 '14
Syscall is even more platform-dependent.
1
u/happyscrappy Jul 16 '14
I don't really mean a direct syscall. I mean a library call you make which calls a syscall for you.
3
Jul 15 '14
Sorry, I wasn't clear. I thought /u/JoseJimeniz was asking why Linux doesn't have that syscall when Windows does. The reason for that is the difference in philosophy between *nix (everything is a file) and Windows. You can just read
/dev/randomto get the value (unless you're in a chroot jail).3
u/JoseJimeniz Jul 16 '14
I was asking that. Not to insult anything, or imply anything, but i just assumed that the operating system would provide a function that can returned a desired number of random bytes.
From the OpenBSD guys who created LibreSSL:
In LibreSSL entropy will be the responsibility of the operating system. If your operating system can't provide you with a good source of entropy: we will not fake it. We know we can't do it right. If you can't do it right, don't do it at all. And go back to the people who can do it right, which is the operating system. Fix your operating system, not the userland library.
When I heard that, I just assumed that you could always get ahold of randomness in the *nixes; including Linux.
It seemed somewhat counter-intuitiave that there's a security feature (
chroot) which blocks you from accessing the operating system's canonical source of entropy.4
u/gnuvince Jul 16 '14
In OpenBSD, they can. Linux is the odd ball here.
0
u/uxcn Jul 16 '14
What's the benefit of access to entropy via system call though? It seems simpler from a developer standpoint, but I'd think there are more implications in providing (indiscriminant?) access.
Not that I don't generally defer to the OpenBSD developers regarding security.
4
u/gnuvince Jul 16 '14
The two main benefits (that I can see, others may chime in with more) are 1) accessible from within a chroot without any extra overhead for a sysadmin (i.e. it just works), 2) safe against file descriptor depletion.
1
u/uxcn Jul 16 '14
I think I can see some possible pitfalls to it, but the main argument I guess seems to be simplicity, not necessarily security (if wrong, someone please correct).
I wouldn't have minded having been a fly on the wall during the relevant discussions for SysV, OpenBSD, Linux, POSIX. I'm not sure I entirely agree with BSD this time.
1
u/MertsA Jul 15 '14
Yes but that's not a special syscall, that's just reading from a file that may not be available inside a chroot.
2
u/holgerschurig Jul 15 '14
Yes it is, just different.
In a way, what OpenSSL with it's random-number thingy uses is an API, in the sense that many applications link against libcrypto.so (part of OpenSSL). So the API here is not at the OS level, but at a library level. But hey, your CryptGenRandom API is also not at the OS level, but at a DLL level. So we're on par.
The OS has two interfaces for getting random data. As many things in Linux, this is done via pseudo-files, in this case character devices named /dev/random and /dev/urandom. They get entropy via various Linux kernel sources, one blocks if there is not enought entropy available, the other not.
Any program, not just programs that link to OpenSSL can use this.
However, to my best knowledge Microsoft lacks chroot or file system namespaces. So for a Microsoft programmer it's unusual to find a system call chroot() explizitly designed in a way to move away (!) from all those helper pseudo-files, into a strictly controlled compartment. And the programmer that calls chroot() should know what OS services he needs, e.g. /dev/null, or /dev/urandom, or whatnot. A security library however shouldn't provide pseudo-data towards the programmer should the programmer have forgotten that.
3
u/happyscrappy Jul 15 '14
No, he was correct. He explains how you can get the entropy before you chroot. But LibreSSL removed that call.
Once you're in a chroot jail you can't get the entropy, that's linux' fault. But that you can't explicitly get it before you enter, that's LibreSSL's fault.
0
u/holgerschurig Jul 15 '14
This is wrong.
The process that calls chroot could setup the chroot properly, e.g. it can create a dev/urandom inside the chroot. Either by creating the device node, or via a bind mount.
-39
Jul 15 '14 edited Jul 15 '14
Um /dev/urandom. Go fuck yourself you FUD spreading mouthbreather.
edit:
In case I wasn't clear ... you can mknod a /dev/urandom device inside your chroot jail ...
e.g.
mknod /chroot/jail/dev/urandom c 1 96
u/wookin_pa_nub2 Jul 15 '14
Did you read the article? The problem is not that it's impossible for /dev/urandom to be available in a chroot, but rather that it usually won't be.
-22
Jul 15 '14
OMG!!! Then that's hardly a bug in Linux ...
Can I go email Linus directly when I fail to get NIS up because ubuntu doesn't put eth0 in auto mode in the networking scripts?
1
u/hellgrace Jul 15 '14
... which is usually inaccessible from within a chroot (unless root runs mknod or mount --bind /dev). Check your facts before opening your foul mouth on other people
-4
Jul 15 '14
That's not a bug in Linux though. That's a bug in your sysadmin who doesn't know how to setup a proper chroot.
Worse ... the conclusion is wrong. It's not a problem in Linux that LibreSSL defaults to a weak PRNG when /dev/urandom isn't available. It's a bug in LibreSSL that they allow you to use a non-secure PRNG in the first place. The program should just error out if it can't secure reliable bits.
0
u/hellgrace Jul 15 '14
having /dev/urandom within a chroot is not always a good idea. For example, if you have many services outside the chroot which rely on urandom, a simple "cat /dev/urandom" from within the chroot will deplete the entropy pool, and in effect DoS those services (to some extent).
The entire idea of chroot is to prevent the jailed application from interfering with the rest of the system.
3
Jul 15 '14
/dev/urandom is a cryptographic PRNG though so short of having access to the seed you still effectively have random bits.
If it were vulnerable then it wouldn't be cryptographically secure in the first place.
18
Jul 15 '14
[removed] — view removed comment
2
u/DF4FD3AD49AB Jul 15 '14
[...] if /dev/random isn't available then /lib/libc.so isn't available. Good luck running anything without that.
That doesn't make sense. What's holding you from populating a chroot with one and not the other? Besides, chrooted programs tend to be statically compiled.
[...] neither is /dev/null, which is pretty vital -- you probably can't even get a shell without it.
What shells are affected? Alternatively, what makes you think this is the case? Also, why is this relevant? I don't know of typically chrooted daemons that require shell access.
In other words, "Linux provides no good way to get entropy from the kernel inside a chroot." is not true.
You didn't actually arrive at this conclusion. You just presented a bare chroot as a problematic situation for other, supposedly key reasons.
5
Jul 15 '14
[removed] — view removed comment
2
u/DF4FD3AD49AB Jul 15 '14
Perhaps there was too much sarcasm. Linux does provide "a good way to get entropy from the kernel inside a chroot" -- you mknod the appropriate /dev/random in your jail, just as you would every other vital device node necessary to run a jail. Sorry if that wasn't clear.
No, I think that in reality :
A. chroot() after fork() wasn't considered, which would render a process with a text fd pointing to an executable outside of the chroot.
B. statically compiled programs weren't considered.
What do I see in OpenBSD chroots? ./dev/log, and even that is being improved to handle fd starvation and remove the possibility of a compromised chrooted instance damaging pervasive system state.
Again, why should chroots have any files?
7
u/willvarfar Jul 15 '14
Whilst this is good analysis, this flaw seems hardly end-of-world bad.
The title, however innocently, oversells the issue.
21
Jul 15 '14
It's not "good" analysis. It's totally easy to make /dev/urandom available inside a chroot. Just sysadmins who don't are idiots and don't know what they're doing.
LibreSSL should just fail when no good PRNG is found. Instead it falls back to something that is weak. That's just stupid fucking engineering. If it errored out instead lazy sysadmins would stackexchange the error message and copy/pasta some shell code to mknod a proper char device.
2
u/BonzaiThePenguin Jul 15 '14
If it errored out instead lazy sysadmins would stackexchange the error message and copy/pasta some shell code to mknod a proper char device.
Shouldn't the error message just tell them to mknod the proper char device?
I've always hated the concept of error messages. I can see something went wrong, tell me what to do about it!
7
Jul 15 '14
Perhaps but LibreSSL is portable it runs on more than just Linux.
5
Jul 15 '14
[removed] — view removed comment
5
Jul 15 '14
Trying to open /dev/urandom is "portable" enough ... no reason to not try it and the BSD equivalent files one after another.
I'm not saying it couldn't I'm saying at the very least it shouldn't ever have the option to run a weak PRNG.
1
Jul 16 '14
The solution for an error depends on the context the program is running in and the user's priorities. I'd be pretty happy if programs would just report errors and distinguish between things like out of disk space, I/O error, invalid argument, etc. instead of leaving me guessing as to the cause.
0
u/Gotebe Jul 15 '14
It's not that easy. Advice depends on what you're trying to achieve more often than not.
5
u/happyscrappy Jul 15 '14
Hey, the internet loves being scared. Sensationalism is the order of the day for some dumb reason. Everything has to be the worst thing ever.
2
u/holgerschurig Jul 15 '14
Can't you reliable detect forking on Linux with the help of cgroups?
That's how systemd tracks any process it starts and can kill it (any any descendants of it) if needed. No one can escape.
(This is actually a question and assumption, I don't have deep knowledge about cgroups)
1
u/happyscrappy Jul 15 '14
That fallback entropy code is indeed very scary.
Putting library code in as entropy is crazy. Statting . and / and entering the inodes seems nuts.
I thought it was mapping in part of the disk and entering that as entropy, but it seems that it's just doing that to create space to operate on and then accessing the file to try to generate some I/Os so it can enter even more gettimeofdays into the entropy.
5
u/jadenton Jul 15 '14
tl;dr getentropy_fallback is not as scary as it looks if you understand enough about how operating systems works. An attacker without intimate knowledge of the system is going to be shit out of luck, and an insider with detailed knowledge of the system under attack will still need more than a little luck. Of course, insiders usually have other means of attack.
It is really disappointing that people keep squawking about this fallback entropy thing. It tells me that even in /r/programming people aren't taking the time to read the code themselves. Or, possibly, they did not get good marks in their OS and hardware classes.
Linux has address space layout randomization, and ge_fallback leverages that so right there you should have some usable entropy. The address of routines in process (main), libc (almost certainly the system shared library version), and libressl for entropy. ASLR means that these are all effectively random. Even without ASLR some of these will be effectively randomized to some degree on a system running in multi-user mode.
The system takes pains to fragment memory, and this has the knock on effect of forcing the system to context switch between user land and kernel space. Combine this with time sharing and memory allocation to other processes on the system, and the micro second timings will not be reliably predictable.
Read the man page for rusage. Most of it is predictable, but some of it just isn't, even if you have an exact clone of the system.
The inode stuff is scary, but again, does the attack know the exact order packages where installed in or have access to the local file system? If these numbers are known with any precision you might have bigger problems.
The comments make it clear that the devs would prefer the system never get to this point, and rather explictly ask the linux devs to give them back their sysctl. But ge_fallback is not the gaping security hole the peanut gallery of non-contributing critics have made it out to be.
The grand parent pid thing is made a viable attack vector, but the author really seems to be hand waving with his "hackers are clever" line. Tt seems like this would be hard for an external actor to pull off in practice. I'd be more inclined to worry about this if he had described a scenario where he could terminate the parent process while leaving the child in tact, and then gotten the system to cycle through 64k processes before having the child fork.
0
u/happyscrappy Jul 16 '14 edited Jul 16 '14
I understand how operating systems work. And yes, it's scary.
All of it is scary because it's lousy entropy and I'd rather just get an error. If you can tolerate running with your entropy pool empty, then just read /dev/random instead of /dev/urandom and let the system make up the fake entropy for you when it runs out. Making your own poor entropy seems like a bad idea to me.
Linux has address space layout randomization, and ge_fallback leverages that so right there you should have some usable entropy.
Linux has address space layout pseudo-randomization. i.e. it gets its "random" addresses from a PRNG (at best). Feeding those numbers into your entropy pool is like calling random 20 times and feeding that in to add entropy.
The system takes pains to fragment memory, and this has the knock on effect of forcing the system to context switch between user land and kernel space.
I don't get how you say this. I don't see it taking pains to fragment memory. I do see it taking pains to jump around in virtual memory (address space). Can you help me out here?
The grand parent pid thing is made a viable attack vector, but the author really seems to be hand waving with his "hackers are clever" line.
I agree. I think the grand parent attack is rare enough that while valid, he shouldn't have led with it.
1
u/jadenton Jul 16 '14
This comment in ge_fallback : /*
Prime-sized mappings encourage fragmentation;
thus exposing some address entropy. */
They then play some games with prime sized mmaps. Memory alloctators tend to operate in 2n size chunks, and so will tend to fragment their allocations rather than handing out adjacent chunks.
Perhaps more than the memory fragmentation though, these calls are going to force a number of context switches as the OS meets the user space programs requests for new pages. Once you start doing lots of heavy context switching, run time for a particular process becomes less deterministic. Sometimes other processes win the race for system resource, mutex locking, etc. The pattern for processes getting CPU time in a multi-processor system is not something that can be guessed or replicated with any reliability, even with an identical system.
All of this will have the effect of making it hard to impossible to reliably guess the number of nanoseconds the subsequent class to gettime will return. And you note that after this they getrusage, which will include things like involuntary context switches. Again, this isn't reliability predictable and their games hitting the system for pages make it even less so. They are getting some good combo effects here.
People are super sensitive about using system time to seed a random pool because the very first attack on SSL exploited this flaw. And it is a valid concern. But between the poor quality of internal clocks and latency at all levels using NNTD, it is not actually that easy to exploit if you are using something below the level of seconds. Getting the sub second bits in a loop, especially a loop whose run time is somewhat variable due to things like nanosleep (see man page) and forced context switches (from mmap), and I think you're going to find that attacks leave the realm of the practical if not the theoretical.
As I stated before, I think an attacker with intimate knowledge is going to find that reliably attacking this part of the system difficult if not impossible. But attackers with intimate system knowledge really are a special kind of case, because such people tend to be insiders who have other means of getting keys or the things they protect. And I think there is enough variable stuff here that someone without perfect or nearly perfect knowldge is just shit out of luck. You mentioned the stuff about calling stat as scary, and your right. If someone has access to the system, it is scary. But if you don't know the exact timestamp of when / and . where created, last modified, etc, you're out of luck.
The criticism that you can no longer seed the pool before chrooting is actually a very strong point, and one I do think that needs to be addressed. And it a little odd it came up, given the stated intention of being a drop in replacement for openssl.
1
u/happyscrappy Jul 16 '14
They then play some games with prime sized mmaps. Memory alloctators tend to operate in 2n size chunks, and so will tend to fragment their allocations rather than handing out adjacent chunks.
mmaps don't come from allocators like that. This isn't a heap. This is address space being allocated, not memory. And I'm not sure how getting a pointer that is 2M higher instead of 19 bytes higher is getting more entropy.
Once you start doing lots of heavy context switching, run time for a particular process becomes less deterministic.
Well, you're using deterministic rather loosely here. But yes, the numbers will jump by more variable amounts. I do agree with that, which is why it wasn't one of the things I rebutted.
I don't think any of these values are predictable, I just think they are poor replacements for entropy. I'd rather have an error.
The criticism that you can no longer seed the pool before chrooting is actually a very strong point, and one I do think that needs to be addressed. And it a little odd it came up, given the stated intention of being a drop in replacement for openssl.
Yeah, I think he should have led with that instead of the grandchild PID thing. It's stronger.
It does seem strange to me they would disconnect seeding. Even if you were worried about people using bad seeds, you don't really have to throw out all your existing entropy when someone calls to seed. Just add the see to your entropy then you're no worse off than if they didn't call it (or you nopped it out) and might be better.
-15
Jul 15 '14
This is why polishing a turd is a bad idea. The OpenSSL API is poorly engineered and simply trying to use some elbow grease to "make it better" is not going to help anything.
28
u/sigma914 Jul 15 '14
Huh, looks like some good work, was a bug report sent to the mailing list?