Skip to contentSkip to conversation

nilfs2: fix use-after-free bug in nilfs_mdt_destroy()

PATCH In review Ryusuke Konishi 3 participants 7 messages Aug 15, 2022 – Aug 16, 2022 NILFS2 filesystem

lore
Share & tools
lore

Ryusuke Konishi

author maintainer
#0 lore raw

PATCH 1/1 nilfs2: fix use-after-free bug in nilfs_mdt_destroy()

In alloc_inode(), inode_init_always() could return -ENOMEM if security_inode_alloc() fails. If this happens for nilfs2, nilfs_free_inode() is called without initializing inode->i_private and nilfs_free_inode() wrongly calls nilfs_mdt_destroy(), which frees uninitialized inode->i_private and can trigger a crash.

Fix this bug by initializing inode->i_private in nilfs_alloc_inode().

---
 fs/nilfs2/super.c | 1 +
 1 file changed, 1 insertion(+)
fs/nilfs2/super.c +1 -0
index ba108f915391..aca5614f1b44 100644
@@ -159,6 +159,7 @@ struct inode *nilfs_alloc_inode(struct super_block *sb)
 	ii->i_cno = 0;
 	ii->i_assoc_inode = NULL;
 	ii->i_bmap = &ii->i_bmap_data;
+	ii->vfs_inode.i_private = NULL;
 	return &ii->vfs_inode;
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 25 lines · Ryusuke Konishi

In alloc_inode(), inode_init_always() could return -ENOMEM if security_inode_alloc() fails. If this happens for nilfs2, nilfs_free_inode() is called without initializing inode->i_private and nilfs_free_inode() wrongly calls nilfs_mdt_destroy(), which frees uninitialized inode->i_private and can trigger a crash.

Fix this bug by initializing inode->i_private in nilfs_alloc_inode().

---
 fs/nilfs2/super.c | 1 +
 1 file changed, 1 insertion(+)
fs/nilfs2/super.c +0 -0
index ba108f915391..aca5614f1b44 100644

fs/nilfs2/super.c struct inode *nilfs_alloc_inode(struct super_block *sb) · line 164

Quoted 6 lines
fs/nilfs2/super.c +1 -0
@@ -159,6 +159,7 @@ struct inode *nilfs_alloc_inode(struct super_block *sb)
 	ii->i_cno = 0;
 	ii->i_assoc_inode = NULL;
 	ii->i_bmap = &ii->i_bmap_data;
+	ii->vfs_inode.i_private = NULL;
 	return &ii->vfs_inode;
 }

FWIW, I think it's better to deal with that in inode_init_always(), but not just moving ->i_private initialization up - we ought to move security_inode_alloc() to the very end. No sense playing whack-a-mole with further possible bugs of that sort...

author maintainer
#2 lore raw
Quoted 22 lines · Al Viro
Quoted 20 lines · Ryusuke Konishi

In alloc_inode(), inode_init_always() could return -ENOMEM if security_inode_alloc() fails. If this happens for nilfs2, nilfs_free_inode() is called without initializing inode->i_private and nilfs_free_inode() wrongly calls nilfs_mdt_destroy(), which frees uninitialized inode->i_private and can trigger a crash.

Fix this bug by initializing inode->i_private in nilfs_alloc_inode().

---
 fs/nilfs2/super.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 16 lines

fs/nilfs2/super.c struct inode *nilfs_alloc_inode(struct super_block *sb) · line 164

Quoted 11 lines
fs/nilfs2/super.c +1 -0
index ba108f915391..aca5614f1b44 100644
@@ -159,6 +159,7 @@ struct inode *nilfs_alloc_inode(struct super_block *sb)
      ii->i_cno = 0;
      ii->i_assoc_inode = NULL;
      ii->i_bmap = &ii->i_bmap_data;
+     ii->vfs_inode.i_private = NULL;
      return &ii->vfs_inode;
 }

FWIW, I think it's better to deal with that in inode_init_always(), but not just moving ->i_private initialization up - we ought to move security_inode_alloc() to the very end. No sense playing whack-a-mole with further possible bugs of that sort...

Yes, I agree it's better if security_inode_alloc() is moved to the end as possible in the sense of avoiding similar issues.
But, would that vfs change be safe to backport to stable trees?

It looks like the error handling for security_inode_alloc() is in the middle of inode_init_always() for a very long time..

If you want to see the impact of the vfs change, I think it's one way to apply this one in advance. Or if you want to fix it in one step, I think it's good too. How do you feel about this ?

Thanks,
Ryusuke Konishi

#3 lore raw
Quoted 3 lines · Ryusuke Konishi

Yes, I agree it's better if security_inode_alloc() is moved to the end as possible in the sense of avoiding similar issues.
But, would that vfs change be safe to backport to stable trees?

Yes.

Quoted 2 lines

It looks like the error handling for security_inode_alloc() is in the middle of inode_init_always() for a very long time..

