nilfs2: fix use-after-free bug in nilfs_mdt_destroy()
Ryusuke Konishi
maintainerPATCH 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
@@ -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
Quoted 25 lines · Ryusuke Konishi
In
alloc_inode(),inode_init_always()could return-ENOMEMifsecurity_inode_alloc()fails. If this happens for nilfs2,nilfs_free_inode()is called without initializinginode->i_privateandnilfs_free_inode()wrongly callsnilfs_mdt_destroy(), which frees uninitializedinode->i_privateand can trigger a crash.Fix this bug by initializing
inode->i_privateinnilfs_alloc_inode().Link https://lkml.kernel.org/r/CAFcO6XOcf1Jj2SeGt=jJV59wmhESeSKpfR0omdFRq+J9nD1vfQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org open hereLink https://lkml.kernel.org/r/20211011030956.2459172-1-mudongliangabcd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org open hereReported-by butt3rflyh4ck <[email protected]>Reported-by Hao Sun <[email protected]>Reported-by Jiacheng Xu <[email protected]>Reported-by Mudong Liang <[email protected]>Signed-off-by Ryusuke Konishi <[email protected]>Cc Al Viro <viro-RmSDqhL/[email protected]>--- fs/nilfs2/super.c | 1 + 1 file changed, 1 insertion(+)fs/nilfs2/super.c +0
-0
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...
Ryusuke Konishi
maintainerQuoted 22 lines · Al Viro
Quoted 20 lines · Ryusuke Konishi
In
alloc_inode(),inode_init_always()could return-ENOMEMifsecurity_inode_alloc()fails. If this happens for nilfs2,nilfs_free_inode()is called without initializinginode->i_privateandnilfs_free_inode()wrongly callsnilfs_mdt_destroy(), which frees uninitializedinode->i_privateand can trigger a crash.Fix this bug by initializing
inode->i_privateinnilfs_alloc_inode().Link https://lkml.kernel.org/r/CAFcO6XOcf1Jj2SeGt=jJV59wmhESeSKpfR0omdFRq+J9nD1vfQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org open hereLink https://lkml.kernel.org/r/20211011030956.2459172-1-mudongliangabcd-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org open hereReported-by butt3rflyh4ck <[email protected]>Reported-by Hao Sun <[email protected]>Reported-by Jiacheng Xu <[email protected]>Reported-by Mudong Liang <[email protected]>Signed-off-by Ryusuke Konishi <[email protected]>Cc Al Viro <viro-RmSDqhL/[email protected]>--- 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@@ -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 movesecurity_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
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 ofinode_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...
Ryusuke Konishi
maintainerQuoted 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 ofinode_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().
Thanks,
Ryusuke Konishi
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 ofinode_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 ofsecurity_inode_alloc()instead ofi_privateinitialization (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 forinode_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
Thanks,
Ryusuke Konishi
Ryusuke Konishi
maintainerQuoted 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 ofsecurity_inode_alloc()instead ofi_privateinitialization (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 forinode_init_always().Sure, I will post a v2 patch that moves
security_inode_allocto the location just prior tothis_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
Thanks,
Ryusuke Konishi