Skip to contentSkip to conversation

ocfs2: fix null-ptr-deref when journal load failed.

PATCH Superseded Julian Sun 2 participants 3 messages Aug 30, 2024 – Sep 2, 2024 OCFS2

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Julian Sun

author
#0 lore raw

PATCH v3 1/1 ocfs2: fix null-ptr-deref when journal load failed.

During the mounting process, if journal_reset() fails because of too short journal, then lead to jbd2_journal_load() fails with NULL j_sb_buffer. Subsequently, ocfs2_journal_shutdown() calls jbd2_journal_flush()->jbd2_cleanup_journal_tail()-> __jbd2_update_log_tail()->jbd2_journal_update_sb_log_tail() ->lock_buffer(journal->j_sb_buffer), resulting in a null-pointer dereference error.

To resolve this issue, we should check the JBD2_LOADED flag to ensure the journal was properly loaded. Additionly, refine code to make it cleaner.

---
 fs/ocfs2/journal.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/ocfs2/journal.c +3 -2
index 530fba34f6d3..ff2a6538b46e 100644
@@ -1074,9 +1074,10 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
 		osb->commit_task = NULL;
 	}
 
-	BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
+	BUG_ON(atomic_read(&(journal->j_num_trans)) != 0);
 
-	if (ocfs2_mount_local(osb)) {
+	if (ocfs2_mount_local(osb) &&
+	   (journal->j_journal->j_flags & JBD2_LOADED)) {
 		jbd2_journal_lock_updates(journal->j_journal);
 		status = jbd2_journal_flush(journal->j_journal, 0);
 		jbd2_journal_unlock_updates(journal->j_journal);
Signature · 1 line
2.39.2
maintainer
#1 lore raw
Quoted 9 lines · Julian Sun

During the mounting process, if journal_reset() fails because of too short journal, then lead to jbd2_journal_load() fails with NULL j_sb_buffer. Subsequently, ocfs2_journal_shutdown() calls jbd2_journal_flush()->jbd2_cleanup_journal_tail()-> __jbd2_update_log_tail()->jbd2_journal_update_sb_log_tail() ->lock_buffer(journal->j_sb_buffer), resulting in a null-pointer dereference error.

Quoted 3 lines

To resolve this issue, we should check the JBD2_LOADED flag to ensure the journal was properly loaded. Additionly, refine code to make it cleaner.

s/Additionly/Additionally

Additionally, use journal instead of osb->journal directly to simplify the code.

Quoted 14 lines
---
 fs/ocfs2/journal.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/ocfs2/journal.c +0 -0
index 530fba34f6d3..ff2a6538b46e 100644

fs/ocfs2/journal.c void ocfs2_journal_shutdown(struct ocfs2_super *osb) · line 1078

Quoted 6 lines
fs/ocfs2/journal.c +1 -1
@@ -1074,9 +1074,10 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
 		osb->commit_task = NULL;
 	}
 
-	BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
+	BUG_ON(atomic_read(&(journal->j_num_trans)) != 0);
 

Please do the same change for the above one:

num_running_trans = atomic_read(&(journal->j_num_trans));

fs/ocfs2/journal.c void ocfs2_journal_shutdown(struct ocfs2_super *osb) · line 1080

Quoted 3 lines
fs/ocfs2/journal.c +2 -1
-	if (ocfs2_mount_local(osb)) {
+	if (ocfs2_mount_local(osb) &&
+	   (journal->j_journal->j_flags & JBD2_LOADED)) {

Align it to the right place, please.

Thanks,
Joseph

Quoted 3 lines
fs/ocfs2/journal.c +0 -0
 		jbd2_journal_lock_updates(journal->j_journal);
 		status = jbd2_journal_flush(journal->j_journal, 0);
 		jbd2_journal_unlock_updates(journal->j_journal);
author
#2 lore raw
Quoted 16 lines · Joseph Qi
Quoted 9 lines · Julian Sun

During the mounting process, if journal_reset() fails because of too short journal, then lead to jbd2_journal_load() fails with NULL j_sb_buffer. Subsequently, ocfs2_journal_shutdown() calls jbd2_journal_flush()->jbd2_cleanup_journal_tail()-> __jbd2_update_log_tail()->jbd2_journal_update_sb_log_tail() ->lock_buffer(journal->j_sb_buffer), resulting in a null-pointer dereference error.

Quoted 3 lines

To resolve this issue, we should check the JBD2_LOADED flag to ensure the journal was properly loaded. Additionly, refine code to make it cleaner.

Quoted 5 lines

s/Additionly/Additionally

Additionally, use journal instead of osb->journal directly to simplify
the code.

Yeah, sorry for the typo..

Quoted 24 lines
Quoted 18 lines

---
 fs/ocfs2/journal.c | 5 +++--

 1 file changed, 3 insertions(+), 2 deletions(-)
fs/ocfs2/journal.c +0 -0
index 530fba34f6d3..ff2a6538b46e 100644
@@ -1074,9 +1074,10 @@ void ocfs2_journal_shutdown(struct
 ocfs2_super *osb)
                 osb->commit_task = NULL;
         }
Quoted 4 lines


-       BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0); +       BUG_ON(atomic_read(&(journal->j_num_trans)) != 0);

Please do the same change for the above one:

Quoted 1 line

num_running_trans = atomic_read(&(journal->j_num_trans));

Ok.

Quoted 5 lines
Quoted 4 lines

-       if (ocfs2_mount_local(osb)) {
+       if (ocfs2_mount_local(osb) &&
+          (journal->j_journal->j_flags & JBD2_LOADED)) {

Align it to the right place, please.

Sure.

Quoted 7 lines

Thanks,
Joseph

Quoted 3 lines

                jbd2_journal_lock_updates(journal->j_journal); status = jbd2_journal_flush(journal->j_journal, 0); jbd2_journal_unlock_updates(journal->j_journal);

Thanks,

Signature · 1 line
Julian Sun <[email protected]>