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