1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include "id.h"
#include "musl-libgen-c.h"
#include "random-seed.h"
#include "util.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
static const char *HEX_CHARS = "0123456789abcdef";
/** Encode hex.
*
* \param dest where to store the encoded data (must have at least size*2+1 bytes)
* \param src the data to encode
* \param size the number of bytes to encode
*/
static void mem2hex(char *dest, const void *src, size_t size) {
size_t i;
for (i = 0; i < size; i++) {
unsigned char c = *((const unsigned char *)src + i);
dest[2*i] = HEX_CHARS[c >> 4];
dest[2*i+1] = HEX_CHARS[c & 0xf];
}
dest[2*i] = '\0';
}
static bool get_rand_pool(unsigned char *buf) {
size_t rand_bytes_gotten = 0;
do {
long this_rand_bytes_gotten = random_get(buf + rand_bytes_gotten, RAND_POOL_SIZE - rand_bytes_gotten, 0);
if (this_rand_bytes_gotten == -1) {
switch (errno) {
case EAGAIN: continue;
case EINTR: return false;
default:
perror("getrandom");
exit(1);
}
} else {
rand_bytes_gotten += (size_t)this_rand_bytes_gotten;
}
} while (rand_bytes_gotten < RAND_POOL_SIZE);
return true;
}
static void hash_str(const unsigned char salt[static SALT_LEN], char hash_out[static HASH_STR_LEN], const void *buf, size_t buflen) {
unsigned char digest[HASH_LEN];
hash(salt, digest, buf, buflen);
mem2hex(hash_out, digest, HASH_LEN);
}
/**
* Save entropy to disk.
*
* \param seed_path the seed file path
* \param random_buf the random buffer. if NULL, get our own entropy.
* \return true means saved successfully, false means received EINTR
*/
bool save(const char *seed_path, const unsigned char *random_ptr) {
assert(seed_path);
bool rv = false;
unsigned char random_buf[RAND_POOL_SIZE];
if (!random_ptr) {
if (!get_rand_pool(random_buf))
return false;
random_ptr = random_buf;
}
const char *machine_id = get_machine_id(&machine_id);
if (!machine_id)
return false;
char machine_id_hash[HASH_STR_LEN];
hash_str(random_ptr, machine_id_hash, machine_id, strlen(machine_id));
struct random_seed rs = {0};
char *seed_path_tmp = strdup(seed_path);
const char *seed_dir, *seed_name;
char *seed_path_last_slash = strrchr(seed_path_tmp, '/');
if (seed_path_last_slash) {
*seed_path_last_slash = '\0';
seed_dir = seed_path_tmp;
seed_name = seed_path_last_slash + 1;
} else {
free(seed_path_tmp);
seed_path_tmp = NULL;
seed_dir = ".";
seed_name = seed_path;
}
char *seed_name_new = NULL;
if (asprintf(&seed_name_new, ".%s.new", seed_name) == -1) {
fputs("out of memory\n", stderr);
goto out;
}
int seed_dir_fd = open(seed_dir, O_RDONLY | O_DIRECTORY);
if (seed_dir_fd == -1) {
perror("error opening seed directory");
goto out;
}
fsid_t fs_id;
bool have_fs_id = get_fs_id(&fs_id, seed_dir_fd);
#ifdef HAVE_FS_UUID
(void)have_fs_id;
#else
if (!have_fs_id) {
fputs("error getting fs id\n", stderr);
return false;
}
#endif
char fs_id_hash[HASH_STR_LEN];
hash_str(random_ptr, fs_id_hash, &fs_id, sizeof(fs_id));
#ifdef HAVE_FS_UUID
ensure_rs_device(&rs, seed_dir_fd);
char fs_uuid_hash[HASH_STR_LEN];
{
const char *fs_uuid = get_fs_uuid(&rs);
if (!fs_uuid) {
fputs("error getting fs uuid\n", stderr);
return false;
}
hash_str(random_ptr, fs_uuid_hash, fs_uuid, strlen(fs_uuid));
}
#endif
#ifdef HAVE_LIBUDEV
bool have_drive_id = false;
char drive_id_hash[HASH_STR_LEN];
{
const char *drive_id = get_drive_id(&rs);
if (drive_id) {
have_drive_id = true;
hash_str(random_ptr, drive_id_hash, drive_id, strlen(drive_id));
}
}
#endif
{
int seed_fd = openat(seed_dir_fd, seed_name_new, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (seed_fd == -1) {
perror("error opening new seed file");
goto err;
}
rs.file = fdopen(seed_fd, "w");
if (!rs.file) {
perror("error converting seed file fd to stream");
goto err;
}
}
if (fwrite(random_ptr, 1, RAND_POOL_SIZE, rs.file) != RAND_POOL_SIZE
|| fputs(MAGIC, rs.file) == EOF
|| fputs("machine-id ", rs.file) == EOF
|| fputs(machine_id_hash, rs.file) == EOF
#ifdef HAVE_FS_UUID
|| fputs("\nfs-uuid ", rs.file) == EOF
|| fputs(fs_uuid_hash, rs.file) == EOF
|| fputs("\nfs-id ", rs.file) == EOF
|| fputs(fs_id_hash, rs.file) == EOF
#else
|| (have_fs_id ? fputs("\nfs-id ", rs.file) == EOF : 0)
|| (have_fs_id ? fputs(fs_id_hash, rs.file) == EOF : 0)
#endif
#ifdef HAVE_LIBUDEV
|| (have_drive_id ? fputs("\ndrive-id ", rs.file) == EOF : 0)
|| (have_drive_id ? fputs(drive_id_hash, rs.file) == EOF : 0)
#endif
|| fputs("\ndone\n", rs.file) == EOF) {
fputs("error writing new seed file\n", stderr);
goto err;
}
if (fflush(rs.file) == EOF) {
perror("error flushing new seed file");
goto err;
}
if (fsync(fileno(rs.file)) == -1) {
perror("error syncing new seed file");
goto err;
}
if (renameat(seed_dir_fd, seed_name_new, seed_dir_fd, seed_name) == -1) {
perror("error installing new seed file");
goto err;
}
if (fclose(rs.file) == EOF) {
perror("error closing new seed file");
goto err;
}
if (fsync(seed_dir_fd) == -1) {
perror("error syncing seed directory");
goto out;
}
rv = true;
goto out;
err:
if (rs.file) {
fclose(rs.file);
if (unlinkat(seed_dir_fd, seed_name_new, 0) == -1) {
perror("error removing temporary seed file");
rv = false;
}
}
out:
if (seed_dir_fd != -1) {
if (close(seed_dir_fd) == -1) {
perror("error closing seed directory");
rv = false;
}
}
free(seed_path_tmp);
free(seed_name_new);
return rv;
}
|