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