U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Lukasz Majewski <l.majewski@majess.pl>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 1/2] FIX: env: import: hashtable: Prevent buffer overrun when importing environment from file
Date: Mon, 14 Sep 2015 00:57:03 +0200	[thread overview]
Message-ID: <1442185024-14645-1-git-send-email-l.majewski@majess.pl> (raw)

Lets consider following scenario:
- One uses echo -n "key=value" to define environment variable in a file (single variable)
- The file content is "key=value" without any terminating byte (e.g. 0x0a or
0x0d).
- The file is loaded to u-boot non zero'ed RAM buffer (with load command).
- Then "env import -t -r $loadaddr $filesize" is executed.
- Due to lack of proper termination byte we have classical example of buffer
  overrun.

This patch prevents from this by allocating one extra byte than size and
explicitly null terminate it.

There should be no change for normal env import operation after applying
this patch.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>
---
 lib/hashtable.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/hashtable.c b/lib/hashtable.c
index 18ed590..7df424a 100644
--- a/lib/hashtable.c
+++ b/lib/hashtable.c
@@ -789,12 +789,13 @@ int himport_r(struct hsearch_data *htab,
 	}
 
 	/* we allocate new space to make sure we can write to the array */
-	if ((data = malloc(size)) == NULL) {
-		debug("himport_r: can't malloc %zu bytes\n", size);
+	if ((data = malloc(size + 1)) == NULL) {
+		debug("himport_r: can't malloc %zu bytes\n", size + 1);
 		__set_errno(ENOMEM);
 		return 0;
 	}
 	memcpy(data, env, size);
+	data[size] = '\0';
 	dp = data;
 
 	/* make a local copy of the list of variables */
-- 
2.1.4

             reply	other threads:[~2015-09-13 22:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-13 22:57 Lukasz Majewski [this message]
2015-09-13 22:57 ` [U-Boot] [PATCH 2/2] FIX: env: import: hashtable: Free memory allocated before exiting from himport_r() Lukasz Majewski
2015-09-15 21:34   ` [U-Boot] [U-Boot, " Tom Rini
2015-09-15 21:34 ` [U-Boot] [U-Boot, 1/2] FIX: env: import: hashtable: Prevent buffer overrun when importing environment from file Tom Rini

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=1442185024-14645-1-git-send-email-l.majewski@majess.pl \
    --to=l.majewski@majess.pl \
    --cc=u-boot@lists.denx.de \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox