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 | */ |
| 22 | |
| 23 | //config:config SEEDRNG |
Jason A. Donenfeld | 3cb40f8 | 2022-04-20 15:38:46 +0200 | [diff] [blame] | 24 | //config: bool "seedrng (2 kb)" |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 25 | //config: default y |
| 26 | //config: help |
| 27 | //config: Seed the kernel RNG from seed files, meant to be called |
| 28 | //config: once during startup, once during shutdown, and optionally |
| 29 | //config: at some periodic interval in between. |
| 30 | |
| 31 | //applet:IF_SEEDRNG(APPLET(seedrng, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 32 | |
| 33 | //kbuild:lib-$(CONFIG_SEEDRNG) += seedrng.o |
| 34 | |
| 35 | //usage:#define seedrng_trivial_usage |
Jason A. Donenfeld | 398bb38 | 2022-04-20 15:34:20 +0200 | [diff] [blame] | 36 | //usage: "[-d SEED_DIRECTORY] [-n]" |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 37 | //usage:#define seedrng_full_usage "\n\n" |
Denys Vlasenko | 137b205 | 2022-04-27 16:53:44 +0200 | [diff] [blame^] | 38 | //usage: "Seed the kernel RNG from seed files" |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 39 | //usage: "\n" |
Denys Vlasenko | 137b205 | 2022-04-27 16:53:44 +0200 | [diff] [blame^] | 40 | //usage: "\n -d DIR Use seed files from DIR (default: /var/lib/seedrng)" |
| 41 | //usage: "\n -n Skip crediting seeds, even if creditable" |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 42 | |
| 43 | #include "libbb.h" |
| 44 | |
| 45 | #include <linux/random.h> |
| 46 | #include <sys/random.h> |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 47 | #include <sys/file.h> |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 48 | |
| 49 | #ifndef GRND_INSECURE |
| 50 | #define GRND_INSECURE 0x0004 /* Apparently some headers don't ship with this yet. */ |
| 51 | #endif |
| 52 | |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 53 | #define DEFAULT_SEED_DIR "/var/lib/seedrng" |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 54 | #define CREDITABLE_SEED_NAME "seed.credit" |
| 55 | #define NON_CREDITABLE_SEED_NAME "seed.no-credit" |
| 56 | |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 57 | enum seedrng_lengths { |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 58 | MIN_SEED_LEN = SHA256_OUTSIZE, |
| 59 | MAX_SEED_LEN = 512 |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 62 | static size_t determine_optimal_seed_len(void) |
| 63 | { |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 64 | char poolsize_str[11] = { 0 }; |
Jason A. Donenfeld | 31ec481 | 2022-04-20 15:27:29 +0200 | [diff] [blame] | 65 | unsigned long poolsize; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 66 | |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 67 | if (open_read_close("/proc/sys/kernel/random/poolsize", poolsize_str, sizeof(poolsize_str) - 1) < 0) { |
Jason A. Donenfeld | ce9a345 | 2022-04-20 15:36:22 +0200 | [diff] [blame] | 68 | bb_perror_msg("unable to determine pool size, assuming %u bits", MIN_SEED_LEN * 8); |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 69 | return MIN_SEED_LEN; |
| 70 | } |
Jason A. Donenfeld | 31ec481 | 2022-04-20 15:27:29 +0200 | [diff] [blame] | 71 | poolsize = (bb_strtoul(poolsize_str, NULL, 10) + 7) / 8; |
| 72 | return MAX(MIN(poolsize, MAX_SEED_LEN), MIN_SEED_LEN); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static int read_new_seed(uint8_t *seed, size_t len, bool *is_creditable) |
| 76 | { |
| 77 | ssize_t ret; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 78 | |
| 79 | *is_creditable = false; |
| 80 | ret = getrandom(seed, len, GRND_NONBLOCK); |
| 81 | if (ret == (ssize_t)len) { |
| 82 | *is_creditable = true; |
| 83 | return 0; |
| 84 | } else if (ret < 0 && errno == ENOSYS) { |
| 85 | struct pollfd random_fd = { |
| 86 | .fd = open("/dev/random", O_RDONLY), |
| 87 | .events = POLLIN |
| 88 | }; |
| 89 | if (random_fd.fd < 0) |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 90 | return -1; |
Jason A. Donenfeld | f9ea8ba | 2022-04-21 12:37:32 +0200 | [diff] [blame] | 91 | *is_creditable = poll(&random_fd, 1, 0) == 1; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 92 | close(random_fd.fd); |
| 93 | } else if (getrandom(seed, len, GRND_INSECURE) == (ssize_t)len) |
| 94 | return 0; |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 95 | if (open_read_close("/dev/urandom", seed, len) == (ssize_t)len) |
| 96 | return 0; |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 97 | return -1; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static int seed_rng(uint8_t *seed, size_t len, bool credit) |
| 101 | { |
| 102 | struct { |
| 103 | int entropy_count; |
| 104 | int buf_size; |
| 105 | uint8_t buffer[MAX_SEED_LEN]; |
| 106 | } req = { |
| 107 | .entropy_count = credit ? len * 8 : 0, |
| 108 | .buf_size = len |
| 109 | }; |
| 110 | int random_fd, ret; |
| 111 | |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 112 | if (len > sizeof(req.buffer)) { |
| 113 | errno = EFBIG; |
| 114 | return -1; |
| 115 | } |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 116 | memcpy(req.buffer, seed, len); |
| 117 | |
Jason A. Donenfeld | 57fea02 | 2022-04-20 15:40:40 +0200 | [diff] [blame] | 118 | random_fd = open("/dev/urandom", O_RDONLY); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 119 | if (random_fd < 0) |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 120 | return -1; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 121 | ret = ioctl(random_fd, RNDADDENTROPY, &req); |
| 122 | if (ret) |
| 123 | ret = -errno ? -errno : -EIO; |
Jason A. Donenfeld | 31ec481 | 2022-04-20 15:27:29 +0200 | [diff] [blame] | 124 | if (ENABLE_FEATURE_CLEAN_UP) |
| 125 | close(random_fd); |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 126 | errno = -ret; |
| 127 | return ret ? -1 : 0; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Jason A. Donenfeld | 3c60711 | 2022-04-20 15:31:01 +0200 | [diff] [blame] | 130 | static int 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] | 131 | { |
| 132 | uint8_t seed[MAX_SEED_LEN]; |
| 133 | ssize_t seed_len; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 134 | |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 135 | seed_len = open_read_close(filename, seed, sizeof(seed)); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 136 | if (seed_len < 0) { |
Jason A. Donenfeld | 398bb38 | 2022-04-20 15:34:20 +0200 | [diff] [blame] | 137 | if (errno == ENOENT) |
| 138 | return 0; |
Jason A. Donenfeld | 57fea02 | 2022-04-20 15:40:40 +0200 | [diff] [blame] | 139 | bb_perror_msg("unable to%s seed", " read"); |
Jason A. Donenfeld | 398bb38 | 2022-04-20 15:34:20 +0200 | [diff] [blame] | 140 | return -1; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 141 | } |
| 142 | if ((unlink(filename) < 0 || fsync(dfd) < 0) && seed_len) { |
Jason A. Donenfeld | 57fea02 | 2022-04-20 15:40:40 +0200 | [diff] [blame] | 143 | bb_perror_msg("unable to%s seed", " remove"); |
Jason A. Donenfeld | 398bb38 | 2022-04-20 15:34:20 +0200 | [diff] [blame] | 144 | return -1; |
| 145 | } else if (!seed_len) |
| 146 | return 0; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 147 | |
| 148 | sha256_hash(hash, &seed_len, sizeof(seed_len)); |
| 149 | sha256_hash(hash, seed, seed_len); |
| 150 | |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 151 | printf("Seeding %zd bits %s crediting\n", seed_len * 8, credit ? "and" : "without"); |
Jason A. Donenfeld | 398bb38 | 2022-04-20 15:34:20 +0200 | [diff] [blame] | 152 | if (seed_rng(seed, seed_len, credit) < 0) { |
Jason A. Donenfeld | 57fea02 | 2022-04-20 15:40:40 +0200 | [diff] [blame] | 153 | bb_perror_msg("unable to%s seed", ""); |
Jason A. Donenfeld | 398bb38 | 2022-04-20 15:34:20 +0200 | [diff] [blame] | 154 | return -1; |
| 155 | } |
| 156 | return 0; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | int seedrng_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE; |
| 160 | int seedrng_main(int argc UNUSED_PARAM, char *argv[]) |
| 161 | { |
Jason A. Donenfeld | 57fea02 | 2022-04-20 15:40:40 +0200 | [diff] [blame] | 162 | const char *seed_dir = DEFAULT_SEED_DIR, *creditable_seed, *non_creditable_seed; |
Jason A. Donenfeld | f9ea8ba | 2022-04-21 12:37:32 +0200 | [diff] [blame] | 163 | int fd, dfd, program_ret = 0; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 164 | uint8_t new_seed[MAX_SEED_LEN]; |
| 165 | size_t new_seed_len; |
Jason A. Donenfeld | f9ea8ba | 2022-04-21 12:37:32 +0200 | [diff] [blame] | 166 | bool new_seed_creditable, skip_credit = false; |
Jason A. Donenfeld | 31ec481 | 2022-04-20 15:27:29 +0200 | [diff] [blame] | 167 | struct timespec timestamp = { 0 }; |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 168 | sha256_ctx_t hash; |
| 169 | |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 170 | enum { |
| 171 | OPT_d = (1 << 0), |
Jason A. Donenfeld | 398bb38 | 2022-04-20 15:34:20 +0200 | [diff] [blame] | 172 | OPT_n = (1 << 1) |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 173 | }; |
| 174 | #if ENABLE_LONG_OPTS |
| 175 | static const char longopts[] ALIGN1 = |
| 176 | "seed-dir\0" Required_argument "d" |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 177 | "skip-credit\0" No_argument "n" |
| 178 | ; |
| 179 | #endif |
| 180 | |
Jason A. Donenfeld | f9ea8ba | 2022-04-21 12:37:32 +0200 | [diff] [blame] | 181 | skip_credit = getopt32long(argv, "d:n", longopts, &seed_dir) & OPT_n; |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 182 | creditable_seed = concat_path_file(seed_dir, CREDITABLE_SEED_NAME); |
| 183 | non_creditable_seed = concat_path_file(seed_dir, NON_CREDITABLE_SEED_NAME); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 184 | |
| 185 | umask(0077); |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 186 | if (getuid()) |
Jason A. Donenfeld | ce9a345 | 2022-04-20 15:36:22 +0200 | [diff] [blame] | 187 | 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] | 188 | |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 189 | if (mkdir(seed_dir, 0700) < 0 && errno != EEXIST) |
Jason A. Donenfeld | 57fea02 | 2022-04-20 15:40:40 +0200 | [diff] [blame] | 190 | bb_perror_msg_and_die("unable to %s seed directory", "create"); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 191 | |
Jason A. Donenfeld | 3c60711 | 2022-04-20 15:31:01 +0200 | [diff] [blame] | 192 | dfd = open(seed_dir, O_DIRECTORY | O_RDONLY); |
Jason A. Donenfeld | f9ea8ba | 2022-04-21 12:37:32 +0200 | [diff] [blame] | 193 | if (dfd < 0 || flock(dfd, LOCK_EX) < 0) |
| 194 | bb_perror_msg_and_die("unable to %s seed directory", "lock"); |
Jason A. Donenfeld | 3c60711 | 2022-04-20 15:31:01 +0200 | [diff] [blame] | 195 | |
Jason A. Donenfeld | 31ec481 | 2022-04-20 15:27:29 +0200 | [diff] [blame] | 196 | sha256_begin(&hash); |
Jason A. Donenfeld | 3cb40f8 | 2022-04-20 15:38:46 +0200 | [diff] [blame] | 197 | sha256_hash(&hash, "SeedRNG v1 Old+New Prefix", 25); |
Jason A. Donenfeld | 31ec481 | 2022-04-20 15:27:29 +0200 | [diff] [blame] | 198 | clock_gettime(CLOCK_REALTIME, ×tamp); |
| 199 | sha256_hash(&hash, ×tamp, sizeof(timestamp)); |
| 200 | clock_gettime(CLOCK_BOOTTIME, ×tamp); |
| 201 | sha256_hash(&hash, ×tamp, sizeof(timestamp)); |
| 202 | |
Jason A. Donenfeld | f9ea8ba | 2022-04-21 12:37:32 +0200 | [diff] [blame] | 203 | for (int i = 1; i < 3; ++i) { |
| 204 | if (seed_from_file_if_exists(i == 1 ? non_creditable_seed : creditable_seed, |
| 205 | dfd, i == 1 ? false : !skip_credit, &hash) < 0) |
| 206 | program_ret |= 1 << i; |
| 207 | } |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 208 | |
| 209 | new_seed_len = determine_optimal_seed_len(); |
Jason A. Donenfeld | f9ea8ba | 2022-04-21 12:37:32 +0200 | [diff] [blame] | 210 | if (read_new_seed(new_seed, new_seed_len, &new_seed_creditable) < 0) { |
Jason A. Donenfeld | 57fea02 | 2022-04-20 15:40:40 +0200 | [diff] [blame] | 211 | bb_perror_msg("unable to%s seed", " read new"); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 212 | new_seed_len = SHA256_OUTSIZE; |
Jason A. Donenfeld | 3cb40f8 | 2022-04-20 15:38:46 +0200 | [diff] [blame] | 213 | memset(new_seed, 0, SHA256_OUTSIZE); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 214 | program_ret |= 1 << 3; |
| 215 | } |
| 216 | sha256_hash(&hash, &new_seed_len, sizeof(new_seed_len)); |
| 217 | sha256_hash(&hash, new_seed, new_seed_len); |
| 218 | sha256_end(&hash, new_seed + new_seed_len - SHA256_OUTSIZE); |
| 219 | |
Jason A. Donenfeld | 57fea02 | 2022-04-20 15:40:40 +0200 | [diff] [blame] | 220 | printf("Saving %zu bits of %screditable seed for next boot\n", new_seed_len * 8, new_seed_creditable ? "" : "non-"); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 221 | fd = open(non_creditable_seed, O_WRONLY | O_CREAT | O_TRUNC, 0400); |
Jason A. Donenfeld | 398bb38 | 2022-04-20 15:34:20 +0200 | [diff] [blame] | 222 | if (fd < 0 || full_write(fd, new_seed, new_seed_len) != (ssize_t)new_seed_len || fsync(fd) < 0) { |
Jason A. Donenfeld | 57fea02 | 2022-04-20 15:40:40 +0200 | [diff] [blame] | 223 | bb_perror_msg("unable to%s seed", " write"); |
Jason A. Donenfeld | f9ea8ba | 2022-04-21 12:37:32 +0200 | [diff] [blame] | 224 | return program_ret | (1 << 4); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 225 | } |
| 226 | if (new_seed_creditable && rename(non_creditable_seed, creditable_seed) < 0) { |
Jason A. Donenfeld | 4538578 | 2022-04-20 15:22:55 +0200 | [diff] [blame] | 227 | bb_simple_perror_msg("unable to make new seed creditable"); |
Jason A. Donenfeld | f9ea8ba | 2022-04-21 12:37:32 +0200 | [diff] [blame] | 228 | return program_ret | (1 << 5); |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 229 | } |
Jason A. Donenfeld | 4b407ba | 2022-04-04 18:21:51 +0200 | [diff] [blame] | 230 | return program_ret; |
| 231 | } |