[PATCH 4/4] ksmbd: refuse write access to a read-only file at open
From: Ian Bridges
Date: Mon Nov 30 2026 - 00:14:33 EST
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@xxxxxxxxxxxx>
---
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