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