public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: U-Boot Mailing List <u-boot@lists.denx.de>
Cc: Tom Rini <trini@konsulko.com>, Simon Glass <sjg@chromium.org>
Subject: [PATCH 05/18] abuf: Correct a corner case with abuf_realloc()
Date: Mon, 28 Feb 2022 12:08:22 -0700	[thread overview]
Message-ID: <20220228190835.1480772-6-sjg@chromium.org> (raw)
In-Reply-To: <20220228190835.1480772-1-sjg@chromium.org>

If the buffer is empty and not allocated, then abuf_realloc() tries to
copy invalid data. This happens because an incorrect change to use
memdup() was added after the original code was written.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 lib/abuf.c      |  4 +++-
 test/lib/abuf.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/lib/abuf.c b/lib/abuf.c
index 4b17e0b8c0..1635d58682 100644
--- a/lib/abuf.c
+++ b/lib/abuf.c
@@ -51,9 +51,11 @@ bool abuf_realloc(struct abuf *abuf, size_t new_size)
 		/* not currently allocated and new size is larger. Alloc and
 		 * copy in data. The new space is not inited.
 		 */
-		ptr = memdup(abuf->data, new_size);
+		ptr = malloc(new_size);
 		if (!ptr)
 			return false;
+		if (abuf->size)
+			memcpy(ptr, abuf->data, abuf->size);
 		abuf->data = ptr;
 		abuf->size = new_size;
 		abuf->alloced = true;
diff --git a/test/lib/abuf.c b/test/lib/abuf.c
index 086c9b2282..42ee4c1755 100644
--- a/test/lib/abuf.c
+++ b/test/lib/abuf.c
@@ -126,6 +126,35 @@ static int lib_test_abuf_realloc(struct unit_test_state *uts)
 }
 LIB_TEST(lib_test_abuf_realloc, 0);
 
+/* Test abuf_realloc() on an non-allocated buffer of zero size */
+static int lib_test_abuf_realloc_size(struct unit_test_state *uts)
+{
+	struct abuf buf;
+	ulong start;
+
+	start = ut_check_free();
+
+	abuf_init(&buf);
+
+	/* Allocate some space */
+	ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
+	ut_assertnonnull(buf.data);
+	ut_asserteq(TEST_DATA_LEN, buf.size);
+	ut_asserteq(true, buf.alloced);
+
+	/* Free it */
+	ut_asserteq(true, abuf_realloc(&buf, 0));
+	ut_assertnull(buf.data);
+	ut_asserteq(0, buf.size);
+	ut_asserteq(false, buf.alloced);
+
+	/* Check for memory leaks */
+	ut_assertok(ut_check_delta(start));
+
+	return 0;
+}
+LIB_TEST(lib_test_abuf_realloc_size, 0);
+
 /* Test handling of buffers that are too large */
 static int lib_test_abuf_large(struct unit_test_state *uts)
 {
-- 
2.35.1.574.g5d30c73bfb-goog


  parent reply	other threads:[~2022-02-28 19:10 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-28 19:08 [PATCH 00/18] Various bugfixes related to verified boot Simon Glass
2022-02-28 19:08 ` [PATCH 01/18] buildman: Update default config to build for sandbox Simon Glass
2022-04-07 13:50   ` Tom Rini
2022-02-28 19:08 ` [PATCH 02/18] buildman: Fix up cfgutil Simon Glass
2022-02-28 19:08 ` [PATCH 03/18] binman: Correct Chromium OS entry types Simon Glass
2022-02-28 19:08 ` [PATCH 04/18] errno: Avoid including strings in SPL Simon Glass
2022-02-28 19:08 ` Simon Glass [this message]
2022-02-28 19:08 ` [PATCH 06/18] fdt: Correct condition for SEPARATE_BSS Simon Glass
2022-02-28 19:08 ` [PATCH 07/18] fdt: sandbox: Avoid looking for an appended device tree Simon Glass
2022-02-28 19:08 ` [PATCH 08/18] lzma: Tidy up the function prototype Simon Glass
2022-02-28 19:08 ` [PATCH 09/18] cbfs: Add some more definititions Simon Glass
2022-02-28 19:08 ` [PATCH 10/18] cros_ec: Complete the comment for cros_ec_read_batt_charge() Simon Glass
2022-02-28 19:08 ` [PATCH 11/18] spi: Avoid checking console in SPL Simon Glass
2022-02-28 19:08 ` [PATCH 12/18] disk: Correct the conditions for SPL Simon Glass
2022-02-28 19:08 ` [PATCH 13/18] Add a default for TPL_TEXT_BASE Simon Glass
2022-02-28 19:08 ` [PATCH 14/18] Make ASYMMETRIC_KEY_TYPE depend on FIT_SIGNATURE Simon Glass
2022-03-01  4:22   ` AKASHI Takahiro
2022-03-12  2:25     ` Simon Glass
2022-03-14  1:59       ` AKASHI Takahiro
2022-02-28 19:08 ` [PATCH 15/18] stdint: Add a definition of UINT8_MAX Simon Glass
2022-02-28 19:08 ` [PATCH 16/18] dm: core: Add a required struct declaration Simon Glass
2022-02-28 19:08 ` [PATCH 17/18] dm: core: Tidy up comments in uclass headers Simon Glass
2022-02-28 19:08 ` [PATCH 18/18] dm: blk: Expand iteration and add tests Simon Glass

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=20220228190835.1480772-6-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --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