smb_common.c 18.1 KB
Newer Older
1 2 3 4 5 6
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
 *   Copyright (C) 2018 Namjae Jeon <linkinjeon@kernel.org>
 */

7 8
#include <linux/user_namespace.h>

9 10 11 12 13 14 15 16 17 18 19 20 21
#include "smb_common.h"
#include "server.h"
#include "misc.h"
#include "smbstatus.h"
#include "connection.h"
#include "ksmbd_work.h"
#include "mgmt/user_session.h"
#include "mgmt/user_config.h"
#include "mgmt/tree_connect.h"
#include "mgmt/share_config.h"

/*for shortname implementation */
static const char basechars[43] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%";
22
#define MANGLE_BASE (sizeof(basechars) / sizeof(char) - 1)
23 24 25 26 27 28 29 30 31 32 33
#define MAGIC_CHAR '~'
#define PERIOD '.'
#define mangle(V) ((char)(basechars[(V) % MANGLE_BASE]))

struct smb_protocol {
	int		index;
	char		*name;
	char		*prot;
	__u16		prot_id;
};

34
static struct smb_protocol smb1_protos[] = {
35 36 37 38 39 40 41 42 43 44 45 46
	{
		SMB21_PROT,
		"\2SMB 2.1",
		"SMB2_10",
		SMB21_PROT_ID
	},
	{
		SMB2X_PROT,
		"\2SMB 2.???",
		"SMB2_22",
		SMB2X_PROT_ID
	},
47 48 49 50 51 52 53 54 55
};

static struct smb_protocol smb2_protos[] = {
	{
		SMB21_PROT,
		"\2SMB 2.1",
		"SMB2_10",
		SMB21_PROT_ID
	},
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	{
		SMB30_PROT,
		"\2SMB 3.0",
		"SMB3_00",
		SMB30_PROT_ID
	},
	{
		SMB302_PROT,
		"\2SMB 3.02",
		"SMB3_02",
		SMB302_PROT_ID
	},
	{
		SMB311_PROT,
		"\2SMB 3.1.1",
		"SMB3_11",
		SMB311_PROT_ID
	},
};

unsigned int ksmbd_server_side_copy_max_chunk_count(void)
{
	return 256;
}

unsigned int ksmbd_server_side_copy_max_chunk_size(void)
{
	return (2U << 30) - 1;
}

unsigned int ksmbd_server_side_copy_max_total_size(void)
{
	return (2U << 30) - 1;
}

inline int ksmbd_min_protocol(void)
{
93
	return SMB21_PROT;
94 95 96 97 98 99 100 101 102
}

inline int ksmbd_max_protocol(void)
{
	return SMB311_PROT;
}

int ksmbd_lookup_protocol_idx(char *str)
{
103
	int offt = ARRAY_SIZE(smb1_protos) - 1;
104 105 106
	int len = strlen(str);

	while (offt >= 0) {
107 108 109 110 111 112 113 114 115 116 117
		if (!strncmp(str, smb1_protos[offt].prot, len)) {
			ksmbd_debug(SMB, "selected %s dialect idx = %d\n",
				    smb1_protos[offt].prot, offt);
			return smb1_protos[offt].index;
		}
		offt--;
	}

	offt = ARRAY_SIZE(smb2_protos) - 1;
	while (offt >= 0) {
		if (!strncmp(str, smb2_protos[offt].prot, len)) {
118
			ksmbd_debug(SMB, "selected %s dialect idx = %d\n",
119 120
				    smb2_protos[offt].prot, offt);
			return smb2_protos[offt].index;
121 122 123 124 125 126 127
		}
		offt--;
	}
	return -1;
}

/**
H
Hyunchul Lee 已提交
128 129
 * ksmbd_verify_smb_message() - check for valid smb2 request header
 * @work:	smb work
130 131 132
 *
 * check for valid smb signature and packet direction(request/response)
 *
133
 * Return:      0 on success, otherwise -EINVAL
134 135 136
 */
int ksmbd_verify_smb_message(struct ksmbd_work *work)
{
137
	struct smb2_hdr *smb2_hdr = ksmbd_req_buf_next(work);
138
	struct smb_hdr *hdr;
139 140 141 142

	if (smb2_hdr->ProtocolId == SMB2_PROTO_NUMBER)
		return ksmbd_smb2_check_message(work);

143 144
	hdr = work->request_buf;
	if (*(__le32 *)hdr->Protocol == SMB1_PROTO_NUMBER &&
145 146
	    hdr->Command == SMB_COM_NEGOTIATE) {
		work->conn->outstanding_credits++;
147
		return 0;
148
	}
149 150

	return -EINVAL;
151 152 153
}

