blob: f73721c3b0e9b7b3d9faddf8cd4f01cd520e4704 [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>
Namjae Jeone8c06192021-06-22 11:06:11 +090013#include <linux/falloc.h>
Namjae Jeone2f34482021-03-16 10:49:09 +090014
15#include "glob.h"
16#include "smb2pdu.h"
17#include "smbfsctl.h"
18#include "oplock.h"
19#include "smbacl.h"
20
21#include "auth.h"
22#include "asn1.h"
Namjae Jeone2f34482021-03-16 10:49:09 +090023#include "connection.h"
24#include "transport_ipc.h"
25#include "vfs.h"
26#include "vfs_cache.h"
27#include "misc.h"
28
Namjae Jeone2f34482021-03-16 10:49:09 +090029#include "server.h"
30#include "smb_common.h"
31#include "smbstatus.h"
32#include "ksmbd_work.h"
33#include "mgmt/user_config.h"
34#include "mgmt/share_config.h"
35#include "mgmt/tree_connect.h"
36#include "mgmt/user_session.h"
37#include "mgmt/ksmbd_ida.h"
38#include "ndr.h"
39
40static void __wbuf(struct ksmbd_work *work, void **req, void **rsp)
41{
42 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +090043 *req = ksmbd_req_buf_next(work);
44 *rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +090045 } else {
Namjae Jeone5066492021-03-30 12:35:23 +090046 *req = work->request_buf;
47 *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +090048 }
49}
50
51#define WORK_BUFFERS(w, rq, rs) __wbuf((w), (void **)&(rq), (void **)&(rs))
52
53/**
54 * check_session_id() - check for valid session id in smb header
55 * @conn: connection instance
56 * @id: session id from smb header
57 *
58 * Return: 1 if valid session id, otherwise 0
59 */
Namjae Jeon64b39f42021-03-30 14:25:35 +090060static inline int check_session_id(struct ksmbd_conn *conn, u64 id)
Namjae Jeone2f34482021-03-16 10:49:09 +090061{
62 struct ksmbd_session *sess;
63
64 if (id == 0 || id == -1)
65 return 0;
66
Namjae Jeonf5a544e2021-06-18 10:04:19 +090067 sess = ksmbd_session_lookup_all(conn, id);
Namjae Jeone2f34482021-03-16 10:49:09 +090068 if (sess)
69 return 1;
Namjae Jeonbde16942021-06-28 15:23:19 +090070 pr_err("Invalid user session id: %llu\n", id);
Namjae Jeone2f34482021-03-16 10:49:09 +090071 return 0;
72}
73
Namjae Jeonf5a544e2021-06-18 10:04:19 +090074struct channel *lookup_chann_list(struct ksmbd_session *sess, struct ksmbd_conn *conn)
Namjae Jeone2f34482021-03-16 10:49:09 +090075{
76 struct channel *chann;
Namjae Jeone2f34482021-03-16 10:49:09 +090077
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +090078 list_for_each_entry(chann, &sess->ksmbd_chann_list, chann_list) {
Namjae Jeon560ac052021-06-22 16:16:45 +090079 if (chann->conn == conn)
Namjae Jeone2f34482021-03-16 10:49:09 +090080 return chann;
81 }
82
83 return NULL;
84}
85
86/**
87 * smb2_get_ksmbd_tcon() - get tree connection information for a tree id
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +090088 * @work: smb work
Namjae Jeone2f34482021-03-16 10:49:09 +090089 *
90 * Return: matching tree connection on success, otherwise error
91 */
92int smb2_get_ksmbd_tcon(struct ksmbd_work *work)
93{
Namjae Jeone5066492021-03-30 12:35:23 +090094 struct smb2_hdr *req_hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +090095 int tree_id;
96
97 work->tcon = NULL;
Namjae Jeon64b39f42021-03-30 14:25:35 +090098 if (work->conn->ops->get_cmd_val(work) == SMB2_TREE_CONNECT_HE ||
99 work->conn->ops->get_cmd_val(work) == SMB2_CANCEL_HE ||
100 work->conn->ops->get_cmd_val(work) == SMB2_LOGOFF_HE) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900101 ksmbd_debug(SMB, "skip to check tree connect request\n");
102 return 0;
103 }
104
Namjae Jeon02b68b22021-04-01 17:45:33 +0900105 if (xa_empty(&work->sess->tree_conns)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900106 ksmbd_debug(SMB, "NO tree connected\n");
107 return -1;
108 }
109
110 tree_id = le32_to_cpu(req_hdr->Id.SyncId.TreeId);
111 work->tcon = ksmbd_tree_conn_lookup(work->sess, tree_id);
112 if (!work->tcon) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900113 pr_err("Invalid tid %d\n", tree_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900114 return -1;
115 }
116
117 return 1;
118}
119
120/**
121 * smb2_set_err_rsp() - set error response code on smb response
122 * @work: smb work containing response buffer
123 */
124void smb2_set_err_rsp(struct ksmbd_work *work)
125{
126 struct smb2_err_rsp *err_rsp;
127
128 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +0900129 err_rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900130 else
Namjae Jeone5066492021-03-30 12:35:23 +0900131 err_rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900132
133 if (err_rsp->hdr.Status != STATUS_STOPPED_ON_SYMLINK) {
134 err_rsp->StructureSize = SMB2_ERROR_STRUCTURE_SIZE2_LE;
135 err_rsp->ErrorContextCount = 0;
136 err_rsp->Reserved = 0;
137 err_rsp->ByteCount = 0;
138 err_rsp->ErrorData[0] = 0;
Namjae Jeone5066492021-03-30 12:35:23 +0900139 inc_rfc1001_len(work->response_buf, SMB2_ERROR_STRUCTURE_SIZE2);
Namjae Jeone2f34482021-03-16 10:49:09 +0900140 }
141}
142
143/**
144 * is_smb2_neg_cmd() - is it smb2 negotiation command
145 * @work: smb work containing smb header
146 *
147 * Return: 1 if smb2 negotiation command, otherwise 0
148 */
149int is_smb2_neg_cmd(struct ksmbd_work *work)
150{
Namjae Jeone5066492021-03-30 12:35:23 +0900151 struct smb2_hdr *hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900152
153 /* is it SMB2 header ? */
154 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
155 return 0;
156
157 /* make sure it is request not response message */
158 if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
159 return 0;
160
161 if (hdr->Command != SMB2_NEGOTIATE)
162 return 0;
163
164 return 1;
165}
166
167/**
168 * is_smb2_rsp() - is it smb2 response
169 * @work: smb work containing smb response buffer
170 *
171 * Return: 1 if smb2 response, otherwise 0
172 */
173int is_smb2_rsp(struct ksmbd_work *work)
174{
Namjae Jeone5066492021-03-30 12:35:23 +0900175 struct smb2_hdr *hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900176
177 /* is it SMB2 header ? */
178 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
179 return 0;
180
181 /* make sure it is response not request message */
182 if (!(hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR))
183 return 0;
184
185 return 1;
186}
187
188/**
189 * get_smb2_cmd_val() - get smb command code from smb header
190 * @work: smb work containing smb request buffer
191 *
192 * Return: smb2 request command value
193 */
Namjae Jeonfc2d1b52021-05-26 18:01:08 +0900194u16 get_smb2_cmd_val(struct ksmbd_work *work)
Namjae Jeone2f34482021-03-16 10:49:09 +0900195{
196 struct smb2_hdr *rcv_hdr;
197
198 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +0900199 rcv_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900200 else
Namjae Jeone5066492021-03-30 12:35:23 +0900201 rcv_hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900202 return le16_to_cpu(rcv_hdr->Command);
203}
204
205/**
206 * set_smb2_rsp_status() - set error response code on smb2 header
207 * @work: smb work containing response buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900208 * @err: error response code
Namjae Jeone2f34482021-03-16 10:49:09 +0900209 */
210void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err)
211{
212 struct smb2_hdr *rsp_hdr;
213
214 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +0900215 rsp_hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900216 else
Namjae Jeone5066492021-03-30 12:35:23 +0900217 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900218 rsp_hdr->Status = err;
219 smb2_set_err_rsp(work);
220}
221
222/**
223 * init_smb2_neg_rsp() - initialize smb2 response for negotiate command
224 * @work: smb work containing smb request buffer
225 *
226 * smb2 negotiate response is sent in reply of smb1 negotiate command for
227 * dialect auto-negotiation.
228 */
229int init_smb2_neg_rsp(struct ksmbd_work *work)
230{
231 struct smb2_hdr *rsp_hdr;
232 struct smb2_negotiate_rsp *rsp;
233 struct ksmbd_conn *conn = work->conn;
234
235 if (conn->need_neg == false)
236 return -EINVAL;
237 if (!(conn->dialect >= SMB20_PROT_ID &&
Namjae Jeon64b39f42021-03-30 14:25:35 +0900238 conn->dialect <= SMB311_PROT_ID))
Namjae Jeone2f34482021-03-16 10:49:09 +0900239 return -EINVAL;
240
Namjae Jeone5066492021-03-30 12:35:23 +0900241 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900242
243 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
244
245 rsp_hdr->smb2_buf_length =
Hyunchul Leed8fb2992021-06-25 11:53:26 +0900246 cpu_to_be32(smb2_hdr_size_no_buflen(conn->vals));
Namjae Jeone2f34482021-03-16 10:49:09 +0900247
248 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
249 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
250 rsp_hdr->CreditRequest = cpu_to_le16(2);
251 rsp_hdr->Command = SMB2_NEGOTIATE;
252 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
253 rsp_hdr->NextCommand = 0;
254 rsp_hdr->MessageId = 0;
255 rsp_hdr->Id.SyncId.ProcessId = 0;
256 rsp_hdr->Id.SyncId.TreeId = 0;
257 rsp_hdr->SessionId = 0;
258 memset(rsp_hdr->Signature, 0, 16);
259
Namjae Jeone5066492021-03-30 12:35:23 +0900260 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900261
262 WARN_ON(ksmbd_conn_good(work));
263
264 rsp->StructureSize = cpu_to_le16(65);
265 ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect);
266 rsp->DialectRevision = cpu_to_le16(conn->dialect);
267 /* Not setting conn guid rsp->ServerGUID, as it
268 * not used by client for identifying connection
269 */
270 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
271 /* Default Max Message Size till SMB2.0, 64K*/
272 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
273 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
274 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
275
276 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
277 rsp->ServerStartTime = 0;
278
279 rsp->SecurityBufferOffset = cpu_to_le16(128);
280 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
281 ksmbd_copy_gss_neg_header(((char *)(&rsp->hdr) +
282 sizeof(rsp->hdr.smb2_buf_length)) +
283 le16_to_cpu(rsp->SecurityBufferOffset));
284 inc_rfc1001_len(rsp, sizeof(struct smb2_negotiate_rsp) -
285 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
286 AUTH_GSS_LENGTH);
287 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
288 if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY)
289 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
290 conn->use_spnego = true;
291
292 ksmbd_conn_set_need_negotiate(work);
293 return 0;
294}
295
296static int smb2_consume_credit_charge(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +0900297 unsigned short credit_charge)
Namjae Jeone2f34482021-03-16 10:49:09 +0900298{
299 struct ksmbd_conn *conn = work->conn;
300 unsigned int rsp_credits = 1;
301
302 if (!conn->total_credits)
303 return 0;
304
305 if (credit_charge > 0)
306 rsp_credits = credit_charge;
307
308 conn->total_credits -= rsp_credits;
309 return rsp_credits;
310}
311
312/**
313 * smb2_set_rsp_credits() - set number of credits in response buffer
314 * @work: smb work containing smb response buffer
315 */
316int smb2_set_rsp_credits(struct ksmbd_work *work)
317{
Namjae Jeon8a893312021-06-25 13:43:37 +0900318 struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work);
319 struct smb2_hdr *hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900320 struct ksmbd_conn *conn = work->conn;
321 unsigned short credits_requested = le16_to_cpu(req_hdr->CreditRequest);
322 unsigned short credit_charge = 1, credits_granted = 0;
323 unsigned short aux_max, aux_credits, min_credits;
324 int rsp_credit_charge;
325
326 if (hdr->Command == SMB2_CANCEL)
327 goto out;
328
329 /* get default minimum credits by shifting maximum credits by 4 */
330 min_credits = conn->max_credits >> 4;
331
332 if (conn->total_credits >= conn->max_credits) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900333 pr_err("Total credits overflow: %d\n", conn->total_credits);
Namjae Jeone2f34482021-03-16 10:49:09 +0900334 conn->total_credits = min_credits;
335 }
336
Namjae Jeon070fb212021-05-26 17:57:12 +0900337 rsp_credit_charge =
338 smb2_consume_credit_charge(work, le16_to_cpu(req_hdr->CreditCharge));
Namjae Jeone2f34482021-03-16 10:49:09 +0900339 if (rsp_credit_charge < 0)
340 return -EINVAL;
341
342 hdr->CreditCharge = cpu_to_le16(rsp_credit_charge);
343
344 if (credits_requested > 0) {
345 aux_credits = credits_requested - 1;
346 aux_max = 32;
347 if (hdr->Command == SMB2_NEGOTIATE)
348 aux_max = 0;
349 aux_credits = (aux_credits < aux_max) ? aux_credits : aux_max;
350 credits_granted = aux_credits + credit_charge;
351
352 /* if credits granted per client is getting bigger than default
353 * minimum credits then we should wrap it up within the limits.
354 */
355 if ((conn->total_credits + credits_granted) > min_credits)
356 credits_granted = min_credits - conn->total_credits;
357 /*
358 * TODO: Need to adjuct CreditRequest value according to
359 * current cpu load
360 */
361 } else if (conn->total_credits == 0) {
362 credits_granted = 1;
363 }
364
365 conn->total_credits += credits_granted;
366 work->credits_granted += credits_granted;
367
368 if (!req_hdr->NextCommand) {
369 /* Update CreditRequest in last request */
370 hdr->CreditRequest = cpu_to_le16(work->credits_granted);
371 }
372out:
373 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900374 "credits: requested[%d] granted[%d] total_granted[%d]\n",
375 credits_requested, credits_granted,
376 conn->total_credits);
Namjae Jeone2f34482021-03-16 10:49:09 +0900377 return 0;
378}
379
380/**
381 * init_chained_smb2_rsp() - initialize smb2 chained response
382 * @work: smb work containing smb response buffer
383 */
384static void init_chained_smb2_rsp(struct ksmbd_work *work)
385{
Namjae Jeon8a893312021-06-25 13:43:37 +0900386 struct smb2_hdr *req = ksmbd_req_buf_next(work);
387 struct smb2_hdr *rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900388 struct smb2_hdr *rsp_hdr;
389 struct smb2_hdr *rcv_hdr;
390 int next_hdr_offset = 0;
391 int len, new_len;
392
393 /* Len of this response = updated RFC len - offset of previous cmd
394 * in the compound rsp
395 */
396
397 /* Storing the current local FID which may be needed by subsequent
398 * command in the compound request
399 */
400 if (req->Command == SMB2_CREATE && rsp->Status == STATUS_SUCCESS) {
401 work->compound_fid =
402 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
403 VolatileFileId);
404 work->compound_pfid =
405 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
406 PersistentFileId);
407 work->compound_sid = le64_to_cpu(rsp->SessionId);
408 }
409
Namjae Jeone5066492021-03-30 12:35:23 +0900410 len = get_rfc1002_len(work->response_buf) - work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +0900411 next_hdr_offset = le32_to_cpu(req->NextCommand);
412
413 new_len = ALIGN(len, 8);
Namjae Jeone5066492021-03-30 12:35:23 +0900414 inc_rfc1001_len(work->response_buf, ((sizeof(struct smb2_hdr) - 4)
Namjae Jeone2f34482021-03-16 10:49:09 +0900415 + new_len - len));
416 rsp->NextCommand = cpu_to_le32(new_len);
417
418 work->next_smb2_rcv_hdr_off += next_hdr_offset;
419 work->next_smb2_rsp_hdr_off += new_len;
420 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900421 "Compound req new_len = %d rcv off = %d rsp off = %d\n",
422 new_len, work->next_smb2_rcv_hdr_off,
423 work->next_smb2_rsp_hdr_off);
Namjae Jeone2f34482021-03-16 10:49:09 +0900424
Namjae Jeon8a893312021-06-25 13:43:37 +0900425 rsp_hdr = ksmbd_resp_buf_next(work);
426 rcv_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900427
428 if (!(rcv_hdr->Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
429 ksmbd_debug(SMB, "related flag should be set\n");
430 work->compound_fid = KSMBD_NO_FID;
431 work->compound_pfid = KSMBD_NO_FID;
432 }
433 memset((char *)rsp_hdr + 4, 0, sizeof(struct smb2_hdr) + 2);
434 rsp_hdr->ProtocolId = rcv_hdr->ProtocolId;
435 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
436 rsp_hdr->Command = rcv_hdr->Command;
437
438 /*
439 * Message is response. We don't grant oplock yet.
440 */
441 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR |
442 SMB2_FLAGS_RELATED_OPERATIONS);
443 rsp_hdr->NextCommand = 0;
444 rsp_hdr->MessageId = rcv_hdr->MessageId;
445 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
446 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
447 rsp_hdr->SessionId = rcv_hdr->SessionId;
448 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
449}
450
451/**
452 * is_chained_smb2_message() - check for chained command
453 * @work: smb work containing smb request buffer
454 *
455 * Return: true if chained request, otherwise false
456 */
457bool is_chained_smb2_message(struct ksmbd_work *work)
458{
Namjae Jeone5066492021-03-30 12:35:23 +0900459 struct smb2_hdr *hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900460 unsigned int len;
461
462 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
463 return false;
464
Namjae Jeon8a893312021-06-25 13:43:37 +0900465 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900466 if (le32_to_cpu(hdr->NextCommand) > 0) {
467 ksmbd_debug(SMB, "got SMB2 chained command\n");
468 init_chained_smb2_rsp(work);
469 return true;
470 } else if (work->next_smb2_rcv_hdr_off) {
471 /*
472 * This is last request in chained command,
473 * align response to 8 byte
474 */
Namjae Jeone5066492021-03-30 12:35:23 +0900475 len = ALIGN(get_rfc1002_len(work->response_buf), 8);
476 len = len - get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900477 if (len) {
478 ksmbd_debug(SMB, "padding len %u\n", len);
Namjae Jeone5066492021-03-30 12:35:23 +0900479 inc_rfc1001_len(work->response_buf, len);
480 if (work->aux_payload_sz)
Namjae Jeone2f34482021-03-16 10:49:09 +0900481 work->aux_payload_sz += len;
482 }
483 }
484 return false;
485}
486
487/**
488 * init_smb2_rsp_hdr() - initialize smb2 response
489 * @work: smb work containing smb request buffer
490 *
491 * Return: 0
492 */
493int init_smb2_rsp_hdr(struct ksmbd_work *work)
494{
Namjae Jeone5066492021-03-30 12:35:23 +0900495 struct smb2_hdr *rsp_hdr = work->response_buf;
496 struct smb2_hdr *rcv_hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900497 struct ksmbd_conn *conn = work->conn;
498
499 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
Hyunchul Leed8fb2992021-06-25 11:53:26 +0900500 rsp_hdr->smb2_buf_length =
501 cpu_to_be32(smb2_hdr_size_no_buflen(conn->vals));
Namjae Jeone2f34482021-03-16 10:49:09 +0900502 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)) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900639 pr_err("failed to get name %ld\n", PTR_ERR(name));
Namjae Jeone2f34482021-03-16 10:49:09 +0900640 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) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900650 pr_err("can not convert absolute name\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900651 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) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900669 pr_err("Failed to alloc async message id\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900670 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)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001010 pr_err("conn->tcp_status is already in CifsGood State\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001011 work->send_no_response = 1;
1012 return rc;
1013 }
1014
1015 if (req->DialectCount == 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001016 pr_err("malformed packet\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001017 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) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001036 pr_err("deassemble_neg_contexts error(0x%x)\n",
1037 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)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001298 pr_err("cannot allocate memory\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001299 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)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001443 pr_err("fail to verify the dialect\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001444 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)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001533 pr_err("fail to verify the dialect\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001534 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)) {
Colin Ian King4951a842021-07-06 13:05:01 +01001618 sess = NULL;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001619 rc = -EACCES;
1620 goto out_err;
Namjae Jeone2f34482021-03-16 10:49:09 +09001621 } else {
1622 sess = ksmbd_session_lookup(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001623 le64_to_cpu(req->hdr.SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09001624 if (!sess) {
1625 rc = -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001626 goto out_err;
1627 }
1628 }
1629 work->sess = sess;
1630
1631 if (sess->state == SMB2_SESSION_EXPIRED)
1632 sess->state = SMB2_SESSION_IN_PROGRESS;
1633
1634 negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId +
1635 le16_to_cpu(req->SecurityBufferOffset));
1636
1637 if (decode_negotiation_token(work, negblob) == 0) {
1638 if (conn->mechToken)
1639 negblob = (struct negotiate_message *)conn->mechToken;
1640 }
1641
1642 if (server_conf.auth_mechs & conn->auth_mechs) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001643 rc = generate_preauth_hash(work);
1644 if (rc)
1645 goto out_err;
1646
Namjae Jeone2f34482021-03-16 10:49:09 +09001647 if (conn->preferred_auth_mech &
1648 (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001649 rc = krb5_authenticate(work);
1650 if (rc) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001651 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001652 goto out_err;
1653 }
1654
1655 ksmbd_conn_set_good(work);
1656 sess->state = SMB2_SESSION_VALID;
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001657 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001658 sess->Preauth_HashValue = NULL;
1659 } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001660 if (negblob->MessageType == NtLmNegotiate) {
1661 rc = ntlm_negotiate(work, negblob);
1662 if (rc)
1663 goto out_err;
1664 rsp->hdr.Status =
1665 STATUS_MORE_PROCESSING_REQUIRED;
1666 /*
1667 * Note: here total size -1 is done as an
1668 * adjustment for 0 size blob
1669 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09001670 inc_rfc1001_len(rsp, le16_to_cpu(rsp->SecurityBufferLength) - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001671
1672 } else if (negblob->MessageType == NtLmAuthenticate) {
1673 rc = ntlm_authenticate(work);
1674 if (rc)
1675 goto out_err;
1676
1677 ksmbd_conn_set_good(work);
1678 sess->state = SMB2_SESSION_VALID;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001679 if (conn->binding) {
1680 struct preauth_session *preauth_sess;
1681
1682 preauth_sess =
1683 ksmbd_preauth_session_lookup(conn, sess->id);
1684 if (preauth_sess) {
1685 list_del(&preauth_sess->preauth_entry);
1686 kfree(preauth_sess);
1687 }
1688 }
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001689 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001690 sess->Preauth_HashValue = NULL;
1691 }
1692 } else {
1693 /* TODO: need one more negotiation */
Namjae Jeonbde16942021-06-28 15:23:19 +09001694 pr_err("Not support the preferred authentication\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001695 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001696 }
1697 } else {
Namjae Jeonbde16942021-06-28 15:23:19 +09001698 pr_err("Not support authentication\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001699 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001700 }
1701
1702out_err:
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001703 if (rc == -EINVAL)
1704 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1705 else if (rc == -ENOENT)
1706 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1707 else if (rc == -EACCES)
1708 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
1709 else if (rc == -EFAULT)
1710 rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED;
1711 else if (rc)
1712 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1713
Namjae Jeone2f34482021-03-16 10:49:09 +09001714 if (conn->use_spnego && conn->mechToken) {
1715 kfree(conn->mechToken);
1716 conn->mechToken = NULL;
1717 }
1718
1719 if (rc < 0 && sess) {
1720 ksmbd_session_destroy(sess);
1721 work->sess = NULL;
1722 }
1723
1724 return rc;
1725}
1726
1727/**
1728 * smb2_tree_connect() - handler for smb2 tree connect command
1729 * @work: smb work containing smb request buffer
1730 *
1731 * Return: 0 on success, otherwise error
1732 */
1733int smb2_tree_connect(struct ksmbd_work *work)
1734{
1735 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09001736 struct smb2_tree_connect_req *req = work->request_buf;
1737 struct smb2_tree_connect_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001738 struct ksmbd_session *sess = work->sess;
1739 char *treename = NULL, *name = NULL;
1740 struct ksmbd_tree_conn_status status;
1741 struct ksmbd_share_config *share;
1742 int rc = -EINVAL;
1743
1744 treename = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09001745 le16_to_cpu(req->PathLength), true,
1746 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09001747 if (IS_ERR(treename)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001748 pr_err("treename is NULL\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001749 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1750 goto out_err1;
1751 }
1752
Stephen Rothwell36ba3862021-03-17 17:01:15 +09001753 name = ksmbd_extract_sharename(treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001754 if (IS_ERR(name)) {
1755 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1756 goto out_err1;
1757 }
1758
1759 ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001760 name, treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001761
1762 status = ksmbd_tree_conn_connect(sess, name);
1763 if (status.ret == KSMBD_TREE_CONN_STATUS_OK)
1764 rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id);
1765 else
1766 goto out_err1;
1767
1768 share = status.tree_conn->share_conf;
1769 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
1770 ksmbd_debug(SMB, "IPC share path request\n");
1771 rsp->ShareType = SMB2_SHARE_TYPE_PIPE;
1772 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1773 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE |
1774 FILE_DELETE_LE | FILE_READ_CONTROL_LE |
1775 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1776 FILE_SYNCHRONIZE_LE;
1777 } else {
1778 rsp->ShareType = SMB2_SHARE_TYPE_DISK;
1779 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1780 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE;
1781 if (test_tree_conn_flag(status.tree_conn,
1782 KSMBD_TREE_CONN_FLAG_WRITABLE)) {
1783 rsp->MaximalAccess |= FILE_WRITE_DATA_LE |
1784 FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE |
Wan Jiabing3aefd542021-06-07 12:54:32 +08001785 FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE |
1786 FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE |
1787 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1788 FILE_SYNCHRONIZE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09001789 }
1790 }
1791
1792 status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1793 if (conn->posix_ext_supported)
1794 status.tree_conn->posix_extensions = true;
1795
1796out_err1:
1797 rsp->StructureSize = cpu_to_le16(16);
1798 rsp->Capabilities = 0;
1799 rsp->Reserved = 0;
1800 /* default manual caching */
1801 rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING;
1802 inc_rfc1001_len(rsp, 16);
1803
1804 if (!IS_ERR(treename))
1805 kfree(treename);
1806 if (!IS_ERR(name))
1807 kfree(name);
1808
1809 switch (status.ret) {
1810 case KSMBD_TREE_CONN_STATUS_OK:
1811 rsp->hdr.Status = STATUS_SUCCESS;
1812 rc = 0;
1813 break;
1814 case KSMBD_TREE_CONN_STATUS_NO_SHARE:
1815 rsp->hdr.Status = STATUS_BAD_NETWORK_PATH;
1816 break;
1817 case -ENOMEM:
1818 case KSMBD_TREE_CONN_STATUS_NOMEM:
1819 rsp->hdr.Status = STATUS_NO_MEMORY;
1820 break;
1821 case KSMBD_TREE_CONN_STATUS_ERROR:
1822 case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS:
1823 case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS:
1824 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1825 break;
1826 case -EINVAL:
1827 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1828 break;
1829 default:
1830 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1831 }
1832
1833 return rc;
1834}
1835
1836/**
1837 * smb2_create_open_flags() - convert smb open flags to unix open flags
1838 * @file_present: is file already present
1839 * @access: file access flags
1840 * @disposition: file disposition flags
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001841 * @may_flags: set with MAY_ flags
Namjae Jeone2f34482021-03-16 10:49:09 +09001842 *
1843 * Return: file open flags
1844 */
1845static int smb2_create_open_flags(bool file_present, __le32 access,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001846 __le32 disposition,
1847 int *may_flags)
Namjae Jeone2f34482021-03-16 10:49:09 +09001848{
1849 int oflags = O_NONBLOCK | O_LARGEFILE;
1850
1851 if (access & FILE_READ_DESIRED_ACCESS_LE &&
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001852 access & FILE_WRITE_DESIRE_ACCESS_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001853 oflags |= O_RDWR;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001854 *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE;
1855 } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001856 oflags |= O_WRONLY;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001857 *may_flags = MAY_OPEN | MAY_WRITE;
1858 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09001859 oflags |= O_RDONLY;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001860 *may_flags = MAY_OPEN | MAY_READ;
1861 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001862
1863 if (access == FILE_READ_ATTRIBUTES_LE)
1864 oflags |= O_PATH;
1865
1866 if (file_present) {
1867 switch (disposition & FILE_CREATE_MASK_LE) {
1868 case FILE_OPEN_LE:
1869 case FILE_CREATE_LE:
1870 break;
1871 case FILE_SUPERSEDE_LE:
1872 case FILE_OVERWRITE_LE:
1873 case FILE_OVERWRITE_IF_LE:
1874 oflags |= O_TRUNC;
1875 break;
1876 default:
1877 break;
1878 }
1879 } else {
1880 switch (disposition & FILE_CREATE_MASK_LE) {
1881 case FILE_SUPERSEDE_LE:
1882 case FILE_CREATE_LE:
1883 case FILE_OPEN_IF_LE:
1884 case FILE_OVERWRITE_IF_LE:
1885 oflags |= O_CREAT;
1886 break;
1887 case FILE_OPEN_LE:
1888 case FILE_OVERWRITE_LE:
1889 oflags &= ~O_CREAT;
1890 break;
1891 default:
1892 break;
1893 }
1894 }
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001895
Namjae Jeone2f34482021-03-16 10:49:09 +09001896 return oflags;
1897}
1898
1899/**
1900 * smb2_tree_disconnect() - handler for smb tree connect request
1901 * @work: smb work containing request buffer
1902 *
1903 * Return: 0
1904 */
1905int smb2_tree_disconnect(struct ksmbd_work *work)
1906{
Namjae Jeone5066492021-03-30 12:35:23 +09001907 struct smb2_tree_disconnect_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001908 struct ksmbd_session *sess = work->sess;
1909 struct ksmbd_tree_connect *tcon = work->tcon;
1910
1911 rsp->StructureSize = cpu_to_le16(4);
1912 inc_rfc1001_len(rsp, 4);
1913
1914 ksmbd_debug(SMB, "request\n");
1915
1916 if (!tcon) {
Namjae Jeone5066492021-03-30 12:35:23 +09001917 struct smb2_tree_disconnect_req *req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001918
1919 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
1920 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
1921 smb2_set_err_rsp(work);
1922 return 0;
1923 }
1924
1925 ksmbd_close_tree_conn_fds(work);
1926 ksmbd_tree_conn_disconnect(sess, tcon);
1927 return 0;
1928}
1929
1930/**
1931 * smb2_session_logoff() - handler for session log off request
1932 * @work: smb work containing request buffer
1933 *
1934 * Return: 0
1935 */
1936int smb2_session_logoff(struct ksmbd_work *work)
1937{
1938 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09001939 struct smb2_logoff_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001940 struct ksmbd_session *sess = work->sess;
1941
1942 rsp->StructureSize = cpu_to_le16(4);
1943 inc_rfc1001_len(rsp, 4);
1944
1945 ksmbd_debug(SMB, "request\n");
1946
1947 /* Got a valid session, set connection state */
1948 WARN_ON(sess->conn != conn);
1949
1950 /* setting CifsExiting here may race with start_tcp_sess */
1951 ksmbd_conn_set_need_reconnect(work);
1952 ksmbd_close_session_fds(work);
1953 ksmbd_conn_wait_idle(conn);
1954
1955 if (ksmbd_tree_conn_session_logoff(sess)) {
Namjae Jeone5066492021-03-30 12:35:23 +09001956 struct smb2_logoff_req *req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001957
1958 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
1959 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
1960 smb2_set_err_rsp(work);
1961 return 0;
1962 }
1963
1964 ksmbd_destroy_file_table(&sess->file_table);
1965 sess->state = SMB2_SESSION_EXPIRED;
1966
1967 ksmbd_free_user(sess->user);
1968 sess->user = NULL;
1969
1970 /* let start_tcp_sess free connection info now */
1971 ksmbd_conn_set_need_negotiate(work);
1972 return 0;
1973}
1974
1975/**
1976 * create_smb2_pipe() - create IPC pipe
1977 * @work: smb work containing request buffer
1978 *
1979 * Return: 0 on success, otherwise error
1980 */
1981static noinline int create_smb2_pipe(struct ksmbd_work *work)
1982{
Namjae Jeone5066492021-03-30 12:35:23 +09001983 struct smb2_create_rsp *rsp = work->response_buf;
1984 struct smb2_create_req *req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001985 int id;
1986 int err;
1987 char *name;
1988
1989 name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09001990 1, work->conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09001991 if (IS_ERR(name)) {
1992 rsp->hdr.Status = STATUS_NO_MEMORY;
1993 err = PTR_ERR(name);
1994 goto out;
1995 }
1996
1997 id = ksmbd_session_rpc_open(work->sess, name);
Marios Makassikis79caa962021-05-06 11:38:35 +09001998 if (id < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001999 pr_err("Unable to open RPC pipe: %d\n", id);
Marios Makassikis79caa962021-05-06 11:38:35 +09002000 err = id;
2001 goto out;
2002 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002003
Marios Makassikis79caa962021-05-06 11:38:35 +09002004 rsp->hdr.Status = STATUS_SUCCESS;
Namjae Jeone2f34482021-03-16 10:49:09 +09002005 rsp->StructureSize = cpu_to_le16(89);
2006 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2007 rsp->Reserved = 0;
2008 rsp->CreateAction = cpu_to_le32(FILE_OPENED);
2009
2010 rsp->CreationTime = cpu_to_le64(0);
2011 rsp->LastAccessTime = cpu_to_le64(0);
2012 rsp->ChangeTime = cpu_to_le64(0);
2013 rsp->AllocationSize = cpu_to_le64(0);
2014 rsp->EndofFile = cpu_to_le64(0);
2015 rsp->FileAttributes = ATTR_NORMAL_LE;
2016 rsp->Reserved2 = 0;
2017 rsp->VolatileFileId = cpu_to_le64(id);
2018 rsp->PersistentFileId = 0;
2019 rsp->CreateContextsOffset = 0;
2020 rsp->CreateContextsLength = 0;
2021
2022 inc_rfc1001_len(rsp, 88); /* StructureSize - 1*/
2023 kfree(name);
2024 return 0;
2025
2026out:
Marios Makassikis79caa962021-05-06 11:38:35 +09002027 switch (err) {
2028 case -EINVAL:
2029 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2030 break;
2031 case -ENOSPC:
2032 case -ENOMEM:
2033 rsp->hdr.Status = STATUS_NO_MEMORY;
2034 break;
2035 }
2036
2037 if (!IS_ERR(name))
2038 kfree(name);
2039
Namjae Jeone2f34482021-03-16 10:49:09 +09002040 smb2_set_err_rsp(work);
2041 return err;
2042}
2043
Namjae Jeone2f34482021-03-16 10:49:09 +09002044/**
2045 * smb2_set_ea() - handler for setting extended attributes using set
2046 * info command
2047 * @eabuf: set info command buffer
2048 * @path: dentry path for get ea
2049 *
2050 * Return: 0 on success, otherwise error
2051 */
2052static int smb2_set_ea(struct smb2_ea_info *eabuf, struct path *path)
2053{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002054 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002055 char *attr_name = NULL, *value;
2056 int rc = 0;
2057 int next = 0;
2058
2059 attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL);
2060 if (!attr_name)
2061 return -ENOMEM;
2062
2063 do {
2064 if (!eabuf->EaNameLength)
2065 goto next;
2066
2067 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002068 "name : <%s>, name_len : %u, value_len : %u, next : %u\n",
2069 eabuf->name, eabuf->EaNameLength,
2070 le16_to_cpu(eabuf->EaValueLength),
2071 le32_to_cpu(eabuf->NextEntryOffset));
Namjae Jeone2f34482021-03-16 10:49:09 +09002072
2073 if (eabuf->EaNameLength >
Namjae Jeon070fb212021-05-26 17:57:12 +09002074 (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002075 rc = -EINVAL;
2076 break;
2077 }
2078
2079 memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2080 memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002081 eabuf->EaNameLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09002082 attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0';
2083 value = (char *)&eabuf->name + eabuf->EaNameLength + 1;
2084
2085 if (!eabuf->EaValueLength) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002086 rc = ksmbd_vfs_casexattr_len(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002087 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002088 attr_name,
2089 XATTR_USER_PREFIX_LEN +
2090 eabuf->EaNameLength);
2091
2092 /* delete the EA only when it exits */
2093 if (rc > 0) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002094 rc = ksmbd_vfs_remove_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002095 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002096 attr_name);
2097
2098 if (rc < 0) {
2099 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002100 "remove xattr failed(%d)\n",
2101 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002102 break;
2103 }
2104 }
2105
2106 /* if the EA doesn't exist, just do nothing. */
2107 rc = 0;
2108 } else {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002109 rc = ksmbd_vfs_setxattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002110 path->dentry, attr_name, value,
Namjae Jeon070fb212021-05-26 17:57:12 +09002111 le16_to_cpu(eabuf->EaValueLength), 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002112 if (rc < 0) {
2113 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002114 "ksmbd_vfs_setxattr is failed(%d)\n",
2115 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002116 break;
2117 }
2118 }
2119
2120next:
2121 next = le32_to_cpu(eabuf->NextEntryOffset);
2122 eabuf = (struct smb2_ea_info *)((char *)eabuf + next);
2123 } while (next != 0);
2124
2125 kfree(attr_name);
2126 return rc;
2127}
2128
2129static inline int check_context_err(void *ctx, char *str)
2130{
2131 int err;
2132
2133 err = PTR_ERR(ctx);
2134 ksmbd_debug(SMB, "find context %s err %d\n", str, err);
2135
2136 if (err == -EINVAL) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002137 pr_err("bad name length\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002138 return err;
2139 }
2140
2141 return 0;
2142}
2143
2144static noinline int smb2_set_stream_name_xattr(struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002145 struct ksmbd_file *fp,
2146 char *stream_name, int s_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09002147{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002148 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002149 size_t xattr_stream_size;
2150 char *xattr_stream_name;
2151 int rc;
2152
2153 rc = ksmbd_vfs_xattr_stream_name(stream_name,
2154 &xattr_stream_name,
2155 &xattr_stream_size,
2156 s_type);
2157 if (rc)
2158 return rc;
2159
2160 fp->stream.name = xattr_stream_name;
2161 fp->stream.size = xattr_stream_size;
2162
2163 /* Check if there is stream prefix in xattr space */
Hyunchul Lee465d7202021-07-03 12:10:36 +09002164 rc = ksmbd_vfs_casexattr_len(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002165 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002166 xattr_stream_name,
2167 xattr_stream_size);
2168 if (rc >= 0)
2169 return 0;
2170
2171 if (fp->cdoption == FILE_OPEN_LE) {
2172 ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc);
2173 return -EBADF;
2174 }
2175
Hyunchul Lee465d7202021-07-03 12:10:36 +09002176 rc = ksmbd_vfs_setxattr(user_ns, path->dentry,
2177 xattr_stream_name, NULL, 0, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002178 if (rc < 0)
Namjae Jeonbde16942021-06-28 15:23:19 +09002179 pr_err("Failed to store XATTR stream name :%d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002180 return 0;
2181}
2182
Hyunchul Leeef24c962021-06-30 18:25:52 +09002183static int smb2_remove_smb_xattrs(struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002184{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002185 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002186 char *name, *xattr_list = NULL;
2187 ssize_t xattr_list_len;
2188 int err = 0;
2189
Hyunchul Leeef24c962021-06-30 18:25:52 +09002190 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002191 if (xattr_list_len < 0) {
2192 goto out;
2193 } else if (!xattr_list_len) {
2194 ksmbd_debug(SMB, "empty xattr in the file\n");
2195 goto out;
2196 }
2197
2198 for (name = xattr_list; name - xattr_list < xattr_list_len;
2199 name += strlen(name) + 1) {
2200 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
2201
2202 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002203 strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX,
2204 DOS_ATTRIBUTE_PREFIX_LEN) &&
2205 strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09002206 continue;
2207
Hyunchul Lee465d7202021-07-03 12:10:36 +09002208 err = ksmbd_vfs_remove_xattr(user_ns, path->dentry, name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002209 if (err)
2210 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
2211 }
2212out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09002213 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002214 return err;
2215}
2216
2217static int smb2_create_truncate(struct path *path)
2218{
2219 int rc = vfs_truncate(path, 0);
2220
2221 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002222 pr_err("vfs_truncate failed, rc %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002223 return rc;
2224 }
2225
Hyunchul Leeef24c962021-06-30 18:25:52 +09002226 rc = smb2_remove_smb_xattrs(path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002227 if (rc == -EOPNOTSUPP)
2228 rc = 0;
2229 if (rc)
2230 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002231 "ksmbd_truncate_stream_name_xattr failed, rc %d\n",
2232 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002233 return rc;
2234}
2235
Namjae Jeon64b39f42021-03-30 14:25:35 +09002236static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002237 struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002238{
2239 struct xattr_dos_attrib da = {0};
2240 int rc;
2241
2242 if (!test_share_config_flag(tcon->share_conf,
2243 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
2244 return;
2245
2246 da.version = 4;
2247 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
2248 da.itime = da.create_time = fp->create_time;
2249 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
2250 XATTR_DOSINFO_ITIME;
2251
Hyunchul Leeaf349832021-06-30 18:25:53 +09002252 rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt),
2253 path->dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09002254 if (rc)
2255 ksmbd_debug(SMB, "failed to store file attribute into xattr\n");
2256}
2257
2258static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon,
Namjae Jeon070fb212021-05-26 17:57:12 +09002259 struct path *path, struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002260{
2261 struct xattr_dos_attrib da;
2262 int rc;
2263
2264 fp->f_ci->m_fattr &= ~(ATTR_HIDDEN_LE | ATTR_SYSTEM_LE);
2265
2266 /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */
2267 if (!test_share_config_flag(tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002268 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
Namjae Jeone2f34482021-03-16 10:49:09 +09002269 return;
2270
Hyunchul Leeaf349832021-06-30 18:25:53 +09002271 rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_user_ns(path->mnt),
2272 path->dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09002273 if (rc > 0) {
2274 fp->f_ci->m_fattr = cpu_to_le32(da.attr);
2275 fp->create_time = da.create_time;
2276 fp->itime = da.itime;
2277 }
2278}
2279
Namjae Jeon64b39f42021-03-30 14:25:35 +09002280static int smb2_creat(struct ksmbd_work *work, struct path *path, char *name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002281 int open_flags, umode_t posix_mode, bool is_dir)
Namjae Jeone2f34482021-03-16 10:49:09 +09002282{
2283 struct ksmbd_tree_connect *tcon = work->tcon;
2284 struct ksmbd_share_config *share = tcon->share_conf;
2285 umode_t mode;
2286 int rc;
2287
2288 if (!(open_flags & O_CREAT))
2289 return -EBADF;
2290
2291 ksmbd_debug(SMB, "file does not exist, so creating\n");
2292 if (is_dir == true) {
2293 ksmbd_debug(SMB, "creating directory\n");
2294
2295 mode = share_config_directory_mode(share, posix_mode);
2296 rc = ksmbd_vfs_mkdir(work, name, mode);
2297 if (rc)
2298 return rc;
2299 } else {
2300 ksmbd_debug(SMB, "creating regular file\n");
2301
2302 mode = share_config_create_mode(share, posix_mode);
2303 rc = ksmbd_vfs_create(work, name, mode);
2304 if (rc)
2305 return rc;
2306 }
2307
2308 rc = ksmbd_vfs_kern_path(name, 0, path, 0);
2309 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002310 pr_err("cannot get linux path (%s), err = %d\n",
2311 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002312 return rc;
2313 }
2314 return 0;
2315}
2316
2317static int smb2_create_sd_buffer(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09002318 struct smb2_create_req *req,
Hyunchul Leeef24c962021-06-30 18:25:52 +09002319 struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002320{
2321 struct create_context *context;
2322 int rc = -ENOENT;
2323
2324 if (!req->CreateContextsOffset)
2325 return rc;
2326
2327 /* Parse SD BUFFER create contexts */
2328 context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER);
2329 if (context && !IS_ERR(context)) {
2330 struct create_sd_buf_req *sd_buf;
2331
2332 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002333 "Set ACLs using SMB2_CREATE_SD_BUFFER context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002334 sd_buf = (struct create_sd_buf_req *)context;
Hyunchul Leeef24c962021-06-30 18:25:52 +09002335 rc = set_info_sec(work->conn, work->tcon,
2336 path, &sd_buf->ntsd,
Namjae Jeon070fb212021-05-26 17:57:12 +09002337 le32_to_cpu(sd_buf->ccontext.DataLength), true);
Namjae Jeone2f34482021-03-16 10:49:09 +09002338 }
2339
2340 return rc;
2341}
2342
Namjae Jeon3d47e542021-04-20 14:25:35 +09002343static void ksmbd_acls_fattr(struct smb_fattr *fattr, struct inode *inode)
2344{
2345 fattr->cf_uid = inode->i_uid;
2346 fattr->cf_gid = inode->i_gid;
2347 fattr->cf_mode = inode->i_mode;
2348 fattr->cf_dacls = NULL;
2349
Namjae Jeon67d1c432021-06-22 11:42:29 +09002350 fattr->cf_acls = get_acl(inode, ACL_TYPE_ACCESS);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002351 if (S_ISDIR(inode->i_mode))
Namjae Jeon67d1c432021-06-22 11:42:29 +09002352 fattr->cf_dacls = get_acl(inode, ACL_TYPE_DEFAULT);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002353}
2354
Namjae Jeone2f34482021-03-16 10:49:09 +09002355/**
2356 * smb2_open() - handler for smb file open request
2357 * @work: smb work containing request buffer
2358 *
2359 * Return: 0 on success, otherwise error
2360 */
2361int smb2_open(struct ksmbd_work *work)
2362{
2363 struct ksmbd_conn *conn = work->conn;
2364 struct ksmbd_session *sess = work->sess;
2365 struct ksmbd_tree_connect *tcon = work->tcon;
2366 struct smb2_create_req *req;
2367 struct smb2_create_rsp *rsp, *rsp_org;
2368 struct path path;
2369 struct ksmbd_share_config *share = tcon->share_conf;
2370 struct ksmbd_file *fp = NULL;
2371 struct file *filp = NULL;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002372 struct user_namespace *user_ns = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002373 struct kstat stat;
2374 struct create_context *context;
2375 struct lease_ctx_info *lc = NULL;
2376 struct create_ea_buf_req *ea_buf = NULL;
2377 struct oplock_info *opinfo;
2378 __le32 *next_ptr = NULL;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002379 int req_op_level = 0, open_flags = 0, may_flags = 0, file_info = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002380 int rc = 0, len = 0;
2381 int contxt_cnt = 0, query_disk_id = 0;
2382 int maximal_access_ctxt = 0, posix_ctxt = 0;
2383 int s_type = 0;
2384 int next_off = 0;
2385 char *name = NULL;
2386 char *stream_name = NULL;
2387 bool file_present = false, created = false, already_permitted = false;
Namjae Jeone2f34482021-03-16 10:49:09 +09002388 int share_ret, need_truncate = 0;
2389 u64 time;
2390 umode_t posix_mode = 0;
2391 __le32 daccess, maximal_access = 0;
2392
Namjae Jeone5066492021-03-30 12:35:23 +09002393 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09002394 WORK_BUFFERS(work, req, rsp);
2395
2396 if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002397 (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002398 ksmbd_debug(SMB, "invalid flag in chained command\n");
2399 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2400 smb2_set_err_rsp(work);
2401 return -EINVAL;
2402 }
2403
2404 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
2405 ksmbd_debug(SMB, "IPC pipe create request\n");
2406 return create_smb2_pipe(work);
2407 }
2408
2409 if (req->NameLength) {
2410 if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002411 *(char *)req->Buffer == '\\') {
Namjae Jeonbde16942021-06-28 15:23:19 +09002412 pr_err("not allow directory name included leading slash\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002413 rc = -EINVAL;
2414 goto err_out1;
2415 }
2416
2417 name = smb2_get_name(share,
2418 req->Buffer,
2419 le16_to_cpu(req->NameLength),
2420 work->conn->local_nls);
2421 if (IS_ERR(name)) {
2422 rc = PTR_ERR(name);
2423 if (rc != -ENOMEM)
2424 rc = -ENOENT;
2425 goto err_out1;
2426 }
2427
2428 ksmbd_debug(SMB, "converted name = %s\n", name);
2429 if (strchr(name, ':')) {
2430 if (!test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002431 KSMBD_SHARE_FLAG_STREAMS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002432 rc = -EBADF;
2433 goto err_out1;
2434 }
2435 rc = parse_stream_name(name, &stream_name, &s_type);
2436 if (rc < 0)
2437 goto err_out1;
2438 }
2439
2440 rc = ksmbd_validate_filename(name);
2441 if (rc < 0)
2442 goto err_out1;
2443
2444 if (ksmbd_share_veto_filename(share, name)) {
2445 rc = -ENOENT;
2446 ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002447 name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002448 goto err_out1;
2449 }
2450 } else {
2451 len = strlen(share->path);
2452 ksmbd_debug(SMB, "share path len %d\n", len);
2453 name = kmalloc(len + 1, GFP_KERNEL);
2454 if (!name) {
2455 rsp->hdr.Status = STATUS_NO_MEMORY;
2456 rc = -ENOMEM;
2457 goto err_out1;
2458 }
2459
2460 memcpy(name, share->path, len);
2461 *(name + len) = '\0';
2462 }
2463
2464 req_op_level = req->RequestedOplockLevel;
Namjae Jeon73f9dad2021-04-16 14:12:06 +09002465 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002466 lc = parse_lease_state(req);
Namjae Jeone2f34482021-03-16 10:49:09 +09002467
Namjae Jeon64b39f42021-03-30 14:25:35 +09002468 if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002469 pr_err("Invalid impersonationlevel : 0x%x\n",
2470 le32_to_cpu(req->ImpersonationLevel));
Namjae Jeone2f34482021-03-16 10:49:09 +09002471 rc = -EIO;
2472 rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL;
2473 goto err_out1;
2474 }
2475
2476 if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002477 pr_err("Invalid create options : 0x%x\n",
2478 le32_to_cpu(req->CreateOptions));
Namjae Jeone2f34482021-03-16 10:49:09 +09002479 rc = -EINVAL;
2480 goto err_out1;
2481 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09002482 if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002483 req->CreateOptions & FILE_RANDOM_ACCESS_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002484 req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE);
2485
Namjae Jeon070fb212021-05-26 17:57:12 +09002486 if (req->CreateOptions &
2487 (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION |
2488 FILE_RESERVE_OPFILTER_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002489 rc = -EOPNOTSUPP;
2490 goto err_out1;
2491 }
2492
2493 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2494 if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) {
2495 rc = -EINVAL;
2496 goto err_out1;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002497 } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002498 req->CreateOptions = ~(FILE_NO_COMPRESSION_LE);
Namjae Jeon64b39f42021-03-30 14:25:35 +09002499 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002500 }
2501 }
2502
2503 if (le32_to_cpu(req->CreateDisposition) >
Namjae Jeon070fb212021-05-26 17:57:12 +09002504 le32_to_cpu(FILE_OVERWRITE_IF_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002505 pr_err("Invalid create disposition : 0x%x\n",
2506 le32_to_cpu(req->CreateDisposition));
Namjae Jeone2f34482021-03-16 10:49:09 +09002507 rc = -EINVAL;
2508 goto err_out1;
2509 }
2510
2511 if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002512 pr_err("Invalid desired access : 0x%x\n",
2513 le32_to_cpu(req->DesiredAccess));
Namjae Jeone2f34482021-03-16 10:49:09 +09002514 rc = -EACCES;
2515 goto err_out1;
2516 }
2517
Namjae Jeon64b39f42021-03-30 14:25:35 +09002518 if (req->FileAttributes && !(req->FileAttributes & ATTR_MASK_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002519 pr_err("Invalid file attribute : 0x%x\n",
2520 le32_to_cpu(req->FileAttributes));
Namjae Jeone2f34482021-03-16 10:49:09 +09002521 rc = -EINVAL;
2522 goto err_out1;
2523 }
2524
2525 if (req->CreateContextsOffset) {
2526 /* Parse non-durable handle create contexts */
2527 context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER);
2528 if (IS_ERR(context)) {
2529 rc = check_context_err(context, SMB2_CREATE_EA_BUFFER);
2530 if (rc < 0)
2531 goto err_out1;
2532 } else {
2533 ea_buf = (struct create_ea_buf_req *)context;
2534 if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) {
2535 rsp->hdr.Status = STATUS_ACCESS_DENIED;
2536 rc = -EACCES;
2537 goto err_out1;
2538 }
2539 }
2540
2541 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002542 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
Namjae Jeone2f34482021-03-16 10:49:09 +09002543 if (IS_ERR(context)) {
2544 rc = check_context_err(context,
Namjae Jeon070fb212021-05-26 17:57:12 +09002545 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
Namjae Jeone2f34482021-03-16 10:49:09 +09002546 if (rc < 0)
2547 goto err_out1;
2548 } else {
2549 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002550 "get query maximal access context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002551 maximal_access_ctxt = 1;
2552 }
2553
2554 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002555 SMB2_CREATE_TIMEWARP_REQUEST);
Namjae Jeone2f34482021-03-16 10:49:09 +09002556 if (IS_ERR(context)) {
2557 rc = check_context_err(context,
Namjae Jeon070fb212021-05-26 17:57:12 +09002558 SMB2_CREATE_TIMEWARP_REQUEST);
Namjae Jeone2f34482021-03-16 10:49:09 +09002559 if (rc < 0)
2560 goto err_out1;
2561 } else {
2562 ksmbd_debug(SMB, "get timewarp context\n");
2563 rc = -EBADF;
2564 goto err_out1;
2565 }
2566
2567 if (tcon->posix_extensions) {
2568 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002569 SMB2_CREATE_TAG_POSIX);
Namjae Jeone2f34482021-03-16 10:49:09 +09002570 if (IS_ERR(context)) {
2571 rc = check_context_err(context,
Namjae Jeon070fb212021-05-26 17:57:12 +09002572 SMB2_CREATE_TAG_POSIX);
Namjae Jeone2f34482021-03-16 10:49:09 +09002573 if (rc < 0)
2574 goto err_out1;
2575 } else {
2576 struct create_posix *posix =
2577 (struct create_posix *)context;
2578 ksmbd_debug(SMB, "get posix context\n");
2579
2580 posix_mode = le32_to_cpu(posix->Mode);
2581 posix_ctxt = 1;
2582 }
2583 }
2584 }
2585
2586 if (ksmbd_override_fsids(work)) {
2587 rc = -ENOMEM;
2588 goto err_out1;
2589 }
2590
2591 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) {
2592 /*
2593 * On delete request, instead of following up, need to
2594 * look the current entity
2595 */
2596 rc = ksmbd_vfs_kern_path(name, 0, &path, 1);
2597 if (!rc) {
2598 /*
2599 * If file exists with under flags, return access
2600 * denied error.
2601 */
2602 if (req->CreateDisposition == FILE_OVERWRITE_IF_LE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09002603 req->CreateDisposition == FILE_OPEN_IF_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002604 rc = -EACCES;
2605 path_put(&path);
2606 goto err_out;
2607 }
2608
Namjae Jeon64b39f42021-03-30 14:25:35 +09002609 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002610 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002611 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002612 rc = -EACCES;
2613 path_put(&path);
2614 goto err_out;
2615 }
2616 }
2617 } else {
2618 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002619 KSMBD_SHARE_FLAG_FOLLOW_SYMLINKS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002620 /*
2621 * Use LOOKUP_FOLLOW to follow the path of
2622 * symlink in path buildup
2623 */
2624 rc = ksmbd_vfs_kern_path(name, LOOKUP_FOLLOW, &path, 1);
2625 if (rc) { /* Case for broken link ?*/
2626 rc = ksmbd_vfs_kern_path(name, 0, &path, 1);
2627 }
2628 } else {
2629 rc = ksmbd_vfs_kern_path(name, 0, &path, 1);
2630 if (!rc && d_is_symlink(path.dentry)) {
2631 rc = -EACCES;
2632 path_put(&path);
2633 goto err_out;
2634 }
2635 }
2636 }
2637
2638 if (rc) {
2639 if (rc == -EACCES) {
2640 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002641 "User does not have right permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002642 goto err_out;
2643 }
2644 ksmbd_debug(SMB, "can not get linux path for %s, rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002645 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002646 rc = 0;
2647 } else {
2648 file_present = true;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002649 user_ns = mnt_user_ns(path.mnt);
2650 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09002651 }
2652 if (stream_name) {
2653 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2654 if (s_type == DATA_STREAM) {
2655 rc = -EIO;
2656 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2657 }
2658 } else {
2659 if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) {
2660 rc = -EIO;
2661 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2662 }
2663 }
2664
2665 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002666 req->FileAttributes & ATTR_NORMAL_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002667 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2668 rc = -EIO;
2669 }
2670
2671 if (rc < 0)
2672 goto err_out;
2673 }
2674
Namjae Jeon64b39f42021-03-30 14:25:35 +09002675 if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE &&
2676 S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002677 ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002678 name, req->CreateOptions);
Namjae Jeone2f34482021-03-16 10:49:09 +09002679 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2680 rc = -EIO;
2681 goto err_out;
2682 }
2683
2684 if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002685 !(req->CreateDisposition == FILE_CREATE_LE) &&
2686 !S_ISDIR(stat.mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002687 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2688 rc = -EIO;
2689 goto err_out;
2690 }
2691
2692 if (!stream_name && file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002693 req->CreateDisposition == FILE_CREATE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002694 rc = -EEXIST;
2695 goto err_out;
2696 }
2697
Namjae Jeone2f34482021-03-16 10:49:09 +09002698 daccess = smb_map_generic_desired_access(req->DesiredAccess);
2699
2700 if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002701 rc = smb_check_perm_dacl(conn, &path, &daccess,
Namjae Jeon070fb212021-05-26 17:57:12 +09002702 sess->user->uid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002703 if (rc)
2704 goto err_out;
2705 }
2706
2707 if (daccess & FILE_MAXIMAL_ACCESS_LE) {
2708 if (!file_present) {
2709 daccess = cpu_to_le32(GENERIC_ALL_FLAGS);
2710 } else {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002711 rc = ksmbd_vfs_query_maximal_access(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002712 path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002713 &daccess);
2714 if (rc)
2715 goto err_out;
2716 already_permitted = true;
2717 }
2718 maximal_access = daccess;
2719 }
2720
Namjae Jeon070fb212021-05-26 17:57:12 +09002721 open_flags = smb2_create_open_flags(file_present, daccess,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002722 req->CreateDisposition,
2723 &may_flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09002724
2725 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
2726 if (open_flags & O_CREAT) {
2727 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002728 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002729 rc = -EACCES;
2730 goto err_out;
2731 }
2732 }
2733
2734 /*create file if not present */
2735 if (!file_present) {
2736 rc = smb2_creat(work, &path, name, open_flags, posix_mode,
Namjae Jeon070fb212021-05-26 17:57:12 +09002737 req->CreateOptions & FILE_DIRECTORY_FILE_LE);
Namjae Jeone2f34482021-03-16 10:49:09 +09002738 if (rc)
2739 goto err_out;
2740
2741 created = true;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002742 user_ns = mnt_user_ns(path.mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002743 if (ea_buf) {
2744 rc = smb2_set_ea(&ea_buf->ea, &path);
2745 if (rc == -EOPNOTSUPP)
2746 rc = 0;
2747 else if (rc)
2748 goto err_out;
2749 }
2750 } else if (!already_permitted) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002751 /* FILE_READ_ATTRIBUTE is allowed without inode_permission,
2752 * because execute(search) permission on a parent directory,
2753 * is already granted.
2754 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09002755 if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002756 rc = inode_permission(user_ns,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002757 d_inode(path.dentry),
2758 may_flags);
Namjae Jeonff1d5722021-04-13 13:18:10 +09002759 if (rc)
Namjae Jeone2f34482021-03-16 10:49:09 +09002760 goto err_out;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002761
2762 if ((daccess & FILE_DELETE_LE) ||
2763 (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002764 rc = ksmbd_vfs_may_delete(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002765 path.dentry);
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002766 if (rc)
2767 goto err_out;
2768 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002769 }
2770 }
2771
2772 rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent));
2773 if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) {
2774 rc = -EBUSY;
2775 goto err_out;
2776 }
2777
2778 rc = 0;
2779 filp = dentry_open(&path, open_flags, current_cred());
2780 if (IS_ERR(filp)) {
2781 rc = PTR_ERR(filp);
Namjae Jeonbde16942021-06-28 15:23:19 +09002782 pr_err("dentry open for dir failed, rc %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002783 goto err_out;
2784 }
2785
2786 if (file_present) {
2787 if (!(open_flags & O_TRUNC))
2788 file_info = FILE_OPENED;
2789 else
2790 file_info = FILE_OVERWRITTEN;
2791
Namjae Jeon070fb212021-05-26 17:57:12 +09002792 if ((req->CreateDisposition & FILE_CREATE_MASK_LE) ==
2793 FILE_SUPERSEDE_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002794 file_info = FILE_SUPERSEDED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002795 } else if (open_flags & O_CREAT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002796 file_info = FILE_CREATED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002797 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002798
2799 ksmbd_vfs_set_fadvise(filp, req->CreateOptions);
2800
2801 /* Obtain Volatile-ID */
2802 fp = ksmbd_open_fd(work, filp);
2803 if (IS_ERR(fp)) {
2804 fput(filp);
2805 rc = PTR_ERR(fp);
2806 fp = NULL;
2807 goto err_out;
2808 }
2809
2810 /* Get Persistent-ID */
2811 ksmbd_open_durable_fd(fp);
Namjae Jeon38673692021-07-08 12:32:27 +09002812 if (!has_file_id(fp->persistent_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002813 rc = -ENOMEM;
2814 goto err_out;
2815 }
2816
2817 fp->filename = name;
2818 fp->cdoption = req->CreateDisposition;
2819 fp->daccess = daccess;
2820 fp->saccess = req->ShareAccess;
2821 fp->coption = req->CreateOptions;
2822
2823 /* Set default windows and posix acls if creating new file */
2824 if (created) {
2825 int posix_acl_rc;
Namjae Jeonfba08fa2021-04-15 10:29:39 +09002826 struct inode *inode = d_inode(path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09002827
Hyunchul Lee465d7202021-07-03 12:10:36 +09002828 posix_acl_rc = ksmbd_vfs_inherit_posix_acl(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002829 inode,
2830 d_inode(path.dentry->d_parent));
Namjae Jeone2f34482021-03-16 10:49:09 +09002831 if (posix_acl_rc)
2832 ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc);
2833
2834 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002835 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002836 rc = smb_inherit_dacl(conn, &path, sess->user->uid,
Namjae Jeon070fb212021-05-26 17:57:12 +09002837 sess->user->gid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002838 }
2839
2840 if (rc) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002841 rc = smb2_create_sd_buffer(work, req, &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002842 if (rc) {
2843 if (posix_acl_rc)
Hyunchul Lee465d7202021-07-03 12:10:36 +09002844 ksmbd_vfs_set_init_posix_acl(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002845 inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09002846
2847 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002848 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002849 struct smb_fattr fattr;
2850 struct smb_ntsd *pntsd;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002851 int pntsd_size, ace_num = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002852
Namjae Jeon3d47e542021-04-20 14:25:35 +09002853 ksmbd_acls_fattr(&fattr, inode);
Marios Makassikise6b10592021-04-15 10:24:56 +09002854 if (fattr.cf_acls)
2855 ace_num = fattr.cf_acls->a_count;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002856 if (fattr.cf_dacls)
2857 ace_num += fattr.cf_dacls->a_count;
Namjae Jeone2f34482021-03-16 10:49:09 +09002858
2859 pntsd = kmalloc(sizeof(struct smb_ntsd) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002860 sizeof(struct smb_sid) * 3 +
Namjae Jeone2f34482021-03-16 10:49:09 +09002861 sizeof(struct smb_acl) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002862 sizeof(struct smb_ace) * ace_num * 2,
Namjae Jeone2f34482021-03-16 10:49:09 +09002863 GFP_KERNEL);
2864 if (!pntsd)
2865 goto err_out;
2866
Hyunchul Lee465d7202021-07-03 12:10:36 +09002867 rc = build_sec_desc(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002868 pntsd, NULL,
Namjae Jeon070fb212021-05-26 17:57:12 +09002869 OWNER_SECINFO |
Hyunchul Leeaf349832021-06-30 18:25:53 +09002870 GROUP_SECINFO |
2871 DACL_SECINFO,
Namjae Jeon070fb212021-05-26 17:57:12 +09002872 &pntsd_size, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09002873 posix_acl_release(fattr.cf_acls);
2874 posix_acl_release(fattr.cf_dacls);
2875
2876 rc = ksmbd_vfs_set_sd_xattr(conn,
Hyunchul Lee465d7202021-07-03 12:10:36 +09002877 user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +09002878 path.dentry,
2879 pntsd,
2880 pntsd_size);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002881 kfree(pntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09002882 if (rc)
Namjae Jeonbde16942021-06-28 15:23:19 +09002883 pr_err("failed to store ntacl in xattr : %d\n",
2884 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002885 }
2886 }
2887 }
2888 rc = 0;
2889 }
2890
2891 if (stream_name) {
2892 rc = smb2_set_stream_name_xattr(&path,
2893 fp,
2894 stream_name,
2895 s_type);
2896 if (rc)
2897 goto err_out;
2898 file_info = FILE_CREATED;
2899 }
2900
2901 fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE |
2902 FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE));
Namjae Jeon64b39f42021-03-30 14:25:35 +09002903 if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC &&
2904 !fp->attrib_only && !stream_name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002905 smb_break_all_oplock(work, fp);
2906 need_truncate = 1;
2907 }
2908
2909 /* fp should be searchable through ksmbd_inode.m_fp_list
2910 * after daccess, saccess, attrib_only, and stream are
2911 * initialized.
2912 */
2913 write_lock(&fp->f_ci->m_lock);
2914 list_add(&fp->node, &fp->f_ci->m_fp_list);
2915 write_unlock(&fp->f_ci->m_lock);
2916
2917 rc = ksmbd_vfs_getattr(&path, &stat);
2918 if (rc) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002919 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09002920 rc = 0;
2921 }
2922
2923 /* Check delete pending among previous fp before oplock break */
2924 if (ksmbd_inode_pending_delete(fp)) {
2925 rc = -EBUSY;
2926 goto err_out;
2927 }
2928
2929 share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp);
Namjae Jeon64b39f42021-03-30 14:25:35 +09002930 if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) ||
2931 (req_op_level == SMB2_OPLOCK_LEVEL_LEASE &&
2932 !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) {
Namjae Jeonab0b2632021-06-29 09:20:13 +09002933 if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002934 rc = share_ret;
2935 goto err_out;
2936 }
2937 } else {
2938 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) {
2939 req_op_level = smb2_map_lease_to_oplock(lc->req_state);
2940 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002941 "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n",
2942 name, req_op_level, lc->req_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09002943 rc = find_same_lease_key(sess, fp->f_ci, lc);
2944 if (rc)
2945 goto err_out;
2946 } else if (open_flags == O_RDONLY &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002947 (req_op_level == SMB2_OPLOCK_LEVEL_BATCH ||
2948 req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
Namjae Jeone2f34482021-03-16 10:49:09 +09002949 req_op_level = SMB2_OPLOCK_LEVEL_II;
2950
2951 rc = smb_grant_oplock(work, req_op_level,
2952 fp->persistent_id, fp,
2953 le32_to_cpu(req->hdr.Id.SyncId.TreeId),
2954 lc, share_ret);
2955 if (rc < 0)
2956 goto err_out;
2957 }
2958
2959 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
2960 ksmbd_fd_set_delete_on_close(fp, file_info);
2961
2962 if (need_truncate) {
2963 rc = smb2_create_truncate(&path);
2964 if (rc)
2965 goto err_out;
2966 }
2967
2968 if (req->CreateContextsOffset) {
2969 struct create_alloc_size_req *az_req;
2970
Namjae Jeon070fb212021-05-26 17:57:12 +09002971 az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req,
2972 SMB2_CREATE_ALLOCATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09002973 if (IS_ERR(az_req)) {
2974 rc = check_context_err(az_req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002975 SMB2_CREATE_ALLOCATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09002976 if (rc < 0)
2977 goto err_out;
2978 } else {
2979 loff_t alloc_size = le64_to_cpu(az_req->AllocationSize);
2980 int err;
2981
2982 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002983 "request smb2 create allocate size : %llu\n",
2984 alloc_size);
Namjae Jeone8c06192021-06-22 11:06:11 +09002985 smb_break_all_levII_oplock(work, fp, 1);
2986 err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
2987 alloc_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09002988 if (err < 0)
2989 ksmbd_debug(SMB,
Namjae Jeone8c06192021-06-22 11:06:11 +09002990 "vfs_fallocate is failed : %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002991 err);
Namjae Jeone2f34482021-03-16 10:49:09 +09002992 }
2993
Namjae Jeon64b39f42021-03-30 14:25:35 +09002994 context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID);
Namjae Jeone2f34482021-03-16 10:49:09 +09002995 if (IS_ERR(context)) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09002996 rc = check_context_err(context, SMB2_CREATE_QUERY_ON_DISK_ID);
Namjae Jeone2f34482021-03-16 10:49:09 +09002997 if (rc < 0)
2998 goto err_out;
2999 } else {
3000 ksmbd_debug(SMB, "get query on disk id context\n");
3001 query_disk_id = 1;
3002 }
3003 }
3004
3005 if (stat.result_mask & STATX_BTIME)
3006 fp->create_time = ksmbd_UnixTimeToNT(stat.btime);
3007 else
3008 fp->create_time = ksmbd_UnixTimeToNT(stat.ctime);
3009 if (req->FileAttributes || fp->f_ci->m_fattr == 0)
Namjae Jeon070fb212021-05-26 17:57:12 +09003010 fp->f_ci->m_fattr =
3011 cpu_to_le32(smb2_get_dos_mode(&stat, le32_to_cpu(req->FileAttributes)));
Namjae Jeone2f34482021-03-16 10:49:09 +09003012
3013 if (!created)
3014 smb2_update_xattrs(tcon, &path, fp);
3015 else
3016 smb2_new_xattrs(tcon, &path, fp);
3017
3018 memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE);
3019
Hyunchul Lee465d7202021-07-03 12:10:36 +09003020 generic_fillattr(user_ns, file_inode(fp->filp),
Hyunchul Leeaf349832021-06-30 18:25:53 +09003021 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09003022
3023 rsp->StructureSize = cpu_to_le16(89);
3024 rcu_read_lock();
3025 opinfo = rcu_dereference(fp->f_opinfo);
3026 rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0;
3027 rcu_read_unlock();
3028 rsp->Reserved = 0;
3029 rsp->CreateAction = cpu_to_le32(file_info);
3030 rsp->CreationTime = cpu_to_le64(fp->create_time);
3031 time = ksmbd_UnixTimeToNT(stat.atime);
3032 rsp->LastAccessTime = cpu_to_le64(time);
3033 time = ksmbd_UnixTimeToNT(stat.mtime);
3034 rsp->LastWriteTime = cpu_to_le64(time);
3035 time = ksmbd_UnixTimeToNT(stat.ctime);
3036 rsp->ChangeTime = cpu_to_le64(time);
3037 rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 :
3038 cpu_to_le64(stat.blocks << 9);
3039 rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
3040 rsp->FileAttributes = fp->f_ci->m_fattr;
3041
3042 rsp->Reserved2 = 0;
3043
3044 rsp->PersistentFileId = cpu_to_le64(fp->persistent_id);
3045 rsp->VolatileFileId = cpu_to_le64(fp->volatile_id);
3046
3047 rsp->CreateContextsOffset = 0;
3048 rsp->CreateContextsLength = 0;
3049 inc_rfc1001_len(rsp_org, 88); /* StructureSize - 1*/
3050
3051 /* If lease is request send lease context response */
3052 if (opinfo && opinfo->is_lease) {
3053 struct create_context *lease_ccontext;
3054
3055 ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003056 name, opinfo->o_lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +09003057 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
3058
3059 lease_ccontext = (struct create_context *)rsp->Buffer;
3060 contxt_cnt++;
3061 create_lease_buf(rsp->Buffer, opinfo->o_lease);
3062 le32_add_cpu(&rsp->CreateContextsLength,
3063 conn->vals->create_lease_size);
3064 inc_rfc1001_len(rsp_org, conn->vals->create_lease_size);
3065 next_ptr = &lease_ccontext->Next;
3066 next_off = conn->vals->create_lease_size;
3067 }
3068
Namjae Jeone2f34482021-03-16 10:49:09 +09003069 if (maximal_access_ctxt) {
3070 struct create_context *mxac_ccontext;
3071
3072 if (maximal_access == 0)
Hyunchul Lee465d7202021-07-03 12:10:36 +09003073 ksmbd_vfs_query_maximal_access(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09003074 path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09003075 &maximal_access);
3076 mxac_ccontext = (struct create_context *)(rsp->Buffer +
3077 le32_to_cpu(rsp->CreateContextsLength));
3078 contxt_cnt++;
3079 create_mxac_rsp_buf(rsp->Buffer +
3080 le32_to_cpu(rsp->CreateContextsLength),
3081 le32_to_cpu(maximal_access));
3082 le32_add_cpu(&rsp->CreateContextsLength,
3083 conn->vals->create_mxac_size);
3084 inc_rfc1001_len(rsp_org, conn->vals->create_mxac_size);
3085 if (next_ptr)
3086 *next_ptr = cpu_to_le32(next_off);
3087 next_ptr = &mxac_ccontext->Next;
3088 next_off = conn->vals->create_mxac_size;
3089 }
3090
3091 if (query_disk_id) {
3092 struct create_context *disk_id_ccontext;
3093
3094 disk_id_ccontext = (struct create_context *)(rsp->Buffer +
3095 le32_to_cpu(rsp->CreateContextsLength));
3096 contxt_cnt++;
3097 create_disk_id_rsp_buf(rsp->Buffer +
3098 le32_to_cpu(rsp->CreateContextsLength),
3099 stat.ino, tcon->id);
3100 le32_add_cpu(&rsp->CreateContextsLength,
3101 conn->vals->create_disk_id_size);
3102 inc_rfc1001_len(rsp_org, conn->vals->create_disk_id_size);
3103 if (next_ptr)
3104 *next_ptr = cpu_to_le32(next_off);
3105 next_ptr = &disk_id_ccontext->Next;
3106 next_off = conn->vals->create_disk_id_size;
3107 }
3108
3109 if (posix_ctxt) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003110 contxt_cnt++;
3111 create_posix_rsp_buf(rsp->Buffer +
3112 le32_to_cpu(rsp->CreateContextsLength),
3113 fp);
3114 le32_add_cpu(&rsp->CreateContextsLength,
3115 conn->vals->create_posix_size);
3116 inc_rfc1001_len(rsp_org, conn->vals->create_posix_size);
3117 if (next_ptr)
3118 *next_ptr = cpu_to_le32(next_off);
3119 }
3120
3121 if (contxt_cnt > 0) {
3122 rsp->CreateContextsOffset =
3123 cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer)
3124 - 4);
3125 }
3126
3127err_out:
3128 if (file_present || created)
3129 path_put(&path);
3130 ksmbd_revert_fsids(work);
3131err_out1:
3132 if (rc) {
3133 if (rc == -EINVAL)
3134 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3135 else if (rc == -EOPNOTSUPP)
3136 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeonff1d5722021-04-13 13:18:10 +09003137 else if (rc == -EACCES || rc == -ESTALE)
Namjae Jeone2f34482021-03-16 10:49:09 +09003138 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3139 else if (rc == -ENOENT)
3140 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
3141 else if (rc == -EPERM)
3142 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
3143 else if (rc == -EBUSY)
3144 rsp->hdr.Status = STATUS_DELETE_PENDING;
3145 else if (rc == -EBADF)
3146 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
3147 else if (rc == -ENOEXEC)
3148 rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID;
3149 else if (rc == -ENXIO)
3150 rsp->hdr.Status = STATUS_NO_SUCH_DEVICE;
3151 else if (rc == -EEXIST)
3152 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
3153 else if (rc == -EMFILE)
3154 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
3155 if (!rsp->hdr.Status)
3156 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3157
3158 if (!fp || !fp->filename)
3159 kfree(name);
3160 if (fp)
3161 ksmbd_fd_put(work, fp);
3162 smb2_set_err_rsp(work);
3163 ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status);
3164 }
3165
3166 kfree(lc);
3167
3168 return 0;
3169}
3170
3171static int readdir_info_level_struct_sz(int info_level)
3172{
3173 switch (info_level) {
3174 case FILE_FULL_DIRECTORY_INFORMATION:
3175 return sizeof(struct file_full_directory_info);
3176 case FILE_BOTH_DIRECTORY_INFORMATION:
3177 return sizeof(struct file_both_directory_info);
3178 case FILE_DIRECTORY_INFORMATION:
3179 return sizeof(struct file_directory_info);
3180 case FILE_NAMES_INFORMATION:
3181 return sizeof(struct file_names_info);
3182 case FILEID_FULL_DIRECTORY_INFORMATION:
3183 return sizeof(struct file_id_full_dir_info);
3184 case FILEID_BOTH_DIRECTORY_INFORMATION:
3185 return sizeof(struct file_id_both_directory_info);
3186 case SMB_FIND_FILE_POSIX_INFO:
3187 return sizeof(struct smb2_posix_info);
3188 default:
3189 return -EOPNOTSUPP;
3190 }
3191}
3192
3193static int dentry_name(struct ksmbd_dir_info *d_info, int info_level)
3194{
3195 switch (info_level) {
3196 case FILE_FULL_DIRECTORY_INFORMATION:
3197 {
3198 struct file_full_directory_info *ffdinfo;
3199
3200 ffdinfo = (struct file_full_directory_info *)d_info->rptr;
3201 d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset);
3202 d_info->name = ffdinfo->FileName;
3203 d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength);
3204 return 0;
3205 }
3206 case FILE_BOTH_DIRECTORY_INFORMATION:
3207 {
3208 struct file_both_directory_info *fbdinfo;
3209
3210 fbdinfo = (struct file_both_directory_info *)d_info->rptr;
3211 d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset);
3212 d_info->name = fbdinfo->FileName;
3213 d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength);
3214 return 0;
3215 }
3216 case FILE_DIRECTORY_INFORMATION:
3217 {
3218 struct file_directory_info *fdinfo;
3219
3220 fdinfo = (struct file_directory_info *)d_info->rptr;
3221 d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset);
3222 d_info->name = fdinfo->FileName;
3223 d_info->name_len = le32_to_cpu(fdinfo->FileNameLength);
3224 return 0;
3225 }
3226 case FILE_NAMES_INFORMATION:
3227 {
3228 struct file_names_info *fninfo;
3229
3230 fninfo = (struct file_names_info *)d_info->rptr;
3231 d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset);
3232 d_info->name = fninfo->FileName;
3233 d_info->name_len = le32_to_cpu(fninfo->FileNameLength);
3234 return 0;
3235 }
3236 case FILEID_FULL_DIRECTORY_INFORMATION:
3237 {
3238 struct file_id_full_dir_info *dinfo;
3239
3240 dinfo = (struct file_id_full_dir_info *)d_info->rptr;
3241 d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset);
3242 d_info->name = dinfo->FileName;
3243 d_info->name_len = le32_to_cpu(dinfo->FileNameLength);
3244 return 0;
3245 }
3246 case FILEID_BOTH_DIRECTORY_INFORMATION:
3247 {
3248 struct file_id_both_directory_info *fibdinfo;
3249
3250 fibdinfo = (struct file_id_both_directory_info *)d_info->rptr;
3251 d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset);
3252 d_info->name = fibdinfo->FileName;
3253 d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength);
3254 return 0;
3255 }
3256 case SMB_FIND_FILE_POSIX_INFO:
3257 {
3258 struct smb2_posix_info *posix_info;
3259
3260 posix_info = (struct smb2_posix_info *)d_info->rptr;
3261 d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset);
3262 d_info->name = posix_info->name;
3263 d_info->name_len = le32_to_cpu(posix_info->name_len);
3264 return 0;
3265 }
3266 default:
3267 return -EINVAL;
3268 }
3269}
3270
3271/**
3272 * smb2_populate_readdir_entry() - encode directory entry in smb2 response
3273 * buffer
3274 * @conn: connection instance
3275 * @info_level: smb information level
3276 * @d_info: structure included variables for query dir
Hyunchul Leeaf349832021-06-30 18:25:53 +09003277 * @user_ns: user namespace
Namjae Jeone2f34482021-03-16 10:49:09 +09003278 * @ksmbd_kstat: ksmbd wrapper of dirent stat information
3279 *
3280 * if directory has many entries, find first can't read it fully.
3281 * find next might be called multiple times to read remaining dir entries
3282 *
3283 * Return: 0 on success, otherwise error
3284 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003285static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level,
Namjae Jeon070fb212021-05-26 17:57:12 +09003286 struct ksmbd_dir_info *d_info,
Hyunchul Leeaf349832021-06-30 18:25:53 +09003287 struct user_namespace *user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +09003288 struct ksmbd_kstat *ksmbd_kstat)
Namjae Jeone2f34482021-03-16 10:49:09 +09003289{
3290 int next_entry_offset = 0;
3291 char *conv_name;
3292 int conv_len;
3293 void *kstat;
Namjae Jeondac0ec62021-07-07 14:57:24 +09003294 int struct_sz, rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09003295
3296 conv_name = ksmbd_convert_dir_info_name(d_info,
3297 conn->local_nls,
3298 &conv_len);
3299 if (!conv_name)
3300 return -ENOMEM;
3301
3302 /* Somehow the name has only terminating NULL bytes */
3303 if (conv_len < 0) {
Namjae Jeondac0ec62021-07-07 14:57:24 +09003304 rc = -EINVAL;
3305 goto free_conv_name;
Namjae Jeone2f34482021-03-16 10:49:09 +09003306 }
3307
3308 struct_sz = readdir_info_level_struct_sz(info_level);
3309 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3310 KSMBD_DIR_INFO_ALIGNMENT);
3311
3312 if (next_entry_offset > d_info->out_buf_len) {
3313 d_info->out_buf_len = 0;
Namjae Jeondac0ec62021-07-07 14:57:24 +09003314 rc = -ENOSPC;
3315 goto free_conv_name;
Namjae Jeone2f34482021-03-16 10:49:09 +09003316 }
3317
3318 kstat = d_info->wptr;
3319 if (info_level != FILE_NAMES_INFORMATION)
3320 kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat);
3321
3322 switch (info_level) {
3323 case FILE_FULL_DIRECTORY_INFORMATION:
3324 {
3325 struct file_full_directory_info *ffdinfo;
3326
3327 ffdinfo = (struct file_full_directory_info *)kstat;
3328 ffdinfo->FileNameLength = cpu_to_le32(conv_len);
3329 ffdinfo->EaSize =
3330 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3331 if (ffdinfo->EaSize)
3332 ffdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3333 if (d_info->hide_dot_file && d_info->name[0] == '.')
3334 ffdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3335 memcpy(ffdinfo->FileName, conv_name, conv_len);
3336 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3337 break;
3338 }
3339 case FILE_BOTH_DIRECTORY_INFORMATION:
3340 {
3341 struct file_both_directory_info *fbdinfo;
3342
3343 fbdinfo = (struct file_both_directory_info *)kstat;
3344 fbdinfo->FileNameLength = cpu_to_le32(conv_len);
3345 fbdinfo->EaSize =
3346 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3347 if (fbdinfo->EaSize)
3348 fbdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3349 fbdinfo->ShortNameLength = 0;
3350 fbdinfo->Reserved = 0;
3351 if (d_info->hide_dot_file && d_info->name[0] == '.')
3352 fbdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3353 memcpy(fbdinfo->FileName, conv_name, conv_len);
3354 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3355 break;
3356 }
3357 case FILE_DIRECTORY_INFORMATION:
3358 {
3359 struct file_directory_info *fdinfo;
3360
3361 fdinfo = (struct file_directory_info *)kstat;
3362 fdinfo->FileNameLength = cpu_to_le32(conv_len);
3363 if (d_info->hide_dot_file && d_info->name[0] == '.')
3364 fdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3365 memcpy(fdinfo->FileName, conv_name, conv_len);
3366 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3367 break;
3368 }
3369 case FILE_NAMES_INFORMATION:
3370 {
3371 struct file_names_info *fninfo;
3372
3373 fninfo = (struct file_names_info *)kstat;
3374 fninfo->FileNameLength = cpu_to_le32(conv_len);
3375 memcpy(fninfo->FileName, conv_name, conv_len);
3376 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3377 break;
3378 }
3379 case FILEID_FULL_DIRECTORY_INFORMATION:
3380 {
3381 struct file_id_full_dir_info *dinfo;
3382
3383 dinfo = (struct file_id_full_dir_info *)kstat;
3384 dinfo->FileNameLength = cpu_to_le32(conv_len);
3385 dinfo->EaSize =
3386 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3387 if (dinfo->EaSize)
3388 dinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3389 dinfo->Reserved = 0;
3390 dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3391 if (d_info->hide_dot_file && d_info->name[0] == '.')
3392 dinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3393 memcpy(dinfo->FileName, conv_name, conv_len);
3394 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3395 break;
3396 }
3397 case FILEID_BOTH_DIRECTORY_INFORMATION:
3398 {
3399 struct file_id_both_directory_info *fibdinfo;
3400
3401 fibdinfo = (struct file_id_both_directory_info *)kstat;
3402 fibdinfo->FileNameLength = cpu_to_le32(conv_len);
3403 fibdinfo->EaSize =
3404 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3405 if (fibdinfo->EaSize)
3406 fibdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3407 fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3408 fibdinfo->ShortNameLength = 0;
3409 fibdinfo->Reserved = 0;
3410 fibdinfo->Reserved2 = cpu_to_le16(0);
3411 if (d_info->hide_dot_file && d_info->name[0] == '.')
3412 fibdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3413 memcpy(fibdinfo->FileName, conv_name, conv_len);
3414 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3415 break;
3416 }
3417 case SMB_FIND_FILE_POSIX_INFO:
3418 {
3419 struct smb2_posix_info *posix_info;
3420 u64 time;
3421
3422 posix_info = (struct smb2_posix_info *)kstat;
3423 posix_info->Ignored = 0;
3424 posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
3425 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
3426 posix_info->ChangeTime = cpu_to_le64(time);
3427 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime);
3428 posix_info->LastAccessTime = cpu_to_le64(time);
3429 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime);
3430 posix_info->LastWriteTime = cpu_to_le64(time);
3431 posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size);
3432 posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9);
3433 posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev);
3434 posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink);
3435 posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode);
3436 posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino);
3437 posix_info->DosAttributes =
3438 S_ISDIR(ksmbd_kstat->kstat->mode) ? ATTR_DIRECTORY_LE : ATTR_ARCHIVE_LE;
3439 if (d_info->hide_dot_file && d_info->name[0] == '.')
3440 posix_info->DosAttributes |= ATTR_HIDDEN_LE;
Hyunchul Leeaf349832021-06-30 18:25:53 +09003441 id_to_sid(from_kuid(user_ns, ksmbd_kstat->kstat->uid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003442 SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]);
Hyunchul Leeaf349832021-06-30 18:25:53 +09003443 id_to_sid(from_kgid(user_ns, ksmbd_kstat->kstat->gid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003444 SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]);
Namjae Jeone2f34482021-03-16 10:49:09 +09003445 memcpy(posix_info->name, conv_name, conv_len);
3446 posix_info->name_len = cpu_to_le32(conv_len);
3447 posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset);
3448 break;
3449 }
3450
3451 } /* switch (info_level) */
3452
3453 d_info->last_entry_offset = d_info->data_count;
3454 d_info->data_count += next_entry_offset;
Marios Makassikise7735c82021-05-06 11:40:02 +09003455 d_info->out_buf_len -= next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003456 d_info->wptr += next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003457
3458 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003459 "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n",
3460 info_level, d_info->out_buf_len,
3461 next_entry_offset, d_info->data_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09003462
Namjae Jeondac0ec62021-07-07 14:57:24 +09003463free_conv_name:
3464 kfree(conv_name);
3465 return rc;
Namjae Jeone2f34482021-03-16 10:49:09 +09003466}
3467
3468struct smb2_query_dir_private {
3469 struct ksmbd_work *work;
3470 char *search_pattern;
3471 struct ksmbd_file *dir_fp;
3472
3473 struct ksmbd_dir_info *d_info;
3474 int info_level;
3475};
3476
3477static void lock_dir(struct ksmbd_file *dir_fp)
3478{
3479 struct dentry *dir = dir_fp->filp->f_path.dentry;
3480
3481 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
3482}
3483
3484static void unlock_dir(struct ksmbd_file *dir_fp)
3485{
3486 struct dentry *dir = dir_fp->filp->f_path.dentry;
3487
3488 inode_unlock(d_inode(dir));
3489}
3490
3491static int process_query_dir_entries(struct smb2_query_dir_private *priv)
3492{
Hyunchul Lee465d7202021-07-03 12:10:36 +09003493 struct user_namespace *user_ns = file_mnt_user_ns(priv->dir_fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003494 struct kstat kstat;
3495 struct ksmbd_kstat ksmbd_kstat;
3496 int rc;
3497 int i;
3498
3499 for (i = 0; i < priv->d_info->num_entry; i++) {
3500 struct dentry *dent;
3501
3502 if (dentry_name(priv->d_info, priv->info_level))
3503 return -EINVAL;
3504
3505 lock_dir(priv->dir_fp);
3506 dent = lookup_one_len(priv->d_info->name,
3507 priv->dir_fp->filp->f_path.dentry,
3508 priv->d_info->name_len);
3509 unlock_dir(priv->dir_fp);
3510
3511 if (IS_ERR(dent)) {
3512 ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003513 priv->d_info->name,
3514 PTR_ERR(dent));
Namjae Jeone2f34482021-03-16 10:49:09 +09003515 continue;
3516 }
3517 if (unlikely(d_is_negative(dent))) {
3518 dput(dent);
3519 ksmbd_debug(SMB, "Negative dentry `%s'\n",
3520 priv->d_info->name);
3521 continue;
3522 }
3523
3524 ksmbd_kstat.kstat = &kstat;
3525 if (priv->info_level != FILE_NAMES_INFORMATION)
3526 ksmbd_vfs_fill_dentry_attrs(priv->work,
Hyunchul Lee465d7202021-07-03 12:10:36 +09003527 user_ns,
Namjae Jeone2f34482021-03-16 10:49:09 +09003528 dent,
3529 &ksmbd_kstat);
3530
3531 rc = smb2_populate_readdir_entry(priv->work->conn,
3532 priv->info_level,
3533 priv->d_info,
Hyunchul Lee465d7202021-07-03 12:10:36 +09003534 user_ns,
Namjae Jeone2f34482021-03-16 10:49:09 +09003535 &ksmbd_kstat);
3536 dput(dent);
3537 if (rc)
3538 return rc;
3539 }
3540 return 0;
3541}
3542
3543static int reserve_populate_dentry(struct ksmbd_dir_info *d_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09003544 int info_level)
Namjae Jeone2f34482021-03-16 10:49:09 +09003545{
3546 int struct_sz;
3547 int conv_len;
3548 int next_entry_offset;
3549
3550 struct_sz = readdir_info_level_struct_sz(info_level);
3551 if (struct_sz == -EOPNOTSUPP)
3552 return -EOPNOTSUPP;
3553
3554 conv_len = (d_info->name_len + 1) * 2;
3555 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3556 KSMBD_DIR_INFO_ALIGNMENT);
3557
3558 if (next_entry_offset > d_info->out_buf_len) {
3559 d_info->out_buf_len = 0;
3560 return -ENOSPC;
3561 }
3562
3563 switch (info_level) {
3564 case FILE_FULL_DIRECTORY_INFORMATION:
3565 {
3566 struct file_full_directory_info *ffdinfo;
3567
3568 ffdinfo = (struct file_full_directory_info *)d_info->wptr;
3569 memcpy(ffdinfo->FileName, d_info->name, d_info->name_len);
3570 ffdinfo->FileName[d_info->name_len] = 0x00;
3571 ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3572 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3573 break;
3574 }
3575 case FILE_BOTH_DIRECTORY_INFORMATION:
3576 {
3577 struct file_both_directory_info *fbdinfo;
3578
3579 fbdinfo = (struct file_both_directory_info *)d_info->wptr;
3580 memcpy(fbdinfo->FileName, d_info->name, d_info->name_len);
3581 fbdinfo->FileName[d_info->name_len] = 0x00;
3582 fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3583 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3584 break;
3585 }
3586 case FILE_DIRECTORY_INFORMATION:
3587 {
3588 struct file_directory_info *fdinfo;
3589
3590 fdinfo = (struct file_directory_info *)d_info->wptr;
3591 memcpy(fdinfo->FileName, d_info->name, d_info->name_len);
3592 fdinfo->FileName[d_info->name_len] = 0x00;
3593 fdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3594 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3595 break;
3596 }
3597 case FILE_NAMES_INFORMATION:
3598 {
3599 struct file_names_info *fninfo;
3600
3601 fninfo = (struct file_names_info *)d_info->wptr;
3602 memcpy(fninfo->FileName, d_info->name, d_info->name_len);
3603 fninfo->FileName[d_info->name_len] = 0x00;
3604 fninfo->FileNameLength = cpu_to_le32(d_info->name_len);
3605 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3606 break;
3607 }
3608 case FILEID_FULL_DIRECTORY_INFORMATION:
3609 {
3610 struct file_id_full_dir_info *dinfo;
3611
3612 dinfo = (struct file_id_full_dir_info *)d_info->wptr;
3613 memcpy(dinfo->FileName, d_info->name, d_info->name_len);
3614 dinfo->FileName[d_info->name_len] = 0x00;
3615 dinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3616 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3617 break;
3618 }
3619 case FILEID_BOTH_DIRECTORY_INFORMATION:
3620 {
3621 struct file_id_both_directory_info *fibdinfo;
3622
3623 fibdinfo = (struct file_id_both_directory_info *)d_info->wptr;
3624 memcpy(fibdinfo->FileName, d_info->name, d_info->name_len);
3625 fibdinfo->FileName[d_info->name_len] = 0x00;
3626 fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3627 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3628 break;
3629 }
3630 case SMB_FIND_FILE_POSIX_INFO:
3631 {
3632 struct smb2_posix_info *posix_info;
3633
3634 posix_info = (struct smb2_posix_info *)d_info->wptr;
3635 memcpy(posix_info->name, d_info->name, d_info->name_len);
3636 posix_info->name[d_info->name_len] = 0x00;
3637 posix_info->name_len = cpu_to_le32(d_info->name_len);
3638 posix_info->NextEntryOffset =
3639 cpu_to_le32(next_entry_offset);
3640 break;
3641 }
3642 } /* switch (info_level) */
3643
3644 d_info->num_entry++;
3645 d_info->out_buf_len -= next_entry_offset;
3646 d_info->wptr += next_entry_offset;
3647 return 0;
3648}
3649
Namjae Jeon64b39f42021-03-30 14:25:35 +09003650static int __query_dir(struct dir_context *ctx, const char *name, int namlen,
Namjae Jeon070fb212021-05-26 17:57:12 +09003651 loff_t offset, u64 ino, unsigned int d_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09003652{
3653 struct ksmbd_readdir_data *buf;
3654 struct smb2_query_dir_private *priv;
3655 struct ksmbd_dir_info *d_info;
3656 int rc;
3657
3658 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
3659 priv = buf->private;
3660 d_info = priv->d_info;
3661
3662 /* dot and dotdot entries are already reserved */
3663 if (!strcmp(".", name) || !strcmp("..", name))
3664 return 0;
3665 if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name))
3666 return 0;
Namjae Jeonb24c9332021-03-21 17:32:19 +09003667 if (!match_pattern(name, namlen, priv->search_pattern))
Namjae Jeone2f34482021-03-16 10:49:09 +09003668 return 0;
3669
3670 d_info->name = name;
3671 d_info->name_len = namlen;
3672 rc = reserve_populate_dentry(d_info, priv->info_level);
3673 if (rc)
3674 return rc;
3675 if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
3676 d_info->out_buf_len = 0;
3677 return 0;
3678 }
3679 return 0;
3680}
3681
3682static void restart_ctx(struct dir_context *ctx)
3683{
3684 ctx->pos = 0;
3685}
3686
3687static int verify_info_level(int info_level)
3688{
3689 switch (info_level) {
3690 case FILE_FULL_DIRECTORY_INFORMATION:
3691 case FILE_BOTH_DIRECTORY_INFORMATION:
3692 case FILE_DIRECTORY_INFORMATION:
3693 case FILE_NAMES_INFORMATION:
3694 case FILEID_FULL_DIRECTORY_INFORMATION:
3695 case FILEID_BOTH_DIRECTORY_INFORMATION:
3696 case SMB_FIND_FILE_POSIX_INFO:
3697 break;
3698 default:
3699 return -EOPNOTSUPP;
3700 }
3701
3702 return 0;
3703}
3704
3705int smb2_query_dir(struct ksmbd_work *work)
3706{
3707 struct ksmbd_conn *conn = work->conn;
3708 struct smb2_query_directory_req *req;
3709 struct smb2_query_directory_rsp *rsp, *rsp_org;
3710 struct ksmbd_share_config *share = work->tcon->share_conf;
3711 struct ksmbd_file *dir_fp = NULL;
3712 struct ksmbd_dir_info d_info;
3713 int rc = 0;
3714 char *srch_ptr = NULL;
3715 unsigned char srch_flag;
3716 int buffer_sz;
3717 struct smb2_query_dir_private query_dir_private = {NULL, };
3718
Namjae Jeone5066492021-03-30 12:35:23 +09003719 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09003720 WORK_BUFFERS(work, req, rsp);
3721
3722 if (ksmbd_override_fsids(work)) {
3723 rsp->hdr.Status = STATUS_NO_MEMORY;
3724 smb2_set_err_rsp(work);
3725 return -ENOMEM;
3726 }
3727
3728 rc = verify_info_level(req->FileInformationClass);
3729 if (rc) {
3730 rc = -EFAULT;
3731 goto err_out2;
3732 }
3733
3734 dir_fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09003735 le64_to_cpu(req->VolatileFileId),
3736 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09003737 if (!dir_fp) {
3738 rc = -EBADF;
3739 goto err_out2;
3740 }
3741
3742 if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) ||
Hyunchul Leeaf349832021-06-30 18:25:53 +09003743 inode_permission(file_mnt_user_ns(dir_fp->filp),
3744 file_inode(dir_fp->filp),
Namjae Jeon070fb212021-05-26 17:57:12 +09003745 MAY_READ | MAY_EXEC)) {
Namjae Jeon493fa2f2021-06-29 09:22:16 +09003746 pr_err("no right to enumerate directory (%pd)\n",
3747 dir_fp->filp->f_path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003748 rc = -EACCES;
3749 goto err_out2;
3750 }
3751
3752 if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09003753 pr_err("can't do query dir for a file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09003754 rc = -EINVAL;
3755 goto err_out2;
3756 }
3757
3758 srch_flag = req->Flags;
3759 srch_ptr = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09003760 le16_to_cpu(req->FileNameLength), 1,
3761 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09003762 if (IS_ERR(srch_ptr)) {
3763 ksmbd_debug(SMB, "Search Pattern not found\n");
3764 rc = -EINVAL;
3765 goto err_out2;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003766 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003767 ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr);
Namjae Jeon64b39f42021-03-30 14:25:35 +09003768 }
Namjae Jeone2f34482021-03-16 10:49:09 +09003769
3770 ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename);
3771
3772 if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) {
3773 ksmbd_debug(SMB, "Restart directory scan\n");
3774 generic_file_llseek(dir_fp->filp, 0, SEEK_SET);
3775 restart_ctx(&dir_fp->readdir_data.ctx);
3776 }
3777
3778 memset(&d_info, 0, sizeof(struct ksmbd_dir_info));
3779 d_info.wptr = (char *)rsp->Buffer;
3780 d_info.rptr = (char *)rsp->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003781 d_info.out_buf_len = (work->response_sz - (get_rfc1002_len(rsp_org) + 4));
Namjae Jeon070fb212021-05-26 17:57:12 +09003782 d_info.out_buf_len = min_t(int, d_info.out_buf_len, le32_to_cpu(req->OutputBufferLength)) -
3783 sizeof(struct smb2_query_directory_rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003784 d_info.flags = srch_flag;
3785
3786 /*
3787 * reserve dot and dotdot entries in head of buffer
3788 * in first response
3789 */
3790 rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass,
Namjae Jeon070fb212021-05-26 17:57:12 +09003791 dir_fp, &d_info, srch_ptr,
3792 smb2_populate_readdir_entry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003793 if (rc == -ENOSPC)
3794 rc = 0;
3795 else if (rc)
3796 goto err_out;
3797
3798 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES))
3799 d_info.hide_dot_file = true;
3800
3801 buffer_sz = d_info.out_buf_len;
3802 d_info.rptr = d_info.wptr;
3803 query_dir_private.work = work;
3804 query_dir_private.search_pattern = srch_ptr;
3805 query_dir_private.dir_fp = dir_fp;
3806 query_dir_private.d_info = &d_info;
3807 query_dir_private.info_level = req->FileInformationClass;
3808 dir_fp->readdir_data.private = &query_dir_private;
3809 set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir);
3810
Namjae Jeone8c06192021-06-22 11:06:11 +09003811 rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx);
Namjae Jeone2f34482021-03-16 10:49:09 +09003812 if (rc == 0)
3813 restart_ctx(&dir_fp->readdir_data.ctx);
3814 if (rc == -ENOSPC)
3815 rc = 0;
3816 if (rc)
3817 goto err_out;
3818
3819 d_info.wptr = d_info.rptr;
3820 d_info.out_buf_len = buffer_sz;
3821 rc = process_query_dir_entries(&query_dir_private);
3822 if (rc)
3823 goto err_out;
3824
3825 if (!d_info.data_count && d_info.out_buf_len >= 0) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09003826 if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003827 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003828 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003829 dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0;
3830 rsp->hdr.Status = STATUS_NO_MORE_FILES;
3831 }
3832 rsp->StructureSize = cpu_to_le16(9);
3833 rsp->OutputBufferOffset = cpu_to_le16(0);
3834 rsp->OutputBufferLength = cpu_to_le32(0);
3835 rsp->Buffer[0] = 0;
3836 inc_rfc1001_len(rsp_org, 9);
3837 } else {
3838 ((struct file_directory_info *)
3839 ((char *)rsp->Buffer + d_info.last_entry_offset))
3840 ->NextEntryOffset = 0;
3841
3842 rsp->StructureSize = cpu_to_le16(9);
3843 rsp->OutputBufferOffset = cpu_to_le16(72);
3844 rsp->OutputBufferLength = cpu_to_le32(d_info.data_count);
3845 inc_rfc1001_len(rsp_org, 8 + d_info.data_count);
3846 }
3847
3848 kfree(srch_ptr);
3849 ksmbd_fd_put(work, dir_fp);
3850 ksmbd_revert_fsids(work);
3851 return 0;
3852
3853err_out:
Namjae Jeonbde16942021-06-28 15:23:19 +09003854 pr_err("error while processing smb2 query dir rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09003855 kfree(srch_ptr);
3856
3857err_out2:
3858 if (rc == -EINVAL)
3859 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3860 else if (rc == -EACCES)
3861 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3862 else if (rc == -ENOENT)
3863 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
3864 else if (rc == -EBADF)
3865 rsp->hdr.Status = STATUS_FILE_CLOSED;
3866 else if (rc == -ENOMEM)
3867 rsp->hdr.Status = STATUS_NO_MEMORY;
3868 else if (rc == -EFAULT)
3869 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
3870 if (!rsp->hdr.Status)
3871 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3872
3873 smb2_set_err_rsp(work);
3874 ksmbd_fd_put(work, dir_fp);
3875 ksmbd_revert_fsids(work);
3876 return 0;
3877}
3878
3879/**
3880 * buffer_check_err() - helper function to check buffer errors
3881 * @reqOutputBufferLength: max buffer length expected in command response
3882 * @rsp: query info response buffer contains output buffer length
3883 * @infoclass_size: query info class response buffer size
3884 *
3885 * Return: 0 on success, otherwise error
3886 */
3887static int buffer_check_err(int reqOutputBufferLength,
Namjae Jeon070fb212021-05-26 17:57:12 +09003888 struct smb2_query_info_rsp *rsp, int infoclass_size)
Namjae Jeone2f34482021-03-16 10:49:09 +09003889{
3890 if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) {
3891 if (reqOutputBufferLength < infoclass_size) {
Namjae Jeonbde16942021-06-28 15:23:19 +09003892 pr_err("Invalid Buffer Size Requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09003893 rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003894 rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09003895 return -EINVAL;
3896 }
3897
3898 ksmbd_debug(SMB, "Buffer Overflow\n");
3899 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003900 rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4 +
3901 reqOutputBufferLength);
3902 rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09003903 }
3904 return 0;
3905}
3906
3907static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp)
3908{
3909 struct smb2_file_standard_info *sinfo;
3910
3911 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
3912
3913 sinfo->AllocationSize = cpu_to_le64(4096);
3914 sinfo->EndOfFile = cpu_to_le64(0);
3915 sinfo->NumberOfLinks = cpu_to_le32(1);
3916 sinfo->DeletePending = 1;
3917 sinfo->Directory = 0;
3918 rsp->OutputBufferLength =
3919 cpu_to_le32(sizeof(struct smb2_file_standard_info));
3920 inc_rfc1001_len(rsp, sizeof(struct smb2_file_standard_info));
3921}
3922
Namjae Jeon070fb212021-05-26 17:57:12 +09003923static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num)
Namjae Jeone2f34482021-03-16 10:49:09 +09003924{
3925 struct smb2_file_internal_info *file_info;
3926
3927 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
3928
3929 /* any unique number */
3930 file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63));
3931 rsp->OutputBufferLength =
3932 cpu_to_le32(sizeof(struct smb2_file_internal_info));
3933 inc_rfc1001_len(rsp, sizeof(struct smb2_file_internal_info));
3934}
3935
Namjae Jeone2f34482021-03-16 10:49:09 +09003936static int smb2_get_info_file_pipe(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09003937 struct smb2_query_info_req *req,
3938 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09003939{
Namjae Jeon64b39f42021-03-30 14:25:35 +09003940 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09003941 int rc;
3942
3943 /*
3944 * Windows can sometime send query file info request on
3945 * pipe without opening it, checking error condition here
3946 */
3947 id = le64_to_cpu(req->VolatileFileId);
3948 if (!ksmbd_session_rpc_method(sess, id))
3949 return -ENOENT;
3950
3951 ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003952 req->FileInfoClass, le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09003953
3954 switch (req->FileInfoClass) {
3955 case FILE_STANDARD_INFORMATION:
3956 get_standard_info_pipe(rsp);
3957 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09003958 rsp, FILE_STANDARD_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09003959 break;
3960 case FILE_INTERNAL_INFORMATION:
3961 get_internal_info_pipe(rsp, id);
3962 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09003963 rsp, FILE_INTERNAL_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09003964 break;
3965 default:
3966 ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003967 req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09003968 rc = -EOPNOTSUPP;
3969 }
3970 return rc;
3971}
3972
3973/**
3974 * smb2_get_ea() - handler for smb2 get extended attribute command
3975 * @work: smb work containing query info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09003976 * @fp: ksmbd_file pointer
3977 * @req: get extended attribute request
3978 * @rsp: response buffer pointer
3979 * @rsp_org: base response buffer pointer in case of chained response
Namjae Jeone2f34482021-03-16 10:49:09 +09003980 *
3981 * Return: 0 on success, otherwise error
3982 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003983static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09003984 struct smb2_query_info_req *req,
3985 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09003986{
3987 struct smb2_ea_info *eainfo, *prev_eainfo;
3988 char *name, *ptr, *xattr_list = NULL, *buf;
3989 int rc, name_len, value_len, xattr_list_len, idx;
3990 ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0;
3991 struct smb2_ea_info_req *ea_req = NULL;
3992 struct path *path;
Hyunchul Lee465d7202021-07-03 12:10:36 +09003993 struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003994
3995 if (!(fp->daccess & FILE_READ_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09003996 pr_err("Not permitted to read ext attr : 0x%x\n",
3997 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09003998 return -EACCES;
3999 }
4000
4001 path = &fp->filp->f_path;
4002 /* single EA entry is requested with given user.* name */
Namjae Jeon64b39f42021-03-30 14:25:35 +09004003 if (req->InputBufferLength) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004004 ea_req = (struct smb2_ea_info_req *)req->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004005 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09004006 /* need to send all EAs, if no specific EA is requested*/
4007 if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY)
4008 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09004009 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n",
4010 le32_to_cpu(req->Flags));
Namjae Jeone2f34482021-03-16 10:49:09 +09004011 }
4012
4013 buf_free_len = work->response_sz -
4014 (get_rfc1002_len(rsp_org) + 4) -
4015 sizeof(struct smb2_query_info_rsp);
4016
4017 if (le32_to_cpu(req->OutputBufferLength) < buf_free_len)
4018 buf_free_len = le32_to_cpu(req->OutputBufferLength);
4019
4020 rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4021 if (rc < 0) {
4022 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4023 goto out;
4024 } else if (!rc) { /* there is no EA in the file */
4025 ksmbd_debug(SMB, "no ea data in the file\n");
4026 goto done;
4027 }
4028 xattr_list_len = rc;
4029
4030 ptr = (char *)rsp->Buffer;
4031 eainfo = (struct smb2_ea_info *)ptr;
4032 prev_eainfo = eainfo;
4033 idx = 0;
4034
4035 while (idx < xattr_list_len) {
4036 name = xattr_list + idx;
4037 name_len = strlen(name);
4038
4039 ksmbd_debug(SMB, "%s, len %d\n", name, name_len);
4040 idx += name_len + 1;
4041
4042 /*
4043 * CIFS does not support EA other than user.* namespace,
4044 * still keep the framework generic, to list other attrs
4045 * in future.
4046 */
4047 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4048 continue;
4049
4050 if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004051 STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004052 continue;
4053
4054 if (req->InputBufferLength &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09004055 strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name,
4056 ea_req->EaNameLength))
Namjae Jeone2f34482021-03-16 10:49:09 +09004057 continue;
4058
4059 if (!strncmp(&name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004060 DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004061 continue;
4062
4063 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4064 name_len -= XATTR_USER_PREFIX_LEN;
4065
4066 ptr = (char *)(&eainfo->name + name_len + 1);
4067 buf_free_len -= (offsetof(struct smb2_ea_info, name) +
4068 name_len + 1);
4069 /* bailout if xattr can't fit in buf_free_len */
Hyunchul Lee465d7202021-07-03 12:10:36 +09004070 value_len = ksmbd_vfs_getxattr(user_ns, path->dentry,
4071 name, &buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004072 if (value_len <= 0) {
4073 rc = -ENOENT;
4074 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4075 goto out;
4076 }
4077
4078 buf_free_len -= value_len;
4079 if (buf_free_len < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09004080 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004081 break;
4082 }
4083
4084 memcpy(ptr, buf, value_len);
Namjae Jeon79f6b112021-04-02 12:47:14 +09004085 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004086
4087 ptr += value_len;
4088 eainfo->Flags = 0;
4089 eainfo->EaNameLength = name_len;
4090
Namjae Jeon64b39f42021-03-30 14:25:35 +09004091 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004092 memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN],
Namjae Jeon070fb212021-05-26 17:57:12 +09004093 name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09004094 else
4095 memcpy(eainfo->name, name, name_len);
4096
4097 eainfo->name[name_len] = '\0';
4098 eainfo->EaValueLength = cpu_to_le16(value_len);
4099 next_offset = offsetof(struct smb2_ea_info, name) +
4100 name_len + 1 + value_len;
4101
4102 /* align next xattr entry at 4 byte bundary */
4103 alignment_bytes = ((next_offset + 3) & ~3) - next_offset;
4104 if (alignment_bytes) {
4105 memset(ptr, '\0', alignment_bytes);
4106 ptr += alignment_bytes;
4107 next_offset += alignment_bytes;
4108 buf_free_len -= alignment_bytes;
4109 }
4110 eainfo->NextEntryOffset = cpu_to_le32(next_offset);
4111 prev_eainfo = eainfo;
4112 eainfo = (struct smb2_ea_info *)ptr;
4113 rsp_data_cnt += next_offset;
4114
4115 if (req->InputBufferLength) {
4116 ksmbd_debug(SMB, "single entry requested\n");
4117 break;
4118 }
4119 }
4120
4121 /* no more ea entries */
4122 prev_eainfo->NextEntryOffset = 0;
4123done:
4124 rc = 0;
4125 if (rsp_data_cnt == 0)
4126 rsp->hdr.Status = STATUS_NO_EAS_ON_FILE;
4127 rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt);
4128 inc_rfc1001_len(rsp_org, rsp_data_cnt);
4129out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004130 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004131 return rc;
4132}
4133
4134static void get_file_access_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004135 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004136{
4137 struct smb2_file_access_info *file_info;
4138
4139 file_info = (struct smb2_file_access_info *)rsp->Buffer;
4140 file_info->AccessFlags = fp->daccess;
4141 rsp->OutputBufferLength =
4142 cpu_to_le32(sizeof(struct smb2_file_access_info));
4143 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info));
4144}
4145
4146static int get_file_basic_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_all_info *basic_info;
4150 struct kstat stat;
4151 u64 time;
4152
4153 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004154 pr_err("no right to read the attributes : 0x%x\n",
4155 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004156 return -EACCES;
4157 }
4158
4159 basic_info = (struct smb2_file_all_info *)rsp->Buffer;
Hyunchul Leeaf349832021-06-30 18:25:53 +09004160 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4161 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004162 basic_info->CreationTime = cpu_to_le64(fp->create_time);
4163 time = ksmbd_UnixTimeToNT(stat.atime);
4164 basic_info->LastAccessTime = cpu_to_le64(time);
4165 time = ksmbd_UnixTimeToNT(stat.mtime);
4166 basic_info->LastWriteTime = cpu_to_le64(time);
4167 time = ksmbd_UnixTimeToNT(stat.ctime);
4168 basic_info->ChangeTime = cpu_to_le64(time);
4169 basic_info->Attributes = fp->f_ci->m_fattr;
4170 basic_info->Pad1 = 0;
4171 rsp->OutputBufferLength =
Namjae Jeon64b39f42021-03-30 14:25:35 +09004172 cpu_to_le32(offsetof(struct smb2_file_all_info, AllocationSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09004173 inc_rfc1001_len(rsp_org, offsetof(struct smb2_file_all_info,
4174 AllocationSize));
4175 return 0;
4176}
4177
4178static unsigned long long get_allocation_size(struct inode *inode,
Namjae Jeon070fb212021-05-26 17:57:12 +09004179 struct kstat *stat)
Namjae Jeone2f34482021-03-16 10:49:09 +09004180{
4181 unsigned long long alloc_size = 0;
4182
4183 if (!S_ISDIR(stat->mode)) {
4184 if ((inode->i_blocks << 9) <= stat->size)
4185 alloc_size = stat->size;
4186 else
4187 alloc_size = inode->i_blocks << 9;
Namjae Jeone2f34482021-03-16 10:49:09 +09004188 }
4189
4190 return alloc_size;
4191}
4192
4193static void get_file_standard_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004194 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004195{
4196 struct smb2_file_standard_info *sinfo;
4197 unsigned int delete_pending;
4198 struct inode *inode;
4199 struct kstat stat;
4200
Namjae Jeonab0b2632021-06-29 09:20:13 +09004201 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004202 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004203
4204 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4205 delete_pending = ksmbd_inode_pending_delete(fp);
4206
4207 sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat));
4208 sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4209 sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending);
4210 sinfo->DeletePending = delete_pending;
4211 sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4212 rsp->OutputBufferLength =
4213 cpu_to_le32(sizeof(struct smb2_file_standard_info));
4214 inc_rfc1001_len(rsp_org,
4215 sizeof(struct smb2_file_standard_info));
4216}
4217
4218static void get_file_alignment_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004219 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004220{
4221 struct smb2_file_alignment_info *file_info;
4222
4223 file_info = (struct smb2_file_alignment_info *)rsp->Buffer;
4224 file_info->AlignmentRequirement = 0;
4225 rsp->OutputBufferLength =
4226 cpu_to_le32(sizeof(struct smb2_file_alignment_info));
4227 inc_rfc1001_len(rsp_org,
4228 sizeof(struct smb2_file_alignment_info));
4229}
4230
4231static int get_file_all_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004232 struct smb2_query_info_rsp *rsp,
4233 struct ksmbd_file *fp,
4234 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004235{
4236 struct ksmbd_conn *conn = work->conn;
4237 struct smb2_file_all_info *file_info;
4238 unsigned int delete_pending;
4239 struct inode *inode;
4240 struct kstat stat;
4241 int conv_len;
4242 char *filename;
4243 u64 time;
4244
4245 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4246 ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004247 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004248 return -EACCES;
4249 }
4250
4251 filename = convert_to_nt_pathname(fp->filename,
4252 work->tcon->share_conf->path);
4253 if (!filename)
4254 return -ENOMEM;
4255
Namjae Jeonab0b2632021-06-29 09:20:13 +09004256 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004257 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004258
4259 ksmbd_debug(SMB, "filename = %s\n", filename);
4260 delete_pending = ksmbd_inode_pending_delete(fp);
4261 file_info = (struct smb2_file_all_info *)rsp->Buffer;
4262
4263 file_info->CreationTime = cpu_to_le64(fp->create_time);
4264 time = ksmbd_UnixTimeToNT(stat.atime);
4265 file_info->LastAccessTime = cpu_to_le64(time);
4266 time = ksmbd_UnixTimeToNT(stat.mtime);
4267 file_info->LastWriteTime = cpu_to_le64(time);
4268 time = ksmbd_UnixTimeToNT(stat.ctime);
4269 file_info->ChangeTime = cpu_to_le64(time);
4270 file_info->Attributes = fp->f_ci->m_fattr;
4271 file_info->Pad1 = 0;
4272 file_info->AllocationSize =
4273 cpu_to_le64(get_allocation_size(inode, &stat));
4274 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4275 file_info->NumberOfLinks =
4276 cpu_to_le32(get_nlink(&stat) - delete_pending);
4277 file_info->DeletePending = delete_pending;
4278 file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4279 file_info->Pad2 = 0;
4280 file_info->IndexNumber = cpu_to_le64(stat.ino);
4281 file_info->EASize = 0;
4282 file_info->AccessFlags = fp->daccess;
4283 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4284 file_info->Mode = fp->coption;
4285 file_info->AlignmentRequirement = 0;
Namjae Jeon070fb212021-05-26 17:57:12 +09004286 conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename,
4287 PATH_MAX, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004288 conv_len *= 2;
4289 file_info->FileNameLength = cpu_to_le32(conv_len);
4290 rsp->OutputBufferLength =
4291 cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1);
4292 kfree(filename);
4293 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4294 return 0;
4295}
4296
4297static void get_file_alternate_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004298 struct smb2_query_info_rsp *rsp,
4299 struct ksmbd_file *fp,
4300 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004301{
4302 struct ksmbd_conn *conn = work->conn;
4303 struct smb2_file_alt_name_info *file_info;
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004304 struct dentry *dentry = fp->filp->f_path.dentry;
Namjae Jeone2f34482021-03-16 10:49:09 +09004305 int conv_len;
Namjae Jeone2f34482021-03-16 10:49:09 +09004306
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004307 spin_lock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004308 file_info = (struct smb2_file_alt_name_info *)rsp->Buffer;
4309 conv_len = ksmbd_extract_shortname(conn,
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004310 dentry->d_name.name,
Namjae Jeone2f34482021-03-16 10:49:09 +09004311 file_info->FileName);
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004312 spin_unlock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004313 file_info->FileNameLength = cpu_to_le32(conv_len);
4314 rsp->OutputBufferLength =
4315 cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len);
4316 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4317}
4318
4319static void get_file_stream_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004320 struct smb2_query_info_rsp *rsp,
4321 struct ksmbd_file *fp,
4322 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004323{
4324 struct ksmbd_conn *conn = work->conn;
4325 struct smb2_file_stream_info *file_info;
4326 char *stream_name, *xattr_list = NULL, *stream_buf;
4327 struct kstat stat;
4328 struct path *path = &fp->filp->f_path;
4329 ssize_t xattr_list_len;
4330 int nbytes = 0, streamlen, stream_name_len, next, idx = 0;
4331
Hyunchul Leeaf349832021-06-30 18:25:53 +09004332 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4333 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004334 file_info = (struct smb2_file_stream_info *)rsp->Buffer;
4335
4336 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4337 if (xattr_list_len < 0) {
4338 goto out;
4339 } else if (!xattr_list_len) {
4340 ksmbd_debug(SMB, "empty xattr in the file\n");
4341 goto out;
4342 }
4343
4344 while (idx < xattr_list_len) {
4345 stream_name = xattr_list + idx;
4346 streamlen = strlen(stream_name);
4347 idx += streamlen + 1;
4348
4349 ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen);
4350
4351 if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004352 STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004353 continue;
4354
4355 stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN +
4356 STREAM_PREFIX_LEN);
4357 streamlen = stream_name_len;
4358
4359 /* plus : size */
4360 streamlen += 1;
4361 stream_buf = kmalloc(streamlen + 1, GFP_KERNEL);
4362 if (!stream_buf)
4363 break;
4364
4365 streamlen = snprintf(stream_buf, streamlen + 1,
Namjae Jeon070fb212021-05-26 17:57:12 +09004366 ":%s", &stream_name[XATTR_NAME_STREAM_LEN]);
Namjae Jeone2f34482021-03-16 10:49:09 +09004367
Namjae Jeon070fb212021-05-26 17:57:12 +09004368 file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes];
Namjae Jeone2f34482021-03-16 10:49:09 +09004369 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004370 stream_buf, streamlen,
4371 conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004372 streamlen *= 2;
4373 kfree(stream_buf);
4374 file_info->StreamNameLength = cpu_to_le32(streamlen);
4375 file_info->StreamSize = cpu_to_le64(stream_name_len);
4376 file_info->StreamAllocationSize = cpu_to_le64(stream_name_len);
4377
4378 next = sizeof(struct smb2_file_stream_info) + streamlen;
4379 nbytes += next;
4380 file_info->NextEntryOffset = cpu_to_le32(next);
4381 }
4382
4383 if (nbytes) {
4384 file_info = (struct smb2_file_stream_info *)
4385 &rsp->Buffer[nbytes];
4386 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004387 "::$DATA", 7, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004388 streamlen *= 2;
4389 file_info->StreamNameLength = cpu_to_le32(streamlen);
4390 file_info->StreamSize = S_ISDIR(stat.mode) ? 0 :
4391 cpu_to_le64(stat.size);
4392 file_info->StreamAllocationSize = S_ISDIR(stat.mode) ? 0 :
4393 cpu_to_le64(stat.size);
4394 nbytes += sizeof(struct smb2_file_stream_info) + streamlen;
4395 }
4396
4397 /* last entry offset should be 0 */
4398 file_info->NextEntryOffset = 0;
4399out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004400 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004401
4402 rsp->OutputBufferLength = cpu_to_le32(nbytes);
4403 inc_rfc1001_len(rsp_org, nbytes);
4404}
4405
4406static void get_file_internal_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004407 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004408{
4409 struct smb2_file_internal_info *file_info;
4410 struct kstat stat;
4411
Hyunchul Leeaf349832021-06-30 18:25:53 +09004412 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4413 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004414 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4415 file_info->IndexNumber = cpu_to_le64(stat.ino);
4416 rsp->OutputBufferLength =
4417 cpu_to_le32(sizeof(struct smb2_file_internal_info));
4418 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
4419}
4420
4421static int get_file_network_open_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004422 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004423{
4424 struct smb2_file_ntwrk_info *file_info;
4425 struct inode *inode;
4426 struct kstat stat;
4427 u64 time;
4428
4429 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004430 pr_err("no right to read the attributes : 0x%x\n",
4431 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004432 return -EACCES;
4433 }
4434
4435 file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer;
4436
Namjae Jeonab0b2632021-06-29 09:20:13 +09004437 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004438 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004439
4440 file_info->CreationTime = cpu_to_le64(fp->create_time);
4441 time = ksmbd_UnixTimeToNT(stat.atime);
4442 file_info->LastAccessTime = cpu_to_le64(time);
4443 time = ksmbd_UnixTimeToNT(stat.mtime);
4444 file_info->LastWriteTime = cpu_to_le64(time);
4445 time = ksmbd_UnixTimeToNT(stat.ctime);
4446 file_info->ChangeTime = cpu_to_le64(time);
4447 file_info->Attributes = fp->f_ci->m_fattr;
4448 file_info->AllocationSize =
4449 cpu_to_le64(get_allocation_size(inode, &stat));
4450 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4451 file_info->Reserved = cpu_to_le32(0);
4452 rsp->OutputBufferLength =
4453 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info));
4454 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info));
4455 return 0;
4456}
4457
Namjae Jeon64b39f42021-03-30 14:25:35 +09004458static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004459{
4460 struct smb2_file_ea_info *file_info;
4461
4462 file_info = (struct smb2_file_ea_info *)rsp->Buffer;
4463 file_info->EASize = 0;
4464 rsp->OutputBufferLength =
4465 cpu_to_le32(sizeof(struct smb2_file_ea_info));
4466 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info));
4467}
4468
4469static void get_file_position_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004470 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004471{
4472 struct smb2_file_pos_info *file_info;
4473
4474 file_info = (struct smb2_file_pos_info *)rsp->Buffer;
4475 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4476 rsp->OutputBufferLength =
4477 cpu_to_le32(sizeof(struct smb2_file_pos_info));
4478 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info));
4479}
4480
4481static void get_file_mode_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004482 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004483{
4484 struct smb2_file_mode_info *file_info;
4485
4486 file_info = (struct smb2_file_mode_info *)rsp->Buffer;
4487 file_info->Mode = fp->coption & FILE_MODE_INFO_MASK;
4488 rsp->OutputBufferLength =
4489 cpu_to_le32(sizeof(struct smb2_file_mode_info));
4490 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info));
4491}
4492
4493static void get_file_compression_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004494 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004495{
4496 struct smb2_file_comp_info *file_info;
4497 struct kstat stat;
4498
Hyunchul Leeaf349832021-06-30 18:25:53 +09004499 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4500 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004501
4502 file_info = (struct smb2_file_comp_info *)rsp->Buffer;
4503 file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9);
4504 file_info->CompressionFormat = COMPRESSION_FORMAT_NONE;
4505 file_info->CompressionUnitShift = 0;
4506 file_info->ChunkShift = 0;
4507 file_info->ClusterShift = 0;
4508 memset(&file_info->Reserved[0], 0, 3);
4509
4510 rsp->OutputBufferLength =
4511 cpu_to_le32(sizeof(struct smb2_file_comp_info));
4512 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info));
4513}
4514
4515static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004516 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004517{
4518 struct smb2_file_attr_tag_info *file_info;
4519
4520 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004521 pr_err("no right to read the attributes : 0x%x\n",
4522 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004523 return -EACCES;
4524 }
4525
4526 file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer;
4527 file_info->FileAttributes = fp->f_ci->m_fattr;
4528 file_info->ReparseTag = 0;
4529 rsp->OutputBufferLength =
4530 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info));
Namjae Jeon070fb212021-05-26 17:57:12 +09004531 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004532 return 0;
4533}
4534
4535static int find_file_posix_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004536 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004537{
4538 struct smb311_posix_qinfo *file_info;
Namjae Jeonab0b2632021-06-29 09:20:13 +09004539 struct inode *inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09004540 u64 time;
4541
4542 file_info = (struct smb311_posix_qinfo *)rsp->Buffer;
4543 file_info->CreationTime = cpu_to_le64(fp->create_time);
4544 time = ksmbd_UnixTimeToNT(inode->i_atime);
4545 file_info->LastAccessTime = cpu_to_le64(time);
4546 time = ksmbd_UnixTimeToNT(inode->i_mtime);
4547 file_info->LastWriteTime = cpu_to_le64(time);
4548 time = ksmbd_UnixTimeToNT(inode->i_ctime);
4549 file_info->ChangeTime = cpu_to_le64(time);
4550 file_info->DosAttributes = fp->f_ci->m_fattr;
4551 file_info->Inode = cpu_to_le64(inode->i_ino);
4552 file_info->EndOfFile = cpu_to_le64(inode->i_size);
4553 file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9);
4554 file_info->HardLinks = cpu_to_le32(inode->i_nlink);
4555 file_info->Mode = cpu_to_le32(inode->i_mode);
4556 file_info->DeviceId = cpu_to_le32(inode->i_rdev);
4557 rsp->OutputBufferLength =
4558 cpu_to_le32(sizeof(struct smb311_posix_qinfo));
Namjae Jeon64b39f42021-03-30 14:25:35 +09004559 inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo));
Namjae Jeone2f34482021-03-16 10:49:09 +09004560 return 0;
4561}
4562
Namjae Jeone2f34482021-03-16 10:49:09 +09004563static int smb2_get_info_file(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004564 struct smb2_query_info_req *req,
4565 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004566{
4567 struct ksmbd_file *fp;
4568 int fileinfoclass = 0;
4569 int rc = 0;
4570 int file_infoclass_size;
4571 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4572
4573 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004574 KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004575 /* smb2 info file called for pipe */
4576 return smb2_get_info_file_pipe(work->sess, req, rsp);
4577 }
4578
4579 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon38673692021-07-08 12:32:27 +09004580 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
4581 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004582 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004583 id = work->compound_fid;
4584 pid = work->compound_pfid;
4585 }
4586 }
4587
Namjae Jeon38673692021-07-08 12:32:27 +09004588 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004589 id = le64_to_cpu(req->VolatileFileId);
4590 pid = le64_to_cpu(req->PersistentFileId);
4591 }
4592
4593 fp = ksmbd_lookup_fd_slow(work, id, pid);
4594 if (!fp)
4595 return -ENOENT;
4596
4597 fileinfoclass = req->FileInfoClass;
4598
4599 switch (fileinfoclass) {
4600 case FILE_ACCESS_INFORMATION:
4601 get_file_access_info(rsp, fp, rsp_org);
4602 file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE;
4603 break;
4604
4605 case FILE_BASIC_INFORMATION:
4606 rc = get_file_basic_info(rsp, fp, rsp_org);
4607 file_infoclass_size = FILE_BASIC_INFORMATION_SIZE;
4608 break;
4609
4610 case FILE_STANDARD_INFORMATION:
4611 get_file_standard_info(rsp, fp, rsp_org);
4612 file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE;
4613 break;
4614
4615 case FILE_ALIGNMENT_INFORMATION:
4616 get_file_alignment_info(rsp, rsp_org);
4617 file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE;
4618 break;
4619
4620 case FILE_ALL_INFORMATION:
4621 rc = get_file_all_info(work, rsp, fp, rsp_org);
4622 file_infoclass_size = FILE_ALL_INFORMATION_SIZE;
4623 break;
4624
4625 case FILE_ALTERNATE_NAME_INFORMATION:
4626 get_file_alternate_info(work, rsp, fp, rsp_org);
4627 file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE;
4628 break;
4629
4630 case FILE_STREAM_INFORMATION:
4631 get_file_stream_info(work, rsp, fp, rsp_org);
4632 file_infoclass_size = FILE_STREAM_INFORMATION_SIZE;
4633 break;
4634
4635 case FILE_INTERNAL_INFORMATION:
4636 get_file_internal_info(rsp, fp, rsp_org);
4637 file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE;
4638 break;
4639
4640 case FILE_NETWORK_OPEN_INFORMATION:
4641 rc = get_file_network_open_info(rsp, fp, rsp_org);
4642 file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE;
4643 break;
4644
4645 case FILE_EA_INFORMATION:
4646 get_file_ea_info(rsp, rsp_org);
4647 file_infoclass_size = FILE_EA_INFORMATION_SIZE;
4648 break;
4649
4650 case FILE_FULL_EA_INFORMATION:
4651 rc = smb2_get_ea(work, fp, req, rsp, rsp_org);
4652 file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE;
4653 break;
4654
4655 case FILE_POSITION_INFORMATION:
4656 get_file_position_info(rsp, fp, rsp_org);
4657 file_infoclass_size = FILE_POSITION_INFORMATION_SIZE;
4658 break;
4659
4660 case FILE_MODE_INFORMATION:
4661 get_file_mode_info(rsp, fp, rsp_org);
4662 file_infoclass_size = FILE_MODE_INFORMATION_SIZE;
4663 break;
4664
4665 case FILE_COMPRESSION_INFORMATION:
4666 get_file_compression_info(rsp, fp, rsp_org);
4667 file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE;
4668 break;
4669
4670 case FILE_ATTRIBUTE_TAG_INFORMATION:
4671 rc = get_file_attribute_tag_info(rsp, fp, rsp_org);
4672 file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE;
4673 break;
4674 case SMB_FIND_FILE_POSIX_INFO:
4675 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004676 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004677 rc = -EOPNOTSUPP;
4678 } else {
4679 rc = find_file_posix_info(rsp, fp, rsp_org);
4680 file_infoclass_size = sizeof(struct smb311_posix_qinfo);
4681 }
4682 break;
4683 default:
4684 ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n",
4685 fileinfoclass);
4686 rc = -EOPNOTSUPP;
4687 }
4688 if (!rc)
4689 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4690 rsp,
4691 file_infoclass_size);
4692 ksmbd_fd_put(work, fp);
4693 return rc;
4694}
4695
Namjae Jeone2f34482021-03-16 10:49:09 +09004696static int smb2_get_info_filesystem(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004697 struct smb2_query_info_req *req,
4698 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004699{
4700 struct ksmbd_session *sess = work->sess;
4701 struct ksmbd_conn *conn = sess->conn;
4702 struct ksmbd_share_config *share = work->tcon->share_conf;
4703 int fsinfoclass = 0;
4704 struct kstatfs stfs;
4705 struct path path;
4706 int rc = 0, len;
4707 int fs_infoclass_size = 0;
Hyunchul Leea6a5fa72021-05-26 18:59:06 +09004708 int lookup_flags = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09004709
Hyunchul Leea6a5fa72021-05-26 18:59:06 +09004710 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_FOLLOW_SYMLINKS))
4711 lookup_flags = LOOKUP_FOLLOW;
4712
4713 rc = ksmbd_vfs_kern_path(share->path, lookup_flags, &path, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004714 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004715 pr_err("cannot create vfs path\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004716 return -EIO;
4717 }
4718
4719 rc = vfs_statfs(&path, &stfs);
4720 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004721 pr_err("cannot do stat of path %s\n", share->path);
Namjae Jeone2f34482021-03-16 10:49:09 +09004722 path_put(&path);
4723 return -EIO;
4724 }
4725
4726 fsinfoclass = req->FileInfoClass;
4727
4728 switch (fsinfoclass) {
4729 case FS_DEVICE_INFORMATION:
4730 {
4731 struct filesystem_device_info *info;
4732
4733 info = (struct filesystem_device_info *)rsp->Buffer;
4734
4735 info->DeviceType = cpu_to_le32(stfs.f_type);
4736 info->DeviceCharacteristics = cpu_to_le32(0x00000020);
4737 rsp->OutputBufferLength = cpu_to_le32(8);
4738 inc_rfc1001_len(rsp_org, 8);
4739 fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE;
4740 break;
4741 }
4742 case FS_ATTRIBUTE_INFORMATION:
4743 {
4744 struct filesystem_attribute_info *info;
4745 size_t sz;
4746
4747 info = (struct filesystem_attribute_info *)rsp->Buffer;
4748 info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS |
4749 FILE_PERSISTENT_ACLS |
4750 FILE_UNICODE_ON_DISK |
4751 FILE_CASE_PRESERVED_NAMES |
Namjae Jeoneb817362021-05-18 10:37:59 +09004752 FILE_CASE_SENSITIVE_SEARCH |
4753 FILE_SUPPORTS_BLOCK_REFCOUNTING);
Namjae Jeone2f34482021-03-16 10:49:09 +09004754
4755 info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps);
4756
4757 info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen);
4758 len = smbConvertToUTF16((__le16 *)info->FileSystemName,
4759 "NTFS", PATH_MAX, conn->local_nls, 0);
4760 len = len * 2;
4761 info->FileSystemNameLen = cpu_to_le32(len);
4762 sz = sizeof(struct filesystem_attribute_info) - 2 + len;
4763 rsp->OutputBufferLength = cpu_to_le32(sz);
4764 inc_rfc1001_len(rsp_org, sz);
4765 fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE;
4766 break;
4767 }
4768 case FS_VOLUME_INFORMATION:
4769 {
4770 struct filesystem_vol_info *info;
4771 size_t sz;
4772
4773 info = (struct filesystem_vol_info *)(rsp->Buffer);
4774 info->VolumeCreationTime = 0;
4775 /* Taking dummy value of serial number*/
4776 info->SerialNumber = cpu_to_le32(0xbc3ac512);
4777 len = smbConvertToUTF16((__le16 *)info->VolumeLabel,
4778 share->name, PATH_MAX,
4779 conn->local_nls, 0);
4780 len = len * 2;
4781 info->VolumeLabelSize = cpu_to_le32(len);
4782 info->Reserved = 0;
4783 sz = sizeof(struct filesystem_vol_info) - 2 + len;
4784 rsp->OutputBufferLength = cpu_to_le32(sz);
4785 inc_rfc1001_len(rsp_org, sz);
4786 fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE;
4787 break;
4788 }
4789 case FS_SIZE_INFORMATION:
4790 {
4791 struct filesystem_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004792
4793 info = (struct filesystem_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004794 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4795 info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004796 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4797 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004798 rsp->OutputBufferLength = cpu_to_le32(24);
4799 inc_rfc1001_len(rsp_org, 24);
4800 fs_infoclass_size = FS_SIZE_INFORMATION_SIZE;
4801 break;
4802 }
4803 case FS_FULL_SIZE_INFORMATION:
4804 {
4805 struct smb2_fs_full_size_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004806
4807 info = (struct smb2_fs_full_size_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004808 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4809 info->CallerAvailableAllocationUnits =
4810 cpu_to_le64(stfs.f_bavail);
4811 info->ActualAvailableAllocationUnits =
4812 cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004813 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4814 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004815 rsp->OutputBufferLength = cpu_to_le32(32);
4816 inc_rfc1001_len(rsp_org, 32);
4817 fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE;
4818 break;
4819 }
4820 case FS_OBJECT_ID_INFORMATION:
4821 {
4822 struct object_id_info *info;
4823
4824 info = (struct object_id_info *)(rsp->Buffer);
4825
4826 if (!user_guest(sess->user))
4827 memcpy(info->objid, user_passkey(sess->user), 16);
4828 else
4829 memset(info->objid, 0, 16);
4830
4831 info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC);
4832 info->extended_info.version = cpu_to_le32(1);
4833 info->extended_info.release = cpu_to_le32(1);
4834 info->extended_info.rel_date = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004835 memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0"));
Namjae Jeone2f34482021-03-16 10:49:09 +09004836 rsp->OutputBufferLength = cpu_to_le32(64);
4837 inc_rfc1001_len(rsp_org, 64);
4838 fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE;
4839 break;
4840 }
4841 case FS_SECTOR_SIZE_INFORMATION:
4842 {
4843 struct smb3_fs_ss_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004844
4845 info = (struct smb3_fs_ss_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004846
Namjae Jeon131bac12021-06-22 16:20:47 +09004847 info->LogicalBytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004848 info->PhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09004849 cpu_to_le32(stfs.f_bsize);
4850 info->PhysicalBytesPerSectorForPerf = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004851 info->FSEffPhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09004852 cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004853 info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE |
4854 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE);
4855 info->ByteOffsetForSectorAlignment = 0;
4856 info->ByteOffsetForPartitionAlignment = 0;
4857 rsp->OutputBufferLength = cpu_to_le32(28);
4858 inc_rfc1001_len(rsp_org, 28);
4859 fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE;
4860 break;
4861 }
4862 case FS_CONTROL_INFORMATION:
4863 {
4864 /*
4865 * TODO : The current implementation is based on
4866 * test result with win7(NTFS) server. It's need to
4867 * modify this to get valid Quota values
4868 * from Linux kernel
4869 */
4870 struct smb2_fs_control_info *info;
4871
4872 info = (struct smb2_fs_control_info *)(rsp->Buffer);
4873 info->FreeSpaceStartFiltering = 0;
4874 info->FreeSpaceThreshold = 0;
4875 info->FreeSpaceStopFiltering = 0;
4876 info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID);
4877 info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID);
4878 info->Padding = 0;
4879 rsp->OutputBufferLength = cpu_to_le32(48);
4880 inc_rfc1001_len(rsp_org, 48);
4881 fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE;
4882 break;
4883 }
4884 case FS_POSIX_INFORMATION:
4885 {
4886 struct filesystem_posix_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004887
4888 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004889 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004890 rc = -EOPNOTSUPP;
4891 } else {
4892 info = (struct filesystem_posix_info *)(rsp->Buffer);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004893 info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004894 info->BlockSize = cpu_to_le32(stfs.f_bsize);
4895 info->TotalBlocks = cpu_to_le64(stfs.f_blocks);
4896 info->BlocksAvail = cpu_to_le64(stfs.f_bfree);
4897 info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail);
4898 info->TotalFileNodes = cpu_to_le64(stfs.f_files);
4899 info->FreeFileNodes = cpu_to_le64(stfs.f_ffree);
4900 rsp->OutputBufferLength = cpu_to_le32(56);
4901 inc_rfc1001_len(rsp_org, 56);
4902 fs_infoclass_size = FS_POSIX_INFORMATION_SIZE;
4903 }
4904 break;
4905 }
4906 default:
4907 path_put(&path);
4908 return -EOPNOTSUPP;
4909 }
4910 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4911 rsp,
4912 fs_infoclass_size);
4913 path_put(&path);
4914 return rc;
4915}
4916
4917static int smb2_get_info_sec(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004918 struct smb2_query_info_req *req,
4919 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004920{
4921 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09004922 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09004923 struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL;
4924 struct smb_fattr fattr = {{0}};
4925 struct inode *inode;
4926 __u32 secdesclen;
4927 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4928 int addition_info = le32_to_cpu(req->AdditionalInformation);
4929 int rc;
4930
Namjae Jeone294f782021-06-28 15:26:37 +09004931 if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
4932 PROTECTED_DACL_SECINFO |
4933 UNPROTECTED_DACL_SECINFO)) {
4934 pr_err("Unsupported addition info: 0x%x)\n",
4935 addition_info);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09004936
4937 pntsd->revision = cpu_to_le16(1);
4938 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED);
4939 pntsd->osidoffset = 0;
4940 pntsd->gsidoffset = 0;
4941 pntsd->sacloffset = 0;
4942 pntsd->dacloffset = 0;
4943
4944 secdesclen = sizeof(struct smb_ntsd);
4945 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
4946 inc_rfc1001_len(rsp_org, secdesclen);
4947
4948 return 0;
4949 }
4950
Namjae Jeone2f34482021-03-16 10:49:09 +09004951 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon38673692021-07-08 12:32:27 +09004952 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
4953 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004954 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004955 id = work->compound_fid;
4956 pid = work->compound_pfid;
4957 }
4958 }
4959
Namjae Jeon38673692021-07-08 12:32:27 +09004960 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004961 id = le64_to_cpu(req->VolatileFileId);
4962 pid = le64_to_cpu(req->PersistentFileId);
4963 }
4964
4965 fp = ksmbd_lookup_fd_slow(work, id, pid);
4966 if (!fp)
4967 return -ENOENT;
4968
Hyunchul Lee465d7202021-07-03 12:10:36 +09004969 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeonab0b2632021-06-29 09:20:13 +09004970 inode = file_inode(fp->filp);
Namjae Jeon3d47e542021-04-20 14:25:35 +09004971 ksmbd_acls_fattr(&fattr, inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09004972
4973 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004974 KSMBD_SHARE_FLAG_ACL_XATTR))
Hyunchul Lee465d7202021-07-03 12:10:36 +09004975 ksmbd_vfs_get_sd_xattr(work->conn, user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09004976 fp->filp->f_path.dentry, &ppntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09004977
Hyunchul Lee465d7202021-07-03 12:10:36 +09004978 rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info,
4979 &secdesclen, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09004980 posix_acl_release(fattr.cf_acls);
4981 posix_acl_release(fattr.cf_dacls);
4982 kfree(ppntsd);
4983 ksmbd_fd_put(work, fp);
4984 if (rc)
4985 return rc;
4986
4987 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
4988 inc_rfc1001_len(rsp_org, secdesclen);
4989 return 0;
4990}
4991
4992/**
4993 * smb2_query_info() - handler for smb2 query info command
4994 * @work: smb work containing query info request buffer
4995 *
4996 * Return: 0 on success, otherwise error
4997 */
4998int smb2_query_info(struct ksmbd_work *work)
4999{
5000 struct smb2_query_info_req *req;
5001 struct smb2_query_info_rsp *rsp, *rsp_org;
5002 int rc = 0;
5003
Namjae Jeone5066492021-03-30 12:35:23 +09005004 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005005 WORK_BUFFERS(work, req, rsp);
5006
5007 ksmbd_debug(SMB, "GOT query info request\n");
5008
5009 switch (req->InfoType) {
5010 case SMB2_O_INFO_FILE:
5011 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
5012 rc = smb2_get_info_file(work, req, rsp, (void *)rsp_org);
5013 break;
5014 case SMB2_O_INFO_FILESYSTEM:
5015 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n");
5016 rc = smb2_get_info_filesystem(work, req, rsp, (void *)rsp_org);
5017 break;
5018 case SMB2_O_INFO_SECURITY:
5019 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
5020 rc = smb2_get_info_sec(work, req, rsp, (void *)rsp_org);
5021 break;
5022 default:
5023 ksmbd_debug(SMB, "InfoType %d not supported yet\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005024 req->InfoType);
Namjae Jeone2f34482021-03-16 10:49:09 +09005025 rc = -EOPNOTSUPP;
5026 }
5027
5028 if (rc < 0) {
5029 if (rc == -EACCES)
5030 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5031 else if (rc == -ENOENT)
5032 rsp->hdr.Status = STATUS_FILE_CLOSED;
5033 else if (rc == -EIO)
5034 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5035 else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0)
5036 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5037 smb2_set_err_rsp(work);
5038
5039 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005040 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005041 return rc;
5042 }
5043 rsp->StructureSize = cpu_to_le16(9);
5044 rsp->OutputBufferOffset = cpu_to_le16(72);
5045 inc_rfc1001_len(rsp_org, 8);
5046 return 0;
5047}
5048
5049/**
5050 * smb2_close_pipe() - handler for closing IPC pipe
5051 * @work: smb work containing close request buffer
5052 *
5053 * Return: 0
5054 */
5055static noinline int smb2_close_pipe(struct ksmbd_work *work)
5056{
Namjae Jeon64b39f42021-03-30 14:25:35 +09005057 u64 id;
Namjae Jeone5066492021-03-30 12:35:23 +09005058 struct smb2_close_req *req = work->request_buf;
5059 struct smb2_close_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005060
5061 id = le64_to_cpu(req->VolatileFileId);
5062 ksmbd_session_rpc_close(work->sess, id);
5063
5064 rsp->StructureSize = cpu_to_le16(60);
5065 rsp->Flags = 0;
5066 rsp->Reserved = 0;
5067 rsp->CreationTime = 0;
5068 rsp->LastAccessTime = 0;
5069 rsp->LastWriteTime = 0;
5070 rsp->ChangeTime = 0;
5071 rsp->AllocationSize = 0;
5072 rsp->EndOfFile = 0;
5073 rsp->Attributes = 0;
5074 inc_rfc1001_len(rsp, 60);
5075 return 0;
5076}
5077
5078/**
5079 * smb2_close() - handler for smb2 close file command
5080 * @work: smb work containing close request buffer
5081 *
5082 * Return: 0
5083 */
5084int smb2_close(struct ksmbd_work *work)
5085{
Namjae Jeon38673692021-07-08 12:32:27 +09005086 u64 volatile_id = KSMBD_NO_FID;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005087 u64 sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09005088 struct smb2_close_req *req;
5089 struct smb2_close_rsp *rsp;
5090 struct smb2_close_rsp *rsp_org;
5091 struct ksmbd_conn *conn = work->conn;
5092 struct ksmbd_file *fp;
5093 struct inode *inode;
5094 u64 time;
5095 int err = 0;
5096
Namjae Jeone5066492021-03-30 12:35:23 +09005097 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005098 WORK_BUFFERS(work, req, rsp);
5099
5100 if (test_share_config_flag(work->tcon->share_conf,
5101 KSMBD_SHARE_FLAG_PIPE)) {
5102 ksmbd_debug(SMB, "IPC pipe close request\n");
5103 return smb2_close_pipe(work);
5104 }
5105
5106 sess_id = le64_to_cpu(req->hdr.SessionId);
5107 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5108 sess_id = work->compound_sid;
5109
5110 work->compound_sid = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005111 if (check_session_id(conn, sess_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005112 work->compound_sid = sess_id;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005113 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005114 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
5115 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5116 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5117 err = -EBADF;
5118 goto out;
5119 }
5120
5121 if (work->next_smb2_rcv_hdr_off &&
Namjae Jeon38673692021-07-08 12:32:27 +09005122 !has_file_id(le64_to_cpu(req->VolatileFileId))) {
5123 if (!has_file_id(work->compound_fid)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005124 /* file already closed, return FILE_CLOSED */
5125 ksmbd_debug(SMB, "file already closed\n");
5126 rsp->hdr.Status = STATUS_FILE_CLOSED;
5127 err = -EBADF;
5128 goto out;
5129 } else {
Namjae Jeon38673692021-07-08 12:32:27 +09005130 ksmbd_debug(SMB,
5131 "Compound request set FID = %llu:%llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005132 work->compound_fid,
5133 work->compound_pfid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005134 volatile_id = work->compound_fid;
5135
5136 /* file closed, stored id is not valid anymore */
5137 work->compound_fid = KSMBD_NO_FID;
5138 work->compound_pfid = KSMBD_NO_FID;
5139 }
5140 } else {
5141 volatile_id = le64_to_cpu(req->VolatileFileId);
5142 }
Namjae Jeon38673692021-07-08 12:32:27 +09005143 ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id);
Namjae Jeone2f34482021-03-16 10:49:09 +09005144
5145 rsp->StructureSize = cpu_to_le16(60);
5146 rsp->Reserved = 0;
5147
5148 if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) {
5149 fp = ksmbd_lookup_fd_fast(work, volatile_id);
5150 if (!fp) {
5151 err = -ENOENT;
5152 goto out;
5153 }
5154
Namjae Jeonab0b2632021-06-29 09:20:13 +09005155 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005156 rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
5157 rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 :
5158 cpu_to_le64(inode->i_blocks << 9);
5159 rsp->EndOfFile = cpu_to_le64(inode->i_size);
5160 rsp->Attributes = fp->f_ci->m_fattr;
5161 rsp->CreationTime = cpu_to_le64(fp->create_time);
5162 time = ksmbd_UnixTimeToNT(inode->i_atime);
5163 rsp->LastAccessTime = cpu_to_le64(time);
5164 time = ksmbd_UnixTimeToNT(inode->i_mtime);
5165 rsp->LastWriteTime = cpu_to_le64(time);
5166 time = ksmbd_UnixTimeToNT(inode->i_ctime);
5167 rsp->ChangeTime = cpu_to_le64(time);
5168 ksmbd_fd_put(work, fp);
5169 } else {
5170 rsp->Flags = 0;
5171 rsp->AllocationSize = 0;
5172 rsp->EndOfFile = 0;
5173 rsp->Attributes = 0;
5174 rsp->CreationTime = 0;
5175 rsp->LastAccessTime = 0;
5176 rsp->LastWriteTime = 0;
5177 rsp->ChangeTime = 0;
5178 }
5179
5180 err = ksmbd_close_fd(work, volatile_id);
5181out:
5182 if (err) {
5183 if (rsp->hdr.Status == 0)
5184 rsp->hdr.Status = STATUS_FILE_CLOSED;
5185 smb2_set_err_rsp(work);
5186 } else {
5187 inc_rfc1001_len(rsp_org, 60);
5188 }
5189
5190 return 0;
5191}
5192
5193/**
5194 * smb2_echo() - handler for smb2 echo(ping) command
5195 * @work: smb work containing echo request buffer
5196 *
5197 * Return: 0
5198 */
5199int smb2_echo(struct ksmbd_work *work)
5200{
Namjae Jeone5066492021-03-30 12:35:23 +09005201 struct smb2_echo_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005202
5203 rsp->StructureSize = cpu_to_le16(4);
5204 rsp->Reserved = 0;
5205 inc_rfc1001_len(rsp, 4);
5206 return 0;
5207}
5208
Namjae Jeone2f34482021-03-16 10:49:09 +09005209static int smb2_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005210 struct smb2_file_rename_info *file_info,
5211 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005212{
5213 struct ksmbd_share_config *share = fp->tcon->share_conf;
5214 char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL;
5215 char *pathname = NULL;
5216 struct path path;
5217 bool file_present = true;
5218 int rc;
5219
5220 ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n");
5221 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5222 if (!pathname)
5223 return -ENOMEM;
5224
5225 abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
5226 if (IS_ERR(abs_oldname)) {
5227 rc = -EINVAL;
5228 goto out;
5229 }
5230 old_name = strrchr(abs_oldname, '/');
Namjae Jeon64b39f42021-03-30 14:25:35 +09005231 if (old_name && old_name[1] != '\0') {
Namjae Jeone2f34482021-03-16 10:49:09 +09005232 old_name++;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005233 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005234 ksmbd_debug(SMB, "can't get last component in path %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005235 abs_oldname);
Namjae Jeone2f34482021-03-16 10:49:09 +09005236 rc = -ENOENT;
5237 goto out;
5238 }
5239
5240 new_name = smb2_get_name(share,
5241 file_info->FileName,
5242 le32_to_cpu(file_info->FileNameLength),
5243 local_nls);
5244 if (IS_ERR(new_name)) {
5245 rc = PTR_ERR(new_name);
5246 goto out;
5247 }
5248
5249 if (strchr(new_name, ':')) {
5250 int s_type;
5251 char *xattr_stream_name, *stream_name = NULL;
5252 size_t xattr_stream_size;
5253 int len;
5254
5255 rc = parse_stream_name(new_name, &stream_name, &s_type);
5256 if (rc < 0)
5257 goto out;
5258
5259 len = strlen(new_name);
5260 if (new_name[len - 1] != '/') {
Namjae Jeonbde16942021-06-28 15:23:19 +09005261 pr_err("not allow base filename in rename\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005262 rc = -ESHARE;
5263 goto out;
5264 }
5265
5266 rc = ksmbd_vfs_xattr_stream_name(stream_name,
5267 &xattr_stream_name,
5268 &xattr_stream_size,
5269 s_type);
5270 if (rc)
5271 goto out;
5272
Hyunchul Leeaf349832021-06-30 18:25:53 +09005273 rc = ksmbd_vfs_setxattr(file_mnt_user_ns(fp->filp),
5274 fp->filp->f_path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09005275 xattr_stream_name,
5276 NULL, 0, 0);
5277 if (rc < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005278 pr_err("failed to store stream name in xattr: %d\n",
5279 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005280 rc = -EINVAL;
5281 goto out;
5282 }
5283
5284 goto out;
5285 }
5286
5287 ksmbd_debug(SMB, "new name %s\n", new_name);
5288 rc = ksmbd_vfs_kern_path(new_name, 0, &path, 1);
5289 if (rc)
5290 file_present = false;
5291 else
5292 path_put(&path);
5293
5294 if (ksmbd_share_veto_filename(share, new_name)) {
5295 rc = -ENOENT;
5296 ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name);
5297 goto out;
5298 }
5299
5300 if (file_info->ReplaceIfExists) {
5301 if (file_present) {
5302 rc = ksmbd_vfs_remove_file(work, new_name);
5303 if (rc) {
5304 if (rc != -ENOTEMPTY)
5305 rc = -EINVAL;
5306 ksmbd_debug(SMB, "cannot delete %s, rc %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005307 new_name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005308 goto out;
5309 }
5310 }
5311 } else {
5312 if (file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005313 strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005314 rc = -EEXIST;
5315 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005316 "cannot rename already existing file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005317 goto out;
5318 }
5319 }
5320
5321 rc = ksmbd_vfs_fp_rename(work, fp, new_name);
5322out:
5323 kfree(pathname);
5324 if (!IS_ERR(new_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005325 kfree(new_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005326 return rc;
5327}
5328
Namjae Jeone2f34482021-03-16 10:49:09 +09005329static int smb2_create_link(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005330 struct ksmbd_share_config *share,
5331 struct smb2_file_link_info *file_info,
5332 struct file *filp,
5333 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005334{
5335 char *link_name = NULL, *target_name = NULL, *pathname = NULL;
5336 struct path path;
5337 bool file_present = true;
5338 int rc;
5339
5340 ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n");
5341 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5342 if (!pathname)
5343 return -ENOMEM;
5344
5345 link_name = smb2_get_name(share,
5346 file_info->FileName,
5347 le32_to_cpu(file_info->FileNameLength),
5348 local_nls);
5349 if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) {
5350 rc = -EINVAL;
5351 goto out;
5352 }
5353
5354 ksmbd_debug(SMB, "link name is %s\n", link_name);
5355 target_name = d_path(&filp->f_path, pathname, PATH_MAX);
5356 if (IS_ERR(target_name)) {
5357 rc = -EINVAL;
5358 goto out;
5359 }
5360
5361 ksmbd_debug(SMB, "target name is %s\n", target_name);
5362 rc = ksmbd_vfs_kern_path(link_name, 0, &path, 0);
5363 if (rc)
5364 file_present = false;
5365 else
5366 path_put(&path);
5367
5368 if (file_info->ReplaceIfExists) {
5369 if (file_present) {
5370 rc = ksmbd_vfs_remove_file(work, link_name);
5371 if (rc) {
5372 rc = -EINVAL;
5373 ksmbd_debug(SMB, "cannot delete %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005374 link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005375 goto out;
5376 }
5377 }
5378 } else {
5379 if (file_present) {
5380 rc = -EEXIST;
5381 ksmbd_debug(SMB, "link already exists\n");
5382 goto out;
5383 }
5384 }
5385
5386 rc = ksmbd_vfs_link(work, target_name, link_name);
5387 if (rc)
5388 rc = -EINVAL;
5389out:
5390 if (!IS_ERR(link_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005391 kfree(link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005392 kfree(pathname);
5393 return rc;
5394}
5395
Namjae Jeon64b39f42021-03-30 14:25:35 +09005396static int set_file_basic_info(struct ksmbd_file *fp, char *buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09005397 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005398{
5399 struct smb2_file_all_info *file_info;
5400 struct iattr attrs;
5401 struct iattr temp_attrs;
5402 struct file *filp;
5403 struct inode *inode;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005404 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09005405 int rc;
5406
Marios Makassikis7adfd4f2021-04-27 15:30:22 +09005407 if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005408 return -EACCES;
5409
5410 file_info = (struct smb2_file_all_info *)buf;
5411 attrs.ia_valid = 0;
5412 filp = fp->filp;
5413 inode = file_inode(filp);
Hyunchul Lee465d7202021-07-03 12:10:36 +09005414 user_ns = file_mnt_user_ns(filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005415
5416 if (file_info->CreationTime)
5417 fp->create_time = le64_to_cpu(file_info->CreationTime);
5418
5419 if (file_info->LastAccessTime) {
5420 attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime);
5421 attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
5422 }
5423
5424 if (file_info->ChangeTime) {
5425 temp_attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime);
5426 attrs.ia_ctime = temp_attrs.ia_ctime;
5427 attrs.ia_valid |= ATTR_CTIME;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005428 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005429 temp_attrs.ia_ctime = inode->i_ctime;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005430 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005431
5432 if (file_info->LastWriteTime) {
5433 attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime);
5434 attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
5435 }
5436
5437 if (file_info->Attributes) {
5438 if (!S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005439 file_info->Attributes & ATTR_DIRECTORY_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005440 pr_err("can't change a file to a directory\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005441 return -EINVAL;
5442 }
5443
5444 if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == ATTR_NORMAL_LE))
5445 fp->f_ci->m_fattr = file_info->Attributes |
5446 (fp->f_ci->m_fattr & ATTR_DIRECTORY_LE);
5447 }
5448
5449 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
5450 (file_info->CreationTime || file_info->Attributes)) {
5451 struct xattr_dos_attrib da = {0};
5452
5453 da.version = 4;
5454 da.itime = fp->itime;
5455 da.create_time = fp->create_time;
5456 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
5457 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
5458 XATTR_DOSINFO_ITIME;
5459
Hyunchul Lee465d7202021-07-03 12:10:36 +09005460 rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005461 filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09005462 if (rc)
5463 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005464 "failed to restore file attribute in EA\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005465 rc = 0;
5466 }
5467
5468 /*
5469 * HACK : set ctime here to avoid ctime changed
5470 * when file_info->ChangeTime is zero.
5471 */
5472 attrs.ia_ctime = temp_attrs.ia_ctime;
5473 attrs.ia_valid |= ATTR_CTIME;
5474
5475 if (attrs.ia_valid) {
5476 struct dentry *dentry = filp->f_path.dentry;
5477 struct inode *inode = d_inode(dentry);
5478
5479 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
5480 return -EACCES;
5481
Hyunchul Lee465d7202021-07-03 12:10:36 +09005482 rc = setattr_prepare(user_ns, dentry, &attrs);
Namjae Jeone2f34482021-03-16 10:49:09 +09005483 if (rc)
5484 return -EINVAL;
5485
5486 inode_lock(inode);
Hyunchul Lee465d7202021-07-03 12:10:36 +09005487 setattr_copy(user_ns, inode, &attrs);
Namjae Jeone2f34482021-03-16 10:49:09 +09005488 attrs.ia_valid &= ~ATTR_CTIME;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005489 rc = notify_change(user_ns, dentry, &attrs, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09005490 inode_unlock(inode);
5491 }
5492 return 0;
5493}
5494
5495static int set_file_allocation_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005496 struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005497{
5498 /*
5499 * TODO : It's working fine only when store dos attributes
5500 * is not yes. need to implement a logic which works
5501 * properly with any smb.conf option
5502 */
5503
5504 struct smb2_file_alloc_info *file_alloc_info;
5505 loff_t alloc_blks;
5506 struct inode *inode;
5507 int rc;
5508
Marios Makassikisa2996692021-04-27 15:29:01 +09005509 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005510 return -EACCES;
5511
5512 file_alloc_info = (struct smb2_file_alloc_info *)buf;
5513 alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9;
5514 inode = file_inode(fp->filp);
5515
5516 if (alloc_blks > inode->i_blocks) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005517 smb_break_all_levII_oplock(work, fp, 1);
5518 rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
5519 alloc_blks * 512);
Namjae Jeone2f34482021-03-16 10:49:09 +09005520 if (rc && rc != -EOPNOTSUPP) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005521 pr_err("vfs_fallocate is failed : %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005522 return rc;
5523 }
5524 } else if (alloc_blks < inode->i_blocks) {
5525 loff_t size;
5526
5527 /*
5528 * Allocation size could be smaller than original one
5529 * which means allocated blocks in file should be
5530 * deallocated. use truncate to cut out it, but inode
5531 * size is also updated with truncate offset.
5532 * inode size is retained by backup inode size.
5533 */
5534 size = i_size_read(inode);
5535 rc = ksmbd_vfs_truncate(work, NULL, fp, alloc_blks * 512);
5536 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005537 pr_err("truncate failed! filename : %s, err %d\n",
5538 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005539 return rc;
5540 }
5541 if (size < alloc_blks * 512)
5542 i_size_write(inode, size);
5543 }
5544 return 0;
5545}
5546
Namjae Jeon64b39f42021-03-30 14:25:35 +09005547static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005548 char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005549{
5550 struct smb2_file_eof_info *file_eof_info;
5551 loff_t newsize;
5552 struct inode *inode;
5553 int rc;
5554
Marios Makassikisa2996692021-04-27 15:29:01 +09005555 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005556 return -EACCES;
5557
5558 file_eof_info = (struct smb2_file_eof_info *)buf;
5559 newsize = le64_to_cpu(file_eof_info->EndOfFile);
5560 inode = file_inode(fp->filp);
5561
5562 /*
5563 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called
5564 * on FAT32 shared device, truncate execution time is too long
5565 * and network error could cause from windows client. because
5566 * truncate of some filesystem like FAT32 fill zero data in
5567 * truncated range.
5568 */
5569 if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) {
5570 ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005571 fp->filename, newsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005572 rc = ksmbd_vfs_truncate(work, NULL, fp, newsize);
5573 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005574 ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005575 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005576 if (rc != -EAGAIN)
5577 rc = -EBADF;
5578 return rc;
5579 }
5580 }
5581 return 0;
5582}
5583
Namjae Jeon64b39f42021-03-30 14:25:35 +09005584static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005585 char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005586{
5587 struct ksmbd_file *parent_fp;
Namjae Jeon12202c02021-06-29 09:23:56 +09005588 struct dentry *parent;
5589 struct dentry *dentry = fp->filp->f_path.dentry;
5590 int ret;
Namjae Jeone2f34482021-03-16 10:49:09 +09005591
5592 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005593 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005594 return -EACCES;
5595 }
5596
5597 if (ksmbd_stream_fd(fp))
5598 goto next;
5599
Namjae Jeon12202c02021-06-29 09:23:56 +09005600 parent = dget_parent(dentry);
5601 ret = ksmbd_vfs_lock_parent(parent, dentry);
5602 if (ret) {
5603 dput(parent);
5604 return ret;
5605 }
5606
5607 parent_fp = ksmbd_lookup_fd_inode(d_inode(parent));
5608 inode_unlock(d_inode(parent));
5609 dput(parent);
5610
Namjae Jeone2f34482021-03-16 10:49:09 +09005611 if (parent_fp) {
5612 if (parent_fp->daccess & FILE_DELETE_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005613 pr_err("parent dir is opened with delete access\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005614 return -ESHARE;
5615 }
5616 }
5617next:
5618 return smb2_rename(work, fp,
5619 (struct smb2_file_rename_info *)buf,
5620 work->sess->conn->local_nls);
5621}
5622
Namjae Jeon64b39f42021-03-30 14:25:35 +09005623static int set_file_disposition_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005624{
5625 struct smb2_file_disposition_info *file_info;
5626 struct inode *inode;
5627
5628 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005629 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005630 return -EACCES;
5631 }
5632
5633 inode = file_inode(fp->filp);
5634 file_info = (struct smb2_file_disposition_info *)buf;
5635 if (file_info->DeletePending) {
5636 if (S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005637 ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY)
Namjae Jeone2f34482021-03-16 10:49:09 +09005638 return -EBUSY;
5639 ksmbd_set_inode_pending_delete(fp);
5640 } else {
5641 ksmbd_clear_inode_pending_delete(fp);
5642 }
5643 return 0;
5644}
5645
Namjae Jeon64b39f42021-03-30 14:25:35 +09005646static int set_file_position_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005647{
5648 struct smb2_file_pos_info *file_info;
5649 loff_t current_byte_offset;
Namjae Jeonee81cae2021-06-26 22:32:34 +09005650 unsigned long sector_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09005651 struct inode *inode;
5652
5653 inode = file_inode(fp->filp);
5654 file_info = (struct smb2_file_pos_info *)buf;
5655 current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset);
Namjae Jeonee81cae2021-06-26 22:32:34 +09005656 sector_size = inode->i_sb->s_blocksize;
Namjae Jeone2f34482021-03-16 10:49:09 +09005657
5658 if (current_byte_offset < 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09005659 (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE &&
5660 current_byte_offset & (sector_size - 1))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005661 pr_err("CurrentByteOffset is not valid : %llu\n",
5662 current_byte_offset);
Namjae Jeone2f34482021-03-16 10:49:09 +09005663 return -EINVAL;
5664 }
5665
5666 fp->filp->f_pos = current_byte_offset;
5667 return 0;
5668}
5669
Namjae Jeon64b39f42021-03-30 14:25:35 +09005670static int set_file_mode_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005671{
5672 struct smb2_file_mode_info *file_info;
5673 __le32 mode;
5674
5675 file_info = (struct smb2_file_mode_info *)buf;
5676 mode = file_info->Mode;
5677
Namjae Jeon64b39f42021-03-30 14:25:35 +09005678 if ((mode & ~FILE_MODE_INFO_MASK) ||
5679 (mode & FILE_SYNCHRONOUS_IO_ALERT_LE &&
5680 mode & FILE_SYNCHRONOUS_IO_NONALERT_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005681 pr_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode));
Namjae Jeone2f34482021-03-16 10:49:09 +09005682 return -EINVAL;
5683 }
5684
5685 /*
5686 * TODO : need to implement consideration for
5687 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT
5688 */
5689 ksmbd_vfs_set_fadvise(fp->filp, mode);
5690 fp->coption = mode;
5691 return 0;
5692}
5693
5694/**
5695 * smb2_set_info_file() - handler for smb2 set info command
5696 * @work: smb work containing set info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09005697 * @fp: ksmbd_file pointer
5698 * @info_class: smb2 set info class
5699 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09005700 *
5701 * Return: 0 on success, otherwise error
5702 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH
5703 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09005704static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005705 int info_class, char *buf,
5706 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005707{
5708 switch (info_class) {
5709 case FILE_BASIC_INFORMATION:
5710 return set_file_basic_info(fp, buf, share);
5711
5712 case FILE_ALLOCATION_INFORMATION:
5713 return set_file_allocation_info(work, fp, buf);
5714
5715 case FILE_END_OF_FILE_INFORMATION:
5716 return set_end_of_file_info(work, fp, buf);
5717
5718 case FILE_RENAME_INFORMATION:
Namjae Jeon64b39f42021-03-30 14:25:35 +09005719 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005720 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005721 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005722 return -EACCES;
5723 }
5724 return set_rename_info(work, fp, buf);
5725
5726 case FILE_LINK_INFORMATION:
5727 return smb2_create_link(work, work->tcon->share_conf,
Namjae Jeon070fb212021-05-26 17:57:12 +09005728 (struct smb2_file_link_info *)buf, fp->filp,
5729 work->sess->conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09005730
5731 case FILE_DISPOSITION_INFORMATION:
Namjae Jeon64b39f42021-03-30 14:25:35 +09005732 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005733 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005734 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005735 return -EACCES;
5736 }
5737 return set_file_disposition_info(fp, buf);
5738
5739 case FILE_FULL_EA_INFORMATION:
5740 {
5741 if (!(fp->daccess & FILE_WRITE_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005742 pr_err("Not permitted to write ext attr: 0x%x\n",
5743 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005744 return -EACCES;
5745 }
5746
5747 return smb2_set_ea((struct smb2_ea_info *)buf,
5748 &fp->filp->f_path);
5749 }
5750
5751 case FILE_POSITION_INFORMATION:
5752 return set_file_position_info(fp, buf);
5753
5754 case FILE_MODE_INFORMATION:
5755 return set_file_mode_info(fp, buf);
5756 }
5757
Namjae Jeonbde16942021-06-28 15:23:19 +09005758 pr_err("Unimplemented Fileinfoclass :%d\n", info_class);
Namjae Jeone2f34482021-03-16 10:49:09 +09005759 return -EOPNOTSUPP;
5760}
5761
Namjae Jeon64b39f42021-03-30 14:25:35 +09005762static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005763 char *buffer, int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005764{
5765 struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer;
5766
5767 fp->saccess |= FILE_SHARE_DELETE_LE;
5768
Hyunchul Leeef24c962021-06-30 18:25:52 +09005769 return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd,
Namjae Jeone2f34482021-03-16 10:49:09 +09005770 buf_len, false);
5771}
5772
5773/**
5774 * smb2_set_info() - handler for smb2 set info command handler
5775 * @work: smb work containing set info request buffer
5776 *
5777 * Return: 0 on success, otherwise error
5778 */
5779int smb2_set_info(struct ksmbd_work *work)
5780{
5781 struct smb2_set_info_req *req;
5782 struct smb2_set_info_rsp *rsp, *rsp_org;
5783 struct ksmbd_file *fp;
5784 int rc = 0;
5785 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5786
5787 ksmbd_debug(SMB, "Received set info request\n");
5788
Namjae Jeone5066492021-03-30 12:35:23 +09005789 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005790 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09005791 req = ksmbd_req_buf_next(work);
5792 rsp = ksmbd_resp_buf_next(work);
Namjae Jeon38673692021-07-08 12:32:27 +09005793 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
5794 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005795 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005796 id = work->compound_fid;
5797 pid = work->compound_pfid;
5798 }
5799 } else {
Namjae Jeone5066492021-03-30 12:35:23 +09005800 req = work->request_buf;
5801 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005802 }
5803
Namjae Jeon38673692021-07-08 12:32:27 +09005804 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005805 id = le64_to_cpu(req->VolatileFileId);
5806 pid = le64_to_cpu(req->PersistentFileId);
5807 }
5808
5809 fp = ksmbd_lookup_fd_slow(work, id, pid);
5810 if (!fp) {
5811 ksmbd_debug(SMB, "Invalid id for close: %u\n", id);
5812 rc = -ENOENT;
5813 goto err_out;
5814 }
5815
5816 switch (req->InfoType) {
5817 case SMB2_O_INFO_FILE:
5818 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
5819 rc = smb2_set_info_file(work, fp, req->FileInfoClass,
5820 req->Buffer, work->tcon->share_conf);
5821 break;
5822 case SMB2_O_INFO_SECURITY:
5823 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
5824 rc = smb2_set_info_sec(fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005825 le32_to_cpu(req->AdditionalInformation),
5826 req->Buffer,
5827 le32_to_cpu(req->BufferLength));
Namjae Jeone2f34482021-03-16 10:49:09 +09005828 break;
5829 default:
5830 rc = -EOPNOTSUPP;
5831 }
5832
5833 if (rc < 0)
5834 goto err_out;
5835
5836 rsp->StructureSize = cpu_to_le16(2);
5837 inc_rfc1001_len(rsp_org, 2);
5838 ksmbd_fd_put(work, fp);
5839 return 0;
5840
5841err_out:
5842 if (rc == -EACCES || rc == -EPERM)
5843 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5844 else if (rc == -EINVAL)
5845 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5846 else if (rc == -ESHARE)
5847 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
5848 else if (rc == -ENOENT)
5849 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
5850 else if (rc == -EBUSY || rc == -ENOTEMPTY)
5851 rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY;
5852 else if (rc == -EAGAIN)
5853 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
Namjae Jeonff1d5722021-04-13 13:18:10 +09005854 else if (rc == -EBADF || rc == -ESTALE)
Namjae Jeone2f34482021-03-16 10:49:09 +09005855 rsp->hdr.Status = STATUS_INVALID_HANDLE;
5856 else if (rc == -EEXIST)
5857 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
5858 else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP)
5859 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5860 smb2_set_err_rsp(work);
5861 ksmbd_fd_put(work, fp);
Namjae Jeon070fb212021-05-26 17:57:12 +09005862 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005863 return rc;
5864}
5865
5866/**
5867 * smb2_read_pipe() - handler for smb2 read from IPC pipe
5868 * @work: smb work containing read IPC pipe command buffer
5869 *
5870 * Return: 0 on success, otherwise error
5871 */
5872static noinline int smb2_read_pipe(struct ksmbd_work *work)
5873{
5874 int nbytes = 0, err;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005875 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09005876 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeone5066492021-03-30 12:35:23 +09005877 struct smb2_read_req *req = work->request_buf;
5878 struct smb2_read_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005879
5880 id = le64_to_cpu(req->VolatileFileId);
5881
5882 inc_rfc1001_len(rsp, 16);
5883 rpc_resp = ksmbd_rpc_read(work->sess, id);
5884 if (rpc_resp) {
5885 if (rpc_resp->flags != KSMBD_RPC_OK) {
5886 err = -EINVAL;
5887 goto out;
5888 }
5889
5890 work->aux_payload_buf =
Namjae Jeon79f6b112021-04-02 12:47:14 +09005891 kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09005892 if (!work->aux_payload_buf) {
5893 err = -ENOMEM;
5894 goto out;
5895 }
5896
5897 memcpy(work->aux_payload_buf, rpc_resp->payload,
Namjae Jeon070fb212021-05-26 17:57:12 +09005898 rpc_resp->payload_sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09005899
5900 nbytes = rpc_resp->payload_sz;
5901 work->resp_hdr_sz = get_rfc1002_len(rsp) + 4;
5902 work->aux_payload_sz = nbytes;
Namjae Jeon79f6b112021-04-02 12:47:14 +09005903 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005904 }
5905
5906 rsp->StructureSize = cpu_to_le16(17);
5907 rsp->DataOffset = 80;
5908 rsp->Reserved = 0;
5909 rsp->DataLength = cpu_to_le32(nbytes);
5910 rsp->DataRemaining = 0;
5911 rsp->Reserved2 = 0;
5912 inc_rfc1001_len(rsp, nbytes);
5913 return 0;
5914
5915out:
5916 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5917 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09005918 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005919 return err;
5920}
5921
5922static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005923 struct smb2_read_req *req, void *data_buf,
5924 size_t length)
Namjae Jeone2f34482021-03-16 10:49:09 +09005925{
5926 struct smb2_buffer_desc_v1 *desc =
5927 (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
5928 int err;
5929
Namjae Jeon64b39f42021-03-30 14:25:35 +09005930 if (work->conn->dialect == SMB30_PROT_ID &&
5931 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09005932 return -EINVAL;
5933
Namjae Jeon64b39f42021-03-30 14:25:35 +09005934 if (req->ReadChannelInfoOffset == 0 ||
5935 le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09005936 return -EINVAL;
5937
5938 work->need_invalidate_rkey =
5939 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
5940 work->remote_key = le32_to_cpu(desc->token);
5941
Namjae Jeon64b39f42021-03-30 14:25:35 +09005942 err = ksmbd_conn_rdma_write(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09005943 le32_to_cpu(desc->token),
5944 le64_to_cpu(desc->offset),
5945 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09005946 if (err)
5947 return err;
5948
5949 return length;
5950}
5951
5952/**
5953 * smb2_read() - handler for smb2 read from file
5954 * @work: smb work containing read command buffer
5955 *
5956 * Return: 0 on success, otherwise error
5957 */
5958int smb2_read(struct ksmbd_work *work)
5959{
5960 struct ksmbd_conn *conn = work->conn;
5961 struct smb2_read_req *req;
5962 struct smb2_read_rsp *rsp, *rsp_org;
5963 struct ksmbd_file *fp;
5964 loff_t offset;
5965 size_t length, mincount;
5966 ssize_t nbytes = 0, remain_bytes = 0;
5967 int err = 0;
5968
Namjae Jeone5066492021-03-30 12:35:23 +09005969 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005970 WORK_BUFFERS(work, req, rsp);
5971
5972 if (test_share_config_flag(work->tcon->share_conf,
5973 KSMBD_SHARE_FLAG_PIPE)) {
5974 ksmbd_debug(SMB, "IPC pipe read request\n");
5975 return smb2_read_pipe(work);
5976 }
5977
Namjae Jeon070fb212021-05-26 17:57:12 +09005978 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
5979 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09005980 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09005981 err = -ENOENT;
5982 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005983 }
5984
5985 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005986 pr_err("Not permitted to read : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005987 err = -EACCES;
5988 goto out;
5989 }
5990
5991 offset = le64_to_cpu(req->Offset);
5992 length = le32_to_cpu(req->Length);
5993 mincount = le32_to_cpu(req->MinimumCount);
5994
5995 if (length > conn->vals->max_read_size) {
5996 ksmbd_debug(SMB, "limiting read size to max size(%u)\n",
5997 conn->vals->max_read_size);
5998 err = -EINVAL;
5999 goto out;
6000 }
6001
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006002 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6003 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006004
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006005 work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006006 if (!work->aux_payload_buf) {
Dan Carpenterc1ea1112021-03-22 17:50:11 +03006007 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09006008 goto out;
6009 }
6010
6011 nbytes = ksmbd_vfs_read(work, fp, length, &offset);
6012 if (nbytes < 0) {
6013 err = nbytes;
6014 goto out;
6015 }
6016
6017 if ((nbytes == 0 && length != 0) || nbytes < mincount) {
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006018 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006019 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006020 rsp->hdr.Status = STATUS_END_OF_FILE;
6021 smb2_set_err_rsp(work);
6022 ksmbd_fd_put(work, fp);
6023 return 0;
6024 }
6025
6026 ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006027 nbytes, offset, mincount);
Namjae Jeone2f34482021-03-16 10:49:09 +09006028
6029 if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006030 req->Channel == SMB2_CHANNEL_RDMA_V1) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006031 /* write data to the client using rdma channel */
6032 remain_bytes = smb2_read_rdma_channel(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09006033 work->aux_payload_buf,
6034 nbytes);
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006035 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006036 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006037
6038 nbytes = 0;
6039 if (remain_bytes < 0) {
6040 err = (int)remain_bytes;
6041 goto out;
6042 }
6043 }
6044
6045 rsp->StructureSize = cpu_to_le16(17);
6046 rsp->DataOffset = 80;
6047 rsp->Reserved = 0;
6048 rsp->DataLength = cpu_to_le32(nbytes);
6049 rsp->DataRemaining = cpu_to_le32(remain_bytes);
6050 rsp->Reserved2 = 0;
6051 inc_rfc1001_len(rsp_org, 16);
6052 work->resp_hdr_sz = get_rfc1002_len(rsp_org) + 4;
6053 work->aux_payload_sz = nbytes;
6054 inc_rfc1001_len(rsp_org, nbytes);
6055 ksmbd_fd_put(work, fp);
6056 return 0;
6057
6058out:
6059 if (err) {
6060 if (err == -EISDIR)
6061 rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST;
6062 else if (err == -EAGAIN)
6063 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6064 else if (err == -ENOENT)
6065 rsp->hdr.Status = STATUS_FILE_CLOSED;
6066 else if (err == -EACCES)
6067 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6068 else if (err == -ESHARE)
6069 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6070 else if (err == -EINVAL)
6071 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6072 else
6073 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6074
6075 smb2_set_err_rsp(work);
6076 }
6077 ksmbd_fd_put(work, fp);
6078 return err;
6079}
6080
6081/**
6082 * smb2_write_pipe() - handler for smb2 write on IPC pipe
6083 * @work: smb work containing write IPC pipe command buffer
6084 *
6085 * Return: 0 on success, otherwise error
6086 */
6087static noinline int smb2_write_pipe(struct ksmbd_work *work)
6088{
Namjae Jeone5066492021-03-30 12:35:23 +09006089 struct smb2_write_req *req = work->request_buf;
6090 struct smb2_write_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006091 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006092 u64 id = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09006093 int err = 0, ret = 0;
6094 char *data_buf;
6095 size_t length;
6096
6097 length = le32_to_cpu(req->Length);
6098 id = le64_to_cpu(req->VolatileFileId);
6099
6100 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09006101 (offsetof(struct smb2_write_req, Buffer) - 4)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006102 data_buf = (char *)&req->Buffer[0];
6103 } else {
6104 if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006105 (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006106 pr_err("invalid write data offset %u, smb_len %u\n",
6107 le16_to_cpu(req->DataOffset),
6108 get_rfc1002_len(req));
Namjae Jeone2f34482021-03-16 10:49:09 +09006109 err = -EINVAL;
6110 goto out;
6111 }
6112
6113 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6114 le16_to_cpu(req->DataOffset));
6115 }
6116
6117 rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length);
6118 if (rpc_resp) {
6119 if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
6120 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006121 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006122 smb2_set_err_rsp(work);
6123 return -EOPNOTSUPP;
6124 }
6125 if (rpc_resp->flags != KSMBD_RPC_OK) {
6126 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6127 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006128 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006129 return ret;
6130 }
Namjae Jeon79f6b112021-04-02 12:47:14 +09006131 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006132 }
6133
6134 rsp->StructureSize = cpu_to_le16(17);
6135 rsp->DataOffset = 0;
6136 rsp->Reserved = 0;
6137 rsp->DataLength = cpu_to_le32(length);
6138 rsp->DataRemaining = 0;
6139 rsp->Reserved2 = 0;
6140 inc_rfc1001_len(rsp, 16);
6141 return 0;
6142out:
6143 if (err) {
6144 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6145 smb2_set_err_rsp(work);
6146 }
6147
6148 return err;
6149}
6150
6151static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006152 struct smb2_write_req *req,
6153 struct ksmbd_file *fp,
6154 loff_t offset, size_t length, bool sync)
Namjae Jeone2f34482021-03-16 10:49:09 +09006155{
6156 struct smb2_buffer_desc_v1 *desc;
6157 char *data_buf;
6158 int ret;
6159 ssize_t nbytes;
6160
6161 desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6162
6163 if (work->conn->dialect == SMB30_PROT_ID &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006164 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09006165 return -EINVAL;
6166
6167 if (req->Length != 0 || req->DataOffset != 0)
6168 return -EINVAL;
6169
Namjae Jeon64b39f42021-03-30 14:25:35 +09006170 if (req->WriteChannelInfoOffset == 0 ||
6171 le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09006172 return -EINVAL;
6173
6174 work->need_invalidate_rkey =
6175 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6176 work->remote_key = le32_to_cpu(desc->token);
6177
Namjae Jeon79f6b112021-04-02 12:47:14 +09006178 data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006179 if (!data_buf)
6180 return -ENOMEM;
6181
6182 ret = ksmbd_conn_rdma_read(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006183 le32_to_cpu(desc->token),
6184 le64_to_cpu(desc->offset),
6185 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006186 if (ret < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09006187 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006188 return ret;
6189 }
6190
Namjae Jeon64b39f42021-03-30 14:25:35 +09006191 ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006192 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006193 if (ret < 0)
6194 return ret;
6195
6196 return nbytes;
6197}
6198
6199/**
6200 * smb2_write() - handler for smb2 write from file
6201 * @work: smb work containing write command buffer
6202 *
6203 * Return: 0 on success, otherwise error
6204 */
6205int smb2_write(struct ksmbd_work *work)
6206{
6207 struct smb2_write_req *req;
6208 struct smb2_write_rsp *rsp, *rsp_org;
Namjae Jeonbcd62a32021-05-10 09:08:19 +09006209 struct ksmbd_file *fp = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006210 loff_t offset;
6211 size_t length;
6212 ssize_t nbytes;
6213 char *data_buf;
6214 bool writethrough = false;
6215 int err = 0;
6216
Namjae Jeone5066492021-03-30 12:35:23 +09006217 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006218 WORK_BUFFERS(work, req, rsp);
6219
Namjae Jeon64b39f42021-03-30 14:25:35 +09006220 if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006221 ksmbd_debug(SMB, "IPC pipe write request\n");
6222 return smb2_write_pipe(work);
6223 }
6224
6225 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
6226 ksmbd_debug(SMB, "User does not have write permission\n");
6227 err = -EACCES;
6228 goto out;
6229 }
6230
Namjae Jeon64b39f42021-03-30 14:25:35 +09006231 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
Namjae Jeon070fb212021-05-26 17:57:12 +09006232 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006233 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006234 err = -ENOENT;
6235 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006236 }
6237
6238 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006239 pr_err("Not permitted to write : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09006240 err = -EACCES;
6241 goto out;
6242 }
6243
6244 offset = le64_to_cpu(req->Offset);
6245 length = le32_to_cpu(req->Length);
6246
6247 if (length > work->conn->vals->max_write_size) {
6248 ksmbd_debug(SMB, "limiting write size to max size(%u)\n",
6249 work->conn->vals->max_write_size);
6250 err = -EINVAL;
6251 goto out;
6252 }
6253
6254 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6255 writethrough = true;
6256
6257 if (req->Channel != SMB2_CHANNEL_RDMA_V1 &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006258 req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006259 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeon070fb212021-05-26 17:57:12 +09006260 (offsetof(struct smb2_write_req, Buffer) - 4)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006261 data_buf = (char *)&req->Buffer[0];
6262 } else {
Namjae Jeon64b39f42021-03-30 14:25:35 +09006263 if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) ||
6264 (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006265 pr_err("invalid write data offset %u, smb_len %u\n",
6266 le16_to_cpu(req->DataOffset),
6267 get_rfc1002_len(req));
Namjae Jeone2f34482021-03-16 10:49:09 +09006268 err = -EINVAL;
6269 goto out;
6270 }
6271
6272 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6273 le16_to_cpu(req->DataOffset));
6274 }
6275
6276 ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags));
6277 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6278 writethrough = true;
6279
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006280 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6281 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006282 err = ksmbd_vfs_write(work, fp, data_buf, length, &offset,
6283 writethrough, &nbytes);
6284 if (err < 0)
6285 goto out;
6286 } else {
6287 /* read data from the client using rdma channel, and
6288 * write the data.
6289 */
6290 nbytes = smb2_write_rdma_channel(work, req, fp, offset,
Namjae Jeon070fb212021-05-26 17:57:12 +09006291 le32_to_cpu(req->RemainingBytes),
6292 writethrough);
Namjae Jeone2f34482021-03-16 10:49:09 +09006293 if (nbytes < 0) {
6294 err = (int)nbytes;
6295 goto out;
6296 }
6297 }
6298
6299 rsp->StructureSize = cpu_to_le16(17);
6300 rsp->DataOffset = 0;
6301 rsp->Reserved = 0;
6302 rsp->DataLength = cpu_to_le32(nbytes);
6303 rsp->DataRemaining = 0;
6304 rsp->Reserved2 = 0;
6305 inc_rfc1001_len(rsp_org, 16);
6306 ksmbd_fd_put(work, fp);
6307 return 0;
6308
6309out:
6310 if (err == -EAGAIN)
6311 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6312 else if (err == -ENOSPC || err == -EFBIG)
6313 rsp->hdr.Status = STATUS_DISK_FULL;
6314 else if (err == -ENOENT)
6315 rsp->hdr.Status = STATUS_FILE_CLOSED;
6316 else if (err == -EACCES)
6317 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6318 else if (err == -ESHARE)
6319 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6320 else if (err == -EINVAL)
6321 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6322 else
6323 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6324
6325 smb2_set_err_rsp(work);
6326 ksmbd_fd_put(work, fp);
6327 return err;
6328}
6329
6330/**
6331 * smb2_flush() - handler for smb2 flush file - fsync
6332 * @work: smb work containing flush command buffer
6333 *
6334 * Return: 0 on success, otherwise error
6335 */
6336int smb2_flush(struct ksmbd_work *work)
6337{
6338 struct smb2_flush_req *req;
6339 struct smb2_flush_rsp *rsp, *rsp_org;
6340 int err;
6341
Namjae Jeone5066492021-03-30 12:35:23 +09006342 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006343 WORK_BUFFERS(work, req, rsp);
6344
6345 ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006346 le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006347
6348 err = ksmbd_vfs_fsync(work,
6349 le64_to_cpu(req->VolatileFileId),
6350 le64_to_cpu(req->PersistentFileId));
6351 if (err)
6352 goto out;
6353
6354 rsp->StructureSize = cpu_to_le16(4);
6355 rsp->Reserved = 0;
6356 inc_rfc1001_len(rsp_org, 4);
6357 return 0;
6358
6359out:
6360 if (err) {
6361 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6362 smb2_set_err_rsp(work);
6363 }
6364
6365 return err;
6366}
6367
6368/**
6369 * smb2_cancel() - handler for smb2 cancel command
6370 * @work: smb work containing cancel command buffer
6371 *
6372 * Return: 0 on success, otherwise error
6373 */
6374int smb2_cancel(struct ksmbd_work *work)
6375{
6376 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09006377 struct smb2_hdr *hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006378 struct smb2_hdr *chdr;
6379 struct ksmbd_work *cancel_work = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006380 int canceled = 0;
6381 struct list_head *command_list;
6382
6383 ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006384 hdr->MessageId, hdr->Flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09006385
6386 if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) {
6387 command_list = &conn->async_requests;
6388
6389 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006390 list_for_each_entry(cancel_work, command_list,
6391 async_request_entry) {
Namjae Jeone5066492021-03-30 12:35:23 +09006392 chdr = cancel_work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006393
6394 if (cancel_work->async_id !=
Namjae Jeon64b39f42021-03-30 14:25:35 +09006395 le64_to_cpu(hdr->Id.AsyncId))
Namjae Jeone2f34482021-03-16 10:49:09 +09006396 continue;
6397
6398 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006399 "smb2 with AsyncId %llu cancelled command = 0x%x\n",
6400 le64_to_cpu(hdr->Id.AsyncId),
6401 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006402 canceled = 1;
6403 break;
6404 }
6405 spin_unlock(&conn->request_lock);
6406 } else {
6407 command_list = &conn->requests;
6408
6409 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006410 list_for_each_entry(cancel_work, command_list, request_entry) {
Namjae Jeone5066492021-03-30 12:35:23 +09006411 chdr = cancel_work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006412
6413 if (chdr->MessageId != hdr->MessageId ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006414 cancel_work == work)
Namjae Jeone2f34482021-03-16 10:49:09 +09006415 continue;
6416
6417 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006418 "smb2 with mid %llu cancelled command = 0x%x\n",
6419 le64_to_cpu(hdr->MessageId),
6420 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006421 canceled = 1;
6422 break;
6423 }
6424 spin_unlock(&conn->request_lock);
6425 }
6426
6427 if (canceled) {
6428 cancel_work->state = KSMBD_WORK_CANCELLED;
6429 if (cancel_work->cancel_fn)
6430 cancel_work->cancel_fn(cancel_work->cancel_argv);
6431 }
6432
6433 /* For SMB2_CANCEL command itself send no response*/
6434 work->send_no_response = 1;
6435 return 0;
6436}
6437
6438struct file_lock *smb_flock_init(struct file *f)
6439{
6440 struct file_lock *fl;
6441
6442 fl = locks_alloc_lock();
6443 if (!fl)
6444 goto out;
6445
6446 locks_init_lock(fl);
6447
6448 fl->fl_owner = f;
6449 fl->fl_pid = current->tgid;
6450 fl->fl_file = f;
6451 fl->fl_flags = FL_POSIX;
6452 fl->fl_ops = NULL;
6453 fl->fl_lmops = NULL;
6454
6455out:
6456 return fl;
6457}
6458
6459static int smb2_set_flock_flags(struct file_lock *flock, int flags)
6460{
6461 int cmd = -EINVAL;
6462
6463 /* Checking for wrong flag combination during lock request*/
6464 switch (flags) {
6465 case SMB2_LOCKFLAG_SHARED:
6466 ksmbd_debug(SMB, "received shared request\n");
6467 cmd = F_SETLKW;
6468 flock->fl_type = F_RDLCK;
6469 flock->fl_flags |= FL_SLEEP;
6470 break;
6471 case SMB2_LOCKFLAG_EXCLUSIVE:
6472 ksmbd_debug(SMB, "received exclusive request\n");
6473 cmd = F_SETLKW;
6474 flock->fl_type = F_WRLCK;
6475 flock->fl_flags |= FL_SLEEP;
6476 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006477 case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006478 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006479 "received shared & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006480 cmd = F_SETLK;
6481 flock->fl_type = F_RDLCK;
6482 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006483 case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006484 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006485 "received exclusive & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006486 cmd = F_SETLK;
6487 flock->fl_type = F_WRLCK;
6488 break;
6489 case SMB2_LOCKFLAG_UNLOCK:
6490 ksmbd_debug(SMB, "received unlock request\n");
6491 flock->fl_type = F_UNLCK;
6492 cmd = 0;
6493 break;
6494 }
6495
6496 return cmd;
6497}
6498
6499static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock,
Namjae Jeon070fb212021-05-26 17:57:12 +09006500 unsigned int cmd, int flags,
6501 struct list_head *lock_list)
Namjae Jeone2f34482021-03-16 10:49:09 +09006502{
6503 struct ksmbd_lock *lock;
6504
6505 lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL);
6506 if (!lock)
6507 return NULL;
6508
6509 lock->cmd = cmd;
6510 lock->fl = flock;
6511 lock->start = flock->fl_start;
6512 lock->end = flock->fl_end;
6513 lock->flags = flags;
6514 if (lock->start == lock->end)
6515 lock->zero_len = 1;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006516 INIT_LIST_HEAD(&lock->clist);
6517 INIT_LIST_HEAD(&lock->flist);
Namjae Jeone2f34482021-03-16 10:49:09 +09006518 INIT_LIST_HEAD(&lock->llist);
Namjae Jeone2f34482021-03-16 10:49:09 +09006519 list_add_tail(&lock->llist, lock_list);
6520
6521 return lock;
6522}
6523
6524static void smb2_remove_blocked_lock(void **argv)
6525{
6526 struct file_lock *flock = (struct file_lock *)argv[0];
6527
6528 ksmbd_vfs_posix_lock_unblock(flock);
6529 wake_up(&flock->fl_wait);
6530}
6531
6532static inline bool lock_defer_pending(struct file_lock *fl)
6533{
6534 /* check pending lock waiters */
6535 return waitqueue_active(&fl->fl_wait);
6536}
6537
6538/**
6539 * smb2_lock() - handler for smb2 file lock command
6540 * @work: smb work containing lock command buffer
6541 *
6542 * Return: 0 on success, otherwise error
6543 */
6544int smb2_lock(struct ksmbd_work *work)
6545{
Namjae Jeone5066492021-03-30 12:35:23 +09006546 struct smb2_lock_req *req = work->request_buf;
6547 struct smb2_lock_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006548 struct smb2_lock_element *lock_ele;
6549 struct ksmbd_file *fp = NULL;
6550 struct file_lock *flock = NULL;
6551 struct file *filp = NULL;
6552 int lock_count;
6553 int flags = 0;
6554 int cmd = 0;
6555 int err = 0, i;
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006556 u64 lock_start, lock_length;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006557 struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2;
6558 struct ksmbd_conn *conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09006559 int nolock = 0;
6560 LIST_HEAD(lock_list);
6561 LIST_HEAD(rollback_list);
6562 int prior_lock = 0;
6563
6564 ksmbd_debug(SMB, "Received lock request\n");
6565 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006566 le64_to_cpu(req->VolatileFileId),
6567 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006568 if (!fp) {
6569 ksmbd_debug(SMB, "Invalid file id for lock : %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006570 le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006571 rsp->hdr.Status = STATUS_FILE_CLOSED;
6572 goto out2;
6573 }
6574
6575 filp = fp->filp;
6576 lock_count = le16_to_cpu(req->LockCount);
6577 lock_ele = req->locks;
6578
6579 ksmbd_debug(SMB, "lock count is %d\n", lock_count);
Namjae Jeon070fb212021-05-26 17:57:12 +09006580 if (!lock_count) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006581 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6582 goto out2;
6583 }
6584
6585 for (i = 0; i < lock_count; i++) {
6586 flags = le32_to_cpu(lock_ele[i].Flags);
6587
6588 flock = smb_flock_init(filp);
6589 if (!flock) {
6590 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6591 goto out;
6592 }
6593
6594 cmd = smb2_set_flock_flags(flock, flags);
6595
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006596 lock_start = le64_to_cpu(lock_ele[i].Offset);
6597 lock_length = le64_to_cpu(lock_ele[i].Length);
6598 if (lock_start > U64_MAX - lock_length) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006599 pr_err("Invalid lock range requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006600 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6601 goto out;
6602 }
6603
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006604 if (lock_start > OFFSET_MAX)
6605 flock->fl_start = OFFSET_MAX;
6606 else
6607 flock->fl_start = lock_start;
6608
Namjae Jeone2f34482021-03-16 10:49:09 +09006609 lock_length = le64_to_cpu(lock_ele[i].Length);
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006610 if (lock_length > OFFSET_MAX - flock->fl_start)
6611 lock_length = OFFSET_MAX - flock->fl_start;
Namjae Jeone2f34482021-03-16 10:49:09 +09006612
6613 flock->fl_end = flock->fl_start + lock_length;
6614
6615 if (flock->fl_end < flock->fl_start) {
6616 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006617 "the end offset(%llx) is smaller than the start offset(%llx)\n",
6618 flock->fl_end, flock->fl_start);
Namjae Jeone2f34482021-03-16 10:49:09 +09006619 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6620 goto out;
6621 }
6622
6623 /* Check conflict locks in one request */
6624 list_for_each_entry(cmp_lock, &lock_list, llist) {
6625 if (cmp_lock->fl->fl_start <= flock->fl_start &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006626 cmp_lock->fl->fl_end >= flock->fl_end) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006627 if (cmp_lock->fl->fl_type != F_UNLCK &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006628 flock->fl_type != F_UNLCK) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006629 pr_err("conflict two locks in one request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006630 rsp->hdr.Status =
6631 STATUS_INVALID_PARAMETER;
6632 goto out;
6633 }
6634 }
6635 }
6636
6637 smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list);
6638 if (!smb_lock) {
6639 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6640 goto out;
6641 }
6642 }
6643
6644 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6645 if (smb_lock->cmd < 0) {
6646 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6647 goto out;
6648 }
6649
6650 if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) {
6651 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6652 goto out;
6653 }
6654
Namjae Jeon64b39f42021-03-30 14:25:35 +09006655 if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) &&
6656 smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) ||
6657 (prior_lock == SMB2_LOCKFLAG_UNLOCK &&
6658 !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006659 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6660 goto out;
6661 }
6662
6663 prior_lock = smb_lock->flags;
6664
6665 if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006666 !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY))
Hyunchul Leed63528e2021-07-10 16:22:41 +09006667 goto no_check_cl;
Namjae Jeone2f34482021-03-16 10:49:09 +09006668
6669 nolock = 1;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006670 /* check locks in connection list */
6671 read_lock(&conn_list_lock);
6672 list_for_each_entry(conn, &conn_list, conns_list) {
6673 spin_lock(&conn->llist_lock);
6674 list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) {
6675 if (file_inode(cmp_lock->fl->fl_file) !=
6676 file_inode(smb_lock->fl->fl_file))
6677 continue;
Namjae Jeone2f34482021-03-16 10:49:09 +09006678
Hyunchul Leed63528e2021-07-10 16:22:41 +09006679 if (smb_lock->fl->fl_type == F_UNLCK) {
6680 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file &&
6681 cmp_lock->start == smb_lock->start &&
6682 cmp_lock->end == smb_lock->end &&
6683 !lock_defer_pending(cmp_lock->fl)) {
6684 nolock = 0;
6685 list_del(&cmp_lock->flist);
6686 list_del(&cmp_lock->clist);
6687 spin_unlock(&conn->llist_lock);
6688 read_unlock(&conn_list_lock);
6689
6690 locks_free_lock(cmp_lock->fl);
6691 kfree(cmp_lock);
6692 goto out_check_cl;
6693 }
6694 continue;
Namjae Jeone2f34482021-03-16 10:49:09 +09006695 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006696
Hyunchul Leed63528e2021-07-10 16:22:41 +09006697 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) {
6698 if (smb_lock->flags & SMB2_LOCKFLAG_SHARED)
6699 continue;
6700 } else {
6701 if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED)
6702 continue;
6703 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006704
Hyunchul Leed63528e2021-07-10 16:22:41 +09006705 /* check zero byte lock range */
6706 if (cmp_lock->zero_len && !smb_lock->zero_len &&
6707 cmp_lock->start > smb_lock->start &&
6708 cmp_lock->start < smb_lock->end) {
6709 spin_unlock(&conn->llist_lock);
6710 read_unlock(&conn_list_lock);
6711 pr_err("previous lock conflict with zero byte lock range\n");
6712 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6713 goto out;
6714 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006715
Hyunchul Leed63528e2021-07-10 16:22:41 +09006716 if (smb_lock->zero_len && !cmp_lock->zero_len &&
6717 smb_lock->start > cmp_lock->start &&
6718 smb_lock->start < cmp_lock->end) {
6719 spin_unlock(&conn->llist_lock);
6720 read_unlock(&conn_list_lock);
6721 pr_err("current lock conflict with zero byte lock range\n");
6722 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6723 goto out;
6724 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006725
Hyunchul Leed63528e2021-07-10 16:22:41 +09006726 if (((cmp_lock->start <= smb_lock->start &&
6727 cmp_lock->end > smb_lock->start) ||
6728 (cmp_lock->start < smb_lock->end &&
6729 cmp_lock->end >= smb_lock->end)) &&
6730 !cmp_lock->zero_len && !smb_lock->zero_len) {
6731 spin_unlock(&conn->llist_lock);
6732 read_unlock(&conn_list_lock);
6733 pr_err("Not allow lock operation on exclusive lock range\n");
6734 rsp->hdr.Status =
6735 STATUS_LOCK_NOT_GRANTED;
6736 goto out;
6737 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006738 }
Hyunchul Leed63528e2021-07-10 16:22:41 +09006739 spin_unlock(&conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006740 }
Hyunchul Leed63528e2021-07-10 16:22:41 +09006741 read_unlock(&conn_list_lock);
6742out_check_cl:
Namjae Jeone2f34482021-03-16 10:49:09 +09006743 if (smb_lock->fl->fl_type == F_UNLCK && nolock) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006744 pr_err("Try to unlock nolocked range\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006745 rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED;
6746 goto out;
6747 }
6748
Hyunchul Leed63528e2021-07-10 16:22:41 +09006749no_check_cl:
Namjae Jeone2f34482021-03-16 10:49:09 +09006750 if (smb_lock->zero_len) {
6751 err = 0;
6752 goto skip;
6753 }
6754
6755 flock = smb_lock->fl;
6756 list_del(&smb_lock->llist);
6757retry:
Namjae Jeone8c06192021-06-22 11:06:11 +09006758 err = vfs_lock_file(filp, smb_lock->cmd, flock, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09006759skip:
6760 if (flags & SMB2_LOCKFLAG_UNLOCK) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09006761 if (!err) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006762 ksmbd_debug(SMB, "File unlocked\n");
Namjae Jeon64b39f42021-03-30 14:25:35 +09006763 } else if (err == -ENOENT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006764 rsp->hdr.Status = STATUS_NOT_LOCKED;
6765 goto out;
6766 }
6767 locks_free_lock(flock);
6768 kfree(smb_lock);
6769 } else {
6770 if (err == FILE_LOCK_DEFERRED) {
6771 void **argv;
6772
6773 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006774 "would have to wait for getting lock\n");
Hyunchul Leed63528e2021-07-10 16:22:41 +09006775 spin_lock(&work->conn->llist_lock);
6776 list_add_tail(&smb_lock->clist,
6777 &work->conn->lock_list);
6778 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006779 list_add(&smb_lock->llist, &rollback_list);
6780
6781 argv = kmalloc(sizeof(void *), GFP_KERNEL);
6782 if (!argv) {
6783 err = -ENOMEM;
6784 goto out;
6785 }
6786 argv[0] = flock;
6787
6788 err = setup_async_work(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006789 smb2_remove_blocked_lock,
6790 argv);
Namjae Jeone2f34482021-03-16 10:49:09 +09006791 if (err) {
6792 rsp->hdr.Status =
6793 STATUS_INSUFFICIENT_RESOURCES;
6794 goto out;
6795 }
6796 spin_lock(&fp->f_lock);
6797 list_add(&work->fp_entry, &fp->blocked_works);
6798 spin_unlock(&fp->f_lock);
6799
6800 smb2_send_interim_resp(work, STATUS_PENDING);
6801
Hyunchul Lee45a64e82021-07-10 09:34:20 +09006802 ksmbd_vfs_posix_lock_wait(flock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006803
Hyunchul Leed4075ab2021-06-25 07:02:10 +09006804 if (work->state != KSMBD_WORK_ACTIVE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006805 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09006806 spin_lock(&work->conn->llist_lock);
6807 list_del(&smb_lock->clist);
6808 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006809 locks_free_lock(flock);
6810
Hyunchul Leed4075ab2021-06-25 07:02:10 +09006811 if (work->state == KSMBD_WORK_CANCELLED) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006812 spin_lock(&fp->f_lock);
6813 list_del(&work->fp_entry);
6814 spin_unlock(&fp->f_lock);
6815 rsp->hdr.Status =
6816 STATUS_CANCELLED;
6817 kfree(smb_lock);
6818 smb2_send_interim_resp(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006819 STATUS_CANCELLED);
Namjae Jeone2f34482021-03-16 10:49:09 +09006820 work->send_no_response = 1;
6821 goto out;
6822 }
6823 init_smb2_rsp_hdr(work);
6824 smb2_set_err_rsp(work);
6825 rsp->hdr.Status =
6826 STATUS_RANGE_NOT_LOCKED;
6827 kfree(smb_lock);
6828 goto out2;
6829 }
6830
6831 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09006832 spin_lock(&work->conn->llist_lock);
6833 list_del(&smb_lock->clist);
6834 spin_unlock(&work->conn->llist_lock);
6835
Namjae Jeone2f34482021-03-16 10:49:09 +09006836 spin_lock(&fp->f_lock);
6837 list_del(&work->fp_entry);
6838 spin_unlock(&fp->f_lock);
6839 goto retry;
6840 } else if (!err) {
Hyunchul Leed63528e2021-07-10 16:22:41 +09006841 spin_lock(&work->conn->llist_lock);
6842 list_add_tail(&smb_lock->clist,
6843 &work->conn->lock_list);
6844 list_add_tail(&smb_lock->flist,
6845 &fp->lock_list);
6846 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006847 list_add(&smb_lock->llist, &rollback_list);
6848 ksmbd_debug(SMB, "successful in taking lock\n");
6849 } else {
6850 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6851 goto out;
6852 }
6853 }
6854 }
6855
6856 if (atomic_read(&fp->f_ci->op_count) > 1)
6857 smb_break_all_oplock(work, fp);
6858
6859 rsp->StructureSize = cpu_to_le16(4);
6860 ksmbd_debug(SMB, "successful in taking lock\n");
6861 rsp->hdr.Status = STATUS_SUCCESS;
6862 rsp->Reserved = 0;
6863 inc_rfc1001_len(rsp, 4);
6864 ksmbd_fd_put(work, fp);
6865 return err;
6866
6867out:
6868 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6869 locks_free_lock(smb_lock->fl);
6870 list_del(&smb_lock->llist);
6871 kfree(smb_lock);
6872 }
6873
6874 list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) {
6875 struct file_lock *rlock = NULL;
6876
6877 rlock = smb_flock_init(filp);
6878 rlock->fl_type = F_UNLCK;
6879 rlock->fl_start = smb_lock->start;
6880 rlock->fl_end = smb_lock->end;
6881
Namjae Jeone8c06192021-06-22 11:06:11 +09006882 err = vfs_lock_file(filp, 0, rlock, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09006883 if (err)
Namjae Jeonbde16942021-06-28 15:23:19 +09006884 pr_err("rollback unlock fail : %d\n", err);
Hyunchul Leed63528e2021-07-10 16:22:41 +09006885
Namjae Jeone2f34482021-03-16 10:49:09 +09006886 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09006887 spin_lock(&work->conn->llist_lock);
6888 if (!list_empty(&smb_lock->flist))
6889 list_del(&smb_lock->flist);
6890 list_del(&smb_lock->clist);
6891 spin_unlock(&work->conn->llist_lock);
6892
Namjae Jeone2f34482021-03-16 10:49:09 +09006893 locks_free_lock(smb_lock->fl);
6894 locks_free_lock(rlock);
6895 kfree(smb_lock);
6896 }
6897out2:
6898 ksmbd_debug(SMB, "failed in taking lock(flags : %x)\n", flags);
6899 smb2_set_err_rsp(work);
6900 ksmbd_fd_put(work, fp);
6901 return 0;
6902}
6903
Namjae Jeon64b39f42021-03-30 14:25:35 +09006904static int fsctl_copychunk(struct ksmbd_work *work, struct smb2_ioctl_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09006905 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09006906{
6907 struct copychunk_ioctl_req *ci_req;
6908 struct copychunk_ioctl_rsp *ci_rsp;
6909 struct ksmbd_file *src_fp = NULL, *dst_fp = NULL;
6910 struct srv_copychunk *chunks;
6911 unsigned int i, chunk_count, chunk_count_written = 0;
6912 unsigned int chunk_size_written = 0;
6913 loff_t total_size_written = 0;
6914 int ret, cnt_code;
6915
6916 cnt_code = le32_to_cpu(req->CntCode);
6917 ci_req = (struct copychunk_ioctl_req *)&req->Buffer[0];
6918 ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0];
6919
6920 rsp->VolatileFileId = req->VolatileFileId;
6921 rsp->PersistentFileId = req->PersistentFileId;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006922 ci_rsp->ChunksWritten =
6923 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count());
6924 ci_rsp->ChunkBytesWritten =
6925 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size());
6926 ci_rsp->TotalBytesWritten =
6927 cpu_to_le32(ksmbd_server_side_copy_max_total_size());
Namjae Jeone2f34482021-03-16 10:49:09 +09006928
6929 chunks = (struct srv_copychunk *)&ci_req->Chunks[0];
6930 chunk_count = le32_to_cpu(ci_req->ChunkCount);
6931 total_size_written = 0;
6932
6933 /* verify the SRV_COPYCHUNK_COPY packet */
6934 if (chunk_count > ksmbd_server_side_copy_max_chunk_count() ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006935 le32_to_cpu(req->InputCount) <
6936 offsetof(struct copychunk_ioctl_req, Chunks) +
6937 chunk_count * sizeof(struct srv_copychunk)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006938 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6939 return -EINVAL;
6940 }
6941
6942 for (i = 0; i < chunk_count; i++) {
6943 if (le32_to_cpu(chunks[i].Length) == 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006944 le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size())
Namjae Jeone2f34482021-03-16 10:49:09 +09006945 break;
6946 total_size_written += le32_to_cpu(chunks[i].Length);
6947 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09006948
6949 if (i < chunk_count ||
6950 total_size_written > ksmbd_server_side_copy_max_total_size()) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006951 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6952 return -EINVAL;
6953 }
6954
6955 src_fp = ksmbd_lookup_foreign_fd(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006956 le64_to_cpu(ci_req->ResumeKey[0]));
Namjae Jeone2f34482021-03-16 10:49:09 +09006957 dst_fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006958 le64_to_cpu(req->VolatileFileId),
6959 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006960 ret = -EINVAL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006961 if (!src_fp ||
6962 src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006963 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
6964 goto out;
6965 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09006966
Namjae Jeone2f34482021-03-16 10:49:09 +09006967 if (!dst_fp) {
6968 rsp->hdr.Status = STATUS_FILE_CLOSED;
6969 goto out;
6970 }
6971
6972 /*
6973 * FILE_READ_DATA should only be included in
6974 * the FSCTL_COPYCHUNK case
6975 */
Namjae Jeon070fb212021-05-26 17:57:12 +09006976 if (cnt_code == FSCTL_COPYCHUNK &&
6977 !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006978 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6979 goto out;
6980 }
6981
6982 ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09006983 chunks, chunk_count,
6984 &chunk_count_written,
6985 &chunk_size_written,
6986 &total_size_written);
Namjae Jeone2f34482021-03-16 10:49:09 +09006987 if (ret < 0) {
6988 if (ret == -EACCES)
6989 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6990 if (ret == -EAGAIN)
6991 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6992 else if (ret == -EBADF)
6993 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6994 else if (ret == -EFBIG || ret == -ENOSPC)
6995 rsp->hdr.Status = STATUS_DISK_FULL;
6996 else if (ret == -EINVAL)
6997 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6998 else if (ret == -EISDIR)
6999 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
7000 else if (ret == -E2BIG)
7001 rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE;
7002 else
7003 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
7004 }
7005
7006 ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written);
7007 ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written);
7008 ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written);
7009out:
7010 ksmbd_fd_put(work, src_fp);
7011 ksmbd_fd_put(work, dst_fp);
7012 return ret;
7013}
7014
7015static __be32 idev_ipv4_address(struct in_device *idev)
7016{
7017 __be32 addr = 0;
7018
7019 struct in_ifaddr *ifa;
7020
7021 rcu_read_lock();
7022 in_dev_for_each_ifa_rcu(ifa, idev) {
7023 if (ifa->ifa_flags & IFA_F_SECONDARY)
7024 continue;
7025
7026 addr = ifa->ifa_address;
7027 break;
7028 }
7029 rcu_read_unlock();
7030 return addr;
7031}
7032
7033static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09007034 struct smb2_ioctl_req *req,
7035 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007036{
7037 struct network_interface_info_ioctl_rsp *nii_rsp = NULL;
7038 int nbytes = 0;
7039 struct net_device *netdev;
7040 struct sockaddr_storage_rsp *sockaddr_storage;
7041 unsigned int flags;
7042 unsigned long long speed;
7043
7044 rtnl_lock();
7045 for_each_netdev(&init_net, netdev) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007046 if (netdev->type == ARPHRD_LOOPBACK)
7047 continue;
7048
7049 flags = dev_get_flags(netdev);
7050 if (!(flags & IFF_RUNNING))
7051 continue;
7052
7053 nii_rsp = (struct network_interface_info_ioctl_rsp *)
7054 &rsp->Buffer[nbytes];
7055 nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex);
7056
7057 /* TODO: specify the RDMA capabilities */
7058 if (netdev->num_tx_queues > 1)
7059 nii_rsp->Capability = cpu_to_le32(RSS_CAPABLE);
7060 else
7061 nii_rsp->Capability = 0;
7062
7063 nii_rsp->Next = cpu_to_le32(152);
7064 nii_rsp->Reserved = 0;
7065
7066 if (netdev->ethtool_ops->get_link_ksettings) {
7067 struct ethtool_link_ksettings cmd;
7068
7069 netdev->ethtool_ops->get_link_ksettings(netdev, &cmd);
7070 speed = cmd.base.speed;
7071 } else {
Namjae Jeonbde16942021-06-28 15:23:19 +09007072 pr_err("%s %s\n", netdev->name,
7073 "speed is unknown, defaulting to 1Gb/sec");
Namjae Jeone2f34482021-03-16 10:49:09 +09007074 speed = SPEED_1000;
7075 }
7076
7077 speed *= 1000000;
7078 nii_rsp->LinkSpeed = cpu_to_le64(speed);
7079
7080 sockaddr_storage = (struct sockaddr_storage_rsp *)
7081 nii_rsp->SockAddr_Storage;
7082 memset(sockaddr_storage, 0, 128);
7083
7084 if (conn->peer_addr.ss_family == PF_INET) {
7085 struct in_device *idev;
7086
7087 sockaddr_storage->Family = cpu_to_le16(INTERNETWORK);
7088 sockaddr_storage->addr4.Port = 0;
7089
7090 idev = __in_dev_get_rtnl(netdev);
7091 if (!idev)
7092 continue;
7093 sockaddr_storage->addr4.IPv4address =
7094 idev_ipv4_address(idev);
7095 } else {
7096 struct inet6_dev *idev6;
7097 struct inet6_ifaddr *ifa;
7098 __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address;
7099
7100 sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6);
7101 sockaddr_storage->addr6.Port = 0;
7102 sockaddr_storage->addr6.FlowInfo = 0;
7103
7104 idev6 = __in6_dev_get(netdev);
7105 if (!idev6)
7106 continue;
7107
7108 list_for_each_entry(ifa, &idev6->addr_list, if_list) {
7109 if (ifa->flags & (IFA_F_TENTATIVE |
7110 IFA_F_DEPRECATED))
7111 continue;
7112 memcpy(ipv6_addr, ifa->addr.s6_addr, 16);
7113 break;
7114 }
7115 sockaddr_storage->addr6.ScopeId = 0;
7116 }
7117
7118 nbytes += sizeof(struct network_interface_info_ioctl_rsp);
7119 }
7120 rtnl_unlock();
7121
7122 /* zero if this is last one */
7123 if (nii_rsp)
7124 nii_rsp->Next = 0;
7125
7126 if (!nbytes) {
7127 rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL;
7128 return -EINVAL;
7129 }
7130
7131 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7132 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7133 return nbytes;
7134}
7135
Namjae Jeone2f34482021-03-16 10:49:09 +09007136static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09007137 struct validate_negotiate_info_req *neg_req,
7138 struct validate_negotiate_info_rsp *neg_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007139{
7140 int ret = 0;
7141 int dialect;
7142
7143 dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects,
Namjae Jeon070fb212021-05-26 17:57:12 +09007144 neg_req->DialectCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09007145 if (dialect == BAD_PROT_ID || dialect != conn->dialect) {
7146 ret = -EINVAL;
7147 goto err_out;
7148 }
7149
7150 if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) {
7151 ret = -EINVAL;
7152 goto err_out;
7153 }
7154
7155 if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) {
7156 ret = -EINVAL;
7157 goto err_out;
7158 }
7159
7160 if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) {
7161 ret = -EINVAL;
7162 goto err_out;
7163 }
7164
7165 neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
7166 memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE);
7167 neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode);
7168 neg_rsp->Dialect = cpu_to_le16(conn->dialect);
7169err_out:
7170 return ret;
7171}
7172
Namjae Jeon64b39f42021-03-30 14:25:35 +09007173static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007174 struct file_allocated_range_buffer *qar_req,
7175 struct file_allocated_range_buffer *qar_rsp,
7176 int in_count, int *out_count)
Namjae Jeone2f34482021-03-16 10:49:09 +09007177{
7178 struct ksmbd_file *fp;
7179 loff_t start, length;
7180 int ret = 0;
7181
7182 *out_count = 0;
7183 if (in_count == 0)
7184 return -EINVAL;
7185
7186 fp = ksmbd_lookup_fd_fast(work, id);
7187 if (!fp)
7188 return -ENOENT;
7189
7190 start = le64_to_cpu(qar_req->file_offset);
7191 length = le64_to_cpu(qar_req->length);
7192
7193 ret = ksmbd_vfs_fqar_lseek(fp, start, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09007194 qar_rsp, in_count, out_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09007195 if (ret && ret != -E2BIG)
7196 *out_count = 0;
7197
7198 ksmbd_fd_put(work, fp);
7199 return ret;
7200}
7201
Namjae Jeon64b39f42021-03-30 14:25:35 +09007202static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007203 int out_buf_len, struct smb2_ioctl_req *req,
7204 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007205{
7206 struct ksmbd_rpc_command *rpc_resp;
7207 char *data_buf = (char *)&req->Buffer[0];
7208 int nbytes = 0;
7209
Namjae Jeon64b39f42021-03-30 14:25:35 +09007210 rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09007211 le32_to_cpu(req->InputCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09007212 if (rpc_resp) {
7213 if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) {
7214 /*
7215 * set STATUS_SOME_NOT_MAPPED response
7216 * for unknown domain sid.
7217 */
7218 rsp->hdr.Status = STATUS_SOME_NOT_MAPPED;
7219 } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
7220 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7221 goto out;
7222 } else if (rpc_resp->flags != KSMBD_RPC_OK) {
7223 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7224 goto out;
7225 }
7226
7227 nbytes = rpc_resp->payload_sz;
7228 if (rpc_resp->payload_sz > out_buf_len) {
7229 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7230 nbytes = out_buf_len;
7231 }
7232
7233 if (!rpc_resp->payload_sz) {
7234 rsp->hdr.Status =
7235 STATUS_UNEXPECTED_IO_ERROR;
7236 goto out;
7237 }
7238
7239 memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes);
7240 }
7241out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09007242 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007243 return nbytes;
7244}
7245
Namjae Jeon64b39f42021-03-30 14:25:35 +09007246static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007247 struct file_sparse *sparse)
Namjae Jeone2f34482021-03-16 10:49:09 +09007248{
7249 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007250 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09007251 int ret = 0;
7252 __le32 old_fattr;
7253
7254 fp = ksmbd_lookup_fd_fast(work, id);
7255 if (!fp)
7256 return -ENOENT;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007257 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007258
7259 old_fattr = fp->f_ci->m_fattr;
7260 if (sparse->SetSparse)
7261 fp->f_ci->m_fattr |= ATTR_SPARSE_FILE_LE;
7262 else
7263 fp->f_ci->m_fattr &= ~ATTR_SPARSE_FILE_LE;
7264
7265 if (fp->f_ci->m_fattr != old_fattr &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007266 test_share_config_flag(work->tcon->share_conf,
7267 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007268 struct xattr_dos_attrib da;
7269
Hyunchul Lee465d7202021-07-03 12:10:36 +09007270 ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007271 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007272 if (ret <= 0)
7273 goto out;
7274
7275 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
Hyunchul Lee465d7202021-07-03 12:10:36 +09007276 ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007277 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007278 if (ret)
7279 fp->f_ci->m_fattr = old_fattr;
7280 }
7281
7282out:
7283 ksmbd_fd_put(work, fp);
7284 return ret;
7285}
7286
7287static int fsctl_request_resume_key(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007288 struct smb2_ioctl_req *req,
7289 struct resume_key_ioctl_rsp *key_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007290{
7291 struct ksmbd_file *fp;
7292
7293 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007294 le64_to_cpu(req->VolatileFileId),
7295 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09007296 if (!fp)
7297 return -ENOENT;
7298
7299 memset(key_rsp, 0, sizeof(*key_rsp));
7300 key_rsp->ResumeKey[0] = req->VolatileFileId;
7301 key_rsp->ResumeKey[1] = req->PersistentFileId;
7302 ksmbd_fd_put(work, fp);
7303
7304 return 0;
7305}
7306
7307/**
7308 * smb2_ioctl() - handler for smb2 ioctl command
7309 * @work: smb work containing ioctl command buffer
7310 *
7311 * Return: 0 on success, otherwise error
7312 */
7313int smb2_ioctl(struct ksmbd_work *work)
7314{
7315 struct smb2_ioctl_req *req;
7316 struct smb2_ioctl_rsp *rsp, *rsp_org;
7317 int cnt_code, nbytes = 0;
7318 int out_buf_len;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007319 u64 id = KSMBD_NO_FID;
Namjae Jeone2f34482021-03-16 10:49:09 +09007320 struct ksmbd_conn *conn = work->conn;
7321 int ret = 0;
7322
Namjae Jeone5066492021-03-30 12:35:23 +09007323 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007324 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09007325 req = ksmbd_req_buf_next(work);
7326 rsp = ksmbd_resp_buf_next(work);
Namjae Jeon38673692021-07-08 12:32:27 +09007327 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
7328 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007329 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09007330 id = work->compound_fid;
7331 }
7332 } else {
Namjae Jeone5066492021-03-30 12:35:23 +09007333 req = work->request_buf;
7334 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007335 }
7336
Namjae Jeon38673692021-07-08 12:32:27 +09007337 if (!has_file_id(id))
Namjae Jeone2f34482021-03-16 10:49:09 +09007338 id = le64_to_cpu(req->VolatileFileId);
7339
7340 if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) {
7341 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7342 goto out;
7343 }
7344
7345 cnt_code = le32_to_cpu(req->CntCode);
7346 out_buf_len = le32_to_cpu(req->MaxOutputResponse);
7347 out_buf_len = min(KSMBD_IPC_MAX_PAYLOAD, out_buf_len);
7348
7349 switch (cnt_code) {
7350 case FSCTL_DFS_GET_REFERRALS:
7351 case FSCTL_DFS_GET_REFERRALS_EX:
7352 /* Not support DFS yet */
7353 rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED;
7354 goto out;
7355 case FSCTL_CREATE_OR_GET_OBJECT_ID:
7356 {
7357 struct file_object_buf_type1_ioctl_rsp *obj_buf;
7358
7359 nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp);
7360 obj_buf = (struct file_object_buf_type1_ioctl_rsp *)
7361 &rsp->Buffer[0];
7362
7363 /*
7364 * TODO: This is dummy implementation to pass smbtorture
7365 * Need to check correct response later
7366 */
7367 memset(obj_buf->ObjectId, 0x0, 16);
7368 memset(obj_buf->BirthVolumeId, 0x0, 16);
7369 memset(obj_buf->BirthObjectId, 0x0, 16);
7370 memset(obj_buf->DomainId, 0x0, 16);
7371
7372 break;
7373 }
7374 case FSCTL_PIPE_TRANSCEIVE:
7375 nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp);
7376 break;
7377 case FSCTL_VALIDATE_NEGOTIATE_INFO:
7378 if (conn->dialect < SMB30_PROT_ID) {
7379 ret = -EOPNOTSUPP;
7380 goto out;
7381 }
7382
7383 ret = fsctl_validate_negotiate_info(conn,
7384 (struct validate_negotiate_info_req *)&req->Buffer[0],
7385 (struct validate_negotiate_info_rsp *)&rsp->Buffer[0]);
7386 if (ret < 0)
7387 goto out;
7388
7389 nbytes = sizeof(struct validate_negotiate_info_rsp);
7390 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7391 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7392 break;
7393 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
7394 nbytes = fsctl_query_iface_info_ioctl(conn, req, rsp);
7395 if (nbytes < 0)
7396 goto out;
7397 break;
7398 case FSCTL_REQUEST_RESUME_KEY:
7399 if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) {
7400 ret = -EINVAL;
7401 goto out;
7402 }
7403
7404 ret = fsctl_request_resume_key(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007405 (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007406 if (ret < 0)
7407 goto out;
7408 rsp->PersistentFileId = req->PersistentFileId;
7409 rsp->VolatileFileId = req->VolatileFileId;
7410 nbytes = sizeof(struct resume_key_ioctl_rsp);
7411 break;
7412 case FSCTL_COPYCHUNK:
7413 case FSCTL_COPYCHUNK_WRITE:
Namjae Jeon64b39f42021-03-30 14:25:35 +09007414 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007415 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007416 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007417 ret = -EACCES;
7418 goto out;
7419 }
7420
7421 if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) {
7422 ret = -EINVAL;
7423 goto out;
7424 }
7425
7426 nbytes = sizeof(struct copychunk_ioctl_rsp);
7427 fsctl_copychunk(work, req, rsp);
7428 break;
7429 case FSCTL_SET_SPARSE:
7430 ret = fsctl_set_sparse(work, id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007431 (struct file_sparse *)&req->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007432 if (ret < 0)
7433 goto out;
7434 break;
7435 case FSCTL_SET_ZERO_DATA:
7436 {
7437 struct file_zero_data_information *zero_data;
7438 struct ksmbd_file *fp;
7439 loff_t off, len;
7440
Namjae Jeon64b39f42021-03-30 14:25:35 +09007441 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007442 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007443 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007444 ret = -EACCES;
7445 goto out;
7446 }
7447
7448 zero_data =
7449 (struct file_zero_data_information *)&req->Buffer[0];
7450
7451 fp = ksmbd_lookup_fd_fast(work, id);
7452 if (!fp) {
7453 ret = -ENOENT;
7454 goto out;
7455 }
7456
7457 off = le64_to_cpu(zero_data->FileOffset);
7458 len = le64_to_cpu(zero_data->BeyondFinalZero) - off;
7459
7460 ret = ksmbd_vfs_zero_data(work, fp, off, len);
7461 ksmbd_fd_put(work, fp);
7462 if (ret < 0)
7463 goto out;
7464 break;
7465 }
7466 case FSCTL_QUERY_ALLOCATED_RANGES:
7467 ret = fsctl_query_allocated_ranges(work, id,
7468 (struct file_allocated_range_buffer *)&req->Buffer[0],
7469 (struct file_allocated_range_buffer *)&rsp->Buffer[0],
7470 out_buf_len /
7471 sizeof(struct file_allocated_range_buffer), &nbytes);
7472 if (ret == -E2BIG) {
7473 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7474 } else if (ret < 0) {
7475 nbytes = 0;
7476 goto out;
7477 }
7478
7479 nbytes *= sizeof(struct file_allocated_range_buffer);
7480 break;
7481 case FSCTL_GET_REPARSE_POINT:
7482 {
7483 struct reparse_data_buffer *reparse_ptr;
7484 struct ksmbd_file *fp;
7485
7486 reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0];
7487 fp = ksmbd_lookup_fd_fast(work, id);
7488 if (!fp) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007489 pr_err("not found fp!!\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007490 ret = -ENOENT;
7491 goto out;
7492 }
7493
7494 reparse_ptr->ReparseTag =
Namjae Jeonab0b2632021-06-29 09:20:13 +09007495 smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode);
Namjae Jeone2f34482021-03-16 10:49:09 +09007496 reparse_ptr->ReparseDataLength = 0;
7497 ksmbd_fd_put(work, fp);
7498 nbytes = sizeof(struct reparse_data_buffer);
7499 break;
7500 }
Namjae Jeoneb817362021-05-18 10:37:59 +09007501 case FSCTL_DUPLICATE_EXTENTS_TO_FILE:
7502 {
7503 struct ksmbd_file *fp_in, *fp_out = NULL;
7504 struct duplicate_extents_to_file *dup_ext;
7505 loff_t src_off, dst_off, length, cloned;
7506
7507 dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0];
7508
7509 fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle,
Namjae Jeon070fb212021-05-26 17:57:12 +09007510 dup_ext->PersistentFileHandle);
Namjae Jeoneb817362021-05-18 10:37:59 +09007511 if (!fp_in) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007512 pr_err("not found file handle in duplicate extent to file\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007513 ret = -ENOENT;
7514 goto out;
7515 }
7516
7517 fp_out = ksmbd_lookup_fd_fast(work, id);
7518 if (!fp_out) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007519 pr_err("not found fp\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007520 ret = -ENOENT;
7521 goto dup_ext_out;
7522 }
7523
7524 src_off = le64_to_cpu(dup_ext->SourceFileOffset);
7525 dst_off = le64_to_cpu(dup_ext->TargetFileOffset);
7526 length = le64_to_cpu(dup_ext->ByteCount);
7527 cloned = vfs_clone_file_range(fp_in->filp, src_off, fp_out->filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007528 dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007529 if (cloned == -EXDEV || cloned == -EOPNOTSUPP) {
7530 ret = -EOPNOTSUPP;
7531 goto dup_ext_out;
7532 } else if (cloned != length) {
Namjae Jeonf8524772021-06-18 10:28:00 +09007533 cloned = vfs_copy_file_range(fp_in->filp, src_off,
7534 fp_out->filp, dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007535 if (cloned != length) {
7536 if (cloned < 0)
7537 ret = cloned;
7538 else
7539 ret = -EINVAL;
7540 }
7541 }
7542
7543dup_ext_out:
7544 ksmbd_fd_put(work, fp_in);
7545 ksmbd_fd_put(work, fp_out);
7546 if (ret < 0)
7547 goto out;
7548 break;
7549 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007550 default:
7551 ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007552 cnt_code);
Namjae Jeone2f34482021-03-16 10:49:09 +09007553 ret = -EOPNOTSUPP;
7554 goto out;
7555 }
7556
7557 rsp->CntCode = cpu_to_le32(cnt_code);
7558 rsp->InputCount = cpu_to_le32(0);
7559 rsp->InputOffset = cpu_to_le32(112);
7560 rsp->OutputOffset = cpu_to_le32(112);
7561 rsp->OutputCount = cpu_to_le32(nbytes);
7562 rsp->StructureSize = cpu_to_le16(49);
7563 rsp->Reserved = cpu_to_le16(0);
7564 rsp->Flags = cpu_to_le32(0);
7565 rsp->Reserved2 = cpu_to_le32(0);
7566 inc_rfc1001_len(rsp_org, 48 + nbytes);
7567
7568 return 0;
7569
7570out:
7571 if (ret == -EACCES)
7572 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7573 else if (ret == -ENOENT)
7574 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7575 else if (ret == -EOPNOTSUPP)
7576 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7577 else if (ret < 0 || rsp->hdr.Status == 0)
7578 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7579 smb2_set_err_rsp(work);
7580 return 0;
7581}
7582
7583/**
7584 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command
7585 * @work: smb work containing oplock break command buffer
7586 *
7587 * Return: 0
7588 */
7589static void smb20_oplock_break_ack(struct ksmbd_work *work)
7590{
Namjae Jeone5066492021-03-30 12:35:23 +09007591 struct smb2_oplock_break *req = work->request_buf;
7592 struct smb2_oplock_break *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007593 struct ksmbd_file *fp;
7594 struct oplock_info *opinfo = NULL;
7595 __le32 err = 0;
7596 int ret = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007597 u64 volatile_id, persistent_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09007598 char req_oplevel = 0, rsp_oplevel = 0;
7599 unsigned int oplock_change_type;
7600
7601 volatile_id = le64_to_cpu(req->VolatileFid);
7602 persistent_id = le64_to_cpu(req->PersistentFid);
7603 req_oplevel = req->OplockLevel;
7604 ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n",
7605 volatile_id, persistent_id, req_oplevel);
7606
7607 fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
7608 if (!fp) {
7609 rsp->hdr.Status = STATUS_FILE_CLOSED;
7610 smb2_set_err_rsp(work);
7611 return;
7612 }
7613
7614 opinfo = opinfo_get(fp);
7615 if (!opinfo) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007616 pr_err("unexpected null oplock_info\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007617 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7618 smb2_set_err_rsp(work);
7619 ksmbd_fd_put(work, fp);
7620 return;
7621 }
7622
7623 if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) {
7624 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7625 goto err_out;
7626 }
7627
7628 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7629 ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state);
7630 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7631 goto err_out;
7632 }
7633
Namjae Jeon64b39f42021-03-30 14:25:35 +09007634 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7635 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7636 (req_oplevel != SMB2_OPLOCK_LEVEL_II &&
7637 req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007638 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7639 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007640 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7641 req_oplevel != SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007642 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7643 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007644 } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II ||
7645 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007646 err = STATUS_INVALID_DEVICE_STATE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007647 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7648 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7649 req_oplevel == SMB2_OPLOCK_LEVEL_II) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007650 oplock_change_type = OPLOCK_WRITE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007651 } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7652 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7653 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007654 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007655 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7656 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007657 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007658 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007659 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007660 }
7661 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007662 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007663 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007664
7665 switch (oplock_change_type) {
7666 case OPLOCK_WRITE_TO_READ:
7667 ret = opinfo_write_to_read(opinfo);
7668 rsp_oplevel = SMB2_OPLOCK_LEVEL_II;
7669 break;
7670 case OPLOCK_WRITE_TO_NONE:
7671 ret = opinfo_write_to_none(opinfo);
7672 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7673 break;
7674 case OPLOCK_READ_TO_NONE:
7675 ret = opinfo_read_to_none(opinfo);
7676 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7677 break;
7678 default:
Namjae Jeonbde16942021-06-28 15:23:19 +09007679 pr_err("unknown oplock change 0x%x -> 0x%x\n",
7680 opinfo->level, rsp_oplevel);
Namjae Jeone2f34482021-03-16 10:49:09 +09007681 }
7682
7683 if (ret < 0) {
7684 rsp->hdr.Status = err;
7685 goto err_out;
7686 }
7687
7688 opinfo_put(opinfo);
7689 ksmbd_fd_put(work, fp);
7690 opinfo->op_state = OPLOCK_STATE_NONE;
7691 wake_up_interruptible_all(&opinfo->oplock_q);
7692
7693 rsp->StructureSize = cpu_to_le16(24);
7694 rsp->OplockLevel = rsp_oplevel;
7695 rsp->Reserved = 0;
7696 rsp->Reserved2 = 0;
7697 rsp->VolatileFid = cpu_to_le64(volatile_id);
7698 rsp->PersistentFid = cpu_to_le64(persistent_id);
7699 inc_rfc1001_len(rsp, 24);
7700 return;
7701
7702err_out:
7703 opinfo->op_state = OPLOCK_STATE_NONE;
7704 wake_up_interruptible_all(&opinfo->oplock_q);
7705
7706 opinfo_put(opinfo);
7707 ksmbd_fd_put(work, fp);
7708 smb2_set_err_rsp(work);
7709}
7710
7711static int check_lease_state(struct lease *lease, __le32 req_state)
7712{
7713 if ((lease->new_state ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09007714 (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) &&
7715 !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007716 lease->new_state = req_state;
7717 return 0;
7718 }
7719
7720 if (lease->new_state == req_state)
7721 return 0;
7722
7723 return 1;
7724}
7725
7726/**
7727 * smb21_lease_break_ack() - handler for smb2.1 lease break command
7728 * @work: smb work containing lease break command buffer
7729 *
7730 * Return: 0
7731 */
7732static void smb21_lease_break_ack(struct ksmbd_work *work)
7733{
7734 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09007735 struct smb2_lease_ack *req = work->request_buf;
7736 struct smb2_lease_ack *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007737 struct oplock_info *opinfo;
7738 __le32 err = 0;
7739 int ret = 0;
7740 unsigned int lease_change_type;
7741 __le32 lease_state;
7742 struct lease *lease;
7743
7744 ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007745 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007746 opinfo = lookup_lease_in_table(conn, req->LeaseKey);
7747 if (!opinfo) {
7748 ksmbd_debug(OPLOCK, "file not opened\n");
7749 smb2_set_err_rsp(work);
7750 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7751 return;
7752 }
7753 lease = opinfo->o_lease;
7754
7755 if (opinfo->op_state == OPLOCK_STATE_NONE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007756 pr_err("unexpected lease break state 0x%x\n",
7757 opinfo->op_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09007758 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7759 goto err_out;
7760 }
7761
7762 if (check_lease_state(lease, req->LeaseState)) {
7763 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
7764 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +09007765 "req lease state: 0x%x, expected state: 0x%x\n",
7766 req->LeaseState, lease->new_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09007767 goto err_out;
7768 }
7769
7770 if (!atomic_read(&opinfo->breaking_cnt)) {
7771 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7772 goto err_out;
7773 }
7774
7775 /* check for bad lease state */
Namjae Jeon070fb212021-05-26 17:57:12 +09007776 if (req->LeaseState &
7777 (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007778 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7779 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7780 lease_change_type = OPLOCK_WRITE_TO_NONE;
7781 else
7782 lease_change_type = OPLOCK_READ_TO_NONE;
7783 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007784 le32_to_cpu(lease->state),
7785 le32_to_cpu(req->LeaseState));
Namjae Jeon64b39f42021-03-30 14:25:35 +09007786 } else if (lease->state == SMB2_LEASE_READ_CACHING_LE &&
7787 req->LeaseState != SMB2_LEASE_NONE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007788 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7789 lease_change_type = OPLOCK_READ_TO_NONE;
7790 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007791 le32_to_cpu(lease->state),
7792 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007793 } else {
7794 /* valid lease state changes */
7795 err = STATUS_INVALID_DEVICE_STATE;
7796 if (req->LeaseState == SMB2_LEASE_NONE_LE) {
7797 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7798 lease_change_type = OPLOCK_WRITE_TO_NONE;
7799 else
7800 lease_change_type = OPLOCK_READ_TO_NONE;
7801 } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) {
7802 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7803 lease_change_type = OPLOCK_WRITE_TO_READ;
7804 else
7805 lease_change_type = OPLOCK_READ_HANDLE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007806 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007807 lease_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007808 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007809 }
7810
7811 switch (lease_change_type) {
7812 case OPLOCK_WRITE_TO_READ:
7813 ret = opinfo_write_to_read(opinfo);
7814 break;
7815 case OPLOCK_READ_HANDLE_TO_READ:
7816 ret = opinfo_read_handle_to_read(opinfo);
7817 break;
7818 case OPLOCK_WRITE_TO_NONE:
7819 ret = opinfo_write_to_none(opinfo);
7820 break;
7821 case OPLOCK_READ_TO_NONE:
7822 ret = opinfo_read_to_none(opinfo);
7823 break;
7824 default:
7825 ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007826 le32_to_cpu(lease->state),
7827 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007828 }
7829
7830 lease_state = lease->state;
7831 opinfo->op_state = OPLOCK_STATE_NONE;
7832 wake_up_interruptible_all(&opinfo->oplock_q);
7833 atomic_dec(&opinfo->breaking_cnt);
7834 wake_up_interruptible_all(&opinfo->oplock_brk);
7835 opinfo_put(opinfo);
7836
7837 if (ret < 0) {
7838 rsp->hdr.Status = err;
7839 goto err_out;
7840 }
7841
7842 rsp->StructureSize = cpu_to_le16(36);
7843 rsp->Reserved = 0;
7844 rsp->Flags = 0;
7845 memcpy(rsp->LeaseKey, req->LeaseKey, 16);
7846 rsp->LeaseState = lease_state;
7847 rsp->LeaseDuration = 0;
7848 inc_rfc1001_len(rsp, 36);
7849 return;
7850
7851err_out:
7852 opinfo->op_state = OPLOCK_STATE_NONE;
7853 wake_up_interruptible_all(&opinfo->oplock_q);
7854 atomic_dec(&opinfo->breaking_cnt);
7855 wake_up_interruptible_all(&opinfo->oplock_brk);
7856
7857 opinfo_put(opinfo);
7858 smb2_set_err_rsp(work);
7859}
7860
7861/**
7862 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break
7863 * @work: smb work containing oplock/lease break command buffer
7864 *
7865 * Return: 0
7866 */
7867int smb2_oplock_break(struct ksmbd_work *work)
7868{
Namjae Jeone5066492021-03-30 12:35:23 +09007869 struct smb2_oplock_break *req = work->request_buf;
7870 struct smb2_oplock_break *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007871
7872 switch (le16_to_cpu(req->StructureSize)) {
7873 case OP_BREAK_STRUCT_SIZE_20:
7874 smb20_oplock_break_ack(work);
7875 break;
7876 case OP_BREAK_STRUCT_SIZE_21:
7877 smb21_lease_break_ack(work);
7878 break;
7879 default:
7880 ksmbd_debug(OPLOCK, "invalid break cmd %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007881 le16_to_cpu(req->StructureSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09007882 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7883 smb2_set_err_rsp(work);
7884 }
7885
7886 return 0;
7887}
7888
7889/**
7890 * smb2_notify() - handler for smb2 notify request
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09007891 * @work: smb work containing notify command buffer
Namjae Jeone2f34482021-03-16 10:49:09 +09007892 *
7893 * Return: 0
7894 */
7895int smb2_notify(struct ksmbd_work *work)
7896{
7897 struct smb2_notify_req *req;
7898 struct smb2_notify_rsp *rsp;
7899
7900 WORK_BUFFERS(work, req, rsp);
7901
7902 if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) {
7903 rsp->hdr.Status = STATUS_INTERNAL_ERROR;
7904 smb2_set_err_rsp(work);
7905 return 0;
7906 }
7907
7908 smb2_set_err_rsp(work);
7909 rsp->hdr.Status = STATUS_NOT_IMPLEMENTED;
7910 return 0;
7911}
7912
7913/**
7914 * smb2_is_sign_req() - handler for checking packet signing status
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09007915 * @work: smb work containing notify command buffer
7916 * @command: SMB2 command id
Namjae Jeone2f34482021-03-16 10:49:09 +09007917 *
7918 * Return: true if packed is signed, false otherwise
7919 */
7920bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command)
7921{
Namjae Jeone5066492021-03-30 12:35:23 +09007922 struct smb2_hdr *rcv_hdr2 = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007923
7924 if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007925 command != SMB2_NEGOTIATE_HE &&
7926 command != SMB2_SESSION_SETUP_HE &&
7927 command != SMB2_OPLOCK_BREAK_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +09007928 return true;
7929
kernel test robot56160152021-05-12 09:24:37 +09007930 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +09007931}
7932
7933/**
7934 * smb2_check_sign_req() - handler for req packet sign processing
7935 * @work: smb work containing notify command buffer
7936 *
7937 * Return: 1 on success, 0 otherwise
7938 */
7939int smb2_check_sign_req(struct ksmbd_work *work)
7940{
7941 struct smb2_hdr *hdr, *hdr_org;
7942 char signature_req[SMB2_SIGNATURE_SIZE];
7943 char signature[SMB2_HMACSHA256_SIZE];
7944 struct kvec iov[1];
7945 size_t len;
7946
Namjae Jeone5066492021-03-30 12:35:23 +09007947 hdr_org = hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007948 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09007949 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09007950
7951 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
7952 len = be32_to_cpu(hdr_org->smb2_buf_length);
7953 else if (hdr->NextCommand)
7954 len = le32_to_cpu(hdr->NextCommand);
7955 else
7956 len = be32_to_cpu(hdr_org->smb2_buf_length) -
7957 work->next_smb2_rcv_hdr_off;
7958
7959 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
7960 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7961
7962 iov[0].iov_base = (char *)&hdr->ProtocolId;
7963 iov[0].iov_len = len;
7964
7965 if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1,
Namjae Jeon64b39f42021-03-30 14:25:35 +09007966 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09007967 return 0;
7968
7969 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007970 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007971 return 0;
7972 }
7973
7974 return 1;
7975}
7976
7977/**
7978 * smb2_set_sign_rsp() - handler for rsp packet sign processing
7979 * @work: smb work containing notify command buffer
7980 *
7981 */
7982void smb2_set_sign_rsp(struct ksmbd_work *work)
7983{
7984 struct smb2_hdr *hdr, *hdr_org;
7985 struct smb2_hdr *req_hdr;
7986 char signature[SMB2_HMACSHA256_SIZE];
7987 struct kvec iov[2];
7988 size_t len;
7989 int n_vec = 1;
7990
Namjae Jeone5066492021-03-30 12:35:23 +09007991 hdr_org = hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007992 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09007993 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09007994
Namjae Jeon8a893312021-06-25 13:43:37 +09007995 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09007996
7997 if (!work->next_smb2_rsp_hdr_off) {
7998 len = get_rfc1002_len(hdr_org);
7999 if (req_hdr->NextCommand)
8000 len = ALIGN(len, 8);
8001 } else {
8002 len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off;
8003 len = ALIGN(len, 8);
8004 }
8005
8006 if (req_hdr->NextCommand)
8007 hdr->NextCommand = cpu_to_le32(len);
8008
8009 hdr->Flags |= SMB2_FLAGS_SIGNED;
8010 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8011
8012 iov[0].iov_base = (char *)&hdr->ProtocolId;
8013 iov[0].iov_len = len;
8014
Namjae Jeone5066492021-03-30 12:35:23 +09008015 if (work->aux_payload_sz) {
8016 iov[0].iov_len -= work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008017
Namjae Jeone5066492021-03-30 12:35:23 +09008018 iov[1].iov_base = work->aux_payload_buf;
8019 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008020 n_vec++;
8021 }
8022
8023 if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec,
Namjae Jeon64b39f42021-03-30 14:25:35 +09008024 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09008025 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8026}
8027
8028/**
8029 * smb3_check_sign_req() - handler for req packet sign processing
8030 * @work: smb work containing notify command buffer
8031 *
8032 * Return: 1 on success, 0 otherwise
8033 */
8034int smb3_check_sign_req(struct ksmbd_work *work)
8035{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008036 struct ksmbd_conn *conn = work->conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09008037 char *signing_key;
8038 struct smb2_hdr *hdr, *hdr_org;
8039 struct channel *chann;
8040 char signature_req[SMB2_SIGNATURE_SIZE];
8041 char signature[SMB2_CMACAES_SIZE];
8042 struct kvec iov[1];
8043 size_t len;
8044
Namjae Jeone5066492021-03-30 12:35:23 +09008045 hdr_org = hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008046 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008047 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008048
8049 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
8050 len = be32_to_cpu(hdr_org->smb2_buf_length);
8051 else if (hdr->NextCommand)
8052 len = le32_to_cpu(hdr->NextCommand);
8053 else
8054 len = be32_to_cpu(hdr_org->smb2_buf_length) -
8055 work->next_smb2_rcv_hdr_off;
8056
8057 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
8058 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008059 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008060 chann = lookup_chann_list(work->sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008061 if (!chann)
8062 return 0;
8063 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008064 }
8065
8066 if (!signing_key) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008067 pr_err("SMB3 signing key is not generated\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008068 return 0;
8069 }
8070
8071 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8072 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8073 iov[0].iov_base = (char *)&hdr->ProtocolId;
8074 iov[0].iov_len = len;
8075
8076 if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature))
8077 return 0;
8078
8079 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008080 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008081 return 0;
8082 }
8083
8084 return 1;
8085}
8086
8087/**
8088 * smb3_set_sign_rsp() - handler for rsp packet sign processing
8089 * @work: smb work containing notify command buffer
8090 *
8091 */
8092void smb3_set_sign_rsp(struct ksmbd_work *work)
8093{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008094 struct ksmbd_conn *conn = work->conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09008095 struct smb2_hdr *req_hdr;
8096 struct smb2_hdr *hdr, *hdr_org;
8097 struct channel *chann;
8098 char signature[SMB2_CMACAES_SIZE];
8099 struct kvec iov[2];
8100 int n_vec = 1;
8101 size_t len;
8102 char *signing_key;
8103
Namjae Jeone5066492021-03-30 12:35:23 +09008104 hdr_org = hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008105 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008106 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008107
Namjae Jeon8a893312021-06-25 13:43:37 +09008108 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008109
8110 if (!work->next_smb2_rsp_hdr_off) {
8111 len = get_rfc1002_len(hdr_org);
8112 if (req_hdr->NextCommand)
8113 len = ALIGN(len, 8);
8114 } else {
8115 len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off;
8116 len = ALIGN(len, 8);
8117 }
8118
8119 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
8120 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008121 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008122 chann = lookup_chann_list(work->sess, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008123 if (!chann)
8124 return;
8125 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008126 }
8127
8128 if (!signing_key)
8129 return;
8130
8131 if (req_hdr->NextCommand)
8132 hdr->NextCommand = cpu_to_le32(len);
8133
8134 hdr->Flags |= SMB2_FLAGS_SIGNED;
8135 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8136 iov[0].iov_base = (char *)&hdr->ProtocolId;
8137 iov[0].iov_len = len;
Namjae Jeone5066492021-03-30 12:35:23 +09008138 if (work->aux_payload_sz) {
8139 iov[0].iov_len -= work->aux_payload_sz;
8140 iov[1].iov_base = work->aux_payload_buf;
8141 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008142 n_vec++;
8143 }
8144
8145 if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature))
8146 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8147}
8148
8149/**
8150 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response
8151 * @work: smb work containing response buffer
8152 *
8153 */
8154void smb3_preauth_hash_rsp(struct ksmbd_work *work)
8155{
8156 struct ksmbd_conn *conn = work->conn;
8157 struct ksmbd_session *sess = work->sess;
8158 struct smb2_hdr *req, *rsp;
8159
8160 if (conn->dialect != SMB311_PROT_ID)
8161 return;
8162
8163 WORK_BUFFERS(work, req, rsp);
8164
8165 if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE)
8166 ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09008167 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09008168
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008169 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008170 __u8 *hash_value;
8171
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008172 if (conn->binding) {
8173 struct preauth_session *preauth_sess;
8174
8175 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
8176 if (!preauth_sess)
8177 return;
8178 hash_value = preauth_sess->Preauth_HashValue;
8179 } else {
8180 hash_value = sess->Preauth_HashValue;
8181 if (!hash_value)
8182 return;
8183 }
Namjae Jeone2f34482021-03-16 10:49:09 +09008184 ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09008185 hash_value);
Namjae Jeone2f34482021-03-16 10:49:09 +09008186 }
8187}
8188
Namjae Jeon64b39f42021-03-30 14:25:35 +09008189static void fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, char *old_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008190 __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09008191{
8192 struct smb2_hdr *hdr = (struct smb2_hdr *)old_buf;
8193 unsigned int orig_len = get_rfc1002_len(old_buf);
8194
8195 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
8196 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
8197 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
8198 tr_hdr->Flags = cpu_to_le16(0x01);
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008199 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
8200 cipher_type == SMB2_ENCRYPTION_AES256_GCM)
8201 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008202 else
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008203 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008204 memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8);
8205 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
8206 inc_rfc1001_len(tr_hdr, orig_len);
8207}
8208
8209int smb3_encrypt_resp(struct ksmbd_work *work)
8210{
Namjae Jeone5066492021-03-30 12:35:23 +09008211 char *buf = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008212 struct smb2_transform_hdr *tr_hdr;
8213 struct kvec iov[3];
8214 int rc = -ENOMEM;
Namjae Jeone5066492021-03-30 12:35:23 +09008215 int buf_size = 0, rq_nvec = 2 + (work->aux_payload_sz ? 1 : 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09008216
8217 if (ARRAY_SIZE(iov) < rq_nvec)
8218 return -ENOMEM;
8219
Namjae Jeon20ea7fd2021-03-30 12:40:47 +09008220 tr_hdr = kzalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09008221 if (!tr_hdr)
8222 return rc;
8223
8224 /* fill transform header */
8225 fill_transform_hdr(tr_hdr, buf, work->conn->cipher_type);
8226
8227 iov[0].iov_base = tr_hdr;
8228 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8229 buf_size += iov[0].iov_len - 4;
8230
8231 iov[1].iov_base = buf + 4;
8232 iov[1].iov_len = get_rfc1002_len(buf);
Namjae Jeone5066492021-03-30 12:35:23 +09008233 if (work->aux_payload_sz) {
8234 iov[1].iov_len = work->resp_hdr_sz - 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008235
Namjae Jeone5066492021-03-30 12:35:23 +09008236 iov[2].iov_base = work->aux_payload_buf;
8237 iov[2].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008238 buf_size += iov[2].iov_len;
8239 }
8240 buf_size += iov[1].iov_len;
8241 work->resp_hdr_sz = iov[1].iov_len;
8242
8243 rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1);
8244 if (rc)
8245 return rc;
8246
8247 memmove(buf, iov[1].iov_base, iov[1].iov_len);
8248 tr_hdr->smb2_buf_length = cpu_to_be32(buf_size);
8249 work->tr_buf = tr_hdr;
8250
8251 return rc;
8252}
8253
8254int smb3_is_transform_hdr(void *buf)
8255{
8256 struct smb2_transform_hdr *trhdr = buf;
8257
8258 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
8259}
8260
8261int smb3_decrypt_req(struct ksmbd_work *work)
8262{
8263 struct ksmbd_conn *conn = work->conn;
8264 struct ksmbd_session *sess;
Namjae Jeone5066492021-03-30 12:35:23 +09008265 char *buf = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008266 struct smb2_hdr *hdr;
8267 unsigned int pdu_length = get_rfc1002_len(buf);
8268 struct kvec iov[2];
8269 unsigned int buf_data_size = pdu_length + 4 -
8270 sizeof(struct smb2_transform_hdr);
8271 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
8272 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
8273 int rc = 0;
8274
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008275 sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09008276 if (!sess) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008277 pr_err("invalid session id(%llx) in transform header\n",
8278 le64_to_cpu(tr_hdr->SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09008279 return -ECONNABORTED;
8280 }
8281
Namjae Jeon070fb212021-05-26 17:57:12 +09008282 if (pdu_length + 4 <
8283 sizeof(struct smb2_transform_hdr) + sizeof(struct smb2_hdr)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008284 pr_err("Transform message is too small (%u)\n",
8285 pdu_length);
Namjae Jeone2f34482021-03-16 10:49:09 +09008286 return -ECONNABORTED;
8287 }
8288
8289 if (pdu_length + 4 < orig_len + sizeof(struct smb2_transform_hdr)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008290 pr_err("Transform message is broken\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008291 return -ECONNABORTED;
8292 }
8293
8294 iov[0].iov_base = buf;
8295 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8296 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
8297 iov[1].iov_len = buf_data_size;
8298 rc = ksmbd_crypt_message(conn, iov, 2, 0);
8299 if (rc)
8300 return rc;
8301
8302 memmove(buf + 4, iov[1].iov_base, buf_data_size);
8303 hdr = (struct smb2_hdr *)buf;
8304 hdr->smb2_buf_length = cpu_to_be32(buf_data_size);
8305
8306 return rc;
8307}
8308
8309bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work)
8310{
8311 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09008312 struct smb2_hdr *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008313
8314 if (conn->dialect < SMB30_PROT_ID)
8315 return false;
8316
8317 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008318 rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008319
8320 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008321 rsp->Status == STATUS_SUCCESS)
Namjae Jeone2f34482021-03-16 10:49:09 +09008322 return true;
8323 return false;
8324}