Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 OR MIT |
| 2 | /* |
| 3 | * Copyright (C) 2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. |
| 4 | * |
Jason A. Donenfeld | 31ec481 | 2022-04-20 15:27:29 +0200 | [diff] [blame] | 5 | * SeedRNG is a simple program made for seeding the Linux kernel random number |
| 6 | * generator from seed files. It is is useful in light of the fact that the |
| 7 | * Linux kernel RNG cannot be initialized from shell scripts, and new seeds |
| 8 | * cannot be safely generated from boot time shell scripts either. It should |
| 9 | * be run once at init time and once at shutdown time. It can be run at other |
| 10 | * times on a timer as well. Whenever it is run, it writes existing seed files |
| 11 | * into the RNG pool, and then creates a new seed file. If the RNG is |
| 12 | * initialized at the time of creating a new seed file, then that new seed file |
| 13 | * is marked as "creditable", which means it can be used to initialize the RNG. |
| 14 | * Otherwise, it is marked as "non-creditable", in which case it is still used |
| 15 | * to seed the RNG's pool, but will not initialize the RNG. In order to ensure |
| 16 | * that entropy only ever stays the same or increases from one seed file to the |
| 17 | * next, old seed values are hashed together with new seed values when writing |
| 18 | * new seed files. |
| 19 | * |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 20 | * This is based on code from <https://git.zx2c4.com/seedrng/about/>. |
| 21 | */ |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 22 | //config:config SEEDRNG |
Denys Vlasenko | 54867fe | 2022-05-01 16:44:36 +0200 | [diff] [blame] | 23 | //config: bool "seedrng (1.3 kb)" |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 24 | //config: default y |
| 25 | //config: help |
| 26 | //config: Seed the kernel RNG from seed files, meant to be called |
| 27 | //config: once during startup, once during shutdown, and optionally |
| 28 | //config: at some periodic interval in between. |
| 29 | |
| 30 | //applet:IF_SEEDRNG(APPLET(seedrng, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 31 | |
| 32 | //kbuild:lib-$(CONFIG_SEEDRNG) += seedrng.o |
| 33 | |
| 34 | //usage:#define seedrng_trivial_usage |
Denys Vlasenko | 54867fe | 2022-05-01 16:44:36 +0200 | [diff] [blame] | 35 | //usage: "[-d DIR] [-n]" |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 36 | //usage:#define seedrng_full_usage "\n\n" |
Denys Vlasenko | 137b205 | 2022-04-27 16:53:44 +0200 | [diff] [blame] | 37 | //usage: "Seed the kernel RNG from seed files" |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 38 | //usage: "\n" |
Denys Vlasenko | 54867fe | 2022-05-01 16:44:36 +0200 | [diff] [blame] | 39 | //usage: "\n -d DIR Use seed files in DIR (default: /var/lib/seedrng)" |
| 40 | //usage: "\n -n Do not credit randomness, even if creditable" |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 41 | |
| 42 | #include "libbb.h" |
| 43 | |
| 44 | #include <linux/random.h> |
| 45 | #include <sys/random.h> |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 46 | #include <sys/file.h> |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 47 | |
| 48 | #ifndef GRND_INSECURE |
| 49 | #define GRND_INSECURE 0x0004 /* Apparently some headers don't ship with this yet. */ |
| 50 | #endif |
| 51 | |
Denys Vlasenko | 54867fe | 2022-05-01 16:44:36 +0200 | [diff] [blame] | 52 | #define DEFAULT_SEED_DIR "/var/lib/seedrng" |
| 53 | #define CREDITABLE_SEED_NAME "seed.credit" |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 54 | #define NON_CREDITABLE_SEED_NAME "seed.no-credit" |
| 55 | |
Denys Vlasenko | 4013565 | 2022-04-27 17:20:43 +0200 | [diff] [blame] | 56 | enum { |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 57 | MIN_SEED_LEN = SHA256_OUTSIZE, |
Denys Vlasenko | b5624be | 2022-05-01 16:37:39 +0200 | [diff] [blame] | 58 | /* kernels < 5.18 could return short reads from getrandom() |
| 59 | * if signal is pending and length is > 256. |
| 60 | * Let's limit our reads to 256 bytes. |
| 61 | */ |
| 62 | MAX_SEED_LEN = 256, |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 65 | static size_t determine_optimal_seed_len(void) |
| 66 | { |
Denys Vlasenko | 4013565 | 2022-04-27 17:20:43 +0200 | [diff] [blame] | 67 | char poolsize_str[12]; |
| 68 | unsigned poolsize; |
| 69 | int n; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 70 | |
Denys Vlasenko | 4013565 | 2022-04-27 17:20:43 +0200 | [diff] [blame] | 71 | n = open_read_close("/proc/sys/kernel/random/poolsize", poolsize_str, sizeof(poolsize_str) - 1); |
| 72 | if (n < 0) { |
Denys Vlasenko | c82a0cd | 2022-04-27 17:33:15 +0200 | [diff] [blame] | 73 | bb_perror_msg("can't determine pool size, assuming %u bits", MIN_SEED_LEN * 8); |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 74 | return MIN_SEED_LEN; |
| 75 | } |
Denys Vlasenko | 4013565 | 2022-04-27 17:20:43 +0200 | [diff] [blame] | 76 | poolsize_str[n] = '\0'; |
Denys Vlasenko | 54867fe | 2022-05-01 16:44:36 +0200 | [diff] [blame] | 77 | poolsize = (bb_strtou(poolsize_str, NULL, 10) + 7) / 8; |
Jason A. Donenfeld | 31ec481 | 2022-04-20 15:27:29 +0200 | [diff] [blame] | 78 | return MAX(MIN(poolsize, MAX_SEED_LEN), MIN_SEED_LEN); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Denys Vlasenko | 52f3cf7 | 2022-04-30 15:33:28 +0200 | [diff] [blame] | 81 | static bool read_new_seed(uint8_t *seed, size_t len) |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 82 | { |
Denys Vlasenko | 52f3cf7 | 2022-04-30 15:33:28 +0200 | [diff] [blame] | 83 | bool is_creditable; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 84 | ssize_t ret; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 85 | |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 86 | ret = getrandom(seed, len, GRND_NONBLOCK); |
| 87 | if (ret == (ssize_t)len) { |
Denys Vlasenko | 52f3cf7 | 2022-04-30 15:33:28 +0200 | [diff] [blame] | 88 | return true; |
Denys Vlasenko | 282b61a | 2022-04-30 15:25:55 +0200 | [diff] [blame] | 89 | } |
| 90 | if (ret < 0 && errno == ENOSYS) { |
Denys Vlasenko | 2cbfd01 | 2022-04-30 15:36:54 +0200 | [diff] [blame] | 91 | int fd = xopen("/dev/random", O_RDONLY); |
| 92 | struct pollfd random_fd; |
| 93 | random_fd.fd = fd; |
| 94 | random_fd.events = POLLIN; |
Denys Vlasenko | 52f3cf7 | 2022-04-30 15:33:28 +0200 | [diff] [blame] | 95 | is_creditable = poll(&random_fd, 1, 0) == 1; |
| 96 | //This is racy. is_creditable can be set to true here, but other process |
| 97 | //can consume "good" random data from /dev/urandom before we do it below. |
Denys Vlasenko | 2cbfd01 | 2022-04-30 15:36:54 +0200 | [diff] [blame] | 98 | close(fd); |
Denys Vlasenko | 282b61a | 2022-04-30 15:25:55 +0200 | [diff] [blame] | 99 | } else { |
Denys Vlasenko | 282b61a | 2022-04-30 15:25:55 +0200 | [diff] [blame] | 100 | if (getrandom(seed, len, GRND_INSECURE) == (ssize_t)len) |
Denys Vlasenko | 52f3cf7 | 2022-04-30 15:33:28 +0200 | [diff] [blame] | 101 | return false; |
| 102 | is_creditable = false; |
Denys Vlasenko | 282b61a | 2022-04-30 15:25:55 +0200 | [diff] [blame] | 103 | } |
Denys Vlasenko | 52f3cf7 | 2022-04-30 15:33:28 +0200 | [diff] [blame] | 104 | |
| 105 | /* Either getrandom() is not implemented, or |
| 106 | * getrandom(GRND_INSECURE) did not give us LEN bytes. |
| 107 | * Fallback to reading /dev/urandom. |
| 108 | */ |
Denys Vlasenko | 282b61a | 2022-04-30 15:25:55 +0200 | [diff] [blame] | 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"); |
Denys Vlasenko | 52f3cf7 | 2022-04-30 15:33:28 +0200 | [diff] [blame] | 112 | return is_creditable; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Bernhard Reutner-Fischer | a157c4c | 2022-05-01 17:01:14 +0200 | [diff] [blame] | 115 | static void seed_from_file_if_exists(const char *filename, int dfd, bool credit, sha256_ctx_t *hash) |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 116 | { |
| 117 | struct { |
| 118 | int entropy_count; |
| 119 | int buf_size; |
Bernhard Reutner-Fischer | a157c4c | 2022-05-01 17:01:14 +0200 | [diff] [blame] | 120 | uint8_t buf[MAX_SEED_LEN]; |
Denys Vlasenko | 6da9947 | 2022-04-27 17:09:38 +0200 | [diff] [blame] | 121 | } req; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 122 | ssize_t seed_len; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 123 | |
Bernhard Reutner-Fischer | a157c4c | 2022-05-01 17:01:14 +0200 | [diff] [blame] | 124 | seed_len = open_read_close(filename, req.buf, sizeof(req.buf)); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 125 | if (seed_len < 0) { |
Denys Vlasenko | 0fa16fc | 2022-04-29 18:37:42 +0200 | [diff] [blame] | 126 | if (errno != ENOENT) |
Denys Vlasenko | 267178c | 2022-04-30 15:50:45 +0200 | [diff] [blame] | 127 | bb_perror_msg_and_die("can't read '%s'", filename); |
Denys Vlasenko | 0fa16fc | 2022-04-29 18:37:42 +0200 | [diff] [blame] | 128 | return; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 129 | } |
Denys Vlasenko | 0fa16fc | 2022-04-29 18:37:42 +0200 | [diff] [blame] | 130 | xunlink(filename); |
| 131 | if (seed_len != 0) { |
Bernhard Reutner-Fischer | a157c4c | 2022-05-01 17:01:14 +0200 | [diff] [blame] | 132 | int fd; |
| 133 | |
Denys Vlasenko | 4648754 | 2022-04-30 23:17:58 +0200 | [diff] [blame] | 134 | /* We are going to use this data to seed the RNG: |
| 135 | * we believe it to genuinely containing entropy. |
| 136 | * If this just-unlinked file survives |
Denys Vlasenko | 3bfbcb5 | 2022-05-02 15:03:32 +0200 | [diff] [blame] | 137 | * (if machine crashes before deletion is recorded on disk) |
Denys Vlasenko | 4648754 | 2022-04-30 23:17:58 +0200 | [diff] [blame] | 138 | * and we reuse it after reboot, this assumption |
Denys Vlasenko | 3bfbcb5 | 2022-05-02 15:03:32 +0200 | [diff] [blame] | 139 | * would be violated, and RNG may end up generating |
| 140 | * the same data. fsync the directory |
| 141 | * to make sure file is gone: |
Denys Vlasenko | 4648754 | 2022-04-30 23:17:58 +0200 | [diff] [blame] | 142 | */ |
Denys Vlasenko | 3bfbcb5 | 2022-05-02 15:03:32 +0200 | [diff] [blame] | 143 | if (fsync(dfd) != 0) |
| 144 | bb_simple_perror_msg_and_die("I/O error"); |
Denys Vlasenko | 4648754 | 2022-04-30 23:17:58 +0200 | [diff] [blame] | 145 | |
Denys Vlasenko | 7471658 | 2022-05-01 02:06:20 +0200 | [diff] [blame] | 146 | //Length is not random, and taking its address spills variable to stack |
| 147 | // sha256_hash(hash, &seed_len, sizeof(seed_len)); |
Bernhard Reutner-Fischer | a157c4c | 2022-05-01 17:01:14 +0200 | [diff] [blame] | 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; |
Denys Vlasenko | d49da38 | 2022-04-30 15:45:53 +0200 | [diff] [blame] | 153 | printf("Seeding %u bits %s crediting\n", |
Bernhard Reutner-Fischer | a157c4c | 2022-05-01 17:01:14 +0200 | [diff] [blame] | 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); |
Jason A. Donenfeld | 398bb38 | 2022-04-20 15:34:20 +0200 | [diff] [blame] | 159 | } |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Denys Vlasenko | 54867fe | 2022-05-01 16:44:36 +0200 | [diff] [blame] | 162 | int seedrng_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 163 | int seedrng_main(int argc UNUSED_PARAM, char **argv) |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 164 | { |
Denys Vlasenko | 8456c21 | 2022-04-27 17:53:12 +0200 | [diff] [blame] | 165 | const char *seed_dir; |
Denys Vlasenko | d49da38 | 2022-04-30 15:45:53 +0200 | [diff] [blame] | 166 | int fd, dfd; |
Denys Vlasenko | fb4546c | 2022-05-01 01:58:57 +0200 | [diff] [blame] | 167 | int i; |
| 168 | unsigned opts; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 169 | uint8_t new_seed[MAX_SEED_LEN]; |
| 170 | size_t new_seed_len; |
Denys Vlasenko | fb4546c | 2022-05-01 01:58:57 +0200 | [diff] [blame] | 171 | bool new_seed_creditable; |
Denys Vlasenko | 67fd6be | 2022-05-03 12:48:50 +0200 | [diff] [blame^] | 172 | struct timespec timestamp[2]; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 173 | sha256_ctx_t hash; |
| 174 | |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 175 | enum { |
Denys Vlasenko | fb4546c | 2022-05-01 01:58:57 +0200 | [diff] [blame] | 176 | OPT_n = (1 << 0), /* must be 1 */ |
| 177 | OPT_d = (1 << 1), |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 178 | }; |
| 179 | #if ENABLE_LONG_OPTS |
| 180 | static const char longopts[] ALIGN1 = |
Denys Vlasenko | 0bca489 | 2022-04-30 23:53:28 +0200 | [diff] [blame] | 181 | "skip-credit\0" No_argument "n" |
Denys Vlasenko | fb4546c | 2022-05-01 01:58:57 +0200 | [diff] [blame] | 182 | "seed-dir\0" Required_argument "d" |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 183 | ; |
| 184 | #endif |
| 185 | |
Denys Vlasenko | 4013565 | 2022-04-27 17:20:43 +0200 | [diff] [blame] | 186 | seed_dir = DEFAULT_SEED_DIR; |
Denys Vlasenko | fb4546c | 2022-05-01 01:58:57 +0200 | [diff] [blame] | 187 | opts = getopt32long(argv, "nd:", longopts, &seed_dir); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 188 | umask(0077); |
Denys Vlasenko | 8456c21 | 2022-04-27 17:53:12 +0200 | [diff] [blame] | 189 | if (getuid() != 0) |
Jason A. Donenfeld | ce9a345 | 2022-04-20 15:36:22 +0200 | [diff] [blame] | 190 | bb_simple_error_msg_and_die(bb_msg_you_must_be_root); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 191 | |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 192 | if (mkdir(seed_dir, 0700) < 0 && errno != EEXIST) |
Denys Vlasenko | 267178c | 2022-04-30 15:50:45 +0200 | [diff] [blame] | 193 | bb_perror_msg_and_die("can't create directory '%s'", seed_dir); |
Denys Vlasenko | d49da38 | 2022-04-30 15:45:53 +0200 | [diff] [blame] | 194 | dfd = xopen(seed_dir, O_DIRECTORY | O_RDONLY); |
Denys Vlasenko | 8456c21 | 2022-04-27 17:53:12 +0200 | [diff] [blame] | 195 | xfchdir(dfd); |
Denys Vlasenko | d5bd2e5 | 2022-05-01 01:50:44 +0200 | [diff] [blame] | 196 | /* Concurrent runs of this tool might feed the same data to RNG twice. |
| 197 | * Avoid concurrent runs by taking a blocking lock on the directory. |
| 198 | * Not checking for errors. Looking at manpage, |
| 199 | * ENOLCK "The kernel ran out of memory for allocating lock records" |
Denys Vlasenko | 67fd6be | 2022-05-03 12:48:50 +0200 | [diff] [blame^] | 200 | * seems to be the only one which is possible - and if that happens, |
Denys Vlasenko | d5bd2e5 | 2022-05-01 01:50:44 +0200 | [diff] [blame] | 201 | * machine is OOMing (much worse problem than inability to lock...). |
| 202 | * Also, typically configured Linux machines do not fail GFP_KERNEL |
| 203 | * allocations (they trigger memory reclaim instead). |
| 204 | */ |
Denys Vlasenko | 67fd6be | 2022-05-03 12:48:50 +0200 | [diff] [blame^] | 205 | flock(dfd, LOCK_EX); /* blocks while another instance runs */ |
Jason A. Donenfeld | 3c60711 | 2022-04-20 15:31:01 +0200 | [diff] [blame] | 206 | |
Jason A. Donenfeld | 31ec481 | 2022-04-20 15:27:29 +0200 | [diff] [blame] | 207 | sha256_begin(&hash); |
Denys Vlasenko | 67fd6be | 2022-05-03 12:48:50 +0200 | [diff] [blame^] | 208 | //Hashing in a constant string doesn't add any entropy |
| 209 | // sha256_hash(&hash, "SeedRNG v1 Old+New Prefix", 25); |
| 210 | clock_gettime(CLOCK_REALTIME, ×tamp[0]); |
| 211 | clock_gettime(CLOCK_BOOTTIME, ×tamp[1]); |
| 212 | sha256_hash(&hash, timestamp, sizeof(timestamp)); |
Jason A. Donenfeld | 31ec481 | 2022-04-20 15:27:29 +0200 | [diff] [blame] | 213 | |
Denys Vlasenko | fb4546c | 2022-05-01 01:58:57 +0200 | [diff] [blame] | 214 | for (i = 0; i <= 1; i++) { |
Denys Vlasenko | 3bfbcb5 | 2022-05-02 15:03:32 +0200 | [diff] [blame] | 215 | seed_from_file_if_exists( |
| 216 | i == 0 ? NON_CREDITABLE_SEED_NAME : CREDITABLE_SEED_NAME, |
| 217 | dfd, |
| 218 | /*credit?*/ (opts ^ OPT_n) & i, /* 0, then 1 unless -n */ |
| 219 | &hash); |
Jason A. Donenfeld | f9ea8ba | 2022-04-21 12:37:32 +0200 | [diff] [blame] | 220 | } |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 221 | |
| 222 | new_seed_len = determine_optimal_seed_len(); |
Denys Vlasenko | 52f3cf7 | 2022-04-30 15:33:28 +0200 | [diff] [blame] | 223 | new_seed_creditable = read_new_seed(new_seed, new_seed_len); |
Denys Vlasenko | 7471658 | 2022-05-01 02:06:20 +0200 | [diff] [blame] | 224 | //Length is not random, and taking its address spills variable to stack |
| 225 | // sha256_hash(&hash, &new_seed_len, sizeof(new_seed_len)); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 226 | sha256_hash(&hash, new_seed, new_seed_len); |
| 227 | sha256_end(&hash, new_seed + new_seed_len - SHA256_OUTSIZE); |
| 228 | |
Denys Vlasenko | 52f3cf7 | 2022-04-30 15:33:28 +0200 | [diff] [blame] | 229 | printf("Saving %u bits of %screditable seed for next boot\n", |
Denys Vlasenko | 3bfbcb5 | 2022-05-02 15:03:32 +0200 | [diff] [blame] | 230 | (unsigned)new_seed_len * 8, new_seed_creditable ? "" : "non-"); |
Denys Vlasenko | d49da38 | 2022-04-30 15:45:53 +0200 | [diff] [blame] | 231 | fd = xopen3(NON_CREDITABLE_SEED_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0400); |
| 232 | xwrite(fd, new_seed, new_seed_len); |
Denys Vlasenko | d5bd2e5 | 2022-05-01 01:50:44 +0200 | [diff] [blame] | 233 | if (new_seed_creditable) { |
| 234 | /* More paranoia when we create a file which we believe contains |
Denys Vlasenko | 54867fe | 2022-05-01 16:44:36 +0200 | [diff] [blame] | 235 | * genuine entropy: make sure disk is not full, quota isn't exceeded, etc: |
Denys Vlasenko | d5bd2e5 | 2022-05-01 01:50:44 +0200 | [diff] [blame] | 236 | */ |
| 237 | if (fsync(fd) < 0) |
| 238 | bb_perror_msg_and_die("can't write '%s'", NON_CREDITABLE_SEED_NAME); |
Denys Vlasenko | d49da38 | 2022-04-30 15:45:53 +0200 | [diff] [blame] | 239 | xrename(NON_CREDITABLE_SEED_NAME, CREDITABLE_SEED_NAME); |
Denys Vlasenko | d5bd2e5 | 2022-05-01 01:50:44 +0200 | [diff] [blame] | 240 | } |
Denys Vlasenko | d49da38 | 2022-04-30 15:45:53 +0200 | [diff] [blame] | 241 | return EXIT_SUCCESS; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 242 | } |