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