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