/**
H
Hyunchul Lee 已提交
154
 * ksmbd_smb_request() - check for valid smb request type
155 156 157 158 159 160
 * @conn:	connection instance
 *
 * Return:      true on success, otherwise false
 */
bool ksmbd_smb_request(struct ksmbd_conn *conn)
{
161 162 163 164 165 166 167 168 169 170 171 172 173
	__le32 *proto = (__le32 *)smb2_get_msg(conn->request_buf);

	if (*proto == SMB2_COMPRESSION_TRANSFORM_ID) {
		pr_err_ratelimited("smb2 compression not support yet");
		return false;
	}

	if (*proto != SMB1_PROTO_NUMBER &&
	    *proto != SMB2_PROTO_NUMBER &&
	    *proto != SMB2_TRANSFORM_PROTO_NUM)
		return false;

	return true;
174 175 176 177 178 179 180 181 182 183
}

static bool supported_protocol(int idx)
{
	if (idx == SMB2X_PROT &&
	    (server_conf.min_protocol >= SMB21_PROT ||
	     server_conf.max_protocol <= SMB311_PROT))
		return true;

	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 191 192
	*next_off = strnlen(dialect, bcount);
	if (dialect[*next_off] != '\0')
		return NULL;
193 194 195 196 197 198 199 200
	return dialect;
}

static int ksmbd_lookup_dialect_by_name(char *cli_dialects, __le16 byte_count)
{
	int i, seq_num, bcount, next;
	char *dialect;

201
	for (i = ARRAY_SIZE(smb1_protos) - 1; i >= 0; i--) {
202 203 204 205 206
		seq_num = 0;
		next = 0;
		dialect = cli_dialects;
		bcount = le16_to_cpu(byte_count);
		do {
207 208 209
			dialect = next_dialect(dialect, &next, bcount);
			if (!dialect)
				break;
210
			ksmbd_debug(SMB, "client requested dialect %s\n",
211
				    dialect);
212 213
			if (!strcmp(dialect, smb1_protos[i].name)) {
				if (supported_protocol(smb1_protos[i].index)) {
214
					ksmbd_debug(SMB,
215
						    "selected %s dialect\n",
216 217
						    smb1_protos[i].name);
					if (smb1_protos[i].index == SMB1_PROT)
218
						return seq_num;
219
					return smb1_protos[i].prot_id;
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
				}
			}
			seq_num++;
			bcount -= (++next);
		} while (bcount > 0);
	}

	return BAD_PROT_ID;
}

int ksmbd_lookup_dialect_by_id(__le16 *cli_dialects, __le16 dialects_count)
{
	int i;
	int count;

235
	for (i = ARRAY_SIZE(smb2_protos) - 1; i >= 0; i--) {
236 237 238
		count = le16_to_cpu(dialects_count);
		while (--count >= 0) {
			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 243
				continue;

244
			if (supported_protocol(smb2_protos[i].index)) {
245
				ksmbd_debug(SMB, "selected %s dialect\n",
246 247
					    smb2_protos[i].name);
				return smb2_protos[i].prot_id;
248 249 250 251 252 253 254
			}
		}
	}

	return BAD_PROT_ID;
}

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 261

	if (proto == SMB2_PROTO_NUMBER) {
		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 267 268 269 270 271 272
		if (smb2_neg_size > smb_buf_length)
			goto err_out;

		if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) >
		    smb_buf_length)
			goto err_out;

273 274 275 276 277 278 279 280 281
		return ksmbd_lookup_dialect_by_id(req->Dialects,
						  req->DialectCount);
	}

	proto = *(__le32 *)((struct smb_hdr *)buf)->Protocol;
	if (proto == SMB1_PROTO_NUMBER) {
		struct smb_negotiate_req *req;

		req = (struct smb_negotiate_req *)buf;
282 283 284 285 286 287 288 289
		if (le16_to_cpu(req->ByteCount) < 2)
			goto err_out;

		if (offsetof(struct smb_negotiate_req, DialectsArray) - 4 +
			le16_to_cpu(req->ByteCount) > smb_buf_length) {
			goto err_out;
		}

290 291 292 293
		return ksmbd_lookup_dialect_by_name(req->DialectsArray,
						    req->ByteCount);
	}

294
err_out:
295 296 297
	return BAD_PROT_ID;
}

