[PATCH] ksmbd: refuse to write to a handle that is not open for writing
From: Andreas Claesson via B4 Relay
Date: Tue Dec 01 2026 - 08:14:27 EST
From: Andreas Claesson <andreas@xxxxxxxxxxxxx>
smb2_create_open_flags() opens block and character device nodes with
O_PATH, whatever access the client asks for, but the open still grants
the requested access. A client that opens a device node in a share with
FILE_WRITE_DATA and then sends a WRITE passes the access check in
ksmbd_vfs_write(), and kernel_write() is called on a file without
FMODE_WRITE:
WARNING: fs/read_write.c:608 at __kernel_write_iter+0x7ad/0x9a0
Workqueue: ksmbd-io handle_ksmbd_work
Call Trace:
kernel_write+0x1f4/0x680
ksmbd_vfs_write+0x30c/0xcc0
smb2_write+0x1000/0x2050
handle_ksmbd_work+0xff6/0x2a30
__kernel_write_iter() returns -EBADF after the warning, so nothing is
written, but any client with write access to a share that contains a
device node can trigger the warning.
Check the file mode before the write and return -EACCES, as
ksmbd_vfs_read() does since commit 2fd145f8865b ("ksmbd: refuse to read
a handle that is not open for reading").
Fixes: 8e2f6a0e2dc9 ("ksmbd: fix open failure from block and char device file")
Assisted-by: Claude:claude-mythos-5-2
Signed-off-by: Andreas Claesson <andreas@xxxxxxxxxxxxx>
---
Found with syzkaller, using local descriptions for ksmbd.
Tested on ksmbd-for-next (87ff512becd4) in QEMU with KASAN. The
reproducer opens a character device node in a share with
FILE_WRITE_DATA and sends a WRITE; a second one does the same with a
block device node.
Without the patch both give the warning above and the WRITE fails with
STATUS_INVALID_HANDLE. With the patch there is no warning and the WRITE
fails with STATUS_ACCESS_DENIED. A WRITE to a FIFO, which is not opened
with O_PATH, succeeds as before.
I also sent FSCTL_SET_ZERO_DATA, FSCTL_SET_SPARSE, FSCTL_COPYCHUNK_WRITE
and FLUSH on a device node handle. None of them reaches kernel_write()
and none gives a warning, with or without the patch, so WRITE is the
only path I found.
I did not add a stable tag. The warning is the only effect, as nothing
is written. Please add one if you think it is worth it.
---
fs/smb/server/vfs.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/fs/smb/server/vfs.c b/fs/smb/server/vfs.c
index 35d449ee3dae3..7340691609f9e 100644
--- a/fs/smb/server/vfs.c
+++ b/fs/smb/server/vfs.c
@@ -564,6 +564,12 @@ int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp,
goto out;
}
+ /* A device node is opened with O_PATH, whatever access was granted */
+ if (!(filp->f_mode & FMODE_WRITE)) {
+ err = -EACCES;
+ goto out;
+ }
+
if (!fp->is_posix_ctxt) {
err = check_lock_range(filp, *pos, *pos + count - 1, WRITE);
if (err) {
---
base-commit: 87ff512becd45a08c44fd47a9f03e607af9ef6bd
change-id: 20261201-ksmbd-write-fmode-88a17b90d929
Best regards,
--
Andreas Claesson <andreas@xxxxxxxxxxxxx>