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