mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hungyu Lin <dennylin0707@gmail.com>
To: linkinjeon@kernel.org
Cc: senozhatsky@chromium.org, tom@talpey.com,
	chenxiaosong@chenxiaosong.com, hyc.lee@gmail.com,
	lsahlber@redhat.com, icb@fastmail.org,
	linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org,
	dennylin0707@gmail.com
Subject: [PATCH v2] ksmbd: refuse to open an append-only file for writing without O_APPEND
Date: Sat, 21 Nov 2026 18:23:13 +0000	[thread overview]
Message-ID: <20261121182313.43201-1-dennylin0707@gmail.com> (raw)

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


                 reply	other threads:[~2026-11-21 18:26 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261121182313.43201-1-dennylin0707@gmail.com \
    --to=dennylin0707@gmail.com \
    --cc=chenxiaosong@chenxiaosong.com \
    --cc=hyc.lee@gmail.com \
    --cc=icb@fastmail.org \
    --cc=linkinjeon@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lsahlber@redhat.com \
    --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®