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