Bug 291080 - nfsrv_updatelock() can use uninitialized memory -> crash
Summary: nfsrv_updatelock() can use uninitialized memory -> crash
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 16.0-CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: Rick Macklem
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-11-18 14:23 UTC by Robert Morris
Modified: 2025-12-10 03:45 UTC (History)
2 users (show)

See Also:
rmacklem: mfc-stable15+
rmacklem: mfc-stable14+
rmacklem: mfc-stable13?


Attachments
fake client that can cause a crash in nfsrv_updatelock() (54.57 KB, text/plain)
2025-11-18 14:23 UTC, Robert Morris
no flags Details
Add sanity checks for lock/locku stateids (945 bytes, patch)
2025-11-24 01:08 UTC, Rick Macklem
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Robert Morris 2025-11-18 14:23:05 UTC
Created attachment 265491 [details]
fake client that can cause a crash in nfsrv_updatelock()

I've attached a demo fake nfs4 client that sends an OPEN rpc to the
server asking for a write delegation, and then a LOCK rpc with the
stateid from the write delegation (rather than for the open).
As a result nfsrv_updatelock() tries to use

                lop = LIST_FIRST(&stp->ls_lock);
                ...;
                if (lop->lo_lfp == lfp) {

but the ls_lock list hasn't been initialized for nfsstate stp.

# uname -a
FreeBSD xxx 16.0-CURRENT FreeBSD 16.0-CURRENT #31 main-n275523-f77ac3afe3e3: Sun Oct 12 16:13:10 AST 2025     root@xxx:/usr/obj/usr/src/amd64.amd64/sys/
# cat /etc/exports
V4: / 127.0.0.1
/mnt 127.0.0.1
# cc nfsd535a.c
# chmod ogu+rwx /mnt
# ./a.out
...
Fatal trap 9: general protection fault while in kernel mode
rdi: deadc0dedeadc0de rsi: fffffe00d708ad28 rdx: fffffe00d708ac10
panic() at panic+0x43/frame 0xfffffe00d708aa80
trap_fatal() at trap_fatal+0x68/frame 0xfffffe00d708aaa0
calltrap() at calltrap+0x8/frame 0xfffffe00d708aaa0
--- trap 0x9, rip = 0xffffffff80a78ad4, rsp = 0xfffffe00d708ab70, rbp = 0xfffffe00d708abc0 ---
nfsrv_updatelock() at nfsrv_updatelock+0x74/frame 0xfffffe00d708abc0
nfsrv_lockctrl() at nfsrv_lockctrl+0x125f/frame 0xfffffe00d708ace0
nfsrvd_lock() at nfsrvd_lock+0x656/frame 0xfffffe00d708b1c0
nfsrvd_dorpc() at nfsrvd_dorpc+0x1672/frame 0xfffffe00d708b3d0
nfssvc_program() at nfssvc_program+0x808/frame 0xfffffe00d708b5d0
svc_run_internal() at svc_run_internal+0xaa8/frame 0xfffffe00d708b700
svc_run() at svc_run+0x280/frame 0xfffffe00d708b760
nfsrvd_nfsd() at nfsrvd_nfsd+0x3d3/frame 0xfffffe00d708b8c0
nfssvc_nfsd() at nfssvc_nfsd+0x535/frame 0xfffffe00d708bde0
sys_nfssvc() at sys_nfssvc+0xcc/frame 0xfffffe00d708be00
amd64_syscall() at amd64_syscall+0x169/frame 0xfffffe00d708bf30
fast_syscall_common() at fast_syscall_common+0xf8/frame 0xfffffe00d708bf30

#0  nfsrv_updatelock (stp=0xffffffd001071500, new_lopp=0xffffffc082ada998, 
    other_lopp=0xffffffc082ada880, lfp=0xffffffd001b92e80)
    at /usr/rtm/symbsd/src/sys/fs/nfsserver/nfs_nfsdstate.c:3907
#1  0xffffffc00039c4ac in nfsrv_lockctrl (vp=<optimized out>, 
    new_stpp=0xffffffc082ada9a0, new_lopp=0xffffffc082ada998, 
    cfp=0xffffffc082ada9a8, clientid=..., stateidp=0xffffffc082ada988, 
    exp=<optimized out>, nd=<optimized out>, p=0xffffffc082874140)
    at /usr/rtm/symbsd/src/sys/fs/nfsserver/nfs_nfsdstate.c:2480
#2  0xffffffc0003af518 in nfsrvd_lock (nd=0xffffffc082adb0c0, 
    isdgram=<optimized out>, vp=0xffffffd012346370, exp=0xffffffc082adaf78)
    at /usr/rtm/symbsd/src/sys/fs/nfsserver/nfs_nfsdserv.c:2630
