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