298 299 300 301 302 303 304 305 306
#define SMB_COM_NEGOTIATE_EX	0x0

/**
 * get_smb1_cmd_val() - get smb command value from smb header
 * @work:	smb work containing smb header
 *
 * Return:      smb command value
 */
static u16 get_smb1_cmd_val(struct ksmbd_work *work)
307
{
308 309
	return SMB_COM_NEGOTIATE_EX;
}
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
/**
 * init_smb1_rsp_hdr() - initialize smb negotiate response header
 * @work:	smb work containing smb request
 *
 * Return:      0 on success, otherwise -EINVAL
 */
static int init_smb1_rsp_hdr(struct ksmbd_work *work)
{
	struct smb_hdr *rsp_hdr = (struct smb_hdr *)work->response_buf;
	struct smb_hdr *rcv_hdr = (struct smb_hdr *)work->request_buf;

	/*
	 * Remove 4 byte direct TCP header.
	 */
	*(__be32 *)work->response_buf =
		cpu_to_be32(sizeof(struct smb_hdr) - 4);

	rsp_hdr->Command = SMB_COM_NEGOTIATE;
	*(__le32 *)rsp_hdr->Protocol = SMB1_PROTO_NUMBER;
	rsp_hdr->Flags = SMBFLG_RESPONSE;
	rsp_hdr->Flags2 = SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS |
		SMBFLG2_EXT_SEC | SMBFLG2_IS_LONG_NAME;
	rsp_hdr->Pid = rcv_hdr->Pid;
	rsp_hdr->Mid = rcv_hdr->Mid;
	return 0;
}

/**
 * smb1_check_user_session() - check for valid session for a user
 * @work:	smb work containing smb request buffer
 *
 * Return:      0 on success, otherwise error
 */
static int smb1_check_user_session(struct ksmbd_work *work)
{
	unsigned int cmd = work->conn->ops->get_cmd_val(work);

	if (cmd == SMB_COM_NEGOTIATE_EX)
349 350
		return 0;

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
	return -EINVAL;
}

/**
 * smb1_allocate_rsp_buf() - allocate response buffer for a command
 * @work:	smb work containing smb request
 *
 * Return:      0 on success, otherwise -ENOMEM
 */
static int smb1_allocate_rsp_buf(struct ksmbd_work *work)
{
	work->response_buf = kmalloc(MAX_CIFS_SMALL_BUFFER_SIZE,
			GFP_KERNEL | __GFP_ZERO);
	work->response_sz = MAX_CIFS_SMALL_BUFFER_SIZE;

	if (!work->response_buf) {
		pr_err("Failed to allocate %u bytes buffer\n",
				MAX_CIFS_SMALL_BUFFER_SIZE);
		return -ENOMEM;
	}
371 372 373 374

	return 0;
}

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
static struct smb_version_ops smb1_server_ops = {
	.get_cmd_val = get_smb1_cmd_val,
	.init_rsp_hdr = init_smb1_rsp_hdr,
	.allocate_rsp_buf = smb1_allocate_rsp_buf,
	.check_user_session = smb1_check_user_session,
};

static int smb1_negotiate(struct ksmbd_work *work)
{
	return ksmbd_smb_negotiate_common(work, SMB_COM_NEGOTIATE);
}

static struct smb_version_cmds smb1_server_cmds[1] = {
	[SMB_COM_NEGOTIATE_EX]	= { .proc = smb1_negotiate, },
};

static void init_smb1_server(struct ksmbd_conn *conn)
{
	conn->ops = &smb1_server_ops;
	conn->cmds = smb1_server_cmds;
	conn->max_cmds = ARRAY_SIZE(smb1_server_cmds);
}

void ksmbd_init_smb_server(struct ksmbd_work *work)
{
	struct ksmbd_conn *conn = work->conn;
	__le32 proto;

	if (conn->need_neg == false)
		return;

	proto = *(__le32 *)((struct smb_hdr *)work->request_buf)->Protocol;
	if (proto == SMB1_PROTO_NUMBER)
		init_smb1_server(conn);
	else
		init_smb3_11_server(conn);
}

413
int ksmbd_populate_dot_dotdot_entries(struct ksmbd_work *work, int info_level,
414 415 416 417 418 419
				      struct ksmbd_file *dir,
				      struct ksmbd_dir_info *d_info,
				      char *search_pattern,
				      int (*fn)(struct ksmbd_conn *, int,
						struct ksmbd_dir_info *,
						struct ksmbd_kstat *))
