sharkcz / rpms / rng-tools

Forked from rpms/rng-tools 3 years ago
Clone
8d6b73f
From 74f1926a81b80ce8719c92b688737c51ece2cb4b Mon Sep 17 00:00:00 2001
8d6b73f
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
8d6b73f
Date: Tue, 18 Oct 2016 10:50:42 -0400
8d6b73f
Subject: [PATCH] If device is not found, exit immediately
8d6b73f
8d6b73f
This avoids stupid warnings in the logs:
8d6b73f
rng[961]: read error
8d6b73f
rng[961]: read error
8d6b73f
...
8d6b73f
8d6b73f
https://bugzilla.redhat.com/show_bug.cgi?id=892178
8d6b73f
---
8d6b73f
 rngd.c           |  4 +---
8d6b73f
 rngd_entsource.c | 38 +++++++++++++++++++++++++++-----------
8d6b73f
 2 files changed, 28 insertions(+), 14 deletions(-)
8d6b73f
8d6b73f
diff --git a/rngd.c b/rngd.c
8d6b73f
index cd5bc8a40b..7755651f1c 100644
8d6b73f
--- a/rngd.c
8d6b73f
+++ b/rngd.c
8d6b73f
@@ -315,9 +315,7 @@ int main(int argc, char **argv)
8d6b73f
 	if (rc_rng && rc_drng && rc_tpm) {
8d6b73f
 		if (!arguments->quiet) {
8d6b73f
 			message(LOG_DAEMON|LOG_ERR,
8d6b73f
-				"can't open any entropy source");
8d6b73f
-			message(LOG_DAEMON|LOG_ERR,
8d6b73f
-				"Maybe RNG device modules are not loaded\n");
8d6b73f
+				"No entropy sources found, exiting");
8d6b73f
 		}
8d6b73f
 		return 66;
8d6b73f
 	}
8d6b73f
diff --git a/rngd_entsource.c b/rngd_entsource.c
8d6b73f
index f0e219d7af..468ad1cfc6 100644
8d6b73f
--- a/rngd_entsource.c
8d6b73f
+++ b/rngd_entsource.c
8d6b73f
@@ -63,8 +63,13 @@ int xread(void *buf, size_t size, struct rng *ent_src)
8d6b73f
 		size -= r;
8d6b73f
 	}
8d6b73f
 
8d6b73f
+	if (errno == ENODEV) {
8d6b73f
+		message(LOG_DAEMON|LOG_ERR, "%s: %m", ent_src->rng_name);
8d6b73f
+		return -ENODEV;
8d6b73f
+	}
8d6b73f
+
8d6b73f
 	if (size) {
8d6b73f
-		message(LOG_DAEMON|LOG_ERR, "read error\n");
8d6b73f
+		message(LOG_DAEMON|LOG_ERR, "%s: %m", ent_src->rng_name);
8d6b73f
 		return -1;
8d6b73f
 	}
8d6b73f
 	return 0;
8d6b73f
@@ -89,14 +94,14 @@ int xread_tpm(void *buf, size_t size, struct rng *ent_src)
8d6b73f
 
8d6b73f
 	ent_src->rng_fd = open(ent_src->rng_name, O_RDWR);
8d6b73f
 	if (ent_src->rng_fd == -1) {
8d6b73f
-		message(LOG_ERR|LOG_INFO,"Unable to open file: %s",ent_src->rng_name);
8d6b73f
+		message(LOG_ERR|LOG_INFO,"%s: %m",ent_src->rng_name);
8d6b73f
 		return -1;
8d6b73f
 	}
8d6b73f
 
8d6b73f
 	temp_buf = (unsigned char *) malloc(size + TPM_GET_RNG_OVERHEAD);
8d6b73f
 	memset(temp_buf, 0, (size+TPM_GET_RNG_OVERHEAD));
8d6b73f
 	if (temp_buf == NULL) {
8d6b73f
-		message(LOG_ERR|LOG_INFO,"No memory");
8d6b73f
+		message(LOG_ERR|LOG_INFO,"%m");
8d6b73f
 		close(ent_src->rng_fd);
8d6b73f
 		return -1;
8d6b73f
 	}
8d6b73f
@@ -114,7 +119,7 @@ int xread_tpm(void *buf, size_t size, struct rng *ent_src)
8d6b73f
 				       sizeof(rng_cmd) - r);
