From: Ian Bridges <icb@fastmail.org>
To: linkinjeon@kernel.org
Cc: senozhatsky@chromium.org, tom@talpey.com,
chenxiaosong@chenxiaosong.com, gael.blivet@gmail.com,
linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org,
Ian Bridges <icb@fastmail.org>,
stable@vger.kernel.org
Subject: [PATCH] ksmbd: delete only the stream whose handle has delete-on-close
Date: Mon, 30 Nov 2026 22:10:29 -0600 [thread overview]
Message-ID: <20261201041029.2181504-1-icb@fastmail.org> (raw)
A delete-on-close open of a named stream sets a flag on the inode
instead of on the handle. The next stream handle of the file to close
takes the flag and deletes its own stream. So when another stream
handle of the file closes first, that other stream is deleted, and the
stream that was opened with delete-on-close is kept.
The stream is removed with the credentials of the closing handle. A
user who cannot write the file can open one stream with delete-on-close
and make another user's handle delete a different stream on close.
Record delete-on-close on the stream handle, as
FileDispositionInformation on a stream handle already does. Deleting a
stream never removes the file, so delete-on-close of a stream no longer
makes a new delete-on-close open close the disconnected durable handles
of the file.
Fixes: f44158485826 ("cifsd: add file operations")
Cc: stable@vger.kernel.org
Signed-off-by: Ian Bridges <icb@fastmail.org>
---
Tested on ksmbd-for-next 87ff512becd4, on x86_64 with KASAN. A script
opened named streams of one file with and without delete-on-close, as
the owner and as a user who cannot write the file, and closed the
handles in different orders. On the base, the first stream handle to
close deleted its own stream. With the patch, only the streams opened
with delete-on-close were deleted, and FileDispositionInformation on a
stream behaved as on the base. smbtorture gives the same results as on
the base, and KASAN reported nothing.
This patch was developed with LLM assistance.
fs/smb/server/vfs_cache.c | 41 ++++++++++++++++++---------------------
fs/smb/server/vfs_cache.h | 1 +
2 files changed, 20 insertions(+), 22 deletions(-)
diff --git a/fs/smb/server/vfs_cache.c b/fs/smb/server/vfs_cache.c
index 7db0a52dd4ae..a71a5de17653 100644
--- a/fs/smb/server/vfs_cache.c
+++ b/fs/smb/server/vfs_cache.c
@@ -29,7 +29,6 @@
#define S_DEL_PENDING 1
#define S_DEL_ON_CLS 2
-#define S_DEL_ON_CLS_STREAM 8
static unsigned int inode_hash_mask __read_mostly;
static unsigned int inode_hash_shift __read_mostly;
@@ -463,11 +462,18 @@ void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp,
{
struct ksmbd_inode *ci = fp->f_ci;
+ if (ksmbd_stream_fd(fp)) {
+ /*
+ * Set only by the initial CREATE and read only at final close.
+ * The opener holds a reference, so the read cannot race with
+ * this write.
+ */
+ fp->stream_del_on_close = true;
+ return;
+ }
+
down_write(&ci->m_lock);
- if (ksmbd_stream_fd(fp))
- ci->m_flags |= S_DEL_ON_CLS_STREAM;
- else
- ci->m_flags |= S_DEL_ON_CLS;
+ ci->m_flags |= S_DEL_ON_CLS;
up_write(&ci->m_lock);
}
@@ -478,10 +484,9 @@ void ksmbd_fd_set_delete_on_close(struct ksmbd_file *fp,
* the entire file's data along with it.
*
* This is tracked on fp itself (stream_del_pending), not the shared
- * ksmbd_inode: the inode-wide S_DEL_ON_CLS_STREAM flag used by
- * ksmbd_fd_set_delete_on_close() can't record *which* stream should be
- * deleted, so if a different stream handle on the same file closed
- * first, it would delete the wrong stream.
+ * ksmbd_inode, so that closing fp deletes only the stream that fp has
+ * open. Delete-on-close of a stream is tracked the same way
+ * (stream_del_on_close).
*/
void ksmbd_fd_set_delete_pending(struct ksmbd_file *fp)
{
@@ -616,20 +621,12 @@ static void __ksmbd_inode_close(struct ksmbd_file *fp)
filp = fp->filp;
if (ksmbd_stream_fd(fp)) {
- bool remove_stream_xattr = false;
-
- down_write(&ci->m_lock);
- if (ci->m_flags & S_DEL_ON_CLS_STREAM) {
- ci->m_flags &= ~S_DEL_ON_CLS_STREAM;
- remove_stream_xattr = true;
- }
- up_write(&ci->m_lock);
+ bool remove_stream_xattr = fp->stream_del_on_close;
/*
- * Per-handle delete-pending from ksmbd_fd_set_delete_pending()
- * (FileDispositionInformation on this stream) -- separate from
- * the inode-wide flag above, which only ever meant "some
- * stream on this file" with no way to say which one.
+ * Delete the stream of this handle if the handle was opened
+ * with delete-on-close or marked delete-pending through
+ * FileDispositionInformation.
*/
spin_lock(&fp->f_lock);
if (fp->stream_del_pending) {
@@ -997,7 +994,7 @@ bool ksmbd_close_disconnected_durable_delete_on_close(struct dentry *dentry)
return false;
down_write(&ci->m_lock);
- if (ci->m_flags & (S_DEL_ON_CLS | S_DEL_ON_CLS_STREAM | S_DEL_PENDING)) {
+ if (ci->m_flags & (S_DEL_ON_CLS | S_DEL_PENDING)) {
list_for_each_entry_safe(fp, tmp, &ci->m_fp_list, node) {
if (fp->conn || !fp->is_durable ||
fp->f_state != FP_INITED)
diff --git a/fs/smb/server/vfs_cache.h b/fs/smb/server/vfs_cache.h
index bc1955b87855..7253b1296a67 100644
--- a/fs/smb/server/vfs_cache.h
+++ b/fs/smb/server/vfs_cache.h
@@ -150,6 +150,7 @@ struct ksmbd_file {
* was actually marked for deletion. See ksmbd_fd_set_delete_pending().
*/
bool stream_del_pending;
+ bool stream_del_on_close;
unsigned int durable_timeout;
unsigned int durable_scavenger_timeout;
base-commit: 87ff512becd45a08c44fd47a9f03e607af9ef6bd
--
2.47.3
next reply other threads:[~2026-12-01 4:10 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 4:10 Ian Bridges [this message]
2026-12-01 6:23 ` ChenXiaoSong
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=20261201041029.2181504-1-icb@fastmail.org \
--to=icb@fastmail.org \
--cc=chenxiaosong@chenxiaosong.com \
--cc=gael.blivet@gmail.com \
--cc=linkinjeon@kernel.org \
--cc=linux-cifs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=senozhatsky@chromium.org \
--cc=stable@vger.kernel.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®