Skip to contentSkip to conversation

ext4: fix null-ptr-deref in ext4_write_info

PATCH Applied Baokun Li 4 participants 5 messages Aug 5, 2022 – Sep 27, 2022 ext4 file system

lore
Share & tools
lore

Baokun Li

author
#0 lore raw

PATCH 1/1 ext4: fix null-ptr-deref in ext4_write_info

I caught a null-ptr-deref bug as follows:
==================================================================

KASAN: null-ptr-deref in range [0x0000000000000068-0x000000000000006f]
CPU: 1 PID: 1589 Comm: umount Not tainted 5.10.0-02219-dirty #339
RIP: 0010:ext4_write_info+0x53/0x1b0
[...]
Call Trace:
 dquot_writeback_dquots+0x341/0x9a0
 ext4_sync_fs+0x19e/0x800
 __sync_filesystem+0x83/0x100
 sync_filesystem+0x89/0xf0
 generic_shutdown_super+0x79/0x3e0
 kill_block_super+0xa1/0x110
 deactivate_locked_super+0xac/0x130
 deactivate_super+0xb6/0xd0
 cleanup_mnt+0x289/0x400
 __cleanup_mnt+0x16/0x20
 task_work_run+0x11c/0x1c0
 exit_to_user_mode_prepare+0x203/0x210
 syscall_exit_to_user_mode+0x5b/0x3a0
 do_syscall_64+0x59/0x70
 entry_SYSCALL_64_after_hwframe+0x44/0xa9
 ==================================================================

Above issue may happen as follows:
-------------------------------------
exit_to_user_mode_prepare
task_work_run
__cleanup_mnt
cleanup_mnt

    deactivate_super
     deactivate_locked_super
      kill_block_super
       generic_shutdown_super
        shrink_dcache_for_umount
         dentry = sb->s_root
         sb->s_root = NULL              <--- Here set NULL
        sync_filesystem
         __sync_filesystem
          sb->s_op->sync_fs > ext4_sync_fs
           dquot_writeback_dquots
            sb->dq_op->write_info > ext4_write_info
             ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2)
              d_inode(sb->s_root)
               s_root->d_inode          <--- Null pointer dereference

To solve this problem, we use ext4_journal_start_sb directly to avoid s_root being used.

---
 fs/ext4/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/super.c +1 -1
index 9a66abcca1a8..0ce4565422f6 100644
@@ -6653,7 +6653,7 @@ static int ext4_write_info(struct super_block *sb, int type)
 	handle_t *handle;
 
 	/* Data block + inode block */
-	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
+	handle = ext4_journal_start_sb(sb, EXT4_HT_QUOTA, 2);
 	if (IS_ERR(handle))
 		return PTR_ERR(handle);
 	ret = dquot_commit_info(sb, type);
Signature · 1 line
2.31.1
#1 lore raw
Quoted 46 lines · Baokun Li

I caught a null-ptr-deref bug as follows:
==================================================================

KASAN: null-ptr-deref in range [0x0000000000000068-0x000000000000006f]
CPU: 1 PID: 1589 Comm: umount Not tainted 5.10.0-02219-dirty #339
RIP: 0010:ext4_write_info+0x53/0x1b0
[...]
Call Trace:
 dquot_writeback_dquots+0x341/0x9a0
 ext4_sync_fs+0x19e/0x800
 __sync_filesystem+0x83/0x100
 sync_filesystem+0x89/0xf0
 generic_shutdown_super+0x79/0x3e0
 kill_block_super+0xa1/0x110
 deactivate_locked_super+0xac/0x130
 deactivate_super+0xb6/0xd0
 cleanup_mnt+0x289/0x400
 __cleanup_mnt+0x16/0x20
 task_work_run+0x11c/0x1c0
 exit_to_user_mode_prepare+0x203/0x210
 syscall_exit_to_user_mode+0x5b/0x3a0
 do_syscall_64+0x59/0x70
 entry_SYSCALL_64_after_hwframe+0x44/0xa9
 ==================================================================

Above issue may happen as follows:
-------------------------------------
exit_to_user_mode_prepare
task_work_run
__cleanup_mnt
cleanup_mnt

    deactivate_super
     deactivate_locked_super
      kill_block_super
       generic_shutdown_super
        shrink_dcache_for_umount
         dentry = sb->s_root
         sb->s_root = NULL              <--- Here set NULL
        sync_filesystem
         __sync_filesystem
          sb->s_op->sync_fs > ext4_sync_fs
           dquot_writeback_dquots
            sb->dq_op->write_info > ext4_write_info
             ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2)
              d_inode(sb->s_root)
               s_root->d_inode          <--- Null pointer dereference
