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