Skip to contentSkip to conversation

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

PATCH Superseded Julian Sun 2 participants 5 messages Aug 23, 2024 – Aug 30, 2024 OCFS2

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Julian Sun

author
#0 lore raw

PATCH v2 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, a new state OCFS2_JOURNAL_INITED has been introduced to replace the previous functionality of OCFS2_JOURNAL_LOADED, the original OCFS2_JOURNAL_LOADED is only set when ocfs2_journal_load() is successful. The jbd2_journal_flush() function is allowed to be called only when this flag is set. The logic here is that if the journal has even not been successfully loaded, there is no need to flush the journal.

---
 fs/ocfs2/journal.c | 9 ++++++---
 fs/ocfs2/journal.h | 1 +
 2 files changed, 7 insertions(+), 3 deletions(-)
fs/ocfs2/journal.c +6 -3
index 530fba34f6d3..da0ffcc5de0a 100644
@@ -968,7 +968,7 @@ int ocfs2_journal_init(struct ocfs2_super *osb, int *dirty)
 
 	ocfs2_set_journal_params(osb);
 
-	journal->j_state = OCFS2_JOURNAL_LOADED;
+	journal->j_state = OCFS2_JOURNAL_INITED;
 
 	status = 0;
 done:
@@ -1039,6 +1039,7 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
 	int status = 0;
 	struct inode *inode = NULL;
 	int num_running_trans = 0;
+	enum ocfs2_journal_state state;
 
 	BUG_ON(!osb);
 
@@ -1047,8 +1048,9 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
 		goto done;
 
 	inode = journal->j_inode;
+	state = journal->j_state;
 
-	if (journal->j_state != OCFS2_JOURNAL_LOADED)
+	if (state != OCFS2_JOURNAL_INITED && state != OCFS2_JOURNAL_LOADED)
 		goto done;
 
 	/* need to inc inode use count - jbd2_journal_destroy will iput. */
@@ -1076,7 +1078,7 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
 
 	BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
 
-	if (ocfs2_mount_local(osb)) {
+	if (ocfs2_mount_local(osb) && state == OCFS2_JOURNAL_LOADED) {
 		jbd2_journal_lock_updates(journal->j_journal);
 		status = jbd2_journal_flush(journal->j_journal, 0);
 		jbd2_journal_unlock_updates(journal->j_journal);
@@ -1174,6 +1176,7 @@ int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
 		}
 	} else
 		osb->commit_task = NULL;
+	journal->j_state = OCFS2_JOURNAL_LOADED;
 
 done:
 	return status;
fs/ocfs2/journal.h +1 -0
index e3c3a35dc5e0..a80f76a8fa0e 100644
@@ -15,6 +15,7 @@
 
 enum ocfs2_journal_state {
 	OCFS2_JOURNAL_FREE = 0,
+	OCFS2_JOURNAL_INITED,
 	OCFS2_JOURNAL_LOADED,
 	OCFS2_JOURNAL_IN_SHUTDOWN,
 };
Signature · 1 line
2.39.2
maintainer
#1 lore raw
Quoted 59 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.

To resolve this issue, a new state OCFS2_JOURNAL_INITED has been introduced to replace the previous functionality of OCFS2_JOURNAL_LOADED, the original OCFS2_JOURNAL_LOADED is only set when ocfs2_journal_load() is successful. The jbd2_journal_flush() function is allowed to be called only when this flag is set. The logic here is that if the journal has even not been successfully loaded, there is no need to flush the journal.

---
 fs/ocfs2/journal.c | 9 ++++++---
 fs/ocfs2/journal.h | 1 +
 2 files changed, 7 insertions(+), 3 deletions(-)
fs/ocfs2/journal.c +4 -2
index 530fba34f6d3..da0ffcc5de0a 100644
@@ -968,7 +968,7 @@ int ocfs2_journal_init(struct ocfs2_super *osb, int *dirty)
 
 	ocfs2_set_journal_params(osb);
 
-	journal->j_state = OCFS2_JOURNAL_LOADED;
+	journal->j_state = OCFS2_JOURNAL_INITED;
 
 	status = 0;
 done:
@@ -1039,6 +1039,7 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
 	int status = 0;
 	struct inode *inode = NULL;
 	int num_running_trans = 0;
+	enum ocfs2_journal_state state;
 
 	BUG_ON(!osb);
 
@@ -1047,8 +1048,9 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
 		goto done;
 
 	inode = journal->j_inode;
+	state = journal->j_state;
 
-	if (journal->j_state != OCFS2_JOURNAL_LOADED)
+	if (state != OCFS2_JOURNAL_INITED && state != OCFS2_JOURNAL_LOADED)
 		goto done;
 
 	/* need to inc inode use count - jbd2_journal_destroy will iput. */

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

