Skip to contentSkip to conversation

Fix misc ASAN reports

PATCH In review Andrew Scull 4 participants 27 messages Apr 3, 2022 – Apr 29, 2022 USB

lore
Share & tools
lore
#1 lore raw

PATCH 1/8 doc: Correct position of gdb '--args' parameter

In reply to Andrew Scull

The '--args' parameter to gdb comes before the binary that the debugger will be attached to rather than after the binary and before the arguments. Fix that in the docs.

---
 doc/develop/tests_sandbox.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
doc/develop/tests_sandbox.rst +1 -1
index 84608dcb84..40cf8ecdd7 100644
@@ -103,7 +103,7 @@ running with -D will produce different results.
 
 You can easily use gdb on these tests, without needing --gdbserver::
 
-   $ gdb u-boot --args -T -c "ut dm gpio"
+   $ gdb --args u-boot -T -c "ut dm gpio"
    ...
    (gdb) break dm_test_gpio
    Breakpoint 1 at 0x1415bd: file test/dm/gpio.c, line 37.
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
#4 lore raw

PATCH 2/8 acpi: Fix buffer overflow in do_acpi_dump()

In reply to Andrew Scull

When do_acpi_dump() converts the table name to upper case, pass the actual size of the output buffer so that the null terminator doesn't get written beyond the end of the buffer.

Cc Simon Glass <[email protected]>
Cc Wolfgang Wallner <[email protected]>
Cc Bin Meng <[email protected]>
---
 cmd/acpi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
cmd/acpi.c +1 -1
index c543f1e3c2..0e473b415d 100644
@@ -178,7 +178,7 @@ static int do_acpi_dump(struct cmd_tbl *cmdtp, int flag, int argc,
 		printf("Table name '%s' must be four characters\n", name);
 		return CMD_RET_FAILURE;
 	}