420 421 422
{
	int i, rc = 0;
	struct ksmbd_conn *conn = work->conn;
423
	struct mnt_idmap *idmap = file_mnt_idmap(dir->filp);
424 425 426 427

	for (i = 0; i < 2; i++) {
		struct kstat kstat;
		struct ksmbd_kstat ksmbd_kstat;
428
		struct dentry *dentry;
429 430 431 432 433

		if (!dir->dot_dotdot[i]) { /* fill dot entry info */
			if (i == 0) {
				d_info->name = ".";
				d_info->name_len = 1;
434
				dentry = dir->filp->f_path.dentry;
435 436 437
			} else {
				d_info->name = "..";
				d_info->name_len = 2;
438
				dentry = dir->filp->f_path.dentry->d_parent;
439 440
			}

441
			if (!match_pattern(d_info->name, d_info->name_len,
442
					   search_pattern)) {
443 444 445 446 447 448
				dir->dot_dotdot[i] = 1;
				continue;
			}

			ksmbd_kstat.kstat = &kstat;
			ksmbd_vfs_fill_dentry_attrs(work,
449
						    idmap,
450
						    dentry,
451
						    &ksmbd_kstat);
452
			rc = fn(conn, info_level, d_info, &ksmbd_kstat);
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
			if (rc)
				break;
			if (d_info->out_buf_len <= 0)
				break;

			dir->dot_dotdot[i] = 1;
			if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
				d_info->out_buf_len = 0;
				break;
			}
		}
	}

	return rc;
}

/**
 * ksmbd_extract_shortname() - get shortname from long filename
 * @conn:	connection instance
 * @longname:	source long filename
 * @shortname:	destination short filename
 *
 * Return:	shortname length or 0 when source long name is '.' or '..'
 * TODO: Though this function comforms the restriction of 8.3 Filename spec,
 * but the result is different with Windows 7's one. need to check.
 */
479
int ksmbd_extract_shortname(struct ksmbd_conn *conn, const char *longname,
480
			    char *shortname)
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
{
	const char *p;
	char base[9], extension[4];
	char out[13] = {0};
	int baselen = 0;
	int extlen = 0, len = 0;
	unsigned int csum = 0;
	const unsigned char *ptr;
	bool dot_present = true;

	p = longname;
	if ((*p == '.') || (!(strcmp(p, "..")))) {
		/*no mangling required */
		return 0;
	}

	p = strrchr(longname, '.');
	if (p == longname) { /*name starts with a dot*/
		strscpy(extension, "___", strlen("___"));
	} else {
501
		if (p) {
502 503 504 505 506 507 508
			p++;
			while (*p && extlen < 3) {
				if (*p != '.')
					extension[extlen++] = toupper(*p);
				p++;
			}
			extension[extlen] = '\0';
509
		} else {
510
			dot_present = false;
511
		}
512 513 514 515 516 517 518 519 520 521 522 523 524 525
	}

	p = longname;
	if (*p == '.') {
		p++;
		longname++;
	}
	while (*p && (baselen < 5)) {
		if (*p != '.')
			base[baselen++] = toupper(*p);
		p++;
	}

	base[baselen] = MAGIC_CHAR;
526
	memcpy(out, base, baselen + 1);
527 528 529 530 531 532 533

	ptr = longname;
	len = strlen(longname);
	for (; len > 0; len--, ptr++)
		csum += *ptr;

	csum = csum % (MANGLE_BASE * MANGLE_BASE);
534 535 536
	out[baselen + 1] = mangle(csum / MANGLE_BASE);
	out[baselen + 2] = mangle(csum);
	out[baselen + 3] = PERIOD;
537 538

	if (dot_present)
539
		memcpy(&out[baselen + 4], extension, 4);
540
	else
541
		out[baselen + 4] = '\0';
542
	smbConvertToUTF16((__le16 *)shortname, out, PATH_MAX,
543
			  conn->local_nls, 0);
544 545 546 547 548 549
	len = strlen(out) * 2;
	return len;
}

static int __smb2_negotiate(struct ksmbd_conn *conn)
{
550
	return (conn->dialect >= SMB20_PROT_ID &&
551
		conn->dialect <= SMB311_PROT_ID);
552 553 554 555
}

