Skip to content

Commit 3e155eb

Browse files
committed
random-seed: move pool size determination to random-util.[ch]
That way we can reuse it elsewhere.
1 parent c18ecf0 commit 3e155eb

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

src/basic/random-util.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525

2626
#include "alloc-util.h"
2727
#include "fd-util.h"
28+
#include "fileio.h"
2829
#include "io-util.h"
2930
#include "missing.h"
31+
#include "parse-util.h"
3032
#include "random-util.h"
3133
#include "siphash24.h"
3234
#include "time-util.h"
@@ -389,3 +391,26 @@ void random_bytes(void *p, size_t n) {
389391
/* If for some reason some user made /dev/urandom unavailable to us, or the kernel has no entropy, use a PRNG instead. */
390392
pseudo_random_bytes(p, n);
391393
}
394+
395+
size_t random_pool_size(void) {
396+
_cleanup_free_ char *s = NULL;
397+
int r;
398+
399+
/* Read pool size, if possible */
400+
r = read_one_line_file("/proc/sys/kernel/random/poolsize", &s);
401+
if (r < 0)
402+
log_debug_errno(r, "Failed to read pool size from kernel: %m");
403+
else {
404+
unsigned sz;
405+
406+
r = safe_atou(s, &sz);
407+
if (r < 0)
408+
log_debug_errno(r, "Failed to parse pool size: %s", s);
409+
else
410+
/* poolsize is in bits on 2.6, but we want bytes */
411+
return CLAMP(sz / 8, RANDOM_POOL_SIZE_MIN, RANDOM_POOL_SIZE_MAX);
412+
}
413+
414+
/* Use the minimum as default, if we can't retrieve the correct value */
415+
return RANDOM_POOL_SIZE_MIN;
416+
}

src/basic/random-util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,9 @@ static inline uint32_t random_u32(void) {
3131
}
3232

3333
int rdrand(unsigned long *ret);
34+
35+
/* Some limits on the pool sizes when we deal with the kernel random pool */
36+
#define RANDOM_POOL_SIZE_MIN 512U
37+
#define RANDOM_POOL_SIZE_MAX (10U*1024U*1024U)
38+
39+
size_t random_pool_size(void);

src/random-seed/random-seed.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,17 @@
1515
#include "log.h"
1616
#include "main-func.h"
1717
#include "mkdir.h"
18+
#include "random-util.h"
1819
#include "string-util.h"
1920
#include "util.h"
2021

21-
#define POOL_SIZE_MIN 512
22-
#define POOL_SIZE_MAX (10*1024*1024)
23-
2422
static int run(int argc, char *argv[]) {
2523
_cleanup_close_ int seed_fd = -1, random_fd = -1;
2624
bool read_seed_file, write_seed_file;
2725
_cleanup_free_ void* buf = NULL;
28-
size_t buf_size = 0;
26+
size_t buf_size;
2927
struct stat st;
3028
ssize_t k;
31-
FILE *f;
3229
int r;
3330

3431
log_setup_service();
@@ -39,18 +36,7 @@ static int run(int argc, char *argv[]) {
3936

4037
umask(0022);
4138

42-
/* Read pool size, if possible */
43-
f = fopen("/proc/sys/kernel/random/poolsize", "re");
44-
if (f) {
45-
if (fscanf(f, "%zu", &buf_size) > 0)
46-
/* poolsize is in bits on 2.6, but we want bytes */
47-
buf_size /= 8;
48-
49-
fclose(f);
50-
}
51-
52-
if (buf_size < POOL_SIZE_MIN)
53-
buf_size = POOL_SIZE_MIN;
39+
buf_size = random_pool_size();
5440

5541
r = mkdir_parents(RANDOM_SEED, 0755);
5642
if (r < 0)
@@ -113,7 +99,7 @@ static int run(int argc, char *argv[]) {
11399

114100
/* If the seed file is larger than what we expect, then honour the existing size and save/restore as much as it says */
115101
if ((uint64_t) st.st_size > buf_size)
116-
buf_size = MIN(st.st_size, POOL_SIZE_MAX);
102+
buf_size = MIN(st.st_size, RANDOM_POOL_SIZE_MAX);
117103

118104
buf = malloc(buf_size);
119105
if (!buf)

0 commit comments

Comments
 (0)