Quoted 2 lines

To solve this problem, we use ext4_journal_start_sb directly to avoid s_root being used.

Are we syncing the file system after the superblock shutdown and getting away with it? This does not look good. Do you have a reproducer?

Thanks!
-Lukas

Quoted 22 lines
---
 fs/ext4/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/super.c +1 -1
index 9a66abcca1a8..0ce4565422f6 100644
@@ -6653,7 +6653,7 @@ static int ext4_write_info(struct super_block *sb, int type)
 	handle_t *handle;
 
 	/* Data block + inode block */
-	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
+	handle = ext4_journal_start_sb(sb, EXT4_HT_QUOTA, 2);
 	if (IS_ERR(handle))
 		return PTR_ERR(handle);
 	ret = dquot_commit_info(sb, type);
Signature · 1 line
2.31.1
author
#2 lore raw
Quoted 47 lines · Lukas Czerner
Quoted 46 lines · Baokun Li

I caught a null-ptr-deref bug as follows:
==================================================================

KASAN: null-ptr-deref in range [0x0000000000000068-0x000000000000006f]
CPU: 1 PID: 1589 Comm: umount Not tainted 5.10.0-02219-dirty #339
RIP: 0010:ext4_write_info+0x53/0x1b0
[...]
Call Trace:
  dquot_writeback_dquots+0x341/0x9a0
  ext4_sync_fs+0x19e/0x800
  __sync_filesystem+0x83/0x100
  sync_filesystem+0x89/0xf0
  generic_shutdown_super+0x79/0x3e0
  kill_block_super+0xa1/0x110
  deactivate_locked_super+0xac/0x130
  deactivate_super+0xb6/0xd0
  cleanup_mnt+0x289/0x400
  __cleanup_mnt+0x16/0x20
  task_work_run+0x11c/0x1c0
  exit_to_user_mode_prepare+0x203/0x210
  syscall_exit_to_user_mode+0x5b/0x3a0
  do_syscall_64+0x59/0x70
  entry_SYSCALL_64_after_hwframe+0x44/0xa9
  ==================================================================

Above issue may happen as follows:
-------------------------------------
exit_to_user_mode_prepare
task_work_run
__cleanup_mnt

    cleanup_mnt
     deactivate_super
      deactivate_locked_super
       kill_block_super
        generic_shutdown_super
         shrink_dcache_for_umount
          dentry = sb->s_root
          sb->s_root = NULL              <--- Here set NULL
         sync_filesystem
          __sync_filesystem
           sb->s_op->sync_fs > ext4_sync_fs
            dquot_writeback_dquots
             sb->dq_op->write_info > ext4_write_info
              ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2)
               d_inode(sb->s_root)
                s_root->d_inode          <--- Null pointer dereference
Quoted 7 lines
Quoted 2 lines

To solve this problem, we use ext4_journal_start_sb directly to avoid s_root being used.

Are we syncing the file system after the superblock shutdown and getting away with it? This does not look good. Do you have a reproducer?

Thanks!
-Lukas

Hi, Lukas!

This problem is triggered by a pressure test when I reproduce another problem.

So I didn't have the reproducer.

I looked at the error stack code and found that it seems there is something wrong.

Moreover, it's really weird to write code like "ext4_journal_start(d_inode(sb->s_root), ...)".

Quoted 12 lines
Quoted 12 lines
---
  fs/ext4/super.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/super.c +0 -0
index 9a66abcca1a8..0ce4565422f6 100644
@@ -6653,7 +6653,7 @@ static int ext4_write_info(struct super_block *sb, int type)
  	handle_t *handle;
 
Quoted 10 lines
Quoted 9 lines
fs/ext4/super.c +1 -1
  	/* Data block + inode block */
-	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
+	handle = ext4_journal_start_sb(sb, EXT4_HT_QUOTA, 2);
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	ret = dquot_commit_info(sb, type);
Signature · 1 line
2.31.1

.

Thanks!

Signature · 2 lines
With Best Regards,
Baokun Li
reviewer Reviewed-by
#3 lore raw
Quoted 61 lines · Baokun Li
Quoted 47 lines · Lukas Czerner
Quoted 46 lines · Baokun Li

I caught a null-ptr-deref bug as follows:
==================================================================

