r/linux • u/atoponce • Mar 01 '22
Linux 5.18 will likely have a blocking /dev/urandom such that calls to the RNG will *always* return secure bytes after initial seeding, which takes no more than 1s after boot. After decades of confusion, all random interfaces will finally be identical.
https://git.kernel.org/pub/scm/linux/kernel/git/crng/random.git/commit/?id=2ad310f93ec3d7062bdb73f06743aa56879a0a28149
u/nintendiator2 Mar 01 '22
How is that going to work when installing the kernel in older machines? Generation of secure random numbers is slow, and sometimes you want something that only needs to be reasonably fast, not perfectly secure (security is a process, not a state).
213
u/atoponce Mar 01 '22
This is mentioned in the commit:
The number of machines without a cycle counter or some other implementation of
random_get_entropy()in 2022, which can also run a mainline kernel, and at the same time have a both broken and out of date userspace that relies on/dev/urandomnever blocking at boot is thought to be exceedingly low. And to be clear: those museum pieces without cycle counters will continue to run Linux just fine, and even/dev/urandomwill be operable just like before; the RNG just needs to be seeded first through the usual means, which should already be the case now.83
u/nintendiator2 Mar 01 '22
; the RNG just needs to be seeded first through the usual means, which should already be the case now
Aha! The key point that interested me, and had missed it, thanks!
48
u/edman007 Mar 01 '22
Also, they are saying without cycle counters, cycle counters were introduced with the pentium (i586). i486 is the oldest CPU supported by glibc, which means i486 is the only x86 processor that actually has this problem unless you go with the some distro using musl or something.
15
u/Topinio Mar 01 '22
Um, sorta not necessarily.
It's reliably there in P6-class Intel chips, but the rest of the x86's from the mid to late 90s (which lived on in embedded systems for much longer) are a bit of a grey area.
22
-5
u/dlarge6510 Mar 02 '22
The complete obliviousness in that commit statement suggests they live in a dream world most of the time.
→ More replies (2)41
u/Pelera Mar 01 '22
Recent kernels have had significant improvements in the performance and initial seeding of the RNG.
You can still ask for something insecure using
getrandom()if truly need be.43
u/atoponce Mar 01 '22
You can still ask for something insecure using
getrandom()if truly need be.
getrandom(GRND_INSECURE)to be precise, which is what systemd does for its hash tables. Otherwise, callinggetrandom()before the RNG is seeded will block.7
u/DeeBoFour20 Mar 01 '22
Well I don't think this affects the speed per say. It will simply block if it's out of entropy. Older machines should work more or less the same. Where I could see this being an issue is on embedded devices with limited entropy.
Also, if you want a fast non-secure PRNG, the program should really be using a user space PRNG rather than reading from /dev/urandom.
71
u/Barafu Mar 01 '22
Don't forget that there are some virtual machines, like Hyper-V, that provide absolutely no hardware entropy to the system. Will they even boot with this change?
101
16
u/Natanael_L Mar 01 '22
Virtual machines can be seeded from the host if drivers supporting it are installed on the guest and if the VM can inject it accordingly
16
6
3
Mar 02 '22
Will they even boot with this change?
I'd assume that the devs testing this change are doing so in virtual machines.
1
26
u/liquidpele Mar 01 '22
By blocking, do you mean a tiny wait for actual entropy that the kernel will provide with a 100% guarantee and not actually blocking? Because I can't tell you how many times a fucking VM froze up because god damn java wanted to use /dev/random and rngd hadn't been set up yet.
6
u/atoponce Mar 01 '22
By blocking, do you mean a tiny wait for actual entropy that the kernel will provide with a 100% guarantee and not actually blocking?
Yes. You don't random bits until the RNG is sufficiently seeded. This will most likely happen via the jitter entropy kernel thread, but can also happen via RDSEED, or in the case of VMs via a virtualized HWRNG.
1
u/liquidpele Mar 01 '22
but can also happen via RDSEED, or in the case of VMs via a virtualized HWRNG.
These are sources, they don't fill /dev/random automatically.
16
u/atoponce Mar 01 '22
/dev/randomdoesn't get "filled". It's not a bucket. It's a userspace device that makes a call to the RNG, which is ChaCha20, and asks for a specific number of bytes out of the stream cipher.For this to be secure, the RNG needs to be keyed with a 256 bit key. So long as the RNG is keyed with 256 bits of information theoretic secure data, we can use fast key erasure to pull out as many bytes as we want until the Heath Death of the Universe, and never worry about producing predictable data.
That's where RDSEED and a virtualized H RNG come in. They need the RNG with at least 256 bits of raw entropy and we use
/dev/{,u}randomandgetrandom()to pull out as many bytes as we need.This is exactly what the Linux RNG is doing now.
1
u/liquidpele Mar 01 '22
That's where RDSEED and a virtualized H RNG come in. They need the RNG with at least 256 bits of raw entropy and we use /dev/{,u}random and getrandom() to pull out as many bytes as we need.
But they don't do that automatically. If it's not done, and I call /dev/random things block right?
I feel like I'm getting the run around here because everyone keeps saying "but entropy sources exist so it's fine"... they've existing for decades, and I still had issues just a couple years ago. I'm also not saying that it's horrible for /dev/random... but people used urandom very intently for a very very long time, and changing this in a way that makes them not trust it anymore is going to cause everyone to use something else that isn't going to get them paged at 4 AM.
10
u/atoponce Mar 01 '22 edited Mar 02 '22
But they don't do that automatically. If it's not done, and I call /dev/random things block right?
The blocking pool was removed in 5.6. Calls to
/dev/randomno longer block.Edit: correction
→ More replies (4)1
u/liquidpele Mar 01 '22
Dude who first replied to me said:
Not exactly, there is a possibility of blocking
If it can't block anymore than that's great!
9
u/atoponce Mar 01 '22
As mentioned in this commit, the only block is within the first second or so while initial seeding takes place, which applies to
getrandom(),/dev/random, and/dev/urandom. It will block before then, but never after.32
u/axonxorz Mar 01 '22
Not exactly, there is a possibility of blocking, but it boils down to:
A) CPU lacking hardware cycle counter, hope you're not running VMs on pre-Pentium chips :)
B) Having an extremely old distribution (would most likely rule out having this kernel installed)
C) Having the distribution's boot sequence not properly seed the RNG (no rngd, as you said), which is not an issue for all major and likely most minor distros, for a while now.
In your example, it shouldn't matter if rngd isn't started as long as the cycle counter is available, the kernel will pre-seed with data from that (I would assume, among some other things), supposedly within 1 second of kernel start.
2
u/liquidpele Mar 01 '22
In the past, rngd would read from whatever forms of entropy you had and fill the random buffer. Having hardware support doesn't mean shit unless you actually 1. configure your VM to recognize it (which isn't easy when dealing with legacy VM systems), and 2. have something actually use the hardware.
TBH this seems like a horribly stupid idea. If it ever blocks... EVER... under any circumstances.... then you can shove this "feature". That's was the whole fuckin' point of urandom and why people used it, because there are plenty of scenarios where it's far more important to never EVER EVER block than being 100% super random for things that aren't even crypto related.
13
u/axonxorz Mar 01 '22
I think they may have thought about these things?
The number of machines without a cycle counter or some other implementation of random_get_entropy() in 2022, which can also run a mainline kernel, and at the same time have a both broken and out of date userspace that relies on /dev/urandom never blocking at boot is thought to be exceedingly low. And to be clear: those museum pieces without cycle counters will continue to run Linux just fine, and even /dev/urandom will be operable just like before; the RNG just needs to be seeded first through the usual means, which should already be the case now.
5
u/liquidpele Mar 01 '22
the RNG just needs to be seeded first through the usual means
So far I have not seen anything saying this is automatic though, which is the whole issue I have. I know there are many sources of entropy. I know you can manually fetch them and use it. I don't want to be paged at 3 AM because a service froze up because an ops intern didn't configure the VM infra correctly when I explicitly chose to use urandom for that purpose. I simply do not see the benefit of taking away choice here unless it's absolutely guaranteed that urandom won't block because that's the whole fuckin' reason people use it.
9
u/za419 Mar 01 '22
It is automatic, though. The modern mainline kernel fills the entropy pool within 1 second or so of boot using the cycle counter on the processor.
If you're concerned that you're going to get paged at 3am because a box rebooted and a service didn't work for one second longer than expected, perhaps you should update your infrastructure a little bit.
5
u/BattlePope Mar 01 '22 edited Mar 01 '22
That wasn't necessarily clear. I share /u/liqudpele's concern.
urandomwas the non-blocking version, and I think we just wanted assurance that it still is.20
u/za419 Mar 01 '22
The problem you both have is that you're conceptualizing
/dev/randomas 'the blocking one' - it hasn't been for around two years.Both of them pull data from a CSPRNG that gets seeded with 256 bits of entropy and then proceeds to churn out cryptographically secure randomness for a long while, until the kernel has collected another 256 bits and rekeys it.
So the only difference right now is the behavior before those first 256 bits - urandom gives you subpar data, random blocks. This will eliminate the urandom behavior.
However, 256 bits of entropy appear very quickly after the kernel starts. By the time you're going through system init (be that upstart, SysV, systemd, et cetera), you're going to have so much jitter that the only processor the kernel won't have 256 bits of randomness from is the i486. And even so, that'll seed fairly quickly from interrupts.
On most systems, the blocking window will close within about a second of grub handing control over to the kernel - after that, urandom will never block again.
→ More replies (5)10
u/BattlePope Mar 01 '22
You're right - I haven't been keeping up with modern kernel internals, I'm just a crusty greybeard ;)
Thanks for the details!
3
u/Natanael_L Mar 01 '22
It will only block until seeded by the entropy collection system, then it will NOT block again while the system is running
2
u/Natanael_L Mar 01 '22
They will only block until the entropy pool is seeded after boot, after that neither of them will ever block.
2
u/LordRybec Mar 02 '22
Yeah, one of those applications is security testing, where we need to run some test against some billions of random numbers that don't need cryptographic security but have to be better than PRNGs produce. Or where the application under test is designed to get its randomness from a file-like kernel interface, and /dev/random blocking, even for fractions of a second, once in a while, could add hours or sometimes days to a test run.
So even in things that are crypto related, a non-blocking source of moderate quality randomness is pretty important.
46
u/hwc Mar 01 '22
I still don't understand why CPU makers never designed an extremely chaotic circuit that produces truely random bits. Just amplify thermal noise.
Edit, I now see https://en.wikipedia.org/wiki/RDRAND is exactly that.
33
u/atoponce Mar 01 '22
Also,
random.chas been designed since version 1.3.30 to collect randomness via:
system interrupts
keyboard interrupts
mouse interrupts
block device interrupts
Even though the computer itself might be deterministic, the precise timing events of the user are not.
22
u/suid Mar 01 '22
Welcome to the world of tiny IoT devices. No keyboard, no mouse, no block device except a small embedded flash with very predictable timings, very little network traffic. So only a tiny trickle of entropy.
But then, generally these devices don't consume a lot of randoms either.
11
u/atoponce Mar 01 '22
Hello jitter entropy. ;-)
10
u/suid Mar 01 '22
Yes! That works great in modern CPUs with out-of-order instruction execution pipelines (most modern ARM and Intel CPUs).
Not so much on older MIPS and ARM and other simple embedded CPUs with strict in-order pipelines and predictable instruction cycle counts.
3
u/bik1230 Mar 01 '22
I think it should be doable by sampling the difference between a crystal oscillator and an oscillator circuit. For example, the watchdog on atmegas is typically driven by an internal oscillator even when the system is driven by an external crystal.
Of course, if the manufacturer haven't done an analysis of just how much randomness that'll create, it wouldn't be wise to do this in production.
2
16
Mar 01 '22
Turn a GPIO pin into a tiny antenna and sample EM noise for your entropy :P
So long as you don't get unlucky and pick a tiny slice of spectrum with a transmitter on it, it should do the job just fine.
11
u/neon_overload Mar 02 '22
Does that not open up the security flaw that something else could emit electromagnetic radiation in a pattern designed to invoke a particular pattern in your random generator?
3
u/neon_overload Mar 02 '22
You don't even have to go very exotic to get a device with much fewer interfaces it could draw randomness from. Something running on a VM/emulator, or a raspberry pi zero, for example
4
u/hwc Mar 01 '22
I was aware of that. But a remote system might have very few interrupts.
6
u/Matir Mar 01 '22
Something serving network traffic will have plenty of interrupts. Both network cards and disk I/O result in interrupts.
4
u/kombiwombi Mar 01 '22
serving network traffic will have plenty of interrupts
You're going to make the RNG entropy accounting depend on externally-influenced events? You do you.
1
u/ICanBeAnyone Mar 02 '22
Well, if an attacker controls the timing of all my incoming network traffic down to the nanosecond I'm basically ready to give up, yes.
2
u/kombiwombi Mar 02 '22
So you're happy for your end-to-end security to be subvertable by the upstream ethernet switch's integrity? That's not how end-to-end encryption is meant to work.
2
1
u/LordRybec Mar 02 '22
Now days, you can get chips that providing randomness from quantum randomness. I don't think most consumer chips do it that way though.
0
Mar 01 '22
[deleted]
90
u/wonkynonce Mar 01 '22
Yup. And it's not just malice https://arstechnica.com/gadgets/2019/10/how-a-months-old-amd-microcode-bug-destroyed-my-weekend/
Unfortunately, unpatched Ryzen 3000 says "yes" to the CPUID 01H call, sets the carry bit indicating it has successfully created the most artisanal, organic high-quality random number possible... and gives you a 0xFFFFFFFF for the "random" number, every single time.
25
u/vytah Mar 01 '22
I'd link the relevant xkcd, but we all know which one it is.
43
10
2
11
u/chrisoboe Mar 01 '22
conspiracy theory time:
if i would have been given the task to make the random deterministic, so three-letter-agencies can abuse this to break encryption and i wouldn't be allowed to talk about this, i would implement the deterministic random in the most fucked-up way by always returning the same result, so nobody will use it anymore and i still fulfilled my task and didn't talk about it.
11
u/Atsch Mar 01 '22
I think the idea of not trusting a CPU to generate random numbers, but then also trusting it to do all of the encryption and run all of the other code is kind of silly. Especially when you then completely trust an external hwrng, call into the firmware thousands of times, trust any device with full DMA access and so on. Sure, it's not a bad idea to augment it with other sources but the idea of defending against the very CPU you are running on is nonsensical and not based in any kind of real threat model.
6
u/kombiwombi Mar 01 '22
That's a fair enough concern. This issue is visibility.
All these things you describe are visible by inspection. For example altering the output of the encryption instructions will cause a failure of decryption (probably on another computer).
But failures of the RNG can be subtle. And RNGs are very difficult to test. So it's more than possible to design a subverted RNG which will sidestep testing.
Since RNG output is used as key material, subversion can be devastating to platform security.
I'd make two criticisms of the current RNG design:
(1) The output of /dev/*random is trusted. There's no sampling of the output into the tests for randomness and halting of the kernel if required. That's probably the biggest difference in practice between commercial crypto and military crypto.
(2) The hardware instructions should be used, as they provide excellent protection from timing attacks. That is, the returned value should have a final statement of
return(xor(software_rng, rdrand))This gets more value from the hardware RNG than mixing those values into the entropy pool. If you are concerned about the quality of the hardware RNG then don't add it to the entropy accounting.
2
u/Atsch Mar 02 '22 edited Mar 02 '22
I don't really agree with this. The way Linux uses RDRAND already does not really strongly depend on the security of the instruction, albeit mostly for performance reasons. For any useful attack you have to predict not just the output of the instruction, but the blake2 hash of the output, plus any other little entropy gathered, plus the position of the bytes read, plus the cpu the task is running on.
The only real way that's feasible is if you could somehow say, have a fairly blatant backdoor that lets you send a magic packet to the ME/PSP to make RDRAND return a known value, in early boot, while it's generating long lived keys. But in that case, why not just have a backdoor that lets you take over the ME instead. I don't think it's a very realistic scenario.
2
u/atoponce Mar 02 '22
Feeding the RNG with RDRAND does have a noticable performance impact on reading from
/dev/urandom. On my system with RDRAND enabled:% pv -S -s 1G /dev/urandom > /dev/null 1.00GiB 0:00:04 [ 233MiB/s] [================================>] 100%If I set
nordrandas a kernel argument and reboot, the perfomance significantly improves:% pv -S -s 1G /dev/urandom > /dev/null 1.00GiB 0:00:02 [ 404MiB/s] [================================>] 100%Oddly enough, with RDRAND enabled on my AMD Ryzen 5, performance is absolutely non-existent:
(amd)% pv -S -s 1G /dev/urandom > /dev/null 1.00GiB 0:00:28 [36.3MiB/s] [================================>] 100%Again, setting
nordrandand rebooting:(amd)% pv -S -s 1G /dev/urandom > /dev/null 1.00GiB 0:00:02 [ 357MiB/s] [================================>] 100%On AMD, 64-bit read actually decomposes into two 32-bit ones, while Intel supports long 64-bit reads. But a whole order of magnitude? Woah. I'm not a tinfoil hat guy, but if you need the performance, you may want to consider disabling RDRAND, especially on AMD.
2
u/Atsch Mar 02 '22
What kernel version is this? That is pretty relevant as some major performance improvements landed recently.
I also recall there are different code paths where rdrand is used, one where it is used in re-seeding (sensible) and another where it is always mixed with the output stream (doesn't really make sense). Presumably this is doing the latter.
4
u/atoponce Mar 02 '22
5.16. Yeah, in 5.17, the SHA-1 entropy extractor will be replaced with BLAKE2s, which should improve things here specifically. However, AMD's performance is a hardware bottleneck.
3
u/LordRybec Mar 02 '22
This isn't a good strategy. It would be faster to just return rdrand, and it wouldn't be significantly less random. There's a reason it doesn't do that already: Hitting rdrand that frequently is too slow.
24
u/chrisoboe Mar 01 '22
These are completely different things.
Encryption and besides rdrand every single other instruction needs to be deterministic. You can easily test and verify cpu to trust it.
A rng should be as undeterministic as possible, so you can't test if a random number generator is malicious or not. You can never trust a non open source rng.
trust any device with full DMA acces
we don't. Thats why modern cpus have a iommu, so devices don't get full dma access anymore. Thats why the fsf and open source enthusiasts don't like proprietary firmware.
Attacks of random number generators are a real thread, that provenly happened in the past. I don't know a single attack where cpu instructions were modified to be malicious.
6
u/LordRybec Mar 02 '22 edited Mar 02 '22
PRNG researcher here. There are ways to test RNGs (including ones designed to be truly random), but nothing is perfectly comprehensive. You can only prove that the output of an RNG is unpredictable up to the end of the stream you are testing. Maybe the next bit will start over some cycle. And testing depends heavily on the quality of the tests. It's basically a pattern matching problem, and we still don't know all of the possible ways in which there may be patterns. Worse yet though, the last PRNG I tested comprehensively, to compare it with known high quality cryptographic PRNGs, took several months to complete testing on fast hardware. (It did pretty well!) An on chip hardware RNG would test faster (because it wouldn't have to deal with so many memory accesses), but it would still take forever.
That said, I wouldn't be too worried about an on-chip RNG being intentionally deterministic as a back door. Good PRNGs that are fast are really hard to design. If you don't hide at least half the state (ie., don't use it to generate the output), it's nearly impossible to avoid detectable patterns that make it easily hackable. If you do hide half of the state or more, it's very hard to make it so that it has a backdoor but still passes quality tests. Basically, anyone doing this successfully would have to be many steps ahead of the state of the art in pseudo-random number generation, at great expense. I just don't see it, when it would cost hundreds or thousands of times more than just putting in some real RNG that uses quantum randomness as its source.
Of course, maybe I'm in on it, and I'm just saying this to put people off of our scent...
As far as CPU maliciousness goes, most modern processors do have AES encryption instructions built in. For the most part, the CPU has no clue what you are trying to do, so even trying to target some narrow application with malicious instructions would be stupid and fruitless. But, if the chip has some instructions intended to be used within a very specific, narrow application, like encryption, those instructions could be altered to be malicious, without affecting the rest of the machine. In this case, hardware encryption could be affected, but software encryption couldn't, and there are many AES implementations using each method. This is enough to rule out malicious AES instructions though. When a computer encrypts something, it can't know how it will be decrypted. If your chip was doing AES differently, to sneak in a backdoor, it wouldn't decrypt correctly for someone using software encryption. Chip makers have to be honest about their on-chip AES implementations, because if they weren't, all of the software implementations would reveal the deception almost immediately.
So in either case, it's very unlikely there is any deception going on. In one case, it would be incredibly difficult and expensive, and in the other, the deception would be trivially detectable.
Again though, I'm some random person on Reddit claiming to be an expert, and even if that is true, I could just be trying to throw you off. Your security is ultimately your responsibility. Do your research and use wisdom in deciding who to trust.
1
u/shigawire Mar 02 '22 edited Mar 02 '22
From memory of reading the original white paper describing the RDRAND implementation, the underlying hardware implementation is known to have predictable components.
In the firmware implementation they apparently"wash" these not very random numbers into "better" random numbers to return from RDRAND,
Despite the marketing claims from Intel, I would not trust it as a cryptographically secure PRNG
4
0
21
Mar 01 '22
So what's the point of having a second interface that is identical? Shouldn't /dev/urandom be removed, and if people actually need it, symlinked to /dev/random?
45
u/atoponce Mar 01 '22
At this point, it's probably historical. All BSDs and macOS also ship both
/dev/randomand/dev/urandomdevices as so much userspace software makes calls to both. I don't doubt there will be work to eventually remove/dev/urandom, but it's likely a very low priority.9
u/bss03 Mar 01 '22
You can always just tweak the udev rules if you'd like to operate without a /dev/urandom, even today. I doubt distros will do so for decades though. :)
→ More replies (14)15
u/Johannes_K_Rexx Mar 01 '22
Shouldn't /dev/urandom be removed
No because "we do not break user space."
13
Mar 01 '22
"Unless we break it in a subtle way by removing a non-blocking guarantee because we figure waiting a second is no big deal and we think programmers are idiots who don't know which random device to read from and need to be saved from themselves"
7
u/not_a_novel_account Mar 02 '22
Programmers aren't idiots (well, they are, but that's not the reason for this), but old software written with old assumptions frequently doesn't get updated.
6
Mar 01 '22
So, do I still need to install haveged?
6
u/atoponce Mar 01 '22 edited Mar 02 '22
No. You haven't needed that since version 5.6 when the blocking pool was removed.
Edit: typo, correction
4
u/Rogurzz Mar 01 '22
Can someone explain what this means in layman terms?
7
u/atoponce Mar 01 '22
Whenever you or an application requests random data from the kernel, it's guaranteed to be cryptographically secure.
2
u/Solderking Mar 02 '22
Isn't it more accurate to say that it's cryptographically secure per current standards, but that future exploits may change this?
At one point, a C64's RNG was considered truly random.
4
u/atoponce Mar 02 '22
Isn't it more accurate to say that it's cryptographically secure per current standards, but that future exploits may change this?
Indeed.
4
4
u/SanityInAnarchy Mar 01 '22
Huh. How does this relate to myths about /dev/urandom? Sounds like Linux is moving towards the FreeBSD model?
10
u/atoponce Mar 01 '22 edited Mar 02 '22
It's a further evolution. When that article was written,
/dev/randomblocked on reads. Since then, 5.6 has been released which removed the blocking pool, allowing/dev/randomto behave more like/dev/urandom.However, before this change, really early calls to
/dev/urandommight happen before the kernel is sufficiently seeded and as such return predictable bytes. This change forces a block to/dev/urandomuntil the RNG has been seeded.Edit: corrections
7
Mar 01 '22
I have no clue what this means, could someone please explain how it will affect the life of a regular linux user such as myself?
11
u/atoponce Mar 01 '22
Guaranteed secure random numbers all the time.
6
Mar 01 '22
So whenever the computer generates a random number it's harder for a malicious third-party to know what that number is?
13
u/atoponce Mar 01 '22
Precisely. The bits coming out of the kernel RNG should be indistinguishable from true random white noise.
4
3
u/kukisRedditer Mar 01 '22
it won't affect a regular user at all tbh, well it might, but the regular user won't notice it
15
u/were_not_talking_we Mar 01 '22
I consider myself borderline linux power user, and I have no idea wtf this means.
16
u/atoponce Mar 01 '22
It means Linux will finally have consistent interfaces when needing random numbers.
40
u/jimicus Mar 01 '22
There's no such thing as a random number.
Instead, computers produce psuedo-random numbers. Numbers that look random at first glance, but if you happen to be the sort of mathematical whiz who works for a big three letter agency, you might be able to figure out a pattern to them.
Why is this a problem? Well, because random number generators are extensively used in cryptography. And if your random number isn't.... well, your cryptography's probably not as secure as you think it is.
For many years, the workaround to this in Linux was to have two random number generators. One which will block - ie. sit there returning nothing - unless and until it's got something that is at least reasonably randomised. And one that returns numbers straight away, but they might not be as random as you'd like.
Of course, this generates confusion. You have people writing to mailing lists asking why their code that gets random numbers just sits there not doing anything. People writing crypto code that uses the not-terribly-good random number generator. And countless websites, blog articles and mailing list replies saying "Ah, yes, the problem you've got there is....". (Sometimes even the people writing the replies get confused, so you wind up with an argument over who's right and who's wrong!).
Newer computers have much more reliable ways to generate random numbers built right in, and Linux has a few clever tricks up its sleeve. In most cases, it's probably no longer necessary to have two random number generators. But we can't just get rid of the not-so-good one, because that'll break everything that uses it. So instead, simply make them both do the same thing.
There is a risk that someone somewhere has a piece of obscure/antique hardware or broken distro that won't play nicely with this, but the likelihood of them trying to run a modern kernel with that is pretty slim.
9
u/Natanael_L Mar 01 '22
Some adjustments;
There's no such thing as a random number
Correct. There are random sources, not random numbers.
Instead, computers produce psuedo-random numbers.
It's more like that the source is called pseudorandom, because we apply cryptographic algorithms to input data which isn't perfect white noise to produce something looking as random as white noise.
Numbers that look random at first glance, but if you happen to be the sort of mathematical whiz who works for a big three letter agency, you might be able to figure out a pattern to them.
The algorithm currently in use is ChaCha20. It has a significantly higher security margin than even AES, and yet it's faster than AES on many systems. The problem isn't really the algorithm, but making sure that you really did collect enough randomness - if nothing random was collected then it's trivial to just guess what went into the algorithm and then calculate your secrets based on that.
For many years, the workaround to this in Linux was to have two random number generators.
The reason for this was a mix of paranoia and performance reasons. /dev/urandom was essentially always OK if you first checked that the entropy pool was properly seeded. But blocking /dev/random was a thing and stuck around because people didn't fully trust the RNG:s and entropy extraction algorithms.
Both had the same pool, so you'd be getting the same numbers when polling both, except when polling /dev/random it will then mark the current entropy pool contents as "used up" and will wait for it to be reseeded before the next response. As mentioned before, this would only ever be necessary if you really don't trust the RNG and entropy extraction algorithms all that much.
5
0
2
u/graywolf0026 Mar 01 '22
This is purely off topic, yet I find it slightly entertaining that "all random interfaces will finally be identical".
Like you'd expect random to be random, and not uniform.
Ah well.
2
u/atoponce Mar 01 '22
Their behavior will be identical. The data will still be uniform however, otherwise it would be biased and not cryptographically secure.
4
u/graywolf0026 Mar 01 '22
Oh no, I'm aware of that! Which is a fully good thing at all. It's simply the way it's worded gave me reason to pause and go, "But it's SUPPOSED to be random, otherwise having it be identical is no longer random." lol
Basically the way they worded it evoked a strange mental image, yet the reasoning is perfectly sound.
3
2
u/khraibani Mar 01 '22
maybe unrelated but:
What about "rand" , "random" and "rand48" in C ... do they use Linux random or have their own implementation !?
4
u/atoponce Mar 01 '22
They are userspace RNGs that are not cryptographically secure. That's perfectly acceptible if you don't need randomness for security. Otherwise, use
getrandom().
2
u/londons_explorer Mar 01 '22
So if I collect the first 128 bits out of /dev/urandom after boot on every machine in the world, I wouldn't expect to find any matches?
Is there any way for someone to actually collect this from a large number of machines to verify?
I am worried that some machines may be such perfect clones of one-another (ie. Same SoC) that they run exactly identically through initial boot, and the random number generator ends up outputting the exact same values.
3
u/atoponce Mar 01 '22
So if I collect the first 128 bits out of /dev/urandom after boot on every machine in the world, I wouldn't expect to find any matches?
Correct. If the entropy estimation is accurate (and conservative) enough, then every machine after initial seeding will be in 1 of 2256 possible states.
Is there any way for someone to actually collect this from a large number of machines to verify?
You could probably compose a Docker container simply enough that once the RNG is seeded, you grab 128 bits, save to disk, and compare against other containers doing the extact same thing.
I am worried that some machines may be such perfect clones of one-another (ie. Same SoC) that they run exactly identically through initial boot, and the random number generator ends up outputting the exact same values.
The jitter across the CPU, even if built off the same manufacturing line, will vary across all devices due to voltage and temperature fluctuations each individual SoC or "golden image" VM experiences. Imperfections in the chips and transistors will magnify that jitter. Within nanoseconds, every system will be diverging from every other.
3
u/londons_explorer Mar 01 '22
The jitter across the CPU, even if built off the same manufacturing line, will vary across all devices due to voltage and temperature fluctuations
There are plenty of CPU designs where the whole system runs in lockstep to a single clock source with no possibility of jitter.
There are also more complex designs which have accidental or deliberate "sync" points. For example, "all flash memory write transactions always complete on a clock cycle that is a multiple of 64". Such sync points can quickly cause otherwise divergent states to converge - usually before the Linux kernel even starts.
What you're saying might be true, but I'd like to see large scale data collection to verify it. Docker wouldn't work because docker doesn't encapsulate urandom. You'd need physical hardware.
3
u/deskpil0t Mar 02 '22
Gonna have to start modifying my power voltage with a Spotify playlist…. Just to be sure
2
u/swordgeek Mar 02 '22
Why is this a good thing?
/dev/random and /dev/urandom always served different purposes. Now...
4
133
u/Xenomyst_ Mar 01 '22
all random interfaces will finally be identical
they shouldn't stop there and add a few more identical interfaces, just in case
109
u/BCMM Mar 01 '22
To make kernel interfaces as consistent as possible, they should work towards making all character devices identical to /dev/urandom. Personally, I think /dev/tty1 would be a good candidate for the next interface to start reliably returning secure, random data.
83
u/SMF67 Mar 01 '22
That can already be arranged. Just have an unfamiliar user attempt to exit vim on tty1
26
u/GreatBigBagOfNope Mar 01 '22
fjeoskfbfbddpleasehelpmeimtryingtoescapenflspsoxjfmspsjchdjybcsjfhcjsm
16
6
Mar 01 '22
If we can crowdsource this and distribute it on some blockchain then it would be truly perfect.
20
7
4
6
1
4
u/TheOneWhoPunchesFish Mar 01 '22 edited Mar 01 '22
So urandom acts like it always did, but instead of never blocking, it only blocks at boot till enough entropy is collected? Or does it block every time I make a https connection and there isn't enough entropy?
I read the mail and this blog post, but that's all I know about how /dev/*random work.
7
u/Natanael_L Mar 01 '22
Block on system boot until seeded, only. After the system is up and running and the entropy pool is seeded it doesn't block again, on any kernel RNG interface.
1
u/Sutarmekeg Mar 01 '22
ELI5 / ELINeanderthal?
2
u/atoponce Mar 01 '22
When you ask the kernel for random data, it will only return cryptographically secure data.
1
u/meni04 Mar 02 '22
Noob here, no performance overhead?
5
6
u/yawkat Mar 02 '22
No, in fact getting rid of all the legacy logic in the Linux rng in the past few years has made it faster.
1
u/the_wandering_nerd Mar 02 '22
So now what? Do we all need a wall covered with lava lamps to generate random numbers now?
1
6
2
0
u/LordRybec Mar 02 '22
"urandom" literally means "unblocking random". The heck? As someone in security, where urandom is really good for large scale testing (but definitely not deployment) because it doesn't block, this really concerns me. There's a reason there's a difference. If they are all identical, then why have more than one? "finally"? They shouldn't be identical. They have different purposes and different use cases. For large scale testing, where cryptographic randomness isn't needed but PRNGs aren't good enough, urandom is really useful. Sure, random might be much faster than it was in the past, but if I'm doing a test over a hundred billion values, even a little bit slower can add up to a lot of extra time spent.
Well, that's disappointing. I guess "don't break userspace" is going out the window.
5
u/atoponce Mar 02 '22 edited Mar 02 '22
"urandom" literally means "unblocking random".
The "u" stands for "unlimited", but the meaning is the same. When Ty Ts'o designed it back in 1995, the two devices were very different:
/dev/randomblocked and/dev/urandomdidn't.His intent, while genuine was miscalculated. The blocking pool kept an estimation of how much entropy had been collected. When you requested data from the CSPRNG via
/dev/random, it would provide you with data while decrementing the estimation counter. If that counter fell below the keyspace of the CSPRNG, it would stop (block) giving you data.That keyspace was 128 bits when it was based on MD5, then 160 bits when it was replaced with SHA-1. Now it's 256 bits. However, the blocking pool was completely removed from the kernel in version 5.6.
/dev/randomhasn't blocked for a couple of years now.The heck? As someone in security, where urandom is really good for large scale testing (but definitely not deployment) because it doesn't block, this really concerns me.
It shouldn't.
/dev/uranadmis and always has been cryptographically secure.There's a reason there's a difference.
If you're a cryptographer studying information theoretic designs in RNGs, that reason is meaningful. Otherwise, you should be using
/dev/urandom.https://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers/
If they are all identical, then why have more than one? "finally"? They shouldn't be identical. They have different purposes and different use cases. For large scale testing, where cryptographic randomness isn't needed but PRNGs aren't good enough, urandom is really useful.
/dev/urandomis the only one you should be usisg. It's cryptographically secure and always has been. The myths between/dev/randomand/dev/urandomare just that: myths.https://www.2uo.de/myths-about-urandom/
Sure, random might be much faster than it was in the past, but if I'm doing a test over a hundred billion values, even a little bit slower can add up to a lot of extra time spent.
Well, that's disappointing. I guess "don't break userspace" is going out the window.
Userspace isn't broken.
Edit: corrections
5
u/bik1230 Mar 02 '22
Random and urandom have both always been based on a CSPRNG, just that random would subtract from the entropy estimate in a way that doesn't really make sense.
Urandom has always been the recommended source of cryptographically secure random numbers, with the caveat that it'll only be secure after enough entropy has been gathered post boot.
This new change ensures that all programs always get fully secure numbers without blocking, except perhaps 1 second of blocking right after boot.
→ More replies (3)
1
u/edthesmokebeard Mar 02 '22
Seems like there's a market for a cheap USB or PCI RNG. $10 should cover the costs.
1
-29
u/Barafu Mar 01 '22 edited Mar 01 '22
Why??? That is nonsense. /dev/random is secure, /dev/urandom is fast.
Only the behavior when their respective pool runs out of entropy, according to some estimate, differs: /dev/random blocks, while /dev/urandom does not.
..which means: /dev/random is secure, /dev/urandom is fast.
Everyone knows that since kindergarten. Why break things?
39
u/atoponce Mar 01 '22
Both
/dev/randomand/dev/urandompull from the exact same CSPRNG and have since version 1.3.30 in 1995 when it was based on MD5. See https://www.2uo.de/myths-about-urandom/ to clear up those misconceptions.-7
u/Barafu Mar 01 '22
From your link:
Only the behavior when their respective pool runs out of entropy, according to some estimate, differs: /dev/random blocks, while /dev/urandom does not.
Nothing new here. Now they are going to do:
Only the behavior when their respective pool runs out of entropy, according to some estimate, differs: /dev/random blocks, while /dev/urandom blocks.
Sounds sane.
29
u/atoponce Mar 01 '22 edited Mar 02 '22
First, the blocking pool was removed in version 5.6.
/dev/randomno longer blocks.Second, the entropy estimating accounting process determines when to rekey ChaCha20. If the entropy estimate has reached 256 bits and the duration between rekeying is 5 minutes, only then will the RNG be rekeyed.
Third, the blocking now is happening with any call to the RNG only before it's been seeded,
getrandom(),/dev/random, or/dev/urandom. After it's been seeded, which happens very early in boot, calls to the RNG will no longer block.Edit: correction
→ More replies (5)23
-7
u/MrGOCE Mar 01 '22 edited Mar 01 '22
NOW IN ENGLISH PLEASE? XD SERIOUSLY, I DIDN'T GET IT :(
5
u/atoponce Mar 01 '22
When you request random data from the kernel, it's guaranteed to be secure.
-2
0
Mar 01 '22
[deleted]
4
u/atoponce Mar 01 '22 edited Mar 02 '22
They have never been needed. Jitter entropy was introduced to the kernel in the 4.x series, and the blocking pool was completely removed in 5.6. Regardless,
/dev/urandomhas always delivered cryptographically secure random data.So yes, haveged, etc. are unnecessary.
Edit: corrections
0
19
Mar 01 '22
[removed] — view removed comment
32
u/atoponce Mar 01 '22
There will be at most a 1 second block for userspace daemons that need random numbers early in the boot process until the RNG is sufficiently seeded.
1
u/LordRybec Mar 02 '22
"At most"? So if somehow the kernel can't get the RNG seeded within that time, then what? The kernel crashes? It starts providing insecure randomness? It terminates the daemon? I mean, if there's a hard time limit, how does it handle it in cases where it might fail?
3
u/atoponce Mar 02 '22 edited Mar 02 '22
From the commit message:
The one slight drawback is that the Linus Jitter Dance relies on random_get_entropy() being implemented. The first lines of try_to_generate_ entropy() are:
stack.now = random_get_entropy();. if (stack.now == random_get_entropy()). return;
On most platforms, random_get_entropy() is simply aliased to get_cycles(). The number of machines without a cycle counter or some other implementation of random_get_entropy() in 2022, which can also run a mainline kernel, and at the same time have a both broken and out of date userspace that relies on /dev/urandom never blocking at boot is thought to be exceedingly low. And to be clear: those museum pieces without cycle counters will continue to run Linux just fine, and even /dev/urandom will be operable just like before; the RNG just needs to be seeded first through the usual means, which should already be the case now.
Edit: formatting
6
Mar 02 '22
I think it will just wait for the RNG to be seeded and continue as usual then. Seeding the RNG will not take much longer than a second, there's no set time limit afaik.
(Someone correct me if I'm wrong)
→ More replies (5)5
u/bik1230 Mar 02 '22
There's no hard limit, it just is very unlikely that it would ever take more than one second for the entropy pool to be seeded with enough random data.
1
4
Mar 01 '22
[removed] — view removed comment
29
u/atoponce Mar 01 '22
You need random numbers for a lot of reasons. Generating TLS session keys, picking the first TCP sequence number, getting a random address space, and many other reasons.
10
Mar 01 '22 edited Mar 01 '22
[removed] — view removed comment
3
u/BattlePope Mar 01 '22
Seems ok, actually. Also on 5.10.16:
$ cat /proc/sys/kernel/random/{poolsize,entropy_avail} 4096 3453 $ dd if=/dev/random of=/dev/null bs=512k status=progress 1032847360 bytes (1.0 GB, 985 MiB) copied, 18 s, 57.4 MB/s^C 2049+0 records in 2048+0 records out 1073741824 bytes (1.1 GB, 1.0 GiB) copied, 18.7273 s, 57.3 MB/s
188
u/DonSimon13 Mar 01 '22 edited Jul 07 '23
https://join-lemmy.org/