gfs2: fix null-ptr-deref in gfs2_quota_hold during unmount
PATCH 1/1 gfs2: fix null-ptr-deref in gfs2_quota_hold during unmount
gfs2_quota_cleanup() frees sd_quota_bitmap early in gfs2_put_super() before the iput() calls that can trigger inode eviction. When an unlinked inode is evicted during unmount, it goes through:
gfs2_evict_inode() -> gfs2_dinode_dealloc() -> gfs2_quota_hold()
-> qdsb_get() -> slot_get() -> find_first_zero_bit(sd_quota_bitmap)
If sd_quota_bitmap has already been freed by gfs2_quota_cleanup(), this results in a null pointer dereference:
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] _find_first_zero_bit+0x29/0xb0 lib/find_bit.c:149 slot_get fs/gfs2/quota.c:355 qdsb_get+0x133/0x340 fs/gfs2/quota.c:522 gfs2_quota_hold+0x191/0x5c0 fs/gfs2/quota.c:606 gfs2_dinode_dealloc+0x176/0x3f0 fs/gfs2/inode.c:488 gfs2_evict_inode+0x11cd/0x1400 fs/gfs2/super.c:1479 gfs2_put_super+0x300/0x860 fs/gfs2/super.c:618
The same problem exists in both paths through gfs2_put_super(): the direct gfs2_quota_cleanup() call in the read-only branch, and indirectly via gfs2_make_fs_ro() in the read-write branch.
Fix this by removing gfs2_quota_cleanup() from gfs2_make_fs_ro() and moving it to after all the iput() calls in gfs2_put_super(), ensuring sd_quota_bitmap remains valid for the entire duration of inode eviction. Restore an explicit gfs2_quota_cleanup() call in gfs2_reconfigure() after gfs2_make_fs_ro() to preserve the remount-ro cleanup behaviour.
--- fs/gfs2/ops_fstype.c | 1 + fs/gfs2/super.c | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-)
fs/gfs2/ops_fstype.c +1 -0
@@ -1592,6 +1592,7 @@ static int gfs2_reconfigure(struct fs_context *fc)if ((sb->s_flags ^ fc->sb_flags) &SB_RDONLY ) {if (fc->sb_flags &SB_RDONLY ) {gfs2_make_fs_ro (sdp); +gfs2_quota_cleanup (sdp); }else { error =gfs2_make_fs_rw (sdp);if (error)
fs/gfs2/super.c +2 -3
@@ -565,7 +565,6 @@ void gfs2_make_fs_ro(struct gfs2_sbd *sdp) HZ * 5);gfs2_assert_warn (sdp,gfs2_log_is_empty (sdp)); } -gfs2_quota_cleanup (sdp); }/**
@@ -604,8 +603,6 @@ static void gfs2_put_super(struct super_block *sb)else {if (gfs2_withdrawn (sdp))gfs2_destroy_threads (sdp); - -gfs2_quota_cleanup (sdp); }/* At this point, we're through modifying the disk */
@@ -637,6 +634,8 @@ static void gfs2_put_super(struct super_block *sb)gfs2_glock_dq_uninit (&sdp->sd_live_gh);gfs2_clear_rgrpd (sdp);gfs2_jindex_free (sdp); + +gfs2_quota_cleanup (sdp);/* Take apart glock structures and buffer lists */ gfs2_gl_hash_clear (sdp);iput (sdp->sd_inode);
Signature · 1 line
2.43.0
Quoted 81 lines · Deepanshu Kartikey
gfs2_quota_cleanup()freessd_quota_bitmapearly ingfs2_put_super()before theiput()calls that can trigger inode eviction. When an unlinked inode is evicted during unmount, it goes through:
gfs2_evict_inode()->gfs2_dinode_dealloc()->gfs2_quota_hold()-> qdsb_get() -> slot_get() -> find_first_zero_bit(sd_quota_bitmap)If
sd_quota_bitmaphas already been freed bygfs2_quota_cleanup(), this results in a null pointer dereference:KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] _find_first_zero_bit+0x29/0xb0 lib/find_bit.c:149 slot_get fs/gfs2/quota.c:355 qdsb_get+0x133/0x340 fs/gfs2/quota.c:522 gfs2_quota_hold+0x191/0x5c0 fs/gfs2/quota.c:606 gfs2_dinode_dealloc+0x176/0x3f0 fs/gfs2/inode.c:488 gfs2_evict_inode+0x11cd/0x1400 fs/gfs2/super.c:1479 gfs2_put_super+0x300/0x860 fs/gfs2/super.c:618The same problem exists in both paths through
gfs2_put_super(): the directgfs2_quota_cleanup()call in the read-only branch, and indirectly viagfs2_make_fs_ro()in the read-write branch.Fix this by removing
gfs2_quota_cleanup()fromgfs2_make_fs_ro()and moving it to after all theiput()calls ingfs2_put_super(), ensuringsd_quota_bitmapremains valid for the entire duration of inode eviction. Restore an explicitgfs2_quota_cleanup()call ingfs2_reconfigure()aftergfs2_make_fs_ro()to preserve the remount-ro cleanup behaviour.Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- fs/gfs2/ops_fstype.c | 1 + fs/gfs2/super.c | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-)fs/gfs2/ops_fstype.c +1
-0@@ -1592,6 +1592,7 @@ static int gfs2_reconfigure(struct fs_context *fc)if ((sb->s_flags ^ fc->sb_flags) &SB_RDONLY ) {if (fc->sb_flags &SB_RDONLY ) {gfs2_make_fs_ro (sdp); +gfs2_quota_cleanup (sdp); }else { error =gfs2_make_fs_rw (sdp);if (error)fs/gfs2/super.c +2
-4@@ -565,7 +565,6 @@ void gfs2_make_fs_ro(struct gfs2_sbd *sdp) HZ * 5);gfs2_assert_warn (sdp,gfs2_log_is_empty (sdp)); } -gfs2_quota_cleanup (sdp); }/** @@ -604,8 +603,6 @@ static void gfs2_put_super(struct super_block *sb)else {if (gfs2_withdrawn (sdp))gfs2_destroy_threads (sdp); - -gfs2_quota_cleanup (sdp); }/* At this point, we're through modifying the disk */ @@ -637,6 +634,8 @@ static void gfs2_put_super(struct super_block *sb)gfs2_glock_dq_uninit (&sdp->sd_live_gh);gfs2_clear_rgrpd (sdp);gfs2_jindex_free (sdp); + +gfs2_quota_cleanup (sdp);/* Take apart glock structures and buffer lists */ gfs2_gl_hash_clear (sdp);iput (sdp->sd_inode); --
Quoted 2 lines
2.43.0
Hi Agruenba,
Gentle reminder. Please let me know the status of this patch
Thanks
Deepanshu
Quoted 81 lines · Deepanshu Kartikey
gfs2_quota_cleanup()freessd_quota_bitmapearly ingfs2_put_super()before theiput()calls that can trigger inode eviction. When an unlinked inode is evicted during unmount, it goes through:
gfs2_evict_inode()->gfs2_dinode_dealloc()->gfs2_quota_hold()-> qdsb_get() -> slot_get() -> find_first_zero_bit(sd_quota_bitmap)If
sd_quota_bitmaphas already been freed bygfs2_quota_cleanup(), this results in a null pointer dereference:KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] _find_first_zero_bit+0x29/0xb0 lib/find_bit.c:149 slot_get fs/gfs2/quota.c:355 qdsb_get+0x133/0x340 fs/gfs2/quota.c:522 gfs2_quota_hold+0x191/0x5c0 fs/gfs2/quota.c:606 gfs2_dinode_dealloc+0x176/0x3f0 fs/gfs2/inode.c:488 gfs2_evict_inode+0x11cd/0x1400 fs/gfs2/super.c:1479 gfs2_put_super+0x300/0x860 fs/gfs2/super.c:618The same problem exists in both paths through
gfs2_put_super(): the directgfs2_quota_cleanup()call in the read-only branch, and indirectly viagfs2_make_fs_ro()in the read-write branch.Fix this by removing
gfs2_quota_cleanup()fromgfs2_make_fs_ro()and moving it to after all theiput()calls ingfs2_put_super(), ensuringsd_quota_bitmapremains valid for the entire duration of inode eviction. Restore an explicitgfs2_quota_cleanup()call ingfs2_reconfigure()aftergfs2_make_fs_ro()to preserve the remount-ro cleanup behaviour.Reported-by [email protected]Signed-off-by Deepanshu Kartikey <[email protected]>--- fs/gfs2/ops_fstype.c | 1 + fs/gfs2/super.c | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-)fs/gfs2/ops_fstype.c +1
-0@@ -1592,6 +1592,7 @@ static int gfs2_reconfigure(struct fs_context *fc)if ((sb->s_flags ^ fc->sb_flags) &SB_RDONLY ) {if (fc->sb_flags &SB_RDONLY ) {gfs2_make_fs_ro (sdp); +gfs2_quota_cleanup (sdp); }else { error =gfs2_make_fs_rw (sdp);if (error)fs/gfs2/super.c +2
-4@@ -565,7 +565,6 @@ void gfs2_make_fs_ro(struct gfs2_sbd *sdp) HZ * 5);gfs2_assert_warn (sdp,gfs2_log_is_empty (sdp)); } -gfs2_quota_cleanup (sdp); }/** @@ -604,8 +603,6 @@ static void gfs2_put_super(struct super_block *sb)else {if (gfs2_withdrawn (sdp))gfs2_destroy_threads (sdp); - -gfs2_quota_cleanup (sdp); }/* At this point, we're through modifying the disk */ @@ -637,6 +634,8 @@ static void gfs2_put_super(struct super_block *sb)gfs2_glock_dq_uninit (&sdp->sd_live_gh);gfs2_clear_rgrpd (sdp);gfs2_jindex_free (sdp); + +gfs2_quota_cleanup (sdp);/* Take apart glock structures and buffer lists */ gfs2_gl_hash_clear (sdp);iput (sdp->sd_inode); --
Quoted 2 lines
2.43.0
Hi Agruenba,
Gentle reminder. Please let me know the status of this patch
Thanks
Deepanshu
Andreas Gruenbacher
maintainerQuoted 10 lines · Deepanshu Kartikey
gfs2_quota_cleanup()freessd_quota_bitmapearly ingfs2_put_super()before theiput()calls that can trigger inode eviction. When an unlinked inode is evicted during unmount, it goes through:
gfs2_evict_inode()->gfs2_dinode_dealloc()->gfs2_quota_hold()-> qdsb_get() -> slot_get() -> find_first_zero_bit(sd_quota_bitmap)If
sd_quota_bitmaphas already been freed bygfs2_quota_cleanup(), this results in a null pointer dereference:
Quoted 8 lines
KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] _find_first_zero_bit+0x29/0xb0 lib/find_bit.c:149 slot_get fs/gfs2/quota.c:355 qdsb_get+0x133/0x340 fs/gfs2/quota.c:522 gfs2_quota_hold+0x191/0x5c0 fs/gfs2/quota.c:606 gfs2_dinode_dealloc+0x176/0x3f0 fs/gfs2/inode.c:488 gfs2_evict_inode+0x11cd/0x1400 fs/gfs2/super.c:1479 gfs2_put_super+0x300/0x860 fs/gfs2/super.c:618
This is happening when the metadata part of a gfs2 filesystem is mounted (mount -o meta), the statfs inode is unlinked, and the metadata filesystem is unmounted. This is not a useful operation; it will leave an inconsistent filesystem. The kernel should nevertheless cope with it.
An appropriate fix would be to skip metadata inodes in the quota code. I will post a patch.
Thanks,
Andreas