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