ksmbd: casefold utf-8 share names and fix ascii lowercase conversion
Subsystems (3)
PATCH 1/2 ksmbd: casefold utf-8 share names and fix ascii lowercase conversion
strtolower() corrupts all UTF-8 share names that have a byte in the C0 (À ISO8859-1) to DE (Þ ISO8859-1) range, since the non-ASCII part of ISO8859-1 is incompatible with UTF-8. Prevent this by checking that a byte is in the ASCII range with isascii(), before the conversion to lowercase with tolower(). Properly handle case-insensitivity of UTF-8 share names by casefolding them, but fallback to ASCII lowercase conversion on failure or if CONFIG_UNICODE is not set. Refactor to move the share name casefolding immediately after the share name extraction. Also, make the associated constness corrections.
--- fs/ksmbd/connection.c | 8 +++++++ fs/ksmbd/connection.h | 1 + fs/ksmbd/mgmt/share_config.c | 18 ++++----------- fs/ksmbd/mgmt/share_config.h | 2 +- fs/ksmbd/mgmt/tree_connect.c | 2 +- fs/ksmbd/mgmt/tree_connect.h | 2 +- fs/ksmbd/misc.c | 45 +++++++++++++++++++++++++++++------- fs/ksmbd/misc.h | 2 +- fs/ksmbd/smb2pdu.c | 2 +- fs/ksmbd/unicode.h | 3 ++- 10 files changed, 57 insertions(+), 28 deletions(-)
fs/ksmbd/connection.c +8 -0
@@ -60,6 +60,12 @@ struct ksmbd_conn *ksmbd_conn_alloc(void) conn->local_nls =load_nls ("utf8" );if (!conn->local_nls) conn->local_nls =load_nls_default (); +if (IS_ENABLED (CONFIG_UNICODE )) + conn->um =utf8_load (UNICODE_AGE (12 ,1 ,0 )); +else + conn->um =ERR_PTR (-EOPNOTSUPP ); +if (IS_ERR (conn->um)) + conn->um =NULL ;atomic_set (&conn->req_running,0 );atomic_set (&conn->r_count,0 ); conn->total_credits =1 ;
@@ -350,6 +356,8 @@ int ksmbd_conn_handler_loop(void *p)wait_event (conn->r_count_q,atomic_read (&conn->r_count) ==0 ); +if (IS_ENABLED (CONFIG_UNICODE )) +utf8_unload (conn->um);unload_nls (conn->local_nls);if (default_conn_ops.terminate_fn) default_conn_ops.terminate_fn (conn);
fs/ksmbd/connection.h +1 -0
@@ -46,6 +46,7 @@ struct ksmbd_conn {char *request_buf;struct ksmbd_transport *transport;struct nls_table *local_nls; +struct unicode_map *um;struct list_head conns_list;/* smb session 1 per user */ struct xarray sessions;
fs/ksmbd/mgmt/share_config.c +4 -14
@@ -26,7 +26,7 @@ struct ksmbd_veto_pattern {struct list_head list; }; -static unsigned int share_name_hash (char *name) +static unsigned int share_name_hash (const char *name) {return jhash (name,strlen (name),0 ); }
@@ -72,7 +72,7 @@ __get_share_config(struct ksmbd_share_config *share)return share; } -static struct ksmbd_share_config *__share_lookup (char *name) +static struct ksmbd_share_config *__share_lookup (const char *name) {struct ksmbd_share_config *share;unsigned int key =share_name_hash (name);
@@ -119,7 +119,7 @@ static int parse_veto_list(struct ksmbd_share_config *share,return 0 ; } -static struct ksmbd_share_config *share_config_request (char *name) +static struct ksmbd_share_config *share_config_request (const char *name) {struct ksmbd_share_config_response *resp;struct ksmbd_share_config *share =NULL ;
@@ -190,20 +190,10 @@ static struct ksmbd_share_config *share_config_request(char *name)return share; } -static void strtolower (char *share_name) -{ -while (*share_name) { - *share_name =tolower (*share_name); - share_name++; - } -} - -struct ksmbd_share_config *ksmbd_share_config_get (char *name) +struct ksmbd_share_config *ksmbd_share_config_get (const char *name) {struct ksmbd_share_config *share; -strtolower (name); -down_read (&shares_table_lock); share =__share_lookup (name);if (share)
fs/ksmbd/mgmt/share_config.h +1 -1
@@ -74,7 +74,7 @@ static inline void ksmbd_share_config_put(struct ksmbd_share_config *share) __ksmbd_share_config_put(share); } -struct ksmbd_share_config *ksmbd_share_config_get (char *name); +struct ksmbd_share_config *ksmbd_share_config_get (const char *name);bool ksmbd_share_veto_filename (struct ksmbd_share_config *share,const char *filename);#endif /* __SHARE_CONFIG_MANAGEMENT_H__ */
fs/ksmbd/mgmt/tree_connect.c +1 -1
@@ -17,7 +17,7 @@struct ksmbd_tree_conn_status ksmbd_tree_conn_connect (struct ksmbd_conn *conn,struct ksmbd_session *sess, -char *share_name) +const char *share_name) {struct ksmbd_tree_conn_status status = {-ENOENT ,NULL };struct ksmbd_tree_connect_response *resp =NULL ;
fs/ksmbd/mgmt/tree_connect.h +1 -1
@@ -42,7 +42,7 @@ struct ksmbd_session;struct ksmbd_tree_conn_status ksmbd_tree_conn_connect (struct ksmbd_conn *conn,struct ksmbd_session *sess, -char *share_name); +const char *share_name);int ksmbd_tree_conn_disconnect (struct ksmbd_session *sess,struct ksmbd_tree_connect *tree_conn);
fs/ksmbd/misc.c +37 -8
69 lines not on this page · raw message
fs/ksmbd/misc.h +1 -1
@@ -20,7 +20,7 @@ int get_nlink(struct kstat *st);void ksmbd_conv_path_to_unix (char *path);void ksmbd_strip_last_slash (char *path);void ksmbd_conv_path_to_windows (char *path); -char *ksmbd_extract_sharename (char *treename); +char *ksmbd_extract_sharename (struct unicode_map *um,const char *treename);char *convert_to_unix_name (struct ksmbd_share_config *share,const char *name);#define KSMBD_DIR_INFO_ALIGNMENT 8
fs/ksmbd/smb2pdu.c +1 -1
@@ -1883,7 +1883,7 @@ int smb2_tree_connect(struct ksmbd_work *work) goto out_err1; } - name =ksmbd_extract_sharename (treename); + name =ksmbd_extract_sharename (conn->um, treename);if (IS_ERR (name)) { status.ret =KSMBD_TREE_CONN_STATUS_ERROR ; goto out_err1;
fs/ksmbd/unicode.h +2 -1
@@ -24,6 +24,7 @@#include <asm/byteorder.h> #include <linux/types.h> #include <linux/nls.h> +#include <linux/unicode.h> #define UNIUPR_NOLOWER /* Example to not expand lower case tables */
@@ -69,7 +70,7 @@ char *smb_strndup_from_utf16(const char *src, const int maxlen, const struct nls_table *codepage);int smbConvertToUTF16 (__le16 *target,const char *source,int srclen,const struct nls_table *cp,int mapchars); -char *ksmbd_extract_sharename (char *treename); +char *ksmbd_extract_sharename (struct unicode_map *um,const char *treename);#endif /*
Signature · 1 line
2.37.3
PATCH 2/2 ksmbd-tools: casefold utf-8 share names
Match kernel space behavior regarding case-insensitivity of UTF-8 share names by first NFD(I) normalizing and then casefolding them. Fallback to ASCII lowercase conversion on failure.
--- addshare/addshare.c | 6 +++--- include/management/share.h | 1 + lib/config_parser.c | 2 +- lib/management/share.c | 19 +++++++++++++++++++ mountd/rpc_srvsvc.c | 2 +- 5 files changed, 25 insertions(+), 5 deletions(-)
addshare/addshare.c +3 -3
@@ -123,17 +123,17 @@ int main(int argc, char *argv[])switch (c) {case 'a' :g_free (share); - share =g_ascii_strdown (optarg,strlen (optarg)); + share =shm_casefold_share_name (optarg,strlen (optarg)); command = command_add_share;break ;case 'd' :g_free (share); - share =g_ascii_strdown (optarg,strlen (optarg)); + share =shm_casefold_share_name (optarg,strlen (optarg)); command = command_del_share;break ;case 'u' :g_free (share); - share =g_ascii_strdown (optarg,strlen (optarg)); + share =shm_casefold_share_name (optarg,strlen (optarg)); command = command_update_share;break ;case 'o' :
include/management/share.h +1 -0
@@ -141,6 +141,7 @@ static inline int test_share_flag(struct ksmbd_share *share, int flag)struct ksmbd_share *get_ksmbd_share (struct ksmbd_share *share);void put_ksmbd_share (struct ksmbd_share *share); +char *shm_casefold_share_name (char *name,size_t len);struct ksmbd_share *shm_lookup_share (char *name);struct smbconf_group ;
lib/config_parser.c +1 -1
@@ -93,7 +93,7 @@ static int add_new_group(char *line)while (*end && *end !=']' ) end =g_utf8_find_next_char (end,NULL ); - name =g_ascii_strdown (begin +1 , end - begin -1 ); + name =shm_casefold_share_name (begin +1 , end - begin -1 );if (!name) goto out_free;
lib/management/share.c +19 -0
@@ -236,6 +236,25 @@ int shm_init(void)return 0 ; } +char *shm_casefold_share_name (char *name,size_t len) +{ +char *nfdi_name, *nfdicf_name; + + nfdi_name =g_utf8_normalize (name, len,G_NORMALIZE_NFD ); +if (!nfdi_name) + goto out_ascii; + + nfdicf_name =g_utf8_casefold (nfdi_name,strlen (nfdi_name)); +if (!nfdicf_name) + goto out_ascii; + +g_free (nfdi_name); +return nfdicf_name; +out_ascii: +g_free (nfdi_name); +return g_ascii_strdown (name, len); +} +static struct ksmbd_share *__shm_lookup_share (char *name) {return g_hash_table_lookup (shares_table, name);
mountd/rpc_srvsvc.c +1 -1
@@ -171,7 +171,7 @@ static int srvsvc_share_get_info_invoke(struct ksmbd_rpc_pipe *pipe,int ret;gchar *share_name; - share_name =g_ascii_strdown (STR_VAL (hdr->share_name), + share_name =shm_casefold_share_name (STR_VAL (hdr->share_name),strlen (STR_VAL (hdr->share_name))); share =shm_lookup_share (share_name);if (!share)
Signature · 1 line
2.37.3
Namjae Jeon
maintainer2022-09-12 5:57 GMT+09:00, Atte Heikkilä <[email protected]>:
Hi Atte,
[snip]
Quoted 4 lines
fs/ksmbd/misc.c +4
-0+static char *casefold_sharename (struct unicode_map *um,const char *name) +{ +char *cf_name; +int cf_len;
fs/ksmbd/misc.c void ksmbd_conv_path_to_windows(char *path) · line 243
Quoted 10 lines
fs/ksmbd/misc.c +10
-0+ + cf_name =kzalloc (KSMBD_REQ_MAX_SHARE_NAME ,GFP_KERNEL ); +if (!cf_name) +return ERR_PTR (-ENOMEM ); + +if (IS_ENABLED (CONFIG_UNICODE )) { +const struct qstr q_name = {.name = name, .len =strlen (name)}; + +if (!um) + goto out_ascii;
Minor nit, Wouldn't it be simpler to change something like the one below?
+ if (IS_ENABLED(CONFIG_UNICODE) && um) {
Thanks!
Quoted 19 lines
fs/ksmbd/misc.c +19
-0+ + cf_len =utf8_casefold (um, &q_name, cf_name, +KSMBD_REQ_MAX_SHARE_NAME ); +if (cf_len <0 ) + goto out_ascii; + +return cf_name; + } + +out_ascii: + cf_len =strscpy (cf_name, name,KSMBD_REQ_MAX_SHARE_NAME ); +if (cf_len <0 ) +return ERR_PTR (-E2BIG ); + +for (; *cf_name; ++cf_name) + *cf_name =isascii (*cf_name) ?tolower (*cf_name) : *cf_name; +return cf_name - cf_len; +} +
Re: [PATCH 1/2] ksmbd: casefold utf-8 share names and fix ascii
Quoted 20 lines · Namjae Jeon
2022-09-12 5:57 GMT+09:00, Atte Heikkilä <[email protected]>:
Hi Atte,[snip]
Quoted 4 lines
fs/ksmbd/misc.c +4
-0+static char *casefold_sharename(struct unicode_map *um, const char *name>) +{ +char *cf_name; +int cf_len;fs/ksmbd/misc.c void ksmbd_conv_path_to_windows(char *path) · line 243
Quoted 10 lines
fs/ksmbd/misc.c +10
-0+ + cf_name =kzalloc (KSMBD_REQ_MAX_SHARE_NAME ,GFP_KERNEL ); +if (!cf_name) +return ERR_PTR (-ENOMEM ); + +if (IS_ENABLED (CONFIG_UNICODE )) { +const struct qstr q_name = {.name = name, .len =strlen (name)}; + +if (!um) + goto out_ascii;Minor nit, Wouldn't it be simpler to change something like the one below?
Review
Quoted 1 line
(quoted diff) +1
-0+ if (IS_ENABLED(CONFIG_UNICODE) && um) {
This mailing list already has a v2 patch series. Please, reply to that one. As for your suggestion, I thought to keep the statements separate since the block with the IS_ENABLED() macro is optimized away when CONFIG_UNICODE is not set. I understand that the behavior is the same with your suggestion.
Thank you.
Quoted 21 lines
Thanks!
Quoted 19 lines
fs/ksmbd/misc.c +19
-0+ + cf_len =utf8_casefold (um, &q_name, cf_name, +KSMBD_REQ_MAX_SHARE_NAME ); +if (cf_len <0 ) + goto out_ascii; + +return cf_name; + } + +out_ascii: + cf_len =strscpy (cf_name, name,KSMBD_REQ_MAX_SHARE_NAME ); +if (cf_len <0 ) +return ERR_PTR (-E2BIG ); + +for (; *cf_name; ++cf_name) + *cf_name =isascii (*cf_name) ?tolower (*cf_name) : *cf_name; +return cf_name - cf_len; +} +
Namjae Jeon
maintainerRe: [PATCH 1/2] ksmbd: casefold utf-8 share names and fix ascii
2022-09-13 1:14 GMT+09:00, Atte Heikkilä <[email protected]>:
Quoted 22 lines · Namjae Jeon
Quoted 21 lines
2022-09-12 5:57 GMT+09:00, Atte Heikkilä <[email protected]>:
Hi Atte,[snip]
Quoted 5 lines
+static char *casefold_sharename(struct
unicode_map*um, const char *name>)fs/ksmbd/misc.c +3
-0+{ +char *cf_name; +int cf_len;fs/ksmbd/misc.c void ksmbd_conv_path_to_windows(char *path) · line 243
Quoted 10 lines
fs/ksmbd/misc.c +10
-0+ + cf_name =kzalloc (KSMBD_REQ_MAX_SHARE_NAME ,GFP_KERNEL ); +if (!cf_name) +return ERR_PTR (-ENOMEM ); + +if (IS_ENABLED (CONFIG_UNICODE )) { +const struct qstr q_name = {.name = name, .len =strlen (name)}; + +if (!um) + goto out_ascii;Minor nit, Wouldn't it be simpler to change something like the one below?
Quoted 3 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ if (IS_ENABLED(CONFIG_UNICODE) && um) {This mailing list already has a v2 patch series. Please, reply to that one.
Okay, but please add cc me when you send the patch to the list.
Quoted 3 lines
As for your suggestion, I thought to keep the statements separate since the block with the
IS_ENABLED()macro is optimized away whenCONFIG_UNICODEis not set. I understand that the behavior is the same with your suggestion.
When CONFIG_UNICODE is not set, um is not checked in my suggestion. Please tell me why my suggestion is worse. if you are okay, I will update it directly.(i.e. no need to send v3 patch). and please check the use of strncasecmp() in __caseless_lookup() also.
And I need to do full test for this patches, I think it will take about two days.
Quoted 25 lines
Thank you.
Quoted 21 lines
Thanks!
Quoted 19 lines
fs/ksmbd/misc.c +19
-0+ + cf_len =utf8_casefold (um, &q_name, cf_name, +KSMBD_REQ_MAX_SHARE_NAME ); +if (cf_len <0 ) + goto out_ascii; + +return cf_name; + } + +out_ascii: + cf_len =strscpy (cf_name, name,KSMBD_REQ_MAX_SHARE_NAME ); +if (cf_len <0 ) +return ERR_PTR (-E2BIG ); + +for (; *cf_name; ++cf_name) + *cf_name =isascii (*cf_name) ?tolower (*cf_name) : *cf_name; +return cf_name - cf_len; +} +
Quoted 4 lines · Namjae Jeon
2022-09-13 1:14 GMT+09:00, Atte Heikkilä <[email protected]>:
Quoted 2 lines
This mailing list already has a v2 patch series. Please, reply to that one.
Okay, but please add cc me when you send the patch to the list.
I sent the v2 patch to the mailing list an hour after the first one:
https://lore.kernel.org/linux-cifs/[email protected]/
Please, let's have further discussion as replies to the v2 patch series and not this one.
Quoted 5 lines
Quoted 4 lines
As for your suggestion, I thought to keep the statements separate since the block with the
IS_ENABLED()macro is optimized away whenCONFIG_UNICODEis not set. I understand that the behavior is the same with your suggestion.When
CONFIG_UNICODEis not set, um is not checked in my suggestion.
I didn't mean to imply that it was.
Quoted 2 lines
Please tell me why my suggestion is worse. if you are okay, I will update it directly.(i.e. no need to send v3 patch).
I didn't mean that your suggestion is worse. Feel free to update it directly if you'd like.
Quoted 1 line
and please check the use of
strncasecmp()in__caseless_lookup()also.
I thought that the change to utf8_strncasecmp() would be out of place for this patch series.
Quoted 3 lines
And I need to do full test for this patches, I think it will take about two days.
Please do not feel inclined to hurry, considering the mistake I managed to make in the the v1 patch series (this one you're replying to).
Quoted 3 lines
Quoted 3 lines
Thank you.