KASAN: null-ptr-deref in range [0x0000000000000068-0x000000000000006f]
CPU: 1 PID: 1589 Comm: umount Not tainted 5.10.0-02219-dirty #339
RIP: 0010:ext4_write_info+0x53/0x1b0
[...]
Call Trace:
  dquot_writeback_dquots+0x341/0x9a0
  ext4_sync_fs+0x19e/0x800
  __sync_filesystem+0x83/0x100
  sync_filesystem+0x89/0xf0
  generic_shutdown_super+0x79/0x3e0
  kill_block_super+0xa1/0x110
  deactivate_locked_super+0xac/0x130
  deactivate_super+0xb6/0xd0
  cleanup_mnt+0x289/0x400
  __cleanup_mnt+0x16/0x20
  task_work_run+0x11c/0x1c0
  exit_to_user_mode_prepare+0x203/0x210
  syscall_exit_to_user_mode+0x5b/0x3a0
  do_syscall_64+0x59/0x70
  entry_SYSCALL_64_after_hwframe+0x44/0xa9
  ==================================================================

Above issue may happen as follows:
-------------------------------------
exit_to_user_mode_prepare
task_work_run
__cleanup_mnt

    cleanup_mnt
     deactivate_super
      deactivate_locked_super
       kill_block_super
        generic_shutdown_super
         shrink_dcache_for_umount
          dentry = sb->s_root
          sb->s_root = NULL              <--- Here set NULL
         sync_filesystem
          __sync_filesystem
           sb->s_op->sync_fs > ext4_sync_fs
            dquot_writeback_dquots
             sb->dq_op->write_info > ext4_write_info
              ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2)
               d_inode(sb->s_root)
                s_root->d_inode          <--- Null pointer dereference
Quoted 7 lines
Quoted 2 lines

To solve this problem, we use ext4_journal_start_sb directly to avoid s_root being used.

Are we syncing the file system after the superblock shutdown and getting away with it? This does not look good. Do you have a reproducer?

Thanks!
-Lukas

Hi, Lukas!

This problem is triggered by a pressure test when I reproduce another problem.

Quoted 7 lines

So I didn't have the reproducer.

I looked at the error stack code and found that it seems there is something wrong.

Moreover, it's really weird to write code like
"ext4_journal_start(d_inode(sb->s_root), ...)".

I agree. That looks like some leftover from the past. Feel free to add:

Honza

Quoted 30 lines
Quoted 6 lines
Quoted 6 lines
---
  fs/ext4/super.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines
Quoted 15 lines
fs/ext4/super.c +1 -1
index 9a66abcca1a8..0ce4565422f6 100644
@@ -6653,7 +6653,7 @@ static int ext4_write_info(struct super_block *sb, int type)
  	handle_t *handle;
  	/* Data block + inode block */
-	handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
+	handle = ext4_journal_start_sb(sb, EXT4_HT_QUOTA, 2);
  	if (IS_ERR(handle))
  		return PTR_ERR(handle);
  	ret = dquot_commit_info(sb, type);
Signature · 1 line
2.31.1

.

Thanks!

Signature · 2 lines
With Best Regards,
Baokun Li
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#4 lore raw
In reply to Baokun Li
Quoted 24 lines · Baokun Li

I caught a null-ptr-deref bug as follows:
==================================================================

KASAN: null-ptr-deref in range [0x0000000000000068-0x000000000000006f]
CPU: 1 PID: 1589 Comm: umount Not tainted 5.10.0-02219-dirty #339
RIP: 0010:ext4_write_info+0x53/0x1b0
[...]
Call Trace:
 dquot_writeback_dquots+0x341/0x9a0
 ext4_sync_fs+0x19e/0x800
 __sync_filesystem+0x83/0x100
 sync_filesystem+0x89/0xf0
 generic_shutdown_super+0x79/0x3e0
 kill_block_super+0xa1/0x110
 deactivate_locked_super+0xac/0x130
 deactivate_super+0xb6/0xd0
 cleanup_mnt+0x289/0x400
 __cleanup_mnt+0x16/0x20
 task_work_run+0x11c/0x1c0
 exit_to_user_mode_prepare+0x203/0x210
 syscall_exit_to_user_mode+0x5b/0x3a0
 do_syscall_64+0x59/0x70
 entry_SYSCALL_64_after_hwframe+0x44/0xa9
 ==================================================================
Quoted 1 line

[...]

Applied, thanks!

[1/1] ext4: fix null-ptr-deref in ext4_write_info

      commit: 647642bf8f326994d7eaf785bba3fa9dad92cff0

Best regards,

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