blob: 82c69b72bcfe1fb789e03ef43b89979de3e48764 [file] [log] [blame]
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +02001// 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. Donenfeld31ec4812022-04-20 15:27:29 +02005 * 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. Donenfeld4b407ba2022-04-04 18:21:51 +020020 * This is based on code from <https://git.zx2c4.com/seedrng/about/>.
21 */
22
23//config:config SEEDRNG
Jason A. Donenfeld3cb40f82022-04-20 15:38:46 +020024//config: bool "seedrng (2 kb)"
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +020025//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. Donenfeld398bb382022-04-20 15:34:20 +020036//usage: "[-d SEED_DIRECTORY] [-n]"
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +020037//usage:#define seedrng_full_usage "\n\n"
Denys Vlasenko137b2052022-04-27 16:53:44 +020038//usage: "Seed the kernel RNG from seed files"
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +020039//usage: "\n"
Denys Vlasenko137b2052022-04-27 16:53:44 +020040//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. Donenfeld4b407ba2022-04-04 18:21:51 +020042
43#include "libbb.h"
44
45#include <linux/random.h>
46#include <sys/random.h>
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +020047#include <sys/file.h>
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +020048
49#ifndef GRND_INSECURE
50#define GRND_INSECURE 0x0004 /* Apparently some headers don't ship with this yet. */
51#endif
52
Jason A. Donenfeld45385782022-04-20 15:22:55 +020053#define DEFAULT_SEED_DIR "/var/lib/seedrng"
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +020054#define CREDITABLE_SEED_NAME "seed.credit"
55#define NON_CREDITABLE_SEED_NAME "seed.no-credit"
56
Denys Vlasenko40135652022-04-27 17:20:43 +020057enum {
Jason A. Donenfeld45385782022-04-20 15:22:55 +020058 MIN_SEED_LEN = SHA256_OUTSIZE,
59 MAX_SEED_LEN = 512
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +020060};
61
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +020062static size_t determine_optimal_seed_len(void)
63{
Denys Vlasenko40135652022-04-27 17:20:43 +020064 char poolsize_str[12];
65 unsigned poolsize;
66 int n;
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +020067
Denys Vlasenko40135652022-04-27 17:20:43 +020068 n = open_read_close("/proc/sys/kernel/random/poolsize", poolsize_str, sizeof(poolsize_str) - 1);
69 if (n < 0) {
Denys Vlasenkoc82a0cd2022-04-27 17:33:15 +020070 bb_perror_msg("can't determine pool size, assuming %u bits", MIN_SEED_LEN * 8);
Jason A. Donenfeld45385782022-04-20 15:22:55 +020071 return MIN_SEED_LEN;
72 }
Denys Vlasenko40135652022-04-27 17:20:43 +020073 poolsize_str[n] = '\0';
Jason A. Donenfeld31ec4812022-04-20 15:27:29 +020074 poolsize = (bb_strtoul(poolsize_str, NULL, 10) + 7) / 8;
75 return MAX(MIN(poolsize, MAX_SEED_LEN), MIN_SEED_LEN);
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +020076}
77
78static int read_new_seed(uint8_t *seed, size_t len, bool *is_creditable)
79{
80 ssize_t ret;
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +020081
82 *is_creditable = false;
83 ret = getrandom(seed, len, GRND_NONBLOCK);
84 if (ret == (ssize_t)len) {
85 *is_creditable = true;
86 return 0;
87 } else if (ret < 0 && errno == ENOSYS) {
88 struct pollfd random_fd = {
89 .fd = open("/dev/random", O_RDONLY),
90 .events = POLLIN
91 };
92 if (random_fd.fd < 0)
Jason A. Donenfeld45385782022-04-20 15:22:55 +020093 return -1;
Jason A. Donenfeldf9ea8ba2022-04-21 12:37:32 +020094 *is_creditable = poll(&random_fd, 1, 0) == 1;
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +020095 close(random_fd.fd);
96 } else if (getrandom(seed, len, GRND_INSECURE) == (ssize_t)len)
97 return 0;
Jason A. Donenfeld45385782022-04-20 15:22:55 +020098 if (open_read_close("/dev/urandom", seed, len) == (ssize_t)len)
99 return 0;
Jason A. Donenfeld45385782022-04-20 15:22:55 +0200100 return -1;
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200101}
102
Denys Vlasenko0fa16fc2022-04-29 18:37:42 +0200103static void seed_rng(uint8_t *seed, size_t len, bool credit)
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200104{
105 struct {
106 int entropy_count;
107 int buf_size;
108 uint8_t buffer[MAX_SEED_LEN];
Denys Vlasenko6da99472022-04-27 17:09:38 +0200109 } req;
Denys Vlasenko0fa16fc2022-04-29 18:37:42 +0200110 int random_fd;
Denys Vlasenko6da99472022-04-27 17:09:38 +0200111
112 req.entropy_count = credit ? len * 8 : 0;
113 req.buf_size = len;
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200114 memcpy(req.buffer, seed, len);
115
Denys Vlasenko0fa16fc2022-04-29 18:37:42 +0200116 random_fd = xopen("/dev/urandom", O_RDONLY);
117 xioctl(random_fd, RNDADDENTROPY, &req);
Jason A. Donenfeld31ec4812022-04-20 15:27:29 +0200118 if (ENABLE_FEATURE_CLEAN_UP)
119 close(random_fd);
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200120}
121
Denys Vlasenko0fa16fc2022-04-29 18:37:42 +0200122static void seed_from_file_if_exists(const char *filename, bool credit, sha256_ctx_t *hash)
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200123{
124 uint8_t seed[MAX_SEED_LEN];
125 ssize_t seed_len;
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200126
Jason A. Donenfeld45385782022-04-20 15:22:55 +0200127 seed_len = open_read_close(filename, seed, sizeof(seed));
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200128 if (seed_len < 0) {
Denys Vlasenko0fa16fc2022-04-29 18:37:42 +0200129 if (errno != ENOENT)
130 bb_perror_msg_and_die("can't%s seed", " read");
131 return;
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200132 }
Denys Vlasenko0fa16fc2022-04-29 18:37:42 +0200133 xunlink(filename);
134 if (seed_len != 0) {
135 sha256_hash(hash, &seed_len, sizeof(seed_len));
136 sha256_hash(hash, seed, seed_len);
137 printf("Seeding %u bits %s crediting\n", (unsigned)seed_len * 8, credit ? "and" : "without");
138 seed_rng(seed, seed_len, credit);
Jason A. Donenfeld398bb382022-04-20 15:34:20 +0200139 }
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200140}
141
142int seedrng_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
143int seedrng_main(int argc UNUSED_PARAM, char *argv[])
144{
Denys Vlasenko8456c212022-04-27 17:53:12 +0200145 const char *seed_dir;
Jason A. Donenfeldf9ea8ba2022-04-21 12:37:32 +0200146 int fd, dfd, program_ret = 0;
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200147 uint8_t new_seed[MAX_SEED_LEN];
148 size_t new_seed_len;
Jason A. Donenfeldf9ea8ba2022-04-21 12:37:32 +0200149 bool new_seed_creditable, skip_credit = false;
Denys Vlasenko6da99472022-04-27 17:09:38 +0200150 struct timespec timestamp;
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200151 sha256_ctx_t hash;
152
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200153 enum {
154 OPT_d = (1 << 0),
Jason A. Donenfeld398bb382022-04-20 15:34:20 +0200155 OPT_n = (1 << 1)
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200156 };
157#if ENABLE_LONG_OPTS
158 static const char longopts[] ALIGN1 =
159 "seed-dir\0" Required_argument "d"
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200160 "skip-credit\0" No_argument "n"
161 ;
162#endif
163
Denys Vlasenko40135652022-04-27 17:20:43 +0200164 seed_dir = DEFAULT_SEED_DIR;
Jason A. Donenfeldf9ea8ba2022-04-21 12:37:32 +0200165 skip_credit = getopt32long(argv, "d:n", longopts, &seed_dir) & OPT_n;
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200166 umask(0077);
Denys Vlasenko8456c212022-04-27 17:53:12 +0200167 if (getuid() != 0)
Jason A. Donenfeldce9a3452022-04-20 15:36:22 +0200168 bb_simple_error_msg_and_die(bb_msg_you_must_be_root);
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200169
Jason A. Donenfeld45385782022-04-20 15:22:55 +0200170 if (mkdir(seed_dir, 0700) < 0 && errno != EEXIST)
Denys Vlasenkoc82a0cd2022-04-27 17:33:15 +0200171 bb_perror_msg_and_die("can't %s seed directory", "create");
Jason A. Donenfeld3c607112022-04-20 15:31:01 +0200172 dfd = open(seed_dir, O_DIRECTORY | O_RDONLY);
Jason A. Donenfeldf9ea8ba2022-04-21 12:37:32 +0200173 if (dfd < 0 || flock(dfd, LOCK_EX) < 0)
Denys Vlasenkoc82a0cd2022-04-27 17:33:15 +0200174 bb_perror_msg_and_die("can't %s seed directory", "lock");
Denys Vlasenko8456c212022-04-27 17:53:12 +0200175 xfchdir(dfd);
Jason A. Donenfeld3c607112022-04-20 15:31:01 +0200176
Jason A. Donenfeld31ec4812022-04-20 15:27:29 +0200177 sha256_begin(&hash);
Jason A. Donenfeld3cb40f82022-04-20 15:38:46 +0200178 sha256_hash(&hash, "SeedRNG v1 Old+New Prefix", 25);
Jason A. Donenfeld31ec4812022-04-20 15:27:29 +0200179 clock_gettime(CLOCK_REALTIME, &timestamp);
180 sha256_hash(&hash, &timestamp, sizeof(timestamp));
181 clock_gettime(CLOCK_BOOTTIME, &timestamp);
182 sha256_hash(&hash, &timestamp, sizeof(timestamp));
183
Jason A. Donenfeldf9ea8ba2022-04-21 12:37:32 +0200184 for (int i = 1; i < 3; ++i) {
Denys Vlasenko0fa16fc2022-04-29 18:37:42 +0200185 seed_from_file_if_exists(i == 1 ? NON_CREDITABLE_SEED_NAME : CREDITABLE_SEED_NAME,
Denys Vlasenko8456c212022-04-27 17:53:12 +0200186 i == 1 ? false : !skip_credit,
Denys Vlasenko0fa16fc2022-04-29 18:37:42 +0200187 &hash);
Jason A. Donenfeldf9ea8ba2022-04-21 12:37:32 +0200188 }
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200189
190 new_seed_len = determine_optimal_seed_len();
Jason A. Donenfeldf9ea8ba2022-04-21 12:37:32 +0200191 if (read_new_seed(new_seed, new_seed_len, &new_seed_creditable) < 0) {
Denys Vlasenkoc82a0cd2022-04-27 17:33:15 +0200192 bb_perror_msg("can't%s seed", " read new");
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200193 new_seed_len = SHA256_OUTSIZE;
Jason A. Donenfeld3cb40f82022-04-20 15:38:46 +0200194 memset(new_seed, 0, SHA256_OUTSIZE);
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200195 program_ret |= 1 << 3;
196 }
197 sha256_hash(&hash, &new_seed_len, sizeof(new_seed_len));
198 sha256_hash(&hash, new_seed, new_seed_len);
199 sha256_end(&hash, new_seed + new_seed_len - SHA256_OUTSIZE);
200
Denys Vlasenkoc82a0cd2022-04-27 17:33:15 +0200201 printf("Saving %u bits of %screditable seed for next boot\n", (unsigned)new_seed_len * 8, new_seed_creditable ? "" : "non-");
Denys Vlasenko8456c212022-04-27 17:53:12 +0200202 fd = open(NON_CREDITABLE_SEED_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0400);
Jason A. Donenfeld398bb382022-04-20 15:34:20 +0200203 if (fd < 0 || full_write(fd, new_seed, new_seed_len) != (ssize_t)new_seed_len || fsync(fd) < 0) {
Denys Vlasenkoc82a0cd2022-04-27 17:33:15 +0200204 bb_perror_msg("can't%s seed", " write");
Jason A. Donenfeldf9ea8ba2022-04-21 12:37:32 +0200205 return program_ret | (1 << 4);
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200206 }
Denys Vlasenko8456c212022-04-27 17:53:12 +0200207 if (new_seed_creditable && rename(NON_CREDITABLE_SEED_NAME, CREDITABLE_SEED_NAME) < 0) {
Denys Vlasenkoc82a0cd2022-04-27 17:33:15 +0200208 bb_simple_perror_msg("can't make new seed creditable");
Jason A. Donenfeldf9ea8ba2022-04-21 12:37:32 +0200209 return program_ret | (1 << 5);
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200210 }
Jason A. Donenfeld4b407ba2022-04-04 18:21:51 +0200211 return program_ret;
212}