20 ms·
Really Fixing Getrandom()
- Nican 7y agoThis is a continuation of this discussion "Fixing Getrandom()" (https://news.ycombinator.com/item?id=21114524 https://news.ycombinator.com/item?id=21114524). I love this story, as a complete unrelated kernel commit that reduced the amount of file I/O, caused the kernel to not generate enough entropy at boot, and in consequence, made the system unbootable.
- ChrisSD 7y agoUsing CPU jitter is a clever solution. I hope it stands up to scrutiny. > but mixing entropy from the hardware with other sources is considered to be safe by most, even if the hardware generator is somehow suspect. To be clear, it's a property of bitwise xor that it preserves entropy. If you xor a random bit with 1 you still have a random bit.
- devit 7y agoAssuming they are independent random variables, since e.g. X xor (not X) is constant.
- dooglius 7y agoWell, that's not entirely true, even if two random bits are independent, xor-ing them only gives you one random bit. What actually happens in Linux is that each new input of random bits gets pushed through a cryptographic hash, before being xor'd.
- ChrisSD 7y agoFrom replies it seems my two sentences weren't as clear as I thought. I'll try to refine it: A property of bitwise xoring random variable `x` with a constant (or otherwise non-random but independent) value is that the entropy of `x` is preserved in the result.
- WAHa_06x36 7y agoHowever, xoring two arbitrary random variables is NOT guaranteed to preserve entropy. Preserving entropy only happens under certain specific conditions.
- deleted 7y ago[deleted]
- ChrisSD 7y agoUnder what circumstances would xoring `x` and `y` not produce a result with entropy at least as great as `x`?
- WAHa_06x36 7y agoIn the very obvious cases where x == y, or x == not y, or x == y + 1, or many other correlations.
- ChrisSD 7y agoOnly if y is known to be dependent on x. If y is random but just so happens to be `not x` or `== x` then that won't make the result any less random. 0 is as random a number as 529890873740477 is.
- WAHa_06x36 7y agoSingle numbers don't really have entropy at all, so that is not something it even makes sense to talk about.
- uasm 7y ago> "Using CPU jitter is a clever solution. I hope it stands up to scrutiny." How safe is it to use CPU jitter in a virtual machine?
- jfindley 7y agoSafe as in safe from host interference? Not at all. But if your VM is running on a malicious host, you have bigger problems honestly.
- stefan_ 7y agoThis is kind of the ongoing joke here, yeah? Like HN users will gladly spend hours discussing the merits of various random number generation schemes, why that Intel HWRNG is probably backdoored and we should all be very afraid, all the while running their micro-services on the cloud where their entropy is literally spoon-fed from the hypervisor. SMC call into code-you-can't-see and back comes pure entropy (trust us)!
- pjc50 7y agoYes, although there's two kinds of VM safety people might care about. By using a VM you accept that the bare metal owner can look into it if they want to and you have to trust them not to. However, that doesn't mean accepting attacks from other customers - which was what all the Spectre/Meltdown risk was primarily about.
- tptacek 7y agoCPU jitter randomness is an old idea, and not especially well thought of.
- Diederich 7y agoIf you have a moment, can you expand on that or provide a link? Especially for the last part. Thanks. This is well outside of my wheelhouse, so please pardon a naive inquiry.
- toast0 7y agoThese timing measurements by nature are going to have a small range of possible values --- the size of the range is going to likely be system specific, and for some systems, there may be only a few possible outcomes.
- clarry 7y agoAnd that's enough. One bit is enough, less than one bit of randomness is enough if you can just repeat the process enough times.
- toast0 7y agoHow are we to know how much entropy the process provides our system? The patch in the article counts one bit per timer interrupt, but there may be much less than that. If the tsc clock and the timer clock are linked, and the system is totally blocked on entropy, it seems plausible that the tsc reads will be predictable/repeatable beyond maybe the first value.
- clarry 7y agoIf you've ever tried to measure cycle-accurate timings for algorithms, you might've noticed that even relatively simple and short instruction streams have variance. Whether tsc clock and timer clock are linked or not is completely irrelevant, since the timer clock is only used to 1) credit entropy 2) possibly introduce additional jitter. The value of TSC when the timer interrupt runs is not used as entropy at all so even if that were 100% predictable, it doesn't matter. It only matters that you do have jitter (between calls to schedule() and random_get_entropy()), and I have a hard time imagining a modern system with a high-res TSC where calling schedule() with active timers in a loop (with branches) would not have any jitter at all. You probably have way more entropy than one bit per jiffy; I think the counter is very conservative. Notice that while entropy is credited only once per tick, actual TSC values are mixed into the entropy pool on every iteration of the loop that calls schedule().
- cabalamat 7y agoWhen I want a randomness source, I use the output from `ps auxw`, `df`, and `date`.
- shakna 7y agoIgnoring that those may not be great sources for cryptographic randomness, how does one do something like that when the userland isn't established yet because the system is still booting? That's why a working getrandom is so important.
- neilsimp1 7y agoDoes anybody who is familiar with the BSDs know how those OSes solve this kind of problem? (I don't know enough to say if this even could be a problem with their architecture, just curious).
- throw0101a 7y agoThe boot loaders read entropy off-disk to seed the kernel: * https://www.freebsd.org/cgi/man.cgi?loader.conf(5) https://www.freebsd.org/cgi/man.cgi?loader.conf(5) (see "entropy_cache_load") When the system is brought down cleanly (init 0 or 6) a seed file is saved. Also, when the system is started up, a seed file is created in case there's a crash. OpenBSD's rc(8): # Push the old seed into the kernel, create a future seed and create a seed # file for the boot-loader. random_seed() { dd if=/var/db/host.random of=/dev/random bs=65536 count=1 status=none chmod 600 /var/db/host.random dd if=/dev/random of=/var/db/host.random bs=65536 count=1 status=none dd if=/dev/random of=/etc/random.seed bs=512 count=1 status=none chmod 600 /etc/random.seed } * https://cvsweb.openbsd.org/src/etc/rc?rev=1.537 https://cvsweb.openbsd.org/src/etc/rc?rev=1.537 FreeBSD also has a cron job that saves 4KB of entropy every 11 minutes in a rotating series of files: * https://svnweb.freebsd.org/base/head/libexec/save-entropy/ https://svnweb.freebsd.org/base/head/libexec/save-entropy/ * https://svnweb.freebsd.org/base/head/libexec/rc/rc.d/random https://svnweb.freebsd.org/base/head/libexec/rc/rc.d/random
- beefhash 7y agoThe installer also leaves a random.seed file so that there's a direct chain from the very first boot.
- throw0101a 7y agoSo the main remaining risk is things like (cloud) images where many people/machines use the same seed. This can be mitigated by stirring in RDRAND, unique CPU information (serial number?), high-res date/time, and other possibly unique information (MAC addresses?).
- lathiat 7y agoI had a great bug in libvirt fixed where two machines being deployed in an automated fashion generated the same MAC address for the virbr0. And at least one other person had reported the same issue previously on a mailing list. Details from the bug I filed (https://bugs.launchpad.net/ubuntu/+source/libvirt/+bug/1710341 https://bugs.launchpad.net/ubuntu/+source/libvirt/+bug/17103...): src/util/virrandom.c:virRandomOnceInit seeds the random number generator using this formula: unsigned int seed = time(NULL) ^ getpid(); This seems to be a popular method after a quick google but it's easy to see how this can be problematic. The time is only in seconds, and during boot of a relatively identical system these numbers are both likely to be relatively similar across multiple systems which is quite likely in cloud-like environments. Secondly, by using bitwise OR only a small difference is created and if the 1st or 2nd MSB of the pid or time are 0 then it would be easy to have colliding values. Though problematic from basic logic, I also tested this with a small test program trying 67,921 unique combinations of time() and pid() which produced only 5,693 random seeds using PID range 6799-6810 and time() range 1502484340 to 1502489999. During the actual incident in question, the 4 systems were all booted within 1-2 seconds of each other. We can see from dmesg that the two systems that generated the same MAC did in fact boot during the same second and the other two did not
- pjc50 7y agoReminds me of mjg's recent attack on Bird: https://mjg59.dreamwidth.org/53258.html https://mjg59.dreamwidth.org/53258.html > Digging through the code revealed 8 bytes worth of key fairly quickly, but the other 8 bytes were less obvious. I finally figured out that 4 more bytes were the value of another Bluetooth variable which could be simply read out by a client. The final 4 bytes were more confusing, because all the evidence made no sense. It looked like it came from passing the scooter serial number to atoi(), which converts an ASCII representation of a number to an integer. But this seemed wrong, because atoi() stops at the first non-numeric value and the scooter serial numbers all started with a letter[2]. It turned out that I was overthinking it and for the vast majority of scooters in the fleet, this section of the key was always "0".
- schoen 7y agoA classic PRNG seeding problem from 1996: https://people.eecs.berkeley.edu/~daw/papers/ddj-netscape.html https://people.eecs.berkeley.edu/~daw/papers/ddj-netscape.ht... Netscape was using a related method for seeing the PRNG to the one you mention, which was still a small enough space of possible seed values that when the resulting values were using in cryptography, an attacker could brute-force the seed.
- enriquto 7y agoSpeaking of which,... does anybody here know a portable, easy to use, and high quality, deterministic random number generator? It is a requirement that you must get the exactly the same sequence of pseudo-random numbers on any architecture (depending on a user-settable seed). I use a hand-crafted (two-line) linear congruential generator, but always feel a bit uneasy about that.
- throw0101a 7y agoAny stream cipher? If you use the same symmetric key and nonce, the output bit stream should be the same every time. This is how OpenBSD's arc4random(3) started out: using the output of RC4 and stirring the entropy pool regularly to ensure forward security. (They've since switched to ChaCha20.) * https://man.openbsd.org/arc4random.3 https://man.openbsd.org/arc4random.3 Remove the stirring and you've got deterministic output. Security comes from the entropy of the initial conditions.
- enriquto 7y agoYes, something like that would be great! But does it come packaged nicely? So that I can compile it out of the box on windows, linux, macos, android?
- jedisct1 7y agoThe `randombytes_deterministic()` function from libsodium or libhydrogen does exactly what the name suggests.
- SAI_Peregrinus 7y agoMonocypher is a decent fit here. Just run its ChaCha20 with (a hash of) your seed as the key. Larger libraries (libsodium, NaCl, etc) are decent too, but bigger.
- tptacek 7y agoAlternative opinion: Don't use Monocypher. Much less safe than libsodium.
- vanni 7y agoSomewhat relevant: CPU Time Jitter Based Non-Physical True Random Number Generator by Stephan Müller http://www.chronox.de/jent/doc/CPU-Jitter-NPTRNG.html#toc-Appendix-B http://www.chronox.de/jent/doc/CPU-Jitter-NPTRNG.html#toc-Ap... https://pdfs.semanticscholar.org/af73/17c970fd416646b2e46659c9624108be4fcc.pdf https://pdfs.semanticscholar.org/af73/17c970fd416646b2e46659...
- mangix 7y agoFWIW this is used in OpenWrt.
- devit 7y agoWell, the proper solution for the future would be for the kernel to save and restore a random seed using UEFI variables, a similar firmware mechanism or free space in a boot sector or swap partition. But of course this doesn't solve the problem of newer kernels on existing installations with no such stored seed and that don't pass command line argument telling the kernel where to store the seed.
- ars 7y agoOn those machines the old way works fine, but not all machine even have a place to store this data, on top of that people clone VM instances, and you don't want the same number each time. Read the previous https://news.ycombinator.com/item?id=21114524 https://news.ycombinator.com/item?id=21114524 story for more on the problem space and why what you wrote is not considered enough.
- mlyle 7y ago> Well, the proper solution for the future would be for the kernel to save and restore a random seed using UEFI variables, a similar firmware mechanism or free space in a boot sector or swap partition. This needs to be invalidated before numbers dependent on the seed are used, else you risk attacks where you get someone to generate the same random numbers on consecutive boots. In turn, if you crash during bootup at the wrong time, you're left without a seed.
- devit 7y agoYes, you need to update the seed immediately after reading it and before using it. There is no problem with a crash assuming that the seed write mechanism is crash-safe (which is achievable with block storage, but indeed I'm not sure whether firmware is properly designed and implemented).
- magashna 7y agoI didn't know that a jiffy was the formal unit for kernel timing. That tickled me quite a bit.
- denton-scratch 7y agoAnalogously, a "mickey" is the (informal) unit for timing mouse movements.
- marios 7y agoI am by not means qualified to judge on the quality of the patch, but I'm glad Linus is moving away from adding flags to getrandom() (resulting in subtly different behaviour than the Open/FreeBSD implementations). Now if only Linux fixed the "/dev/random provides higher quality random numbers than /dev/urandom and blocks when the entropy pool is empty" nonsense, that would be great.
- dev_dull 7y agoThey should do exactly what’s been done in Mac/bsd variants and make both of them the same CSPRNG via fortuna[1]. Then the only thing the system ever need to worry about is getting a few bytes of random data seeded and you can read securely all day long. 1. https://en.m.wikipedia.org/wiki/Fortuna_(PRNG) https://en.m.wikipedia.org/wiki/Fortuna_(PRNG)
- the_why_of_y 7y agoThey already use the same CSPRNG algorithm; the only difference is the entropy accounting. See 2nd point here: https://www.2uo.de/myths-about-urandom/ https://www.2uo.de/myths-about-urandom/
- thenewnewguy 7y agoWhat's the issue there? As far as I can tell that's intended behavior - for most randomness /dev/urandom _never_ blocks, and for the paranoid /dev/random blocks when there's no entropy.
- feanaro 7y agoThe issue is that entropy isn't actually expended by generating a large amount of random data from a CSPRNG. Therefore, the only time there is a need for blocking is prior to the initial seeding of the generator. Later on, entropy counting is just hoodoo and cargo culting.
- toast0 7y agoClassically, on Linux you had /dev/urandom which always gave you something, even if the system hadn't achieved a seeded state, and /dev/random which would block in case the system wasn't seeded and also in case random had been used too much without more input into the entropy. Neither one of those is usable for key generation. urandom may give you repeatable data if the system hasn't seeded itself yet. random may block on accounting that most experts find problematic. getrandom() finally provided the right semantics of only blocking when the system isn't seeded, and without a filesystem/device node dependency; however changes in startup software, including filesystem improvements have resulted in shortened boot sequences and less entropy gathered. In some cases, systems were blocked waiting on entropy before any (non human interfaced) entropy sources were enabled.
- tytso 7y agoMy response to Linus: I'm OK with this as a starting point. If a jitter entropy system allow us to get pass this logjam, let's do it. At least for the x86 architecture, it will be security through obscurity. And if the alternative is potentially failing where the adversary can attack the CRNG, it's my preference. It's certainly better than nothing, in that in that very worst case, "security through obscurity" is better than "don't block, because blocking is always worse than an guessable value being returned through getrandom(0)". And so if these are the only two options, I'm certainly going to choose the former. That being said, I'm still very worried that people with deep access to the implementation details of a CPU might be able to reverse engineer what a jitter entropy scheme produces. For that reason, I'd very much like to see someone do an analysis of how well these jitter schemes work on an open, simple architecture such as an RISC-V iplementation (you know, the ones that were so simplistic and didn't have any speculation so they weren't vulnerable to Specture/Meltdown). If jitter approaches turn out not to work that well on RISC-V, perhaps that will be a goad for future RISC-V chips to include the crypto extension to their ISA. In the long term (not in time for the 5.4 merge window), I'm convinced that we should be trying as many ways of getting entropy as possible. If we're using UEFI, we should be trying to get it from UEFI's secure random number generator; if there is a TPM, we should be trying to get random numbers from the RPM, and mix them in, and so on. After all, the reason why lived with getrandom(0) blocking for five years was because for the vast majority of x86 platforms, it simply wasn't problem in practice. We need to get back to that place where in practice, we've harvested as much uncertainty from hardware as possible, so most folks are comfortable that attacking the CRNG is no longer the simplest way to crack system security. (The above is a lightly edited almagamation of https://lore.kernel.org/r/20190930033706.GD4994@mit.edu https://lore.kernel.org/r/20190930033706.GD4994@mit.edu and https://lore.kernel.org/r/20190930131639.GF4994@mit.edu https://lore.kernel.org/r/20190930131639.GF4994@mit.edu)
- pjc50 7y ago> "don't block, because blocking is always worse than an guessable value being returned through getrandom(0)". It seems that this fight has continued for so long because both possible answers are valid here, depending on the exact use case of the system as a whole(+)? Have we got an interface which lets you properly specify the paranoia vs. best-effort choice yet? (+) e.g. the SSH server running on your lightbulb may accept that it's not exposed to the internet and therefore not subject to targeted attack, but it's really inconvenient to sit in the dark, so the security-availability tradeoff is worth it. > most folks are comfortable that attacking the CRNG is no longer the simplest way to crack system security. Was it ever? It seems that phishing the admins has always been the easiest way.
- derefr 7y agoSo, the big outlier in terms of TSC predictability would be virtual machines, no? Which VM hypervisors pass through RDTSC to the host (and then, presumably, add an offset before returning it, such that the VM can start at TSC=0); and which ones just have a “virtual” TSC unrelated to the host’s? Do instances on any production IaaS compute cloud have predictable virtual TSCs?
- aidenn0 7y agoHas anyone considered fixing systemd to be able to load the on-disk entropy store sooner? This would be only an issue when booting machines for the first time (with the exception of diskless machines) if that is fixed.
- tzs 7y agoAn issue with that is how to deal with things that want random numbers before the disk is writable. If you read the seed while the disk is read-only, and then something uses random numbers based on that seed, and then something causes the boot to abort before the on-disk seed is updated, then the next boot attempt will reuse the same on-disk seed. Depending on what random numbers are used for between seeding and writing the updated seed, that could range from harmless to disastrous. With some care in how things are ordered during system startup, it could probably be made safe.
- aidenn0 7y agoAs long as you order it so the network isn't up before the disk is writable, then no persistent effects could happen from reusing numbers, right? If you can't send out a network packet and can't write to the disk, what other recordable events are there? An attacker writing down numbers observed on the console?
- viraptor 7y agoThat assumes your disk is not provided over network. Which may or may not be true.
- fjp 7y agoHow the hell do people get to a skill level where they can do this stuff.
- tenebrisalietum 7y agoDo what? Fix getrandom() or take advantage of getrandom() vulnerabilities?
- kzrdude 7y agohm, working on linux for every day since 1991?
- codedokode 7y agoMaybe random values should not be necessary to boot the system? I remember reading somewhere that systemd needs cryptographically random numbers to start up and I don't understand why.