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