All of lore.kernel.org
 help / color / mirror / Atom feed
From: Roel Kluin <roel.kluin@gmail.com>
To: Matt Mackall <mpm@selenic.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH] random: kmalloc failure ignored in init_std_data()
Date: Sat, 19 Sep 2009 01:03:53 +0200	[thread overview]
Message-ID: <4AB411D9.6070702@gmail.com> (raw)

Clean up and error out if kmalloc() fails.

Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
---
Found with sed: http://kernelnewbies.org/roelkluin

Build tested. Please review

diff --git a/drivers/char/random.c b/drivers/char/random.c
index d8a9255..8a68be8 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -939,7 +939,7 @@ EXPORT_SYMBOL(get_random_bytes);
  * data into the pool to prepare it for use. The pool is not cleared
  * as that can only decrease the entropy in the pool.
  */
-static void init_std_data(struct entropy_store *r)
+static int init_std_data(struct entropy_store *r)
 {
 	ktime_t now;
 	unsigned long flags;
@@ -952,16 +952,35 @@ static void init_std_data(struct entropy_store *r)
 	mix_pool_bytes(r, &now, sizeof(now));
 	mix_pool_bytes(r, utsname(), sizeof(*(utsname())));
 	/* Enable continuous test in fips mode */
-	if (fips_enabled)
+	if (fips_enabled) {
 		r->last_data = kmalloc(EXTRACT_SIZE, GFP_KERNEL);
+		if (r->last_data == NULL)
+			return -ENOMEM;
+	}
+	return 0;
 }
 
 static int rand_initialize(void)
 {
-	init_std_data(&input_pool);
-	init_std_data(&blocking_pool);
-	init_std_data(&nonblocking_pool);
+	int ret;
+	ret = init_std_data(&input_pool);
+	if (ret != 0)
+		return ret;
+
+	ret = init_std_data(&blocking_pool);
+	if (ret != 0)
+		goto free_ip_ld;
+
+	ret = init_std_data(&nonblocking_pool);
+	if (ret != 0)
+		goto free_bp_ld;
+
 	return 0;
+free_bp_ld:
+	kfree(blocking_pool.last_data);
+free_ip_ld:
+	kfree(input_pool.last_data);
+	return ret;
 }
 module_init(rand_initialize);
 
@@ -1160,8 +1179,8 @@ static long random_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
 		/* Clear the entropy pool counters. */
 		if (!capable(CAP_SYS_ADMIN))
 			return -EPERM;
-		rand_initialize();
-		return 0;
+		retval = rand_initialize();
+		return retval;
 	default:
 		return -EINVAL;
 	}

             reply	other threads:[~2009-09-18 22:56 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-18 23:03 Roel Kluin [this message]
2009-09-19  0:10 ` [PATCH] random: kmalloc failure ignored in init_std_data() Matt Mackall
2009-09-19  0:54   ` Roel Kluin
2009-09-19  7:19     ` Matt Mackall
2009-10-09  7:35     ` Andrew Morton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4AB411D9.6070702@gmail.com \
    --to=roel.kluin@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpm@selenic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.