mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ksmbd: refuse to write to a handle that is not open for writing
@ 2026-12-01 13:14 Andreas Claesson via B4 Relay
  0 siblings, 0 replies; only message in thread
From: Andreas Claesson via B4 Relay @ 2026-12-01 13:14 UTC (permalink / raw)
  To: Namjae Jeon
  Cc: Sergey Senozhatsky, Tom Talpey, ChenXiaoSong, linux-cifs, linux-kernel

From: Andreas Claesson <andreas@aclaesson.com>

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@aclaesson.com>
---
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@aclaesson.com>



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-01 13:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01 13:14 [PATCH] ksmbd: refuse to write to a handle that is not open for writing Andreas Claesson via B4 Relay

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®