Skip to contentSkip to conversation

ocfs2: Fix shift-out-of-bounds UBSAN bug in ocfs2_verify_volume()

PATCH Superseded qasdev 3 participants 5 messages Aug 16, 2024 – Aug 20, 2024 OCFS2

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

qasdev

author
#0 lore raw

PATCH 1/1 ocfs2: Fix shift-out-of-bounds UBSAN bug in ocfs2_verify_volume()

From ad1ca2fd2ecf4eb7ec2c76fcbbf34639f0ad87ca Mon Sep 17 00:00:00 2001
From: Qasim Ijaz <[email protected]>
Date: Fri, 16 Aug 2024 02:30:25 +0100
Subject: [PATCH] ocfs2: Fix shift-out-of-bounds UBSAN bug in
ocfs2_verify_volume()

This patch addresses a shift-out-of-bounds error in the ocfs2_verify_volume() function, identified by UBSAN. The bug was triggered by an invalid s_clustersize_bits value (e.g., 1548), which caused the expression "1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits)" to exceed the limits of a 32-bit integer,
leading to an out-of-bounds shift.

---
 fs/ocfs2/super.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
fs/ocfs2/super.c +6 -2
index afee70125ae3..1e43cdca7f40 100644
@@ -2357,8 +2357,12 @@ static int ocfs2_verify_volume(struct ocfs2_dinode *di,
 			     (unsigned long long)bh->b_blocknr);
 		} else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
 			    le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
-			mlog(ML_ERROR, "bad cluster size found: %u\n",
-			     1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
+			if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 32)
+				mlog(ML_ERROR, "bad cluster size found: %u\n",
+				     1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
+			else
+				mlog(ML_ERROR, "invalid cluster size bit value: %u\n",
+				     le32_to_cpu(di->id2.i_super.s_clustersize_bits));
 		} else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
 			mlog(ML_ERROR, "bad root_blkno: 0\n");
 		} else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
Signature · 1 line
2.30.2
reviewer
#1 lore raw
Quoted 27 lines · qasdev

From ad1ca2fd2ecf4eb7ec2c76fcbbf34639f0ad87ca Mon Sep 17 00:00:00 2001
From: Qasim Ijaz <[email protected]>
Date: Fri, 16 Aug 2024 02:30:25 +0100
Subject: [PATCH] ocfs2: Fix shift-out-of-bounds UBSAN bug in
ocfs2_verify_volume()

This patch addresses a shift-out-of-bounds error in the ocfs2_verify_volume() function, identified by UBSAN. The bug was triggered by an invalid s_clustersize_bits value (e.g., 1548), which caused the expression "1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits)" to exceed the limits of a 32-bit integer,
leading to an out-of-bounds shift.

---
  fs/ocfs2/super.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
fs/ocfs2/super.c +0 -0
index afee70125ae3..1e43cdca7f40 100644
@@ -2357,8 +2357,12 @@ static int ocfs2_verify_volume(struct ocfs2_dinode *di,
  			     (unsigned long long)bh->b_blocknr);

fs/ocfs2/super.c static int ocfs2_verify_volume(struct ocfs2_dinode *di, · line 2365

Quoted 10 lines
fs/ocfs2/super.c +6 -2
  		} else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
  			    le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
-			mlog(ML_ERROR, "bad cluster size found: %u\n",
-			     1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
+			if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 32)
+				mlog(ML_ERROR, "bad cluster size found: %u\n",
+				     1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
+			else
+				mlog(ML_ERROR, "invalid cluster size bit value: %u\n",
+				     le32_to_cpu(di->id2.i_super.s_clustersize_bits));

I prefer to use concise code to fix the error.
Do you like below code?
- mlog(ML_ERROR, "bad cluster size found: %u\n",
- 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
+ mlog(ML_ERROR, "bad cluster size bit found: %u\n",
+ le32_to_cpu(di->id2.i_super.s_clustersize_bits));

Thanks,
Heming

Quoted 3 lines
fs/ocfs2/super.c +0 -0
  		} else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
  			mlog(ML_ERROR, "bad root_blkno: 0\n");
  		} else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
maintainer
#2 lore raw
Quoted 7 lines · Heming Zhao
Quoted 6 lines · qasdev

 From ad1ca2fd2ecf4eb7ec2c76fcbbf34639f0ad87ca Mon Sep 17 00:00:00 2001
