3 ms·
"A UUID is 128 bit long, and consists of a 60-bit time value, a 16-bit sequence number and a 48-bit node identifier. The time value is taken from the s
by jasonwatkinspdx 14y ago
"A UUID is 128 bit long, and consists of a 60-bit time
value, a 16-bit sequence number and a 48-bit node
identifier.
The time value is taken from the system clock, and is
monotonically incrementing. However, since it is possible
to set the system clock backward, a sequence number is
added. The sequence number is incremented each time the
UUID generator is started. The combination guarantees that
identifiers created on the same machine are unique with a
high degree of probability."
Looking at the code, the node id is derived from the MAC, and the sequence number state is stored in /tmp or your home dir.
In an era of vm's that are frozen and copied around between hosts, this seems like a spectacularly bad implementation. It's way to easy for this code to end up running from the same start state in such a situation, and as soon as that happens you'll have tons of exactly duplicated UUID sequences. I would hate to have to debug the mess that would produce. And that's aside from possible security issues.
Please do not use this gem. SecureRandom.uuid has a sane (and standard) implementation.
- mikeash 14y agoNot that people necessarily do this, but if you duplicate a VM without changing the copy's MAC address then you're setting yourself up for all sorts of problems. It's not all that unreasonable to assume that MAC addresses are unique. After all, that's their whole point.
- jasonwatkinspdx 14y agoI looked further into the code and it's actually uglier than that. Since ruby can't get at the MAC directly, instead they call the ruby global rand function (which is a fast and weak psuedorandom generator on many platforms) 256 times, concatenate the results and sha1 it to create a fake MAC, which is then stuffed into the state file. If a state file exists, it is preferred to creating a new one. So really the entire initial sequence state is in the state file and system clock. That's way to likely to overlap IMHO, particularly on vm images that may not update the system clock immediately on spawning an image, before the app comes up. Your point about always using a new MAC when you thaw or duplicate a vm image is well taken. I'm convinced that when code has cryptographic implications, people should not roll their own ad hoc weird solutions. Nearly every platform has a way of accessing high quality entropy. Version 4 random UUID's have been standardized for a while now. I don't understand the logic of using some "I invented this" implementation.
- mikeash 14y agoGood lord. Even if they used true randomness to generate their fake MAC, you'd still expect a collision after doing this only about 16 million times. What were they thinking?