aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2022-04-30 23:17:58 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2022-04-30 23:17:58 +0200
commit464875411926d4085e3496f94551e532676d2e9d (patch)
treea5b0fb3cd79462e5ca9c090adedbd3c174cd3859
parent267178c62851a2e0fa3825bb49a67e362f41d4c0 (diff)
downloadbusybox-w32-464875411926d4085e3496f94551e532676d2e9d.tar.gz
busybox-w32-464875411926d4085e3496f94551e532676d2e9d.tar.bz2
busybox-w32-464875411926d4085e3496f94551e532676d2e9d.zip
seedrng: re-add fsync after unlink, and explain its purpose
function old new delta seedrng_main 1003 1022 +19 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--util-linux/seedrng.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/util-linux/seedrng.c b/util-linux/seedrng.c
index 74bf633a7..b79ce6627 100644
--- a/util-linux/seedrng.c
+++ b/util-linux/seedrng.c
@@ -128,7 +128,7 @@ static void seed_rng(uint8_t *seed, size_t len, bool credit)
128 close(random_fd); 128 close(random_fd);
129} 129}
130 130
131static void seed_from_file_if_exists(const char *filename, bool credit, sha256_ctx_t *hash) 131static void seed_from_file_if_exists(const char *filename, int dfd, bool credit, sha256_ctx_t *hash)
132{ 132{
133 uint8_t seed[MAX_SEED_LEN]; 133 uint8_t seed[MAX_SEED_LEN];
134 ssize_t seed_len; 134 ssize_t seed_len;
@@ -141,6 +141,16 @@ static void seed_from_file_if_exists(const char *filename, bool credit, sha256_c
141 } 141 }
142 xunlink(filename); 142 xunlink(filename);
143 if (seed_len != 0) { 143 if (seed_len != 0) {
144 /* We are going to use this data to seed the RNG:
145 * we believe it to genuinely containing entropy.
146 * If this just-unlinked file survives
147 * (e.g. if machine crashes _right now_)
148 * and we reuse it after reboot, this assumption
149 * would be violated. Fsync the directory to
150 * make sure file is gone:
151 */
152 fsync(dfd);
153
144 sha256_hash(hash, &seed_len, sizeof(seed_len)); 154 sha256_hash(hash, &seed_len, sizeof(seed_len));
145 sha256_hash(hash, seed, seed_len); 155 sha256_hash(hash, seed, seed_len);
146 printf("Seeding %u bits %s crediting\n", 156 printf("Seeding %u bits %s crediting\n",
@@ -193,6 +203,7 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
193 203
194 for (int i = 1; i < 3; ++i) { 204 for (int i = 1; i < 3; ++i) {
195 seed_from_file_if_exists(i == 1 ? NON_CREDITABLE_SEED_NAME : CREDITABLE_SEED_NAME, 205 seed_from_file_if_exists(i == 1 ? NON_CREDITABLE_SEED_NAME : CREDITABLE_SEED_NAME,
206 dfd,
196 i == 1 ? false : !skip_credit, 207 i == 1 ? false : !skip_credit,
197 &hash); 208 &hash);
198 } 209 }