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