xref: /openbmc/linux/fs/smb/client/fs_context.c (revision 36db6e8484ed455bbb320d89a119378897ae991c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2020, Microsoft Corporation.
4  *
5  *   Author(s): Steve French <stfrench@microsoft.com>
6  *              David Howells <dhowells@redhat.com>
7  */
8 
9 /*
10 #include <linux/module.h>
11 #include <linux/nsproxy.h>
12 #include <linux/slab.h>
13 #include <linux/magic.h>
14 #include <linux/security.h>
15 #include <net/net_namespace.h>
16 #ifdef CONFIG_CIFS_DFS_UPCALL
17 #include "dfs_cache.h"
18 #endif
19 */
20 
21 #include <linux/ctype.h>
22 #include <linux/fs_context.h>
23 #include <linux/fs_parser.h>
24 #include <linux/fs.h>
25 #include <linux/mount.h>
26 #include <linux/parser.h>
27 #include <linux/utsname.h>
28 #include "cifsfs.h"
29 #include "cifspdu.h"
30 #include "cifsglob.h"
31 #include "cifsproto.h"
32 #include "cifs_unicode.h"
33 #include "cifs_debug.h"
34 #include "cifs_fs_sb.h"
35 #include "ntlmssp.h"
36 #include "nterr.h"
37 #include "rfc1002pdu.h"
38 #include "fs_context.h"
39 
40 DEFINE_MUTEX(cifs_mount_mutex);
41 
42 static const match_table_t cifs_smb_version_tokens = {
43 	{ Smb_1, SMB1_VERSION_STRING },
44 	{ Smb_20, SMB20_VERSION_STRING},
45 	{ Smb_21, SMB21_VERSION_STRING },
46 	{ Smb_30, SMB30_VERSION_STRING },
47 	{ Smb_302, SMB302_VERSION_STRING },
48 	{ Smb_302, ALT_SMB302_VERSION_STRING },
49 	{ Smb_311, SMB311_VERSION_STRING },
50 	{ Smb_311, ALT_SMB311_VERSION_STRING },
51 	{ Smb_3any, SMB3ANY_VERSION_STRING },
52 	{ Smb_default, SMBDEFAULT_VERSION_STRING },
53 	{ Smb_version_err, NULL }
54 };
55 
56 static const match_table_t cifs_secflavor_tokens = {
57 	{ Opt_sec_krb5, "krb5" },
58 	{ Opt_sec_krb5i, "krb5i" },
59 	{ Opt_sec_krb5p, "krb5p" },
60 	{ Opt_sec_ntlmsspi, "ntlmsspi" },
61 	{ Opt_sec_ntlmssp, "ntlmssp" },
62 	{ Opt_sec_ntlmv2, "nontlm" },
63 	{ Opt_sec_ntlmv2, "ntlmv2" },
64 	{ Opt_sec_ntlmv2i, "ntlmv2i" },
65 	{ Opt_sec_none, "none" },
66 
67 	{ Opt_sec_err, NULL }
68 };
69 
70 const struct fs_parameter_spec smb3_fs_parameters[] = {
71 	/* Mount options that take no arguments */
72 	fsparam_flag_no("user_xattr", Opt_user_xattr),
73 	fsparam_flag_no("forceuid", Opt_forceuid),
74 	fsparam_flag_no("multichannel", Opt_multichannel),
75 	fsparam_flag_no("forcegid", Opt_forcegid),
76 	fsparam_flag("noblocksend", Opt_noblocksend),
77 	fsparam_flag("noautotune", Opt_noautotune),
78 	fsparam_flag("nolease", Opt_nolease),
79 	fsparam_flag_no("hard", Opt_hard),
80 	fsparam_flag_no("soft", Opt_soft),
81 	fsparam_flag_no("perm", Opt_perm),
82 	fsparam_flag("nodelete", Opt_nodelete),
83 	fsparam_flag_no("mapposix", Opt_mapposix),
84 	fsparam_flag("mapchars", Opt_mapchars),
85 	fsparam_flag("nomapchars", Opt_nomapchars),
86 	fsparam_flag_no("sfu", Opt_sfu),
87 	fsparam_flag("nodfs", Opt_nodfs),
88 	fsparam_flag_no("posixpaths", Opt_posixpaths),
89 	fsparam_flag_no("unix", Opt_unix),
90 	fsparam_flag_no("linux", Opt_unix),
91 	fsparam_flag_no("posix", Opt_unix),
92 	fsparam_flag("nocase", Opt_nocase),
93 	fsparam_flag("ignorecase", Opt_nocase),
94 	fsparam_flag_no("brl", Opt_brl),
95 	fsparam_flag_no("handlecache", Opt_handlecache),
96 	fsparam_flag("forcemandatorylock", Opt_forcemandatorylock),
97 	fsparam_flag("forcemand", Opt_forcemandatorylock),
98 	fsparam_flag("setuidfromacl", Opt_setuidfromacl),
99 	fsparam_flag("idsfromsid", Opt_setuidfromacl),
100 	fsparam_flag_no("setuids", Opt_setuids),
101 	fsparam_flag_no("dynperm", Opt_dynperm),
102 	fsparam_flag_no("intr", Opt_intr),
103 	fsparam_flag_no("strictsync", Opt_strictsync),
104 	fsparam_flag_no("serverino", Opt_serverino),
105 	fsparam_flag("rwpidforward", Opt_rwpidforward),
106 	fsparam_flag("cifsacl", Opt_cifsacl),
107 	fsparam_flag_no("acl", Opt_acl),
108 	fsparam_flag("locallease", Opt_locallease),
109 	fsparam_flag("sign", Opt_sign),
110 	fsparam_flag("ignore_signature", Opt_ignore_signature),
111 	fsparam_flag("signloosely", Opt_ignore_signature),
112 	fsparam_flag("seal", Opt_seal),
113 	fsparam_flag("noac", Opt_noac),
114 	fsparam_flag("fsc", Opt_fsc),
115 	fsparam_flag("mfsymlinks", Opt_mfsymlinks),
116 	fsparam_flag("multiuser", Opt_multiuser),
117 	fsparam_flag("sloppy", Opt_sloppy),
118 	fsparam_flag("nosharesock", Opt_nosharesock),
119 	fsparam_flag_no("persistenthandles", Opt_persistent),
120 	fsparam_flag_no("resilienthandles", Opt_resilient),
121 	fsparam_flag_no("tcpnodelay", Opt_tcp_nodelay),
122 	fsparam_flag("nosparse", Opt_nosparse),
123 	fsparam_flag("domainauto", Opt_domainauto),
124 	fsparam_flag("rdma", Opt_rdma),
125 	fsparam_flag("modesid", Opt_modesid),
126 	fsparam_flag("modefromsid", Opt_modesid),
127 	fsparam_flag("rootfs", Opt_rootfs),
128 	fsparam_flag("compress", Opt_compress),
129 	fsparam_flag("witness", Opt_witness),
130 
131 	/* Mount options which take numeric value */
132 	fsparam_u32("backupuid", Opt_backupuid),
133 	fsparam_u32("backupgid", Opt_backupgid),
134 	fsparam_u32("uid", Opt_uid),
135 	fsparam_u32("cruid", Opt_cruid),
136 	fsparam_u32("gid", Opt_gid),
137 	fsparam_u32("file_mode", Opt_file_mode),
138 	fsparam_u32("dirmode", Opt_dirmode),
139 	fsparam_u32("dir_mode", Opt_dirmode),
140 	fsparam_u32("port", Opt_port),
141 	fsparam_u32("min_enc_offload", Opt_min_enc_offload),
142 	fsparam_u32("retrans", Opt_retrans),
143 	fsparam_u32("esize", Opt_min_enc_offload),
144 	fsparam_u32("bsize", Opt_blocksize),
145 	fsparam_u32("rasize", Opt_rasize),
146 	fsparam_u32("rsize", Opt_rsize),
147 	fsparam_u32("wsize", Opt_wsize),
148 	fsparam_u32("actimeo", Opt_actimeo),
149 	fsparam_u32("acdirmax", Opt_acdirmax),
150 	fsparam_u32("acregmax", Opt_acregmax),
151 	fsparam_u32("closetimeo", Opt_closetimeo),
152 	fsparam_u32("echo_interval", Opt_echo_interval),
153 	fsparam_u32("max_credits", Opt_max_credits),
154 	fsparam_u32("max_cached_dirs", Opt_max_cached_dirs),
155 	fsparam_u32("handletimeout", Opt_handletimeout),
156 	fsparam_u64("snapshot", Opt_snapshot),
157 	fsparam_u32("max_channels", Opt_max_channels),
158 
159 	/* Mount options which take string value */
160 	fsparam_string("source", Opt_source),
161 	fsparam_string("user", Opt_user),
162 	fsparam_string("username", Opt_user),
163 	fsparam_string("pass", Opt_pass),
164 	fsparam_string("password", Opt_pass),
165 	fsparam_string("pass2", Opt_pass2),
166 	fsparam_string("password2", Opt_pass2),
167 	fsparam_string("ip", Opt_ip),
168 	fsparam_string("addr", Opt_ip),
169 	fsparam_string("domain", Opt_domain),
170 	fsparam_string("dom", Opt_domain),
171 	fsparam_string("srcaddr", Opt_srcaddr),
172 	fsparam_string("iocharset", Opt_iocharset),
173 	fsparam_string("netbiosname", Opt_netbiosname),
174 	fsparam_string("servern", Opt_servern),
175 	fsparam_string("ver", Opt_ver),
176 	fsparam_string("vers", Opt_vers),
177 	fsparam_string("sec", Opt_sec),
178 	fsparam_string("cache", Opt_cache),
179 	fsparam_string("reparse", Opt_reparse),
180 
181 	/* Arguments that should be ignored */
182 	fsparam_flag("guest", Opt_ignore),
183 	fsparam_flag("noatime", Opt_ignore),
184 	fsparam_flag("relatime", Opt_ignore),
185 	fsparam_flag("_netdev", Opt_ignore),
186 	fsparam_flag_no("suid", Opt_ignore),
187 	fsparam_flag_no("exec", Opt_ignore),
188 	fsparam_flag_no("dev", Opt_ignore),
189 	fsparam_flag_no("mand", Opt_ignore),
190 	fsparam_flag_no("auto", Opt_ignore),
191 	fsparam_string("cred", Opt_ignore),
192 	fsparam_string("credentials", Opt_ignore),
193 	/*
194 	 * UNC and prefixpath is now extracted from Opt_source
195 	 * in the new mount API so we can just ignore them going forward.
196 	 */
197 	fsparam_string("unc", Opt_ignore),
198 	fsparam_string("prefixpath", Opt_ignore),
199 	{}
200 };
201 
202 static int
cifs_parse_security_flavors(struct fs_context * fc,char * value,struct smb3_fs_context * ctx)203 cifs_parse_security_flavors(struct fs_context *fc, char *value, struct smb3_fs_context *ctx)
204 {
205 
206 	substring_t args[MAX_OPT_ARGS];
207 
208 	/*
209 	 * With mount options, the last one should win. Reset any existing
210 	 * settings back to default.
211 	 */
212 	ctx->sectype = Unspecified;
213 	ctx->sign = false;
214 
215 	switch (match_token(value, cifs_secflavor_tokens, args)) {
216 	case Opt_sec_krb5p:
217 		cifs_errorf(fc, "sec=krb5p is not supported. Use sec=krb5,seal instead\n");
218 		return 1;
219 	case Opt_sec_krb5i:
220 		ctx->sign = true;
221 		fallthrough;
222 	case Opt_sec_krb5:
223 		ctx->sectype = Kerberos;
224 		break;
225 	case Opt_sec_ntlmsspi:
226 		ctx->sign = true;
227 		fallthrough;
228 	case Opt_sec_ntlmssp:
229 		ctx->sectype = RawNTLMSSP;
230 		break;
231 	case Opt_sec_ntlmv2i:
232 		ctx->sign = true;
233 		fallthrough;
234 	case Opt_sec_ntlmv2:
235 		ctx->sectype = NTLMv2;
236 		break;
237 	case Opt_sec_none:
238 		ctx->nullauth = 1;
239 		kfree(ctx->username);
240 		ctx->username = NULL;
241 		break;
242 	default:
243 		cifs_errorf(fc, "bad security option: %s\n", value);
244 		return 1;
245 	}
246 
247 	return 0;
248 }
249 
250 static const match_table_t cifs_cacheflavor_tokens = {
251 	{ Opt_cache_loose, "loose" },
252 	{ Opt_cache_strict, "strict" },
253 	{ Opt_cache_none, "none" },
254 	{ Opt_cache_ro, "ro" },
255 	{ Opt_cache_rw, "singleclient" },
256 	{ Opt_cache_err, NULL }
257 };
258 
259 static int
cifs_parse_cache_flavor(struct fs_context * fc,char * value,struct smb3_fs_context * ctx)260 cifs_parse_cache_flavor(struct fs_context *fc, char *value, struct smb3_fs_context *ctx)
261 {
262 	substring_t args[MAX_OPT_ARGS];
263 
264 	switch (match_token(value, cifs_cacheflavor_tokens, args)) {
265 	case Opt_cache_loose:
266 		ctx->direct_io = false;
267 		ctx->strict_io = false;
268 		ctx->cache_ro = false;
269 		ctx->cache_rw = false;
270 		break;
271 	case Opt_cache_strict:
272 		ctx->direct_io = false;
273 		ctx->strict_io = true;
274 		ctx->cache_ro = false;
275 		ctx->cache_rw = false;
276 		break;
277 	case Opt_cache_none:
278 		ctx->direct_io = true;
279 		ctx->strict_io = false;
280 		ctx->cache_ro = false;
281 		ctx->cache_rw = false;
282 		break;
283 	case Opt_cache_ro:
284 		ctx->direct_io = false;
285 		ctx->strict_io = false;
286 		ctx->cache_ro = true;
287 		ctx->cache_rw = false;
288 		break;
289 	case Opt_cache_rw:
290 		ctx->direct_io = false;
291 		ctx->strict_io = false;
292 		ctx->cache_ro = false;
293 		ctx->cache_rw = true;
294 		break;
295 	default:
296 		cifs_errorf(fc, "bad cache= option: %s\n", value);
297 		return 1;
298 	}
299 	return 0;
300 }
301 
302 static const match_table_t reparse_flavor_tokens = {
303 	{ Opt_reparse_default,	"default" },
304 	{ Opt_reparse_nfs,	"nfs" },
305 	{ Opt_reparse_wsl,	"wsl" },
306 	{ Opt_reparse_err,	NULL },
307 };
308 
parse_reparse_flavor(struct fs_context * fc,char * value,struct smb3_fs_context * ctx)309 static int parse_reparse_flavor(struct fs_context *fc, char *value,
310 				struct smb3_fs_context *ctx)
311 {
312 	substring_t args[MAX_OPT_ARGS];
313 
314 	switch (match_token(value, reparse_flavor_tokens, args)) {
315 	case Opt_reparse_default:
316 		ctx->reparse_type = CIFS_REPARSE_TYPE_DEFAULT;
317 		break;
318 	case Opt_reparse_nfs:
319 		ctx->reparse_type = CIFS_REPARSE_TYPE_NFS;
320 		break;
321 	case Opt_reparse_wsl:
322 		ctx->reparse_type = CIFS_REPARSE_TYPE_WSL;
323 		break;
324 	default:
325 		cifs_errorf(fc, "bad reparse= option: %s\n", value);
326 		return 1;
327 	}
328 	return 0;
329 }
330 
331 #define DUP_CTX_STR(field)						\
332 do {									\
333 	if (ctx->field) {						\
334 		new_ctx->field = kstrdup(ctx->field, GFP_ATOMIC);	\
335 		if (new_ctx->field == NULL) {				\
336 			smb3_cleanup_fs_context_contents(new_ctx);	\
337 			return -ENOMEM;					\
338 		}							\
339 	}								\
340 } while (0)
341 
342 int
smb3_fs_context_dup(struct smb3_fs_context * new_ctx,struct smb3_fs_context * ctx)343 smb3_fs_context_dup(struct smb3_fs_context *new_ctx, struct smb3_fs_context *ctx)
344 {
345 	memcpy(new_ctx, ctx, sizeof(*ctx));
346 	new_ctx->prepath = NULL;
347 	new_ctx->nodename = NULL;
348 	new_ctx->username = NULL;
349 	new_ctx->password = NULL;
350 	new_ctx->password2 = NULL;
351 	new_ctx->server_hostname = NULL;
352 	new_ctx->domainname = NULL;
353 	new_ctx->UNC = NULL;
354 	new_ctx->source = NULL;
355 	new_ctx->iocharset = NULL;
356 	new_ctx->leaf_fullpath = NULL;
357 	/*
358 	 * Make sure to stay in sync with smb3_cleanup_fs_context_contents()
359 	 */
360 	DUP_CTX_STR(prepath);
361 	DUP_CTX_STR(username);
362 	DUP_CTX_STR(password);
363 	DUP_CTX_STR(password2);
364 	DUP_CTX_STR(server_hostname);
365 	DUP_CTX_STR(UNC);
366 	DUP_CTX_STR(source);
367 	DUP_CTX_STR(domainname);
368 	DUP_CTX_STR(nodename);
369 	DUP_CTX_STR(iocharset);
370 	DUP_CTX_STR(leaf_fullpath);
371 
372 	return 0;
373 }
374 
375 static int
cifs_parse_smb_version(struct fs_context * fc,char * value,struct smb3_fs_context * ctx,bool is_smb3)376 cifs_parse_smb_version(struct fs_context *fc, char *value, struct smb3_fs_context *ctx, bool is_smb3)
377 {
378 	substring_t args[MAX_OPT_ARGS];
379 
380 	switch (match_token(value, cifs_smb_version_tokens, args)) {
381 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
382 	case Smb_1:
383 		if (disable_legacy_dialects) {
384 			cifs_errorf(fc, "mount with legacy dialect disabled\n");
385 			return 1;
386 		}
387 		if (is_smb3) {
388 			cifs_errorf(fc, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
389 			return 1;
390 		}
391 		cifs_errorf(fc, "Use of the less secure dialect vers=1.0 is not recommended unless required for access to very old servers\n");
392 		ctx->ops = &smb1_operations;
393 		ctx->vals = &smb1_values;
394 		break;
395 	case Smb_20:
396 		if (disable_legacy_dialects) {
397 			cifs_errorf(fc, "mount with legacy dialect disabled\n");
398 			return 1;
399 		}
400 		if (is_smb3) {
401 			cifs_errorf(fc, "vers=2.0 not permitted when mounting with smb3\n");
402 			return 1;
403 		}
404 		ctx->ops = &smb20_operations;
405 		ctx->vals = &smb20_values;
406 		break;
407 #else
408 	case Smb_1:
409 		cifs_errorf(fc, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
410 		return 1;
411 	case Smb_20:
412 		cifs_errorf(fc, "vers=2.0 mount not permitted when legacy dialects disabled\n");
413 		return 1;
414 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
415 	case Smb_21:
416 		ctx->ops = &smb21_operations;
417 		ctx->vals = &smb21_values;
418 		break;
419 	case Smb_30:
420 		ctx->ops = &smb30_operations;
421 		ctx->vals = &smb30_values;
422 		break;
423 	case Smb_302:
424 		ctx->ops = &smb30_operations; /* currently identical with 3.0 */
425 		ctx->vals = &smb302_values;
426 		break;
427 	case Smb_311:
428 		ctx->ops = &smb311_operations;
429 		ctx->vals = &smb311_values;
430 		break;
431 	case Smb_3any:
432 		ctx->ops = &smb30_operations; /* currently identical with 3.0 */
433 		ctx->vals = &smb3any_values;
434 		break;
435 	case Smb_default:
436 		ctx->ops = &smb30_operations;
437 		ctx->vals = &smbdefault_values;
438 		break;
439 	default:
440 		cifs_errorf(fc, "Unknown vers= option specified: %s\n", value);
441 		return 1;
442 	}
443 	return 0;
444 }
445 
smb3_parse_opt(const char * options,const char * key,char ** val)446 int smb3_parse_opt(const char *options, const char *key, char **val)
447 {
448 	int rc = -ENOENT;
449 	char *opts, *orig, *p;
450 
451 	orig = opts = kstrdup(options, GFP_KERNEL);
452 	if (!opts)
453 		return -ENOMEM;
454 
455 	while ((p = strsep(&opts, ","))) {
456 		char *nval;
457 
458 		if (!*p)
459 			continue;
460 		if (strncasecmp(p, key, strlen(key)))
461 			continue;
462 		nval = strchr(p, '=');
463 		if (nval) {
464 			if (nval == p)
465 				continue;
466 			*nval++ = 0;
467 			*val = kstrdup(nval, GFP_KERNEL);
468 			rc = !*val ? -ENOMEM : 0;
469 			goto out;
470 		}
471 	}
472 out:
473 	kfree(orig);
474 	return rc;
475 }
476 
477 /*
478  * Remove duplicate path delimiters. Windows is supposed to do that
479  * but there are some bugs that prevent rename from working if there are
480  * multiple delimiters.
481  *
482  * Return a sanitized duplicate of @path or NULL for empty prefix paths.
483  * Otherwise, return ERR_PTR.
484  *
485  * @gfp indicates the GFP_* flags for kstrdup.
486  * The caller is responsible for freeing the original.
487  */
488 #define IS_DELIM(c) ((c) == '/' || (c) == '\\')
cifs_sanitize_prepath(char * prepath,gfp_t gfp)489 char *cifs_sanitize_prepath(char *prepath, gfp_t gfp)
490 {
491 	char *cursor1 = prepath, *cursor2 = prepath;
492 	char *s;
493 
494 	/* skip all prepended delimiters */
495 	while (IS_DELIM(*cursor1))
496 		cursor1++;
497 
498 	/* copy the first letter */
499 	*cursor2 = *cursor1;
500 
501 	/* copy the remainder... */
502 	while (*(cursor1++)) {
503 		/* ... skipping all duplicated delimiters */
504 		if (IS_DELIM(*cursor1) && IS_DELIM(*cursor2))
505 			continue;
506 		*(++cursor2) = *cursor1;
507 	}
508 
509 	/* if the last character is a delimiter, skip it */
510 	if (IS_DELIM(*(cursor2 - 1)))
511 		cursor2--;
512 
513 	*cursor2 = '\0';
514 	if (!*prepath)
515 		return NULL;
516 	s = kstrdup(prepath, gfp);
517 	if (!s)
518 		return ERR_PTR(-ENOMEM);
519 	return s;
520 }
521 
522 /*
523  * Return full path based on the values of @ctx->{UNC,prepath}.
524  *
525  * It is assumed that both values were already parsed by smb3_parse_devname().
526  */
smb3_fs_context_fullpath(const struct smb3_fs_context * ctx,char dirsep)527 char *smb3_fs_context_fullpath(const struct smb3_fs_context *ctx, char dirsep)
528 {
529 	size_t ulen, plen;
530 	char *s;
531 
532 	ulen = strlen(ctx->UNC);
533 	plen = ctx->prepath ? strlen(ctx->prepath) + 1 : 0;
534 
535 	s = kmalloc(ulen + plen + 1, GFP_KERNEL);
536 	if (!s)
537 		return ERR_PTR(-ENOMEM);
538 	memcpy(s, ctx->UNC, ulen);
539 	if (plen) {
540 		s[ulen] = dirsep;
541 		memcpy(s + ulen + 1, ctx->prepath, plen);
542 	}
543 	s[ulen + plen] = '\0';
544 	convert_delimiter(s, dirsep);
545 	return s;
546 }
547 
548 /*
549  * Parse a devname into substrings and populate the ctx->UNC and ctx->prepath
550  * fields with the result. Returns 0 on success and an error otherwise
551  * (e.g. ENOMEM or EINVAL)
552  */
553 int
smb3_parse_devname(const char * devname,struct smb3_fs_context * ctx)554 smb3_parse_devname(const char *devname, struct smb3_fs_context *ctx)
555 {
556 	char *pos;
557 	const char *delims = "/\\";
558 	size_t len;
559 	int rc;
560 
561 	if (unlikely(!devname || !*devname)) {
562 		cifs_dbg(VFS, "Device name not specified\n");
563 		return -EINVAL;
564 	}
565 
566 	/* make sure we have a valid UNC double delimiter prefix */
567 	len = strspn(devname, delims);
568 	if (len != 2)
569 		return -EINVAL;
570 
571 	/* find delimiter between host and sharename */
572 	pos = strpbrk(devname + 2, delims);
573 	if (!pos)
574 		return -EINVAL;
575 
576 	/* record the server hostname */
577 	kfree(ctx->server_hostname);
578 	ctx->server_hostname = kstrndup(devname + 2, pos - devname - 2, GFP_KERNEL);
579 	if (!ctx->server_hostname)
580 		return -ENOMEM;
581 
582 	/* skip past delimiter */
583 	++pos;
584 
585 	/* now go until next delimiter or end of string */
586 	len = strcspn(pos, delims);
587 	if (!len)
588 		return -EINVAL;
589 
590 	/* move "pos" up to delimiter or NULL */
591 	pos += len;
592 	kfree(ctx->UNC);
593 	ctx->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
594 	if (!ctx->UNC)
595 		return -ENOMEM;
596 
597 	convert_delimiter(ctx->UNC, '\\');
598 
599 	/* skip any delimiter */
600 	if (*pos == '/' || *pos == '\\')
601 		pos++;
602 
603 	kfree(ctx->prepath);
604 	ctx->prepath = NULL;
605 
606 	/* If pos is NULL then no prepath */
607 	if (!*pos)
608 		return 0;
609 
610 	ctx->prepath = cifs_sanitize_prepath(pos, GFP_KERNEL);
611 	if (IS_ERR(ctx->prepath)) {
612 		rc = PTR_ERR(ctx->prepath);
613 		ctx->prepath = NULL;
614 		return rc;
615 	}
616 
617 	return 0;
618 }
619 
620 static void smb3_fs_context_free(struct fs_context *fc);
621 static int smb3_fs_context_parse_param(struct fs_context *fc,
622 				       struct fs_parameter *param);
623 static int smb3_fs_context_parse_monolithic(struct fs_context *fc,
624 					    void *data);
625 static int smb3_get_tree(struct fs_context *fc);
626 static int smb3_reconfigure(struct fs_context *fc);
627 
628 static const struct fs_context_operations smb3_fs_context_ops = {
629 	.free			= smb3_fs_context_free,
630 	.parse_param		= smb3_fs_context_parse_param,
631 	.parse_monolithic	= smb3_fs_context_parse_monolithic,
632 	.get_tree		= smb3_get_tree,
633 	.reconfigure		= smb3_reconfigure,
634 };
635 
636 /*
637  * Parse a monolithic block of data from sys_mount().
638  * smb3_fs_context_parse_monolithic - Parse key[=val][,key[=val]]* mount data
639  * @ctx: The superblock configuration to fill in.
640  * @data: The data to parse
641  *
642  * Parse a blob of data that's in key[=val][,key[=val]]* form.  This can be
643  * called from the ->monolithic_mount_data() fs_context operation.
644  *
645  * Returns 0 on success or the error returned by the ->parse_option() fs_context
646  * operation on failure.
647  */
smb3_fs_context_parse_monolithic(struct fs_context * fc,void * data)648 static int smb3_fs_context_parse_monolithic(struct fs_context *fc,
649 					   void *data)
650 {
651 	char *options = data, *key;
652 	int ret = 0;
653 
654 	if (!options)
655 		return 0;
656 
657 	ret = security_sb_eat_lsm_opts(options, &fc->security);
658 	if (ret)
659 		return ret;
660 
661 	/* BB Need to add support for sep= here TBD */
662 	while ((key = strsep(&options, ",")) != NULL) {
663 		size_t len;
664 		char *value;
665 
666 		if (*key == 0)
667 			break;
668 
669 		/* Check if following character is the deliminator If yes,
670 		 * we have encountered a double deliminator reset the NULL
671 		 * character to the deliminator
672 		 */
673 		while (options && options[0] == ',') {
674 			len = strlen(key);
675 			strcpy(key + len, options);
676 			options = strchr(options, ',');
677 			if (options)
678 				*options++ = 0;
679 		}
680 
681 
682 		len = 0;
683 		value = strchr(key, '=');
684 		if (value) {
685 			if (value == key)
686 				continue;
687 			*value++ = 0;
688 			len = strlen(value);
689 		}
690 
691 		ret = vfs_parse_fs_string(fc, key, value, len);
692 		if (ret < 0)
693 			break;
694 	}
695 
696 	return ret;
697 }
698 
699 /*
700  * Validate the preparsed information in the config.
701  */
smb3_fs_context_validate(struct fs_context * fc)702 static int smb3_fs_context_validate(struct fs_context *fc)
703 {
704 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
705 
706 	if (ctx->rdma && ctx->vals->protocol_id < SMB30_PROT_ID) {
707 		cifs_errorf(fc, "SMB Direct requires Version >=3.0\n");
708 		return -EOPNOTSUPP;
709 	}
710 
711 #ifndef CONFIG_KEYS
712 	/* Muliuser mounts require CONFIG_KEYS support */
713 	if (ctx->multiuser) {
714 		cifs_errorf(fc, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
715 		return -1;
716 	}
717 #endif
718 
719 	if (ctx->got_version == false)
720 		pr_warn_once("No dialect specified on mount. Default has changed to a more secure dialect, SMB2.1 or later (e.g. SMB3.1.1), from CIFS (SMB1). To use the less secure SMB1 dialect to access old servers which do not support SMB3.1.1 (or even SMB3 or SMB2.1) specify vers=1.0 on mount.\n");
721 
722 
723 	if (!ctx->UNC) {
724 		cifs_errorf(fc, "CIFS mount error: No usable UNC path provided in device string!\n");
725 		return -1;
726 	}
727 
728 	/* make sure UNC has a share name */
729 	if (strlen(ctx->UNC) < 3 || !strchr(ctx->UNC + 3, '\\')) {
730 		cifs_errorf(fc, "Malformed UNC. Unable to find share name.\n");
731 		return -ENOENT;
732 	}
733 
734 	if (!ctx->got_ip) {
735 		int len;
736 		const char *slash;
737 
738 		/* No ip= option specified? Try to get it from UNC */
739 		/* Use the address part of the UNC. */
740 		slash = strchr(&ctx->UNC[2], '\\');
741 		len = slash - &ctx->UNC[2];
742 		if (!cifs_convert_address((struct sockaddr *)&ctx->dstaddr,
743 					  &ctx->UNC[2], len)) {
744 			pr_err("Unable to determine destination address\n");
745 			return -EHOSTUNREACH;
746 		}
747 	}
748 
749 	/* set the port that we got earlier */
750 	cifs_set_port((struct sockaddr *)&ctx->dstaddr, ctx->port);
751 
752 	if (ctx->uid_specified && !ctx->forceuid_specified) {
753 		ctx->override_uid = 1;
754 		pr_notice("enabling forceuid mount option implicitly because uid= option is specified\n");
755 	}
756 
757 	if (ctx->gid_specified && !ctx->forcegid_specified) {
758 		ctx->override_gid = 1;
759 		pr_notice("enabling forcegid mount option implicitly because gid= option is specified\n");
760 	}
761 
762 	if (ctx->override_uid && !ctx->uid_specified) {
763 		ctx->override_uid = 0;
764 		pr_notice("ignoring forceuid mount option specified with no uid= option\n");
765 	}
766 
767 	if (ctx->override_gid && !ctx->gid_specified) {
768 		ctx->override_gid = 0;
769 		pr_notice("ignoring forcegid mount option specified with no gid= option\n");
770 	}
771 
772 	return 0;
773 }
774 
smb3_get_tree_common(struct fs_context * fc)775 static int smb3_get_tree_common(struct fs_context *fc)
776 {
777 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
778 	struct dentry *root;
779 	int rc = 0;
780 
781 	root = cifs_smb3_do_mount(fc->fs_type, 0, ctx);
782 	if (IS_ERR(root))
783 		return PTR_ERR(root);
784 
785 	fc->root = root;
786 
787 	return rc;
788 }
789 
790 /*
791  * Create an SMB3 superblock from the parameters passed.
792  */
smb3_get_tree(struct fs_context * fc)793 static int smb3_get_tree(struct fs_context *fc)
794 {
795 	int err = smb3_fs_context_validate(fc);
796 	int ret;
797 
798 	if (err)
799 		return err;
800 	cifs_mount_lock();
801 	ret = smb3_get_tree_common(fc);
802 	cifs_mount_unlock();
803 	return ret;
804 }
805 
smb3_fs_context_free(struct fs_context * fc)806 static void smb3_fs_context_free(struct fs_context *fc)
807 {
808 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
809 
810 	smb3_cleanup_fs_context(ctx);
811 }
812 
813 /*
814  * Compare the old and new proposed context during reconfigure
815  * and check if the changes are compatible.
816  */
smb3_verify_reconfigure_ctx(struct fs_context * fc,struct smb3_fs_context * new_ctx,struct smb3_fs_context * old_ctx,bool need_recon)817 static int smb3_verify_reconfigure_ctx(struct fs_context *fc,
818 				       struct smb3_fs_context *new_ctx,
819 				       struct smb3_fs_context *old_ctx, bool need_recon)
820 {
821 	if (new_ctx->posix_paths != old_ctx->posix_paths) {
822 		cifs_errorf(fc, "can not change posixpaths during remount\n");
823 		return -EINVAL;
824 	}
825 	if (new_ctx->sectype != old_ctx->sectype) {
826 		cifs_errorf(fc, "can not change sec during remount\n");
827 		return -EINVAL;
828 	}
829 	if (new_ctx->multiuser != old_ctx->multiuser) {
830 		cifs_errorf(fc, "can not change multiuser during remount\n");
831 		return -EINVAL;
832 	}
833 	if (new_ctx->UNC &&
834 	    (!old_ctx->UNC || strcmp(new_ctx->UNC, old_ctx->UNC))) {
835 		cifs_errorf(fc, "can not change UNC during remount\n");
836 		return -EINVAL;
837 	}
838 	if (new_ctx->username &&
839 	    (!old_ctx->username || strcmp(new_ctx->username, old_ctx->username))) {
840 		cifs_errorf(fc, "can not change username during remount\n");
841 		return -EINVAL;
842 	}
843 	if (new_ctx->password &&
844 	    (!old_ctx->password || strcmp(new_ctx->password, old_ctx->password))) {
845 		if (need_recon == false) {
846 			cifs_errorf(fc,
847 				    "can not change password of active session during remount\n");
848 			return -EINVAL;
849 		} else if (old_ctx->sectype == Kerberos) {
850 			cifs_errorf(fc,
851 				    "can not change password for Kerberos via remount\n");
852 			return -EINVAL;
853 		}
854 	}
855 	if (new_ctx->domainname &&
856 	    (!old_ctx->domainname || strcmp(new_ctx->domainname, old_ctx->domainname))) {
857 		cifs_errorf(fc, "can not change domainname during remount\n");
858 		return -EINVAL;
859 	}
860 	if (strcmp(new_ctx->workstation_name, old_ctx->workstation_name)) {
861 		cifs_errorf(fc, "can not change workstation_name during remount\n");
862 		return -EINVAL;
863 	}
864 	if (new_ctx->nodename &&
865 	    (!old_ctx->nodename || strcmp(new_ctx->nodename, old_ctx->nodename))) {
866 		cifs_errorf(fc, "can not change nodename during remount\n");
867 		return -EINVAL;
868 	}
869 	if (new_ctx->iocharset &&
870 	    (!old_ctx->iocharset || strcmp(new_ctx->iocharset, old_ctx->iocharset))) {
871 		cifs_errorf(fc, "can not change iocharset during remount\n");
872 		return -EINVAL;
873 	}
874 
875 	return 0;
876 }
877 
878 #define STEAL_STRING(cifs_sb, ctx, field)				\
879 do {									\
880 	kfree(ctx->field);						\
881 	ctx->field = cifs_sb->ctx->field;				\
882 	cifs_sb->ctx->field = NULL;					\
883 } while (0)
884 
885 #define STEAL_STRING_SENSITIVE(cifs_sb, ctx, field)			\
886 do {									\
887 	kfree_sensitive(ctx->field);					\
888 	ctx->field = cifs_sb->ctx->field;				\
889 	cifs_sb->ctx->field = NULL;					\
890 } while (0)
891 
smb3_sync_session_ctx_passwords(struct cifs_sb_info * cifs_sb,struct cifs_ses * ses)892 int smb3_sync_session_ctx_passwords(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses)
893 {
894 	if (ses->password &&
895 	    cifs_sb->ctx->password &&
896 	    strcmp(ses->password, cifs_sb->ctx->password)) {
897 		kfree_sensitive(cifs_sb->ctx->password);
898 		cifs_sb->ctx->password = kstrdup(ses->password, GFP_KERNEL);
899 		if (!cifs_sb->ctx->password)
900 			return -ENOMEM;
901 	}
902 	if (ses->password2 &&
903 	    cifs_sb->ctx->password2 &&
904 	    strcmp(ses->password2, cifs_sb->ctx->password2)) {
905 		kfree_sensitive(cifs_sb->ctx->password2);
906 		cifs_sb->ctx->password2 = kstrdup(ses->password2, GFP_KERNEL);
907 		if (!cifs_sb->ctx->password2) {
908 			kfree_sensitive(cifs_sb->ctx->password);
909 			cifs_sb->ctx->password = NULL;
910 			return -ENOMEM;
911 		}
912 	}
913 	return 0;
914 }
915 
smb3_reconfigure(struct fs_context * fc)916 static int smb3_reconfigure(struct fs_context *fc)
917 {
918 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
919 	struct dentry *root = fc->root;
920 	struct cifs_sb_info *cifs_sb = CIFS_SB(root->d_sb);
921 	struct cifs_ses *ses = cifs_sb_master_tcon(cifs_sb)->ses;
922 	char *new_password = NULL, *new_password2 = NULL;
923 	bool need_recon = false;
924 	int rc;
925 
926 	if (ses->expired_pwd)
927 		need_recon = true;
928 
929 	rc = smb3_verify_reconfigure_ctx(fc, ctx, cifs_sb->ctx, need_recon);
930 	if (rc)
931 		return rc;
932 
933 	/*
934 	 * We can not change UNC/username/password/domainname/
935 	 * workstation_name/nodename/iocharset
936 	 * during reconnect so ignore what we have in the new context and
937 	 * just use what we already have in cifs_sb->ctx.
938 	 */
939 	STEAL_STRING(cifs_sb, ctx, UNC);
940 	STEAL_STRING(cifs_sb, ctx, source);
941 	STEAL_STRING(cifs_sb, ctx, username);
942 
943 	if (need_recon == false)
944 		STEAL_STRING_SENSITIVE(cifs_sb, ctx, password);
945 	else  {
946 		if (ctx->password) {
947 			new_password = kstrdup(ctx->password, GFP_KERNEL);
948 			if (!new_password)
949 				return -ENOMEM;
950 		} else
951 			STEAL_STRING_SENSITIVE(cifs_sb, ctx, password);
952 	}
953 
954 	/*
955 	 * if a new password2 has been specified, then reset it's value
956 	 * inside the ses struct
957 	 */
958 	if (ctx->password2) {
959 		new_password2 = kstrdup(ctx->password2, GFP_KERNEL);
960 		if (!new_password2) {
961 			kfree_sensitive(new_password);
962 			return -ENOMEM;
963 		}
964 	} else
965 		STEAL_STRING_SENSITIVE(cifs_sb, ctx, password2);
966 
967 	/*
968 	 * we may update the passwords in the ses struct below. Make sure we do
969 	 * not race with smb2_reconnect
970 	 */
971 	mutex_lock(&ses->session_mutex);
972 
973 	/*
974 	 * smb2_reconnect may swap password and password2 in case session setup
975 	 * failed. First get ctx passwords in sync with ses passwords. It should
976 	 * be okay to do this even if this function were to return an error at a
977 	 * later stage
978 	 */
979 	rc = smb3_sync_session_ctx_passwords(cifs_sb, ses);
980 	if (rc) {
981 		mutex_unlock(&ses->session_mutex);
982 		return rc;
983 	}
984 
985 	/*
986 	 * now that allocations for passwords are done, commit them
987 	 */
988 	if (new_password) {
989 		kfree_sensitive(ses->password);
990 		ses->password = new_password;
991 	}
992 	if (new_password2) {
993 		kfree_sensitive(ses->password2);
994 		ses->password2 = new_password2;
995 	}
996 
997 	mutex_unlock(&ses->session_mutex);
998 
999 	STEAL_STRING(cifs_sb, ctx, domainname);
1000 	STEAL_STRING(cifs_sb, ctx, nodename);
1001 	STEAL_STRING(cifs_sb, ctx, iocharset);
1002 
1003 	/* if rsize or wsize not passed in on remount, use previous values */
1004 	if (ctx->rsize == 0)
1005 		ctx->rsize = cifs_sb->ctx->rsize;
1006 	if (ctx->wsize == 0)
1007 		ctx->wsize = cifs_sb->ctx->wsize;
1008 
1009 
1010 	smb3_cleanup_fs_context_contents(cifs_sb->ctx);
1011 	rc = smb3_fs_context_dup(cifs_sb->ctx, ctx);
1012 	smb3_update_mnt_flags(cifs_sb);
1013 #ifdef CONFIG_CIFS_DFS_UPCALL
1014 	if (!rc)
1015 		rc = dfs_cache_remount_fs(cifs_sb);
1016 #endif
1017 
1018 	return rc;
1019 }
1020 
smb3_fs_context_parse_param(struct fs_context * fc,struct fs_parameter * param)1021 static int smb3_fs_context_parse_param(struct fs_context *fc,
1022 				      struct fs_parameter *param)
1023 {
1024 	struct fs_parse_result result;
1025 	struct smb3_fs_context *ctx = smb3_fc2context(fc);
1026 	int i, opt;
1027 	bool is_smb3 = !strcmp(fc->fs_type->name, "smb3");
1028 	bool skip_parsing = false;
1029 	kuid_t uid;
1030 	kgid_t gid;
1031 
1032 	cifs_dbg(FYI, "CIFS: parsing cifs mount option '%s'\n", param->key);
1033 
1034 	/*
1035 	 * fs_parse can not handle string options with an empty value so
1036 	 * we will need special handling of them.
1037 	 */
1038 	if (param->type == fs_value_is_string && param->string[0] == 0) {
1039 		if (!strcmp("pass", param->key) || !strcmp("password", param->key)) {
1040 			skip_parsing = true;
1041 			opt = Opt_pass;
1042 		} else if (!strcmp("user", param->key) || !strcmp("username", param->key)) {
1043 			skip_parsing = true;
1044 			opt = Opt_user;
1045 		} else if (!strcmp("pass2", param->key) || !strcmp("password2", param->key)) {
1046 			skip_parsing = true;
1047 			opt = Opt_pass2;
1048 		}
1049 	}
1050 
1051 	if (!skip_parsing) {
1052 		opt = fs_parse(fc, smb3_fs_parameters, param, &result);
1053 		if (opt < 0)
1054 			return ctx->sloppy ? 1 : opt;
1055 	}
1056 
1057 	switch (opt) {
1058 	case Opt_compress:
1059 		ctx->compress = true;
1060 		cifs_dbg(VFS,
1061 			"SMB3 compression support is experimental\n");
1062 		break;
1063 	case Opt_nodfs:
1064 		ctx->nodfs = 1;
1065 		break;
1066 	case Opt_hard:
1067 		if (result.negated) {
1068 			if (ctx->retry == 1)
1069 				cifs_dbg(VFS, "conflicting hard vs. soft mount options\n");
1070 			ctx->retry = 0;
1071 		} else
1072 			ctx->retry = 1;
1073 		break;
1074 	case Opt_soft:
1075 		if (result.negated)
1076 			ctx->retry = 1;
1077 		else {
1078 			if (ctx->retry == 1)
1079 				cifs_dbg(VFS, "conflicting hard vs soft mount options\n");
1080 			ctx->retry = 0;
1081 		}
1082 		break;
1083 	case Opt_mapposix:
1084 		if (result.negated)
1085 			ctx->remap = false;
1086 		else {
1087 			ctx->remap = true;
1088 			ctx->sfu_remap = false; /* disable SFU mapping */
1089 		}
1090 		break;
1091 	case Opt_mapchars:
1092 		if (result.negated)
1093 			ctx->sfu_remap = false;
1094 		else {
1095 			ctx->sfu_remap = true;
1096 			ctx->remap = false; /* disable SFM (mapposix) mapping */
1097 		}
1098 		break;
1099 	case Opt_user_xattr:
1100 		if (result.negated)
1101 			ctx->no_xattr = 1;
1102 		else
1103 			ctx->no_xattr = 0;
1104 		break;
1105 	case Opt_forceuid:
1106 		if (result.negated)
1107 			ctx->override_uid = 0;
1108 		else
1109 			ctx->override_uid = 1;
1110 		ctx->forceuid_specified = true;
1111 		break;
1112 	case Opt_forcegid:
1113 		if (result.negated)
1114 			ctx->override_gid = 0;
1115 		else
1116 			ctx->override_gid = 1;
1117 		ctx->forcegid_specified = true;
1118 		break;
1119 	case Opt_perm:
1120 		if (result.negated)
1121 			ctx->noperm = 1;
1122 		else
1123 			ctx->noperm = 0;
1124 		break;
1125 	case Opt_dynperm:
1126 		if (result.negated)
1127 			ctx->dynperm = 0;
1128 		else
1129 			ctx->dynperm = 1;
1130 		break;
1131 	case Opt_sfu:
1132 		if (result.negated)
1133 			ctx->sfu_emul = 0;
1134 		else
1135 			ctx->sfu_emul = 1;
1136 		break;
1137 	case Opt_noblocksend:
1138 		ctx->noblocksnd = 1;
1139 		break;
1140 	case Opt_noautotune:
1141 		ctx->noautotune = 1;
1142 		break;
1143 	case Opt_nolease:
1144 		ctx->no_lease = 1;
1145 		break;
1146 	case Opt_nosparse:
1147 		ctx->no_sparse = 1;
1148 		break;
1149 	case Opt_nodelete:
1150 		ctx->nodelete = 1;
1151 		break;
1152 	case Opt_multichannel:
1153 		if (result.negated) {
1154 			ctx->multichannel = false;
1155 			ctx->max_channels = 1;
1156 		} else {
1157 			ctx->multichannel = true;
1158 			/* if number of channels not specified, default to 2 */
1159 			if (ctx->max_channels < 2)
1160 				ctx->max_channels = 2;
1161 		}
1162 		break;
1163 	case Opt_uid:
1164 		uid = make_kuid(current_user_ns(), result.uint_32);
1165 		if (!uid_valid(uid))
1166 			goto cifs_parse_mount_err;
1167 		ctx->linux_uid = uid;
1168 		ctx->uid_specified = true;
1169 		break;
1170 	case Opt_cruid:
1171 		uid = make_kuid(current_user_ns(), result.uint_32);
1172 		if (!uid_valid(uid))
1173 			goto cifs_parse_mount_err;
1174 		ctx->cred_uid = uid;
1175 		ctx->cruid_specified = true;
1176 		break;
1177 	case Opt_backupuid:
1178 		uid = make_kuid(current_user_ns(), result.uint_32);
1179 		if (!uid_valid(uid))
1180 			goto cifs_parse_mount_err;
1181 		ctx->backupuid = uid;
1182 		ctx->backupuid_specified = true;
1183 		break;
1184 	case Opt_backupgid:
1185 		gid = make_kgid(current_user_ns(), result.uint_32);
1186 		if (!gid_valid(gid))
1187 			goto cifs_parse_mount_err;
1188 		ctx->backupgid = gid;
1189 		ctx->backupgid_specified = true;
1190 		break;
1191 	case Opt_gid:
1192 		gid = make_kgid(current_user_ns(), result.uint_32);
1193 		if (!gid_valid(gid))
1194 			goto cifs_parse_mount_err;
1195 		ctx->linux_gid = gid;
1196 		ctx->gid_specified = true;
1197 		break;
1198 	case Opt_port:
1199 		ctx->port = result.uint_32;
1200 		break;
1201 	case Opt_file_mode:
1202 		ctx->file_mode = result.uint_32;
1203 		break;
1204 	case Opt_dirmode:
1205 		ctx->dir_mode = result.uint_32;
1206 		break;
1207 	case Opt_min_enc_offload:
1208 		ctx->min_offload = result.uint_32;
1209 		break;
1210 	case Opt_retrans:
1211 		ctx->retrans = result.uint_32;
1212 		break;
1213 	case Opt_blocksize:
1214 		/*
1215 		 * inode blocksize realistically should never need to be
1216 		 * less than 16K or greater than 16M and default is 1MB.
1217 		 * Note that small inode block sizes (e.g. 64K) can lead
1218 		 * to very poor performance of common tools like cp and scp
1219 		 */
1220 		if ((result.uint_32 < CIFS_MAX_MSGSIZE) ||
1221 		   (result.uint_32 > (4 * SMB3_DEFAULT_IOSIZE))) {
1222 			cifs_errorf(fc, "%s: Invalid blocksize\n",
1223 				__func__);
1224 			goto cifs_parse_mount_err;
1225 		}
1226 		ctx->bsize = result.uint_32;
1227 		ctx->got_bsize = true;
1228 		break;
1229 	case Opt_rasize:
1230 		/*
1231 		 * readahead size realistically should never need to be
1232 		 * less than 1M (CIFS_DEFAULT_IOSIZE) or greater than 32M
1233 		 * (perhaps an exception should be considered in the
1234 		 * for the case of a large number of channels
1235 		 * when multichannel is negotiated) since that would lead
1236 		 * to plenty of parallel I/O in flight to the server.
1237 		 * Note that smaller read ahead sizes would
1238 		 * hurt performance of common tools like cp and scp
1239 		 * which often trigger sequential i/o with read ahead
1240 		 */
1241 		if ((result.uint_32 > (8 * SMB3_DEFAULT_IOSIZE)) ||
1242 		    (result.uint_32 < CIFS_DEFAULT_IOSIZE)) {
1243 			cifs_errorf(fc, "%s: Invalid rasize %d vs. %d\n",
1244 				__func__, result.uint_32, SMB3_DEFAULT_IOSIZE);
1245 			goto cifs_parse_mount_err;
1246 		}
1247 		ctx->rasize = result.uint_32;
1248 		break;
1249 	case Opt_rsize:
1250 		ctx->rsize = result.uint_32;
1251 		ctx->got_rsize = true;
1252 		break;
1253 	case Opt_wsize:
1254 		ctx->wsize = result.uint_32;
1255 		ctx->got_wsize = true;
1256 		if (ctx->wsize % PAGE_SIZE != 0) {
1257 			ctx->wsize = round_down(ctx->wsize, PAGE_SIZE);
1258 			if (ctx->wsize == 0) {
1259 				ctx->wsize = PAGE_SIZE;
1260 				cifs_dbg(VFS, "wsize too small, reset to minimum %ld\n", PAGE_SIZE);
1261 			} else {
1262 				cifs_dbg(VFS,
1263 					 "wsize rounded down to %d to multiple of PAGE_SIZE %ld\n",
1264 					 ctx->wsize, PAGE_SIZE);
1265 			}
1266 		}
1267 		break;
1268 	case Opt_acregmax:
1269 		if (result.uint_32 > CIFS_MAX_ACTIMEO / HZ) {
1270 			cifs_errorf(fc, "acregmax too large\n");
1271 			goto cifs_parse_mount_err;
1272 		}
1273 		ctx->acregmax = HZ * result.uint_32;
1274 		break;
1275 	case Opt_acdirmax:
1276 		if (result.uint_32 > CIFS_MAX_ACTIMEO / HZ) {
1277 			cifs_errorf(fc, "acdirmax too large\n");
1278 			goto cifs_parse_mount_err;
1279 		}
1280 		ctx->acdirmax = HZ * result.uint_32;
1281 		break;
1282 	case Opt_actimeo:
1283 		if (result.uint_32 > CIFS_MAX_ACTIMEO / HZ) {
1284 			cifs_errorf(fc, "timeout too large\n");
1285 			goto cifs_parse_mount_err;
1286 		}
1287 		if ((ctx->acdirmax != CIFS_DEF_ACTIMEO) ||
1288 		    (ctx->acregmax != CIFS_DEF_ACTIMEO)) {
1289 			cifs_errorf(fc, "actimeo ignored since acregmax or acdirmax specified\n");
1290 			break;
1291 		}
1292 		ctx->acdirmax = ctx->acregmax = HZ * result.uint_32;
1293 		break;
1294 	case Opt_closetimeo:
1295 		if (result.uint_32 > SMB3_MAX_DCLOSETIMEO / HZ) {
1296 			cifs_errorf(fc, "closetimeo too large\n");
1297 			goto cifs_parse_mount_err;
1298 		}
1299 		ctx->closetimeo = HZ * result.uint_32;
1300 		break;
1301 	case Opt_echo_interval:
1302 		ctx->echo_interval = result.uint_32;
1303 		break;
1304 	case Opt_snapshot:
1305 		ctx->snapshot_time = result.uint_64;
1306 		break;
1307 	case Opt_max_credits:
1308 		if (result.uint_32 < 20 || result.uint_32 > 60000) {
1309 			cifs_errorf(fc, "%s: Invalid max_credits value\n",
1310 				 __func__);
1311 			goto cifs_parse_mount_err;
1312 		}
1313 		ctx->max_credits = result.uint_32;
1314 		break;
1315 	case Opt_max_channels:
1316 		if (result.uint_32 < 1 || result.uint_32 > CIFS_MAX_CHANNELS) {
1317 			cifs_errorf(fc, "%s: Invalid max_channels value, needs to be 1-%d\n",
1318 				 __func__, CIFS_MAX_CHANNELS);
1319 			goto cifs_parse_mount_err;
1320 		}
1321 		ctx->max_channels = result.uint_32;
1322 		/* If more than one channel requested ... they want multichan */
1323 		if (result.uint_32 > 1)
1324 			ctx->multichannel = true;
1325 		break;
1326 	case Opt_max_cached_dirs:
1327 		if (result.uint_32 < 1) {
1328 			cifs_errorf(fc, "%s: Invalid max_cached_dirs, needs to be 1 or more\n",
1329 				    __func__);
1330 			goto cifs_parse_mount_err;
1331 		}
1332 		ctx->max_cached_dirs = result.uint_32;
1333 		break;
1334 	case Opt_handletimeout:
1335 		ctx->handle_timeout = result.uint_32;
1336 		if (ctx->handle_timeout > SMB3_MAX_HANDLE_TIMEOUT) {
1337 			cifs_errorf(fc, "Invalid handle cache timeout, longer than 16 minutes\n");
1338 			goto cifs_parse_mount_err;
1339 		}
1340 		break;
1341 	case Opt_source:
1342 		kfree(ctx->UNC);
1343 		ctx->UNC = NULL;
1344 		switch (smb3_parse_devname(param->string, ctx)) {
1345 		case 0:
1346 			break;
1347 		case -ENOMEM:
1348 			cifs_errorf(fc, "Unable to allocate memory for devname\n");
1349 			goto cifs_parse_mount_err;
1350 		case -EINVAL:
1351 			cifs_errorf(fc, "Malformed UNC in devname\n");
1352 			goto cifs_parse_mount_err;
1353 		default:
1354 			cifs_errorf(fc, "Unknown error parsing devname\n");
1355 			goto cifs_parse_mount_err;
1356 		}
1357 		ctx->source = smb3_fs_context_fullpath(ctx, '/');
1358 		if (IS_ERR(ctx->source)) {
1359 			ctx->source = NULL;
1360 			cifs_errorf(fc, "OOM when copying UNC string\n");
1361 			goto cifs_parse_mount_err;
1362 		}
1363 		fc->source = kstrdup(ctx->source, GFP_KERNEL);
1364 		if (fc->source == NULL) {
1365 			cifs_errorf(fc, "OOM when copying UNC string\n");
1366 			goto cifs_parse_mount_err;
1367 		}
1368 		break;
1369 	case Opt_user:
1370 		kfree(ctx->username);
1371 		ctx->username = NULL;
1372 		if (ctx->nullauth)
1373 			break;
1374 		if (strlen(param->string) == 0) {
1375 			/* null user, ie. anonymous authentication */
1376 			ctx->nullauth = 1;
1377 			break;
1378 		}
1379 
1380 		if (strnlen(param->string, CIFS_MAX_USERNAME_LEN) >
1381 		    CIFS_MAX_USERNAME_LEN) {
1382 			pr_warn("username too long\n");
1383 			goto cifs_parse_mount_err;
1384 		}
1385 		ctx->username = kstrdup(param->string, GFP_KERNEL);
1386 		if (ctx->username == NULL) {
1387 			cifs_errorf(fc, "OOM when copying username string\n");
1388 			goto cifs_parse_mount_err;
1389 		}
1390 		break;
1391 	case Opt_pass:
1392 		kfree_sensitive(ctx->password);
1393 		ctx->password = NULL;
1394 		if (strlen(param->string) == 0)
1395 			break;
1396 
1397 		ctx->password = kstrdup(param->string, GFP_KERNEL);
1398 		if (ctx->password == NULL) {
1399 			cifs_errorf(fc, "OOM when copying password string\n");
1400 			goto cifs_parse_mount_err;
1401 		}
1402 		break;
1403 	case Opt_pass2:
1404 		kfree_sensitive(ctx->password2);
1405 		ctx->password2 = NULL;
1406 		if (strlen(param->string) == 0)
1407 			break;
1408 
1409 		ctx->password2 = kstrdup(param->string, GFP_KERNEL);
1410 		if (ctx->password2 == NULL) {
1411 			cifs_errorf(fc, "OOM when copying password2 string\n");
1412 			goto cifs_parse_mount_err;
1413 		}
1414 		break;
1415 	case Opt_ip:
1416 		if (strlen(param->string) == 0) {
1417 			ctx->got_ip = false;
1418 			break;
1419 		}
1420 		if (!cifs_convert_address((struct sockaddr *)&ctx->dstaddr,
1421 					  param->string,
1422 					  strlen(param->string))) {
1423 			pr_err("bad ip= option (%s)\n", param->string);
1424 			goto cifs_parse_mount_err;
1425 		}
1426 		ctx->got_ip = true;
1427 		break;
1428 	case Opt_domain:
1429 		if (strnlen(param->string, CIFS_MAX_DOMAINNAME_LEN)
1430 				== CIFS_MAX_DOMAINNAME_LEN) {
1431 			pr_warn("domain name too long\n");
1432 			goto cifs_parse_mount_err;
1433 		}
1434 
1435 		kfree(ctx->domainname);
1436 		ctx->domainname = kstrdup(param->string, GFP_KERNEL);
1437 		if (ctx->domainname == NULL) {
1438 			cifs_errorf(fc, "OOM when copying domainname string\n");
1439 			goto cifs_parse_mount_err;
1440 		}
1441 		cifs_dbg(FYI, "Domain name set\n");
1442 		break;
1443 	case Opt_srcaddr:
1444 		if (!cifs_convert_address(
1445 				(struct sockaddr *)&ctx->srcaddr,
1446 				param->string, strlen(param->string))) {
1447 			pr_warn("Could not parse srcaddr: %s\n",
1448 				param->string);
1449 			goto cifs_parse_mount_err;
1450 		}
1451 		break;
1452 	case Opt_iocharset:
1453 		if (strnlen(param->string, 1024) >= 65) {
1454 			pr_warn("iocharset name too long\n");
1455 			goto cifs_parse_mount_err;
1456 		}
1457 
1458 		if (strncasecmp(param->string, "default", 7) != 0) {
1459 			kfree(ctx->iocharset);
1460 			ctx->iocharset = kstrdup(param->string, GFP_KERNEL);
1461 			if (ctx->iocharset == NULL) {
1462 				cifs_errorf(fc, "OOM when copying iocharset string\n");
1463 				goto cifs_parse_mount_err;
1464 			}
1465 		}
1466 		/* if iocharset not set then load_nls_default
1467 		 * is used by caller
1468 		 */
1469 		cifs_dbg(FYI, "iocharset set to %s\n", ctx->iocharset);
1470 		break;
1471 	case Opt_netbiosname:
1472 		memset(ctx->source_rfc1001_name, 0x20,
1473 			RFC1001_NAME_LEN);
1474 		/*
1475 		 * FIXME: are there cases in which a comma can
1476 		 * be valid in workstation netbios name (and
1477 		 * need special handling)?
1478 		 */
1479 		for (i = 0; i < RFC1001_NAME_LEN; i++) {
1480 			/* don't ucase netbiosname for user */
1481 			if (param->string[i] == 0)
1482 				break;
1483 			ctx->source_rfc1001_name[i] = param->string[i];
1484 		}
1485 		/* The string has 16th byte zero still from
1486 		 * set at top of the function
1487 		 */
1488 		if (i == RFC1001_NAME_LEN && param->string[i] != 0)
1489 			pr_warn("netbiosname longer than 15 truncated\n");
1490 		break;
1491 	case Opt_servern:
1492 		/* last byte, type, is 0x20 for servr type */
1493 		memset(ctx->target_rfc1001_name, 0x20,
1494 			RFC1001_NAME_LEN_WITH_NULL);
1495 		/*
1496 		 * BB are there cases in which a comma can be valid in this
1497 		 * workstation netbios name (and need special handling)?
1498 		 */
1499 
1500 		/* user or mount helper must uppercase the netbios name */
1501 		for (i = 0; i < 15; i++) {
1502 			if (param->string[i] == 0)
1503 				break;
1504 			ctx->target_rfc1001_name[i] = param->string[i];
1505 		}
1506 
1507 		/* The string has 16th byte zero still from set at top of function */
1508 		if (i == RFC1001_NAME_LEN && param->string[i] != 0)
1509 			pr_warn("server netbiosname longer than 15 truncated\n");
1510 		break;
1511 	case Opt_ver:
1512 		/* version of mount userspace tools, not dialect */
1513 		/* If interface changes in mount.cifs bump to new ver */
1514 		if (strncasecmp(param->string, "1", 1) == 0) {
1515 			if (strlen(param->string) > 1) {
1516 				pr_warn("Bad mount helper ver=%s. Did you want SMB1 (CIFS) dialect and mean to type vers=1.0 instead?\n",
1517 					param->string);
1518 				goto cifs_parse_mount_err;
1519 			}
1520 			/* This is the default */
1521 			break;
1522 		}
1523 		/* For all other value, error */
1524 		pr_warn("Invalid mount helper version specified\n");
1525 		goto cifs_parse_mount_err;
1526 	case Opt_vers:
1527 		/* protocol version (dialect) */
1528 		if (cifs_parse_smb_version(fc, param->string, ctx, is_smb3) != 0)
1529 			goto cifs_parse_mount_err;
1530 		ctx->got_version = true;
1531 		break;
1532 	case Opt_sec:
1533 		if (cifs_parse_security_flavors(fc, param->string, ctx) != 0)
1534 			goto cifs_parse_mount_err;
1535 		break;
1536 	case Opt_cache:
1537 		if (cifs_parse_cache_flavor(fc, param->string, ctx) != 0)
1538 			goto cifs_parse_mount_err;
1539 		break;
1540 	case Opt_witness:
1541 #ifndef CONFIG_CIFS_SWN_UPCALL
1542 		cifs_errorf(fc, "Witness support needs CONFIG_CIFS_SWN_UPCALL config option\n");
1543 			goto cifs_parse_mount_err;
1544 #endif
1545 		ctx->witness = true;
1546 		pr_warn_once("Witness protocol support is experimental\n");
1547 		break;
1548 	case Opt_rootfs:
1549 #ifndef CONFIG_CIFS_ROOT
1550 		cifs_dbg(VFS, "rootfs support requires CONFIG_CIFS_ROOT config option\n");
1551 		goto cifs_parse_mount_err;
1552 #endif
1553 		ctx->rootfs = true;
1554 		break;
1555 	case Opt_posixpaths:
1556 		if (result.negated)
1557 			ctx->posix_paths = 0;
1558 		else
1559 			ctx->posix_paths = 1;
1560 		break;
1561 	case Opt_unix:
1562 		if (result.negated) {
1563 			if (ctx->linux_ext == 1)
1564 				pr_warn_once("conflicting posix mount options specified\n");
1565 			ctx->linux_ext = 0;
1566 			ctx->no_linux_ext = 1;
1567 		} else {
1568 			if (ctx->no_linux_ext == 1)
1569 				pr_warn_once("conflicting posix mount options specified\n");
1570 			ctx->linux_ext = 1;
1571 			ctx->no_linux_ext = 0;
1572 		}
1573 		break;
1574 	case Opt_nocase:
1575 		ctx->nocase = 1;
1576 		break;
1577 	case Opt_brl:
1578 		if (result.negated) {
1579 			/*
1580 			 * turn off mandatory locking in mode
1581 			 * if remote locking is turned off since the
1582 			 * local vfs will do advisory
1583 			 */
1584 			if (ctx->file_mode ==
1585 				(S_IALLUGO & ~(S_ISUID | S_IXGRP)))
1586 				ctx->file_mode = S_IALLUGO;
1587 			ctx->nobrl =  1;
1588 		} else
1589 			ctx->nobrl =  0;
1590 		break;
1591 	case Opt_handlecache:
1592 		if (result.negated)
1593 			ctx->nohandlecache = 1;
1594 		else
1595 			ctx->nohandlecache = 0;
1596 		break;
1597 	case Opt_forcemandatorylock:
1598 		ctx->mand_lock = 1;
1599 		break;
1600 	case Opt_setuids:
1601 		ctx->setuids = result.negated;
1602 		break;
1603 	case Opt_intr:
1604 		ctx->intr = !result.negated;
1605 		break;
1606 	case Opt_setuidfromacl:
1607 		ctx->setuidfromacl = 1;
1608 		break;
1609 	case Opt_strictsync:
1610 		ctx->nostrictsync = result.negated;
1611 		break;
1612 	case Opt_serverino:
1613 		ctx->server_ino = !result.negated;
1614 		break;
1615 	case Opt_rwpidforward:
1616 		ctx->rwpidforward = 1;
1617 		break;
1618 	case Opt_modesid:
1619 		ctx->mode_ace = 1;
1620 		break;
1621 	case Opt_cifsacl:
1622 		ctx->cifs_acl = !result.negated;
1623 		break;
1624 	case Opt_acl:
1625 		ctx->no_psx_acl = result.negated;
1626 		break;
1627 	case Opt_locallease:
1628 		ctx->local_lease = 1;
1629 		break;
1630 	case Opt_sign:
1631 		ctx->sign = true;
1632 		break;
1633 	case Opt_ignore_signature:
1634 		ctx->sign = true;
1635 		ctx->ignore_signature = true;
1636 		break;
1637 	case Opt_seal:
1638 		/* we do not do the following in secFlags because seal
1639 		 * is a per tree connection (mount) not a per socket
1640 		 * or per-smb connection option in the protocol
1641 		 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1642 		 */
1643 		ctx->seal = 1;
1644 		break;
1645 	case Opt_noac:
1646 		pr_warn("Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
1647 		break;
1648 	case Opt_fsc:
1649 #ifndef CONFIG_CIFS_FSCACHE
1650 		cifs_errorf(fc, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
1651 		goto cifs_parse_mount_err;
1652 #endif
1653 		ctx->fsc = true;
1654 		break;
1655 	case Opt_mfsymlinks:
1656 		ctx->mfsymlinks = true;
1657 		break;
1658 	case Opt_multiuser:
1659 		ctx->multiuser = true;
1660 		break;
1661 	case Opt_sloppy:
1662 		ctx->sloppy = true;
1663 		break;
1664 	case Opt_nosharesock:
1665 		ctx->nosharesock = true;
1666 		break;
1667 	case Opt_persistent:
1668 		if (result.negated) {
1669 			ctx->nopersistent = true;
1670 			if (ctx->persistent) {
1671 				cifs_errorf(fc, "persistenthandles mount options conflict\n");
1672 				goto cifs_parse_mount_err;
1673 			}
1674 		} else {
1675 			ctx->persistent = true;
1676 			if ((ctx->nopersistent) || (ctx->resilient)) {
1677 				cifs_errorf(fc, "persistenthandles mount options conflict\n");
1678 				goto cifs_parse_mount_err;
1679 			}
1680 		}
1681 		break;
1682 	case Opt_resilient:
1683 		if (result.negated) {
1684 			ctx->resilient = false; /* already the default */
1685 		} else {
1686 			ctx->resilient = true;
1687 			if (ctx->persistent) {
1688 				cifs_errorf(fc, "persistenthandles mount options conflict\n");
1689 				goto cifs_parse_mount_err;
1690 			}
1691 		}
1692 		break;
1693 	case Opt_tcp_nodelay:
1694 		/* tcp nodelay should not usually be needed since we CORK/UNCORK the socket */
1695 		if (result.negated)
1696 			ctx->sockopt_tcp_nodelay = false;
1697 		else
1698 			ctx->sockopt_tcp_nodelay = true;
1699 		break;
1700 	case Opt_domainauto:
1701 		ctx->domainauto = true;
1702 		break;
1703 	case Opt_rdma:
1704 		ctx->rdma = true;
1705 		break;
1706 	case Opt_reparse:
1707 		if (parse_reparse_flavor(fc, param->string, ctx))
1708 			goto cifs_parse_mount_err;
1709 		break;
1710 	}
1711 	/* case Opt_ignore: - is ignored as expected ... */
1712 
1713 	return 0;
1714 
1715  cifs_parse_mount_err:
1716 	kfree_sensitive(ctx->password);
1717 	ctx->password = NULL;
1718 	kfree_sensitive(ctx->password2);
1719 	ctx->password2 = NULL;
1720 	return -EINVAL;
1721 }
1722 
smb3_init_fs_context(struct fs_context * fc)1723 int smb3_init_fs_context(struct fs_context *fc)
1724 {
1725 	struct smb3_fs_context *ctx;
1726 	char *nodename = utsname()->nodename;
1727 	int i;
1728 
1729 	ctx = kzalloc(sizeof(struct smb3_fs_context), GFP_KERNEL);
1730 	if (unlikely(!ctx))
1731 		return -ENOMEM;
1732 
1733 	strscpy(ctx->workstation_name, nodename, sizeof(ctx->workstation_name));
1734 
1735 	/*
1736 	 * does not have to be perfect mapping since field is
1737 	 * informational, only used for servers that do not support
1738 	 * port 445 and it can be overridden at mount time
1739 	 */
1740 	memset(ctx->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1741 	for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
1742 		ctx->source_rfc1001_name[i] = toupper(nodename[i]);
1743 
1744 	ctx->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
1745 	/*
1746 	 * null target name indicates to use *SMBSERVR default called name
1747 	 *  if we end up sending RFC1001 session initialize
1748 	 */
1749 	ctx->target_rfc1001_name[0] = 0;
1750 	ctx->cred_uid = current_uid();
1751 	ctx->linux_uid = current_uid();
1752 	ctx->linux_gid = current_gid();
1753 	/* By default 4MB read ahead size, 1MB block size */
1754 	ctx->bsize = CIFS_DEFAULT_IOSIZE; /* can improve cp performance significantly */
1755 	ctx->rasize = 0; /* 0 = use default (ie negotiated rsize) for read ahead pages */
1756 
1757 	/*
1758 	 * default to SFM style remapping of seven reserved characters
1759 	 * unless user overrides it or we negotiate CIFS POSIX where
1760 	 * it is unnecessary.  Can not simultaneously use more than one mapping
1761 	 * since then readdir could list files that open could not open
1762 	 */
1763 	ctx->remap = true;
1764 
1765 	/* default to only allowing write access to owner of the mount */
1766 	ctx->dir_mode = ctx->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
1767 
1768 	/* ctx->retry default is 0 (i.e. "soft" limited retry not hard retry) */
1769 	/* default is always to request posix paths. */
1770 	ctx->posix_paths = 1;
1771 	/* default to using server inode numbers where available */
1772 	ctx->server_ino = 1;
1773 
1774 	/* default is to use strict cifs caching semantics */
1775 	ctx->strict_io = true;
1776 
1777 	ctx->acregmax = CIFS_DEF_ACTIMEO;
1778 	ctx->acdirmax = CIFS_DEF_ACTIMEO;
1779 	ctx->closetimeo = SMB3_DEF_DCLOSETIMEO;
1780 	ctx->max_cached_dirs = MAX_CACHED_FIDS;
1781 	/* Most clients set timeout to 0, allows server to use its default */
1782 	ctx->handle_timeout = 0; /* See MS-SMB2 spec section 2.2.14.2.12 */
1783 
1784 	/* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1785 	ctx->ops = &smb30_operations;
1786 	ctx->vals = &smbdefault_values;
1787 
1788 	ctx->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1789 
1790 	/* default to no multichannel (single server connection) */
1791 	ctx->multichannel = false;
1792 	ctx->max_channels = 1;
1793 
1794 	ctx->backupuid_specified = false; /* no backup intent for a user */
1795 	ctx->backupgid_specified = false; /* no backup intent for a group */
1796 
1797 	ctx->retrans = 1;
1798 	ctx->reparse_type = CIFS_REPARSE_TYPE_DEFAULT;
1799 
1800 /*
1801  *	short int override_uid = -1;
1802  *	short int override_gid = -1;
1803  *	char *nodename = strdup(utsname()->nodename);
1804  *	struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
1805  */
1806 
1807 	fc->fs_private = ctx;
1808 	fc->ops = &smb3_fs_context_ops;
1809 	return 0;
1810 }
1811 
1812 void
smb3_cleanup_fs_context_contents(struct smb3_fs_context * ctx)1813 smb3_cleanup_fs_context_contents(struct smb3_fs_context *ctx)
1814 {
1815 	if (ctx == NULL)
1816 		return;
1817 
1818 	/*
1819 	 * Make sure this stays in sync with smb3_fs_context_dup()
1820 	 */
1821 	kfree(ctx->username);
1822 	ctx->username = NULL;
1823 	kfree_sensitive(ctx->password);
1824 	ctx->password = NULL;
1825 	kfree_sensitive(ctx->password2);
1826 	ctx->password2 = NULL;
1827 	kfree(ctx->server_hostname);
1828 	ctx->server_hostname = NULL;
1829 	kfree(ctx->UNC);
1830 	ctx->UNC = NULL;
1831 	kfree(ctx->source);
1832 	ctx->source = NULL;
1833 	kfree(ctx->domainname);
1834 	ctx->domainname = NULL;
1835 	kfree(ctx->nodename);
1836 	ctx->nodename = NULL;
1837 	kfree(ctx->iocharset);
1838 	ctx->iocharset = NULL;
1839 	kfree(ctx->prepath);
1840 	ctx->prepath = NULL;
1841 	kfree(ctx->leaf_fullpath);
1842 	ctx->leaf_fullpath = NULL;
1843 }
1844 
1845 void
smb3_cleanup_fs_context(struct smb3_fs_context * ctx)1846 smb3_cleanup_fs_context(struct smb3_fs_context *ctx)
1847 {
1848 	if (!ctx)
1849 		return;
1850 	smb3_cleanup_fs_context_contents(ctx);
1851 	kfree(ctx);
1852 }
1853 
smb3_update_mnt_flags(struct cifs_sb_info * cifs_sb)1854 void smb3_update_mnt_flags(struct cifs_sb_info *cifs_sb)
1855 {
1856 	struct smb3_fs_context *ctx = cifs_sb->ctx;
1857 
1858 	if (ctx->nodfs)
1859 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_DFS;
1860 	else
1861 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_DFS;
1862 
1863 	if (ctx->noperm)
1864 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
1865 	else
1866 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_PERM;
1867 
1868 	if (ctx->setuids)
1869 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
1870 	else
1871 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SET_UID;
1872 
1873 	if (ctx->setuidfromacl)
1874 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
1875 	else
1876 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_UID_FROM_ACL;
1877 
1878 	if (ctx->server_ino)
1879 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
1880 	else
1881 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
1882 
1883 	if (ctx->remap)
1884 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
1885 	else
1886 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MAP_SFM_CHR;
1887 
1888 	if (ctx->sfu_remap)
1889 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
1890 	else
1891 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MAP_SPECIAL_CHR;
1892 
1893 	if (ctx->no_xattr)
1894 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
1895 	else
1896 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_XATTR;
1897 
1898 	if (ctx->sfu_emul)
1899 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
1900 	else
1901 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_UNX_EMUL;
1902 
1903 	if (ctx->nobrl)
1904 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
1905 	else
1906 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_BRL;
1907 
1908 	if (ctx->nohandlecache)
1909 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
1910 	else
1911 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NO_HANDLE_CACHE;
1912 
1913 	if (ctx->nostrictsync)
1914 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
1915 	else
1916 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NOSSYNC;
1917 
1918 	if (ctx->mand_lock)
1919 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
1920 	else
1921 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_NOPOSIXBRL;
1922 
1923 	if (ctx->rwpidforward)
1924 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
1925 	else
1926 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_RWPIDFORWARD;
1927 
1928 	if (ctx->mode_ace)
1929 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MODE_FROM_SID;
1930 	else
1931 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MODE_FROM_SID;
1932 
1933 	if (ctx->cifs_acl)
1934 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
1935 	else
1936 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_CIFS_ACL;
1937 
1938 	if (ctx->backupuid_specified)
1939 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
1940 	else
1941 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_CIFS_BACKUPUID;
1942 
1943 	if (ctx->backupgid_specified)
1944 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
1945 	else
1946 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_CIFS_BACKUPGID;
1947 
1948 	if (ctx->override_uid)
1949 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
1950 	else
1951 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_OVERR_UID;
1952 
1953 	if (ctx->override_gid)
1954 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
1955 	else
1956 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_OVERR_GID;
1957 
1958 	if (ctx->dynperm)
1959 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
1960 	else
1961 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_DYNPERM;
1962 
1963 	if (ctx->fsc)
1964 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
1965 	else
1966 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_FSCACHE;
1967 
1968 	if (ctx->multiuser)
1969 		cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
1970 					    CIFS_MOUNT_NO_PERM);
1971 	else
1972 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MULTIUSER;
1973 
1974 
1975 	if (ctx->strict_io)
1976 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
1977 	else
1978 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_STRICT_IO;
1979 
1980 	if (ctx->direct_io)
1981 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
1982 	else
1983 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_DIRECT_IO;
1984 
1985 	if (ctx->mfsymlinks)
1986 		cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
1987 	else
1988 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_MF_SYMLINKS;
1989 	if (ctx->mfsymlinks) {
1990 		if (ctx->sfu_emul) {
1991 			/*
1992 			 * Our SFU ("Services for Unix" emulation does not allow
1993 			 * creating symlinks but does allow reading existing SFU
1994 			 * symlinks (it does allow both creating and reading SFU
1995 			 * style mknod and FIFOs though). When "mfsymlinks" and
1996 			 * "sfu" are both enabled at the same time, it allows
1997 			 * reading both types of symlinks, but will only create
1998 			 * them with mfsymlinks format. This allows better
1999 			 * Apple compatibility (probably better for Samba too)
2000 			 * while still recognizing old Windows style symlinks.
2001 			 */
2002 			cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
2003 		}
2004 	}
2005 	cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SHUTDOWN;
2006 
2007 	return;
2008 }
2009