Bug 266885

Summary: [FUSEFS] fcntl(F_GETLK) overwrites flock->l_pid even if unlocked
Product: Base System Reporter: John Millikin <jmillikin>
Component: kernAssignee: Alan Somers <asomers>
Status: Closed FIXED    
Severity: Affects Some People CC: asomers, emaste, grahamperrin
Priority: --- Flags: asomers: mfc-stable13+
asomers: mfc-stable12+
Version: CURRENT   
Hardware: Any   
OS: Any   
URL: https://reviews.freebsd.org/D36905

Description John Millikin 2022-10-07 10:18:13 UTC
The documentation for fcntl(F_GETLK) says:

  If no lock is found that would prevent this lock from being created,
  the structure is left unchanged by this system call except for
  the lock type which is set to F_UNLCK.

However, the code is currently overwriting the flock structure's `l_pid` field:

	/* sys/fs/fuse/fuse_vnops.c :: fuse_vnop_advlock() */
	if (err == 0 && op == FUSE_GETLK) {
		flo = fdi.answ;
		fl->l_type = flo->lk.type;
		fl->l_pid = flo->lk.pid;            /* <- here */
		if (flo->lk.type != F_UNLCK) {
			fl->l_start = flo->lk.start;

I think that assignment should be moved down a line, into the `!= F_UNLCK` branch.
Comment 1 commit-hook freebsd_committer freebsd_triage 2022-10-07 15:10:14 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=46fcf947c6c8db1e1ceb3cbd75b69d1d1e494929

commit 46fcf947c6c8db1e1ceb3cbd75b69d1d1e494929
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2022-10-07 14:46:22 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2022-10-07 15:09:21 +0000

    fusefs: during F_GETLK, don't change l_pid if no lock is found

    PR:             266885
    MFC after:      2 weeks
    Submitted by:   John Millikin <jmillikin@gmail.com>
    Sponsored by:   Axcient
    Reviewed by:    emaste
    Differential Revision: https://reviews.freebsd.org/D36905

 sys/fs/fuse/fuse_vnops.c     |  2 +-
 tests/sys/fs/fusefs/locks.cc | 16 ++++++++++++++--
 2 files changed, 15 insertions(+), 3 deletions(-)
Comment 2 commit-hook freebsd_committer freebsd_triage 2022-10-19 02:42:09 UTC
A commit in branch stable/12 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=274f5099706f33bf69fa29546dd6c94fde8eb001

commit 274f5099706f33bf69fa29546dd6c94fde8eb001
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2022-10-07 14:46:22 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2022-10-19 01:13:55 +0000

    fusefs: during F_GETLK, don't change l_pid if no lock is found

    PR:             266885
    Submitted by:   John Millikin <jmillikin@gmail.com>
    Sponsored by:   Axcient
    Reviewed by:    emaste
    Differential Revision: https://reviews.freebsd.org/D36905

    (cherry picked from commit 46fcf947c6c8db1e1ceb3cbd75b69d1d1e494929)

 sys/fs/fuse/fuse_vnops.c     |  2 +-
 tests/sys/fs/fusefs/locks.cc | 16 ++++++++++++++--
 2 files changed, 15 insertions(+), 3 deletions(-)
Comment 3 Graham Perrin freebsd_committer freebsd_triage 2022-10-29 19:29:36 UTC
Triage: 

* assign to committer resolving

* asomers@, attention to flags?
Comment 4 commit-hook freebsd_committer freebsd_triage 2022-10-31 01:51:09 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=ab7955aec728c03d9388aff7b03ca748c69a0eb4

commit ab7955aec728c03d9388aff7b03ca748c69a0eb4
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2022-10-07 14:46:22 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2022-10-31 00:51:30 +0000

    fusefs: during F_GETLK, don't change l_pid if no lock is found

    PR:             266885
    Submitted by:   John Millikin <jmillikin@gmail.com>
    Sponsored by:   Axcient
    Reviewed by:    emaste
    Differential Revision: https://reviews.freebsd.org/D36905

    (cherry picked from commit 46fcf947c6c8db1e1ceb3cbd75b69d1d1e494929)

 sys/fs/fuse/fuse_vnops.c     |  2 +-
 tests/sys/fs/fusefs/locks.cc | 16 ++++++++++++++--
 2 files changed, 15 insertions(+), 3 deletions(-)