mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3] ksmbd: refuse to open an append-only file for writing without O_APPEND
@ 2026-12-04 21:11 Hungyu Lin
  2026-12-06  6:30 ` Namjae Jeon
  0 siblings, 1 reply; 2+ messages in thread
From: Hungyu Lin @ 2026-12-04 21:11 UTC (permalink / raw)
  To: linkinjeon
  Cc: senozhatsky, tom, chenxiaosong, hyc.lee, lsahlber, icb,
	linux-cifs, linux-kernel, dennylin0707

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@vger.kernel.org
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
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@gmail.com/

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@gmail.com/

 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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] ksmbd: refuse to open an append-only file for writing without O_APPEND
  2026-12-04 21:11 [PATCH v3] ksmbd: refuse to open an append-only file for writing without O_APPEND Hungyu Lin
@ 2026-12-06  6:30 ` Namjae Jeon
  0 siblings, 0 replies; 2+ messages in thread
From: Namjae Jeon @ 2026-12-06  6:30 UTC (permalink / raw)
  To: Hungyu Lin
  Cc: senozhatsky, tom, chenxiaosong, hyc.lee, lsahlber, icb,
	linux-cifs, linux-kernel

On Sat, Dec 5, 2026 at 6:11 AM Hungyu Lin <dennylin0707@gmail.com> wrote:
>
> 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@vger.kernel.org
> Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
Can you check Sashiko's review comments ?
https://sashiko.dev/#/patchset/20261204211104.93304-1-dennylin0707%40gmail.com
Thanks.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-06  6:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-04 21:11 [PATCH v3] ksmbd: refuse to open an append-only file for writing without O_APPEND Hungyu Lin
2026-12-06  6:30 ` Namjae Jeon

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®