[PATCH] uclinux: fix gzip header parsing in binfmt_flat.c



There are off-by-one errors in decompress_exec() when calculating the length of
optional "original file name" and "comment" fields: the "ret" index is not
incremented when terminating '\0' character is reached. The check of the buffer
overflow (after an "extra-field" length was taken into account) is also fixed.

Signed-off-by: Volodymyr G Lukiianyk <volodymyrgl@xxxxxxxxx>
---

I've encountered this off-by-one error when tried to reuse gzip-header-parsing
part of the decompress_exec() function. There was an "original file name" field
in the payload (with miscalculated length) and zlib_inflate() returned
Z_DATA_ERROR. But after the fix similar to this one all worked fine.

WARNING: the proposed patch wasn't properly tested.
diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
index dfc0197..ccb781a 100644
--- a/fs/binfmt_flat.c
+++ b/fs/binfmt_flat.c
@@ -229,13 +229,13 @@ static int decompress_exec(
 ret = 10;
 if (buf[3] & EXTRA_FIELD) {
 ret += 2 + buf[10] + (buf[11] << 8);
- if (unlikely(LBUFSIZE == ret)) {
+ if (unlikely(LBUFSIZE <= ret)) {
DBG_FLT("binfmt_flat: buffer overflow (EXTRA)?\n");
 goto out_free_buf;
 }
 }
 if (buf[3] & ORIG_NAME) {
- for (; ret < LBUFSIZE && (buf[ret] != 0); ret++)
+ while (ret < LBUFSIZE && buf[ret++] != 0)
;
 if (unlikely(LBUFSIZE == ret)) {
 DBG_FLT("binfmt_flat: buffer overflow (ORIG_NAME)?\n");
@@ -243,7 +243,7 @@ static int decompress_exec(
 }
 }
 if (buf[3] & COMMENT) {
- for (; ret < LBUFSIZE && (buf[ret] != 0); ret++)
+ while (ret < LBUFSIZE && buf[ret++] != 0)
;
 if (unlikely(LBUFSIZE == ret)) {
 DBG_FLT("binfmt_flat: buffer overflow (COMMENT)?\n");


Relevant Pages

  • Re: _stprintf
    ... It won't overflow the buffer, ... StringCchPrintf will format the string, which is one character plus a terminal null ...
    (microsoft.public.vc.mfc)
  • Re: [patch 2/2] track and print last unloaded module in the oops trace
    ... Why use sprintf? ... If a module name contains the % character we could ... overflow the buffer. ...
    (Linux-Kernel)
  • Re: [PATCH] uclinux: fix gzip header parsing in binfmt_flat.c
    ... The check of the buffer ... overflow (after an "extra-field" length was taken into account) is also fixed. ... ret = 10; ...
    (Linux-Kernel)
  • renee.rtf.xaa
    ... renee is RTF parser/macro processor I wrote. ... Character Stream\ ... Write Output Buffer to Files\ ...
    (comp.lang.tcl)
  • Re: input & output in assembly
    ... [As you've not specified OS or assembler, ... using individual character I/O and handling the rest yourself in your ... it finds in that string, ... ENTER key is pressed (maximum buffer size: ...
    (comp.lang.asm.x86)

Loading