Skip to contentSkip to conversation

ubifs: fix out-of-bounds write in LPT commit padding

PATCH In review Jenny Guanni Qu 2 participants 3 messages Mar 13 – Mar 14

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ubifs: fix out-of-bounds write in LPT commit padding

In write_cnodes(), when writing LPT data to a LEB, the code pads the write buffer to min_io_size alignment using:

    memset(buf + offs, 0xff, alen - wlen)

where wlen = offs - from and alen = ALIGN(wlen, min_io_size). The buffer (c->lpt_buf) is allocated with size c->leb_size. When offs is near leb_size and from is non-zero, the alignment padding can push the memset past the end of the buffer.

For example, with leb_size=4096, offs=4000, from=3584, min_io_size=1024:
wlen=416, alen=1024, padding=608 bytes at offset 4000 writes to offsets 4000..4607, 512 bytes past the 4096-byte buffer

Clamp the padding size to not exceed the buffer boundary in all 4 instances of this pattern.

The OOB write was confirmed with KASAN using a test module that reproduces the arithmetic with matching buffer and alignment values.

Fixes 1e51764a3c2a ("UBIFS: add new flash file system")
---
 fs/ubifs/lpt_commit.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/ubifs/lpt_commit.c +4 -4
index 07351fdce722..77a7a2cd418f 100644
@@ -402,7 +402,7 @@ static int write_cnodes(struct ubifs_info *c)
 			wlen = offs - from;
 			if (wlen) {
 				alen = ALIGN(wlen, c->min_io_size);
-				memset(buf + offs, 0xff, alen - wlen);
+				memset(buf + offs, 0xff, min_t(int, alen - wlen, c->leb_size - offs));
 				err = ubifs_leb_write(c, lnum, buf + from, from,
 						       alen);
 				if (err)
@@ -461,7 +461,7 @@ static int write_cnodes(struct ubifs_info *c)
 		if (offs + c->lsave_sz > c->leb_size) {
 			wlen = offs - from;
 			alen = ALIGN(wlen, c->min_io_size);
-			memset(buf + offs, 0xff, alen - wlen);
+			memset(buf + offs, 0xff, min_t(int, alen - wlen, c->leb_size - offs));
 			err = ubifs_leb_write(c, lnum, buf + from, from, alen);
 			if (err)
 				return err;
@@ -487,7 +487,7 @@ static int write_cnodes(struct ubifs_info *c)
 		if (offs + c->ltab_sz > c->leb_size) {
 			wlen = offs - from;
 			alen = ALIGN(wlen, c->min_io_size);
-			memset(buf + offs, 0xff, alen - wlen);
+			memset(buf + offs, 0xff, min_t(int, alen - wlen, c->leb_size - offs));
 			err = ubifs_leb_write(c, lnum, buf + from, from, alen);
 			if (err)
 				return err;
@@ -510,7 +510,7 @@ static int write_cnodes(struct ubifs_info *c)
 	/* Write remaining data in buffer */
 	wlen = offs - from;
 	alen = ALIGN(wlen, c->min_io_size);
-	memset(buf + offs, 0xff, alen - wlen);
+	memset(buf + offs, 0xff, min_t(int, alen - wlen, c->leb_size - offs));
 	err = ubifs_leb_write(c, lnum, buf + from, from, alen);
 	if (err)
 		return err;
Signature · 6 lines
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
reviewer
#1 lore raw
Quoted 5 lines · Jenny Guanni Qu

In write_cnodes(), when writing LPT data to a LEB, the code pads the write buffer to min_io_size alignment using:

     memset(buf + offs, 0xff, alen - wlen)
Quoted 8 lines

where wlen = offs - from and alen = ALIGN(wlen, min_io_size). The buffer (c->lpt_buf) is allocated with size c->leb_size. When offs is near leb_size and from is non-zero, the alignment padding can push the memset past the end of the buffer.

For example, with leb_size=4096, offs=4000, from=3584, min_io_size=1024:
wlen=416, alen=1024, padding=608 bytes at offset 4000 writes to offsets 4000..4607, 512 bytes past the 4096-byte buffer

Hi, Guanni
How does 'from' become '3584'(which is not aligned with min_io_size[1024])?
As far as I know, the 'from' comes from 'c->nhead_offs', which is always aligned with min_io_size.

Quoted 49 lines

Clamp the padding size to not exceed the buffer boundary in all 4 instances of this pattern.

The OOB write was confirmed with KASAN using a test module that reproduces the arithmetic with matching buffer and alignment values.

Fixes 1e51764a3c2a ("UBIFS: add new flash file system")
---
  fs/ubifs/lpt_commit.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
fs/ubifs/lpt_commit.c +3 -3
index 07351fdce722..77a7a2cd418f 100644
@@ -402,7 +402,7 @@ static int write_cnodes(struct ubifs_info *c)
  			wlen = offs - from;
  			if (wlen) {
  				alen = ALIGN(wlen, c->min_io_size);
-				memset(buf + offs, 0xff, alen - wlen);
+				memset(buf + offs, 0xff, min_t(int, alen - wlen, c->leb_size - offs));
  				err = ubifs_leb_write(c, lnum, buf + from, from,
  						       alen);
  				if (err)
@@ -461,7 +461,7 @@ static int write_cnodes(struct ubifs_info *c)
  		if (offs + c->lsave_sz > c->leb_size) {
  			wlen = offs - from;
  			alen = ALIGN(wlen, c->min_io_size);
-			memset(buf + offs, 0xff, alen - wlen);
+			memset(buf + offs, 0xff, min_t(int, alen - wlen, c->leb_size - offs));
  			err = ubifs_leb_write(c, lnum, buf + from, from, alen);
  			if (err)
  				return err;
@@ -487,7 +487,7 @@ static int write_cnodes(struct ubifs_info *c)
  		if (offs + c->ltab_sz > c->leb_size) {
  			wlen = offs - from;
  			alen = ALIGN(wlen, c->min_io_size);
-			memset(buf + offs, 0xff, alen - wlen);
+			memset(buf + offs, 0xff, min_t(int, alen - wlen, c->leb_size - offs));
  			err = ubifs_leb_write(c, lnum, buf + from, from, alen);
  			if (err)
  				return err;

fs/ubifs/lpt_commit.c static int write_cnodes(struct ubifs_info *c) · line 516

Quoted 8 lines
fs/ubifs/lpt_commit.c +1 -1
@@ -510,7 +510,7 @@ static int write_cnodes(struct ubifs_info *c)
  	/* Write remaining data in buffer */
  	wlen = offs - from;
  	alen = ALIGN(wlen, c->min_io_size);
-	memset(buf + offs, 0xff, alen - wlen);
+	memset(buf + offs, 0xff, min_t(int, alen - wlen, c->leb_size - offs));
  	err = ubifs_leb_write(c, lnum, buf + from, from, alen);
  	if (err)
  		return err;

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

author
#2 lore raw

Hi Zhihao,

You're right. 'from' is always either 0 or c->nhead_offs, which is ALIGN(offs, c->min_io_size) (line 524). Since leb_size is also a multiple of min_io_size, from + alen can never exceed leb_size.

My validation used artificial values that don't occur in real execution. I'll withdraw this patch.

Apologies for the noise.

Jenny

Quoted 68 lines · Zhihao Cheng
Quoted 5 lines · Jenny Guanni Qu

In write_cnodes(), when writing LPT data to a LEB, the code pads the write buffer to min_io_size alignment using:

     memset(buf + offs, 0xff, alen - wlen)
Quoted 8 lines

where wlen = offs - from and alen = ALIGN(wlen, min_io_size). The buffer (c->lpt_buf) is allocated with size c->leb_size. When offs is near leb_size and from is non-zero, the alignment padding can push the memset past the end of the buffer.

For example, with leb_size=4096, offs=4000, from=3584, min_io_size=1024:
wlen=416, alen=1024, padding=608 bytes at offset 4000 writes to offsets 4000..4607, 512 bytes past the 4096-byte buffer

Hi, Guanni
How does 'from' become '3584'(which is not aligned with min_io_size[1024])? As far as I know, the 'from' comes from 'c->nhead_offs', which is always aligned with min_io_size.

Quoted 49 lines

Clamp the padding size to not exceed the buffer boundary in all 4 instances of this pattern.

The OOB write was confirmed with KASAN using a test module that reproduces the arithmetic with matching buffer and alignment values.

Fixes 1e51764a3c2a ("UBIFS: add new flash file system")
---
  fs/ubifs/lpt_commit.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
fs/ubifs/lpt_commit.c +3 -3
index 07351fdce722..77a7a2cd418f 100644
@@ -402,7 +402,7 @@ static int write_cnodes(struct ubifs_info *c)
                      wlen = offs - from;
                      if (wlen) {
                              alen = ALIGN(wlen, c->min_io_size);
-                             memset(buf + offs, 0xff, alen - wlen);
+                             memset(buf + offs, 0xff, min_t(int, alen - wlen, c->leb_size - offs));
                              err = ubifs_leb_write(c, lnum, buf + from, from,
                                                     alen);
                              if (err)
@@ -461,7 +461,7 @@ static int write_cnodes(struct ubifs_info *c)
              if (offs + c->lsave_sz > c->leb_size) {
                      wlen = offs - from;
                      alen = ALIGN(wlen, c->min_io_size);
-                     memset(buf + offs, 0xff, alen - wlen);
+                     memset(buf + offs, 0xff, min_t(int, alen - wlen, c->leb_size - offs));
                      err = ubifs_leb_write(c, lnum, buf + from, from, alen);
                      if (err)
                              return err;
@@ -487,7 +487,7 @@ static int write_cnodes(struct ubifs_info *c)
              if (offs + c->ltab_sz > c->leb_size) {
                      wlen = offs - from;
                      alen = ALIGN(wlen, c->min_io_size);
-                     memset(buf + offs, 0xff, alen - wlen);
+                     memset(buf + offs, 0xff, min_t(int, alen - wlen, c->leb_size - offs));
                      err = ubifs_leb_write(c, lnum, buf + from, from, alen);
                      if (err)
                              return err;
@@ -510,7 +510,7 @@ static int write_cnodes(struct ubifs_info *c)
      /* Write remaining data in buffer */
Quoted 9 lines

fs/ubifs/lpt_commit.c static int write_cnodes(struct ubifs_info *c) · line 467

Quoted 8 lines
fs/ubifs/lpt_commit.c +1 -1
      wlen = offs - from;
      alen = ALIGN(wlen, c->min_io_size);
-     memset(buf + offs, 0xff, alen - wlen);
+     memset(buf + offs, 0xff, min_t(int, alen - wlen, c->leb_size - offs));
      err = ubifs_leb_write(c, lnum, buf + from, from, alen);
      if (err)
              return err;

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/