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

From: Hungyu Lin

Date: Fri Dec 04 2026 - 16:11:24 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, and refuse the open when no access is left. 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 v3:
- Refuse a MAXIMUM_ALLOWED open that is left with no access. With a
stored DACL that grants only write rights the open used to succeed
with an empty access mask.
- Rebase on ksmbd-for-next.

A POSIX open that asks for FILE_APPEND_DATA together with other write
rights is opened with O_APPEND and keeps those rights. FSCTL_SET_SPARSE
on such a handle appends zeroes to the file. That is the same without
this patch, also for a file that is not append-only, and the data that
is already in the file is not changed, so it is not handled here.

v2: https://lore.kernel.org/linux-cifs/20261121182313.43201-1-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 | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)

diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index a7bf61a6fd6a..390deee31539 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -5168,6 +5168,18 @@ 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);
+ if (!daccess) {
+ rc = -EACCES;
+ goto err_out;
+ }
+ }
+
/* MAXIMUM_ALLOWED may not have granted the requested DELETE */
if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE &&
!(daccess & FILE_DELETE_LE)) {
@@ -5262,6 +5274,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