linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH V2] namei: free new_dentry late
@ 2018-11-25  0:15 Pan Bian
  2018-11-27  9:25 ` Jan Kara
  0 siblings, 1 reply; 5+ messages in thread
From: Pan Bian @ 2018-11-25  0:15 UTC (permalink / raw)
  To: Alexander Viro; +Cc: linux-fsdevel, linux-kernel, Pan Bian

After calling dput(new_dentry), new_dentry is passed to fsnotify_move.
This may result in a use-after-free bug. This patch moves the put
operation late.

Fixes: da1ce0670c14("vfs: add cross-rename")
Signed-off-by: Pan Bian <bianpan2016@163.com>
---
V2: correct the fixes commit information
---
 fs/namei.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/namei.c b/fs/namei.c
index 0cab649..8b104d9 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -4498,7 +4498,6 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 		unlock_two_nondirectories(source, target);
 	else if (target)
 		inode_unlock(target);
-	dput(new_dentry);
 	if (!error) {
 		fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
 			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
@@ -4507,6 +4506,7 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
 				      new_is_dir, NULL, new_dentry);
 		}
 	}
+	dput(new_dentry);
 	release_dentry_name_snapshot(&old_name);
 
 	return error;
-- 
2.7.4



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH V2] namei: free new_dentry late
  2018-11-25  0:15 [PATCH V2] namei: free new_dentry late Pan Bian
@ 2018-11-27  9:25 ` Jan Kara
  2018-11-27  9:57   ` PanBian
  0 siblings, 1 reply; 5+ messages in thread
From: Jan Kara @ 2018-11-27  9:25 UTC (permalink / raw)
  To: Pan Bian; +Cc: Alexander Viro, linux-fsdevel, linux-kernel

On Sun 25-11-18 08:15:23, Pan Bian wrote:
> After calling dput(new_dentry), new_dentry is passed to fsnotify_move.
> This may result in a use-after-free bug. This patch moves the put
> operation late.
> 
> Fixes: da1ce0670c14("vfs: add cross-rename")
> Signed-off-by: Pan Bian <bianpan2016@163.com>

The code is actually fine AFAICT. One new_dentry reference is passed into
vfs_rename() and another one is acquired directly inside vfs_rename(). That
is the one dropped early but there's still the original reference passed in
protecting new_dentry from freeing. Am I missing something?

								Honza

> ---
> V2: correct the fixes commit information
> ---
>  fs/namei.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/fs/namei.c b/fs/namei.c
> index 0cab649..8b104d9 100644
> --- a/fs/namei.c
> +++ b/fs/namei.c
> @@ -4498,7 +4498,6 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
>  		unlock_two_nondirectories(source, target);
>  	else if (target)
>  		inode_unlock(target);
> -	dput(new_dentry);
>  	if (!error) {
>  		fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
>  			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
> @@ -4507,6 +4506,7 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
>  				      new_is_dir, NULL, new_dentry);
>  		}
>  	}
> +	dput(new_dentry);
>  	release_dentry_name_snapshot(&old_name);
>  
>  	return error;
> -- 
> 2.7.4
> 
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH V2] namei: free new_dentry late
  2018-11-27  9:25 ` Jan Kara
@ 2018-11-27  9:57   ` PanBian
  2018-11-27 10:05     ` Jan Kara
  0 siblings, 1 reply; 5+ messages in thread
From: PanBian @ 2018-11-27  9:57 UTC (permalink / raw)
  To: Jan Kara; +Cc: Alexander Viro, linux-fsdevel, linux-kernel

On Tue, Nov 27, 2018 at 10:25:51AM +0100, Jan Kara wrote:
> On Sun 25-11-18 08:15:23, Pan Bian wrote:
> > After calling dput(new_dentry), new_dentry is passed to fsnotify_move.
> > This may result in a use-after-free bug. This patch moves the put
> > operation late.
> > 
> > Fixes: da1ce0670c14("vfs: add cross-rename")
> > Signed-off-by: Pan Bian <bianpan2016@163.com>
> 
> The code is actually fine AFAICT. One new_dentry reference is passed into
> vfs_rename() and another one is acquired directly inside vfs_rename(). That
> is the one dropped early but there's still the original reference passed in
> protecting new_dentry from freeing. Am I missing something?

I am not quite sure about the actual execution logic. But I guess new_dentry 
reference may be dropped outside vfs_rename in cocurrent executions.
Otherwise, there is no need to acquire & drop new_dentry reference as it
is always alive along vfs_rename.

Best regards,
Pan

