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