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