mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ian Bridges <icb@fastmail.org>
To: linkinjeon@kernel.org
Cc: senozhatsky@chromium.org, tom@talpey.com,
	chenxiaosong@chenxiaosong.com, linux-cifs@vger.kernel.org,
	linux-kernel@vger.kernel.org, Ian Bridges <icb@fastmail.org>
Subject: [PATCH 2/4] ksmbd: fail an overwrite or supersede of a directory
Date: Sun, 29 Nov 2026 23:13:47 -0600	[thread overview]
Message-ID: <f2fc9cd37880a8eaf45a9a19f6a8743dfb4cf0e4.1796015315.git.icb@fastmail.org> (raw)
In-Reply-To: <cover.1796015315.git.icb@fastmail.org>

ksmbd reports success for an open of an existing directory with
FILE_SUPERSEDE, FILE_OVERWRITE or FILE_OVERWRITE_IF, although nothing
is overwritten. ksmbd also accepts an open that combines
FILE_DIRECTORY_FILE with one of these dispositions, which is invalid.
For a name that does not exist, FILE_SUPERSEDE and FILE_OVERWRITE_IF
then create a directory.

Fail the overwrite or supersede of an existing directory with
STATUS_OBJECT_NAME_COLLISION, or with STATUS_ACCESS_DENIED for the root
of the share. Fail the invalid combination with
STATUS_INVALID_PARAMETER, together with the other checks of the request
parameters. These are the statuses that MS-FSA gives.

A named data stream of a directory can still be overwritten. Explicit
$INDEX_ALLOCATION names also keep their previous behavior. A share that
is not writable still refuses the overwrite with STATUS_ACCESS_DENIED.

Signed-off-by: Ian Bridges <icb@fastmail.org>
---
 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 3818d003e47a..e500653084ee 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -4570,7 +4570,7 @@ int smb2_open(struct ksmbd_work *work)
 	char *stream_name = NULL;
 	bool file_present = false, created = false, already_permitted = false;
 	bool app_instance_id_checked = false, app_instance_closed;
-	bool stream_created = false;
+	bool stream_created = false, overwrite = false;
 	int share_ret, need_truncate = 0;
 	u64 time, alloc_size = 0;
 	umode_t posix_mode = 0;
@@ -4806,6 +4806,15 @@ int smb2_open(struct ksmbd_work *work)
 		rc = -EINVAL;
 		goto err_out2;
 	}
+	overwrite = req->CreateDisposition == FILE_SUPERSEDE_LE ||
+		    req->CreateDisposition == FILE_OVERWRITE_LE ||
+		    req->CreateDisposition == FILE_OVERWRITE_IF_LE;
+
+	/* A directory open only creates or opens */
+	if (req->CreateOptions & FILE_DIRECTORY_FILE_LE && overwrite) {
+		rc = -EINVAL;
+		goto err_out2;
+	}
 
 	if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) {
 		pr_err("Invalid desired access : 0x%x\n",
@@ -5013,6 +5022,19 @@ int smb2_open(struct ksmbd_work *work)
 		goto err_out;
 	}
 
+	/* An existing directory is not overwritten */
+	if (!stream_name && file_present && overwrite &&
+	    S_ISDIR(d_inode(path.dentry)->i_mode) &&
+	    test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
+		if (path_equal(&path, &tcon->share_conf->vfs_path)) {
+			rsp->hdr.Status = STATUS_ACCESS_DENIED;
+			rc = -EACCES;
+		} else {
+			rc = -EEXIST;
+		}
+		goto err_out;
+	}
+
 	daccess = smb_map_generic_desired_access(req->DesiredAccess);
 
 	/*
-- 
2.47.3


  parent reply	other threads:[~2026-11-30  5:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  5:13 [PATCH 0/4] ksmbd: fix the checks on an open of an existing file Ian Bridges
2026-11-30  5:13 ` [PATCH 1/4] ksmbd: check the cached read-only attribute for delete-on-close Ian Bridges
2026-11-30  5:13 ` Ian Bridges [this message]
2026-11-30  5:13 ` [PATCH 3/4] ksmbd: fail an overwrite that would clear the hidden or system attribute Ian Bridges
2026-11-30  5:13 ` [PATCH 4/4] ksmbd: refuse write access to a read-only file at open Ian Bridges
2026-11-30 12:42 ` [PATCH 0/4] ksmbd: fix the checks on an open of an existing file Namjae Jeon

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=f2fc9cd37880a8eaf45a9a19f6a8743dfb4cf0e4.1796015315.git.icb@fastmail.org \
    --to=icb@fastmail.org \
    --cc=chenxiaosong@chenxiaosong.com \
    --cc=linkinjeon@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --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®