Re: [PATCH] binderfs: rework superblock destruction

From: Christian Brauner
Date: Wed Aug 17 2022 - 11:24:27 EST


On Wed, Aug 17, 2022 at 04:21:11PM +0100, Al Viro wrote:
> On Wed, Aug 17, 2022 at 04:51:44PM +0200, Christian Brauner wrote:
>
> > diff --git a/arch/s390/hypfs/inode.c b/arch/s390/hypfs/inode.c
> > index 5c97f48cea91..d7d275ef132f 100644
> > --- a/arch/s390/hypfs/inode.c
> > +++ b/arch/s390/hypfs/inode.c
> > @@ -329,9 +329,8 @@ static void hypfs_kill_super(struct super_block *sb)
> > hypfs_delete_tree(sb->s_root);
> > if (sb_info && sb_info->update_file)
> > hypfs_remove(sb_info->update_file);
> > - kfree(sb->s_fs_info);
> > - sb->s_fs_info = NULL;
> > kill_litter_super(sb);
> > + kfree(sb->s_fs_info);
>
> UAF, that - *sb gets freed by the time you try to fetch sb->s_fs_info...
> Fetch the pointer first, then destroy the object you've fetched it
> from, then free what it points to...

Please note the "completely untested" in the draft... ;)

If you want me to, I can turn this into something serious to review.

>
> > diff --git a/fs/devpts/inode.c b/fs/devpts/inode.c
> > index 4f25015aa534..78a9095e1748 100644
> > --- a/fs/devpts/inode.c
> > +++ b/fs/devpts/inode.c
> > @@ -509,10 +509,10 @@ static void devpts_kill_sb(struct super_block *sb)
> > {
> > struct pts_fs_info *fsi = DEVPTS_SB(sb);
> >
> > + kill_litter_super(sb);
> > if (fsi)
> > ida_destroy(&fsi->allocated_ptys);
> > kfree(fsi);
> > - kill_litter_super(sb);
> > }
> >
>
> That one's fine.
>
> > static struct file_system_type devpts_fs_type = {
> > diff --git a/fs/ramfs/inode.c b/fs/ramfs/inode.c
> > index bc66d0173e33..bff49294e037 100644
> > --- a/fs/ramfs/inode.c
> > +++ b/fs/ramfs/inode.c
> > @@ -280,8 +280,10 @@ int ramfs_init_fs_context(struct fs_context *fc)
> >
> > static void ramfs_kill_sb(struct super_block *sb)
> > {
> > - kfree(sb->s_fs_info);
> > + struct ramfs_fs_info *fsi = sb->s_fs_info;
> > +
> > kill_litter_super(sb);
> > + kfree(fsi);
> > }
>
> Cosmetical, really - see another posting in the same thread.
>
> > static struct file_system_type ramfs_fs_type =
> > diff --git a/security/selinux/selinuxfs.c b/security/selinux/selinuxfs.c
> > index 8fcdd494af27..fb1dae422d93 100644
> > --- a/security/selinux/selinuxfs.c
> > +++ b/security/selinux/selinuxfs.c
> > @@ -96,9 +96,8 @@ static int selinux_fs_info_create(struct super_block *sb)
> > return 0;
> > }
> >
> > -static void selinux_fs_info_free(struct super_block *sb)
> > +static void selinux_fs_info_free(struct selinux_fs_info *fsi)
> > {
> > - struct selinux_fs_info *fsi = sb->s_fs_info;
> > int i;
> >
> > if (fsi) {
> > @@ -107,8 +106,7 @@ static void selinux_fs_info_free(struct super_block *sb)
> > kfree(fsi->bool_pending_names);
> > kfree(fsi->bool_pending_values);
> > }
> > - kfree(sb->s_fs_info);
> > - sb->s_fs_info = NULL;
> > + kfree(fsi);
> > }
> >
> > #define SEL_INITCON_INO_OFFSET 0x01000000
> > @@ -2180,7 +2178,7 @@ static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
> > pr_err("SELinux: %s: failed while creating inodes\n",
> > __func__);
> >
> > - selinux_fs_info_free(sb);
> > + selinux_fs_info_free(fsi);
> >
> > return ret;
> > }
> > @@ -2202,8 +2200,10 @@ static int sel_init_fs_context(struct fs_context *fc)
> >
> > static void sel_kill_sb(struct super_block *sb)
> > {
> > - selinux_fs_info_free(sb);
> > + struct selinux_fs_info *fsi = sb->s_fs_info;
> > +
> > kill_litter_super(sb);
> > + selinux_fs_info_free(fsi);
> > }
>
> A real bug, but an incomplete fix - you've just gotten yourself a double-free;
> failure in sel_fill_super() has no need to do selinux_fs_info_free() now.

Please note the "completely untested" in the draft... ;)