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