Skip to contentSkip to conversation

jbd2: Fix use after free in jbd2_log_do_checkpoint()

PATCH Applied Jan Kara 4 participants 9 messages Oct 4, 2018 – Oct 6, 2018

Subsystems (2)
lore
Share & tools
lore

Jan Kara

author
#0 lore raw

PATCH 1/1 jbd2: Fix use after free in jbd2_log_do_checkpoint()

The code cleaning transaction's lists of checkpoint buffers has a bug where it increases bh refcount only after releasing journal->j_list_lock. Thus the following race is possible:

CPU0 CPU1
jbd2_log_do_checkpoint()
jbd2_journal_try_to_free_buffers()
__journal_try_to_free_buffer(bh)
...
while (transaction->t_checkpoint_io_list)
...

    if (buffer_locked(bh)) {

<-- IO completes now, buffer gets unlocked -->

      spin_unlock(&journal->j_list_lock);
					    spin_lock(&journal->j_list_lock);
					    __jbd2_journal_remove_checkpoint(jh);
					    spin_unlock(&journal->j_list_lock);
					  try_to_free_buffers(page);
      get_bh(bh) <-- accesses freed bh

Fix the problem by grabbing bh reference before unlocking journal->j_list_lock.

Fixes dc6e8d669cf5cb3ff84707c372c0a2a8a5e80845
Fixes be1158cc615fd723552f0d9912087423c7cadda5
Reported-by [email protected]
---
 fs/jbd2/checkpoint.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/jbd2/checkpoint.c +2 -2
index c125d662777c..26f8d7e46462 100644
@@ -251,8 +251,8 @@ int jbd2_log_do_checkpoint(journal_t *journal)
 		bh = jh2bh(jh);
 
 		if (buffer_locked(bh)) {
-			spin_unlock(&journal->j_list_lock);
 			get_bh(bh);
+			spin_unlock(&journal->j_list_lock);
 			wait_on_buffer(bh);
 			/* the journal_head may have gone by now */
 			BUFFER_TRACE(bh, "brelse");
@@ -333,8 +333,8 @@ int jbd2_log_do_checkpoint(journal_t *journal)
 		jh = transaction->t_checkpoint_io_list;
 		bh = jh2bh(jh);
 		if (buffer_locked(bh)) {
-			spin_unlock(&journal->j_list_lock);
 			get_bh(bh);
+			spin_unlock(&journal->j_list_lock);
 			wait_on_buffer(bh);
 			/* the journal_head may have gone by now */
 			BUFFER_TRACE(bh, "brelse");
Signature · 1 line
2.16.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 22 lines · Jan Kara

The code cleaning transaction's lists of checkpoint buffers has a bug where it increases bh refcount only after releasing journal->j_list_lock. Thus the following race is possible:

CPU0 CPU1
jbd2_log_do_checkpoint()
jbd2_journal_try_to_free_buffers()
__journal_try_to_free_buffer(bh)
...
while (transaction->t_checkpoint_io_list)
...

    if (buffer_locked(bh)) {

<-- IO completes now, buffer gets unlocked -->

      spin_unlock(&journal->j_list_lock);
					    spin_lock(&journal->j_list_lock);
					    __jbd2_journal_remove_checkpoint(jh);
					    spin_unlock(&journal->j_list_lock);
					  try_to_free_buffers(page);
      get_bh(bh) <-- accesses freed bh
Quoted 2 lines

Fix the problem by grabbing bh reference before unlocking journal->j_list_lock.

Hi Jan,

nice catch. The patch looks good, you can add

Btw, do you by any chance have a reproducer for this ?

-Lukas

Quoted 37 lines
Fixes dc6e8d669cf5cb3ff84707c372c0a2a8a5e80845
Fixes be1158cc615fd723552f0d9912087423c7cadda5
Reported-by [email protected]
---
 fs/jbd2/checkpoint.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/jbd2/checkpoint.c +2 -2
index c125d662777c..26f8d7e46462 100644
@@ -251,8 +251,8 @@ int jbd2_log_do_checkpoint(journal_t *journal)
 		bh = jh2bh(jh);
 
 		if (buffer_locked(bh)) {
-			spin_unlock(&journal->j_list_lock);
 			get_bh(bh);
+			spin_unlock(&journal->j_list_lock);
 			wait_on_buffer(bh);
 			/* the journal_head may have gone by now */
 			BUFFER_TRACE(bh, "brelse");
@@ -333,8 +333,8 @@ int jbd2_log_do_checkpoint(journal_t *journal)
 		jh = transaction->t_checkpoint_io_list;
 		bh = jh2bh(jh);
 		if (buffer_locked(bh)) {
-			spin_unlock(&journal->j_list_lock);
 			get_bh(bh);
+			spin_unlock(&journal->j_list_lock);
 			wait_on_buffer(bh);
 			/* the journal_head may have gone by now */
 			BUFFER_TRACE(bh, "brelse");
Signature · 1 line
2.16.4
author
#2 lore raw
Quoted 26 lines · Lukas Czerner
Quoted 22 lines · Jan Kara

The code cleaning transaction's lists of checkpoint buffers has a bug where it increases bh refcount only after releasing journal->j_list_lock. Thus the following race is possible:

CPU0 CPU1
jbd2_log_do_checkpoint()
jbd2_journal_try_to_free_buffers()
__journal_try_to_free_buffer(bh)
...
while (transaction->t_checkpoint_io_list)
...

    if (buffer_locked(bh)) {

<-- IO completes now, buffer gets unlocked -->

      spin_unlock(&journal->j_list_lock);
					    spin_lock(&journal->j_list_lock);
					    __jbd2_journal_remove_checkpoint(jh);
					    spin_unlock(&journal->j_list_lock);
					  try_to_free_buffers(page);
      get_bh(bh) <-- accesses freed bh
Quoted 2 lines

Fix the problem by grabbing bh reference before unlocking journal->j_list_lock.

Quoted 7 lines

Hi Jan,

nice catch. The patch looks good, you can add

Btw, do you by any chance have a reproducer for this ?

No, syzbot hit it but the race window is really small so I don't think you can create reasonably reliable reproducer...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw
In reply to Jan Kara
Quoted 22 lines · Jan Kara

The code cleaning transaction's lists of checkpoint buffers has a bug where it increases bh refcount only after releasing journal->j_list_lock. Thus the following race is possible:

CPU0 CPU1
jbd2_log_do_checkpoint()
jbd2_journal_try_to_free_buffers()
__journal_try_to_free_buffer(bh)
...
while (transaction->t_checkpoint_io_list)
...

    if (buffer_locked(bh)) {

<-- IO completes now, buffer gets unlocked -->

      spin_unlock(&journal->j_list_lock);
					    spin_lock(&journal->j_list_lock);
					    __jbd2_journal_remove_checkpoint(jh);
					    spin_unlock(&journal->j_list_lock);
					  try_to_free_buffers(page);
      get_bh(bh) <-- accesses freed bh
Quoted 5 lines

Fix the problem by grabbing bh reference before unlocking journal->j_list_lock.

Fixes dc6e8d669cf5cb3ff84707c372c0a2a8a5e80845
Fixes be1158cc615fd723552f0d9912087423c7cadda5

Nit, this normally looks like:

Fixes dc6e8d669cf5 ("jbd2: don't call get_bh() before calling __jbd2_journal_remove_checkpoint()")

And this is created by:

	git show -s --abbrev-commit --abbrev=12 --pretty=format:"%h (\"%s\")%n"

thanks

greg k-h

author
#4 lore raw
Quoted 29 lines · Greg KH
Quoted 22 lines · Jan Kara

The code cleaning transaction's lists of checkpoint buffers has a bug where it increases bh refcount only after releasing journal->j_list_lock. Thus the following race is possible:

CPU0 CPU1
jbd2_log_do_checkpoint()
jbd2_journal_try_to_free_buffers()
__journal_try_to_free_buffer(bh)
...
while (transaction->t_checkpoint_io_list)
...

    if (buffer_locked(bh)) {

<-- IO completes now, buffer gets unlocked -->

      spin_unlock(&journal->j_list_lock);
					    spin_lock(&journal->j_list_lock);
					    __jbd2_journal_remove_checkpoint(jh);
					    spin_unlock(&journal->j_list_lock);
					  try_to_free_buffers(page);
      get_bh(bh) <-- accesses freed bh
Quoted 5 lines

Fix the problem by grabbing bh reference before unlocking journal->j_list_lock.

Fixes dc6e8d669cf5cb3ff84707c372c0a2a8a5e80845
Fixes be1158cc615fd723552f0d9912087423c7cadda5
Quoted 6 lines

Nit, this normally looks like:

Fixes dc6e8d669cf5 ("jbd2: don't call get_bh() before calling __jbd2_journal_remove_checkpoint()")

And this is created by:

	git show -s --abbrev-commit --abbrev=12 --pretty=format:"%h (\"%s\")%n"

OK, ok, will do next time. The full git commit ID is faster to cut-n-paste which shows how lazy I'm ;)

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw
Quoted 32 lines · Jan Kara
Quoted 31 lines · Greg KH
Quoted 22 lines · Jan Kara

The code cleaning transaction's lists of checkpoint buffers has a bug where it increases bh refcount only after releasing journal->j_list_lock. Thus the following race is possible:

CPU0 CPU1
jbd2_log_do_checkpoint()
jbd2_journal_try_to_free_buffers()
__journal_try_to_free_buffer(bh)
...
while (transaction->t_checkpoint_io_list)
...

    if (buffer_locked(bh)) {

<-- IO completes now, buffer gets unlocked -->

      spin_unlock(&journal->j_list_lock);
					    spin_lock(&journal->j_list_lock);
					    __jbd2_journal_remove_checkpoint(jh);
					    spin_unlock(&journal->j_list_lock);
					  try_to_free_buffers(page);
      get_bh(bh) <-- accesses freed bh
Quoted 5 lines

Fix the problem by grabbing bh reference before unlocking journal->j_list_lock.

Fixes dc6e8d669cf5cb3ff84707c372c0a2a8a5e80845
Fixes be1158cc615fd723552f0d9912087423c7cadda5

Nit, this normally looks like:

Quoted 7 lines
Quoted 4 lines
Fixes dc6e8d669cf5 ("jbd2: don't call get_bh() before calling __jbd2_journal_remove_checkpoint()")

And this is created by:

	git show -s --abbrev-commit --abbrev=12 --pretty=format:"%h (\"%s\")%n"

OK, ok, will do next time. The full git commit ID is faster to cut-n-paste which shows how lazy I'm ;)

I have this in the .gitconfig

[pretty]

	fixes = Fixes: %h (\"%s\")

which helps. I think I found it in the docs somewhere.

-Lukas

Quoted 5 lines

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#6 lore raw
In reply to Jan Kara
Quoted 3 lines · Jan Kara

OK, ok, will do next time. The full git commit ID is faster to cut-n-paste which shows how lazy I'm ;)

No worires, I'll fix that up for you when I apply it.

- Ted

author
#7 lore raw
Quoted 6 lines · Theodore Y. Ts'o
Quoted 3 lines · Jan Kara

OK, ok, will do next time. The full git commit ID is faster to cut-n-paste which shows how lazy I'm ;)

No worires, I'll fix that up for you when I apply it.

Thanks!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#8 lore raw
In reply to Jan Kara
Quoted 22 lines · Jan Kara

The code cleaning transaction's lists of checkpoint buffers has a bug where it increases bh refcount only after releasing journal->j_list_lock. Thus the following race is possible:

CPU0 CPU1
jbd2_log_do_checkpoint()
jbd2_journal_try_to_free_buffers()
__journal_try_to_free_buffer(bh)
...
while (transaction->t_checkpoint_io_list)
...

    if (buffer_locked(bh)) {

<-- IO completes now, buffer gets unlocked -->

      spin_unlock(&journal->j_list_lock);
					    spin_lock(&journal->j_list_lock);
					    __jbd2_journal_remove_checkpoint(jh);
					    spin_unlock(&journal->j_list_lock);
					  try_to_free_buffers(page);
      get_bh(bh) <-- accesses freed bh
Quoted 8 lines

Fix the problem by grabbing bh reference before unlocking journal->j_list_lock.

Fixes dc6e8d669cf5cb3ff84707c372c0a2a8a5e80845
Fixes be1158cc615fd723552f0d9912087423c7cadda5
Reported-by [email protected]

Applied, thanks (with the Fixes field adjusted).

- Ted