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