Skip to contentSkip to conversation

ubifs: Fix lockup/crash when reading files

PATCH In review Pali Rohár 3 participants 7 messages May 17, 2022 – Jun 3, 2022 UBI file system (UBIFS)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ubifs: Fix lockup/crash when reading files

Commit b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size") added optimization to do not read more bytes than it is really needed. But this commit introduced incorrect handling of the hole at the end of file. This logic cause U-Boot to crash or lockup when trying to read from the ubifs filesystem.

When read_block() call returns -ENOENT error (not an error, but the hole) then dn-> structure is not filled and contain garbage. So using of dn->size for memcpy() argument cause that U-Boot tries to copy unspecified amount of bytes from possible unmapped memory. Which randomly cause lockup of P2020 CPU.

Fix this issue by copying UBIFS_BLOCK_SIZE bytes from read buffer when dn->size is not available. UBIFS_BLOCK_SIZE is the size of the buffer itself and read_block() fills buffer by zeros when it returns -ENOENT.

This patch fixes ubifsload on P2020.

Fixes b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size")

---

Stefan, could you please look at this patch? Mentioned commit was introduced by you more than 11 years ago. I'm surprised that nobody hit this issue yet, but it looks like older U-Boot versions somehow filled small garbage number into dn->size and did not cause U-Boot lockup/crash.

---
 fs/ubifs/ubifs.c | 2 ++
 1 file changed, 2 insertions(+)
fs/ubifs/ubifs.c +2 -0
index d6be5c947d7e..d3026e310168 100644
@@ -771,40 +771,42 @@ static int do_readpage(struct ubifs_info *c, struct inode *inode,
 				buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
 				if (!buff) {
 					printf("%s: Error, malloc fails!\n",
 					       __func__);
 					err = -ENOMEM;
 					break;
 				}
 
 				/* Read block-size into temp buffer */
 				ret = read_block(inode, buff, block, dn);
 				if (ret) {
 					err = ret;
 					if (err != -ENOENT) {
 						free(buff);
 						break;
 					}
 				}
 
 				if (last_block_size)
 					dlen = last_block_size;
+				else if (ret)
+					dlen = UBIFS_BLOCK_SIZE;
 				else
 					dlen = le32_to_cpu(dn->size);
 
 				/* Now copy required size back to dest */
 				memcpy(addr, buff, dlen);
 
 				free(buff);
 			} else {
 				ret = read_block(inode, addr, block, dn);
 				if (ret) {
 					err = ret;
 					if (err != -ENOENT)
 						break;
 				}
 			}
 		}
 		if (++i >= UBIFS_BLOCKS_PER_PAGE)
 			break;
 		block += 1;
 		addr += UBIFS_BLOCK_SIZE;
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Pali Rohár

Commit b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size") added optimization to do not read more bytes than it is really needed. But this commit introduced incorrect handling of the hole at the end of file. This logic cause U-Boot to crash or lockup when trying to read from the ubifs filesystem.

When read_block() call returns -ENOENT error (not an error, but the hole) then dn-> structure is not filled and contain garbage. So using of dn->size for memcpy() argument cause that U-Boot tries to copy unspecified amount of bytes from possible unmapped memory. Which randomly cause lockup of P2020 CPU.

Quoted 8 lines

Fix this issue by copying UBIFS_BLOCK_SIZE bytes from read buffer when dn->size is not available. UBIFS_BLOCK_SIZE is the size of the buffer itself and read_block() fills buffer by zeros when it returns -ENOENT.

This patch fixes ubifsload on P2020.

Fixes b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size")
Quoted 7 lines

---

Stefan, could you please look at this patch? Mentioned commit was introduced by you more than 11 years ago. I'm surprised that nobody hit this issue yet, but it looks like older U-Boot versions somehow filled small garbage number into dn->size and did not cause U-Boot lockup/crash.

So long ago. I don't really remember the details. IIRC, I introduced the UBIFS support for some MIPS based board at that time. My best assumption is, that UBIFS is rarely used in U-Boot in general.

Thanks,
Stefan

Quoted 41 lines
---
  fs/ubifs/ubifs.c | 2 ++
  1 file changed, 2 insertions(+)
