Skip to contentSkip to conversation

jfs: fix array-index-out-of-bounds in dbFindLeaf

PATCH In review Manas Ghandat 2 participants 6 messages Aug 29, 2023 – Sep 3, 2023 JFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 jfs: fix array-index-out-of-bounds in dbFindLeaf

Currently while searching for dmtree_t for sufficient free blocks there is an array out of bounds while getting element in tp->dm_stree. Added the required bound check.

Ps: After I added the check I am getting the following log

[   22.661748][ T4425] ERROR: (device loop0): dbAllocAny: unable to allocate blocks
[   22.661748][ T4425]
[   22.665536][ T4425] ERROR: (device loop0): remounting filesystem as read-only
[   22.667856][ T4425] jfs_mkdir: dtInsert returned -EIO
[   22.669750][ T4425] ERROR: (device loop0): txAbort:

I was wondering if these checks are significant of not?

---
 fs/jfs/jfs_dmap.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/jfs/jfs_dmap.c +4 -0
index a14a0f18a4c4..5af17b2287be 100644
@@ -2948,6 +2948,10 @@ static int dbFindLeaf(dmtree_t * tp, int l2nb, int *leafidx)
 			/* sufficient free space found.  move to the next
 			 * level (or quit if this is the last level).
 			 */
+
+			if (x + n > TREESIZE)
+				return -ENOSPC;
+
 			if (l2nb <= tp->dmt_stree[x + n])
 				break;
 		}
Signature · 6 lines
2.37.2

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#1 lore raw
Quoted 6 lines · Manas Ghandat

Currently while searching for dmtree_t for sufficient free blocks there is an array out of bounds while getting element in tp->dm_stree. Added the required bound check.

Ps: After I added the check I am getting the following log

Quoted 7 lines
[   22.661748][ T4425] ERROR: (device loop0): dbAllocAny: unable to allocate blocks
[   22.661748][ T4425]
[   22.665536][ T4425] ERROR: (device loop0): remounting filesystem as read-only
[   22.667856][ T4425] jfs_mkdir: dtInsert returned -EIO
[   22.669750][ T4425] ERROR: (device loop0): txAbort:

I was wondering if these checks are significant of not?

This won't work. dbFindLeaf() can be called from dbFindCtl() with struct dmapctl whose stree index can be as high as CTLTREESIZE which is larger than TREESIZE. A check against CTLTREESIZE might be better than nothing at all but won't necessarily detect an overflow. Currently, dbFindLeaf doesn't have anything to tell it which tree it is working on.

We could pass in the treesize as an argument to dbFindCtl() if we can't come up with something simpler.

Shaggy

Quoted 13 lines
---
  fs/jfs/jfs_dmap.c | 4 ++++
  1 file changed, 4 insertions(+)
fs/jfs/jfs_dmap.c +0 -0
index a14a0f18a4c4..5af17b2287be 100644

fs/jfs/jfs_dmap.c static int dbFindLeaf(dmtree_t * tp, int l2nb, int *leafidx) · line 2957

Quoted 10 lines
fs/jfs/jfs_dmap.c +4 -0
@@ -2948,6 +2948,10 @@ static int dbFindLeaf(dmtree_t * tp, int l2nb, int *leafidx)
  			/* sufficient free space found.  move to the next
  			 * level (or quit if this is the last level).
  			 */
+
+			if (x + n > TREESIZE)
+				return -ENOSPC;
+
  			if (l2nb <= tp->dmt_stree[x + n])
  				break;
  		}

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#2 lore raw

I was wondering if we could implement a get_tree_size macro wherein  we could find the tree size so that we can do the comparison. SInce the tp->dmt_stree is an array we can get its size and fix the out of bounds. Would this thing work?

Quoted 14 lines · Dave Kleikamp

This won't work. dbFindLeaf() can be called from dbFindCtl() with struct dmapctl whose stree index can be as high as CTLTREESIZE which is larger than TREESIZE. A check against CTLTREESIZE might be better than nothing at all but won't necessarily detect an overflow. Currently, dbFindLeaf doesn't have anything to tell it which tree it is working on.

We could pass in the treesize as an argument to dbFindCtl() if we can't come up with something simpler.

Shaggy

