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