2014-08-29 17:43:00 +02:00
|
|
|
Simply writing /var/lib/systemd/random-seed back to /dev/(u)random does not
|
|
|
|
increase the entropy bit count. Therefore use the RNDADDENTROPY ioctl to
|
|
|
|
write back the bytes and increase the entropy bit counter.
|
|
|
|
|
|
|
|
Related to bnc#892096
|
|
|
|
|
|
|
|
---
|
2015-09-27 14:32:31 +02:00
|
|
|
src/random-seed/random-seed.c | 71 ++++++++++++++++++++++++++++++++++++------
|
2015-04-30 11:50:12 +02:00
|
|
|
1 file changed, 61 insertions(+), 10 deletions(-)
|
2014-08-29 17:43:00 +02:00
|
|
|
|
2015-09-27 14:32:31 +02:00
|
|
|
Index: systemd-221/src/random-seed/random-seed.c
|
|
|
|
===================================================================
|
|
|
|
--- systemd-221.orig/src/random-seed/random-seed.c
|
|
|
|
+++ systemd-221/src/random-seed/random-seed.c
|
2014-08-29 17:43:00 +02:00
|
|
|
@@ -22,7 +22,9 @@
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
+#include <linux/random.h>
|
|
|
|
#include <string.h>
|
|
|
|
+#include <sys/ioctl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
2015-04-30 11:50:12 +02:00
|
|
|
@@ -32,8 +34,8 @@
|
2014-08-29 17:43:00 +02:00
|
|
|
#define POOL_SIZE_MIN 512
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
- _cleanup_close_ int seed_fd = -1, random_fd = -1;
|
2015-04-30 11:50:12 +02:00
|
|
|
- _cleanup_free_ void* buf = NULL;
|
2014-08-29 17:43:00 +02:00
|
|
|
+ _cleanup_close_ int seed_fd = -1, random_fd = -1, entropy_fd = -1;
|
2015-04-30 11:50:12 +02:00
|
|
|
+ _cleanup_free_ struct rand_pool_info *entropy = NULL;
|
2014-08-29 17:43:00 +02:00
|
|
|
size_t buf_size = 0;
|
|
|
|
ssize_t k;
|
|
|
|
int r;
|
2015-04-30 11:50:12 +02:00
|
|
|
@@ -65,11 +67,12 @@ int main(int argc, char *argv[]) {
|
|
|
|
if (buf_size <= POOL_SIZE_MIN)
|
|
|
|
buf_size = POOL_SIZE_MIN;
|
|
|
|
|
|
|
|
- buf = malloc(buf_size);
|
|
|
|
- if (!buf) {
|
|
|
|
+ entropy = (struct rand_pool_info*) malloc(sizeof(struct rand_pool_info) + buf_size);
|
|
|
|
+ if (!entropy) {
|
|
|
|
r = log_oom();
|
|
|
|
goto finish;
|
|
|
|
}
|
2015-09-27 14:32:31 +02:00
|
|
|
+ entropy->buf_size = (typeof(entropy->buf_size)) buf_size;
|
2015-04-30 11:50:12 +02:00
|
|
|
|
|
|
|
r = mkdir_parents_label(RANDOM_SEED, 0755);
|
|
|
|
if (r < 0) {
|
|
|
|
@@ -83,6 +86,23 @@ int main(int argc, char *argv[]) {
|
2014-08-29 17:43:00 +02:00
|
|
|
|
|
|
|
if (streq(argv[1], "load")) {
|
|
|
|
|
|
|
|
+ entropy_fd = open(RANDOM_SEED_DIR "entropy_count", O_RDONLY|O_CLOEXEC|O_NOCTTY, 0600);
|
|
|
|
+ if (entropy_fd < 0) {
|
2015-09-27 14:32:31 +02:00
|
|
|
+ entropy->entropy_count = 0;
|
2014-08-29 17:43:00 +02:00
|
|
|
+ if (errno != ENOENT) {
|
|
|
|
+ log_error("Failed to open " RANDOM_SEED "/entropy_count: %m");
|
|
|
|
+ r = -errno;
|
|
|
|
+ goto finish;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
2015-04-30 11:50:12 +02:00
|
|
|
+ r = read(entropy_fd, &entropy->entropy_count, sizeof(entropy->entropy_count));
|
2014-08-29 17:43:00 +02:00
|
|
|
+ if (r < 0) {
|
|
|
|
+ log_error("Failed to read entropy count file: %m");
|
|
|
|
+ r = -errno;
|
|
|
|
+ goto finish;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
seed_fd = open(RANDOM_SEED, O_RDWR|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600);
|
|
|
|
if (seed_fd < 0) {
|
|
|
|
seed_fd = open(RANDOM_SEED, O_RDONLY|O_CLOEXEC|O_NOCTTY);
|
2015-09-27 14:32:31 +02:00
|
|
|
@@ -103,7 +123,7 @@ int main(int argc, char *argv[]) {
|
2015-04-30 11:50:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- k = loop_read(seed_fd, buf, buf_size, false);
|
2015-09-27 14:32:31 +02:00
|
|
|
+ k = loop_read(seed_fd, entropy->buf, (size_t) entropy->buf_size, false);
|
|
|
|
if (k < 0)
|
|
|
|
r = log_error_errno(k, "Failed to read seed from " RANDOM_SEED ": %m");
|
|
|
|
else if (k == 0)
|
|
|
|
@@ -111,13 +131,29 @@ int main(int argc, char *argv[]) {
|
|
|
|
else {
|
|
|
|
(void) lseek(seed_fd, 0, SEEK_SET);
|
2014-08-29 17:43:00 +02:00
|
|
|
|
2015-03-27 09:39:40 +01:00
|
|
|
- r = loop_write(random_fd, buf, (size_t) k, false);
|
|
|
|
- if (r < 0)
|
|
|
|
- log_error_errno(r, "Failed to write seed to /dev/urandom: %m");
|
2015-09-27 14:32:31 +02:00
|
|
|
+ if (entropy->entropy_count && (size_t)k == (size_t)entropy->buf_size) {
|
2015-04-30 11:50:12 +02:00
|
|
|
+ r = ioctl(random_fd, RNDADDENTROPY, entropy);
|
2014-08-29 17:43:00 +02:00
|
|
|
+ if (r < 0) {
|
2015-04-30 11:50:12 +02:00
|
|
|
+ log_error_errno(errno, "Failed to write seed to /dev/urandom: %m");
|
2014-08-29 17:43:00 +02:00
|
|
|
+ r = -errno;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
2015-04-30 11:50:12 +02:00
|
|
|
+ r = loop_write(random_fd, entropy->buf, (size_t) k, false);
|
|
|
|
+ if (r < 0)
|
|
|
|
+ log_error_errno(r, "Failed to write seed to /dev/urandom: %m");
|
|
|
|
+ }
|
2014-08-29 17:43:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (streq(argv[1], "save")) {
|
2015-04-30 11:50:12 +02:00
|
|
|
|
2014-08-29 17:43:00 +02:00
|
|
|
+ /* Read available entropy count, if possible */
|
|
|
|
+ f = fopen("/proc/sys/kernel/random/entropy_avail", "re");
|
|
|
|
+ if (f) {
|
2015-04-30 11:50:12 +02:00
|
|
|
+ if (fscanf(f, "%d", &entropy->entropy_count) < 0)
|
|
|
|
+ entropy->entropy_count = 0;
|
2014-08-29 17:43:00 +02:00
|
|
|
+ fclose(f);
|
|
|
|
+ }
|
2015-04-30 11:50:12 +02:00
|
|
|
+
|
2014-08-29 17:43:00 +02:00
|
|
|
seed_fd = open(RANDOM_SEED, O_WRONLY|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600);
|
|
|
|
if (seed_fd < 0) {
|
2015-09-27 14:32:31 +02:00
|
|
|
r = log_error_errno(errno, "Failed to open " RANDOM_SEED ": %m");
|
|
|
|
@@ -130,6 +166,21 @@ int main(int argc, char *argv[]) {
|
2014-08-29 17:43:00 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
2015-04-30 11:50:12 +02:00
|
|
|
+ if (entropy->entropy_count) {
|
2014-08-29 17:43:00 +02:00
|
|
|
+ entropy_fd = open(RANDOM_SEED_DIR "entropy_count", O_WRONLY|O_CLOEXEC|O_NOCTTY|O_CREAT, 0600);
|
|
|
|
+ if (seed_fd < 0) {
|
|
|
|
+ log_error("Failed to open " RANDOM_SEED_DIR "entropy_count: %m");
|
|
|
|
+ r = -errno;
|
|
|
|
+ goto finish;
|
|
|
|
+ }
|
2015-04-30 11:50:12 +02:00
|
|
|
+ r = write(entropy_fd, &entropy->entropy_count, sizeof(entropy->entropy_count));
|
2014-08-29 17:43:00 +02:00
|
|
|
+ if (r < 0) {
|
|
|
|
+ log_error("Failed to write entropy count file: %m");
|
|
|
|
+ r = -errno;
|
|
|
|
+ goto finish;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
} else {
|
2015-09-27 14:32:31 +02:00
|
|
|
log_error("Unknown verb '%s'.", argv[1]);
|
2014-08-29 17:43:00 +02:00
|
|
|
r = -EINVAL;
|
2015-09-27 14:32:31 +02:00
|
|
|
@@ -144,7 +195,7 @@ int main(int argc, char *argv[]) {
|
|
|
|
(void) fchmod(seed_fd, 0600);
|
|
|
|
(void) fchown(seed_fd, 0, 0);
|
2015-04-30 11:50:12 +02:00
|
|
|
|
|
|
|
- k = loop_read(random_fd, buf, buf_size, false);
|
2015-09-27 14:32:31 +02:00
|
|
|
+ k = loop_read(random_fd, entropy->buf, (size_t) entropy->buf_size, false);
|
|
|
|
if (k < 0) {
|
|
|
|
r = log_error_errno(k, "Failed to read new seed from /dev/urandom: %m");
|
|
|
|
goto finish;
|
|
|
|
@@ -155,7 +206,7 @@ int main(int argc, char *argv[]) {
|
|
|
|
goto finish;
|
2015-04-30 11:50:12 +02:00
|
|
|
}
|
2015-09-27 14:32:31 +02:00
|
|
|
|
|
|
|
- r = loop_write(seed_fd, buf, (size_t) k, false);
|
|
|
|
+ r = loop_write(seed_fd, entropy->buf, (size_t) k, false);
|
|
|
|
if (r < 0)
|
|
|
|
log_error_errno(r, "Failed to write new random seed file: %m");
|
|
|
|
}
|