blob: 2df8217c7395eee18f6f44b7c2999ef09cca88ac [file] [log] [blame]
Namjae Jeone2f34482021-03-16 10:49:09 +09001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
5 */
6
7#include <linux/inetdevice.h>
8#include <net/addrconf.h>
9#include <linux/syscalls.h>
10#include <linux/namei.h>
11#include <linux/statfs.h>
12#include <linux/ethtool.h>
13
14#include "glob.h"
15#include "smb2pdu.h"
16#include "smbfsctl.h"
17#include "oplock.h"
18#include "smbacl.h"
19
20#include "auth.h"
21#include "asn1.h"
Namjae Jeone2f34482021-03-16 10:49:09 +090022#include "connection.h"
23#include "transport_ipc.h"
24#include "vfs.h"
25#include "vfs_cache.h"
26#include "misc.h"
27
Namjae Jeone2f34482021-03-16 10:49:09 +090028#include "server.h"
29#include "smb_common.h"
30#include "smbstatus.h"
31#include "ksmbd_work.h"
32#include "mgmt/user_config.h"
33#include "mgmt/share_config.h"
34#include "mgmt/tree_connect.h"
35#include "mgmt/user_session.h"
36#include "mgmt/ksmbd_ida.h"
37#include "ndr.h"
38
39static void __wbuf(struct ksmbd_work *work, void **req, void **rsp)
40{
41 if (work->next_smb2_rcv_hdr_off) {
42 *req = REQUEST_BUF_NEXT(work);
43 *rsp = RESPONSE_BUF_NEXT(work);
44 } else {
Namjae Jeone5066492021-03-30 12:35:23 +090045 *req = work->request_buf;
46 *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +090047 }
48}
49
50#define WORK_BUFFERS(w, rq, rs) __wbuf((w), (void **)&(rq), (void **)&(rs))
51
52/**
53 * check_session_id() - check for valid session id in smb header
54 * @conn: connection instance
55 * @id: session id from smb header
56 *
57 * Return: 1 if valid session id, otherwise 0
58 */
Namjae Jeon64b39f42021-03-30 14:25:35 +090059static inline int check_session_id(struct ksmbd_conn *conn, u64 id)
Namjae Jeone2f34482021-03-16 10:49:09 +090060{
61 struct ksmbd_session *sess;
62
63 if (id == 0 || id == -1)
64 return 0;
65
Namjae Jeonf5a544e2021-06-18 10:04:19 +090066 sess = ksmbd_session_lookup_all(conn, id);
Namjae Jeone2f34482021-03-16 10:49:09 +090067 if (sess)
68 return 1;
69 ksmbd_err("Invalid user session id: %llu\n", id);
70 return 0;
71}
72
Namjae Jeonf5a544e2021-06-18 10:04:19 +090073struct channel *lookup_chann_list(struct ksmbd_session *sess, struct ksmbd_conn *conn)
Namjae Jeone2f34482021-03-16 10:49:09 +090074{
75 struct channel *chann;
76 struct list_head *t;
77
78 list_for_each(t, &sess->ksmbd_chann_list) {
79 chann = list_entry(t, struct channel, chann_list);
Namjae Jeonf5a544e2021-06-18 10:04:19 +090080 if (chann && chann->conn == conn)
Namjae Jeone2f34482021-03-16 10:49:09 +090081 return chann;
82 }
83
84 return NULL;
85}
86
87/**
88 * smb2_get_ksmbd_tcon() - get tree connection information for a tree id
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +090089 * @work: smb work
Namjae Jeone2f34482021-03-16 10:49:09 +090090 *
91 * Return: matching tree connection on success, otherwise error
92 */
93int smb2_get_ksmbd_tcon(struct ksmbd_work *work)
94{
Namjae Jeone5066492021-03-30 12:35:23 +090095 struct smb2_hdr *req_hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +090096 int tree_id;
97
98 work->tcon = NULL;
Namjae Jeon64b39f42021-03-30 14:25:35 +090099 if (work->conn->ops->get_cmd_val(work) == SMB2_TREE_CONNECT_HE ||
100 work->conn->ops->get_cmd_val(work) == SMB2_CANCEL_HE ||
101 work->conn->ops->get_cmd_val(work) == SMB2_LOGOFF_HE) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900102 ksmbd_debug(SMB, "skip to check tree connect request\n");
103 return 0;
104 }
105
Namjae Jeon02b68b22021-04-01 17:45:33 +0900106 if (xa_empty(&work->sess->tree_conns)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900107 ksmbd_debug(SMB, "NO tree connected\n");
108 return -1;
109 }
110
111 tree_id = le32_to_cpu(req_hdr->Id.SyncId.TreeId);
112 work->tcon = ksmbd_tree_conn_lookup(work->sess, tree_id);
113 if (!work->tcon) {
114 ksmbd_err("Invalid tid %d\n", tree_id);
115 return -1;
116 }
117
118 return 1;
119}
120
121/**
122 * smb2_set_err_rsp() - set error response code on smb response
123 * @work: smb work containing response buffer
124 */
125void smb2_set_err_rsp(struct ksmbd_work *work)
126{
127 struct smb2_err_rsp *err_rsp;
128
129 if (work->next_smb2_rcv_hdr_off)
130 err_rsp = RESPONSE_BUF_NEXT(work);
131 else
Namjae Jeone5066492021-03-30 12:35:23 +0900132 err_rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900133
134 if (err_rsp->hdr.Status != STATUS_STOPPED_ON_SYMLINK) {
135 err_rsp->StructureSize = SMB2_ERROR_STRUCTURE_SIZE2_LE;
136 err_rsp->ErrorContextCount = 0;
137 err_rsp->Reserved = 0;
138 err_rsp->ByteCount = 0;
139 err_rsp->ErrorData[0] = 0;
Namjae Jeone5066492021-03-30 12:35:23 +0900140 inc_rfc1001_len(work->response_buf, SMB2_ERROR_STRUCTURE_SIZE2);
Namjae Jeone2f34482021-03-16 10:49:09 +0900141 }
142}
143
144/**
145 * is_smb2_neg_cmd() - is it smb2 negotiation command
146 * @work: smb work containing smb header
147 *
148 * Return: 1 if smb2 negotiation command, otherwise 0
149 */
150int is_smb2_neg_cmd(struct ksmbd_work *work)
151{
Namjae Jeone5066492021-03-30 12:35:23 +0900152 struct smb2_hdr *hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900153
154 /* is it SMB2 header ? */
155 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
156 return 0;
157
158 /* make sure it is request not response message */
159 if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
160 return 0;
161
162 if (hdr->Command != SMB2_NEGOTIATE)
163 return 0;
164
165 return 1;
166}
167
168/**
169 * is_smb2_rsp() - is it smb2 response
170 * @work: smb work containing smb response buffer
171 *
172 * Return: 1 if smb2 response, otherwise 0
173 */
174int is_smb2_rsp(struct ksmbd_work *work)
175{
Namjae Jeone5066492021-03-30 12:35:23 +0900176 struct smb2_hdr *hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900177
178 /* is it SMB2 header ? */
179 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
180 return 0;
181
182 /* make sure it is response not request message */
183 if (!(hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR))
184 return 0;
185
186 return 1;
187}
188
189/**
190 * get_smb2_cmd_val() - get smb command code from smb header
191 * @work: smb work containing smb request buffer
192 *
193 * Return: smb2 request command value
194 */
Namjae Jeonfc2d1b52021-05-26 18:01:08 +0900195u16 get_smb2_cmd_val(struct ksmbd_work *work)
Namjae Jeone2f34482021-03-16 10:49:09 +0900196{
197 struct smb2_hdr *rcv_hdr;
198
199 if (work->next_smb2_rcv_hdr_off)
200 rcv_hdr = REQUEST_BUF_NEXT(work);
201 else
Namjae Jeone5066492021-03-30 12:35:23 +0900202 rcv_hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900203 return le16_to_cpu(rcv_hdr->Command);
204}
205
206/**
207 * set_smb2_rsp_status() - set error response code on smb2 header
208 * @work: smb work containing response buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900209 * @err: error response code
Namjae Jeone2f34482021-03-16 10:49:09 +0900210 */
211void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err)
212{
213 struct smb2_hdr *rsp_hdr;
214
215 if (work->next_smb2_rcv_hdr_off)
216 rsp_hdr = RESPONSE_BUF_NEXT(work);
217 else
Namjae Jeone5066492021-03-30 12:35:23 +0900218 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900219 rsp_hdr->Status = err;
220 smb2_set_err_rsp(work);
221}
222
223/**
224 * init_smb2_neg_rsp() - initialize smb2 response for negotiate command
225 * @work: smb work containing smb request buffer
226 *
227 * smb2 negotiate response is sent in reply of smb1 negotiate command for
228 * dialect auto-negotiation.
229 */
230int init_smb2_neg_rsp(struct ksmbd_work *work)
231{
232 struct smb2_hdr *rsp_hdr;
233 struct smb2_negotiate_rsp *rsp;
234 struct ksmbd_conn *conn = work->conn;
235
236 if (conn->need_neg == false)
237 return -EINVAL;
238 if (!(conn->dialect >= SMB20_PROT_ID &&
Namjae Jeon64b39f42021-03-30 14:25:35 +0900239 conn->dialect <= SMB311_PROT_ID))
Namjae Jeone2f34482021-03-16 10:49:09 +0900240 return -EINVAL;
241
Namjae Jeone5066492021-03-30 12:35:23 +0900242 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900243
244 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
245
246 rsp_hdr->smb2_buf_length =
247 cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
248
249 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
250 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
251 rsp_hdr->CreditRequest = cpu_to_le16(2);
252 rsp_hdr->Command = SMB2_NEGOTIATE;
253 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
254 rsp_hdr->NextCommand = 0;
255 rsp_hdr->MessageId = 0;
256 rsp_hdr->Id.SyncId.ProcessId = 0;
257 rsp_hdr->Id.SyncId.TreeId = 0;
258 rsp_hdr->SessionId = 0;
259 memset(rsp_hdr->Signature, 0, 16);
260
Namjae Jeone5066492021-03-30 12:35:23 +0900261 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900262
263 WARN_ON(ksmbd_conn_good(work));
264
265 rsp->StructureSize = cpu_to_le16(65);
266 ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect);
267 rsp->DialectRevision = cpu_to_le16(conn->dialect);
268 /* Not setting conn guid rsp->ServerGUID, as it
269 * not used by client for identifying connection
270 */
271 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
272 /* Default Max Message Size till SMB2.0, 64K*/
273 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
274 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
275 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
276
277 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
278 rsp->ServerStartTime = 0;
279
280 rsp->SecurityBufferOffset = cpu_to_le16(128);
281 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
282 ksmbd_copy_gss_neg_header(((char *)(&rsp->hdr) +
283 sizeof(rsp->hdr.smb2_buf_length)) +
284 le16_to_cpu(rsp->SecurityBufferOffset));
285 inc_rfc1001_len(rsp, sizeof(struct smb2_negotiate_rsp) -
286 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
287 AUTH_GSS_LENGTH);
288 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
289 if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY)
290 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
291 conn->use_spnego = true;
292
293 ksmbd_conn_set_need_negotiate(work);
294 return 0;
295}
296
297static int smb2_consume_credit_charge(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +0900298 unsigned short credit_charge)
Namjae Jeone2f34482021-03-16 10:49:09 +0900299{
300 struct ksmbd_conn *conn = work->conn;
301 unsigned int rsp_credits = 1;
302
303 if (!conn->total_credits)
304 return 0;
305
306 if (credit_charge > 0)
307 rsp_credits = credit_charge;
308
309 conn->total_credits -= rsp_credits;
310 return rsp_credits;
311}
312
313/**
314 * smb2_set_rsp_credits() - set number of credits in response buffer
315 * @work: smb work containing smb response buffer
316 */
317int smb2_set_rsp_credits(struct ksmbd_work *work)
318{
319 struct smb2_hdr *req_hdr = REQUEST_BUF_NEXT(work);
320 struct smb2_hdr *hdr = RESPONSE_BUF_NEXT(work);
321 struct ksmbd_conn *conn = work->conn;
322 unsigned short credits_requested = le16_to_cpu(req_hdr->CreditRequest);
323 unsigned short credit_charge = 1, credits_granted = 0;
324 unsigned short aux_max, aux_credits, min_credits;
325 int rsp_credit_charge;
326
327 if (hdr->Command == SMB2_CANCEL)
328 goto out;
329
330 /* get default minimum credits by shifting maximum credits by 4 */
331 min_credits = conn->max_credits >> 4;
332
333 if (conn->total_credits >= conn->max_credits) {
334 ksmbd_err("Total credits overflow: %d\n", conn->total_credits);
335 conn->total_credits = min_credits;
336 }
337
Namjae Jeon070fb212021-05-26 17:57:12 +0900338 rsp_credit_charge =
339 smb2_consume_credit_charge(work, le16_to_cpu(req_hdr->CreditCharge));
Namjae Jeone2f34482021-03-16 10:49:09 +0900340 if (rsp_credit_charge < 0)
341 return -EINVAL;
342
343 hdr->CreditCharge = cpu_to_le16(rsp_credit_charge);
344
345 if (credits_requested > 0) {
346 aux_credits = credits_requested - 1;
347 aux_max = 32;
348 if (hdr->Command == SMB2_NEGOTIATE)
349 aux_max = 0;
350 aux_credits = (aux_credits < aux_max) ? aux_credits : aux_max;
351 credits_granted = aux_credits + credit_charge;
352
353 /* if credits granted per client is getting bigger than default
354 * minimum credits then we should wrap it up within the limits.
355 */
356 if ((conn->total_credits + credits_granted) > min_credits)
357 credits_granted = min_credits - conn->total_credits;
358 /*
359 * TODO: Need to adjuct CreditRequest value according to
360 * current cpu load
361 */
362 } else if (conn->total_credits == 0) {
363 credits_granted = 1;
364 }
365
366 conn->total_credits += credits_granted;
367 work->credits_granted += credits_granted;
368
369 if (!req_hdr->NextCommand) {
370 /* Update CreditRequest in last request */
371 hdr->CreditRequest = cpu_to_le16(work->credits_granted);
372 }
373out:
374 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900375 "credits: requested[%d] granted[%d] total_granted[%d]\n",
376 credits_requested, credits_granted,
377 conn->total_credits);
Namjae Jeone2f34482021-03-16 10:49:09 +0900378 return 0;
379}
380
381/**
382 * init_chained_smb2_rsp() - initialize smb2 chained response
383 * @work: smb work containing smb response buffer
384 */
385static void init_chained_smb2_rsp(struct ksmbd_work *work)
386{
387 struct smb2_hdr *req = REQUEST_BUF_NEXT(work);
388 struct smb2_hdr *rsp = RESPONSE_BUF_NEXT(work);
389 struct smb2_hdr *rsp_hdr;
390 struct smb2_hdr *rcv_hdr;
391 int next_hdr_offset = 0;
392 int len, new_len;
393
394 /* Len of this response = updated RFC len - offset of previous cmd
395 * in the compound rsp
396 */
397
398 /* Storing the current local FID which may be needed by subsequent
399 * command in the compound request
400 */
401 if (req->Command == SMB2_CREATE && rsp->Status == STATUS_SUCCESS) {
402 work->compound_fid =
403 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
404 VolatileFileId);
405 work->compound_pfid =
406 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
407 PersistentFileId);
408 work->compound_sid = le64_to_cpu(rsp->SessionId);
409 }
410
Namjae Jeone5066492021-03-30 12:35:23 +0900411 len = get_rfc1002_len(work->response_buf) - work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +0900412 next_hdr_offset = le32_to_cpu(req->NextCommand);
413
414 new_len = ALIGN(len, 8);
Namjae Jeone5066492021-03-30 12:35:23 +0900415 inc_rfc1001_len(work->response_buf, ((sizeof(struct smb2_hdr) - 4)
Namjae Jeone2f34482021-03-16 10:49:09 +0900416 + new_len - len));
417 rsp->NextCommand = cpu_to_le32(new_len);
418
419 work->next_smb2_rcv_hdr_off += next_hdr_offset;
420 work->next_smb2_rsp_hdr_off += new_len;
421 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900422 "Compound req new_len = %d rcv off = %d rsp off = %d\n",
423 new_len, work->next_smb2_rcv_hdr_off,
424 work->next_smb2_rsp_hdr_off);
Namjae Jeone2f34482021-03-16 10:49:09 +0900425
426 rsp_hdr = RESPONSE_BUF_NEXT(work);
427 rcv_hdr = REQUEST_BUF_NEXT(work);
428
429 if (!(rcv_hdr->Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
430 ksmbd_debug(SMB, "related flag should be set\n");
431 work->compound_fid = KSMBD_NO_FID;
432 work->compound_pfid = KSMBD_NO_FID;
433 }
434 memset((char *)rsp_hdr + 4, 0, sizeof(struct smb2_hdr) + 2);
435 rsp_hdr->ProtocolId = rcv_hdr->ProtocolId;
436 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
437 rsp_hdr->Command = rcv_hdr->Command;
438
439 /*
440 * Message is response. We don't grant oplock yet.
441 */
442 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR |
443 SMB2_FLAGS_RELATED_OPERATIONS);
444 rsp_hdr->NextCommand = 0;
445 rsp_hdr->MessageId = rcv_hdr->MessageId;
446 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
447 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
448 rsp_hdr->SessionId = rcv_hdr->SessionId;
449 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
450}
451
452/**
453 * is_chained_smb2_message() - check for chained command
454 * @work: smb work containing smb request buffer
455 *
456 * Return: true if chained request, otherwise false
457 */
458bool is_chained_smb2_message(struct ksmbd_work *work)
459{
Namjae Jeone5066492021-03-30 12:35:23 +0900460 struct smb2_hdr *hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900461 unsigned int len;
462
463 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
464 return false;
465
466 hdr = REQUEST_BUF_NEXT(work);
467 if (le32_to_cpu(hdr->NextCommand) > 0) {
468 ksmbd_debug(SMB, "got SMB2 chained command\n");
469 init_chained_smb2_rsp(work);
470 return true;
471 } else if (work->next_smb2_rcv_hdr_off) {
472 /*
473 * This is last request in chained command,
474 * align response to 8 byte
475 */
Namjae Jeone5066492021-03-30 12:35:23 +0900476 len = ALIGN(get_rfc1002_len(work->response_buf), 8);
477 len = len - get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900478 if (len) {
479 ksmbd_debug(SMB, "padding len %u\n", len);
Namjae Jeone5066492021-03-30 12:35:23 +0900480 inc_rfc1001_len(work->response_buf, len);
481 if (work->aux_payload_sz)
Namjae Jeone2f34482021-03-16 10:49:09 +0900482 work->aux_payload_sz += len;
483 }
484 }
485 return false;
486}
487
488/**
489 * init_smb2_rsp_hdr() - initialize smb2 response
490 * @work: smb work containing smb request buffer
491 *
492 * Return: 0
493 */
494int init_smb2_rsp_hdr(struct ksmbd_work *work)
495{
Namjae Jeone5066492021-03-30 12:35:23 +0900496 struct smb2_hdr *rsp_hdr = work->response_buf;
497 struct smb2_hdr *rcv_hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900498 struct ksmbd_conn *conn = work->conn;
499
500 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
501 rsp_hdr->smb2_buf_length = cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
502 rsp_hdr->ProtocolId = rcv_hdr->ProtocolId;
503 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
504 rsp_hdr->Command = rcv_hdr->Command;
505
506 /*
507 * Message is response. We don't grant oplock yet.
508 */
509 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
510 rsp_hdr->NextCommand = 0;
511 rsp_hdr->MessageId = rcv_hdr->MessageId;
512 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
513 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
514 rsp_hdr->SessionId = rcv_hdr->SessionId;
515 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
516
517 work->syncronous = true;
518 if (work->async_id) {
Namjae Jeond40012a2021-04-13 13:06:30 +0900519 ksmbd_release_id(&conn->async_ida, work->async_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900520 work->async_id = 0;
521 }
522
523 return 0;
524}
525
526/**
527 * smb2_allocate_rsp_buf() - allocate smb2 response buffer
528 * @work: smb work containing smb request buffer
529 *
530 * Return: 0 on success, otherwise -ENOMEM
531 */
532int smb2_allocate_rsp_buf(struct ksmbd_work *work)
533{
Namjae Jeone5066492021-03-30 12:35:23 +0900534 struct smb2_hdr *hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900535 size_t small_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
536 size_t large_sz = work->conn->vals->max_trans_size + MAX_SMB2_HDR_SIZE;
537 size_t sz = small_sz;
538 int cmd = le16_to_cpu(hdr->Command);
539
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900540 if (cmd == SMB2_IOCTL_HE || cmd == SMB2_QUERY_DIRECTORY_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900541 sz = large_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +0900542
543 if (cmd == SMB2_QUERY_INFO_HE) {
544 struct smb2_query_info_req *req;
545
Namjae Jeone5066492021-03-30 12:35:23 +0900546 req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900547 if (req->InfoType == SMB2_O_INFO_FILE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +0900548 (req->FileInfoClass == FILE_FULL_EA_INFORMATION ||
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900549 req->FileInfoClass == FILE_ALL_INFORMATION))
Namjae Jeone2f34482021-03-16 10:49:09 +0900550 sz = large_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +0900551 }
552
553 /* allocate large response buf for chained commands */
554 if (le32_to_cpu(hdr->NextCommand) > 0)
555 sz = large_sz;
556
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900557 work->response_buf = kvmalloc(sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeon63c454f2021-04-20 14:24:28 +0900558 if (!work->response_buf)
Namjae Jeone2f34482021-03-16 10:49:09 +0900559 return -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +0900560
561 work->response_sz = sz;
562 return 0;
563}
564
565/**
566 * smb2_check_user_session() - check for valid session for a user
567 * @work: smb work containing smb request buffer
568 *
569 * Return: 0 on success, otherwise error
570 */
571int smb2_check_user_session(struct ksmbd_work *work)
572{
Namjae Jeone5066492021-03-30 12:35:23 +0900573 struct smb2_hdr *req_hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900574 struct ksmbd_conn *conn = work->conn;
575 unsigned int cmd = conn->ops->get_cmd_val(work);
576 unsigned long long sess_id;
577
578 work->sess = NULL;
579 /*
580 * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not
581 * require a session id, so no need to validate user session's for
582 * these commands.
583 */
584 if (cmd == SMB2_ECHO_HE || cmd == SMB2_NEGOTIATE_HE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +0900585 cmd == SMB2_SESSION_SETUP_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900586 return 0;
587
588 if (!ksmbd_conn_good(work))
589 return -EINVAL;
590
591 sess_id = le64_to_cpu(req_hdr->SessionId);
592 /* Check for validity of user session */
Namjae Jeonf5a544e2021-06-18 10:04:19 +0900593 work->sess = ksmbd_session_lookup_all(conn, sess_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900594 if (work->sess)
595 return 1;
596 ksmbd_debug(SMB, "Invalid user session, Uid %llu\n", sess_id);
597 return -EINVAL;
598}
599
Namjae Jeon64b39f42021-03-30 14:25:35 +0900600static void destroy_previous_session(struct ksmbd_user *user, u64 id)
Namjae Jeone2f34482021-03-16 10:49:09 +0900601{
602 struct ksmbd_session *prev_sess = ksmbd_session_lookup_slowpath(id);
603 struct ksmbd_user *prev_user;
604
605 if (!prev_sess)
606 return;
607
608 prev_user = prev_sess->user;
609
Marios Makassikis1fca8032021-05-06 11:41:54 +0900610 if (!prev_user ||
611 strcmp(user->name, prev_user->name) ||
Namjae Jeone2f34482021-03-16 10:49:09 +0900612 user->passkey_sz != prev_user->passkey_sz ||
613 memcmp(user->passkey, prev_user->passkey, user->passkey_sz)) {
614 put_session(prev_sess);
615 return;
616 }
617
618 put_session(prev_sess);
619 ksmbd_session_destroy(prev_sess);
620}
621
622/**
623 * smb2_get_name() - get filename string from on the wire smb format
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900624 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +0900625 * @src: source buffer
626 * @maxlen: maxlen of source string
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900627 * @nls_table: nls_table pointer
Namjae Jeone2f34482021-03-16 10:49:09 +0900628 *
629 * Return: matching converted filename on success, otherwise error ptr
630 */
631static char *
Namjae Jeon64b39f42021-03-30 14:25:35 +0900632smb2_get_name(struct ksmbd_share_config *share, const char *src,
Namjae Jeon070fb212021-05-26 17:57:12 +0900633 const int maxlen, struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +0900634{
635 char *name, *unixname;
636
Namjae Jeon64b39f42021-03-30 14:25:35 +0900637 name = smb_strndup_from_utf16(src, maxlen, 1, local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +0900638 if (IS_ERR(name)) {
639 ksmbd_err("failed to get name %ld\n", PTR_ERR(name));
640 return name;
641 }
642
643 /* change it to absolute unix name */
644 ksmbd_conv_path_to_unix(name);
645 ksmbd_strip_last_slash(name);
646
647 unixname = convert_to_unix_name(share, name);
648 kfree(name);
649 if (!unixname) {
650 ksmbd_err("can not convert absolute name\n");
651 return ERR_PTR(-ENOMEM);
652 }
653
654 ksmbd_debug(SMB, "absolute name = %s\n", unixname);
655 return unixname;
656}
657
Namjae Jeone2f34482021-03-16 10:49:09 +0900658int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg)
659{
660 struct smb2_hdr *rsp_hdr;
661 struct ksmbd_conn *conn = work->conn;
662 int id;
663
Namjae Jeone5066492021-03-30 12:35:23 +0900664 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900665 rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND;
666
Namjae Jeond40012a2021-04-13 13:06:30 +0900667 id = ksmbd_acquire_async_msg_id(&conn->async_ida);
Namjae Jeone2f34482021-03-16 10:49:09 +0900668 if (id < 0) {
669 ksmbd_err("Failed to alloc async message id\n");
670 return id;
671 }
672 work->syncronous = false;
673 work->async_id = id;
674 rsp_hdr->Id.AsyncId = cpu_to_le64(id);
675
676 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900677 "Send interim Response to inform async request id : %d\n",
678 work->async_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900679
680 work->cancel_fn = fn;
681 work->cancel_argv = arg;
682
Namjae Jeon6c4e6752021-06-07 09:08:45 +0900683 if (list_empty(&work->async_request_entry)) {
684 spin_lock(&conn->request_lock);
685 list_add_tail(&work->async_request_entry, &conn->async_requests);
686 spin_unlock(&conn->request_lock);
687 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900688
689 return 0;
690}
691
692void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
693{
694 struct smb2_hdr *rsp_hdr;
695
Namjae Jeone5066492021-03-30 12:35:23 +0900696 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900697 smb2_set_err_rsp(work);
698 rsp_hdr->Status = status;
699
700 work->multiRsp = 1;
701 ksmbd_conn_write(work);
702 rsp_hdr->Status = 0;
703 work->multiRsp = 0;
704}
705
706static __le32 smb2_get_reparse_tag_special_file(umode_t mode)
707{
708 if (S_ISDIR(mode) || S_ISREG(mode))
709 return 0;
710
711 if (S_ISLNK(mode))
712 return IO_REPARSE_TAG_LX_SYMLINK_LE;
713 else if (S_ISFIFO(mode))
714 return IO_REPARSE_TAG_LX_FIFO_LE;
715 else if (S_ISSOCK(mode))
716 return IO_REPARSE_TAG_AF_UNIX_LE;
717 else if (S_ISCHR(mode))
718 return IO_REPARSE_TAG_LX_CHR_LE;
719 else if (S_ISBLK(mode))
720 return IO_REPARSE_TAG_LX_BLK_LE;
721
722 return 0;
723}
724
725/**
726 * smb2_get_dos_mode() - get file mode in dos format from unix mode
727 * @stat: kstat containing file mode
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900728 * @attribute: attribute flags
Namjae Jeone2f34482021-03-16 10:49:09 +0900729 *
730 * Return: converted dos mode
731 */
732static int smb2_get_dos_mode(struct kstat *stat, int attribute)
733{
734 int attr = 0;
735
Namjae Jeon64b39f42021-03-30 14:25:35 +0900736 if (S_ISDIR(stat->mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900737 attr = ATTR_DIRECTORY |
738 (attribute & (ATTR_HIDDEN | ATTR_SYSTEM));
Namjae Jeon64b39f42021-03-30 14:25:35 +0900739 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +0900740 attr = (attribute & 0x00005137) | ATTR_ARCHIVE;
741 attr &= ~(ATTR_DIRECTORY);
742 if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps &
743 FILE_SUPPORTS_SPARSE_FILES))
744 attr |= ATTR_SPARSE;
745
746 if (smb2_get_reparse_tag_special_file(stat->mode))
747 attr |= ATTR_REPARSE;
748 }
749
750 return attr;
751}
752
753static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900754 __le16 hash_id)
Namjae Jeone2f34482021-03-16 10:49:09 +0900755{
756 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
757 pneg_ctxt->DataLength = cpu_to_le16(38);
758 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
759 pneg_ctxt->Reserved = cpu_to_le32(0);
760 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
761 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
762 pneg_ctxt->HashAlgorithms = hash_id;
763}
764
765static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900766 __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +0900767{
768 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
769 pneg_ctxt->DataLength = cpu_to_le16(4);
770 pneg_ctxt->Reserved = cpu_to_le32(0);
771 pneg_ctxt->CipherCount = cpu_to_le16(1);
772 pneg_ctxt->Ciphers[0] = cipher_type;
773}
774
775static void build_compression_ctxt(struct smb2_compression_ctx *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900776 __le16 comp_algo)
Namjae Jeone2f34482021-03-16 10:49:09 +0900777{
778 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
779 pneg_ctxt->DataLength =
780 cpu_to_le16(sizeof(struct smb2_compression_ctx)
781 - sizeof(struct smb2_neg_context));
782 pneg_ctxt->Reserved = cpu_to_le32(0);
783 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(1);
784 pneg_ctxt->Reserved1 = cpu_to_le32(0);
785 pneg_ctxt->CompressionAlgorithms[0] = comp_algo;
786}
787
Namjae Jeon64b39f42021-03-30 14:25:35 +0900788static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900789{
790 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
791 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
792 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
793 pneg_ctxt->Name[0] = 0x93;
794 pneg_ctxt->Name[1] = 0xAD;
795 pneg_ctxt->Name[2] = 0x25;
796 pneg_ctxt->Name[3] = 0x50;
797 pneg_ctxt->Name[4] = 0x9C;
798 pneg_ctxt->Name[5] = 0xB4;
799 pneg_ctxt->Name[6] = 0x11;
800 pneg_ctxt->Name[7] = 0xE7;
801 pneg_ctxt->Name[8] = 0xB4;
802 pneg_ctxt->Name[9] = 0x23;
803 pneg_ctxt->Name[10] = 0x83;
804 pneg_ctxt->Name[11] = 0xDE;
805 pneg_ctxt->Name[12] = 0x96;
806 pneg_ctxt->Name[13] = 0x8B;
807 pneg_ctxt->Name[14] = 0xCD;
808 pneg_ctxt->Name[15] = 0x7C;
809}
810
Namjae Jeon64b39f42021-03-30 14:25:35 +0900811static void assemble_neg_contexts(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900812 struct smb2_negotiate_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +0900813{
814 /* +4 is to account for the RFC1001 len field */
815 char *pneg_ctxt = (char *)rsp +
816 le32_to_cpu(rsp->NegotiateContextOffset) + 4;
817 int neg_ctxt_cnt = 1;
818 int ctxt_size;
819
820 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900821 "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900822 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900823 conn->preauth_info->Preauth_HashId);
Namjae Jeone2f34482021-03-16 10:49:09 +0900824 rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt);
825 inc_rfc1001_len(rsp, AUTH_GSS_PADDING);
826 ctxt_size = sizeof(struct smb2_preauth_neg_context);
827 /* Round to 8 byte boundary */
828 pneg_ctxt += round_up(sizeof(struct smb2_preauth_neg_context), 8);
829
830 if (conn->cipher_type) {
831 ctxt_size = round_up(ctxt_size, 8);
832 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900833 "assemble SMB2_ENCRYPTION_CAPABILITIES context\n");
Namjae Jeon64b39f42021-03-30 14:25:35 +0900834 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900835 conn->cipher_type);
Namjae Jeone2f34482021-03-16 10:49:09 +0900836 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
837 ctxt_size += sizeof(struct smb2_encryption_neg_context);
838 /* Round to 8 byte boundary */
839 pneg_ctxt +=
840 round_up(sizeof(struct smb2_encryption_neg_context),
841 8);
842 }
843
844 if (conn->compress_algorithm) {
845 ctxt_size = round_up(ctxt_size, 8);
846 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900847 "assemble SMB2_COMPRESSION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900848 /* Temporarily set to SMB3_COMPRESS_NONE */
849 build_compression_ctxt((struct smb2_compression_ctx *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900850 conn->compress_algorithm);
Namjae Jeone2f34482021-03-16 10:49:09 +0900851 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
852 ctxt_size += sizeof(struct smb2_compression_ctx);
853 /* Round to 8 byte boundary */
854 pneg_ctxt += round_up(sizeof(struct smb2_compression_ctx), 8);
855 }
856
857 if (conn->posix_ext_supported) {
858 ctxt_size = round_up(ctxt_size, 8);
859 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900860 "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900861 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
862 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
863 ctxt_size += sizeof(struct smb2_posix_neg_context);
864 }
865
866 inc_rfc1001_len(rsp, ctxt_size);
867}
868
Namjae Jeon64b39f42021-03-30 14:25:35 +0900869static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900870 struct smb2_preauth_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900871{
872 __le32 err = STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP;
873
Namjae Jeon070fb212021-05-26 17:57:12 +0900874 if (pneg_ctxt->HashAlgorithms == SMB2_PREAUTH_INTEGRITY_SHA512) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900875 conn->preauth_info->Preauth_HashId =
876 SMB2_PREAUTH_INTEGRITY_SHA512;
877 err = STATUS_SUCCESS;
878 }
879
880 return err;
881}
882
883static int decode_encrypt_ctxt(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900884 struct smb2_encryption_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900885{
886 int i;
887 int cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount);
888
889 conn->cipher_type = 0;
890
891 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION))
892 goto out;
893
894 for (i = 0; i < cph_cnt; i++) {
895 if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM ||
Namjae Jeon5a0ca772021-05-06 11:43:37 +0900896 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM ||
897 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM ||
898 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900899 ksmbd_debug(SMB, "Cipher ID = 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +0900900 pneg_ctxt->Ciphers[i]);
Namjae Jeone2f34482021-03-16 10:49:09 +0900901 conn->cipher_type = pneg_ctxt->Ciphers[i];
902 break;
903 }
904 }
905
906out:
907 /*
908 * Return encrypt context size in request.
909 * So need to plus extra number of ciphers size.
910 */
911 return sizeof(struct smb2_encryption_neg_context) +
912 ((cph_cnt - 1) * 2);
913}
914
915static int decode_compress_ctxt(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900916 struct smb2_compression_ctx *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900917{
918 int algo_cnt = le16_to_cpu(pneg_ctxt->CompressionAlgorithmCount);
919
920 conn->compress_algorithm = SMB3_COMPRESS_NONE;
921
922 /*
923 * Return compression context size in request.
924 * So need to plus extra number of CompressionAlgorithms size.
925 */
926 return sizeof(struct smb2_encryption_neg_context) +
927 ((algo_cnt - 1) * 2);
928}
929
930static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900931 struct smb2_negotiate_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +0900932{
933 int i = 0;
934 __le32 status = 0;
935 /* +4 is to account for the RFC1001 len field */
936 char *pneg_ctxt = (char *)req +
937 le32_to_cpu(req->NegotiateContextOffset) + 4;
938 __le16 *ContextType = (__le16 *)pneg_ctxt;
939 int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount);
940 int ctxt_size;
941
942 ksmbd_debug(SMB, "negotiate context count = %d\n", neg_ctxt_cnt);
943 status = STATUS_INVALID_PARAMETER;
944 while (i++ < neg_ctxt_cnt) {
945 if (*ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) {
946 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900947 "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900948 if (conn->preauth_info->Preauth_HashId)
949 break;
950
951 status = decode_preauth_ctxt(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900952 (struct smb2_preauth_neg_context *)pneg_ctxt);
Namjae Jeon64b39f42021-03-30 14:25:35 +0900953 pneg_ctxt += DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
Namjae Jeone2f34482021-03-16 10:49:09 +0900954 } else if (*ContextType == SMB2_ENCRYPTION_CAPABILITIES) {
955 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900956 "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900957 if (conn->cipher_type)
958 break;
959
960 ctxt_size = decode_encrypt_ctxt(conn,
Namjae Jeon64b39f42021-03-30 14:25:35 +0900961 (struct smb2_encryption_neg_context *)pneg_ctxt);
Namjae Jeone2f34482021-03-16 10:49:09 +0900962 pneg_ctxt += DIV_ROUND_UP(ctxt_size, 8) * 8;
963 } else if (*ContextType == SMB2_COMPRESSION_CAPABILITIES) {
964 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900965 "deassemble SMB2_COMPRESSION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900966 if (conn->compress_algorithm)
967 break;
968
969 ctxt_size = decode_compress_ctxt(conn,
Namjae Jeon10268f72021-05-26 16:44:21 +0900970 (struct smb2_compression_ctx *)pneg_ctxt);
Namjae Jeone2f34482021-03-16 10:49:09 +0900971 pneg_ctxt += DIV_ROUND_UP(ctxt_size, 8) * 8;
972 } else if (*ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) {
973 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900974 "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900975 ctxt_size = sizeof(struct smb2_netname_neg_context);
Namjae Jeon64b39f42021-03-30 14:25:35 +0900976 ctxt_size += DIV_ROUND_UP(le16_to_cpu(((struct smb2_netname_neg_context *)
Namjae Jeon070fb212021-05-26 17:57:12 +0900977 pneg_ctxt)->DataLength), 8) * 8;
Namjae Jeone2f34482021-03-16 10:49:09 +0900978 pneg_ctxt += ctxt_size;
979 } else if (*ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) {
980 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900981 "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900982 conn->posix_ext_supported = true;
Namjae Jeon64b39f42021-03-30 14:25:35 +0900983 pneg_ctxt += DIV_ROUND_UP(sizeof(struct smb2_posix_neg_context), 8) * 8;
Namjae Jeone2f34482021-03-16 10:49:09 +0900984 }
985 ContextType = (__le16 *)pneg_ctxt;
986
987 if (status != STATUS_SUCCESS)
988 break;
989 }
990 return status;
991}
992
993/**
994 * smb2_handle_negotiate() - handler for smb2 negotiate command
995 * @work: smb work containing smb request buffer
996 *
997 * Return: 0
998 */
999int smb2_handle_negotiate(struct ksmbd_work *work)
1000{
1001 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09001002 struct smb2_negotiate_req *req = work->request_buf;
1003 struct smb2_negotiate_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001004 int rc = 0;
1005 __le32 status;
1006
1007 ksmbd_debug(SMB, "Received negotiate request\n");
1008 conn->need_neg = false;
1009 if (ksmbd_conn_good(work)) {
1010 ksmbd_err("conn->tcp_status is already in CifsGood State\n");
1011 work->send_no_response = 1;
1012 return rc;
1013 }
1014
1015 if (req->DialectCount == 0) {
1016 ksmbd_err("malformed packet\n");
1017 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1018 rc = -EINVAL;
1019 goto err_out;
1020 }
1021
1022 conn->cli_cap = le32_to_cpu(req->Capabilities);
1023 switch (conn->dialect) {
1024 case SMB311_PROT_ID:
1025 conn->preauth_info =
1026 kzalloc(sizeof(struct preauth_integrity_info),
Namjae Jeon070fb212021-05-26 17:57:12 +09001027 GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09001028 if (!conn->preauth_info) {
1029 rc = -ENOMEM;
1030 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1031 goto err_out;
1032 }
1033
1034 status = deassemble_neg_contexts(conn, req);
1035 if (status != STATUS_SUCCESS) {
1036 ksmbd_err("deassemble_neg_contexts error(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001037 status);
Namjae Jeone2f34482021-03-16 10:49:09 +09001038 rsp->hdr.Status = status;
1039 rc = -EINVAL;
1040 goto err_out;
1041 }
1042
1043 rc = init_smb3_11_server(conn);
1044 if (rc < 0) {
1045 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1046 goto err_out;
1047 }
1048
1049 ksmbd_gen_preauth_integrity_hash(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001050 work->request_buf,
1051 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001052 rsp->NegotiateContextOffset =
1053 cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
1054 assemble_neg_contexts(conn, rsp);
1055 break;
1056 case SMB302_PROT_ID:
1057 init_smb3_02_server(conn);
1058 break;
1059 case SMB30_PROT_ID:
1060 init_smb3_0_server(conn);
1061 break;
1062 case SMB21_PROT_ID:
1063 init_smb2_1_server(conn);
1064 break;
1065 case SMB20_PROT_ID:
1066 rc = init_smb2_0_server(conn);
1067 if (rc) {
1068 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
1069 goto err_out;
1070 }
1071 break;
1072 case SMB2X_PROT_ID:
1073 case BAD_PROT_ID:
1074 default:
1075 ksmbd_debug(SMB, "Server dialect :0x%x not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001076 conn->dialect);
Namjae Jeone2f34482021-03-16 10:49:09 +09001077 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
1078 rc = -EINVAL;
1079 goto err_out;
1080 }
1081 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
1082
1083 /* For stats */
1084 conn->connection_type = conn->dialect;
1085
1086 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
1087 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
1088 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
1089
1090 if (conn->dialect > SMB20_PROT_ID) {
1091 memcpy(conn->ClientGUID, req->ClientGUID,
Namjae Jeon070fb212021-05-26 17:57:12 +09001092 SMB2_CLIENT_GUID_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09001093 conn->cli_sec_mode = le16_to_cpu(req->SecurityMode);
1094 }
1095
1096 rsp->StructureSize = cpu_to_le16(65);
1097 rsp->DialectRevision = cpu_to_le16(conn->dialect);
1098 /* Not setting conn guid rsp->ServerGUID, as it
1099 * not used by client for identifying server
1100 */
1101 memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE);
1102
1103 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
1104 rsp->ServerStartTime = 0;
1105 ksmbd_debug(SMB, "negotiate context offset %d, count %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001106 le32_to_cpu(rsp->NegotiateContextOffset),
1107 le16_to_cpu(rsp->NegotiateContextCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09001108
1109 rsp->SecurityBufferOffset = cpu_to_le16(128);
1110 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
1111 ksmbd_copy_gss_neg_header(((char *)(&rsp->hdr) +
Namjae Jeon070fb212021-05-26 17:57:12 +09001112 sizeof(rsp->hdr.smb2_buf_length)) +
1113 le16_to_cpu(rsp->SecurityBufferOffset));
Namjae Jeone2f34482021-03-16 10:49:09 +09001114 inc_rfc1001_len(rsp, sizeof(struct smb2_negotiate_rsp) -
Namjae Jeon070fb212021-05-26 17:57:12 +09001115 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
1116 AUTH_GSS_LENGTH);
Namjae Jeone2f34482021-03-16 10:49:09 +09001117 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
1118 conn->use_spnego = true;
1119
1120 if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001121 server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) &&
1122 req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09001123 conn->sign = true;
1124 else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) {
1125 server_conf.enforced_signing = true;
1126 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
1127 conn->sign = true;
1128 }
1129
1130 conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode);
1131 ksmbd_conn_set_need_negotiate(work);
1132
1133err_out:
1134 if (rc < 0)
1135 smb2_set_err_rsp(work);
1136
1137 return rc;
1138}
1139
1140static int alloc_preauth_hash(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09001141 struct ksmbd_conn *conn)
Namjae Jeone2f34482021-03-16 10:49:09 +09001142{
1143 if (sess->Preauth_HashValue)
1144 return 0;
1145
kernel test robot86f52972021-04-02 12:17:24 +09001146 sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue,
Namjae Jeon070fb212021-05-26 17:57:12 +09001147 PREAUTH_HASHVALUE_SIZE, GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09001148 if (!sess->Preauth_HashValue)
1149 return -ENOMEM;
1150
Namjae Jeone2f34482021-03-16 10:49:09 +09001151 return 0;
1152}
1153
1154static int generate_preauth_hash(struct ksmbd_work *work)
1155{
1156 struct ksmbd_conn *conn = work->conn;
1157 struct ksmbd_session *sess = work->sess;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001158 u8 *preauth_hash;
Namjae Jeone2f34482021-03-16 10:49:09 +09001159
1160 if (conn->dialect != SMB311_PROT_ID)
1161 return 0;
1162
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001163 if (conn->binding) {
1164 struct preauth_session *preauth_sess;
1165
1166 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
1167 if (!preauth_sess) {
1168 preauth_sess = ksmbd_preauth_session_alloc(conn, sess->id);
1169 if (!preauth_sess)
1170 return -ENOMEM;
1171 }
1172
1173 preauth_hash = preauth_sess->Preauth_HashValue;
1174 } else {
1175 if (!sess->Preauth_HashValue)
1176 if (alloc_preauth_hash(sess, conn))
1177 return -ENOMEM;
1178 preauth_hash = sess->Preauth_HashValue;
Namjae Jeone2f34482021-03-16 10:49:09 +09001179 }
1180
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001181 ksmbd_gen_preauth_integrity_hash(conn, work->request_buf, preauth_hash);
Namjae Jeone2f34482021-03-16 10:49:09 +09001182 return 0;
1183}
1184
1185static int decode_negotiation_token(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09001186 struct negotiate_message *negblob)
Namjae Jeone2f34482021-03-16 10:49:09 +09001187{
1188 struct ksmbd_conn *conn = work->conn;
1189 struct smb2_sess_setup_req *req;
1190 int sz;
1191
1192 if (!conn->use_spnego)
1193 return -EINVAL;
1194
Namjae Jeone5066492021-03-30 12:35:23 +09001195 req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001196 sz = le16_to_cpu(req->SecurityBufferLength);
1197
Hyunchul Leefad41612021-04-19 17:26:15 +09001198 if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) {
1199 if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001200 conn->auth_mechs |= KSMBD_AUTH_NTLMSSP;
1201 conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP;
1202 conn->use_spnego = false;
1203 }
1204 }
1205 return 0;
1206}
1207
1208static int ntlm_negotiate(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09001209 struct negotiate_message *negblob)
Namjae Jeone2f34482021-03-16 10:49:09 +09001210{
Namjae Jeone5066492021-03-30 12:35:23 +09001211 struct smb2_sess_setup_req *req = work->request_buf;
1212 struct smb2_sess_setup_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001213 struct challenge_message *chgblob;
1214 unsigned char *spnego_blob = NULL;
1215 u16 spnego_blob_len;
1216 char *neg_blob;
1217 int sz, rc;
1218
1219 ksmbd_debug(SMB, "negotiate phase\n");
1220 sz = le16_to_cpu(req->SecurityBufferLength);
1221 rc = ksmbd_decode_ntlmssp_neg_blob(negblob, sz, work->sess);
1222 if (rc)
1223 return rc;
1224
1225 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1226 chgblob =
1227 (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz);
1228 memset(chgblob, 0, sizeof(struct challenge_message));
1229
1230 if (!work->conn->use_spnego) {
1231 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess);
1232 if (sz < 0)
1233 return -ENOMEM;
1234
1235 rsp->SecurityBufferLength = cpu_to_le16(sz);
1236 return 0;
1237 }
1238
1239 sz = sizeof(struct challenge_message);
1240 sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6;
1241
1242 neg_blob = kzalloc(sz, GFP_KERNEL);
1243 if (!neg_blob)
1244 return -ENOMEM;
1245
1246 chgblob = (struct challenge_message *)neg_blob;
1247 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess);
1248 if (sz < 0) {
1249 rc = -ENOMEM;
1250 goto out;
1251 }
1252
Namjae Jeon070fb212021-05-26 17:57:12 +09001253 rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len,
1254 neg_blob, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09001255 if (rc) {
1256 rc = -ENOMEM;
1257 goto out;
1258 }
1259
1260 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1261 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
1262 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1263
1264out:
1265 kfree(spnego_blob);
1266 kfree(neg_blob);
1267 return rc;
1268}
1269
1270static struct authenticate_message *user_authblob(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001271 struct smb2_sess_setup_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +09001272{
1273 int sz;
1274
1275 if (conn->use_spnego && conn->mechToken)
1276 return (struct authenticate_message *)conn->mechToken;
1277
1278 sz = le16_to_cpu(req->SecurityBufferOffset);
1279 return (struct authenticate_message *)((char *)&req->hdr.ProtocolId
1280 + sz);
1281}
1282
1283static struct ksmbd_user *session_user(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001284 struct smb2_sess_setup_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +09001285{
1286 struct authenticate_message *authblob;
1287 struct ksmbd_user *user;
1288 char *name;
1289 int sz;
1290
1291 authblob = user_authblob(conn, req);
1292 sz = le32_to_cpu(authblob->UserName.BufferOffset);
1293 name = smb_strndup_from_utf16((const char *)authblob + sz,
1294 le16_to_cpu(authblob->UserName.Length),
1295 true,
1296 conn->local_nls);
1297 if (IS_ERR(name)) {
1298 ksmbd_err("cannot allocate memory\n");
1299 return NULL;
1300 }
1301
1302 ksmbd_debug(SMB, "session setup request for user %s\n", name);
1303 user = ksmbd_login_user(name);
1304 kfree(name);
1305 return user;
1306}
1307
1308static int ntlm_authenticate(struct ksmbd_work *work)
1309{
Namjae Jeone5066492021-03-30 12:35:23 +09001310 struct smb2_sess_setup_req *req = work->request_buf;
1311 struct smb2_sess_setup_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001312 struct ksmbd_conn *conn = work->conn;
1313 struct ksmbd_session *sess = work->sess;
1314 struct channel *chann = NULL;
1315 struct ksmbd_user *user;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001316 u64 prev_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09001317 int sz, rc;
1318
1319 ksmbd_debug(SMB, "authenticate phase\n");
1320 if (conn->use_spnego) {
1321 unsigned char *spnego_blob;
1322 u16 spnego_blob_len;
1323
1324 rc = build_spnego_ntlmssp_auth_blob(&spnego_blob,
1325 &spnego_blob_len,
1326 0);
1327 if (rc)
1328 return -ENOMEM;
1329
1330 sz = le16_to_cpu(rsp->SecurityBufferOffset);
Namjae Jeon64b39f42021-03-30 14:25:35 +09001331 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001332 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1333 kfree(spnego_blob);
1334 inc_rfc1001_len(rsp, spnego_blob_len - 1);
1335 }
1336
1337 user = session_user(conn, req);
1338 if (!user) {
1339 ksmbd_debug(SMB, "Unknown user name or an error\n");
1340 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1341 return -EINVAL;
1342 }
1343
1344 /* Check for previous session */
1345 prev_id = le64_to_cpu(req->PreviousSessionId);
1346 if (prev_id && prev_id != sess->id)
1347 destroy_previous_session(user, prev_id);
1348
1349 if (sess->state == SMB2_SESSION_VALID) {
1350 /*
1351 * Reuse session if anonymous try to connect
1352 * on reauthetication.
1353 */
1354 if (ksmbd_anonymous_user(user)) {
1355 ksmbd_free_user(user);
1356 return 0;
1357 }
1358 ksmbd_free_user(sess->user);
1359 }
1360
1361 sess->user = user;
1362 if (user_guest(sess->user)) {
1363 if (conn->sign) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001364 ksmbd_debug(SMB, "Guest login not allowed when signing enabled\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001365 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1366 return -EACCES;
1367 }
1368
1369 rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE;
1370 } else {
1371 struct authenticate_message *authblob;
1372
1373 authblob = user_authblob(conn, req);
1374 sz = le16_to_cpu(req->SecurityBufferLength);
1375 rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, sess);
1376 if (rc) {
1377 set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD);
1378 ksmbd_debug(SMB, "authentication failed\n");
1379 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1380 return -EINVAL;
1381 }
1382
1383 /*
1384 * If session state is SMB2_SESSION_VALID, We can assume
1385 * that it is reauthentication. And the user/password
1386 * has been verified, so return it here.
1387 */
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001388 if (sess->state == SMB2_SESSION_VALID) {
1389 if (conn->binding)
1390 goto binding_session;
Namjae Jeone2f34482021-03-16 10:49:09 +09001391 return 0;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001392 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001393
1394 if ((conn->sign || server_conf.enforced_signing) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001395 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
Namjae Jeone2f34482021-03-16 10:49:09 +09001396 sess->sign = true;
1397
1398 if (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION &&
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001399 conn->ops->generate_encryptionkey &&
1400 !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001401 rc = conn->ops->generate_encryptionkey(sess);
1402 if (rc) {
1403 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001404 "SMB3 encryption key generation failed\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001405 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1406 return rc;
1407 }
1408 sess->enc = true;
1409 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1410 /*
1411 * signing is disable if encryption is enable
1412 * on this session
1413 */
1414 sess->sign = false;
1415 }
1416 }
1417
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001418binding_session:
Namjae Jeone2f34482021-03-16 10:49:09 +09001419 if (conn->dialect >= SMB30_PROT_ID) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001420 chann = lookup_chann_list(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001421 if (!chann) {
1422 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1423 if (!chann)
1424 return -ENOMEM;
1425
1426 chann->conn = conn;
1427 INIT_LIST_HEAD(&chann->chann_list);
1428 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1429 }
1430 }
1431
1432 if (conn->ops->generate_signingkey) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001433 rc = conn->ops->generate_signingkey(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001434 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001435 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001436 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1437 return rc;
1438 }
1439 }
1440
1441 if (conn->dialect > SMB20_PROT_ID) {
1442 if (!ksmbd_conn_lookup_dialect(conn)) {
1443 ksmbd_err("fail to verify the dialect\n");
1444 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1445 return -EPERM;
1446 }
1447 }
1448 return 0;
1449}
1450
1451#ifdef CONFIG_SMB_SERVER_KERBEROS5
1452static int krb5_authenticate(struct ksmbd_work *work)
1453{
Namjae Jeone5066492021-03-30 12:35:23 +09001454 struct smb2_sess_setup_req *req = work->request_buf;
1455 struct smb2_sess_setup_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001456 struct ksmbd_conn *conn = work->conn;
1457 struct ksmbd_session *sess = work->sess;
1458 char *in_blob, *out_blob;
1459 struct channel *chann = NULL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001460 u64 prev_sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09001461 int in_len, out_len;
1462 int retval;
1463
1464 in_blob = (char *)&req->hdr.ProtocolId +
1465 le16_to_cpu(req->SecurityBufferOffset);
1466 in_len = le16_to_cpu(req->SecurityBufferLength);
1467 out_blob = (char *)&rsp->hdr.ProtocolId +
1468 le16_to_cpu(rsp->SecurityBufferOffset);
1469 out_len = work->response_sz -
1470 offsetof(struct smb2_hdr, smb2_buf_length) -
1471 le16_to_cpu(rsp->SecurityBufferOffset);
1472
1473 /* Check previous session */
1474 prev_sess_id = le64_to_cpu(req->PreviousSessionId);
1475 if (prev_sess_id && prev_sess_id != sess->id)
1476 destroy_previous_session(sess->user, prev_sess_id);
1477
1478 if (sess->state == SMB2_SESSION_VALID)
1479 ksmbd_free_user(sess->user);
1480
1481 retval = ksmbd_krb5_authenticate(sess, in_blob, in_len,
Namjae Jeon070fb212021-05-26 17:57:12 +09001482 out_blob, &out_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001483 if (retval) {
1484 ksmbd_debug(SMB, "krb5 authentication failed\n");
1485 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1486 return retval;
1487 }
1488 rsp->SecurityBufferLength = cpu_to_le16(out_len);
1489 inc_rfc1001_len(rsp, out_len - 1);
1490
1491 if ((conn->sign || server_conf.enforced_signing) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001492 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
Namjae Jeone2f34482021-03-16 10:49:09 +09001493 sess->sign = true;
1494
1495 if ((conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09001496 conn->ops->generate_encryptionkey) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001497 retval = conn->ops->generate_encryptionkey(sess);
1498 if (retval) {
1499 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001500 "SMB3 encryption key generation failed\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001501 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1502 return retval;
1503 }
1504 sess->enc = true;
1505 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1506 sess->sign = false;
1507 }
1508
1509 if (conn->dialect >= SMB30_PROT_ID) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001510 chann = lookup_chann_list(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001511 if (!chann) {
1512 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1513 if (!chann)
1514 return -ENOMEM;
1515
1516 chann->conn = conn;
1517 INIT_LIST_HEAD(&chann->chann_list);
1518 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1519 }
1520 }
1521
1522 if (conn->ops->generate_signingkey) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001523 retval = conn->ops->generate_signingkey(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001524 if (retval) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001525 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001526 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1527 return retval;
1528 }
1529 }
1530
1531 if (conn->dialect > SMB20_PROT_ID) {
1532 if (!ksmbd_conn_lookup_dialect(conn)) {
1533 ksmbd_err("fail to verify the dialect\n");
1534 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1535 return -EPERM;
1536 }
1537 }
1538 return 0;
1539}
1540#else
1541static int krb5_authenticate(struct ksmbd_work *work)
1542{
1543 return -EOPNOTSUPP;
1544}
1545#endif
1546
1547int smb2_sess_setup(struct ksmbd_work *work)
1548{
1549 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09001550 struct smb2_sess_setup_req *req = work->request_buf;
1551 struct smb2_sess_setup_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001552 struct ksmbd_session *sess;
1553 struct negotiate_message *negblob;
1554 int rc = 0;
1555
1556 ksmbd_debug(SMB, "Received request for session setup\n");
1557
1558 rsp->StructureSize = cpu_to_le16(9);
1559 rsp->SessionFlags = 0;
1560 rsp->SecurityBufferOffset = cpu_to_le16(72);
1561 rsp->SecurityBufferLength = 0;
1562 inc_rfc1001_len(rsp, 9);
1563
1564 if (!req->hdr.SessionId) {
1565 sess = ksmbd_smb2_session_create();
1566 if (!sess) {
1567 rc = -ENOMEM;
1568 goto out_err;
1569 }
1570 rsp->hdr.SessionId = cpu_to_le64(sess->id);
1571 ksmbd_session_register(conn, sess);
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001572 } else if (conn->dialect >= SMB30_PROT_ID &&
1573 (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
1574 req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) {
1575 u64 sess_id = le64_to_cpu(req->hdr.SessionId);
1576
1577 sess = ksmbd_session_lookup_slowpath(sess_id);
1578 if (!sess) {
1579 rc = -ENOENT;
1580 goto out_err;
1581 }
1582
1583 if (conn->dialect != sess->conn->dialect) {
1584 rc = -EINVAL;
1585 goto out_err;
1586 }
1587
1588 if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) {
1589 rc = -EINVAL;
1590 goto out_err;
1591 }
1592
1593 if (strncmp(conn->ClientGUID, sess->conn->ClientGUID,
1594 SMB2_CLIENT_GUID_SIZE)) {
1595 rc = -ENOENT;
1596 goto out_err;
1597 }
1598
1599 if (sess->state == SMB2_SESSION_IN_PROGRESS) {
1600 rc = -EACCES;
1601 goto out_err;
1602 }
1603
1604 if (sess->state == SMB2_SESSION_EXPIRED) {
1605 rc = -EFAULT;
1606 goto out_err;
1607 }
1608
1609 if (ksmbd_session_lookup(conn, sess_id)) {
1610 rc = -EACCES;
1611 goto out_err;
1612 }
1613
1614 conn->binding = true;
1615 } else if ((conn->dialect < SMB30_PROT_ID ||
1616 server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
1617 (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
1618 rc = -EACCES;
1619 goto out_err;
Namjae Jeone2f34482021-03-16 10:49:09 +09001620 } else {
1621 sess = ksmbd_session_lookup(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001622 le64_to_cpu(req->hdr.SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09001623 if (!sess) {
1624 rc = -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001625 goto out_err;
1626 }
1627 }
1628 work->sess = sess;
1629
1630 if (sess->state == SMB2_SESSION_EXPIRED)
1631 sess->state = SMB2_SESSION_IN_PROGRESS;
1632
1633 negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId +
1634 le16_to_cpu(req->SecurityBufferOffset));
1635
1636 if (decode_negotiation_token(work, negblob) == 0) {
1637 if (conn->mechToken)
1638 negblob = (struct negotiate_message *)conn->mechToken;
1639 }
1640
1641 if (server_conf.auth_mechs & conn->auth_mechs) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001642 rc = generate_preauth_hash(work);
1643 if (rc)
1644 goto out_err;
1645
Namjae Jeone2f34482021-03-16 10:49:09 +09001646 if (conn->preferred_auth_mech &
1647 (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001648 rc = krb5_authenticate(work);
1649 if (rc) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001650 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001651 goto out_err;
1652 }
1653
1654 ksmbd_conn_set_good(work);
1655 sess->state = SMB2_SESSION_VALID;
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001656 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001657 sess->Preauth_HashValue = NULL;
1658 } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001659 if (negblob->MessageType == NtLmNegotiate) {
1660 rc = ntlm_negotiate(work, negblob);
1661 if (rc)
1662 goto out_err;
1663 rsp->hdr.Status =
1664 STATUS_MORE_PROCESSING_REQUIRED;
1665 /*
1666 * Note: here total size -1 is done as an
1667 * adjustment for 0 size blob
1668 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09001669 inc_rfc1001_len(rsp, le16_to_cpu(rsp->SecurityBufferLength) - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001670
1671 } else if (negblob->MessageType == NtLmAuthenticate) {
1672 rc = ntlm_authenticate(work);
1673 if (rc)
1674 goto out_err;
1675
1676 ksmbd_conn_set_good(work);
1677 sess->state = SMB2_SESSION_VALID;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001678 if (conn->binding) {
1679 struct preauth_session *preauth_sess;
1680
1681 preauth_sess =
1682 ksmbd_preauth_session_lookup(conn, sess->id);
1683 if (preauth_sess) {
1684 list_del(&preauth_sess->preauth_entry);
1685 kfree(preauth_sess);
1686 }
1687 }
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001688 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001689 sess->Preauth_HashValue = NULL;
1690 }
1691 } else {
1692 /* TODO: need one more negotiation */
1693 ksmbd_err("Not support the preferred authentication\n");
1694 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001695 }
1696 } else {
1697 ksmbd_err("Not support authentication\n");
1698 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001699 }
1700
1701out_err:
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001702 if (rc == -EINVAL)
1703 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1704 else if (rc == -ENOENT)
1705 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1706 else if (rc == -EACCES)
1707 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
1708 else if (rc == -EFAULT)
1709 rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED;
1710 else if (rc)
1711 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1712
Namjae Jeone2f34482021-03-16 10:49:09 +09001713 if (conn->use_spnego && conn->mechToken) {
1714 kfree(conn->mechToken);
1715 conn->mechToken = NULL;
1716 }
1717
1718 if (rc < 0 && sess) {
1719 ksmbd_session_destroy(sess);
1720 work->sess = NULL;
1721 }
1722
1723 return rc;
1724}
1725
1726/**
1727 * smb2_tree_connect() - handler for smb2 tree connect command
1728 * @work: smb work containing smb request buffer
1729 *
1730 * Return: 0 on success, otherwise error
1731 */
1732int smb2_tree_connect(struct ksmbd_work *work)
1733{
1734 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09001735 struct smb2_tree_connect_req *req = work->request_buf;
1736 struct smb2_tree_connect_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001737 struct ksmbd_session *sess = work->sess;
1738 char *treename = NULL, *name = NULL;
1739 struct ksmbd_tree_conn_status status;
1740 struct ksmbd_share_config *share;
1741 int rc = -EINVAL;
1742
1743 treename = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09001744 le16_to_cpu(req->PathLength), true,
1745 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09001746 if (IS_ERR(treename)) {
1747 ksmbd_err("treename is NULL\n");
1748 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1749 goto out_err1;
1750 }
1751
Stephen Rothwell36ba3862021-03-17 17:01:15 +09001752 name = ksmbd_extract_sharename(treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001753 if (IS_ERR(name)) {
1754 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1755 goto out_err1;
1756 }
1757
1758 ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001759 name, treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001760
1761 status = ksmbd_tree_conn_connect(sess, name);
1762 if (status.ret == KSMBD_TREE_CONN_STATUS_OK)
1763 rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id);
1764 else
1765 goto out_err1;
1766
1767 share = status.tree_conn->share_conf;
1768 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
1769 ksmbd_debug(SMB, "IPC share path request\n");
1770 rsp->ShareType = SMB2_SHARE_TYPE_PIPE;
1771 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1772 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE |
1773 FILE_DELETE_LE | FILE_READ_CONTROL_LE |
1774 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1775 FILE_SYNCHRONIZE_LE;
1776 } else {
1777 rsp->ShareType = SMB2_SHARE_TYPE_DISK;
1778 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1779 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE;
1780 if (test_tree_conn_flag(status.tree_conn,
1781 KSMBD_TREE_CONN_FLAG_WRITABLE)) {
1782 rsp->MaximalAccess |= FILE_WRITE_DATA_LE |
1783 FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE |
Wan Jiabing3aefd542021-06-07 12:54:32 +08001784 FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE |
1785 FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE |
1786 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1787 FILE_SYNCHRONIZE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09001788 }
1789 }
1790
1791 status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1792 if (conn->posix_ext_supported)
1793 status.tree_conn->posix_extensions = true;
1794
1795out_err1:
1796 rsp->StructureSize = cpu_to_le16(16);
1797 rsp->Capabilities = 0;
1798 rsp->Reserved = 0;
1799 /* default manual caching */
1800 rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING;
1801 inc_rfc1001_len(rsp, 16);
1802
1803 if (!IS_ERR(treename))
1804 kfree(treename);
1805 if (!IS_ERR(name))
1806 kfree(name);
1807
1808 switch (status.ret) {
1809 case KSMBD_TREE_CONN_STATUS_OK:
1810 rsp->hdr.Status = STATUS_SUCCESS;
1811 rc = 0;
1812 break;
1813 case KSMBD_TREE_CONN_STATUS_NO_SHARE:
1814 rsp->hdr.Status = STATUS_BAD_NETWORK_PATH;
1815 break;
1816 case -ENOMEM:
1817 case KSMBD_TREE_CONN_STATUS_NOMEM:
1818 rsp->hdr.Status = STATUS_NO_MEMORY;
1819 break;
1820 case KSMBD_TREE_CONN_STATUS_ERROR:
1821 case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS:
1822 case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS:
1823 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1824 break;
1825 case -EINVAL:
1826 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1827 break;
1828 default:
1829 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1830 }
1831
1832 return rc;
1833}
1834
1835/**
1836 * smb2_create_open_flags() - convert smb open flags to unix open flags
1837 * @file_present: is file already present
1838 * @access: file access flags
1839 * @disposition: file disposition flags
Namjae Jeone2f34482021-03-16 10:49:09 +09001840 *
1841 * Return: file open flags
1842 */
1843static int smb2_create_open_flags(bool file_present, __le32 access,
Namjae Jeon070fb212021-05-26 17:57:12 +09001844 __le32 disposition)
Namjae Jeone2f34482021-03-16 10:49:09 +09001845{
1846 int oflags = O_NONBLOCK | O_LARGEFILE;
1847
1848 if (access & FILE_READ_DESIRED_ACCESS_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09001849 access & FILE_WRITE_DESIRE_ACCESS_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09001850 oflags |= O_RDWR;
1851 else if (access & FILE_WRITE_DESIRE_ACCESS_LE)
1852 oflags |= O_WRONLY;
1853 else
1854 oflags |= O_RDONLY;
1855
1856 if (access == FILE_READ_ATTRIBUTES_LE)
1857 oflags |= O_PATH;
1858
1859 if (file_present) {
1860 switch (disposition & FILE_CREATE_MASK_LE) {
1861 case FILE_OPEN_LE:
1862 case FILE_CREATE_LE:
1863 break;
1864 case FILE_SUPERSEDE_LE:
1865 case FILE_OVERWRITE_LE:
1866 case FILE_OVERWRITE_IF_LE:
1867 oflags |= O_TRUNC;
1868 break;
1869 default:
1870 break;
1871 }
1872 } else {
1873 switch (disposition & FILE_CREATE_MASK_LE) {
1874 case FILE_SUPERSEDE_LE:
1875 case FILE_CREATE_LE:
1876 case FILE_OPEN_IF_LE:
1877 case FILE_OVERWRITE_IF_LE:
1878 oflags |= O_CREAT;
1879 break;
1880 case FILE_OPEN_LE:
1881 case FILE_OVERWRITE_LE:
1882 oflags &= ~O_CREAT;
1883 break;
1884 default:
1885 break;
1886 }
1887 }
1888 return oflags;
1889}
1890
1891/**
1892 * smb2_tree_disconnect() - handler for smb tree connect request
1893 * @work: smb work containing request buffer
1894 *
1895 * Return: 0
1896 */
1897int smb2_tree_disconnect(struct ksmbd_work *work)
1898{
Namjae Jeone5066492021-03-30 12:35:23 +09001899 struct smb2_tree_disconnect_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001900 struct ksmbd_session *sess = work->sess;
1901 struct ksmbd_tree_connect *tcon = work->tcon;
1902
1903 rsp->StructureSize = cpu_to_le16(4);
1904 inc_rfc1001_len(rsp, 4);
1905
1906 ksmbd_debug(SMB, "request\n");
1907
1908 if (!tcon) {
Namjae Jeone5066492021-03-30 12:35:23 +09001909 struct smb2_tree_disconnect_req *req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001910
1911 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
1912 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
1913 smb2_set_err_rsp(work);
1914 return 0;
1915 }
1916
1917 ksmbd_close_tree_conn_fds(work);
1918 ksmbd_tree_conn_disconnect(sess, tcon);
1919 return 0;
1920}
1921
1922/**
1923 * smb2_session_logoff() - handler for session log off request
1924 * @work: smb work containing request buffer
1925 *
1926 * Return: 0
1927 */
1928int smb2_session_logoff(struct ksmbd_work *work)
1929{
1930 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09001931 struct smb2_logoff_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001932 struct ksmbd_session *sess = work->sess;
1933
1934 rsp->StructureSize = cpu_to_le16(4);
1935 inc_rfc1001_len(rsp, 4);
1936
1937 ksmbd_debug(SMB, "request\n");
1938
1939 /* Got a valid session, set connection state */
1940 WARN_ON(sess->conn != conn);
1941
1942 /* setting CifsExiting here may race with start_tcp_sess */
1943 ksmbd_conn_set_need_reconnect(work);
1944 ksmbd_close_session_fds(work);
1945 ksmbd_conn_wait_idle(conn);
1946
1947 if (ksmbd_tree_conn_session_logoff(sess)) {
Namjae Jeone5066492021-03-30 12:35:23 +09001948 struct smb2_logoff_req *req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001949
1950 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
1951 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
1952 smb2_set_err_rsp(work);
1953 return 0;
1954 }
1955
1956 ksmbd_destroy_file_table(&sess->file_table);
1957 sess->state = SMB2_SESSION_EXPIRED;
1958
1959 ksmbd_free_user(sess->user);
1960 sess->user = NULL;
1961
1962 /* let start_tcp_sess free connection info now */
1963 ksmbd_conn_set_need_negotiate(work);
1964 return 0;
1965}
1966
1967/**
1968 * create_smb2_pipe() - create IPC pipe
1969 * @work: smb work containing request buffer
1970 *
1971 * Return: 0 on success, otherwise error
1972 */
1973static noinline int create_smb2_pipe(struct ksmbd_work *work)
1974{
Namjae Jeone5066492021-03-30 12:35:23 +09001975 struct smb2_create_rsp *rsp = work->response_buf;
1976 struct smb2_create_req *req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001977 int id;
1978 int err;
1979 char *name;
1980
1981 name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09001982 1, work->conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09001983 if (IS_ERR(name)) {
1984 rsp->hdr.Status = STATUS_NO_MEMORY;
1985 err = PTR_ERR(name);
1986 goto out;
1987 }
1988
1989 id = ksmbd_session_rpc_open(work->sess, name);
Marios Makassikis79caa962021-05-06 11:38:35 +09001990 if (id < 0) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001991 ksmbd_err("Unable to open RPC pipe: %d\n", id);
Marios Makassikis79caa962021-05-06 11:38:35 +09001992 err = id;
1993 goto out;
1994 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001995
Marios Makassikis79caa962021-05-06 11:38:35 +09001996 rsp->hdr.Status = STATUS_SUCCESS;
Namjae Jeone2f34482021-03-16 10:49:09 +09001997 rsp->StructureSize = cpu_to_le16(89);
1998 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
1999 rsp->Reserved = 0;
2000 rsp->CreateAction = cpu_to_le32(FILE_OPENED);
2001
2002 rsp->CreationTime = cpu_to_le64(0);
2003 rsp->LastAccessTime = cpu_to_le64(0);
2004 rsp->ChangeTime = cpu_to_le64(0);
2005 rsp->AllocationSize = cpu_to_le64(0);
2006 rsp->EndofFile = cpu_to_le64(0);
2007 rsp->FileAttributes = ATTR_NORMAL_LE;
2008 rsp->Reserved2 = 0;
2009 rsp->VolatileFileId = cpu_to_le64(id);
2010 rsp->PersistentFileId = 0;
2011 rsp->CreateContextsOffset = 0;
2012 rsp->CreateContextsLength = 0;
2013
2014 inc_rfc1001_len(rsp, 88); /* StructureSize - 1*/
2015 kfree(name);
2016 return 0;
2017
2018out:
Marios Makassikis79caa962021-05-06 11:38:35 +09002019 switch (err) {
2020 case -EINVAL:
2021 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2022 break;
2023 case -ENOSPC:
2024 case -ENOMEM:
2025 rsp->hdr.Status = STATUS_NO_MEMORY;
2026 break;
2027 }
2028
2029 if (!IS_ERR(name))
2030 kfree(name);
2031
Namjae Jeone2f34482021-03-16 10:49:09 +09002032 smb2_set_err_rsp(work);
2033 return err;
2034}
2035
Namjae Jeone2f34482021-03-16 10:49:09 +09002036/**
2037 * smb2_set_ea() - handler for setting extended attributes using set
2038 * info command
2039 * @eabuf: set info command buffer
2040 * @path: dentry path for get ea
2041 *
2042 * Return: 0 on success, otherwise error
2043 */
2044static int smb2_set_ea(struct smb2_ea_info *eabuf, struct path *path)
2045{
2046 char *attr_name = NULL, *value;
2047 int rc = 0;
2048 int next = 0;
2049
2050 attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL);
2051 if (!attr_name)
2052 return -ENOMEM;
2053
2054 do {
2055 if (!eabuf->EaNameLength)
2056 goto next;
2057
2058 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002059 "name : <%s>, name_len : %u, value_len : %u, next : %u\n",
2060 eabuf->name, eabuf->EaNameLength,
2061 le16_to_cpu(eabuf->EaValueLength),
2062 le32_to_cpu(eabuf->NextEntryOffset));
Namjae Jeone2f34482021-03-16 10:49:09 +09002063
2064 if (eabuf->EaNameLength >
Namjae Jeon070fb212021-05-26 17:57:12 +09002065 (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002066 rc = -EINVAL;
2067 break;
2068 }
2069
2070 memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2071 memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002072 eabuf->EaNameLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09002073 attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0';
2074 value = (char *)&eabuf->name + eabuf->EaNameLength + 1;
2075
2076 if (!eabuf->EaValueLength) {
2077 rc = ksmbd_vfs_casexattr_len(path->dentry,
2078 attr_name,
2079 XATTR_USER_PREFIX_LEN +
2080 eabuf->EaNameLength);
2081
2082 /* delete the EA only when it exits */
2083 if (rc > 0) {
2084 rc = ksmbd_vfs_remove_xattr(path->dentry,
2085 attr_name);
2086
2087 if (rc < 0) {
2088 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002089 "remove xattr failed(%d)\n",
2090 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002091 break;
2092 }
2093 }
2094
2095 /* if the EA doesn't exist, just do nothing. */
2096 rc = 0;
2097 } else {
2098 rc = ksmbd_vfs_setxattr(path->dentry, attr_name, value,
Namjae Jeon070fb212021-05-26 17:57:12 +09002099 le16_to_cpu(eabuf->EaValueLength), 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002100 if (rc < 0) {
2101 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002102 "ksmbd_vfs_setxattr is failed(%d)\n",
2103 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002104 break;
2105 }
2106 }
2107
2108next:
2109 next = le32_to_cpu(eabuf->NextEntryOffset);
2110 eabuf = (struct smb2_ea_info *)((char *)eabuf + next);
2111 } while (next != 0);
2112
2113 kfree(attr_name);
2114 return rc;
2115}
2116
2117static inline int check_context_err(void *ctx, char *str)
2118{
2119 int err;
2120
2121 err = PTR_ERR(ctx);
2122 ksmbd_debug(SMB, "find context %s err %d\n", str, err);
2123
2124 if (err == -EINVAL) {
2125 ksmbd_err("bad name length\n");
2126 return err;
2127 }
2128
2129 return 0;
2130}
2131
2132static noinline int smb2_set_stream_name_xattr(struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002133 struct ksmbd_file *fp,
2134 char *stream_name, int s_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09002135{
2136 size_t xattr_stream_size;
2137 char *xattr_stream_name;
2138 int rc;
2139
2140 rc = ksmbd_vfs_xattr_stream_name(stream_name,
2141 &xattr_stream_name,
2142 &xattr_stream_size,
2143 s_type);
2144 if (rc)
2145 return rc;
2146
2147 fp->stream.name = xattr_stream_name;
2148 fp->stream.size = xattr_stream_size;
2149
2150 /* Check if there is stream prefix in xattr space */
2151 rc = ksmbd_vfs_casexattr_len(path->dentry,
2152 xattr_stream_name,
2153 xattr_stream_size);
2154 if (rc >= 0)
2155 return 0;
2156
2157 if (fp->cdoption == FILE_OPEN_LE) {
2158 ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc);
2159 return -EBADF;
2160 }
2161
2162 rc = ksmbd_vfs_setxattr(path->dentry, xattr_stream_name, NULL, 0, 0);
2163 if (rc < 0)
2164 ksmbd_err("Failed to store XATTR stream name :%d\n", rc);
2165 return 0;
2166}
2167
2168static int smb2_remove_smb_xattrs(struct dentry *dentry)
2169{
2170 char *name, *xattr_list = NULL;
2171 ssize_t xattr_list_len;
2172 int err = 0;
2173
2174 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
2175 if (xattr_list_len < 0) {
2176 goto out;
2177 } else if (!xattr_list_len) {
2178 ksmbd_debug(SMB, "empty xattr in the file\n");
2179 goto out;
2180 }
2181
2182 for (name = xattr_list; name - xattr_list < xattr_list_len;
2183 name += strlen(name) + 1) {
2184 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
2185
2186 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002187 strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX,
2188 DOS_ATTRIBUTE_PREFIX_LEN) &&
2189 strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09002190 continue;
2191
2192 err = ksmbd_vfs_remove_xattr(dentry, name);
2193 if (err)
2194 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
2195 }
2196out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09002197 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002198 return err;
2199}
2200
2201static int smb2_create_truncate(struct path *path)
2202{
2203 int rc = vfs_truncate(path, 0);
2204
2205 if (rc) {
2206 ksmbd_err("vfs_truncate failed, rc %d\n", rc);
2207 return rc;
2208 }
2209
2210 rc = smb2_remove_smb_xattrs(path->dentry);
2211 if (rc == -EOPNOTSUPP)
2212 rc = 0;
2213 if (rc)
2214 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002215 "ksmbd_truncate_stream_name_xattr failed, rc %d\n",
2216 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002217 return rc;
2218}
2219
Namjae Jeon64b39f42021-03-30 14:25:35 +09002220static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002221 struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002222{
2223 struct xattr_dos_attrib da = {0};
2224 int rc;
2225
2226 if (!test_share_config_flag(tcon->share_conf,
2227 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
2228 return;
2229
2230 da.version = 4;
2231 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
2232 da.itime = da.create_time = fp->create_time;
2233 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
2234 XATTR_DOSINFO_ITIME;
2235
2236 rc = ksmbd_vfs_set_dos_attrib_xattr(path->dentry, &da);
2237 if (rc)
2238 ksmbd_debug(SMB, "failed to store file attribute into xattr\n");
2239}
2240
2241static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon,
Namjae Jeon070fb212021-05-26 17:57:12 +09002242 struct path *path, struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002243{
2244 struct xattr_dos_attrib da;
2245 int rc;
2246
2247 fp->f_ci->m_fattr &= ~(ATTR_HIDDEN_LE | ATTR_SYSTEM_LE);
2248
2249 /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */
2250 if (!test_share_config_flag(tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002251 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
Namjae Jeone2f34482021-03-16 10:49:09 +09002252 return;
2253
2254 rc = ksmbd_vfs_get_dos_attrib_xattr(path->dentry, &da);
2255 if (rc > 0) {
2256 fp->f_ci->m_fattr = cpu_to_le32(da.attr);
2257 fp->create_time = da.create_time;
2258 fp->itime = da.itime;
2259 }
2260}
2261
Namjae Jeon64b39f42021-03-30 14:25:35 +09002262static int smb2_creat(struct ksmbd_work *work, struct path *path, char *name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002263 int open_flags, umode_t posix_mode, bool is_dir)
Namjae Jeone2f34482021-03-16 10:49:09 +09002264{
2265 struct ksmbd_tree_connect *tcon = work->tcon;
2266 struct ksmbd_share_config *share = tcon->share_conf;
2267 umode_t mode;
2268 int rc;
2269
2270 if (!(open_flags & O_CREAT))
2271 return -EBADF;
2272
2273 ksmbd_debug(SMB, "file does not exist, so creating\n");
2274 if (is_dir == true) {
2275 ksmbd_debug(SMB, "creating directory\n");
2276
2277 mode = share_config_directory_mode(share, posix_mode);
2278 rc = ksmbd_vfs_mkdir(work, name, mode);
2279 if (rc)
2280 return rc;
2281 } else {
2282 ksmbd_debug(SMB, "creating regular file\n");
2283
2284 mode = share_config_create_mode(share, posix_mode);
2285 rc = ksmbd_vfs_create(work, name, mode);
2286 if (rc)
2287 return rc;
2288 }
2289
2290 rc = ksmbd_vfs_kern_path(name, 0, path, 0);
2291 if (rc) {
2292 ksmbd_err("cannot get linux path (%s), err = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002293 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002294 return rc;
2295 }
2296 return 0;
2297}
2298
2299static int smb2_create_sd_buffer(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09002300 struct smb2_create_req *req,
2301 struct dentry *dentry)
Namjae Jeone2f34482021-03-16 10:49:09 +09002302{
2303 struct create_context *context;
2304 int rc = -ENOENT;
2305
2306 if (!req->CreateContextsOffset)
2307 return rc;
2308
2309 /* Parse SD BUFFER create contexts */
2310 context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER);
2311 if (context && !IS_ERR(context)) {
2312 struct create_sd_buf_req *sd_buf;
2313
2314 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002315 "Set ACLs using SMB2_CREATE_SD_BUFFER context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002316 sd_buf = (struct create_sd_buf_req *)context;
2317 rc = set_info_sec(work->conn, work->tcon, dentry, &sd_buf->ntsd,
Namjae Jeon070fb212021-05-26 17:57:12 +09002318 le32_to_cpu(sd_buf->ccontext.DataLength), true);
Namjae Jeone2f34482021-03-16 10:49:09 +09002319 }
2320
2321 return rc;
2322}
2323
Namjae Jeon3d47e542021-04-20 14:25:35 +09002324static void ksmbd_acls_fattr(struct smb_fattr *fattr, struct inode *inode)
2325{
2326 fattr->cf_uid = inode->i_uid;
2327 fattr->cf_gid = inode->i_gid;
2328 fattr->cf_mode = inode->i_mode;
2329 fattr->cf_dacls = NULL;
2330
2331 fattr->cf_acls = ksmbd_vfs_get_acl(inode, ACL_TYPE_ACCESS);
2332 if (S_ISDIR(inode->i_mode))
2333 fattr->cf_dacls = ksmbd_vfs_get_acl(inode, ACL_TYPE_DEFAULT);
2334}
2335
Namjae Jeone2f34482021-03-16 10:49:09 +09002336/**
2337 * smb2_open() - handler for smb file open request
2338 * @work: smb work containing request buffer
2339 *
2340 * Return: 0 on success, otherwise error
2341 */
2342int smb2_open(struct ksmbd_work *work)
2343{
2344 struct ksmbd_conn *conn = work->conn;
2345 struct ksmbd_session *sess = work->sess;
2346 struct ksmbd_tree_connect *tcon = work->tcon;
2347 struct smb2_create_req *req;
2348 struct smb2_create_rsp *rsp, *rsp_org;
2349 struct path path;
2350 struct ksmbd_share_config *share = tcon->share_conf;
2351 struct ksmbd_file *fp = NULL;
2352 struct file *filp = NULL;
2353 struct kstat stat;
2354 struct create_context *context;
2355 struct lease_ctx_info *lc = NULL;
2356 struct create_ea_buf_req *ea_buf = NULL;
2357 struct oplock_info *opinfo;
2358 __le32 *next_ptr = NULL;
2359 int req_op_level = 0, open_flags = 0, file_info = 0;
2360 int rc = 0, len = 0;
2361 int contxt_cnt = 0, query_disk_id = 0;
2362 int maximal_access_ctxt = 0, posix_ctxt = 0;
2363 int s_type = 0;
2364 int next_off = 0;
2365 char *name = NULL;
2366 char *stream_name = NULL;
2367 bool file_present = false, created = false, already_permitted = false;
Namjae Jeone2f34482021-03-16 10:49:09 +09002368 int share_ret, need_truncate = 0;
2369 u64 time;
2370 umode_t posix_mode = 0;
2371 __le32 daccess, maximal_access = 0;
2372
Namjae Jeone5066492021-03-30 12:35:23 +09002373 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09002374 WORK_BUFFERS(work, req, rsp);
2375
2376 if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002377 (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002378 ksmbd_debug(SMB, "invalid flag in chained command\n");
2379 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2380 smb2_set_err_rsp(work);
2381 return -EINVAL;
2382 }
2383
2384 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
2385 ksmbd_debug(SMB, "IPC pipe create request\n");
2386 return create_smb2_pipe(work);
2387 }
2388
2389 if (req->NameLength) {
2390 if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002391 *(char *)req->Buffer == '\\') {
Colin Ian King1e853b92021-03-17 09:36:58 +00002392 ksmbd_err("not allow directory name included leading slash\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002393 rc = -EINVAL;
2394 goto err_out1;
2395 }
2396
2397 name = smb2_get_name(share,
2398 req->Buffer,
2399 le16_to_cpu(req->NameLength),
2400 work->conn->local_nls);
2401 if (IS_ERR(name)) {
2402 rc = PTR_ERR(name);
2403 if (rc != -ENOMEM)
2404 rc = -ENOENT;
2405 goto err_out1;
2406 }
2407
2408 ksmbd_debug(SMB, "converted name = %s\n", name);
2409 if (strchr(name, ':')) {
2410 if (!test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002411 KSMBD_SHARE_FLAG_STREAMS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002412 rc = -EBADF;
2413 goto err_out1;
2414 }
2415 rc = parse_stream_name(name, &stream_name, &s_type);
2416 if (rc < 0)
2417 goto err_out1;
2418 }
2419
2420 rc = ksmbd_validate_filename(name);
2421 if (rc < 0)
2422 goto err_out1;
2423
2424 if (ksmbd_share_veto_filename(share, name)) {
2425 rc = -ENOENT;
2426 ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002427 name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002428 goto err_out1;
2429 }
2430 } else {
2431 len = strlen(share->path);
2432 ksmbd_debug(SMB, "share path len %d\n", len);
2433 name = kmalloc(len + 1, GFP_KERNEL);
2434 if (!name) {
2435 rsp->hdr.Status = STATUS_NO_MEMORY;
2436 rc = -ENOMEM;
2437 goto err_out1;
2438 }
2439
2440 memcpy(name, share->path, len);
2441 *(name + len) = '\0';
2442 }
2443
2444 req_op_level = req->RequestedOplockLevel;
Namjae Jeon73f9dad2021-04-16 14:12:06 +09002445 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002446 lc = parse_lease_state(req);
Namjae Jeone2f34482021-03-16 10:49:09 +09002447
Namjae Jeon64b39f42021-03-30 14:25:35 +09002448 if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002449 ksmbd_err("Invalid impersonationlevel : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002450 le32_to_cpu(req->ImpersonationLevel));
Namjae Jeone2f34482021-03-16 10:49:09 +09002451 rc = -EIO;
2452 rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL;
2453 goto err_out1;
2454 }
2455
2456 if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK)) {
2457 ksmbd_err("Invalid create options : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002458 le32_to_cpu(req->CreateOptions));
Namjae Jeone2f34482021-03-16 10:49:09 +09002459 rc = -EINVAL;
2460 goto err_out1;
2461 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09002462 if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002463 req->CreateOptions & FILE_RANDOM_ACCESS_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002464 req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE);
2465
Namjae Jeon070fb212021-05-26 17:57:12 +09002466 if (req->CreateOptions &
2467 (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION |
2468 FILE_RESERVE_OPFILTER_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002469 rc = -EOPNOTSUPP;
2470 goto err_out1;
2471 }
2472
2473 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2474 if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) {
2475 rc = -EINVAL;
2476 goto err_out1;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002477 } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002478 req->CreateOptions = ~(FILE_NO_COMPRESSION_LE);
Namjae Jeon64b39f42021-03-30 14:25:35 +09002479 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002480 }
2481 }
2482
2483 if (le32_to_cpu(req->CreateDisposition) >
Namjae Jeon070fb212021-05-26 17:57:12 +09002484 le32_to_cpu(FILE_OVERWRITE_IF_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002485 ksmbd_err("Invalid create disposition : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002486 le32_to_cpu(req->CreateDisposition));
Namjae Jeone2f34482021-03-16 10:49:09 +09002487 rc = -EINVAL;
2488 goto err_out1;
2489 }
2490
2491 if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) {
Colin Ian King1e853b92021-03-17 09:36:58 +00002492 ksmbd_err("Invalid desired access : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002493 le32_to_cpu(req->DesiredAccess));
Namjae Jeone2f34482021-03-16 10:49:09 +09002494 rc = -EACCES;
2495 goto err_out1;
2496 }
2497
Namjae Jeon64b39f42021-03-30 14:25:35 +09002498 if (req->FileAttributes && !(req->FileAttributes & ATTR_MASK_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002499 ksmbd_err("Invalid file attribute : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002500 le32_to_cpu(req->FileAttributes));
Namjae Jeone2f34482021-03-16 10:49:09 +09002501 rc = -EINVAL;
2502 goto err_out1;
2503 }
2504
2505 if (req->CreateContextsOffset) {
2506 /* Parse non-durable handle create contexts */
2507 context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER);
2508 if (IS_ERR(context)) {
2509 rc = check_context_err(context, SMB2_CREATE_EA_BUFFER);
2510 if (rc < 0)
2511 goto err_out1;
2512 } else {
2513 ea_buf = (struct create_ea_buf_req *)context;
2514 if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) {
2515 rsp->hdr.Status = STATUS_ACCESS_DENIED;
2516 rc = -EACCES;
2517 goto err_out1;
2518 }
2519 }
2520
2521 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002522 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
Namjae Jeone2f34482021-03-16 10:49:09 +09002523 if (IS_ERR(context)) {
2524 rc = check_context_err(context,
Namjae Jeon070fb212021-05-26 17:57:12 +09002525 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
Namjae Jeone2f34482021-03-16 10:49:09 +09002526 if (rc < 0)
2527 goto err_out1;
2528 } else {
2529 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002530 "get query maximal access context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002531 maximal_access_ctxt = 1;
2532 }
2533
2534 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002535 SMB2_CREATE_TIMEWARP_REQUEST);
Namjae Jeone2f34482021-03-16 10:49:09 +09002536 if (IS_ERR(context)) {
2537 rc = check_context_err(context,
Namjae Jeon070fb212021-05-26 17:57:12 +09002538 SMB2_CREATE_TIMEWARP_REQUEST);
Namjae Jeone2f34482021-03-16 10:49:09 +09002539 if (rc < 0)
2540 goto err_out1;
2541 } else {
2542 ksmbd_debug(SMB, "get timewarp context\n");
2543 rc = -EBADF;
2544 goto err_out1;
2545 }
2546
2547 if (tcon->posix_extensions) {
2548 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002549 SMB2_CREATE_TAG_POSIX);
Namjae Jeone2f34482021-03-16 10:49:09 +09002550 if (IS_ERR(context)) {
2551 rc = check_context_err(context,
Namjae Jeon070fb212021-05-26 17:57:12 +09002552 SMB2_CREATE_TAG_POSIX);
Namjae Jeone2f34482021-03-16 10:49:09 +09002553 if (rc < 0)
2554 goto err_out1;
2555 } else {
2556 struct create_posix *posix =
2557 (struct create_posix *)context;
2558 ksmbd_debug(SMB, "get posix context\n");
2559
2560 posix_mode = le32_to_cpu(posix->Mode);
2561 posix_ctxt = 1;
2562 }
2563 }
2564 }
2565
2566 if (ksmbd_override_fsids(work)) {
2567 rc = -ENOMEM;
2568 goto err_out1;
2569 }
2570
2571 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) {
2572 /*
2573 * On delete request, instead of following up, need to
2574 * look the current entity
2575 */
2576 rc = ksmbd_vfs_kern_path(name, 0, &path, 1);
2577 if (!rc) {
2578 /*
2579 * If file exists with under flags, return access
2580 * denied error.
2581 */
2582 if (req->CreateDisposition == FILE_OVERWRITE_IF_LE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09002583 req->CreateDisposition == FILE_OPEN_IF_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002584 rc = -EACCES;
2585 path_put(&path);
2586 goto err_out;
2587 }
2588
Namjae Jeon64b39f42021-03-30 14:25:35 +09002589 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002590 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002591 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002592 rc = -EACCES;
2593 path_put(&path);
2594 goto err_out;
2595 }
2596 }
2597 } else {
2598 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002599 KSMBD_SHARE_FLAG_FOLLOW_SYMLINKS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002600 /*
2601 * Use LOOKUP_FOLLOW to follow the path of
2602 * symlink in path buildup
2603 */
2604 rc = ksmbd_vfs_kern_path(name, LOOKUP_FOLLOW, &path, 1);
2605 if (rc) { /* Case for broken link ?*/
2606 rc = ksmbd_vfs_kern_path(name, 0, &path, 1);
2607 }
2608 } else {
2609 rc = ksmbd_vfs_kern_path(name, 0, &path, 1);
2610 if (!rc && d_is_symlink(path.dentry)) {
2611 rc = -EACCES;
2612 path_put(&path);
2613 goto err_out;
2614 }
2615 }
2616 }
2617
2618 if (rc) {
2619 if (rc == -EACCES) {
2620 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002621 "User does not have right permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002622 goto err_out;
2623 }
2624 ksmbd_debug(SMB, "can not get linux path for %s, rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002625 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002626 rc = 0;
2627 } else {
2628 file_present = true;
2629 generic_fillattr(&init_user_ns, d_inode(path.dentry), &stat);
2630 }
2631 if (stream_name) {
2632 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2633 if (s_type == DATA_STREAM) {
2634 rc = -EIO;
2635 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2636 }
2637 } else {
2638 if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) {
2639 rc = -EIO;
2640 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2641 }
2642 }
2643
2644 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002645 req->FileAttributes & ATTR_NORMAL_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002646 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2647 rc = -EIO;
2648 }
2649
2650 if (rc < 0)
2651 goto err_out;
2652 }
2653
Namjae Jeon64b39f42021-03-30 14:25:35 +09002654 if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE &&
2655 S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002656 ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002657 name, req->CreateOptions);
Namjae Jeone2f34482021-03-16 10:49:09 +09002658 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2659 rc = -EIO;
2660 goto err_out;
2661 }
2662
2663 if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002664 !(req->CreateDisposition == FILE_CREATE_LE) &&
2665 !S_ISDIR(stat.mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002666 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2667 rc = -EIO;
2668 goto err_out;
2669 }
2670
2671 if (!stream_name && file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002672 req->CreateDisposition == FILE_CREATE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002673 rc = -EEXIST;
2674 goto err_out;
2675 }
2676
Namjae Jeone2f34482021-03-16 10:49:09 +09002677 daccess = smb_map_generic_desired_access(req->DesiredAccess);
2678
2679 if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
2680 rc = smb_check_perm_dacl(conn, path.dentry, &daccess,
Namjae Jeon070fb212021-05-26 17:57:12 +09002681 sess->user->uid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002682 if (rc)
2683 goto err_out;
2684 }
2685
2686 if (daccess & FILE_MAXIMAL_ACCESS_LE) {
2687 if (!file_present) {
2688 daccess = cpu_to_le32(GENERIC_ALL_FLAGS);
2689 } else {
2690 rc = ksmbd_vfs_query_maximal_access(path.dentry,
2691 &daccess);
2692 if (rc)
2693 goto err_out;
2694 already_permitted = true;
2695 }
2696 maximal_access = daccess;
2697 }
2698
Namjae Jeon070fb212021-05-26 17:57:12 +09002699 open_flags = smb2_create_open_flags(file_present, daccess,
2700 req->CreateDisposition);
Namjae Jeone2f34482021-03-16 10:49:09 +09002701
2702 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
2703 if (open_flags & O_CREAT) {
2704 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002705 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002706 rc = -EACCES;
2707 goto err_out;
2708 }
2709 }
2710
2711 /*create file if not present */
2712 if (!file_present) {
2713 rc = smb2_creat(work, &path, name, open_flags, posix_mode,
Namjae Jeon070fb212021-05-26 17:57:12 +09002714 req->CreateOptions & FILE_DIRECTORY_FILE_LE);
Namjae Jeone2f34482021-03-16 10:49:09 +09002715 if (rc)
2716 goto err_out;
2717
2718 created = true;
2719 if (ea_buf) {
2720 rc = smb2_set_ea(&ea_buf->ea, &path);
2721 if (rc == -EOPNOTSUPP)
2722 rc = 0;
2723 else if (rc)
2724 goto err_out;
2725 }
2726 } else if (!already_permitted) {
2727 bool may_delete;
2728
2729 may_delete = daccess & FILE_DELETE_LE ||
2730 req->CreateOptions & FILE_DELETE_ON_CLOSE_LE;
2731
2732 /* FILE_READ_ATTRIBUTE is allowed without inode_permission,
2733 * because execute(search) permission on a parent directory,
2734 * is already granted.
2735 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09002736 if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) {
Namjae Jeonff1d5722021-04-13 13:18:10 +09002737 rc = ksmbd_vfs_inode_permission(path.dentry,
Namjae Jeon070fb212021-05-26 17:57:12 +09002738 open_flags & O_ACCMODE,
2739 may_delete);
Namjae Jeonff1d5722021-04-13 13:18:10 +09002740 if (rc)
Namjae Jeone2f34482021-03-16 10:49:09 +09002741 goto err_out;
Namjae Jeone2f34482021-03-16 10:49:09 +09002742 }
2743 }
2744
2745 rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent));
2746 if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) {
2747 rc = -EBUSY;
2748 goto err_out;
2749 }
2750
2751 rc = 0;
2752 filp = dentry_open(&path, open_flags, current_cred());
2753 if (IS_ERR(filp)) {
2754 rc = PTR_ERR(filp);
2755 ksmbd_err("dentry open for dir failed, rc %d\n", rc);
2756 goto err_out;
2757 }
2758
2759 if (file_present) {
2760 if (!(open_flags & O_TRUNC))
2761 file_info = FILE_OPENED;
2762 else
2763 file_info = FILE_OVERWRITTEN;
2764
Namjae Jeon070fb212021-05-26 17:57:12 +09002765 if ((req->CreateDisposition & FILE_CREATE_MASK_LE) ==
2766 FILE_SUPERSEDE_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002767 file_info = FILE_SUPERSEDED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002768 } else if (open_flags & O_CREAT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002769 file_info = FILE_CREATED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002770 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002771
2772 ksmbd_vfs_set_fadvise(filp, req->CreateOptions);
2773
2774 /* Obtain Volatile-ID */
2775 fp = ksmbd_open_fd(work, filp);
2776 if (IS_ERR(fp)) {
2777 fput(filp);
2778 rc = PTR_ERR(fp);
2779 fp = NULL;
2780 goto err_out;
2781 }
2782
2783 /* Get Persistent-ID */
2784 ksmbd_open_durable_fd(fp);
2785 if (!HAS_FILE_ID(fp->persistent_id)) {
2786 rc = -ENOMEM;
2787 goto err_out;
2788 }
2789
2790 fp->filename = name;
2791 fp->cdoption = req->CreateDisposition;
2792 fp->daccess = daccess;
2793 fp->saccess = req->ShareAccess;
2794 fp->coption = req->CreateOptions;
2795
2796 /* Set default windows and posix acls if creating new file */
2797 if (created) {
2798 int posix_acl_rc;
Namjae Jeonfba08fa2021-04-15 10:29:39 +09002799 struct inode *inode = d_inode(path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09002800
Namjae Jeonfba08fa2021-04-15 10:29:39 +09002801 posix_acl_rc = ksmbd_vfs_inherit_posix_acl(inode, d_inode(path.dentry->d_parent));
Namjae Jeone2f34482021-03-16 10:49:09 +09002802 if (posix_acl_rc)
2803 ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc);
2804
2805 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002806 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002807 rc = smb_inherit_dacl(conn, path.dentry, sess->user->uid,
Namjae Jeon070fb212021-05-26 17:57:12 +09002808 sess->user->gid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002809 }
2810
2811 if (rc) {
2812 rc = smb2_create_sd_buffer(work, req, path.dentry);
2813 if (rc) {
2814 if (posix_acl_rc)
2815 ksmbd_vfs_set_init_posix_acl(inode);
2816
2817 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002818 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002819 struct smb_fattr fattr;
2820 struct smb_ntsd *pntsd;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002821 int pntsd_size, ace_num = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002822
Namjae Jeon3d47e542021-04-20 14:25:35 +09002823 ksmbd_acls_fattr(&fattr, inode);
Marios Makassikise6b10592021-04-15 10:24:56 +09002824 if (fattr.cf_acls)
2825 ace_num = fattr.cf_acls->a_count;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002826 if (fattr.cf_dacls)
2827 ace_num += fattr.cf_dacls->a_count;
Namjae Jeone2f34482021-03-16 10:49:09 +09002828
2829 pntsd = kmalloc(sizeof(struct smb_ntsd) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002830 sizeof(struct smb_sid) * 3 +
Namjae Jeone2f34482021-03-16 10:49:09 +09002831 sizeof(struct smb_acl) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002832 sizeof(struct smb_ace) * ace_num * 2,
Namjae Jeone2f34482021-03-16 10:49:09 +09002833 GFP_KERNEL);
2834 if (!pntsd)
2835 goto err_out;
2836
2837 rc = build_sec_desc(pntsd, NULL,
Namjae Jeon070fb212021-05-26 17:57:12 +09002838 OWNER_SECINFO |
2839 GROUP_SECINFO |
2840 DACL_SECINFO,
2841 &pntsd_size, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09002842 posix_acl_release(fattr.cf_acls);
2843 posix_acl_release(fattr.cf_dacls);
2844
2845 rc = ksmbd_vfs_set_sd_xattr(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09002846 path.dentry,
2847 pntsd,
2848 pntsd_size);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002849 kfree(pntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09002850 if (rc)
2851 ksmbd_err("failed to store ntacl in xattr : %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002852 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002853 }
2854 }
2855 }
2856 rc = 0;
2857 }
2858
2859 if (stream_name) {
2860 rc = smb2_set_stream_name_xattr(&path,
2861 fp,
2862 stream_name,
2863 s_type);
2864 if (rc)
2865 goto err_out;
2866 file_info = FILE_CREATED;
2867 }
2868
2869 fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE |
2870 FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE));
Namjae Jeon64b39f42021-03-30 14:25:35 +09002871 if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC &&
2872 !fp->attrib_only && !stream_name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002873 smb_break_all_oplock(work, fp);
2874 need_truncate = 1;
2875 }
2876
2877 /* fp should be searchable through ksmbd_inode.m_fp_list
2878 * after daccess, saccess, attrib_only, and stream are
2879 * initialized.
2880 */
2881 write_lock(&fp->f_ci->m_lock);
2882 list_add(&fp->node, &fp->f_ci->m_fp_list);
2883 write_unlock(&fp->f_ci->m_lock);
2884
2885 rc = ksmbd_vfs_getattr(&path, &stat);
2886 if (rc) {
2887 generic_fillattr(&init_user_ns, d_inode(path.dentry), &stat);
2888 rc = 0;
2889 }
2890
2891 /* Check delete pending among previous fp before oplock break */
2892 if (ksmbd_inode_pending_delete(fp)) {
2893 rc = -EBUSY;
2894 goto err_out;
2895 }
2896
2897 share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp);
Namjae Jeon64b39f42021-03-30 14:25:35 +09002898 if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) ||
2899 (req_op_level == SMB2_OPLOCK_LEVEL_LEASE &&
2900 !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002901 if (share_ret < 0 && !S_ISDIR(FP_INODE(fp)->i_mode)) {
2902 rc = share_ret;
2903 goto err_out;
2904 }
2905 } else {
2906 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) {
2907 req_op_level = smb2_map_lease_to_oplock(lc->req_state);
2908 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002909 "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n",
2910 name, req_op_level, lc->req_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09002911 rc = find_same_lease_key(sess, fp->f_ci, lc);
2912 if (rc)
2913 goto err_out;
2914 } else if (open_flags == O_RDONLY &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002915 (req_op_level == SMB2_OPLOCK_LEVEL_BATCH ||
2916 req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
Namjae Jeone2f34482021-03-16 10:49:09 +09002917 req_op_level = SMB2_OPLOCK_LEVEL_II;
2918
2919 rc = smb_grant_oplock(work, req_op_level,
2920 fp->persistent_id, fp,
2921 le32_to_cpu(req->hdr.Id.SyncId.TreeId),
2922 lc, share_ret);
2923 if (rc < 0)
2924 goto err_out;
2925 }
2926
2927 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
2928 ksmbd_fd_set_delete_on_close(fp, file_info);
2929
2930 if (need_truncate) {
2931 rc = smb2_create_truncate(&path);
2932 if (rc)
2933 goto err_out;
2934 }
2935
2936 if (req->CreateContextsOffset) {
2937 struct create_alloc_size_req *az_req;
2938
Namjae Jeon070fb212021-05-26 17:57:12 +09002939 az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req,
2940 SMB2_CREATE_ALLOCATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09002941 if (IS_ERR(az_req)) {
2942 rc = check_context_err(az_req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002943 SMB2_CREATE_ALLOCATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09002944 if (rc < 0)
2945 goto err_out;
2946 } else {
2947 loff_t alloc_size = le64_to_cpu(az_req->AllocationSize);
2948 int err;
2949
2950 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002951 "request smb2 create allocate size : %llu\n",
2952 alloc_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09002953 err = ksmbd_vfs_alloc_size(work, fp, alloc_size);
2954 if (err < 0)
2955 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002956 "ksmbd_vfs_alloc_size is failed : %d\n",
2957 err);
Namjae Jeone2f34482021-03-16 10:49:09 +09002958 }
2959
Namjae Jeon64b39f42021-03-30 14:25:35 +09002960 context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID);
Namjae Jeone2f34482021-03-16 10:49:09 +09002961 if (IS_ERR(context)) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09002962 rc = check_context_err(context, SMB2_CREATE_QUERY_ON_DISK_ID);
Namjae Jeone2f34482021-03-16 10:49:09 +09002963 if (rc < 0)
2964 goto err_out;
2965 } else {
2966 ksmbd_debug(SMB, "get query on disk id context\n");
2967 query_disk_id = 1;
2968 }
2969 }
2970
2971 if (stat.result_mask & STATX_BTIME)
2972 fp->create_time = ksmbd_UnixTimeToNT(stat.btime);
2973 else
2974 fp->create_time = ksmbd_UnixTimeToNT(stat.ctime);
2975 if (req->FileAttributes || fp->f_ci->m_fattr == 0)
Namjae Jeon070fb212021-05-26 17:57:12 +09002976 fp->f_ci->m_fattr =
2977 cpu_to_le32(smb2_get_dos_mode(&stat, le32_to_cpu(req->FileAttributes)));
Namjae Jeone2f34482021-03-16 10:49:09 +09002978
2979 if (!created)
2980 smb2_update_xattrs(tcon, &path, fp);
2981 else
2982 smb2_new_xattrs(tcon, &path, fp);
2983
2984 memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE);
2985
Namjae Jeone2f34482021-03-16 10:49:09 +09002986 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
2987
2988 rsp->StructureSize = cpu_to_le16(89);
2989 rcu_read_lock();
2990 opinfo = rcu_dereference(fp->f_opinfo);
2991 rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0;
2992 rcu_read_unlock();
2993 rsp->Reserved = 0;
2994 rsp->CreateAction = cpu_to_le32(file_info);
2995 rsp->CreationTime = cpu_to_le64(fp->create_time);
2996 time = ksmbd_UnixTimeToNT(stat.atime);
2997 rsp->LastAccessTime = cpu_to_le64(time);
2998 time = ksmbd_UnixTimeToNT(stat.mtime);
2999 rsp->LastWriteTime = cpu_to_le64(time);
3000 time = ksmbd_UnixTimeToNT(stat.ctime);
3001 rsp->ChangeTime = cpu_to_le64(time);
3002 rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 :
3003 cpu_to_le64(stat.blocks << 9);
3004 rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
3005 rsp->FileAttributes = fp->f_ci->m_fattr;
3006
3007 rsp->Reserved2 = 0;
3008
3009 rsp->PersistentFileId = cpu_to_le64(fp->persistent_id);
3010 rsp->VolatileFileId = cpu_to_le64(fp->volatile_id);
3011
3012 rsp->CreateContextsOffset = 0;
3013 rsp->CreateContextsLength = 0;
3014 inc_rfc1001_len(rsp_org, 88); /* StructureSize - 1*/
3015
3016 /* If lease is request send lease context response */
3017 if (opinfo && opinfo->is_lease) {
3018 struct create_context *lease_ccontext;
3019
3020 ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003021 name, opinfo->o_lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +09003022 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
3023
3024 lease_ccontext = (struct create_context *)rsp->Buffer;
3025 contxt_cnt++;
3026 create_lease_buf(rsp->Buffer, opinfo->o_lease);
3027 le32_add_cpu(&rsp->CreateContextsLength,
3028 conn->vals->create_lease_size);
3029 inc_rfc1001_len(rsp_org, conn->vals->create_lease_size);
3030 next_ptr = &lease_ccontext->Next;
3031 next_off = conn->vals->create_lease_size;
3032 }
3033
Namjae Jeone2f34482021-03-16 10:49:09 +09003034 if (maximal_access_ctxt) {
3035 struct create_context *mxac_ccontext;
3036
3037 if (maximal_access == 0)
3038 ksmbd_vfs_query_maximal_access(path.dentry,
3039 &maximal_access);
3040 mxac_ccontext = (struct create_context *)(rsp->Buffer +
3041 le32_to_cpu(rsp->CreateContextsLength));
3042 contxt_cnt++;
3043 create_mxac_rsp_buf(rsp->Buffer +
3044 le32_to_cpu(rsp->CreateContextsLength),
3045 le32_to_cpu(maximal_access));
3046 le32_add_cpu(&rsp->CreateContextsLength,
3047 conn->vals->create_mxac_size);
3048 inc_rfc1001_len(rsp_org, conn->vals->create_mxac_size);
3049 if (next_ptr)
3050 *next_ptr = cpu_to_le32(next_off);
3051 next_ptr = &mxac_ccontext->Next;
3052 next_off = conn->vals->create_mxac_size;
3053 }
3054
3055 if (query_disk_id) {
3056 struct create_context *disk_id_ccontext;
3057
3058 disk_id_ccontext = (struct create_context *)(rsp->Buffer +
3059 le32_to_cpu(rsp->CreateContextsLength));
3060 contxt_cnt++;
3061 create_disk_id_rsp_buf(rsp->Buffer +
3062 le32_to_cpu(rsp->CreateContextsLength),
3063 stat.ino, tcon->id);
3064 le32_add_cpu(&rsp->CreateContextsLength,
3065 conn->vals->create_disk_id_size);
3066 inc_rfc1001_len(rsp_org, conn->vals->create_disk_id_size);
3067 if (next_ptr)
3068 *next_ptr = cpu_to_le32(next_off);
3069 next_ptr = &disk_id_ccontext->Next;
3070 next_off = conn->vals->create_disk_id_size;
3071 }
3072
3073 if (posix_ctxt) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003074 contxt_cnt++;
3075 create_posix_rsp_buf(rsp->Buffer +
3076 le32_to_cpu(rsp->CreateContextsLength),
3077 fp);
3078 le32_add_cpu(&rsp->CreateContextsLength,
3079 conn->vals->create_posix_size);
3080 inc_rfc1001_len(rsp_org, conn->vals->create_posix_size);
3081 if (next_ptr)
3082 *next_ptr = cpu_to_le32(next_off);
3083 }
3084
3085 if (contxt_cnt > 0) {
3086 rsp->CreateContextsOffset =
3087 cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer)
3088 - 4);
3089 }
3090
3091err_out:
3092 if (file_present || created)
3093 path_put(&path);
3094 ksmbd_revert_fsids(work);
3095err_out1:
3096 if (rc) {
3097 if (rc == -EINVAL)
3098 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3099 else if (rc == -EOPNOTSUPP)
3100 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeonff1d5722021-04-13 13:18:10 +09003101 else if (rc == -EACCES || rc == -ESTALE)
Namjae Jeone2f34482021-03-16 10:49:09 +09003102 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3103 else if (rc == -ENOENT)
3104 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
3105 else if (rc == -EPERM)
3106 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
3107 else if (rc == -EBUSY)
3108 rsp->hdr.Status = STATUS_DELETE_PENDING;
3109 else if (rc == -EBADF)
3110 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
3111 else if (rc == -ENOEXEC)
3112 rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID;
3113 else if (rc == -ENXIO)
3114 rsp->hdr.Status = STATUS_NO_SUCH_DEVICE;
3115 else if (rc == -EEXIST)
3116 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
3117 else if (rc == -EMFILE)
3118 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
3119 if (!rsp->hdr.Status)
3120 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3121
3122 if (!fp || !fp->filename)
3123 kfree(name);
3124 if (fp)
3125 ksmbd_fd_put(work, fp);
3126 smb2_set_err_rsp(work);
3127 ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status);
3128 }
3129
3130 kfree(lc);
3131
3132 return 0;
3133}
3134
3135static int readdir_info_level_struct_sz(int info_level)
3136{
3137 switch (info_level) {
3138 case FILE_FULL_DIRECTORY_INFORMATION:
3139 return sizeof(struct file_full_directory_info);
3140 case FILE_BOTH_DIRECTORY_INFORMATION:
3141 return sizeof(struct file_both_directory_info);
3142 case FILE_DIRECTORY_INFORMATION:
3143 return sizeof(struct file_directory_info);
3144 case FILE_NAMES_INFORMATION:
3145 return sizeof(struct file_names_info);
3146 case FILEID_FULL_DIRECTORY_INFORMATION:
3147 return sizeof(struct file_id_full_dir_info);
3148 case FILEID_BOTH_DIRECTORY_INFORMATION:
3149 return sizeof(struct file_id_both_directory_info);
3150 case SMB_FIND_FILE_POSIX_INFO:
3151 return sizeof(struct smb2_posix_info);
3152 default:
3153 return -EOPNOTSUPP;
3154 }
3155}
3156
3157static int dentry_name(struct ksmbd_dir_info *d_info, int info_level)
3158{
3159 switch (info_level) {
3160 case FILE_FULL_DIRECTORY_INFORMATION:
3161 {
3162 struct file_full_directory_info *ffdinfo;
3163
3164 ffdinfo = (struct file_full_directory_info *)d_info->rptr;
3165 d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset);
3166 d_info->name = ffdinfo->FileName;
3167 d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength);
3168 return 0;
3169 }
3170 case FILE_BOTH_DIRECTORY_INFORMATION:
3171 {
3172 struct file_both_directory_info *fbdinfo;
3173
3174 fbdinfo = (struct file_both_directory_info *)d_info->rptr;
3175 d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset);
3176 d_info->name = fbdinfo->FileName;
3177 d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength);
3178 return 0;
3179 }
3180 case FILE_DIRECTORY_INFORMATION:
3181 {
3182 struct file_directory_info *fdinfo;
3183
3184 fdinfo = (struct file_directory_info *)d_info->rptr;
3185 d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset);
3186 d_info->name = fdinfo->FileName;
3187 d_info->name_len = le32_to_cpu(fdinfo->FileNameLength);
3188 return 0;
3189 }
3190 case FILE_NAMES_INFORMATION:
3191 {
3192 struct file_names_info *fninfo;
3193
3194 fninfo = (struct file_names_info *)d_info->rptr;
3195 d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset);
3196 d_info->name = fninfo->FileName;
3197 d_info->name_len = le32_to_cpu(fninfo->FileNameLength);
3198 return 0;
3199 }
3200 case FILEID_FULL_DIRECTORY_INFORMATION:
3201 {
3202 struct file_id_full_dir_info *dinfo;
3203
3204 dinfo = (struct file_id_full_dir_info *)d_info->rptr;
3205 d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset);
3206 d_info->name = dinfo->FileName;
3207 d_info->name_len = le32_to_cpu(dinfo->FileNameLength);
3208 return 0;
3209 }
3210 case FILEID_BOTH_DIRECTORY_INFORMATION:
3211 {
3212 struct file_id_both_directory_info *fibdinfo;
3213
3214 fibdinfo = (struct file_id_both_directory_info *)d_info->rptr;
3215 d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset);
3216 d_info->name = fibdinfo->FileName;
3217 d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength);
3218 return 0;
3219 }
3220 case SMB_FIND_FILE_POSIX_INFO:
3221 {
3222 struct smb2_posix_info *posix_info;
3223
3224 posix_info = (struct smb2_posix_info *)d_info->rptr;
3225 d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset);
3226 d_info->name = posix_info->name;
3227 d_info->name_len = le32_to_cpu(posix_info->name_len);
3228 return 0;
3229 }
3230 default:
3231 return -EINVAL;
3232 }
3233}
3234
3235/**
3236 * smb2_populate_readdir_entry() - encode directory entry in smb2 response
3237 * buffer
3238 * @conn: connection instance
3239 * @info_level: smb information level
3240 * @d_info: structure included variables for query dir
3241 * @ksmbd_kstat: ksmbd wrapper of dirent stat information
3242 *
3243 * if directory has many entries, find first can't read it fully.
3244 * find next might be called multiple times to read remaining dir entries
3245 *
3246 * Return: 0 on success, otherwise error
3247 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003248static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level,
Namjae Jeon070fb212021-05-26 17:57:12 +09003249 struct ksmbd_dir_info *d_info,
3250 struct ksmbd_kstat *ksmbd_kstat)
Namjae Jeone2f34482021-03-16 10:49:09 +09003251{
3252 int next_entry_offset = 0;
3253 char *conv_name;
3254 int conv_len;
3255 void *kstat;
3256 int struct_sz;
3257
3258 conv_name = ksmbd_convert_dir_info_name(d_info,
3259 conn->local_nls,
3260 &conv_len);
3261 if (!conv_name)
3262 return -ENOMEM;
3263
3264 /* Somehow the name has only terminating NULL bytes */
3265 if (conv_len < 0) {
3266 kfree(conv_name);
3267 return -EINVAL;
3268 }
3269
3270 struct_sz = readdir_info_level_struct_sz(info_level);
3271 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3272 KSMBD_DIR_INFO_ALIGNMENT);
3273
3274 if (next_entry_offset > d_info->out_buf_len) {
3275 d_info->out_buf_len = 0;
3276 return -ENOSPC;
3277 }
3278
3279 kstat = d_info->wptr;
3280 if (info_level != FILE_NAMES_INFORMATION)
3281 kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat);
3282
3283 switch (info_level) {
3284 case FILE_FULL_DIRECTORY_INFORMATION:
3285 {
3286 struct file_full_directory_info *ffdinfo;
3287
3288 ffdinfo = (struct file_full_directory_info *)kstat;
3289 ffdinfo->FileNameLength = cpu_to_le32(conv_len);
3290 ffdinfo->EaSize =
3291 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3292 if (ffdinfo->EaSize)
3293 ffdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3294 if (d_info->hide_dot_file && d_info->name[0] == '.')
3295 ffdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3296 memcpy(ffdinfo->FileName, conv_name, conv_len);
3297 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3298 break;
3299 }
3300 case FILE_BOTH_DIRECTORY_INFORMATION:
3301 {
3302 struct file_both_directory_info *fbdinfo;
3303
3304 fbdinfo = (struct file_both_directory_info *)kstat;
3305 fbdinfo->FileNameLength = cpu_to_le32(conv_len);
3306 fbdinfo->EaSize =
3307 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3308 if (fbdinfo->EaSize)
3309 fbdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3310 fbdinfo->ShortNameLength = 0;
3311 fbdinfo->Reserved = 0;
3312 if (d_info->hide_dot_file && d_info->name[0] == '.')
3313 fbdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3314 memcpy(fbdinfo->FileName, conv_name, conv_len);
3315 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3316 break;
3317 }
3318 case FILE_DIRECTORY_INFORMATION:
3319 {
3320 struct file_directory_info *fdinfo;
3321
3322 fdinfo = (struct file_directory_info *)kstat;
3323 fdinfo->FileNameLength = cpu_to_le32(conv_len);
3324 if (d_info->hide_dot_file && d_info->name[0] == '.')
3325 fdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3326 memcpy(fdinfo->FileName, conv_name, conv_len);
3327 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3328 break;
3329 }
3330 case FILE_NAMES_INFORMATION:
3331 {
3332 struct file_names_info *fninfo;
3333
3334 fninfo = (struct file_names_info *)kstat;
3335 fninfo->FileNameLength = cpu_to_le32(conv_len);
3336 memcpy(fninfo->FileName, conv_name, conv_len);
3337 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3338 break;
3339 }
3340 case FILEID_FULL_DIRECTORY_INFORMATION:
3341 {
3342 struct file_id_full_dir_info *dinfo;
3343
3344 dinfo = (struct file_id_full_dir_info *)kstat;
3345 dinfo->FileNameLength = cpu_to_le32(conv_len);
3346 dinfo->EaSize =
3347 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3348 if (dinfo->EaSize)
3349 dinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3350 dinfo->Reserved = 0;
3351 dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3352 if (d_info->hide_dot_file && d_info->name[0] == '.')
3353 dinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3354 memcpy(dinfo->FileName, conv_name, conv_len);
3355 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3356 break;
3357 }
3358 case FILEID_BOTH_DIRECTORY_INFORMATION:
3359 {
3360 struct file_id_both_directory_info *fibdinfo;
3361
3362 fibdinfo = (struct file_id_both_directory_info *)kstat;
3363 fibdinfo->FileNameLength = cpu_to_le32(conv_len);
3364 fibdinfo->EaSize =
3365 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3366 if (fibdinfo->EaSize)
3367 fibdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3368 fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3369 fibdinfo->ShortNameLength = 0;
3370 fibdinfo->Reserved = 0;
3371 fibdinfo->Reserved2 = cpu_to_le16(0);
3372 if (d_info->hide_dot_file && d_info->name[0] == '.')
3373 fibdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3374 memcpy(fibdinfo->FileName, conv_name, conv_len);
3375 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3376 break;
3377 }
3378 case SMB_FIND_FILE_POSIX_INFO:
3379 {
3380 struct smb2_posix_info *posix_info;
3381 u64 time;
3382
3383 posix_info = (struct smb2_posix_info *)kstat;
3384 posix_info->Ignored = 0;
3385 posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
3386 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
3387 posix_info->ChangeTime = cpu_to_le64(time);
3388 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime);
3389 posix_info->LastAccessTime = cpu_to_le64(time);
3390 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime);
3391 posix_info->LastWriteTime = cpu_to_le64(time);
3392 posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size);
3393 posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9);
3394 posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev);
3395 posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink);
3396 posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode);
3397 posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino);
3398 posix_info->DosAttributes =
3399 S_ISDIR(ksmbd_kstat->kstat->mode) ? ATTR_DIRECTORY_LE : ATTR_ARCHIVE_LE;
3400 if (d_info->hide_dot_file && d_info->name[0] == '.')
3401 posix_info->DosAttributes |= ATTR_HIDDEN_LE;
3402 id_to_sid(from_kuid(&init_user_ns, ksmbd_kstat->kstat->uid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003403 SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09003404 id_to_sid(from_kgid(&init_user_ns, ksmbd_kstat->kstat->gid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003405 SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]);
Namjae Jeone2f34482021-03-16 10:49:09 +09003406 memcpy(posix_info->name, conv_name, conv_len);
3407 posix_info->name_len = cpu_to_le32(conv_len);
3408 posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset);
3409 break;
3410 }
3411
3412 } /* switch (info_level) */
3413
3414 d_info->last_entry_offset = d_info->data_count;
3415 d_info->data_count += next_entry_offset;
Marios Makassikise7735c82021-05-06 11:40:02 +09003416 d_info->out_buf_len -= next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003417 d_info->wptr += next_entry_offset;
3418 kfree(conv_name);
3419
3420 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003421 "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n",
3422 info_level, d_info->out_buf_len,
3423 next_entry_offset, d_info->data_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09003424
3425 return 0;
3426}
3427
3428struct smb2_query_dir_private {
3429 struct ksmbd_work *work;
3430 char *search_pattern;
3431 struct ksmbd_file *dir_fp;
3432
3433 struct ksmbd_dir_info *d_info;
3434 int info_level;
3435};
3436
3437static void lock_dir(struct ksmbd_file *dir_fp)
3438{
3439 struct dentry *dir = dir_fp->filp->f_path.dentry;
3440
3441 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
3442}
3443
3444static void unlock_dir(struct ksmbd_file *dir_fp)
3445{
3446 struct dentry *dir = dir_fp->filp->f_path.dentry;
3447
3448 inode_unlock(d_inode(dir));
3449}
3450
3451static int process_query_dir_entries(struct smb2_query_dir_private *priv)
3452{
3453 struct kstat kstat;
3454 struct ksmbd_kstat ksmbd_kstat;
3455 int rc;
3456 int i;
3457
3458 for (i = 0; i < priv->d_info->num_entry; i++) {
3459 struct dentry *dent;
3460
3461 if (dentry_name(priv->d_info, priv->info_level))
3462 return -EINVAL;
3463
3464 lock_dir(priv->dir_fp);
3465 dent = lookup_one_len(priv->d_info->name,
3466 priv->dir_fp->filp->f_path.dentry,
3467 priv->d_info->name_len);
3468 unlock_dir(priv->dir_fp);
3469
3470 if (IS_ERR(dent)) {
3471 ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003472 priv->d_info->name,
3473 PTR_ERR(dent));
Namjae Jeone2f34482021-03-16 10:49:09 +09003474 continue;
3475 }
3476 if (unlikely(d_is_negative(dent))) {
3477 dput(dent);
3478 ksmbd_debug(SMB, "Negative dentry `%s'\n",
3479 priv->d_info->name);
3480 continue;
3481 }
3482
3483 ksmbd_kstat.kstat = &kstat;
3484 if (priv->info_level != FILE_NAMES_INFORMATION)
3485 ksmbd_vfs_fill_dentry_attrs(priv->work,
3486 dent,
3487 &ksmbd_kstat);
3488
3489 rc = smb2_populate_readdir_entry(priv->work->conn,
3490 priv->info_level,
3491 priv->d_info,
3492 &ksmbd_kstat);
3493 dput(dent);
3494 if (rc)
3495 return rc;
3496 }
3497 return 0;
3498}
3499
3500static int reserve_populate_dentry(struct ksmbd_dir_info *d_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09003501 int info_level)
Namjae Jeone2f34482021-03-16 10:49:09 +09003502{
3503 int struct_sz;
3504 int conv_len;
3505 int next_entry_offset;
3506
3507 struct_sz = readdir_info_level_struct_sz(info_level);
3508 if (struct_sz == -EOPNOTSUPP)
3509 return -EOPNOTSUPP;
3510
3511 conv_len = (d_info->name_len + 1) * 2;
3512 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3513 KSMBD_DIR_INFO_ALIGNMENT);
3514
3515 if (next_entry_offset > d_info->out_buf_len) {
3516 d_info->out_buf_len = 0;
3517 return -ENOSPC;
3518 }
3519
3520 switch (info_level) {
3521 case FILE_FULL_DIRECTORY_INFORMATION:
3522 {
3523 struct file_full_directory_info *ffdinfo;
3524
3525 ffdinfo = (struct file_full_directory_info *)d_info->wptr;
3526 memcpy(ffdinfo->FileName, d_info->name, d_info->name_len);
3527 ffdinfo->FileName[d_info->name_len] = 0x00;
3528 ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3529 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3530 break;
3531 }
3532 case FILE_BOTH_DIRECTORY_INFORMATION:
3533 {
3534 struct file_both_directory_info *fbdinfo;
3535
3536 fbdinfo = (struct file_both_directory_info *)d_info->wptr;
3537 memcpy(fbdinfo->FileName, d_info->name, d_info->name_len);
3538 fbdinfo->FileName[d_info->name_len] = 0x00;
3539 fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3540 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3541 break;
3542 }
3543 case FILE_DIRECTORY_INFORMATION:
3544 {
3545 struct file_directory_info *fdinfo;
3546
3547 fdinfo = (struct file_directory_info *)d_info->wptr;
3548 memcpy(fdinfo->FileName, d_info->name, d_info->name_len);
3549 fdinfo->FileName[d_info->name_len] = 0x00;
3550 fdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3551 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3552 break;
3553 }
3554 case FILE_NAMES_INFORMATION:
3555 {
3556 struct file_names_info *fninfo;
3557
3558 fninfo = (struct file_names_info *)d_info->wptr;
3559 memcpy(fninfo->FileName, d_info->name, d_info->name_len);
3560 fninfo->FileName[d_info->name_len] = 0x00;
3561 fninfo->FileNameLength = cpu_to_le32(d_info->name_len);
3562 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3563 break;
3564 }
3565 case FILEID_FULL_DIRECTORY_INFORMATION:
3566 {
3567 struct file_id_full_dir_info *dinfo;
3568
3569 dinfo = (struct file_id_full_dir_info *)d_info->wptr;
3570 memcpy(dinfo->FileName, d_info->name, d_info->name_len);
3571 dinfo->FileName[d_info->name_len] = 0x00;
3572 dinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3573 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3574 break;
3575 }
3576 case FILEID_BOTH_DIRECTORY_INFORMATION:
3577 {
3578 struct file_id_both_directory_info *fibdinfo;
3579
3580 fibdinfo = (struct file_id_both_directory_info *)d_info->wptr;
3581 memcpy(fibdinfo->FileName, d_info->name, d_info->name_len);
3582 fibdinfo->FileName[d_info->name_len] = 0x00;
3583 fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3584 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3585 break;
3586 }
3587 case SMB_FIND_FILE_POSIX_INFO:
3588 {
3589 struct smb2_posix_info *posix_info;
3590
3591 posix_info = (struct smb2_posix_info *)d_info->wptr;
3592 memcpy(posix_info->name, d_info->name, d_info->name_len);
3593 posix_info->name[d_info->name_len] = 0x00;
3594 posix_info->name_len = cpu_to_le32(d_info->name_len);
3595 posix_info->NextEntryOffset =
3596 cpu_to_le32(next_entry_offset);
3597 break;
3598 }
3599 } /* switch (info_level) */
3600
3601 d_info->num_entry++;
3602 d_info->out_buf_len -= next_entry_offset;
3603 d_info->wptr += next_entry_offset;
3604 return 0;
3605}
3606
Namjae Jeon64b39f42021-03-30 14:25:35 +09003607static int __query_dir(struct dir_context *ctx, const char *name, int namlen,
Namjae Jeon070fb212021-05-26 17:57:12 +09003608 loff_t offset, u64 ino, unsigned int d_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09003609{
3610 struct ksmbd_readdir_data *buf;
3611 struct smb2_query_dir_private *priv;
3612 struct ksmbd_dir_info *d_info;
3613 int rc;
3614
3615 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
3616 priv = buf->private;
3617 d_info = priv->d_info;
3618
3619 /* dot and dotdot entries are already reserved */
3620 if (!strcmp(".", name) || !strcmp("..", name))
3621 return 0;
3622 if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name))
3623 return 0;
Namjae Jeonb24c9332021-03-21 17:32:19 +09003624 if (!match_pattern(name, namlen, priv->search_pattern))
Namjae Jeone2f34482021-03-16 10:49:09 +09003625 return 0;
3626
3627 d_info->name = name;
3628 d_info->name_len = namlen;
3629 rc = reserve_populate_dentry(d_info, priv->info_level);
3630 if (rc)
3631 return rc;
3632 if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
3633 d_info->out_buf_len = 0;
3634 return 0;
3635 }
3636 return 0;
3637}
3638
3639static void restart_ctx(struct dir_context *ctx)
3640{
3641 ctx->pos = 0;
3642}
3643
3644static int verify_info_level(int info_level)
3645{
3646 switch (info_level) {
3647 case FILE_FULL_DIRECTORY_INFORMATION:
3648 case FILE_BOTH_DIRECTORY_INFORMATION:
3649 case FILE_DIRECTORY_INFORMATION:
3650 case FILE_NAMES_INFORMATION:
3651 case FILEID_FULL_DIRECTORY_INFORMATION:
3652 case FILEID_BOTH_DIRECTORY_INFORMATION:
3653 case SMB_FIND_FILE_POSIX_INFO:
3654 break;
3655 default:
3656 return -EOPNOTSUPP;
3657 }
3658
3659 return 0;
3660}
3661
3662int smb2_query_dir(struct ksmbd_work *work)
3663{
3664 struct ksmbd_conn *conn = work->conn;
3665 struct smb2_query_directory_req *req;
3666 struct smb2_query_directory_rsp *rsp, *rsp_org;
3667 struct ksmbd_share_config *share = work->tcon->share_conf;
3668 struct ksmbd_file *dir_fp = NULL;
3669 struct ksmbd_dir_info d_info;
3670 int rc = 0;
3671 char *srch_ptr = NULL;
3672 unsigned char srch_flag;
3673 int buffer_sz;
3674 struct smb2_query_dir_private query_dir_private = {NULL, };
3675
Namjae Jeone5066492021-03-30 12:35:23 +09003676 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09003677 WORK_BUFFERS(work, req, rsp);
3678
3679 if (ksmbd_override_fsids(work)) {
3680 rsp->hdr.Status = STATUS_NO_MEMORY;
3681 smb2_set_err_rsp(work);
3682 return -ENOMEM;
3683 }
3684
3685 rc = verify_info_level(req->FileInformationClass);
3686 if (rc) {
3687 rc = -EFAULT;
3688 goto err_out2;
3689 }
3690
3691 dir_fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09003692 le64_to_cpu(req->VolatileFileId),
3693 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09003694 if (!dir_fp) {
3695 rc = -EBADF;
3696 goto err_out2;
3697 }
3698
3699 if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) ||
Namjae Jeon070fb212021-05-26 17:57:12 +09003700 inode_permission(&init_user_ns, file_inode(dir_fp->filp),
3701 MAY_READ | MAY_EXEC)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003702 ksmbd_err("no right to enumerate directory (%s)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003703 FP_FILENAME(dir_fp));
Namjae Jeone2f34482021-03-16 10:49:09 +09003704 rc = -EACCES;
3705 goto err_out2;
3706 }
3707
3708 if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) {
3709 ksmbd_err("can't do query dir for a file\n");
3710 rc = -EINVAL;
3711 goto err_out2;
3712 }
3713
3714 srch_flag = req->Flags;
3715 srch_ptr = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09003716 le16_to_cpu(req->FileNameLength), 1,
3717 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09003718 if (IS_ERR(srch_ptr)) {
3719 ksmbd_debug(SMB, "Search Pattern not found\n");
3720 rc = -EINVAL;
3721 goto err_out2;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003722 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003723 ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr);
Namjae Jeon64b39f42021-03-30 14:25:35 +09003724 }
Namjae Jeone2f34482021-03-16 10:49:09 +09003725
3726 ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename);
3727
3728 if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) {
3729 ksmbd_debug(SMB, "Restart directory scan\n");
3730 generic_file_llseek(dir_fp->filp, 0, SEEK_SET);
3731 restart_ctx(&dir_fp->readdir_data.ctx);
3732 }
3733
3734 memset(&d_info, 0, sizeof(struct ksmbd_dir_info));
3735 d_info.wptr = (char *)rsp->Buffer;
3736 d_info.rptr = (char *)rsp->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003737 d_info.out_buf_len = (work->response_sz - (get_rfc1002_len(rsp_org) + 4));
Namjae Jeon070fb212021-05-26 17:57:12 +09003738 d_info.out_buf_len = min_t(int, d_info.out_buf_len, le32_to_cpu(req->OutputBufferLength)) -
3739 sizeof(struct smb2_query_directory_rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003740 d_info.flags = srch_flag;
3741
3742 /*
3743 * reserve dot and dotdot entries in head of buffer
3744 * in first response
3745 */
3746 rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass,
Namjae Jeon070fb212021-05-26 17:57:12 +09003747 dir_fp, &d_info, srch_ptr,
3748 smb2_populate_readdir_entry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003749 if (rc == -ENOSPC)
3750 rc = 0;
3751 else if (rc)
3752 goto err_out;
3753
3754 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES))
3755 d_info.hide_dot_file = true;
3756
3757 buffer_sz = d_info.out_buf_len;
3758 d_info.rptr = d_info.wptr;
3759 query_dir_private.work = work;
3760 query_dir_private.search_pattern = srch_ptr;
3761 query_dir_private.dir_fp = dir_fp;
3762 query_dir_private.d_info = &d_info;
3763 query_dir_private.info_level = req->FileInformationClass;
3764 dir_fp->readdir_data.private = &query_dir_private;
3765 set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir);
3766
3767 rc = ksmbd_vfs_readdir(dir_fp->filp, &dir_fp->readdir_data);
3768 if (rc == 0)
3769 restart_ctx(&dir_fp->readdir_data.ctx);
3770 if (rc == -ENOSPC)
3771 rc = 0;
3772 if (rc)
3773 goto err_out;
3774
3775 d_info.wptr = d_info.rptr;
3776 d_info.out_buf_len = buffer_sz;
3777 rc = process_query_dir_entries(&query_dir_private);
3778 if (rc)
3779 goto err_out;
3780
3781 if (!d_info.data_count && d_info.out_buf_len >= 0) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09003782 if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003783 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003784 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003785 dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0;
3786 rsp->hdr.Status = STATUS_NO_MORE_FILES;
3787 }
3788 rsp->StructureSize = cpu_to_le16(9);
3789 rsp->OutputBufferOffset = cpu_to_le16(0);
3790 rsp->OutputBufferLength = cpu_to_le32(0);
3791 rsp->Buffer[0] = 0;
3792 inc_rfc1001_len(rsp_org, 9);
3793 } else {
3794 ((struct file_directory_info *)
3795 ((char *)rsp->Buffer + d_info.last_entry_offset))
3796 ->NextEntryOffset = 0;
3797
3798 rsp->StructureSize = cpu_to_le16(9);
3799 rsp->OutputBufferOffset = cpu_to_le16(72);
3800 rsp->OutputBufferLength = cpu_to_le32(d_info.data_count);
3801 inc_rfc1001_len(rsp_org, 8 + d_info.data_count);
3802 }
3803
3804 kfree(srch_ptr);
3805 ksmbd_fd_put(work, dir_fp);
3806 ksmbd_revert_fsids(work);
3807 return 0;
3808
3809err_out:
3810 ksmbd_err("error while processing smb2 query dir rc = %d\n", rc);
3811 kfree(srch_ptr);
3812
3813err_out2:
3814 if (rc == -EINVAL)
3815 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3816 else if (rc == -EACCES)
3817 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3818 else if (rc == -ENOENT)
3819 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
3820 else if (rc == -EBADF)
3821 rsp->hdr.Status = STATUS_FILE_CLOSED;
3822 else if (rc == -ENOMEM)
3823 rsp->hdr.Status = STATUS_NO_MEMORY;
3824 else if (rc == -EFAULT)
3825 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
3826 if (!rsp->hdr.Status)
3827 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3828
3829 smb2_set_err_rsp(work);
3830 ksmbd_fd_put(work, dir_fp);
3831 ksmbd_revert_fsids(work);
3832 return 0;
3833}
3834
3835/**
3836 * buffer_check_err() - helper function to check buffer errors
3837 * @reqOutputBufferLength: max buffer length expected in command response
3838 * @rsp: query info response buffer contains output buffer length
3839 * @infoclass_size: query info class response buffer size
3840 *
3841 * Return: 0 on success, otherwise error
3842 */
3843static int buffer_check_err(int reqOutputBufferLength,
Namjae Jeon070fb212021-05-26 17:57:12 +09003844 struct smb2_query_info_rsp *rsp, int infoclass_size)
Namjae Jeone2f34482021-03-16 10:49:09 +09003845{
3846 if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) {
3847 if (reqOutputBufferLength < infoclass_size) {
3848 ksmbd_err("Invalid Buffer Size Requested\n");
3849 rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003850 rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09003851 return -EINVAL;
3852 }
3853
3854 ksmbd_debug(SMB, "Buffer Overflow\n");
3855 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003856 rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4 +
3857 reqOutputBufferLength);
3858 rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09003859 }
3860 return 0;
3861}
3862
3863static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp)
3864{
3865 struct smb2_file_standard_info *sinfo;
3866
3867 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
3868
3869 sinfo->AllocationSize = cpu_to_le64(4096);
3870 sinfo->EndOfFile = cpu_to_le64(0);
3871 sinfo->NumberOfLinks = cpu_to_le32(1);
3872 sinfo->DeletePending = 1;
3873 sinfo->Directory = 0;
3874 rsp->OutputBufferLength =
3875 cpu_to_le32(sizeof(struct smb2_file_standard_info));
3876 inc_rfc1001_len(rsp, sizeof(struct smb2_file_standard_info));
3877}
3878
Namjae Jeon070fb212021-05-26 17:57:12 +09003879static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num)
Namjae Jeone2f34482021-03-16 10:49:09 +09003880{
3881 struct smb2_file_internal_info *file_info;
3882
3883 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
3884
3885 /* any unique number */
3886 file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63));
3887 rsp->OutputBufferLength =
3888 cpu_to_le32(sizeof(struct smb2_file_internal_info));
3889 inc_rfc1001_len(rsp, sizeof(struct smb2_file_internal_info));
3890}
3891
Namjae Jeone2f34482021-03-16 10:49:09 +09003892static int smb2_get_info_file_pipe(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09003893 struct smb2_query_info_req *req,
3894 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09003895{
Namjae Jeon64b39f42021-03-30 14:25:35 +09003896 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09003897 int rc;
3898
3899 /*
3900 * Windows can sometime send query file info request on
3901 * pipe without opening it, checking error condition here
3902 */
3903 id = le64_to_cpu(req->VolatileFileId);
3904 if (!ksmbd_session_rpc_method(sess, id))
3905 return -ENOENT;
3906
3907 ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003908 req->FileInfoClass, le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09003909
3910 switch (req->FileInfoClass) {
3911 case FILE_STANDARD_INFORMATION:
3912 get_standard_info_pipe(rsp);
3913 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09003914 rsp, FILE_STANDARD_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09003915 break;
3916 case FILE_INTERNAL_INFORMATION:
3917 get_internal_info_pipe(rsp, id);
3918 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09003919 rsp, FILE_INTERNAL_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09003920 break;
3921 default:
3922 ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003923 req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09003924 rc = -EOPNOTSUPP;
3925 }
3926 return rc;
3927}
3928
3929/**
3930 * smb2_get_ea() - handler for smb2 get extended attribute command
3931 * @work: smb work containing query info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09003932 * @fp: ksmbd_file pointer
3933 * @req: get extended attribute request
3934 * @rsp: response buffer pointer
3935 * @rsp_org: base response buffer pointer in case of chained response
Namjae Jeone2f34482021-03-16 10:49:09 +09003936 *
3937 * Return: 0 on success, otherwise error
3938 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003939static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09003940 struct smb2_query_info_req *req,
3941 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09003942{
3943 struct smb2_ea_info *eainfo, *prev_eainfo;
3944 char *name, *ptr, *xattr_list = NULL, *buf;
3945 int rc, name_len, value_len, xattr_list_len, idx;
3946 ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0;
3947 struct smb2_ea_info_req *ea_req = NULL;
3948 struct path *path;
3949
3950 if (!(fp->daccess & FILE_READ_EA_LE)) {
3951 ksmbd_err("Not permitted to read ext attr : 0x%x\n",
3952 fp->daccess);
3953 return -EACCES;
3954 }
3955
3956 path = &fp->filp->f_path;
3957 /* single EA entry is requested with given user.* name */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003958 if (req->InputBufferLength) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003959 ea_req = (struct smb2_ea_info_req *)req->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003960 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003961 /* need to send all EAs, if no specific EA is requested*/
3962 if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY)
3963 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003964 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n",
3965 le32_to_cpu(req->Flags));
Namjae Jeone2f34482021-03-16 10:49:09 +09003966 }
3967
3968 buf_free_len = work->response_sz -
3969 (get_rfc1002_len(rsp_org) + 4) -
3970 sizeof(struct smb2_query_info_rsp);
3971
3972 if (le32_to_cpu(req->OutputBufferLength) < buf_free_len)
3973 buf_free_len = le32_to_cpu(req->OutputBufferLength);
3974
3975 rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
3976 if (rc < 0) {
3977 rsp->hdr.Status = STATUS_INVALID_HANDLE;
3978 goto out;
3979 } else if (!rc) { /* there is no EA in the file */
3980 ksmbd_debug(SMB, "no ea data in the file\n");
3981 goto done;
3982 }
3983 xattr_list_len = rc;
3984
3985 ptr = (char *)rsp->Buffer;
3986 eainfo = (struct smb2_ea_info *)ptr;
3987 prev_eainfo = eainfo;
3988 idx = 0;
3989
3990 while (idx < xattr_list_len) {
3991 name = xattr_list + idx;
3992 name_len = strlen(name);
3993
3994 ksmbd_debug(SMB, "%s, len %d\n", name, name_len);
3995 idx += name_len + 1;
3996
3997 /*
3998 * CIFS does not support EA other than user.* namespace,
3999 * still keep the framework generic, to list other attrs
4000 * in future.
4001 */
4002 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4003 continue;
4004
4005 if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004006 STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004007 continue;
4008
4009 if (req->InputBufferLength &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09004010 strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name,
4011 ea_req->EaNameLength))
Namjae Jeone2f34482021-03-16 10:49:09 +09004012 continue;
4013
4014 if (!strncmp(&name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004015 DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004016 continue;
4017
4018 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4019 name_len -= XATTR_USER_PREFIX_LEN;
4020
4021 ptr = (char *)(&eainfo->name + name_len + 1);
4022 buf_free_len -= (offsetof(struct smb2_ea_info, name) +
4023 name_len + 1);
4024 /* bailout if xattr can't fit in buf_free_len */
4025 value_len = ksmbd_vfs_getxattr(path->dentry, name, &buf);
4026 if (value_len <= 0) {
4027 rc = -ENOENT;
4028 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4029 goto out;
4030 }
4031
4032 buf_free_len -= value_len;
4033 if (buf_free_len < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09004034 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004035 break;
4036 }
4037
4038 memcpy(ptr, buf, value_len);
Namjae Jeon79f6b112021-04-02 12:47:14 +09004039 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004040
4041 ptr += value_len;
4042 eainfo->Flags = 0;
4043 eainfo->EaNameLength = name_len;
4044
Namjae Jeon64b39f42021-03-30 14:25:35 +09004045 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004046 memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN],
Namjae Jeon070fb212021-05-26 17:57:12 +09004047 name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09004048 else
4049 memcpy(eainfo->name, name, name_len);
4050
4051 eainfo->name[name_len] = '\0';
4052 eainfo->EaValueLength = cpu_to_le16(value_len);
4053 next_offset = offsetof(struct smb2_ea_info, name) +
4054 name_len + 1 + value_len;
4055
4056 /* align next xattr entry at 4 byte bundary */
4057 alignment_bytes = ((next_offset + 3) & ~3) - next_offset;
4058 if (alignment_bytes) {
4059 memset(ptr, '\0', alignment_bytes);
4060 ptr += alignment_bytes;
4061 next_offset += alignment_bytes;
4062 buf_free_len -= alignment_bytes;
4063 }
4064 eainfo->NextEntryOffset = cpu_to_le32(next_offset);
4065 prev_eainfo = eainfo;
4066 eainfo = (struct smb2_ea_info *)ptr;
4067 rsp_data_cnt += next_offset;
4068
4069 if (req->InputBufferLength) {
4070 ksmbd_debug(SMB, "single entry requested\n");
4071 break;
4072 }
4073 }
4074
4075 /* no more ea entries */
4076 prev_eainfo->NextEntryOffset = 0;
4077done:
4078 rc = 0;
4079 if (rsp_data_cnt == 0)
4080 rsp->hdr.Status = STATUS_NO_EAS_ON_FILE;
4081 rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt);
4082 inc_rfc1001_len(rsp_org, rsp_data_cnt);
4083out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004084 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004085 return rc;
4086}
4087
4088static void get_file_access_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004089 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004090{
4091 struct smb2_file_access_info *file_info;
4092
4093 file_info = (struct smb2_file_access_info *)rsp->Buffer;
4094 file_info->AccessFlags = fp->daccess;
4095 rsp->OutputBufferLength =
4096 cpu_to_le32(sizeof(struct smb2_file_access_info));
4097 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info));
4098}
4099
4100static int get_file_basic_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004101 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004102{
4103 struct smb2_file_all_info *basic_info;
4104 struct kstat stat;
4105 u64 time;
4106
4107 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4108 ksmbd_err("no right to read the attributes : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004109 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004110 return -EACCES;
4111 }
4112
4113 basic_info = (struct smb2_file_all_info *)rsp->Buffer;
4114 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
4115 basic_info->CreationTime = cpu_to_le64(fp->create_time);
4116 time = ksmbd_UnixTimeToNT(stat.atime);
4117 basic_info->LastAccessTime = cpu_to_le64(time);
4118 time = ksmbd_UnixTimeToNT(stat.mtime);
4119 basic_info->LastWriteTime = cpu_to_le64(time);
4120 time = ksmbd_UnixTimeToNT(stat.ctime);
4121 basic_info->ChangeTime = cpu_to_le64(time);
4122 basic_info->Attributes = fp->f_ci->m_fattr;
4123 basic_info->Pad1 = 0;
4124 rsp->OutputBufferLength =
Namjae Jeon64b39f42021-03-30 14:25:35 +09004125 cpu_to_le32(offsetof(struct smb2_file_all_info, AllocationSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09004126 inc_rfc1001_len(rsp_org, offsetof(struct smb2_file_all_info,
4127 AllocationSize));
4128 return 0;
4129}
4130
4131static unsigned long long get_allocation_size(struct inode *inode,
Namjae Jeon070fb212021-05-26 17:57:12 +09004132 struct kstat *stat)
Namjae Jeone2f34482021-03-16 10:49:09 +09004133{
4134 unsigned long long alloc_size = 0;
4135
4136 if (!S_ISDIR(stat->mode)) {
4137 if ((inode->i_blocks << 9) <= stat->size)
4138 alloc_size = stat->size;
4139 else
4140 alloc_size = inode->i_blocks << 9;
Namjae Jeone2f34482021-03-16 10:49:09 +09004141 }
4142
4143 return alloc_size;
4144}
4145
4146static void get_file_standard_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004147 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004148{
4149 struct smb2_file_standard_info *sinfo;
4150 unsigned int delete_pending;
4151 struct inode *inode;
4152 struct kstat stat;
4153
4154 inode = FP_INODE(fp);
4155 generic_fillattr(&init_user_ns, inode, &stat);
4156
4157 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4158 delete_pending = ksmbd_inode_pending_delete(fp);
4159
4160 sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat));
4161 sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4162 sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending);
4163 sinfo->DeletePending = delete_pending;
4164 sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4165 rsp->OutputBufferLength =
4166 cpu_to_le32(sizeof(struct smb2_file_standard_info));
4167 inc_rfc1001_len(rsp_org,
4168 sizeof(struct smb2_file_standard_info));
4169}
4170
4171static void get_file_alignment_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004172 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004173{
4174 struct smb2_file_alignment_info *file_info;
4175
4176 file_info = (struct smb2_file_alignment_info *)rsp->Buffer;
4177 file_info->AlignmentRequirement = 0;
4178 rsp->OutputBufferLength =
4179 cpu_to_le32(sizeof(struct smb2_file_alignment_info));
4180 inc_rfc1001_len(rsp_org,
4181 sizeof(struct smb2_file_alignment_info));
4182}
4183
4184static int get_file_all_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004185 struct smb2_query_info_rsp *rsp,
4186 struct ksmbd_file *fp,
4187 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004188{
4189 struct ksmbd_conn *conn = work->conn;
4190 struct smb2_file_all_info *file_info;
4191 unsigned int delete_pending;
4192 struct inode *inode;
4193 struct kstat stat;
4194 int conv_len;
4195 char *filename;
4196 u64 time;
4197
4198 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4199 ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004200 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004201 return -EACCES;
4202 }
4203
4204 filename = convert_to_nt_pathname(fp->filename,
4205 work->tcon->share_conf->path);
4206 if (!filename)
4207 return -ENOMEM;
4208
4209 inode = FP_INODE(fp);
4210 generic_fillattr(&init_user_ns, inode, &stat);
4211
4212 ksmbd_debug(SMB, "filename = %s\n", filename);
4213 delete_pending = ksmbd_inode_pending_delete(fp);
4214 file_info = (struct smb2_file_all_info *)rsp->Buffer;
4215
4216 file_info->CreationTime = cpu_to_le64(fp->create_time);
4217 time = ksmbd_UnixTimeToNT(stat.atime);
4218 file_info->LastAccessTime = cpu_to_le64(time);
4219 time = ksmbd_UnixTimeToNT(stat.mtime);
4220 file_info->LastWriteTime = cpu_to_le64(time);
4221 time = ksmbd_UnixTimeToNT(stat.ctime);
4222 file_info->ChangeTime = cpu_to_le64(time);
4223 file_info->Attributes = fp->f_ci->m_fattr;
4224 file_info->Pad1 = 0;
4225 file_info->AllocationSize =
4226 cpu_to_le64(get_allocation_size(inode, &stat));
4227 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4228 file_info->NumberOfLinks =
4229 cpu_to_le32(get_nlink(&stat) - delete_pending);
4230 file_info->DeletePending = delete_pending;
4231 file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4232 file_info->Pad2 = 0;
4233 file_info->IndexNumber = cpu_to_le64(stat.ino);
4234 file_info->EASize = 0;
4235 file_info->AccessFlags = fp->daccess;
4236 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4237 file_info->Mode = fp->coption;
4238 file_info->AlignmentRequirement = 0;
Namjae Jeon070fb212021-05-26 17:57:12 +09004239 conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename,
4240 PATH_MAX, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004241 conv_len *= 2;
4242 file_info->FileNameLength = cpu_to_le32(conv_len);
4243 rsp->OutputBufferLength =
4244 cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1);
4245 kfree(filename);
4246 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4247 return 0;
4248}
4249
4250static void get_file_alternate_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004251 struct smb2_query_info_rsp *rsp,
4252 struct ksmbd_file *fp,
4253 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004254{
4255 struct ksmbd_conn *conn = work->conn;
4256 struct smb2_file_alt_name_info *file_info;
4257 int conv_len;
4258 char *filename;
4259
4260 filename = (char *)FP_FILENAME(fp);
4261 file_info = (struct smb2_file_alt_name_info *)rsp->Buffer;
4262 conv_len = ksmbd_extract_shortname(conn,
4263 filename,
4264 file_info->FileName);
4265 file_info->FileNameLength = cpu_to_le32(conv_len);
4266 rsp->OutputBufferLength =
4267 cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len);
4268 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4269}
4270
4271static void get_file_stream_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004272 struct smb2_query_info_rsp *rsp,
4273 struct ksmbd_file *fp,
4274 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004275{
4276 struct ksmbd_conn *conn = work->conn;
4277 struct smb2_file_stream_info *file_info;
4278 char *stream_name, *xattr_list = NULL, *stream_buf;
4279 struct kstat stat;
4280 struct path *path = &fp->filp->f_path;
4281 ssize_t xattr_list_len;
4282 int nbytes = 0, streamlen, stream_name_len, next, idx = 0;
4283
4284 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
4285 file_info = (struct smb2_file_stream_info *)rsp->Buffer;
4286
4287 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4288 if (xattr_list_len < 0) {
4289 goto out;
4290 } else if (!xattr_list_len) {
4291 ksmbd_debug(SMB, "empty xattr in the file\n");
4292 goto out;
4293 }
4294
4295 while (idx < xattr_list_len) {
4296 stream_name = xattr_list + idx;
4297 streamlen = strlen(stream_name);
4298 idx += streamlen + 1;
4299
4300 ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen);
4301
4302 if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004303 STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004304 continue;
4305
4306 stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN +
4307 STREAM_PREFIX_LEN);
4308 streamlen = stream_name_len;
4309
4310 /* plus : size */
4311 streamlen += 1;
4312 stream_buf = kmalloc(streamlen + 1, GFP_KERNEL);
4313 if (!stream_buf)
4314 break;
4315
4316 streamlen = snprintf(stream_buf, streamlen + 1,
Namjae Jeon070fb212021-05-26 17:57:12 +09004317 ":%s", &stream_name[XATTR_NAME_STREAM_LEN]);
Namjae Jeone2f34482021-03-16 10:49:09 +09004318
Namjae Jeon070fb212021-05-26 17:57:12 +09004319 file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes];
Namjae Jeone2f34482021-03-16 10:49:09 +09004320 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004321 stream_buf, streamlen,
4322 conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004323 streamlen *= 2;
4324 kfree(stream_buf);
4325 file_info->StreamNameLength = cpu_to_le32(streamlen);
4326 file_info->StreamSize = cpu_to_le64(stream_name_len);
4327 file_info->StreamAllocationSize = cpu_to_le64(stream_name_len);
4328
4329 next = sizeof(struct smb2_file_stream_info) + streamlen;
4330 nbytes += next;
4331 file_info->NextEntryOffset = cpu_to_le32(next);
4332 }
4333
4334 if (nbytes) {
4335 file_info = (struct smb2_file_stream_info *)
4336 &rsp->Buffer[nbytes];
4337 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004338 "::$DATA", 7, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004339 streamlen *= 2;
4340 file_info->StreamNameLength = cpu_to_le32(streamlen);
4341 file_info->StreamSize = S_ISDIR(stat.mode) ? 0 :
4342 cpu_to_le64(stat.size);
4343 file_info->StreamAllocationSize = S_ISDIR(stat.mode) ? 0 :
4344 cpu_to_le64(stat.size);
4345 nbytes += sizeof(struct smb2_file_stream_info) + streamlen;
4346 }
4347
4348 /* last entry offset should be 0 */
4349 file_info->NextEntryOffset = 0;
4350out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004351 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004352
4353 rsp->OutputBufferLength = cpu_to_le32(nbytes);
4354 inc_rfc1001_len(rsp_org, nbytes);
4355}
4356
4357static void get_file_internal_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004358 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004359{
4360 struct smb2_file_internal_info *file_info;
4361 struct kstat stat;
4362
4363 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
4364 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4365 file_info->IndexNumber = cpu_to_le64(stat.ino);
4366 rsp->OutputBufferLength =
4367 cpu_to_le32(sizeof(struct smb2_file_internal_info));
4368 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
4369}
4370
4371static int get_file_network_open_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004372 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004373{
4374 struct smb2_file_ntwrk_info *file_info;
4375 struct inode *inode;
4376 struct kstat stat;
4377 u64 time;
4378
4379 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4380 ksmbd_err("no right to read the attributes : 0x%x\n",
4381 fp->daccess);
4382 return -EACCES;
4383 }
4384
4385 file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer;
4386
4387 inode = FP_INODE(fp);
4388 generic_fillattr(&init_user_ns, inode, &stat);
4389
4390 file_info->CreationTime = cpu_to_le64(fp->create_time);
4391 time = ksmbd_UnixTimeToNT(stat.atime);
4392 file_info->LastAccessTime = cpu_to_le64(time);
4393 time = ksmbd_UnixTimeToNT(stat.mtime);
4394 file_info->LastWriteTime = cpu_to_le64(time);
4395 time = ksmbd_UnixTimeToNT(stat.ctime);
4396 file_info->ChangeTime = cpu_to_le64(time);
4397 file_info->Attributes = fp->f_ci->m_fattr;
4398 file_info->AllocationSize =
4399 cpu_to_le64(get_allocation_size(inode, &stat));
4400 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4401 file_info->Reserved = cpu_to_le32(0);
4402 rsp->OutputBufferLength =
4403 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info));
4404 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info));
4405 return 0;
4406}
4407
Namjae Jeon64b39f42021-03-30 14:25:35 +09004408static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004409{
4410 struct smb2_file_ea_info *file_info;
4411
4412 file_info = (struct smb2_file_ea_info *)rsp->Buffer;
4413 file_info->EASize = 0;
4414 rsp->OutputBufferLength =
4415 cpu_to_le32(sizeof(struct smb2_file_ea_info));
4416 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info));
4417}
4418
4419static void get_file_position_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004420 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004421{
4422 struct smb2_file_pos_info *file_info;
4423
4424 file_info = (struct smb2_file_pos_info *)rsp->Buffer;
4425 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4426 rsp->OutputBufferLength =
4427 cpu_to_le32(sizeof(struct smb2_file_pos_info));
4428 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info));
4429}
4430
4431static void get_file_mode_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004432 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004433{
4434 struct smb2_file_mode_info *file_info;
4435
4436 file_info = (struct smb2_file_mode_info *)rsp->Buffer;
4437 file_info->Mode = fp->coption & FILE_MODE_INFO_MASK;
4438 rsp->OutputBufferLength =
4439 cpu_to_le32(sizeof(struct smb2_file_mode_info));
4440 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info));
4441}
4442
4443static void get_file_compression_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004444 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004445{
4446 struct smb2_file_comp_info *file_info;
4447 struct kstat stat;
4448
4449 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
4450
4451 file_info = (struct smb2_file_comp_info *)rsp->Buffer;
4452 file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9);
4453 file_info->CompressionFormat = COMPRESSION_FORMAT_NONE;
4454 file_info->CompressionUnitShift = 0;
4455 file_info->ChunkShift = 0;
4456 file_info->ClusterShift = 0;
4457 memset(&file_info->Reserved[0], 0, 3);
4458
4459 rsp->OutputBufferLength =
4460 cpu_to_le32(sizeof(struct smb2_file_comp_info));
4461 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info));
4462}
4463
4464static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004465 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004466{
4467 struct smb2_file_attr_tag_info *file_info;
4468
4469 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4470 ksmbd_err("no right to read the attributes : 0x%x\n",
4471 fp->daccess);
4472 return -EACCES;
4473 }
4474
4475 file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer;
4476 file_info->FileAttributes = fp->f_ci->m_fattr;
4477 file_info->ReparseTag = 0;
4478 rsp->OutputBufferLength =
4479 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info));
Namjae Jeon070fb212021-05-26 17:57:12 +09004480 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004481 return 0;
4482}
4483
4484static int find_file_posix_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004485 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004486{
4487 struct smb311_posix_qinfo *file_info;
4488 struct inode *inode = FP_INODE(fp);
4489 u64 time;
4490
4491 file_info = (struct smb311_posix_qinfo *)rsp->Buffer;
4492 file_info->CreationTime = cpu_to_le64(fp->create_time);
4493 time = ksmbd_UnixTimeToNT(inode->i_atime);
4494 file_info->LastAccessTime = cpu_to_le64(time);
4495 time = ksmbd_UnixTimeToNT(inode->i_mtime);
4496 file_info->LastWriteTime = cpu_to_le64(time);
4497 time = ksmbd_UnixTimeToNT(inode->i_ctime);
4498 file_info->ChangeTime = cpu_to_le64(time);
4499 file_info->DosAttributes = fp->f_ci->m_fattr;
4500 file_info->Inode = cpu_to_le64(inode->i_ino);
4501 file_info->EndOfFile = cpu_to_le64(inode->i_size);
4502 file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9);
4503 file_info->HardLinks = cpu_to_le32(inode->i_nlink);
4504 file_info->Mode = cpu_to_le32(inode->i_mode);
4505 file_info->DeviceId = cpu_to_le32(inode->i_rdev);
4506 rsp->OutputBufferLength =
4507 cpu_to_le32(sizeof(struct smb311_posix_qinfo));
Namjae Jeon64b39f42021-03-30 14:25:35 +09004508 inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo));
Namjae Jeone2f34482021-03-16 10:49:09 +09004509 return 0;
4510}
4511
Namjae Jeone2f34482021-03-16 10:49:09 +09004512static int smb2_get_info_file(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004513 struct smb2_query_info_req *req,
4514 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004515{
4516 struct ksmbd_file *fp;
4517 int fileinfoclass = 0;
4518 int rc = 0;
4519 int file_infoclass_size;
4520 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4521
4522 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004523 KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004524 /* smb2 info file called for pipe */
4525 return smb2_get_info_file_pipe(work->sess, req, rsp);
4526 }
4527
4528 if (work->next_smb2_rcv_hdr_off) {
4529 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
4530 ksmbd_debug(SMB, "Compound request set FID = %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004531 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004532 id = work->compound_fid;
4533 pid = work->compound_pfid;
4534 }
4535 }
4536
4537 if (!HAS_FILE_ID(id)) {
4538 id = le64_to_cpu(req->VolatileFileId);
4539 pid = le64_to_cpu(req->PersistentFileId);
4540 }
4541
4542 fp = ksmbd_lookup_fd_slow(work, id, pid);
4543 if (!fp)
4544 return -ENOENT;
4545
4546 fileinfoclass = req->FileInfoClass;
4547
4548 switch (fileinfoclass) {
4549 case FILE_ACCESS_INFORMATION:
4550 get_file_access_info(rsp, fp, rsp_org);
4551 file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE;
4552 break;
4553
4554 case FILE_BASIC_INFORMATION:
4555 rc = get_file_basic_info(rsp, fp, rsp_org);
4556 file_infoclass_size = FILE_BASIC_INFORMATION_SIZE;
4557 break;
4558
4559 case FILE_STANDARD_INFORMATION:
4560 get_file_standard_info(rsp, fp, rsp_org);
4561 file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE;
4562 break;
4563
4564 case FILE_ALIGNMENT_INFORMATION:
4565 get_file_alignment_info(rsp, rsp_org);
4566 file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE;
4567 break;
4568
4569 case FILE_ALL_INFORMATION:
4570 rc = get_file_all_info(work, rsp, fp, rsp_org);
4571 file_infoclass_size = FILE_ALL_INFORMATION_SIZE;
4572 break;
4573
4574 case FILE_ALTERNATE_NAME_INFORMATION:
4575 get_file_alternate_info(work, rsp, fp, rsp_org);
4576 file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE;
4577 break;
4578
4579 case FILE_STREAM_INFORMATION:
4580 get_file_stream_info(work, rsp, fp, rsp_org);
4581 file_infoclass_size = FILE_STREAM_INFORMATION_SIZE;
4582 break;
4583
4584 case FILE_INTERNAL_INFORMATION:
4585 get_file_internal_info(rsp, fp, rsp_org);
4586 file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE;
4587 break;
4588
4589 case FILE_NETWORK_OPEN_INFORMATION:
4590 rc = get_file_network_open_info(rsp, fp, rsp_org);
4591 file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE;
4592 break;
4593
4594 case FILE_EA_INFORMATION:
4595 get_file_ea_info(rsp, rsp_org);
4596 file_infoclass_size = FILE_EA_INFORMATION_SIZE;
4597 break;
4598
4599 case FILE_FULL_EA_INFORMATION:
4600 rc = smb2_get_ea(work, fp, req, rsp, rsp_org);
4601 file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE;
4602 break;
4603
4604 case FILE_POSITION_INFORMATION:
4605 get_file_position_info(rsp, fp, rsp_org);
4606 file_infoclass_size = FILE_POSITION_INFORMATION_SIZE;
4607 break;
4608
4609 case FILE_MODE_INFORMATION:
4610 get_file_mode_info(rsp, fp, rsp_org);
4611 file_infoclass_size = FILE_MODE_INFORMATION_SIZE;
4612 break;
4613
4614 case FILE_COMPRESSION_INFORMATION:
4615 get_file_compression_info(rsp, fp, rsp_org);
4616 file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE;
4617 break;
4618
4619 case FILE_ATTRIBUTE_TAG_INFORMATION:
4620 rc = get_file_attribute_tag_info(rsp, fp, rsp_org);
4621 file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE;
4622 break;
4623 case SMB_FIND_FILE_POSIX_INFO:
4624 if (!work->tcon->posix_extensions) {
4625 ksmbd_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
4626 rc = -EOPNOTSUPP;
4627 } else {
4628 rc = find_file_posix_info(rsp, fp, rsp_org);
4629 file_infoclass_size = sizeof(struct smb311_posix_qinfo);
4630 }
4631 break;
4632 default:
4633 ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n",
4634 fileinfoclass);
4635 rc = -EOPNOTSUPP;
4636 }
4637 if (!rc)
4638 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4639 rsp,
4640 file_infoclass_size);
4641 ksmbd_fd_put(work, fp);
4642 return rc;
4643}
4644
Namjae Jeone2f34482021-03-16 10:49:09 +09004645static int smb2_get_info_filesystem(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004646 struct smb2_query_info_req *req,
4647 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004648{
4649 struct ksmbd_session *sess = work->sess;
4650 struct ksmbd_conn *conn = sess->conn;
4651 struct ksmbd_share_config *share = work->tcon->share_conf;
4652 int fsinfoclass = 0;
4653 struct kstatfs stfs;
4654 struct path path;
4655 int rc = 0, len;
4656 int fs_infoclass_size = 0;
Hyunchul Leea6a5fa72021-05-26 18:59:06 +09004657 int lookup_flags = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09004658
Hyunchul Leea6a5fa72021-05-26 18:59:06 +09004659 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_FOLLOW_SYMLINKS))
4660 lookup_flags = LOOKUP_FOLLOW;
4661
4662 rc = ksmbd_vfs_kern_path(share->path, lookup_flags, &path, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004663 if (rc) {
4664 ksmbd_err("cannot create vfs path\n");
4665 return -EIO;
4666 }
4667
4668 rc = vfs_statfs(&path, &stfs);
4669 if (rc) {
4670 ksmbd_err("cannot do stat of path %s\n", share->path);
4671 path_put(&path);
4672 return -EIO;
4673 }
4674
4675 fsinfoclass = req->FileInfoClass;
4676
4677 switch (fsinfoclass) {
4678 case FS_DEVICE_INFORMATION:
4679 {
4680 struct filesystem_device_info *info;
4681
4682 info = (struct filesystem_device_info *)rsp->Buffer;
4683
4684 info->DeviceType = cpu_to_le32(stfs.f_type);
4685 info->DeviceCharacteristics = cpu_to_le32(0x00000020);
4686 rsp->OutputBufferLength = cpu_to_le32(8);
4687 inc_rfc1001_len(rsp_org, 8);
4688 fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE;
4689 break;
4690 }
4691 case FS_ATTRIBUTE_INFORMATION:
4692 {
4693 struct filesystem_attribute_info *info;
4694 size_t sz;
4695
4696 info = (struct filesystem_attribute_info *)rsp->Buffer;
4697 info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS |
4698 FILE_PERSISTENT_ACLS |
4699 FILE_UNICODE_ON_DISK |
4700 FILE_CASE_PRESERVED_NAMES |
Namjae Jeoneb817362021-05-18 10:37:59 +09004701 FILE_CASE_SENSITIVE_SEARCH |
4702 FILE_SUPPORTS_BLOCK_REFCOUNTING);
Namjae Jeone2f34482021-03-16 10:49:09 +09004703
4704 info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps);
4705
4706 info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen);
4707 len = smbConvertToUTF16((__le16 *)info->FileSystemName,
4708 "NTFS", PATH_MAX, conn->local_nls, 0);
4709 len = len * 2;
4710 info->FileSystemNameLen = cpu_to_le32(len);
4711 sz = sizeof(struct filesystem_attribute_info) - 2 + len;
4712 rsp->OutputBufferLength = cpu_to_le32(sz);
4713 inc_rfc1001_len(rsp_org, sz);
4714 fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE;
4715 break;
4716 }
4717 case FS_VOLUME_INFORMATION:
4718 {
4719 struct filesystem_vol_info *info;
4720 size_t sz;
4721
4722 info = (struct filesystem_vol_info *)(rsp->Buffer);
4723 info->VolumeCreationTime = 0;
4724 /* Taking dummy value of serial number*/
4725 info->SerialNumber = cpu_to_le32(0xbc3ac512);
4726 len = smbConvertToUTF16((__le16 *)info->VolumeLabel,
4727 share->name, PATH_MAX,
4728 conn->local_nls, 0);
4729 len = len * 2;
4730 info->VolumeLabelSize = cpu_to_le32(len);
4731 info->Reserved = 0;
4732 sz = sizeof(struct filesystem_vol_info) - 2 + len;
4733 rsp->OutputBufferLength = cpu_to_le32(sz);
4734 inc_rfc1001_len(rsp_org, sz);
4735 fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE;
4736 break;
4737 }
4738 case FS_SIZE_INFORMATION:
4739 {
4740 struct filesystem_info *info;
4741 unsigned short logical_sector_size;
4742
4743 info = (struct filesystem_info *)(rsp->Buffer);
4744 logical_sector_size =
4745 ksmbd_vfs_logical_sector_size(d_inode(path.dentry));
4746
4747 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4748 info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree);
4749 info->SectorsPerAllocationUnit = cpu_to_le32(stfs.f_bsize >> 9);
4750 info->BytesPerSector = cpu_to_le32(logical_sector_size);
4751 rsp->OutputBufferLength = cpu_to_le32(24);
4752 inc_rfc1001_len(rsp_org, 24);
4753 fs_infoclass_size = FS_SIZE_INFORMATION_SIZE;
4754 break;
4755 }
4756 case FS_FULL_SIZE_INFORMATION:
4757 {
4758 struct smb2_fs_full_size_info *info;
4759 unsigned short logical_sector_size;
4760
4761 info = (struct smb2_fs_full_size_info *)(rsp->Buffer);
4762 logical_sector_size =
4763 ksmbd_vfs_logical_sector_size(d_inode(path.dentry));
4764
4765 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4766 info->CallerAvailableAllocationUnits =
4767 cpu_to_le64(stfs.f_bavail);
4768 info->ActualAvailableAllocationUnits =
4769 cpu_to_le64(stfs.f_bfree);
4770 info->SectorsPerAllocationUnit = cpu_to_le32(stfs.f_bsize >> 9);
4771 info->BytesPerSector = cpu_to_le32(logical_sector_size);
4772 rsp->OutputBufferLength = cpu_to_le32(32);
4773 inc_rfc1001_len(rsp_org, 32);
4774 fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE;
4775 break;
4776 }
4777 case FS_OBJECT_ID_INFORMATION:
4778 {
4779 struct object_id_info *info;
4780
4781 info = (struct object_id_info *)(rsp->Buffer);
4782
4783 if (!user_guest(sess->user))
4784 memcpy(info->objid, user_passkey(sess->user), 16);
4785 else
4786 memset(info->objid, 0, 16);
4787
4788 info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC);
4789 info->extended_info.version = cpu_to_le32(1);
4790 info->extended_info.release = cpu_to_le32(1);
4791 info->extended_info.rel_date = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004792 memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0"));
Namjae Jeone2f34482021-03-16 10:49:09 +09004793 rsp->OutputBufferLength = cpu_to_le32(64);
4794 inc_rfc1001_len(rsp_org, 64);
4795 fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE;
4796 break;
4797 }
4798 case FS_SECTOR_SIZE_INFORMATION:
4799 {
4800 struct smb3_fs_ss_info *info;
4801 struct ksmbd_fs_sector_size fs_ss;
4802
4803 info = (struct smb3_fs_ss_info *)(rsp->Buffer);
4804 ksmbd_vfs_smb2_sector_size(d_inode(path.dentry), &fs_ss);
4805
4806 info->LogicalBytesPerSector =
4807 cpu_to_le32(fs_ss.logical_sector_size);
4808 info->PhysicalBytesPerSectorForAtomicity =
4809 cpu_to_le32(fs_ss.physical_sector_size);
4810 info->PhysicalBytesPerSectorForPerf =
4811 cpu_to_le32(fs_ss.optimal_io_size);
4812 info->FSEffPhysicalBytesPerSectorForAtomicity =
4813 cpu_to_le32(fs_ss.optimal_io_size);
4814 info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE |
4815 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE);
4816 info->ByteOffsetForSectorAlignment = 0;
4817 info->ByteOffsetForPartitionAlignment = 0;
4818 rsp->OutputBufferLength = cpu_to_le32(28);
4819 inc_rfc1001_len(rsp_org, 28);
4820 fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE;
4821 break;
4822 }
4823 case FS_CONTROL_INFORMATION:
4824 {
4825 /*
4826 * TODO : The current implementation is based on
4827 * test result with win7(NTFS) server. It's need to
4828 * modify this to get valid Quota values
4829 * from Linux kernel
4830 */
4831 struct smb2_fs_control_info *info;
4832
4833 info = (struct smb2_fs_control_info *)(rsp->Buffer);
4834 info->FreeSpaceStartFiltering = 0;
4835 info->FreeSpaceThreshold = 0;
4836 info->FreeSpaceStopFiltering = 0;
4837 info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID);
4838 info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID);
4839 info->Padding = 0;
4840 rsp->OutputBufferLength = cpu_to_le32(48);
4841 inc_rfc1001_len(rsp_org, 48);
4842 fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE;
4843 break;
4844 }
4845 case FS_POSIX_INFORMATION:
4846 {
4847 struct filesystem_posix_info *info;
4848 unsigned short logical_sector_size;
4849
4850 if (!work->tcon->posix_extensions) {
4851 ksmbd_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
4852 rc = -EOPNOTSUPP;
4853 } else {
4854 info = (struct filesystem_posix_info *)(rsp->Buffer);
4855 logical_sector_size =
4856 ksmbd_vfs_logical_sector_size(d_inode(path.dentry));
4857 info->OptimalTransferSize = cpu_to_le32(logical_sector_size);
4858 info->BlockSize = cpu_to_le32(stfs.f_bsize);
4859 info->TotalBlocks = cpu_to_le64(stfs.f_blocks);
4860 info->BlocksAvail = cpu_to_le64(stfs.f_bfree);
4861 info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail);
4862 info->TotalFileNodes = cpu_to_le64(stfs.f_files);
4863 info->FreeFileNodes = cpu_to_le64(stfs.f_ffree);
4864 rsp->OutputBufferLength = cpu_to_le32(56);
4865 inc_rfc1001_len(rsp_org, 56);
4866 fs_infoclass_size = FS_POSIX_INFORMATION_SIZE;
4867 }
4868 break;
4869 }
4870 default:
4871 path_put(&path);
4872 return -EOPNOTSUPP;
4873 }
4874 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4875 rsp,
4876 fs_infoclass_size);
4877 path_put(&path);
4878 return rc;
4879}
4880
4881static int smb2_get_info_sec(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004882 struct smb2_query_info_req *req,
4883 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004884{
4885 struct ksmbd_file *fp;
4886 struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL;
4887 struct smb_fattr fattr = {{0}};
4888 struct inode *inode;
4889 __u32 secdesclen;
4890 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4891 int addition_info = le32_to_cpu(req->AdditionalInformation);
4892 int rc;
4893
Sebastian Gottschallced2b262021-04-27 15:33:54 +09004894 if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO)) {
4895 ksmbd_debug(SMB, "Unsupported addition info: 0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004896 addition_info);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09004897
4898 pntsd->revision = cpu_to_le16(1);
4899 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED);
4900 pntsd->osidoffset = 0;
4901 pntsd->gsidoffset = 0;
4902 pntsd->sacloffset = 0;
4903 pntsd->dacloffset = 0;
4904
4905 secdesclen = sizeof(struct smb_ntsd);
4906 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
4907 inc_rfc1001_len(rsp_org, secdesclen);
4908
4909 return 0;
4910 }
4911
Namjae Jeone2f34482021-03-16 10:49:09 +09004912 if (work->next_smb2_rcv_hdr_off) {
4913 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
4914 ksmbd_debug(SMB, "Compound request set FID = %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004915 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004916 id = work->compound_fid;
4917 pid = work->compound_pfid;
4918 }
4919 }
4920
4921 if (!HAS_FILE_ID(id)) {
4922 id = le64_to_cpu(req->VolatileFileId);
4923 pid = le64_to_cpu(req->PersistentFileId);
4924 }
4925
4926 fp = ksmbd_lookup_fd_slow(work, id, pid);
4927 if (!fp)
4928 return -ENOENT;
4929
4930 inode = FP_INODE(fp);
Namjae Jeon3d47e542021-04-20 14:25:35 +09004931 ksmbd_acls_fattr(&fattr, inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09004932
4933 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004934 KSMBD_SHARE_FLAG_ACL_XATTR))
Namjae Jeone2f34482021-03-16 10:49:09 +09004935 ksmbd_vfs_get_sd_xattr(work->conn, fp->filp->f_path.dentry, &ppntsd);
4936
4937 rc = build_sec_desc(pntsd, ppntsd, addition_info, &secdesclen, &fattr);
4938 posix_acl_release(fattr.cf_acls);
4939 posix_acl_release(fattr.cf_dacls);
4940 kfree(ppntsd);
4941 ksmbd_fd_put(work, fp);
4942 if (rc)
4943 return rc;
4944
4945 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
4946 inc_rfc1001_len(rsp_org, secdesclen);
4947 return 0;
4948}
4949
4950/**
4951 * smb2_query_info() - handler for smb2 query info command
4952 * @work: smb work containing query info request buffer
4953 *
4954 * Return: 0 on success, otherwise error
4955 */
4956int smb2_query_info(struct ksmbd_work *work)
4957{
4958 struct smb2_query_info_req *req;
4959 struct smb2_query_info_rsp *rsp, *rsp_org;
4960 int rc = 0;
4961
Namjae Jeone5066492021-03-30 12:35:23 +09004962 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09004963 WORK_BUFFERS(work, req, rsp);
4964
4965 ksmbd_debug(SMB, "GOT query info request\n");
4966
4967 switch (req->InfoType) {
4968 case SMB2_O_INFO_FILE:
4969 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
4970 rc = smb2_get_info_file(work, req, rsp, (void *)rsp_org);
4971 break;
4972 case SMB2_O_INFO_FILESYSTEM:
4973 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n");
4974 rc = smb2_get_info_filesystem(work, req, rsp, (void *)rsp_org);
4975 break;
4976 case SMB2_O_INFO_SECURITY:
4977 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
4978 rc = smb2_get_info_sec(work, req, rsp, (void *)rsp_org);
4979 break;
4980 default:
4981 ksmbd_debug(SMB, "InfoType %d not supported yet\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004982 req->InfoType);
Namjae Jeone2f34482021-03-16 10:49:09 +09004983 rc = -EOPNOTSUPP;
4984 }
4985
4986 if (rc < 0) {
4987 if (rc == -EACCES)
4988 rsp->hdr.Status = STATUS_ACCESS_DENIED;
4989 else if (rc == -ENOENT)
4990 rsp->hdr.Status = STATUS_FILE_CLOSED;
4991 else if (rc == -EIO)
4992 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
4993 else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0)
4994 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
4995 smb2_set_err_rsp(work);
4996
4997 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004998 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09004999 return rc;
5000 }
5001 rsp->StructureSize = cpu_to_le16(9);
5002 rsp->OutputBufferOffset = cpu_to_le16(72);
5003 inc_rfc1001_len(rsp_org, 8);
5004 return 0;
5005}
5006
5007/**
5008 * smb2_close_pipe() - handler for closing IPC pipe
5009 * @work: smb work containing close request buffer
5010 *
5011 * Return: 0
5012 */
5013static noinline int smb2_close_pipe(struct ksmbd_work *work)
5014{
Namjae Jeon64b39f42021-03-30 14:25:35 +09005015 u64 id;
Namjae Jeone5066492021-03-30 12:35:23 +09005016 struct smb2_close_req *req = work->request_buf;
5017 struct smb2_close_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005018
5019 id = le64_to_cpu(req->VolatileFileId);
5020 ksmbd_session_rpc_close(work->sess, id);
5021
5022 rsp->StructureSize = cpu_to_le16(60);
5023 rsp->Flags = 0;
5024 rsp->Reserved = 0;
5025 rsp->CreationTime = 0;
5026 rsp->LastAccessTime = 0;
5027 rsp->LastWriteTime = 0;
5028 rsp->ChangeTime = 0;
5029 rsp->AllocationSize = 0;
5030 rsp->EndOfFile = 0;
5031 rsp->Attributes = 0;
5032 inc_rfc1001_len(rsp, 60);
5033 return 0;
5034}
5035
5036/**
5037 * smb2_close() - handler for smb2 close file command
5038 * @work: smb work containing close request buffer
5039 *
5040 * Return: 0
5041 */
5042int smb2_close(struct ksmbd_work *work)
5043{
5044 unsigned int volatile_id = KSMBD_NO_FID;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005045 u64 sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09005046 struct smb2_close_req *req;
5047 struct smb2_close_rsp *rsp;
5048 struct smb2_close_rsp *rsp_org;
5049 struct ksmbd_conn *conn = work->conn;
5050 struct ksmbd_file *fp;
5051 struct inode *inode;
5052 u64 time;
5053 int err = 0;
5054
Namjae Jeone5066492021-03-30 12:35:23 +09005055 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005056 WORK_BUFFERS(work, req, rsp);
5057
5058 if (test_share_config_flag(work->tcon->share_conf,
5059 KSMBD_SHARE_FLAG_PIPE)) {
5060 ksmbd_debug(SMB, "IPC pipe close request\n");
5061 return smb2_close_pipe(work);
5062 }
5063
5064 sess_id = le64_to_cpu(req->hdr.SessionId);
5065 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5066 sess_id = work->compound_sid;
5067
5068 work->compound_sid = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005069 if (check_session_id(conn, sess_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005070 work->compound_sid = sess_id;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005071 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005072 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
5073 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5074 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5075 err = -EBADF;
5076 goto out;
5077 }
5078
5079 if (work->next_smb2_rcv_hdr_off &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005080 !HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005081 if (!HAS_FILE_ID(work->compound_fid)) {
5082 /* file already closed, return FILE_CLOSED */
5083 ksmbd_debug(SMB, "file already closed\n");
5084 rsp->hdr.Status = STATUS_FILE_CLOSED;
5085 err = -EBADF;
5086 goto out;
5087 } else {
5088 ksmbd_debug(SMB, "Compound request set FID = %u:%u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005089 work->compound_fid,
5090 work->compound_pfid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005091 volatile_id = work->compound_fid;
5092
5093 /* file closed, stored id is not valid anymore */
5094 work->compound_fid = KSMBD_NO_FID;
5095 work->compound_pfid = KSMBD_NO_FID;
5096 }
5097 } else {
5098 volatile_id = le64_to_cpu(req->VolatileFileId);
5099 }
5100 ksmbd_debug(SMB, "volatile_id = %u\n", volatile_id);
5101
5102 rsp->StructureSize = cpu_to_le16(60);
5103 rsp->Reserved = 0;
5104
5105 if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) {
5106 fp = ksmbd_lookup_fd_fast(work, volatile_id);
5107 if (!fp) {
5108 err = -ENOENT;
5109 goto out;
5110 }
5111
5112 inode = FP_INODE(fp);
5113 rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
5114 rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 :
5115 cpu_to_le64(inode->i_blocks << 9);
5116 rsp->EndOfFile = cpu_to_le64(inode->i_size);
5117 rsp->Attributes = fp->f_ci->m_fattr;
5118 rsp->CreationTime = cpu_to_le64(fp->create_time);
5119 time = ksmbd_UnixTimeToNT(inode->i_atime);
5120 rsp->LastAccessTime = cpu_to_le64(time);
5121 time = ksmbd_UnixTimeToNT(inode->i_mtime);
5122 rsp->LastWriteTime = cpu_to_le64(time);
5123 time = ksmbd_UnixTimeToNT(inode->i_ctime);
5124 rsp->ChangeTime = cpu_to_le64(time);
5125 ksmbd_fd_put(work, fp);
5126 } else {
5127 rsp->Flags = 0;
5128 rsp->AllocationSize = 0;
5129 rsp->EndOfFile = 0;
5130 rsp->Attributes = 0;
5131 rsp->CreationTime = 0;
5132 rsp->LastAccessTime = 0;
5133 rsp->LastWriteTime = 0;
5134 rsp->ChangeTime = 0;
5135 }
5136
5137 err = ksmbd_close_fd(work, volatile_id);
5138out:
5139 if (err) {
5140 if (rsp->hdr.Status == 0)
5141 rsp->hdr.Status = STATUS_FILE_CLOSED;
5142 smb2_set_err_rsp(work);
5143 } else {
5144 inc_rfc1001_len(rsp_org, 60);
5145 }
5146
5147 return 0;
5148}
5149
5150/**
5151 * smb2_echo() - handler for smb2 echo(ping) command
5152 * @work: smb work containing echo request buffer
5153 *
5154 * Return: 0
5155 */
5156int smb2_echo(struct ksmbd_work *work)
5157{
Namjae Jeone5066492021-03-30 12:35:23 +09005158 struct smb2_echo_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005159
5160 rsp->StructureSize = cpu_to_le16(4);
5161 rsp->Reserved = 0;
5162 inc_rfc1001_len(rsp, 4);
5163 return 0;
5164}
5165
Namjae Jeone2f34482021-03-16 10:49:09 +09005166static int smb2_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005167 struct smb2_file_rename_info *file_info,
5168 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005169{
5170 struct ksmbd_share_config *share = fp->tcon->share_conf;
5171 char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL;
5172 char *pathname = NULL;
5173 struct path path;
5174 bool file_present = true;
5175 int rc;
5176
5177 ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n");
5178 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5179 if (!pathname)
5180 return -ENOMEM;
5181
5182 abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
5183 if (IS_ERR(abs_oldname)) {
5184 rc = -EINVAL;
5185 goto out;
5186 }
5187 old_name = strrchr(abs_oldname, '/');
Namjae Jeon64b39f42021-03-30 14:25:35 +09005188 if (old_name && old_name[1] != '\0') {
Namjae Jeone2f34482021-03-16 10:49:09 +09005189 old_name++;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005190 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005191 ksmbd_debug(SMB, "can't get last component in path %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005192 abs_oldname);
Namjae Jeone2f34482021-03-16 10:49:09 +09005193 rc = -ENOENT;
5194 goto out;
5195 }
5196
5197 new_name = smb2_get_name(share,
5198 file_info->FileName,
5199 le32_to_cpu(file_info->FileNameLength),
5200 local_nls);
5201 if (IS_ERR(new_name)) {
5202 rc = PTR_ERR(new_name);
5203 goto out;
5204 }
5205
5206 if (strchr(new_name, ':')) {
5207 int s_type;
5208 char *xattr_stream_name, *stream_name = NULL;
5209 size_t xattr_stream_size;
5210 int len;
5211
5212 rc = parse_stream_name(new_name, &stream_name, &s_type);
5213 if (rc < 0)
5214 goto out;
5215
5216 len = strlen(new_name);
5217 if (new_name[len - 1] != '/') {
5218 ksmbd_err("not allow base filename in rename\n");
5219 rc = -ESHARE;
5220 goto out;
5221 }
5222
5223 rc = ksmbd_vfs_xattr_stream_name(stream_name,
5224 &xattr_stream_name,
5225 &xattr_stream_size,
5226 s_type);
5227 if (rc)
5228 goto out;
5229
5230 rc = ksmbd_vfs_setxattr(fp->filp->f_path.dentry,
5231 xattr_stream_name,
5232 NULL, 0, 0);
5233 if (rc < 0) {
5234 ksmbd_err("failed to store stream name in xattr: %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005235 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005236 rc = -EINVAL;
5237 goto out;
5238 }
5239
5240 goto out;
5241 }
5242
5243 ksmbd_debug(SMB, "new name %s\n", new_name);
5244 rc = ksmbd_vfs_kern_path(new_name, 0, &path, 1);
5245 if (rc)
5246 file_present = false;
5247 else
5248 path_put(&path);
5249
5250 if (ksmbd_share_veto_filename(share, new_name)) {
5251 rc = -ENOENT;
5252 ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name);
5253 goto out;
5254 }
5255
5256 if (file_info->ReplaceIfExists) {
5257 if (file_present) {
5258 rc = ksmbd_vfs_remove_file(work, new_name);
5259 if (rc) {
5260 if (rc != -ENOTEMPTY)
5261 rc = -EINVAL;
5262 ksmbd_debug(SMB, "cannot delete %s, rc %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005263 new_name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005264 goto out;
5265 }
5266 }
5267 } else {
5268 if (file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005269 strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005270 rc = -EEXIST;
5271 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005272 "cannot rename already existing file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005273 goto out;
5274 }
5275 }
5276
5277 rc = ksmbd_vfs_fp_rename(work, fp, new_name);
5278out:
5279 kfree(pathname);
5280 if (!IS_ERR(new_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005281 kfree(new_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005282 return rc;
5283}
5284
Namjae Jeone2f34482021-03-16 10:49:09 +09005285static int smb2_create_link(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005286 struct ksmbd_share_config *share,
5287 struct smb2_file_link_info *file_info,
5288 struct file *filp,
5289 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005290{
5291 char *link_name = NULL, *target_name = NULL, *pathname = NULL;
5292 struct path path;
5293 bool file_present = true;
5294 int rc;
5295
5296 ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n");
5297 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5298 if (!pathname)
5299 return -ENOMEM;
5300
5301 link_name = smb2_get_name(share,
5302 file_info->FileName,
5303 le32_to_cpu(file_info->FileNameLength),
5304 local_nls);
5305 if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) {
5306 rc = -EINVAL;
5307 goto out;
5308 }
5309
5310 ksmbd_debug(SMB, "link name is %s\n", link_name);
5311 target_name = d_path(&filp->f_path, pathname, PATH_MAX);
5312 if (IS_ERR(target_name)) {
5313 rc = -EINVAL;
5314 goto out;
5315 }
5316
5317 ksmbd_debug(SMB, "target name is %s\n", target_name);
5318 rc = ksmbd_vfs_kern_path(link_name, 0, &path, 0);
5319 if (rc)
5320 file_present = false;
5321 else
5322 path_put(&path);
5323
5324 if (file_info->ReplaceIfExists) {
5325 if (file_present) {
5326 rc = ksmbd_vfs_remove_file(work, link_name);
5327 if (rc) {
5328 rc = -EINVAL;
5329 ksmbd_debug(SMB, "cannot delete %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005330 link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005331 goto out;
5332 }
5333 }
5334 } else {
5335 if (file_present) {
5336 rc = -EEXIST;
5337 ksmbd_debug(SMB, "link already exists\n");
5338 goto out;
5339 }
5340 }
5341
5342 rc = ksmbd_vfs_link(work, target_name, link_name);
5343 if (rc)
5344 rc = -EINVAL;
5345out:
5346 if (!IS_ERR(link_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005347 kfree(link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005348 kfree(pathname);
5349 return rc;
5350}
5351
Namjae Jeon64b39f42021-03-30 14:25:35 +09005352static int set_file_basic_info(struct ksmbd_file *fp, char *buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09005353 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005354{
5355 struct smb2_file_all_info *file_info;
5356 struct iattr attrs;
5357 struct iattr temp_attrs;
5358 struct file *filp;
5359 struct inode *inode;
5360 int rc;
5361
Marios Makassikis7adfd4f2021-04-27 15:30:22 +09005362 if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005363 return -EACCES;
5364
5365 file_info = (struct smb2_file_all_info *)buf;
5366 attrs.ia_valid = 0;
5367 filp = fp->filp;
5368 inode = file_inode(filp);
5369
5370 if (file_info->CreationTime)
5371 fp->create_time = le64_to_cpu(file_info->CreationTime);
5372
5373 if (file_info->LastAccessTime) {
5374 attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime);
5375 attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
5376 }
5377
5378 if (file_info->ChangeTime) {
5379 temp_attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime);
5380 attrs.ia_ctime = temp_attrs.ia_ctime;
5381 attrs.ia_valid |= ATTR_CTIME;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005382 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005383 temp_attrs.ia_ctime = inode->i_ctime;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005384 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005385
5386 if (file_info->LastWriteTime) {
5387 attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime);
5388 attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
5389 }
5390
5391 if (file_info->Attributes) {
5392 if (!S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005393 file_info->Attributes & ATTR_DIRECTORY_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005394 ksmbd_err("can't change a file to a directory\n");
5395 return -EINVAL;
5396 }
5397
5398 if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == ATTR_NORMAL_LE))
5399 fp->f_ci->m_fattr = file_info->Attributes |
5400 (fp->f_ci->m_fattr & ATTR_DIRECTORY_LE);
5401 }
5402
5403 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
5404 (file_info->CreationTime || file_info->Attributes)) {
5405 struct xattr_dos_attrib da = {0};
5406
5407 da.version = 4;
5408 da.itime = fp->itime;
5409 da.create_time = fp->create_time;
5410 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
5411 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
5412 XATTR_DOSINFO_ITIME;
5413
5414 rc = ksmbd_vfs_set_dos_attrib_xattr(filp->f_path.dentry, &da);
5415 if (rc)
5416 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005417 "failed to restore file attribute in EA\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005418 rc = 0;
5419 }
5420
5421 /*
5422 * HACK : set ctime here to avoid ctime changed
5423 * when file_info->ChangeTime is zero.
5424 */
5425 attrs.ia_ctime = temp_attrs.ia_ctime;
5426 attrs.ia_valid |= ATTR_CTIME;
5427
5428 if (attrs.ia_valid) {
5429 struct dentry *dentry = filp->f_path.dentry;
5430 struct inode *inode = d_inode(dentry);
5431
5432 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
5433 return -EACCES;
5434
5435 rc = setattr_prepare(&init_user_ns, dentry, &attrs);
5436 if (rc)
5437 return -EINVAL;
5438
5439 inode_lock(inode);
5440 setattr_copy(&init_user_ns, inode, &attrs);
5441 attrs.ia_valid &= ~ATTR_CTIME;
5442 rc = notify_change(&init_user_ns, dentry, &attrs, NULL);
5443 inode_unlock(inode);
5444 }
5445 return 0;
5446}
5447
5448static int set_file_allocation_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005449 struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005450{
5451 /*
5452 * TODO : It's working fine only when store dos attributes
5453 * is not yes. need to implement a logic which works
5454 * properly with any smb.conf option
5455 */
5456
5457 struct smb2_file_alloc_info *file_alloc_info;
5458 loff_t alloc_blks;
5459 struct inode *inode;
5460 int rc;
5461
Marios Makassikisa2996692021-04-27 15:29:01 +09005462 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005463 return -EACCES;
5464
5465 file_alloc_info = (struct smb2_file_alloc_info *)buf;
5466 alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9;
5467 inode = file_inode(fp->filp);
5468
5469 if (alloc_blks > inode->i_blocks) {
5470 rc = ksmbd_vfs_alloc_size(work, fp, alloc_blks * 512);
5471 if (rc && rc != -EOPNOTSUPP) {
5472 ksmbd_err("ksmbd_vfs_alloc_size is failed : %d\n", rc);
5473 return rc;
5474 }
5475 } else if (alloc_blks < inode->i_blocks) {
5476 loff_t size;
5477
5478 /*
5479 * Allocation size could be smaller than original one
5480 * which means allocated blocks in file should be
5481 * deallocated. use truncate to cut out it, but inode
5482 * size is also updated with truncate offset.
5483 * inode size is retained by backup inode size.
5484 */
5485 size = i_size_read(inode);
5486 rc = ksmbd_vfs_truncate(work, NULL, fp, alloc_blks * 512);
5487 if (rc) {
5488 ksmbd_err("truncate failed! filename : %s, err %d\n",
5489 fp->filename, rc);
5490 return rc;
5491 }
5492 if (size < alloc_blks * 512)
5493 i_size_write(inode, size);
5494 }
5495 return 0;
5496}
5497
Namjae Jeon64b39f42021-03-30 14:25:35 +09005498static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005499 char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005500{
5501 struct smb2_file_eof_info *file_eof_info;
5502 loff_t newsize;
5503 struct inode *inode;
5504 int rc;
5505
Marios Makassikisa2996692021-04-27 15:29:01 +09005506 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005507 return -EACCES;
5508
5509 file_eof_info = (struct smb2_file_eof_info *)buf;
5510 newsize = le64_to_cpu(file_eof_info->EndOfFile);
5511 inode = file_inode(fp->filp);
5512
5513 /*
5514 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called
5515 * on FAT32 shared device, truncate execution time is too long
5516 * and network error could cause from windows client. because
5517 * truncate of some filesystem like FAT32 fill zero data in
5518 * truncated range.
5519 */
5520 if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) {
5521 ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005522 fp->filename, newsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005523 rc = ksmbd_vfs_truncate(work, NULL, fp, newsize);
5524 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005525 ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005526 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005527 if (rc != -EAGAIN)
5528 rc = -EBADF;
5529 return rc;
5530 }
5531 }
5532 return 0;
5533}
5534
Namjae Jeon64b39f42021-03-30 14:25:35 +09005535static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005536 char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005537{
5538 struct ksmbd_file *parent_fp;
5539
5540 if (!(fp->daccess & FILE_DELETE_LE)) {
5541 ksmbd_err("no right to delete : 0x%x\n", fp->daccess);
5542 return -EACCES;
5543 }
5544
5545 if (ksmbd_stream_fd(fp))
5546 goto next;
5547
5548 parent_fp = ksmbd_lookup_fd_inode(PARENT_INODE(fp));
5549 if (parent_fp) {
5550 if (parent_fp->daccess & FILE_DELETE_LE) {
5551 ksmbd_err("parent dir is opened with delete access\n");
5552 return -ESHARE;
5553 }
5554 }
5555next:
5556 return smb2_rename(work, fp,
5557 (struct smb2_file_rename_info *)buf,
5558 work->sess->conn->local_nls);
5559}
5560
Namjae Jeon64b39f42021-03-30 14:25:35 +09005561static int set_file_disposition_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005562{
5563 struct smb2_file_disposition_info *file_info;
5564 struct inode *inode;
5565
5566 if (!(fp->daccess & FILE_DELETE_LE)) {
5567 ksmbd_err("no right to delete : 0x%x\n", fp->daccess);
5568 return -EACCES;
5569 }
5570
5571 inode = file_inode(fp->filp);
5572 file_info = (struct smb2_file_disposition_info *)buf;
5573 if (file_info->DeletePending) {
5574 if (S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005575 ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY)
Namjae Jeone2f34482021-03-16 10:49:09 +09005576 return -EBUSY;
5577 ksmbd_set_inode_pending_delete(fp);
5578 } else {
5579 ksmbd_clear_inode_pending_delete(fp);
5580 }
5581 return 0;
5582}
5583
Namjae Jeon64b39f42021-03-30 14:25:35 +09005584static int set_file_position_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005585{
5586 struct smb2_file_pos_info *file_info;
5587 loff_t current_byte_offset;
5588 unsigned short sector_size;
5589 struct inode *inode;
5590
5591 inode = file_inode(fp->filp);
5592 file_info = (struct smb2_file_pos_info *)buf;
5593 current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset);
5594 sector_size = ksmbd_vfs_logical_sector_size(inode);
5595
5596 if (current_byte_offset < 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09005597 (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE &&
5598 current_byte_offset & (sector_size - 1))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005599 ksmbd_err("CurrentByteOffset is not valid : %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005600 current_byte_offset);
Namjae Jeone2f34482021-03-16 10:49:09 +09005601 return -EINVAL;
5602 }
5603
5604 fp->filp->f_pos = current_byte_offset;
5605 return 0;
5606}
5607
Namjae Jeon64b39f42021-03-30 14:25:35 +09005608static int set_file_mode_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005609{
5610 struct smb2_file_mode_info *file_info;
5611 __le32 mode;
5612
5613 file_info = (struct smb2_file_mode_info *)buf;
5614 mode = file_info->Mode;
5615
Namjae Jeon64b39f42021-03-30 14:25:35 +09005616 if ((mode & ~FILE_MODE_INFO_MASK) ||
5617 (mode & FILE_SYNCHRONOUS_IO_ALERT_LE &&
5618 mode & FILE_SYNCHRONOUS_IO_NONALERT_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005619 ksmbd_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode));
5620 return -EINVAL;
5621 }
5622
5623 /*
5624 * TODO : need to implement consideration for
5625 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT
5626 */
5627 ksmbd_vfs_set_fadvise(fp->filp, mode);
5628 fp->coption = mode;
5629 return 0;
5630}
5631
5632/**
5633 * smb2_set_info_file() - handler for smb2 set info command
5634 * @work: smb work containing set info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09005635 * @fp: ksmbd_file pointer
5636 * @info_class: smb2 set info class
5637 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09005638 *
5639 * Return: 0 on success, otherwise error
5640 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH
5641 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09005642static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005643 int info_class, char *buf,
5644 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005645{
5646 switch (info_class) {
5647 case FILE_BASIC_INFORMATION:
5648 return set_file_basic_info(fp, buf, share);
5649
5650 case FILE_ALLOCATION_INFORMATION:
5651 return set_file_allocation_info(work, fp, buf);
5652
5653 case FILE_END_OF_FILE_INFORMATION:
5654 return set_end_of_file_info(work, fp, buf);
5655
5656 case FILE_RENAME_INFORMATION:
Namjae Jeon64b39f42021-03-30 14:25:35 +09005657 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005658 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005659 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005660 return -EACCES;
5661 }
5662 return set_rename_info(work, fp, buf);
5663
5664 case FILE_LINK_INFORMATION:
5665 return smb2_create_link(work, work->tcon->share_conf,
Namjae Jeon070fb212021-05-26 17:57:12 +09005666 (struct smb2_file_link_info *)buf, fp->filp,
5667 work->sess->conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09005668
5669 case FILE_DISPOSITION_INFORMATION:
Namjae Jeon64b39f42021-03-30 14:25:35 +09005670 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005671 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005672 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005673 return -EACCES;
5674 }
5675 return set_file_disposition_info(fp, buf);
5676
5677 case FILE_FULL_EA_INFORMATION:
5678 {
5679 if (!(fp->daccess & FILE_WRITE_EA_LE)) {
5680 ksmbd_err("Not permitted to write ext attr: 0x%x\n",
5681 fp->daccess);
5682 return -EACCES;
5683 }
5684
5685 return smb2_set_ea((struct smb2_ea_info *)buf,
5686 &fp->filp->f_path);
5687 }
5688
5689 case FILE_POSITION_INFORMATION:
5690 return set_file_position_info(fp, buf);
5691
5692 case FILE_MODE_INFORMATION:
5693 return set_file_mode_info(fp, buf);
5694 }
5695
5696 ksmbd_err("Unimplemented Fileinfoclass :%d\n", info_class);
5697 return -EOPNOTSUPP;
5698}
5699
Namjae Jeon64b39f42021-03-30 14:25:35 +09005700static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005701 char *buffer, int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005702{
5703 struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer;
5704
5705 fp->saccess |= FILE_SHARE_DELETE_LE;
5706
5707 return set_info_sec(fp->conn, fp->tcon, fp->filp->f_path.dentry, pntsd,
5708 buf_len, false);
5709}
5710
5711/**
5712 * smb2_set_info() - handler for smb2 set info command handler
5713 * @work: smb work containing set info request buffer
5714 *
5715 * Return: 0 on success, otherwise error
5716 */
5717int smb2_set_info(struct ksmbd_work *work)
5718{
5719 struct smb2_set_info_req *req;
5720 struct smb2_set_info_rsp *rsp, *rsp_org;
5721 struct ksmbd_file *fp;
5722 int rc = 0;
5723 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5724
5725 ksmbd_debug(SMB, "Received set info request\n");
5726
Namjae Jeone5066492021-03-30 12:35:23 +09005727 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005728 if (work->next_smb2_rcv_hdr_off) {
5729 req = REQUEST_BUF_NEXT(work);
5730 rsp = RESPONSE_BUF_NEXT(work);
5731 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
5732 ksmbd_debug(SMB, "Compound request set FID = %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005733 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005734 id = work->compound_fid;
5735 pid = work->compound_pfid;
5736 }
5737 } else {
Namjae Jeone5066492021-03-30 12:35:23 +09005738 req = work->request_buf;
5739 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005740 }
5741
5742 if (!HAS_FILE_ID(id)) {
5743 id = le64_to_cpu(req->VolatileFileId);
5744 pid = le64_to_cpu(req->PersistentFileId);
5745 }
5746
5747 fp = ksmbd_lookup_fd_slow(work, id, pid);
5748 if (!fp) {
5749 ksmbd_debug(SMB, "Invalid id for close: %u\n", id);
5750 rc = -ENOENT;
5751 goto err_out;
5752 }
5753
5754 switch (req->InfoType) {
5755 case SMB2_O_INFO_FILE:
5756 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
5757 rc = smb2_set_info_file(work, fp, req->FileInfoClass,
5758 req->Buffer, work->tcon->share_conf);
5759 break;
5760 case SMB2_O_INFO_SECURITY:
5761 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
5762 rc = smb2_set_info_sec(fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005763 le32_to_cpu(req->AdditionalInformation),
5764 req->Buffer,
5765 le32_to_cpu(req->BufferLength));
Namjae Jeone2f34482021-03-16 10:49:09 +09005766 break;
5767 default:
5768 rc = -EOPNOTSUPP;
5769 }
5770
5771 if (rc < 0)
5772 goto err_out;
5773
5774 rsp->StructureSize = cpu_to_le16(2);
5775 inc_rfc1001_len(rsp_org, 2);
5776 ksmbd_fd_put(work, fp);
5777 return 0;
5778
5779err_out:
5780 if (rc == -EACCES || rc == -EPERM)
5781 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5782 else if (rc == -EINVAL)
5783 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5784 else if (rc == -ESHARE)
5785 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
5786 else if (rc == -ENOENT)
5787 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
5788 else if (rc == -EBUSY || rc == -ENOTEMPTY)
5789 rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY;
5790 else if (rc == -EAGAIN)
5791 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
Namjae Jeonff1d5722021-04-13 13:18:10 +09005792 else if (rc == -EBADF || rc == -ESTALE)
Namjae Jeone2f34482021-03-16 10:49:09 +09005793 rsp->hdr.Status = STATUS_INVALID_HANDLE;
5794 else if (rc == -EEXIST)
5795 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
5796 else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP)
5797 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5798 smb2_set_err_rsp(work);
5799 ksmbd_fd_put(work, fp);
Namjae Jeon070fb212021-05-26 17:57:12 +09005800 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005801 return rc;
5802}
5803
5804/**
5805 * smb2_read_pipe() - handler for smb2 read from IPC pipe
5806 * @work: smb work containing read IPC pipe command buffer
5807 *
5808 * Return: 0 on success, otherwise error
5809 */
5810static noinline int smb2_read_pipe(struct ksmbd_work *work)
5811{
5812 int nbytes = 0, err;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005813 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09005814 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeone5066492021-03-30 12:35:23 +09005815 struct smb2_read_req *req = work->request_buf;
5816 struct smb2_read_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005817
5818 id = le64_to_cpu(req->VolatileFileId);
5819
5820 inc_rfc1001_len(rsp, 16);
5821 rpc_resp = ksmbd_rpc_read(work->sess, id);
5822 if (rpc_resp) {
5823 if (rpc_resp->flags != KSMBD_RPC_OK) {
5824 err = -EINVAL;
5825 goto out;
5826 }
5827
5828 work->aux_payload_buf =
Namjae Jeon79f6b112021-04-02 12:47:14 +09005829 kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09005830 if (!work->aux_payload_buf) {
5831 err = -ENOMEM;
5832 goto out;
5833 }
5834
5835 memcpy(work->aux_payload_buf, rpc_resp->payload,
Namjae Jeon070fb212021-05-26 17:57:12 +09005836 rpc_resp->payload_sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09005837
5838 nbytes = rpc_resp->payload_sz;
5839 work->resp_hdr_sz = get_rfc1002_len(rsp) + 4;
5840 work->aux_payload_sz = nbytes;
Namjae Jeon79f6b112021-04-02 12:47:14 +09005841 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005842 }
5843
5844 rsp->StructureSize = cpu_to_le16(17);
5845 rsp->DataOffset = 80;
5846 rsp->Reserved = 0;
5847 rsp->DataLength = cpu_to_le32(nbytes);
5848 rsp->DataRemaining = 0;
5849 rsp->Reserved2 = 0;
5850 inc_rfc1001_len(rsp, nbytes);
5851 return 0;
5852
5853out:
5854 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5855 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09005856 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005857 return err;
5858}
5859
5860static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005861 struct smb2_read_req *req, void *data_buf,
5862 size_t length)
Namjae Jeone2f34482021-03-16 10:49:09 +09005863{
5864 struct smb2_buffer_desc_v1 *desc =
5865 (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
5866 int err;
5867
Namjae Jeon64b39f42021-03-30 14:25:35 +09005868 if (work->conn->dialect == SMB30_PROT_ID &&
5869 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09005870 return -EINVAL;
5871
Namjae Jeon64b39f42021-03-30 14:25:35 +09005872 if (req->ReadChannelInfoOffset == 0 ||
5873 le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09005874 return -EINVAL;
5875
5876 work->need_invalidate_rkey =
5877 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
5878 work->remote_key = le32_to_cpu(desc->token);
5879
Namjae Jeon64b39f42021-03-30 14:25:35 +09005880 err = ksmbd_conn_rdma_write(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09005881 le32_to_cpu(desc->token),
5882 le64_to_cpu(desc->offset),
5883 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09005884 if (err)
5885 return err;
5886
5887 return length;
5888}
5889
5890/**
5891 * smb2_read() - handler for smb2 read from file
5892 * @work: smb work containing read command buffer
5893 *
5894 * Return: 0 on success, otherwise error
5895 */
5896int smb2_read(struct ksmbd_work *work)
5897{
5898 struct ksmbd_conn *conn = work->conn;
5899 struct smb2_read_req *req;
5900 struct smb2_read_rsp *rsp, *rsp_org;
5901 struct ksmbd_file *fp;
5902 loff_t offset;
5903 size_t length, mincount;
5904 ssize_t nbytes = 0, remain_bytes = 0;
5905 int err = 0;
5906
Namjae Jeone5066492021-03-30 12:35:23 +09005907 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005908 WORK_BUFFERS(work, req, rsp);
5909
5910 if (test_share_config_flag(work->tcon->share_conf,
5911 KSMBD_SHARE_FLAG_PIPE)) {
5912 ksmbd_debug(SMB, "IPC pipe read request\n");
5913 return smb2_read_pipe(work);
5914 }
5915
Namjae Jeon070fb212021-05-26 17:57:12 +09005916 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
5917 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09005918 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09005919 err = -ENOENT;
5920 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005921 }
5922
5923 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
5924 ksmbd_err("Not permitted to read : 0x%x\n", fp->daccess);
5925 err = -EACCES;
5926 goto out;
5927 }
5928
5929 offset = le64_to_cpu(req->Offset);
5930 length = le32_to_cpu(req->Length);
5931 mincount = le32_to_cpu(req->MinimumCount);
5932
5933 if (length > conn->vals->max_read_size) {
5934 ksmbd_debug(SMB, "limiting read size to max size(%u)\n",
5935 conn->vals->max_read_size);
5936 err = -EINVAL;
5937 goto out;
5938 }
5939
5940 ksmbd_debug(SMB, "filename %s, offset %lld, len %zu\n", FP_FILENAME(fp),
Namjae Jeon070fb212021-05-26 17:57:12 +09005941 offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09005942
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09005943 work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09005944 if (!work->aux_payload_buf) {
Dan Carpenterc1ea1112021-03-22 17:50:11 +03005945 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09005946 goto out;
5947 }
5948
5949 nbytes = ksmbd_vfs_read(work, fp, length, &offset);
5950 if (nbytes < 0) {
5951 err = nbytes;
5952 goto out;
5953 }
5954
5955 if ((nbytes == 0 && length != 0) || nbytes < mincount) {
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09005956 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09005957 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005958 rsp->hdr.Status = STATUS_END_OF_FILE;
5959 smb2_set_err_rsp(work);
5960 ksmbd_fd_put(work, fp);
5961 return 0;
5962 }
5963
5964 ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005965 nbytes, offset, mincount);
Namjae Jeone2f34482021-03-16 10:49:09 +09005966
5967 if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09005968 req->Channel == SMB2_CHANNEL_RDMA_V1) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005969 /* write data to the client using rdma channel */
5970 remain_bytes = smb2_read_rdma_channel(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09005971 work->aux_payload_buf,
5972 nbytes);
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09005973 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09005974 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005975
5976 nbytes = 0;
5977 if (remain_bytes < 0) {
5978 err = (int)remain_bytes;
5979 goto out;
5980 }
5981 }
5982
5983 rsp->StructureSize = cpu_to_le16(17);
5984 rsp->DataOffset = 80;
5985 rsp->Reserved = 0;
5986 rsp->DataLength = cpu_to_le32(nbytes);
5987 rsp->DataRemaining = cpu_to_le32(remain_bytes);
5988 rsp->Reserved2 = 0;
5989 inc_rfc1001_len(rsp_org, 16);
5990 work->resp_hdr_sz = get_rfc1002_len(rsp_org) + 4;
5991 work->aux_payload_sz = nbytes;
5992 inc_rfc1001_len(rsp_org, nbytes);
5993 ksmbd_fd_put(work, fp);
5994 return 0;
5995
5996out:
5997 if (err) {
5998 if (err == -EISDIR)
5999 rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST;
6000 else if (err == -EAGAIN)
6001 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6002 else if (err == -ENOENT)
6003 rsp->hdr.Status = STATUS_FILE_CLOSED;
6004 else if (err == -EACCES)
6005 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6006 else if (err == -ESHARE)
6007 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6008 else if (err == -EINVAL)
6009 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6010 else
6011 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6012
6013 smb2_set_err_rsp(work);
6014 }
6015 ksmbd_fd_put(work, fp);
6016 return err;
6017}
6018
6019/**
6020 * smb2_write_pipe() - handler for smb2 write on IPC pipe
6021 * @work: smb work containing write IPC pipe command buffer
6022 *
6023 * Return: 0 on success, otherwise error
6024 */
6025static noinline int smb2_write_pipe(struct ksmbd_work *work)
6026{
Namjae Jeone5066492021-03-30 12:35:23 +09006027 struct smb2_write_req *req = work->request_buf;
6028 struct smb2_write_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006029 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006030 u64 id = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09006031 int err = 0, ret = 0;
6032 char *data_buf;
6033 size_t length;
6034
6035 length = le32_to_cpu(req->Length);
6036 id = le64_to_cpu(req->VolatileFileId);
6037
6038 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09006039 (offsetof(struct smb2_write_req, Buffer) - 4)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006040 data_buf = (char *)&req->Buffer[0];
6041 } else {
6042 if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006043 (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006044 ksmbd_err("invalid write data offset %u, smb_len %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006045 le16_to_cpu(req->DataOffset),
6046 get_rfc1002_len(req));
Namjae Jeone2f34482021-03-16 10:49:09 +09006047 err = -EINVAL;
6048 goto out;
6049 }
6050
6051 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6052 le16_to_cpu(req->DataOffset));
6053 }
6054
6055 rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length);
6056 if (rpc_resp) {
6057 if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
6058 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006059 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006060 smb2_set_err_rsp(work);
6061 return -EOPNOTSUPP;
6062 }
6063 if (rpc_resp->flags != KSMBD_RPC_OK) {
6064 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6065 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006066 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006067 return ret;
6068 }
Namjae Jeon79f6b112021-04-02 12:47:14 +09006069 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006070 }
6071
6072 rsp->StructureSize = cpu_to_le16(17);
6073 rsp->DataOffset = 0;
6074 rsp->Reserved = 0;
6075 rsp->DataLength = cpu_to_le32(length);
6076 rsp->DataRemaining = 0;
6077 rsp->Reserved2 = 0;
6078 inc_rfc1001_len(rsp, 16);
6079 return 0;
6080out:
6081 if (err) {
6082 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6083 smb2_set_err_rsp(work);
6084 }
6085
6086 return err;
6087}
6088
6089static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006090 struct smb2_write_req *req,
6091 struct ksmbd_file *fp,
6092 loff_t offset, size_t length, bool sync)
Namjae Jeone2f34482021-03-16 10:49:09 +09006093{
6094 struct smb2_buffer_desc_v1 *desc;
6095 char *data_buf;
6096 int ret;
6097 ssize_t nbytes;
6098
6099 desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6100
6101 if (work->conn->dialect == SMB30_PROT_ID &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006102 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09006103 return -EINVAL;
6104
6105 if (req->Length != 0 || req->DataOffset != 0)
6106 return -EINVAL;
6107
Namjae Jeon64b39f42021-03-30 14:25:35 +09006108 if (req->WriteChannelInfoOffset == 0 ||
6109 le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09006110 return -EINVAL;
6111
6112 work->need_invalidate_rkey =
6113 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6114 work->remote_key = le32_to_cpu(desc->token);
6115
Namjae Jeon79f6b112021-04-02 12:47:14 +09006116 data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006117 if (!data_buf)
6118 return -ENOMEM;
6119
6120 ret = ksmbd_conn_rdma_read(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006121 le32_to_cpu(desc->token),
6122 le64_to_cpu(desc->offset),
6123 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006124 if (ret < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09006125 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006126 return ret;
6127 }
6128
Namjae Jeon64b39f42021-03-30 14:25:35 +09006129 ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006130 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006131 if (ret < 0)
6132 return ret;
6133
6134 return nbytes;
6135}
6136
6137/**
6138 * smb2_write() - handler for smb2 write from file
6139 * @work: smb work containing write command buffer
6140 *
6141 * Return: 0 on success, otherwise error
6142 */
6143int smb2_write(struct ksmbd_work *work)
6144{
6145 struct smb2_write_req *req;
6146 struct smb2_write_rsp *rsp, *rsp_org;
Namjae Jeonbcd62a32021-05-10 09:08:19 +09006147 struct ksmbd_file *fp = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006148 loff_t offset;
6149 size_t length;
6150 ssize_t nbytes;
6151 char *data_buf;
6152 bool writethrough = false;
6153 int err = 0;
6154
Namjae Jeone5066492021-03-30 12:35:23 +09006155 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006156 WORK_BUFFERS(work, req, rsp);
6157
Namjae Jeon64b39f42021-03-30 14:25:35 +09006158 if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006159 ksmbd_debug(SMB, "IPC pipe write request\n");
6160 return smb2_write_pipe(work);
6161 }
6162
6163 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
6164 ksmbd_debug(SMB, "User does not have write permission\n");
6165 err = -EACCES;
6166 goto out;
6167 }
6168
Namjae Jeon64b39f42021-03-30 14:25:35 +09006169 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
Namjae Jeon070fb212021-05-26 17:57:12 +09006170 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006171 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006172 err = -ENOENT;
6173 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006174 }
6175
6176 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
6177 ksmbd_err("Not permitted to write : 0x%x\n", fp->daccess);
6178 err = -EACCES;
6179 goto out;
6180 }
6181
6182 offset = le64_to_cpu(req->Offset);
6183 length = le32_to_cpu(req->Length);
6184
6185 if (length > work->conn->vals->max_write_size) {
6186 ksmbd_debug(SMB, "limiting write size to max size(%u)\n",
6187 work->conn->vals->max_write_size);
6188 err = -EINVAL;
6189 goto out;
6190 }
6191
6192 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6193 writethrough = true;
6194
6195 if (req->Channel != SMB2_CHANNEL_RDMA_V1 &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006196 req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006197 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeon070fb212021-05-26 17:57:12 +09006198 (offsetof(struct smb2_write_req, Buffer) - 4)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006199 data_buf = (char *)&req->Buffer[0];
6200 } else {
Namjae Jeon64b39f42021-03-30 14:25:35 +09006201 if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) ||
6202 (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006203 ksmbd_err("invalid write data offset %u, smb_len %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006204 le16_to_cpu(req->DataOffset),
6205 get_rfc1002_len(req));
Namjae Jeone2f34482021-03-16 10:49:09 +09006206 err = -EINVAL;
6207 goto out;
6208 }
6209
6210 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6211 le16_to_cpu(req->DataOffset));
6212 }
6213
6214 ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags));
6215 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6216 writethrough = true;
6217
6218 ksmbd_debug(SMB, "filename %s, offset %lld, len %zu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006219 FP_FILENAME(fp), offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006220 err = ksmbd_vfs_write(work, fp, data_buf, length, &offset,
6221 writethrough, &nbytes);
6222 if (err < 0)
6223 goto out;
6224 } else {
6225 /* read data from the client using rdma channel, and
6226 * write the data.
6227 */
6228 nbytes = smb2_write_rdma_channel(work, req, fp, offset,
Namjae Jeon070fb212021-05-26 17:57:12 +09006229 le32_to_cpu(req->RemainingBytes),
6230 writethrough);
Namjae Jeone2f34482021-03-16 10:49:09 +09006231 if (nbytes < 0) {
6232 err = (int)nbytes;
6233 goto out;
6234 }
6235 }
6236
6237 rsp->StructureSize = cpu_to_le16(17);
6238 rsp->DataOffset = 0;
6239 rsp->Reserved = 0;
6240 rsp->DataLength = cpu_to_le32(nbytes);
6241 rsp->DataRemaining = 0;
6242 rsp->Reserved2 = 0;
6243 inc_rfc1001_len(rsp_org, 16);
6244 ksmbd_fd_put(work, fp);
6245 return 0;
6246
6247out:
6248 if (err == -EAGAIN)
6249 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6250 else if (err == -ENOSPC || err == -EFBIG)
6251 rsp->hdr.Status = STATUS_DISK_FULL;
6252 else if (err == -ENOENT)
6253 rsp->hdr.Status = STATUS_FILE_CLOSED;
6254 else if (err == -EACCES)
6255 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6256 else if (err == -ESHARE)
6257 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6258 else if (err == -EINVAL)
6259 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6260 else
6261 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6262
6263 smb2_set_err_rsp(work);
6264 ksmbd_fd_put(work, fp);
6265 return err;
6266}
6267
6268/**
6269 * smb2_flush() - handler for smb2 flush file - fsync
6270 * @work: smb work containing flush command buffer
6271 *
6272 * Return: 0 on success, otherwise error
6273 */
6274int smb2_flush(struct ksmbd_work *work)
6275{
6276 struct smb2_flush_req *req;
6277 struct smb2_flush_rsp *rsp, *rsp_org;
6278 int err;
6279
Namjae Jeone5066492021-03-30 12:35:23 +09006280 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006281 WORK_BUFFERS(work, req, rsp);
6282
6283 ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006284 le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006285
6286 err = ksmbd_vfs_fsync(work,
6287 le64_to_cpu(req->VolatileFileId),
6288 le64_to_cpu(req->PersistentFileId));
6289 if (err)
6290 goto out;
6291
6292 rsp->StructureSize = cpu_to_le16(4);
6293 rsp->Reserved = 0;
6294 inc_rfc1001_len(rsp_org, 4);
6295 return 0;
6296
6297out:
6298 if (err) {
6299 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6300 smb2_set_err_rsp(work);
6301 }
6302
6303 return err;
6304}
6305
6306/**
6307 * smb2_cancel() - handler for smb2 cancel command
6308 * @work: smb work containing cancel command buffer
6309 *
6310 * Return: 0 on success, otherwise error
6311 */
6312int smb2_cancel(struct ksmbd_work *work)
6313{
6314 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09006315 struct smb2_hdr *hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006316 struct smb2_hdr *chdr;
6317 struct ksmbd_work *cancel_work = NULL;
6318 struct list_head *tmp;
6319 int canceled = 0;
6320 struct list_head *command_list;
6321
6322 ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006323 hdr->MessageId, hdr->Flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09006324
6325 if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) {
6326 command_list = &conn->async_requests;
6327
6328 spin_lock(&conn->request_lock);
6329 list_for_each(tmp, command_list) {
6330 cancel_work = list_entry(tmp, struct ksmbd_work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006331 async_request_entry);
Namjae Jeone5066492021-03-30 12:35:23 +09006332 chdr = cancel_work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006333
6334 if (cancel_work->async_id !=
Namjae Jeon64b39f42021-03-30 14:25:35 +09006335 le64_to_cpu(hdr->Id.AsyncId))
Namjae Jeone2f34482021-03-16 10:49:09 +09006336 continue;
6337
6338 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006339 "smb2 with AsyncId %llu cancelled command = 0x%x\n",
6340 le64_to_cpu(hdr->Id.AsyncId),
6341 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006342 canceled = 1;
6343 break;
6344 }
6345 spin_unlock(&conn->request_lock);
6346 } else {
6347 command_list = &conn->requests;
6348
6349 spin_lock(&conn->request_lock);
6350 list_for_each(tmp, command_list) {
6351 cancel_work = list_entry(tmp, struct ksmbd_work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006352 request_entry);
Namjae Jeone5066492021-03-30 12:35:23 +09006353 chdr = cancel_work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006354
6355 if (chdr->MessageId != hdr->MessageId ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006356 cancel_work == work)
Namjae Jeone2f34482021-03-16 10:49:09 +09006357 continue;
6358
6359 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006360 "smb2 with mid %llu cancelled command = 0x%x\n",
6361 le64_to_cpu(hdr->MessageId),
6362 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006363 canceled = 1;
6364 break;
6365 }
6366 spin_unlock(&conn->request_lock);
6367 }
6368
6369 if (canceled) {
6370 cancel_work->state = KSMBD_WORK_CANCELLED;
6371 if (cancel_work->cancel_fn)
6372 cancel_work->cancel_fn(cancel_work->cancel_argv);
6373 }
6374
6375 /* For SMB2_CANCEL command itself send no response*/
6376 work->send_no_response = 1;
6377 return 0;
6378}
6379
6380struct file_lock *smb_flock_init(struct file *f)
6381{
6382 struct file_lock *fl;
6383
6384 fl = locks_alloc_lock();
6385 if (!fl)
6386 goto out;
6387
6388 locks_init_lock(fl);
6389
6390 fl->fl_owner = f;
6391 fl->fl_pid = current->tgid;
6392 fl->fl_file = f;
6393 fl->fl_flags = FL_POSIX;
6394 fl->fl_ops = NULL;
6395 fl->fl_lmops = NULL;
6396
6397out:
6398 return fl;
6399}
6400
6401static int smb2_set_flock_flags(struct file_lock *flock, int flags)
6402{
6403 int cmd = -EINVAL;
6404
6405 /* Checking for wrong flag combination during lock request*/
6406 switch (flags) {
6407 case SMB2_LOCKFLAG_SHARED:
6408 ksmbd_debug(SMB, "received shared request\n");
6409 cmd = F_SETLKW;
6410 flock->fl_type = F_RDLCK;
6411 flock->fl_flags |= FL_SLEEP;
6412 break;
6413 case SMB2_LOCKFLAG_EXCLUSIVE:
6414 ksmbd_debug(SMB, "received exclusive request\n");
6415 cmd = F_SETLKW;
6416 flock->fl_type = F_WRLCK;
6417 flock->fl_flags |= FL_SLEEP;
6418 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006419 case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006420 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006421 "received shared & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006422 cmd = F_SETLK;
6423 flock->fl_type = F_RDLCK;
6424 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006425 case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006426 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006427 "received exclusive & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006428 cmd = F_SETLK;
6429 flock->fl_type = F_WRLCK;
6430 break;
6431 case SMB2_LOCKFLAG_UNLOCK:
6432 ksmbd_debug(SMB, "received unlock request\n");
6433 flock->fl_type = F_UNLCK;
6434 cmd = 0;
6435 break;
6436 }
6437
6438 return cmd;
6439}
6440
6441static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock,
Namjae Jeon070fb212021-05-26 17:57:12 +09006442 unsigned int cmd, int flags,
6443 struct list_head *lock_list)
Namjae Jeone2f34482021-03-16 10:49:09 +09006444{
6445 struct ksmbd_lock *lock;
6446
6447 lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL);
6448 if (!lock)
6449 return NULL;
6450
6451 lock->cmd = cmd;
6452 lock->fl = flock;
6453 lock->start = flock->fl_start;
6454 lock->end = flock->fl_end;
6455 lock->flags = flags;
6456 if (lock->start == lock->end)
6457 lock->zero_len = 1;
6458 INIT_LIST_HEAD(&lock->llist);
6459 INIT_LIST_HEAD(&lock->glist);
6460 list_add_tail(&lock->llist, lock_list);
6461
6462 return lock;
6463}
6464
6465static void smb2_remove_blocked_lock(void **argv)
6466{
6467 struct file_lock *flock = (struct file_lock *)argv[0];
6468
6469 ksmbd_vfs_posix_lock_unblock(flock);
6470 wake_up(&flock->fl_wait);
6471}
6472
6473static inline bool lock_defer_pending(struct file_lock *fl)
6474{
6475 /* check pending lock waiters */
6476 return waitqueue_active(&fl->fl_wait);
6477}
6478
6479/**
6480 * smb2_lock() - handler for smb2 file lock command
6481 * @work: smb work containing lock command buffer
6482 *
6483 * Return: 0 on success, otherwise error
6484 */
6485int smb2_lock(struct ksmbd_work *work)
6486{
Namjae Jeone5066492021-03-30 12:35:23 +09006487 struct smb2_lock_req *req = work->request_buf;
6488 struct smb2_lock_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006489 struct smb2_lock_element *lock_ele;
6490 struct ksmbd_file *fp = NULL;
6491 struct file_lock *flock = NULL;
6492 struct file *filp = NULL;
6493 int lock_count;
6494 int flags = 0;
6495 int cmd = 0;
6496 int err = 0, i;
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006497 u64 lock_start, lock_length;
Namjae Jeone2f34482021-03-16 10:49:09 +09006498 struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp;
6499 int nolock = 0;
6500 LIST_HEAD(lock_list);
6501 LIST_HEAD(rollback_list);
6502 int prior_lock = 0;
6503
6504 ksmbd_debug(SMB, "Received lock request\n");
6505 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006506 le64_to_cpu(req->VolatileFileId),
6507 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006508 if (!fp) {
6509 ksmbd_debug(SMB, "Invalid file id for lock : %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006510 le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006511 rsp->hdr.Status = STATUS_FILE_CLOSED;
6512 goto out2;
6513 }
6514
6515 filp = fp->filp;
6516 lock_count = le16_to_cpu(req->LockCount);
6517 lock_ele = req->locks;
6518
6519 ksmbd_debug(SMB, "lock count is %d\n", lock_count);
Namjae Jeon070fb212021-05-26 17:57:12 +09006520 if (!lock_count) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006521 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6522 goto out2;
6523 }
6524
6525 for (i = 0; i < lock_count; i++) {
6526 flags = le32_to_cpu(lock_ele[i].Flags);
6527
6528 flock = smb_flock_init(filp);
6529 if (!flock) {
6530 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6531 goto out;
6532 }
6533
6534 cmd = smb2_set_flock_flags(flock, flags);
6535
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006536 lock_start = le64_to_cpu(lock_ele[i].Offset);
6537 lock_length = le64_to_cpu(lock_ele[i].Length);
6538 if (lock_start > U64_MAX - lock_length) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006539 ksmbd_err("Invalid lock range requested\n");
6540 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6541 goto out;
6542 }
6543
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006544 if (lock_start > OFFSET_MAX)
6545 flock->fl_start = OFFSET_MAX;
6546 else
6547 flock->fl_start = lock_start;
6548
Namjae Jeone2f34482021-03-16 10:49:09 +09006549 lock_length = le64_to_cpu(lock_ele[i].Length);
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006550 if (lock_length > OFFSET_MAX - flock->fl_start)
6551 lock_length = OFFSET_MAX - flock->fl_start;
Namjae Jeone2f34482021-03-16 10:49:09 +09006552
6553 flock->fl_end = flock->fl_start + lock_length;
6554
6555 if (flock->fl_end < flock->fl_start) {
6556 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006557 "the end offset(%llx) is smaller than the start offset(%llx)\n",
6558 flock->fl_end, flock->fl_start);
Namjae Jeone2f34482021-03-16 10:49:09 +09006559 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6560 goto out;
6561 }
6562
6563 /* Check conflict locks in one request */
6564 list_for_each_entry(cmp_lock, &lock_list, llist) {
6565 if (cmp_lock->fl->fl_start <= flock->fl_start &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006566 cmp_lock->fl->fl_end >= flock->fl_end) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006567 if (cmp_lock->fl->fl_type != F_UNLCK &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006568 flock->fl_type != F_UNLCK) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006569 ksmbd_err("conflict two locks in one request\n");
6570 rsp->hdr.Status =
6571 STATUS_INVALID_PARAMETER;
6572 goto out;
6573 }
6574 }
6575 }
6576
6577 smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list);
6578 if (!smb_lock) {
6579 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6580 goto out;
6581 }
6582 }
6583
6584 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6585 if (smb_lock->cmd < 0) {
6586 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6587 goto out;
6588 }
6589
6590 if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) {
6591 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6592 goto out;
6593 }
6594
Namjae Jeon64b39f42021-03-30 14:25:35 +09006595 if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) &&
6596 smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) ||
6597 (prior_lock == SMB2_LOCKFLAG_UNLOCK &&
6598 !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006599 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6600 goto out;
6601 }
6602
6603 prior_lock = smb_lock->flags;
6604
6605 if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006606 !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY))
Namjae Jeone2f34482021-03-16 10:49:09 +09006607 goto no_check_gl;
6608
6609 nolock = 1;
6610 /* check locks in global list */
6611 list_for_each_entry(cmp_lock, &global_lock_list, glist) {
6612 if (file_inode(cmp_lock->fl->fl_file) !=
Namjae Jeon64b39f42021-03-30 14:25:35 +09006613 file_inode(smb_lock->fl->fl_file))
Namjae Jeone2f34482021-03-16 10:49:09 +09006614 continue;
6615
6616 if (smb_lock->fl->fl_type == F_UNLCK) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09006617 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file &&
6618 cmp_lock->start == smb_lock->start &&
6619 cmp_lock->end == smb_lock->end &&
6620 !lock_defer_pending(cmp_lock->fl)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006621 nolock = 0;
6622 locks_free_lock(cmp_lock->fl);
6623 list_del(&cmp_lock->glist);
6624 kfree(cmp_lock);
6625 break;
6626 }
6627 continue;
6628 }
6629
6630 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) {
6631 if (smb_lock->flags & SMB2_LOCKFLAG_SHARED)
6632 continue;
6633 } else {
6634 if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED)
6635 continue;
6636 }
6637
6638 /* check zero byte lock range */
6639 if (cmp_lock->zero_len && !smb_lock->zero_len &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006640 cmp_lock->start > smb_lock->start &&
6641 cmp_lock->start < smb_lock->end) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006642 ksmbd_err("previous lock conflict with zero byte lock range\n");
6643 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6644 goto out;
6645 }
6646
6647 if (smb_lock->zero_len && !cmp_lock->zero_len &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006648 smb_lock->start > cmp_lock->start &&
6649 smb_lock->start < cmp_lock->end) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006650 ksmbd_err("current lock conflict with zero byte lock range\n");
6651 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6652 goto out;
6653 }
6654
6655 if (((cmp_lock->start <= smb_lock->start &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006656 cmp_lock->end > smb_lock->start) ||
6657 (cmp_lock->start < smb_lock->end && cmp_lock->end >= smb_lock->end)) &&
6658 !cmp_lock->zero_len && !smb_lock->zero_len) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006659 ksmbd_err("Not allow lock operation on exclusive lock range\n");
6660 rsp->hdr.Status =
6661 STATUS_LOCK_NOT_GRANTED;
6662 goto out;
6663 }
6664 }
6665
6666 if (smb_lock->fl->fl_type == F_UNLCK && nolock) {
6667 ksmbd_err("Try to unlock nolocked range\n");
6668 rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED;
6669 goto out;
6670 }
6671
6672no_check_gl:
6673 if (smb_lock->zero_len) {
6674 err = 0;
6675 goto skip;
6676 }
6677
6678 flock = smb_lock->fl;
6679 list_del(&smb_lock->llist);
6680retry:
6681 err = ksmbd_vfs_lock(filp, smb_lock->cmd, flock);
6682skip:
6683 if (flags & SMB2_LOCKFLAG_UNLOCK) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09006684 if (!err) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006685 ksmbd_debug(SMB, "File unlocked\n");
Namjae Jeon64b39f42021-03-30 14:25:35 +09006686 } else if (err == -ENOENT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006687 rsp->hdr.Status = STATUS_NOT_LOCKED;
6688 goto out;
6689 }
6690 locks_free_lock(flock);
6691 kfree(smb_lock);
6692 } else {
6693 if (err == FILE_LOCK_DEFERRED) {
6694 void **argv;
6695
6696 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006697 "would have to wait for getting lock\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006698 list_add_tail(&smb_lock->glist,
Namjae Jeon070fb212021-05-26 17:57:12 +09006699 &global_lock_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09006700 list_add(&smb_lock->llist, &rollback_list);
6701
6702 argv = kmalloc(sizeof(void *), GFP_KERNEL);
6703 if (!argv) {
6704 err = -ENOMEM;
6705 goto out;
6706 }
6707 argv[0] = flock;
6708
6709 err = setup_async_work(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006710 smb2_remove_blocked_lock,
6711 argv);
Namjae Jeone2f34482021-03-16 10:49:09 +09006712 if (err) {
6713 rsp->hdr.Status =
6714 STATUS_INSUFFICIENT_RESOURCES;
6715 goto out;
6716 }
6717 spin_lock(&fp->f_lock);
6718 list_add(&work->fp_entry, &fp->blocked_works);
6719 spin_unlock(&fp->f_lock);
6720
6721 smb2_send_interim_resp(work, STATUS_PENDING);
6722
6723 err = ksmbd_vfs_posix_lock_wait(flock);
6724
6725 if (!WORK_ACTIVE(work)) {
6726 list_del(&smb_lock->llist);
6727 list_del(&smb_lock->glist);
6728 locks_free_lock(flock);
6729
6730 if (WORK_CANCELLED(work)) {
6731 spin_lock(&fp->f_lock);
6732 list_del(&work->fp_entry);
6733 spin_unlock(&fp->f_lock);
6734 rsp->hdr.Status =
6735 STATUS_CANCELLED;
6736 kfree(smb_lock);
6737 smb2_send_interim_resp(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006738 STATUS_CANCELLED);
Namjae Jeone2f34482021-03-16 10:49:09 +09006739 work->send_no_response = 1;
6740 goto out;
6741 }
6742 init_smb2_rsp_hdr(work);
6743 smb2_set_err_rsp(work);
6744 rsp->hdr.Status =
6745 STATUS_RANGE_NOT_LOCKED;
6746 kfree(smb_lock);
6747 goto out2;
6748 }
6749
6750 list_del(&smb_lock->llist);
6751 list_del(&smb_lock->glist);
6752 spin_lock(&fp->f_lock);
6753 list_del(&work->fp_entry);
6754 spin_unlock(&fp->f_lock);
6755 goto retry;
6756 } else if (!err) {
6757 list_add_tail(&smb_lock->glist,
Namjae Jeon070fb212021-05-26 17:57:12 +09006758 &global_lock_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09006759 list_add(&smb_lock->llist, &rollback_list);
6760 ksmbd_debug(SMB, "successful in taking lock\n");
6761 } else {
6762 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6763 goto out;
6764 }
6765 }
6766 }
6767
6768 if (atomic_read(&fp->f_ci->op_count) > 1)
6769 smb_break_all_oplock(work, fp);
6770
6771 rsp->StructureSize = cpu_to_le16(4);
6772 ksmbd_debug(SMB, "successful in taking lock\n");
6773 rsp->hdr.Status = STATUS_SUCCESS;
6774 rsp->Reserved = 0;
6775 inc_rfc1001_len(rsp, 4);
6776 ksmbd_fd_put(work, fp);
6777 return err;
6778
6779out:
6780 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6781 locks_free_lock(smb_lock->fl);
6782 list_del(&smb_lock->llist);
6783 kfree(smb_lock);
6784 }
6785
6786 list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) {
6787 struct file_lock *rlock = NULL;
6788
6789 rlock = smb_flock_init(filp);
6790 rlock->fl_type = F_UNLCK;
6791 rlock->fl_start = smb_lock->start;
6792 rlock->fl_end = smb_lock->end;
6793
6794 err = ksmbd_vfs_lock(filp, 0, rlock);
6795 if (err)
6796 ksmbd_err("rollback unlock fail : %d\n", err);
6797 list_del(&smb_lock->llist);
6798 list_del(&smb_lock->glist);
6799 locks_free_lock(smb_lock->fl);
6800 locks_free_lock(rlock);
6801 kfree(smb_lock);
6802 }
6803out2:
6804 ksmbd_debug(SMB, "failed in taking lock(flags : %x)\n", flags);
6805 smb2_set_err_rsp(work);
6806 ksmbd_fd_put(work, fp);
6807 return 0;
6808}
6809
Namjae Jeon64b39f42021-03-30 14:25:35 +09006810static int fsctl_copychunk(struct ksmbd_work *work, struct smb2_ioctl_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09006811 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09006812{
6813 struct copychunk_ioctl_req *ci_req;
6814 struct copychunk_ioctl_rsp *ci_rsp;
6815 struct ksmbd_file *src_fp = NULL, *dst_fp = NULL;
6816 struct srv_copychunk *chunks;
6817 unsigned int i, chunk_count, chunk_count_written = 0;
6818 unsigned int chunk_size_written = 0;
6819 loff_t total_size_written = 0;
6820 int ret, cnt_code;
6821
6822 cnt_code = le32_to_cpu(req->CntCode);
6823 ci_req = (struct copychunk_ioctl_req *)&req->Buffer[0];
6824 ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0];
6825
6826 rsp->VolatileFileId = req->VolatileFileId;
6827 rsp->PersistentFileId = req->PersistentFileId;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006828 ci_rsp->ChunksWritten =
6829 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count());
6830 ci_rsp->ChunkBytesWritten =
6831 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size());
6832 ci_rsp->TotalBytesWritten =
6833 cpu_to_le32(ksmbd_server_side_copy_max_total_size());
Namjae Jeone2f34482021-03-16 10:49:09 +09006834
6835 chunks = (struct srv_copychunk *)&ci_req->Chunks[0];
6836 chunk_count = le32_to_cpu(ci_req->ChunkCount);
6837 total_size_written = 0;
6838
6839 /* verify the SRV_COPYCHUNK_COPY packet */
6840 if (chunk_count > ksmbd_server_side_copy_max_chunk_count() ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006841 le32_to_cpu(req->InputCount) <
6842 offsetof(struct copychunk_ioctl_req, Chunks) +
6843 chunk_count * sizeof(struct srv_copychunk)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006844 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6845 return -EINVAL;
6846 }
6847
6848 for (i = 0; i < chunk_count; i++) {
6849 if (le32_to_cpu(chunks[i].Length) == 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006850 le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size())
Namjae Jeone2f34482021-03-16 10:49:09 +09006851 break;
6852 total_size_written += le32_to_cpu(chunks[i].Length);
6853 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09006854
6855 if (i < chunk_count ||
6856 total_size_written > ksmbd_server_side_copy_max_total_size()) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006857 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6858 return -EINVAL;
6859 }
6860
6861 src_fp = ksmbd_lookup_foreign_fd(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006862 le64_to_cpu(ci_req->ResumeKey[0]));
Namjae Jeone2f34482021-03-16 10:49:09 +09006863 dst_fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006864 le64_to_cpu(req->VolatileFileId),
6865 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006866 ret = -EINVAL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006867 if (!src_fp ||
6868 src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006869 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
6870 goto out;
6871 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09006872
Namjae Jeone2f34482021-03-16 10:49:09 +09006873 if (!dst_fp) {
6874 rsp->hdr.Status = STATUS_FILE_CLOSED;
6875 goto out;
6876 }
6877
6878 /*
6879 * FILE_READ_DATA should only be included in
6880 * the FSCTL_COPYCHUNK case
6881 */
Namjae Jeon070fb212021-05-26 17:57:12 +09006882 if (cnt_code == FSCTL_COPYCHUNK &&
6883 !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006884 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6885 goto out;
6886 }
6887
6888 ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09006889 chunks, chunk_count,
6890 &chunk_count_written,
6891 &chunk_size_written,
6892 &total_size_written);
Namjae Jeone2f34482021-03-16 10:49:09 +09006893 if (ret < 0) {
6894 if (ret == -EACCES)
6895 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6896 if (ret == -EAGAIN)
6897 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6898 else if (ret == -EBADF)
6899 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6900 else if (ret == -EFBIG || ret == -ENOSPC)
6901 rsp->hdr.Status = STATUS_DISK_FULL;
6902 else if (ret == -EINVAL)
6903 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6904 else if (ret == -EISDIR)
6905 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
6906 else if (ret == -E2BIG)
6907 rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE;
6908 else
6909 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
6910 }
6911
6912 ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written);
6913 ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written);
6914 ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written);
6915out:
6916 ksmbd_fd_put(work, src_fp);
6917 ksmbd_fd_put(work, dst_fp);
6918 return ret;
6919}
6920
6921static __be32 idev_ipv4_address(struct in_device *idev)
6922{
6923 __be32 addr = 0;
6924
6925 struct in_ifaddr *ifa;
6926
6927 rcu_read_lock();
6928 in_dev_for_each_ifa_rcu(ifa, idev) {
6929 if (ifa->ifa_flags & IFA_F_SECONDARY)
6930 continue;
6931
6932 addr = ifa->ifa_address;
6933 break;
6934 }
6935 rcu_read_unlock();
6936 return addr;
6937}
6938
6939static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09006940 struct smb2_ioctl_req *req,
6941 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09006942{
6943 struct network_interface_info_ioctl_rsp *nii_rsp = NULL;
6944 int nbytes = 0;
6945 struct net_device *netdev;
6946 struct sockaddr_storage_rsp *sockaddr_storage;
6947 unsigned int flags;
6948 unsigned long long speed;
6949
6950 rtnl_lock();
6951 for_each_netdev(&init_net, netdev) {
6952 if (unlikely(!netdev)) {
6953 rtnl_unlock();
6954 return -EINVAL;
6955 }
6956
6957 if (netdev->type == ARPHRD_LOOPBACK)
6958 continue;
6959
6960 flags = dev_get_flags(netdev);
6961 if (!(flags & IFF_RUNNING))
6962 continue;
6963
6964 nii_rsp = (struct network_interface_info_ioctl_rsp *)
6965 &rsp->Buffer[nbytes];
6966 nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex);
6967
6968 /* TODO: specify the RDMA capabilities */
6969 if (netdev->num_tx_queues > 1)
6970 nii_rsp->Capability = cpu_to_le32(RSS_CAPABLE);
6971 else
6972 nii_rsp->Capability = 0;
6973
6974 nii_rsp->Next = cpu_to_le32(152);
6975 nii_rsp->Reserved = 0;
6976
6977 if (netdev->ethtool_ops->get_link_ksettings) {
6978 struct ethtool_link_ksettings cmd;
6979
6980 netdev->ethtool_ops->get_link_ksettings(netdev, &cmd);
6981 speed = cmd.base.speed;
6982 } else {
Namjae Jeon64b39f42021-03-30 14:25:35 +09006983 ksmbd_err("%s %s\n", netdev->name,
Namjae Jeon070fb212021-05-26 17:57:12 +09006984 "speed is unknown, defaulting to 1Gb/sec");
Namjae Jeone2f34482021-03-16 10:49:09 +09006985 speed = SPEED_1000;
6986 }
6987
6988 speed *= 1000000;
6989 nii_rsp->LinkSpeed = cpu_to_le64(speed);
6990
6991 sockaddr_storage = (struct sockaddr_storage_rsp *)
6992 nii_rsp->SockAddr_Storage;
6993 memset(sockaddr_storage, 0, 128);
6994
6995 if (conn->peer_addr.ss_family == PF_INET) {
6996 struct in_device *idev;
6997
6998 sockaddr_storage->Family = cpu_to_le16(INTERNETWORK);
6999 sockaddr_storage->addr4.Port = 0;
7000
7001 idev = __in_dev_get_rtnl(netdev);
7002 if (!idev)
7003 continue;
7004 sockaddr_storage->addr4.IPv4address =
7005 idev_ipv4_address(idev);
7006 } else {
7007 struct inet6_dev *idev6;
7008 struct inet6_ifaddr *ifa;
7009 __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address;
7010
7011 sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6);
7012 sockaddr_storage->addr6.Port = 0;
7013 sockaddr_storage->addr6.FlowInfo = 0;
7014
7015 idev6 = __in6_dev_get(netdev);
7016 if (!idev6)
7017 continue;
7018
7019 list_for_each_entry(ifa, &idev6->addr_list, if_list) {
7020 if (ifa->flags & (IFA_F_TENTATIVE |
7021 IFA_F_DEPRECATED))
7022 continue;
7023 memcpy(ipv6_addr, ifa->addr.s6_addr, 16);
7024 break;
7025 }
7026 sockaddr_storage->addr6.ScopeId = 0;
7027 }
7028
7029 nbytes += sizeof(struct network_interface_info_ioctl_rsp);
7030 }
7031 rtnl_unlock();
7032
7033 /* zero if this is last one */
7034 if (nii_rsp)
7035 nii_rsp->Next = 0;
7036
7037 if (!nbytes) {
7038 rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL;
7039 return -EINVAL;
7040 }
7041
7042 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7043 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7044 return nbytes;
7045}
7046
Namjae Jeone2f34482021-03-16 10:49:09 +09007047static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09007048 struct validate_negotiate_info_req *neg_req,
7049 struct validate_negotiate_info_rsp *neg_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007050{
7051 int ret = 0;
7052 int dialect;
7053
7054 dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects,
Namjae Jeon070fb212021-05-26 17:57:12 +09007055 neg_req->DialectCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09007056 if (dialect == BAD_PROT_ID || dialect != conn->dialect) {
7057 ret = -EINVAL;
7058 goto err_out;
7059 }
7060
7061 if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) {
7062 ret = -EINVAL;
7063 goto err_out;
7064 }
7065
7066 if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) {
7067 ret = -EINVAL;
7068 goto err_out;
7069 }
7070
7071 if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) {
7072 ret = -EINVAL;
7073 goto err_out;
7074 }
7075
7076 neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
7077 memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE);
7078 neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode);
7079 neg_rsp->Dialect = cpu_to_le16(conn->dialect);
7080err_out:
7081 return ret;
7082}
7083
Namjae Jeon64b39f42021-03-30 14:25:35 +09007084static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007085 struct file_allocated_range_buffer *qar_req,
7086 struct file_allocated_range_buffer *qar_rsp,
7087 int in_count, int *out_count)
Namjae Jeone2f34482021-03-16 10:49:09 +09007088{
7089 struct ksmbd_file *fp;
7090 loff_t start, length;
7091 int ret = 0;
7092
7093 *out_count = 0;
7094 if (in_count == 0)
7095 return -EINVAL;
7096
7097 fp = ksmbd_lookup_fd_fast(work, id);
7098 if (!fp)
7099 return -ENOENT;
7100
7101 start = le64_to_cpu(qar_req->file_offset);
7102 length = le64_to_cpu(qar_req->length);
7103
7104 ret = ksmbd_vfs_fqar_lseek(fp, start, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09007105 qar_rsp, in_count, out_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09007106 if (ret && ret != -E2BIG)
7107 *out_count = 0;
7108
7109 ksmbd_fd_put(work, fp);
7110 return ret;
7111}
7112
Namjae Jeon64b39f42021-03-30 14:25:35 +09007113static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007114 int out_buf_len, struct smb2_ioctl_req *req,
7115 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007116{
7117 struct ksmbd_rpc_command *rpc_resp;
7118 char *data_buf = (char *)&req->Buffer[0];
7119 int nbytes = 0;
7120
Namjae Jeon64b39f42021-03-30 14:25:35 +09007121 rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09007122 le32_to_cpu(req->InputCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09007123 if (rpc_resp) {
7124 if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) {
7125 /*
7126 * set STATUS_SOME_NOT_MAPPED response
7127 * for unknown domain sid.
7128 */
7129 rsp->hdr.Status = STATUS_SOME_NOT_MAPPED;
7130 } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
7131 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7132 goto out;
7133 } else if (rpc_resp->flags != KSMBD_RPC_OK) {
7134 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7135 goto out;
7136 }
7137
7138 nbytes = rpc_resp->payload_sz;
7139 if (rpc_resp->payload_sz > out_buf_len) {
7140 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7141 nbytes = out_buf_len;
7142 }
7143
7144 if (!rpc_resp->payload_sz) {
7145 rsp->hdr.Status =
7146 STATUS_UNEXPECTED_IO_ERROR;
7147 goto out;
7148 }
7149
7150 memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes);
7151 }
7152out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09007153 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007154 return nbytes;
7155}
7156
Namjae Jeon64b39f42021-03-30 14:25:35 +09007157static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007158 struct file_sparse *sparse)
Namjae Jeone2f34482021-03-16 10:49:09 +09007159{
7160 struct ksmbd_file *fp;
7161 int ret = 0;
7162 __le32 old_fattr;
7163
7164 fp = ksmbd_lookup_fd_fast(work, id);
7165 if (!fp)
7166 return -ENOENT;
7167
7168 old_fattr = fp->f_ci->m_fattr;
7169 if (sparse->SetSparse)
7170 fp->f_ci->m_fattr |= ATTR_SPARSE_FILE_LE;
7171 else
7172 fp->f_ci->m_fattr &= ~ATTR_SPARSE_FILE_LE;
7173
7174 if (fp->f_ci->m_fattr != old_fattr &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007175 test_share_config_flag(work->tcon->share_conf,
7176 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007177 struct xattr_dos_attrib da;
7178
7179 ret = ksmbd_vfs_get_dos_attrib_xattr(fp->filp->f_path.dentry, &da);
7180 if (ret <= 0)
7181 goto out;
7182
7183 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
7184 ret = ksmbd_vfs_set_dos_attrib_xattr(fp->filp->f_path.dentry, &da);
7185 if (ret)
7186 fp->f_ci->m_fattr = old_fattr;
7187 }
7188
7189out:
7190 ksmbd_fd_put(work, fp);
7191 return ret;
7192}
7193
7194static int fsctl_request_resume_key(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007195 struct smb2_ioctl_req *req,
7196 struct resume_key_ioctl_rsp *key_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007197{
7198 struct ksmbd_file *fp;
7199
7200 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007201 le64_to_cpu(req->VolatileFileId),
7202 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09007203 if (!fp)
7204 return -ENOENT;
7205
7206 memset(key_rsp, 0, sizeof(*key_rsp));
7207 key_rsp->ResumeKey[0] = req->VolatileFileId;
7208 key_rsp->ResumeKey[1] = req->PersistentFileId;
7209 ksmbd_fd_put(work, fp);
7210
7211 return 0;
7212}
7213
7214/**
7215 * smb2_ioctl() - handler for smb2 ioctl command
7216 * @work: smb work containing ioctl command buffer
7217 *
7218 * Return: 0 on success, otherwise error
7219 */
7220int smb2_ioctl(struct ksmbd_work *work)
7221{
7222 struct smb2_ioctl_req *req;
7223 struct smb2_ioctl_rsp *rsp, *rsp_org;
7224 int cnt_code, nbytes = 0;
7225 int out_buf_len;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007226 u64 id = KSMBD_NO_FID;
Namjae Jeone2f34482021-03-16 10:49:09 +09007227 struct ksmbd_conn *conn = work->conn;
7228 int ret = 0;
7229
Namjae Jeone5066492021-03-30 12:35:23 +09007230 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007231 if (work->next_smb2_rcv_hdr_off) {
7232 req = REQUEST_BUF_NEXT(work);
7233 rsp = RESPONSE_BUF_NEXT(work);
7234 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
7235 ksmbd_debug(SMB, "Compound request set FID = %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007236 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09007237 id = work->compound_fid;
7238 }
7239 } else {
Namjae Jeone5066492021-03-30 12:35:23 +09007240 req = work->request_buf;
7241 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007242 }
7243
7244 if (!HAS_FILE_ID(id))
7245 id = le64_to_cpu(req->VolatileFileId);
7246
7247 if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) {
7248 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7249 goto out;
7250 }
7251
7252 cnt_code = le32_to_cpu(req->CntCode);
7253 out_buf_len = le32_to_cpu(req->MaxOutputResponse);
7254 out_buf_len = min(KSMBD_IPC_MAX_PAYLOAD, out_buf_len);
7255
7256 switch (cnt_code) {
7257 case FSCTL_DFS_GET_REFERRALS:
7258 case FSCTL_DFS_GET_REFERRALS_EX:
7259 /* Not support DFS yet */
7260 rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED;
7261 goto out;
7262 case FSCTL_CREATE_OR_GET_OBJECT_ID:
7263 {
7264 struct file_object_buf_type1_ioctl_rsp *obj_buf;
7265
7266 nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp);
7267 obj_buf = (struct file_object_buf_type1_ioctl_rsp *)
7268 &rsp->Buffer[0];
7269
7270 /*
7271 * TODO: This is dummy implementation to pass smbtorture
7272 * Need to check correct response later
7273 */
7274 memset(obj_buf->ObjectId, 0x0, 16);
7275 memset(obj_buf->BirthVolumeId, 0x0, 16);
7276 memset(obj_buf->BirthObjectId, 0x0, 16);
7277 memset(obj_buf->DomainId, 0x0, 16);
7278
7279 break;
7280 }
7281 case FSCTL_PIPE_TRANSCEIVE:
7282 nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp);
7283 break;
7284 case FSCTL_VALIDATE_NEGOTIATE_INFO:
7285 if (conn->dialect < SMB30_PROT_ID) {
7286 ret = -EOPNOTSUPP;
7287 goto out;
7288 }
7289
7290 ret = fsctl_validate_negotiate_info(conn,
7291 (struct validate_negotiate_info_req *)&req->Buffer[0],
7292 (struct validate_negotiate_info_rsp *)&rsp->Buffer[0]);
7293 if (ret < 0)
7294 goto out;
7295
7296 nbytes = sizeof(struct validate_negotiate_info_rsp);
7297 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7298 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7299 break;
7300 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
7301 nbytes = fsctl_query_iface_info_ioctl(conn, req, rsp);
7302 if (nbytes < 0)
7303 goto out;
7304 break;
7305 case FSCTL_REQUEST_RESUME_KEY:
7306 if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) {
7307 ret = -EINVAL;
7308 goto out;
7309 }
7310
7311 ret = fsctl_request_resume_key(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007312 (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007313 if (ret < 0)
7314 goto out;
7315 rsp->PersistentFileId = req->PersistentFileId;
7316 rsp->VolatileFileId = req->VolatileFileId;
7317 nbytes = sizeof(struct resume_key_ioctl_rsp);
7318 break;
7319 case FSCTL_COPYCHUNK:
7320 case FSCTL_COPYCHUNK_WRITE:
Namjae Jeon64b39f42021-03-30 14:25:35 +09007321 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007322 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007323 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007324 ret = -EACCES;
7325 goto out;
7326 }
7327
7328 if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) {
7329 ret = -EINVAL;
7330 goto out;
7331 }
7332
7333 nbytes = sizeof(struct copychunk_ioctl_rsp);
7334 fsctl_copychunk(work, req, rsp);
7335 break;
7336 case FSCTL_SET_SPARSE:
7337 ret = fsctl_set_sparse(work, id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007338 (struct file_sparse *)&req->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007339 if (ret < 0)
7340 goto out;
7341 break;
7342 case FSCTL_SET_ZERO_DATA:
7343 {
7344 struct file_zero_data_information *zero_data;
7345 struct ksmbd_file *fp;
7346 loff_t off, len;
7347
Namjae Jeon64b39f42021-03-30 14:25:35 +09007348 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007349 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007350 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007351 ret = -EACCES;
7352 goto out;
7353 }
7354
7355 zero_data =
7356 (struct file_zero_data_information *)&req->Buffer[0];
7357
7358 fp = ksmbd_lookup_fd_fast(work, id);
7359 if (!fp) {
7360 ret = -ENOENT;
7361 goto out;
7362 }
7363
7364 off = le64_to_cpu(zero_data->FileOffset);
7365 len = le64_to_cpu(zero_data->BeyondFinalZero) - off;
7366
7367 ret = ksmbd_vfs_zero_data(work, fp, off, len);
7368 ksmbd_fd_put(work, fp);
7369 if (ret < 0)
7370 goto out;
7371 break;
7372 }
7373 case FSCTL_QUERY_ALLOCATED_RANGES:
7374 ret = fsctl_query_allocated_ranges(work, id,
7375 (struct file_allocated_range_buffer *)&req->Buffer[0],
7376 (struct file_allocated_range_buffer *)&rsp->Buffer[0],
7377 out_buf_len /
7378 sizeof(struct file_allocated_range_buffer), &nbytes);
7379 if (ret == -E2BIG) {
7380 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7381 } else if (ret < 0) {
7382 nbytes = 0;
7383 goto out;
7384 }
7385
7386 nbytes *= sizeof(struct file_allocated_range_buffer);
7387 break;
7388 case FSCTL_GET_REPARSE_POINT:
7389 {
7390 struct reparse_data_buffer *reparse_ptr;
7391 struct ksmbd_file *fp;
7392
7393 reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0];
7394 fp = ksmbd_lookup_fd_fast(work, id);
7395 if (!fp) {
7396 ksmbd_err("not found fp!!\n");
7397 ret = -ENOENT;
7398 goto out;
7399 }
7400
7401 reparse_ptr->ReparseTag =
7402 smb2_get_reparse_tag_special_file(FP_INODE(fp)->i_mode);
7403 reparse_ptr->ReparseDataLength = 0;
7404 ksmbd_fd_put(work, fp);
7405 nbytes = sizeof(struct reparse_data_buffer);
7406 break;
7407 }
Namjae Jeoneb817362021-05-18 10:37:59 +09007408 case FSCTL_DUPLICATE_EXTENTS_TO_FILE:
7409 {
7410 struct ksmbd_file *fp_in, *fp_out = NULL;
7411 struct duplicate_extents_to_file *dup_ext;
7412 loff_t src_off, dst_off, length, cloned;
7413
7414 dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0];
7415
7416 fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle,
Namjae Jeon070fb212021-05-26 17:57:12 +09007417 dup_ext->PersistentFileHandle);
Namjae Jeoneb817362021-05-18 10:37:59 +09007418 if (!fp_in) {
7419 ksmbd_err("not found file handle in duplicate extent to file\n");
7420 ret = -ENOENT;
7421 goto out;
7422 }
7423
7424 fp_out = ksmbd_lookup_fd_fast(work, id);
7425 if (!fp_out) {
7426 ksmbd_err("not found fp\n");
7427 ret = -ENOENT;
7428 goto dup_ext_out;
7429 }
7430
7431 src_off = le64_to_cpu(dup_ext->SourceFileOffset);
7432 dst_off = le64_to_cpu(dup_ext->TargetFileOffset);
7433 length = le64_to_cpu(dup_ext->ByteCount);
7434 cloned = vfs_clone_file_range(fp_in->filp, src_off, fp_out->filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007435 dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007436 if (cloned == -EXDEV || cloned == -EOPNOTSUPP) {
7437 ret = -EOPNOTSUPP;
7438 goto dup_ext_out;
7439 } else if (cloned != length) {
Namjae Jeonf8524772021-06-18 10:28:00 +09007440 cloned = vfs_copy_file_range(fp_in->filp, src_off,
7441 fp_out->filp, dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007442 if (cloned != length) {
7443 if (cloned < 0)
7444 ret = cloned;
7445 else
7446 ret = -EINVAL;
7447 }
7448 }
7449
7450dup_ext_out:
7451 ksmbd_fd_put(work, fp_in);
7452 ksmbd_fd_put(work, fp_out);
7453 if (ret < 0)
7454 goto out;
7455 break;
7456 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007457 default:
7458 ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007459 cnt_code);
Namjae Jeone2f34482021-03-16 10:49:09 +09007460 ret = -EOPNOTSUPP;
7461 goto out;
7462 }
7463
7464 rsp->CntCode = cpu_to_le32(cnt_code);
7465 rsp->InputCount = cpu_to_le32(0);
7466 rsp->InputOffset = cpu_to_le32(112);
7467 rsp->OutputOffset = cpu_to_le32(112);
7468 rsp->OutputCount = cpu_to_le32(nbytes);
7469 rsp->StructureSize = cpu_to_le16(49);
7470 rsp->Reserved = cpu_to_le16(0);
7471 rsp->Flags = cpu_to_le32(0);
7472 rsp->Reserved2 = cpu_to_le32(0);
7473 inc_rfc1001_len(rsp_org, 48 + nbytes);
7474
7475 return 0;
7476
7477out:
7478 if (ret == -EACCES)
7479 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7480 else if (ret == -ENOENT)
7481 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7482 else if (ret == -EOPNOTSUPP)
7483 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7484 else if (ret < 0 || rsp->hdr.Status == 0)
7485 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7486 smb2_set_err_rsp(work);
7487 return 0;
7488}
7489
7490/**
7491 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command
7492 * @work: smb work containing oplock break command buffer
7493 *
7494 * Return: 0
7495 */
7496static void smb20_oplock_break_ack(struct ksmbd_work *work)
7497{
Namjae Jeone5066492021-03-30 12:35:23 +09007498 struct smb2_oplock_break *req = work->request_buf;
7499 struct smb2_oplock_break *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007500 struct ksmbd_file *fp;
7501 struct oplock_info *opinfo = NULL;
7502 __le32 err = 0;
7503 int ret = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007504 u64 volatile_id, persistent_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09007505 char req_oplevel = 0, rsp_oplevel = 0;
7506 unsigned int oplock_change_type;
7507
7508 volatile_id = le64_to_cpu(req->VolatileFid);
7509 persistent_id = le64_to_cpu(req->PersistentFid);
7510 req_oplevel = req->OplockLevel;
7511 ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n",
7512 volatile_id, persistent_id, req_oplevel);
7513
7514 fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
7515 if (!fp) {
7516 rsp->hdr.Status = STATUS_FILE_CLOSED;
7517 smb2_set_err_rsp(work);
7518 return;
7519 }
7520
7521 opinfo = opinfo_get(fp);
7522 if (!opinfo) {
7523 ksmbd_err("unexpected null oplock_info\n");
7524 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7525 smb2_set_err_rsp(work);
7526 ksmbd_fd_put(work, fp);
7527 return;
7528 }
7529
7530 if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) {
7531 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7532 goto err_out;
7533 }
7534
7535 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7536 ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state);
7537 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7538 goto err_out;
7539 }
7540
Namjae Jeon64b39f42021-03-30 14:25:35 +09007541 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7542 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7543 (req_oplevel != SMB2_OPLOCK_LEVEL_II &&
7544 req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007545 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7546 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007547 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7548 req_oplevel != SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007549 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7550 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007551 } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II ||
7552 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007553 err = STATUS_INVALID_DEVICE_STATE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007554 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7555 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7556 req_oplevel == SMB2_OPLOCK_LEVEL_II) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007557 oplock_change_type = OPLOCK_WRITE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007558 } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7559 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7560 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007561 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007562 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7563 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007564 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007565 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007566 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007567 }
7568 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007569 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007570 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007571
7572 switch (oplock_change_type) {
7573 case OPLOCK_WRITE_TO_READ:
7574 ret = opinfo_write_to_read(opinfo);
7575 rsp_oplevel = SMB2_OPLOCK_LEVEL_II;
7576 break;
7577 case OPLOCK_WRITE_TO_NONE:
7578 ret = opinfo_write_to_none(opinfo);
7579 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7580 break;
7581 case OPLOCK_READ_TO_NONE:
7582 ret = opinfo_read_to_none(opinfo);
7583 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7584 break;
7585 default:
7586 ksmbd_err("unknown oplock change 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007587 opinfo->level, rsp_oplevel);
Namjae Jeone2f34482021-03-16 10:49:09 +09007588 }
7589
7590 if (ret < 0) {
7591 rsp->hdr.Status = err;
7592 goto err_out;
7593 }
7594
7595 opinfo_put(opinfo);
7596 ksmbd_fd_put(work, fp);
7597 opinfo->op_state = OPLOCK_STATE_NONE;
7598 wake_up_interruptible_all(&opinfo->oplock_q);
7599
7600 rsp->StructureSize = cpu_to_le16(24);
7601 rsp->OplockLevel = rsp_oplevel;
7602 rsp->Reserved = 0;
7603 rsp->Reserved2 = 0;
7604 rsp->VolatileFid = cpu_to_le64(volatile_id);
7605 rsp->PersistentFid = cpu_to_le64(persistent_id);
7606 inc_rfc1001_len(rsp, 24);
7607 return;
7608
7609err_out:
7610 opinfo->op_state = OPLOCK_STATE_NONE;
7611 wake_up_interruptible_all(&opinfo->oplock_q);
7612
7613 opinfo_put(opinfo);
7614 ksmbd_fd_put(work, fp);
7615 smb2_set_err_rsp(work);
7616}
7617
7618static int check_lease_state(struct lease *lease, __le32 req_state)
7619{
7620 if ((lease->new_state ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09007621 (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) &&
7622 !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007623 lease->new_state = req_state;
7624 return 0;
7625 }
7626
7627 if (lease->new_state == req_state)
7628 return 0;
7629
7630 return 1;
7631}
7632
7633/**
7634 * smb21_lease_break_ack() - handler for smb2.1 lease break command
7635 * @work: smb work containing lease break command buffer
7636 *
7637 * Return: 0
7638 */
7639static void smb21_lease_break_ack(struct ksmbd_work *work)
7640{
7641 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09007642 struct smb2_lease_ack *req = work->request_buf;
7643 struct smb2_lease_ack *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007644 struct oplock_info *opinfo;
7645 __le32 err = 0;
7646 int ret = 0;
7647 unsigned int lease_change_type;
7648 __le32 lease_state;
7649 struct lease *lease;
7650
7651 ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007652 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007653 opinfo = lookup_lease_in_table(conn, req->LeaseKey);
7654 if (!opinfo) {
7655 ksmbd_debug(OPLOCK, "file not opened\n");
7656 smb2_set_err_rsp(work);
7657 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7658 return;
7659 }
7660 lease = opinfo->o_lease;
7661
7662 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7663 ksmbd_err("unexpected lease break state 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007664 opinfo->op_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09007665 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7666 goto err_out;
7667 }
7668
7669 if (check_lease_state(lease, req->LeaseState)) {
7670 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
7671 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +09007672 "req lease state: 0x%x, expected state: 0x%x\n",
7673 req->LeaseState, lease->new_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09007674 goto err_out;
7675 }
7676
7677 if (!atomic_read(&opinfo->breaking_cnt)) {
7678 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7679 goto err_out;
7680 }
7681
7682 /* check for bad lease state */
Namjae Jeon070fb212021-05-26 17:57:12 +09007683 if (req->LeaseState &
7684 (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007685 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7686 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7687 lease_change_type = OPLOCK_WRITE_TO_NONE;
7688 else
7689 lease_change_type = OPLOCK_READ_TO_NONE;
7690 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007691 le32_to_cpu(lease->state),
7692 le32_to_cpu(req->LeaseState));
Namjae Jeon64b39f42021-03-30 14:25:35 +09007693 } else if (lease->state == SMB2_LEASE_READ_CACHING_LE &&
7694 req->LeaseState != SMB2_LEASE_NONE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007695 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7696 lease_change_type = OPLOCK_READ_TO_NONE;
7697 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007698 le32_to_cpu(lease->state),
7699 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007700 } else {
7701 /* valid lease state changes */
7702 err = STATUS_INVALID_DEVICE_STATE;
7703 if (req->LeaseState == SMB2_LEASE_NONE_LE) {
7704 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7705 lease_change_type = OPLOCK_WRITE_TO_NONE;
7706 else
7707 lease_change_type = OPLOCK_READ_TO_NONE;
7708 } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) {
7709 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7710 lease_change_type = OPLOCK_WRITE_TO_READ;
7711 else
7712 lease_change_type = OPLOCK_READ_HANDLE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007713 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007714 lease_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007715 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007716 }
7717
7718 switch (lease_change_type) {
7719 case OPLOCK_WRITE_TO_READ:
7720 ret = opinfo_write_to_read(opinfo);
7721 break;
7722 case OPLOCK_READ_HANDLE_TO_READ:
7723 ret = opinfo_read_handle_to_read(opinfo);
7724 break;
7725 case OPLOCK_WRITE_TO_NONE:
7726 ret = opinfo_write_to_none(opinfo);
7727 break;
7728 case OPLOCK_READ_TO_NONE:
7729 ret = opinfo_read_to_none(opinfo);
7730 break;
7731 default:
7732 ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007733 le32_to_cpu(lease->state),
7734 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007735 }
7736
7737 lease_state = lease->state;
7738 opinfo->op_state = OPLOCK_STATE_NONE;
7739 wake_up_interruptible_all(&opinfo->oplock_q);
7740 atomic_dec(&opinfo->breaking_cnt);
7741 wake_up_interruptible_all(&opinfo->oplock_brk);
7742 opinfo_put(opinfo);
7743
7744 if (ret < 0) {
7745 rsp->hdr.Status = err;
7746 goto err_out;
7747 }
7748
7749 rsp->StructureSize = cpu_to_le16(36);
7750 rsp->Reserved = 0;
7751 rsp->Flags = 0;
7752 memcpy(rsp->LeaseKey, req->LeaseKey, 16);
7753 rsp->LeaseState = lease_state;
7754 rsp->LeaseDuration = 0;
7755 inc_rfc1001_len(rsp, 36);
7756 return;
7757
7758err_out:
7759 opinfo->op_state = OPLOCK_STATE_NONE;
7760 wake_up_interruptible_all(&opinfo->oplock_q);
7761 atomic_dec(&opinfo->breaking_cnt);
7762 wake_up_interruptible_all(&opinfo->oplock_brk);
7763
7764 opinfo_put(opinfo);
7765 smb2_set_err_rsp(work);
7766}
7767
7768/**
7769 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break
7770 * @work: smb work containing oplock/lease break command buffer
7771 *
7772 * Return: 0
7773 */
7774int smb2_oplock_break(struct ksmbd_work *work)
7775{
Namjae Jeone5066492021-03-30 12:35:23 +09007776 struct smb2_oplock_break *req = work->request_buf;
7777 struct smb2_oplock_break *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007778
7779 switch (le16_to_cpu(req->StructureSize)) {
7780 case OP_BREAK_STRUCT_SIZE_20:
7781 smb20_oplock_break_ack(work);
7782 break;
7783 case OP_BREAK_STRUCT_SIZE_21:
7784 smb21_lease_break_ack(work);
7785 break;
7786 default:
7787 ksmbd_debug(OPLOCK, "invalid break cmd %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007788 le16_to_cpu(req->StructureSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09007789 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7790 smb2_set_err_rsp(work);
7791 }
7792
7793 return 0;
7794}
7795
7796/**
7797 * smb2_notify() - handler for smb2 notify request
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09007798 * @work: smb work containing notify command buffer
Namjae Jeone2f34482021-03-16 10:49:09 +09007799 *
7800 * Return: 0
7801 */
7802int smb2_notify(struct ksmbd_work *work)
7803{
7804 struct smb2_notify_req *req;
7805 struct smb2_notify_rsp *rsp;
7806
7807 WORK_BUFFERS(work, req, rsp);
7808
7809 if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) {
7810 rsp->hdr.Status = STATUS_INTERNAL_ERROR;
7811 smb2_set_err_rsp(work);
7812 return 0;
7813 }
7814
7815 smb2_set_err_rsp(work);
7816 rsp->hdr.Status = STATUS_NOT_IMPLEMENTED;
7817 return 0;
7818}
7819
7820/**
7821 * smb2_is_sign_req() - handler for checking packet signing status
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09007822 * @work: smb work containing notify command buffer
7823 * @command: SMB2 command id
Namjae Jeone2f34482021-03-16 10:49:09 +09007824 *
7825 * Return: true if packed is signed, false otherwise
7826 */
7827bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command)
7828{
Namjae Jeone5066492021-03-30 12:35:23 +09007829 struct smb2_hdr *rcv_hdr2 = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007830
7831 if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007832 command != SMB2_NEGOTIATE_HE &&
7833 command != SMB2_SESSION_SETUP_HE &&
7834 command != SMB2_OPLOCK_BREAK_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +09007835 return true;
7836
kernel test robot56160152021-05-12 09:24:37 +09007837 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +09007838}
7839
7840/**
7841 * smb2_check_sign_req() - handler for req packet sign processing
7842 * @work: smb work containing notify command buffer
7843 *
7844 * Return: 1 on success, 0 otherwise
7845 */
7846int smb2_check_sign_req(struct ksmbd_work *work)
7847{
7848 struct smb2_hdr *hdr, *hdr_org;
7849 char signature_req[SMB2_SIGNATURE_SIZE];
7850 char signature[SMB2_HMACSHA256_SIZE];
7851 struct kvec iov[1];
7852 size_t len;
7853
Namjae Jeone5066492021-03-30 12:35:23 +09007854 hdr_org = hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007855 if (work->next_smb2_rcv_hdr_off)
7856 hdr = REQUEST_BUF_NEXT(work);
7857
7858 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
7859 len = be32_to_cpu(hdr_org->smb2_buf_length);
7860 else if (hdr->NextCommand)
7861 len = le32_to_cpu(hdr->NextCommand);
7862 else
7863 len = be32_to_cpu(hdr_org->smb2_buf_length) -
7864 work->next_smb2_rcv_hdr_off;
7865
7866 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
7867 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7868
7869 iov[0].iov_base = (char *)&hdr->ProtocolId;
7870 iov[0].iov_len = len;
7871
7872 if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1,
Namjae Jeon64b39f42021-03-30 14:25:35 +09007873 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09007874 return 0;
7875
7876 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
7877 ksmbd_err("bad smb2 signature\n");
7878 return 0;
7879 }
7880
7881 return 1;
7882}
7883
7884/**
7885 * smb2_set_sign_rsp() - handler for rsp packet sign processing
7886 * @work: smb work containing notify command buffer
7887 *
7888 */
7889void smb2_set_sign_rsp(struct ksmbd_work *work)
7890{
7891 struct smb2_hdr *hdr, *hdr_org;
7892 struct smb2_hdr *req_hdr;
7893 char signature[SMB2_HMACSHA256_SIZE];
7894 struct kvec iov[2];
7895 size_t len;
7896 int n_vec = 1;
7897
Namjae Jeone5066492021-03-30 12:35:23 +09007898 hdr_org = hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007899 if (work->next_smb2_rsp_hdr_off)
7900 hdr = RESPONSE_BUF_NEXT(work);
7901
7902 req_hdr = REQUEST_BUF_NEXT(work);
7903
7904 if (!work->next_smb2_rsp_hdr_off) {
7905 len = get_rfc1002_len(hdr_org);
7906 if (req_hdr->NextCommand)
7907 len = ALIGN(len, 8);
7908 } else {
7909 len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off;
7910 len = ALIGN(len, 8);
7911 }
7912
7913 if (req_hdr->NextCommand)
7914 hdr->NextCommand = cpu_to_le32(len);
7915
7916 hdr->Flags |= SMB2_FLAGS_SIGNED;
7917 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7918
7919 iov[0].iov_base = (char *)&hdr->ProtocolId;
7920 iov[0].iov_len = len;
7921
Namjae Jeone5066492021-03-30 12:35:23 +09007922 if (work->aux_payload_sz) {
7923 iov[0].iov_len -= work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09007924
Namjae Jeone5066492021-03-30 12:35:23 +09007925 iov[1].iov_base = work->aux_payload_buf;
7926 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09007927 n_vec++;
7928 }
7929
7930 if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec,
Namjae Jeon64b39f42021-03-30 14:25:35 +09007931 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09007932 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
7933}
7934
7935/**
7936 * smb3_check_sign_req() - handler for req packet sign processing
7937 * @work: smb work containing notify command buffer
7938 *
7939 * Return: 1 on success, 0 otherwise
7940 */
7941int smb3_check_sign_req(struct ksmbd_work *work)
7942{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09007943 struct ksmbd_conn *conn = work->conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09007944 char *signing_key;
7945 struct smb2_hdr *hdr, *hdr_org;
7946 struct channel *chann;
7947 char signature_req[SMB2_SIGNATURE_SIZE];
7948 char signature[SMB2_CMACAES_SIZE];
7949 struct kvec iov[1];
7950 size_t len;
7951
Namjae Jeone5066492021-03-30 12:35:23 +09007952 hdr_org = hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007953 if (work->next_smb2_rcv_hdr_off)
7954 hdr = REQUEST_BUF_NEXT(work);
7955
7956 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
7957 len = be32_to_cpu(hdr_org->smb2_buf_length);
7958 else if (hdr->NextCommand)
7959 len = le32_to_cpu(hdr->NextCommand);
7960 else
7961 len = be32_to_cpu(hdr_org->smb2_buf_length) -
7962 work->next_smb2_rcv_hdr_off;
7963
7964 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
7965 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09007966 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09007967 chann = lookup_chann_list(work->sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09007968 if (!chann)
7969 return 0;
7970 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09007971 }
7972
7973 if (!signing_key) {
7974 ksmbd_err("SMB3 signing key is not generated\n");
7975 return 0;
7976 }
7977
7978 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
7979 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7980 iov[0].iov_base = (char *)&hdr->ProtocolId;
7981 iov[0].iov_len = len;
7982
7983 if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature))
7984 return 0;
7985
7986 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
7987 ksmbd_err("bad smb2 signature\n");
7988 return 0;
7989 }
7990
7991 return 1;
7992}
7993
7994/**
7995 * smb3_set_sign_rsp() - handler for rsp packet sign processing
7996 * @work: smb work containing notify command buffer
7997 *
7998 */
7999void smb3_set_sign_rsp(struct ksmbd_work *work)
8000{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008001 struct ksmbd_conn *conn = work->conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09008002 struct smb2_hdr *req_hdr;
8003 struct smb2_hdr *hdr, *hdr_org;
8004 struct channel *chann;
8005 char signature[SMB2_CMACAES_SIZE];
8006 struct kvec iov[2];
8007 int n_vec = 1;
8008 size_t len;
8009 char *signing_key;
8010
Namjae Jeone5066492021-03-30 12:35:23 +09008011 hdr_org = hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008012 if (work->next_smb2_rsp_hdr_off)
8013 hdr = RESPONSE_BUF_NEXT(work);
8014
8015 req_hdr = REQUEST_BUF_NEXT(work);
8016
8017 if (!work->next_smb2_rsp_hdr_off) {
8018 len = get_rfc1002_len(hdr_org);
8019 if (req_hdr->NextCommand)
8020 len = ALIGN(len, 8);
8021 } else {
8022 len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off;
8023 len = ALIGN(len, 8);
8024 }
8025
8026 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
8027 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008028 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008029 chann = lookup_chann_list(work->sess, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008030 if (!chann)
8031 return;
8032 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008033 }
8034
8035 if (!signing_key)
8036 return;
8037
8038 if (req_hdr->NextCommand)
8039 hdr->NextCommand = cpu_to_le32(len);
8040
8041 hdr->Flags |= SMB2_FLAGS_SIGNED;
8042 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8043 iov[0].iov_base = (char *)&hdr->ProtocolId;
8044 iov[0].iov_len = len;
Namjae Jeone5066492021-03-30 12:35:23 +09008045 if (work->aux_payload_sz) {
8046 iov[0].iov_len -= work->aux_payload_sz;
8047 iov[1].iov_base = work->aux_payload_buf;
8048 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008049 n_vec++;
8050 }
8051
8052 if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature))
8053 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8054}
8055
8056/**
8057 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response
8058 * @work: smb work containing response buffer
8059 *
8060 */
8061void smb3_preauth_hash_rsp(struct ksmbd_work *work)
8062{
8063 struct ksmbd_conn *conn = work->conn;
8064 struct ksmbd_session *sess = work->sess;
8065 struct smb2_hdr *req, *rsp;
8066
8067 if (conn->dialect != SMB311_PROT_ID)
8068 return;
8069
8070 WORK_BUFFERS(work, req, rsp);
8071
8072 if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE)
8073 ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09008074 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09008075
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008076 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008077 __u8 *hash_value;
8078
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008079 if (conn->binding) {
8080 struct preauth_session *preauth_sess;
8081
8082 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
8083 if (!preauth_sess)
8084 return;
8085 hash_value = preauth_sess->Preauth_HashValue;
8086 } else {
8087 hash_value = sess->Preauth_HashValue;
8088 if (!hash_value)
8089 return;
8090 }
Namjae Jeone2f34482021-03-16 10:49:09 +09008091 ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09008092 hash_value);
Namjae Jeone2f34482021-03-16 10:49:09 +09008093 }
8094}
8095
Namjae Jeon64b39f42021-03-30 14:25:35 +09008096static void fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, char *old_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008097 __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09008098{
8099 struct smb2_hdr *hdr = (struct smb2_hdr *)old_buf;
8100 unsigned int orig_len = get_rfc1002_len(old_buf);
8101
8102 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
8103 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
8104 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
8105 tr_hdr->Flags = cpu_to_le16(0x01);
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008106 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
8107 cipher_type == SMB2_ENCRYPTION_AES256_GCM)
8108 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008109 else
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008110 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008111 memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8);
8112 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
8113 inc_rfc1001_len(tr_hdr, orig_len);
8114}
8115
8116int smb3_encrypt_resp(struct ksmbd_work *work)
8117{
Namjae Jeone5066492021-03-30 12:35:23 +09008118 char *buf = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008119 struct smb2_transform_hdr *tr_hdr;
8120 struct kvec iov[3];
8121 int rc = -ENOMEM;
Namjae Jeone5066492021-03-30 12:35:23 +09008122 int buf_size = 0, rq_nvec = 2 + (work->aux_payload_sz ? 1 : 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09008123
8124 if (ARRAY_SIZE(iov) < rq_nvec)
8125 return -ENOMEM;
8126
Namjae Jeon20ea7fd2021-03-30 12:40:47 +09008127 tr_hdr = kzalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09008128 if (!tr_hdr)
8129 return rc;
8130
8131 /* fill transform header */
8132 fill_transform_hdr(tr_hdr, buf, work->conn->cipher_type);
8133
8134 iov[0].iov_base = tr_hdr;
8135 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8136 buf_size += iov[0].iov_len - 4;
8137
8138 iov[1].iov_base = buf + 4;
8139 iov[1].iov_len = get_rfc1002_len(buf);
Namjae Jeone5066492021-03-30 12:35:23 +09008140 if (work->aux_payload_sz) {
8141 iov[1].iov_len = work->resp_hdr_sz - 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008142
Namjae Jeone5066492021-03-30 12:35:23 +09008143 iov[2].iov_base = work->aux_payload_buf;
8144 iov[2].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008145 buf_size += iov[2].iov_len;
8146 }
8147 buf_size += iov[1].iov_len;
8148 work->resp_hdr_sz = iov[1].iov_len;
8149
8150 rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1);
8151 if (rc)
8152 return rc;
8153
8154 memmove(buf, iov[1].iov_base, iov[1].iov_len);
8155 tr_hdr->smb2_buf_length = cpu_to_be32(buf_size);
8156 work->tr_buf = tr_hdr;
8157
8158 return rc;
8159}
8160
8161int smb3_is_transform_hdr(void *buf)
8162{
8163 struct smb2_transform_hdr *trhdr = buf;
8164
8165 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
8166}
8167
8168int smb3_decrypt_req(struct ksmbd_work *work)
8169{
8170 struct ksmbd_conn *conn = work->conn;
8171 struct ksmbd_session *sess;
Namjae Jeone5066492021-03-30 12:35:23 +09008172 char *buf = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008173 struct smb2_hdr *hdr;
8174 unsigned int pdu_length = get_rfc1002_len(buf);
8175 struct kvec iov[2];
8176 unsigned int buf_data_size = pdu_length + 4 -
8177 sizeof(struct smb2_transform_hdr);
8178 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
8179 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
8180 int rc = 0;
8181
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008182 sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09008183 if (!sess) {
8184 ksmbd_err("invalid session id(%llx) in transform header\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008185 le64_to_cpu(tr_hdr->SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09008186 return -ECONNABORTED;
8187 }
8188
Namjae Jeon070fb212021-05-26 17:57:12 +09008189 if (pdu_length + 4 <
8190 sizeof(struct smb2_transform_hdr) + sizeof(struct smb2_hdr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008191 ksmbd_err("Transform message is too small (%u)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008192 pdu_length);
Namjae Jeone2f34482021-03-16 10:49:09 +09008193 return -ECONNABORTED;
8194 }
8195
8196 if (pdu_length + 4 < orig_len + sizeof(struct smb2_transform_hdr)) {
8197 ksmbd_err("Transform message is broken\n");
8198 return -ECONNABORTED;
8199 }
8200
8201 iov[0].iov_base = buf;
8202 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8203 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
8204 iov[1].iov_len = buf_data_size;
8205 rc = ksmbd_crypt_message(conn, iov, 2, 0);
8206 if (rc)
8207 return rc;
8208
8209 memmove(buf + 4, iov[1].iov_base, buf_data_size);
8210 hdr = (struct smb2_hdr *)buf;
8211 hdr->smb2_buf_length = cpu_to_be32(buf_data_size);
8212
8213 return rc;
8214}
8215
8216bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work)
8217{
8218 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09008219 struct smb2_hdr *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008220
8221 if (conn->dialect < SMB30_PROT_ID)
8222 return false;
8223
8224 if (work->next_smb2_rcv_hdr_off)
8225 rsp = RESPONSE_BUF_NEXT(work);
8226
8227 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008228 rsp->Status == STATUS_SUCCESS)
Namjae Jeone2f34482021-03-16 10:49:09 +09008229 return true;
8230 return false;
8231}