From: Qasim Ijaz <[email protected]>
Date: Fri, 16 Aug 2024 02:30:25 +0100
Subject: [PATCH] ocfs2: Fix shift-out-of-bounds UBSAN bug in ocfs2_verify_volume()

The above should be eliminated from patch body.

Quoted 30 lines
Quoted 30 lines

This patch addresses a shift-out-of-bounds error in the ocfs2_verify_volume() function, identified by UBSAN. The bug was triggered by an invalid s_clustersize_bits value (e.g., 1548), which caused the expression "1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits)" to exceed the limits of a 32-bit integer,
leading to an out-of-bounds shift.

---
  fs/ocfs2/super.c | 8 ++++++--

  1 file changed, 6 insertions(+), 2 deletions(-)
fs/ocfs2/super.c +5 -2
index afee70125ae3..1e43cdca7f40 100644
@@ -2357,8 +2357,12 @@ static int ocfs2_verify_volume(struct ocfs2_dinode *di,
                    (unsigned long long)bh->b_blocknr);
           } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
                   le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
-            mlog(ML_ERROR, "bad cluster size found: %u\n",
-                 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
+            if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 32)
+                mlog(ML_ERROR, "bad cluster size found: %u\n",
+                     1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
+            else
+                mlog(ML_ERROR, "invalid cluster size bit value: %u\n",
Quoted 9 lines

fs/ocfs2/super.c static int ocfs2_verify_volume(struct ocfs2_dinode *di, · line 2365

Quoted 1 line
fs/ocfs2/super.c +1 -0
+                     le32_to_cpu(di->id2.i_super.s_clustersize_bits));

I prefer to use concise code to fix the error.
Do you like below code?
-        mlog(ML_ERROR, "bad cluster size found: %u\n",
-                 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits)); +        mlog(ML_ERROR, "bad cluster size bit found: %u\n", +                 le32_to_cpu(di->id2.i_super.s_clustersize_bits));

Agree. qasdev, Could you please update and send v2?

Thanks,
Joseph

author
#3 lore raw

PATCH v2 1/1 ocfs2: Fix shift-out-of-bounds UBSAN bug in ocfs2_verify_volume()

Quoted 53 lines · Joseph Qi
Quoted 7 lines · Heming Zhao
Quoted 6 lines · qasdev

 From ad1ca2fd2ecf4eb7ec2c76fcbbf34639f0ad87ca Mon Sep 17 00:00:00 2001
From: Qasim Ijaz <[email protected]>
Date: Fri, 16 Aug 2024 02:30:25 +0100
Subject: [PATCH] ocfs2: Fix shift-out-of-bounds UBSAN bug in ocfs2_verify_volume()

The above should be eliminated from patch body.

Quoted 30 lines
Quoted 30 lines

This patch addresses a shift-out-of-bounds error in the ocfs2_verify_volume() function, identified by UBSAN. The bug was triggered by an invalid s_clustersize_bits value (e.g., 1548), which caused the expression "1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits)" to exceed the limits of a 32-bit integer,
leading to an out-of-bounds shift.

---
  fs/ocfs2/super.c | 8 ++++++--

  1 file changed, 6 insertions(+), 2 deletions(-)
fs/ocfs2/super.c +5 -2
index afee70125ae3..1e43cdca7f40 100644
@@ -2357,8 +2357,12 @@ static int ocfs2_verify_volume(struct ocfs2_dinode *di,
                    (unsigned long long)bh->b_blocknr);
           } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
                   le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
