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