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 1/4] ksmbd: check the cached read-only attribute for delete-on-close
Date: Sun, 29 Nov 2026 23:13:46 -0600 [thread overview]
Message-ID: <0b59bc735ec6399e7f034717f4c432389f19029f.1796015315.git.icb@fastmail.org> (raw)
In-Reply-To: <cover.1796015315.git.icb@fastmail.org>
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
next prev 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 ` Ian Bridges [this message]
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
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=0b59bc735ec6399e7f034717f4c432389f19029f.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®