Skip to contentSkip to conversation

handle compression buffer overflows

PATCH Superseded Kees Cook 3 participants 15 messages Aug 16, 2013 – Nov 8, 2013 Library code

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

v2 cover handle compression buffer overflows

v2: added acks, various suggested cleanups

This series fixes gzip, lzma, and lzo to not overflow when writing to output buffers. Without this, it might be possible for untrusted compressed input to overflow the buffers used to hold the decompressed image.

To catch these conditions, I added a series of compression tests available in the sandbox build. Without the fixes in patches 3, 4, and 5, the overflows are visible.

Thanks,

-Kees

Kees Cook (6):
sandbox: add compression tests
documentation: add more compression configs
gzip: correctly bounds-check output buffer
lzma: correctly bounds-check output buffer
lzo: correctly bounds-check output buffer
bootm: allow correct bounds-check of destination

 README                     |    9 ++
 common/cmd_bootm.c         |    2 +-
 include/configs/sandbox.h  |    5 +
 lib/gunzip.c               |    4 +-
 lib/lzma/LzmaTools.c       |    8 +-
 lib/lzo/lzo1x_decompress.c |    8 +-
 test/Makefile              |    1 +
 test/compression.c         |  335 ++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 366 insertions(+), 6 deletions(-)
 create mode 100644 test/compression.c
author
#1 lore raw

PATCH v2 1/6 sandbox: add compression tests

This adds the "test_compression" command when building the sandbox. This tests the existing compression and decompression routines for simple sanity and for buffer overflow conditions.

---
v2:
- updates, suggested by Simon Glass:
- replace license text with correct stub
- drop redundant #ifdefs

---
 include/configs/sandbox.h |    5 +
 test/Makefile             |    1 +
 test/compression.c        |  335 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 341 insertions(+)
 create mode 100644 test/compression.c
include/configs/sandbox.h +5 -0
index af3d6ad..4027030 100644
@@ -109,4 +109,9 @@
 					"stdout=serial\0" \
 					"stderr=serial\0"
 
+#define CONFIG_GZIP_COMPRESSED
+#define CONFIG_BZIP2
+#define CONFIG_LZO
+#define CONFIG_LZMA
+
 #endif
test/Makefile +1 -0
index 99ce890..a68613d 100644
@@ -9,6 +9,7 @@ include $(TOPDIR)/config.mk
 LIB	= $(obj)libtest.o
 
 COBJS-$(CONFIG_SANDBOX) += command_ut.o
+COBJS-$(CONFIG_SANDBOX) += compression.o
 
 COBJS	:= $(sort $(COBJS-y))
 SRCS	:= $(COBJS:.o=.c)
test/compression.c new file +335 -0

335 lines not on this page · raw message

Signature · 1 line
1.7.9.5
author
#3 lore raw

PATCH v2 2/6 documentation: add more compression configs

In reply to Kees Cook

This adds the missing compression config items to the README.

---
v2:
- adjusted language slightly, thanks to Simon Glass

---
 README |    9 +++++++++
 1 file changed, 9 insertions(+)
README +9 -0
index 3918807..6485350 100644
@@ -1680,6 +1680,10 @@ CBFS (Coreboot Filesystem) support
 		to compress the specified memory at its best effort.
 
 - Compression support:
+		CONFIG_GZIP
+
+		Enabled by default to support gzip compressed images.
+
 		CONFIG_BZIP2
 
 		If this option is set, support for bzip2 compressed
@@ -1713,6 +1717,11 @@ CBFS (Coreboot Filesystem) support
 		then calculate the amount of needed dynamic memory (ensuring
 		the appropriate CONFIG_SYS_MALLOC_LEN value).
 
+		CONFIG_LZO
+
+		If this option is set, support for LZO compressed images
+		is included.
+
 - MII/PHY support:
 		CONFIG_PHY_ADDR
 
Signature · 1 line
1.7.9.5
author
#5 lore raw

PATCH v2 3/6 gzip: correctly bounds-check output buffer

In reply to Kees Cook

