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