> 
> 								Honza
> 
> > ---
> > V2: correct the fixes commit information
> > ---
> >  fs/namei.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/fs/namei.c b/fs/namei.c
> > index 0cab649..8b104d9 100644
> > --- a/fs/namei.c
> > +++ b/fs/namei.c
> > @@ -4498,7 +4498,6 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
> >  		unlock_two_nondirectories(source, target);
> >  	else if (target)
> >  		inode_unlock(target);
> > -	dput(new_dentry);
> >  	if (!error) {
> >  		fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
> >  			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
> > @@ -4507,6 +4506,7 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
> >  				      new_is_dir, NULL, new_dentry);
> >  		}
> >  	}
> > +	dput(new_dentry);
> >  	release_dentry_name_snapshot(&old_name);
> >  
> >  	return error;
> > -- 
> > 2.7.4
> > 
> > 
> -- 
> Jan Kara <jack@suse.com>
> SUSE Labs, CR


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH V2] namei: free new_dentry late
  2018-11-27  9:57   ` PanBian
@ 2018-11-27 10:05     ` Jan Kara
  2018-11-27 10:33       ` Miklos Szeredi
  0 siblings, 1 reply; 5+ messages in thread
From: Jan Kara @ 2018-11-27 10:05 UTC (permalink / raw)
  To: PanBian
  Cc: Jan Kara, Alexander Viro, linux-fsdevel, linux-kernel, Miklos Szeredi

On Tue 27-11-18 17:57:12, PanBian wrote:
> On Tue, Nov 27, 2018 at 10:25:51AM +0100, Jan Kara wrote:
> > On Sun 25-11-18 08:15:23, Pan Bian wrote:
> > > After calling dput(new_dentry), new_dentry is passed to fsnotify_move.
> > > This may result in a use-after-free bug. This patch moves the put
> > > operation late.
> > > 
> > > Fixes: da1ce0670c14("vfs: add cross-rename")
> > > Signed-off-by: Pan Bian <bianpan2016@163.com>
> > 
> > The code is actually fine AFAICT. One new_dentry reference is passed into
> > vfs_rename() and another one is acquired directly inside vfs_rename(). That
> > is the one dropped early but there's still the original reference passed in
> > protecting new_dentry from freeing. Am I missing something?
> 
> I am not quite sure about the actual execution logic. But I guess new_dentry 
> reference may be dropped outside vfs_rename in cocurrent executions.
> Otherwise, there is no need to acquire & drop new_dentry reference as it
> is always alive along vfs_rename.

I don't think that's the case. The dget() - dput() pair just looks
superfluous to me in vfs_rename(). Am I missing something Miklos?

								Honza

> > > diff --git a/fs/namei.c b/fs/namei.c
> > > index 0cab649..8b104d9 100644
> > > --- a/fs/namei.c
> > > +++ b/fs/namei.c
> > > @@ -4498,7 +4498,6 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
> > >  		unlock_two_nondirectories(source, target);
> > >  	else if (target)
> > >  		inode_unlock(target);
> > > -	dput(new_dentry);
> > >  	if (!error) {
> > >  		fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
> > >  			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
> > > @@ -4507,6 +4506,7 @@ int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
> > >  				      new_is_dir, NULL, new_dentry);
> > >  		}
> > >  	}
> > > +	dput(new_dentry);
> > >  	release_dentry_name_snapshot(&old_name);
> > >  
> > >  	return error;
> > > -- 
> > > 2.7.4
> > > 
> > > 
> > -- 
> > Jan Kara <jack@suse.com>
> > SUSE Labs, CR
> 
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH V2] namei: free new_dentry late
  2018-11-27 10:05     ` Jan Kara
@ 2018-11-27 10:33       ` Miklos Szeredi
  0 siblings, 0 replies; 5+ messages in thread
From: Miklos Szeredi @ 2018-11-27 10:33 UTC (permalink / raw)
  To: Jan Kara; +Cc: bianpan2016, Al Viro, linux-fsdevel, linux-kernel

On Tue, Nov 27, 2018 at 11:05 AM Jan Kara <jack@suse.cz> wrote:
>
> On Tue 27-11-18 17:57:12, PanBian wrote:

> > I am not quite sure about the actual execution logic. But I guess new_dentry
> > reference may be dropped outside vfs_rename in cocurrent executions.
> > Otherwise, there is no need to acquire & drop new_dentry reference as it
> > is always alive along vfs_rename.
>
> I don't think that's the case. The dget() - dput() pair just looks
> superfluous to me in vfs_rename(). Am I missing something Miklos?

I think those are to protect against d_delete() called from fs to
reset d_inode.  The caller indeed has to hold one ref anyway.

So not superfluous, but only needed due to d_delete() and not to
protect against freeing of dentry.

Thanks,
Miklos

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-11-27 10:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-25  0:15 [PATCH V2] namei: free new_dentry late Pan Bian
2018-11-27  9:25 ` Jan Kara
2018-11-27  9:57   ` PanBian
2018-11-27 10:05     ` Jan Kara
2018-11-27 10:33       ` Miklos Szeredi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).