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