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