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