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