diff mbox series

CIFS: unlock file across process

Message ID 20200214043513.uh2jtb62qf54nmud@xzhoux.usersys.redhat.com (mailing list archive)
State New, archived
Headers show
Series CIFS: unlock file across process | expand

Commit Message

Murphy Zhou Feb. 14, 2020, 4:35 a.m. UTC
Now child can't unlock the same file that has been locked by
parent. Fix this by not skipping unlock if requesting from
different process.

Patch tested by LTP and xfstests using samba server.

Signed-off-by: Murphy Zhou <jencce.kernel@gmail.com>
---
 fs/cifs/smb2file.c | 2 --
 1 file changed, 2 deletions(-)

Comments

Steve French Feb. 14, 2020, 5:32 a.m. UTC | #1
merged into cifs-2.6.git for-next

On Thu, Feb 13, 2020 at 10:35 PM Murphy Zhou <jencce.kernel@gmail.com> wrote:
>
> Now child can't unlock the same file that has been locked by
> parent. Fix this by not skipping unlock if requesting from
> different process.
>
> Patch tested by LTP and xfstests using samba server.
>
> Signed-off-by: Murphy Zhou <jencce.kernel@gmail.com>
> ---
>  fs/cifs/smb2file.c | 2 --
>  1 file changed, 2 deletions(-)
>
> diff --git a/fs/cifs/smb2file.c b/fs/cifs/smb2file.c
> index afe1f03aabe3..b5bca0e13d51 100644
> --- a/fs/cifs/smb2file.c
> +++ b/fs/cifs/smb2file.c
> @@ -151,8 +151,6 @@ smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
>                     (flock->fl_start + length) <
>                     (li->offset + li->length))
>                         continue;
> -               if (current->tgid != li->pid)
> -                       continue;
>                 if (cinode->can_cache_brlcks) {
>                         /*
>                          * We can cache brlock requests - simply remove a lock
> --
> 2.20.1
>
Jeff Layton Feb. 14, 2020, 12:26 p.m. UTC | #2
On Fri, 2020-02-14 at 12:35 +0800, Murphy Zhou wrote:
> Now child can't unlock the same file that has been locked by
> parent. Fix this by not skipping unlock if requesting from
> different process.
> 
> Patch tested by LTP and xfstests using samba server.
> 
> Signed-off-by: Murphy Zhou <jencce.kernel@gmail.com>
> ---
>  fs/cifs/smb2file.c | 2 --
>  1 file changed, 2 deletions(-)
> 
> diff --git a/fs/cifs/smb2file.c b/fs/cifs/smb2file.c
> index afe1f03aabe3..b5bca0e13d51 100644
> --- a/fs/cifs/smb2file.c
> +++ b/fs/cifs/smb2file.c
> @@ -151,8 +151,6 @@ smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
>  		    (flock->fl_start + length) <
>  		    (li->offset + li->length))
>  			continue;
> -		if (current->tgid != li->pid)
> -			continue;
>  		if (cinode->can_cache_brlcks) {
>  			/*
>  			 * We can cache brlock requests - simply remove a lock

I'm not as familiar with this code as I once was, but...

From fork(2) manpage:

       *  The  child does not inherit process-associated record locks from its
          parent (fcntl(2)).  (On the other hand,  it  does  inherit  fcntl(2)
          open file description locks and flock(2) locks from its parent.)

It looks like cifs_setlk calls mand_unlock_range, and that gets called
from both fcntl and flock codepaths.

So, I'm not sure about just removing this. It seems like the pid check
is probably correct for traditional posix locks, but probably not for
OFD and flock locks? What ensures that completely unrelated tasks can't
unlock your locks?
Murphy Zhou Feb. 14, 2020, 2:28 p.m. UTC | #3
On Fri, Feb 14, 2020 at 07:26:46AM -0500, Jeff Layton wrote:
> On Fri, 2020-02-14 at 12:35 +0800, Murphy Zhou wrote:
> > Now child can't unlock the same file that has been locked by
> > parent. Fix this by not skipping unlock if requesting from
> > different process.
> > 
> > Patch tested by LTP and xfstests using samba server.
> > 
> > Signed-off-by: Murphy Zhou <jencce.kernel@gmail.com>
> > ---
> >  fs/cifs/smb2file.c | 2 --
> >  1 file changed, 2 deletions(-)
> > 
> > diff --git a/fs/cifs/smb2file.c b/fs/cifs/smb2file.c
> > index afe1f03aabe3..b5bca0e13d51 100644
> > --- a/fs/cifs/smb2file.c
> > +++ b/fs/cifs/smb2file.c
> > @@ -151,8 +151,6 @@ smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
> >  		    (flock->fl_start + length) <
> >  		    (li->offset + li->length))
> >  			continue;
> > -		if (current->tgid != li->pid)
> > -			continue;
> >  		if (cinode->can_cache_brlcks) {
> >  			/*
> >  			 * We can cache brlock requests - simply remove a lock
> 
> I'm not as familiar with this code as I once was, but...
> 
> From fork(2) manpage:
> 
>        *  The  child does not inherit process-associated record locks from its
>           parent (fcntl(2)).  (On the other hand,  it  does  inherit  fcntl(2)
>           open file description locks and flock(2) locks from its parent.)
> 
> It looks like cifs_setlk calls mand_unlock_range, and that gets called
> from both fcntl and flock codepaths.
> 
> So, I'm not sure about just removing this. It seems like the pid check
> is probably correct for traditional posix locks, but probably not for
> OFD and flock locks? What ensures that completely unrelated tasks can't
> unlock your locks?

You are right Jeff. Just removing this is not right. We need to handle
at least 3 types of locks: posix, OFD and flock.

Thanks very much for reviewing! I'll try to sort this out.
> -- 
> Jeff Layton <jlayton@kernel.org>
>
Pavel Shilovsky Feb. 14, 2020, 7:03 p.m. UTC | #4
Also, please make sure that resulting patch works against Windows file
share since the locking semantics may be different there.

Depending on a kind of lease we have on a file, locks may be cached or
not. We probably don't want to have different behavior for cached and
non-cached locks. Especially given the fact that a lease may be broken
in the middle of app execution and the different behavior will be
applied immediately.

--
Best regards,
Pavel Shilovsky

пт, 14 февр. 2020 г. в 06:30, Murphy Zhou <jencce.kernel@gmail.com>:
>
> On Fri, Feb 14, 2020 at 07:26:46AM -0500, Jeff Layton wrote:
> > On Fri, 2020-02-14 at 12:35 +0800, Murphy Zhou wrote:
> > > Now child can't unlock the same file that has been locked by
> > > parent. Fix this by not skipping unlock if requesting from
> > > different process.
> > >
> > > Patch tested by LTP and xfstests using samba server.
> > >
> > > Signed-off-by: Murphy Zhou <jencce.kernel@gmail.com>
> > > ---
> > >  fs/cifs/smb2file.c | 2 --
> > >  1 file changed, 2 deletions(-)
> > >
> > > diff --git a/fs/cifs/smb2file.c b/fs/cifs/smb2file.c
> > > index afe1f03aabe3..b5bca0e13d51 100644
> > > --- a/fs/cifs/smb2file.c
> > > +++ b/fs/cifs/smb2file.c
> > > @@ -151,8 +151,6 @@ smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
> > >                 (flock->fl_start + length) <
> > >                 (li->offset + li->length))
> > >                     continue;
> > > -           if (current->tgid != li->pid)
> > > -                   continue;
> > >             if (cinode->can_cache_brlcks) {
> > >                     /*
> > >                      * We can cache brlock requests - simply remove a lock
> >
> > I'm not as familiar with this code as I once was, but...
> >
> > From fork(2) manpage:
> >
> >        *  The  child does not inherit process-associated record locks from its
> >           parent (fcntl(2)).  (On the other hand,  it  does  inherit  fcntl(2)
> >           open file description locks and flock(2) locks from its parent.)
> >
> > It looks like cifs_setlk calls mand_unlock_range, and that gets called
> > from both fcntl and flock codepaths.
> >
> > So, I'm not sure about just removing this. It seems like the pid check
> > is probably correct for traditional posix locks, but probably not for
> > OFD and flock locks? What ensures that completely unrelated tasks can't
> > unlock your locks?
>
> You are right Jeff. Just removing this is not right. We need to handle
> at least 3 types of locks: posix, OFD and flock.
>
> Thanks very much for reviewing! I'll try to sort this out.
> > --
> > Jeff Layton <jlayton@kernel.org>
> >
Murphy Zhou Feb. 19, 2020, 2:10 a.m. UTC | #5
On Fri, Feb 14, 2020 at 11:03:00AM -0800, Pavel Shilovsky wrote:
> Also, please make sure that resulting patch works against Windows file
> share since the locking semantics may be different there.

OK.

> 
> Depending on a kind of lease we have on a file, locks may be cached or
> not. We probably don't want to have different behavior for cached and
> non-cached locks. Especially given the fact that a lease may be broken
> in the middle of app execution and the different behavior will be
> applied immediately.

Testing new patch with and without cache=none option, both samba
and Win2019 server.

Thanks very much for reviewing!

Murphy
> 
> --
> Best regards,
> Pavel Shilovsky
> 
> пт, 14 февр. 2020 г. в 06:30, Murphy Zhou <jencce.kernel@gmail.com>:
> >
> > On Fri, Feb 14, 2020 at 07:26:46AM -0500, Jeff Layton wrote:
> > > On Fri, 2020-02-14 at 12:35 +0800, Murphy Zhou wrote:
> > > > Now child can't unlock the same file that has been locked by
> > > > parent. Fix this by not skipping unlock if requesting from
> > > > different process.
> > > >
> > > > Patch tested by LTP and xfstests using samba server.
> > > >
> > > > Signed-off-by: Murphy Zhou <jencce.kernel@gmail.com>
> > > > ---
> > > >  fs/cifs/smb2file.c | 2 --
> > > >  1 file changed, 2 deletions(-)
> > > >
> > > > diff --git a/fs/cifs/smb2file.c b/fs/cifs/smb2file.c
> > > > index afe1f03aabe3..b5bca0e13d51 100644
> > > > --- a/fs/cifs/smb2file.c
> > > > +++ b/fs/cifs/smb2file.c
> > > > @@ -151,8 +151,6 @@ smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
> > > >                 (flock->fl_start + length) <
> > > >                 (li->offset + li->length))
> > > >                     continue;
> > > > -           if (current->tgid != li->pid)
> > > > -                   continue;
> > > >             if (cinode->can_cache_brlcks) {
> > > >                     /*
> > > >                      * We can cache brlock requests - simply remove a lock
> > >
> > > I'm not as familiar with this code as I once was, but...
> > >
> > > From fork(2) manpage:
> > >
> > >        *  The  child does not inherit process-associated record locks from its
> > >           parent (fcntl(2)).  (On the other hand,  it  does  inherit  fcntl(2)
> > >           open file description locks and flock(2) locks from its parent.)
> > >
> > > It looks like cifs_setlk calls mand_unlock_range, and that gets called
> > > from both fcntl and flock codepaths.
> > >
> > > So, I'm not sure about just removing this. It seems like the pid check
> > > is probably correct for traditional posix locks, but probably not for
> > > OFD and flock locks? What ensures that completely unrelated tasks can't
> > > unlock your locks?
> >
> > You are right Jeff. Just removing this is not right. We need to handle
> > at least 3 types of locks: posix, OFD and flock.
> >
> > Thanks very much for reviewing! I'll try to sort this out.
> > > --
> > > Jeff Layton <jlayton@kernel.org>
> > >
Pavel Shilovsky Feb. 24, 2020, 7:39 p.m. UTC | #6
вт, 18 февр. 2020 г. в 18:10, Murphy Zhou <jencce.kernel@gmail.com>:
>
> On Fri, Feb 14, 2020 at 11:03:00AM -0800, Pavel Shilovsky wrote:
> > Also, please make sure that resulting patch works against Windows file
> > share since the locking semantics may be different there.
>
> OK.
>
> >
> > Depending on a kind of lease we have on a file, locks may be cached or
> > not. We probably don't want to have different behavior for cached and
> > non-cached locks. Especially given the fact that a lease may be broken
> > in the middle of app execution and the different behavior will be
> > applied immediately.
>
> Testing new patch with and without cache=none option, both samba
> and Win2019 server.
>
> Thanks very much for reviewing!
>

cache=none only affects IO and doesn't change the client behavior
regarding locks. "nolease" mount option can be used to turn off leases
and make all locks go to the server.

--
Best regards,
Pavel Shilovsky
Murphy Zhou Feb. 25, 2020, 5:15 a.m. UTC | #7
On Mon, Feb 24, 2020 at 11:39:27AM -0800, Pavel Shilovsky wrote:
> вт, 18 февр. 2020 г. в 18:10, Murphy Zhou <jencce.kernel@gmail.com>:
> >
> > On Fri, Feb 14, 2020 at 11:03:00AM -0800, Pavel Shilovsky wrote:
> > > Also, please make sure that resulting patch works against Windows file
> > > share since the locking semantics may be different there.
> >
> > OK.
> >
> > >
> > > Depending on a kind of lease we have on a file, locks may be cached or
> > > not. We probably don't want to have different behavior for cached and
> > > non-cached locks. Especially given the fact that a lease may be broken
> > > in the middle of app execution and the different behavior will be
> > > applied immediately.
> >
> > Testing new patch with and without cache=none option, both samba
> > and Win2019 server.
> >
> > Thanks very much for reviewing!
> >
> 
> cache=none only affects IO and doesn't change the client behavior
> regarding locks. "nolease" mount option can be used to turn off leases
> and make all locks go to the server.

Great to know! I can't find it in any man page. Doing more tests.

Thanks!

> 
> --
> Best regards,
> Pavel Shilovsky
Pavel Shilovsky Feb. 25, 2020, 7:21 p.m. UTC | #8
пн, 24 февр. 2020 г. в 21:16, Murphy Zhou <jencce.kernel@gmail.com>:
>
> On Mon, Feb 24, 2020 at 11:39:27AM -0800, Pavel Shilovsky wrote:
> > вт, 18 февр. 2020 г. в 18:10, Murphy Zhou <jencce.kernel@gmail.com>:
> > >
> > > On Fri, Feb 14, 2020 at 11:03:00AM -0800, Pavel Shilovsky wrote:
> > > > Also, please make sure that resulting patch works against Windows file
> > > > share since the locking semantics may be different there.
> > >
> > > OK.
> > >
> > > >
> > > > Depending on a kind of lease we have on a file, locks may be cached or
> > > > not. We probably don't want to have different behavior for cached and
> > > > non-cached locks. Especially given the fact that a lease may be broken
> > > > in the middle of app execution and the different behavior will be
> > > > applied immediately.
> > >
> > > Testing new patch with and without cache=none option, both samba
> > > and Win2019 server.
> > >
> > > Thanks very much for reviewing!
> > >
> >
> > cache=none only affects IO and doesn't change the client behavior
> > regarding locks. "nolease" mount option can be used to turn off leases
> > and make all locks go to the server.
>
> Great to know! I can't find it in any man page. Doing more tests.
>

Good catch, it is missing in the man pages.

Now added: https://github.com/piastry/cifs-utils/commit/4b8b2e2680e7e4aa9cc8bd4278d04e5fe07d885e

Thanks!

--
Best regards,
Pavel Shilovsky
diff mbox series

Patch

diff --git a/fs/cifs/smb2file.c b/fs/cifs/smb2file.c
index afe1f03aabe3..b5bca0e13d51 100644
--- a/fs/cifs/smb2file.c
+++ b/fs/cifs/smb2file.c
@@ -151,8 +151,6 @@  smb2_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
 		    (flock->fl_start + length) <
 		    (li->offset + li->length))
 			continue;
-		if (current->tgid != li->pid)
-			continue;
 		if (cinode->can_cache_brlcks) {
 			/*
 			 * We can cache brlock requests - simply remove a lock