8d6b73f
 			if (retval < 0) {
8d6b73f
 				message(LOG_ERR|LOG_INFO,
8d6b73f
-					"Error writing %s\n",
8d6b73f
+					"Error writing %s",
8d6b73f
 					ent_src->rng_name);
8d6b73f
 				retval = -1;
8d6b73f
 				goto error_out;
8d6b73f
@@ -123,7 +128,7 @@ int xread_tpm(void *buf, size_t size, struct rng *ent_src)
8d6b73f
 		}
8d6b73f
 		if (r < sizeof(rng_cmd)) {
8d6b73f
 			message(LOG_ERR|LOG_INFO,
8d6b73f
-				"Error writing %s\n", ent_src->rng_name);
8d6b73f
+				"Error writing %s", ent_src->rng_name);
8d6b73f
 			retval = -1;
8d6b73f
 			goto error_out;
8d6b73f
 		}
8d6b73f
@@ -152,22 +157,27 @@ error_out:
8d6b73f
 }
8d6b73f
 
8d6b73f
 /* Initialize entropy source */
8d6b73f
-static int discard_initial_data(struct rng *ent_src)
8d6b73f
+static int discard_initial_data(struct rng *ent_src, int *data)
8d6b73f
 {
8d6b73f
 	/* Trash 32 bits of what is probably stale (non-random)
8d6b73f
-	 * initial state from the RNG.  For Intel's, 8 bits would
8d6b73f
+	 * initial state from the RNG.	For Intel's, 8 bits would
8d6b73f
 	 * be enough, but since AMD's generates 32 bits at a time...
8d6b73f
 	 *
8d6b73f
 	 * The kernel drivers should be doing this at device powerup,
8d6b73f
 	 * but at least up to 2.4.24, it doesn't. */
8d6b73f
 	unsigned char tempbuf[4];
8d6b73f
-	xread(tempbuf, sizeof(tempbuf), ent_src);
8d6b73f
+	int r;
8d6b73f
+
8d6b73f
+	r = xread(tempbuf, sizeof(tempbuf), ent_src);
8d6b73f
+	if (r < 0)
8d6b73f
+		return r;
8d6b73f
 
8d6b73f
 	/* Return 32 bits of bootstrap data */
8d6b73f
 	xread(tempbuf, sizeof(tempbuf), ent_src);
8d6b73f
 
8d6b73f
-	return tempbuf[0] | (tempbuf[1] << 8) |
8d6b73f
+	*data = tempbuf[0] | (tempbuf[1] << 8) |
8d6b73f
 		(tempbuf[2] << 16) | (tempbuf[3] << 24);
8d6b73f
+	return 0;
8d6b73f
 }
8d6b73f
 
8d6b73f
 /*
8d6b73f
@@ -175,14 +185,20 @@ static int discard_initial_data(struct rng *ent_src)
8d6b73f
  */
8d6b73f
 int init_entropy_source(struct rng *ent_src)
8d6b73f
 {
8d6b73f
+	int data;
8d6b73f
+
8d6b73f
 	ent_src->rng_fd = open(ent_src->rng_name, O_RDONLY);
8d6b73f
 	if (ent_src->rng_fd == -1) {
8d6b73f
 		return 1;
8d6b73f
 	}
8d6b73f
+	if (discard_initial_data(ent_src, &data)) {
8d6b73f
+		return 1;
8d6b73f
+	}
8d6b73f
+
8d6b73f
 	src_list_add(ent_src);
8d6b73f
 	/* Bootstrap FIPS tests */
8d6b73f
 	ent_src->fipsctx = malloc(sizeof(fips_ctx_t));
8d6b73f
-	fips_init(ent_src->fipsctx, discard_initial_data(ent_src));
8d6b73f
+	fips_init(ent_src->fipsctx, data);
8d6b73f
 	return 0;
8d6b73f
 }
8d6b73f
 
8d6b73f
@@ -193,7 +209,7 @@ int init_tpm_entropy_source(struct rng *ent_src)
8d6b73f
 {
8d6b73f
 	ent_src->rng_fd = open(ent_src->rng_name, O_RDWR);
8d6b73f
 	if (ent_src->rng_fd == -1) {
8d6b73f
-		message(LOG_ERR|LOG_INFO,"Unable to open file: %s",ent_src->rng_name);
8d6b73f
+		message(LOG_ERR|LOG_INFO,"%s: %m",ent_src->rng_name);
8d6b73f
 		return 1;
8d6b73f
 	}
8d6b73f
 	src_list_add(ent_src);
8d6b73f
-- 
8d6b73f
2.9.0
8d6b73f