Skip to contentSkip to conversation

jbd2: fix integer underflow in jbd2_journal_initialize_fast_commit()

PATCH Superseded Junrui Luo 5 participants 9 messages May 12 – Jun 4

Subsystems (2)
lore
Share & tools
lore

Junrui Luo

author
#0 lore raw

PATCH 1/1 jbd2: fix integer underflow in jbd2_journal_initialize_fast_commit()

jbd2_journal_initialize_fast_commit() validates journal capacity by checking (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS). Both j_last and num_fc_blks are unsigned, so when num_fc_blks exceeds j_last the subtraction wraps to a large value, bypassing the bounds check.

The resulting underflow corrupts j_last, j_fc_first, and j_free, leading to journal abort.

Fix by adding an overflow guard that checks num_fc_blks against j_last before performing the subtraction.

Fixes 6866d7b3f2bb ("ext4 / jbd2: add fast commit initialization")
---
 fs/jbd2/journal.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/jbd2/journal.c +2 -1
index cb2c529a8f1b..a54146576c3f 100644
@@ -2263,7 +2263,8 @@ jbd2_journal_initialize_fast_commit(journal_t *journal)
 	unsigned long long num_fc_blks;
 
 	num_fc_blks = jbd2_journal_get_num_fc_blks(sb);
-	if (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS)
+	if (num_fc_blks > journal->j_last ||
+	    journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS)
 		return -ENOSPC;
 
 	/* Are we called twice? */

---
base-commit: 7aaa8047eafd0bd628065b15757d9b48c5f9c07d
change-id: 20260512-fixes-2ff4f9f7d064

Best regards,

Signature · 1 line
Junrui Luo <[email protected]>
#1 lore raw
Quoted 5 lines · Junrui Luo

jbd2_journal_initialize_fast_commit() validates journal capacity by checking (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS). Both j_last and num_fc_blks are unsigned, so when num_fc_blks exceeds j_last the subtraction wraps to a large value, bypassing the bounds check.

I'm wondering, how does the "num_fc_blks exceeds j_last" error occur? Under normal circumstances, journal->j_last is initialized to sb->s_maxlen, which is set to the total number of journal blocks (i.e., the sum of the normal journal area and the fast commit journal area) during filesystem formatting by mkfs. Therefore, num_fc_blocks shoud never exceed journal->j_last. Right?

Have you mounted a deliberately constructed corrupted file system? If so, I'd prefer to return EFSCORRUPTED here.

Thanks,
Yi.

Quoted 35 lines

The resulting underflow corrupts j_last, j_fc_first, and j_free, leading to journal abort.

Fix by adding an overflow guard that checks num_fc_blks against j_last before performing the subtraction.

Fixes 6866d7b3f2bb ("ext4 / jbd2: add fast commit initialization")
---
 fs/jbd2/journal.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/jbd2/journal.c +2 -2
index cb2c529a8f1b..a54146576c3f 100644
@@ -2263,7 +2263,8 @@ jbd2_journal_initialize_fast_commit(journal_t *journal)
 	unsigned long long num_fc_blks;
 
 	num_fc_blks = jbd2_journal_get_num_fc_blks(sb);
-	if (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS)
+	if (num_fc_blks > journal->j_last ||
+	    journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS)
 		return -ENOSPC;
 
 	/* Are we called twice? */
 
---

base-commit: 7aaa8047eafd0bd628065b15757d9b48c5f9c07d
change-id: 20260512-fixes-2ff4f9f7d064

Best regards,

author
#2 lore raw
Quoted 7 lines · Zhang Yi
Quoted 5 lines · Junrui Luo

jbd2_journal_initialize_fast_commit() validates journal capacity by checking (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS). Both j_last and num_fc_blks are unsigned, so when num_fc_blks exceeds j_last the subtraction wraps to a large value, bypassing the bounds check.

Quoted 6 lines

I'm wondering, how does the "num_fc_blks exceeds j_last" error occur? Under normal circumstances, journal->j_last is initialized to sb->s_maxlen, which is set to the total number of journal blocks (i.e., the sum of the normal journal area and the fast commit journal area) during filesystem formatting by mkfs. Therefore, num_fc_blocks shoud never exceed journal->j_last. Right?

Yes, this is triggered by mounting a crafted filesystem where the ext4 superblock has fast_commit enabled but the journal superblock does not, while s_num_fc_blks is set larger than s_maxlen.

Quoted 2 lines

Have you mounted a deliberately constructed corrupted file system? If so, I'd prefer to return EFSCORRUPTED here.

I will change it in v2.

Thanks,
Junrui Luo

author
#3 lore raw

PATCH v2 1/1 jbd2: fix integer underflow in jbd2_journal_initialize_fast_commit()

In reply to Junrui Luo