-	str_to_upper(name, sig, -1);
+	str_to_upper(name, sig, ACPI_NAME_LEN);
 	ret = dump_table_name(sig);
 	if (ret) {
 		printf("Table '%.*s' not found\n", ACPI_NAME_LEN, sig);
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
#7 lore raw

PATCH 3/8 x86: sandbox: Add missing PCI bar to barinfo

In reply to Andrew Scull

There are expecte to be bars 0 through 5, but the last of these was missing leading to an read beyond the buffer. Add the missing element with zero values.

---
 drivers/power/acpi_pmc/pmc_emul.c | 1 +
 1 file changed, 1 insertion(+)
drivers/power/acpi_pmc/pmc_emul.c +1 -0
index a61eb5bd85..8015031da8 100644
@@ -37,6 +37,7 @@ static struct pci_bar {
 	{ 0, 0 },
 	{ 0, 0 },
 	{ PCI_BASE_ADDRESS_SPACE_IO, 256 },
+	{ 0, 0 },
 };
 
 struct pmc_emul_priv {
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
#0 lore raw

cover Fix misc ASAN reports

I've been experimenting with ASAN on sandbox and turned up a few issues that are fixed in this series.

Basic ASAN was easy to turn on, but integrating with dlmalloc was messier and fairly intrusive. Even when I had it working, there was only a small redzone between allocations which limits the usefulness.

I saw another series on the list by Sean Anderson to enable valgrind which was finding a different set of issues, though there was one overlap that Sean is fixing with
"[PATCH] IOMUX: Fix access past end of console_devices".

With these issues fixed, I was able to run the dm tests without any ASAN issues. There are a couple of leaks reported at the end, but that's for another day.

Andrew Scull (8):
doc: Correct position of gdb '--args' parameter
acpi: Fix buffer overflow in do_acpi_dump()
x86: sandbox: Add missing PCI bar to barinfo
usb: sandbox: Check for string end in copy_to_unicode()
usb: sandbox: Bounds check read from buffer
sound: Fix buffer overflow in square wave generation
test: Fix pointer overrun in dm_test_devm_regmap()
test: dm: devres: Remove use-after-free

 cmd/acpi.c                         | 2 +-
 doc/develop/tests_sandbox.rst      | 2 +-
 drivers/power/acpi_pmc/pmc_emul.c  | 1 +
 drivers/sound/sound.c              | 6 ++----
 drivers/usb/emul/sandbox_flash.c   | 2 ++
 drivers/usb/emul/usb-emul-uclass.c | 5 ++---
 test/dm/devres.c                   | 5 +----
 test/dm/regmap.c                   | 9 ++++-----
 8 files changed, 14 insertions(+), 18 deletions(-)
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
#10 lore raw

PATCH 4/8 usb: sandbox: Check for string end in copy_to_unicode()

When copying the string in copy_to_unicode(), check for the null terminator in each position, not just at the start, to avoid reading beyond the end of the string.

Cc Simon Glass <[email protected]>
Cc Marek Vasut <[email protected]>
---
 drivers/usb/emul/usb-emul-uclass.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/usb/emul/usb-emul-uclass.c +2 -3
index 05f6d3d9e2..b31dc950e3 100644
@@ -15,13 +15,12 @@
 static int copy_to_unicode(char *buff, int length, const char *str)
 {
 	int ptr;
-	int i;
 
 	if (length < 2)
 		return 0;
 	buff[1] = USB_DT_STRING;
-	for (ptr = 2, i = 0; ptr + 1 < length && *str; i++, ptr += 2) {
-		buff[ptr] = str[i];
+	for (ptr = 2; ptr + 1 < length && *str; str++, ptr += 2) {
+		buff[ptr] = *str;
 		buff[ptr + 1] = 0;
 	}
 	buff[0] = ptr;
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
#13 lore raw

PATCH 5/8 usb: sandbox: Bounds check read from buffer

In reply to Andrew Scull

The buffer is 512 bytes but read requests can be 800 bytes. Limit the request to the size of the buffer.

Cc Simon Glass <[email protected]>
Cc Marek Vasut <[email protected]>
---
 drivers/usb/emul/sandbox_flash.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/emul/sandbox_flash.c +2 -0
index edabc1b3a7..cc80f67133 100644
@@ -345,6 +345,8 @@ static int sandbox_flash_bulk(struct udevice *dev, struct usb_device *udev,
 			} else {
 				if (priv->alloc_len && len > priv->alloc_len)
 					len = priv->alloc_len;
+				if (len > sizeof(priv->buff))
+					len = sizeof(priv->buff);
 				memcpy(buff, priv->buff, len);
 				priv->phase = PHASE_STATUS;
 			}
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
#19 lore raw

PATCH 7/8 test: Fix pointer overrun in dm_test_devm_regmap()

In reply to Andrew Scull

This tests calls regmap_read() which takes a uint pointer as an output parameter. The test was passing a pointer to a u16 which resulted in an overflow when the output was written. Fix this by following the regmap_read() API and passing a uint pointer instead.

Cc Simon Glass <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Jean-Jacques Hiblot <[email protected]>
Cc Pratyush Yadav <[email protected]>
---
 test/dm/regmap.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
test/dm/regmap.c +4 -5
index 04bb1645d1..8560f2afc2 100644
@@ -286,8 +286,7 @@ U_BOOT_DRIVER(regmap_test) = {
 static int dm_test_devm_regmap(struct unit_test_state *uts)
 {
 	int i = 0;
-	u16 val;
-	void *valp = &val;
+	uint val;
 	u16 pattern[REGMAP_TEST_BUF_SZ];
 	u16 *buffer;
 	struct udevice *dev;
@@ -311,7 +310,7 @@ static int dm_test_devm_regmap(struct unit_test_state *uts)
 		ut_assertok(regmap_write(priv->cfg_regmap, i, pattern[i]));
 	}
 	for (i = 0; i < REGMAP_TEST_BUF_SZ; i++) {
-		ut_assertok(regmap_read(priv->cfg_regmap, i, valp));
+		ut_assertok(regmap_read(priv->cfg_regmap, i, &val));
 		ut_asserteq(val, buffer[i]);
 		ut_asserteq(val, pattern[i]);
 	}
@@ -319,9 +318,9 @@ static int dm_test_devm_regmap(struct unit_test_state *uts)
 	ut_asserteq(-ERANGE, regmap_write(priv->cfg_regmap, REGMAP_TEST_BUF_SZ,
 					  val));
 	ut_asserteq(-ERANGE, regmap_read(priv->cfg_regmap, REGMAP_TEST_BUF_SZ,
-					 valp));
+					 &val));
 	ut_asserteq(-ERANGE, regmap_write(priv->cfg_regmap, -1, val));
-	ut_asserteq(-ERANGE, regmap_read(priv->cfg_regmap, -1, valp));
+	ut_asserteq(-ERANGE, regmap_read(priv->cfg_regmap, -1, &val));
 
 	return 0;
 }
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
#16 lore raw

PATCH 6/8 sound: Fix buffer overflow in square wave generation

In reply to Andrew Scull

Data is written for each channel but is only tracked as having one channel written. This resulted in a buffer overflow and corruption of the allocator's metadata which caused further problems when the buffer was later freed. This could be observed with sandbox unit tests.

Resolve the overflow by tracking the writes for each channel.

Fixes f987177db9 ("dm: sound: Use the correct number of channels for sound")
Cc Simon Glass <[email protected]>
---
 drivers/sound/sound.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/sound/sound.c +2 -4
index b0eab23391..041dfdccfe 100644
@@ -25,13 +25,11 @@ void sound_create_square_wave(uint sample_rate, unsigned short *data, int size,
 		int i, j;
 
 		for (i = 0; size && i < half; i++) {
-			size -= 2;
-			for (j = 0; j < channels; j++)
+			for (j = 0; size && j < channels; j++, size -= 2)
 				*data++ = amplitude;
 		}
 		for (i = 0; size && i < period - half; i++) {
-			size -= 2;
-			for (j = 0; j < channels; j++)
+			for (j = 0; size && j < channels; j++, size -= 2)
 				*data++ = -amplitude;
 		}
 	}
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
#22 lore raw

PATCH 8/8 test: dm: devres: Remove use-after-free

In reply to Andrew Scull

Use-after-free shouldn't be used, even in tests. It's bad practice and makes the test brittle.

---
 test/dm/devres.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
test/dm/devres.c +1 -4
index 4f959d11da..524114c833 100644
@@ -178,11 +178,8 @@ static int dm_test_devres_phase(struct unit_test_state *uts)
 	ut_asserteq(1, stats.allocs);
 	ut_asserteq(TEST_DEVRES_SIZE, stats.total_size);
 
-	/* Unbinding removes the other. Note this access a freed pointer */
+	/* Unbinding removes the other. */
 	device_unbind(dev);
-	devres_get_stats(dev, &stats);
-	ut_asserteq(0, stats.allocs);
-	ut_asserteq(0, stats.total_size);
 
 	return 0;
 }
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
#25 lore raw
In reply to Andrew Scull
Quoted 6 lines · Andrew Scull

I've been experimenting with ASAN on sandbox and turned up a few issues that are fixed in this series.

Basic ASAN was easy to turn on, but integrating with dlmalloc was messier and fairly intrusive. Even when I had it working, there was only a small redzone between allocations which limits the usefulness.

Do you have any patches for this?

--Sean

Quoted 29 lines

I saw another series on the list by Sean Anderson to enable valgrind which was finding a different set of issues, though there was one overlap that Sean is fixing with
"[PATCH] IOMUX: Fix access past end of console_devices".

With these issues fixed, I was able to run the dm tests without any ASAN issues. There are a couple of leaks reported at the end, but that's for another day.

Andrew Scull (8):
doc: Correct position of gdb '--args' parameter
acpi: Fix buffer overflow in do_acpi_dump()
x86: sandbox: Add missing PCI bar to barinfo
usb: sandbox: Check for string end in copy_to_unicode()
usb: sandbox: Bounds check read from buffer
sound: Fix buffer overflow in square wave generation
test: Fix pointer overrun in dm_test_devm_regmap()
test: dm: devres: Remove use-after-free

  cmd/acpi.c                         | 2 +-
  doc/develop/tests_sandbox.rst      | 2 +-
  drivers/power/acpi_pmc/pmc_emul.c  | 1 +
  drivers/sound/sound.c              | 6 ++----
  drivers/usb/emul/sandbox_flash.c   | 2 ++
  drivers/usb/emul/usb-emul-uclass.c | 5 ++---
  test/dm/devres.c                   | 5 +----
  test/dm/regmap.c                   | 9 ++++-----
  8 files changed, 14 insertions(+), 18 deletions(-)
#26 lore raw
Quoted 10 lines · Sean Anderson
Quoted 6 lines · Andrew Scull

I've been experimenting with ASAN on sandbox and turned up a few issues that are fixed in this series.

Basic ASAN was easy to turn on, but integrating with dlmalloc was messier and fairly intrusive. Even when I had it working, there was only a small redzone between allocations which limits the usefulness.

Do you have any patches for this?

They're becoming less of a mess as we speak, except the dlmalloc part which I don't have a good idea how to make neater. I'm actually doing it as a prerequisite for fuzzing, which I've almost got an acceptable feeling framework for and hope to be able to share fairly soon. But I could post just the ASAN parts separately if there's interest for that.

If you're using gcc, you can just add -fsanitize=address to the compiler and linker commands; clang requires some more work to rename symbols that begin with '.'.

Quoted 33 lines

--Sean

Quoted 29 lines

I saw another series on the list by Sean Anderson to enable valgrind which was finding a different set of issues, though there was one overlap that Sean is fixing with
"[PATCH] IOMUX: Fix access past end of console_devices".

With these issues fixed, I was able to run the dm tests without any ASAN issues. There are a couple of leaks reported at the end, but that's for another day.

Andrew Scull (8):
doc: Correct position of gdb '--args' parameter
acpi: Fix buffer overflow in do_acpi_dump()
x86: sandbox: Add missing PCI bar to barinfo
usb: sandbox: Check for string end in copy_to_unicode()
usb: sandbox: Bounds check read from buffer
sound: Fix buffer overflow in square wave generation
test: Fix pointer overrun in dm_test_devm_regmap()
test: dm: devres: Remove use-after-free

  cmd/acpi.c                         | 2 +-
  doc/develop/tests_sandbox.rst      | 2 +-
  drivers/power/acpi_pmc/pmc_emul.c  | 1 +
  drivers/sound/sound.c              | 6 ++----
  drivers/usb/emul/sandbox_flash.c   | 2 ++
  drivers/usb/emul/usb-emul-uclass.c | 5 ++---
  test/dm/devres.c                   | 5 +----
  test/dm/regmap.c                   | 9 ++++-----
  8 files changed, 14 insertions(+), 18 deletions(-)
Reviewed-by on v1 4/8
#11 lore raw

Re: [PATCH 4/8] usb: sandbox: Check for string end in copy_to_unicode()

In reply to Andrew Scull
Quoted 11 lines · Andrew Scull

When copying the string in copy_to_unicode(), check for the null terminator in each position, not just at the start, to avoid reading beyond the end of the string.

Cc Simon Glass <[email protected]>
Cc Marek Vasut <[email protected]>
---
 drivers/usb/emul/usb-emul-uclass.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
Reviewed-by on v1 5/8
#14 lore raw

Re: [PATCH 5/8] usb: sandbox: Bounds check read from buffer

In reply to Andrew Scull
Quoted 10 lines · Andrew Scull

The buffer is 512 bytes but read requests can be 800 bytes. Limit the request to the size of the buffer.

Cc Simon Glass <[email protected]>
Cc Marek Vasut <[email protected]>
---
 drivers/usb/emul/sandbox_flash.c | 2 ++
 1 file changed, 2 insertions(+)
Reviewed-by on v1 3/8
#8 lore raw

Re: [PATCH 3/8] x86: sandbox: Add missing PCI bar to barinfo

In reply to Andrew Scull
Quoted 11 lines · Andrew Scull

There are expecte to be bars 0 through 5, but the last of these was missing leading to an read beyond the buffer. Add the missing element with zero values.

---
 drivers/power/acpi_pmc/pmc_emul.c | 1 +
 1 file changed, 1 insertion(+)
Reviewed-by on v1 1/8
#2 lore raw

Re: [PATCH 1/8] doc: Correct position of gdb '--args' parameter

In reply to Andrew Scull
Quoted 11 lines · Andrew Scull

The '--args' parameter to gdb comes before the binary that the debugger will be attached to rather than after the binary and before the arguments. Fix that in the docs.

---
 doc/develop/tests_sandbox.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Reviewed-by on v1 7/8
#20 lore raw

Re: [PATCH 7/8] test: Fix pointer overrun in dm_test_devm_regmap()

In reply to Andrew Scull
Quoted 6 lines · Andrew Scull

This tests calls regmap_read() which takes a uint pointer as an output parameter. The test was passing a pointer to a u16 which resulted in an overflow when the output was written. Fix this by following the regmap_read() API and passing a uint pointer instead.

Quoted 8 lines
Cc Simon Glass <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Jean-Jacques Hiblot <[email protected]>
Cc Pratyush Yadav <[email protected]>
---
 test/dm/regmap.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
Reviewed-by on v1 6/8
#17 lore raw

Re: [PATCH 6/8] sound: Fix buffer overflow in square wave generation

In reply to Andrew Scull
Quoted 6 lines · Andrew Scull

Data is written for each channel but is only tracked as having one channel written. This resulted in a buffer overflow and corruption of the allocator's metadata which caused further problems when the buffer was later freed. This could be observed with sandbox unit tests.

Quoted 8 lines

Resolve the overflow by tracking the writes for each channel.

Fixes f987177db9 ("dm: sound: Use the correct number of channels for sound")
Cc Simon Glass <[email protected]>
---
 drivers/sound/sound.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
Reviewed-by on v1 2/8
#5 lore raw

Re: [PATCH 2/8] acpi: Fix buffer overflow in do_acpi_dump()

In reply to Andrew Scull
Quoted 5 lines · Andrew Scull

When do_acpi_dump() converts the table name to upper case, pass the actual size of the output buffer so that the null terminator doesn't get written beyond the end of the buffer.

Quoted 7 lines
Cc Simon Glass <[email protected]>
Cc Wolfgang Wallner <[email protected]>
Cc Bin Meng <[email protected]>
---
 cmd/acpi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#3 lore raw

Re: [PATCH 1/8] doc: Correct position of gdb '--args' parameter

In reply to Andrew Scull
Quoted 7 lines · Andrew Scull

The '--args' parameter to gdb comes before the binary that the debugger will be attached to rather than after the binary and before the arguments. Fix that in the docs.

Applied to u-boot/master, thanks!

Signature · 1 line
Tom
#6 lore raw

Re: [PATCH 2/8] acpi: Fix buffer overflow in do_acpi_dump()

In reply to Andrew Scull
Quoted 9 lines · Andrew Scull

When do_acpi_dump() converts the table name to upper case, pass the actual size of the output buffer so that the null terminator doesn't get written beyond the end of the buffer.

Applied to u-boot/master, thanks!

Signature · 1 line
Tom
#9 lore raw

Re: [PATCH 3/8] x86: sandbox: Add missing PCI bar to barinfo

In reply to Andrew Scull
Quoted 8 lines · Andrew Scull

There are expecte to be bars 0 through 5, but the last of these was missing leading to an read beyond the buffer. Add the missing element with zero values.

Applied to u-boot/master, thanks!

Signature · 1 line
Tom
#12 lore raw

Re: [PATCH 4/8] usb: sandbox: Check for string end in copy_to_unicode()

In reply to Andrew Scull
Quoted 8 lines · Andrew Scull

When copying the string in copy_to_unicode(), check for the null terminator in each position, not just at the start, to avoid reading beyond the end of the string.

Applied to u-boot/master, thanks!

Signature · 1 line
Tom
#18 lore raw

Re: [PATCH 6/8] sound: Fix buffer overflow in square wave generation

In reply to Andrew Scull
Quoted 11 lines · Andrew Scull

Data is written for each channel but is only tracked as having one channel written. This resulted in a buffer overflow and corruption of the allocator's metadata which caused further problems when the buffer was later freed. This could be observed with sandbox unit tests.

Resolve the overflow by tracking the writes for each channel.

Fixes f987177db9 ("dm: sound: Use the correct number of channels for sound")
Cc Simon Glass <[email protected]>

Applied to u-boot/master, thanks!

Signature · 1 line
Tom
#21 lore raw

Re: [PATCH 7/8] test: Fix pointer overrun in dm_test_devm_regmap()

In reply to Andrew Scull
Quoted 11 lines · Andrew Scull

This tests calls regmap_read() which takes a uint pointer as an output parameter. The test was passing a pointer to a u16 which resulted in an overflow when the output was written. Fix this by following the regmap_read() API and passing a uint pointer instead.

Cc Simon Glass <[email protected]>
Cc Heinrich Schuchardt <[email protected]>
Cc Jean-Jacques Hiblot <[email protected]>
Cc Pratyush Yadav <[email protected]>

Applied to u-boot/master, thanks!

Signature · 1 line
Tom