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