]> git.hungrycats.org Git - linux/commitdiff
ksmbd: use the existing xattr name for a named stream
authorDaeMyung Kang <charsyam@gmail.com>
Tue, 3 Nov 2026 19:48:46 +0000 (14:48 -0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 6 Nov 2026 17:06:13 +0000 (12:06 -0500)
[ Upstream commit a7d1d52748e0fe4d06fdb871fc9899904fbba93f ]

SMB stream names are case-insensitive, but ksmbd stores each named
stream in an xattr whose name is case-sensitive. Opening an existing
stream finds its xattr with a case-insensitive match, yet the handle
keeps the name as the client spelled it, and every later operation that
needs an exact name uses that spelling:

 - a WRITE or an end-of-file change creates a second xattr, so a stream
   created as "Foo" and written through "foo" ends up stored twice, and
   which value a later lookup returns depends on xattr list order;
 - delete-on-close and delete-pending removal can fail with -ENODATA,
   or remove only a case-variant copy and leave the original;
 - the share mode check compares stream names with strcmp(), so two
   opens of the same stream that differ only in case do not conflict.

Have ksmbd_vfs_casexattr_len() copy the matching xattr's name into the
handle when the stream is opened, so later operations use the name
already on disk. The match must have the same length as the name it
replaces; the stream lookup passes a length that includes the
terminating NUL, which already guarantees that, and the check makes the
copy safe for any caller. A stream that does not exist yet keeps the
client's spelling, as before.

smb2_rename() compares and looks up fp->stream.name case-insensitively,
so it behaves the same with either spelling.

Fixes: e2f34481b24d ("cifsd: add server-side procedures for SMB3")
Cc: stable@vger.kernel.org
Signed-off-by: DaeMyung Kang <charsyam@gmail.com>
Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
[ sashal: adapted stream lookup signature changes to the existing callers and non-const mnt_idmap interface ]
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/smb/server/smb2pdu.c
fs/smb/server/vfs.c
fs/smb/server/vfs.h

index 070da678d2ce0863e05c0895ba2d3da7f6439e6d..847470490a209855bb8e6dd92abf73b34bf3f793 100644 (file)
@@ -2475,7 +2475,7 @@ static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len,
                                                     path->dentry,
                                                     attr_name,
                                                     XATTR_USER_PREFIX_LEN +
-                                                    eabuf->EaNameLength);
+                                                    eabuf->EaNameLength, NULL);
 
                        /* delete the EA only when it exits */
                        if (rc > 0) {
@@ -2547,11 +2547,11 @@ static noinline int smb2_set_stream_name_xattr(const struct path *path,
        fp->stream.name = xattr_stream_name;
        fp->stream.size = xattr_stream_size;
 
-       /* Check if there is stream prefix in xattr space */
+       /* Keep the existing xattr's case for subsequent writes and removal. */
        rc = ksmbd_vfs_casexattr_len(idmap,
                                     path->dentry,
                                     xattr_stream_name,
-                                    xattr_stream_size);
+                                    xattr_stream_size, xattr_stream_name);
        if (rc >= 0)
                return 0;
        if (rc != -ENOENT && rc != -ENODATA)
@@ -5276,7 +5276,7 @@ static int get_file_stream_info(struct ksmbd_work *work,
                 */
                slen = ksmbd_vfs_casexattr_len(file_mnt_idmap(fp->filp),
                                                path->dentry, stream_name,
-                                               strlen(stream_name) + 1);
+                                               strlen(stream_name) + 1, NULL);
                ssize = slen < 0 ? 0 : (loff_t)slen;
                file_info->StreamSize = cpu_to_le64(ssize);
                file_info->StreamAllocationSize = cpu_to_le64(ssize);
index 29b204776b961bacce5eb52f51a8cab3b79b96cf..0a27cb445cc17d09d2160de59afe108540d82506 100644 (file)
@@ -1816,7 +1816,7 @@ int ksmbd_vfs_fill_dentry_attrs(struct ksmbd_work *work,
 
 ssize_t ksmbd_vfs_casexattr_len(struct mnt_idmap *idmap,
                                struct dentry *dentry, char *attr_name,
-                               int attr_name_len)
+                               int attr_name_len, char *actual_name)
 {
        char *name, *xattr_list = NULL;
        ssize_t value_len = -ENOENT, xattr_list_len;
@@ -1834,8 +1834,13 @@ ssize_t ksmbd_vfs_casexattr_len(struct mnt_idmap *idmap,
                ksmbd_debug(VFS, "%s, len %zd\n", name, strlen(name));
                if (strncasecmp(attr_name, name, attr_name_len))
                        continue;
+               if (actual_name && strlen(name) + 1 != attr_name_len)
+                       continue;
 
                value_len = ksmbd_vfs_xattr_len(idmap, dentry, name);
+               /* The caller provides attr_name_len bytes for the actual name. */
+               if (value_len >= 0 && actual_name)
+                       memcpy(actual_name, name, attr_name_len);
                break;
        }
 
index df6421b4590baea593a7f20fc64d5ed1e9067ea7..ccf289592718c7e42a2ef615fbff279add0ef464 100644 (file)
@@ -107,7 +107,7 @@ ssize_t ksmbd_vfs_getxattr(struct mnt_idmap *idmap,
                           char **xattr_buf);
 ssize_t ksmbd_vfs_casexattr_len(struct mnt_idmap *idmap,
                                struct dentry *dentry, char *attr_name,
-                               int attr_name_len);
+                               int attr_name_len, char *actual_name);
 int ksmbd_vfs_setxattr(struct mnt_idmap *idmap,
                       const struct path *path, const char *attr_name,
                       void *attr_value, size_t attr_size, int flags,