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