projects/linux·blob master · refs

363 lines · 7.6 KiB

1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2016 Namjae Jeon <[email protected]>
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
5 */
6
7#include <linux/kernel.h>
8#include <linux/xattr.h>
9#include <linux/fs.h>
10#include <linux/unicode.h>
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 *
24 * @str: string to compare with a pattern
25 * @len: string length
26 * @pattern: pattern string which might include wildcard '*' and '?'
27 *
28 * Return: 0 if pattern matched with the string, otherwise non zero value
29 */
30int match_pattern(const char *str, size_t len, const char *pattern)
31{
32 const char *s = str;
33 const char *p = pattern;
34 bool star = false;
35
36 while (*s && len) {
37 switch (*p) {
38 case '?':
39 s++;
40 len--;
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++;
53 len--;
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 */
77static inline int is_char_allowed(char ch)
78{
79 /* check for control chars, wildcards etc. */
80 if (!(ch & 0x80) &&
81 (ch <= 0x1f ||
82 ch == '?' || ch == '"' || ch == '<' ||
83 ch == '>' || ch == '|' || ch == '*'))
84 return 0;
85
86 return 1;
87}
88
89int 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
104static 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 == '\\') {
111 pr_err("Stream name validation failed: %c\n", c);
112 return -ENOENT;
113 }
114 }
115
116 return 0;
117}
118
119int 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 bool has_stream_type = false;
125
126 *stream_name = NULL;
127 s_name = filename;
128 filename = strsep(&s_name, ":");
129 ksmbd_debug(SMB, "filename : %s, streams : %s\n", filename, s_name);
130 if (strchr(s_name, ':')) {
131 stream_type = s_name;
132 s_name = strsep(&stream_type, ":");
133
134 rc = ksmbd_validate_stream_name(s_name);
135 if (rc < 0) {
136 rc = -ENOENT;
137 goto out;
138 }
139
140 ksmbd_debug(SMB, "stream name : %s, stream type : %s\n", s_name,
141 stream_type);
142 if (!strncasecmp("$data", stream_type, 5)) {
143 *s_type = DATA_STREAM;
144 has_stream_type = true;
145 } else if (!strncasecmp("$index_allocation", stream_type, 17)) {
146 *s_type = DIR_STREAM;
147 has_stream_type = true;
148 } else {
149 rc = -ENOENT;
150 }
151 }
152
153 if (has_stream_type && !s_name[0] && *s_type == DATA_STREAM)
154 goto out;
155
156 *stream_name = s_name;
157out:
158 return rc;
159}
160
161/**
162 * convert_to_nt_pathname() - extract and return windows path string
163 * whose share directory prefix was removed from file path
164 * @share: ksmbd_share_config pointer
165 * @path: path to report
166 *
167 * Return : windows path string or error
168 */
169
170char *convert_to_nt_pathname(struct ksmbd_share_config *share,
171 const struct path *path)
172{
173 char *pathname, *ab_pathname, *nt_pathname;
174 int share_path_len = share->path_sz;
175 size_t ab_pathname_len;
176 int prefix;
177
178 pathname = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP);
179 if (!pathname)
180 return ERR_PTR(-EACCES);
181
182 ab_pathname = d_path(path, pathname, PATH_MAX);
183 if (IS_ERR(ab_pathname)) {
184 nt_pathname = ERR_PTR(-EACCES);
185 goto free_pathname;
186 }
187
188 if (strncmp(ab_pathname, share->path, share_path_len)) {
189 nt_pathname = ERR_PTR(-EACCES);
190 goto free_pathname;
191 }
192
193 ab_pathname_len = strlen(&ab_pathname[share_path_len]);
194 prefix = ab_pathname[share_path_len] == '\0' ? 1 : 0;
195 nt_pathname = kmalloc(prefix + ab_pathname_len + 1, KSMBD_DEFAULT_GFP);
196 if (!nt_pathname) {
197 nt_pathname = ERR_PTR(-ENOMEM);
198 goto free_pathname;
199 }
200
201 if (prefix)
202 *nt_pathname = '/';
203 memcpy(nt_pathname + prefix, &ab_pathname[share_path_len],
204 ab_pathname_len + 1);
205
206 ksmbd_conv_path_to_windows(nt_pathname);
207
208free_pathname:
209 kfree(pathname);
210 return nt_pathname;
211}
212
213int get_nlink(struct kstat *st)
214{
215 int nlink;
216
217 nlink = st->nlink;
218 if (S_ISDIR(st->mode))
219 nlink--;
220
221 return nlink;
222}
223
224void ksmbd_conv_path_to_unix(char *path)
225{
226 strreplace(path, '\\', '/');
227}
228
229void ksmbd_strip_last_slash(char *path)
230{
231 int len = strlen(path);
232
233 while (len && path[len - 1] == '/') {
234 path[len - 1] = '\0';
235 len--;
236 }
237}
238
239void ksmbd_conv_path_to_windows(char *path)
240{
241 strreplace(path, '/', '\\');
242}
243
244char *ksmbd_casefold_sharename(struct unicode_map *um, const char *name)
245{
246 char *cf_name;
247 int cf_len;
248
249 cf_name = kzalloc(KSMBD_REQ_MAX_SHARE_NAME, KSMBD_DEFAULT_GFP);
250 if (!cf_name)
251 return ERR_PTR(-ENOMEM);
252
253 if (IS_ENABLED(CONFIG_UNICODE) && um) {
254 const struct qstr q_name = {.name = name, .len = strlen(name)};
255
256 cf_len = utf8_casefold(um, &q_name, cf_name,
257 KSMBD_REQ_MAX_SHARE_NAME);
258 if (cf_len < 0)
259 goto out_ascii;
260
261 return cf_name;
262 }
263
264out_ascii:
265 cf_len = strscpy(cf_name, name, KSMBD_REQ_MAX_SHARE_NAME);
266 if (cf_len < 0) {
267 kfree(cf_name);
268 return ERR_PTR(-E2BIG);
269 }
270
271 for (; *cf_name; ++cf_name)
272 *cf_name = isascii(*cf_name) ? tolower(*cf_name) : *cf_name;
273 return cf_name - cf_len;
274}
275
276/**
277 * ksmbd_extract_sharename() - get share name from tree connect request
278 * @um: pointer to a unicode_map structure for character encoding handling
279 * @treename: buffer containing tree name and share name
280 *
281 * Return: share name on success, otherwise error
282 */
283char *ksmbd_extract_sharename(struct unicode_map *um, const char *treename)
284{
285 const char *name = treename, *pos = strrchr(name, '\\');
286
287 if (pos)
288 name = (pos + 1);
289
290 /* caller has to free the memory */
291 return ksmbd_casefold_sharename(um, name);
292}
293
294char *ksmbd_convert_dir_info_name(struct ksmbd_dir_info *d_info,
295 const struct nls_table *local_nls,
296 int *conv_len)
297{
298 char *conv;
299 int sz = min(4 * d_info->name_len, PATH_MAX);
300
301 if (!sz)
302 return NULL;
303
304 conv = kmalloc(sz, KSMBD_DEFAULT_GFP);
305 if (!conv)
306 return NULL;
307
308 /* XXX */
309 *conv_len = smbConvertToUTF16((__le16 *)conv, d_info->name,
310 d_info->name_len, local_nls, 0);
311 *conv_len *= 2;
312
313 /* We allocate buffer twice bigger than needed. */
314 conv[*conv_len] = 0x00;
315 conv[*conv_len + 1] = 0x00;
316 return conv;
317}
318
319/*
320 * Convert the NT UTC (based 1601-01-01, in hundred nanosecond units)
321 * into Unix UTC (based 1970-01-01, in seconds).
322 */
323struct timespec64 ksmbd_NTtimeToUnix(__le64 ntutc)
324{
325 struct timespec64 ts;
326
327 /* Subtract the NTFS time offset, then convert to 1s intervals. */
328 s64 t = le64_to_cpu(ntutc) - NTFS_TIME_OFFSET;
329 u64 abs_t;
330
331 /*
332 * Unfortunately can not use normal 64 bit division on 32 bit arch, but
333 * the alternative, do_div, does not work with negative numbers so have
334 * to special case them
335 */
336 if (t < 0) {
337 abs_t = -t;
338 ts.tv_nsec = do_div(abs_t, 10000000) * 100;
339 ts.tv_nsec = -ts.tv_nsec;
340 ts.tv_sec = -abs_t;
341 } else {
342 abs_t = t;
343 ts.tv_nsec = do_div(abs_t, 10000000) * 100;
344 ts.tv_sec = abs_t;
345 }
346
347 return ts;
348}
349
350/* Convert the Unix UTC into NT UTC. */
351inline u64 ksmbd_UnixTimeToNT(struct timespec64 t)
352{
353 /* Convert to 100ns intervals and then add the NTFS time offset. */
354 return (u64)t.tv_sec * 10000000 + t.tv_nsec / 100 + NTFS_TIME_OFFSET;
355}
356
357inline long long ksmbd_systime(void)
358{
359 struct timespec64 ts;
360
361 ktime_get_real_ts64(&ts);
362 return ksmbd_UnixTimeToNT(ts);
363}