The output buffer size must not be reset by the gzip decoder or there is a risk of overflowing memory during decompression.

---
 lib/gunzip.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/gunzip.c +2 -2
index 9959781..35abfb3 100644
@@ -89,13 +89,13 @@ int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
 	s.avail_out = dstlen;
 	do {
 		r = inflate(&s, Z_FINISH);
-		if (r != Z_STREAM_END && r != Z_BUF_ERROR && stoponerr == 1) {
+		if (stoponerr == 1 && r != Z_STREAM_END &&
+		    (s.avail_out == 0 || r != Z_BUF_ERROR)) {
 			printf("Error: inflate() returned %d\n", r);
 			inflateEnd(&s);
 			return -1;
 		}
 		s.avail_in = *lenp - offset - (int)(s.next_out - (unsigned char*)dst);
-		s.avail_out = dstlen;
 	} while (r == Z_BUF_ERROR);
 	*lenp = s.next_out - (unsigned char *) dst;
 	inflateEnd(&s);
Signature · 1 line
1.7.9.5
#6 lore raw

PATCH 3/6 gzip: correctly bounds-check output buffer

Hi Kees,

Quoted 20 lines · Kees Cook

The output buffer size must not be reset by the gzip decoder or there is a risk of overflowing memory during decompression.

---
 lib/gunzip.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/gunzip.c +1 -1
index 9959781..35abfb3 100644
@@ -89,13 +89,13 @@ int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
 	s.avail_out = dstlen;
 	do {
 		r = inflate(&s, Z_FINISH);
-		if (r != Z_STREAM_END && r != Z_BUF_ERROR && stoponerr == 1) {
+		if (stoponerr == 1 && r != Z_STREAM_END &&

lib/gunzip.c int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp, · line 101

Quoted 10 lines
lib/gunzip.c +1 -1
+		    (s.avail_out == 0 || r != Z_BUF_ERROR)) {
 			printf("Error: inflate() returned %d\n", r);
 			inflateEnd(&s);
 			return -1;
 		}
 		s.avail_in = *lenp - offset - (int)(s.next_out - (unsigned char*)dst);
-		s.avail_out = dstlen;
 	} while (r == Z_BUF_ERROR);
 	*lenp = s.next_out - (unsigned char *) dst;
 	inflateEnd(&s);

I have done u-boot upgrade to v2013.10 version and I see the problem with this patch when I am trying to boot my zynq image.

After reverting this patch everything works as expected.

Here is the image I am using.
http://www.monstr.eu/20131108-image.ub

Below is the bootlog.

Do you have any idea what can be wrong?

Thanks,
Michal

U-Boot 2013.10 (Nov 08 2013 - 13:02:26)

Memory: ECC disabled
DRAM:  1 GiB
WARNING: Caches not enabled
MMC:   zynq_sdhci: 0
SF: Detected N25Q128A with page size 256 Bytes, erase size 4 KiB, total 16 MiB

*** Warning - bad CRC, using default environment

In:    serial
Out:   serial
Err:   serial
Net:   Gem.e000b000

U-BOOT for zynq-zc702

Gem.e000b000 Waiting for PHY auto negotiation to complete.... done BOOTP broadcast 1
DHCP client bound to address 192.168.0.90
Hit any key to stop autoboot: 0
U-Boot-PetaLinux> run netboot
Gem.e000b000:7 is connected to Gem.e000b000. Reconnecting to Gem.e000b000 Gem.e000b000 Waiting for PHY auto negotiation to complete.... done Using Gem.e000b000 device
TFTP from server 192.168.0.100; our IP address is 192.168.0.90 Filename 'image.ub'.
Load address: 0x1000000
Loading: #################################################################

	 #################################################################
	 #################################################################
	 #################################################################
	 #################################################################
	 #################################################################
	 #################################################################
	 #################################################################
	 #################################################################
	 #################################################################
	 #################################################################
	 #################################################################
	 #################################################################
	 #######################################
	 2 MiB/s

done
Bytes transferred = 12964752 (c5d390 hex)
## Loading kernel from FIT Image at 01000000 ...
Using 'conf at 1' configuration
Trying 'kernel at 1' kernel subimage

     Description:  PetaLinux Kernel
     Type:         Kernel Image
     Compression:  gzip compressed
     Data Start:   0x010000f0
     Data Size:    12949283 Bytes = 12.3 MiB
     Architecture: ARM
     OS:           Linux
     Load Address: 0x10008000
     Entry Point:  0x10008000
     Hash algo:    crc32
     Hash value:   39564940

Verifying Hash Integrity ... crc32+ OK
## Loading fdt from FIT Image at 01000000 ...
Using 'conf at 1' configuration
Trying 'fdt at 1' fdt subimage

     Description:  Flattened Device Tree blob
     Type:         Flat Device Tree
     Compression:  uncompressed
     Data Start:   0x01c598f8
     Data Size:    14133 Bytes = 13.8 KiB

Architecture: ARM
Hash algo: crc32
Hash value: be457cb0
Hash algo: sha1
Hash value: 206ffdb413e297d4a143a47fa8598cee4527a63a
Verifying Hash Integrity ... crc32+ sha1+ OK
Booting using the fdt blob at 0x1c598f8
Uncompressing Kernel Image ... Error: inflate() returned -5
GUNZIP: uncompress, out-of-mem or overwrite error - must RESET board to recover resetting ...

Signature · 14 lines
Michal Simek, Ing. (M.Eng), OpenPGP -> KeyID: FE3D1F91
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel - Microblaze cpu - http://www.monstr.eu/fdt/
Maintainer of Linux kernel - Xilinx Zynq ARM architecture
Microblaze U-BOOT custodian and responsible for u-boot arm zynq platform


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 263 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20131108/6b21f45d/attachment.pgp>
author
#7 lore raw

PATCH v2 3/6 gzip: correctly bounds-check output buffer

Quoted 37 lines · Michal Simek

Hi Kees,

Quoted 20 lines · Kees Cook

The output buffer size must not be reset by the gzip decoder or there is a risk of overflowing memory during decompression.

---
 lib/gunzip.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/gunzip.c +1 -1
index 9959781..35abfb3 100644
@@ -89,13 +89,13 @@ int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
      s.avail_out = dstlen;
      do {
              r = inflate(&s, Z_FINISH);
-             if (r != Z_STREAM_END && r != Z_BUF_ERROR && stoponerr == 1) {
+             if (stoponerr == 1 && r != Z_STREAM_END &&

lib/gunzip.c int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp, · line 101

Quoted 10 lines
lib/gunzip.c +1 -1
+                 (s.avail_out == 0 || r != Z_BUF_ERROR)) {
                      printf("Error: inflate() returned %d\n", r);
                      inflateEnd(&s);
                      return -1;
              }
              s.avail_in = *lenp - offset - (int)(s.next_out - (unsigned char*)dst);
-             s.avail_out = dstlen;
      } while (r == Z_BUF_ERROR);
      *lenp = s.next_out - (unsigned char *) dst;
      inflateEnd(&s);

I have done u-boot upgrade to v2013.10 version and I see the problem with this patch when I am trying to boot my zynq image.

Quoted 1 line

After reverting this patch everything works as expected.

Eek, sorry this is causing you trouble!

Quoted 2 lines

Here is the image I am using.
http://www.monstr.eu/20131108-image.ub

Is there any way you can extract just the gzipped kernel from this image? I'm not sure how to get at it from this .ub file.

Quoted 7 lines

Below is the bootlog.

Do you have any idea what can be wrong?
[...]
Uncompressing Kernel Image ... Error: inflate() returned -5
GUNZIP: uncompress, out-of-mem or overwrite error - must RESET board to recover resetting ...

Either my change is failing to detect end-of-buffer correctly, or it _is_, in which case this has uncovered an unsafe caller of gunzip. This is after the "Uncompressing" message, so it's this caller:

        case IH_COMP_GZIP:
                printf("   Uncompressing %s ... ", type_name);
                if (gunzip(load_buf, unc_len, image_buf, &image_len) != 0) {
                        puts("GUNZIP: uncompress, out-of-mem or overwrite "
                                "error - must RESET board to recover\n");
                        if (boot_progress)
                                bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
                        return BOOTM_ERR_RESET;
                }

                *load_end = load + image_len;
                break;

If the uncompressed length of the kernel image is larger than "unc_len", then this is catching a legitimate memory overflow. This is entirely controlled by CONFIG_SYS_BOOTM_LEN. Is it possible this is set too low for your build?

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#8 lore raw

PATCH 3/6 gzip: correctly bounds-check output buffer

Quoted 38 lines · Kees Cook
Quoted 37 lines · Michal Simek

Hi Kees,

Quoted 20 lines · Kees Cook

The output buffer size must not be reset by the gzip decoder or there is a risk of overflowing memory during decompression.

---
 lib/gunzip.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/gunzip.c +1 -1
index 9959781..35abfb3 100644
@@ -89,13 +89,13 @@ int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
      s.avail_out = dstlen;
      do {
              r = inflate(&s, Z_FINISH);
-             if (r != Z_STREAM_END && r != Z_BUF_ERROR && stoponerr == 1) {
+             if (stoponerr == 1 && r != Z_STREAM_END &&

lib/gunzip.c int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp, · line 101

Quoted 10 lines
lib/gunzip.c +1 -1
+                 (s.avail_out == 0 || r != Z_BUF_ERROR)) {
                      printf("Error: inflate() returned %d\n", r);
                      inflateEnd(&s);
                      return -1;
              }
              s.avail_in = *lenp - offset - (int)(s.next_out - (unsigned char*)dst);
-             s.avail_out = dstlen;
      } while (r == Z_BUF_ERROR);
      *lenp = s.next_out - (unsigned char *) dst;
      inflateEnd(&s);

I have done u-boot upgrade to v2013.10 version and I see the problem with this patch when I am trying to boot my zynq image.

Quoted 3 lines
Quoted 1 line

After reverting this patch everything works as expected.

Eek, sorry this is causing you trouble!

no worries. Problem is on my side. Look below.

Quoted 5 lines
Quoted 2 lines

Here is the image I am using.
http://www.monstr.eu/20131108-image.ub

Is there any way you can extract just the gzipped kernel from this image? I'm not sure how to get at it from this .ub file.

Sure just run imi. Then you will get data start address and length. And you can use unzip command.

Quoted 25 lines
Quoted 7 lines

Below is the bootlog.

Do you have any idea what can be wrong?
[...]
Uncompressing Kernel Image ... Error: inflate() returned -5
GUNZIP: uncompress, out-of-mem or overwrite error - must RESET board to recover resetting ...

Either my change is failing to detect end-of-buffer correctly, or it _is_, in which case this has uncovered an unsafe caller of gunzip. This is after the "Uncompressing" message, so it's this caller:

        case IH_COMP_GZIP:
                printf("   Uncompressing %s ... ", type_name);
                if (gunzip(load_buf, unc_len, image_buf, &image_len) != 0) {
                        puts("GUNZIP: uncompress, out-of-mem or overwrite "
                                "error - must RESET board to recover\n");
                        if (boot_progress)
                                bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
                        return BOOTM_ERR_RESET;
                }

                *load_end = load + image_len;
                break;
Quoted 4 lines

If the uncompressed length of the kernel image is larger than "unc_len", then this is catching a legitimate memory overflow. This is entirely controlled by CONFIG_SYS_BOOTM_LEN. Is it possible this is set too low for your build?

Ah yes, that's the issue. My image is 14MB and have just 16MB BOOTM_LEN.

Thanks for pointing to this.
Michal

Signature · 14 lines
Michal Simek, Ing. (M.Eng), OpenPGP -> KeyID: FE3D1F91
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel - Microblaze cpu - http://www.monstr.eu/fdt/
Maintainer of Linux kernel - Xilinx Zynq ARM architecture
Microblaze U-BOOT custodian and responsible for u-boot arm zynq platform


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 263 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20131108/f50fc63a/attachment.pgp>
#9 lore raw

PATCH 3/6 gzip: correctly bounds-check output buffer

Quoted 79 lines · Michal Simek
Quoted 38 lines · Kees Cook
Quoted 37 lines · Michal Simek

Hi Kees,

Quoted 20 lines · Kees Cook

The output buffer size must not be reset by the gzip decoder or there is a risk of overflowing memory during decompression.

---
 lib/gunzip.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/gunzip.c +1 -1
index 9959781..35abfb3 100644
@@ -89,13 +89,13 @@ int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
      s.avail_out = dstlen;
      do {
              r = inflate(&s, Z_FINISH);
-             if (r != Z_STREAM_END && r != Z_BUF_ERROR && stoponerr == 1) {
+             if (stoponerr == 1 && r != Z_STREAM_END &&

lib/gunzip.c int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp, · line 101

Quoted 10 lines
lib/gunzip.c +1 -1
+                 (s.avail_out == 0 || r != Z_BUF_ERROR)) {
                      printf("Error: inflate() returned %d\n", r);
                      inflateEnd(&s);
                      return -1;
              }
              s.avail_in = *lenp - offset - (int)(s.next_out - (unsigned char*)dst);
-             s.avail_out = dstlen;
      } while (r == Z_BUF_ERROR);
      *lenp = s.next_out - (unsigned char *) dst;
      inflateEnd(&s);

I have done u-boot upgrade to v2013.10 version and I see the problem with this patch when I am trying to boot my zynq image.

Quoted 3 lines
Quoted 1 line

After reverting this patch everything works as expected.

Eek, sorry this is causing you trouble!

no worries. Problem is on my side. Look below.

Quoted 5 lines
Quoted 2 lines

Here is the image I am using.
http://www.monstr.eu/20131108-image.ub

Is there any way you can extract just the gzipped kernel from this image? I'm not sure how to get at it from this .ub file.

Sure just run imi. Then you will get data start address and length. And you can use unzip command.

Quoted 25 lines
Quoted 7 lines

Below is the bootlog.

Do you have any idea what can be wrong?
[...]
Uncompressing Kernel Image ... Error: inflate() returned -5
GUNZIP: uncompress, out-of-mem or overwrite error - must RESET board to recover resetting ...

Either my change is failing to detect end-of-buffer correctly, or it _is_, in which case this has uncovered an unsafe caller of gunzip. This is after the "Uncompressing" message, so it's this caller:

        case IH_COMP_GZIP:
                printf("   Uncompressing %s ... ", type_name);
                if (gunzip(load_buf, unc_len, image_buf, &image_len) != 0) {
                        puts("GUNZIP: uncompress, out-of-mem or overwrite "
                                "error - must RESET board to recover\n");
                        if (boot_progress)
                                bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
                        return BOOTM_ERR_RESET;
                }

                *load_end = load + image_len;
                break;
Quoted 7 lines
Quoted 4 lines

If the uncompressed length of the kernel image is larger than "unc_len", then this is catching a legitimate memory overflow. This is entirely controlled by CONFIG_SYS_BOOTM_LEN. Is it possible this is set too low for your build?

Ah yes, that's the issue. My image is 14MB and have just 16MB BOOTM_LEN.

I have read README about BOOTM_LEN and it cares just about compressed images but macro is generic enough to also handle uncompressed images and this checking should be probably done too.

Thanks,
Michal

Signature · 14 lines
Michal Simek, Ing. (M.Eng), OpenPGP -> KeyID: FE3D1F91
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel - Microblaze cpu - http://www.monstr.eu/fdt/
Maintainer of Linux kernel - Xilinx Zynq ARM architecture
Microblaze U-BOOT custodian and responsible for u-boot arm zynq platform


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 263 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20131108/cbda11c7/attachment.pgp>
author
#10 lore raw

PATCH v2 4/6 lzma: correctly bounds-check output buffer

In reply to Kees Cook

The output buffer size must be correctly passed to the lzma decoder or there is a risk of overflowing memory during decompression. Switching to the LZMA_FINISH_END mode means nothing is left in an unknown state once the buffer becomes full.

---
 lib/lzma/LzmaTools.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
lib/lzma/LzmaTools.c +6 -2
index 8d1165e11b..0aec2f9 100644
@@ -97,15 +97,19 @@ int lzmaBuffToBuffDecompress (unsigned char *outStream, SizeT *uncompressedSize,
     g_Alloc.Alloc = SzAlloc;
     g_Alloc.Free = SzFree;
 
+    /* Short-circuit early if we know the buffer can't hold the results. */
+    if (outSizeFull != (SizeT)-1 && *uncompressedSize < outSizeFull)
+        return SZ_ERROR_OUTPUT_EOF;
+
     /* Decompress */
-    outProcessed = outSizeFull;
+    outProcessed = *uncompressedSize;
 
     WATCHDOG_RESET();
 
     res = LzmaDecode(
         outStream, &outProcessed,
         inStream + LZMA_DATA_OFFSET, &compressedSize,
-        inStream, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &state, &g_Alloc);
+        inStream, LZMA_PROPS_SIZE, LZMA_FINISH_END, &state, &g_Alloc);
     *uncompressedSize = outProcessed;
     if (res != SZ_OK)  {
         return res;
Signature · 1 line
1.7.9.5
author
#11 lore raw

PATCH v2 5/6 lzo: correctly bounds-check output buffer

In reply to Kees Cook

This checks the size of the output buffer and fails if it was going to overflow the buffer during lzo decompression.

---
 lib/lzo/lzo1x_decompress.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
lib/lzo/lzo1x_decompress.c +7 -1
index e6ff708..35f3793 100644
@@ -68,13 +68,14 @@ int lzop_decompress(const unsigned char *src, size_t src_len,
 	unsigned char *start = dst;
 	const unsigned char *send = src + src_len;
 	u32 slen, dlen;
-	size_t tmp;
+	size_t tmp, remaining;
 	int r;
 
 	src = parse_header(src);
 	if (!src)
 		return LZO_E_ERROR;
 
+	remaining = *dst_len;
 	while (src < send) {
 		/* read uncompressed block size */
 		dlen = get_unaligned_be32(src);
@@ -93,6 +94,10 @@ int lzop_decompress(const unsigned char *src, size_t src_len,
 		if (slen <= 0 || slen > dlen)
 			return LZO_E_ERROR;
 
+		/* abort if buffer ran out of room */
+		if (dlen > remaining)
+			return LZO_E_OUTPUT_OVERRUN;
+
 		/* decompress */
 		tmp = dlen;
 		r = lzo1x_decompress_safe((u8 *) src, slen, dst, &tmp);
@@ -105,6 +110,7 @@ int lzop_decompress(const unsigned char *src, size_t src_len,
 
 		src += slen;
 		dst += dlen;
+		remaining -= dlen;
 	}
 
 	return LZO_E_INPUT_OVERRUN;
Signature · 1 line
1.7.9.5
author
#12 lore raw

PATCH v2 6/6 bootm: allow correct bounds-check of destination

In reply to Kees Cook

While nothing presently examines the destination size, it should at least be correct so that future users of sys_mapmem() will not be surprised. Without this, it might be possible to overflow memory.

---
 common/cmd_bootm.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/cmd_bootm.c +1 -1
index 046e22f..0f67112 100644
@@ -368,7 +368,7 @@ static int bootm_load_os(bootm_headers_t *images, unsigned long *load_end,
 
 	const char *type_name = genimg_get_type_name(os.type);
 
-	load_buf = map_sysmem(load, image_len);
+	load_buf = map_sysmem(load, unc_len);
 	image_buf = map_sysmem(image_start, image_len);
 	switch (comp) {
 	case IH_COMP_NONE:
Signature · 1 line
1.7.9.5
author
#13 lore raw

v2 cover handle compression buffer overflows

In reply to Kees Cook

Hi,

Can someone commit this series? It's been fully acked now...

Thanks,

-Kees

Quoted 38 lines · Kees Cook

v2: added acks, various suggested cleanups

This series fixes gzip, lzma, and lzo to not overflow when writing to output buffers. Without this, it might be possible for untrusted compressed input to overflow the buffers used to hold the decompressed image.

To catch these conditions, I added a series of compression tests available in the sandbox build. Without the fixes in patches 3, 4, and 5, the overflows are visible.

Thanks,

-Kees

Kees Cook (6):
sandbox: add compression tests
documentation: add more compression configs
gzip: correctly bounds-check output buffer
lzma: correctly bounds-check output buffer
lzo: correctly bounds-check output buffer
bootm: allow correct bounds-check of destination

 README                     |    9 ++
 common/cmd_bootm.c         |    2 +-
 include/configs/sandbox.h  |    5 +
 lib/gunzip.c               |    4 +-
 lib/lzma/LzmaTools.c       |    8 +-
 lib/lzo/lzo1x_decompress.c |    8 +-
 test/Makefile              |    1 +
 test/compression.c         |  335 ++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 366 insertions(+), 6 deletions(-)
 create mode 100644 test/compression.c

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

Signature · 2 lines
Kees Cook
Chrome OS Security
#14 lore raw

v2 cover handle compression buffer overflows

Hi Kees,

Quoted 3 lines · Kees Cook

Hi,

Can someone commit this series? It's been fully acked now...

I'm happy to pull these in for Tom.

I see a few warnings when I run buildman:

$ ./tools/buildman/buildman -b us-kees sandbox -se
Summary of 7 commits for 1 boards (1 thread, 32 jobs per thread)
01: omap5: Correct include order, drop CONFIG_SYS_PROMPT define
02: sandbox: add compression tests
   sandbox: +   sandbox
+cmd_bootm.c: In function ?bootm_load_os?:
+cmd_bootm.c:443:11: warning: passing argument 4 of ?lzop_decompress?
from incompatible pointer type [enabled by default]
+/usr/local/google/c/cosarm/src/third_party/u-boot/us-kees/.bm-work/00/include/linux/lzo.h:31:5:
note: expected ?size_t *? but argument is of type ?uint *?
+cmd_ximg.c: In function ?do_imgextract?:
+cmd_ximg.c:225:6: warning: cast to pointer from integer of different
size [-Wint-to-pointer-cast]
+cmd_ximg.c:225:14: warning: ?hdr? may be used uninitialized in this
function [-Wuninitialized]
03: documentation: add more compression configs
04: gzip: correctly bounds-check output buffer
05: lzma: correctly bounds-check output buffer
06: lzo: correctly bounds-check output buffer
07: bootm: allow correct bounds-check of destination

I believe these are pre-existing, but didn't happen for sandbox since it was not enabling these options, but could you please create a patch to fix these that we can apply first?

To build for sandbox: 'make sandbox_config; make'

Regards,
Simon

Quoted 50 lines

Thanks,

-Kees

Quoted 34 lines · Kees Cook

v2: added acks, various suggested cleanups

This series fixes gzip, lzma, and lzo to not overflow when writing to output buffers. Without this, it might be possible for untrusted compressed input to overflow the buffers used to hold the decompressed image.

To catch these conditions, I added a series of compression tests available in the sandbox build. Without the fixes in patches 3, 4, and 5, the overflows are visible.

Thanks,

-Kees

Kees Cook (6):
sandbox: add compression tests
documentation: add more compression configs
gzip: correctly bounds-check output buffer
lzma: correctly bounds-check output buffer
lzo: correctly bounds-check output buffer
bootm: allow correct bounds-check of destination

 README                     |    9 ++
 common/cmd_bootm.c         |    2 +-
 include/configs/sandbox.h  |    5 +
 lib/gunzip.c               |    4 +-
 lib/lzma/LzmaTools.c       |    8 +-
 lib/lzo/lzo1x_decompress.c |    8 +-
 test/Makefile              |    1 +
 test/compression.c         |  335 ++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 366 insertions(+), 6 deletions(-)
 create mode 100644 test/compression.c
Quoted 4 lines

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

--
Kees Cook
Chrome OS Security