static int smb_handle_negotiate(struct ksmbd_work *work)
{
N
Namjae Jeon 已提交
556
	struct smb_negotiate_rsp *neg_rsp = work->response_buf;
557

558 559
	ksmbd_debug(SMB, "Unsupported SMB1 protocol\n");

560 561
	/* Add 2 byte bcc and 2 byte DialectIndex. */
	inc_rfc1001_len(work->response_buf, 4);
562 563 564 565 566 567
	neg_rsp->hdr.Status.CifsError = STATUS_SUCCESS;

	neg_rsp->hdr.WordCount = 1;
	neg_rsp->DialectIndex = cpu_to_le16(work->conn->dialect);
	neg_rsp->ByteCount = 0;
	return 0;
568 569 570 571 572 573 574
}

int ksmbd_smb_negotiate_common(struct ksmbd_work *work, unsigned int command)
{
	struct ksmbd_conn *conn = work->conn;
	int ret;

575 576
	conn->dialect =
		ksmbd_negotiate_smb_dialect(work->request_buf);
577 578
	ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect);

579
	if (command == SMB2_NEGOTIATE_HE) {
580 581 582 583 584 585 586 587 588 589 590 591 592 593
		ret = smb2_handle_negotiate(work);
		return ret;
	}

	if (command == SMB_COM_NEGOTIATE) {
		if (__smb2_negotiate(conn)) {
			init_smb3_11_server(conn);
			init_smb2_neg_rsp(work);
			ksmbd_debug(SMB, "Upgrade to SMB2 negotiation\n");
			return 0;
		}
		return smb_handle_negotiate(work);
	}

N
Namjae Jeon 已提交
594
	pr_err("Unknown SMB negotiation command: %u\n", command);
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
	return -EINVAL;
}

enum SHARED_MODE_ERRORS {
	SHARE_DELETE_ERROR,
	SHARE_READ_ERROR,
	SHARE_WRITE_ERROR,
	FILE_READ_ERROR,
	FILE_WRITE_ERROR,
	FILE_DELETE_ERROR,
};

static const char * const shared_mode_errors[] = {
	"Current access mode does not permit SHARE_DELETE",
	"Current access mode does not permit SHARE_READ",
	"Current access mode does not permit SHARE_WRITE",
	"Desired access mode does not permit FILE_READ",
	"Desired access mode does not permit FILE_WRITE",
	"Desired access mode does not permit FILE_DELETE",
};

616
static void smb_shared_mode_error(int error, struct ksmbd_file *prev_fp,
617
				  struct ksmbd_file *curr_fp)
618 619 620
{
	ksmbd_debug(SMB, "%s\n", shared_mode_errors[error]);
	ksmbd_debug(SMB, "Current mode: 0x%x Desired mode: 0x%x\n",
621
		    prev_fp->saccess, curr_fp->daccess);
622 623 624 625 626 627 628 629 630 631 632 633
}

int ksmbd_smb_check_shared_mode(struct file *filp, struct ksmbd_file *curr_fp)
{
	int rc = 0;
	struct ksmbd_file *prev_fp;

	/*
	 * Lookup fp in master fp list, and check desired access and
	 * shared mode between previous open and current open.
	 */
	read_lock(&curr_fp->f_ci->m_lock);
634
	list_for_each_entry(prev_fp, &curr_fp->f_ci->m_fp_list, node) {
635
		if (file_inode(filp) != file_inode(prev_fp->filp))
636 637 638 639 640 641 642 643 644 645 646 647 648
			continue;

		if (filp == prev_fp->filp)
			continue;

		if (ksmbd_stream_fd(prev_fp) && ksmbd_stream_fd(curr_fp))
			if (strcmp(prev_fp->stream.name, curr_fp->stream.name))
				continue;

		if (prev_fp->attrib_only != curr_fp->attrib_only)
			continue;

		if (!(prev_fp->saccess & FILE_SHARE_DELETE_LE) &&
649
		    curr_fp->daccess & FILE_DELETE_LE) {
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
			smb_shared_mode_error(SHARE_DELETE_ERROR,
					      prev_fp,
					      curr_fp);
			rc = -EPERM;
			break;
		}

		/*
		 * Only check FILE_SHARE_DELETE if stream opened and
		 * normal file opened.
		 */
		if (ksmbd_stream_fd(prev_fp) && !ksmbd_stream_fd(curr_fp))
			continue;

		if (!(prev_fp->saccess & FILE_SHARE_READ_LE) &&
665
		    curr_fp->daccess & (FILE_EXECUTE_LE | FILE_READ_DATA_LE)) {
666 667 668 669 670 671 672 673
			smb_shared_mode_error(SHARE_READ_ERROR,
					      prev_fp,
					      curr_fp);
			rc = -EPERM;
			break;
		}

		if (!(prev_fp->saccess & FILE_SHARE_WRITE_LE) &&
674
		    curr_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE)) {
675 676 677 678 679 680 681
			smb_shared_mode_error(SHARE_WRITE_ERROR,
					      prev_fp,
					      curr_fp);
			rc = -EPERM;
			break;
		}

682 683
		if (prev_fp->daccess & (FILE_EXECUTE_LE | FILE_READ_DATA_LE) &&
		    !(curr_fp->saccess & FILE_SHARE_READ_LE)) {
684 685 686 687 688 689 690
			smb_shared_mode_error(FILE_READ_ERROR,
					      prev_fp,
					      curr_fp);
			rc = -EPERM;
			break;
		}

691 692
		if (prev_fp->daccess & (FILE_WRITE_DATA_LE | FILE_APPEND_DATA_LE) &&
		    !(curr_fp->saccess & FILE_SHARE_WRITE_LE)) {
693 694 695 696 697 698 699 700
			smb_shared_mode_error(FILE_WRITE_ERROR,
					      prev_fp,
					      curr_fp);
			rc = -EPERM;
			break;
		}

		if (prev_fp->daccess & FILE_DELETE_LE &&
701
		    !(curr_fp->saccess & FILE_SHARE_DELETE_LE)) {
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
			smb_shared_mode_error(FILE_DELETE_ERROR,
					      prev_fp,
					      curr_fp);
			rc = -EPERM;
			break;
		}
	}
	read_unlock(&curr_fp->f_ci->m_lock);

	return rc;
}

