mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ian Bridges <icb@fastmail.org>
To: linkinjeon@kernel.org
Cc: senozhatsky@chromium.org, tom@talpey.com,
	chenxiaosong@chenxiaosong.com, linux-cifs@vger.kernel.org,
	linux-kernel@vger.kernel.org, Ian Bridges <icb@fastmail.org>
Subject: [PATCH 4/4] ksmbd: refuse write access to a read-only file at open
Date: Sun, 29 Nov 2026 23:13:49 -0600	[thread overview]
Message-ID: <890112dda26783a96c9d762377a34f5cb3d12cf9.1796015315.git.icb@fastmail.org> (raw)
In-Reply-To: <cover.1796015315.git.icb@fastmail.org>

ksmbd lets a client open a read-only file with FILE_WRITE_DATA or
FILE_APPEND_DATA, and writes through the handle succeed. An overwrite
of a read-only file also succeeds and empties the file, even when the
client asked only to read.

Refuse these opens with STATUS_ACCESS_DENIED. An overwrite counts as a
request for FILE_WRITE_DATA. A supersede needs DELETE access instead,
so a supersede that asks only to read is not refused. Named streams,
directories and POSIX opens are not checked.

The check comes before the delete-on-close check. So a delete-on-close
open of a read-only file that also asks for write access or overwrites
the file now gets STATUS_ACCESS_DENIED instead of STATUS_CANNOT_DELETE.

MAXIMUM_ALLOWED is not changed, so a plain MAXIMUM_ALLOWED open of a
read-only file can still get write access.

Signed-off-by: Ian Bridges <icb@fastmail.org>
---
 fs/smb/server/smb2pdu.c | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 2312765cad8f..89aad1acb370 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -5046,7 +5046,10 @@ int smb2_open(struct ksmbd_work *work)
 	 */
 	if (file_present &&
 	    (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE ||
-	     (!stream_name && !posix_ctxt && overwrite)))
+	     (!stream_name && !posix_ctxt &&
+	      (overwrite ||
+	       (!S_ISDIR(d_inode(path.dentry)->i_mode) &&
+		daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))))))
 		file_attrs = smb2_file_attributes(tcon, &path,
 						  !stream_name && !posix_ctxt);
 
@@ -5062,6 +5065,25 @@ int smb2_open(struct ksmbd_work *work)
 		goto err_out;
 	}
 
+	/*
+	 * A read-only file refuses write access to its data. FILE_OVERWRITE
+	 * and FILE_OVERWRITE_IF write the data, so an overwrite is refused
+	 * even when the client asked only to read. FILE_SUPERSEDE needs
+	 * DELETE access instead, so a supersede is refused only when the
+	 * client asked for write access. Named streams, directories and POSIX
+	 * opens are not checked.
+	 */
+	if (!stream_name && !posix_ctxt && file_present &&
+	    !S_ISDIR(d_inode(path.dentry)->i_mode) &&
+	    (req->CreateDisposition == FILE_OVERWRITE_LE ||
+	     req->CreateDisposition == FILE_OVERWRITE_IF_LE ||
+	     daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE)) &&
+	    file_attrs & FILE_ATTRIBUTE_READONLY_LE) {
+		rsp->hdr.Status = STATUS_ACCESS_DENIED;
+		rc = -EACCES;
+		goto err_out;
+	}
+
 	/* A read-only file is not deleted on close */
 	if (file_present && req->CreateOptions & FILE_DELETE_ON_CLOSE_LE &&
 	    file_attrs & FILE_ATTRIBUTE_READONLY_LE) {
-- 
2.47.3


  parent reply	other threads:[~2026-11-30  5:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  5:13 [PATCH 0/4] ksmbd: fix the checks on an open of an existing file Ian Bridges
2026-11-30  5:13 ` [PATCH 1/4] ksmbd: check the cached read-only attribute for delete-on-close Ian Bridges
2026-11-30  5:13 ` [PATCH 2/4] ksmbd: fail an overwrite or supersede of a directory Ian Bridges
2026-11-30  5:13 ` [PATCH 3/4] ksmbd: fail an overwrite that would clear the hidden or system attribute Ian Bridges
2026-11-30  5:13 ` Ian Bridges [this message]
2026-11-30 12:42 ` [PATCH 0/4] ksmbd: fix the checks on an open of an existing file Namjae Jeon

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=890112dda26783a96c9d762377a34f5cb3d12cf9.1796015315.git.icb@fastmail.org \
    --to=icb@fastmail.org \
    --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®