Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org> |
| 4 | * Copyright (C) 2018 Samsung Electronics Co., Ltd. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/kernel.h> |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 8 | #include <linux/xattr.h> |
| 9 | #include <linux/fs.h> |
Atte Heikkilä | 16b5f54 | 2022-09-15 22:49:11 +0900 | [diff] [blame] | 10 | #include <linux/unicode.h> |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 11 | |
| 12 | #include "misc.h" |
| 13 | #include "smb_common.h" |
| 14 | #include "connection.h" |
| 15 | #include "vfs.h" |
| 16 | |
| 17 | #include "mgmt/share_config.h" |
| 18 | |
| 19 | /** |
| 20 | * match_pattern() - compare a string with a pattern which might include |
| 21 | * wildcard '*' and '?' |
| 22 | * TODO : implement consideration about DOS_DOT, DOS_QM and DOS_STAR |
| 23 | * |
Yang Li | 7820c6e | 2022-05-12 15:56:05 +0800 | [diff] [blame] | 24 | * @str: string to compare with a pattern |
Namjae Jeon | b24c933 | 2021-03-21 17:32:19 +0900 | [diff] [blame] | 25 | * @len: string length |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 26 | * @pattern: pattern string which might include wildcard '*' and '?' |
| 27 | * |
| 28 | * Return: 0 if pattern matched with the string, otherwise non zero value |
| 29 | */ |
Namjae Jeon | b24c933 | 2021-03-21 17:32:19 +0900 | [diff] [blame] | 30 | int match_pattern(const char *str, size_t len, const char *pattern) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 31 | { |
| 32 | const char *s = str; |
| 33 | const char *p = pattern; |
| 34 | bool star = false; |
| 35 | |
Namjae Jeon | b24c933 | 2021-03-21 17:32:19 +0900 | [diff] [blame] | 36 | while (*s && len) { |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 37 | switch (*p) { |
| 38 | case '?': |
| 39 | s++; |
Namjae Jeon | b24c933 | 2021-03-21 17:32:19 +0900 | [diff] [blame] | 40 | len--; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 41 | p++; |
| 42 | break; |
| 43 | case '*': |
| 44 | star = true; |
| 45 | str = s; |
| 46 | if (!*++p) |
| 47 | return true; |
| 48 | pattern = p; |
| 49 | break; |
| 50 | default: |
| 51 | if (tolower(*s) == tolower(*p)) { |
| 52 | s++; |
Namjae Jeon | b24c933 | 2021-03-21 17:32:19 +0900 | [diff] [blame] | 53 | len--; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 54 | p++; |
| 55 | } else { |
| 56 | if (!star) |
| 57 | return false; |
| 58 | str++; |
| 59 | s = str; |
| 60 | p = pattern; |
| 61 | } |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (*p == '*') |
| 67 | ++p; |
| 68 | return !*p; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * is_char_allowed() - check for valid character |
| 73 | * @ch: input character to be checked |
| 74 | * |
| 75 | * Return: 1 if char is allowed, otherwise 0 |
| 76 | */ |
| 77 | static inline int is_char_allowed(char ch) |
| 78 | { |
| 79 | /* check for control chars, wildcards etc. */ |
| 80 | if (!(ch & 0x80) && |
Namjae Jeon | 64b39f4 | 2021-03-30 14:25:35 +0900 | [diff] [blame] | 81 | (ch <= 0x1f || |
| 82 | ch == '?' || ch == '"' || ch == '<' || |
| 83 | ch == '>' || ch == '|' || ch == '*')) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 84 | return 0; |
| 85 | |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | int ksmbd_validate_filename(char *filename) |
| 90 | { |
| 91 | while (*filename) { |
| 92 | char c = *filename; |
| 93 | |
| 94 | filename++; |
| 95 | if (!is_char_allowed(c)) { |
| 96 | ksmbd_debug(VFS, "File name validation failed: 0x%x\n", c); |
| 97 | return -ENOENT; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int ksmbd_validate_stream_name(char *stream_name) |
| 105 | { |
| 106 | while (*stream_name) { |
| 107 | char c = *stream_name; |
| 108 | |
| 109 | stream_name++; |
| 110 | if (c == '/' || c == ':' || c == '\\') { |
Namjae Jeon | bde1694 | 2021-06-28 15:23:19 +0900 | [diff] [blame] | 111 | pr_err("Stream name validation failed: %c\n", c); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 112 | return -ENOENT; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int parse_stream_name(char *filename, char **stream_name, int *s_type) |
| 120 | { |
| 121 | char *stream_type; |
| 122 | char *s_name; |
| 123 | int rc = 0; |
| 124 | |
| 125 | s_name = filename; |
| 126 | filename = strsep(&s_name, ":"); |
| 127 | ksmbd_debug(SMB, "filename : %s, streams : %s\n", filename, s_name); |
| 128 | if (strchr(s_name, ':')) { |
| 129 | stream_type = s_name; |
| 130 | s_name = strsep(&stream_type, ":"); |
| 131 | |
| 132 | rc = ksmbd_validate_stream_name(s_name); |
| 133 | if (rc < 0) { |
| 134 | rc = -ENOENT; |
| 135 | goto out; |
| 136 | } |
| 137 | |
| 138 | ksmbd_debug(SMB, "stream name : %s, stream type : %s\n", s_name, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 139 | stream_type); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 140 | if (!strncasecmp("$data", stream_type, 5)) |
| 141 | *s_type = DATA_STREAM; |
| 142 | else if (!strncasecmp("$index_allocation", stream_type, 17)) |
| 143 | *s_type = DIR_STREAM; |
| 144 | else |
| 145 | rc = -ENOENT; |
| 146 | } |
| 147 | |
| 148 | *stream_name = s_name; |
| 149 | out: |
| 150 | return rc; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * convert_to_nt_pathname() - extract and return windows path string |
| 155 | * whose share directory prefix was removed from file path |
Yang Li | 7820c6e | 2022-05-12 15:56:05 +0800 | [diff] [blame] | 156 | * @share: ksmbd_share_config pointer |
| 157 | * @path: path to report |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 158 | * |
| 159 | * Return : windows path string or error |
| 160 | */ |
| 161 | |
Namjae Jeon | 50f500b | 2022-02-24 11:03:41 +0900 | [diff] [blame] | 162 | char *convert_to_nt_pathname(struct ksmbd_share_config *share, |
Al Viro | c22180a | 2022-08-04 12:51:14 -0400 | [diff] [blame] | 163 | const struct path *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 164 | { |
Namjae Jeon | 50f500b | 2022-02-24 11:03:41 +0900 | [diff] [blame] | 165 | char *pathname, *ab_pathname, *nt_pathname; |
| 166 | int share_path_len = share->path_sz; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 167 | |
Namjae Jeon | 50f500b | 2022-02-24 11:03:41 +0900 | [diff] [blame] | 168 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); |
| 169 | if (!pathname) |
| 170 | return ERR_PTR(-EACCES); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 171 | |
Namjae Jeon | 50f500b | 2022-02-24 11:03:41 +0900 | [diff] [blame] | 172 | ab_pathname = d_path(path, pathname, PATH_MAX); |
| 173 | if (IS_ERR(ab_pathname)) { |
| 174 | nt_pathname = ERR_PTR(-EACCES); |
| 175 | goto free_pathname; |
| 176 | } |
Dan Carpenter | 87ffb31 | 2021-09-30 15:24:56 +0300 | [diff] [blame] | 177 | |
Namjae Jeon | 50f500b | 2022-02-24 11:03:41 +0900 | [diff] [blame] | 178 | if (strncmp(ab_pathname, share->path, share_path_len)) { |
| 179 | nt_pathname = ERR_PTR(-EACCES); |
| 180 | goto free_pathname; |
| 181 | } |
| 182 | |
| 183 | nt_pathname = kzalloc(strlen(&ab_pathname[share_path_len]) + 2, GFP_KERNEL); |
| 184 | if (!nt_pathname) { |
| 185 | nt_pathname = ERR_PTR(-ENOMEM); |
| 186 | goto free_pathname; |
| 187 | } |
| 188 | if (ab_pathname[share_path_len] == '\0') |
| 189 | strcpy(nt_pathname, "/"); |
| 190 | strcat(nt_pathname, &ab_pathname[share_path_len]); |
| 191 | |
| 192 | ksmbd_conv_path_to_windows(nt_pathname); |
| 193 | |
| 194 | free_pathname: |
| 195 | kfree(pathname); |
| 196 | return nt_pathname; |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | int get_nlink(struct kstat *st) |
| 200 | { |
| 201 | int nlink; |
| 202 | |
| 203 | nlink = st->nlink; |
| 204 | if (S_ISDIR(st->mode)) |
| 205 | nlink--; |
| 206 | |
| 207 | return nlink; |
| 208 | } |
| 209 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 210 | void ksmbd_conv_path_to_unix(char *path) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 211 | { |
| 212 | strreplace(path, '\\', '/'); |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 213 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 214 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 215 | void ksmbd_strip_last_slash(char *path) |
| 216 | { |
| 217 | int len = strlen(path); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 218 | |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 219 | while (len && path[len - 1] == '/') { |
| 220 | path[len - 1] = '\0'; |
| 221 | len--; |
| 222 | } |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void ksmbd_conv_path_to_windows(char *path) |
| 226 | { |
| 227 | strreplace(path, '/', '\\'); |
| 228 | } |
| 229 | |
Atte Heikkilä | f5ba1cd | 2022-10-03 02:09:34 +0300 | [diff] [blame] | 230 | char *ksmbd_casefold_sharename(struct unicode_map *um, const char *name) |
Atte Heikkilä | 16b5f54 | 2022-09-15 22:49:11 +0900 | [diff] [blame] | 231 | { |
| 232 | char *cf_name; |
| 233 | int cf_len; |
| 234 | |
| 235 | cf_name = kzalloc(KSMBD_REQ_MAX_SHARE_NAME, GFP_KERNEL); |
| 236 | if (!cf_name) |
| 237 | return ERR_PTR(-ENOMEM); |
| 238 | |
| 239 | if (IS_ENABLED(CONFIG_UNICODE) && um) { |
| 240 | const struct qstr q_name = {.name = name, .len = strlen(name)}; |
| 241 | |
| 242 | cf_len = utf8_casefold(um, &q_name, cf_name, |
| 243 | KSMBD_REQ_MAX_SHARE_NAME); |
| 244 | if (cf_len < 0) |
| 245 | goto out_ascii; |
| 246 | |
| 247 | return cf_name; |
| 248 | } |
| 249 | |
| 250 | out_ascii: |
| 251 | cf_len = strscpy(cf_name, name, KSMBD_REQ_MAX_SHARE_NAME); |
| 252 | if (cf_len < 0) { |
| 253 | kfree(cf_name); |
| 254 | return ERR_PTR(-E2BIG); |
| 255 | } |
| 256 | |
| 257 | for (; *cf_name; ++cf_name) |
| 258 | *cf_name = isascii(*cf_name) ? tolower(*cf_name) : *cf_name; |
| 259 | return cf_name - cf_len; |
| 260 | } |
| 261 | |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 262 | /** |
Stephen Rothwell | 36ba386 | 2021-03-17 17:01:15 +0900 | [diff] [blame] | 263 | * ksmbd_extract_sharename() - get share name from tree connect request |
Yang Li | a12bc36 | 2024-02-02 16:13:17 +0800 | [diff] [blame] | 264 | * @um: pointer to a unicode_map structure for character encoding handling |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 265 | * @treename: buffer containing tree name and share name |
| 266 | * |
| 267 | * Return: share name on success, otherwise error |
| 268 | */ |
Atte Heikkilä | 16b5f54 | 2022-09-15 22:49:11 +0900 | [diff] [blame] | 269 | char *ksmbd_extract_sharename(struct unicode_map *um, const char *treename) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 270 | { |
Atte Heikkilä | 16b5f54 | 2022-09-15 22:49:11 +0900 | [diff] [blame] | 271 | const char *name = treename, *pos = strrchr(name, '\\'); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 272 | |
| 273 | if (pos) |
| 274 | name = (pos + 1); |
| 275 | |
| 276 | /* caller has to free the memory */ |
Atte Heikkilä | f5ba1cd | 2022-10-03 02:09:34 +0300 | [diff] [blame] | 277 | return ksmbd_casefold_sharename(um, name); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
| 281 | * convert_to_unix_name() - convert windows name to unix format |
Yang Li | 7820c6e | 2022-05-12 15:56:05 +0800 | [diff] [blame] | 282 | * @share: ksmbd_share_config pointer |
| 283 | * @name: file name that is relative to share |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 284 | * |
| 285 | * Return: converted name on success, otherwise NULL |
| 286 | */ |
Hyunchul Lee | 265fd19 | 2021-09-25 00:06:16 +0900 | [diff] [blame] | 287 | char *convert_to_unix_name(struct ksmbd_share_config *share, const char *name) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 288 | { |
| 289 | int no_slash = 0, name_len, path_len; |
| 290 | char *new_name; |
| 291 | |
| 292 | if (name[0] == '/') |
| 293 | name++; |
| 294 | |
| 295 | path_len = share->path_sz; |
| 296 | name_len = strlen(name); |
| 297 | new_name = kmalloc(path_len + name_len + 2, GFP_KERNEL); |
| 298 | if (!new_name) |
| 299 | return new_name; |
| 300 | |
| 301 | memcpy(new_name, share->path, path_len); |
| 302 | if (new_name[path_len - 1] != '/') { |
| 303 | new_name[path_len] = '/'; |
| 304 | no_slash = 1; |
| 305 | } |
| 306 | |
| 307 | memcpy(new_name + path_len + no_slash, name, name_len); |
| 308 | path_len += name_len + no_slash; |
| 309 | new_name[path_len] = 0x00; |
| 310 | return new_name; |
| 311 | } |
| 312 | |
| 313 | char *ksmbd_convert_dir_info_name(struct ksmbd_dir_info *d_info, |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 314 | const struct nls_table *local_nls, |
| 315 | int *conv_len) |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 316 | { |
| 317 | char *conv; |
| 318 | int sz = min(4 * d_info->name_len, PATH_MAX); |
| 319 | |
| 320 | if (!sz) |
| 321 | return NULL; |
| 322 | |
| 323 | conv = kmalloc(sz, GFP_KERNEL); |
| 324 | if (!conv) |
| 325 | return NULL; |
| 326 | |
| 327 | /* XXX */ |
Namjae Jeon | 070fb21 | 2021-05-26 17:57:12 +0900 | [diff] [blame] | 328 | *conv_len = smbConvertToUTF16((__le16 *)conv, d_info->name, |
| 329 | d_info->name_len, local_nls, 0); |
Namjae Jeon | e2f3448 | 2021-03-16 10:49:09 +0900 | [diff] [blame] | 330 | *conv_len *= 2; |
| 331 | |
| 332 | /* We allocate buffer twice bigger than needed. */ |
| 333 | conv[*conv_len] = 0x00; |
| 334 | conv[*conv_len + 1] = 0x00; |
| 335 | return conv; |
| 336 | } |
Namjae Jeon | 5626518 | 2021-04-14 09:24:11 +0900 | [diff] [blame] | 337 | |
| 338 | /* |
| 339 | * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units) |
| 340 | * into Unix UTC (based 1970-01-01, in seconds). |
| 341 | */ |
| 342 | struct timespec64 ksmbd_NTtimeToUnix(__le64 ntutc) |
| 343 | { |
| 344 | struct timespec64 ts; |
| 345 | |
| 346 | /* Subtract the NTFS time offset, then convert to 1s intervals. */ |
| 347 | s64 t = le64_to_cpu(ntutc) - NTFS_TIME_OFFSET; |
| 348 | u64 abs_t; |
| 349 | |
| 350 | /* |
| 351 | * Unfortunately can not use normal 64 bit division on 32 bit arch, but |
| 352 | * the alternative, do_div, does not work with negative numbers so have |
| 353 | * to special case them |
| 354 | */ |
| 355 | if (t < 0) { |
| 356 | abs_t = -t; |
| 357 | ts.tv_nsec = do_div(abs_t, 10000000) * 100; |
| 358 | ts.tv_nsec = -ts.tv_nsec; |
| 359 | ts.tv_sec = -abs_t; |
| 360 | } else { |
| 361 | abs_t = t; |
| 362 | ts.tv_nsec = do_div(abs_t, 10000000) * 100; |
| 363 | ts.tv_sec = abs_t; |
| 364 | } |
| 365 | |
| 366 | return ts; |
| 367 | } |
| 368 | |
| 369 | /* Convert the Unix UTC into NT UTC. */ |
| 370 | inline u64 ksmbd_UnixTimeToNT(struct timespec64 t) |
| 371 | { |
| 372 | /* Convert to 100ns intervals and then add the NTFS time offset. */ |
| 373 | return (u64)t.tv_sec * 10000000 + t.tv_nsec / 100 + NTFS_TIME_OFFSET; |
| 374 | } |
| 375 | |
| 376 | inline long long ksmbd_systime(void) |
| 377 | { |
| 378 | struct timespec64 ts; |
| 379 | |
| 380 | ktime_get_real_ts64(&ts); |
| 381 | return ksmbd_UnixTimeToNT(ts); |
| 382 | } |