2023-10-29 21:11:29

by Juntong Deng

[permalink] [raw]
Subject: [PATCH] fs/gfs2: Fix slab-use-after-free in gfs2_qd_dealloc

In gfs2_put_super(), whether withdrawn or not, the quota should
be cleaned up by gfs2_quota_cleanup() and wait for all quota to
be deallocated.

Otherwise struct gfs2_sbd has been freed before gfs2_qd_dealloc
(rcu callback) is completed, resulting in use-after-free.

Also, gfs2_destroy_threads() and gfs2_quota_cleanup() have
already been called in gfs2_make_fs_ro(), so there is no need to
call them again subsequently, whether withdrawn or not.

Reported-by: [email protected]
Closes: https://syzkaller.appspot.com/bug?extid=29c47e9e51895928698c
Signed-off-by: Juntong Deng <[email protected]>
---
fs/gfs2/super.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/fs/gfs2/super.c b/fs/gfs2/super.c
index a4b3655c5361..d21c04a22d73 100644
--- a/fs/gfs2/super.c
+++ b/fs/gfs2/super.c
@@ -602,13 +602,15 @@ static void gfs2_put_super(struct super_block *sb)
}
spin_unlock(&sdp->sd_jindex_spin);

- if (!sb_rdonly(sb)) {
+ if (!sb_rdonly(sb))
gfs2_make_fs_ro(sdp);
- }
- if (gfs2_withdrawn(sdp)) {
- gfs2_destroy_threads(sdp);
+ else {
+ if (gfs2_withdrawn(sdp))
+ gfs2_destroy_threads(sdp);
+
gfs2_quota_cleanup(sdp);
}
+
WARN_ON(gfs2_withdrawing(sdp));

/* At this point, we're through modifying the disk */
--
2.39.2


2023-10-30 22:44:35

by Andreas Gruenbacher

[permalink] [raw]
Subject: Re: [PATCH] fs/gfs2: Fix slab-use-after-free in gfs2_qd_dealloc

Hello Juntong,

On Sun, Oct 29, 2023 at 10:11 PM Juntong Deng <[email protected]> wrote:
> In gfs2_put_super(), whether withdrawn or not, the quota should
> be cleaned up by gfs2_quota_cleanup() and wait for all quota to
> be deallocated.
>
> Otherwise struct gfs2_sbd has been freed before gfs2_qd_dealloc
> (rcu callback) is completed, resulting in use-after-free.
>
> Also, gfs2_destroy_threads() and gfs2_quota_cleanup() have
> already been called in gfs2_make_fs_ro(), so there is no need to
> call them again subsequently, whether withdrawn or not.
>
> Reported-by: [email protected]
> Closes: https://syzkaller.appspot.com/bug?extid=29c47e9e51895928698c
> Signed-off-by: Juntong Deng <[email protected]>
> ---
> fs/gfs2/super.c | 10 ++++++----
> 1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/fs/gfs2/super.c b/fs/gfs2/super.c
> index a4b3655c5361..d21c04a22d73 100644
> --- a/fs/gfs2/super.c
> +++ b/fs/gfs2/super.c
> @@ -602,13 +602,15 @@ static void gfs2_put_super(struct super_block *sb)
> }
> spin_unlock(&sdp->sd_jindex_spin);
>
> - if (!sb_rdonly(sb)) {
> + if (!sb_rdonly(sb))
> gfs2_make_fs_ro(sdp);
> - }
> - if (gfs2_withdrawn(sdp)) {
> - gfs2_destroy_threads(sdp);
> + else {
> + if (gfs2_withdrawn(sdp))
> + gfs2_destroy_threads(sdp);
> +
> gfs2_quota_cleanup(sdp);
> }
> +
> WARN_ON(gfs2_withdrawing(sdp));
>
> /* At this point, we're through modifying the disk */
> --
> 2.39.2

thanks for this fix, I've merged it.

Andreas