* [PATCH 0/4] ksmbd: fix the checks on an open of an existing file
@ 2026-11-30 5:13 Ian Bridges
2026-11-30 5:13 ` [PATCH 1/4] ksmbd: check the cached read-only attribute for delete-on-close Ian Bridges
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Ian Bridges @ 2026-11-30 5:13 UTC (permalink / raw)
To: linkinjeon
Cc: senozhatsky, tom, chenxiaosong, linux-cifs, linux-kernel,
Ian Bridges
These patches fix four bugs in an open of an existing file.
Patch 1 stops a delete-on-close open from deleting a read-only file on a
share that does not store DOS attributes. Patch 2 fails an overwrite or
supersede of a directory, which ksmbd lets succeed. Patch 3 fails an
overwrite of a hidden or system file that leaves the attribute out,
which ksmbd lets empty the file. Patch 4 refuses write access to a
read-only file.
Patch 1 adds the attribute read used by patches 3 and 4. Patch 2 adds
the disposition check used by patch 3. The patches apply in the order
shown.
Tests
=====
I tested the series on ksmbd-for-next b572ec503ef0, on x86_64 with
KASAN.
- Each patch builds with W=1 and sparse. The series adds no smatch
warning.
- A script sent one request for each changed rule to the base and to
the tree at each patch. Each request got the status that its patch
describes. The requests that are not meant to change, POSIX opens
among them, gave the same results as on the base.
- smbtorture gives the same results at each patch as on the base.
KASAN reported nothing.
This series was developed with LLM assistance.
Ian Bridges (4):
ksmbd: check the cached read-only attribute for delete-on-close
ksmbd: fail an overwrite or supersede of a directory
ksmbd: fail an overwrite that would clear the hidden or system
attribute
ksmbd: refuse write access to a read-only file at open
fs/smb/server/smb2pdu.c | 131 +++++++++++++++++++++++++++++++-------
fs/smb/server/vfs_cache.c | 23 +++++++
fs/smb/server/vfs_cache.h | 1 +
3 files changed, 133 insertions(+), 22 deletions(-)
base-commit: b572ec503ef0c9dc74507b4a624df7f218da6a64
--
2.47.3
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/4] ksmbd: check the cached read-only attribute for delete-on-close
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 ` Ian Bridges
2026-11-30 5:13 ` [PATCH 2/4] ksmbd: fail an overwrite or supersede of a directory Ian Bridges
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Ian Bridges @ 2026-11-30 5:13 UTC (permalink / raw)
To: linkinjeon
Cc: senozhatsky, tom, chenxiaosong, linux-cifs, linux-kernel,
Ian Bridges
On a share that does not store DOS attributes, SET_INFO can mark an
open file read-only in the inode cache. While a handle keeps that
entry alive, queries report the attribute and SET_INFO refuses to
mark the file for deletion. A second delete-on-close open ignores the
attribute and can delete the file.
Check the attributes in memory when no stored attributes can be read.
Named streams and POSIX opens keep the old check.
The check now comes after the checks of the file type and the name. So
a delete-on-close open with FILE_CREATE of an existing read-only file
now fails with STATUS_OBJECT_NAME_COLLISION.
FileDispositionInformation checks the read-only attribute before it
looks at the request. So clearing delete pending on a read-only file
fails, and a handle without DELETE gets STATUS_CANNOT_DELETE instead of
STATUS_ACCESS_DENIED. Check the read-only attribute only when delete
pending is set, after the check for DELETE.
Signed-off-by: Ian Bridges <icb@fastmail.org>
---
fs/smb/server/smb2pdu.c | 71 +++++++++++++++++++++++++++------------
fs/smb/server/vfs_cache.c | 23 +++++++++++++
fs/smb/server/vfs_cache.h | 1 +
3 files changed, 74 insertions(+), 21 deletions(-)
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index 38326db8da40..3818d003e47a 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -3914,6 +3914,26 @@ static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon,
}
}
+/*
+ * Return the DOS attributes of an existing file. On a share that stores DOS
+ * attributes, read the stored attributes first, because an open copies the
+ * stored attributes into the inode cache only after adding the file to the
+ * cache. If no stored attributes can be read, return the cached attributes
+ * when @cached is set, and zero otherwise.
+ */
+static __le32 smb2_file_attributes(struct ksmbd_tree_connect *tcon,
+ const struct path *path, bool cached)
+{
+ struct xattr_dos_attrib da;
+
+ if (test_share_config_flag(tcon->share_conf,
+ KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
+ ksmbd_vfs_get_dos_attrib_xattr(mnt_idmap(path->mnt), path->dentry,
+ &da) > 0)
+ return cpu_to_le32(da.attr);
+ return cached ? ksmbd_inode_fattr(path->dentry) : 0;
+}
+
static int smb2_creat(struct ksmbd_work *work,
struct path *path, char *name, int open_flags,
umode_t posix_mode, bool is_dir)
@@ -4554,7 +4574,7 @@ int smb2_open(struct ksmbd_work *work)
int share_ret, need_truncate = 0;
u64 time, alloc_size = 0;
umode_t posix_mode = 0;
- __le32 daccess, maximal_access = 0;
+ __le32 daccess, maximal_access = 0, file_attrs = 0;
u32 dos_attr;
int iov_len = 0;
@@ -4900,8 +4920,6 @@ int smb2_open(struct ksmbd_work *work)
file_present = true;
if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) {
- struct xattr_dos_attrib da;
-
/*
* If file exists with under flags, return access
* denied error.
@@ -4918,16 +4936,6 @@ int smb2_open(struct ksmbd_work *work)
rc = -EACCES;
goto err_out;
}
-
- if (test_share_config_flag(tcon->share_conf,
- KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
- ksmbd_vfs_get_dos_attrib_xattr(mnt_idmap(path.mnt),
- path.dentry, &da) > 0 &&
- da.attr & FILE_ATTRIBUTE_READONLY) {
- rsp->hdr.Status = STATUS_CANNOT_DELETE;
- rc = -EACCES;
- goto err_out;
- }
} else if (d_is_symlink(path.dentry)) {
rc = -EACCES;
goto err_out;
@@ -5007,6 +5015,25 @@ int smb2_open(struct ksmbd_work *work)
daccess = smb_map_generic_desired_access(req->DesiredAccess);
+ /*
+ * Read the existing file's attributes once for the checks below. A
+ * named stream is looked up only later, and the POSIX extensions do
+ * not refuse delete-on-close of a read-only file in the same way. So
+ * for a named stream or a POSIX open, read only the stored attributes,
+ * as before.
+ */
+ if (file_present && req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
+ file_attrs = smb2_file_attributes(tcon, &path,
+ !stream_name && !posix_ctxt);
+
+ /* A read-only file is not deleted on close */
+ if (file_present && req->CreateOptions & FILE_DELETE_ON_CLOSE_LE &&
+ file_attrs & FILE_ATTRIBUTE_READONLY_LE) {
+ rsp->hdr.Status = STATUS_CANNOT_DELETE;
+ rc = -EACCES;
+ goto err_out;
+ }
+
if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
rc = smb_check_perm_dacl(conn, &path, &daccess,
req->DesiredAccess,
@@ -9232,11 +9259,17 @@ static int set_file_disposition_info(struct ksmbd_work *work,
return -EACCES;
}
- if (fp->f_ci->m_fattr & FILE_ATTRIBUTE_READONLY_LE)
- return -EACCES;
-
inode = file_inode(fp->filp);
if (file_info->DeletePending) {
+ if (fp->f_ci->m_fattr & FILE_ATTRIBUTE_READONLY_LE) {
+ struct smb2_set_info_req *req;
+ struct smb2_set_info_rsp *rsp;
+
+ WORK_BUFFERS(work, req, rsp);
+ rsp->hdr.Status = STATUS_CANNOT_DELETE;
+ return -EACCES;
+ }
+
if (ksmbd_has_stream_without_delete_share(fp))
return -ESHARE;
@@ -9520,11 +9553,7 @@ int smb2_set_info(struct ksmbd_work *work)
err_out:
if (rc == -EACCES || rc == -EPERM || rc == -EXDEV) {
- if (fp && req->InfoType == SMB2_O_INFO_FILE &&
- req->FileInfoClass == FILE_DISPOSITION_INFORMATION &&
- fp->f_ci->m_fattr & FILE_ATTRIBUTE_READONLY_LE)
- rsp->hdr.Status = STATUS_CANNOT_DELETE;
- else
+ if (!rsp->hdr.Status)
rsp->hdr.Status = STATUS_ACCESS_DENIED;
}
else if (rc == -EINVAL)
diff --git a/fs/smb/server/vfs_cache.c b/fs/smb/server/vfs_cache.c
index c846023e5ada..7db0a52dd4ae 100644
--- a/fs/smb/server/vfs_cache.c
+++ b/fs/smb/server/vfs_cache.c
@@ -345,6 +345,29 @@ struct ksmbd_inode *ksmbd_inode_lookup_lock(struct dentry *d)
return ci;
}
+/*
+ * No reference is taken. The inode is freed only after it is unhashed under
+ * the write lock, so it stays valid under the read lock.
+ */
+__le32 ksmbd_inode_fattr(struct dentry *d)
+{
+ struct hlist_head *head = inode_hashtable +
+ inode_hash(d_inode(d)->i_sb, (unsigned long)d);
+ struct ksmbd_inode *ci;
+ __le32 fattr = 0;
+
+ read_lock(&inode_hash_lock);
+ hlist_for_each_entry(ci, head, m_hash) {
+ if (ci->m_de == d) {
+ if (atomic_read(&ci->m_count))
+ fattr = READ_ONCE(ci->m_fattr);
+ break;
+ }
+ }
+ read_unlock(&inode_hash_lock);
+ return fattr;
+}
+
int ksmbd_query_inode_status(struct dentry *dentry)
{
struct ksmbd_inode *ci;
diff --git a/fs/smb/server/vfs_cache.h b/fs/smb/server/vfs_cache.h
index 57c81287efe6..bc1955b87855 100644
--- a/fs/smb/server/vfs_cache.h
+++ b/fs/smb/server/vfs_cache.h
@@ -218,6 +218,7 @@ int ksmbd_vfs_set_durable_owner(struct ksmbd_file *fp,
struct ksmbd_file *ksmbd_file_get(struct ksmbd_file *fp);
void ksmbd_fd_put(struct ksmbd_work *work, struct ksmbd_file *fp);
struct ksmbd_inode *ksmbd_inode_lookup_lock(struct dentry *d);
+__le32 ksmbd_inode_fattr(struct dentry *d);
void ksmbd_inode_put(struct ksmbd_inode *ci);
bool ksmbd_close_disconnected_durable_delete_on_close(struct dentry *dentry);
struct ksmbd_file *ksmbd_lookup_global_fd(unsigned long long id);
--
2.47.3
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/4] ksmbd: fail an overwrite or supersede of a directory
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
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
3 siblings, 0 replies; 5+ messages in thread
From: Ian Bridges @ 2026-11-30 5:13 UTC (permalink / raw)
To: linkinjeon
Cc: senozhatsky, tom, chenxiaosong, linux-cifs, linux-kernel,
Ian Bridges
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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 3/4] ksmbd: fail an overwrite that would clear the hidden or system attribute
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 ` [PATCH 2/4] ksmbd: fail an overwrite or supersede of a directory Ian Bridges
@ 2026-11-30 5:13 ` Ian Bridges
2026-11-30 5:13 ` [PATCH 4/4] ksmbd: refuse write access to a read-only file at open Ian Bridges
3 siblings, 0 replies; 5+ messages in thread
From: Ian Bridges @ 2026-11-30 5:13 UTC (permalink / raw)
To: linkinjeon
Cc: senozhatsky, tom, chenxiaosong, linux-cifs, linux-kernel,
Ian Bridges
ksmbd accepts an overwrite or supersede of a file with
FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_SYSTEM when the request leaves
that attribute out. The open can empty the file and clear the
attribute. Such a request should fail with STATUS_ACCESS_DENIED, so
that a client does not overwrite a hidden or system file by accident.
Fail the request. Extend the existing attribute read to cover
overwrites and supersedes. Keep the existing behavior for POSIX opens.
Signed-off-by: Ian Bridges <icb@fastmail.org>
---
fs/smb/server/smb2pdu.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index e500653084ee..2312765cad8f 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -5044,10 +5044,24 @@ int smb2_open(struct ksmbd_work *work)
* for a named stream or a POSIX open, read only the stored attributes,
* as before.
*/
- if (file_present && req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
+ if (file_present &&
+ (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE ||
+ (!stream_name && !posix_ctxt && overwrite)))
file_attrs = smb2_file_attributes(tcon, &path,
!stream_name && !posix_ctxt);
+ /*
+ * An overwrite of a file's unnamed stream keeps its hidden and system
+ * attributes. A POSIX open does not use them.
+ */
+ if (!stream_name && !posix_ctxt && file_present && overwrite &&
+ file_attrs & ~req->FileAttributes &
+ (FILE_ATTRIBUTE_HIDDEN_LE | FILE_ATTRIBUTE_SYSTEM_LE)) {
+ rsp->hdr.Status = STATUS_ACCESS_DENIED;
+ rc = -EACCES;
+ goto err_out;
+ }
+
/* A read-only file is not deleted on close */
if (file_present && req->CreateOptions & FILE_DELETE_ON_CLOSE_LE &&
file_attrs & FILE_ATTRIBUTE_READONLY_LE) {
--
2.47.3
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 4/4] ksmbd: refuse write access to a read-only file at open
2026-11-30 5:13 [PATCH 0/4] ksmbd: fix the checks on an open of an existing file Ian Bridges
` (2 preceding siblings ...)
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 ` Ian Bridges
3 siblings, 0 replies; 5+ messages in thread
From: Ian Bridges @ 2026-11-30 5:13 UTC (permalink / raw)
To: linkinjeon
Cc: senozhatsky, tom, chenxiaosong, linux-cifs, linux-kernel,
Ian Bridges
ksmbd lets a client open a read-only file with FILE_WRITE_DATA or
FILE_APPEND_DATA, and writes through the handle succeed. An overwrite
of a read-only file also succeeds and empties the file, even when the
client asked only to read.
Refuse these opens with STATUS_ACCESS_DENIED. An overwrite counts as a
request for FILE_WRITE_DATA. A supersede needs DELETE access instead,
so a supersede that asks only to read is not refused. Named streams,
directories and POSIX opens are not checked.
The check comes before the delete-on-close check. So a delete-on-close
open of a read-only file that also asks for write access or overwrites
the file now gets STATUS_ACCESS_DENIED instead of STATUS_CANNOT_DELETE.
MAXIMUM_ALLOWED is not changed, so a plain MAXIMUM_ALLOWED open of a
read-only file can still get write access.
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 2312765cad8f..89aad1acb370 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -5046,7 +5046,10 @@ int smb2_open(struct ksmbd_work *work)
*/
if (file_present &&
(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE ||
- (!stream_name && !posix_ctxt && overwrite)))
+ (!stream_name && !posix_ctxt &&
+ (overwrite ||
+ (!S_ISDIR(d_inode(path.dentry)->i_mode) &&
+ daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE))))))
file_attrs = smb2_file_attributes(tcon, &path,
!stream_name && !posix_ctxt);
@@ -5062,6 +5065,25 @@ int smb2_open(struct ksmbd_work *work)
goto err_out;
}
+ /*
+ * A read-only file refuses write access to its data. FILE_OVERWRITE
+ * and FILE_OVERWRITE_IF write the data, so an overwrite is refused
+ * even when the client asked only to read. FILE_SUPERSEDE needs
+ * DELETE access instead, so a supersede is refused only when the
+ * client asked for write access. Named streams, directories and POSIX
+ * opens are not checked.
+ */
+ if (!stream_name && !posix_ctxt && file_present &&
+ !S_ISDIR(d_inode(path.dentry)->i_mode) &&
+ (req->CreateDisposition == FILE_OVERWRITE_LE ||
+ req->CreateDisposition == FILE_OVERWRITE_IF_LE ||
+ daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE)) &&
+ file_attrs & FILE_ATTRIBUTE_READONLY_LE) {
+ rsp->hdr.Status = STATUS_ACCESS_DENIED;
+ rc = -EACCES;
+ goto err_out;
+ }
+
/* A read-only file is not deleted on close */
if (file_present && req->CreateOptions & FILE_DELETE_ON_CLOSE_LE &&
file_attrs & FILE_ATTRIBUTE_READONLY_LE) {
--
2.47.3
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-30 5:14 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 2/4] ksmbd: fail an overwrite or supersede of a directory Ian Bridges
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox