xref: /openbmc/linux/fs/smb/server/smb_common.c (revision c4c3c32d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
4  *   Copyright (C) 2018 Namjae Jeon <linkinjeon@kernel.org>
5  */
6 
7 #include <linux/user_namespace.h>
8 
9 #include "smb_common.h"
10 #include "server.h"
11 #include "misc.h"
12 #include "smbstatus.h"
13 #include "connection.h"
14 #include "ksmbd_work.h"
15 #include "mgmt/user_session.h"
16 #include "mgmt/user_config.h"
17 #include "mgmt/tree_connect.h"
18 #include "mgmt/share_config.h"
19 
20 /*for shortname implementation */
21 static const char basechars[43] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
22 #define MANGLE_BASE (sizeof(basechars) / sizeof(char) - 1)
23 #define MAGIC_CHAR '~'
24 #define PERIOD '.'
25 #define mangle(V) ((char)(basechars[(V) % MANGLE_BASE]))
26 
27 struct smb_protocol {
28 	int		index;
29 	char		*name;
30 	char		*prot;
31 	__u16		prot_id;
32 };
33 
34 static struct smb_protocol smb1_protos[] = {
35 	{
36 		SMB21_PROT,
37 		"\2SMB 2.1",
38 		"SMB2_10",
39 		SMB21_PROT_ID
40 	},
41 	{
42 		SMB2X_PROT,
43 		"\2SMB 2.???",
44 		"SMB2_22",
45 		SMB2X_PROT_ID
46 	},
47 };
48 
49 static struct smb_protocol smb2_protos[] = {
50 	{
51 		SMB21_PROT,
52 		"\2SMB 2.1",
53 		"SMB2_10",
54 		SMB21_PROT_ID
55 	},
56 	{
57 		SMB30_PROT,
58 		"\2SMB 3.0",
59 		"SMB3_00",
60 		SMB30_PROT_ID
61 	},
62 	{
63 		SMB302_PROT,
64 		"\2SMB 3.02",
65 		"SMB3_02",
66 		SMB302_PROT_ID
67 	},
68 	{
69 		SMB311_PROT,
70 		"\2SMB 3.1.1",
71 		"SMB3_11",
72 		SMB311_PROT_ID
73 	},
74 };
75 
76 unsigned int ksmbd_server_side_copy_max_chunk_count(void)
77 {
78 	return 256;
79 }
80 
81 unsigned int ksmbd_server_side_copy_max_chunk_size(void)
82 {
83 	return (2U << 30) - 1;
84 }
85 
86 unsigned int ksmbd_server_side_copy_max_total_size(void)
87 {
88 	return (2U << 30) - 1;
89 }
90 
91 inline int ksmbd_min_protocol(void)
92 {
93 	return SMB21_PROT;
94 }
95 
96 inline int ksmbd_max_protocol(void)
97 {
98 	return SMB311_PROT;
99 }
100 
101 int ksmbd_lookup_protocol_idx(char *str)
102 {
103 	int offt = ARRAY_SIZE(smb1_protos) - 1;
104 	int len = strlen(str);
105 
106 	while (offt >= 0) {
107 		if (!strncmp(str, smb1_protos[offt].prot, len)) {
108 			ksmbd_debug(SMB, "selected %s dialect idx = %d\n",
109 				    smb1_protos[offt].prot, offt);
110 			return smb1_protos[offt].index;
111 		}
112 		offt--;
113 	}
114 
115 	offt = ARRAY_SIZE(smb2_protos) - 1;
116 	while (offt >= 0) {
117 		if (!strncmp(str, smb2_protos[offt].prot, len)) {
118 			ksmbd_debug(SMB, "selected %s dialect idx = %d\n",
119 				    smb2_protos[offt].prot, offt);
120 			return smb2_protos[offt].index;
121 		}
122 		offt--;
123 	}
124 	return -1;
125 }
126 
127 /**
128  * ksmbd_verify_smb_message() - check for valid smb2 request header
129  * @work:	smb work
130  *
131  * check for valid smb signature and packet direction(request/response)
132  *
133  * Return:      0 on success, otherwise -EINVAL
134  */
135 int ksmbd_verify_smb_message(struct ksmbd_work *work)
136 {
137 	struct smb2_hdr *smb2_hdr = ksmbd_req_buf_next(work);
138 	struct smb_hdr *hdr;
139 
140 	if (smb2_hdr->ProtocolId == SMB2_PROTO_NUMBER)
141 		return ksmbd_smb2_check_message(work);
142 
143 	hdr = work->request_buf;
144 	if (*(__le32 *)hdr->Protocol == SMB1_PROTO_NUMBER &&
145 	    hdr->Command == SMB_COM_NEGOTIATE) {
146 		work->conn->outstanding_credits++;
147 		return 0;
148 	}
149 
150 	return -EINVAL;
151 }
152 
153 /**
154  * ksmbd_smb_request() - check for valid smb request type
155  * @conn:	connection instance
156  *
157  * Return:      true on success, otherwise false
158  */
159 bool ksmbd_smb_request(struct ksmbd_conn *conn)
160 {
161 	__le32 *proto = (__le32 *)smb2_get_msg(conn->request_buf);
162 
163 	if (*proto == SMB2_COMPRESSION_TRANSFORM_ID) {
164 		pr_err_ratelimited("smb2 compression not support yet");
165 		return false;
166 	}
167 
168 	if (*proto != SMB1_PROTO_NUMBER &&
169 	    *proto != SMB2_PROTO_NUMBER &&
170 	    *proto != SMB2_TRANSFORM_PROTO_NUM)
171 		return false;
172 
173 	return true;
174 }
175 
176 static bool supported_protocol(int idx)
177 {
178 	if (idx == SMB2X_PROT &&
179 	    (server_conf.min_protocol >= SMB21_PROT ||
180 	     server_conf.max_protocol <= SMB311_PROT))
181 		return true;
182 
183 	return (server_conf.min_protocol <= idx &&
184 		idx <= server_conf.max_protocol);
185 }
186 
187 static char *next_dialect(char *dialect, int *next_off, int bcount)
188 {
189 	dialect = dialect + *next_off;
190 	*next_off = strnlen(dialect, bcount);
191 	if (dialect[*next_off] != '\0')
192 		return NULL;
193 	return dialect;
194 }
195 
196 static int ksmbd_lookup_dialect_by_name(char *cli_dialects, __le16 byte_count)
197 {
198 	int i, seq_num, bcount, next;
199 	char *dialect;
200 
201 	for (i = ARRAY_SIZE(smb1_protos) - 1; i >= 0; i--) {
202 		seq_num = 0;
203 		next = 0;
204 		dialect = cli_dialects;
205 		bcount = le16_to_cpu(byte_count);
206 		do {
207 			dialect = next_dialect(dialect, &next, bcount);
208 			if (!dialect)
209 				break;
210 			ksmbd_debug(SMB, "client requested dialect %s\n",
211 				    dialect);
212 			if (!strcmp(dialect, smb1_protos[i].name)) {
213 				if (supported_protocol(smb1_protos[i].index)) {
214 					ksmbd_debug(SMB,
215 						    "selected %s dialect\n",
216 						    smb1_protos[i].name);
217 					if (smb1_protos[i].index == SMB1_PROT)
218 						return seq_num;
219 					return smb1_protos[i].prot_id;
220 				}
221 			}
222 			seq_num++;
223 			bcount -= (++next);
224 		} while (bcount > 0);
225 	}
226 
227 	return BAD_PROT_ID;
228 }
229 
230 int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count)
231 {
232 	int i;
233 	int count;
234 
235 	for (i = ARRAY_SIZE(smb2_protos) - 1; i >= 0; i--) {
236 		count = le16_to_cpu(dialects_count);
237 		while (--count >= 0) {
238 			ksmbd_debug(SMB, "client requested dialect 0x%x\n",
239 				    le16_to_cpu(cli_dialects[count]));
240 			if (le16_to_cpu(cli_dialects[count]) !=
241 					smb2_protos[i].prot_id)
242 				continue;
243 
244 			if (supported_protocol(smb2_protos[i].index)) {
245 				ksmbd_debug(SMB, "selected %s dialect\n",
246 					    smb2_protos[i].name);
247 				return smb2_protos[i].prot_id;
248 			}
249 		}
250 	}
251 
252 	return BAD_PROT_ID;
253 }
254 
255 static int ksmbd_negotiate_smb_dialect(void *buf)
256 {
257 	int smb_buf_length = get_rfc1002_len(buf);
258 	__le32 proto = ((struct smb2_hdr *)smb2_get_msg(buf))->ProtocolId;
259 
260 	if (proto == SMB2_PROTO_NUMBER) {
261 		struct smb2_negotiate_req *req;
262 		int smb2_neg_size =
263 			offsetof(struct smb2_negotiate_req, Dialects);
264 
265 		req = (struct smb2_negotiate_req *)smb2_get_msg(buf);
266 		if (smb2_neg_size > smb_buf_length)
267 			goto err_out;
268 
269 		if (struct_size(req, Dialects, le16_to_cpu(req->DialectCount)) >
270 		    smb_buf_length)
271 			goto err_out;
272 
273 		return ksmbd_lookup_dialect_by_id(req->Dialects,
274 						  req->DialectCount);
275 	}
276 
277 	proto = *(__le32 *)((struct smb_hdr *)buf)->Protocol;
278 	if (proto == SMB1_PROTO_NUMBER) {
279 		struct smb_negotiate_req *req;
280 
281 		req = (struct smb_negotiate_req *)buf;
282 		if (le16_to_cpu(req->ByteCount) < 2)
283 			goto err_out;
284 
285 		if (offsetof(struct smb_negotiate_req, DialectsArray) - 4 +
286 			le16_to_cpu(req->ByteCount) > smb_buf_length) {
287 			goto err_out;
288 		}
289 
290 		return ksmbd_lookup_dialect_by_name(req->DialectsArray,
291 						    req->ByteCount);
292 	}
293 
294 err_out:
295 	return BAD_PROT_ID;
296 }
297 
298 #define SMB_COM_NEGOTIATE_EX	0x0
299 
300 /**
301  * get_smb1_cmd_val() - get smb command value from smb header
302  * @work:	smb work containing smb header
303  *
304  * Return:      smb command value
305  */
306 static u16 get_smb1_cmd_val(struct ksmbd_work *work)
307 {
308 	return SMB_COM_NEGOTIATE_EX;
309 }
310 
311 /**
312  * init_smb1_rsp_hdr() - initialize smb negotiate response header
313  * @work:	smb work containing smb request
314  *
315  * Return:      0 on success, otherwise -EINVAL
316  */
317 static int init_smb1_rsp_hdr(struct ksmbd_work *work)
318 {
319 	struct smb_hdr *rsp_hdr = (struct smb_hdr *)work->response_buf;
320 	struct smb_hdr *rcv_hdr = (struct smb_hdr *)work->request_buf;
321 
322 	/*
323 	 * Remove 4 byte direct TCP header.
324 	 */
325 	*(__be32 *)work->response_buf =
326 		cpu_to_be32(sizeof(struct smb_hdr) - 4);
327 
328 	rsp_hdr->Command = SMB_COM_NEGOTIATE;
329 	*(__le32 *)rsp_hdr->Protocol = SMB1_PROTO_NUMBER;
330 	rsp_hdr->Flags = SMBFLG_RESPONSE;
331 	rsp_hdr->Flags2 = SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS |
332 		SMBFLG2_EXT_SEC | SMBFLG2_IS_LONG_NAME;
333 	rsp_hdr->Pid = rcv_hdr->Pid;
334 	rsp_hdr->Mid = rcv_hdr->Mid;
335 	return 0;
336 }
337 
338 /**
339  * smb1_check_user_session() - check for valid session for a user
340  * @work:	smb work containing smb request buffer
341  *
342  * Return:      0 on success, otherwise error
343  */
344 static int smb1_check_user_session(struct ksmbd_work *work)
345 {
346 	unsigned int cmd = work->conn->ops->get_cmd_val(work);
347 
348 	if (cmd == SMB_COM_NEGOTIATE_EX)
349 		return 0;
350 
351 	return -EINVAL;
352 }
353 
354 /**
355  * smb1_allocate_rsp_buf() - allocate response buffer for a command
356  * @work:	smb work containing smb request
357  *
358  * Return:      0 on success, otherwise -ENOMEM
359  */
360 static int smb1_allocate_rsp_buf(struct ksmbd_work *work)
361 {
362 	work->response_buf = kzalloc(MAX_CIFS_SMALL_BUFFER_SIZE,
363 			GFP_KERNEL);
364 	work->response_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
365 
366 	if (!work->response_buf) {
367 		pr_err("Failed to allocate %u bytes buffer\n",
368 				MAX_CIFS_SMALL_BUFFER_SIZE);
369 		return -ENOMEM;
370 	}
371 
372 	return 0;
373 }
374 
375 static struct smb_version_ops smb1_server_ops = {
376 	.get_cmd_val = get_smb1_cmd_val,
377 	.init_rsp_hdr = init_smb1_rsp_hdr,
378 	.allocate_rsp_buf = smb1_allocate_rsp_buf,
379 	.check_user_session = smb1_check_user_session,
380 };
381 
382 static int smb1_negotiate(struct ksmbd_work *work)
383 {
384 	return ksmbd_smb_negotiate_common(work, SMB_COM_NEGOTIATE);
385 }
386 
387 static struct smb_version_cmds smb1_server_cmds[1] = {
388 	[SMB_COM_NEGOTIATE_EX]	= { .proc = smb1_negotiate, },
389 };
390 
391 static int init_smb1_server(struct ksmbd_conn *conn)
392 {
393 	conn->ops = &smb1_server_ops;
394 	conn->cmds = smb1_server_cmds;
395 	conn->max_cmds = ARRAY_SIZE(smb1_server_cmds);
396 	return 0;
397 }
398 
399 int ksmbd_init_smb_server(struct ksmbd_work *work)
400 {
401 	struct ksmbd_conn *conn = work->conn;
402 	__le32 proto;
403 
404 	proto = *(__le32 *)((struct smb_hdr *)work->request_buf)->Protocol;
405 	if (conn->need_neg == false) {
406 		if (proto == SMB1_PROTO_NUMBER)
407 			return -EINVAL;
408 		return 0;
409 	}
410 
411 	if (proto == SMB1_PROTO_NUMBER)
412 		return init_smb1_server(conn);
413 	return init_smb3_11_server(conn);
414 }
415 
416 int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work, int info_level,
417 				      struct ksmbd_file *dir,
418 				      struct ksmbd_dir_info *d_info,
419 				      char *search_pattern,
420 				      int (*fn)(struct ksmbd_conn *, int,
421 						struct ksmbd_dir_info *,
422 						struct ksmbd_kstat *))
423 {
424 	int i, rc = 0;
425 	struct ksmbd_conn *conn = work->conn;
426 	struct mnt_idmap *idmap = file_mnt_idmap(dir->filp);
427 
428 	for (i = 0; i < 2; i++) {
429 		struct kstat kstat;
430 		struct ksmbd_kstat ksmbd_kstat;
431 		struct dentry *dentry;
432 
433 		if (!dir->dot_dotdot[i]) { /* fill dot entry info */
434 			if (i == 0) {
435 				d_info->name = ".";
436 				d_info->name_len = 1;
437 				dentry = dir->filp->f_path.dentry;
438 			} else {
439 				d_info->name = "..";
440 				d_info->name_len = 2;
441 				dentry = dir->filp->f_path.dentry->d_parent;
442 			}
443 
444 			if (!match_pattern(d_info->name, d_info->name_len,
445 					   search_pattern)) {
446 				dir->dot_dotdot[i] = 1;
447 				continue;
448 			}
449 
450 			ksmbd_kstat.kstat = &kstat;
451 			ksmbd_vfs_fill_dentry_attrs(work,
452 						    idmap,
453 						    dentry,
454 						    &ksmbd_kstat);
455 			rc = fn(conn, info_level, d_info, &ksmbd_kstat);
456 			if (rc)
457 				break;
458 			if (d_info->out_buf_len <= 0)
459 				break;
460 
461 			dir->dot_dotdot[i] = 1;
462 			if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
463 				d_info->out_buf_len = 0;
464 				break;
465 			}
466 		}
467 	}
468 
469 	return rc;
470 }
471 
472 /**
473  * ksmbd_extract_shortname() - get shortname from long filename
474  * @conn:	connection instance
475  * @longname:	source long filename
476  * @shortname:	destination short filename
477  *
478  * Return:	shortname length or 0 when source long name is '.' or '..'
479  * TODO: Though this function comforms the restriction of 8.3 Filename spec,
480  * but the result is different with Windows 7's one. need to check.
481  */
482 int ksmbd_extract_shortname(struct ksmbd_conn *conn, const char *longname,
483 			    char *shortname)
484 {
485 	const char *p;
486 	char base[9], extension[4];
487 	char out[13] = {0};
488 	int baselen = 0;
489 	int extlen = 0, len = 0;
490 	unsigned int csum = 0;
491 	const unsigned char *ptr;
492 	bool dot_present = true;
493 
494 	p = longname;
495 	if ((*p == '.') || (!(strcmp(p, "..")))) {
496 		/*no mangling required */
497 		return 0;
498 	}
499 
500 	p = strrchr(longname, '.');
501 	if (p == longname) { /*name starts with a dot*/
502 		strscpy(extension, "___", strlen("___"));
503 	} else {
504 		if (p) {
505 			p++;
506 			while (*p && extlen < 3) {
507 				if (*p != '.')
508 					extension[extlen++] = toupper(*p);
509 				p++;
510 			}
511 			extension[extlen] = '\0';
512 		} else {
513 			dot_present = false;
514 		}
515 	}
516 
517 	p = longname;
518 	if (*p == '.') {
519 		p++;
520 		longname++;
521 	}
522 	while (*p && (baselen < 5)) {
523 		if (*p != '.')
524 			base[baselen++] = toupper(*p);
525 		p++;
526 	}
527 
528 	base[baselen] = MAGIC_CHAR;
529 	memcpy(out, base, baselen + 1);
530 
531 	ptr = longname;
532 	len = strlen(longname);
533 	for (; len > 0; len--, ptr++)
534 		csum += *ptr;
535 
536 	csum = csum % (MANGLE_BASE * MANGLE_BASE);
537 	out[baselen + 1] = mangle(csum / MANGLE_BASE);
538 	out[baselen + 2] = mangle(csum);
539 	out[baselen + 3] = PERIOD;
540 
541 	if (dot_present)
542 		memcpy(out + baselen + 4, extension, 4);
543 	else
544 		out[baselen + 4] = '\0';
545 	smbConvertToUTF16((__le16 *)shortname, out, PATH_MAX,
546 			  conn->local_nls, 0);
547 	len = strlen(out) * 2;
548 	return len;
549 }
550 
551 static int __smb2_negotiate(struct ksmbd_conn *conn)
552 {
553 	return (conn->dialect >= SMB20_PROT_ID &&
554 		conn->dialect <= SMB311_PROT_ID);
555 }
556 
557 static int smb_handle_negotiate(struct ksmbd_work *work)
558 {
559 	struct smb_negotiate_rsp *neg_rsp = work->response_buf;
560 
561 	ksmbd_debug(SMB, "Unsupported SMB1 protocol\n");
562 
563 	/* Add 2 byte bcc and 2 byte DialectIndex. */
564 	inc_rfc1001_len(work->response_buf, 4);
565 	neg_rsp->hdr.Status.CifsError = STATUS_SUCCESS;
566 
567 	neg_rsp->hdr.WordCount = 1;
568 	neg_rsp->DialectIndex = cpu_to_le16(work->conn->dialect);
569 	neg_rsp->ByteCount = 0;
570 	return 0;
571 }
572 
573 int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command)
574 {
575 	struct ksmbd_conn *conn = work->conn;
576 	int ret;
577 
578 	conn->dialect =
579 		ksmbd_negotiate_smb_dialect(work->request_buf);
580 	ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect);
581 
582 	if (command == SMB2_NEGOTIATE_HE) {
583 		ret = smb2_handle_negotiate(work);
584 		return ret;
585 	}
586 
587 	if (command == SMB_COM_NEGOTIATE) {
588 		if (__smb2_negotiate(conn)) {
589 			init_smb3_11_server(conn);
590 			init_smb2_neg_rsp(work);
591 			ksmbd_debug(SMB, "Upgrade to SMB2 negotiation\n");
592 			return 0;
593 		}
594 		return smb_handle_negotiate(work);
595 	}
596 
597 	pr_err("Unknown SMB negotiation command: %u\n", command);
598 	return -EINVAL;
599 }
600 
601 enum SHARED_MODE_ERRORS {
602 	SHARE_DELETE_ERROR,
603 	SHARE_READ_ERROR,
604 	SHARE_WRITE_ERROR,
605 	FILE_READ_ERROR,
606 	FILE_WRITE_ERROR,
607 	FILE_DELETE_ERROR,
608 };
609 
610 static const char * const shared_mode_errors[] = {
611 	"Current access mode does not permit SHARE_DELETE",
612 	"Current access mode does not permit SHARE_READ",
613 	"Current access mode does not permit SHARE_WRITE",
614 	"Desired access mode does not permit FILE_READ",
615 	"Desired access mode does not permit FILE_WRITE",
616 	"Desired access mode does not permit FILE_DELETE",
617 };
618 
619 static void smb_shared_mode_error(int error, struct ksmbd_file *prev_fp,
620 				  struct ksmbd_file *curr_fp)
621 {
622 	ksmbd_debug(SMB, "%s\n", shared_mode_errors[error]);
623 	ksmbd_debug(SMB, "Current mode: 0x%x Desired mode: 0x%x\n",
624 		    prev_fp->saccess, curr_fp->daccess);
625 }
626 
627 int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp)
628 {
629 	int rc = 0;
630 	struct ksmbd_file *prev_fp;
631 
632 	/*
633 	 * Lookup fp in master fp list, and check desired access and
634 	 * shared mode between previous open and current open.
635 	 */
636 	read_lock(&curr_fp->f_ci->m_lock);
637 	list_for_each_entry(prev_fp, &curr_fp->f_ci->m_fp_list, node) {
638 		if (file_inode(filp) != file_inode(prev_fp->filp))
639 			continue;
640 
641 		if (filp == prev_fp->filp)
642 			continue;
643 
644 		if (ksmbd_stream_fd(prev_fp) && ksmbd_stream_fd(curr_fp))
645 			if (strcmp(prev_fp->stream.name, curr_fp->stream.name))
646 				continue;
647 
648 		if (prev_fp->attrib_only != curr_fp->attrib_only)
649 			continue;
650 
651 		if (!(prev_fp->saccess & FILE_SHARE_DELETE_LE) &&
652 		    curr_fp->daccess & FILE_DELETE_LE) {
653 			smb_shared_mode_error(SHARE_DELETE_ERROR,
654 					      prev_fp,
655 					      curr_fp);
656 			rc = -EPERM;
657 			break;
658 		}
659 
660 		/*
661 		 * Only check FILE_SHARE_DELETE if stream opened and
662 		 * normal file opened.
663 		 */
664 		if (ksmbd_stream_fd(prev_fp) && !ksmbd_stream_fd(curr_fp))
665 			continue;
666 
667 		if (!(prev_fp->saccess & FILE_SHARE_READ_LE) &&
668 		    curr_fp->daccess & (FILE_EXECUTE_LE | FILE_READ_DATA_LE)) {
669 			smb_shared_mode_error(SHARE_READ_ERROR,
670 					      prev_fp,
671 					      curr_fp);
672 			rc = -EPERM;
673 			break;
674 		}
675 
676 		if (!(prev_fp->saccess & FILE_SHARE_WRITE_LE) &&
677 		    curr_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE)) {
678 			smb_shared_mode_error(SHARE_WRITE_ERROR,
679 					      prev_fp,
680 					      curr_fp);
681 			rc = -EPERM;
682 			break;
683 		}
684 
685 		if (prev_fp->daccess & (FILE_EXECUTE_LE | FILE_READ_DATA_LE) &&
686 		    !(curr_fp->saccess & FILE_SHARE_READ_LE)) {
687 			smb_shared_mode_error(FILE_READ_ERROR,
688 					      prev_fp,
689 					      curr_fp);
690 			rc = -EPERM;
691 			break;
692 		}
693 
694 		if (prev_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE) &&
695 		    !(curr_fp->saccess & FILE_SHARE_WRITE_LE)) {
696 			smb_shared_mode_error(FILE_WRITE_ERROR,
697 					      prev_fp,
698 					      curr_fp);
699 			rc = -EPERM;
700 			break;
701 		}
702 
703 		if (prev_fp->daccess & FILE_DELETE_LE &&
704 		    !(curr_fp->saccess & FILE_SHARE_DELETE_LE)) {
705 			smb_shared_mode_error(FILE_DELETE_ERROR,
706 					      prev_fp,
707 					      curr_fp);
708 			rc = -EPERM;
709 			break;
710 		}
711 	}
712 	read_unlock(&curr_fp->f_ci->m_lock);
713 
714 	return rc;
715 }
716 
717 bool is_asterisk(char *p)
718 {
719 	return p && p[0] == '*';
720 }
721 
722 int ksmbd_override_fsids(struct ksmbd_work *work)
723 {
724 	struct ksmbd_session *sess = work->sess;
725 	struct ksmbd_share_config *share = work->tcon->share_conf;
726 	struct cred *cred;
727 	struct group_info *gi;
728 	unsigned int uid;
729 	unsigned int gid;
730 
731 	uid = user_uid(sess->user);
732 	gid = user_gid(sess->user);
733 	if (share->force_uid != KSMBD_SHARE_INVALID_UID)
734 		uid = share->force_uid;
735 	if (share->force_gid != KSMBD_SHARE_INVALID_GID)
736 		gid = share->force_gid;
737 
738 	cred = prepare_kernel_cred(&init_task);
739 	if (!cred)
740 		return -ENOMEM;
741 
742 	cred->fsuid = make_kuid(&init_user_ns, uid);
743 	cred->fsgid = make_kgid(&init_user_ns, gid);
744 
745 	gi = groups_alloc(0);
746 	if (!gi) {
747 		abort_creds(cred);
748 		return -ENOMEM;
749 	}
750 	set_groups(cred, gi);
751 	put_group_info(gi);
752 
753 	if (!uid_eq(cred->fsuid, GLOBAL_ROOT_UID))
754 		cred->cap_effective = cap_drop_fs_set(cred->cap_effective);
755 
756 	WARN_ON(work->saved_cred);
757 	work->saved_cred = override_creds(cred);
758 	if (!work->saved_cred) {
759 		abort_creds(cred);
760 		return -EINVAL;
761 	}
762 	return 0;
763 }
764 
765 void ksmbd_revert_fsids(struct ksmbd_work *work)
766 {
767 	const struct cred *cred;
768 
769 	WARN_ON(!work->saved_cred);
770 
771 	cred = current_cred();
772 	revert_creds(work->saved_cred);
773 	put_cred(cred);
774 	work->saved_cred = NULL;
775 }
776 
777 __le32 smb_map_generic_desired_access(__le32 daccess)
778 {
779 	if (daccess & FILE_GENERIC_READ_LE) {
780 		daccess |= cpu_to_le32(GENERIC_READ_FLAGS);
781 		daccess &= ~FILE_GENERIC_READ_LE;
782 	}
783 
784 	if (daccess & FILE_GENERIC_WRITE_LE) {
785 		daccess |= cpu_to_le32(GENERIC_WRITE_FLAGS);
786 		daccess &= ~FILE_GENERIC_WRITE_LE;
787 	}
788 
789 	if (daccess & FILE_GENERIC_EXECUTE_LE) {
790 		daccess |= cpu_to_le32(GENERIC_EXECUTE_FLAGS);
791 		daccess &= ~FILE_GENERIC_EXECUTE_LE;
792 	}
793 
794 	if (daccess & FILE_GENERIC_ALL_LE) {
795 		daccess |= cpu_to_le32(GENERIC_ALL_FLAGS);
796 		daccess &= ~FILE_GENERIC_ALL_LE;
797 	}
798 
799 	return daccess;
800 }
801