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