Quoted 8 lines

---
  fs/jfs/jfs_dmap.c | 4 ++++

  1 file changed, 4 insertions(+)
Quoted 16 lines
Quoted 15 lines
fs/jfs/jfs_dmap.c +4 -0
index a14a0f18a4c4..5af17b2287be 100644
@@ -2948,6 +2948,10 @@ static int dbFindLeaf(dmtree_t * tp, int l2nb, 
 int *leafidx)
               /* sufficient free space found.  move to the next
                * level (or quit if this is the last level).
                */
+
+            if (x + n > TREESIZE)
+                return -ENOSPC;
+
               if (l2nb <= tp->dmt_stree[x + n])
                   break;
Quoted 1 line

          }

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#3 lore raw
Quoted 1 line · Manas Ghandat

I was wondering if we could implement a get_tree_size macro wherein  we could find the tree size so that we can do the comparison. SInce the tp->dmt_stree is an array we can get its size and fix the out of bounds. Would this thing work?

dmtree_t is a union of two nearly identical structures that both contain an stree. The only real difference in the structures is the size of the stree, so dbFindLeaf doesn't really know which is being used by the caller.

Quoted 16 lines · Dave Kleikamp
Quoted 14 lines

This won't work. dbFindLeaf() can be called from dbFindCtl() with struct dmapctl whose stree index can be as high as CTLTREESIZE which is larger than TREESIZE. A check against CTLTREESIZE might be better than nothing at all but won't necessarily detect an overflow. Currently, dbFindLeaf doesn't have anything to tell it which tree it is working on.

We could pass in the treesize as an argument to dbFindCtl() if we can't come up with something simpler.

Shaggy

Quoted 8 lines

---
  fs/jfs/jfs_dmap.c | 4 ++++

  1 file changed, 4 insertions(+)
Quoted 16 lines
Quoted 8 lines
Quoted 8 lines
fs/jfs/jfs_dmap.c +0 -0
index a14a0f18a4c4..5af17b2287be 100644
@@ -2948,6 +2948,10 @@ static int dbFindLeaf(dmtree_t * tp, int l2nb, 
 int *leafidx)
               /* sufficient free space found.  move to the next
                * level (or quit if this is the last level).
Quoted 8 lines
Quoted 8 lines
fs/jfs/jfs_dmap.c +4 -0
                */
+
+            if (x + n > TREESIZE)
+                return -ENOSPC;
+
               if (l2nb <= tp->dmt_stree[x + n])
                   break;

          }

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#4 lore raw

Actually I was talking about the stree attribute of dmtree which is present in both dmaptree and dmapctl.

Since it is an array we can add a check for the size of array like the code below.

+            if (x + n > (sizeof(tp->dmt_stree)/sizeof(s8))) +                 return -ENOSPC;

Quoted 5 lines · Dave Kleikamp
Quoted 1 line · Manas Ghandat

I was wondering if we could implement a get_tree_size macro wherein  we could find the tree size so that we can do the comparison. SInce the tp->dmt_stree is an array we can get its size and fix the out of bounds. Would this thing work?

dmtree_t is a union of two nearly identical structures that both contain an stree. The only real difference in the structures is the size of the stree, so dbFindLeaf doesn't really know which is being used by the caller.

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

#5 lore raw
Quoted 9 lines · Manas Ghandat

Actually I was talking about the stree attribute of dmtree which is present in both dmaptree and dmapctl.

Since it is an array we can add a check for the size of array like the code below.

+            if (x + n > (sizeof(tp->dmt_stree)/sizeof(s8))) +                 return -ENOSPC;

That will always evaluate to the same thing (the size of stree in dmapctl). It would be valid, since that is the larger number, but it wouldn't catch all overflows of dmaptree's stree.

Quoted 7 lines · Dave Kleikamp
Quoted 5 lines · Manas Ghandat
Quoted 1 line

I was wondering if we could implement a get_tree_size macro wherein we could find the tree size so that we can do the comparison. SInce the tp->dmt_stree is an array we can get its size and fix the out of bounds. Would this thing work?

dmtree_t is a union of two nearly identical structures that both contain an stree. The only real difference in the structures is the size of the stree, so dbFindLeaf doesn't really know which is being used by the caller.