Quoted 5 lines
fs/ocfs2/journal.c +1 -1
@@ -1076,7 +1078,7 @@ void ocfs2_journal_shutdown(struct ocfs2_super *osb)
 
 	BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
 
-	if (ocfs2_mount_local(osb)) {
+	if (ocfs2_mount_local(osb) && state == OCFS2_JOURNAL_LOADED) {

The only intent of the new introduced state is to identify if journal is truly loaded or not.
So it seems that the simplest way to fix this is just check JBD2_LOADED here.

if (ocfs2_mount_local(osb) &&

    (journal->j_journal->j_flags & JBD2_LOADED)) {
	...
}

BTW, could you please also replace 'osb->journal->j_num_trans' to 'journal->j_num_trans'?

fs/ocfs2/journal.c int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed) · line 1179

Quoted 8 lines
fs/ocfs2/journal.c +1 -0
 		jbd2_journal_lock_updates(journal->j_journal);
 		status = jbd2_journal_flush(journal->j_journal, 0);
 		jbd2_journal_unlock_updates(journal->j_journal);
@@ -1174,6 +1176,7 @@ int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
 		}
 	} else
 		osb->commit_task = NULL;
+	journal->j_state = OCFS2_JOURNAL_LOADED;

It seems that this has to be moved just after jbd2_journal_load(). Anyway, I don't think we have to introduce a new state. See above.

Joseph

Quoted 15 lines
fs/ocfs2/journal.c +0 -0
 done:
 	return status;
fs/ocfs2/journal.h +1 -0
index e3c3a35dc5e0..a80f76a8fa0e 100644
@@ -15,6 +15,7 @@
 
 enum ocfs2_journal_state {
 	OCFS2_JOURNAL_FREE = 0,
+	OCFS2_JOURNAL_INITED,
 	OCFS2_JOURNAL_LOADED,
 	OCFS2_JOURNAL_IN_SHUTDOWN,
 };
author
#2 lore raw
Quoted 82 lines · Joseph Qi
Quoted 65 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.

To resolve this issue, a new state OCFS2_JOURNAL_INITED has been introduced to replace the previous functionality of OCFS2_JOURNAL_LOADED, the original OCFS2_JOURNAL_LOADED is only set when ocfs2_journal_load() is successful. The jbd2_journal_flush() function is allowed to be called only when this flag is set. The logic here is that if the journal has even not been successfully loaded, there is no need to flush the journal.

---
 fs/ocfs2/journal.c | 9 ++++++---
 fs/ocfs2/journal.h | 1 +

 2 files changed, 7 insertions(+), 3 deletions(-)
fs/ocfs2/journal.c +0 -0
index 530fba34f6d3..da0ffcc5de0a 100644
@@ -968,7 +968,7 @@ int ocfs2_journal_init(struct ocfs2_super *osb,
 int *dirty)


        ocfs2_set_journal_params(osb);

-       journal->j_state = OCFS2_JOURNAL_LOADED; +       journal->j_state = OCFS2_JOURNAL_INITED;

        status = 0;
 done:

fs/ocfs2/journal.c +1 -0
@@ -1039,6 +1039,7 @@ void ocfs2_journal_shutdown(struct
 ocfs2_super *osb)
         int status = 0;
         struct inode *inode = NULL;
         int num_running_trans = 0;
+       enum ocfs2_journal_state state;


        BUG_ON(!osb);

fs/ocfs2/journal.c +0 -0
@@ -1047,8 +1048,9 @@ void ocfs2_journal_shutdown(struct
 ocfs2_super *osb)
                 goto done;


        inode = journal->j_inode;
+       state = journal->j_state;

-       if (journal->j_state != OCFS2_JOURNAL_LOADED)
+       if (state != OCFS2_JOURNAL_INITED && state != OCFS2_JOURNAL_LOADED)
                goto done;

        /* need to inc inode use count - jbd2_journal_destroy will iput. */

fs/ocfs2/journal.c +0 -0
@@ -1076,7 +1078,7 @@ void ocfs2_journal_shutdown(struct
 ocfs2_super *osb)
Quoted 6 lines


        BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
