[PATCH 2/4] ksmbd: fail an overwrite or supersede of a directory

From: Ian Bridges

Date: Mon Nov 30 2026 - 00:14:17 EST


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@xxxxxxxxxxxx>
---
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