[PATCH] ksmbd: unblock a deferred byte-range lock before freeing it
From: Piyus Kedia
Date: Fri Dec 04 2026 - 23:30:50 EST
When smb2_lock() has to wait for a byte-range lock, its file_lock is on
the posix blocker tree while the work sleeps in
ksmbd_vfs_posix_lock_wait(), and a later request that conflicts with it
is queued under it, on its flc_blocked_requests list. The holder's
unlock wakes the waiter but leaves those requests queued on it. If the
work is then no longer KSMBD_WORK_ACTIVE, smb2_lock() frees the
file_lock without unblocking it:
kernel BUG at fs/locks.c:312!
Oops: invalid opcode: 0000 [#1] SMP KASAN PTI
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
The work's cancel_fn, smb2_remove_blocked_lock(), does unblock it, but
a CANCEL can set KSMBD_WORK_CANCELLED while no cancel_fn is installed.
smb2_cancel() matches a synchronous CANCEL by MessageId on
conn->requests and calls cancel_fn only if one is set, and smb2_lock()
clears it in release_async_work() after every wake-up, before it tests
work->state for the retry; it is not set yet either between a deferring
vfs_lock_file() and setup_async_work(). A CLOSE after such a CANCEL
does not call cancel_fn, since set_close_state_blocked_works() calls it
only for an ACTIVE work. A retry that fails, with -EDEADLK for
instance, frees the woken file_lock the same way.
Unblock it on each exit that frees it, as do_lock_file_wait() does with
locks_delete_block() after its wait loop. That wakes the requests
queued on it, which retry against the locks that remain. For a request
that never waited, which can take the error exit too, the call finds
nothing to do.
We found this with a syzkaller-based fuzzing harness driven by an AI tool.
Fixes: e2f34481b24d ("cifsd: add server-side procedures for SMB3")
Closes: https://lore.kernel.org/linux-cifs/ksmbd-lock-cancel-unblock.20261205@xxxxxxxxxxx/
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---
Tested on ksmbd-for-next (d70570cf8003) with KASAN, the same config and
image before and after. The reproducer plays the user-space daemon and
opens three connections to one file: A locks a byte, B's lock on it is
deferred under A's, C's is queued under B's, A unlocks and B sends a
synchronous CANCEL for its LOCK 0-40 us later, every round on a new
byte. A test-only hook (not part of this patch, off unless a boot
parameter sets it) sleeps after the release_async_work() above, to make
the CANCEL land in the window every time:
before four vCPUs: kernel BUG at fs/locks.c:312 in 2 of 2 runs,
within about two seconds; with the hook: 2 of 2
after four vCPUs: 8 runs and 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; with the hook: no report
Two problems that are not this one ended those four-vCPU runs early
(the report has the details): ksmbd tears a connection down after a few
hundred cancelled requests, and ksmbd_vfs_posix_lock_wait() can miss a
wake-up. With the second worked around as well, two runs completed
20,000 rounds each, 795 of them in the window, with no report.
fs/smb/server/smb2pdu.c | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index a7bf61a6fd6a..2b10ceeada68 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -11455,6 +11455,14 @@ int smb2_lock(struct ksmbd_work *work)
STATUS_CANCELLED);
release_async_work(work);
work->send_no_response = 1;
+ /*
+ * flock may have waited above, and requests that
+ * conflict with it may be queued on it; a cancel
+ * that found no cancel_fn installed did not
+ * unblock it. Unblock it before it is freed, as
+ * do_lock_file_wait() does.
+ */
+ ksmbd_vfs_posix_lock_unblock(flock);
if (!smb_lock->virtual_lock) {
kfree(smb_lock);
locks_free_lock(flock);
@@ -11469,6 +11477,7 @@ int smb2_lock(struct ksmbd_work *work)
rsp->hdr.Status =
STATUS_RANGE_NOT_LOCKED;
+ ksmbd_vfs_posix_lock_unblock(flock);
if (!smb_lock->virtual_lock) {
locks_free_lock(flock);
kfree(smb_lock);
@@ -11482,6 +11491,7 @@ int smb2_lock(struct ksmbd_work *work)
ksmbd_debug(SMB, "successful in taking lock\n");
} else {
err = rc;
+ ksmbd_vfs_posix_lock_unblock(flock);
if (!smb_lock->virtual_lock) {
locks_free_lock(flock);
kfree(smb_lock);
--
2.34.1