ksmbd: fail FILE_CREATE of a named stream that exists
Subsystems (2)
PATCH 1/1 ksmbd: fail FILE_CREATE of a named stream that exists
An open with FILE_CREATE of a named stream that already exists succeeds, and the client gets a handle to the existing stream. The stream lookup ignores case, so a name that differs only in case also opens the existing stream. A client that uses FILE_CREATE to make a new stream cannot tell that the stream existed, and its writes change the existing data.
Fail an open with FILE_CREATE of an existing named stream with STATUS_OBJECT_NAME_COLLISION, as an open with FILE_CREATE of an existing file already fails.
---
Tested on ksmbd-for-next 87ff512becd4, on x86_64 with KASAN. A script sent FILE_CREATE of an existing named stream in several forms, and the other dispositions, to the base and to the patched server. Each FILE_CREATE of an existing stream got STATUS_OBJECT_NAME_COLLISION and left the stream as it was. The others gave the same results 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/smb2pdu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/smb/server/smb2pdu.c +1 -1
@@ -3712,7 +3712,7 @@ static noinline int smb2_set_stream_name_xattr(const struct path *path, xattr_stream_name, xattr_stream_size, xattr_stream_name);if (rc >=0 ) -return 0 ; +return fp->cdoption ==FILE_CREATE_LE ? -EEXIST :0 ;if (rc != -ENOENT && rc != -ENODATA )return rc;
base-commit: 87ff512becd45a08c44fd47a9f03e607af9ef6bd
Signature · 1 line
2.47.3
ChenXiaoSong
reviewerSashiko's comment seems valid: https://sashiko.dev/#/patchset/20261201022248.2167102-1-icb%40fastmail.org
We can also include the following changes:
```
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -3690,7 +3690,7 @@ static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len,
static noinline int smb2_set_stream_name_xattr(const struct path *path,
struct ksmbd_file *fp,
char *stream_name, int
s_type,
- bool *created)
+ struct stream *stream, bool *created)
{
const struct mnt_idmap *idmap = mnt_idmap(path->mnt);
size_t xattr_stream_size;
@@ -3704,8 +3704,8 @@ static noinline int smb2_set_stream_name_xattr(const struct path *path,
if (rc)
return rc;
- fp->stream.name = xattr_stream_name;
- fp->stream.size = xattr_stream_size;
+ stream->name = xattr_stream_name;
+ stream->size = xattr_stream_size;
/* Keep the existing xattr's case for subsequent writes and
removal. */
rc = ksmbd_vfs_casexattr_len(idmap,
@@ -5291,13 +5291,20 @@ int smb2_open(struct ksmbd_work *work)
}
if (stream_name) {
+ struct stream stream = {};
+
rc = smb2_set_stream_name_xattr(&path,
fp,
stream_name,
s_type,
+ &stream,
&stream_created);
- if (rc)
+ if (rc) {
+ kfree(stream.name);
goto err_out;
+ }
+ /* Only an opened stream may participate in delete-on-close. */
+ fp->stream = stream;
file_info = FILE_CREATED;
}
```Quoted 11 lines · Ian Bridges
fs/smb/server/smb2pdu.c +1
-1@@ -3712,7 +3712,7 @@ static noinline int smb2_set_stream_name_xattr(const struct path *path, xattr_stream_name, xattr_stream_size, xattr_stream_name);if (rc >=0 ) -return 0 ; +return fp->cdoption ==FILE_CREATE_LE ? -EEXIST :0 ;if (rc != -ENOENT && rc != -ENODATA )return rc;
Signature · 3 lines
ChenXiaoSong <[email protected]> Chinese Homepage: https://chenxiaosong.com English Homepage: https://chenxiaosong.com/en
PATCH v2 1/1 ksmbd: fail FILE_CREATE of a named stream that exists
In reply to Ian BridgesAn open with FILE_CREATE of a named stream that already exists succeeds, and the client gets a handle to the existing stream. The stream lookup ignores case, so a name that differs only in case also opens the existing stream. A client that uses FILE_CREATE to make a new stream cannot tell that the stream existed, and its writes change the existing data.
Fail an open with FILE_CREATE of an existing named stream with STATUS_OBJECT_NAME_COLLISION, as an open with FILE_CREATE of an existing file already fails.
The handle gets its stream name before the stream is looked up, so a failed open of a stream is closed as a stream handle. When another handle of the file has delete-on-close on a stream, that close can remove the stream that the failed open named. The new failure would then delete the existing stream. Make the handle a stream handle only after the stream is opened.
---
v2: Make the handle a stream handle only after the stream is opened, as ChenXiaoSong suggested. Sashiko found that the failed FILE_CREATE could remove the existing stream when another handle of the file has delete-on-close on a stream.
Tested on ksmbd-for-next 87ff512becd4, on x86_64 with KASAN. A script sent FILE_CREATE of an existing named stream in several forms, and the other dispositions, to the base and to the patched server. Each FILE_CREATE of an existing stream got STATUS_OBJECT_NAME_COLLISION and left the stream as it was. The others gave the same results as on the base. A second script held delete-on-close on one stream of a file and then sent a FILE_CREATE of another existing stream of the file. With v1 the existing stream was removed. With v2 the existing stream was kept. smbtorture gives the same results as on the base, and KASAN reported nothing.
This patch was developed with LLM assistance.
fs/smb/server/smb2pdu.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
fs/smb/server/smb2pdu.c +15 -4
@@ -3689,6 +3689,7 @@ static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len, static noinline int smb2_set_stream_name_xattr(const struct path *path,struct ksmbd_file *fp,char *stream_name,int s_type, +struct stream *stream,bool *created) {const struct mnt_idmap *idmap =mnt_idmap (path->mnt);
@@ -3703,8 +3704,8 @@ static noinline int smb2_set_stream_name_xattr(const struct path *path,if (rc)return rc; - fp->stream.name = xattr_stream_name; - fp->stream.size = xattr_stream_size; + stream->name = xattr_stream_name; + stream->size = xattr_stream_size;/* Keep the existing xattr's case for subsequent writes and removal. */ rc = ksmbd_vfs_casexattr_len(idmap,
@@ -3712,7 +3713,7 @@ static noinline int smb2_set_stream_name_xattr(const struct path *path, xattr_stream_name, xattr_stream_size, xattr_stream_name);if (rc >=0 ) -return 0 ; +return fp->cdoption ==FILE_CREATE_LE ? -EEXIST :0 ;if (rc != -ENOENT && rc != -ENODATA )return rc;
@@ -5370,13 +5371,23 @@ int smb2_open(struct ksmbd_work *work) }if (stream_name) { +struct stream stream = {}; + rc =smb2_set_stream_name_xattr (&path, fp, stream_name, s_type, + &stream, &stream_created); -if (rc) +if (rc) { +kfree (stream.name); goto err_out; + } +/* +* Attach the stream only on success, so that a failed lookup +* or creation cannot delete a stream when fp is closed. +*/ + fp->stream = stream; file_info =FILE_CREATED ; }
base-commit: 87ff512becd45a08c44fd47a9f03e607af9ef6bd
Signature · 1 line
2.47.3
Sorry, I sent the v2 of this patch as a reply to this thread by mistake. I am sending the same v2 again as a new thread. Please ignore the v2 in this thread.