misc.c 8.1 KB

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