fs/ubifs/ubifs.c +2 -0
index d6be5c947d7e..d3026e310168 100644
@@ -771,40 +771,42 @@ static int do_readpage(struct ubifs_info *c, struct inode *inode,
  				buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
  				if (!buff) {
  					printf("%s: Error, malloc fails!\n",
  					       __func__);
  					err = -ENOMEM;
  					break;
  				}
 
  				/* Read block-size into temp buffer */
  				ret = read_block(inode, buff, block, dn);
  				if (ret) {
  					err = ret;
  					if (err != -ENOENT) {
  						free(buff);
  						break;
  					}
  				}
 
  				if (last_block_size)
  					dlen = last_block_size;
+				else if (ret)
+					dlen = UBIFS_BLOCK_SIZE;
  				else
  					dlen = le32_to_cpu(dn->size);
 
  				/* Now copy required size back to dest */
  				memcpy(addr, buff, dlen);
 
  				free(buff);
  			} else {
  				ret = read_block(inode, addr, block, dn);
  				if (ret) {

fs/ubifs/ubifs.c static int do_readpage(struct ubifs_info *c, struct inode *inode, · line 812

Quoted 10 lines
fs/ubifs/ubifs.c +0 -0
  					err = ret;
  					if (err != -ENOENT)
  						break;
  				}
  			}
  		}
  		if (++i >= UBIFS_BLOCKS_PER_PAGE)
  			break;
  		block += 1;
  		addr += UBIFS_BLOCK_SIZE;

Viele Grüße,
Stefan Roese

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
#2 lore raw
Quoted 32 lines · On Thursday 19 May 2022 07:01:19 Stefan Roese wrote:
Quoted 12 lines · Pali Rohár

Commit b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size") added optimization to do not read more bytes than it is really needed. But this commit introduced incorrect handling of the hole at the end of file. This logic cause U-Boot to crash or lockup when trying to read from the ubifs filesystem.

When read_block() call returns -ENOENT error (not an error, but the hole) then dn-> structure is not filled and contain garbage. So using of dn->size for memcpy() argument cause that U-Boot tries to copy unspecified amount of bytes from possible unmapped memory. Which randomly cause lockup of P2020 CPU.

Quoted 8 lines

Fix this issue by copying UBIFS_BLOCK_SIZE bytes from read buffer when dn->size is not available. UBIFS_BLOCK_SIZE is the size of the buffer itself and read_block() fills buffer by zeros when it returns -ENOENT.

This patch fixes ubifsload on P2020.

Fixes b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size")
Quoted 7 lines

---

Stefan, could you please look at this patch? Mentioned commit was introduced by you more than 11 years ago. I'm surprised that nobody hit this issue yet, but it looks like older U-Boot versions somehow filled small garbage number into dn->size and did not cause U-Boot lockup/crash.

Quoted 3 lines

So long ago. I don't really remember the details. IIRC, I introduced the UBIFS support for some MIPS based board at that time. My best assumption is, that UBIFS is rarely used in U-Boot in general.

It is used on Turris 1.x (powerpc).

Quoted 58 lines

Thanks,
Stefan

Quoted 37 lines
---
  fs/ubifs/ubifs.c | 2 ++
  1 file changed, 2 insertions(+)
fs/ubifs/ubifs.c +2 -0
index d6be5c947d7e..d3026e310168 100644
@@ -771,40 +771,42 @@ static int do_readpage(struct ubifs_info *c, struct inode *inode,
  				buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
  				if (!buff) {
  					printf("%s: Error, malloc fails!\n",
  					       __func__);
  					err = -ENOMEM;
  					break;
  				}
  				/* Read block-size into temp buffer */
  				ret = read_block(inode, buff, block, dn);
  				if (ret) {
  					err = ret;
  					if (err != -ENOENT) {
  						free(buff);
  						break;
  					}
  				}
  				if (last_block_size)
  					dlen = last_block_size;
+				else if (ret)
+					dlen = UBIFS_BLOCK_SIZE;
  				else
  					dlen = le32_to_cpu(dn->size);
  				/* Now copy required size back to dest */
  				memcpy(addr, buff, dlen);
  				free(buff);
  			} else {
  				ret = read_block(inode, addr, block, dn);
  				if (ret) {

fs/ubifs/ubifs.c static int do_readpage(struct ubifs_info *c, struct inode *inode, · line 808

Quoted 10 lines
fs/ubifs/ubifs.c +0 -0
  					err = ret;
  					if (err != -ENOENT)
  						break;
  				}
  			}
  		}
  		if (++i >= UBIFS_BLOCKS_PER_PAGE)
  			break;
  		block += 1;
  		addr += UBIFS_BLOCK_SIZE;

Viele Grüße,
Stefan Roese

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
#3 lore raw
Quoted 18 lines · On Thursday 19 May 2022 10:04:31 Pali Rohár wrote:
Quoted 17 lines · On Thursday 19 May 2022 07:01:19 Stefan Roese wrote:
Quoted 16 lines · Pali Rohár

