mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andreas Claesson via B4 Relay <devnull+andreas.aclaesson.com@kernel.org>
To: Namjae Jeon <linkinjeon@kernel.org>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>,
	 Tom Talpey <tom@talpey.com>,
	ChenXiaoSong <chenxiaosong@chenxiaosong.com>,
	 linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] ksmbd: refuse to write to a handle that is not open for writing
Date: Tue, 01 Dec 2026 13:14:19 +0000	[thread overview]
Message-ID: <20261201-ksmbd-write-fmode-v1-1-2848c4e5b36b@aclaesson.com> (raw)

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>



                 reply	other threads:[~2026-12-01 13:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201-ksmbd-write-fmode-v1-1-2848c4e5b36b@aclaesson.com \
    --to=devnull+andreas.aclaesson.com@kernel.org \
    --cc=andreas@aclaesson.com \
    --cc=chenxiaosong@chenxiaosong.com \
    --cc=linkinjeon@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=senozhatsky@chromium.org \
    --cc=tom@talpey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®