Look at the initializations done after it. The only thing with effects outside of inode itself is (since 2010) an increment of nr_inodes.

Quoted 3 lines

If you want to see the impact of the vfs change, I think it's one way to apply this one in advance. Or if you want to fix it in one step, I think it's good too. How do you feel about this ?

IMO that should go into inode_init_always(), with Cc:stable. If you (or Dongliang Mu, or anybody else) would post such variant with reasonable commit message, I'll pick it into vfs.git and feed to Linus in the next window. E.g. into #work.inode, with that branch being made never-rebased, so that you could pull it into your development branch as soon as it's there...

author maintainer
#4 lore raw
Quoted 19 lines · Al Viro
Quoted 3 lines · Ryusuke Konishi

Yes, I agree it's better if security_inode_alloc() is moved to the end as possible in the sense of avoiding similar issues.
But, would that vfs change be safe to backport to stable trees?

Yes.

Quoted 2 lines

It looks like the error handling for security_inode_alloc() is in the middle of inode_init_always() for a very long time..

Look at the initializations done after it. The only thing with effects outside of inode itself is (since 2010) an increment of nr_inodes.

Quoted 3 lines

If you want to see the impact of the vfs change, I think it's one way to apply this one in advance. Or if you want to fix it in one step, I think it's good too. How do you feel about this ?

Quoted 6 lines

IMO that should go into inode_init_always(), with Cc:stable. If you (or Dongliang Mu, or anybody else) would post such variant with reasonable commit message, I'll pick it into vfs.git and feed to Linus in the next window. E.g. into #work.inode, with that branch being made never-rebased, so that you could pull it into your development branch as soon as it's there...

I agree with your thoughts on the course of action. Andrew, I withdraw this patch.

Dongliang (or Jiacheng?), would it be possible for you to post a revised patch against inode_init_always() that moves the call of security_inode_alloc() instead of i_private initialization (as Al Viro said in a nearby thread [1]) ? If you have time, I would like to leave it to you since you wrote the original patch for inode_init_always().

[1] https://lkml.kernel.org/r/CAO4S-mficMz1mQW06EuCF+o11+mRDiCpufqVfoHkcRbQbs8kVw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

Thanks,
Ryusuke Konishi

#5 lore raw
Quoted 28 lines · Ryusuke Konishi
Quoted 19 lines · Al Viro
Quoted 3 lines · Ryusuke Konishi

Yes, I agree it's better if security_inode_alloc() is moved to the end as possible in the sense of avoiding similar issues.
But, would that vfs change be safe to backport to stable trees?

Yes.

Quoted 2 lines

It looks like the error handling for security_inode_alloc() is in the middle of inode_init_always() for a very long time..

Look at the initializations done after it. The only thing with effects outside of inode itself is (since 2010) an increment of nr_inodes.

Quoted 3 lines

If you want to see the impact of the vfs change, I think it's one way to apply this one in advance. Or if you want to fix it in one step, I think it's good too. How do you feel about this ?

Quoted 6 lines

IMO that should go into inode_init_always(), with Cc:stable. If you (or Dongliang Mu, or anybody else) would post such variant with reasonable commit message, I'll pick it into vfs.git and feed to Linus in the next window. E.g. into #work.inode, with that branch being made never-rebased, so that you could pull it into your development branch as soon as it's there...

Quoted 8 lines

I agree with your thoughts on the course of action. Andrew, I withdraw this patch.

Dongliang (or Jiacheng?), would it be possible for you to post a revised patch against inode_init_always() that moves the call of security_inode_alloc() instead of i_private initialization (as Al Viro said in a nearby thread [1]) ? If you have time, I would like to leave it to you since you wrote the original patch for inode_init_always().

Sure, I will post a v2 patch that moves security_inode_alloc to the location just prior to

        this_cpu_inc(nr_inodes);

with proper commit message.

Quoted 5 lines

[1] https://lkml.kernel.org/r/CAO4S-mficMz1mQW06EuCF+o11+mRDiCpufqVfoHkcRbQbs8kVw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

Thanks,
Ryusuke Konishi

author maintainer
#6 lore raw
Quoted 11 lines · Dongliang Mu
Quoted 5 lines

Dongliang (or Jiacheng?), would it be possible for you to post a revised patch against inode_init_always() that moves the call of security_inode_alloc() instead of i_private initialization (as Al Viro said in a nearby thread [1]) ? If you have time, I would like to leave it to you since you wrote the original patch for inode_init_always().

Sure, I will post a v2 patch that moves security_inode_alloc to the location just prior to

        this_cpu_inc(nr_inodes);

with proper commit message.

I saw you already sent the v2 patch on linux-fsdevel, etc. Just thank you for your quick follow.

Regards,
Ryusuke Konishi

Quoted 5 lines
Quoted 5 lines

[1] https://lkml.kernel.org/r/CAO4S-mficMz1mQW06EuCF+o11+mRDiCpufqVfoHkcRbQbs8kVw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

Thanks,
Ryusuke Konishi