(gdb) print lop
$2 = (struct nfslock *) 0xdeadc0dedeadc0de
(gdb) print/x stp->ls_flags 
$3 = 0x100003
Comment 1 Rick Macklem freebsd_committer freebsd_triage 2025-11-24 01:08:28 UTC
Created attachment 265611 [details]
Add sanity checks for lock/locku stateids

I think this patch fixes the problem.
It adds sanity checks for the type of
stateid for both Lock and LockU.
Comment 2 Rick Macklem freebsd_committer freebsd_triage 2025-11-24 01:09:43 UTC
I did try your test program, but please test the
attached patch if you can.

Thanks for reporting it, rick
Comment 3 Robert Morris 2025-11-24 10:56:58 UTC
(In reply to Rick Macklem from comment #2)
Yes -- your patch does make the problem go away for me too.
Comment 4 commit-hook freebsd_committer freebsd_triage 2025-11-26 19:22:54 UTC
A commit in branch main references this bug:

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

commit aa1cf240887ddcca66dfb969fdc5a8d545396037
Author:     Rick Macklem <rmacklem@FreeBSD.org>
AuthorDate: 2025-11-26 19:20:27 +0000
Commit:     Rick Macklem <rmacklem@FreeBSD.org>
CommitDate: 2025-11-26 19:20:27 +0000

    nfs_nfsdstate.c: Add sanity checks for lock stateids

    Bugzilla PR reported a crash caused by a synthetic client
    doing a Lock operation request with a delegation stateid.

    This patch fixes the problem by adding sanity checks
    for the type of stateid provided as an argument to the
    Lock and LockU operations.

    It has been tested with the FreeBSD, Linux and Solaris 11.4
    clients.  Hopefully, other NFSv4 clients will work ok
    as well.

    PR:     291080
    Tested by:      Robert Morris <rtm@lcs.mit.edu>
    MFC after:      2 weeks

 sys/fs/nfsserver/nfs_nfsdstate.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
Comment 5 Rick Macklem freebsd_committer freebsd_triage 2025-11-26 19:24:06 UTC
Patch has been committed to main and
will be MFC'd.
Comment 6 commit-hook freebsd_committer freebsd_triage 2025-12-10 03:37:50 UTC
A commit in branch stable/15 references this bug:

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

commit ffd47a4bc6716452da795aeaed3429d2392abb73
Author:     Rick Macklem <rmacklem@FreeBSD.org>
AuthorDate: 2025-11-26 19:20:27 +0000
Commit:     Rick Macklem <rmacklem@FreeBSD.org>
CommitDate: 2025-12-10 03:36:04 +0000

    nfs_nfsdstate.c: Add sanity checks for lock stateids

    Bugzilla PR reported a crash caused by a synthetic client
    doing a Lock operation request with a delegation stateid.

    This patch fixes the problem by adding sanity checks
    for the type of stateid provided as an argument to the
    Lock and LockU operations.

    It has been tested with the FreeBSD, Linux and Solaris 11.4
    clients.  Hopefully, other NFSv4 clients will work ok
    as well.

    PR:     291080

    (cherry picked from commit aa1cf240887ddcca66dfb969fdc5a8d545396037)

 sys/fs/nfsserver/nfs_nfsdstate.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
Comment 7 commit-hook freebsd_committer freebsd_triage 2025-12-10 03:42:52 UTC
A commit in branch stable/14 references this bug:

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

commit c8180893d7fef8557d8f8cdd5945468be5023da2
Author:     Rick Macklem <rmacklem@FreeBSD.org>
AuthorDate: 2025-11-26 19:20:27 +0000
Commit:     Rick Macklem <rmacklem@FreeBSD.org>
CommitDate: 2025-12-10 03:40:53 +0000

    nfs_nfsdstate.c: Add sanity checks for lock stateids

    Bugzilla PR reported a crash caused by a synthetic client
    doing a Lock operation request with a delegation stateid.

    This patch fixes the problem by adding sanity checks
    for the type of stateid provided as an argument to the
    Lock and LockU operations.

    It has been tested with the FreeBSD, Linux and Solaris 11.4
    clients.  Hopefully, other NFSv4 clients will work ok
    as well.

    PR:     291080

    (cherry picked from commit aa1cf240887ddcca66dfb969fdc5a8d545396037)

 sys/fs/nfsserver/nfs_nfsdstate.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
Comment 8 Rick Macklem freebsd_committer freebsd_triage 2025-12-10 03:45:26 UTC
Patch has been MFC'd.