ksmbd: make utf-8 file name comparison work in __caseless_lookup()
Subsystems (2)
PATCH 1/1 ksmbd: make utf-8 file name comparison work in __caseless_lookup()
Case-insensitive file name lookups with __caseless_lookup() use strncasecmp() for file name comparison. strncasecmp() assumes an ISO8859-1-compatible encoding, which is not the case here as UTF-8 is always used. As such, use of strncasecmp() here produces correct results only if both strings use characters in the ASCII range only. Fix this by using utf8_strncasecmp() if CONFIG_UNICODE is set. On failure or if CONFIG_UNICODE is not set, fallback to strncasecmp(). Also, as we are adding an include for linux/unicode.h', include it in fs/ksmbd/connection.h' as well since it should be explicit there.
--- fs/ksmbd/connection.h | 1 + fs/ksmbd/vfs.c | 20 +++++++++++++++++--- fs/ksmbd/vfs.h | 2 ++ 3 files changed, 20 insertions(+), 3 deletions(-)
fs/ksmbd/connection.h +1 -0
@@ -14,6 +14,7 @@#include <net/request_sock.h> #include <linux/kthread.h> #include <linux/nls.h> +#include <linux/unicode.h> #include "smb_common.h" #include "ksmbd_work.h"
fs/ksmbd/vfs.c +17 -3
@@ -1145,12 +1145,23 @@ static int __caseless_lookup(struct dir_context *ctx, const char *name, unsigned int d_type) {struct ksmbd_readdir_data *buf; +int cmp; buf = container_of(ctx, struct ksmbd_readdir_data, ctx);if (buf->used != namlen)return 0 ; -if (!strncasecmp ((char *)buf->private, name, namlen)) { +if (IS_ENABLED (CONFIG_UNICODE ) && buf->um) { +const struct qstr q_buf = {.name = buf->private, + .len = buf->used}; +const struct qstr q_name = {.name = name, + .len = namlen}; + + cmp =utf8_strncasecmp (buf->um, &q_buf, &q_name); + } +if (!(IS_ENABLED (CONFIG_UNICODE ) && buf->um) || cmp <0 ) + cmp =strncasecmp ((char *)buf->private, name, namlen); +if (!cmp) {memcpy ((char *)buf->private, name, namlen); buf->dirent_count =1 ;return -EEXIST ;
@@ -1166,7 +1177,8 @@ static int __caseless_lookup(struct dir_context *ctx, const char *name,* * Return: 0 on success, otherwise error */ -static int ksmbd_vfs_lookup_in_dir (const struct path *dir,char *name,size_t namelen) +static int ksmbd_vfs_lookup_in_dir (const struct path *dir,char *name, +size_t namelen,struct unicode_map *um) {int ret;struct file *dfilp;
@@ -1176,6 +1188,7 @@ static int ksmbd_vfs_lookup_in_dir(const struct path *dir, char *name, size_t na .private = name, .used = namelen, .dirent_count = 0, + .um = um, }; dfilp =dentry_open (dir, flags,current_cred ());
@@ -1238,7 +1251,8 @@ int ksmbd_vfs_kern_path(struct ksmbd_work *work, char *name,break ; err =ksmbd_vfs_lookup_in_dir (&parent, filename, - filename_len); + filename_len, + work->conn->um);path_put (&parent);if (err) goto out;
fs/ksmbd/vfs.h +2 -0
@@ -12,6 +12,7 @@#include <linux/namei.h> #include <uapi/linux/xattr.h> #include <linux/posix_acl.h> +#include <linux/unicode.h> #include "smbacl.h" #include "xattr.h"
@@ -60,6 +61,7 @@ struct ksmbd_readdir_data {unsigned int used;unsigned int dirent_count;unsigned int file_attr; +struct unicode_map *um; };/* ksmbd kstat wrapper to get valid create time when reading dir entry */
Signature · 1 line
2.37.3
Namjae Jeon
maintainer2022-09-24 11:23 GMT+09:00, Atte Heikkilä <[email protected]>:
Quoted 34 lines
Case-insensitive file name lookups with
__caseless_lookup()usestrncasecmp()for file name comparison.strncasecmp()assumes an ISO8859-1-compatible encoding, which is not the case here as UTF-8 is always used. As such, use ofstrncasecmp()here produces correct results only if both strings use characters in the ASCII range only. Fix this by usingutf8_strncasecmp()ifCONFIG_UNICODEis set. On failure or ifCONFIG_UNICODEis not set, fallback tostrncasecmp(). Also, as we are adding an include forlinux/unicode.h', include it infs/ksmbd/connection.h' as well since it should be explicit there.Signed-off-by Atte Heikkilä <[email protected]>--- fs/ksmbd/connection.h | 1 + fs/ksmbd/vfs.c | 20 +++++++++++++++++--- fs/ksmbd/vfs.h | 2 ++ 3 files changed, 20 insertions(+), 3 deletions(-)fs/ksmbd/connection.h +1
-0@@ -14,6 +14,7 @@#include <net/request_sock.h> #include <linux/kthread.h> #include <linux/nls.h> +#include <linux/unicode.h> #include "smb_common.h" #include "ksmbd_work.h" fs/ksmbd/vfs.c +0
-0
fs/ksmbd/vfs.c static int __caseless_lookup(struct dir_context · line 1149
Quoted 5 lines
fs/ksmbd/vfs.c +1
-0@@ -1145,12 +1145,23 @@ static int __caseless_lookup(struct dir_context *ctx, const char *name, unsigned int d_type) {struct ksmbd_readdir_data *buf; +int cmp;
cmp should be initialized with -EINVAL to fallback strncasecmp() ?
Quoted 5 lines
fs/ksmbd/vfs.c +0
-0buf = container_of(ctx, struct ksmbd_readdir_data, ctx);if (buf->used != namlen)return 0 ;
fs/ksmbd/vfs.c static int __caseless_lookup(struct dir_context *ctx, const char *name, · line 1162
Quoted 10 lines
fs/ksmbd/vfs.c +9
-1-if (!strncasecmp ((char *)buf->private, name, namlen)) { +if (IS_ENABLED (CONFIG_UNICODE ) && buf->um) { +const struct qstr q_buf = {.name = buf->private, + .len = buf->used}; +const struct qstr q_name = {.name = name, + .len = namlen}; + + cmp =utf8_strncasecmp (buf->um, &q_buf, &q_name); + } +if (!(IS_ENABLED (CONFIG_UNICODE ) && buf->um) || cmp <0 )
I wonder why ->um is checked with CONFIG_UNICODE.
Thanks.
Quoted 61 lines
fs/ksmbd/vfs.c +4
-1+ cmp =strncasecmp ((char *)buf->private, name, namlen); +if (!cmp) {memcpy ((char *)buf->private, name, namlen); buf->dirent_count =1 ;return -EEXIST ;@@ -1166,7 +1177,8 @@ static int __caseless_lookup(struct dir_context *ctx,const char *name, * * Return: 0 on success, otherwise error */ -static int ksmbd_vfs_lookup_in_dir (const struct path *dir,char *name, size_t namelen) +static int ksmbd_vfs_lookup_in_dir (const struct path *dir,char *name, +size_t namelen,struct unicode_map *um) {fs/ksmbd/vfs.c +1
-0int ret;struct file *dfilp;@@ -1176,6 +1188,7 @@ static int ksmbd_vfs_lookup_in_dir(const struct path *dir, char *name, size_t na .private = name, .used = namelen, .dirent_count = 0, + .um = um, };fs/ksmbd/vfs.c +2
-1dfilp =dentry_open (dir, flags,current_cred ());@@ -1238,7 +1251,8 @@ int ksmbd_vfs_kern_path(struct ksmbd_work *work, char *name, break; err =ksmbd_vfs_lookup_in_dir (&parent, filename, - filename_len); + filename_len, + work->conn->um);path_put (&parent);if (err)fs/ksmbd/vfs.c +0
-0goto out;fs/ksmbd/vfs.h +2
-1@@ -12,6 +12,7 @@#include <linux/namei.h> #include <uapi/linux/xattr.h> #include <linux/posix_acl.h> +#include <linux/unicode.h> #include "smbacl.h" #include "xattr.h" @@ -60,6 +61,7 @@ struct ksmbd_readdir_data {unsigned int used;unsigned int dirent_count;unsigned int file_attr; +struct unicode_map *um; };/* ksmbd kstat wrapper to get valid create time when reading dir entry */ --2.37.3
Quoted 28 lines · Namjae Jeon
2022-09-24 11:23 GMT+09:00, Atte Heikkilä <[email protected]>:
Quoted 27 lines
Case-insensitive file name lookups with
__caseless_lookup()usestrncasecmp()for file name comparison.strncasecmp()assumes an ISO8859-1-compatible encoding, which is not the case here as UTF-8 is always used. As such, use ofstrncasecmp()here produces correct results only if both strings use characters in the ASCII range only. Fix this by usingutf8_strncasecmp()ifCONFIG_UNICODEis set. On failure or ifCONFIG_UNICODEis not set, fallback tostrncasecmp(). Also, as we are adding an include forlinux/unicode.h', include it infs/ksmbd/connection.h' as well since it should be explicit there.Signed-off-by Atte Heikkilä <[email protected]>--- fs/ksmbd/connection.h | 1 + fs/ksmbd/vfs.c | 20 +++++++++++++++++--- fs/ksmbd/vfs.h | 2 ++ 3 files changed, 20 insertions(+), 3 deletions(-)fs/ksmbd/connection.h +1
-0@@ -14,6 +14,7 @@#include <net/request_sock.h> #include <linux/kthread.h> #include <linux/nls.h> +#include <linux/unicode.h>
Quoted 13 lines
fs/ksmbd/vfs.c static int __caseless_lookup(struct dir_context · line 1149
Quoted 12 lines
fs/ksmbd/connection.h +0
-0#include "smb_common.h" #include "ksmbd_work.h" fs/ksmbd/vfs.c +1
-0@@ -1145,12 +1145,23 @@ static int __caseless_lookup(struct dir_context *ctx, const char *name, unsigned int d_type) {struct ksmbd_readdir_data *buf; +int cmp;cmp should be initialized with
-EINVALto fallbackstrncasecmp()?
Please see below for the explanation.
Quoted 4 lines
Quoted 4 lines
fs/ksmbd/vfs.c +0
-0buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
Quoted 13 lines
fs/ksmbd/vfs.c static int __caseless_lookup(struct dir_context *ctx, const char *name, · line 1162
Quoted 12 lines
fs/ksmbd/vfs.c +9
-1if (buf->used != namlen)return 0 ; -if (!strncasecmp ((char *)buf->private, name, namlen)) { +if (IS_ENABLED (CONFIG_UNICODE ) && buf->um) { +const struct qstr q_buf = {.name = buf->private, + .len = buf->used}; +const struct qstr q_name = {.name = name, + .len = namlen}; + + cmp =utf8_strncasecmp (buf->um, &q_buf, &q_name); + } +if (!(IS_ENABLED (CONFIG_UNICODE ) && buf->um) || cmp <0 )I wonder why ->um is checked with
CONFIG_UNICODE.
If !(IS_ENABLED(CONFIG_UNICODE) && buf->um) is true, then utf8_strncasecmp() was not called. If !(IS_ENABLED(CONFIG_UNICODE) && buf->um) is false, then utf8_strncasecmp() was called and we check for an error with cmp < 0'. Alternatively, cmp' can be initialized to -EINVAL and then !(IS_ENABLED(CONFIG_UNICODE) && buf->um) can be removed.
The latter is preferred, right?
Quoted 64 lines
Thanks.
Quoted 61 lines
fs/ksmbd/vfs.c +4
-1+ cmp =strncasecmp ((char *)buf->private, name, namlen); +if (!cmp) {memcpy ((char *)buf->private, name, namlen); buf->dirent_count =1 ;return -EEXIST ;@@ -1166,7 +1177,8 @@ static int __caseless_lookup(struct dir_context *ct> x,const char *name, * * Return: 0 on success, otherwise error */ -static int ksmbd_vfs_lookup_in_dir (const struct path *dir,char *name, size_t namelen) +static int ksmbd_vfs_lookup_in_dir (const struct path *dir,char *name, +size_t namelen,struct unicode_map *um) {fs/ksmbd/vfs.c +1
-0int ret;struct file *dfilp;@@ -1176,6 +1188,7 @@ static int ksmbd_vfs_lookup_in_dir(const struct pat> h *dir, char *name, size_t na .private = name, .used = namelen, .dirent_count = 0, + .um = um, };fs/ksmbd/vfs.c +2
-1dfilp =dentry_open (dir, flags,current_cred ());@@ -1238,7 +1251,8 @@ int ksmbd_vfs_kern_path(struct ksmbd_work *work, ch> ar *name, break; err =ksmbd_vfs_lookup_in_dir (&parent, filename, - filename_len); + filename_len, + work->conn->um);path_put (&parent);if (err)fs/ksmbd/vfs.c +0
-0goto out;fs/ksmbd/vfs.h +2
-1@@ -12,6 +12,7 @@#include <linux/namei.h> #include <uapi/linux/xattr.h> #include <linux/posix_acl.h> +#include <linux/unicode.h> #include "smbacl.h" #include "xattr.h" @@ -60,6 +61,7 @@ struct ksmbd_readdir_data {unsigned int used;unsigned int dirent_count;unsigned int file_attr; +struct unicode_map *um; };/* ksmbd kstat wrapper to get valid create time when reading dir entry *> / -- 2.37.3
Namjae Jeon
maintainer2022-09-25 23:23 GMT+09:00, Atte Heikkilä <[email protected]>:
Quoted 62 lines · Namjae Jeon
Quoted 28 lines
2022-09-24 11:23 GMT+09:00, Atte Heikkilä <[email protected]>:
Quoted 27 lines
Case-insensitive file name lookups with
__caseless_lookup()usestrncasecmp()for file name comparison.strncasecmp()assumes an ISO8859-1-compatible encoding, which is not the case here as UTF-8 is always used. As such, use ofstrncasecmp()here produces correct results only if both strings use characters in the ASCII range only. Fix this by usingutf8_strncasecmp()ifCONFIG_UNICODEis set. On failure or ifCONFIG_UNICODEis not set, fallback tostrncasecmp(). Also, as we are adding an include forlinux/unicode.h', include it infs/ksmbd/connection.h' as well since it should be explicit there.Signed-off-by Atte Heikkilä <[email protected]>--- fs/ksmbd/connection.h | 1 + fs/ksmbd/vfs.c | 20 +++++++++++++++++--- fs/ksmbd/vfs.h | 2 ++ 3 files changed, 20 insertions(+), 3 deletions(-)fs/ksmbd/connection.h +1
-0@@ -14,6 +14,7 @@#include <net/request_sock.h> #include <linux/kthread.h> #include <linux/nls.h> +#include <linux/unicode.h> Quoted 13 lines
fs/ksmbd/vfs.c static int __caseless_lookup(struct dir_context · line 1149
Quoted 12 lines
fs/ksmbd/connection.h +0
-0#include "smb_common.h" #include "ksmbd_work.h" fs/ksmbd/vfs.c +1
-0@@ -1145,12 +1145,23 @@ static int __caseless_lookup(struct dir_context *ctx, const char *name, unsigned int d_type) {struct ksmbd_readdir_data *buf; +int cmp;cmp should be initialized with
-EINVALto fallbackstrncasecmp()?Please see below for the explanation.
Quoted 4 lines
Quoted 4 lines
fs/ksmbd/vfs.c +0
-0buf = container_of(ctx, struct ksmbd_readdir_data, ctx);Quoted 13 lines
fs/ksmbd/vfs.c static int __caseless_lookup(struct dir_context *ctx, const char *name, · line 1162
Quoted 12 lines
fs/ksmbd/vfs.c +9
-1if (buf->used != namlen)return 0 ; -if (!strncasecmp ((char *)buf->private, name, namlen)) { +if (IS_ENABLED (CONFIG_UNICODE ) && buf->um) { +const struct qstr q_buf = {.name = buf->private, + .len = buf->used}; +const struct qstr q_name = {.name = name, + .len = namlen}; + + cmp =utf8_strncasecmp (buf->um, &q_buf, &q_name); + } +if (!(IS_ENABLED (CONFIG_UNICODE ) && buf->um) || cmp <0 )I wonder why ->um is checked with
CONFIG_UNICODE.
Quoted 7 lines
If !(IS_ENABLED(CONFIG_UNICODE) &&
buf->um) is true, thenutf8_strncasecmp()
was not called. If !(IS_ENABLED(CONFIG_UNICODE) &&buf->um) is false, thenutf8_strncasecmp()was called and we check for an error withcmp < 0'. Alternatively,cmp' can be initialized to-EINVALand then !(IS_ENABLED(CONFIG_UNICODE) &&buf->um) can be removed.
The latter is preferred, right?
Yes:) The latter would be better.
Quoted 69 lines
Quoted 68 lines
Thanks.
Quoted 65 lines
fs/ksmbd/vfs.c +4
-1+ cmp =strncasecmp ((char *)buf->private, name, namlen); +if (!cmp) {memcpy ((char *)buf->private, name, namlen); buf->dirent_count =1 ;return -EEXIST ;@@ -1166,7 +1177,8 @@ static int __caseless_lookup(struct dir_context*ct> x, const char *name, * * Return: 0 on success, otherwise error */ -static int ksmbd_vfs_lookup_in_dir (const struct path *dir,char *name, size_t namelen) +static int ksmbd_vfs_lookup_in_dir (const struct path *dir,char *name, +size_t namelen,struct unicode_map *um){
fs/ksmbd/vfs.c +0
-0int ret;struct file *dfilp;@@ -1176,6 +1188,7 @@ static int ksmbd_vfs_lookup_in_dir(const structpat> h
*dir, char *name,size_tnafs/ksmbd/vfs.c +1
-0.private = name, .used = namelen, .dirent_count = 0, + .um = um, }; dfilp =dentry_open (dir, flags,current_cred ());@@ -1238,7 +1251,8 @@ int ksmbd_vfs_kern_path(struct ksmbd_work *work,ch> ar
*name,fs/ksmbd/vfs.c +2
-1break ; err =ksmbd_vfs_lookup_in_dir (&parent, filename, - filename_len); + filename_len, + work->conn->um);path_put (&parent);if (err) goto out;fs/ksmbd/vfs.h +2
-0@@ -12,6 +12,7 @@#include <linux/namei.h> #include <uapi/linux/xattr.h> #include <linux/posix_acl.h> +#include <linux/unicode.h> #include "smbacl.h" #include "xattr.h" @@ -60,6 +61,7 @@ struct ksmbd_readdir_data {unsigned int used;unsigned int dirent_count;unsigned int file_attr; +struct unicode_map *um; };/* ksmbd kstat wrapper to get valid create time when reading dir entry *> /
--
2.37.3