[PATCH v2] ksmbd: refuse to open an append-only file for writing without O_APPEND

From: Hungyu Lin

Date: Sat Nov 21 2026 - 13:26:27 EST


smb2_open() opens the file with dentry_open(), which does not go through
may_open(). inode_permission() refuses write access to an immutable
inode, but nothing looks at S_APPEND. A client that has write permission
on an append-only file can open it for writing and overwrite the data
that is already there, which open(2) on the server and nfsd do not
allow.

Refuse the open like may_open() does, when the file is opened for
writing without O_APPEND or the create disposition asks to overwrite
it. smb2_create_open_flags() also selects O_WRONLY or O_RDWR for
FILE_WRITE_EA and FILE_WRITE_ATTRIBUTES, so these opens are refused
too.

An open with MAXIMUM_ALLOWED is granted the write rights that the
permission bits or the stored DACL allow and would be refused as well.
Leave the write rights that the client did not ask for explicitly out
of the access granted to such an open, so that it gets a handle for
reading. The maximal access returned in the MxAc create context is not
changed.

Directories are left alone: an append-only directory still takes new
entries.

Fixes: e2f34481b24d ("cifsd: add server-side procedures for SMB3")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hungyu Lin <dennylin0707@xxxxxxxxx>
---
Changes in v2:
- Refuse every open for writing without O_APPEND, not only the ones
that ask for FILE_WRITE_DATA or FILE_APPEND_DATA. A handle opened for
FILE_WRITE_ATTRIBUTES could still reach kernel_write() through
FSCTL_SET_SPARSE.
- Refuse an overwrite disposition up front. The truncating ones used to
fail in vfs_truncate() after the oplocks were broken, with
STATUS_SHARING_VIOLATION.
- Drop the write rights of a MAXIMUM_ALLOWED open in smb2_open()
instead of ksmbd_vfs_query_maximal_access(), so that a file with a
stored DACL is covered too and the MxAc reply stays as it is.
- Keep the rights the client asked for next to MAXIMUM_ALLOWED. A POSIX
open for MAXIMUM_ALLOWED | FILE_APPEND_DATA can append again, as
reported by Ian Bridges.

v1: https://lore.kernel.org/linux-cifs/20261120071553.28739-1-dennylin0707@xxxxxxxxx/

fs/smb/server/smb2pdu.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)

diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 6bd63c5e7da3..bcd25c197ffa 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -5028,6 +5028,14 @@ int smb2_open(struct ksmbd_work *work)
maximal_access = daccess;
}

+ if (req->DesiredAccess & FILE_MAXIMAL_ACCESS_LE && file_present &&
+ IS_APPEND(d_inode(path.dentry)) &&
+ !S_ISDIR(d_inode(path.dentry)->i_mode)) {
+ __le32 asked = smb_map_generic_desired_access(req->DesiredAccess);
+
+ daccess &= ~(FILE_WRITE_DESIRE_ACCESS_LE & ~asked);
+ }
+
/* MAXIMUM_ALLOWED may not have granted the requested DELETE */
if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE &&
!(daccess & FILE_DELETE_LE)) {
@@ -5122,6 +5130,19 @@ int smb2_open(struct ksmbd_work *work)
goto err_out;
}

+ if (IS_APPEND(d_inode(path.dentry)) &&
+ !S_ISDIR(d_inode(path.dentry)->i_mode)) {
+ if ((open_flags & O_ACCMODE) != O_RDONLY &&
+ !(open_flags & O_APPEND)) {
+ rc = -EACCES;
+ goto err_out;
+ }
+ if (open_flags & O_TRUNC) {
+ rc = -EACCES;
+ goto err_out;
+ }
+ }
+
rc = 0;
filp = dentry_open(&path, open_flags, current_cred());
if (IS_ERR(filp)) {
--
2.43.0