Debian Codegraph @

fs/smb/client/fs_context.c

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