bool is_asterisk(char *p)
{
	return p && p[0] == '*';
}

int ksmbd_override_fsids(struct ksmbd_work *work)
{
	struct ksmbd_session *sess = work->sess;
	struct ksmbd_share_config *share = work->tcon->share_conf;
	struct cred *cred;
	struct group_info *gi;
	unsigned int uid;
	unsigned int gid;

	uid = user_uid(sess->user);
	gid = user_gid(sess->user);
	if (share->force_uid != KSMBD_SHARE_INVALID_UID)
		uid = share->force_uid;
	if (share->force_gid != KSMBD_SHARE_INVALID_GID)
		gid = share->force_gid;

735
	cred = prepare_kernel_cred(&init_task);
736 737 738
	if (!cred)
		return -ENOMEM;

739 740
	cred->fsuid = make_kuid(&init_user_ns, uid);
	cred->fsgid = make_kgid(&init_user_ns, gid);
741 742 743 744 745 746 747 748 749 750 751 752

	gi = groups_alloc(0);
	if (!gi) {
		abort_creds(cred);
		return -ENOMEM;
	}
	set_groups(cred, gi);
	put_group_info(gi);

	if (!uid_eq(cred->fsuid, GLOBAL_ROOT_UID))
		cred->cap_effective = cap_drop_fs_set(cred->cap_effective);

753
	WARN_ON(work->saved_cred);
754 755 756 757 758 759 760 761 762 763 764 765
	work->saved_cred = override_creds(cred);
	if (!work->saved_cred) {
		abort_creds(cred);
		return -EINVAL;
	}
	return 0;
}

void ksmbd_revert_fsids(struct ksmbd_work *work)
{
	const struct cred *cred;

766
	WARN_ON(!work->saved_cred);
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797

	cred = current_cred();
	revert_creds(work->saved_cred);
	put_cred(cred);
	work->saved_cred = NULL;
}

__le32 smb_map_generic_desired_access(__le32 daccess)
{
	if (daccess & FILE_GENERIC_READ_LE) {
		daccess |= cpu_to_le32(GENERIC_READ_FLAGS);
		daccess &= ~FILE_GENERIC_READ_LE;
	}

	if (daccess & FILE_GENERIC_WRITE_LE) {
		daccess |= cpu_to_le32(GENERIC_WRITE_FLAGS);
		daccess &= ~FILE_GENERIC_WRITE_LE;
	}

	if (daccess & FILE_GENERIC_EXECUTE_LE) {
		daccess |= cpu_to_le32(GENERIC_EXECUTE_FLAGS);
		daccess &= ~FILE_GENERIC_EXECUTE_LE;
	}

	if (daccess & FILE_GENERIC_ALL_LE) {
		daccess |= cpu_to_le32(GENERIC_ALL_FLAGS);
		daccess &= ~FILE_GENERIC_ALL_LE;
	}

	return daccess;
}