Commit b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size") added optimization to do not read more bytes than it is really needed. But this commit introduced incorrect handling of the hole at the end of file. This logic cause U-Boot to crash or lockup when trying to read from the ubifs filesystem.

When read_block() call returns -ENOENT error (not an error, but the hole) then dn-> structure is not filled and contain garbage. So using of dn->size for memcpy() argument cause that U-Boot tries to copy unspecified amount of bytes from possible unmapped memory. Which randomly cause lockup of P2020 CPU.

Fix this issue by copying UBIFS_BLOCK_SIZE bytes from read buffer when dn->size is not available. UBIFS_BLOCK_SIZE is the size of the buffer itself and read_block() fills buffer by zeros when it returns -ENOENT.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

This patch fixes ubifsload on P2020.

Fixes b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size")

Anyway, who is maintainer of fs / ubifs code and can take this bugfix patch?

Quoted 73 lines
Quoted 9 lines
Quoted 8 lines

---

Stefan, could you please look at this patch? Mentioned commit was introduced by you more than 11 years ago. I'm surprised that nobody hit this issue yet, but it looks like older U-Boot versions somehow filled small garbage number into dn->size and did not cause U-Boot lockup/crash.

Quoted 3 lines

So long ago. I don't really remember the details. IIRC, I introduced the UBIFS support for some MIPS based board at that time. My best assumption is, that UBIFS is rarely used in U-Boot in general.

It is used on Turris 1.x (powerpc).

Quoted 58 lines

Thanks,
Stefan

Quoted 37 lines
---
  fs/ubifs/ubifs.c | 2 ++
  1 file changed, 2 insertions(+)
fs/ubifs/ubifs.c +2 -0
index d6be5c947d7e..d3026e310168 100644
@@ -771,40 +771,42 @@ static int do_readpage(struct ubifs_info *c, struct inode *inode,
  				buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
  				if (!buff) {
  					printf("%s: Error, malloc fails!\n",
  					       __func__);
  					err = -ENOMEM;
  					break;
  				}
  				/* Read block-size into temp buffer */
  				ret = read_block(inode, buff, block, dn);
  				if (ret) {
  					err = ret;
  					if (err != -ENOENT) {
  						free(buff);
  						break;
  					}
  				}
  				if (last_block_size)
  					dlen = last_block_size;
+				else if (ret)
+					dlen = UBIFS_BLOCK_SIZE;
  				else
  					dlen = le32_to_cpu(dn->size);
  				/* Now copy required size back to dest */
  				memcpy(addr, buff, dlen);
  				free(buff);
  			} else {
  				ret = read_block(inode, addr, block, dn);
  				if (ret) {

fs/ubifs/ubifs.c static int do_readpage(struct ubifs_info *c, struct inode *inode, · line 808

Quoted 10 lines
fs/ubifs/ubifs.c +0 -0
  					err = ret;
  					if (err != -ENOENT)
  						break;
  				}
  			}
  		}
  		if (++i >= UBIFS_BLOCKS_PER_PAGE)
  			break;
  		block += 1;
  		addr += UBIFS_BLOCK_SIZE;

Viele Grüße,
Stefan Roese

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
#4 lore raw

Added Tom to Cc...

Quoted 19 lines · Pali Rohár
Quoted 18 lines · On Thursday 19 May 2022 10:04:31 Pali Rohár wrote:
Quoted 17 lines · On Thursday 19 May 2022 07:01:19 Stefan Roese wrote:
Quoted 16 lines · Pali Rohár

Commit b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size") added optimization to do not read more bytes than it is really needed. But this commit introduced incorrect handling of the hole at the end of file. This logic cause U-Boot to crash or lockup when trying to read from the ubifs filesystem.

When read_block() call returns -ENOENT error (not an error, but the hole) then dn-> structure is not filled and contain garbage. So using of dn->size for memcpy() argument cause that U-Boot tries to copy unspecified amount of bytes from possible unmapped memory. Which randomly cause lockup of P2020 CPU.

Fix this issue by copying UBIFS_BLOCK_SIZE bytes from read buffer when dn->size is not available. UBIFS_BLOCK_SIZE is the size of the buffer itself and read_block() fills buffer by zeros when it returns -ENOENT.

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

This patch fixes ubifsload on P2020.

Fixes b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size")

Anyway, who is maintainer of fs / ubifs code and can take this bugfix patch?

Tom, could you please pick this patch up? I've seen that you've already assigned it to yourself in patchwork:

http://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

Thanks,
Stefan

Quoted 69 lines
Quoted 69 lines
Quoted 9 lines
Quoted 8 lines

---