jbd2_journal_initialize_fast_commit() validates journal capacity by checking (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS). Both j_last and num_fc_blks are unsigned, so when num_fc_blks exceeds j_last the subtraction wraps to a large value, bypassing the bounds check.

The resulting underflow corrupts j_last, j_fc_first, and j_free, leading to journal abort.

Fix by checking num_fc_blks against j_last before the subtraction, returning -EFSCORRUPTED.

Fixes 6866d7b3f2bb ("ext4 / jbd2: add fast commit initialization")

---
Changes in v2:
- Return -EFSCORRUPTED instead of -ENOSPC
- Link to v1: https://lore.kernel.org/all/SYBPR01MB78813DD23B28BD49B1AA1123AF392@SYBPR01MB7881.ausprd01.prod.outlook.com/

---
 fs/jbd2/journal.c | 2 ++
 1 file changed, 2 insertions(+)
fs/jbd2/journal.c +2 -0
index cb2c529a8f1b..0bb97459fbf0 100644
@@ -2263,6 +2263,8 @@ jbd2_journal_initialize_fast_commit(journal_t *journal)
 	unsigned long long num_fc_blks;
 
 	num_fc_blks = jbd2_journal_get_num_fc_blks(sb);
+	if (num_fc_blks > journal->j_last)
+		return -EFSCORRUPTED;
 	if (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS)
 		return -ENOSPC;
 

---
base-commit: 7aaa8047eafd0bd628065b15757d9b48c5f9c07d
change-id: 20260513-fixes-e6dcda3273d4

Best regards,

Signature · 1 line
Junrui Luo <[email protected]>
Reviewed-by on v2 1/1
#4 lore raw
Quoted 9 lines · Junrui Luo

jbd2_journal_initialize_fast_commit() validates journal capacity by checking (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS). Both j_last and num_fc_blks are unsigned, so when num_fc_blks exceeds j_last the subtraction wraps to a large value, bypassing the bounds check.

The resulting underflow corrupts j_last, j_fc_first, and j_free, leading to journal abort.

Quoted 7 lines

Fix by checking num_fc_blks against j_last before the subtraction, returning -EFSCORRUPTED.

Fixes 6866d7b3f2bb ("ext4 / jbd2: add fast commit initialization")

Looks good. Feel free to add:

Honza

Quoted 30 lines

---
Changes in v2:
- Return -EFSCORRUPTED instead of -ENOSPC
- Link to v1: https://lore.kernel.org/all/SYBPR01MB78813DD23B28BD49B1AA1123AF392@SYBPR01MB7881.ausprd01.prod.outlook.com/

---
 fs/jbd2/journal.c | 2 ++
 1 file changed, 2 insertions(+)
fs/jbd2/journal.c +2 -1
index cb2c529a8f1b..0bb97459fbf0 100644
@@ -2263,6 +2263,8 @@ jbd2_journal_initialize_fast_commit(journal_t *journal)
 	unsigned long long num_fc_blks;
 
 	num_fc_blks = jbd2_journal_get_num_fc_blks(sb);
+	if (num_fc_blks > journal->j_last)
+		return -EFSCORRUPTED;
 	if (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS)
 		return -ENOSPC;
 
 
---

base-commit: 7aaa8047eafd0bd628065b15757d9b48c5f9c07d
change-id: 20260513-fixes-e6dcda3273d4

Best regards,

Signature · 1 line
Junrui Luo <[email protected]>
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
Reviewed-by on v2 1/1
#5 lore raw
In reply to Junrui Luo
Quoted 9 lines · Junrui Luo

jbd2_journal_initialize_fast_commit() validates journal capacity by checking (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS). Both j_last and num_fc_blks are unsigned, so when num_fc_blks exceeds j_last the subtraction wraps to a large value, bypassing the bounds check.

The resulting underflow corrupts j_last, j_fc_first, and j_free, leading to journal abort.

Quoted 7 lines

Fix by checking num_fc_blks against j_last before the subtraction, returning -EFSCORRUPTED.

Fixes 6866d7b3f2bb ("ext4 / jbd2: add fast commit initialization")

Looks good to me.

Quoted 27 lines

---
Changes in v2:
- Return -EFSCORRUPTED instead of -ENOSPC
- Link to v1: https://lore.kernel.org/all/SYBPR01MB78813DD23B28BD49B1AA1123AF392@SYBPR01MB7881.ausprd01.prod.outlook.com/

---
 fs/jbd2/journal.c | 2 ++
 1 file changed, 2 insertions(+)
fs/jbd2/journal.c +2 -1
index cb2c529a8f1b..0bb97459fbf0 100644
@@ -2263,6 +2263,8 @@ jbd2_journal_initialize_fast_commit(journal_t *journal)
 	unsigned long long num_fc_blks;
 
 	num_fc_blks = jbd2_journal_get_num_fc_blks(sb);
