aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--util-linux/mkswap.c59
1 files changed, 55 insertions, 4 deletions
diff --git a/util-linux/mkswap.c b/util-linux/mkswap.c
index 167b9ee03..c4e30fd92 100644
--- a/util-linux/mkswap.c
+++ b/util-linux/mkswap.c
@@ -53,14 +53,65 @@ static void mkswap_selinux_setcontext(int fd, const char *path)
53#if ENABLE_DESKTOP 53#if ENABLE_DESKTOP
54static void mkswap_generate_uuid(uint8_t *buf) 54static void mkswap_generate_uuid(uint8_t *buf)
55{ 55{
56 pid_t pid;
56 unsigned i; 57 unsigned i;
57 char uuid_string[32]; 58 char uuid_string[32];
58 59
59 /* rand() is guaranteed to generate at least [0, 2^15) range, 60 /* http://www.ietf.org/rfc/rfc4122.txt
61 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
62 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 * | time_low |
64 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65 * | time_mid | time_hi_and_version |
66 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 * |clk_seq__and_variant | node (0-1) |
68 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69 * | node (2-5) |
70 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 * IOW, uuid has this layout:
72 * uint32_t time_low (big endian)
73 * uint16_t time_mid (big endian)
74 * uint16_t time_hi_and_version (big endian)
75 * version is a 4-bit field:
76 * 1 Time-based version
77 * 2 DCE Security version, with embedded POSIX UIDs
78 * 3 Name-based version (MD5)
79 * 4 Randomly generated version
80 * 5 Name-based version (SHA-1)
81 * uint16_t clk_seq_and_variant (big endian)
82 * variant is a 3-bit field:
83 * 0xx Reserved, NCS backward compatibility
84 * 10x The variant specified in rfc4122
85 * 110 Reserved, Microsoft backward compatibility
86 * 111 Reserved for future definition
87 * uint8_t node[6]
88 *
89 * For version 4, these bits are set/cleared:
90 * time_hi_and_version & 0x0fff | 0x4000
91 * clk_seq_and_variant & 0x3fff | 0x8000
92 */
93
94 i = open("/dev/urandom", O_RDONLY);
95 if (i >= 0) {
96 read(i, buf, 16);
97 close(i);
98 }
99 /* Paranoia. /dev/urandom may be missing.
100 * rand() is guaranteed to generate at least [0, 2^15) range,
60 * but lowest bits in some libc are not so "random". */ 101 * but lowest bits in some libc are not so "random". */
61 srand((unsigned)monotonic_us() + getpid()); 102 srand(monotonic_us());
62 for (i = 0; i < 16; i++) 103 pid = getpid();
63 buf[i] = rand() >> 5; 104 while (1) {
105 for (i = 0; i < 16; i++)
106 buf[i] ^= rand() >> 5;
107 if (pid == 0)
108 break;
109 srand(pid);
110 pid = 0;
111 }
112
113 buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40; /* time_hi_and_version */
114 buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80; /* clk_seq_and_variant */
64 115
65 bin2hex(uuid_string, (void*) buf, 16); 116 bin2hex(uuid_string, (void*) buf, 16);
66 /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */ 117 /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */