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