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#include "libbb.h"
43
44#include <linux/random.h>
45#include <sys/random.h>
46#include <sys/file.h>
47
48#ifndef GRND_INSECURE
49#define GRND_INSECURE 0x0004
50#endif
51
52#define DEFAULT_SEED_DIR "/var/lib/seedrng"
53#define CREDITABLE_SEED_NAME "seed.credit"
54#define NON_CREDITABLE_SEED_NAME "seed.no-credit"
55
56enum {
57 MIN_SEED_LEN = SHA256_OUTSIZE,
58
59
60
61
62 MAX_SEED_LEN = 256,
63};
64
65static size_t determine_optimal_seed_len(void)
66{
67 char poolsize_str[12];
68 unsigned poolsize;
69 int n;
70
71 n = open_read_close("/proc/sys/kernel/random/poolsize", poolsize_str, sizeof(poolsize_str) - 1);
72 if (n < 0) {
73 bb_perror_msg("can't determine pool size, assuming %u bits", MIN_SEED_LEN * 8);
74 return MIN_SEED_LEN;
75 }
76 poolsize_str[n] = '\0';
77 poolsize = (bb_strtou(poolsize_str, NULL, 10) + 7) / 8;
78 return MAX(MIN(poolsize, MAX_SEED_LEN), MIN_SEED_LEN);
79}
80
81static bool read_new_seed(uint8_t *seed, size_t len)
82{
83 bool is_creditable;
84 ssize_t ret;
85
86 ret = getrandom(seed, len, GRND_NONBLOCK);
87 if (ret == (ssize_t)len) {
88 return true;
89 }
90 if (ret < 0 && errno == ENOSYS) {
91 int fd = xopen("/dev/random", O_RDONLY);
92 struct pollfd random_fd;
93 random_fd.fd = fd;
94 random_fd.events = POLLIN;
95 is_creditable = poll(&random_fd, 1, 0) == 1;
96
97
98 close(fd);
99 } else {
100 if (getrandom(seed, len, GRND_INSECURE) == (ssize_t)len)
101 return false;
102 is_creditable = false;
103 }
104
105
106
107
108
109 errno = 0;
110 if (open_read_close("/dev/urandom", seed, len) != (ssize_t)len)
111 bb_perror_msg_and_die("can't read '%s'", "/dev/urandom");
112 return is_creditable;
113}
114
115static void seed_from_file_if_exists(const char *filename, int dfd, bool credit, sha256_ctx_t *hash)
116{
117 struct {
118 int entropy_count;
119 int buf_size;
120 uint8_t buf[MAX_SEED_LEN];
121 } req;
122 ssize_t seed_len;
123
124 seed_len = open_read_close(filename, req.buf, sizeof(req.buf));
125 if (seed_len < 0) {
126 if (errno != ENOENT)
127 bb_perror_msg_and_die("can't read '%s'", filename);
128 return;
129 }
130 xunlink(filename);
131 if (seed_len != 0) {
132 int fd;
133
134
135
136
137
138
139
140
141
142
143 if (fsync(dfd) != 0)
144 bb_simple_perror_msg_and_die("I/O error");
145
146
147
148 sha256_hash(hash, req.buf, seed_len);
149
150 req.buf_size = seed_len;
151 seed_len *= 8;
152 req.entropy_count = credit ? seed_len : 0;
153 printf("Seeding %u bits %s crediting\n",
154 (unsigned)seed_len, credit ? "and" : "without");
155 fd = xopen("/dev/urandom", O_RDONLY);
156 xioctl(fd, RNDADDENTROPY, &req);
157 if (ENABLE_FEATURE_CLEAN_UP)
158 close(fd);
159 }
160}
161
162int seedrng_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
163int seedrng_main(int argc UNUSED_PARAM, char **argv)
164{
165 const char *seed_dir;
166 int fd, dfd;
167 int i;
168 unsigned opts;
169 uint8_t new_seed[MAX_SEED_LEN];
170 size_t new_seed_len;
171 bool new_seed_creditable;
172 struct timespec timestamp[2];
173 sha256_ctx_t hash;
174
175 enum {
176 OPT_n = (1 << 0),
177 OPT_d = (1 << 1),
178 };
179#if ENABLE_LONG_OPTS
180 static const char longopts[] ALIGN1 =
181 "skip-credit\0" No_argument "n"
182 "seed-dir\0" Required_argument "d"
183 ;
184#endif
185
186 seed_dir = DEFAULT_SEED_DIR;
187 opts = getopt32long(argv, "nd:", longopts, &seed_dir);
188 umask(0077);
189 if (getuid() != 0)
190 bb_simple_error_msg_and_die(bb_msg_you_must_be_root);
191
192 if (mkdir(seed_dir, 0700) < 0 && errno != EEXIST)
193 bb_perror_msg_and_die("can't create directory '%s'", seed_dir);
194 dfd = xopen(seed_dir, O_DIRECTORY | O_RDONLY);
195 xfchdir(dfd);
196
197
198
199
200
201
202
203
204
205 flock(dfd, LOCK_EX);
206
207 sha256_begin(&hash);
208
209
210 clock_gettime(CLOCK_REALTIME, ×tamp[0]);
211 clock_gettime(CLOCK_BOOTTIME, ×tamp[1]);
212 sha256_hash(&hash, timestamp, sizeof(timestamp));
213
214 for (i = 0; i <= 1; i++) {
215 seed_from_file_if_exists(
216 i == 0 ? NON_CREDITABLE_SEED_NAME : CREDITABLE_SEED_NAME,
217 dfd,
218 (opts ^ OPT_n) & i,
219 &hash);
220 }
221
222 new_seed_len = determine_optimal_seed_len();
223 new_seed_creditable = read_new_seed(new_seed, new_seed_len);
224
225
226 sha256_hash(&hash, new_seed, new_seed_len);
227 sha256_end(&hash, new_seed + new_seed_len - SHA256_OUTSIZE);
228
229 printf("Saving %u bits of %screditable seed for next boot\n",
230 (unsigned)new_seed_len * 8, new_seed_creditable ? "" : "non-");
231 fd = xopen3(NON_CREDITABLE_SEED_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0400);
232 xwrite(fd, new_seed, new_seed_len);
233 if (new_seed_creditable) {
234
235
236
237 if (fsync(fd) < 0)
238 bb_perror_msg_and_die("can't write '%s'", NON_CREDITABLE_SEED_NAME);
239 xrename(NON_CREDITABLE_SEED_NAME, CREDITABLE_SEED_NAME);
240 }
241 return EXIT_SUCCESS;
242}
243