1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org> 4 * Copyright (C) 2018 Samsung Electronics Co., Ltd. 5 */ 6 7 #include <linux/inetdevice.h> 8 #include <net/addrconf.h> 9 #include <linux/syscalls.h> 10 #include <linux/namei.h> 11 #include <linux/statfs.h> 12 #include <linux/ethtool.h> 13 #include <linux/falloc.h> 14 #include <linux/mount.h> 15 #include <linux/filelock.h> 16 17 #include "glob.h" 18 #include "smbfsctl.h" 19 #include "oplock.h" 20 #include "smbacl.h" 21 22 #include "auth.h" 23 #include "asn1.h" 24 #include "connection.h" 25 #include "transport_ipc.h" 26 #include "transport_rdma.h" 27 #include "vfs.h" 28 #include "vfs_cache.h" 29 #include "misc.h" 30 31 #include "server.h" 32 #include "smb_common.h" 33 #include "smbstatus.h" 34 #include "ksmbd_work.h" 35 #include "mgmt/user_config.h" 36 #include "mgmt/share_config.h" 37 #include "mgmt/tree_connect.h" 38 #include "mgmt/user_session.h" 39 #include "mgmt/ksmbd_ida.h" 40 #include "ndr.h" 41 42 static void __wbuf(struct ksmbd_work *work, void **req, void **rsp) 43 { 44 if (work->next_smb2_rcv_hdr_off) { 45 *req = ksmbd_req_buf_next(work); 46 *rsp = ksmbd_resp_buf_next(work); 47 } else { 48 *req = smb2_get_msg(work->request_buf); 49 *rsp = smb2_get_msg(work->response_buf); 50 } 51 } 52 53 #define WORK_BUFFERS(w, rq, rs) __wbuf((w), (void **)&(rq), (void **)&(rs)) 54 55 /** 56 * check_session_id() - check for valid session id in smb header 57 * @conn: connection instance 58 * @id: session id from smb header 59 * 60 * Return: 1 if valid session id, otherwise 0 61 */ 62 static inline bool check_session_id(struct ksmbd_conn *conn, u64 id) 63 { 64 struct ksmbd_session *sess; 65 66 if (id == 0 || id == -1) 67 return false; 68 69 sess = ksmbd_session_lookup_all(conn, id); 70 if (sess) { 71 ksmbd_user_session_put(sess); 72 return true; 73 } 74 pr_err("Invalid user session id: %llu\n", id); 75 return false; 76 } 77 78 struct channel *lookup_chann_list(struct ksmbd_session *sess, struct ksmbd_conn *conn) 79 { 80 return xa_load(&sess->ksmbd_chann_list, (long)conn); 81 } 82 83 /** 84 * smb2_get_ksmbd_tcon() - get tree connection information using a tree id. 85 * @work: smb work 86 * 87 * Return: 0 if there is a tree connection matched or these are 88 * skipable commands, otherwise error 89 */ 90 int smb2_get_ksmbd_tcon(struct ksmbd_work *work) 91 { 92 struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work); 93 unsigned int cmd = le16_to_cpu(req_hdr->Command); 94 unsigned int tree_id; 95 96 if (cmd == SMB2_TREE_CONNECT_HE || 97 cmd == SMB2_CANCEL_HE || 98 cmd == SMB2_LOGOFF_HE) { 99 ksmbd_debug(SMB, "skip to check tree connect request\n"); 100 return 0; 101 } 102 103 if (xa_empty(&work->sess->tree_conns)) { 104 ksmbd_debug(SMB, "NO tree connected\n"); 105 return -ENOENT; 106 } 107 108 tree_id = le32_to_cpu(req_hdr->Id.SyncId.TreeId); 109 110 /* 111 * If request is not the first in Compound request, 112 * Just validate tree id in header with work->tcon->id. 113 */ 114 if (work->next_smb2_rcv_hdr_off) { 115 if (!work->tcon) { 116 pr_err("The first operation in the compound does not have tcon\n"); 117 return -EINVAL; 118 } 119 if (tree_id != UINT_MAX && work->tcon->id != tree_id) { 120 pr_err("tree id(%u) is different with id(%u) in first operation\n", 121 tree_id, work->tcon->id); 122 return -EINVAL; 123 } 124 return 1; 125 } 126 127 work->tcon = ksmbd_tree_conn_lookup(work->sess, tree_id); 128 if (!work->tcon) { 129 pr_err("Invalid tid %d\n", tree_id); 130 return -ENOENT; 131 } 132 133 return 1; 134 } 135 136 /** 137 * smb2_set_err_rsp() - set error response code on smb response 138 * @work: smb work containing response buffer 139 */ 140 void smb2_set_err_rsp(struct ksmbd_work *work) 141 { 142 struct smb2_err_rsp *err_rsp; 143 144 if (work->next_smb2_rcv_hdr_off) 145 err_rsp = ksmbd_resp_buf_next(work); 146 else 147 err_rsp = smb2_get_msg(work->response_buf); 148 149 if (err_rsp->hdr.Status != STATUS_STOPPED_ON_SYMLINK) { 150 int err; 151 152 err_rsp->StructureSize = SMB2_ERROR_STRUCTURE_SIZE2_LE; 153 err_rsp->ErrorContextCount = 0; 154 err_rsp->Reserved = 0; 155 err_rsp->ByteCount = 0; 156 err_rsp->ErrorData[0] = 0; 157 err = ksmbd_iov_pin_rsp(work, (void *)err_rsp, 158 __SMB2_HEADER_STRUCTURE_SIZE + 159 SMB2_ERROR_STRUCTURE_SIZE2); 160 if (err) 161 work->send_no_response = 1; 162 } 163 } 164 165 /** 166 * is_smb2_neg_cmd() - is it smb2 negotiation command 167 * @work: smb work containing smb header 168 * 169 * Return: true if smb2 negotiation command, otherwise false 170 */ 171 bool is_smb2_neg_cmd(struct ksmbd_work *work) 172 { 173 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); 174 175 /* is it SMB2 header ? */ 176 if (hdr->ProtocolId != SMB2_PROTO_NUMBER) 177 return false; 178 179 /* make sure it is request not response message */ 180 if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR) 181 return false; 182 183 if (hdr->Command != SMB2_NEGOTIATE) 184 return false; 185 186 return true; 187 } 188 189 /** 190 * is_smb2_rsp() - is it smb2 response 191 * @work: smb work containing smb response buffer 192 * 193 * Return: true if smb2 response, otherwise false 194 */ 195 bool is_smb2_rsp(struct ksmbd_work *work) 196 { 197 struct smb2_hdr *hdr = smb2_get_msg(work->response_buf); 198 199 /* is it SMB2 header ? */ 200 if (hdr->ProtocolId != SMB2_PROTO_NUMBER) 201 return false; 202 203 /* make sure it is response not request message */ 204 if (!(hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)) 205 return false; 206 207 return true; 208 } 209 210 /** 211 * get_smb2_cmd_val() - get smb command code from smb header 212 * @work: smb work containing smb request buffer 213 * 214 * Return: smb2 request command value 215 */ 216 u16 get_smb2_cmd_val(struct ksmbd_work *work) 217 { 218 struct smb2_hdr *rcv_hdr; 219 220 if (work->next_smb2_rcv_hdr_off) 221 rcv_hdr = ksmbd_req_buf_next(work); 222 else 223 rcv_hdr = smb2_get_msg(work->request_buf); 224 return le16_to_cpu(rcv_hdr->Command); 225 } 226 227 /** 228 * set_smb2_rsp_status() - set error response code on smb2 header 229 * @work: smb work containing response buffer 230 * @err: error response code 231 */ 232 void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err) 233 { 234 struct smb2_hdr *rsp_hdr; 235 236 rsp_hdr = smb2_get_msg(work->response_buf); 237 rsp_hdr->Status = err; 238 239 work->iov_idx = 0; 240 work->iov_cnt = 0; 241 work->next_smb2_rcv_hdr_off = 0; 242 smb2_set_err_rsp(work); 243 } 244 245 /** 246 * init_smb2_neg_rsp() - initialize smb2 response for negotiate command 247 * @work: smb work containing smb request buffer 248 * 249 * smb2 negotiate response is sent in reply of smb1 negotiate command for 250 * dialect auto-negotiation. 251 */ 252 int init_smb2_neg_rsp(struct ksmbd_work *work) 253 { 254 struct smb2_hdr *rsp_hdr; 255 struct smb2_negotiate_rsp *rsp; 256 struct ksmbd_conn *conn = work->conn; 257 int err; 258 259 rsp_hdr = smb2_get_msg(work->response_buf); 260 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); 261 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; 262 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; 263 rsp_hdr->CreditRequest = cpu_to_le16(2); 264 rsp_hdr->Command = SMB2_NEGOTIATE; 265 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); 266 rsp_hdr->NextCommand = 0; 267 rsp_hdr->MessageId = 0; 268 rsp_hdr->Id.SyncId.ProcessId = 0; 269 rsp_hdr->Id.SyncId.TreeId = 0; 270 rsp_hdr->SessionId = 0; 271 memset(rsp_hdr->Signature, 0, 16); 272 273 rsp = smb2_get_msg(work->response_buf); 274 275 WARN_ON(ksmbd_conn_good(conn)); 276 277 rsp->StructureSize = cpu_to_le16(65); 278 ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect); 279 rsp->DialectRevision = cpu_to_le16(conn->dialect); 280 /* Not setting conn guid rsp->ServerGUID, as it 281 * not used by client for identifying connection 282 */ 283 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); 284 /* Default Max Message Size till SMB2.0, 64K*/ 285 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size); 286 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size); 287 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size); 288 289 rsp->SystemTime = cpu_to_le64(ksmbd_systime()); 290 rsp->ServerStartTime = 0; 291 292 rsp->SecurityBufferOffset = cpu_to_le16(128); 293 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH); 294 ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) + 295 le16_to_cpu(rsp->SecurityBufferOffset)); 296 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE; 297 if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) 298 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE; 299 err = ksmbd_iov_pin_rsp(work, rsp, 300 sizeof(struct smb2_negotiate_rsp) + AUTH_GSS_LENGTH); 301 if (err) 302 return err; 303 conn->use_spnego = true; 304 305 ksmbd_conn_set_need_negotiate(conn); 306 return 0; 307 } 308 309 /** 310 * smb2_set_rsp_credits() - set number of credits in response buffer 311 * @work: smb work containing smb response buffer 312 */ 313 int smb2_set_rsp_credits(struct ksmbd_work *work) 314 { 315 struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work); 316 struct smb2_hdr *hdr = ksmbd_resp_buf_next(work); 317 struct ksmbd_conn *conn = work->conn; 318 unsigned short credits_requested, aux_max; 319 unsigned short credit_charge, credits_granted = 0; 320 321 if (work->send_no_response) 322 return 0; 323 324 hdr->CreditCharge = req_hdr->CreditCharge; 325 326 if (conn->total_credits > conn->vals->max_credits) { 327 hdr->CreditRequest = 0; 328 pr_err("Total credits overflow: %d\n", conn->total_credits); 329 return -EINVAL; 330 } 331 332 credit_charge = max_t(unsigned short, 333 le16_to_cpu(req_hdr->CreditCharge), 1); 334 if (credit_charge > conn->total_credits) { 335 ksmbd_debug(SMB, "Insufficient credits granted, given: %u, granted: %u\n", 336 credit_charge, conn->total_credits); 337 return -EINVAL; 338 } 339 340 conn->total_credits -= credit_charge; 341 conn->outstanding_credits -= credit_charge; 342 credits_requested = max_t(unsigned short, 343 le16_to_cpu(req_hdr->CreditRequest), 1); 344 345 /* according to smb2.credits smbtorture, Windows server 346 * 2016 or later grant up to 8192 credits at once. 347 * 348 * TODO: Need to adjuct CreditRequest value according to 349 * current cpu load 350 */ 351 if (hdr->Command == SMB2_NEGOTIATE) 352 aux_max = 1; 353 else 354 aux_max = conn->vals->max_credits - conn->total_credits; 355 credits_granted = min_t(unsigned short, credits_requested, aux_max); 356 357 conn->total_credits += credits_granted; 358 work->credits_granted += credits_granted; 359 360 if (!req_hdr->NextCommand) { 361 /* Update CreditRequest in last request */ 362 hdr->CreditRequest = cpu_to_le16(work->credits_granted); 363 } 364 ksmbd_debug(SMB, 365 "credits: requested[%d] granted[%d] total_granted[%d]\n", 366 credits_requested, credits_granted, 367 conn->total_credits); 368 return 0; 369 } 370 371 /** 372 * init_chained_smb2_rsp() - initialize smb2 chained response 373 * @work: smb work containing smb response buffer 374 */ 375 static void init_chained_smb2_rsp(struct ksmbd_work *work) 376 { 377 struct smb2_hdr *req = ksmbd_req_buf_next(work); 378 struct smb2_hdr *rsp = ksmbd_resp_buf_next(work); 379 struct smb2_hdr *rsp_hdr; 380 struct smb2_hdr *rcv_hdr; 381 int next_hdr_offset = 0; 382 int len, new_len; 383 384 /* Len of this response = updated RFC len - offset of previous cmd 385 * in the compound rsp 386 */ 387 388 /* Storing the current local FID which may be needed by subsequent 389 * command in the compound request 390 */ 391 if (req->Command == SMB2_CREATE && rsp->Status == STATUS_SUCCESS) { 392 work->compound_fid = ((struct smb2_create_rsp *)rsp)->VolatileFileId; 393 work->compound_pfid = ((struct smb2_create_rsp *)rsp)->PersistentFileId; 394 work->compound_sid = le64_to_cpu(rsp->SessionId); 395 } 396 397 len = get_rfc1002_len(work->response_buf) - work->next_smb2_rsp_hdr_off; 398 next_hdr_offset = le32_to_cpu(req->NextCommand); 399 400 new_len = ALIGN(len, 8); 401 work->iov[work->iov_idx].iov_len += (new_len - len); 402 inc_rfc1001_len(work->response_buf, new_len - len); 403 rsp->NextCommand = cpu_to_le32(new_len); 404 405 work->next_smb2_rcv_hdr_off += next_hdr_offset; 406 work->curr_smb2_rsp_hdr_off = work->next_smb2_rsp_hdr_off; 407 work->next_smb2_rsp_hdr_off += new_len; 408 ksmbd_debug(SMB, 409 "Compound req new_len = %d rcv off = %d rsp off = %d\n", 410 new_len, work->next_smb2_rcv_hdr_off, 411 work->next_smb2_rsp_hdr_off); 412 413 rsp_hdr = ksmbd_resp_buf_next(work); 414 rcv_hdr = ksmbd_req_buf_next(work); 415 416 if (!(rcv_hdr->Flags & SMB2_FLAGS_RELATED_OPERATIONS)) { 417 ksmbd_debug(SMB, "related flag should be set\n"); 418 work->compound_fid = KSMBD_NO_FID; 419 work->compound_pfid = KSMBD_NO_FID; 420 } 421 memset((char *)rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); 422 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; 423 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; 424 rsp_hdr->Command = rcv_hdr->Command; 425 426 /* 427 * Message is response. We don't grant oplock yet. 428 */ 429 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR | 430 SMB2_FLAGS_RELATED_OPERATIONS); 431 rsp_hdr->NextCommand = 0; 432 rsp_hdr->MessageId = rcv_hdr->MessageId; 433 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId; 434 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId; 435 rsp_hdr->SessionId = rcv_hdr->SessionId; 436 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16); 437 } 438 439 /** 440 * is_chained_smb2_message() - check for chained command 441 * @work: smb work containing smb request buffer 442 * 443 * Return: true if chained request, otherwise false 444 */ 445 bool is_chained_smb2_message(struct ksmbd_work *work) 446 { 447 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); 448 unsigned int len, next_cmd; 449 450 if (hdr->ProtocolId != SMB2_PROTO_NUMBER) 451 return false; 452 453 hdr = ksmbd_req_buf_next(work); 454 next_cmd = le32_to_cpu(hdr->NextCommand); 455 if (next_cmd > 0) { 456 if ((u64)work->next_smb2_rcv_hdr_off + next_cmd + 457 __SMB2_HEADER_STRUCTURE_SIZE > 458 get_rfc1002_len(work->request_buf)) { 459 pr_err("next command(%u) offset exceeds smb msg size\n", 460 next_cmd); 461 return false; 462 } 463 464 if ((u64)get_rfc1002_len(work->response_buf) + MAX_CIFS_SMALL_BUFFER_SIZE > 465 work->response_sz) { 466 pr_err("next response offset exceeds response buffer size\n"); 467 return false; 468 } 469 470 ksmbd_debug(SMB, "got SMB2 chained command\n"); 471 init_chained_smb2_rsp(work); 472 return true; 473 } else if (work->next_smb2_rcv_hdr_off) { 474 /* 475 * This is last request in chained command, 476 * align response to 8 byte 477 */ 478 len = ALIGN(get_rfc1002_len(work->response_buf), 8); 479 len = len - get_rfc1002_len(work->response_buf); 480 if (len) { 481 ksmbd_debug(SMB, "padding len %u\n", len); 482 work->iov[work->iov_idx].iov_len += len; 483 inc_rfc1001_len(work->response_buf, len); 484 } 485 work->curr_smb2_rsp_hdr_off = work->next_smb2_rsp_hdr_off; 486 } 487 return false; 488 } 489 490 /** 491 * init_smb2_rsp_hdr() - initialize smb2 response 492 * @work: smb work containing smb request buffer 493 * 494 * Return: 0 495 */ 496 int init_smb2_rsp_hdr(struct ksmbd_work *work) 497 { 498 struct smb2_hdr *rsp_hdr = smb2_get_msg(work->response_buf); 499 struct smb2_hdr *rcv_hdr = smb2_get_msg(work->request_buf); 500 501 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); 502 rsp_hdr->ProtocolId = rcv_hdr->ProtocolId; 503 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; 504 rsp_hdr->Command = rcv_hdr->Command; 505 506 /* 507 * Message is response. We don't grant oplock yet. 508 */ 509 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); 510 rsp_hdr->NextCommand = 0; 511 rsp_hdr->MessageId = rcv_hdr->MessageId; 512 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId; 513 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId; 514 rsp_hdr->SessionId = rcv_hdr->SessionId; 515 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16); 516 517 return 0; 518 } 519 520 /** 521 * smb2_allocate_rsp_buf() - allocate smb2 response buffer 522 * @work: smb work containing smb request buffer 523 * 524 * Return: 0 on success, otherwise -ENOMEM 525 */ 526 int smb2_allocate_rsp_buf(struct ksmbd_work *work) 527 { 528 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); 529 size_t small_sz = MAX_CIFS_SMALL_BUFFER_SIZE; 530 size_t large_sz = small_sz + work->conn->vals->max_trans_size; 531 size_t sz = small_sz; 532 int cmd = le16_to_cpu(hdr->Command); 533 534 if (cmd == SMB2_IOCTL_HE || cmd == SMB2_QUERY_DIRECTORY_HE) 535 sz = large_sz; 536 537 if (cmd == SMB2_QUERY_INFO_HE) { 538 struct smb2_query_info_req *req; 539 540 if (get_rfc1002_len(work->request_buf) < 541 offsetof(struct smb2_query_info_req, OutputBufferLength)) 542 return -EINVAL; 543 544 req = smb2_get_msg(work->request_buf); 545 if ((req->InfoType == SMB2_O_INFO_FILE && 546 (req->FileInfoClass == FILE_FULL_EA_INFORMATION || 547 req->FileInfoClass == FILE_ALL_INFORMATION)) || 548 req->InfoType == SMB2_O_INFO_SECURITY) 549 sz = large_sz; 550 } 551 552 /* allocate large response buf for chained commands */ 553 if (le32_to_cpu(hdr->NextCommand) > 0) 554 sz = large_sz; 555 556 work->response_buf = kvzalloc(sz, GFP_KERNEL); 557 if (!work->response_buf) 558 return -ENOMEM; 559 560 work->response_sz = sz; 561 return 0; 562 } 563 564 /** 565 * smb2_check_user_session() - check for valid session for a user 566 * @work: smb work containing smb request buffer 567 * 568 * Return: 0 on success, otherwise error 569 */ 570 int smb2_check_user_session(struct ksmbd_work *work) 571 { 572 struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work); 573 struct ksmbd_conn *conn = work->conn; 574 unsigned int cmd = le16_to_cpu(req_hdr->Command); 575 unsigned long long sess_id; 576 577 /* 578 * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not 579 * require a session id, so no need to validate user session's for 580 * these commands. 581 */ 582 if (cmd == SMB2_ECHO_HE || cmd == SMB2_NEGOTIATE_HE || 583 cmd == SMB2_SESSION_SETUP_HE) 584 return 0; 585 586 if (!ksmbd_conn_good(conn)) 587 return -EIO; 588 589 sess_id = le64_to_cpu(req_hdr->SessionId); 590 591 /* 592 * If request is not the first in Compound request, 593 * Just validate session id in header with work->sess->id. 594 */ 595 if (work->next_smb2_rcv_hdr_off) { 596 if (!work->sess) { 597 pr_err("The first operation in the compound does not have sess\n"); 598 return -EINVAL; 599 } 600 if (sess_id != ULLONG_MAX && work->sess->id != sess_id) { 601 pr_err("session id(%llu) is different with the first operation(%lld)\n", 602 sess_id, work->sess->id); 603 return -EINVAL; 604 } 605 return 1; 606 } 607 608 /* Check for validity of user session */ 609 work->sess = ksmbd_session_lookup_all(conn, sess_id); 610 if (work->sess) 611 return 1; 612 ksmbd_debug(SMB, "Invalid user session, Uid %llu\n", sess_id); 613 return -ENOENT; 614 } 615 616 /** 617 * smb2_get_name() - get filename string from on the wire smb format 618 * @src: source buffer 619 * @maxlen: maxlen of source string 620 * @local_nls: nls_table pointer 621 * 622 * Return: matching converted filename on success, otherwise error ptr 623 */ 624 static char * 625 smb2_get_name(const char *src, const int maxlen, struct nls_table *local_nls) 626 { 627 char *name; 628 629 name = smb_strndup_from_utf16(src, maxlen, 1, local_nls); 630 if (IS_ERR(name)) { 631 pr_err("failed to get name %ld\n", PTR_ERR(name)); 632 return name; 633 } 634 635 if (*name == '\\') { 636 pr_err("not allow directory name included leading slash\n"); 637 kfree(name); 638 return ERR_PTR(-EINVAL); 639 } 640 641 ksmbd_conv_path_to_unix(name); 642 ksmbd_strip_last_slash(name); 643 return name; 644 } 645 646 int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg) 647 { 648 struct ksmbd_conn *conn = work->conn; 649 int id; 650 651 id = ksmbd_acquire_async_msg_id(&conn->async_ida); 652 if (id < 0) { 653 pr_err("Failed to alloc async message id\n"); 654 return id; 655 } 656 work->asynchronous = true; 657 work->async_id = id; 658 659 ksmbd_debug(SMB, 660 "Send interim Response to inform async request id : %d\n", 661 work->async_id); 662 663 work->cancel_fn = fn; 664 work->cancel_argv = arg; 665 666 if (list_empty(&work->async_request_entry)) { 667 spin_lock(&conn->request_lock); 668 list_add_tail(&work->async_request_entry, &conn->async_requests); 669 spin_unlock(&conn->request_lock); 670 } 671 672 return 0; 673 } 674 675 void release_async_work(struct ksmbd_work *work) 676 { 677 struct ksmbd_conn *conn = work->conn; 678 679 spin_lock(&conn->request_lock); 680 list_del_init(&work->async_request_entry); 681 spin_unlock(&conn->request_lock); 682 683 work->asynchronous = 0; 684 work->cancel_fn = NULL; 685 kfree(work->cancel_argv); 686 work->cancel_argv = NULL; 687 if (work->async_id) { 688 ksmbd_release_id(&conn->async_ida, work->async_id); 689 work->async_id = 0; 690 } 691 } 692 693 void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status) 694 { 695 struct smb2_hdr *rsp_hdr; 696 struct ksmbd_work *in_work = ksmbd_alloc_work_struct(); 697 698 if (!in_work) 699 return; 700 701 if (allocate_interim_rsp_buf(in_work)) { 702 pr_err("smb_allocate_rsp_buf failed!\n"); 703 ksmbd_free_work_struct(in_work); 704 return; 705 } 706 707 in_work->conn = work->conn; 708 memcpy(smb2_get_msg(in_work->response_buf), ksmbd_resp_buf_next(work), 709 __SMB2_HEADER_STRUCTURE_SIZE); 710 711 rsp_hdr = smb2_get_msg(in_work->response_buf); 712 rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND; 713 rsp_hdr->Id.AsyncId = cpu_to_le64(work->async_id); 714 smb2_set_err_rsp(in_work); 715 rsp_hdr->Status = status; 716 717 ksmbd_conn_write(in_work); 718 ksmbd_free_work_struct(in_work); 719 } 720 721 static __le32 smb2_get_reparse_tag_special_file(umode_t mode) 722 { 723 if (S_ISDIR(mode) || S_ISREG(mode)) 724 return 0; 725 726 if (S_ISLNK(mode)) 727 return IO_REPARSE_TAG_LX_SYMLINK_LE; 728 else if (S_ISFIFO(mode)) 729 return IO_REPARSE_TAG_LX_FIFO_LE; 730 else if (S_ISSOCK(mode)) 731 return IO_REPARSE_TAG_AF_UNIX_LE; 732 else if (S_ISCHR(mode)) 733 return IO_REPARSE_TAG_LX_CHR_LE; 734 else if (S_ISBLK(mode)) 735 return IO_REPARSE_TAG_LX_BLK_LE; 736 737 return 0; 738 } 739 740 /** 741 * smb2_get_dos_mode() - get file mode in dos format from unix mode 742 * @stat: kstat containing file mode 743 * @attribute: attribute flags 744 * 745 * Return: converted dos mode 746 */ 747 static int smb2_get_dos_mode(struct kstat *stat, int attribute) 748 { 749 int attr = 0; 750 751 if (S_ISDIR(stat->mode)) { 752 attr = FILE_ATTRIBUTE_DIRECTORY | 753 (attribute & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)); 754 } else { 755 attr = (attribute & 0x00005137) | FILE_ATTRIBUTE_ARCHIVE; 756 attr &= ~(FILE_ATTRIBUTE_DIRECTORY); 757 if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps & 758 FILE_SUPPORTS_SPARSE_FILES)) 759 attr |= FILE_ATTRIBUTE_SPARSE_FILE; 760 761 if (smb2_get_reparse_tag_special_file(stat->mode)) 762 attr |= FILE_ATTRIBUTE_REPARSE_POINT; 763 } 764 765 return attr; 766 } 767 768 static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt, 769 __le16 hash_id) 770 { 771 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES; 772 pneg_ctxt->DataLength = cpu_to_le16(38); 773 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1); 774 pneg_ctxt->Reserved = cpu_to_le32(0); 775 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE); 776 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE); 777 pneg_ctxt->HashAlgorithms = hash_id; 778 } 779 780 static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt, 781 __le16 cipher_type) 782 { 783 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES; 784 pneg_ctxt->DataLength = cpu_to_le16(4); 785 pneg_ctxt->Reserved = cpu_to_le32(0); 786 pneg_ctxt->CipherCount = cpu_to_le16(1); 787 pneg_ctxt->Ciphers[0] = cipher_type; 788 } 789 790 static void build_sign_cap_ctxt(struct smb2_signing_capabilities *pneg_ctxt, 791 __le16 sign_algo) 792 { 793 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES; 794 pneg_ctxt->DataLength = 795 cpu_to_le16((sizeof(struct smb2_signing_capabilities) + 2) 796 - sizeof(struct smb2_neg_context)); 797 pneg_ctxt->Reserved = cpu_to_le32(0); 798 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(1); 799 pneg_ctxt->SigningAlgorithms[0] = sign_algo; 800 } 801 802 static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt) 803 { 804 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE; 805 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); 806 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ 807 pneg_ctxt->Name[0] = 0x93; 808 pneg_ctxt->Name[1] = 0xAD; 809 pneg_ctxt->Name[2] = 0x25; 810 pneg_ctxt->Name[3] = 0x50; 811 pneg_ctxt->Name[4] = 0x9C; 812 pneg_ctxt->Name[5] = 0xB4; 813 pneg_ctxt->Name[6] = 0x11; 814 pneg_ctxt->Name[7] = 0xE7; 815 pneg_ctxt->Name[8] = 0xB4; 816 pneg_ctxt->Name[9] = 0x23; 817 pneg_ctxt->Name[10] = 0x83; 818 pneg_ctxt->Name[11] = 0xDE; 819 pneg_ctxt->Name[12] = 0x96; 820 pneg_ctxt->Name[13] = 0x8B; 821 pneg_ctxt->Name[14] = 0xCD; 822 pneg_ctxt->Name[15] = 0x7C; 823 } 824 825 static unsigned int assemble_neg_contexts(struct ksmbd_conn *conn, 826 struct smb2_negotiate_rsp *rsp) 827 { 828 char * const pneg_ctxt = (char *)rsp + 829 le32_to_cpu(rsp->NegotiateContextOffset); 830 int neg_ctxt_cnt = 1; 831 int ctxt_size; 832 833 ksmbd_debug(SMB, 834 "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n"); 835 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt, 836 conn->preauth_info->Preauth_HashId); 837 ctxt_size = sizeof(struct smb2_preauth_neg_context); 838 839 if (conn->cipher_type) { 840 /* Round to 8 byte boundary */ 841 ctxt_size = round_up(ctxt_size, 8); 842 ksmbd_debug(SMB, 843 "assemble SMB2_ENCRYPTION_CAPABILITIES context\n"); 844 build_encrypt_ctxt((struct smb2_encryption_neg_context *) 845 (pneg_ctxt + ctxt_size), 846 conn->cipher_type); 847 neg_ctxt_cnt++; 848 ctxt_size += sizeof(struct smb2_encryption_neg_context) + 2; 849 } 850 851 /* compression context not yet supported */ 852 WARN_ON(conn->compress_algorithm != SMB3_COMPRESS_NONE); 853 854 if (conn->posix_ext_supported) { 855 ctxt_size = round_up(ctxt_size, 8); 856 ksmbd_debug(SMB, 857 "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n"); 858 build_posix_ctxt((struct smb2_posix_neg_context *) 859 (pneg_ctxt + ctxt_size)); 860 neg_ctxt_cnt++; 861 ctxt_size += sizeof(struct smb2_posix_neg_context); 862 } 863 864 if (conn->signing_negotiated) { 865 ctxt_size = round_up(ctxt_size, 8); 866 ksmbd_debug(SMB, 867 "assemble SMB2_SIGNING_CAPABILITIES context\n"); 868 build_sign_cap_ctxt((struct smb2_signing_capabilities *) 869 (pneg_ctxt + ctxt_size), 870 conn->signing_algorithm); 871 neg_ctxt_cnt++; 872 ctxt_size += sizeof(struct smb2_signing_capabilities) + 2; 873 } 874 875 rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt); 876 return ctxt_size + AUTH_GSS_PADDING; 877 } 878 879 static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn, 880 struct smb2_preauth_neg_context *pneg_ctxt, 881 int ctxt_len) 882 { 883 /* 884 * sizeof(smb2_preauth_neg_context) assumes SMB311_SALT_SIZE Salt, 885 * which may not be present. Only check for used HashAlgorithms[1]. 886 */ 887 if (ctxt_len < 888 sizeof(struct smb2_neg_context) + MIN_PREAUTH_CTXT_DATA_LEN) 889 return STATUS_INVALID_PARAMETER; 890 891 if (pneg_ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512) 892 return STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP; 893 894 conn->preauth_info->Preauth_HashId = SMB2_PREAUTH_INTEGRITY_SHA512; 895 return STATUS_SUCCESS; 896 } 897 898 static void decode_encrypt_ctxt(struct ksmbd_conn *conn, 899 struct smb2_encryption_neg_context *pneg_ctxt, 900 int ctxt_len) 901 { 902 int cph_cnt; 903 int i, cphs_size; 904 905 if (sizeof(struct smb2_encryption_neg_context) > ctxt_len) { 906 pr_err("Invalid SMB2_ENCRYPTION_CAPABILITIES context size\n"); 907 return; 908 } 909 910 conn->cipher_type = 0; 911 912 cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount); 913 cphs_size = cph_cnt * sizeof(__le16); 914 915 if (sizeof(struct smb2_encryption_neg_context) + cphs_size > 916 ctxt_len) { 917 pr_err("Invalid cipher count(%d)\n", cph_cnt); 918 return; 919 } 920 921 if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION_OFF) 922 return; 923 924 for (i = 0; i < cph_cnt; i++) { 925 if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM || 926 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM || 927 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM || 928 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) { 929 ksmbd_debug(SMB, "Cipher ID = 0x%x\n", 930 pneg_ctxt->Ciphers[i]); 931 conn->cipher_type = pneg_ctxt->Ciphers[i]; 932 break; 933 } 934 } 935 } 936 937 /** 938 * smb3_encryption_negotiated() - checks if server and client agreed on enabling encryption 939 * @conn: smb connection 940 * 941 * Return: true if connection should be encrypted, else false 942 */ 943 bool smb3_encryption_negotiated(struct ksmbd_conn *conn) 944 { 945 if (!conn->ops->generate_encryptionkey) 946 return false; 947 948 /* 949 * SMB 3.0 and 3.0.2 dialects use the SMB2_GLOBAL_CAP_ENCRYPTION flag. 950 * SMB 3.1.1 uses the cipher_type field. 951 */ 952 return (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) || 953 conn->cipher_type; 954 } 955 956 static void decode_compress_ctxt(struct ksmbd_conn *conn, 957 struct smb2_compression_capabilities_context *pneg_ctxt) 958 { 959 conn->compress_algorithm = SMB3_COMPRESS_NONE; 960 } 961 962 static void decode_sign_cap_ctxt(struct ksmbd_conn *conn, 963 struct smb2_signing_capabilities *pneg_ctxt, 964 int ctxt_len) 965 { 966 int sign_algo_cnt; 967 int i, sign_alos_size; 968 969 if (sizeof(struct smb2_signing_capabilities) > ctxt_len) { 970 pr_err("Invalid SMB2_SIGNING_CAPABILITIES context length\n"); 971 return; 972 } 973 974 conn->signing_negotiated = false; 975 sign_algo_cnt = le16_to_cpu(pneg_ctxt->SigningAlgorithmCount); 976 sign_alos_size = sign_algo_cnt * sizeof(__le16); 977 978 if (sizeof(struct smb2_signing_capabilities) + sign_alos_size > 979 ctxt_len) { 980 pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt); 981 return; 982 } 983 984 for (i = 0; i < sign_algo_cnt; i++) { 985 if (pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_HMAC_SHA256_LE || 986 pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_AES_CMAC_LE) { 987 ksmbd_debug(SMB, "Signing Algorithm ID = 0x%x\n", 988 pneg_ctxt->SigningAlgorithms[i]); 989 conn->signing_negotiated = true; 990 conn->signing_algorithm = 991 pneg_ctxt->SigningAlgorithms[i]; 992 break; 993 } 994 } 995 } 996 997 static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn, 998 struct smb2_negotiate_req *req, 999 unsigned int len_of_smb) 1000 { 1001 /* +4 is to account for the RFC1001 len field */ 1002 struct smb2_neg_context *pctx = (struct smb2_neg_context *)req; 1003 int i = 0, len_of_ctxts; 1004 unsigned int offset = le32_to_cpu(req->NegotiateContextOffset); 1005 unsigned int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount); 1006 __le32 status = STATUS_INVALID_PARAMETER; 1007 1008 ksmbd_debug(SMB, "decoding %d negotiate contexts\n", neg_ctxt_cnt); 1009 if (len_of_smb <= offset) { 1010 ksmbd_debug(SMB, "Invalid response: negotiate context offset\n"); 1011 return status; 1012 } 1013 1014 len_of_ctxts = len_of_smb - offset; 1015 1016 while (i++ < neg_ctxt_cnt) { 1017 int clen, ctxt_len; 1018 1019 if (len_of_ctxts < (int)sizeof(struct smb2_neg_context)) 1020 break; 1021 1022 pctx = (struct smb2_neg_context *)((char *)pctx + offset); 1023 clen = le16_to_cpu(pctx->DataLength); 1024 ctxt_len = clen + sizeof(struct smb2_neg_context); 1025 1026 if (ctxt_len > len_of_ctxts) 1027 break; 1028 1029 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) { 1030 ksmbd_debug(SMB, 1031 "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n"); 1032 if (conn->preauth_info->Preauth_HashId) 1033 break; 1034 1035 status = decode_preauth_ctxt(conn, 1036 (struct smb2_preauth_neg_context *)pctx, 1037 ctxt_len); 1038 if (status != STATUS_SUCCESS) 1039 break; 1040 } else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) { 1041 ksmbd_debug(SMB, 1042 "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n"); 1043 if (conn->cipher_type) 1044 break; 1045 1046 decode_encrypt_ctxt(conn, 1047 (struct smb2_encryption_neg_context *)pctx, 1048 ctxt_len); 1049 } else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) { 1050 ksmbd_debug(SMB, 1051 "deassemble SMB2_COMPRESSION_CAPABILITIES context\n"); 1052 if (conn->compress_algorithm) 1053 break; 1054 1055 decode_compress_ctxt(conn, 1056 (struct smb2_compression_capabilities_context *)pctx); 1057 } else if (pctx->ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) { 1058 ksmbd_debug(SMB, 1059 "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n"); 1060 } else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) { 1061 ksmbd_debug(SMB, 1062 "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n"); 1063 conn->posix_ext_supported = true; 1064 } else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES) { 1065 ksmbd_debug(SMB, 1066 "deassemble SMB2_SIGNING_CAPABILITIES context\n"); 1067 1068 decode_sign_cap_ctxt(conn, 1069 (struct smb2_signing_capabilities *)pctx, 1070 ctxt_len); 1071 } 1072 1073 /* offsets must be 8 byte aligned */ 1074 offset = (ctxt_len + 7) & ~0x7; 1075 len_of_ctxts -= offset; 1076 } 1077 return status; 1078 } 1079 1080 /** 1081 * smb2_handle_negotiate() - handler for smb2 negotiate command 1082 * @work: smb work containing smb request buffer 1083 * 1084 * Return: 0 1085 */ 1086 int smb2_handle_negotiate(struct ksmbd_work *work) 1087 { 1088 struct ksmbd_conn *conn = work->conn; 1089 struct smb2_negotiate_req *req = smb2_get_msg(work->request_buf); 1090 struct smb2_negotiate_rsp *rsp = smb2_get_msg(work->response_buf); 1091 int rc = 0; 1092 unsigned int smb2_buf_len, smb2_neg_size, neg_ctxt_len = 0; 1093 __le32 status; 1094 1095 ksmbd_debug(SMB, "Received negotiate request\n"); 1096 conn->need_neg = false; 1097 if (ksmbd_conn_good(conn)) { 1098 pr_err("conn->tcp_status is already in CifsGood State\n"); 1099 work->send_no_response = 1; 1100 return rc; 1101 } 1102 1103 smb2_buf_len = get_rfc1002_len(work->request_buf); 1104 smb2_neg_size = offsetof(struct smb2_negotiate_req, Dialects); 1105 if (smb2_neg_size > smb2_buf_len) { 1106 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 1107 rc = -EINVAL; 1108 goto err_out; 1109 } 1110 1111 if (req->DialectCount == 0) { 1112 pr_err("malformed packet\n"); 1113 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 1114 rc = -EINVAL; 1115 goto err_out; 1116 } 1117 1118 if (conn->dialect == SMB311_PROT_ID) { 1119 unsigned int nego_ctxt_off = le32_to_cpu(req->NegotiateContextOffset); 1120 1121 if (smb2_buf_len < nego_ctxt_off) { 1122 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 1123 rc = -EINVAL; 1124 goto err_out; 1125 } 1126 1127 if (smb2_neg_size > nego_ctxt_off) { 1128 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 1129 rc = -EINVAL; 1130 goto err_out; 1131 } 1132 1133 if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) > 1134 nego_ctxt_off) { 1135 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 1136 rc = -EINVAL; 1137 goto err_out; 1138 } 1139 } else { 1140 if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) > 1141 smb2_buf_len) { 1142 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 1143 rc = -EINVAL; 1144 goto err_out; 1145 } 1146 } 1147 1148 conn->cli_cap = le32_to_cpu(req->Capabilities); 1149 switch (conn->dialect) { 1150 case SMB311_PROT_ID: 1151 conn->preauth_info = 1152 kzalloc(sizeof(struct preauth_integrity_info), 1153 GFP_KERNEL); 1154 if (!conn->preauth_info) { 1155 rc = -ENOMEM; 1156 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 1157 goto err_out; 1158 } 1159 1160 status = deassemble_neg_contexts(conn, req, 1161 get_rfc1002_len(work->request_buf)); 1162 if (status != STATUS_SUCCESS) { 1163 pr_err("deassemble_neg_contexts error(0x%x)\n", 1164 status); 1165 rsp->hdr.Status = status; 1166 rc = -EINVAL; 1167 kfree(conn->preauth_info); 1168 conn->preauth_info = NULL; 1169 goto err_out; 1170 } 1171 1172 rc = init_smb3_11_server(conn); 1173 if (rc < 0) { 1174 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 1175 kfree(conn->preauth_info); 1176 conn->preauth_info = NULL; 1177 goto err_out; 1178 } 1179 1180 ksmbd_gen_preauth_integrity_hash(conn, 1181 work->request_buf, 1182 conn->preauth_info->Preauth_HashValue); 1183 rsp->NegotiateContextOffset = 1184 cpu_to_le32(OFFSET_OF_NEG_CONTEXT); 1185 neg_ctxt_len = assemble_neg_contexts(conn, rsp); 1186 break; 1187 case SMB302_PROT_ID: 1188 init_smb3_02_server(conn); 1189 break; 1190 case SMB30_PROT_ID: 1191 init_smb3_0_server(conn); 1192 break; 1193 case SMB21_PROT_ID: 1194 init_smb2_1_server(conn); 1195 break; 1196 case SMB2X_PROT_ID: 1197 case BAD_PROT_ID: 1198 default: 1199 ksmbd_debug(SMB, "Server dialect :0x%x not supported\n", 1200 conn->dialect); 1201 rsp->hdr.Status = STATUS_NOT_SUPPORTED; 1202 rc = -EINVAL; 1203 goto err_out; 1204 } 1205 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); 1206 1207 /* For stats */ 1208 conn->connection_type = conn->dialect; 1209 1210 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size); 1211 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size); 1212 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size); 1213 1214 memcpy(conn->ClientGUID, req->ClientGUID, 1215 SMB2_CLIENT_GUID_SIZE); 1216 conn->cli_sec_mode = le16_to_cpu(req->SecurityMode); 1217 1218 rsp->StructureSize = cpu_to_le16(65); 1219 rsp->DialectRevision = cpu_to_le16(conn->dialect); 1220 /* Not setting conn guid rsp->ServerGUID, as it 1221 * not used by client for identifying server 1222 */ 1223 memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE); 1224 1225 rsp->SystemTime = cpu_to_le64(ksmbd_systime()); 1226 rsp->ServerStartTime = 0; 1227 ksmbd_debug(SMB, "negotiate context offset %d, count %d\n", 1228 le32_to_cpu(rsp->NegotiateContextOffset), 1229 le16_to_cpu(rsp->NegotiateContextCount)); 1230 1231 rsp->SecurityBufferOffset = cpu_to_le16(128); 1232 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH); 1233 ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) + 1234 le16_to_cpu(rsp->SecurityBufferOffset)); 1235 1236 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE; 1237 conn->use_spnego = true; 1238 1239 if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO || 1240 server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) && 1241 req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE) 1242 conn->sign = true; 1243 else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) { 1244 server_conf.enforced_signing = true; 1245 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE; 1246 conn->sign = true; 1247 } 1248 1249 conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode); 1250 ksmbd_conn_set_need_negotiate(conn); 1251 1252 err_out: 1253 if (rc) 1254 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; 1255 1256 if (!rc) 1257 rc = ksmbd_iov_pin_rsp(work, rsp, 1258 sizeof(struct smb2_negotiate_rsp) + 1259 AUTH_GSS_LENGTH + neg_ctxt_len); 1260 if (rc < 0) 1261 smb2_set_err_rsp(work); 1262 return rc; 1263 } 1264 1265 static int alloc_preauth_hash(struct ksmbd_session *sess, 1266 struct ksmbd_conn *conn) 1267 { 1268 if (sess->Preauth_HashValue) 1269 return 0; 1270 1271 sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue, 1272 PREAUTH_HASHVALUE_SIZE, GFP_KERNEL); 1273 if (!sess->Preauth_HashValue) 1274 return -ENOMEM; 1275 1276 return 0; 1277 } 1278 1279 static int generate_preauth_hash(struct ksmbd_work *work) 1280 { 1281 struct ksmbd_conn *conn = work->conn; 1282 struct ksmbd_session *sess = work->sess; 1283 u8 *preauth_hash; 1284 1285 if (conn->dialect != SMB311_PROT_ID) 1286 return 0; 1287 1288 if (conn->binding) { 1289 struct preauth_session *preauth_sess; 1290 1291 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); 1292 if (!preauth_sess) { 1293 preauth_sess = ksmbd_preauth_session_alloc(conn, sess->id); 1294 if (!preauth_sess) 1295 return -ENOMEM; 1296 } 1297 1298 preauth_hash = preauth_sess->Preauth_HashValue; 1299 } else { 1300 if (!sess->Preauth_HashValue) 1301 if (alloc_preauth_hash(sess, conn)) 1302 return -ENOMEM; 1303 preauth_hash = sess->Preauth_HashValue; 1304 } 1305 1306 ksmbd_gen_preauth_integrity_hash(conn, work->request_buf, preauth_hash); 1307 return 0; 1308 } 1309 1310 static int decode_negotiation_token(struct ksmbd_conn *conn, 1311 struct negotiate_message *negblob, 1312 size_t sz) 1313 { 1314 if (!conn->use_spnego) 1315 return -EINVAL; 1316 1317 if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) { 1318 if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) { 1319 conn->auth_mechs |= KSMBD_AUTH_NTLMSSP; 1320 conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP; 1321 conn->use_spnego = false; 1322 } 1323 } 1324 return 0; 1325 } 1326 1327 static int ntlm_negotiate(struct ksmbd_work *work, 1328 struct negotiate_message *negblob, 1329 size_t negblob_len, struct smb2_sess_setup_rsp *rsp) 1330 { 1331 struct challenge_message *chgblob; 1332 unsigned char *spnego_blob = NULL; 1333 u16 spnego_blob_len; 1334 char *neg_blob; 1335 int sz, rc; 1336 1337 ksmbd_debug(SMB, "negotiate phase\n"); 1338 rc = ksmbd_decode_ntlmssp_neg_blob(negblob, negblob_len, work->conn); 1339 if (rc) 1340 return rc; 1341 1342 sz = le16_to_cpu(rsp->SecurityBufferOffset); 1343 chgblob = 1344 (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz); 1345 memset(chgblob, 0, sizeof(struct challenge_message)); 1346 1347 if (!work->conn->use_spnego) { 1348 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->conn); 1349 if (sz < 0) 1350 return -ENOMEM; 1351 1352 rsp->SecurityBufferLength = cpu_to_le16(sz); 1353 return 0; 1354 } 1355 1356 sz = sizeof(struct challenge_message); 1357 sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6; 1358 1359 neg_blob = kzalloc(sz, GFP_KERNEL); 1360 if (!neg_blob) 1361 return -ENOMEM; 1362 1363 chgblob = (struct challenge_message *)neg_blob; 1364 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->conn); 1365 if (sz < 0) { 1366 rc = -ENOMEM; 1367 goto out; 1368 } 1369 1370 rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len, 1371 neg_blob, sz); 1372 if (rc) { 1373 rc = -ENOMEM; 1374 goto out; 1375 } 1376 1377 sz = le16_to_cpu(rsp->SecurityBufferOffset); 1378 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len); 1379 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len); 1380 1381 out: 1382 kfree(spnego_blob); 1383 kfree(neg_blob); 1384 return rc; 1385 } 1386 1387 static struct authenticate_message *user_authblob(struct ksmbd_conn *conn, 1388 struct smb2_sess_setup_req *req) 1389 { 1390 int sz; 1391 1392 if (conn->use_spnego && conn->mechToken) 1393 return (struct authenticate_message *)conn->mechToken; 1394 1395 sz = le16_to_cpu(req->SecurityBufferOffset); 1396 return (struct authenticate_message *)((char *)&req->hdr.ProtocolId 1397 + sz); 1398 } 1399 1400 static struct ksmbd_user *session_user(struct ksmbd_conn *conn, 1401 struct smb2_sess_setup_req *req) 1402 { 1403 struct authenticate_message *authblob; 1404 struct ksmbd_user *user; 1405 char *name; 1406 unsigned int name_off, name_len, secbuf_len; 1407 1408 if (conn->use_spnego && conn->mechToken) 1409 secbuf_len = conn->mechTokenLen; 1410 else 1411 secbuf_len = le16_to_cpu(req->SecurityBufferLength); 1412 if (secbuf_len < sizeof(struct authenticate_message)) { 1413 ksmbd_debug(SMB, "blob len %d too small\n", secbuf_len); 1414 return NULL; 1415 } 1416 authblob = user_authblob(conn, req); 1417 name_off = le32_to_cpu(authblob->UserName.BufferOffset); 1418 name_len = le16_to_cpu(authblob->UserName.Length); 1419 1420 if (secbuf_len < (u64)name_off + name_len) 1421 return NULL; 1422 1423 name = smb_strndup_from_utf16((const char *)authblob + name_off, 1424 name_len, 1425 true, 1426 conn->local_nls); 1427 if (IS_ERR(name)) { 1428 pr_err("cannot allocate memory\n"); 1429 return NULL; 1430 } 1431 1432 ksmbd_debug(SMB, "session setup request for user %s\n", name); 1433 user = ksmbd_login_user(name); 1434 kfree(name); 1435 return user; 1436 } 1437 1438 static int ntlm_authenticate(struct ksmbd_work *work, 1439 struct smb2_sess_setup_req *req, 1440 struct smb2_sess_setup_rsp *rsp) 1441 { 1442 struct ksmbd_conn *conn = work->conn; 1443 struct ksmbd_session *sess = work->sess; 1444 struct channel *chann = NULL; 1445 struct ksmbd_user *user; 1446 u64 prev_id; 1447 int sz, rc; 1448 1449 ksmbd_debug(SMB, "authenticate phase\n"); 1450 if (conn->use_spnego) { 1451 unsigned char *spnego_blob; 1452 u16 spnego_blob_len; 1453 1454 rc = build_spnego_ntlmssp_auth_blob(&spnego_blob, 1455 &spnego_blob_len, 1456 0); 1457 if (rc) 1458 return -ENOMEM; 1459 1460 sz = le16_to_cpu(rsp->SecurityBufferOffset); 1461 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len); 1462 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len); 1463 kfree(spnego_blob); 1464 } 1465 1466 user = session_user(conn, req); 1467 if (!user) { 1468 ksmbd_debug(SMB, "Unknown user name or an error\n"); 1469 return -EPERM; 1470 } 1471 1472 /* Check for previous session */ 1473 prev_id = le64_to_cpu(req->PreviousSessionId); 1474 if (prev_id && prev_id != sess->id) 1475 destroy_previous_session(conn, user, prev_id); 1476 1477 if (sess->state == SMB2_SESSION_VALID) { 1478 /* 1479 * Reuse session if anonymous try to connect 1480 * on reauthetication. 1481 */ 1482 if (conn->binding == false && ksmbd_anonymous_user(user)) { 1483 ksmbd_free_user(user); 1484 return 0; 1485 } 1486 1487 if (!ksmbd_compare_user(sess->user, user)) { 1488 ksmbd_free_user(user); 1489 return -EPERM; 1490 } 1491 ksmbd_free_user(user); 1492 } else { 1493 sess->user = user; 1494 } 1495 1496 if (conn->binding == false && user_guest(sess->user)) { 1497 rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE; 1498 } else { 1499 struct authenticate_message *authblob; 1500 1501 authblob = user_authblob(conn, req); 1502 if (conn->use_spnego && conn->mechToken) 1503 sz = conn->mechTokenLen; 1504 else 1505 sz = le16_to_cpu(req->SecurityBufferLength); 1506 rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, conn, sess); 1507 if (rc) { 1508 set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD); 1509 ksmbd_debug(SMB, "authentication failed\n"); 1510 return -EPERM; 1511 } 1512 } 1513 1514 /* 1515 * If session state is SMB2_SESSION_VALID, We can assume 1516 * that it is reauthentication. And the user/password 1517 * has been verified, so return it here. 1518 */ 1519 if (sess->state == SMB2_SESSION_VALID) { 1520 if (conn->binding) 1521 goto binding_session; 1522 return 0; 1523 } 1524 1525 if ((rsp->SessionFlags != SMB2_SESSION_FLAG_IS_GUEST_LE && 1526 (conn->sign || server_conf.enforced_signing)) || 1527 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) 1528 sess->sign = true; 1529 1530 if (smb3_encryption_negotiated(conn) && 1531 !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { 1532 rc = conn->ops->generate_encryptionkey(conn, sess); 1533 if (rc) { 1534 ksmbd_debug(SMB, 1535 "SMB3 encryption key generation failed\n"); 1536 return -EINVAL; 1537 } 1538 sess->enc = true; 1539 if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION) 1540 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE; 1541 /* 1542 * signing is disable if encryption is enable 1543 * on this session 1544 */ 1545 sess->sign = false; 1546 } 1547 1548 binding_session: 1549 if (conn->dialect >= SMB30_PROT_ID) { 1550 chann = lookup_chann_list(sess, conn); 1551 if (!chann) { 1552 chann = kmalloc(sizeof(struct channel), GFP_KERNEL); 1553 if (!chann) 1554 return -ENOMEM; 1555 1556 chann->conn = conn; 1557 xa_store(&sess->ksmbd_chann_list, (long)conn, chann, GFP_KERNEL); 1558 } 1559 } 1560 1561 if (conn->ops->generate_signingkey) { 1562 rc = conn->ops->generate_signingkey(sess, conn); 1563 if (rc) { 1564 ksmbd_debug(SMB, "SMB3 signing key generation failed\n"); 1565 return -EINVAL; 1566 } 1567 } 1568 1569 if (!ksmbd_conn_lookup_dialect(conn)) { 1570 pr_err("fail to verify the dialect\n"); 1571 return -ENOENT; 1572 } 1573 return 0; 1574 } 1575 1576 #ifdef CONFIG_SMB_SERVER_KERBEROS5 1577 static int krb5_authenticate(struct ksmbd_work *work, 1578 struct smb2_sess_setup_req *req, 1579 struct smb2_sess_setup_rsp *rsp) 1580 { 1581 struct ksmbd_conn *conn = work->conn; 1582 struct ksmbd_session *sess = work->sess; 1583 char *in_blob, *out_blob; 1584 struct channel *chann = NULL; 1585 u64 prev_sess_id; 1586 int in_len, out_len; 1587 int retval; 1588 1589 in_blob = (char *)&req->hdr.ProtocolId + 1590 le16_to_cpu(req->SecurityBufferOffset); 1591 in_len = le16_to_cpu(req->SecurityBufferLength); 1592 out_blob = (char *)&rsp->hdr.ProtocolId + 1593 le16_to_cpu(rsp->SecurityBufferOffset); 1594 out_len = work->response_sz - 1595 (le16_to_cpu(rsp->SecurityBufferOffset) + 4); 1596 1597 /* Check previous session */ 1598 prev_sess_id = le64_to_cpu(req->PreviousSessionId); 1599 if (prev_sess_id && prev_sess_id != sess->id) 1600 destroy_previous_session(conn, sess->user, prev_sess_id); 1601 1602 if (sess->state == SMB2_SESSION_VALID) 1603 ksmbd_free_user(sess->user); 1604 1605 retval = ksmbd_krb5_authenticate(sess, in_blob, in_len, 1606 out_blob, &out_len); 1607 if (retval) { 1608 ksmbd_debug(SMB, "krb5 authentication failed\n"); 1609 return -EINVAL; 1610 } 1611 rsp->SecurityBufferLength = cpu_to_le16(out_len); 1612 1613 if ((conn->sign || server_conf.enforced_signing) || 1614 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) 1615 sess->sign = true; 1616 1617 if (smb3_encryption_negotiated(conn)) { 1618 retval = conn->ops->generate_encryptionkey(conn, sess); 1619 if (retval) { 1620 ksmbd_debug(SMB, 1621 "SMB3 encryption key generation failed\n"); 1622 return -EINVAL; 1623 } 1624 sess->enc = true; 1625 if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION) 1626 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE; 1627 sess->sign = false; 1628 } 1629 1630 if (conn->dialect >= SMB30_PROT_ID) { 1631 chann = lookup_chann_list(sess, conn); 1632 if (!chann) { 1633 chann = kmalloc(sizeof(struct channel), GFP_KERNEL); 1634 if (!chann) 1635 return -ENOMEM; 1636 1637 chann->conn = conn; 1638 xa_store(&sess->ksmbd_chann_list, (long)conn, chann, GFP_KERNEL); 1639 } 1640 } 1641 1642 if (conn->ops->generate_signingkey) { 1643 retval = conn->ops->generate_signingkey(sess, conn); 1644 if (retval) { 1645 ksmbd_debug(SMB, "SMB3 signing key generation failed\n"); 1646 return -EINVAL; 1647 } 1648 } 1649 1650 if (!ksmbd_conn_lookup_dialect(conn)) { 1651 pr_err("fail to verify the dialect\n"); 1652 return -ENOENT; 1653 } 1654 return 0; 1655 } 1656 #else 1657 static int krb5_authenticate(struct ksmbd_work *work, 1658 struct smb2_sess_setup_req *req, 1659 struct smb2_sess_setup_rsp *rsp) 1660 { 1661 return -EOPNOTSUPP; 1662 } 1663 #endif 1664 1665 int smb2_sess_setup(struct ksmbd_work *work) 1666 { 1667 struct ksmbd_conn *conn = work->conn; 1668 struct smb2_sess_setup_req *req; 1669 struct smb2_sess_setup_rsp *rsp; 1670 struct ksmbd_session *sess; 1671 struct negotiate_message *negblob; 1672 unsigned int negblob_len, negblob_off; 1673 int rc = 0; 1674 1675 ksmbd_debug(SMB, "Received request for session setup\n"); 1676 1677 WORK_BUFFERS(work, req, rsp); 1678 1679 rsp->StructureSize = cpu_to_le16(9); 1680 rsp->SessionFlags = 0; 1681 rsp->SecurityBufferOffset = cpu_to_le16(72); 1682 rsp->SecurityBufferLength = 0; 1683 1684 ksmbd_conn_lock(conn); 1685 if (!req->hdr.SessionId) { 1686 sess = ksmbd_smb2_session_create(); 1687 if (!sess) { 1688 rc = -ENOMEM; 1689 goto out_err; 1690 } 1691 rsp->hdr.SessionId = cpu_to_le64(sess->id); 1692 rc = ksmbd_session_register(conn, sess); 1693 if (rc) 1694 goto out_err; 1695 1696 conn->binding = false; 1697 } else if (conn->dialect >= SMB30_PROT_ID && 1698 (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) && 1699 req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) { 1700 u64 sess_id = le64_to_cpu(req->hdr.SessionId); 1701 1702 sess = ksmbd_session_lookup_slowpath(sess_id); 1703 if (!sess) { 1704 rc = -ENOENT; 1705 goto out_err; 1706 } 1707 1708 if (conn->dialect != sess->dialect) { 1709 rc = -EINVAL; 1710 ksmbd_user_session_put(sess); 1711 goto out_err; 1712 } 1713 1714 if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) { 1715 rc = -EINVAL; 1716 ksmbd_user_session_put(sess); 1717 goto out_err; 1718 } 1719 1720 if (strncmp(conn->ClientGUID, sess->ClientGUID, 1721 SMB2_CLIENT_GUID_SIZE)) { 1722 rc = -ENOENT; 1723 ksmbd_user_session_put(sess); 1724 goto out_err; 1725 } 1726 1727 if (sess->state == SMB2_SESSION_IN_PROGRESS) { 1728 rc = -EACCES; 1729 ksmbd_user_session_put(sess); 1730 goto out_err; 1731 } 1732 1733 if (sess->state == SMB2_SESSION_EXPIRED) { 1734 rc = -EFAULT; 1735 ksmbd_user_session_put(sess); 1736 goto out_err; 1737 } 1738 ksmbd_user_session_put(sess); 1739 1740 if (ksmbd_conn_need_reconnect(conn)) { 1741 rc = -EFAULT; 1742 sess = NULL; 1743 goto out_err; 1744 } 1745 1746 sess = ksmbd_session_lookup(conn, sess_id); 1747 if (!sess) { 1748 rc = -EACCES; 1749 goto out_err; 1750 } 1751 1752 if (user_guest(sess->user)) { 1753 rc = -EOPNOTSUPP; 1754 goto out_err; 1755 } 1756 1757 conn->binding = true; 1758 } else if ((conn->dialect < SMB30_PROT_ID || 1759 server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) && 1760 (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { 1761 sess = NULL; 1762 rc = -EACCES; 1763 goto out_err; 1764 } else { 1765 sess = ksmbd_session_lookup(conn, 1766 le64_to_cpu(req->hdr.SessionId)); 1767 if (!sess) { 1768 rc = -ENOENT; 1769 goto out_err; 1770 } 1771 1772 if (sess->state == SMB2_SESSION_EXPIRED) { 1773 rc = -EFAULT; 1774 goto out_err; 1775 } 1776 1777 if (ksmbd_conn_need_reconnect(conn)) { 1778 rc = -EFAULT; 1779 sess = NULL; 1780 goto out_err; 1781 } 1782 1783 conn->binding = false; 1784 } 1785 work->sess = sess; 1786 1787 negblob_off = le16_to_cpu(req->SecurityBufferOffset); 1788 negblob_len = le16_to_cpu(req->SecurityBufferLength); 1789 if (negblob_off < offsetof(struct smb2_sess_setup_req, Buffer)) { 1790 rc = -EINVAL; 1791 goto out_err; 1792 } 1793 1794 negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId + 1795 negblob_off); 1796 1797 if (decode_negotiation_token(conn, negblob, negblob_len) == 0) { 1798 if (conn->mechToken) { 1799 negblob = (struct negotiate_message *)conn->mechToken; 1800 negblob_len = conn->mechTokenLen; 1801 } 1802 } 1803 1804 if (negblob_len < offsetof(struct negotiate_message, NegotiateFlags)) { 1805 rc = -EINVAL; 1806 goto out_err; 1807 } 1808 1809 if (server_conf.auth_mechs & conn->auth_mechs) { 1810 rc = generate_preauth_hash(work); 1811 if (rc) 1812 goto out_err; 1813 1814 if (conn->preferred_auth_mech & 1815 (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) { 1816 rc = krb5_authenticate(work, req, rsp); 1817 if (rc) { 1818 rc = -EINVAL; 1819 goto out_err; 1820 } 1821 1822 if (!ksmbd_conn_need_reconnect(conn)) { 1823 ksmbd_conn_set_good(conn); 1824 sess->state = SMB2_SESSION_VALID; 1825 } 1826 kfree(sess->Preauth_HashValue); 1827 sess->Preauth_HashValue = NULL; 1828 } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) { 1829 if (negblob->MessageType == NtLmNegotiate) { 1830 rc = ntlm_negotiate(work, negblob, negblob_len, rsp); 1831 if (rc) 1832 goto out_err; 1833 rsp->hdr.Status = 1834 STATUS_MORE_PROCESSING_REQUIRED; 1835 } else if (negblob->MessageType == NtLmAuthenticate) { 1836 rc = ntlm_authenticate(work, req, rsp); 1837 if (rc) 1838 goto out_err; 1839 1840 if (!ksmbd_conn_need_reconnect(conn)) { 1841 ksmbd_conn_set_good(conn); 1842 sess->state = SMB2_SESSION_VALID; 1843 } 1844 if (conn->binding) { 1845 struct preauth_session *preauth_sess; 1846 1847 preauth_sess = 1848 ksmbd_preauth_session_lookup(conn, sess->id); 1849 if (preauth_sess) { 1850 list_del(&preauth_sess->preauth_entry); 1851 kfree(preauth_sess); 1852 } 1853 } 1854 kfree(sess->Preauth_HashValue); 1855 sess->Preauth_HashValue = NULL; 1856 } else { 1857 pr_info_ratelimited("Unknown NTLMSSP message type : 0x%x\n", 1858 le32_to_cpu(negblob->MessageType)); 1859 rc = -EINVAL; 1860 } 1861 } else { 1862 /* TODO: need one more negotiation */ 1863 pr_err("Not support the preferred authentication\n"); 1864 rc = -EINVAL; 1865 } 1866 } else { 1867 pr_err("Not support authentication\n"); 1868 rc = -EINVAL; 1869 } 1870 1871 out_err: 1872 if (rc == -EINVAL) 1873 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 1874 else if (rc == -ENOENT) 1875 rsp->hdr.Status = STATUS_USER_SESSION_DELETED; 1876 else if (rc == -EACCES) 1877 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; 1878 else if (rc == -EFAULT) 1879 rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED; 1880 else if (rc == -ENOMEM) 1881 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; 1882 else if (rc == -EOPNOTSUPP) 1883 rsp->hdr.Status = STATUS_NOT_SUPPORTED; 1884 else if (rc) 1885 rsp->hdr.Status = STATUS_LOGON_FAILURE; 1886 1887 if (conn->use_spnego && conn->mechToken) { 1888 kfree(conn->mechToken); 1889 conn->mechToken = NULL; 1890 } 1891 1892 if (rc < 0) { 1893 /* 1894 * SecurityBufferOffset should be set to zero 1895 * in session setup error response. 1896 */ 1897 rsp->SecurityBufferOffset = 0; 1898 1899 if (sess) { 1900 bool try_delay = false; 1901 1902 /* 1903 * To avoid dictionary attacks (repeated session setups rapidly sent) to 1904 * connect to server, ksmbd make a delay of a 5 seconds on session setup 1905 * failure to make it harder to send enough random connection requests 1906 * to break into a server. 1907 */ 1908 if (sess->user && sess->user->flags & KSMBD_USER_FLAG_DELAY_SESSION) 1909 try_delay = true; 1910 1911 sess->last_active = jiffies; 1912 sess->state = SMB2_SESSION_EXPIRED; 1913 if (try_delay) { 1914 ksmbd_conn_set_need_reconnect(conn); 1915 ssleep(5); 1916 ksmbd_conn_set_need_negotiate(conn); 1917 } 1918 } 1919 smb2_set_err_rsp(work); 1920 } else { 1921 unsigned int iov_len; 1922 1923 if (rsp->SecurityBufferLength) 1924 iov_len = offsetof(struct smb2_sess_setup_rsp, Buffer) + 1925 le16_to_cpu(rsp->SecurityBufferLength); 1926 else 1927 iov_len = sizeof(struct smb2_sess_setup_rsp); 1928 rc = ksmbd_iov_pin_rsp(work, rsp, iov_len); 1929 if (rc) 1930 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; 1931 } 1932 1933 ksmbd_conn_unlock(conn); 1934 return rc; 1935 } 1936 1937 /** 1938 * smb2_tree_connect() - handler for smb2 tree connect command 1939 * @work: smb work containing smb request buffer 1940 * 1941 * Return: 0 on success, otherwise error 1942 */ 1943 int smb2_tree_connect(struct ksmbd_work *work) 1944 { 1945 struct ksmbd_conn *conn = work->conn; 1946 struct smb2_tree_connect_req *req; 1947 struct smb2_tree_connect_rsp *rsp; 1948 struct ksmbd_session *sess = work->sess; 1949 char *treename = NULL, *name = NULL; 1950 struct ksmbd_tree_conn_status status; 1951 struct ksmbd_share_config *share = NULL; 1952 int rc = -EINVAL; 1953 1954 WORK_BUFFERS(work, req, rsp); 1955 1956 treename = smb_strndup_from_utf16((char *)req + le16_to_cpu(req->PathOffset), 1957 le16_to_cpu(req->PathLength), true, 1958 conn->local_nls); 1959 if (IS_ERR(treename)) { 1960 pr_err("treename is NULL\n"); 1961 status.ret = KSMBD_TREE_CONN_STATUS_ERROR; 1962 goto out_err1; 1963 } 1964 1965 name = ksmbd_extract_sharename(conn->um, treename); 1966 if (IS_ERR(name)) { 1967 status.ret = KSMBD_TREE_CONN_STATUS_ERROR; 1968 goto out_err1; 1969 } 1970 1971 ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n", 1972 name, treename); 1973 1974 status = ksmbd_tree_conn_connect(work, name); 1975 if (status.ret == KSMBD_TREE_CONN_STATUS_OK) 1976 rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id); 1977 else 1978 goto out_err1; 1979 1980 share = status.tree_conn->share_conf; 1981 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { 1982 ksmbd_debug(SMB, "IPC share path request\n"); 1983 rsp->ShareType = SMB2_SHARE_TYPE_PIPE; 1984 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | 1985 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE | 1986 FILE_DELETE_LE | FILE_READ_CONTROL_LE | 1987 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | 1988 FILE_SYNCHRONIZE_LE; 1989 } else { 1990 rsp->ShareType = SMB2_SHARE_TYPE_DISK; 1991 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | 1992 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE; 1993 if (test_tree_conn_flag(status.tree_conn, 1994 KSMBD_TREE_CONN_FLAG_WRITABLE)) { 1995 rsp->MaximalAccess |= FILE_WRITE_DATA_LE | 1996 FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE | 1997 FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE | 1998 FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE | 1999 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | 2000 FILE_SYNCHRONIZE_LE; 2001 } 2002 } 2003 2004 status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess); 2005 if (conn->posix_ext_supported) 2006 status.tree_conn->posix_extensions = true; 2007 2008 write_lock(&sess->tree_conns_lock); 2009 status.tree_conn->t_state = TREE_CONNECTED; 2010 write_unlock(&sess->tree_conns_lock); 2011 rsp->StructureSize = cpu_to_le16(16); 2012 out_err1: 2013 if (server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE && share && 2014 test_share_config_flag(share, 2015 KSMBD_SHARE_FLAG_CONTINUOUS_AVAILABILITY)) 2016 rsp->Capabilities = SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY; 2017 else 2018 rsp->Capabilities = 0; 2019 rsp->Reserved = 0; 2020 /* default manual caching */ 2021 rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING; 2022 2023 rc = ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_tree_connect_rsp)); 2024 if (rc) 2025 status.ret = KSMBD_TREE_CONN_STATUS_NOMEM; 2026 2027 if (!IS_ERR(treename)) 2028 kfree(treename); 2029 if (!IS_ERR(name)) 2030 kfree(name); 2031 2032 switch (status.ret) { 2033 case KSMBD_TREE_CONN_STATUS_OK: 2034 rsp->hdr.Status = STATUS_SUCCESS; 2035 rc = 0; 2036 break; 2037 case -ESTALE: 2038 case -ENOENT: 2039 case KSMBD_TREE_CONN_STATUS_NO_SHARE: 2040 rsp->hdr.Status = STATUS_BAD_NETWORK_NAME; 2041 break; 2042 case -ENOMEM: 2043 case KSMBD_TREE_CONN_STATUS_NOMEM: 2044 rsp->hdr.Status = STATUS_NO_MEMORY; 2045 break; 2046 case KSMBD_TREE_CONN_STATUS_ERROR: 2047 case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS: 2048 case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS: 2049 rsp->hdr.Status = STATUS_ACCESS_DENIED; 2050 break; 2051 case -EINVAL: 2052 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 2053 break; 2054 default: 2055 rsp->hdr.Status = STATUS_ACCESS_DENIED; 2056 } 2057 2058 if (status.ret != KSMBD_TREE_CONN_STATUS_OK) 2059 smb2_set_err_rsp(work); 2060 2061 return rc; 2062 } 2063 2064 /** 2065 * smb2_create_open_flags() - convert smb open flags to unix open flags 2066 * @file_present: is file already present 2067 * @access: file access flags 2068 * @disposition: file disposition flags 2069 * @may_flags: set with MAY_ flags 2070 * @is_dir: is creating open flags for directory 2071 * 2072 * Return: file open flags 2073 */ 2074 static int smb2_create_open_flags(bool file_present, __le32 access, 2075 __le32 disposition, 2076 int *may_flags, 2077 bool is_dir) 2078 { 2079 int oflags = O_NONBLOCK | O_LARGEFILE; 2080 2081 if (is_dir) { 2082 access &= ~FILE_WRITE_DESIRE_ACCESS_LE; 2083 ksmbd_debug(SMB, "Discard write access to a directory\n"); 2084 } 2085 2086 if (access & FILE_READ_DESIRED_ACCESS_LE && 2087 access & FILE_WRITE_DESIRE_ACCESS_LE) { 2088 oflags |= O_RDWR; 2089 *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE; 2090 } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) { 2091 oflags |= O_WRONLY; 2092 *may_flags = MAY_OPEN | MAY_WRITE; 2093 } else { 2094 oflags |= O_RDONLY; 2095 *may_flags = MAY_OPEN | MAY_READ; 2096 } 2097 2098 if (access == FILE_READ_ATTRIBUTES_LE) 2099 oflags |= O_PATH; 2100 2101 if (file_present) { 2102 switch (disposition & FILE_CREATE_MASK_LE) { 2103 case FILE_OPEN_LE: 2104 case FILE_CREATE_LE: 2105 break; 2106 case FILE_SUPERSEDE_LE: 2107 case FILE_OVERWRITE_LE: 2108 case FILE_OVERWRITE_IF_LE: 2109 oflags |= O_TRUNC; 2110 break; 2111 default: 2112 break; 2113 } 2114 } else { 2115 switch (disposition & FILE_CREATE_MASK_LE) { 2116 case FILE_SUPERSEDE_LE: 2117 case FILE_CREATE_LE: 2118 case FILE_OPEN_IF_LE: 2119 case FILE_OVERWRITE_IF_LE: 2120 oflags |= O_CREAT; 2121 break; 2122 case FILE_OPEN_LE: 2123 case FILE_OVERWRITE_LE: 2124 oflags &= ~O_CREAT; 2125 break; 2126 default: 2127 break; 2128 } 2129 } 2130 2131 return oflags; 2132 } 2133 2134 /** 2135 * smb2_tree_disconnect() - handler for smb tree connect request 2136 * @work: smb work containing request buffer 2137 * 2138 * Return: 0 2139 */ 2140 int smb2_tree_disconnect(struct ksmbd_work *work) 2141 { 2142 struct smb2_tree_disconnect_rsp *rsp; 2143 struct smb2_tree_disconnect_req *req; 2144 struct ksmbd_session *sess = work->sess; 2145 struct ksmbd_tree_connect *tcon = work->tcon; 2146 int err; 2147 2148 WORK_BUFFERS(work, req, rsp); 2149 2150 ksmbd_debug(SMB, "request\n"); 2151 2152 if (!tcon) { 2153 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); 2154 2155 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; 2156 err = -ENOENT; 2157 goto err_out; 2158 } 2159 2160 ksmbd_close_tree_conn_fds(work); 2161 2162 write_lock(&sess->tree_conns_lock); 2163 if (tcon->t_state == TREE_DISCONNECTED) { 2164 write_unlock(&sess->tree_conns_lock); 2165 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; 2166 err = -ENOENT; 2167 goto err_out; 2168 } 2169 2170 WARN_ON_ONCE(atomic_dec_and_test(&tcon->refcount)); 2171 tcon->t_state = TREE_DISCONNECTED; 2172 write_unlock(&sess->tree_conns_lock); 2173 2174 err = ksmbd_tree_conn_disconnect(sess, tcon); 2175 if (err) { 2176 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; 2177 goto err_out; 2178 } 2179 2180 work->tcon = NULL; 2181 2182 rsp->StructureSize = cpu_to_le16(4); 2183 err = ksmbd_iov_pin_rsp(work, rsp, 2184 sizeof(struct smb2_tree_disconnect_rsp)); 2185 if (err) { 2186 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; 2187 goto err_out; 2188 } 2189 2190 return 0; 2191 2192 err_out: 2193 smb2_set_err_rsp(work); 2194 return err; 2195 2196 } 2197 2198 /** 2199 * smb2_session_logoff() - handler for session log off request 2200 * @work: smb work containing request buffer 2201 * 2202 * Return: 0 2203 */ 2204 int smb2_session_logoff(struct ksmbd_work *work) 2205 { 2206 struct ksmbd_conn *conn = work->conn; 2207 struct ksmbd_session *sess = work->sess; 2208 struct smb2_logoff_req *req; 2209 struct smb2_logoff_rsp *rsp; 2210 u64 sess_id; 2211 int err; 2212 2213 WORK_BUFFERS(work, req, rsp); 2214 2215 ksmbd_debug(SMB, "request\n"); 2216 2217 ksmbd_conn_lock(conn); 2218 if (!ksmbd_conn_good(conn)) { 2219 ksmbd_conn_unlock(conn); 2220 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; 2221 smb2_set_err_rsp(work); 2222 return -ENOENT; 2223 } 2224 sess_id = le64_to_cpu(req->hdr.SessionId); 2225 ksmbd_all_conn_set_status(sess_id, KSMBD_SESS_NEED_RECONNECT); 2226 ksmbd_conn_unlock(conn); 2227 2228 ksmbd_close_session_fds(work); 2229 ksmbd_conn_wait_idle(conn); 2230 2231 if (ksmbd_tree_conn_session_logoff(sess)) { 2232 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); 2233 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; 2234 smb2_set_err_rsp(work); 2235 return -ENOENT; 2236 } 2237 2238 ksmbd_destroy_file_table(&sess->file_table); 2239 down_write(&conn->session_lock); 2240 sess->state = SMB2_SESSION_EXPIRED; 2241 up_write(&conn->session_lock); 2242 2243 ksmbd_free_user(sess->user); 2244 sess->user = NULL; 2245 ksmbd_all_conn_set_status(sess_id, KSMBD_SESS_NEED_NEGOTIATE); 2246 2247 rsp->StructureSize = cpu_to_le16(4); 2248 err = ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_logoff_rsp)); 2249 if (err) { 2250 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; 2251 smb2_set_err_rsp(work); 2252 return err; 2253 } 2254 return 0; 2255 } 2256 2257 /** 2258 * create_smb2_pipe() - create IPC pipe 2259 * @work: smb work containing request buffer 2260 * 2261 * Return: 0 on success, otherwise error 2262 */ 2263 static noinline int create_smb2_pipe(struct ksmbd_work *work) 2264 { 2265 struct smb2_create_rsp *rsp; 2266 struct smb2_create_req *req; 2267 int id; 2268 int err; 2269 char *name; 2270 2271 WORK_BUFFERS(work, req, rsp); 2272 2273 name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength), 2274 1, work->conn->local_nls); 2275 if (IS_ERR(name)) { 2276 rsp->hdr.Status = STATUS_NO_MEMORY; 2277 err = PTR_ERR(name); 2278 goto out; 2279 } 2280 2281 id = ksmbd_session_rpc_open(work->sess, name); 2282 if (id < 0) { 2283 pr_err("Unable to open RPC pipe: %d\n", id); 2284 err = id; 2285 goto out; 2286 } 2287 2288 rsp->hdr.Status = STATUS_SUCCESS; 2289 rsp->StructureSize = cpu_to_le16(89); 2290 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE; 2291 rsp->Flags = 0; 2292 rsp->CreateAction = cpu_to_le32(FILE_OPENED); 2293 2294 rsp->CreationTime = cpu_to_le64(0); 2295 rsp->LastAccessTime = cpu_to_le64(0); 2296 rsp->ChangeTime = cpu_to_le64(0); 2297 rsp->AllocationSize = cpu_to_le64(0); 2298 rsp->EndofFile = cpu_to_le64(0); 2299 rsp->FileAttributes = FILE_ATTRIBUTE_NORMAL_LE; 2300 rsp->Reserved2 = 0; 2301 rsp->VolatileFileId = id; 2302 rsp->PersistentFileId = 0; 2303 rsp->CreateContextsOffset = 0; 2304 rsp->CreateContextsLength = 0; 2305 2306 err = ksmbd_iov_pin_rsp(work, rsp, offsetof(struct smb2_create_rsp, Buffer)); 2307 if (err) 2308 goto out; 2309 2310 kfree(name); 2311 return 0; 2312 2313 out: 2314 switch (err) { 2315 case -EINVAL: 2316 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 2317 break; 2318 case -ENOSPC: 2319 case -ENOMEM: 2320 rsp->hdr.Status = STATUS_NO_MEMORY; 2321 break; 2322 } 2323 2324 if (!IS_ERR(name)) 2325 kfree(name); 2326 2327 smb2_set_err_rsp(work); 2328 return err; 2329 } 2330 2331 /** 2332 * smb2_set_ea() - handler for setting extended attributes using set 2333 * info command 2334 * @eabuf: set info command buffer 2335 * @buf_len: set info command buffer length 2336 * @path: dentry path for get ea 2337 * @get_write: get write access to a mount 2338 * 2339 * Return: 0 on success, otherwise error 2340 */ 2341 static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len, 2342 const struct path *path, bool get_write) 2343 { 2344 struct mnt_idmap *idmap = mnt_idmap(path->mnt); 2345 char *attr_name = NULL, *value; 2346 int rc = 0; 2347 unsigned int next = 0; 2348 2349 if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength + 2350 le16_to_cpu(eabuf->EaValueLength)) 2351 return -EINVAL; 2352 2353 attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL); 2354 if (!attr_name) 2355 return -ENOMEM; 2356 2357 do { 2358 if (!eabuf->EaNameLength) 2359 goto next; 2360 2361 ksmbd_debug(SMB, 2362 "name : <%s>, name_len : %u, value_len : %u, next : %u\n", 2363 eabuf->name, eabuf->EaNameLength, 2364 le16_to_cpu(eabuf->EaValueLength), 2365 le32_to_cpu(eabuf->NextEntryOffset)); 2366 2367 if (eabuf->EaNameLength > 2368 (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) { 2369 rc = -EINVAL; 2370 break; 2371 } 2372 2373 memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); 2374 memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name, 2375 eabuf->EaNameLength); 2376 attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0'; 2377 value = (char *)&eabuf->name + eabuf->EaNameLength + 1; 2378 2379 if (!eabuf->EaValueLength) { 2380 rc = ksmbd_vfs_casexattr_len(idmap, 2381 path->dentry, 2382 attr_name, 2383 XATTR_USER_PREFIX_LEN + 2384 eabuf->EaNameLength); 2385 2386 /* delete the EA only when it exits */ 2387 if (rc > 0) { 2388 rc = ksmbd_vfs_remove_xattr(idmap, 2389 path, 2390 attr_name, 2391 get_write); 2392 2393 if (rc < 0) { 2394 ksmbd_debug(SMB, 2395 "remove xattr failed(%d)\n", 2396 rc); 2397 break; 2398 } 2399 } 2400 2401 /* if the EA doesn't exist, just do nothing. */ 2402 rc = 0; 2403 } else { 2404 rc = ksmbd_vfs_setxattr(idmap, path, attr_name, value, 2405 le16_to_cpu(eabuf->EaValueLength), 2406 0, get_write); 2407 if (rc < 0) { 2408 ksmbd_debug(SMB, 2409 "ksmbd_vfs_setxattr is failed(%d)\n", 2410 rc); 2411 break; 2412 } 2413 } 2414 2415 next: 2416 next = le32_to_cpu(eabuf->NextEntryOffset); 2417 if (next == 0 || buf_len < next) 2418 break; 2419 buf_len -= next; 2420 eabuf = (struct smb2_ea_info *)((char *)eabuf + next); 2421 if (buf_len < sizeof(struct smb2_ea_info)) { 2422 rc = -EINVAL; 2423 break; 2424 } 2425 2426 if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength + 2427 le16_to_cpu(eabuf->EaValueLength)) { 2428 rc = -EINVAL; 2429 break; 2430 } 2431 } while (next != 0); 2432 2433 kfree(attr_name); 2434 return rc; 2435 } 2436 2437 static noinline int smb2_set_stream_name_xattr(const struct path *path, 2438 struct ksmbd_file *fp, 2439 char *stream_name, int s_type) 2440 { 2441 struct mnt_idmap *idmap = mnt_idmap(path->mnt); 2442 size_t xattr_stream_size; 2443 char *xattr_stream_name; 2444 int rc; 2445 2446 rc = ksmbd_vfs_xattr_stream_name(stream_name, 2447 &xattr_stream_name, 2448 &xattr_stream_size, 2449 s_type); 2450 if (rc) 2451 return rc; 2452 2453 fp->stream.name = xattr_stream_name; 2454 fp->stream.size = xattr_stream_size; 2455 2456 /* Check if there is stream prefix in xattr space */ 2457 rc = ksmbd_vfs_casexattr_len(idmap, 2458 path->dentry, 2459 xattr_stream_name, 2460 xattr_stream_size); 2461 if (rc >= 0) 2462 return 0; 2463 2464 if (fp->cdoption == FILE_OPEN_LE) { 2465 ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc); 2466 return -EBADF; 2467 } 2468 2469 rc = ksmbd_vfs_setxattr(idmap, path, xattr_stream_name, NULL, 0, 0, false); 2470 if (rc < 0) 2471 pr_err("Failed to store XATTR stream name :%d\n", rc); 2472 return 0; 2473 } 2474 2475 static int smb2_remove_smb_xattrs(const struct path *path) 2476 { 2477 struct mnt_idmap *idmap = mnt_idmap(path->mnt); 2478 char *name, *xattr_list = NULL; 2479 ssize_t xattr_list_len; 2480 int err = 0; 2481 2482 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); 2483 if (xattr_list_len < 0) { 2484 goto out; 2485 } else if (!xattr_list_len) { 2486 ksmbd_debug(SMB, "empty xattr in the file\n"); 2487 goto out; 2488 } 2489 2490 for (name = xattr_list; name - xattr_list < xattr_list_len; 2491 name += strlen(name) + 1) { 2492 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); 2493 2494 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) && 2495 !strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, 2496 STREAM_PREFIX_LEN)) { 2497 err = ksmbd_vfs_remove_xattr(idmap, path, 2498 name, true); 2499 if (err) 2500 ksmbd_debug(SMB, "remove xattr failed : %s\n", 2501 name); 2502 } 2503 } 2504 out: 2505 kvfree(xattr_list); 2506 return err; 2507 } 2508 2509 static int smb2_create_truncate(const struct path *path) 2510 { 2511 int rc = vfs_truncate(path, 0); 2512 2513 if (rc) { 2514 pr_err("vfs_truncate failed, rc %d\n", rc); 2515 return rc; 2516 } 2517 2518 rc = smb2_remove_smb_xattrs(path); 2519 if (rc == -EOPNOTSUPP) 2520 rc = 0; 2521 if (rc) 2522 ksmbd_debug(SMB, 2523 "ksmbd_truncate_stream_name_xattr failed, rc %d\n", 2524 rc); 2525 return rc; 2526 } 2527 2528 static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, const struct path *path, 2529 struct ksmbd_file *fp) 2530 { 2531 struct xattr_dos_attrib da = {0}; 2532 int rc; 2533 2534 if (!test_share_config_flag(tcon->share_conf, 2535 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) 2536 return; 2537 2538 da.version = 4; 2539 da.attr = le32_to_cpu(fp->f_ci->m_fattr); 2540 da.itime = da.create_time = fp->create_time; 2541 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | 2542 XATTR_DOSINFO_ITIME; 2543 2544 rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_idmap(path->mnt), path, &da, true); 2545 if (rc) 2546 ksmbd_debug(SMB, "failed to store file attribute into xattr\n"); 2547 } 2548 2549 static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon, 2550 const struct path *path, struct ksmbd_file *fp) 2551 { 2552 struct xattr_dos_attrib da; 2553 int rc; 2554 2555 fp->f_ci->m_fattr &= ~(FILE_ATTRIBUTE_HIDDEN_LE | FILE_ATTRIBUTE_SYSTEM_LE); 2556 2557 /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */ 2558 if (!test_share_config_flag(tcon->share_conf, 2559 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) 2560 return; 2561 2562 rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_idmap(path->mnt), 2563 path->dentry, &da); 2564 if (rc > 0) { 2565 fp->f_ci->m_fattr = cpu_to_le32(da.attr); 2566 fp->create_time = da.create_time; 2567 fp->itime = da.itime; 2568 } 2569 } 2570 2571 static int smb2_creat(struct ksmbd_work *work, struct path *parent_path, 2572 struct path *path, char *name, int open_flags, 2573 umode_t posix_mode, bool is_dir) 2574 { 2575 struct ksmbd_tree_connect *tcon = work->tcon; 2576 struct ksmbd_share_config *share = tcon->share_conf; 2577 umode_t mode; 2578 int rc; 2579 2580 if (!(open_flags & O_CREAT)) 2581 return -EBADF; 2582 2583 ksmbd_debug(SMB, "file does not exist, so creating\n"); 2584 if (is_dir == true) { 2585 ksmbd_debug(SMB, "creating directory\n"); 2586 2587 mode = share_config_directory_mode(share, posix_mode); 2588 rc = ksmbd_vfs_mkdir(work, name, mode); 2589 if (rc) 2590 return rc; 2591 } else { 2592 ksmbd_debug(SMB, "creating regular file\n"); 2593 2594 mode = share_config_create_mode(share, posix_mode); 2595 rc = ksmbd_vfs_create(work, name, mode); 2596 if (rc) 2597 return rc; 2598 } 2599 2600 rc = ksmbd_vfs_kern_path_locked(work, name, 0, parent_path, path, 0); 2601 if (rc) { 2602 pr_err("cannot get linux path (%s), err = %d\n", 2603 name, rc); 2604 return rc; 2605 } 2606 return 0; 2607 } 2608 2609 static int smb2_create_sd_buffer(struct ksmbd_work *work, 2610 struct smb2_create_req *req, 2611 const struct path *path) 2612 { 2613 struct create_context *context; 2614 struct create_sd_buf_req *sd_buf; 2615 2616 if (!req->CreateContextsOffset) 2617 return -ENOENT; 2618 2619 /* Parse SD BUFFER create contexts */ 2620 context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER, 4); 2621 if (!context) 2622 return -ENOENT; 2623 else if (IS_ERR(context)) 2624 return PTR_ERR(context); 2625 2626 ksmbd_debug(SMB, 2627 "Set ACLs using SMB2_CREATE_SD_BUFFER context\n"); 2628 sd_buf = (struct create_sd_buf_req *)context; 2629 if (le16_to_cpu(context->DataOffset) + 2630 le32_to_cpu(context->DataLength) < 2631 sizeof(struct create_sd_buf_req)) 2632 return -EINVAL; 2633 return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd, 2634 le32_to_cpu(sd_buf->ccontext.DataLength), true, false); 2635 } 2636 2637 static void ksmbd_acls_fattr(struct smb_fattr *fattr, 2638 struct mnt_idmap *idmap, 2639 struct inode *inode) 2640 { 2641 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); 2642 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); 2643 2644 fattr->cf_uid = vfsuid_into_kuid(vfsuid); 2645 fattr->cf_gid = vfsgid_into_kgid(vfsgid); 2646 fattr->cf_mode = inode->i_mode; 2647 fattr->cf_acls = NULL; 2648 fattr->cf_dacls = NULL; 2649 2650 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { 2651 fattr->cf_acls = get_inode_acl(inode, ACL_TYPE_ACCESS); 2652 if (S_ISDIR(inode->i_mode)) 2653 fattr->cf_dacls = get_inode_acl(inode, ACL_TYPE_DEFAULT); 2654 } 2655 } 2656 2657 enum { 2658 DURABLE_RECONN_V2 = 1, 2659 DURABLE_RECONN, 2660 DURABLE_REQ_V2, 2661 DURABLE_REQ, 2662 }; 2663 2664 struct durable_info { 2665 struct ksmbd_file *fp; 2666 unsigned short int type; 2667 bool persistent; 2668 bool reconnected; 2669 unsigned int timeout; 2670 char *CreateGuid; 2671 }; 2672 2673 static int parse_durable_handle_context(struct ksmbd_work *work, 2674 struct smb2_create_req *req, 2675 struct lease_ctx_info *lc, 2676 struct durable_info *dh_info) 2677 { 2678 struct ksmbd_conn *conn = work->conn; 2679 struct create_context *context; 2680 int dh_idx, err = 0; 2681 u64 persistent_id = 0; 2682 int req_op_level; 2683 static const char * const durable_arr[] = {"DH2C", "DHnC", "DH2Q", "DHnQ"}; 2684 2685 req_op_level = req->RequestedOplockLevel; 2686 for (dh_idx = DURABLE_RECONN_V2; dh_idx <= ARRAY_SIZE(durable_arr); 2687 dh_idx++) { 2688 context = smb2_find_context_vals(req, durable_arr[dh_idx - 1], 4); 2689 if (IS_ERR(context)) { 2690 err = PTR_ERR(context); 2691 goto out; 2692 } 2693 if (!context) 2694 continue; 2695 2696 switch (dh_idx) { 2697 case DURABLE_RECONN_V2: 2698 { 2699 struct create_durable_reconn_v2_req *recon_v2; 2700 2701 if (dh_info->type == DURABLE_RECONN || 2702 dh_info->type == DURABLE_REQ_V2) { 2703 err = -EINVAL; 2704 goto out; 2705 } 2706 2707 recon_v2 = (struct create_durable_reconn_v2_req *)context; 2708 persistent_id = recon_v2->Fid.PersistentFileId; 2709 dh_info->fp = ksmbd_lookup_durable_fd(persistent_id); 2710 if (!dh_info->fp) { 2711 ksmbd_debug(SMB, "Failed to get durable handle state\n"); 2712 err = -EBADF; 2713 goto out; 2714 } 2715 2716 if (memcmp(dh_info->fp->create_guid, recon_v2->CreateGuid, 2717 SMB2_CREATE_GUID_SIZE)) { 2718 err = -EBADF; 2719 ksmbd_put_durable_fd(dh_info->fp); 2720 goto out; 2721 } 2722 2723 dh_info->type = dh_idx; 2724 dh_info->reconnected = true; 2725 ksmbd_debug(SMB, 2726 "reconnect v2 Persistent-id from reconnect = %llu\n", 2727 persistent_id); 2728 break; 2729 } 2730 case DURABLE_RECONN: 2731 { 2732 struct create_durable_reconn_req *recon; 2733 2734 if (dh_info->type == DURABLE_RECONN_V2 || 2735 dh_info->type == DURABLE_REQ_V2) { 2736 err = -EINVAL; 2737 goto out; 2738 } 2739 2740 recon = (struct create_durable_reconn_req *)context; 2741 persistent_id = recon->Data.Fid.PersistentFileId; 2742 dh_info->fp = ksmbd_lookup_durable_fd(persistent_id); 2743 if (!dh_info->fp) { 2744 ksmbd_debug(SMB, "Failed to get durable handle state\n"); 2745 err = -EBADF; 2746 goto out; 2747 } 2748 2749 dh_info->type = dh_idx; 2750 dh_info->reconnected = true; 2751 ksmbd_debug(SMB, "reconnect Persistent-id from reconnect = %llu\n", 2752 persistent_id); 2753 break; 2754 } 2755 case DURABLE_REQ_V2: 2756 { 2757 struct create_durable_req_v2 *durable_v2_blob; 2758 2759 if (dh_info->type == DURABLE_RECONN || 2760 dh_info->type == DURABLE_RECONN_V2) { 2761 err = -EINVAL; 2762 goto out; 2763 } 2764 2765 durable_v2_blob = 2766 (struct create_durable_req_v2 *)context; 2767 ksmbd_debug(SMB, "Request for durable v2 open\n"); 2768 dh_info->fp = ksmbd_lookup_fd_cguid(durable_v2_blob->CreateGuid); 2769 if (dh_info->fp) { 2770 if (!memcmp(conn->ClientGUID, dh_info->fp->client_guid, 2771 SMB2_CLIENT_GUID_SIZE)) { 2772 if (!(req->hdr.Flags & SMB2_FLAGS_REPLAY_OPERATION)) { 2773 err = -ENOEXEC; 2774 goto out; 2775 } 2776 2777 dh_info->fp->conn = conn; 2778 dh_info->reconnected = true; 2779 goto out; 2780 } 2781 } 2782 2783 if ((lc && (lc->req_state & SMB2_LEASE_HANDLE_CACHING_LE)) || 2784 req_op_level == SMB2_OPLOCK_LEVEL_BATCH) { 2785 dh_info->CreateGuid = 2786 durable_v2_blob->CreateGuid; 2787 dh_info->persistent = 2788 le32_to_cpu(durable_v2_blob->Flags); 2789 dh_info->timeout = 2790 le32_to_cpu(durable_v2_blob->Timeout); 2791 dh_info->type = dh_idx; 2792 } 2793 break; 2794 } 2795 case DURABLE_REQ: 2796 if (dh_info->type == DURABLE_RECONN) 2797 goto out; 2798 if (dh_info->type == DURABLE_RECONN_V2 || 2799 dh_info->type == DURABLE_REQ_V2) { 2800 err = -EINVAL; 2801 goto out; 2802 } 2803 2804 if ((lc && (lc->req_state & SMB2_LEASE_HANDLE_CACHING_LE)) || 2805 req_op_level == SMB2_OPLOCK_LEVEL_BATCH) { 2806 ksmbd_debug(SMB, "Request for durable open\n"); 2807 dh_info->type = dh_idx; 2808 } 2809 } 2810 } 2811 2812 out: 2813 return err; 2814 } 2815 2816 /** 2817 * smb2_open() - handler for smb file open request 2818 * @work: smb work containing request buffer 2819 * 2820 * Return: 0 on success, otherwise error 2821 */ 2822 int smb2_open(struct ksmbd_work *work) 2823 { 2824 struct ksmbd_conn *conn = work->conn; 2825 struct ksmbd_session *sess = work->sess; 2826 struct ksmbd_tree_connect *tcon = work->tcon; 2827 struct smb2_create_req *req; 2828 struct smb2_create_rsp *rsp; 2829 struct path path, parent_path; 2830 struct ksmbd_share_config *share = tcon->share_conf; 2831 struct ksmbd_file *fp = NULL; 2832 struct file *filp = NULL; 2833 struct mnt_idmap *idmap = NULL; 2834 struct kstat stat; 2835 struct create_context *context; 2836 struct lease_ctx_info *lc = NULL; 2837 struct create_ea_buf_req *ea_buf = NULL; 2838 struct oplock_info *opinfo; 2839 struct durable_info dh_info = {0}; 2840 __le32 *next_ptr = NULL; 2841 int req_op_level = 0, open_flags = 0, may_flags = 0, file_info = 0; 2842 int rc = 0; 2843 int contxt_cnt = 0, query_disk_id = 0; 2844 int maximal_access_ctxt = 0, posix_ctxt = 0; 2845 int s_type = 0; 2846 int next_off = 0; 2847 char *name = NULL; 2848 char *stream_name = NULL; 2849 bool file_present = false, created = false, already_permitted = false; 2850 int share_ret, need_truncate = 0; 2851 u64 time; 2852 umode_t posix_mode = 0; 2853 __le32 daccess, maximal_access = 0; 2854 int iov_len = 0; 2855 2856 WORK_BUFFERS(work, req, rsp); 2857 2858 if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off && 2859 (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) { 2860 ksmbd_debug(SMB, "invalid flag in chained command\n"); 2861 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 2862 smb2_set_err_rsp(work); 2863 return -EINVAL; 2864 } 2865 2866 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { 2867 ksmbd_debug(SMB, "IPC pipe create request\n"); 2868 return create_smb2_pipe(work); 2869 } 2870 2871 if (req->NameLength) { 2872 name = smb2_get_name((char *)req + le16_to_cpu(req->NameOffset), 2873 le16_to_cpu(req->NameLength), 2874 work->conn->local_nls); 2875 if (IS_ERR(name)) { 2876 rc = PTR_ERR(name); 2877 name = NULL; 2878 goto err_out2; 2879 } 2880 2881 ksmbd_debug(SMB, "converted name = %s\n", name); 2882 if (strchr(name, ':')) { 2883 if (!test_share_config_flag(work->tcon->share_conf, 2884 KSMBD_SHARE_FLAG_STREAMS)) { 2885 rc = -EBADF; 2886 goto err_out2; 2887 } 2888 rc = parse_stream_name(name, &stream_name, &s_type); 2889 if (rc < 0) 2890 goto err_out2; 2891 } 2892 2893 rc = ksmbd_validate_filename(name); 2894 if (rc < 0) 2895 goto err_out2; 2896 2897 if (ksmbd_share_veto_filename(share, name)) { 2898 rc = -ENOENT; 2899 ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n", 2900 name); 2901 goto err_out2; 2902 } 2903 } else { 2904 name = kstrdup("", GFP_KERNEL); 2905 if (!name) { 2906 rc = -ENOMEM; 2907 goto err_out2; 2908 } 2909 } 2910 2911 req_op_level = req->RequestedOplockLevel; 2912 2913 if (server_conf.flags & KSMBD_GLOBAL_FLAG_DURABLE_HANDLE && 2914 req->CreateContextsOffset) { 2915 lc = parse_lease_state(req); 2916 rc = parse_durable_handle_context(work, req, lc, &dh_info); 2917 if (rc) { 2918 ksmbd_debug(SMB, "error parsing durable handle context\n"); 2919 goto err_out2; 2920 } 2921 2922 if (dh_info.reconnected == true) { 2923 rc = smb2_check_durable_oplock(conn, share, dh_info.fp, lc, name); 2924 if (rc) { 2925 ksmbd_put_durable_fd(dh_info.fp); 2926 goto err_out2; 2927 } 2928 2929 rc = ksmbd_reopen_durable_fd(work, dh_info.fp); 2930 if (rc) { 2931 ksmbd_put_durable_fd(dh_info.fp); 2932 goto err_out2; 2933 } 2934 2935 if (ksmbd_override_fsids(work)) { 2936 rc = -ENOMEM; 2937 ksmbd_put_durable_fd(dh_info.fp); 2938 goto err_out2; 2939 } 2940 2941 fp = dh_info.fp; 2942 file_info = FILE_OPENED; 2943 2944 rc = ksmbd_vfs_getattr(&fp->filp->f_path, &stat); 2945 if (rc) 2946 goto err_out2; 2947 2948 ksmbd_put_durable_fd(fp); 2949 goto reconnected_fp; 2950 } 2951 } else if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) 2952 lc = parse_lease_state(req); 2953 2954 if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE)) { 2955 pr_err("Invalid impersonationlevel : 0x%x\n", 2956 le32_to_cpu(req->ImpersonationLevel)); 2957 rc = -EIO; 2958 rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL; 2959 goto err_out2; 2960 } 2961 2962 if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK_LE)) { 2963 pr_err("Invalid create options : 0x%x\n", 2964 le32_to_cpu(req->CreateOptions)); 2965 rc = -EINVAL; 2966 goto err_out2; 2967 } else { 2968 if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE && 2969 req->CreateOptions & FILE_RANDOM_ACCESS_LE) 2970 req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE); 2971 2972 if (req->CreateOptions & 2973 (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION | 2974 FILE_RESERVE_OPFILTER_LE)) { 2975 rc = -EOPNOTSUPP; 2976 goto err_out2; 2977 } 2978 2979 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { 2980 if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) { 2981 rc = -EINVAL; 2982 goto err_out2; 2983 } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) { 2984 req->CreateOptions = ~(FILE_NO_COMPRESSION_LE); 2985 } 2986 } 2987 } 2988 2989 if (le32_to_cpu(req->CreateDisposition) > 2990 le32_to_cpu(FILE_OVERWRITE_IF_LE)) { 2991 pr_err("Invalid create disposition : 0x%x\n", 2992 le32_to_cpu(req->CreateDisposition)); 2993 rc = -EINVAL; 2994 goto err_out2; 2995 } 2996 2997 if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) { 2998 pr_err("Invalid desired access : 0x%x\n", 2999 le32_to_cpu(req->DesiredAccess)); 3000 rc = -EACCES; 3001 goto err_out2; 3002 } 3003 3004 if (req->FileAttributes && !(req->FileAttributes & FILE_ATTRIBUTE_MASK_LE)) { 3005 pr_err("Invalid file attribute : 0x%x\n", 3006 le32_to_cpu(req->FileAttributes)); 3007 rc = -EINVAL; 3008 goto err_out2; 3009 } 3010 3011 if (req->CreateContextsOffset) { 3012 /* Parse non-durable handle create contexts */ 3013 context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER, 4); 3014 if (IS_ERR(context)) { 3015 rc = PTR_ERR(context); 3016 goto err_out2; 3017 } else if (context) { 3018 ea_buf = (struct create_ea_buf_req *)context; 3019 if (le16_to_cpu(context->DataOffset) + 3020 le32_to_cpu(context->DataLength) < 3021 sizeof(struct create_ea_buf_req)) { 3022 rc = -EINVAL; 3023 goto err_out2; 3024 } 3025 if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) { 3026 rsp->hdr.Status = STATUS_ACCESS_DENIED; 3027 rc = -EACCES; 3028 goto err_out2; 3029 } 3030 } 3031 3032 context = smb2_find_context_vals(req, 3033 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST, 4); 3034 if (IS_ERR(context)) { 3035 rc = PTR_ERR(context); 3036 goto err_out2; 3037 } else if (context) { 3038 ksmbd_debug(SMB, 3039 "get query maximal access context\n"); 3040 maximal_access_ctxt = 1; 3041 } 3042 3043 context = smb2_find_context_vals(req, 3044 SMB2_CREATE_TIMEWARP_REQUEST, 4); 3045 if (IS_ERR(context)) { 3046 rc = PTR_ERR(context); 3047 goto err_out2; 3048 } else if (context) { 3049 ksmbd_debug(SMB, "get timewarp context\n"); 3050 rc = -EBADF; 3051 goto err_out2; 3052 } 3053 3054 if (tcon->posix_extensions) { 3055 context = smb2_find_context_vals(req, 3056 SMB2_CREATE_TAG_POSIX, 16); 3057 if (IS_ERR(context)) { 3058 rc = PTR_ERR(context); 3059 goto err_out2; 3060 } else if (context) { 3061 struct create_posix *posix = 3062 (struct create_posix *)context; 3063 if (le16_to_cpu(context->DataOffset) + 3064 le32_to_cpu(context->DataLength) < 3065 sizeof(struct create_posix) - 4) { 3066 rc = -EINVAL; 3067 goto err_out2; 3068 } 3069 ksmbd_debug(SMB, "get posix context\n"); 3070 3071 posix_mode = le32_to_cpu(posix->Mode); 3072 posix_ctxt = 1; 3073 } 3074 } 3075 } 3076 3077 if (ksmbd_override_fsids(work)) { 3078 rc = -ENOMEM; 3079 goto err_out2; 3080 } 3081 3082 rc = ksmbd_vfs_kern_path_locked(work, name, LOOKUP_NO_SYMLINKS, 3083 &parent_path, &path, 1); 3084 if (!rc) { 3085 file_present = true; 3086 3087 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) { 3088 /* 3089 * If file exists with under flags, return access 3090 * denied error. 3091 */ 3092 if (req->CreateDisposition == FILE_OVERWRITE_IF_LE || 3093 req->CreateDisposition == FILE_OPEN_IF_LE) { 3094 rc = -EACCES; 3095 goto err_out; 3096 } 3097 3098 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { 3099 ksmbd_debug(SMB, 3100 "User does not have write permission\n"); 3101 rc = -EACCES; 3102 goto err_out; 3103 } 3104 } else if (d_is_symlink(path.dentry)) { 3105 rc = -EACCES; 3106 goto err_out; 3107 } 3108 3109 file_present = true; 3110 idmap = mnt_idmap(path.mnt); 3111 } else { 3112 if (rc != -ENOENT) 3113 goto err_out; 3114 ksmbd_debug(SMB, "can not get linux path for %s, rc = %d\n", 3115 name, rc); 3116 rc = 0; 3117 } 3118 3119 if (stream_name) { 3120 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { 3121 if (s_type == DATA_STREAM) { 3122 rc = -EIO; 3123 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; 3124 } 3125 } else { 3126 if (file_present && S_ISDIR(d_inode(path.dentry)->i_mode) && 3127 s_type == DATA_STREAM) { 3128 rc = -EIO; 3129 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; 3130 } 3131 } 3132 3133 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE && 3134 req->FileAttributes & FILE_ATTRIBUTE_NORMAL_LE) { 3135 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; 3136 rc = -EIO; 3137 } 3138 3139 if (rc < 0) 3140 goto err_out; 3141 } 3142 3143 if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE && 3144 S_ISDIR(d_inode(path.dentry)->i_mode) && 3145 !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { 3146 ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n", 3147 name, req->CreateOptions); 3148 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; 3149 rc = -EIO; 3150 goto err_out; 3151 } 3152 3153 if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) && 3154 !(req->CreateDisposition == FILE_CREATE_LE) && 3155 !S_ISDIR(d_inode(path.dentry)->i_mode)) { 3156 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; 3157 rc = -EIO; 3158 goto err_out; 3159 } 3160 3161 if (!stream_name && file_present && 3162 req->CreateDisposition == FILE_CREATE_LE) { 3163 rc = -EEXIST; 3164 goto err_out; 3165 } 3166 3167 daccess = smb_map_generic_desired_access(req->DesiredAccess); 3168 3169 if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { 3170 rc = smb_check_perm_dacl(conn, &path, &daccess, 3171 sess->user->uid); 3172 if (rc) 3173 goto err_out; 3174 } 3175 3176 if (daccess & FILE_MAXIMAL_ACCESS_LE) { 3177 if (!file_present) { 3178 daccess = cpu_to_le32(GENERIC_ALL_FLAGS); 3179 } else { 3180 ksmbd_vfs_query_maximal_access(idmap, 3181 path.dentry, 3182 &daccess); 3183 already_permitted = true; 3184 } 3185 maximal_access = daccess; 3186 } 3187 3188 open_flags = smb2_create_open_flags(file_present, daccess, 3189 req->CreateDisposition, 3190 &may_flags, 3191 req->CreateOptions & FILE_DIRECTORY_FILE_LE || 3192 (file_present && S_ISDIR(d_inode(path.dentry)->i_mode))); 3193 3194 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { 3195 if (open_flags & (O_CREAT | O_TRUNC)) { 3196 ksmbd_debug(SMB, 3197 "User does not have write permission\n"); 3198 rc = -EACCES; 3199 goto err_out; 3200 } 3201 } 3202 3203 /*create file if not present */ 3204 if (!file_present) { 3205 rc = smb2_creat(work, &parent_path, &path, name, open_flags, 3206 posix_mode, 3207 req->CreateOptions & FILE_DIRECTORY_FILE_LE); 3208 if (rc) { 3209 if (rc == -ENOENT) { 3210 rc = -EIO; 3211 rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND; 3212 } 3213 goto err_out; 3214 } 3215 3216 created = true; 3217 idmap = mnt_idmap(path.mnt); 3218 if (ea_buf) { 3219 if (le32_to_cpu(ea_buf->ccontext.DataLength) < 3220 sizeof(struct smb2_ea_info)) { 3221 rc = -EINVAL; 3222 goto err_out; 3223 } 3224 3225 rc = smb2_set_ea(&ea_buf->ea, 3226 le32_to_cpu(ea_buf->ccontext.DataLength), 3227 &path, false); 3228 if (rc == -EOPNOTSUPP) 3229 rc = 0; 3230 else if (rc) 3231 goto err_out; 3232 } 3233 } else if (!already_permitted) { 3234 /* FILE_READ_ATTRIBUTE is allowed without inode_permission, 3235 * because execute(search) permission on a parent directory, 3236 * is already granted. 3237 */ 3238 if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) { 3239 rc = inode_permission(idmap, 3240 d_inode(path.dentry), 3241 may_flags); 3242 if (rc) 3243 goto err_out; 3244 3245 if ((daccess & FILE_DELETE_LE) || 3246 (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { 3247 rc = inode_permission(idmap, 3248 d_inode(path.dentry->d_parent), 3249 MAY_EXEC | MAY_WRITE); 3250 if (rc) 3251 goto err_out; 3252 } 3253 } 3254 } 3255 3256 rc = ksmbd_query_inode_status(path.dentry->d_parent); 3257 if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) { 3258 rc = -EBUSY; 3259 goto err_out; 3260 } 3261 3262 rc = 0; 3263 filp = dentry_open(&path, open_flags, current_cred()); 3264 if (IS_ERR(filp)) { 3265 rc = PTR_ERR(filp); 3266 pr_err("dentry open for dir failed, rc %d\n", rc); 3267 goto err_out; 3268 } 3269 3270 if (file_present) { 3271 if (!(open_flags & O_TRUNC)) 3272 file_info = FILE_OPENED; 3273 else 3274 file_info = FILE_OVERWRITTEN; 3275 3276 if ((req->CreateDisposition & FILE_CREATE_MASK_LE) == 3277 FILE_SUPERSEDE_LE) 3278 file_info = FILE_SUPERSEDED; 3279 } else if (open_flags & O_CREAT) { 3280 file_info = FILE_CREATED; 3281 } 3282 3283 ksmbd_vfs_set_fadvise(filp, req->CreateOptions); 3284 3285 /* Obtain Volatile-ID */ 3286 fp = ksmbd_open_fd(work, filp); 3287 if (IS_ERR(fp)) { 3288 fput(filp); 3289 rc = PTR_ERR(fp); 3290 fp = NULL; 3291 goto err_out; 3292 } 3293 3294 /* Get Persistent-ID */ 3295 ksmbd_open_durable_fd(fp); 3296 if (!has_file_id(fp->persistent_id)) { 3297 rc = -ENOMEM; 3298 goto err_out; 3299 } 3300 3301 fp->cdoption = req->CreateDisposition; 3302 fp->daccess = daccess; 3303 fp->saccess = req->ShareAccess; 3304 fp->coption = req->CreateOptions; 3305 3306 /* Set default windows and posix acls if creating new file */ 3307 if (created) { 3308 int posix_acl_rc; 3309 struct inode *inode = d_inode(path.dentry); 3310 3311 posix_acl_rc = ksmbd_vfs_inherit_posix_acl(idmap, 3312 &path, 3313 d_inode(path.dentry->d_parent)); 3314 if (posix_acl_rc) 3315 ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc); 3316 3317 if (test_share_config_flag(work->tcon->share_conf, 3318 KSMBD_SHARE_FLAG_ACL_XATTR)) { 3319 rc = smb_inherit_dacl(conn, &path, sess->user->uid, 3320 sess->user->gid); 3321 } 3322 3323 if (rc) { 3324 rc = smb2_create_sd_buffer(work, req, &path); 3325 if (rc) { 3326 if (posix_acl_rc) 3327 ksmbd_vfs_set_init_posix_acl(idmap, 3328 &path); 3329 3330 if (test_share_config_flag(work->tcon->share_conf, 3331 KSMBD_SHARE_FLAG_ACL_XATTR)) { 3332 struct smb_fattr fattr; 3333 struct smb_ntsd *pntsd; 3334 int pntsd_size, ace_num = 0; 3335 3336 ksmbd_acls_fattr(&fattr, idmap, inode); 3337 if (fattr.cf_acls) 3338 ace_num = fattr.cf_acls->a_count; 3339 if (fattr.cf_dacls) 3340 ace_num += fattr.cf_dacls->a_count; 3341 3342 pntsd = kmalloc(sizeof(struct smb_ntsd) + 3343 sizeof(struct smb_sid) * 3 + 3344 sizeof(struct smb_acl) + 3345 sizeof(struct smb_ace) * ace_num * 2, 3346 GFP_KERNEL); 3347 if (!pntsd) { 3348 posix_acl_release(fattr.cf_acls); 3349 posix_acl_release(fattr.cf_dacls); 3350 goto err_out; 3351 } 3352 3353 rc = build_sec_desc(idmap, 3354 pntsd, NULL, 0, 3355 OWNER_SECINFO | 3356 GROUP_SECINFO | 3357 DACL_SECINFO, 3358 &pntsd_size, &fattr); 3359 posix_acl_release(fattr.cf_acls); 3360 posix_acl_release(fattr.cf_dacls); 3361 if (rc) { 3362 kfree(pntsd); 3363 goto err_out; 3364 } 3365 3366 rc = ksmbd_vfs_set_sd_xattr(conn, 3367 idmap, 3368 &path, 3369 pntsd, 3370 pntsd_size, 3371 false); 3372 kfree(pntsd); 3373 if (rc) 3374 pr_err("failed to store ntacl in xattr : %d\n", 3375 rc); 3376 } 3377 } 3378 } 3379 rc = 0; 3380 } 3381 3382 if (stream_name) { 3383 rc = smb2_set_stream_name_xattr(&path, 3384 fp, 3385 stream_name, 3386 s_type); 3387 if (rc) 3388 goto err_out; 3389 file_info = FILE_CREATED; 3390 } 3391 3392 fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE | 3393 FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE)); 3394 3395 /* fp should be searchable through ksmbd_inode.m_fp_list 3396 * after daccess, saccess, attrib_only, and stream are 3397 * initialized. 3398 */ 3399 down_write(&fp->f_ci->m_lock); 3400 list_add(&fp->node, &fp->f_ci->m_fp_list); 3401 up_write(&fp->f_ci->m_lock); 3402 3403 /* Check delete pending among previous fp before oplock break */ 3404 if (ksmbd_inode_pending_delete(fp)) { 3405 rc = -EBUSY; 3406 goto err_out; 3407 } 3408 3409 if (file_present || created) 3410 ksmbd_vfs_kern_path_unlock(&parent_path, &path); 3411 3412 if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC && 3413 !fp->attrib_only && !stream_name) { 3414 smb_break_all_oplock(work, fp); 3415 need_truncate = 1; 3416 } 3417 3418 share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp); 3419 if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) || 3420 (req_op_level == SMB2_OPLOCK_LEVEL_LEASE && 3421 !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) { 3422 if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) { 3423 rc = share_ret; 3424 goto err_out1; 3425 } 3426 } else { 3427 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE && lc) { 3428 if (S_ISDIR(file_inode(filp)->i_mode)) { 3429 lc->req_state &= ~SMB2_LEASE_WRITE_CACHING_LE; 3430 lc->is_dir = true; 3431 } 3432 3433 /* 3434 * Compare parent lease using parent key. If there is no 3435 * a lease that has same parent key, Send lease break 3436 * notification. 3437 */ 3438 smb_send_parent_lease_break_noti(fp, lc); 3439 3440 req_op_level = smb2_map_lease_to_oplock(lc->req_state); 3441 ksmbd_debug(SMB, 3442 "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n", 3443 name, req_op_level, lc->req_state); 3444 rc = find_same_lease_key(sess, fp->f_ci, lc); 3445 if (rc) 3446 goto err_out1; 3447 } else if (open_flags == O_RDONLY && 3448 (req_op_level == SMB2_OPLOCK_LEVEL_BATCH || 3449 req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) 3450 req_op_level = SMB2_OPLOCK_LEVEL_II; 3451 3452 rc = smb_grant_oplock(work, req_op_level, 3453 fp->persistent_id, fp, 3454 le32_to_cpu(req->hdr.Id.SyncId.TreeId), 3455 lc, share_ret); 3456 if (rc < 0) 3457 goto err_out1; 3458 } 3459 3460 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) 3461 ksmbd_fd_set_delete_on_close(fp, file_info); 3462 3463 if (need_truncate) { 3464 rc = smb2_create_truncate(&fp->filp->f_path); 3465 if (rc) 3466 goto err_out1; 3467 } 3468 3469 if (req->CreateContextsOffset) { 3470 struct create_alloc_size_req *az_req; 3471 3472 az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req, 3473 SMB2_CREATE_ALLOCATION_SIZE, 4); 3474 if (IS_ERR(az_req)) { 3475 rc = PTR_ERR(az_req); 3476 goto err_out1; 3477 } else if (az_req) { 3478 loff_t alloc_size; 3479 int err; 3480 3481 if (le16_to_cpu(az_req->ccontext.DataOffset) + 3482 le32_to_cpu(az_req->ccontext.DataLength) < 3483 sizeof(struct create_alloc_size_req)) { 3484 rc = -EINVAL; 3485 goto err_out1; 3486 } 3487 alloc_size = le64_to_cpu(az_req->AllocationSize); 3488 ksmbd_debug(SMB, 3489 "request smb2 create allocate size : %llu\n", 3490 alloc_size); 3491 smb_break_all_levII_oplock(work, fp, 1); 3492 err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, 3493 alloc_size); 3494 if (err < 0) 3495 ksmbd_debug(SMB, 3496 "vfs_fallocate is failed : %d\n", 3497 err); 3498 } 3499 3500 context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID, 4); 3501 if (IS_ERR(context)) { 3502 rc = PTR_ERR(context); 3503 goto err_out1; 3504 } else if (context) { 3505 ksmbd_debug(SMB, "get query on disk id context\n"); 3506 query_disk_id = 1; 3507 } 3508 } 3509 3510 rc = ksmbd_vfs_getattr(&path, &stat); 3511 if (rc) 3512 goto err_out1; 3513 3514 if (stat.result_mask & STATX_BTIME) 3515 fp->create_time = ksmbd_UnixTimeToNT(stat.btime); 3516 else 3517 fp->create_time = ksmbd_UnixTimeToNT(stat.ctime); 3518 if (req->FileAttributes || fp->f_ci->m_fattr == 0) 3519 fp->f_ci->m_fattr = 3520 cpu_to_le32(smb2_get_dos_mode(&stat, le32_to_cpu(req->FileAttributes))); 3521 3522 if (!created) 3523 smb2_update_xattrs(tcon, &path, fp); 3524 else 3525 smb2_new_xattrs(tcon, &path, fp); 3526 3527 memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE); 3528 3529 if (dh_info.type == DURABLE_REQ_V2 || dh_info.type == DURABLE_REQ) { 3530 if (dh_info.type == DURABLE_REQ_V2 && dh_info.persistent && 3531 test_share_config_flag(work->tcon->share_conf, 3532 KSMBD_SHARE_FLAG_CONTINUOUS_AVAILABILITY)) 3533 fp->is_persistent = true; 3534 else 3535 fp->is_durable = true; 3536 3537 if (dh_info.type == DURABLE_REQ_V2) { 3538 memcpy(fp->create_guid, dh_info.CreateGuid, 3539 SMB2_CREATE_GUID_SIZE); 3540 if (dh_info.timeout) 3541 fp->durable_timeout = min(dh_info.timeout, 3542 300000); 3543 else 3544 fp->durable_timeout = 60; 3545 } 3546 } 3547 3548 reconnected_fp: 3549 rsp->StructureSize = cpu_to_le16(89); 3550 rcu_read_lock(); 3551 opinfo = rcu_dereference(fp->f_opinfo); 3552 rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0; 3553 rcu_read_unlock(); 3554 rsp->Flags = 0; 3555 rsp->CreateAction = cpu_to_le32(file_info); 3556 rsp->CreationTime = cpu_to_le64(fp->create_time); 3557 time = ksmbd_UnixTimeToNT(stat.atime); 3558 rsp->LastAccessTime = cpu_to_le64(time); 3559 time = ksmbd_UnixTimeToNT(stat.mtime); 3560 rsp->LastWriteTime = cpu_to_le64(time); 3561 time = ksmbd_UnixTimeToNT(stat.ctime); 3562 rsp->ChangeTime = cpu_to_le64(time); 3563 rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 : 3564 cpu_to_le64(stat.blocks << 9); 3565 rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); 3566 rsp->FileAttributes = fp->f_ci->m_fattr; 3567 3568 rsp->Reserved2 = 0; 3569 3570 rsp->PersistentFileId = fp->persistent_id; 3571 rsp->VolatileFileId = fp->volatile_id; 3572 3573 rsp->CreateContextsOffset = 0; 3574 rsp->CreateContextsLength = 0; 3575 iov_len = offsetof(struct smb2_create_rsp, Buffer); 3576 3577 /* If lease is request send lease context response */ 3578 if (opinfo && opinfo->is_lease) { 3579 struct create_context *lease_ccontext; 3580 3581 ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n", 3582 name, opinfo->o_lease->state); 3583 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE; 3584 3585 lease_ccontext = (struct create_context *)rsp->Buffer; 3586 contxt_cnt++; 3587 create_lease_buf(rsp->Buffer, opinfo->o_lease); 3588 le32_add_cpu(&rsp->CreateContextsLength, 3589 conn->vals->create_lease_size); 3590 iov_len += conn->vals->create_lease_size; 3591 next_ptr = &lease_ccontext->Next; 3592 next_off = conn->vals->create_lease_size; 3593 } 3594 3595 if (maximal_access_ctxt) { 3596 struct create_context *mxac_ccontext; 3597 3598 if (maximal_access == 0) 3599 ksmbd_vfs_query_maximal_access(idmap, 3600 path.dentry, 3601 &maximal_access); 3602 mxac_ccontext = (struct create_context *)(rsp->Buffer + 3603 le32_to_cpu(rsp->CreateContextsLength)); 3604 contxt_cnt++; 3605 create_mxac_rsp_buf(rsp->Buffer + 3606 le32_to_cpu(rsp->CreateContextsLength), 3607 le32_to_cpu(maximal_access)); 3608 le32_add_cpu(&rsp->CreateContextsLength, 3609 conn->vals->create_mxac_size); 3610 iov_len += conn->vals->create_mxac_size; 3611 if (next_ptr) 3612 *next_ptr = cpu_to_le32(next_off); 3613 next_ptr = &mxac_ccontext->Next; 3614 next_off = conn->vals->create_mxac_size; 3615 } 3616 3617 if (query_disk_id) { 3618 struct create_context *disk_id_ccontext; 3619 3620 disk_id_ccontext = (struct create_context *)(rsp->Buffer + 3621 le32_to_cpu(rsp->CreateContextsLength)); 3622 contxt_cnt++; 3623 create_disk_id_rsp_buf(rsp->Buffer + 3624 le32_to_cpu(rsp->CreateContextsLength), 3625 stat.ino, tcon->id); 3626 le32_add_cpu(&rsp->CreateContextsLength, 3627 conn->vals->create_disk_id_size); 3628 iov_len += conn->vals->create_disk_id_size; 3629 if (next_ptr) 3630 *next_ptr = cpu_to_le32(next_off); 3631 next_ptr = &disk_id_ccontext->Next; 3632 next_off = conn->vals->create_disk_id_size; 3633 } 3634 3635 if (dh_info.type == DURABLE_REQ || dh_info.type == DURABLE_REQ_V2) { 3636 struct create_context *durable_ccontext; 3637 3638 durable_ccontext = (struct create_context *)(rsp->Buffer + 3639 le32_to_cpu(rsp->CreateContextsLength)); 3640 contxt_cnt++; 3641 if (dh_info.type == DURABLE_REQ) { 3642 create_durable_rsp_buf(rsp->Buffer + 3643 le32_to_cpu(rsp->CreateContextsLength)); 3644 le32_add_cpu(&rsp->CreateContextsLength, 3645 conn->vals->create_durable_size); 3646 iov_len += conn->vals->create_durable_size; 3647 } else { 3648 create_durable_v2_rsp_buf(rsp->Buffer + 3649 le32_to_cpu(rsp->CreateContextsLength), 3650 fp); 3651 le32_add_cpu(&rsp->CreateContextsLength, 3652 conn->vals->create_durable_v2_size); 3653 iov_len += conn->vals->create_durable_v2_size; 3654 } 3655 3656 if (next_ptr) 3657 *next_ptr = cpu_to_le32(next_off); 3658 next_ptr = &durable_ccontext->Next; 3659 next_off = conn->vals->create_durable_size; 3660 } 3661 3662 if (posix_ctxt) { 3663 contxt_cnt++; 3664 create_posix_rsp_buf(rsp->Buffer + 3665 le32_to_cpu(rsp->CreateContextsLength), 3666 fp); 3667 le32_add_cpu(&rsp->CreateContextsLength, 3668 conn->vals->create_posix_size); 3669 iov_len += conn->vals->create_posix_size; 3670 if (next_ptr) 3671 *next_ptr = cpu_to_le32(next_off); 3672 } 3673 3674 if (contxt_cnt > 0) { 3675 rsp->CreateContextsOffset = 3676 cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer)); 3677 } 3678 3679 err_out: 3680 if (rc && (file_present || created)) 3681 ksmbd_vfs_kern_path_unlock(&parent_path, &path); 3682 3683 err_out1: 3684 ksmbd_revert_fsids(work); 3685 3686 err_out2: 3687 if (!rc) { 3688 ksmbd_update_fstate(&work->sess->file_table, fp, FP_INITED); 3689 rc = ksmbd_iov_pin_rsp(work, (void *)rsp, iov_len); 3690 } 3691 if (rc) { 3692 if (rc == -EINVAL) 3693 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 3694 else if (rc == -EOPNOTSUPP) 3695 rsp->hdr.Status = STATUS_NOT_SUPPORTED; 3696 else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV) 3697 rsp->hdr.Status = STATUS_ACCESS_DENIED; 3698 else if (rc == -ENOENT) 3699 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; 3700 else if (rc == -EPERM) 3701 rsp->hdr.Status = STATUS_SHARING_VIOLATION; 3702 else if (rc == -EBUSY) 3703 rsp->hdr.Status = STATUS_DELETE_PENDING; 3704 else if (rc == -EBADF) 3705 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; 3706 else if (rc == -ENOEXEC) 3707 rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID; 3708 else if (rc == -ENXIO) 3709 rsp->hdr.Status = STATUS_NO_SUCH_DEVICE; 3710 else if (rc == -EEXIST) 3711 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; 3712 else if (rc == -EMFILE) 3713 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; 3714 if (!rsp->hdr.Status) 3715 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; 3716 3717 if (fp) 3718 ksmbd_fd_put(work, fp); 3719 smb2_set_err_rsp(work); 3720 ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status); 3721 } 3722 3723 kfree(name); 3724 kfree(lc); 3725 3726 return rc; 3727 } 3728 3729 static int readdir_info_level_struct_sz(int info_level) 3730 { 3731 switch (info_level) { 3732 case FILE_FULL_DIRECTORY_INFORMATION: 3733 return sizeof(struct file_full_directory_info); 3734 case FILE_BOTH_DIRECTORY_INFORMATION: 3735 return sizeof(struct file_both_directory_info); 3736 case FILE_DIRECTORY_INFORMATION: 3737 return sizeof(struct file_directory_info); 3738 case FILE_NAMES_INFORMATION: 3739 return sizeof(struct file_names_info); 3740 case FILEID_FULL_DIRECTORY_INFORMATION: 3741 return sizeof(struct file_id_full_dir_info); 3742 case FILEID_BOTH_DIRECTORY_INFORMATION: 3743 return sizeof(struct file_id_both_directory_info); 3744 case SMB_FIND_FILE_POSIX_INFO: 3745 return sizeof(struct smb2_posix_info); 3746 default: 3747 return -EOPNOTSUPP; 3748 } 3749 } 3750 3751 static int dentry_name(struct ksmbd_dir_info *d_info, int info_level) 3752 { 3753 switch (info_level) { 3754 case FILE_FULL_DIRECTORY_INFORMATION: 3755 { 3756 struct file_full_directory_info *ffdinfo; 3757 3758 ffdinfo = (struct file_full_directory_info *)d_info->rptr; 3759 d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset); 3760 d_info->name = ffdinfo->FileName; 3761 d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength); 3762 return 0; 3763 } 3764 case FILE_BOTH_DIRECTORY_INFORMATION: 3765 { 3766 struct file_both_directory_info *fbdinfo; 3767 3768 fbdinfo = (struct file_both_directory_info *)d_info->rptr; 3769 d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset); 3770 d_info->name = fbdinfo->FileName; 3771 d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength); 3772 return 0; 3773 } 3774 case FILE_DIRECTORY_INFORMATION: 3775 { 3776 struct file_directory_info *fdinfo; 3777 3778 fdinfo = (struct file_directory_info *)d_info->rptr; 3779 d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset); 3780 d_info->name = fdinfo->FileName; 3781 d_info->name_len = le32_to_cpu(fdinfo->FileNameLength); 3782 return 0; 3783 } 3784 case FILE_NAMES_INFORMATION: 3785 { 3786 struct file_names_info *fninfo; 3787 3788 fninfo = (struct file_names_info *)d_info->rptr; 3789 d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset); 3790 d_info->name = fninfo->FileName; 3791 d_info->name_len = le32_to_cpu(fninfo->FileNameLength); 3792 return 0; 3793 } 3794 case FILEID_FULL_DIRECTORY_INFORMATION: 3795 { 3796 struct file_id_full_dir_info *dinfo; 3797 3798 dinfo = (struct file_id_full_dir_info *)d_info->rptr; 3799 d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset); 3800 d_info->name = dinfo->FileName; 3801 d_info->name_len = le32_to_cpu(dinfo->FileNameLength); 3802 return 0; 3803 } 3804 case FILEID_BOTH_DIRECTORY_INFORMATION: 3805 { 3806 struct file_id_both_directory_info *fibdinfo; 3807 3808 fibdinfo = (struct file_id_both_directory_info *)d_info->rptr; 3809 d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset); 3810 d_info->name = fibdinfo->FileName; 3811 d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength); 3812 return 0; 3813 } 3814 case SMB_FIND_FILE_POSIX_INFO: 3815 { 3816 struct smb2_posix_info *posix_info; 3817 3818 posix_info = (struct smb2_posix_info *)d_info->rptr; 3819 d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset); 3820 d_info->name = posix_info->name; 3821 d_info->name_len = le32_to_cpu(posix_info->name_len); 3822 return 0; 3823 } 3824 default: 3825 return -EINVAL; 3826 } 3827 } 3828 3829 /** 3830 * smb2_populate_readdir_entry() - encode directory entry in smb2 response 3831 * buffer 3832 * @conn: connection instance 3833 * @info_level: smb information level 3834 * @d_info: structure included variables for query dir 3835 * @ksmbd_kstat: ksmbd wrapper of dirent stat information 3836 * 3837 * if directory has many entries, find first can't read it fully. 3838 * find next might be called multiple times to read remaining dir entries 3839 * 3840 * Return: 0 on success, otherwise error 3841 */ 3842 static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level, 3843 struct ksmbd_dir_info *d_info, 3844 struct ksmbd_kstat *ksmbd_kstat) 3845 { 3846 int next_entry_offset = 0; 3847 char *conv_name; 3848 int conv_len; 3849 void *kstat; 3850 int struct_sz, rc = 0; 3851 3852 conv_name = ksmbd_convert_dir_info_name(d_info, 3853 conn->local_nls, 3854 &conv_len); 3855 if (!conv_name) 3856 return -ENOMEM; 3857 3858 /* Somehow the name has only terminating NULL bytes */ 3859 if (conv_len < 0) { 3860 rc = -EINVAL; 3861 goto free_conv_name; 3862 } 3863 3864 struct_sz = readdir_info_level_struct_sz(info_level) + conv_len; 3865 next_entry_offset = ALIGN(struct_sz, KSMBD_DIR_INFO_ALIGNMENT); 3866 d_info->last_entry_off_align = next_entry_offset - struct_sz; 3867 3868 if (next_entry_offset > d_info->out_buf_len) { 3869 d_info->out_buf_len = 0; 3870 rc = -ENOSPC; 3871 goto free_conv_name; 3872 } 3873 3874 kstat = d_info->wptr; 3875 if (info_level != FILE_NAMES_INFORMATION) 3876 kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat); 3877 3878 switch (info_level) { 3879 case FILE_FULL_DIRECTORY_INFORMATION: 3880 { 3881 struct file_full_directory_info *ffdinfo; 3882 3883 ffdinfo = (struct file_full_directory_info *)kstat; 3884 ffdinfo->FileNameLength = cpu_to_le32(conv_len); 3885 ffdinfo->EaSize = 3886 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); 3887 if (ffdinfo->EaSize) 3888 ffdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; 3889 if (d_info->hide_dot_file && d_info->name[0] == '.') 3890 ffdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; 3891 memcpy(ffdinfo->FileName, conv_name, conv_len); 3892 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); 3893 break; 3894 } 3895 case FILE_BOTH_DIRECTORY_INFORMATION: 3896 { 3897 struct file_both_directory_info *fbdinfo; 3898 3899 fbdinfo = (struct file_both_directory_info *)kstat; 3900 fbdinfo->FileNameLength = cpu_to_le32(conv_len); 3901 fbdinfo->EaSize = 3902 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); 3903 if (fbdinfo->EaSize) 3904 fbdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; 3905 fbdinfo->ShortNameLength = 0; 3906 fbdinfo->Reserved = 0; 3907 if (d_info->hide_dot_file && d_info->name[0] == '.') 3908 fbdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; 3909 memcpy(fbdinfo->FileName, conv_name, conv_len); 3910 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); 3911 break; 3912 } 3913 case FILE_DIRECTORY_INFORMATION: 3914 { 3915 struct file_directory_info *fdinfo; 3916 3917 fdinfo = (struct file_directory_info *)kstat; 3918 fdinfo->FileNameLength = cpu_to_le32(conv_len); 3919 if (d_info->hide_dot_file && d_info->name[0] == '.') 3920 fdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; 3921 memcpy(fdinfo->FileName, conv_name, conv_len); 3922 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); 3923 break; 3924 } 3925 case FILE_NAMES_INFORMATION: 3926 { 3927 struct file_names_info *fninfo; 3928 3929 fninfo = (struct file_names_info *)kstat; 3930 fninfo->FileNameLength = cpu_to_le32(conv_len); 3931 memcpy(fninfo->FileName, conv_name, conv_len); 3932 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); 3933 break; 3934 } 3935 case FILEID_FULL_DIRECTORY_INFORMATION: 3936 { 3937 struct file_id_full_dir_info *dinfo; 3938 3939 dinfo = (struct file_id_full_dir_info *)kstat; 3940 dinfo->FileNameLength = cpu_to_le32(conv_len); 3941 dinfo->EaSize = 3942 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); 3943 if (dinfo->EaSize) 3944 dinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; 3945 dinfo->Reserved = 0; 3946 dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); 3947 if (d_info->hide_dot_file && d_info->name[0] == '.') 3948 dinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; 3949 memcpy(dinfo->FileName, conv_name, conv_len); 3950 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); 3951 break; 3952 } 3953 case FILEID_BOTH_DIRECTORY_INFORMATION: 3954 { 3955 struct file_id_both_directory_info *fibdinfo; 3956 3957 fibdinfo = (struct file_id_both_directory_info *)kstat; 3958 fibdinfo->FileNameLength = cpu_to_le32(conv_len); 3959 fibdinfo->EaSize = 3960 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); 3961 if (fibdinfo->EaSize) 3962 fibdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; 3963 fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); 3964 fibdinfo->ShortNameLength = 0; 3965 fibdinfo->Reserved = 0; 3966 fibdinfo->Reserved2 = cpu_to_le16(0); 3967 if (d_info->hide_dot_file && d_info->name[0] == '.') 3968 fibdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; 3969 memcpy(fibdinfo->FileName, conv_name, conv_len); 3970 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); 3971 break; 3972 } 3973 case SMB_FIND_FILE_POSIX_INFO: 3974 { 3975 struct smb2_posix_info *posix_info; 3976 u64 time; 3977 3978 posix_info = (struct smb2_posix_info *)kstat; 3979 posix_info->Ignored = 0; 3980 posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time); 3981 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime); 3982 posix_info->ChangeTime = cpu_to_le64(time); 3983 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime); 3984 posix_info->LastAccessTime = cpu_to_le64(time); 3985 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime); 3986 posix_info->LastWriteTime = cpu_to_le64(time); 3987 posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size); 3988 posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9); 3989 posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev); 3990 posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink); 3991 posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode & 0777); 3992 switch (ksmbd_kstat->kstat->mode & S_IFMT) { 3993 case S_IFDIR: 3994 posix_info->Mode |= cpu_to_le32(POSIX_TYPE_DIR << POSIX_FILETYPE_SHIFT); 3995 break; 3996 case S_IFLNK: 3997 posix_info->Mode |= cpu_to_le32(POSIX_TYPE_SYMLINK << POSIX_FILETYPE_SHIFT); 3998 break; 3999 case S_IFCHR: 4000 posix_info->Mode |= cpu_to_le32(POSIX_TYPE_CHARDEV << POSIX_FILETYPE_SHIFT); 4001 break; 4002 case S_IFBLK: 4003 posix_info->Mode |= cpu_to_le32(POSIX_TYPE_BLKDEV << POSIX_FILETYPE_SHIFT); 4004 break; 4005 case S_IFIFO: 4006 posix_info->Mode |= cpu_to_le32(POSIX_TYPE_FIFO << POSIX_FILETYPE_SHIFT); 4007 break; 4008 case S_IFSOCK: 4009 posix_info->Mode |= cpu_to_le32(POSIX_TYPE_SOCKET << POSIX_FILETYPE_SHIFT); 4010 } 4011 4012 posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino); 4013 posix_info->DosAttributes = 4014 S_ISDIR(ksmbd_kstat->kstat->mode) ? 4015 FILE_ATTRIBUTE_DIRECTORY_LE : FILE_ATTRIBUTE_ARCHIVE_LE; 4016 if (d_info->hide_dot_file && d_info->name[0] == '.') 4017 posix_info->DosAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; 4018 /* 4019 * SidBuffer(32) contain two sids(Domain sid(16), UNIX group sid(16)). 4020 * UNIX sid(16) = revision(1) + num_subauth(1) + authority(6) + 4021 * sub_auth(4 * 1(num_subauth)) + RID(4). 4022 */ 4023 id_to_sid(from_kuid_munged(&init_user_ns, ksmbd_kstat->kstat->uid), 4024 SIDUNIX_USER, (struct smb_sid *)&posix_info->SidBuffer[0]); 4025 id_to_sid(from_kgid_munged(&init_user_ns, ksmbd_kstat->kstat->gid), 4026 SIDUNIX_GROUP, (struct smb_sid *)&posix_info->SidBuffer[16]); 4027 memcpy(posix_info->name, conv_name, conv_len); 4028 posix_info->name_len = cpu_to_le32(conv_len); 4029 posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset); 4030 break; 4031 } 4032 4033 } /* switch (info_level) */ 4034 4035 d_info->last_entry_offset = d_info->data_count; 4036 d_info->data_count += next_entry_offset; 4037 d_info->out_buf_len -= next_entry_offset; 4038 d_info->wptr += next_entry_offset; 4039 4040 ksmbd_debug(SMB, 4041 "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n", 4042 info_level, d_info->out_buf_len, 4043 next_entry_offset, d_info->data_count); 4044 4045 free_conv_name: 4046 kfree(conv_name); 4047 return rc; 4048 } 4049 4050 struct smb2_query_dir_private { 4051 struct ksmbd_work *work; 4052 char *search_pattern; 4053 struct ksmbd_file *dir_fp; 4054 4055 struct ksmbd_dir_info *d_info; 4056 int info_level; 4057 }; 4058 4059 static void lock_dir(struct ksmbd_file *dir_fp) 4060 { 4061 struct dentry *dir = dir_fp->filp->f_path.dentry; 4062 4063 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); 4064 } 4065 4066 static void unlock_dir(struct ksmbd_file *dir_fp) 4067 { 4068 struct dentry *dir = dir_fp->filp->f_path.dentry; 4069 4070 inode_unlock(d_inode(dir)); 4071 } 4072 4073 static int process_query_dir_entries(struct smb2_query_dir_private *priv) 4074 { 4075 struct mnt_idmap *idmap = file_mnt_idmap(priv->dir_fp->filp); 4076 struct kstat kstat; 4077 struct ksmbd_kstat ksmbd_kstat; 4078 int rc; 4079 int i; 4080 4081 for (i = 0; i < priv->d_info->num_entry; i++) { 4082 struct dentry *dent; 4083 4084 if (dentry_name(priv->d_info, priv->info_level)) 4085 return -EINVAL; 4086 4087 lock_dir(priv->dir_fp); 4088 dent = lookup_one(idmap, priv->d_info->name, 4089 priv->dir_fp->filp->f_path.dentry, 4090 priv->d_info->name_len); 4091 unlock_dir(priv->dir_fp); 4092 4093 if (IS_ERR(dent)) { 4094 ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n", 4095 priv->d_info->name, 4096 PTR_ERR(dent)); 4097 continue; 4098 } 4099 if (unlikely(d_is_negative(dent))) { 4100 dput(dent); 4101 ksmbd_debug(SMB, "Negative dentry `%s'\n", 4102 priv->d_info->name); 4103 continue; 4104 } 4105 4106 ksmbd_kstat.kstat = &kstat; 4107 if (priv->info_level != FILE_NAMES_INFORMATION) { 4108 rc = ksmbd_vfs_fill_dentry_attrs(priv->work, 4109 idmap, 4110 dent, 4111 &ksmbd_kstat); 4112 if (rc) { 4113 dput(dent); 4114 continue; 4115 } 4116 } 4117 4118 rc = smb2_populate_readdir_entry(priv->work->conn, 4119 priv->info_level, 4120 priv->d_info, 4121 &ksmbd_kstat); 4122 dput(dent); 4123 if (rc) 4124 return rc; 4125 } 4126 return 0; 4127 } 4128 4129 static int reserve_populate_dentry(struct ksmbd_dir_info *d_info, 4130 int info_level) 4131 { 4132 int struct_sz; 4133 int conv_len; 4134 int next_entry_offset; 4135 4136 struct_sz = readdir_info_level_struct_sz(info_level); 4137 if (struct_sz == -EOPNOTSUPP) 4138 return -EOPNOTSUPP; 4139 4140 conv_len = (d_info->name_len + 1) * 2; 4141 next_entry_offset = ALIGN(struct_sz + conv_len, 4142 KSMBD_DIR_INFO_ALIGNMENT); 4143 4144 if (next_entry_offset > d_info->out_buf_len) { 4145 d_info->out_buf_len = 0; 4146 return -ENOSPC; 4147 } 4148 4149 switch (info_level) { 4150 case FILE_FULL_DIRECTORY_INFORMATION: 4151 { 4152 struct file_full_directory_info *ffdinfo; 4153 4154 ffdinfo = (struct file_full_directory_info *)d_info->wptr; 4155 memcpy(ffdinfo->FileName, d_info->name, d_info->name_len); 4156 ffdinfo->FileName[d_info->name_len] = 0x00; 4157 ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len); 4158 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); 4159 break; 4160 } 4161 case FILE_BOTH_DIRECTORY_INFORMATION: 4162 { 4163 struct file_both_directory_info *fbdinfo; 4164 4165 fbdinfo = (struct file_both_directory_info *)d_info->wptr; 4166 memcpy(fbdinfo->FileName, d_info->name, d_info->name_len); 4167 fbdinfo->FileName[d_info->name_len] = 0x00; 4168 fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len); 4169 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); 4170 break; 4171 } 4172 case FILE_DIRECTORY_INFORMATION: 4173 { 4174 struct file_directory_info *fdinfo; 4175 4176 fdinfo = (struct file_directory_info *)d_info->wptr; 4177 memcpy(fdinfo->FileName, d_info->name, d_info->name_len); 4178 fdinfo->FileName[d_info->name_len] = 0x00; 4179 fdinfo->FileNameLength = cpu_to_le32(d_info->name_len); 4180 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); 4181 break; 4182 } 4183 case FILE_NAMES_INFORMATION: 4184 { 4185 struct file_names_info *fninfo; 4186 4187 fninfo = (struct file_names_info *)d_info->wptr; 4188 memcpy(fninfo->FileName, d_info->name, d_info->name_len); 4189 fninfo->FileName[d_info->name_len] = 0x00; 4190 fninfo->FileNameLength = cpu_to_le32(d_info->name_len); 4191 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); 4192 break; 4193 } 4194 case FILEID_FULL_DIRECTORY_INFORMATION: 4195 { 4196 struct file_id_full_dir_info *dinfo; 4197 4198 dinfo = (struct file_id_full_dir_info *)d_info->wptr; 4199 memcpy(dinfo->FileName, d_info->name, d_info->name_len); 4200 dinfo->FileName[d_info->name_len] = 0x00; 4201 dinfo->FileNameLength = cpu_to_le32(d_info->name_len); 4202 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); 4203 break; 4204 } 4205 case FILEID_BOTH_DIRECTORY_INFORMATION: 4206 { 4207 struct file_id_both_directory_info *fibdinfo; 4208 4209 fibdinfo = (struct file_id_both_directory_info *)d_info->wptr; 4210 memcpy(fibdinfo->FileName, d_info->name, d_info->name_len); 4211 fibdinfo->FileName[d_info->name_len] = 0x00; 4212 fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len); 4213 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); 4214 break; 4215 } 4216 case SMB_FIND_FILE_POSIX_INFO: 4217 { 4218 struct smb2_posix_info *posix_info; 4219 4220 posix_info = (struct smb2_posix_info *)d_info->wptr; 4221 memcpy(posix_info->name, d_info->name, d_info->name_len); 4222 posix_info->name[d_info->name_len] = 0x00; 4223 posix_info->name_len = cpu_to_le32(d_info->name_len); 4224 posix_info->NextEntryOffset = 4225 cpu_to_le32(next_entry_offset); 4226 break; 4227 } 4228 } /* switch (info_level) */ 4229 4230 d_info->num_entry++; 4231 d_info->out_buf_len -= next_entry_offset; 4232 d_info->wptr += next_entry_offset; 4233 return 0; 4234 } 4235 4236 static bool __query_dir(struct dir_context *ctx, const char *name, int namlen, 4237 loff_t offset, u64 ino, unsigned int d_type) 4238 { 4239 struct ksmbd_readdir_data *buf; 4240 struct smb2_query_dir_private *priv; 4241 struct ksmbd_dir_info *d_info; 4242 int rc; 4243 4244 buf = container_of(ctx, struct ksmbd_readdir_data, ctx); 4245 priv = buf->private; 4246 d_info = priv->d_info; 4247 4248 /* dot and dotdot entries are already reserved */ 4249 if (!strcmp(".", name) || !strcmp("..", name)) 4250 return true; 4251 d_info->num_scan++; 4252 if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name)) 4253 return true; 4254 if (!match_pattern(name, namlen, priv->search_pattern)) 4255 return true; 4256 4257 d_info->name = name; 4258 d_info->name_len = namlen; 4259 rc = reserve_populate_dentry(d_info, priv->info_level); 4260 if (rc) 4261 return false; 4262 if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) 4263 d_info->out_buf_len = 0; 4264 return true; 4265 } 4266 4267 static int verify_info_level(int info_level) 4268 { 4269 switch (info_level) { 4270 case FILE_FULL_DIRECTORY_INFORMATION: 4271 case FILE_BOTH_DIRECTORY_INFORMATION: 4272 case FILE_DIRECTORY_INFORMATION: 4273 case FILE_NAMES_INFORMATION: 4274 case FILEID_FULL_DIRECTORY_INFORMATION: 4275 case FILEID_BOTH_DIRECTORY_INFORMATION: 4276 case SMB_FIND_FILE_POSIX_INFO: 4277 break; 4278 default: 4279 return -EOPNOTSUPP; 4280 } 4281 4282 return 0; 4283 } 4284 4285 static int smb2_resp_buf_len(struct ksmbd_work *work, unsigned short hdr2_len) 4286 { 4287 int free_len; 4288 4289 free_len = (int)(work->response_sz - 4290 (get_rfc1002_len(work->response_buf) + 4)) - hdr2_len; 4291 return free_len; 4292 } 4293 4294 static int smb2_calc_max_out_buf_len(struct ksmbd_work *work, 4295 unsigned short hdr2_len, 4296 unsigned int out_buf_len) 4297 { 4298 int free_len; 4299 4300 if (out_buf_len > work->conn->vals->max_trans_size) 4301 return -EINVAL; 4302 4303 free_len = smb2_resp_buf_len(work, hdr2_len); 4304 if (free_len < 0) 4305 return -EINVAL; 4306 4307 return min_t(int, out_buf_len, free_len); 4308 } 4309 4310 int smb2_query_dir(struct ksmbd_work *work) 4311 { 4312 struct ksmbd_conn *conn = work->conn; 4313 struct smb2_query_directory_req *req; 4314 struct smb2_query_directory_rsp *rsp; 4315 struct ksmbd_share_config *share = work->tcon->share_conf; 4316 struct ksmbd_file *dir_fp = NULL; 4317 struct ksmbd_dir_info d_info; 4318 int rc = 0; 4319 char *srch_ptr = NULL; 4320 unsigned char srch_flag; 4321 int buffer_sz; 4322 struct smb2_query_dir_private query_dir_private = {NULL, }; 4323 4324 WORK_BUFFERS(work, req, rsp); 4325 4326 if (ksmbd_override_fsids(work)) { 4327 rsp->hdr.Status = STATUS_NO_MEMORY; 4328 smb2_set_err_rsp(work); 4329 return -ENOMEM; 4330 } 4331 4332 rc = verify_info_level(req->FileInformationClass); 4333 if (rc) { 4334 rc = -EFAULT; 4335 goto err_out2; 4336 } 4337 4338 dir_fp = ksmbd_lookup_fd_slow(work, req->VolatileFileId, req->PersistentFileId); 4339 if (!dir_fp) { 4340 rc = -EBADF; 4341 goto err_out2; 4342 } 4343 4344 if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) || 4345 inode_permission(file_mnt_idmap(dir_fp->filp), 4346 file_inode(dir_fp->filp), 4347 MAY_READ | MAY_EXEC)) { 4348 pr_err("no right to enumerate directory (%pD)\n", dir_fp->filp); 4349 rc = -EACCES; 4350 goto err_out2; 4351 } 4352 4353 if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) { 4354 pr_err("can't do query dir for a file\n"); 4355 rc = -EINVAL; 4356 goto err_out2; 4357 } 4358 4359 srch_flag = req->Flags; 4360 srch_ptr = smb_strndup_from_utf16((char *)req + le16_to_cpu(req->FileNameOffset), 4361 le16_to_cpu(req->FileNameLength), 1, 4362 conn->local_nls); 4363 if (IS_ERR(srch_ptr)) { 4364 ksmbd_debug(SMB, "Search Pattern not found\n"); 4365 rc = -EINVAL; 4366 goto err_out2; 4367 } else { 4368 ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr); 4369 } 4370 4371 if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) { 4372 ksmbd_debug(SMB, "Restart directory scan\n"); 4373 generic_file_llseek(dir_fp->filp, 0, SEEK_SET); 4374 } 4375 4376 memset(&d_info, 0, sizeof(struct ksmbd_dir_info)); 4377 d_info.wptr = (char *)rsp->Buffer; 4378 d_info.rptr = (char *)rsp->Buffer; 4379 d_info.out_buf_len = 4380 smb2_calc_max_out_buf_len(work, 8, 4381 le32_to_cpu(req->OutputBufferLength)); 4382 if (d_info.out_buf_len < 0) { 4383 rc = -EINVAL; 4384 goto err_out; 4385 } 4386 d_info.flags = srch_flag; 4387 4388 /* 4389 * reserve dot and dotdot entries in head of buffer 4390 * in first response 4391 */ 4392 rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass, 4393 dir_fp, &d_info, srch_ptr, 4394 smb2_populate_readdir_entry); 4395 if (rc == -ENOSPC) 4396 rc = 0; 4397 else if (rc) 4398 goto err_out; 4399 4400 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES)) 4401 d_info.hide_dot_file = true; 4402 4403 buffer_sz = d_info.out_buf_len; 4404 d_info.rptr = d_info.wptr; 4405 query_dir_private.work = work; 4406 query_dir_private.search_pattern = srch_ptr; 4407 query_dir_private.dir_fp = dir_fp; 4408 query_dir_private.d_info = &d_info; 4409 query_dir_private.info_level = req->FileInformationClass; 4410 dir_fp->readdir_data.private = &query_dir_private; 4411 set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir); 4412 again: 4413 d_info.num_scan = 0; 4414 rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx); 4415 /* 4416 * num_entry can be 0 if the directory iteration stops before reaching 4417 * the end of the directory and no file is matched with the search 4418 * pattern. 4419 */ 4420 if (rc >= 0 && !d_info.num_entry && d_info.num_scan && 4421 d_info.out_buf_len > 0) 4422 goto again; 4423 /* 4424 * req->OutputBufferLength is too small to contain even one entry. 4425 * In this case, it immediately returns OutputBufferLength 0 to client. 4426 */ 4427 if (!d_info.out_buf_len && !d_info.num_entry) 4428 goto no_buf_len; 4429 if (rc > 0 || rc == -ENOSPC) 4430 rc = 0; 4431 else if (rc) 4432 goto err_out; 4433 4434 d_info.wptr = d_info.rptr; 4435 d_info.out_buf_len = buffer_sz; 4436 rc = process_query_dir_entries(&query_dir_private); 4437 if (rc) 4438 goto err_out; 4439 4440 if (!d_info.data_count && d_info.out_buf_len >= 0) { 4441 if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) { 4442 rsp->hdr.Status = STATUS_NO_SUCH_FILE; 4443 } else { 4444 dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0; 4445 rsp->hdr.Status = STATUS_NO_MORE_FILES; 4446 } 4447 rsp->StructureSize = cpu_to_le16(9); 4448 rsp->OutputBufferOffset = cpu_to_le16(0); 4449 rsp->OutputBufferLength = cpu_to_le32(0); 4450 rsp->Buffer[0] = 0; 4451 rc = ksmbd_iov_pin_rsp(work, (void *)rsp, 4452 offsetof(struct smb2_query_directory_rsp, Buffer) 4453 + 1); 4454 if (rc) 4455 goto err_out; 4456 } else { 4457 no_buf_len: 4458 ((struct file_directory_info *) 4459 ((char *)rsp->Buffer + d_info.last_entry_offset)) 4460 ->NextEntryOffset = 0; 4461 if (d_info.data_count >= d_info.last_entry_off_align) 4462 d_info.data_count -= d_info.last_entry_off_align; 4463 4464 rsp->StructureSize = cpu_to_le16(9); 4465 rsp->OutputBufferOffset = cpu_to_le16(72); 4466 rsp->OutputBufferLength = cpu_to_le32(d_info.data_count); 4467 rc = ksmbd_iov_pin_rsp(work, (void *)rsp, 4468 offsetof(struct smb2_query_directory_rsp, Buffer) + 4469 d_info.data_count); 4470 if (rc) 4471 goto err_out; 4472 } 4473 4474 kfree(srch_ptr); 4475 ksmbd_fd_put(work, dir_fp); 4476 ksmbd_revert_fsids(work); 4477 return 0; 4478 4479 err_out: 4480 pr_err("error while processing smb2 query dir rc = %d\n", rc); 4481 kfree(srch_ptr); 4482 4483 err_out2: 4484 if (rc == -EINVAL) 4485 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 4486 else if (rc == -EACCES) 4487 rsp->hdr.Status = STATUS_ACCESS_DENIED; 4488 else if (rc == -ENOENT) 4489 rsp->hdr.Status = STATUS_NO_SUCH_FILE; 4490 else if (rc == -EBADF) 4491 rsp->hdr.Status = STATUS_FILE_CLOSED; 4492 else if (rc == -ENOMEM) 4493 rsp->hdr.Status = STATUS_NO_MEMORY; 4494 else if (rc == -EFAULT) 4495 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; 4496 else if (rc == -EIO) 4497 rsp->hdr.Status = STATUS_FILE_CORRUPT_ERROR; 4498 if (!rsp->hdr.Status) 4499 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; 4500 4501 smb2_set_err_rsp(work); 4502 ksmbd_fd_put(work, dir_fp); 4503 ksmbd_revert_fsids(work); 4504 return 0; 4505 } 4506 4507 /** 4508 * buffer_check_err() - helper function to check buffer errors 4509 * @reqOutputBufferLength: max buffer length expected in command response 4510 * @rsp: query info response buffer contains output buffer length 4511 * @rsp_org: base response buffer pointer in case of chained response 4512 * 4513 * Return: 0 on success, otherwise error 4514 */ 4515 static int buffer_check_err(int reqOutputBufferLength, 4516 struct smb2_query_info_rsp *rsp, 4517 void *rsp_org) 4518 { 4519 if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) { 4520 pr_err("Invalid Buffer Size Requested\n"); 4521 rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH; 4522 *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr)); 4523 return -EINVAL; 4524 } 4525 return 0; 4526 } 4527 4528 static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp, 4529 void *rsp_org) 4530 { 4531 struct smb2_file_standard_info *sinfo; 4532 4533 sinfo = (struct smb2_file_standard_info *)rsp->Buffer; 4534 4535 sinfo->AllocationSize = cpu_to_le64(4096); 4536 sinfo->EndOfFile = cpu_to_le64(0); 4537 sinfo->NumberOfLinks = cpu_to_le32(1); 4538 sinfo->DeletePending = 1; 4539 sinfo->Directory = 0; 4540 rsp->OutputBufferLength = 4541 cpu_to_le32(sizeof(struct smb2_file_standard_info)); 4542 } 4543 4544 static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num, 4545 void *rsp_org) 4546 { 4547 struct smb2_file_internal_info *file_info; 4548 4549 file_info = (struct smb2_file_internal_info *)rsp->Buffer; 4550 4551 /* any unique number */ 4552 file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63)); 4553 rsp->OutputBufferLength = 4554 cpu_to_le32(sizeof(struct smb2_file_internal_info)); 4555 } 4556 4557 static int smb2_get_info_file_pipe(struct ksmbd_session *sess, 4558 struct smb2_query_info_req *req, 4559 struct smb2_query_info_rsp *rsp, 4560 void *rsp_org) 4561 { 4562 u64 id; 4563 int rc; 4564 4565 /* 4566 * Windows can sometime send query file info request on 4567 * pipe without opening it, checking error condition here 4568 */ 4569 id = req->VolatileFileId; 4570 if (!ksmbd_session_rpc_method(sess, id)) 4571 return -ENOENT; 4572 4573 ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n", 4574 req->FileInfoClass, req->VolatileFileId); 4575 4576 switch (req->FileInfoClass) { 4577 case FILE_STANDARD_INFORMATION: 4578 get_standard_info_pipe(rsp, rsp_org); 4579 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), 4580 rsp, rsp_org); 4581 break; 4582 case FILE_INTERNAL_INFORMATION: 4583 get_internal_info_pipe(rsp, id, rsp_org); 4584 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), 4585 rsp, rsp_org); 4586 break; 4587 default: 4588 ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n", 4589 req->FileInfoClass); 4590 rc = -EOPNOTSUPP; 4591 } 4592 return rc; 4593 } 4594 4595 /** 4596 * smb2_get_ea() - handler for smb2 get extended attribute command 4597 * @work: smb work containing query info command buffer 4598 * @fp: ksmbd_file pointer 4599 * @req: get extended attribute request 4600 * @rsp: response buffer pointer 4601 * @rsp_org: base response buffer pointer in case of chained response 4602 * 4603 * Return: 0 on success, otherwise error 4604 */ 4605 static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp, 4606 struct smb2_query_info_req *req, 4607 struct smb2_query_info_rsp *rsp, void *rsp_org) 4608 { 4609 struct smb2_ea_info *eainfo, *prev_eainfo; 4610 char *name, *ptr, *xattr_list = NULL, *buf; 4611 int rc, name_len, value_len, xattr_list_len, idx; 4612 ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0; 4613 struct smb2_ea_info_req *ea_req = NULL; 4614 const struct path *path; 4615 struct mnt_idmap *idmap = file_mnt_idmap(fp->filp); 4616 4617 if (!(fp->daccess & FILE_READ_EA_LE)) { 4618 pr_err("Not permitted to read ext attr : 0x%x\n", 4619 fp->daccess); 4620 return -EACCES; 4621 } 4622 4623 path = &fp->filp->f_path; 4624 /* single EA entry is requested with given user.* name */ 4625 if (req->InputBufferLength) { 4626 if (le32_to_cpu(req->InputBufferLength) < 4627 sizeof(struct smb2_ea_info_req)) 4628 return -EINVAL; 4629 4630 ea_req = (struct smb2_ea_info_req *)((char *)req + 4631 le16_to_cpu(req->InputBufferOffset)); 4632 } else { 4633 /* need to send all EAs, if no specific EA is requested*/ 4634 if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY) 4635 ksmbd_debug(SMB, 4636 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n", 4637 le32_to_cpu(req->Flags)); 4638 } 4639 4640 buf_free_len = 4641 smb2_calc_max_out_buf_len(work, 8, 4642 le32_to_cpu(req->OutputBufferLength)); 4643 if (buf_free_len < 0) 4644 return -EINVAL; 4645 4646 rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list); 4647 if (rc < 0) { 4648 rsp->hdr.Status = STATUS_INVALID_HANDLE; 4649 goto out; 4650 } else if (!rc) { /* there is no EA in the file */ 4651 ksmbd_debug(SMB, "no ea data in the file\n"); 4652 goto done; 4653 } 4654 xattr_list_len = rc; 4655 4656 ptr = (char *)rsp->Buffer; 4657 eainfo = (struct smb2_ea_info *)ptr; 4658 prev_eainfo = eainfo; 4659 idx = 0; 4660 4661 while (idx < xattr_list_len) { 4662 name = xattr_list + idx; 4663 name_len = strlen(name); 4664 4665 ksmbd_debug(SMB, "%s, len %d\n", name, name_len); 4666 idx += name_len + 1; 4667 4668 /* 4669 * CIFS does not support EA other than user.* namespace, 4670 * still keep the framework generic, to list other attrs 4671 * in future. 4672 */ 4673 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) 4674 continue; 4675 4676 if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, 4677 STREAM_PREFIX_LEN)) 4678 continue; 4679 4680 if (req->InputBufferLength && 4681 strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name, 4682 ea_req->EaNameLength)) 4683 continue; 4684 4685 if (!strncmp(&name[XATTR_USER_PREFIX_LEN], 4686 DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN)) 4687 continue; 4688 4689 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) 4690 name_len -= XATTR_USER_PREFIX_LEN; 4691 4692 ptr = eainfo->name + name_len + 1; 4693 buf_free_len -= (offsetof(struct smb2_ea_info, name) + 4694 name_len + 1); 4695 /* bailout if xattr can't fit in buf_free_len */ 4696 value_len = ksmbd_vfs_getxattr(idmap, path->dentry, 4697 name, &buf); 4698 if (value_len <= 0) { 4699 rc = -ENOENT; 4700 rsp->hdr.Status = STATUS_INVALID_HANDLE; 4701 goto out; 4702 } 4703 4704 buf_free_len -= value_len; 4705 if (buf_free_len < 0) { 4706 kfree(buf); 4707 break; 4708 } 4709 4710 memcpy(ptr, buf, value_len); 4711 kfree(buf); 4712 4713 ptr += value_len; 4714 eainfo->Flags = 0; 4715 eainfo->EaNameLength = name_len; 4716 4717 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) 4718 memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN], 4719 name_len); 4720 else 4721 memcpy(eainfo->name, name, name_len); 4722 4723 eainfo->name[name_len] = '\0'; 4724 eainfo->EaValueLength = cpu_to_le16(value_len); 4725 next_offset = offsetof(struct smb2_ea_info, name) + 4726 name_len + 1 + value_len; 4727 4728 /* align next xattr entry at 4 byte bundary */ 4729 alignment_bytes = ((next_offset + 3) & ~3) - next_offset; 4730 if (alignment_bytes) { 4731 memset(ptr, '\0', alignment_bytes); 4732 ptr += alignment_bytes; 4733 next_offset += alignment_bytes; 4734 buf_free_len -= alignment_bytes; 4735 } 4736 eainfo->NextEntryOffset = cpu_to_le32(next_offset); 4737 prev_eainfo = eainfo; 4738 eainfo = (struct smb2_ea_info *)ptr; 4739 rsp_data_cnt += next_offset; 4740 4741 if (req->InputBufferLength) { 4742 ksmbd_debug(SMB, "single entry requested\n"); 4743 break; 4744 } 4745 } 4746 4747 /* no more ea entries */ 4748 prev_eainfo->NextEntryOffset = 0; 4749 done: 4750 rc = 0; 4751 if (rsp_data_cnt == 0) 4752 rsp->hdr.Status = STATUS_NO_EAS_ON_FILE; 4753 rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt); 4754 out: 4755 kvfree(xattr_list); 4756 return rc; 4757 } 4758 4759 static void get_file_access_info(struct smb2_query_info_rsp *rsp, 4760 struct ksmbd_file *fp, void *rsp_org) 4761 { 4762 struct smb2_file_access_info *file_info; 4763 4764 file_info = (struct smb2_file_access_info *)rsp->Buffer; 4765 file_info->AccessFlags = fp->daccess; 4766 rsp->OutputBufferLength = 4767 cpu_to_le32(sizeof(struct smb2_file_access_info)); 4768 } 4769 4770 static int get_file_basic_info(struct smb2_query_info_rsp *rsp, 4771 struct ksmbd_file *fp, void *rsp_org) 4772 { 4773 struct smb2_file_basic_info *basic_info; 4774 struct kstat stat; 4775 u64 time; 4776 int ret; 4777 4778 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { 4779 pr_err("no right to read the attributes : 0x%x\n", 4780 fp->daccess); 4781 return -EACCES; 4782 } 4783 4784 ret = vfs_getattr(&fp->filp->f_path, &stat, STATX_BASIC_STATS, 4785 AT_STATX_SYNC_AS_STAT); 4786 if (ret) 4787 return ret; 4788 4789 basic_info = (struct smb2_file_basic_info *)rsp->Buffer; 4790 basic_info->CreationTime = cpu_to_le64(fp->create_time); 4791 time = ksmbd_UnixTimeToNT(stat.atime); 4792 basic_info->LastAccessTime = cpu_to_le64(time); 4793 time = ksmbd_UnixTimeToNT(stat.mtime); 4794 basic_info->LastWriteTime = cpu_to_le64(time); 4795 time = ksmbd_UnixTimeToNT(stat.ctime); 4796 basic_info->ChangeTime = cpu_to_le64(time); 4797 basic_info->Attributes = fp->f_ci->m_fattr; 4798 basic_info->Pad1 = 0; 4799 rsp->OutputBufferLength = 4800 cpu_to_le32(sizeof(struct smb2_file_basic_info)); 4801 return 0; 4802 } 4803 4804 static int get_file_standard_info(struct smb2_query_info_rsp *rsp, 4805 struct ksmbd_file *fp, void *rsp_org) 4806 { 4807 struct smb2_file_standard_info *sinfo; 4808 unsigned int delete_pending; 4809 struct kstat stat; 4810 int ret; 4811 4812 ret = vfs_getattr(&fp->filp->f_path, &stat, STATX_BASIC_STATS, 4813 AT_STATX_SYNC_AS_STAT); 4814 if (ret) 4815 return ret; 4816 4817 sinfo = (struct smb2_file_standard_info *)rsp->Buffer; 4818 delete_pending = ksmbd_inode_pending_delete(fp); 4819 4820 sinfo->AllocationSize = cpu_to_le64(stat.blocks << 9); 4821 sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); 4822 sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending); 4823 sinfo->DeletePending = delete_pending; 4824 sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0; 4825 rsp->OutputBufferLength = 4826 cpu_to_le32(sizeof(struct smb2_file_standard_info)); 4827 4828 return 0; 4829 } 4830 4831 static void get_file_alignment_info(struct smb2_query_info_rsp *rsp, 4832 void *rsp_org) 4833 { 4834 struct smb2_file_alignment_info *file_info; 4835 4836 file_info = (struct smb2_file_alignment_info *)rsp->Buffer; 4837 file_info->AlignmentRequirement = 0; 4838 rsp->OutputBufferLength = 4839 cpu_to_le32(sizeof(struct smb2_file_alignment_info)); 4840 } 4841 4842 static int get_file_all_info(struct ksmbd_work *work, 4843 struct smb2_query_info_rsp *rsp, 4844 struct ksmbd_file *fp, 4845 void *rsp_org) 4846 { 4847 struct ksmbd_conn *conn = work->conn; 4848 struct smb2_file_all_info *file_info; 4849 unsigned int delete_pending; 4850 struct kstat stat; 4851 int conv_len; 4852 char *filename; 4853 u64 time; 4854 int ret; 4855 4856 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { 4857 ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n", 4858 fp->daccess); 4859 return -EACCES; 4860 } 4861 4862 filename = convert_to_nt_pathname(work->tcon->share_conf, &fp->filp->f_path); 4863 if (IS_ERR(filename)) 4864 return PTR_ERR(filename); 4865 4866 ret = vfs_getattr(&fp->filp->f_path, &stat, STATX_BASIC_STATS, 4867 AT_STATX_SYNC_AS_STAT); 4868 if (ret) 4869 return ret; 4870 4871 ksmbd_debug(SMB, "filename = %s\n", filename); 4872 delete_pending = ksmbd_inode_pending_delete(fp); 4873 file_info = (struct smb2_file_all_info *)rsp->Buffer; 4874 4875 file_info->CreationTime = cpu_to_le64(fp->create_time); 4876 time = ksmbd_UnixTimeToNT(stat.atime); 4877 file_info->LastAccessTime = cpu_to_le64(time); 4878 time = ksmbd_UnixTimeToNT(stat.mtime); 4879 file_info->LastWriteTime = cpu_to_le64(time); 4880 time = ksmbd_UnixTimeToNT(stat.ctime); 4881 file_info->ChangeTime = cpu_to_le64(time); 4882 file_info->Attributes = fp->f_ci->m_fattr; 4883 file_info->Pad1 = 0; 4884 file_info->AllocationSize = 4885 cpu_to_le64(stat.blocks << 9); 4886 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); 4887 file_info->NumberOfLinks = 4888 cpu_to_le32(get_nlink(&stat) - delete_pending); 4889 file_info->DeletePending = delete_pending; 4890 file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0; 4891 file_info->Pad2 = 0; 4892 file_info->IndexNumber = cpu_to_le64(stat.ino); 4893 file_info->EASize = 0; 4894 file_info->AccessFlags = fp->daccess; 4895 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); 4896 file_info->Mode = fp->coption; 4897 file_info->AlignmentRequirement = 0; 4898 conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename, 4899 PATH_MAX, conn->local_nls, 0); 4900 conv_len *= 2; 4901 file_info->FileNameLength = cpu_to_le32(conv_len); 4902 rsp->OutputBufferLength = 4903 cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1); 4904 kfree(filename); 4905 return 0; 4906 } 4907 4908 static void get_file_alternate_info(struct ksmbd_work *work, 4909 struct smb2_query_info_rsp *rsp, 4910 struct ksmbd_file *fp, 4911 void *rsp_org) 4912 { 4913 struct ksmbd_conn *conn = work->conn; 4914 struct smb2_file_alt_name_info *file_info; 4915 struct dentry *dentry = fp->filp->f_path.dentry; 4916 int conv_len; 4917 4918 spin_lock(&dentry->d_lock); 4919 file_info = (struct smb2_file_alt_name_info *)rsp->Buffer; 4920 conv_len = ksmbd_extract_shortname(conn, 4921 dentry->d_name.name, 4922 file_info->FileName); 4923 spin_unlock(&dentry->d_lock); 4924 file_info->FileNameLength = cpu_to_le32(conv_len); 4925 rsp->OutputBufferLength = 4926 cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len); 4927 } 4928 4929 static int get_file_stream_info(struct ksmbd_work *work, 4930 struct smb2_query_info_rsp *rsp, 4931 struct ksmbd_file *fp, 4932 void *rsp_org) 4933 { 4934 struct ksmbd_conn *conn = work->conn; 4935 struct smb2_file_stream_info *file_info; 4936 char *stream_name, *xattr_list = NULL, *stream_buf; 4937 struct kstat stat; 4938 const struct path *path = &fp->filp->f_path; 4939 ssize_t xattr_list_len; 4940 int nbytes = 0, streamlen, stream_name_len, next, idx = 0; 4941 int buf_free_len; 4942 struct smb2_query_info_req *req = ksmbd_req_buf_next(work); 4943 int ret; 4944 4945 ret = vfs_getattr(&fp->filp->f_path, &stat, STATX_BASIC_STATS, 4946 AT_STATX_SYNC_AS_STAT); 4947 if (ret) 4948 return ret; 4949 4950 file_info = (struct smb2_file_stream_info *)rsp->Buffer; 4951 4952 buf_free_len = 4953 smb2_calc_max_out_buf_len(work, 8, 4954 le32_to_cpu(req->OutputBufferLength)); 4955 if (buf_free_len < 0) 4956 goto out; 4957 4958 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); 4959 if (xattr_list_len < 0) { 4960 goto out; 4961 } else if (!xattr_list_len) { 4962 ksmbd_debug(SMB, "empty xattr in the file\n"); 4963 goto out; 4964 } 4965 4966 while (idx < xattr_list_len) { 4967 stream_name = xattr_list + idx; 4968 streamlen = strlen(stream_name); 4969 idx += streamlen + 1; 4970 4971 ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen); 4972 4973 if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN], 4974 STREAM_PREFIX, STREAM_PREFIX_LEN)) 4975 continue; 4976 4977 stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN + 4978 STREAM_PREFIX_LEN); 4979 streamlen = stream_name_len; 4980 4981 /* plus : size */ 4982 streamlen += 1; 4983 stream_buf = kmalloc(streamlen + 1, GFP_KERNEL); 4984 if (!stream_buf) 4985 break; 4986 4987 streamlen = snprintf(stream_buf, streamlen + 1, 4988 ":%s", &stream_name[XATTR_NAME_STREAM_LEN]); 4989 4990 next = sizeof(struct smb2_file_stream_info) + streamlen * 2; 4991 if (next > buf_free_len) { 4992 kfree(stream_buf); 4993 break; 4994 } 4995 4996 file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes]; 4997 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, 4998 stream_buf, streamlen, 4999 conn->local_nls, 0); 5000 streamlen *= 2; 5001 kfree(stream_buf); 5002 file_info->StreamNameLength = cpu_to_le32(streamlen); 5003 file_info->StreamSize = cpu_to_le64(stream_name_len); 5004 file_info->StreamAllocationSize = cpu_to_le64(stream_name_len); 5005 5006 nbytes += next; 5007 buf_free_len -= next; 5008 file_info->NextEntryOffset = cpu_to_le32(next); 5009 } 5010 5011 out: 5012 if (!S_ISDIR(stat.mode) && 5013 buf_free_len >= sizeof(struct smb2_file_stream_info) + 7 * 2) { 5014 file_info = (struct smb2_file_stream_info *) 5015 &rsp->Buffer[nbytes]; 5016 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, 5017 "::$DATA", 7, conn->local_nls, 0); 5018 streamlen *= 2; 5019 file_info->StreamNameLength = cpu_to_le32(streamlen); 5020 file_info->StreamSize = cpu_to_le64(stat.size); 5021 file_info->StreamAllocationSize = cpu_to_le64(stat.blocks << 9); 5022 nbytes += sizeof(struct smb2_file_stream_info) + streamlen; 5023 } 5024 5025 /* last entry offset should be 0 */ 5026 file_info->NextEntryOffset = 0; 5027 kvfree(xattr_list); 5028 5029 rsp->OutputBufferLength = cpu_to_le32(nbytes); 5030 5031 return 0; 5032 } 5033 5034 static int get_file_internal_info(struct smb2_query_info_rsp *rsp, 5035 struct ksmbd_file *fp, void *rsp_org) 5036 { 5037 struct smb2_file_internal_info *file_info; 5038 struct kstat stat; 5039 int ret; 5040 5041 ret = vfs_getattr(&fp->filp->f_path, &stat, STATX_BASIC_STATS, 5042 AT_STATX_SYNC_AS_STAT); 5043 if (ret) 5044 return ret; 5045 5046 file_info = (struct smb2_file_internal_info *)rsp->Buffer; 5047 file_info->IndexNumber = cpu_to_le64(stat.ino); 5048 rsp->OutputBufferLength = 5049 cpu_to_le32(sizeof(struct smb2_file_internal_info)); 5050 5051 return 0; 5052 } 5053 5054 static int get_file_network_open_info(struct smb2_query_info_rsp *rsp, 5055 struct ksmbd_file *fp, void *rsp_org) 5056 { 5057 struct smb2_file_ntwrk_info *file_info; 5058 struct kstat stat; 5059 u64 time; 5060 int ret; 5061 5062 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { 5063 pr_err("no right to read the attributes : 0x%x\n", 5064 fp->daccess); 5065 return -EACCES; 5066 } 5067 5068 ret = vfs_getattr(&fp->filp->f_path, &stat, STATX_BASIC_STATS, 5069 AT_STATX_SYNC_AS_STAT); 5070 if (ret) 5071 return ret; 5072 5073 file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer; 5074 5075 file_info->CreationTime = cpu_to_le64(fp->create_time); 5076 time = ksmbd_UnixTimeToNT(stat.atime); 5077 file_info->LastAccessTime = cpu_to_le64(time); 5078 time = ksmbd_UnixTimeToNT(stat.mtime); 5079 file_info->LastWriteTime = cpu_to_le64(time); 5080 time = ksmbd_UnixTimeToNT(stat.ctime); 5081 file_info->ChangeTime = cpu_to_le64(time); 5082 file_info->Attributes = fp->f_ci->m_fattr; 5083 file_info->AllocationSize = cpu_to_le64(stat.blocks << 9); 5084 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); 5085 file_info->Reserved = cpu_to_le32(0); 5086 rsp->OutputBufferLength = 5087 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info)); 5088 return 0; 5089 } 5090 5091 static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org) 5092 { 5093 struct smb2_file_ea_info *file_info; 5094 5095 file_info = (struct smb2_file_ea_info *)rsp->Buffer; 5096 file_info->EASize = 0; 5097 rsp->OutputBufferLength = 5098 cpu_to_le32(sizeof(struct smb2_file_ea_info)); 5099 } 5100 5101 static void get_file_position_info(struct smb2_query_info_rsp *rsp, 5102 struct ksmbd_file *fp, void *rsp_org) 5103 { 5104 struct smb2_file_pos_info *file_info; 5105 5106 file_info = (struct smb2_file_pos_info *)rsp->Buffer; 5107 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); 5108 rsp->OutputBufferLength = 5109 cpu_to_le32(sizeof(struct smb2_file_pos_info)); 5110 } 5111 5112 static void get_file_mode_info(struct smb2_query_info_rsp *rsp, 5113 struct ksmbd_file *fp, void *rsp_org) 5114 { 5115 struct smb2_file_mode_info *file_info; 5116 5117 file_info = (struct smb2_file_mode_info *)rsp->Buffer; 5118 file_info->Mode = fp->coption & FILE_MODE_INFO_MASK; 5119 rsp->OutputBufferLength = 5120 cpu_to_le32(sizeof(struct smb2_file_mode_info)); 5121 } 5122 5123 static int get_file_compression_info(struct smb2_query_info_rsp *rsp, 5124 struct ksmbd_file *fp, void *rsp_org) 5125 { 5126 struct smb2_file_comp_info *file_info; 5127 struct kstat stat; 5128 int ret; 5129 5130 ret = vfs_getattr(&fp->filp->f_path, &stat, STATX_BASIC_STATS, 5131 AT_STATX_SYNC_AS_STAT); 5132 if (ret) 5133 return ret; 5134 5135 file_info = (struct smb2_file_comp_info *)rsp->Buffer; 5136 file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9); 5137 file_info->CompressionFormat = COMPRESSION_FORMAT_NONE; 5138 file_info->CompressionUnitShift = 0; 5139 file_info->ChunkShift = 0; 5140 file_info->ClusterShift = 0; 5141 memset(&file_info->Reserved[0], 0, 3); 5142 5143 rsp->OutputBufferLength = 5144 cpu_to_le32(sizeof(struct smb2_file_comp_info)); 5145 5146 return 0; 5147 } 5148 5149 static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp, 5150 struct ksmbd_file *fp, void *rsp_org) 5151 { 5152 struct smb2_file_attr_tag_info *file_info; 5153 5154 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { 5155 pr_err("no right to read the attributes : 0x%x\n", 5156 fp->daccess); 5157 return -EACCES; 5158 } 5159 5160 file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer; 5161 file_info->FileAttributes = fp->f_ci->m_fattr; 5162 file_info->ReparseTag = 0; 5163 rsp->OutputBufferLength = 5164 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info)); 5165 return 0; 5166 } 5167 5168 static int find_file_posix_info(struct smb2_query_info_rsp *rsp, 5169 struct ksmbd_file *fp, void *rsp_org) 5170 { 5171 struct smb311_posix_qinfo *file_info; 5172 struct inode *inode = file_inode(fp->filp); 5173 struct mnt_idmap *idmap = file_mnt_idmap(fp->filp); 5174 vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); 5175 vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); 5176 struct kstat stat; 5177 u64 time; 5178 int out_buf_len = sizeof(struct smb311_posix_qinfo) + 32; 5179 int ret; 5180 5181 ret = vfs_getattr(&fp->filp->f_path, &stat, STATX_BASIC_STATS, 5182 AT_STATX_SYNC_AS_STAT); 5183 if (ret) 5184 return ret; 5185 5186 file_info = (struct smb311_posix_qinfo *)rsp->Buffer; 5187 file_info->CreationTime = cpu_to_le64(fp->create_time); 5188 time = ksmbd_UnixTimeToNT(stat.atime); 5189 file_info->LastAccessTime = cpu_to_le64(time); 5190 time = ksmbd_UnixTimeToNT(stat.mtime); 5191 file_info->LastWriteTime = cpu_to_le64(time); 5192 time = ksmbd_UnixTimeToNT(stat.ctime); 5193 file_info->ChangeTime = cpu_to_le64(time); 5194 file_info->DosAttributes = fp->f_ci->m_fattr; 5195 file_info->Inode = cpu_to_le64(stat.ino); 5196 file_info->EndOfFile = cpu_to_le64(stat.size); 5197 file_info->AllocationSize = cpu_to_le64(stat.blocks << 9); 5198 file_info->HardLinks = cpu_to_le32(stat.nlink); 5199 file_info->Mode = cpu_to_le32(stat.mode & 0777); 5200 switch (stat.mode & S_IFMT) { 5201 case S_IFDIR: 5202 file_info->Mode |= cpu_to_le32(POSIX_TYPE_DIR << POSIX_FILETYPE_SHIFT); 5203 break; 5204 case S_IFLNK: 5205 file_info->Mode |= cpu_to_le32(POSIX_TYPE_SYMLINK << POSIX_FILETYPE_SHIFT); 5206 break; 5207 case S_IFCHR: 5208 file_info->Mode |= cpu_to_le32(POSIX_TYPE_CHARDEV << POSIX_FILETYPE_SHIFT); 5209 break; 5210 case S_IFBLK: 5211 file_info->Mode |= cpu_to_le32(POSIX_TYPE_BLKDEV << POSIX_FILETYPE_SHIFT); 5212 break; 5213 case S_IFIFO: 5214 file_info->Mode |= cpu_to_le32(POSIX_TYPE_FIFO << POSIX_FILETYPE_SHIFT); 5215 break; 5216 case S_IFSOCK: 5217 file_info->Mode |= cpu_to_le32(POSIX_TYPE_SOCKET << POSIX_FILETYPE_SHIFT); 5218 } 5219 5220 file_info->DeviceId = cpu_to_le32(stat.rdev); 5221 5222 /* 5223 * Sids(32) contain two sids(Domain sid(16), UNIX group sid(16)). 5224 * UNIX sid(16) = revision(1) + num_subauth(1) + authority(6) + 5225 * sub_auth(4 * 1(num_subauth)) + RID(4). 5226 */ 5227 id_to_sid(from_kuid_munged(&init_user_ns, vfsuid_into_kuid(vfsuid)), 5228 SIDUNIX_USER, (struct smb_sid *)&file_info->Sids[0]); 5229 id_to_sid(from_kgid_munged(&init_user_ns, vfsgid_into_kgid(vfsgid)), 5230 SIDUNIX_GROUP, (struct smb_sid *)&file_info->Sids[16]); 5231 5232 rsp->OutputBufferLength = cpu_to_le32(out_buf_len); 5233 5234 return 0; 5235 } 5236 5237 static int smb2_get_info_file(struct ksmbd_work *work, 5238 struct smb2_query_info_req *req, 5239 struct smb2_query_info_rsp *rsp) 5240 { 5241 struct ksmbd_file *fp; 5242 int fileinfoclass = 0; 5243 int rc = 0; 5244 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; 5245 5246 if (test_share_config_flag(work->tcon->share_conf, 5247 KSMBD_SHARE_FLAG_PIPE)) { 5248 /* smb2 info file called for pipe */ 5249 return smb2_get_info_file_pipe(work->sess, req, rsp, 5250 work->response_buf); 5251 } 5252 5253 if (work->next_smb2_rcv_hdr_off) { 5254 if (!has_file_id(req->VolatileFileId)) { 5255 ksmbd_debug(SMB, "Compound request set FID = %llu\n", 5256 work->compound_fid); 5257 id = work->compound_fid; 5258 pid = work->compound_pfid; 5259 } 5260 } 5261 5262 if (!has_file_id(id)) { 5263 id = req->VolatileFileId; 5264 pid = req->PersistentFileId; 5265 } 5266 5267 fp = ksmbd_lookup_fd_slow(work, id, pid); 5268 if (!fp) 5269 return -ENOENT; 5270 5271 fileinfoclass = req->FileInfoClass; 5272 5273 switch (fileinfoclass) { 5274 case FILE_ACCESS_INFORMATION: 5275 get_file_access_info(rsp, fp, work->response_buf); 5276 break; 5277 5278 case FILE_BASIC_INFORMATION: 5279 rc = get_file_basic_info(rsp, fp, work->response_buf); 5280 break; 5281 5282 case FILE_STANDARD_INFORMATION: 5283 rc = get_file_standard_info(rsp, fp, work->response_buf); 5284 break; 5285 5286 case FILE_ALIGNMENT_INFORMATION: 5287 get_file_alignment_info(rsp, work->response_buf); 5288 break; 5289 5290 case FILE_ALL_INFORMATION: 5291 rc = get_file_all_info(work, rsp, fp, work->response_buf); 5292 break; 5293 5294 case FILE_ALTERNATE_NAME_INFORMATION: 5295 get_file_alternate_info(work, rsp, fp, work->response_buf); 5296 break; 5297 5298 case FILE_STREAM_INFORMATION: 5299 rc = get_file_stream_info(work, rsp, fp, work->response_buf); 5300 break; 5301 5302 case FILE_INTERNAL_INFORMATION: 5303 rc = get_file_internal_info(rsp, fp, work->response_buf); 5304 break; 5305 5306 case FILE_NETWORK_OPEN_INFORMATION: 5307 rc = get_file_network_open_info(rsp, fp, work->response_buf); 5308 break; 5309 5310 case FILE_EA_INFORMATION: 5311 get_file_ea_info(rsp, work->response_buf); 5312 break; 5313 5314 case FILE_FULL_EA_INFORMATION: 5315 rc = smb2_get_ea(work, fp, req, rsp, work->response_buf); 5316 break; 5317 5318 case FILE_POSITION_INFORMATION: 5319 get_file_position_info(rsp, fp, work->response_buf); 5320 break; 5321 5322 case FILE_MODE_INFORMATION: 5323 get_file_mode_info(rsp, fp, work->response_buf); 5324 break; 5325 5326 case FILE_COMPRESSION_INFORMATION: 5327 rc = get_file_compression_info(rsp, fp, work->response_buf); 5328 break; 5329 5330 case FILE_ATTRIBUTE_TAG_INFORMATION: 5331 rc = get_file_attribute_tag_info(rsp, fp, work->response_buf); 5332 break; 5333 case SMB_FIND_FILE_POSIX_INFO: 5334 if (!work->tcon->posix_extensions) { 5335 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n"); 5336 rc = -EOPNOTSUPP; 5337 } else { 5338 rc = find_file_posix_info(rsp, fp, work->response_buf); 5339 } 5340 break; 5341 default: 5342 ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n", 5343 fileinfoclass); 5344 rc = -EOPNOTSUPP; 5345 } 5346 if (!rc) 5347 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), 5348 rsp, work->response_buf); 5349 ksmbd_fd_put(work, fp); 5350 return rc; 5351 } 5352 5353 static int smb2_get_info_filesystem(struct ksmbd_work *work, 5354 struct smb2_query_info_req *req, 5355 struct smb2_query_info_rsp *rsp) 5356 { 5357 struct ksmbd_session *sess = work->sess; 5358 struct ksmbd_conn *conn = work->conn; 5359 struct ksmbd_share_config *share = work->tcon->share_conf; 5360 int fsinfoclass = 0; 5361 struct kstatfs stfs; 5362 struct path path; 5363 int rc = 0, len; 5364 5365 if (!share->path) 5366 return -EIO; 5367 5368 rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path); 5369 if (rc) { 5370 pr_err("cannot create vfs path\n"); 5371 return -EIO; 5372 } 5373 5374 rc = vfs_statfs(&path, &stfs); 5375 if (rc) { 5376 pr_err("cannot do stat of path %s\n", share->path); 5377 path_put(&path); 5378 return -EIO; 5379 } 5380 5381 fsinfoclass = req->FileInfoClass; 5382 5383 switch (fsinfoclass) { 5384 case FS_DEVICE_INFORMATION: 5385 { 5386 struct filesystem_device_info *info; 5387 5388 info = (struct filesystem_device_info *)rsp->Buffer; 5389 5390 info->DeviceType = cpu_to_le32(FILE_DEVICE_DISK); 5391 info->DeviceCharacteristics = 5392 cpu_to_le32(FILE_DEVICE_IS_MOUNTED); 5393 if (!test_tree_conn_flag(work->tcon, 5394 KSMBD_TREE_CONN_FLAG_WRITABLE)) 5395 info->DeviceCharacteristics |= 5396 cpu_to_le32(FILE_READ_ONLY_DEVICE); 5397 rsp->OutputBufferLength = cpu_to_le32(8); 5398 break; 5399 } 5400 case FS_ATTRIBUTE_INFORMATION: 5401 { 5402 struct filesystem_attribute_info *info; 5403 size_t sz; 5404 5405 info = (struct filesystem_attribute_info *)rsp->Buffer; 5406 info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS | 5407 FILE_PERSISTENT_ACLS | 5408 FILE_UNICODE_ON_DISK | 5409 FILE_CASE_PRESERVED_NAMES | 5410 FILE_CASE_SENSITIVE_SEARCH | 5411 FILE_SUPPORTS_BLOCK_REFCOUNTING); 5412 5413 info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps); 5414 5415 if (test_share_config_flag(work->tcon->share_conf, 5416 KSMBD_SHARE_FLAG_STREAMS)) 5417 info->Attributes |= cpu_to_le32(FILE_NAMED_STREAMS); 5418 5419 info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen); 5420 len = smbConvertToUTF16((__le16 *)info->FileSystemName, 5421 "NTFS", PATH_MAX, conn->local_nls, 0); 5422 len = len * 2; 5423 info->FileSystemNameLen = cpu_to_le32(len); 5424 sz = sizeof(struct filesystem_attribute_info) - 2 + len; 5425 rsp->OutputBufferLength = cpu_to_le32(sz); 5426 break; 5427 } 5428 case FS_VOLUME_INFORMATION: 5429 { 5430 struct filesystem_vol_info *info; 5431 size_t sz; 5432 unsigned int serial_crc = 0; 5433 5434 info = (struct filesystem_vol_info *)(rsp->Buffer); 5435 info->VolumeCreationTime = 0; 5436 serial_crc = crc32_le(serial_crc, share->name, 5437 strlen(share->name)); 5438 serial_crc = crc32_le(serial_crc, share->path, 5439 strlen(share->path)); 5440 serial_crc = crc32_le(serial_crc, ksmbd_netbios_name(), 5441 strlen(ksmbd_netbios_name())); 5442 /* Taking dummy value of serial number*/ 5443 info->SerialNumber = cpu_to_le32(serial_crc); 5444 len = smbConvertToUTF16((__le16 *)info->VolumeLabel, 5445 share->name, PATH_MAX, 5446 conn->local_nls, 0); 5447 len = len * 2; 5448 info->VolumeLabelSize = cpu_to_le32(len); 5449 info->Reserved = 0; 5450 sz = sizeof(struct filesystem_vol_info) - 2 + len; 5451 rsp->OutputBufferLength = cpu_to_le32(sz); 5452 break; 5453 } 5454 case FS_SIZE_INFORMATION: 5455 { 5456 struct filesystem_info *info; 5457 5458 info = (struct filesystem_info *)(rsp->Buffer); 5459 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); 5460 info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree); 5461 info->SectorsPerAllocationUnit = cpu_to_le32(1); 5462 info->BytesPerSector = cpu_to_le32(stfs.f_bsize); 5463 rsp->OutputBufferLength = cpu_to_le32(24); 5464 break; 5465 } 5466 case FS_FULL_SIZE_INFORMATION: 5467 { 5468 struct smb2_fs_full_size_info *info; 5469 5470 info = (struct smb2_fs_full_size_info *)(rsp->Buffer); 5471 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); 5472 info->CallerAvailableAllocationUnits = 5473 cpu_to_le64(stfs.f_bavail); 5474 info->ActualAvailableAllocationUnits = 5475 cpu_to_le64(stfs.f_bfree); 5476 info->SectorsPerAllocationUnit = cpu_to_le32(1); 5477 info->BytesPerSector = cpu_to_le32(stfs.f_bsize); 5478 rsp->OutputBufferLength = cpu_to_le32(32); 5479 break; 5480 } 5481 case FS_OBJECT_ID_INFORMATION: 5482 { 5483 struct object_id_info *info; 5484 5485 info = (struct object_id_info *)(rsp->Buffer); 5486 5487 if (!user_guest(sess->user)) 5488 memcpy(info->objid, user_passkey(sess->user), 16); 5489 else 5490 memset(info->objid, 0, 16); 5491 5492 info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC); 5493 info->extended_info.version = cpu_to_le32(1); 5494 info->extended_info.release = cpu_to_le32(1); 5495 info->extended_info.rel_date = 0; 5496 memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0")); 5497 rsp->OutputBufferLength = cpu_to_le32(64); 5498 break; 5499 } 5500 case FS_SECTOR_SIZE_INFORMATION: 5501 { 5502 struct smb3_fs_ss_info *info; 5503 unsigned int sector_size = 5504 min_t(unsigned int, path.mnt->mnt_sb->s_blocksize, 4096); 5505 5506 info = (struct smb3_fs_ss_info *)(rsp->Buffer); 5507 5508 info->LogicalBytesPerSector = cpu_to_le32(sector_size); 5509 info->PhysicalBytesPerSectorForAtomicity = 5510 cpu_to_le32(sector_size); 5511 info->PhysicalBytesPerSectorForPerf = cpu_to_le32(sector_size); 5512 info->FSEffPhysicalBytesPerSectorForAtomicity = 5513 cpu_to_le32(sector_size); 5514 info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE | 5515 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE); 5516 info->ByteOffsetForSectorAlignment = 0; 5517 info->ByteOffsetForPartitionAlignment = 0; 5518 rsp->OutputBufferLength = cpu_to_le32(28); 5519 break; 5520 } 5521 case FS_CONTROL_INFORMATION: 5522 { 5523 /* 5524 * TODO : The current implementation is based on 5525 * test result with win7(NTFS) server. It's need to 5526 * modify this to get valid Quota values 5527 * from Linux kernel 5528 */ 5529 struct smb2_fs_control_info *info; 5530 5531 info = (struct smb2_fs_control_info *)(rsp->Buffer); 5532 info->FreeSpaceStartFiltering = 0; 5533 info->FreeSpaceThreshold = 0; 5534 info->FreeSpaceStopFiltering = 0; 5535 info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID); 5536 info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID); 5537 info->Padding = 0; 5538 rsp->OutputBufferLength = cpu_to_le32(48); 5539 break; 5540 } 5541 case FS_POSIX_INFORMATION: 5542 { 5543 struct filesystem_posix_info *info; 5544 5545 if (!work->tcon->posix_extensions) { 5546 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n"); 5547 rc = -EOPNOTSUPP; 5548 } else { 5549 info = (struct filesystem_posix_info *)(rsp->Buffer); 5550 info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize); 5551 info->BlockSize = cpu_to_le32(stfs.f_bsize); 5552 info->TotalBlocks = cpu_to_le64(stfs.f_blocks); 5553 info->BlocksAvail = cpu_to_le64(stfs.f_bfree); 5554 info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail); 5555 info->TotalFileNodes = cpu_to_le64(stfs.f_files); 5556 info->FreeFileNodes = cpu_to_le64(stfs.f_ffree); 5557 rsp->OutputBufferLength = cpu_to_le32(56); 5558 } 5559 break; 5560 } 5561 default: 5562 path_put(&path); 5563 return -EOPNOTSUPP; 5564 } 5565 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), 5566 rsp, work->response_buf); 5567 path_put(&path); 5568 return rc; 5569 } 5570 5571 static int smb2_get_info_sec(struct ksmbd_work *work, 5572 struct smb2_query_info_req *req, 5573 struct smb2_query_info_rsp *rsp) 5574 { 5575 struct ksmbd_file *fp; 5576 struct mnt_idmap *idmap; 5577 struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL; 5578 struct smb_fattr fattr = {{0}}; 5579 struct inode *inode; 5580 __u32 secdesclen = 0; 5581 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; 5582 int addition_info = le32_to_cpu(req->AdditionalInformation); 5583 int rc = 0, ppntsd_size = 0; 5584 5585 if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO | 5586 PROTECTED_DACL_SECINFO | 5587 UNPROTECTED_DACL_SECINFO)) { 5588 ksmbd_debug(SMB, "Unsupported addition info: 0x%x)\n", 5589 addition_info); 5590 5591 pntsd->revision = cpu_to_le16(1); 5592 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED); 5593 pntsd->osidoffset = 0; 5594 pntsd->gsidoffset = 0; 5595 pntsd->sacloffset = 0; 5596 pntsd->dacloffset = 0; 5597 5598 secdesclen = sizeof(struct smb_ntsd); 5599 rsp->OutputBufferLength = cpu_to_le32(secdesclen); 5600 5601 return 0; 5602 } 5603 5604 if (work->next_smb2_rcv_hdr_off) { 5605 if (!has_file_id(req->VolatileFileId)) { 5606 ksmbd_debug(SMB, "Compound request set FID = %llu\n", 5607 work->compound_fid); 5608 id = work->compound_fid; 5609 pid = work->compound_pfid; 5610 } 5611 } 5612 5613 if (!has_file_id(id)) { 5614 id = req->VolatileFileId; 5615 pid = req->PersistentFileId; 5616 } 5617 5618 fp = ksmbd_lookup_fd_slow(work, id, pid); 5619 if (!fp) 5620 return -ENOENT; 5621 5622 idmap = file_mnt_idmap(fp->filp); 5623 inode = file_inode(fp->filp); 5624 ksmbd_acls_fattr(&fattr, idmap, inode); 5625 5626 if (test_share_config_flag(work->tcon->share_conf, 5627 KSMBD_SHARE_FLAG_ACL_XATTR)) 5628 ppntsd_size = ksmbd_vfs_get_sd_xattr(work->conn, idmap, 5629 fp->filp->f_path.dentry, 5630 &ppntsd); 5631 5632 /* Check if sd buffer size exceeds response buffer size */ 5633 if (smb2_resp_buf_len(work, 8) > ppntsd_size) 5634 rc = build_sec_desc(idmap, pntsd, ppntsd, ppntsd_size, 5635 addition_info, &secdesclen, &fattr); 5636 posix_acl_release(fattr.cf_acls); 5637 posix_acl_release(fattr.cf_dacls); 5638 kfree(ppntsd); 5639 ksmbd_fd_put(work, fp); 5640 if (rc) 5641 return rc; 5642 5643 rsp->OutputBufferLength = cpu_to_le32(secdesclen); 5644 return 0; 5645 } 5646 5647 /** 5648 * smb2_query_info() - handler for smb2 query info command 5649 * @work: smb work containing query info request buffer 5650 * 5651 * Return: 0 on success, otherwise error 5652 */ 5653 int smb2_query_info(struct ksmbd_work *work) 5654 { 5655 struct smb2_query_info_req *req; 5656 struct smb2_query_info_rsp *rsp; 5657 int rc = 0; 5658 5659 WORK_BUFFERS(work, req, rsp); 5660 5661 ksmbd_debug(SMB, "GOT query info request\n"); 5662 5663 if (ksmbd_override_fsids(work)) { 5664 rc = -ENOMEM; 5665 goto err_out; 5666 } 5667 5668 switch (req->InfoType) { 5669 case SMB2_O_INFO_FILE: 5670 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); 5671 rc = smb2_get_info_file(work, req, rsp); 5672 break; 5673 case SMB2_O_INFO_FILESYSTEM: 5674 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n"); 5675 rc = smb2_get_info_filesystem(work, req, rsp); 5676 break; 5677 case SMB2_O_INFO_SECURITY: 5678 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); 5679 rc = smb2_get_info_sec(work, req, rsp); 5680 break; 5681 default: 5682 ksmbd_debug(SMB, "InfoType %d not supported yet\n", 5683 req->InfoType); 5684 rc = -EOPNOTSUPP; 5685 } 5686 ksmbd_revert_fsids(work); 5687 5688 if (!rc) { 5689 rsp->StructureSize = cpu_to_le16(9); 5690 rsp->OutputBufferOffset = cpu_to_le16(72); 5691 rc = ksmbd_iov_pin_rsp(work, (void *)rsp, 5692 offsetof(struct smb2_query_info_rsp, Buffer) + 5693 le32_to_cpu(rsp->OutputBufferLength)); 5694 } 5695 5696 err_out: 5697 if (rc < 0) { 5698 if (rc == -EACCES) 5699 rsp->hdr.Status = STATUS_ACCESS_DENIED; 5700 else if (rc == -ENOENT) 5701 rsp->hdr.Status = STATUS_FILE_CLOSED; 5702 else if (rc == -EIO) 5703 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; 5704 else if (rc == -ENOMEM) 5705 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; 5706 else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0) 5707 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; 5708 smb2_set_err_rsp(work); 5709 5710 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", 5711 rc); 5712 return rc; 5713 } 5714 return 0; 5715 } 5716 5717 /** 5718 * smb2_close_pipe() - handler for closing IPC pipe 5719 * @work: smb work containing close request buffer 5720 * 5721 * Return: 0 5722 */ 5723 static noinline int smb2_close_pipe(struct ksmbd_work *work) 5724 { 5725 u64 id; 5726 struct smb2_close_req *req; 5727 struct smb2_close_rsp *rsp; 5728 5729 WORK_BUFFERS(work, req, rsp); 5730 5731 id = req->VolatileFileId; 5732 ksmbd_session_rpc_close(work->sess, id); 5733 5734 rsp->StructureSize = cpu_to_le16(60); 5735 rsp->Flags = 0; 5736 rsp->Reserved = 0; 5737 rsp->CreationTime = 0; 5738 rsp->LastAccessTime = 0; 5739 rsp->LastWriteTime = 0; 5740 rsp->ChangeTime = 0; 5741 rsp->AllocationSize = 0; 5742 rsp->EndOfFile = 0; 5743 rsp->Attributes = 0; 5744 5745 return ksmbd_iov_pin_rsp(work, (void *)rsp, 5746 sizeof(struct smb2_close_rsp)); 5747 } 5748 5749 /** 5750 * smb2_close() - handler for smb2 close file command 5751 * @work: smb work containing close request buffer 5752 * 5753 * Return: 0 5754 */ 5755 int smb2_close(struct ksmbd_work *work) 5756 { 5757 u64 volatile_id = KSMBD_NO_FID; 5758 u64 sess_id; 5759 struct smb2_close_req *req; 5760 struct smb2_close_rsp *rsp; 5761 struct ksmbd_conn *conn = work->conn; 5762 struct ksmbd_file *fp; 5763 u64 time; 5764 int err = 0; 5765 5766 WORK_BUFFERS(work, req, rsp); 5767 5768 if (test_share_config_flag(work->tcon->share_conf, 5769 KSMBD_SHARE_FLAG_PIPE)) { 5770 ksmbd_debug(SMB, "IPC pipe close request\n"); 5771 return smb2_close_pipe(work); 5772 } 5773 5774 sess_id = le64_to_cpu(req->hdr.SessionId); 5775 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) 5776 sess_id = work->compound_sid; 5777 5778 work->compound_sid = 0; 5779 if (check_session_id(conn, sess_id)) { 5780 work->compound_sid = sess_id; 5781 } else { 5782 rsp->hdr.Status = STATUS_USER_SESSION_DELETED; 5783 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) 5784 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 5785 err = -EBADF; 5786 goto out; 5787 } 5788 5789 if (work->next_smb2_rcv_hdr_off && 5790 !has_file_id(req->VolatileFileId)) { 5791 if (!has_file_id(work->compound_fid)) { 5792 /* file already closed, return FILE_CLOSED */ 5793 ksmbd_debug(SMB, "file already closed\n"); 5794 rsp->hdr.Status = STATUS_FILE_CLOSED; 5795 err = -EBADF; 5796 goto out; 5797 } else { 5798 ksmbd_debug(SMB, 5799 "Compound request set FID = %llu:%llu\n", 5800 work->compound_fid, 5801 work->compound_pfid); 5802 volatile_id = work->compound_fid; 5803 5804 /* file closed, stored id is not valid anymore */ 5805 work->compound_fid = KSMBD_NO_FID; 5806 work->compound_pfid = KSMBD_NO_FID; 5807 } 5808 } else { 5809 volatile_id = req->VolatileFileId; 5810 } 5811 ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id); 5812 5813 rsp->StructureSize = cpu_to_le16(60); 5814 rsp->Reserved = 0; 5815 5816 if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) { 5817 struct kstat stat; 5818 int ret; 5819 5820 fp = ksmbd_lookup_fd_fast(work, volatile_id); 5821 if (!fp) { 5822 err = -ENOENT; 5823 goto out; 5824 } 5825 5826 ret = vfs_getattr(&fp->filp->f_path, &stat, STATX_BASIC_STATS, 5827 AT_STATX_SYNC_AS_STAT); 5828 if (ret) { 5829 ksmbd_fd_put(work, fp); 5830 goto out; 5831 } 5832 5833 rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB; 5834 rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 : 5835 cpu_to_le64(stat.blocks << 9); 5836 rsp->EndOfFile = cpu_to_le64(stat.size); 5837 rsp->Attributes = fp->f_ci->m_fattr; 5838 rsp->CreationTime = cpu_to_le64(fp->create_time); 5839 time = ksmbd_UnixTimeToNT(stat.atime); 5840 rsp->LastAccessTime = cpu_to_le64(time); 5841 time = ksmbd_UnixTimeToNT(stat.mtime); 5842 rsp->LastWriteTime = cpu_to_le64(time); 5843 time = ksmbd_UnixTimeToNT(stat.ctime); 5844 rsp->ChangeTime = cpu_to_le64(time); 5845 ksmbd_fd_put(work, fp); 5846 } else { 5847 rsp->Flags = 0; 5848 rsp->AllocationSize = 0; 5849 rsp->EndOfFile = 0; 5850 rsp->Attributes = 0; 5851 rsp->CreationTime = 0; 5852 rsp->LastAccessTime = 0; 5853 rsp->LastWriteTime = 0; 5854 rsp->ChangeTime = 0; 5855 } 5856 5857 err = ksmbd_close_fd(work, volatile_id); 5858 out: 5859 if (!err) 5860 err = ksmbd_iov_pin_rsp(work, (void *)rsp, 5861 sizeof(struct smb2_close_rsp)); 5862 5863 if (err) { 5864 if (rsp->hdr.Status == 0) 5865 rsp->hdr.Status = STATUS_FILE_CLOSED; 5866 smb2_set_err_rsp(work); 5867 } 5868 5869 return err; 5870 } 5871 5872 /** 5873 * smb2_echo() - handler for smb2 echo(ping) command 5874 * @work: smb work containing echo request buffer 5875 * 5876 * Return: 0 5877 */ 5878 int smb2_echo(struct ksmbd_work *work) 5879 { 5880 struct smb2_echo_rsp *rsp = smb2_get_msg(work->response_buf); 5881 5882 if (work->next_smb2_rcv_hdr_off) 5883 rsp = ksmbd_resp_buf_next(work); 5884 5885 rsp->StructureSize = cpu_to_le16(4); 5886 rsp->Reserved = 0; 5887 return ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_echo_rsp)); 5888 } 5889 5890 static int smb2_rename(struct ksmbd_work *work, 5891 struct ksmbd_file *fp, 5892 struct smb2_file_rename_info *file_info, 5893 struct nls_table *local_nls) 5894 { 5895 struct ksmbd_share_config *share = fp->tcon->share_conf; 5896 char *new_name = NULL; 5897 int rc, flags = 0; 5898 5899 ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n"); 5900 new_name = smb2_get_name(file_info->FileName, 5901 le32_to_cpu(file_info->FileNameLength), 5902 local_nls); 5903 if (IS_ERR(new_name)) 5904 return PTR_ERR(new_name); 5905 5906 if (strchr(new_name, ':')) { 5907 int s_type; 5908 char *xattr_stream_name, *stream_name = NULL; 5909 size_t xattr_stream_size; 5910 int len; 5911 5912 rc = parse_stream_name(new_name, &stream_name, &s_type); 5913 if (rc < 0) 5914 goto out; 5915 5916 len = strlen(new_name); 5917 if (len > 0 && new_name[len - 1] != '/') { 5918 pr_err("not allow base filename in rename\n"); 5919 rc = -ESHARE; 5920 goto out; 5921 } 5922 5923 rc = ksmbd_vfs_xattr_stream_name(stream_name, 5924 &xattr_stream_name, 5925 &xattr_stream_size, 5926 s_type); 5927 if (rc) 5928 goto out; 5929 5930 rc = ksmbd_vfs_setxattr(file_mnt_idmap(fp->filp), 5931 &fp->filp->f_path, 5932 xattr_stream_name, 5933 NULL, 0, 0, true); 5934 if (rc < 0) { 5935 pr_err("failed to store stream name in xattr: %d\n", 5936 rc); 5937 rc = -EINVAL; 5938 goto out; 5939 } 5940 5941 goto out; 5942 } 5943 5944 ksmbd_debug(SMB, "new name %s\n", new_name); 5945 if (ksmbd_share_veto_filename(share, new_name)) { 5946 rc = -ENOENT; 5947 ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name); 5948 goto out; 5949 } 5950 5951 if (!file_info->ReplaceIfExists) 5952 flags = RENAME_NOREPLACE; 5953 5954 rc = ksmbd_vfs_rename(work, &fp->filp->f_path, new_name, flags); 5955 if (!rc) 5956 smb_break_all_levII_oplock(work, fp, 0); 5957 out: 5958 kfree(new_name); 5959 return rc; 5960 } 5961 5962 static int smb2_create_link(struct ksmbd_work *work, 5963 struct ksmbd_share_config *share, 5964 struct smb2_file_link_info *file_info, 5965 unsigned int buf_len, struct file *filp, 5966 struct nls_table *local_nls) 5967 { 5968 char *link_name = NULL, *target_name = NULL, *pathname = NULL; 5969 struct path path, parent_path; 5970 bool file_present = false; 5971 int rc; 5972 5973 if (buf_len < (u64)sizeof(struct smb2_file_link_info) + 5974 le32_to_cpu(file_info->FileNameLength)) 5975 return -EINVAL; 5976 5977 ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n"); 5978 pathname = kmalloc(PATH_MAX, GFP_KERNEL); 5979 if (!pathname) 5980 return -ENOMEM; 5981 5982 link_name = smb2_get_name(file_info->FileName, 5983 le32_to_cpu(file_info->FileNameLength), 5984 local_nls); 5985 if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) { 5986 rc = -EINVAL; 5987 goto out; 5988 } 5989 5990 ksmbd_debug(SMB, "link name is %s\n", link_name); 5991 target_name = file_path(filp, pathname, PATH_MAX); 5992 if (IS_ERR(target_name)) { 5993 rc = -EINVAL; 5994 goto out; 5995 } 5996 5997 ksmbd_debug(SMB, "target name is %s\n", target_name); 5998 rc = ksmbd_vfs_kern_path_locked(work, link_name, LOOKUP_NO_SYMLINKS, 5999 &parent_path, &path, 0); 6000 if (rc) { 6001 if (rc != -ENOENT) 6002 goto out; 6003 } else 6004 file_present = true; 6005 6006 if (file_info->ReplaceIfExists) { 6007 if (file_present) { 6008 rc = ksmbd_vfs_remove_file(work, &path); 6009 if (rc) { 6010 rc = -EINVAL; 6011 ksmbd_debug(SMB, "cannot delete %s\n", 6012 link_name); 6013 goto out; 6014 } 6015 } 6016 } else { 6017 if (file_present) { 6018 rc = -EEXIST; 6019 ksmbd_debug(SMB, "link already exists\n"); 6020 goto out; 6021 } 6022 } 6023 6024 rc = ksmbd_vfs_link(work, target_name, link_name); 6025 if (rc) 6026 rc = -EINVAL; 6027 out: 6028 if (file_present) 6029 ksmbd_vfs_kern_path_unlock(&parent_path, &path); 6030 6031 if (!IS_ERR(link_name)) 6032 kfree(link_name); 6033 kfree(pathname); 6034 return rc; 6035 } 6036 6037 static int set_file_basic_info(struct ksmbd_file *fp, 6038 struct smb2_file_basic_info *file_info, 6039 struct ksmbd_share_config *share) 6040 { 6041 struct iattr attrs; 6042 struct file *filp; 6043 struct inode *inode; 6044 struct mnt_idmap *idmap; 6045 int rc = 0; 6046 6047 if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE)) 6048 return -EACCES; 6049 6050 attrs.ia_valid = 0; 6051 filp = fp->filp; 6052 inode = file_inode(filp); 6053 idmap = file_mnt_idmap(filp); 6054 6055 if (file_info->CreationTime) 6056 fp->create_time = le64_to_cpu(file_info->CreationTime); 6057 6058 if (file_info->LastAccessTime) { 6059 attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime); 6060 attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET); 6061 } 6062 6063 if (file_info->ChangeTime) 6064 inode_set_ctime_to_ts(inode, 6065 ksmbd_NTtimeToUnix(file_info->ChangeTime)); 6066 6067 if (file_info->LastWriteTime) { 6068 attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime); 6069 attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET | ATTR_CTIME); 6070 } 6071 6072 if (file_info->Attributes) { 6073 if (!S_ISDIR(inode->i_mode) && 6074 file_info->Attributes & FILE_ATTRIBUTE_DIRECTORY_LE) { 6075 pr_err("can't change a file to a directory\n"); 6076 return -EINVAL; 6077 } 6078 6079 if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == FILE_ATTRIBUTE_NORMAL_LE)) 6080 fp->f_ci->m_fattr = file_info->Attributes | 6081 (fp->f_ci->m_fattr & FILE_ATTRIBUTE_DIRECTORY_LE); 6082 } 6083 6084 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) && 6085 (file_info->CreationTime || file_info->Attributes)) { 6086 struct xattr_dos_attrib da = {0}; 6087 6088 da.version = 4; 6089 da.itime = fp->itime; 6090 da.create_time = fp->create_time; 6091 da.attr = le32_to_cpu(fp->f_ci->m_fattr); 6092 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | 6093 XATTR_DOSINFO_ITIME; 6094 6095 rc = ksmbd_vfs_set_dos_attrib_xattr(idmap, &filp->f_path, &da, 6096 true); 6097 if (rc) 6098 ksmbd_debug(SMB, 6099 "failed to restore file attribute in EA\n"); 6100 rc = 0; 6101 } 6102 6103 if (attrs.ia_valid) { 6104 struct dentry *dentry = filp->f_path.dentry; 6105 struct inode *inode = d_inode(dentry); 6106 6107 if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) 6108 return -EACCES; 6109 6110 inode_lock(inode); 6111 rc = notify_change(idmap, dentry, &attrs, NULL); 6112 inode_unlock(inode); 6113 } 6114 return rc; 6115 } 6116 6117 static int set_file_allocation_info(struct ksmbd_work *work, 6118 struct ksmbd_file *fp, 6119 struct smb2_file_alloc_info *file_alloc_info) 6120 { 6121 /* 6122 * TODO : It's working fine only when store dos attributes 6123 * is not yes. need to implement a logic which works 6124 * properly with any smb.conf option 6125 */ 6126 6127 loff_t alloc_blks; 6128 struct inode *inode; 6129 struct kstat stat; 6130 int rc; 6131 6132 if (!(fp->daccess & FILE_WRITE_DATA_LE)) 6133 return -EACCES; 6134 6135 rc = vfs_getattr(&fp->filp->f_path, &stat, STATX_BASIC_STATS, 6136 AT_STATX_SYNC_AS_STAT); 6137 if (rc) 6138 return rc; 6139 6140 alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9; 6141 inode = file_inode(fp->filp); 6142 6143 if (alloc_blks > stat.blocks) { 6144 smb_break_all_levII_oplock(work, fp, 1); 6145 rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, 6146 alloc_blks * 512); 6147 if (rc && rc != -EOPNOTSUPP) { 6148 pr_err("vfs_fallocate is failed : %d\n", rc); 6149 return rc; 6150 } 6151 } else if (alloc_blks < stat.blocks) { 6152 loff_t size; 6153 6154 /* 6155 * Allocation size could be smaller than original one 6156 * which means allocated blocks in file should be 6157 * deallocated. use truncate to cut out it, but inode 6158 * size is also updated with truncate offset. 6159 * inode size is retained by backup inode size. 6160 */ 6161 size = i_size_read(inode); 6162 rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512); 6163 if (rc) { 6164 pr_err("truncate failed!, err %d\n", rc); 6165 return rc; 6166 } 6167 if (size < alloc_blks * 512) 6168 i_size_write(inode, size); 6169 } 6170 return 0; 6171 } 6172 6173 static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp, 6174 struct smb2_file_eof_info *file_eof_info) 6175 { 6176 loff_t newsize; 6177 struct inode *inode; 6178 int rc; 6179 6180 if (!(fp->daccess & FILE_WRITE_DATA_LE)) 6181 return -EACCES; 6182 6183 newsize = le64_to_cpu(file_eof_info->EndOfFile); 6184 inode = file_inode(fp->filp); 6185 6186 /* 6187 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called 6188 * on FAT32 shared device, truncate execution time is too long 6189 * and network error could cause from windows client. because 6190 * truncate of some filesystem like FAT32 fill zero data in 6191 * truncated range. 6192 */ 6193 if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) { 6194 ksmbd_debug(SMB, "truncated to newsize %lld\n", newsize); 6195 rc = ksmbd_vfs_truncate(work, fp, newsize); 6196 if (rc) { 6197 ksmbd_debug(SMB, "truncate failed!, err %d\n", rc); 6198 if (rc != -EAGAIN) 6199 rc = -EBADF; 6200 return rc; 6201 } 6202 } 6203 return 0; 6204 } 6205 6206 static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp, 6207 struct smb2_file_rename_info *rename_info, 6208 unsigned int buf_len) 6209 { 6210 if (!(fp->daccess & FILE_DELETE_LE)) { 6211 pr_err("no right to delete : 0x%x\n", fp->daccess); 6212 return -EACCES; 6213 } 6214 6215 if (buf_len < (u64)sizeof(struct smb2_file_rename_info) + 6216 le32_to_cpu(rename_info->FileNameLength)) 6217 return -EINVAL; 6218 6219 if (!le32_to_cpu(rename_info->FileNameLength)) 6220 return -EINVAL; 6221 6222 return smb2_rename(work, fp, rename_info, work->conn->local_nls); 6223 } 6224 6225 static int set_file_disposition_info(struct ksmbd_file *fp, 6226 struct smb2_file_disposition_info *file_info) 6227 { 6228 struct inode *inode; 6229 6230 if (!(fp->daccess & FILE_DELETE_LE)) { 6231 pr_err("no right to delete : 0x%x\n", fp->daccess); 6232 return -EACCES; 6233 } 6234 6235 inode = file_inode(fp->filp); 6236 if (file_info->DeletePending) { 6237 if (S_ISDIR(inode->i_mode) && 6238 ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY) 6239 return -EBUSY; 6240 ksmbd_set_inode_pending_delete(fp); 6241 } else { 6242 ksmbd_clear_inode_pending_delete(fp); 6243 } 6244 return 0; 6245 } 6246 6247 static int set_file_position_info(struct ksmbd_file *fp, 6248 struct smb2_file_pos_info *file_info) 6249 { 6250 loff_t current_byte_offset; 6251 unsigned long sector_size; 6252 struct inode *inode; 6253 6254 inode = file_inode(fp->filp); 6255 current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset); 6256 sector_size = inode->i_sb->s_blocksize; 6257 6258 if (current_byte_offset < 0 || 6259 (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE && 6260 current_byte_offset & (sector_size - 1))) { 6261 pr_err("CurrentByteOffset is not valid : %llu\n", 6262 current_byte_offset); 6263 return -EINVAL; 6264 } 6265 6266 fp->filp->f_pos = current_byte_offset; 6267 return 0; 6268 } 6269 6270 static int set_file_mode_info(struct ksmbd_file *fp, 6271 struct smb2_file_mode_info *file_info) 6272 { 6273 __le32 mode; 6274 6275 mode = file_info->Mode; 6276 6277 if ((mode & ~FILE_MODE_INFO_MASK)) { 6278 pr_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode)); 6279 return -EINVAL; 6280 } 6281 6282 /* 6283 * TODO : need to implement consideration for 6284 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT 6285 */ 6286 ksmbd_vfs_set_fadvise(fp->filp, mode); 6287 fp->coption = mode; 6288 return 0; 6289 } 6290 6291 /** 6292 * smb2_set_info_file() - handler for smb2 set info command 6293 * @work: smb work containing set info command buffer 6294 * @fp: ksmbd_file pointer 6295 * @req: request buffer pointer 6296 * @share: ksmbd_share_config pointer 6297 * 6298 * Return: 0 on success, otherwise error 6299 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH 6300 */ 6301 static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp, 6302 struct smb2_set_info_req *req, 6303 struct ksmbd_share_config *share) 6304 { 6305 unsigned int buf_len = le32_to_cpu(req->BufferLength); 6306 char *buffer = (char *)req + le16_to_cpu(req->BufferOffset); 6307 6308 switch (req->FileInfoClass) { 6309 case FILE_BASIC_INFORMATION: 6310 { 6311 if (buf_len < sizeof(struct smb2_file_basic_info)) 6312 return -EINVAL; 6313 6314 return set_file_basic_info(fp, (struct smb2_file_basic_info *)buffer, share); 6315 } 6316 case FILE_ALLOCATION_INFORMATION: 6317 { 6318 if (buf_len < sizeof(struct smb2_file_alloc_info)) 6319 return -EINVAL; 6320 6321 return set_file_allocation_info(work, fp, 6322 (struct smb2_file_alloc_info *)buffer); 6323 } 6324 case FILE_END_OF_FILE_INFORMATION: 6325 { 6326 if (buf_len < sizeof(struct smb2_file_eof_info)) 6327 return -EINVAL; 6328 6329 return set_end_of_file_info(work, fp, 6330 (struct smb2_file_eof_info *)buffer); 6331 } 6332 case FILE_RENAME_INFORMATION: 6333 { 6334 if (buf_len < sizeof(struct smb2_file_rename_info)) 6335 return -EINVAL; 6336 6337 return set_rename_info(work, fp, 6338 (struct smb2_file_rename_info *)buffer, 6339 buf_len); 6340 } 6341 case FILE_LINK_INFORMATION: 6342 { 6343 if (buf_len < sizeof(struct smb2_file_link_info)) 6344 return -EINVAL; 6345 6346 return smb2_create_link(work, work->tcon->share_conf, 6347 (struct smb2_file_link_info *)buffer, 6348 buf_len, fp->filp, 6349 work->conn->local_nls); 6350 } 6351 case FILE_DISPOSITION_INFORMATION: 6352 { 6353 if (buf_len < sizeof(struct smb2_file_disposition_info)) 6354 return -EINVAL; 6355 6356 return set_file_disposition_info(fp, 6357 (struct smb2_file_disposition_info *)buffer); 6358 } 6359 case FILE_FULL_EA_INFORMATION: 6360 { 6361 if (!(fp->daccess & FILE_WRITE_EA_LE)) { 6362 pr_err("Not permitted to write ext attr: 0x%x\n", 6363 fp->daccess); 6364 return -EACCES; 6365 } 6366 6367 if (buf_len < sizeof(struct smb2_ea_info)) 6368 return -EINVAL; 6369 6370 return smb2_set_ea((struct smb2_ea_info *)buffer, 6371 buf_len, &fp->filp->f_path, true); 6372 } 6373 case FILE_POSITION_INFORMATION: 6374 { 6375 if (buf_len < sizeof(struct smb2_file_pos_info)) 6376 return -EINVAL; 6377 6378 return set_file_position_info(fp, (struct smb2_file_pos_info *)buffer); 6379 } 6380 case FILE_MODE_INFORMATION: 6381 { 6382 if (buf_len < sizeof(struct smb2_file_mode_info)) 6383 return -EINVAL; 6384 6385 return set_file_mode_info(fp, (struct smb2_file_mode_info *)buffer); 6386 } 6387 } 6388 6389 pr_err("Unimplemented Fileinfoclass :%d\n", req->FileInfoClass); 6390 return -EOPNOTSUPP; 6391 } 6392 6393 static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info, 6394 char *buffer, int buf_len) 6395 { 6396 struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer; 6397 6398 fp->saccess |= FILE_SHARE_DELETE_LE; 6399 6400 return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd, 6401 buf_len, false, true); 6402 } 6403 6404 /** 6405 * smb2_set_info() - handler for smb2 set info command handler 6406 * @work: smb work containing set info request buffer 6407 * 6408 * Return: 0 on success, otherwise error 6409 */ 6410 int smb2_set_info(struct ksmbd_work *work) 6411 { 6412 struct smb2_set_info_req *req; 6413 struct smb2_set_info_rsp *rsp; 6414 struct ksmbd_file *fp = NULL; 6415 int rc = 0; 6416 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; 6417 6418 ksmbd_debug(SMB, "Received set info request\n"); 6419 6420 if (work->next_smb2_rcv_hdr_off) { 6421 req = ksmbd_req_buf_next(work); 6422 rsp = ksmbd_resp_buf_next(work); 6423 if (!has_file_id(req->VolatileFileId)) { 6424 ksmbd_debug(SMB, "Compound request set FID = %llu\n", 6425 work->compound_fid); 6426 id = work->compound_fid; 6427 pid = work->compound_pfid; 6428 } 6429 } else { 6430 req = smb2_get_msg(work->request_buf); 6431 rsp = smb2_get_msg(work->response_buf); 6432 } 6433 6434 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { 6435 ksmbd_debug(SMB, "User does not have write permission\n"); 6436 pr_err("User does not have write permission\n"); 6437 rc = -EACCES; 6438 goto err_out; 6439 } 6440 6441 if (!has_file_id(id)) { 6442 id = req->VolatileFileId; 6443 pid = req->PersistentFileId; 6444 } 6445 6446 fp = ksmbd_lookup_fd_slow(work, id, pid); 6447 if (!fp) { 6448 ksmbd_debug(SMB, "Invalid id for close: %u\n", id); 6449 rc = -ENOENT; 6450 goto err_out; 6451 } 6452 6453 switch (req->InfoType) { 6454 case SMB2_O_INFO_FILE: 6455 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); 6456 rc = smb2_set_info_file(work, fp, req, work->tcon->share_conf); 6457 break; 6458 case SMB2_O_INFO_SECURITY: 6459 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); 6460 if (ksmbd_override_fsids(work)) { 6461 rc = -ENOMEM; 6462 goto err_out; 6463 } 6464 rc = smb2_set_info_sec(fp, 6465 le32_to_cpu(req->AdditionalInformation), 6466 (char *)req + le16_to_cpu(req->BufferOffset), 6467 le32_to_cpu(req->BufferLength)); 6468 ksmbd_revert_fsids(work); 6469 break; 6470 default: 6471 rc = -EOPNOTSUPP; 6472 } 6473 6474 if (rc < 0) 6475 goto err_out; 6476 6477 rsp->StructureSize = cpu_to_le16(2); 6478 rc = ksmbd_iov_pin_rsp(work, (void *)rsp, 6479 sizeof(struct smb2_set_info_rsp)); 6480 if (rc) 6481 goto err_out; 6482 ksmbd_fd_put(work, fp); 6483 return 0; 6484 6485 err_out: 6486 if (rc == -EACCES || rc == -EPERM || rc == -EXDEV) 6487 rsp->hdr.Status = STATUS_ACCESS_DENIED; 6488 else if (rc == -EINVAL) 6489 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 6490 else if (rc == -ESHARE) 6491 rsp->hdr.Status = STATUS_SHARING_VIOLATION; 6492 else if (rc == -ENOENT) 6493 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; 6494 else if (rc == -EBUSY || rc == -ENOTEMPTY) 6495 rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY; 6496 else if (rc == -EAGAIN) 6497 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; 6498 else if (rc == -EBADF || rc == -ESTALE) 6499 rsp->hdr.Status = STATUS_INVALID_HANDLE; 6500 else if (rc == -EEXIST) 6501 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; 6502 else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP) 6503 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; 6504 smb2_set_err_rsp(work); 6505 ksmbd_fd_put(work, fp); 6506 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc); 6507 return rc; 6508 } 6509 6510 /** 6511 * smb2_read_pipe() - handler for smb2 read from IPC pipe 6512 * @work: smb work containing read IPC pipe command buffer 6513 * 6514 * Return: 0 on success, otherwise error 6515 */ 6516 static noinline int smb2_read_pipe(struct ksmbd_work *work) 6517 { 6518 int nbytes = 0, err; 6519 u64 id; 6520 struct ksmbd_rpc_command *rpc_resp; 6521 struct smb2_read_req *req; 6522 struct smb2_read_rsp *rsp; 6523 6524 WORK_BUFFERS(work, req, rsp); 6525 6526 id = req->VolatileFileId; 6527 6528 rpc_resp = ksmbd_rpc_read(work->sess, id); 6529 if (rpc_resp) { 6530 void *aux_payload_buf; 6531 6532 if (rpc_resp->flags != KSMBD_RPC_OK) { 6533 err = -EINVAL; 6534 goto out; 6535 } 6536 6537 aux_payload_buf = 6538 kvmalloc(rpc_resp->payload_sz, GFP_KERNEL); 6539 if (!aux_payload_buf) { 6540 err = -ENOMEM; 6541 goto out; 6542 } 6543 6544 memcpy(aux_payload_buf, rpc_resp->payload, rpc_resp->payload_sz); 6545 6546 nbytes = rpc_resp->payload_sz; 6547 err = ksmbd_iov_pin_rsp_read(work, (void *)rsp, 6548 offsetof(struct smb2_read_rsp, Buffer), 6549 aux_payload_buf, nbytes); 6550 if (err) { 6551 kvfree(aux_payload_buf); 6552 goto out; 6553 } 6554 kvfree(rpc_resp); 6555 } else { 6556 err = ksmbd_iov_pin_rsp(work, (void *)rsp, 6557 offsetof(struct smb2_read_rsp, Buffer)); 6558 if (err) 6559 goto out; 6560 } 6561 6562 rsp->StructureSize = cpu_to_le16(17); 6563 rsp->DataOffset = 80; 6564 rsp->Reserved = 0; 6565 rsp->DataLength = cpu_to_le32(nbytes); 6566 rsp->DataRemaining = 0; 6567 rsp->Flags = 0; 6568 return 0; 6569 6570 out: 6571 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; 6572 smb2_set_err_rsp(work); 6573 kvfree(rpc_resp); 6574 return err; 6575 } 6576 6577 static int smb2_set_remote_key_for_rdma(struct ksmbd_work *work, 6578 struct smb2_buffer_desc_v1 *desc, 6579 __le32 Channel, 6580 __le16 ChannelInfoLength) 6581 { 6582 unsigned int i, ch_count; 6583 6584 if (work->conn->dialect == SMB30_PROT_ID && 6585 Channel != SMB2_CHANNEL_RDMA_V1) 6586 return -EINVAL; 6587 6588 ch_count = le16_to_cpu(ChannelInfoLength) / sizeof(*desc); 6589 if (ksmbd_debug_types & KSMBD_DEBUG_RDMA) { 6590 for (i = 0; i < ch_count; i++) { 6591 pr_info("RDMA r/w request %#x: token %#x, length %#x\n", 6592 i, 6593 le32_to_cpu(desc[i].token), 6594 le32_to_cpu(desc[i].length)); 6595 } 6596 } 6597 if (!ch_count) 6598 return -EINVAL; 6599 6600 work->need_invalidate_rkey = 6601 (Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE); 6602 if (Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE) 6603 work->remote_key = le32_to_cpu(desc->token); 6604 return 0; 6605 } 6606 6607 static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work, 6608 struct smb2_read_req *req, void *data_buf, 6609 size_t length) 6610 { 6611 int err; 6612 6613 err = ksmbd_conn_rdma_write(work->conn, data_buf, length, 6614 (struct smb2_buffer_desc_v1 *) 6615 ((char *)req + le16_to_cpu(req->ReadChannelInfoOffset)), 6616 le16_to_cpu(req->ReadChannelInfoLength)); 6617 if (err) 6618 return err; 6619 6620 return length; 6621 } 6622 6623 /** 6624 * smb2_read() - handler for smb2 read from file 6625 * @work: smb work containing read command buffer 6626 * 6627 * Return: 0 on success, otherwise error 6628 */ 6629 int smb2_read(struct ksmbd_work *work) 6630 { 6631 struct ksmbd_conn *conn = work->conn; 6632 struct smb2_read_req *req; 6633 struct smb2_read_rsp *rsp; 6634 struct ksmbd_file *fp = NULL; 6635 loff_t offset; 6636 size_t length, mincount; 6637 ssize_t nbytes = 0, remain_bytes = 0; 6638 int err = 0; 6639 bool is_rdma_channel = false; 6640 unsigned int max_read_size = conn->vals->max_read_size; 6641 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; 6642 void *aux_payload_buf; 6643 6644 if (test_share_config_flag(work->tcon->share_conf, 6645 KSMBD_SHARE_FLAG_PIPE)) { 6646 ksmbd_debug(SMB, "IPC pipe read request\n"); 6647 return smb2_read_pipe(work); 6648 } 6649 6650 if (work->next_smb2_rcv_hdr_off) { 6651 req = ksmbd_req_buf_next(work); 6652 rsp = ksmbd_resp_buf_next(work); 6653 if (!has_file_id(req->VolatileFileId)) { 6654 ksmbd_debug(SMB, "Compound request set FID = %llu\n", 6655 work->compound_fid); 6656 id = work->compound_fid; 6657 pid = work->compound_pfid; 6658 } 6659 } else { 6660 req = smb2_get_msg(work->request_buf); 6661 rsp = smb2_get_msg(work->response_buf); 6662 } 6663 6664 if (!has_file_id(id)) { 6665 id = req->VolatileFileId; 6666 pid = req->PersistentFileId; 6667 } 6668 6669 if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE || 6670 req->Channel == SMB2_CHANNEL_RDMA_V1) { 6671 is_rdma_channel = true; 6672 max_read_size = get_smbd_max_read_write_size(); 6673 } 6674 6675 if (is_rdma_channel == true) { 6676 unsigned int ch_offset = le16_to_cpu(req->ReadChannelInfoOffset); 6677 6678 if (ch_offset < offsetof(struct smb2_read_req, Buffer)) { 6679 err = -EINVAL; 6680 goto out; 6681 } 6682 err = smb2_set_remote_key_for_rdma(work, 6683 (struct smb2_buffer_desc_v1 *) 6684 ((char *)req + ch_offset), 6685 req->Channel, 6686 req->ReadChannelInfoLength); 6687 if (err) 6688 goto out; 6689 } 6690 6691 fp = ksmbd_lookup_fd_slow(work, id, pid); 6692 if (!fp) { 6693 err = -ENOENT; 6694 goto out; 6695 } 6696 6697 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { 6698 pr_err("Not permitted to read : 0x%x\n", fp->daccess); 6699 err = -EACCES; 6700 goto out; 6701 } 6702 6703 offset = le64_to_cpu(req->Offset); 6704 if (offset < 0) { 6705 err = -EINVAL; 6706 goto out; 6707 } 6708 length = le32_to_cpu(req->Length); 6709 mincount = le32_to_cpu(req->MinimumCount); 6710 6711 if (length > max_read_size) { 6712 ksmbd_debug(SMB, "limiting read size to max size(%u)\n", 6713 max_read_size); 6714 err = -EINVAL; 6715 goto out; 6716 } 6717 6718 ksmbd_debug(SMB, "filename %pD, offset %lld, len %zu\n", 6719 fp->filp, offset, length); 6720 6721 aux_payload_buf = kvzalloc(length, GFP_KERNEL); 6722 if (!aux_payload_buf) { 6723 err = -ENOMEM; 6724 goto out; 6725 } 6726 6727 nbytes = ksmbd_vfs_read(work, fp, length, &offset, aux_payload_buf); 6728 if (nbytes < 0) { 6729 err = nbytes; 6730 goto out; 6731 } 6732 6733 if ((nbytes == 0 && length != 0) || nbytes < mincount) { 6734 kvfree(aux_payload_buf); 6735 rsp->hdr.Status = STATUS_END_OF_FILE; 6736 smb2_set_err_rsp(work); 6737 ksmbd_fd_put(work, fp); 6738 return 0; 6739 } 6740 6741 ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n", 6742 nbytes, offset, mincount); 6743 6744 if (is_rdma_channel == true) { 6745 /* write data to the client using rdma channel */ 6746 remain_bytes = smb2_read_rdma_channel(work, req, 6747 aux_payload_buf, 6748 nbytes); 6749 kvfree(aux_payload_buf); 6750 aux_payload_buf = NULL; 6751 nbytes = 0; 6752 if (remain_bytes < 0) { 6753 err = (int)remain_bytes; 6754 goto out; 6755 } 6756 } 6757 6758 rsp->StructureSize = cpu_to_le16(17); 6759 rsp->DataOffset = 80; 6760 rsp->Reserved = 0; 6761 rsp->DataLength = cpu_to_le32(nbytes); 6762 rsp->DataRemaining = cpu_to_le32(remain_bytes); 6763 rsp->Flags = 0; 6764 err = ksmbd_iov_pin_rsp_read(work, (void *)rsp, 6765 offsetof(struct smb2_read_rsp, Buffer), 6766 aux_payload_buf, nbytes); 6767 if (err) { 6768 kvfree(aux_payload_buf); 6769 goto out; 6770 } 6771 ksmbd_fd_put(work, fp); 6772 return 0; 6773 6774 out: 6775 if (err) { 6776 if (err == -EISDIR) 6777 rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST; 6778 else if (err == -EAGAIN) 6779 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; 6780 else if (err == -ENOENT) 6781 rsp->hdr.Status = STATUS_FILE_CLOSED; 6782 else if (err == -EACCES) 6783 rsp->hdr.Status = STATUS_ACCESS_DENIED; 6784 else if (err == -ESHARE) 6785 rsp->hdr.Status = STATUS_SHARING_VIOLATION; 6786 else if (err == -EINVAL) 6787 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 6788 else 6789 rsp->hdr.Status = STATUS_INVALID_HANDLE; 6790 6791 smb2_set_err_rsp(work); 6792 } 6793 ksmbd_fd_put(work, fp); 6794 return err; 6795 } 6796 6797 /** 6798 * smb2_write_pipe() - handler for smb2 write on IPC pipe 6799 * @work: smb work containing write IPC pipe command buffer 6800 * 6801 * Return: 0 on success, otherwise error 6802 */ 6803 static noinline int smb2_write_pipe(struct ksmbd_work *work) 6804 { 6805 struct smb2_write_req *req; 6806 struct smb2_write_rsp *rsp; 6807 struct ksmbd_rpc_command *rpc_resp; 6808 u64 id = 0; 6809 int err = 0, ret = 0; 6810 char *data_buf; 6811 size_t length; 6812 6813 WORK_BUFFERS(work, req, rsp); 6814 6815 length = le32_to_cpu(req->Length); 6816 id = req->VolatileFileId; 6817 6818 if ((u64)le16_to_cpu(req->DataOffset) + length > 6819 get_rfc1002_len(work->request_buf)) { 6820 pr_err("invalid write data offset %u, smb_len %u\n", 6821 le16_to_cpu(req->DataOffset), 6822 get_rfc1002_len(work->request_buf)); 6823 err = -EINVAL; 6824 goto out; 6825 } 6826 6827 data_buf = (char *)(((char *)&req->hdr.ProtocolId) + 6828 le16_to_cpu(req->DataOffset)); 6829 6830 rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length); 6831 if (rpc_resp) { 6832 if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { 6833 rsp->hdr.Status = STATUS_NOT_SUPPORTED; 6834 kvfree(rpc_resp); 6835 smb2_set_err_rsp(work); 6836 return -EOPNOTSUPP; 6837 } 6838 if (rpc_resp->flags != KSMBD_RPC_OK) { 6839 rsp->hdr.Status = STATUS_INVALID_HANDLE; 6840 smb2_set_err_rsp(work); 6841 kvfree(rpc_resp); 6842 return ret; 6843 } 6844 kvfree(rpc_resp); 6845 } 6846 6847 rsp->StructureSize = cpu_to_le16(17); 6848 rsp->DataOffset = 0; 6849 rsp->Reserved = 0; 6850 rsp->DataLength = cpu_to_le32(length); 6851 rsp->DataRemaining = 0; 6852 rsp->Reserved2 = 0; 6853 err = ksmbd_iov_pin_rsp(work, (void *)rsp, 6854 offsetof(struct smb2_write_rsp, Buffer)); 6855 out: 6856 if (err) { 6857 rsp->hdr.Status = STATUS_INVALID_HANDLE; 6858 smb2_set_err_rsp(work); 6859 } 6860 6861 return err; 6862 } 6863 6864 static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work, 6865 struct smb2_write_req *req, 6866 struct ksmbd_file *fp, 6867 loff_t offset, size_t length, bool sync) 6868 { 6869 char *data_buf; 6870 int ret; 6871 ssize_t nbytes; 6872 6873 data_buf = kvzalloc(length, GFP_KERNEL); 6874 if (!data_buf) 6875 return -ENOMEM; 6876 6877 ret = ksmbd_conn_rdma_read(work->conn, data_buf, length, 6878 (struct smb2_buffer_desc_v1 *) 6879 ((char *)req + le16_to_cpu(req->WriteChannelInfoOffset)), 6880 le16_to_cpu(req->WriteChannelInfoLength)); 6881 if (ret < 0) { 6882 kvfree(data_buf); 6883 return ret; 6884 } 6885 6886 ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes); 6887 kvfree(data_buf); 6888 if (ret < 0) 6889 return ret; 6890 6891 return nbytes; 6892 } 6893 6894 /** 6895 * smb2_write() - handler for smb2 write from file 6896 * @work: smb work containing write command buffer 6897 * 6898 * Return: 0 on success, otherwise error 6899 */ 6900 int smb2_write(struct ksmbd_work *work) 6901 { 6902 struct smb2_write_req *req; 6903 struct smb2_write_rsp *rsp; 6904 struct ksmbd_file *fp = NULL; 6905 loff_t offset; 6906 size_t length; 6907 ssize_t nbytes; 6908 char *data_buf; 6909 bool writethrough = false, is_rdma_channel = false; 6910 int err = 0; 6911 unsigned int max_write_size = work->conn->vals->max_write_size; 6912 6913 WORK_BUFFERS(work, req, rsp); 6914 6915 if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) { 6916 ksmbd_debug(SMB, "IPC pipe write request\n"); 6917 return smb2_write_pipe(work); 6918 } 6919 6920 offset = le64_to_cpu(req->Offset); 6921 if (offset < 0) 6922 return -EINVAL; 6923 length = le32_to_cpu(req->Length); 6924 6925 if (req->Channel == SMB2_CHANNEL_RDMA_V1 || 6926 req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE) { 6927 is_rdma_channel = true; 6928 max_write_size = get_smbd_max_read_write_size(); 6929 length = le32_to_cpu(req->RemainingBytes); 6930 } 6931 6932 if (is_rdma_channel == true) { 6933 unsigned int ch_offset = le16_to_cpu(req->WriteChannelInfoOffset); 6934 6935 if (req->Length != 0 || req->DataOffset != 0 || 6936 ch_offset < offsetof(struct smb2_write_req, Buffer)) { 6937 err = -EINVAL; 6938 goto out; 6939 } 6940 err = smb2_set_remote_key_for_rdma(work, 6941 (struct smb2_buffer_desc_v1 *) 6942 ((char *)req + ch_offset), 6943 req->Channel, 6944 req->WriteChannelInfoLength); 6945 if (err) 6946 goto out; 6947 } 6948 6949 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { 6950 ksmbd_debug(SMB, "User does not have write permission\n"); 6951 err = -EACCES; 6952 goto out; 6953 } 6954 6955 fp = ksmbd_lookup_fd_slow(work, req->VolatileFileId, req->PersistentFileId); 6956 if (!fp) { 6957 err = -ENOENT; 6958 goto out; 6959 } 6960 6961 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { 6962 pr_err("Not permitted to write : 0x%x\n", fp->daccess); 6963 err = -EACCES; 6964 goto out; 6965 } 6966 6967 if (length > max_write_size) { 6968 ksmbd_debug(SMB, "limiting write size to max size(%u)\n", 6969 max_write_size); 6970 err = -EINVAL; 6971 goto out; 6972 } 6973 6974 ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags)); 6975 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH) 6976 writethrough = true; 6977 6978 if (is_rdma_channel == false) { 6979 if (le16_to_cpu(req->DataOffset) < 6980 offsetof(struct smb2_write_req, Buffer)) { 6981 err = -EINVAL; 6982 goto out; 6983 } 6984 6985 data_buf = (char *)(((char *)&req->hdr.ProtocolId) + 6986 le16_to_cpu(req->DataOffset)); 6987 6988 ksmbd_debug(SMB, "filename %pD, offset %lld, len %zu\n", 6989 fp->filp, offset, length); 6990 err = ksmbd_vfs_write(work, fp, data_buf, length, &offset, 6991 writethrough, &nbytes); 6992 if (err < 0) 6993 goto out; 6994 } else { 6995 /* read data from the client using rdma channel, and 6996 * write the data. 6997 */ 6998 nbytes = smb2_write_rdma_channel(work, req, fp, offset, length, 6999 writethrough); 7000 if (nbytes < 0) { 7001 err = (int)nbytes; 7002 goto out; 7003 } 7004 } 7005 7006 rsp->StructureSize = cpu_to_le16(17); 7007 rsp->DataOffset = 0; 7008 rsp->Reserved = 0; 7009 rsp->DataLength = cpu_to_le32(nbytes); 7010 rsp->DataRemaining = 0; 7011 rsp->Reserved2 = 0; 7012 err = ksmbd_iov_pin_rsp(work, rsp, offsetof(struct smb2_write_rsp, Buffer)); 7013 if (err) 7014 goto out; 7015 ksmbd_fd_put(work, fp); 7016 return 0; 7017 7018 out: 7019 if (err == -EAGAIN) 7020 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; 7021 else if (err == -ENOSPC || err == -EFBIG) 7022 rsp->hdr.Status = STATUS_DISK_FULL; 7023 else if (err == -ENOENT) 7024 rsp->hdr.Status = STATUS_FILE_CLOSED; 7025 else if (err == -EACCES) 7026 rsp->hdr.Status = STATUS_ACCESS_DENIED; 7027 else if (err == -ESHARE) 7028 rsp->hdr.Status = STATUS_SHARING_VIOLATION; 7029 else if (err == -EINVAL) 7030 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 7031 else 7032 rsp->hdr.Status = STATUS_INVALID_HANDLE; 7033 7034 smb2_set_err_rsp(work); 7035 ksmbd_fd_put(work, fp); 7036 return err; 7037 } 7038 7039 /** 7040 * smb2_flush() - handler for smb2 flush file - fsync 7041 * @work: smb work containing flush command buffer 7042 * 7043 * Return: 0 on success, otherwise error 7044 */ 7045 int smb2_flush(struct ksmbd_work *work) 7046 { 7047 struct smb2_flush_req *req; 7048 struct smb2_flush_rsp *rsp; 7049 int err; 7050 7051 WORK_BUFFERS(work, req, rsp); 7052 7053 ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n", req->VolatileFileId); 7054 7055 err = ksmbd_vfs_fsync(work, req->VolatileFileId, req->PersistentFileId); 7056 if (err) 7057 goto out; 7058 7059 rsp->StructureSize = cpu_to_le16(4); 7060 rsp->Reserved = 0; 7061 return ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_flush_rsp)); 7062 7063 out: 7064 rsp->hdr.Status = STATUS_INVALID_HANDLE; 7065 smb2_set_err_rsp(work); 7066 return err; 7067 } 7068 7069 /** 7070 * smb2_cancel() - handler for smb2 cancel command 7071 * @work: smb work containing cancel command buffer 7072 * 7073 * Return: 0 on success, otherwise error 7074 */ 7075 int smb2_cancel(struct ksmbd_work *work) 7076 { 7077 struct ksmbd_conn *conn = work->conn; 7078 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); 7079 struct smb2_hdr *chdr; 7080 struct ksmbd_work *iter; 7081 struct list_head *command_list; 7082 7083 if (work->next_smb2_rcv_hdr_off) 7084 hdr = ksmbd_resp_buf_next(work); 7085 7086 ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n", 7087 hdr->MessageId, hdr->Flags); 7088 7089 if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) { 7090 command_list = &conn->async_requests; 7091 7092 spin_lock(&conn->request_lock); 7093 list_for_each_entry(iter, command_list, 7094 async_request_entry) { 7095 chdr = smb2_get_msg(iter->request_buf); 7096 7097 if (iter->async_id != 7098 le64_to_cpu(hdr->Id.AsyncId)) 7099 continue; 7100 7101 ksmbd_debug(SMB, 7102 "smb2 with AsyncId %llu cancelled command = 0x%x\n", 7103 le64_to_cpu(hdr->Id.AsyncId), 7104 le16_to_cpu(chdr->Command)); 7105 iter->state = KSMBD_WORK_CANCELLED; 7106 if (iter->cancel_fn) 7107 iter->cancel_fn(iter->cancel_argv); 7108 break; 7109 } 7110 spin_unlock(&conn->request_lock); 7111 } else { 7112 command_list = &conn->requests; 7113 7114 spin_lock(&conn->request_lock); 7115 list_for_each_entry(iter, command_list, request_entry) { 7116 chdr = smb2_get_msg(iter->request_buf); 7117 7118 if (chdr->MessageId != hdr->MessageId || 7119 iter == work) 7120 continue; 7121 7122 ksmbd_debug(SMB, 7123 "smb2 with mid %llu cancelled command = 0x%x\n", 7124 le64_to_cpu(hdr->MessageId), 7125 le16_to_cpu(chdr->Command)); 7126 iter->state = KSMBD_WORK_CANCELLED; 7127 break; 7128 } 7129 spin_unlock(&conn->request_lock); 7130 } 7131 7132 /* For SMB2_CANCEL command itself send no response*/ 7133 work->send_no_response = 1; 7134 return 0; 7135 } 7136 7137 struct file_lock *smb_flock_init(struct file *f) 7138 { 7139 struct file_lock *fl; 7140 7141 fl = locks_alloc_lock(); 7142 if (!fl) 7143 goto out; 7144 7145 locks_init_lock(fl); 7146 7147 fl->fl_owner = f; 7148 fl->fl_pid = current->tgid; 7149 fl->fl_file = f; 7150 fl->fl_flags = FL_POSIX; 7151 fl->fl_ops = NULL; 7152 fl->fl_lmops = NULL; 7153 7154 out: 7155 return fl; 7156 } 7157 7158 static int smb2_set_flock_flags(struct file_lock *flock, int flags) 7159 { 7160 int cmd = -EINVAL; 7161 7162 /* Checking for wrong flag combination during lock request*/ 7163 switch (flags) { 7164 case SMB2_LOCKFLAG_SHARED: 7165 ksmbd_debug(SMB, "received shared request\n"); 7166 cmd = F_SETLKW; 7167 flock->fl_type = F_RDLCK; 7168 flock->fl_flags |= FL_SLEEP; 7169 break; 7170 case SMB2_LOCKFLAG_EXCLUSIVE: 7171 ksmbd_debug(SMB, "received exclusive request\n"); 7172 cmd = F_SETLKW; 7173 flock->fl_type = F_WRLCK; 7174 flock->fl_flags |= FL_SLEEP; 7175 break; 7176 case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: 7177 ksmbd_debug(SMB, 7178 "received shared & fail immediately request\n"); 7179 cmd = F_SETLK; 7180 flock->fl_type = F_RDLCK; 7181 break; 7182 case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: 7183 ksmbd_debug(SMB, 7184 "received exclusive & fail immediately request\n"); 7185 cmd = F_SETLK; 7186 flock->fl_type = F_WRLCK; 7187 break; 7188 case SMB2_LOCKFLAG_UNLOCK: 7189 ksmbd_debug(SMB, "received unlock request\n"); 7190 flock->fl_type = F_UNLCK; 7191 cmd = F_SETLK; 7192 break; 7193 } 7194 7195 return cmd; 7196 } 7197 7198 static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock, 7199 unsigned int cmd, int flags, 7200 struct list_head *lock_list) 7201 { 7202 struct ksmbd_lock *lock; 7203 7204 lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL); 7205 if (!lock) 7206 return NULL; 7207 7208 lock->cmd = cmd; 7209 lock->fl = flock; 7210 lock->start = flock->fl_start; 7211 lock->end = flock->fl_end; 7212 lock->flags = flags; 7213 if (lock->start == lock->end) 7214 lock->zero_len = 1; 7215 INIT_LIST_HEAD(&lock->clist); 7216 INIT_LIST_HEAD(&lock->flist); 7217 INIT_LIST_HEAD(&lock->llist); 7218 list_add_tail(&lock->llist, lock_list); 7219 7220 return lock; 7221 } 7222 7223 static void smb2_remove_blocked_lock(void **argv) 7224 { 7225 struct file_lock *flock = (struct file_lock *)argv[0]; 7226 7227 ksmbd_vfs_posix_lock_unblock(flock); 7228 wake_up(&flock->fl_wait); 7229 } 7230 7231 static inline bool lock_defer_pending(struct file_lock *fl) 7232 { 7233 /* check pending lock waiters */ 7234 return waitqueue_active(&fl->fl_wait); 7235 } 7236 7237 /** 7238 * smb2_lock() - handler for smb2 file lock command 7239 * @work: smb work containing lock command buffer 7240 * 7241 * Return: 0 on success, otherwise error 7242 */ 7243 int smb2_lock(struct ksmbd_work *work) 7244 { 7245 struct smb2_lock_req *req; 7246 struct smb2_lock_rsp *rsp; 7247 struct smb2_lock_element *lock_ele; 7248 struct ksmbd_file *fp = NULL; 7249 struct file_lock *flock = NULL; 7250 struct file *filp = NULL; 7251 int lock_count; 7252 int flags = 0; 7253 int cmd = 0; 7254 int err = -EIO, i, rc = 0; 7255 u64 lock_start, lock_length; 7256 struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2; 7257 struct ksmbd_conn *conn; 7258 int nolock = 0; 7259 LIST_HEAD(lock_list); 7260 LIST_HEAD(rollback_list); 7261 int prior_lock = 0; 7262 7263 WORK_BUFFERS(work, req, rsp); 7264 7265 ksmbd_debug(SMB, "Received lock request\n"); 7266 fp = ksmbd_lookup_fd_slow(work, req->VolatileFileId, req->PersistentFileId); 7267 if (!fp) { 7268 ksmbd_debug(SMB, "Invalid file id for lock : %llu\n", req->VolatileFileId); 7269 err = -ENOENT; 7270 goto out2; 7271 } 7272 7273 filp = fp->filp; 7274 lock_count = le16_to_cpu(req->LockCount); 7275 lock_ele = req->locks; 7276 7277 ksmbd_debug(SMB, "lock count is %d\n", lock_count); 7278 if (!lock_count) { 7279 err = -EINVAL; 7280 goto out2; 7281 } 7282 7283 for (i = 0; i < lock_count; i++) { 7284 flags = le32_to_cpu(lock_ele[i].Flags); 7285 7286 flock = smb_flock_init(filp); 7287 if (!flock) 7288 goto out; 7289 7290 cmd = smb2_set_flock_flags(flock, flags); 7291 7292 lock_start = le64_to_cpu(lock_ele[i].Offset); 7293 lock_length = le64_to_cpu(lock_ele[i].Length); 7294 if (lock_start > U64_MAX - lock_length) { 7295 pr_err("Invalid lock range requested\n"); 7296 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; 7297 locks_free_lock(flock); 7298 goto out; 7299 } 7300 7301 if (lock_start > OFFSET_MAX) 7302 flock->fl_start = OFFSET_MAX; 7303 else 7304 flock->fl_start = lock_start; 7305 7306 lock_length = le64_to_cpu(lock_ele[i].Length); 7307 if (lock_length > OFFSET_MAX - flock->fl_start) 7308 lock_length = OFFSET_MAX - flock->fl_start; 7309 7310 flock->fl_end = flock->fl_start + lock_length; 7311 7312 if (flock->fl_end < flock->fl_start) { 7313 ksmbd_debug(SMB, 7314 "the end offset(%llx) is smaller than the start offset(%llx)\n", 7315 flock->fl_end, flock->fl_start); 7316 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; 7317 locks_free_lock(flock); 7318 goto out; 7319 } 7320 7321 /* Check conflict locks in one request */ 7322 list_for_each_entry(cmp_lock, &lock_list, llist) { 7323 if (cmp_lock->fl->fl_start <= flock->fl_start && 7324 cmp_lock->fl->fl_end >= flock->fl_end) { 7325 if (cmp_lock->fl->fl_type != F_UNLCK && 7326 flock->fl_type != F_UNLCK) { 7327 pr_err("conflict two locks in one request\n"); 7328 err = -EINVAL; 7329 locks_free_lock(flock); 7330 goto out; 7331 } 7332 } 7333 } 7334 7335 smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list); 7336 if (!smb_lock) { 7337 err = -EINVAL; 7338 locks_free_lock(flock); 7339 goto out; 7340 } 7341 } 7342 7343 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { 7344 if (smb_lock->cmd < 0) { 7345 err = -EINVAL; 7346 goto out; 7347 } 7348 7349 if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) { 7350 err = -EINVAL; 7351 goto out; 7352 } 7353 7354 if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) && 7355 smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) || 7356 (prior_lock == SMB2_LOCKFLAG_UNLOCK && 7357 !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) { 7358 err = -EINVAL; 7359 goto out; 7360 } 7361 7362 prior_lock = smb_lock->flags; 7363 7364 if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) && 7365 !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY)) 7366 goto no_check_cl; 7367 7368 nolock = 1; 7369 /* check locks in connection list */ 7370 down_read(&conn_list_lock); 7371 list_for_each_entry(conn, &conn_list, conns_list) { 7372 spin_lock(&conn->llist_lock); 7373 list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) { 7374 if (file_inode(cmp_lock->fl->fl_file) != 7375 file_inode(smb_lock->fl->fl_file)) 7376 continue; 7377 7378 if (smb_lock->fl->fl_type == F_UNLCK) { 7379 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file && 7380 cmp_lock->start == smb_lock->start && 7381 cmp_lock->end == smb_lock->end && 7382 !lock_defer_pending(cmp_lock->fl)) { 7383 nolock = 0; 7384 list_del(&cmp_lock->flist); 7385 list_del(&cmp_lock->clist); 7386 spin_unlock(&conn->llist_lock); 7387 up_read(&conn_list_lock); 7388 7389 locks_free_lock(cmp_lock->fl); 7390 kfree(cmp_lock); 7391 goto out_check_cl; 7392 } 7393 continue; 7394 } 7395 7396 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) { 7397 if (smb_lock->flags & SMB2_LOCKFLAG_SHARED) 7398 continue; 7399 } else { 7400 if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED) 7401 continue; 7402 } 7403 7404 /* check zero byte lock range */ 7405 if (cmp_lock->zero_len && !smb_lock->zero_len && 7406 cmp_lock->start > smb_lock->start && 7407 cmp_lock->start < smb_lock->end) { 7408 spin_unlock(&conn->llist_lock); 7409 up_read(&conn_list_lock); 7410 pr_err("previous lock conflict with zero byte lock range\n"); 7411 goto out; 7412 } 7413 7414 if (smb_lock->zero_len && !cmp_lock->zero_len && 7415 smb_lock->start > cmp_lock->start && 7416 smb_lock->start < cmp_lock->end) { 7417 spin_unlock(&conn->llist_lock); 7418 up_read(&conn_list_lock); 7419 pr_err("current lock conflict with zero byte lock range\n"); 7420 goto out; 7421 } 7422 7423 if (((cmp_lock->start <= smb_lock->start && 7424 cmp_lock->end > smb_lock->start) || 7425 (cmp_lock->start < smb_lock->end && 7426 cmp_lock->end >= smb_lock->end)) && 7427 !cmp_lock->zero_len && !smb_lock->zero_len) { 7428 spin_unlock(&conn->llist_lock); 7429 up_read(&conn_list_lock); 7430 pr_err("Not allow lock operation on exclusive lock range\n"); 7431 goto out; 7432 } 7433 } 7434 spin_unlock(&conn->llist_lock); 7435 } 7436 up_read(&conn_list_lock); 7437 out_check_cl: 7438 if (smb_lock->fl->fl_type == F_UNLCK && nolock) { 7439 pr_err("Try to unlock nolocked range\n"); 7440 rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED; 7441 goto out; 7442 } 7443 7444 no_check_cl: 7445 if (smb_lock->zero_len) { 7446 err = 0; 7447 goto skip; 7448 } 7449 7450 flock = smb_lock->fl; 7451 list_del(&smb_lock->llist); 7452 retry: 7453 rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL); 7454 skip: 7455 if (flags & SMB2_LOCKFLAG_UNLOCK) { 7456 if (!rc) { 7457 ksmbd_debug(SMB, "File unlocked\n"); 7458 } else if (rc == -ENOENT) { 7459 rsp->hdr.Status = STATUS_NOT_LOCKED; 7460 goto out; 7461 } 7462 locks_free_lock(flock); 7463 kfree(smb_lock); 7464 } else { 7465 if (rc == FILE_LOCK_DEFERRED) { 7466 void **argv; 7467 7468 ksmbd_debug(SMB, 7469 "would have to wait for getting lock\n"); 7470 list_add(&smb_lock->llist, &rollback_list); 7471 7472 argv = kmalloc(sizeof(void *), GFP_KERNEL); 7473 if (!argv) { 7474 err = -ENOMEM; 7475 goto out; 7476 } 7477 argv[0] = flock; 7478 7479 rc = setup_async_work(work, 7480 smb2_remove_blocked_lock, 7481 argv); 7482 if (rc) { 7483 kfree(argv); 7484 err = -ENOMEM; 7485 goto out; 7486 } 7487 spin_lock(&fp->f_lock); 7488 list_add(&work->fp_entry, &fp->blocked_works); 7489 spin_unlock(&fp->f_lock); 7490 7491 smb2_send_interim_resp(work, STATUS_PENDING); 7492 7493 ksmbd_vfs_posix_lock_wait(flock); 7494 7495 spin_lock(&fp->f_lock); 7496 list_del(&work->fp_entry); 7497 spin_unlock(&fp->f_lock); 7498 7499 if (work->state != KSMBD_WORK_ACTIVE) { 7500 list_del(&smb_lock->llist); 7501 locks_free_lock(flock); 7502 7503 if (work->state == KSMBD_WORK_CANCELLED) { 7504 rsp->hdr.Status = 7505 STATUS_CANCELLED; 7506 kfree(smb_lock); 7507 smb2_send_interim_resp(work, 7508 STATUS_CANCELLED); 7509 work->send_no_response = 1; 7510 goto out; 7511 } 7512 7513 rsp->hdr.Status = 7514 STATUS_RANGE_NOT_LOCKED; 7515 kfree(smb_lock); 7516 goto out2; 7517 } 7518 7519 list_del(&smb_lock->llist); 7520 release_async_work(work); 7521 goto retry; 7522 } else if (!rc) { 7523 list_add(&smb_lock->llist, &rollback_list); 7524 spin_lock(&work->conn->llist_lock); 7525 list_add_tail(&smb_lock->clist, 7526 &work->conn->lock_list); 7527 list_add_tail(&smb_lock->flist, 7528 &fp->lock_list); 7529 spin_unlock(&work->conn->llist_lock); 7530 ksmbd_debug(SMB, "successful in taking lock\n"); 7531 } else { 7532 goto out; 7533 } 7534 } 7535 } 7536 7537 if (atomic_read(&fp->f_ci->op_count) > 1) 7538 smb_break_all_oplock(work, fp); 7539 7540 rsp->StructureSize = cpu_to_le16(4); 7541 ksmbd_debug(SMB, "successful in taking lock\n"); 7542 rsp->hdr.Status = STATUS_SUCCESS; 7543 rsp->Reserved = 0; 7544 err = ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_lock_rsp)); 7545 if (err) 7546 goto out; 7547 7548 ksmbd_fd_put(work, fp); 7549 return 0; 7550 7551 out: 7552 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { 7553 locks_free_lock(smb_lock->fl); 7554 list_del(&smb_lock->llist); 7555 kfree(smb_lock); 7556 } 7557 7558 list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) { 7559 struct file_lock *rlock = NULL; 7560 7561 rlock = smb_flock_init(filp); 7562 rlock->fl_type = F_UNLCK; 7563 rlock->fl_start = smb_lock->start; 7564 rlock->fl_end = smb_lock->end; 7565 7566 rc = vfs_lock_file(filp, F_SETLK, rlock, NULL); 7567 if (rc) 7568 pr_err("rollback unlock fail : %d\n", rc); 7569 7570 list_del(&smb_lock->llist); 7571 spin_lock(&work->conn->llist_lock); 7572 if (!list_empty(&smb_lock->flist)) 7573 list_del(&smb_lock->flist); 7574 list_del(&smb_lock->clist); 7575 spin_unlock(&work->conn->llist_lock); 7576 7577 locks_free_lock(smb_lock->fl); 7578 locks_free_lock(rlock); 7579 kfree(smb_lock); 7580 } 7581 out2: 7582 ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err); 7583 7584 if (!rsp->hdr.Status) { 7585 if (err == -EINVAL) 7586 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 7587 else if (err == -ENOMEM) 7588 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; 7589 else if (err == -ENOENT) 7590 rsp->hdr.Status = STATUS_FILE_CLOSED; 7591 else 7592 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED; 7593 } 7594 7595 smb2_set_err_rsp(work); 7596 ksmbd_fd_put(work, fp); 7597 return err; 7598 } 7599 7600 static int fsctl_copychunk(struct ksmbd_work *work, 7601 struct copychunk_ioctl_req *ci_req, 7602 unsigned int cnt_code, 7603 unsigned int input_count, 7604 unsigned long long volatile_id, 7605 unsigned long long persistent_id, 7606 struct smb2_ioctl_rsp *rsp) 7607 { 7608 struct copychunk_ioctl_rsp *ci_rsp; 7609 struct ksmbd_file *src_fp = NULL, *dst_fp = NULL; 7610 struct srv_copychunk *chunks; 7611 unsigned int i, chunk_count, chunk_count_written = 0; 7612 unsigned int chunk_size_written = 0; 7613 loff_t total_size_written = 0; 7614 int ret = 0; 7615 7616 ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0]; 7617 7618 rsp->VolatileFileId = volatile_id; 7619 rsp->PersistentFileId = persistent_id; 7620 ci_rsp->ChunksWritten = 7621 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count()); 7622 ci_rsp->ChunkBytesWritten = 7623 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size()); 7624 ci_rsp->TotalBytesWritten = 7625 cpu_to_le32(ksmbd_server_side_copy_max_total_size()); 7626 7627 chunks = (struct srv_copychunk *)&ci_req->Chunks[0]; 7628 chunk_count = le32_to_cpu(ci_req->ChunkCount); 7629 if (chunk_count == 0) 7630 goto out; 7631 total_size_written = 0; 7632 7633 /* verify the SRV_COPYCHUNK_COPY packet */ 7634 if (chunk_count > ksmbd_server_side_copy_max_chunk_count() || 7635 input_count < offsetof(struct copychunk_ioctl_req, Chunks) + 7636 chunk_count * sizeof(struct srv_copychunk)) { 7637 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 7638 return -EINVAL; 7639 } 7640 7641 for (i = 0; i < chunk_count; i++) { 7642 if (le32_to_cpu(chunks[i].Length) == 0 || 7643 le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size()) 7644 break; 7645 total_size_written += le32_to_cpu(chunks[i].Length); 7646 } 7647 7648 if (i < chunk_count || 7649 total_size_written > ksmbd_server_side_copy_max_total_size()) { 7650 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 7651 return -EINVAL; 7652 } 7653 7654 src_fp = ksmbd_lookup_foreign_fd(work, 7655 le64_to_cpu(ci_req->ResumeKey[0])); 7656 dst_fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); 7657 ret = -EINVAL; 7658 if (!src_fp || 7659 src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) { 7660 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; 7661 goto out; 7662 } 7663 7664 if (!dst_fp) { 7665 rsp->hdr.Status = STATUS_FILE_CLOSED; 7666 goto out; 7667 } 7668 7669 /* 7670 * FILE_READ_DATA should only be included in 7671 * the FSCTL_COPYCHUNK case 7672 */ 7673 if (cnt_code == FSCTL_COPYCHUNK && 7674 !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) { 7675 rsp->hdr.Status = STATUS_ACCESS_DENIED; 7676 goto out; 7677 } 7678 7679 ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp, 7680 chunks, chunk_count, 7681 &chunk_count_written, 7682 &chunk_size_written, 7683 &total_size_written); 7684 if (ret < 0) { 7685 if (ret == -EACCES) 7686 rsp->hdr.Status = STATUS_ACCESS_DENIED; 7687 if (ret == -EAGAIN) 7688 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; 7689 else if (ret == -EBADF) 7690 rsp->hdr.Status = STATUS_INVALID_HANDLE; 7691 else if (ret == -EFBIG || ret == -ENOSPC) 7692 rsp->hdr.Status = STATUS_DISK_FULL; 7693 else if (ret == -EINVAL) 7694 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 7695 else if (ret == -EISDIR) 7696 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; 7697 else if (ret == -E2BIG) 7698 rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE; 7699 else 7700 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; 7701 } 7702 7703 ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written); 7704 ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written); 7705 ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written); 7706 out: 7707 ksmbd_fd_put(work, src_fp); 7708 ksmbd_fd_put(work, dst_fp); 7709 return ret; 7710 } 7711 7712 static __be32 idev_ipv4_address(struct in_device *idev) 7713 { 7714 __be32 addr = 0; 7715 7716 struct in_ifaddr *ifa; 7717 7718 rcu_read_lock(); 7719 in_dev_for_each_ifa_rcu(ifa, idev) { 7720 if (ifa->ifa_flags & IFA_F_SECONDARY) 7721 continue; 7722 7723 addr = ifa->ifa_address; 7724 break; 7725 } 7726 rcu_read_unlock(); 7727 return addr; 7728 } 7729 7730 static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn, 7731 struct smb2_ioctl_rsp *rsp, 7732 unsigned int out_buf_len) 7733 { 7734 struct network_interface_info_ioctl_rsp *nii_rsp = NULL; 7735 int nbytes = 0; 7736 struct net_device *netdev; 7737 struct sockaddr_storage_rsp *sockaddr_storage; 7738 unsigned int flags; 7739 unsigned long long speed; 7740 7741 rtnl_lock(); 7742 for_each_netdev(&init_net, netdev) { 7743 bool ipv4_set = false; 7744 7745 if (netdev->type == ARPHRD_LOOPBACK) 7746 continue; 7747 7748 flags = dev_get_flags(netdev); 7749 if (!(flags & IFF_RUNNING)) 7750 continue; 7751 ipv6_retry: 7752 if (out_buf_len < 7753 nbytes + sizeof(struct network_interface_info_ioctl_rsp)) { 7754 rtnl_unlock(); 7755 return -ENOSPC; 7756 } 7757 7758 nii_rsp = (struct network_interface_info_ioctl_rsp *) 7759 &rsp->Buffer[nbytes]; 7760 nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex); 7761 7762 nii_rsp->Capability = 0; 7763 if (netdev->real_num_tx_queues > 1) 7764 nii_rsp->Capability |= cpu_to_le32(RSS_CAPABLE); 7765 if (ksmbd_rdma_capable_netdev(netdev)) 7766 nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE); 7767 7768 nii_rsp->Next = cpu_to_le32(152); 7769 nii_rsp->Reserved = 0; 7770 7771 if (netdev->ethtool_ops->get_link_ksettings) { 7772 struct ethtool_link_ksettings cmd; 7773 7774 netdev->ethtool_ops->get_link_ksettings(netdev, &cmd); 7775 speed = cmd.base.speed; 7776 } else { 7777 ksmbd_debug(SMB, "%s %s\n", netdev->name, 7778 "speed is unknown, defaulting to 1Gb/sec"); 7779 speed = SPEED_1000; 7780 } 7781 7782 speed *= 1000000; 7783 nii_rsp->LinkSpeed = cpu_to_le64(speed); 7784 7785 sockaddr_storage = (struct sockaddr_storage_rsp *) 7786 nii_rsp->SockAddr_Storage; 7787 memset(sockaddr_storage, 0, 128); 7788 7789 if (!ipv4_set) { 7790 struct in_device *idev; 7791 7792 sockaddr_storage->Family = cpu_to_le16(INTERNETWORK); 7793 sockaddr_storage->addr4.Port = 0; 7794 7795 idev = __in_dev_get_rtnl(netdev); 7796 if (!idev) 7797 continue; 7798 sockaddr_storage->addr4.IPv4address = 7799 idev_ipv4_address(idev); 7800 nbytes += sizeof(struct network_interface_info_ioctl_rsp); 7801 ipv4_set = true; 7802 goto ipv6_retry; 7803 } else { 7804 struct inet6_dev *idev6; 7805 struct inet6_ifaddr *ifa; 7806 __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address; 7807 7808 sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6); 7809 sockaddr_storage->addr6.Port = 0; 7810 sockaddr_storage->addr6.FlowInfo = 0; 7811 7812 idev6 = __in6_dev_get(netdev); 7813 if (!idev6) 7814 continue; 7815 7816 list_for_each_entry(ifa, &idev6->addr_list, if_list) { 7817 if (ifa->flags & (IFA_F_TENTATIVE | 7818 IFA_F_DEPRECATED)) 7819 continue; 7820 memcpy(ipv6_addr, ifa->addr.s6_addr, 16); 7821 break; 7822 } 7823 sockaddr_storage->addr6.ScopeId = 0; 7824 nbytes += sizeof(struct network_interface_info_ioctl_rsp); 7825 } 7826 } 7827 rtnl_unlock(); 7828 7829 /* zero if this is last one */ 7830 if (nii_rsp) 7831 nii_rsp->Next = 0; 7832 7833 rsp->PersistentFileId = SMB2_NO_FID; 7834 rsp->VolatileFileId = SMB2_NO_FID; 7835 return nbytes; 7836 } 7837 7838 static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn, 7839 struct validate_negotiate_info_req *neg_req, 7840 struct validate_negotiate_info_rsp *neg_rsp, 7841 unsigned int in_buf_len) 7842 { 7843 int ret = 0; 7844 int dialect; 7845 7846 if (in_buf_len < offsetof(struct validate_negotiate_info_req, Dialects) + 7847 le16_to_cpu(neg_req->DialectCount) * sizeof(__le16)) 7848 return -EINVAL; 7849 7850 dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects, 7851 neg_req->DialectCount); 7852 if (dialect == BAD_PROT_ID || dialect != conn->dialect) { 7853 ret = -EINVAL; 7854 goto err_out; 7855 } 7856 7857 if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) { 7858 ret = -EINVAL; 7859 goto err_out; 7860 } 7861 7862 if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) { 7863 ret = -EINVAL; 7864 goto err_out; 7865 } 7866 7867 if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) { 7868 ret = -EINVAL; 7869 goto err_out; 7870 } 7871 7872 neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); 7873 memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE); 7874 neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode); 7875 neg_rsp->Dialect = cpu_to_le16(conn->dialect); 7876 err_out: 7877 return ret; 7878 } 7879 7880 static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id, 7881 struct file_allocated_range_buffer *qar_req, 7882 struct file_allocated_range_buffer *qar_rsp, 7883 unsigned int in_count, unsigned int *out_count) 7884 { 7885 struct ksmbd_file *fp; 7886 loff_t start, length; 7887 int ret = 0; 7888 7889 *out_count = 0; 7890 if (in_count == 0) 7891 return -EINVAL; 7892 7893 start = le64_to_cpu(qar_req->file_offset); 7894 length = le64_to_cpu(qar_req->length); 7895 7896 if (start < 0 || length < 0) 7897 return -EINVAL; 7898 7899 fp = ksmbd_lookup_fd_fast(work, id); 7900 if (!fp) 7901 return -ENOENT; 7902 7903 ret = ksmbd_vfs_fqar_lseek(fp, start, length, 7904 qar_rsp, in_count, out_count); 7905 if (ret && ret != -E2BIG) 7906 *out_count = 0; 7907 7908 ksmbd_fd_put(work, fp); 7909 return ret; 7910 } 7911 7912 static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id, 7913 unsigned int out_buf_len, 7914 struct smb2_ioctl_req *req, 7915 struct smb2_ioctl_rsp *rsp) 7916 { 7917 struct ksmbd_rpc_command *rpc_resp; 7918 char *data_buf = (char *)req + le32_to_cpu(req->InputOffset); 7919 int nbytes = 0; 7920 7921 rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf, 7922 le32_to_cpu(req->InputCount)); 7923 if (rpc_resp) { 7924 if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) { 7925 /* 7926 * set STATUS_SOME_NOT_MAPPED response 7927 * for unknown domain sid. 7928 */ 7929 rsp->hdr.Status = STATUS_SOME_NOT_MAPPED; 7930 } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { 7931 rsp->hdr.Status = STATUS_NOT_SUPPORTED; 7932 goto out; 7933 } else if (rpc_resp->flags != KSMBD_RPC_OK) { 7934 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 7935 goto out; 7936 } 7937 7938 nbytes = rpc_resp->payload_sz; 7939 if (rpc_resp->payload_sz > out_buf_len) { 7940 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; 7941 nbytes = out_buf_len; 7942 } 7943 7944 if (!rpc_resp->payload_sz) { 7945 rsp->hdr.Status = 7946 STATUS_UNEXPECTED_IO_ERROR; 7947 goto out; 7948 } 7949 7950 memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes); 7951 } 7952 out: 7953 kvfree(rpc_resp); 7954 return nbytes; 7955 } 7956 7957 static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id, 7958 struct file_sparse *sparse) 7959 { 7960 struct ksmbd_file *fp; 7961 struct mnt_idmap *idmap; 7962 int ret = 0; 7963 __le32 old_fattr; 7964 7965 fp = ksmbd_lookup_fd_fast(work, id); 7966 if (!fp) 7967 return -ENOENT; 7968 idmap = file_mnt_idmap(fp->filp); 7969 7970 old_fattr = fp->f_ci->m_fattr; 7971 if (sparse->SetSparse) 7972 fp->f_ci->m_fattr |= FILE_ATTRIBUTE_SPARSE_FILE_LE; 7973 else 7974 fp->f_ci->m_fattr &= ~FILE_ATTRIBUTE_SPARSE_FILE_LE; 7975 7976 if (fp->f_ci->m_fattr != old_fattr && 7977 test_share_config_flag(work->tcon->share_conf, 7978 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) { 7979 struct xattr_dos_attrib da; 7980 7981 ret = ksmbd_vfs_get_dos_attrib_xattr(idmap, 7982 fp->filp->f_path.dentry, &da); 7983 if (ret <= 0) 7984 goto out; 7985 7986 da.attr = le32_to_cpu(fp->f_ci->m_fattr); 7987 ret = ksmbd_vfs_set_dos_attrib_xattr(idmap, 7988 &fp->filp->f_path, 7989 &da, true); 7990 if (ret) 7991 fp->f_ci->m_fattr = old_fattr; 7992 } 7993 7994 out: 7995 ksmbd_fd_put(work, fp); 7996 return ret; 7997 } 7998 7999 static int fsctl_request_resume_key(struct ksmbd_work *work, 8000 struct smb2_ioctl_req *req, 8001 struct resume_key_ioctl_rsp *key_rsp) 8002 { 8003 struct ksmbd_file *fp; 8004 8005 fp = ksmbd_lookup_fd_slow(work, req->VolatileFileId, req->PersistentFileId); 8006 if (!fp) 8007 return -ENOENT; 8008 8009 memset(key_rsp, 0, sizeof(*key_rsp)); 8010 key_rsp->ResumeKey[0] = req->VolatileFileId; 8011 key_rsp->ResumeKey[1] = req->PersistentFileId; 8012 ksmbd_fd_put(work, fp); 8013 8014 return 0; 8015 } 8016 8017 /** 8018 * smb2_ioctl() - handler for smb2 ioctl command 8019 * @work: smb work containing ioctl command buffer 8020 * 8021 * Return: 0 on success, otherwise error 8022 */ 8023 int smb2_ioctl(struct ksmbd_work *work) 8024 { 8025 struct smb2_ioctl_req *req; 8026 struct smb2_ioctl_rsp *rsp; 8027 unsigned int cnt_code, nbytes = 0, out_buf_len, in_buf_len; 8028 u64 id = KSMBD_NO_FID; 8029 struct ksmbd_conn *conn = work->conn; 8030 int ret = 0; 8031 char *buffer; 8032 8033 if (work->next_smb2_rcv_hdr_off) { 8034 req = ksmbd_req_buf_next(work); 8035 rsp = ksmbd_resp_buf_next(work); 8036 if (!has_file_id(req->VolatileFileId)) { 8037 ksmbd_debug(SMB, "Compound request set FID = %llu\n", 8038 work->compound_fid); 8039 id = work->compound_fid; 8040 } 8041 } else { 8042 req = smb2_get_msg(work->request_buf); 8043 rsp = smb2_get_msg(work->response_buf); 8044 } 8045 8046 if (!has_file_id(id)) 8047 id = req->VolatileFileId; 8048 8049 if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) { 8050 rsp->hdr.Status = STATUS_NOT_SUPPORTED; 8051 goto out; 8052 } 8053 8054 buffer = (char *)req + le32_to_cpu(req->InputOffset); 8055 8056 cnt_code = le32_to_cpu(req->CtlCode); 8057 ret = smb2_calc_max_out_buf_len(work, 48, 8058 le32_to_cpu(req->MaxOutputResponse)); 8059 if (ret < 0) { 8060 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 8061 goto out; 8062 } 8063 out_buf_len = (unsigned int)ret; 8064 in_buf_len = le32_to_cpu(req->InputCount); 8065 8066 switch (cnt_code) { 8067 case FSCTL_DFS_GET_REFERRALS: 8068 case FSCTL_DFS_GET_REFERRALS_EX: 8069 /* Not support DFS yet */ 8070 rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED; 8071 goto out; 8072 case FSCTL_CREATE_OR_GET_OBJECT_ID: 8073 { 8074 struct file_object_buf_type1_ioctl_rsp *obj_buf; 8075 8076 nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp); 8077 obj_buf = (struct file_object_buf_type1_ioctl_rsp *) 8078 &rsp->Buffer[0]; 8079 8080 /* 8081 * TODO: This is dummy implementation to pass smbtorture 8082 * Need to check correct response later 8083 */ 8084 memset(obj_buf->ObjectId, 0x0, 16); 8085 memset(obj_buf->BirthVolumeId, 0x0, 16); 8086 memset(obj_buf->BirthObjectId, 0x0, 16); 8087 memset(obj_buf->DomainId, 0x0, 16); 8088 8089 break; 8090 } 8091 case FSCTL_PIPE_TRANSCEIVE: 8092 out_buf_len = min_t(u32, KSMBD_IPC_MAX_PAYLOAD, out_buf_len); 8093 nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp); 8094 break; 8095 case FSCTL_VALIDATE_NEGOTIATE_INFO: 8096 if (conn->dialect < SMB30_PROT_ID) { 8097 ret = -EOPNOTSUPP; 8098 goto out; 8099 } 8100 8101 if (in_buf_len < offsetof(struct validate_negotiate_info_req, 8102 Dialects)) { 8103 ret = -EINVAL; 8104 goto out; 8105 } 8106 8107 if (out_buf_len < sizeof(struct validate_negotiate_info_rsp)) { 8108 ret = -EINVAL; 8109 goto out; 8110 } 8111 8112 ret = fsctl_validate_negotiate_info(conn, 8113 (struct validate_negotiate_info_req *)buffer, 8114 (struct validate_negotiate_info_rsp *)&rsp->Buffer[0], 8115 in_buf_len); 8116 if (ret < 0) 8117 goto out; 8118 8119 nbytes = sizeof(struct validate_negotiate_info_rsp); 8120 rsp->PersistentFileId = SMB2_NO_FID; 8121 rsp->VolatileFileId = SMB2_NO_FID; 8122 break; 8123 case FSCTL_QUERY_NETWORK_INTERFACE_INFO: 8124 ret = fsctl_query_iface_info_ioctl(conn, rsp, out_buf_len); 8125 if (ret < 0) 8126 goto out; 8127 nbytes = ret; 8128 break; 8129 case FSCTL_REQUEST_RESUME_KEY: 8130 if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) { 8131 ret = -EINVAL; 8132 goto out; 8133 } 8134 8135 ret = fsctl_request_resume_key(work, req, 8136 (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]); 8137 if (ret < 0) 8138 goto out; 8139 rsp->PersistentFileId = req->PersistentFileId; 8140 rsp->VolatileFileId = req->VolatileFileId; 8141 nbytes = sizeof(struct resume_key_ioctl_rsp); 8142 break; 8143 case FSCTL_COPYCHUNK: 8144 case FSCTL_COPYCHUNK_WRITE: 8145 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { 8146 ksmbd_debug(SMB, 8147 "User does not have write permission\n"); 8148 ret = -EACCES; 8149 goto out; 8150 } 8151 8152 if (in_buf_len < sizeof(struct copychunk_ioctl_req)) { 8153 ret = -EINVAL; 8154 goto out; 8155 } 8156 8157 if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) { 8158 ret = -EINVAL; 8159 goto out; 8160 } 8161 8162 nbytes = sizeof(struct copychunk_ioctl_rsp); 8163 rsp->VolatileFileId = req->VolatileFileId; 8164 rsp->PersistentFileId = req->PersistentFileId; 8165 fsctl_copychunk(work, 8166 (struct copychunk_ioctl_req *)buffer, 8167 le32_to_cpu(req->CtlCode), 8168 le32_to_cpu(req->InputCount), 8169 req->VolatileFileId, 8170 req->PersistentFileId, 8171 rsp); 8172 break; 8173 case FSCTL_SET_SPARSE: 8174 if (in_buf_len < sizeof(struct file_sparse)) { 8175 ret = -EINVAL; 8176 goto out; 8177 } 8178 8179 ret = fsctl_set_sparse(work, id, (struct file_sparse *)buffer); 8180 if (ret < 0) 8181 goto out; 8182 break; 8183 case FSCTL_SET_ZERO_DATA: 8184 { 8185 struct file_zero_data_information *zero_data; 8186 struct ksmbd_file *fp; 8187 loff_t off, len, bfz; 8188 8189 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { 8190 ksmbd_debug(SMB, 8191 "User does not have write permission\n"); 8192 ret = -EACCES; 8193 goto out; 8194 } 8195 8196 if (in_buf_len < sizeof(struct file_zero_data_information)) { 8197 ret = -EINVAL; 8198 goto out; 8199 } 8200 8201 zero_data = 8202 (struct file_zero_data_information *)buffer; 8203 8204 off = le64_to_cpu(zero_data->FileOffset); 8205 bfz = le64_to_cpu(zero_data->BeyondFinalZero); 8206 if (off < 0 || bfz < 0 || off > bfz) { 8207 ret = -EINVAL; 8208 goto out; 8209 } 8210 8211 len = bfz - off; 8212 if (len) { 8213 fp = ksmbd_lookup_fd_fast(work, id); 8214 if (!fp) { 8215 ret = -ENOENT; 8216 goto out; 8217 } 8218 8219 ret = ksmbd_vfs_zero_data(work, fp, off, len); 8220 ksmbd_fd_put(work, fp); 8221 if (ret < 0) 8222 goto out; 8223 } 8224 break; 8225 } 8226 case FSCTL_QUERY_ALLOCATED_RANGES: 8227 if (in_buf_len < sizeof(struct file_allocated_range_buffer)) { 8228 ret = -EINVAL; 8229 goto out; 8230 } 8231 8232 ret = fsctl_query_allocated_ranges(work, id, 8233 (struct file_allocated_range_buffer *)buffer, 8234 (struct file_allocated_range_buffer *)&rsp->Buffer[0], 8235 out_buf_len / 8236 sizeof(struct file_allocated_range_buffer), &nbytes); 8237 if (ret == -E2BIG) { 8238 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; 8239 } else if (ret < 0) { 8240 nbytes = 0; 8241 goto out; 8242 } 8243 8244 nbytes *= sizeof(struct file_allocated_range_buffer); 8245 break; 8246 case FSCTL_GET_REPARSE_POINT: 8247 { 8248 struct reparse_data_buffer *reparse_ptr; 8249 struct ksmbd_file *fp; 8250 8251 reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0]; 8252 fp = ksmbd_lookup_fd_fast(work, id); 8253 if (!fp) { 8254 pr_err("not found fp!!\n"); 8255 ret = -ENOENT; 8256 goto out; 8257 } 8258 8259 reparse_ptr->ReparseTag = 8260 smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode); 8261 reparse_ptr->ReparseDataLength = 0; 8262 ksmbd_fd_put(work, fp); 8263 nbytes = sizeof(struct reparse_data_buffer); 8264 break; 8265 } 8266 case FSCTL_DUPLICATE_EXTENTS_TO_FILE: 8267 { 8268 struct ksmbd_file *fp_in, *fp_out = NULL; 8269 struct duplicate_extents_to_file *dup_ext; 8270 loff_t src_off, dst_off, length, cloned; 8271 8272 if (in_buf_len < sizeof(struct duplicate_extents_to_file)) { 8273 ret = -EINVAL; 8274 goto out; 8275 } 8276 8277 dup_ext = (struct duplicate_extents_to_file *)buffer; 8278 8279 fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle, 8280 dup_ext->PersistentFileHandle); 8281 if (!fp_in) { 8282 pr_err("not found file handle in duplicate extent to file\n"); 8283 ret = -ENOENT; 8284 goto out; 8285 } 8286 8287 fp_out = ksmbd_lookup_fd_fast(work, id); 8288 if (!fp_out) { 8289 pr_err("not found fp\n"); 8290 ret = -ENOENT; 8291 goto dup_ext_out; 8292 } 8293 8294 src_off = le64_to_cpu(dup_ext->SourceFileOffset); 8295 dst_off = le64_to_cpu(dup_ext->TargetFileOffset); 8296 length = le64_to_cpu(dup_ext->ByteCount); 8297 /* 8298 * XXX: It is not clear if FSCTL_DUPLICATE_EXTENTS_TO_FILE 8299 * should fall back to vfs_copy_file_range(). This could be 8300 * beneficial when re-exporting nfs/smb mount, but note that 8301 * this can result in partial copy that returns an error status. 8302 * If/when FSCTL_DUPLICATE_EXTENTS_TO_FILE_EX is implemented, 8303 * fall back to vfs_copy_file_range(), should be avoided when 8304 * the flag DUPLICATE_EXTENTS_DATA_EX_SOURCE_ATOMIC is set. 8305 */ 8306 cloned = vfs_clone_file_range(fp_in->filp, src_off, 8307 fp_out->filp, dst_off, length, 0); 8308 if (cloned == -EXDEV || cloned == -EOPNOTSUPP) { 8309 ret = -EOPNOTSUPP; 8310 goto dup_ext_out; 8311 } else if (cloned != length) { 8312 cloned = vfs_copy_file_range(fp_in->filp, src_off, 8313 fp_out->filp, dst_off, 8314 length, 0); 8315 if (cloned != length) { 8316 if (cloned < 0) 8317 ret = cloned; 8318 else 8319 ret = -EINVAL; 8320 } 8321 } 8322 8323 dup_ext_out: 8324 ksmbd_fd_put(work, fp_in); 8325 ksmbd_fd_put(work, fp_out); 8326 if (ret < 0) 8327 goto out; 8328 break; 8329 } 8330 default: 8331 ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n", 8332 cnt_code); 8333 ret = -EOPNOTSUPP; 8334 goto out; 8335 } 8336 8337 rsp->CtlCode = cpu_to_le32(cnt_code); 8338 rsp->InputCount = cpu_to_le32(0); 8339 rsp->InputOffset = cpu_to_le32(112); 8340 rsp->OutputOffset = cpu_to_le32(112); 8341 rsp->OutputCount = cpu_to_le32(nbytes); 8342 rsp->StructureSize = cpu_to_le16(49); 8343 rsp->Reserved = cpu_to_le16(0); 8344 rsp->Flags = cpu_to_le32(0); 8345 rsp->Reserved2 = cpu_to_le32(0); 8346 ret = ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_ioctl_rsp) + nbytes); 8347 if (!ret) 8348 return ret; 8349 8350 out: 8351 if (ret == -EACCES) 8352 rsp->hdr.Status = STATUS_ACCESS_DENIED; 8353 else if (ret == -ENOENT) 8354 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; 8355 else if (ret == -EOPNOTSUPP) 8356 rsp->hdr.Status = STATUS_NOT_SUPPORTED; 8357 else if (ret == -ENOSPC) 8358 rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL; 8359 else if (ret < 0 || rsp->hdr.Status == 0) 8360 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 8361 smb2_set_err_rsp(work); 8362 return 0; 8363 } 8364 8365 /** 8366 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command 8367 * @work: smb work containing oplock break command buffer 8368 * 8369 * Return: 0 8370 */ 8371 static void smb20_oplock_break_ack(struct ksmbd_work *work) 8372 { 8373 struct smb2_oplock_break *req; 8374 struct smb2_oplock_break *rsp; 8375 struct ksmbd_file *fp; 8376 struct oplock_info *opinfo = NULL; 8377 __le32 err = 0; 8378 int ret = 0; 8379 u64 volatile_id, persistent_id; 8380 char req_oplevel = 0, rsp_oplevel = 0; 8381 unsigned int oplock_change_type; 8382 8383 WORK_BUFFERS(work, req, rsp); 8384 8385 volatile_id = req->VolatileFid; 8386 persistent_id = req->PersistentFid; 8387 req_oplevel = req->OplockLevel; 8388 ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n", 8389 volatile_id, persistent_id, req_oplevel); 8390 8391 fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); 8392 if (!fp) { 8393 rsp->hdr.Status = STATUS_FILE_CLOSED; 8394 smb2_set_err_rsp(work); 8395 return; 8396 } 8397 8398 opinfo = opinfo_get(fp); 8399 if (!opinfo) { 8400 pr_err("unexpected null oplock_info\n"); 8401 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; 8402 smb2_set_err_rsp(work); 8403 ksmbd_fd_put(work, fp); 8404 return; 8405 } 8406 8407 if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) { 8408 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; 8409 goto err_out; 8410 } 8411 8412 if (opinfo->op_state == OPLOCK_STATE_NONE) { 8413 ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state); 8414 rsp->hdr.Status = STATUS_UNSUCCESSFUL; 8415 goto err_out; 8416 } 8417 8418 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || 8419 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && 8420 (req_oplevel != SMB2_OPLOCK_LEVEL_II && 8421 req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) { 8422 err = STATUS_INVALID_OPLOCK_PROTOCOL; 8423 oplock_change_type = OPLOCK_WRITE_TO_NONE; 8424 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && 8425 req_oplevel != SMB2_OPLOCK_LEVEL_NONE) { 8426 err = STATUS_INVALID_OPLOCK_PROTOCOL; 8427 oplock_change_type = OPLOCK_READ_TO_NONE; 8428 } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II || 8429 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { 8430 err = STATUS_INVALID_DEVICE_STATE; 8431 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || 8432 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && 8433 req_oplevel == SMB2_OPLOCK_LEVEL_II) { 8434 oplock_change_type = OPLOCK_WRITE_TO_READ; 8435 } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || 8436 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && 8437 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { 8438 oplock_change_type = OPLOCK_WRITE_TO_NONE; 8439 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && 8440 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { 8441 oplock_change_type = OPLOCK_READ_TO_NONE; 8442 } else { 8443 oplock_change_type = 0; 8444 } 8445 } else { 8446 oplock_change_type = 0; 8447 } 8448 8449 switch (oplock_change_type) { 8450 case OPLOCK_WRITE_TO_READ: 8451 ret = opinfo_write_to_read(opinfo); 8452 rsp_oplevel = SMB2_OPLOCK_LEVEL_II; 8453 break; 8454 case OPLOCK_WRITE_TO_NONE: 8455 ret = opinfo_write_to_none(opinfo); 8456 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; 8457 break; 8458 case OPLOCK_READ_TO_NONE: 8459 ret = opinfo_read_to_none(opinfo); 8460 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; 8461 break; 8462 default: 8463 pr_err("unknown oplock change 0x%x -> 0x%x\n", 8464 opinfo->level, rsp_oplevel); 8465 } 8466 8467 if (ret < 0) { 8468 rsp->hdr.Status = err; 8469 goto err_out; 8470 } 8471 8472 opinfo->op_state = OPLOCK_STATE_NONE; 8473 wake_up_interruptible_all(&opinfo->oplock_q); 8474 opinfo_put(opinfo); 8475 ksmbd_fd_put(work, fp); 8476 8477 rsp->StructureSize = cpu_to_le16(24); 8478 rsp->OplockLevel = rsp_oplevel; 8479 rsp->Reserved = 0; 8480 rsp->Reserved2 = 0; 8481 rsp->VolatileFid = volatile_id; 8482 rsp->PersistentFid = persistent_id; 8483 ret = ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_oplock_break)); 8484 if (!ret) 8485 return; 8486 8487 err_out: 8488 opinfo->op_state = OPLOCK_STATE_NONE; 8489 wake_up_interruptible_all(&opinfo->oplock_q); 8490 8491 opinfo_put(opinfo); 8492 ksmbd_fd_put(work, fp); 8493 smb2_set_err_rsp(work); 8494 } 8495 8496 static int check_lease_state(struct lease *lease, __le32 req_state) 8497 { 8498 if ((lease->new_state == 8499 (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) && 8500 !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) { 8501 lease->new_state = req_state; 8502 return 0; 8503 } 8504 8505 if (lease->new_state == req_state) 8506 return 0; 8507 8508 return 1; 8509 } 8510 8511 /** 8512 * smb21_lease_break_ack() - handler for smb2.1 lease break command 8513 * @work: smb work containing lease break command buffer 8514 * 8515 * Return: 0 8516 */ 8517 static void smb21_lease_break_ack(struct ksmbd_work *work) 8518 { 8519 struct ksmbd_conn *conn = work->conn; 8520 struct smb2_lease_ack *req; 8521 struct smb2_lease_ack *rsp; 8522 struct oplock_info *opinfo; 8523 __le32 err = 0; 8524 int ret = 0; 8525 unsigned int lease_change_type; 8526 __le32 lease_state; 8527 struct lease *lease; 8528 8529 WORK_BUFFERS(work, req, rsp); 8530 8531 ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n", 8532 le32_to_cpu(req->LeaseState)); 8533 opinfo = lookup_lease_in_table(conn, req->LeaseKey); 8534 if (!opinfo) { 8535 ksmbd_debug(OPLOCK, "file not opened\n"); 8536 smb2_set_err_rsp(work); 8537 rsp->hdr.Status = STATUS_UNSUCCESSFUL; 8538 return; 8539 } 8540 lease = opinfo->o_lease; 8541 8542 if (opinfo->op_state == OPLOCK_STATE_NONE) { 8543 pr_err("unexpected lease break state 0x%x\n", 8544 opinfo->op_state); 8545 rsp->hdr.Status = STATUS_UNSUCCESSFUL; 8546 goto err_out; 8547 } 8548 8549 if (check_lease_state(lease, req->LeaseState)) { 8550 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; 8551 ksmbd_debug(OPLOCK, 8552 "req lease state: 0x%x, expected state: 0x%x\n", 8553 req->LeaseState, lease->new_state); 8554 goto err_out; 8555 } 8556 8557 if (!atomic_read(&opinfo->breaking_cnt)) { 8558 rsp->hdr.Status = STATUS_UNSUCCESSFUL; 8559 goto err_out; 8560 } 8561 8562 /* check for bad lease state */ 8563 if (req->LeaseState & 8564 (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) { 8565 err = STATUS_INVALID_OPLOCK_PROTOCOL; 8566 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) 8567 lease_change_type = OPLOCK_WRITE_TO_NONE; 8568 else 8569 lease_change_type = OPLOCK_READ_TO_NONE; 8570 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", 8571 le32_to_cpu(lease->state), 8572 le32_to_cpu(req->LeaseState)); 8573 } else if (lease->state == SMB2_LEASE_READ_CACHING_LE && 8574 req->LeaseState != SMB2_LEASE_NONE_LE) { 8575 err = STATUS_INVALID_OPLOCK_PROTOCOL; 8576 lease_change_type = OPLOCK_READ_TO_NONE; 8577 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", 8578 le32_to_cpu(lease->state), 8579 le32_to_cpu(req->LeaseState)); 8580 } else { 8581 /* valid lease state changes */ 8582 err = STATUS_INVALID_DEVICE_STATE; 8583 if (req->LeaseState == SMB2_LEASE_NONE_LE) { 8584 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) 8585 lease_change_type = OPLOCK_WRITE_TO_NONE; 8586 else 8587 lease_change_type = OPLOCK_READ_TO_NONE; 8588 } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) { 8589 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) 8590 lease_change_type = OPLOCK_WRITE_TO_READ; 8591 else 8592 lease_change_type = OPLOCK_READ_HANDLE_TO_READ; 8593 } else { 8594 lease_change_type = 0; 8595 } 8596 } 8597 8598 switch (lease_change_type) { 8599 case OPLOCK_WRITE_TO_READ: 8600 ret = opinfo_write_to_read(opinfo); 8601 break; 8602 case OPLOCK_READ_HANDLE_TO_READ: 8603 ret = opinfo_read_handle_to_read(opinfo); 8604 break; 8605 case OPLOCK_WRITE_TO_NONE: 8606 ret = opinfo_write_to_none(opinfo); 8607 break; 8608 case OPLOCK_READ_TO_NONE: 8609 ret = opinfo_read_to_none(opinfo); 8610 break; 8611 default: 8612 ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n", 8613 le32_to_cpu(lease->state), 8614 le32_to_cpu(req->LeaseState)); 8615 } 8616 8617 if (ret < 0) { 8618 rsp->hdr.Status = err; 8619 goto err_out; 8620 } 8621 8622 lease_state = lease->state; 8623 opinfo->op_state = OPLOCK_STATE_NONE; 8624 wake_up_interruptible_all(&opinfo->oplock_q); 8625 atomic_dec(&opinfo->breaking_cnt); 8626 wake_up_interruptible_all(&opinfo->oplock_brk); 8627 opinfo_put(opinfo); 8628 8629 rsp->StructureSize = cpu_to_le16(36); 8630 rsp->Reserved = 0; 8631 rsp->Flags = 0; 8632 memcpy(rsp->LeaseKey, req->LeaseKey, 16); 8633 rsp->LeaseState = lease_state; 8634 rsp->LeaseDuration = 0; 8635 ret = ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_lease_ack)); 8636 if (!ret) 8637 return; 8638 8639 err_out: 8640 wake_up_interruptible_all(&opinfo->oplock_q); 8641 atomic_dec(&opinfo->breaking_cnt); 8642 wake_up_interruptible_all(&opinfo->oplock_brk); 8643 8644 opinfo_put(opinfo); 8645 smb2_set_err_rsp(work); 8646 } 8647 8648 /** 8649 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break 8650 * @work: smb work containing oplock/lease break command buffer 8651 * 8652 * Return: 0 8653 */ 8654 int smb2_oplock_break(struct ksmbd_work *work) 8655 { 8656 struct smb2_oplock_break *req; 8657 struct smb2_oplock_break *rsp; 8658 8659 WORK_BUFFERS(work, req, rsp); 8660 8661 switch (le16_to_cpu(req->StructureSize)) { 8662 case OP_BREAK_STRUCT_SIZE_20: 8663 smb20_oplock_break_ack(work); 8664 break; 8665 case OP_BREAK_STRUCT_SIZE_21: 8666 smb21_lease_break_ack(work); 8667 break; 8668 default: 8669 ksmbd_debug(OPLOCK, "invalid break cmd %d\n", 8670 le16_to_cpu(req->StructureSize)); 8671 rsp->hdr.Status = STATUS_INVALID_PARAMETER; 8672 smb2_set_err_rsp(work); 8673 } 8674 8675 return 0; 8676 } 8677 8678 /** 8679 * smb2_notify() - handler for smb2 notify request 8680 * @work: smb work containing notify command buffer 8681 * 8682 * Return: 0 8683 */ 8684 int smb2_notify(struct ksmbd_work *work) 8685 { 8686 struct smb2_change_notify_req *req; 8687 struct smb2_change_notify_rsp *rsp; 8688 8689 WORK_BUFFERS(work, req, rsp); 8690 8691 if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) { 8692 rsp->hdr.Status = STATUS_INTERNAL_ERROR; 8693 smb2_set_err_rsp(work); 8694 return 0; 8695 } 8696 8697 smb2_set_err_rsp(work); 8698 rsp->hdr.Status = STATUS_NOT_IMPLEMENTED; 8699 return 0; 8700 } 8701 8702 /** 8703 * smb2_is_sign_req() - handler for checking packet signing status 8704 * @work: smb work containing notify command buffer 8705 * @command: SMB2 command id 8706 * 8707 * Return: true if packed is signed, false otherwise 8708 */ 8709 bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command) 8710 { 8711 struct smb2_hdr *rcv_hdr2 = smb2_get_msg(work->request_buf); 8712 8713 if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) && 8714 command != SMB2_NEGOTIATE_HE && 8715 command != SMB2_SESSION_SETUP_HE && 8716 command != SMB2_OPLOCK_BREAK_HE) 8717 return true; 8718 8719 return false; 8720 } 8721 8722 /** 8723 * smb2_check_sign_req() - handler for req packet sign processing 8724 * @work: smb work containing notify command buffer 8725 * 8726 * Return: 1 on success, 0 otherwise 8727 */ 8728 int smb2_check_sign_req(struct ksmbd_work *work) 8729 { 8730 struct smb2_hdr *hdr; 8731 char signature_req[SMB2_SIGNATURE_SIZE]; 8732 char signature[SMB2_HMACSHA256_SIZE]; 8733 struct kvec iov[1]; 8734 size_t len; 8735 8736 hdr = smb2_get_msg(work->request_buf); 8737 if (work->next_smb2_rcv_hdr_off) 8738 hdr = ksmbd_req_buf_next(work); 8739 8740 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) 8741 len = get_rfc1002_len(work->request_buf); 8742 else if (hdr->NextCommand) 8743 len = le32_to_cpu(hdr->NextCommand); 8744 else 8745 len = get_rfc1002_len(work->request_buf) - 8746 work->next_smb2_rcv_hdr_off; 8747 8748 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); 8749 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); 8750 8751 iov[0].iov_base = (char *)&hdr->ProtocolId; 8752 iov[0].iov_len = len; 8753 8754 if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1, 8755 signature)) 8756 return 0; 8757 8758 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { 8759 pr_err("bad smb2 signature\n"); 8760 return 0; 8761 } 8762 8763 return 1; 8764 } 8765 8766 /** 8767 * smb2_set_sign_rsp() - handler for rsp packet sign processing 8768 * @work: smb work containing notify command buffer 8769 * 8770 */ 8771 void smb2_set_sign_rsp(struct ksmbd_work *work) 8772 { 8773 struct smb2_hdr *hdr; 8774 char signature[SMB2_HMACSHA256_SIZE]; 8775 struct kvec *iov; 8776 int n_vec = 1; 8777 8778 hdr = ksmbd_resp_buf_curr(work); 8779 hdr->Flags |= SMB2_FLAGS_SIGNED; 8780 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); 8781 8782 if (hdr->Command == SMB2_READ) { 8783 iov = &work->iov[work->iov_idx - 1]; 8784 n_vec++; 8785 } else { 8786 iov = &work->iov[work->iov_idx]; 8787 } 8788 8789 if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec, 8790 signature)) 8791 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); 8792 } 8793 8794 /** 8795 * smb3_check_sign_req() - handler for req packet sign processing 8796 * @work: smb work containing notify command buffer 8797 * 8798 * Return: 1 on success, 0 otherwise 8799 */ 8800 int smb3_check_sign_req(struct ksmbd_work *work) 8801 { 8802 struct ksmbd_conn *conn = work->conn; 8803 char *signing_key; 8804 struct smb2_hdr *hdr; 8805 struct channel *chann; 8806 char signature_req[SMB2_SIGNATURE_SIZE]; 8807 char signature[SMB2_CMACAES_SIZE]; 8808 struct kvec iov[1]; 8809 size_t len; 8810 8811 hdr = smb2_get_msg(work->request_buf); 8812 if (work->next_smb2_rcv_hdr_off) 8813 hdr = ksmbd_req_buf_next(work); 8814 8815 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) 8816 len = get_rfc1002_len(work->request_buf); 8817 else if (hdr->NextCommand) 8818 len = le32_to_cpu(hdr->NextCommand); 8819 else 8820 len = get_rfc1002_len(work->request_buf) - 8821 work->next_smb2_rcv_hdr_off; 8822 8823 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { 8824 signing_key = work->sess->smb3signingkey; 8825 } else { 8826 chann = lookup_chann_list(work->sess, conn); 8827 if (!chann) { 8828 return 0; 8829 } 8830 signing_key = chann->smb3signingkey; 8831 } 8832 8833 if (!signing_key) { 8834 pr_err("SMB3 signing key is not generated\n"); 8835 return 0; 8836 } 8837 8838 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); 8839 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); 8840 iov[0].iov_base = (char *)&hdr->ProtocolId; 8841 iov[0].iov_len = len; 8842 8843 if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature)) 8844 return 0; 8845 8846 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { 8847 pr_err("bad smb2 signature\n"); 8848 return 0; 8849 } 8850 8851 return 1; 8852 } 8853 8854 /** 8855 * smb3_set_sign_rsp() - handler for rsp packet sign processing 8856 * @work: smb work containing notify command buffer 8857 * 8858 */ 8859 void smb3_set_sign_rsp(struct ksmbd_work *work) 8860 { 8861 struct ksmbd_conn *conn = work->conn; 8862 struct smb2_hdr *hdr; 8863 struct channel *chann; 8864 char signature[SMB2_CMACAES_SIZE]; 8865 struct kvec *iov; 8866 int n_vec = 1; 8867 char *signing_key; 8868 8869 hdr = ksmbd_resp_buf_curr(work); 8870 8871 if (conn->binding == false && 8872 le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { 8873 signing_key = work->sess->smb3signingkey; 8874 } else { 8875 chann = lookup_chann_list(work->sess, work->conn); 8876 if (!chann) { 8877 return; 8878 } 8879 signing_key = chann->smb3signingkey; 8880 } 8881 8882 if (!signing_key) 8883 return; 8884 8885 hdr->Flags |= SMB2_FLAGS_SIGNED; 8886 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); 8887 8888 if (hdr->Command == SMB2_READ) { 8889 iov = &work->iov[work->iov_idx - 1]; 8890 n_vec++; 8891 } else { 8892 iov = &work->iov[work->iov_idx]; 8893 } 8894 8895 if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, 8896 signature)) 8897 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); 8898 } 8899 8900 /** 8901 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response 8902 * @work: smb work containing response buffer 8903 * 8904 */ 8905 void smb3_preauth_hash_rsp(struct ksmbd_work *work) 8906 { 8907 struct ksmbd_conn *conn = work->conn; 8908 struct ksmbd_session *sess = work->sess; 8909 struct smb2_hdr *req, *rsp; 8910 8911 if (conn->dialect != SMB311_PROT_ID) 8912 return; 8913 8914 WORK_BUFFERS(work, req, rsp); 8915 8916 if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE && 8917 conn->preauth_info) 8918 ksmbd_gen_preauth_integrity_hash(conn, work->response_buf, 8919 conn->preauth_info->Preauth_HashValue); 8920 8921 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) { 8922 __u8 *hash_value; 8923 8924 if (conn->binding) { 8925 struct preauth_session *preauth_sess; 8926 8927 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); 8928 if (!preauth_sess) 8929 return; 8930 hash_value = preauth_sess->Preauth_HashValue; 8931 } else { 8932 hash_value = sess->Preauth_HashValue; 8933 if (!hash_value) 8934 return; 8935 } 8936 ksmbd_gen_preauth_integrity_hash(conn, work->response_buf, 8937 hash_value); 8938 } 8939 } 8940 8941 static void fill_transform_hdr(void *tr_buf, char *old_buf, __le16 cipher_type) 8942 { 8943 struct smb2_transform_hdr *tr_hdr = tr_buf + 4; 8944 struct smb2_hdr *hdr = smb2_get_msg(old_buf); 8945 unsigned int orig_len = get_rfc1002_len(old_buf); 8946 8947 /* tr_buf must be cleared by the caller */ 8948 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM; 8949 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len); 8950 tr_hdr->Flags = cpu_to_le16(TRANSFORM_FLAG_ENCRYPTED); 8951 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM || 8952 cipher_type == SMB2_ENCRYPTION_AES256_GCM) 8953 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE); 8954 else 8955 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE); 8956 memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8); 8957 inc_rfc1001_len(tr_buf, sizeof(struct smb2_transform_hdr)); 8958 inc_rfc1001_len(tr_buf, orig_len); 8959 } 8960 8961 int smb3_encrypt_resp(struct ksmbd_work *work) 8962 { 8963 struct kvec *iov = work->iov; 8964 int rc = -ENOMEM; 8965 void *tr_buf; 8966 8967 tr_buf = kzalloc(sizeof(struct smb2_transform_hdr) + 4, GFP_KERNEL); 8968 if (!tr_buf) 8969 return rc; 8970 8971 /* fill transform header */ 8972 fill_transform_hdr(tr_buf, work->response_buf, work->conn->cipher_type); 8973 8974 iov[0].iov_base = tr_buf; 8975 iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4; 8976 work->tr_buf = tr_buf; 8977 8978 return ksmbd_crypt_message(work, iov, work->iov_idx + 1, 1); 8979 } 8980 8981 bool smb3_is_transform_hdr(void *buf) 8982 { 8983 struct smb2_transform_hdr *trhdr = smb2_get_msg(buf); 8984 8985 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM; 8986 } 8987 8988 int smb3_decrypt_req(struct ksmbd_work *work) 8989 { 8990 struct ksmbd_session *sess; 8991 char *buf = work->request_buf; 8992 unsigned int pdu_length = get_rfc1002_len(buf); 8993 struct kvec iov[2]; 8994 int buf_data_size = pdu_length - sizeof(struct smb2_transform_hdr); 8995 struct smb2_transform_hdr *tr_hdr = smb2_get_msg(buf); 8996 int rc = 0; 8997 8998 if (pdu_length < sizeof(struct smb2_transform_hdr) || 8999 buf_data_size < sizeof(struct smb2_hdr)) { 9000 pr_err("Transform message is too small (%u)\n", 9001 pdu_length); 9002 return -ECONNABORTED; 9003 } 9004 9005 if (buf_data_size < le32_to_cpu(tr_hdr->OriginalMessageSize)) { 9006 pr_err("Transform message is broken\n"); 9007 return -ECONNABORTED; 9008 } 9009 9010 sess = ksmbd_session_lookup_all(work->conn, le64_to_cpu(tr_hdr->SessionId)); 9011 if (!sess) { 9012 pr_err("invalid session id(%llx) in transform header\n", 9013 le64_to_cpu(tr_hdr->SessionId)); 9014 return -ECONNABORTED; 9015 } 9016 ksmbd_user_session_put(sess); 9017 9018 iov[0].iov_base = buf; 9019 iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4; 9020 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr) + 4; 9021 iov[1].iov_len = buf_data_size; 9022 rc = ksmbd_crypt_message(work, iov, 2, 0); 9023 if (rc) 9024 return rc; 9025 9026 memmove(buf + 4, iov[1].iov_base, buf_data_size); 9027 *(__be32 *)buf = cpu_to_be32(buf_data_size); 9028 9029 return rc; 9030 } 9031 9032 bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work) 9033 { 9034 struct ksmbd_conn *conn = work->conn; 9035 struct ksmbd_session *sess = work->sess; 9036 struct smb2_hdr *rsp = smb2_get_msg(work->response_buf); 9037 9038 if (conn->dialect < SMB30_PROT_ID) 9039 return false; 9040 9041 if (work->next_smb2_rcv_hdr_off) 9042 rsp = ksmbd_resp_buf_next(work); 9043 9044 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && 9045 sess->user && !user_guest(sess->user) && 9046 rsp->Status == STATUS_SUCCESS) 9047 return true; 9048 return false; 9049 } 9050