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