aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2022-04-27 17:53:12 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2022-04-27 17:53:12 +0200
commit8456c21c09189da8eadb78ef5cc7fdb9825fbc13 (patch)
treed7312ba177c4467b4db3fcd5ca4d08eea42a8d0e
parentc82a0cd2b06f768ec569c42bbde328b1cebc347e (diff)
downloadbusybox-w32-8456c21c09189da8eadb78ef5cc7fdb9825fbc13.tar.gz
busybox-w32-8456c21c09189da8eadb78ef5cc7fdb9825fbc13.tar.bz2
busybox-w32-8456c21c09189da8eadb78ef5cc7fdb9825fbc13.zip
seedrng: chdir to the SEED_DIRECTORY - avoid concat_path_file's
function old new delta seedrng_main 1273 1225 -48 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--util-linux/seedrng.c18
1 files changed, 9 insertions, 9 deletions
diff --git a/util-linux/seedrng.c b/util-linux/seedrng.c
index 441bb7b93..c42274759 100644
--- a/util-linux/seedrng.c
+++ b/util-linux/seedrng.c
@@ -162,7 +162,7 @@ static int seed_from_file_if_exists(const char *filename, int dfd, bool credit,
162int seedrng_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE; 162int seedrng_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
163int seedrng_main(int argc UNUSED_PARAM, char *argv[]) 163int seedrng_main(int argc UNUSED_PARAM, char *argv[])
164{ 164{
165 const char *seed_dir, *creditable_seed, *non_creditable_seed; 165 const char *seed_dir;
166 int fd, dfd, program_ret = 0; 166 int fd, dfd, program_ret = 0;
167 uint8_t new_seed[MAX_SEED_LEN]; 167 uint8_t new_seed[MAX_SEED_LEN];
168 size_t new_seed_len; 168 size_t new_seed_len;
@@ -184,15 +184,15 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
184 seed_dir = DEFAULT_SEED_DIR; 184 seed_dir = DEFAULT_SEED_DIR;
185 skip_credit = getopt32long(argv, "d:n", longopts, &seed_dir) & OPT_n; 185 skip_credit = getopt32long(argv, "d:n", longopts, &seed_dir) & OPT_n;
186 umask(0077); 186 umask(0077);
187 if (getuid()) 187 if (getuid() != 0)
188 bb_simple_error_msg_and_die(bb_msg_you_must_be_root); 188 bb_simple_error_msg_and_die(bb_msg_you_must_be_root);
189 189
190 if (mkdir(seed_dir, 0700) < 0 && errno != EEXIST) 190 if (mkdir(seed_dir, 0700) < 0 && errno != EEXIST)
191 bb_perror_msg_and_die("can't %s seed directory", "create"); 191 bb_perror_msg_and_die("can't %s seed directory", "create");
192
193 dfd = open(seed_dir, O_DIRECTORY | O_RDONLY); 192 dfd = open(seed_dir, O_DIRECTORY | O_RDONLY);
194 if (dfd < 0 || flock(dfd, LOCK_EX) < 0) 193 if (dfd < 0 || flock(dfd, LOCK_EX) < 0)
195 bb_perror_msg_and_die("can't %s seed directory", "lock"); 194 bb_perror_msg_and_die("can't %s seed directory", "lock");
195 xfchdir(dfd);
196 196
197 sha256_begin(&hash); 197 sha256_begin(&hash);
198 sha256_hash(&hash, "SeedRNG v1 Old+New Prefix", 25); 198 sha256_hash(&hash, "SeedRNG v1 Old+New Prefix", 25);
@@ -201,11 +201,11 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
201 clock_gettime(CLOCK_BOOTTIME, &timestamp); 201 clock_gettime(CLOCK_BOOTTIME, &timestamp);
202 sha256_hash(&hash, &timestamp, sizeof(timestamp)); 202 sha256_hash(&hash, &timestamp, sizeof(timestamp));
203 203
204 creditable_seed = concat_path_file(seed_dir, CREDITABLE_SEED_NAME);
205 non_creditable_seed = concat_path_file(seed_dir, NON_CREDITABLE_SEED_NAME);
206 for (int i = 1; i < 3; ++i) { 204 for (int i = 1; i < 3; ++i) {
207 if (seed_from_file_if_exists(i == 1 ? non_creditable_seed : creditable_seed, 205 if (seed_from_file_if_exists(i == 1 ? NON_CREDITABLE_SEED_NAME : CREDITABLE_SEED_NAME,
208 dfd, i == 1 ? false : !skip_credit, &hash) < 0) 206 dfd,
207 i == 1 ? false : !skip_credit,
208 &hash) < 0)
209 program_ret |= 1 << i; 209 program_ret |= 1 << i;
210 } 210 }
211 211
@@ -221,12 +221,12 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
221 sha256_end(&hash, new_seed + new_seed_len - SHA256_OUTSIZE); 221 sha256_end(&hash, new_seed + new_seed_len - SHA256_OUTSIZE);
222 222
223 printf("Saving %u bits of %screditable seed for next boot\n", (unsigned)new_seed_len * 8, new_seed_creditable ? "" : "non-"); 223 printf("Saving %u bits of %screditable seed for next boot\n", (unsigned)new_seed_len * 8, new_seed_creditable ? "" : "non-");
224 fd = open(non_creditable_seed, O_WRONLY | O_CREAT | O_TRUNC, 0400); 224 fd = open(NON_CREDITABLE_SEED_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0400);
225 if (fd < 0 || full_write(fd, new_seed, new_seed_len) != (ssize_t)new_seed_len || fsync(fd) < 0) { 225 if (fd < 0 || full_write(fd, new_seed, new_seed_len) != (ssize_t)new_seed_len || fsync(fd) < 0) {
226 bb_perror_msg("can't%s seed", " write"); 226 bb_perror_msg("can't%s seed", " write");
227 return program_ret | (1 << 4); 227 return program_ret | (1 << 4);
228 } 228 }
229 if (new_seed_creditable && rename(non_creditable_seed, creditable_seed) < 0) { 229 if (new_seed_creditable && rename(NON_CREDITABLE_SEED_NAME, CREDITABLE_SEED_NAME) < 0) {
230 bb_simple_perror_msg("can't make new seed creditable"); 230 bb_simple_perror_msg("can't make new seed creditable");
231 return program_ret | (1 << 5); 231 return program_ret | (1 << 5);
232 } 232 }