-       if (ocfs2_mount_local(osb)) {
+       if (ocfs2_mount_local(osb) && state == OCFS2_JOURNAL_LOADED) {

The only intent of the new introduced state is to identify if journal is
truly loaded or not.
So it seems that the simplest way to fix this is just check JBD2_LOADED
here.

Quoted 4 lines

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

Hi, Joseph, thanks for your review and comments.

Yeah! It's absolutely the simplest and cleanest way to fix this issue. Thanks for your suggestion.

Quoted 3 lines

BTW, could you please also replace 'osb->journal->j_num_trans' to 'journal->j_num_trans'?

Sure.

Quoted 14 lines

fs/ocfs2/journal.c int ocfs2_journal_load(struct ocfs2_journal · line 1180

Quoted 10 lines

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

fs/ocfs2/journal.c +1 -0
@@ -1174,6 +1176,7 @@ int ocfs2_journal_load(struct ocfs2_journal
 *journal, int local, int replayed)
                 }
         } else
                 osb->commit_task = NULL;
+       journal->j_state = OCFS2_JOURNAL_LOADED;

It seems that this has to be moved just after jbd2_journal_load(). Anyway, I don't think we have to introduce a new state. See above.

Agreed. And now OCFS2_JOURNAL_LOADED is set when ocfs2_journal_init() succeed, it may led to some misunderstanding: the journal was not really loaded when OCFS2_JOURNAL_LOADED was set. I would like to rename it to OCFS2_JOURNAL_INITED in another patch to make it clearer.

Quoted 17 lines

Joseph

Quoted 8 lines


 done:
        return status;

fs/ocfs2/journal.h +0 -0
index e3c3a35dc5e0..a80f76a8fa0e 100644
@@ -15,6 +15,7 @@
Quoted 7 lines


 enum ocfs2_journal_state {
        OCFS2_JOURNAL_FREE = 0,
+       OCFS2_JOURNAL_INITED,
        OCFS2_JOURNAL_LOADED,
        OCFS2_JOURNAL_IN_SHUTDOWN,
 };

Thanks,

Signature · 1 line
Julian Sun <[email protected]>
maintainer
#3 lore raw
Quoted 109 lines · Julian Sun
Quoted 82 lines · Joseph Qi
Quoted 65 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.

To resolve this issue, a new state OCFS2_JOURNAL_INITED has been introduced to replace the previous functionality of OCFS2_JOURNAL_LOADED, the original OCFS2_JOURNAL_LOADED is only set when ocfs2_journal_load() is successful. The jbd2_journal_flush() function is allowed to be called only when this flag is set. The logic here is that if the journal has even not been successfully loaded, there is no need to flush the journal.

---
 fs/ocfs2/journal.c | 9 ++++++---
 fs/ocfs2/journal.h | 1 +

 2 files changed, 7 insertions(+), 3 deletions(-)
fs/ocfs2/journal.c +0 -0
index 530fba34f6d3..da0ffcc5de0a 100644
@@ -968,7 +968,7 @@ int ocfs2_journal_init(struct ocfs2_super *osb,
 int *dirty)


        ocfs2_set_journal_params(osb);

-       journal->j_state = OCFS2_JOURNAL_LOADED; +       journal->j_state = OCFS2_JOURNAL_INITED;

        status = 0;
 done:

fs/ocfs2/journal.c +1 -0
@@ -1039,6 +1039,7 @@ void ocfs2_journal_shutdown(struct
 ocfs2_super *osb)
         int status = 0;
         struct inode *inode = NULL;
         int num_running_trans = 0;
+       enum ocfs2_journal_state state;


        BUG_ON(!osb);

fs/ocfs2/journal.c +0 -0
@@ -1047,8 +1048,9 @@ void ocfs2_journal_shutdown(struct
 ocfs2_super *osb)
                 goto done;


        inode = journal->j_inode;
+       state = journal->j_state;

-       if (journal->j_state != OCFS2_JOURNAL_LOADED)
+       if (state != OCFS2_JOURNAL_INITED && state != OCFS2_JOURNAL_LOADED)
                goto done;

        /* need to inc inode use count - jbd2_journal_destroy will iput. */

fs/ocfs2/journal.c +0 -0
@@ -1076,7 +1078,7 @@ void ocfs2_journal_shutdown(struct
 ocfs2_super *osb)
Quoted 6 lines


        BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
-       if (ocfs2_mount_local(osb)) {
+       if (ocfs2_mount_local(osb) && state == OCFS2_JOURNAL_LOADED) {

The only intent of the new introduced state is to identify if journal is
truly loaded or not.
So it seems that the simplest way to fix this is just check JBD2_LOADED
here.

Quoted 4 lines

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

Hi, Joseph, thanks for your review and comments.

Yeah! It's absolutely the simplest and cleanest way to fix this issue. Thanks for your suggestion.

Quoted 3 lines

BTW, could you please also replace 'osb->journal->j_num_trans' to 'journal->j_num_trans'?

Sure.

Quoted 14 lines

fs/ocfs2/journal.c int ocfs2_journal_load(struct ocfs2_journal · line 1180

Quoted 10 lines

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

fs/ocfs2/journal.c +1 -0
@@ -1174,6 +1176,7 @@ int ocfs2_journal_load(struct ocfs2_journal
 *journal, int local, int replayed)
                 }
         } else
                 osb->commit_task = NULL;
+       journal->j_state = OCFS2_JOURNAL_LOADED;

It seems that this has to be moved just after jbd2_journal_load(). Anyway, I don't think we have to introduce a new state. See above.

Quoted 4 lines

Agreed. And now OCFS2_JOURNAL_LOADED is set when ocfs2_journal_init() succeed, it may led to some misunderstanding: the journal was not really loaded when OCFS2_JOURNAL_LOADED was set. I would like to rename it to OCFS2_JOURNAL_INITED in another patch to make it clearer.

Umm... You can treat it as ocfs2 journal state only, not jbd2. Also jbd2 doesn't have 'initialized' state either. So I don't think we have to do this change in the fix.

Joseph

author
#4 lore raw
Quoted 130 lines · Joseph Qi
Quoted 120 lines · Julian Sun
Quoted 87 lines · Joseph Qi
Quoted 69 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.

To resolve this issue, a new state OCFS2_JOURNAL_INITED has been introduced to replace the previous functionality of OCFS2_JOURNAL_LOADED, the original OCFS2_JOURNAL_LOADED is only set when ocfs2_journal_load() is successful. The jbd2_journal_flush() function is allowed to be called only when this flag is set. The logic here is that if the journal has even not been successfully loaded, there is no need to flush the journal.

Link:
https://syzkaller.appspot.com/bug?extid=05b9b39d8bdfe1a0861f
Reported-by:
[email protected]

---
 fs/ocfs2/journal.c | 9 ++++++---
 fs/ocfs2/journal.h | 1 +

 2 files changed, 7 insertions(+), 3 deletions(-)
fs/ocfs2/journal.c +0 -0
index 530fba34f6d3..da0ffcc5de0a 100644
@@ -968,7 +968,7 @@ int ocfs2_journal_init(struct ocfs2_super
 *osb,
 int *dirty)


        ocfs2_set_journal_params(osb);

-       journal->j_state = OCFS2_JOURNAL_LOADED; +       journal->j_state = OCFS2_JOURNAL_INITED;

        status = 0;
 done:

fs/ocfs2/journal.c +1 -0
@@ -1039,6 +1039,7 @@ void ocfs2_journal_shutdown(struct
 ocfs2_super *osb)
         int status = 0;
         struct inode *inode = NULL;
         int num_running_trans = 0;
+       enum ocfs2_journal_state state;


        BUG_ON(!osb);

fs/ocfs2/journal.c +0 -0
@@ -1047,8 +1048,9 @@ void ocfs2_journal_shutdown(struct
 ocfs2_super *osb)
                 goto done;


        inode = journal->j_inode;
+       state = journal->j_state;

-       if (journal->j_state != OCFS2_JOURNAL_LOADED) +       if (state != OCFS2_JOURNAL_INITED && state != OCFS2_JOURNAL_LOADED)
                goto done;

        /* need to inc inode use count - jbd2_journal_destroy will
iput. */

fs/ocfs2/journal.c +0 -0
@@ -1076,7 +1078,7 @@ void ocfs2_journal_shutdown(struct
 ocfs2_super *osb)
Quoted 6 lines


        BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
-       if (ocfs2_mount_local(osb)) {
+       if (ocfs2_mount_local(osb) && state == OCFS2_JOURNAL_LOADED) {

The only intent of the new introduced state is to identify if journal
is
truly loaded or not.
So it seems that the simplest way to fix this is just check JBD2_LOADED
here.

Quoted 4 lines

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

Hi, Joseph, thanks for your review and comments.

Yeah! It's absolutely the simplest and cleanest way to fix this issue.
Thanks for your suggestion.

Quoted 3 lines

BTW, could you please also replace 'osb->journal->j_num_trans' to 'journal->j_num_trans'?

Sure.

Quoted 6 lines
Quoted 6 lines

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

Quoted 1 line

j_journal);

Quoted 13 lines
Quoted 7 lines
fs/ocfs2/journal.c +0 -0
@@ -1174,6 +1176,7 @@ int ocfs2_journal_load(struct

ocfs2_journal
*journal, int local, int replayed)
                }
        } else
                osb->commit_task = NULL;
+       journal->j_state = OCFS2_JOURNAL_LOADED;

It seems that this has to be moved just after jbd2_journal_load().
Anyway, I don't think we have to introduce a new state. See above.

Quoted 6 lines

Agreed. And now OCFS2_JOURNAL_LOADED is set when ocfs2_journal_init()
succeed, it may led to some misunderstanding: the journal was not really loaded when OCFS2_JOURNAL_LOADED was set. I would like to rename
it to OCFS2_JOURNAL_INITED in another patch to make it clearer.

Quoted 5 lines

Umm... You can treat it as ocfs2 journal state only, not jbd2. Also jbd2 doesn't have 'initialized' state either. So I don't think we have to do this change in the fix.

Joseph

Got it. Thanks for your explanation.

Thanks,

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