Stefan, could you please look at this patch? Mentioned commit was introduced by you more than 11 years ago. I'm surprised that nobody hit this issue yet, but it looks like older U-Boot versions somehow filled small garbage number into dn->size and did not cause U-Boot lockup/crash.

Quoted 3 lines

So long ago. I don't really remember the details. IIRC, I introduced the UBIFS support for some MIPS based board at that time. My best assumption is, that UBIFS is rarely used in U-Boot in general.

It is used on Turris 1.x (powerpc).

Quoted 54 lines

Thanks,
Stefan

Quoted 37 lines
---
   fs/ubifs/ubifs.c | 2 ++
   1 file changed, 2 insertions(+)
fs/ubifs/ubifs.c +2 -0
index d6be5c947d7e..d3026e310168 100644
@@ -771,40 +771,42 @@ static int do_readpage(struct ubifs_info *c, struct inode *inode,
   				buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
   				if (!buff) {
   					printf("%s: Error, malloc fails!\n",
   					       __func__);
   					err = -ENOMEM;
   					break;
   				}
   				/* Read block-size into temp buffer */
   				ret = read_block(inode, buff, block, dn);
   				if (ret) {
   					err = ret;
   					if (err != -ENOENT) {
   						free(buff);
   						break;
   					}
   				}
   				if (last_block_size)
   					dlen = last_block_size;
+				else if (ret)
+					dlen = UBIFS_BLOCK_SIZE;
   				else
   					dlen = le32_to_cpu(dn->size);
   				/* Now copy required size back to dest */
   				memcpy(addr, buff, dlen);
   				free(buff);
   			} else {
   				ret = read_block(inode, addr, block, dn);
   				if (ret) {

fs/ubifs/ubifs.c static int do_readpage(struct ubifs_info *c, struct inode *inode, · line 808

Quoted 10 lines
fs/ubifs/ubifs.c +0 -0
   					err = ret;
   					if (err != -ENOENT)
   						break;
   				}
   			}
   		}
   		if (++i >= UBIFS_BLOCKS_PER_PAGE)
   			break;
   		block += 1;
   		addr += UBIFS_BLOCK_SIZE;

Viele Grüße,
Stefan Roese

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]

Viele Grüße,
Stefan Roese

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: [email protected]
#5 lore raw
Quoted 27 lines · Stefan Roese

Added Tom to Cc...

Quoted 24 lines · Pali Rohár
Quoted 23 lines · On Thursday 19 May 2022 10:04:31 Pali Rohár wrote:
Quoted 22 lines · On Thursday 19 May 2022 07:01:19 Stefan Roese wrote:
Quoted 20 lines · Pali Rohár

Commit b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size") added optimization to do not read more bytes than it is really needed. But this commit introduced incorrect handling of the hole at the end of file. This logic cause U-Boot to crash or lockup when trying to read from the ubifs filesystem.

When read_block() call returns -ENOENT error (not an error, but the hole) then dn-> structure is not filled and contain garbage. So using of dn->size for memcpy() argument cause that U-Boot tries to copy unspecified amount of bytes from possible unmapped memory. Which randomly cause lockup of P2020 CPU.

Fix this issue by copying UBIFS_BLOCK_SIZE bytes from read buffer when dn->size is not available. UBIFS_BLOCK_SIZE is the size of the buffer itself and read_block() fills buffer by zeros when it returns -ENOENT.

This patch fixes ubifsload on P2020.

Fixes b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size")
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

Anyway, who is maintainer of fs / ubifs code and can take this bugfix patch?

Tom, could you please pick this patch up? I've seen that you've already assigned it to yourself in patchwork:

http://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

I've put it in my queue, thanks.

Signature · 1 line
Tom
#6 lore raw
In reply to Pali Rohár
Quoted 16 lines · Pali Rohár

Commit b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size") added optimization to do not read more bytes than it is really needed. But this commit introduced incorrect handling of the hole at the end of file. This logic cause U-Boot to crash or lockup when trying to read from the ubifs filesystem.

When read_block() call returns -ENOENT error (not an error, but the hole) then dn-> structure is not filled and contain garbage. So using of dn->size for memcpy() argument cause that U-Boot tries to copy unspecified amount of bytes from possible unmapped memory. Which randomly cause lockup of P2020 CPU.

Fix this issue by copying UBIFS_BLOCK_SIZE bytes from read buffer when dn->size is not available. UBIFS_BLOCK_SIZE is the size of the buffer itself and read_block() fills buffer by zeros when it returns -ENOENT.

Quoted 5 lines

This patch fixes ubifsload on P2020.

Fixes b1a14f8a1c2e ("UBIFS: Change ubifsload to not read beyond the requested size")

Applied to u-boot/master, thanks!

Signature · 1 line
Tom