public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Simon Goldschmidt <simon.k.r.goldschmidt@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v10 02/10] lmb: fix allocation at end of address range
Date: Mon, 14 Jan 2019 22:38:15 +0100	[thread overview]
Message-ID: <20190114213823.32486-3-simon.k.r.goldschmidt@gmail.com> (raw)
In-Reply-To: <20190114213823.32486-1-simon.k.r.goldschmidt@gmail.com>

The lmb code fails if base + size of RAM overflows to zero.

Fix this by calculating end as 'base + size - 1' instead of 'base + size'
where appropriate.

Added tests to assert this is fixed.

Signed-off-by: Simon Goldschmidt <simon.k.r.goldschmidt@gmail.com>
---

Changes in v10: None
Changes in v9: None
Changes in v8: None
Changes in v7: None
Changes in v6: None
Changes in v5:
- this patch is new in v5

Changes in v4: None
Changes in v2: None

 lib/lmb.c      | 29 ++++++++++++-----------------
 test/lib/lmb.c | 29 ++++++++++++++++++++++++++---
 2 files changed, 38 insertions(+), 20 deletions(-)

diff --git a/lib/lmb.c b/lib/lmb.c
index 1705417348..6d3dcf4e09 100644
--- a/lib/lmb.c
+++ b/lib/lmb.c
@@ -43,7 +43,10 @@ void lmb_dump_all(struct lmb *lmb)
 static long lmb_addrs_overlap(phys_addr_t base1,
 		phys_size_t size1, phys_addr_t base2, phys_size_t size2)
 {
-	return ((base1 < (base2+size2)) && (base2 < (base1+size1)));
+	const phys_addr_t base1_end = base1 + size1 - 1;
+	const phys_addr_t base2_end = base2 + size2 - 1;
+
+	return ((base1 <= base2_end) && (base2 <= base1_end));
 }
 
 static long lmb_addrs_adjacent(phys_addr_t base1, phys_size_t size1,
@@ -89,18 +92,9 @@ static void lmb_coalesce_regions(struct lmb_region *rgn,
 
 void lmb_init(struct lmb *lmb)
 {
-	/* Create a dummy zero size LMB which will get coalesced away later.
-	 * This simplifies the lmb_add() code below...
-	 */
-	lmb->memory.region[0].base = 0;
-	lmb->memory.region[0].size = 0;
-	lmb->memory.cnt = 1;
+	lmb->memory.cnt = 0;
 	lmb->memory.size = 0;
-
-	/* Ditto. */
-	lmb->reserved.region[0].base = 0;
-	lmb->reserved.region[0].size = 0;
-	lmb->reserved.cnt = 1;
+	lmb->reserved.cnt = 0;
 	lmb->reserved.size = 0;
 }
 
@@ -110,9 +104,10 @@ static long lmb_add_region(struct lmb_region *rgn, phys_addr_t base, phys_size_t
 	unsigned long coalesced = 0;
 	long adjacent, i;
 
-	if ((rgn->cnt == 1) && (rgn->region[0].size == 0)) {
+	if (rgn->cnt == 0) {
 		rgn->region[0].base = base;
 		rgn->region[0].size = size;
+		rgn->cnt = 1;
 		return 0;
 	}
 
@@ -183,7 +178,7 @@ long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
 {
 	struct lmb_region *rgn = &(lmb->reserved);
 	phys_addr_t rgnbegin, rgnend;
-	phys_addr_t end = base + size;
+	phys_addr_t end = base + size - 1;
 	int i;
 
 	rgnbegin = rgnend = 0; /* supress gcc warnings */
@@ -191,7 +186,7 @@ long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
 	/* Find the region where (base, size) belongs to */
 	for (i=0; i < rgn->cnt; i++) {
 		rgnbegin = rgn->region[i].base;
-		rgnend = rgnbegin + rgn->region[i].size;
+		rgnend = rgnbegin + rgn->region[i].size - 1;
 
 		if ((rgnbegin <= base) && (end <= rgnend))
 			break;
@@ -209,7 +204,7 @@ long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
 
 	/* Check to see if region is matching@the front */
 	if (rgnbegin == base) {
-		rgn->region[i].base = end;
+		rgn->region[i].base = end + 1;
 		rgn->region[i].size -= size;
 		return 0;
 	}
@@ -225,7 +220,7 @@ long lmb_free(struct lmb *lmb, phys_addr_t base, phys_size_t size)
 	 * beginging of the hole and add the region after hole.
 	 */
 	rgn->region[i].size = base - rgn->region[i].base;
-	return lmb_add_region(rgn, end, rgnend - end);
+	return lmb_add_region(rgn, end + 1, rgnend - end);
 }
 
 long lmb_reserve(struct lmb *lmb, phys_addr_t base, phys_size_t size)
diff --git a/test/lib/lmb.c b/test/lib/lmb.c
index dd7ba14b34..fb7ca45ef1 100644
--- a/test/lib/lmb.c
+++ b/test/lib/lmb.c
@@ -146,8 +146,15 @@ static int test_multi_alloc_512mb(struct unit_test_state *uts,
 /* Create a memory region with one reserved region and allocate */
 static int lib_test_lmb_simple(struct unit_test_state *uts)
 {
+	int ret;
+
 	/* simulate 512 MiB RAM beginning at 1GiB */
-	return test_multi_alloc_512mb(uts, 0x40000000);
+	ret = test_multi_alloc_512mb(uts, 0x40000000);
+	if (ret)
+		return ret;
+
+	/* simulate 512 MiB RAM beginning at 1.5GiB */
+	return test_multi_alloc_512mb(uts, 0xE0000000);
 }
 
 DM_TEST(lib_test_lmb_simple, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
@@ -206,7 +213,15 @@ static int test_bigblock(struct unit_test_state *uts, const phys_addr_t ram)
 
 static int lib_test_lmb_big(struct unit_test_state *uts)
 {
-	return test_bigblock(uts, 0x40000000);
+	int ret;
+
+	/* simulate 512 MiB RAM beginning at 1GiB */
+	ret = test_bigblock(uts, 0x40000000);
+	if (ret)
+		return ret;
+
+	/* simulate 512 MiB RAM beginning at 1.5GiB */
+	return test_bigblock(uts, 0xE0000000);
 }
 
 DM_TEST(lib_test_lmb_big, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
@@ -247,7 +262,15 @@ static int test_noreserved(struct unit_test_state *uts, const phys_addr_t ram)
 
 static int lib_test_lmb_noreserved(struct unit_test_state *uts)
 {
-	return test_noreserved(uts, 0x40000000);
+	int ret;
+
+	/* simulate 512 MiB RAM beginning at 1GiB */
+	ret = test_noreserved(uts, 0x40000000);
+	if (ret)
+		return ret;
+
+	/* simulate 512 MiB RAM beginning at 1.5GiB */
+	return test_noreserved(uts, 0xE0000000);
 }
 
 DM_TEST(lib_test_lmb_noreserved, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
-- 
2.17.1

  parent reply	other threads:[~2019-01-14 21:38 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-14 21:38 [U-Boot] [PATCH v10 00/10] Fix CVE-2018-18440 and CVE-2018-18439 Simon Goldschmidt
2019-01-14 21:38 ` [U-Boot] [PATCH v10 01/10] test: add test for lib/lmb.c Simon Goldschmidt
2019-01-17 22:44   ` [U-Boot] [U-Boot,v10,01/10] " Tom Rini
2019-01-14 21:38 ` Simon Goldschmidt [this message]
2019-01-16 21:34   ` [U-Boot] [PATCH v10 02/10] lmb: fix allocation at end of address range Simon Glass
2019-01-17 22:44   ` [U-Boot] [U-Boot, v10, " Tom Rini
2019-01-14 21:38 ` [U-Boot] [PATCH v10 03/10] lib: lmb: reserving overlapping regions should fail Simon Goldschmidt
2019-01-16 21:34   ` Simon Glass
2019-01-17 22:44   ` [U-Boot] [U-Boot, v10, " Tom Rini
2019-01-14 21:38 ` [U-Boot] [PATCH v10 04/10] fdt: parse "reserved-memory" for memory reservation Simon Goldschmidt
2019-01-17 22:44   ` [U-Boot] [U-Boot, v10, " Tom Rini
2019-03-05 23:26     ` Eugeniu Rosca
2019-03-05 23:36       ` Marek Vasut
2019-01-14 21:38 ` [U-Boot] [PATCH v10 05/10] lib: lmb: extend lmb for checks at load time Simon Goldschmidt
2019-01-16 21:34   ` Simon Glass
2019-01-16 21:44     ` Simon Goldschmidt
2019-01-16 21:49       ` Tom Rini
2019-01-16 21:51         ` Simon Glass
2019-01-17 22:44   ` [U-Boot] [U-Boot, v10, " Tom Rini
2019-01-14 21:38 ` [U-Boot] [PATCH v10 06/10] fs: prevent overwriting reserved memory Simon Goldschmidt
2019-01-17 22:44   ` [U-Boot] [U-Boot, v10, " Tom Rini
2019-01-14 21:38 ` [U-Boot] [PATCH v10 07/10] bootm: use new common function lmb_init_and_reserve Simon Goldschmidt
2019-01-17 22:44   ` [U-Boot] [U-Boot, v10, " Tom Rini
2019-01-14 21:38 ` [U-Boot] [PATCH v10 08/10] lmb: remove unused extern declaration Simon Goldschmidt
2019-01-16 21:34   ` Simon Glass
2019-01-17 22:44   ` [U-Boot] [U-Boot, v10, " Tom Rini
2019-01-14 21:38 ` [U-Boot] [PATCH v10 09/10] tftp: prevent overwriting reserved memory Simon Goldschmidt
2019-01-17 22:44   ` [U-Boot] [U-Boot, v10, " Tom Rini
2019-01-26  3:20   ` Heinrich Schuchardt
2019-01-26  8:46     ` Simon Goldschmidt
2019-01-26  9:56       ` Heinrich Schuchardt
2019-01-26 13:25         ` Heinrich Schuchardt
2019-01-26 21:20         ` Simon Goldschmidt
2019-01-26 13:17       ` Tom Rini
2019-01-26 21:15         ` Simon Goldschmidt
2019-01-14 21:38 ` [U-Boot] [PATCH v10 10/10] arm: bootm: fix sp detection at end of address range Simon Goldschmidt
2019-01-17 22:44   ` [U-Boot] [U-Boot, v10, " Tom Rini
2019-01-14 22:54 ` [U-Boot] [PATCH v10 00/10] Fix CVE-2018-18440 and CVE-2018-18439 Tom Rini
2019-01-15  5:08   ` Simon Goldschmidt

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=20190114213823.32486-3-simon.k.r.goldschmidt@gmail.com \
    --to=simon.k.r.goldschmidt@gmail.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