-            mlog(ML_ERROR, "bad cluster size found: %u\n",
-                 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
+            if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 32)
+                mlog(ML_ERROR, "bad cluster size found: %u\n",
+                     1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
+            else
+                mlog(ML_ERROR, "invalid cluster size bit value: %u\n",
Quoted 9 lines

fs/ocfs2/super.c static int ocfs2_verify_volume(struct ocfs2_dinode *di, · line 2365

Quoted 1 line
fs/ocfs2/super.c +1 -0
+                     le32_to_cpu(di->id2.i_super.s_clustersize_bits));

I prefer to use concise code to fix the error.
Do you like below code?
-        mlog(ML_ERROR, "bad cluster size found: %u\n",
-                 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits)); +        mlog(ML_ERROR, "bad cluster size bit found: %u\n", +                 le32_to_cpu(di->id2.i_super.s_clustersize_bits));

Quoted 4 lines

Agree. qasdev, Could you please update and send v2?

Thanks,
Joseph

Thanks for the feedback. After considering the input, I've refined the patch to make it more concise. I've updated the patch and included it below:

This patch addresses a shift-out-of-bounds error in the ocfs2_verify_volume() function, identified by UBSAN. The bug was triggered by an invalid s_clustersize_bits value (e.g., 1548), which caused the expression "1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits)" to exceed the limits of a 32-bit integer,
leading to an out-of-bounds shift.

---
 fs/ocfs2/super.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/ocfs2/super.c +2 -2
index afee70125ae3..b704983b2112 100644
@@ -2357,8 +2357,8 @@ static int ocfs2_verify_volume(struct ocfs2_dinode *di,
                     (unsigned long long)bh->b_blocknr);
             } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
                     le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
-                   mlog(ML_ERROR, "bad cluster size found: %u\n",
-                        1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
+                   mlog(ML_ERROR, "bad cluster size bit found: %u\n",
+                        le32_to_cpu(di->id2.i_super.s_clustersize_bits));
             } else if (!le64_to_cpu(di->id2.i_super.s_root_blkno)) {
                     mlog(ML_ERROR, "bad root_blkno: 0\n");
             } else if (!le64_to_cpu(di->id2.i_super.s_system_dir_blkno)) {
Signature · 1 line
2.30.2
maintainer
#4 lore raw
Quoted 54 lines · qasdev
Quoted 53 lines · Joseph Qi
Quoted 7 lines · Heming Zhao
Quoted 6 lines · qasdev

 From ad1ca2fd2ecf4eb7ec2c76fcbbf34639f0ad87ca Mon Sep 17 00:00:00 2001
From: Qasim Ijaz <[email protected]>
Date: Fri, 16 Aug 2024 02:30:25 +0100
Subject: [PATCH] ocfs2: Fix shift-out-of-bounds UBSAN bug in ocfs2_verify_volume()

The above should be eliminated from patch body.

Quoted 39 lines
Quoted 21 lines

This patch addresses a shift-out-of-bounds error in the ocfs2_verify_volume() function, identified by UBSAN. The bug was triggered by an invalid s_clustersize_bits value (e.g., 1548), which caused the expression "1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits)" to exceed the limits of a 32-bit integer,
leading to an out-of-bounds shift.

---
  fs/ocfs2/super.c | 8 ++++++--

  1 file changed, 6 insertions(+), 2 deletions(-)
fs/ocfs2/super.c +0 -0
index afee70125ae3..1e43cdca7f40 100644
@@ -2357,8 +2357,12 @@ static int ocfs2_verify_volume(struct ocfs2_dinode *di,
                    (unsigned long long)bh->b_blocknr);

fs/ocfs2/super.c static int ocfs2_verify_volume(struct ocfs2_dinode *di, · line 2365

Quoted 10 lines
fs/ocfs2/super.c +6 -2
           } else if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 12 ||
                   le32_to_cpu(di->id2.i_super.s_clustersize_bits) > 20) {
-            mlog(ML_ERROR, "bad cluster size found: %u\n",
-                 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
+            if (le32_to_cpu(di->id2.i_super.s_clustersize_bits) < 32)
+                mlog(ML_ERROR, "bad cluster size found: %u\n",
+                     1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits));
+            else
+                mlog(ML_ERROR, "invalid cluster size bit value: %u\n",
+                     le32_to_cpu(di->id2.i_super.s_clustersize_bits));

I prefer to use concise code to fix the error.
Do you like below code?
-        mlog(ML_ERROR, "bad cluster size found: %u\n",
-                 1 << le32_to_cpu(di->id2.i_super.s_clustersize_bits)); +        mlog(ML_ERROR, "bad cluster size bit found: %u\n", +                 le32_to_cpu(di->id2.i_super.s_clustersize_bits));

Quoted 8 lines
Quoted 4 lines

Agree. qasdev, Could you please update and send v2?

Thanks,
Joseph

Thanks for the feedback. After considering the input, I've refined the patch to make it more concise. I've updated the patch and included it below:

Hi, please send v2 as a standalone thread.

Thanks,
Joseph