[BUG] ksmbd: kernel BUG in locks_release_private() when a woken blocking lock is cancelled
From: Piyus Kedia
Date: Fri Dec 04 2026 - 23:30:43 EST
Hi,
smb2_lock() can free a deferred byte-range lock that other lock
requests are still queued on, which trips the BUG_ON in
locks_release_private():
fs/locks.c:312
BUG_ON(!list_empty(&flc->flc_blocked_requests));
On ksmbd-for-next, with KASAN and nothing else changed:
kernel BUG at fs/locks.c:312!
Oops: invalid opcode: 0000 [#1] SMP KASAN PTI
CPU: 2 UID: 0 PID: 9306 Comm: kworker/2:3 7.4.0-rc4-01180-gd70570cf8003 #3
Workqueue: ksmbd-io handle_ksmbd_work
RIP: 0010:locks_release_private+0x19a/0x1b0 fs/locks.c:312
locks_free_lock+0x15/0x30 fs/locks.c:362
smb2_lock+0x2809/0x2f40 fs/smb/server/smb2pdu.c:11486
handle_ksmbd_work+0x4d7/0x13d0 fs/smb/server/server.c:223
process_one_work+0x6f7/0x11a0 kernel/workqueue.c:3497
worker_thread+0x417/0x7b0 kernel/workqueue.c:3580
kthread+0x23c/0x290 kernel/kthread.c:436
(smb2pdu.c:11486 is the one locks_free_lock() call the compiler kept
for both the STATUS_RANGE_NOT_LOCKED exit, 11473 on ksmbd-for-next, and
the error exit after it; on 7.4.0-rc5, built differently, the same BUG
decoded to the STATUS_RANGE_NOT_LOCKED exit, which is smb2pdu.c:11144
in mainline.)
While a blocking SMB2 LOCK waits in ksmbd_vfs_posix_lock_wait(), its
file_lock is on the posix blocker tree, and a later request that
conflicts with it is queued under it, on its flc_blocked_requests. The
holder's unlock wakes the waiter but leaves those requests queued on
it. After the wait, smb2_lock() retries only while the work is
KSMBD_WORK_ACTIVE; otherwise it takes one of two exits, CANCELLED
(smb2pdu.c:11131 in mainline) or STATUS_RANGE_NOT_LOCKED
(smb2pdu.c:11144), and both free the file_lock without unblocking it.
So does the error exit after them (smb2pdu.c:11157), which a retry that
fails, with -EDEADLK for instance, takes. do_lock_file_wait() in
fs/locks.c calls locks_delete_block() after its wait loop for exactly
this reason.
The work's cancel_fn, smb2_remove_blocked_lock(), does unblock it, but
a CANCEL can change the state while no cancel_fn is installed.
smb2_cancel() matches a synchronous CANCEL by MessageId on
conn->requests, sets KSMBD_WORK_CANCELLED and calls cancel_fn only if
one is set, and smb2_lock() clears it in release_async_work() after
every wake-up:
release_async_work(work); /* cancel_fn = NULL */
if (work->state == KSMBD_WORK_ACTIVE)
goto retry;
A CANCEL processed between those two lines sends the woken lock to the
STATUS_RANGE_NOT_LOCKED exit with the requests queued on it, and that
is the trace above. The same holds for a CANCEL between a deferring
vfs_lock_file() and setup_async_work() (the CANCELLED exit, after the
next unlock), and a CLOSE after such a CANCEL does not call cancel_fn
either, since set_close_state_blocked_works() calls it only for an
ACTIVE work. The fuzzer hit both exits.
How it is reached. Three connections on one share open the same file:
A locks byte r exclusively, B asks for the same byte and is deferred
under A's lock, C asks for it too and, conflicting with B's request, is
queued under B's. A unlocks, which wakes B's request, and B sends a
synchronous SMB2 CANCEL for its own LOCK by MessageId. If the CANCEL
lands in the window above, B's file_lock is freed with C's request on
it. The window is a few instructions wide, so this is a race:
on ksmbd-for-next with KASAN and four vCPUs it hit the BUG in both of
our runs, within about two seconds of starting; on one vCPU, 13,653
rounds did not hit it.
To confirm the mechanism, and to test the fix against it, we also ran
the same program on a kernel with a test-only 500 ms sleep after that
release_async_work() (not part of the patch). There the CANCEL lands
in the window every time: the BUG in both runs. On the patched kernel
with the same delay, B's lock is answered STATUS_RANGE_NOT_LOCKED - the
CANCEL did land in the window - C's queued lock is granted, and nothing
is reported.
Impact - who can trigger it:
- Any SMB client that can open a file on a share and take a
byte-range lock on it: an authenticated user, or anyone where
guest access is enabled (our runs used a guest session). It needs
only ordinary LOCK and CANCEL requests over three connections, so
it can be done remotely. It is a race, but on four vCPUs our
program won it within about two seconds (above).
- A local unprivileged user can do the same over loopback with
whatever SMB access the server gives them. Our reproducer also
plays the ksmbd user-space daemon, which needs CAP_NET_ADMIN with
the default CONFIG_SMB_SERVER_CHECK_CAP_NET_ADMIN=y, but that is a
convenience of the reproducer: against a running ksmbd.mountd the
trigger is SMB traffic alone.
- What happens: BUG_ON, so an invalid-opcode oops in a ksmbd-io
worker; on a system with panic_on_oops set the machine panics.
Otherwise the worker is killed in the middle of the request; we
have not tested what ksmbd does after that. The BUG fires before
the free, so the file_lock is not freed with requests on it.
The code is the same in mainline (c3500c5ef648, 2026-12-04) and
ksmbd-for-next (d70570cf8003, 2026-12-04). These exits have freed
the file_lock without unblocking it since the deferred-lock wait was
added in e2f34481b24d ("cifsd: add server-side procedures for SMB3"),
v5.15; the release_async_work() before the state test came with
7b461610882c ("ksmbd: fix AsyncId zeroed before use in smb2_lock()
cancel response").
A patch follows in reply to this mail. It unblocks the file_lock on
all three exits before freeing it, as do_lock_file_wait() does.
On ksmbd-for-next with KASAN, same config and image; besides the
patch, the only change to fs/smb/server is the test hook above, off
unless its boot parameter is set:
before four vCPUs: kernel BUG in 2 of 2 runs, within about two
seconds; with the delay: kernel BUG in 2 of 2 runs
after four vCPUs: 8 runs with at least 97 rounds in which the
CANCEL landed in the window (B's lock answered
STATUS_RANGE_NOT_LOCKED), C's lock granted after each, no
report; one vCPU: 20,000 rounds, no report; with the delay:
no report
Those four-vCPU runs ended after 280 to 4,600 rounds on two other
problems that we also see without the patch, and that we will follow
up on separately:
- A connection is torn down after a few hundred cancelled requests:
ksmbd grants no credit for a request it cancels, so the client's
MessageId window shrinks by one each time. We see it on unpatched
ksmbd-for-next when B is cancelled only while it still waits, so
that smb2_lock() never reaches the window above.
- Now and then a woken lock request never resumes.
ksmbd_vfs_posix_lock_wait() waits for !flc_blocker, which
__locks_wake_up_blocks() clears only after the wake-up, while
do_lock_file_wait() waits for flc_blocked_member to be empty,
which is cleared before it. We saw it in a round with no cancel
at all: B took the lock and released it, and C's request, woken
by that unlock, never completed.
With the reproducer reconnecting B every 300 rounds and the wait in
ksmbd_vfs_posix_lock_wait() changed to list_empty(&flc_blocked_member)
as well as this patch, two four-vCPU runs completed all 20,000 rounds:
795 rounds in which the CANCEL landed in the window, no report and no
stuck request.
This was found by AI-driven fuzzing built on top of syzkaller; its
analysis was then checked by hand against the code and every claim
above was reproduced. The reproducer is derived from the fuzzer's
program, runs the daemon and the three clients in one program over
loopback, and we are happy to send it on request.
Kapil Vaswani
Piyus Kedia