+	if (num_fc_blks > journal->j_last)
+		return -EFSCORRUPTED;
 	if (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS)
 		return -ENOSPC;
 
 
---

base-commit: 7aaa8047eafd0bd628065b15757d9b48c5f9c07d
change-id: 20260513-fixes-e6dcda3273d4

Best regards,

Reviewed-by on v2 1/1
#6 lore raw
In reply to Junrui Luo
Quoted 9 lines · Junrui Luo

jbd2_journal_initialize_fast_commit() validates journal capacity by checking (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS). Both j_last and num_fc_blks are unsigned, so when num_fc_blks exceeds j_last the subtraction wraps to a large value, bypassing the bounds check.

The resulting underflow corrupts j_last, j_fc_first, and j_free, leading to journal abort.

Quoted 7 lines

Fix by checking num_fc_blks against j_last before the subtraction, returning -EFSCORRUPTED.

Fixes 6866d7b3f2bb ("ext4 / jbd2: add fast commit initialization")

The Fixes tag is not quite accurate, it should be:

Fixes e029c5f279872 ("ext4: make num of fast commit blocks configurable")

Otherwise looks good to me:

Quoted 27 lines

---
Changes in v2:
- Return -EFSCORRUPTED instead of -ENOSPC
- Link to v1: https://lore.kernel.org/all/SYBPR01MB78813DD23B28BD49B1AA1123AF392@SYBPR01MB7881.ausprd01.prod.outlook.com/

---
 fs/jbd2/journal.c | 2 ++
 1 file changed, 2 insertions(+)
fs/jbd2/journal.c +2 -1
index cb2c529a8f1b..0bb97459fbf0 100644
@@ -2263,6 +2263,8 @@ jbd2_journal_initialize_fast_commit(journal_t *journal)
 	unsigned long long num_fc_blks;
 
 	num_fc_blks = jbd2_journal_get_num_fc_blks(sb);
+	if (num_fc_blks > journal->j_last)
+		return -EFSCORRUPTED;
 	if (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS)
 		return -ENOSPC;
 
 
---

base-commit: 7aaa8047eafd0bd628065b15757d9b48c5f9c07d
change-id: 20260513-fixes-e6dcda3273d4

Best regards,

Reviewed-by on v2 1/1
#7 lore raw
In reply to Junrui Luo
Quoted 9 lines · Junrui Luo

jbd2_journal_initialize_fast_commit() validates journal capacity by checking (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS). Both j_last and num_fc_blks are unsigned, so when num_fc_blks exceeds j_last the subtraction wraps to a large value, bypassing the bounds check.

The resulting underflow corrupts j_last, j_fc_first, and j_free, leading to journal abort.

Quoted 7 lines

Fix by checking num_fc_blks against j_last before the subtraction, returning -EFSCORRUPTED.

Fixes 6866d7b3f2bb ("ext4 / jbd2: add fast commit initialization")

The Fixes tag is not quite accurate, it should be:

Fixes e029c5f27987 ("ext4: make num of fast commit blocks configurable")

Otherwise looks good to me:

(P.S. Resend due to malformed email. Sorry for the noise.)

Quoted 27 lines

---
Changes in v2:
- Return -EFSCORRUPTED instead of -ENOSPC
- Link to v1: https://lore.kernel.org/all/SYBPR01MB78813DD23B28BD49B1AA1123AF392@SYBPR01MB7881.ausprd01.prod.outlook.com/

---
 fs/jbd2/journal.c | 2 ++
 1 file changed, 2 insertions(+)
fs/jbd2/journal.c +2 -1
index cb2c529a8f1b..0bb97459fbf0 100644
@@ -2263,6 +2263,8 @@ jbd2_journal_initialize_fast_commit(journal_t *journal)
 	unsigned long long num_fc_blks;
 
 	num_fc_blks = jbd2_journal_get_num_fc_blks(sb);
+	if (num_fc_blks > journal->j_last)
+		return -EFSCORRUPTED;
 	if (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS)
 		return -ENOSPC;
 
 
---

base-commit: 7aaa8047eafd0bd628065b15757d9b48c5f9c07d
change-id: 20260513-fixes-e6dcda3273d4

Best regards,

maintainer Applied
#8 lore raw
In reply to Junrui Luo
Quoted 10 lines · Junrui Luo

jbd2_journal_initialize_fast_commit() validates journal capacity by checking (journal->j_last - num_fc_blks < JBD2_MIN_JOURNAL_BLOCKS). Both j_last and num_fc_blks are unsigned, so when num_fc_blks exceeds j_last the subtraction wraps to a large value, bypassing the bounds check.

The resulting underflow corrupts j_last, j_fc_first, and j_free, leading to journal abort.

[...]

Applied, thanks!

[1/1] jbd2: fix integer underflow in jbd2_journal_initialize_fast_commit()

      commit: 289a2ca0c9b7eae74f93fc213b0b971669b8683d

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>