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