* [PATCH] ksmbd: delete only the stream whose handle has delete-on-close
@ 2026-12-01 4:10 Ian Bridges
2026-12-01 6:23 ` ChenXiaoSong
0 siblings, 1 reply; 2+ messages in thread
From: Ian Bridges @ 2026-12-01 4:10 UTC (permalink / raw)
To: linkinjeon
Cc: senozhatsky, tom, chenxiaosong, gael.blivet, linux-cifs,
linux-kernel, Ian Bridges, stable
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] ksmbd: delete only the stream whose handle has delete-on-close
2026-12-01 4:10 [PATCH] ksmbd: delete only the stream whose handle has delete-on-close Ian Bridges
@ 2026-12-01 6:23 ` ChenXiaoSong
0 siblings, 0 replies; 2+ messages in thread
From: ChenXiaoSong @ 2026-12-01 6:23 UTC (permalink / raw)
To: Ian Bridges, linkinjeon
Cc: senozhatsky, tom, gael.blivet, linux-cifs, linux-kernel, stable
Reviewed-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
On 12/1/26 12:10, Ian Bridges wrote:
> 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(-)
--
ChenXiaoSong <chenxiaosong@chenxiaosong.com>
Chinese Homepage: https://chenxiaosong.com
English Homepage: https://chenxiaosong.com/en
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-01 6:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01 4:10 [PATCH] ksmbd: delete only the stream whose handle has delete-on-close Ian Bridges
2026-12-01 6:23 ` ChenXiaoSong
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®