blob: 2811dfabfa75a4fc726f3c08226f62b562e7fc3b [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);
2812 if (!HAS_FILE_ID(fp->persistent_id)) {
2813 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;
3294 int struct_sz;
3295
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) {
3304 kfree(conv_name);
3305 return -EINVAL;
3306 }
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;
3314 return -ENOSPC;
3315 }
3316
3317 kstat = d_info->wptr;
3318 if (info_level != FILE_NAMES_INFORMATION)
3319 kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat);
3320
3321 switch (info_level) {
3322 case FILE_FULL_DIRECTORY_INFORMATION:
3323 {
3324 struct file_full_directory_info *ffdinfo;
3325
3326 ffdinfo = (struct file_full_directory_info *)kstat;
3327 ffdinfo->FileNameLength = cpu_to_le32(conv_len);
3328 ffdinfo->EaSize =
3329 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3330 if (ffdinfo->EaSize)
3331 ffdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3332 if (d_info->hide_dot_file && d_info->name[0] == '.')
3333 ffdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3334 memcpy(ffdinfo->FileName, conv_name, conv_len);
3335 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3336 break;
3337 }
3338 case FILE_BOTH_DIRECTORY_INFORMATION:
3339 {
3340 struct file_both_directory_info *fbdinfo;
3341
3342 fbdinfo = (struct file_both_directory_info *)kstat;
3343 fbdinfo->FileNameLength = cpu_to_le32(conv_len);
3344 fbdinfo->EaSize =
3345 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3346 if (fbdinfo->EaSize)
3347 fbdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3348 fbdinfo->ShortNameLength = 0;
3349 fbdinfo->Reserved = 0;
3350 if (d_info->hide_dot_file && d_info->name[0] == '.')
3351 fbdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3352 memcpy(fbdinfo->FileName, conv_name, conv_len);
3353 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3354 break;
3355 }
3356 case FILE_DIRECTORY_INFORMATION:
3357 {
3358 struct file_directory_info *fdinfo;
3359
3360 fdinfo = (struct file_directory_info *)kstat;
3361 fdinfo->FileNameLength = cpu_to_le32(conv_len);
3362 if (d_info->hide_dot_file && d_info->name[0] == '.')
3363 fdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3364 memcpy(fdinfo->FileName, conv_name, conv_len);
3365 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3366 break;
3367 }
3368 case FILE_NAMES_INFORMATION:
3369 {
3370 struct file_names_info *fninfo;
3371
3372 fninfo = (struct file_names_info *)kstat;
3373 fninfo->FileNameLength = cpu_to_le32(conv_len);
3374 memcpy(fninfo->FileName, conv_name, conv_len);
3375 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3376 break;
3377 }
3378 case FILEID_FULL_DIRECTORY_INFORMATION:
3379 {
3380 struct file_id_full_dir_info *dinfo;
3381
3382 dinfo = (struct file_id_full_dir_info *)kstat;
3383 dinfo->FileNameLength = cpu_to_le32(conv_len);
3384 dinfo->EaSize =
3385 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3386 if (dinfo->EaSize)
3387 dinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3388 dinfo->Reserved = 0;
3389 dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3390 if (d_info->hide_dot_file && d_info->name[0] == '.')
3391 dinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3392 memcpy(dinfo->FileName, conv_name, conv_len);
3393 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3394 break;
3395 }
3396 case FILEID_BOTH_DIRECTORY_INFORMATION:
3397 {
3398 struct file_id_both_directory_info *fibdinfo;
3399
3400 fibdinfo = (struct file_id_both_directory_info *)kstat;
3401 fibdinfo->FileNameLength = cpu_to_le32(conv_len);
3402 fibdinfo->EaSize =
3403 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3404 if (fibdinfo->EaSize)
3405 fibdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3406 fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3407 fibdinfo->ShortNameLength = 0;
3408 fibdinfo->Reserved = 0;
3409 fibdinfo->Reserved2 = cpu_to_le16(0);
3410 if (d_info->hide_dot_file && d_info->name[0] == '.')
3411 fibdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3412 memcpy(fibdinfo->FileName, conv_name, conv_len);
3413 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3414 break;
3415 }
3416 case SMB_FIND_FILE_POSIX_INFO:
3417 {
3418 struct smb2_posix_info *posix_info;
3419 u64 time;
3420
3421 posix_info = (struct smb2_posix_info *)kstat;
3422 posix_info->Ignored = 0;
3423 posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
3424 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
3425 posix_info->ChangeTime = cpu_to_le64(time);
3426 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime);
3427 posix_info->LastAccessTime = cpu_to_le64(time);
3428 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime);
3429 posix_info->LastWriteTime = cpu_to_le64(time);
3430 posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size);
3431 posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9);
3432 posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev);
3433 posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink);
3434 posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode);
3435 posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino);
3436 posix_info->DosAttributes =
3437 S_ISDIR(ksmbd_kstat->kstat->mode) ? ATTR_DIRECTORY_LE : ATTR_ARCHIVE_LE;
3438 if (d_info->hide_dot_file && d_info->name[0] == '.')
3439 posix_info->DosAttributes |= ATTR_HIDDEN_LE;
Hyunchul Leeaf349832021-06-30 18:25:53 +09003440 id_to_sid(from_kuid(user_ns, ksmbd_kstat->kstat->uid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003441 SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]);
Hyunchul Leeaf349832021-06-30 18:25:53 +09003442 id_to_sid(from_kgid(user_ns, ksmbd_kstat->kstat->gid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003443 SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]);
Namjae Jeone2f34482021-03-16 10:49:09 +09003444 memcpy(posix_info->name, conv_name, conv_len);
3445 posix_info->name_len = cpu_to_le32(conv_len);
3446 posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset);
3447 break;
3448 }
3449
3450 } /* switch (info_level) */
3451
3452 d_info->last_entry_offset = d_info->data_count;
3453 d_info->data_count += next_entry_offset;
Marios Makassikise7735c82021-05-06 11:40:02 +09003454 d_info->out_buf_len -= next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003455 d_info->wptr += next_entry_offset;
3456 kfree(conv_name);
3457
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
3463 return 0;
3464}
3465
3466struct smb2_query_dir_private {
3467 struct ksmbd_work *work;
3468 char *search_pattern;
3469 struct ksmbd_file *dir_fp;
3470
3471 struct ksmbd_dir_info *d_info;
3472 int info_level;
3473};
3474
3475static void lock_dir(struct ksmbd_file *dir_fp)
3476{
3477 struct dentry *dir = dir_fp->filp->f_path.dentry;
3478
3479 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
3480}
3481
3482static void unlock_dir(struct ksmbd_file *dir_fp)
3483{
3484 struct dentry *dir = dir_fp->filp->f_path.dentry;
3485
3486 inode_unlock(d_inode(dir));
3487}
3488
3489static int process_query_dir_entries(struct smb2_query_dir_private *priv)
3490{
Hyunchul Lee465d7202021-07-03 12:10:36 +09003491 struct user_namespace *user_ns = file_mnt_user_ns(priv->dir_fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003492 struct kstat kstat;
3493 struct ksmbd_kstat ksmbd_kstat;
3494 int rc;
3495 int i;
3496
3497 for (i = 0; i < priv->d_info->num_entry; i++) {
3498 struct dentry *dent;
3499
3500 if (dentry_name(priv->d_info, priv->info_level))
3501 return -EINVAL;
3502
3503 lock_dir(priv->dir_fp);
3504 dent = lookup_one_len(priv->d_info->name,
3505 priv->dir_fp->filp->f_path.dentry,
3506 priv->d_info->name_len);
3507 unlock_dir(priv->dir_fp);
3508
3509 if (IS_ERR(dent)) {
3510 ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003511 priv->d_info->name,
3512 PTR_ERR(dent));
Namjae Jeone2f34482021-03-16 10:49:09 +09003513 continue;
3514 }
3515 if (unlikely(d_is_negative(dent))) {
3516 dput(dent);
3517 ksmbd_debug(SMB, "Negative dentry `%s'\n",
3518 priv->d_info->name);
3519 continue;
3520 }
3521
3522 ksmbd_kstat.kstat = &kstat;
3523 if (priv->info_level != FILE_NAMES_INFORMATION)
3524 ksmbd_vfs_fill_dentry_attrs(priv->work,
Hyunchul Lee465d7202021-07-03 12:10:36 +09003525 user_ns,
Namjae Jeone2f34482021-03-16 10:49:09 +09003526 dent,
3527 &ksmbd_kstat);
3528
3529 rc = smb2_populate_readdir_entry(priv->work->conn,
3530 priv->info_level,
3531 priv->d_info,
Hyunchul Lee465d7202021-07-03 12:10:36 +09003532 user_ns,
Namjae Jeone2f34482021-03-16 10:49:09 +09003533 &ksmbd_kstat);
3534 dput(dent);
3535 if (rc)
3536 return rc;
3537 }
3538 return 0;
3539}
3540
3541static int reserve_populate_dentry(struct ksmbd_dir_info *d_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09003542 int info_level)
Namjae Jeone2f34482021-03-16 10:49:09 +09003543{
3544 int struct_sz;
3545 int conv_len;
3546 int next_entry_offset;
3547
3548 struct_sz = readdir_info_level_struct_sz(info_level);
3549 if (struct_sz == -EOPNOTSUPP)
3550 return -EOPNOTSUPP;
3551
3552 conv_len = (d_info->name_len + 1) * 2;
3553 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3554 KSMBD_DIR_INFO_ALIGNMENT);
3555
3556 if (next_entry_offset > d_info->out_buf_len) {
3557 d_info->out_buf_len = 0;
3558 return -ENOSPC;
3559 }
3560
3561 switch (info_level) {
3562 case FILE_FULL_DIRECTORY_INFORMATION:
3563 {
3564 struct file_full_directory_info *ffdinfo;
3565
3566 ffdinfo = (struct file_full_directory_info *)d_info->wptr;
3567 memcpy(ffdinfo->FileName, d_info->name, d_info->name_len);
3568 ffdinfo->FileName[d_info->name_len] = 0x00;
3569 ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3570 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3571 break;
3572 }
3573 case FILE_BOTH_DIRECTORY_INFORMATION:
3574 {
3575 struct file_both_directory_info *fbdinfo;
3576
3577 fbdinfo = (struct file_both_directory_info *)d_info->wptr;
3578 memcpy(fbdinfo->FileName, d_info->name, d_info->name_len);
3579 fbdinfo->FileName[d_info->name_len] = 0x00;
3580 fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3581 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3582 break;
3583 }
3584 case FILE_DIRECTORY_INFORMATION:
3585 {
3586 struct file_directory_info *fdinfo;
3587
3588 fdinfo = (struct file_directory_info *)d_info->wptr;
3589 memcpy(fdinfo->FileName, d_info->name, d_info->name_len);
3590 fdinfo->FileName[d_info->name_len] = 0x00;
3591 fdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3592 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3593 break;
3594 }
3595 case FILE_NAMES_INFORMATION:
3596 {
3597 struct file_names_info *fninfo;
3598
3599 fninfo = (struct file_names_info *)d_info->wptr;
3600 memcpy(fninfo->FileName, d_info->name, d_info->name_len);
3601 fninfo->FileName[d_info->name_len] = 0x00;
3602 fninfo->FileNameLength = cpu_to_le32(d_info->name_len);
3603 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3604 break;
3605 }
3606 case FILEID_FULL_DIRECTORY_INFORMATION:
3607 {
3608 struct file_id_full_dir_info *dinfo;
3609
3610 dinfo = (struct file_id_full_dir_info *)d_info->wptr;
3611 memcpy(dinfo->FileName, d_info->name, d_info->name_len);
3612 dinfo->FileName[d_info->name_len] = 0x00;
3613 dinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3614 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3615 break;
3616 }
3617 case FILEID_BOTH_DIRECTORY_INFORMATION:
3618 {
3619 struct file_id_both_directory_info *fibdinfo;
3620
3621 fibdinfo = (struct file_id_both_directory_info *)d_info->wptr;
3622 memcpy(fibdinfo->FileName, d_info->name, d_info->name_len);
3623 fibdinfo->FileName[d_info->name_len] = 0x00;
3624 fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3625 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3626 break;
3627 }
3628 case SMB_FIND_FILE_POSIX_INFO:
3629 {
3630 struct smb2_posix_info *posix_info;
3631
3632 posix_info = (struct smb2_posix_info *)d_info->wptr;
3633 memcpy(posix_info->name, d_info->name, d_info->name_len);
3634 posix_info->name[d_info->name_len] = 0x00;
3635 posix_info->name_len = cpu_to_le32(d_info->name_len);
3636 posix_info->NextEntryOffset =
3637 cpu_to_le32(next_entry_offset);
3638 break;
3639 }
3640 } /* switch (info_level) */
3641
3642 d_info->num_entry++;
3643 d_info->out_buf_len -= next_entry_offset;
3644 d_info->wptr += next_entry_offset;
3645 return 0;
3646}
3647
Namjae Jeon64b39f42021-03-30 14:25:35 +09003648static int __query_dir(struct dir_context *ctx, const char *name, int namlen,
Namjae Jeon070fb212021-05-26 17:57:12 +09003649 loff_t offset, u64 ino, unsigned int d_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09003650{
3651 struct ksmbd_readdir_data *buf;
3652 struct smb2_query_dir_private *priv;
3653 struct ksmbd_dir_info *d_info;
3654 int rc;
3655
3656 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
3657 priv = buf->private;
3658 d_info = priv->d_info;
3659
3660 /* dot and dotdot entries are already reserved */
3661 if (!strcmp(".", name) || !strcmp("..", name))
3662 return 0;
3663 if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name))
3664 return 0;
Namjae Jeonb24c9332021-03-21 17:32:19 +09003665 if (!match_pattern(name, namlen, priv->search_pattern))
Namjae Jeone2f34482021-03-16 10:49:09 +09003666 return 0;
3667
3668 d_info->name = name;
3669 d_info->name_len = namlen;
3670 rc = reserve_populate_dentry(d_info, priv->info_level);
3671 if (rc)
3672 return rc;
3673 if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
3674 d_info->out_buf_len = 0;
3675 return 0;
3676 }
3677 return 0;
3678}
3679
3680static void restart_ctx(struct dir_context *ctx)
3681{
3682 ctx->pos = 0;
3683}
3684
3685static int verify_info_level(int info_level)
3686{
3687 switch (info_level) {
3688 case FILE_FULL_DIRECTORY_INFORMATION:
3689 case FILE_BOTH_DIRECTORY_INFORMATION:
3690 case FILE_DIRECTORY_INFORMATION:
3691 case FILE_NAMES_INFORMATION:
3692 case FILEID_FULL_DIRECTORY_INFORMATION:
3693 case FILEID_BOTH_DIRECTORY_INFORMATION:
3694 case SMB_FIND_FILE_POSIX_INFO:
3695 break;
3696 default:
3697 return -EOPNOTSUPP;
3698 }
3699
3700 return 0;
3701}
3702
3703int smb2_query_dir(struct ksmbd_work *work)
3704{
3705 struct ksmbd_conn *conn = work->conn;
3706 struct smb2_query_directory_req *req;
3707 struct smb2_query_directory_rsp *rsp, *rsp_org;
3708 struct ksmbd_share_config *share = work->tcon->share_conf;
3709 struct ksmbd_file *dir_fp = NULL;
3710 struct ksmbd_dir_info d_info;
3711 int rc = 0;
3712 char *srch_ptr = NULL;
3713 unsigned char srch_flag;
3714 int buffer_sz;
3715 struct smb2_query_dir_private query_dir_private = {NULL, };
3716
Namjae Jeone5066492021-03-30 12:35:23 +09003717 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09003718 WORK_BUFFERS(work, req, rsp);
3719
3720 if (ksmbd_override_fsids(work)) {
3721 rsp->hdr.Status = STATUS_NO_MEMORY;
3722 smb2_set_err_rsp(work);
3723 return -ENOMEM;
3724 }
3725
3726 rc = verify_info_level(req->FileInformationClass);
3727 if (rc) {
3728 rc = -EFAULT;
3729 goto err_out2;
3730 }
3731
3732 dir_fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09003733 le64_to_cpu(req->VolatileFileId),
3734 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09003735 if (!dir_fp) {
3736 rc = -EBADF;
3737 goto err_out2;
3738 }
3739
3740 if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) ||
Hyunchul Leeaf349832021-06-30 18:25:53 +09003741 inode_permission(file_mnt_user_ns(dir_fp->filp),
3742 file_inode(dir_fp->filp),
Namjae Jeon070fb212021-05-26 17:57:12 +09003743 MAY_READ | MAY_EXEC)) {
Namjae Jeon493fa2f2021-06-29 09:22:16 +09003744 pr_err("no right to enumerate directory (%pd)\n",
3745 dir_fp->filp->f_path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003746 rc = -EACCES;
3747 goto err_out2;
3748 }
3749
3750 if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09003751 pr_err("can't do query dir for a file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09003752 rc = -EINVAL;
3753 goto err_out2;
3754 }
3755
3756 srch_flag = req->Flags;
3757 srch_ptr = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09003758 le16_to_cpu(req->FileNameLength), 1,
3759 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09003760 if (IS_ERR(srch_ptr)) {
3761 ksmbd_debug(SMB, "Search Pattern not found\n");
3762 rc = -EINVAL;
3763 goto err_out2;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003764 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003765 ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr);
Namjae Jeon64b39f42021-03-30 14:25:35 +09003766 }
Namjae Jeone2f34482021-03-16 10:49:09 +09003767
3768 ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename);
3769
3770 if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) {
3771 ksmbd_debug(SMB, "Restart directory scan\n");
3772 generic_file_llseek(dir_fp->filp, 0, SEEK_SET);
3773 restart_ctx(&dir_fp->readdir_data.ctx);
3774 }
3775
3776 memset(&d_info, 0, sizeof(struct ksmbd_dir_info));
3777 d_info.wptr = (char *)rsp->Buffer;
3778 d_info.rptr = (char *)rsp->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003779 d_info.out_buf_len = (work->response_sz - (get_rfc1002_len(rsp_org) + 4));
Namjae Jeon070fb212021-05-26 17:57:12 +09003780 d_info.out_buf_len = min_t(int, d_info.out_buf_len, le32_to_cpu(req->OutputBufferLength)) -
3781 sizeof(struct smb2_query_directory_rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003782 d_info.flags = srch_flag;
3783
3784 /*
3785 * reserve dot and dotdot entries in head of buffer
3786 * in first response
3787 */
3788 rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass,
Namjae Jeon070fb212021-05-26 17:57:12 +09003789 dir_fp, &d_info, srch_ptr,
3790 smb2_populate_readdir_entry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003791 if (rc == -ENOSPC)
3792 rc = 0;
3793 else if (rc)
3794 goto err_out;
3795
3796 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES))
3797 d_info.hide_dot_file = true;
3798
3799 buffer_sz = d_info.out_buf_len;
3800 d_info.rptr = d_info.wptr;
3801 query_dir_private.work = work;
3802 query_dir_private.search_pattern = srch_ptr;
3803 query_dir_private.dir_fp = dir_fp;
3804 query_dir_private.d_info = &d_info;
3805 query_dir_private.info_level = req->FileInformationClass;
3806 dir_fp->readdir_data.private = &query_dir_private;
3807 set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir);
3808
Namjae Jeone8c06192021-06-22 11:06:11 +09003809 rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx);
Namjae Jeone2f34482021-03-16 10:49:09 +09003810 if (rc == 0)
3811 restart_ctx(&dir_fp->readdir_data.ctx);
3812 if (rc == -ENOSPC)
3813 rc = 0;
3814 if (rc)
3815 goto err_out;
3816
3817 d_info.wptr = d_info.rptr;
3818 d_info.out_buf_len = buffer_sz;
3819 rc = process_query_dir_entries(&query_dir_private);
3820 if (rc)
3821 goto err_out;
3822
3823 if (!d_info.data_count && d_info.out_buf_len >= 0) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09003824 if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003825 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003826 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003827 dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0;
3828 rsp->hdr.Status = STATUS_NO_MORE_FILES;
3829 }
3830 rsp->StructureSize = cpu_to_le16(9);
3831 rsp->OutputBufferOffset = cpu_to_le16(0);
3832 rsp->OutputBufferLength = cpu_to_le32(0);
3833 rsp->Buffer[0] = 0;
3834 inc_rfc1001_len(rsp_org, 9);
3835 } else {
3836 ((struct file_directory_info *)
3837 ((char *)rsp->Buffer + d_info.last_entry_offset))
3838 ->NextEntryOffset = 0;
3839
3840 rsp->StructureSize = cpu_to_le16(9);
3841 rsp->OutputBufferOffset = cpu_to_le16(72);
3842 rsp->OutputBufferLength = cpu_to_le32(d_info.data_count);
3843 inc_rfc1001_len(rsp_org, 8 + d_info.data_count);
3844 }
3845
3846 kfree(srch_ptr);
3847 ksmbd_fd_put(work, dir_fp);
3848 ksmbd_revert_fsids(work);
3849 return 0;
3850
3851err_out:
Namjae Jeonbde16942021-06-28 15:23:19 +09003852 pr_err("error while processing smb2 query dir rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09003853 kfree(srch_ptr);
3854
3855err_out2:
3856 if (rc == -EINVAL)
3857 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3858 else if (rc == -EACCES)
3859 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3860 else if (rc == -ENOENT)
3861 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
3862 else if (rc == -EBADF)
3863 rsp->hdr.Status = STATUS_FILE_CLOSED;
3864 else if (rc == -ENOMEM)
3865 rsp->hdr.Status = STATUS_NO_MEMORY;
3866 else if (rc == -EFAULT)
3867 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
3868 if (!rsp->hdr.Status)
3869 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3870
3871 smb2_set_err_rsp(work);
3872 ksmbd_fd_put(work, dir_fp);
3873 ksmbd_revert_fsids(work);
3874 return 0;
3875}
3876
3877/**
3878 * buffer_check_err() - helper function to check buffer errors
3879 * @reqOutputBufferLength: max buffer length expected in command response
3880 * @rsp: query info response buffer contains output buffer length
3881 * @infoclass_size: query info class response buffer size
3882 *
3883 * Return: 0 on success, otherwise error
3884 */
3885static int buffer_check_err(int reqOutputBufferLength,
Namjae Jeon070fb212021-05-26 17:57:12 +09003886 struct smb2_query_info_rsp *rsp, int infoclass_size)
Namjae Jeone2f34482021-03-16 10:49:09 +09003887{
3888 if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) {
3889 if (reqOutputBufferLength < infoclass_size) {
Namjae Jeonbde16942021-06-28 15:23:19 +09003890 pr_err("Invalid Buffer Size Requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09003891 rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003892 rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09003893 return -EINVAL;
3894 }
3895
3896 ksmbd_debug(SMB, "Buffer Overflow\n");
3897 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003898 rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4 +
3899 reqOutputBufferLength);
3900 rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09003901 }
3902 return 0;
3903}
3904
3905static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp)
3906{
3907 struct smb2_file_standard_info *sinfo;
3908
3909 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
3910
3911 sinfo->AllocationSize = cpu_to_le64(4096);
3912 sinfo->EndOfFile = cpu_to_le64(0);
3913 sinfo->NumberOfLinks = cpu_to_le32(1);
3914 sinfo->DeletePending = 1;
3915 sinfo->Directory = 0;
3916 rsp->OutputBufferLength =
3917 cpu_to_le32(sizeof(struct smb2_file_standard_info));
3918 inc_rfc1001_len(rsp, sizeof(struct smb2_file_standard_info));
3919}
3920
Namjae Jeon070fb212021-05-26 17:57:12 +09003921static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num)
Namjae Jeone2f34482021-03-16 10:49:09 +09003922{
3923 struct smb2_file_internal_info *file_info;
3924
3925 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
3926
3927 /* any unique number */
3928 file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63));
3929 rsp->OutputBufferLength =
3930 cpu_to_le32(sizeof(struct smb2_file_internal_info));
3931 inc_rfc1001_len(rsp, sizeof(struct smb2_file_internal_info));
3932}
3933
Namjae Jeone2f34482021-03-16 10:49:09 +09003934static int smb2_get_info_file_pipe(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09003935 struct smb2_query_info_req *req,
3936 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09003937{
Namjae Jeon64b39f42021-03-30 14:25:35 +09003938 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09003939 int rc;
3940
3941 /*
3942 * Windows can sometime send query file info request on
3943 * pipe without opening it, checking error condition here
3944 */
3945 id = le64_to_cpu(req->VolatileFileId);
3946 if (!ksmbd_session_rpc_method(sess, id))
3947 return -ENOENT;
3948
3949 ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003950 req->FileInfoClass, le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09003951
3952 switch (req->FileInfoClass) {
3953 case FILE_STANDARD_INFORMATION:
3954 get_standard_info_pipe(rsp);
3955 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09003956 rsp, FILE_STANDARD_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09003957 break;
3958 case FILE_INTERNAL_INFORMATION:
3959 get_internal_info_pipe(rsp, id);
3960 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09003961 rsp, FILE_INTERNAL_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09003962 break;
3963 default:
3964 ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003965 req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09003966 rc = -EOPNOTSUPP;
3967 }
3968 return rc;
3969}
3970
3971/**
3972 * smb2_get_ea() - handler for smb2 get extended attribute command
3973 * @work: smb work containing query info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09003974 * @fp: ksmbd_file pointer
3975 * @req: get extended attribute request
3976 * @rsp: response buffer pointer
3977 * @rsp_org: base response buffer pointer in case of chained response
Namjae Jeone2f34482021-03-16 10:49:09 +09003978 *
3979 * Return: 0 on success, otherwise error
3980 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003981static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09003982 struct smb2_query_info_req *req,
3983 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09003984{
3985 struct smb2_ea_info *eainfo, *prev_eainfo;
3986 char *name, *ptr, *xattr_list = NULL, *buf;
3987 int rc, name_len, value_len, xattr_list_len, idx;
3988 ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0;
3989 struct smb2_ea_info_req *ea_req = NULL;
3990 struct path *path;
Hyunchul Lee465d7202021-07-03 12:10:36 +09003991 struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003992
3993 if (!(fp->daccess & FILE_READ_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09003994 pr_err("Not permitted to read ext attr : 0x%x\n",
3995 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09003996 return -EACCES;
3997 }
3998
3999 path = &fp->filp->f_path;
4000 /* single EA entry is requested with given user.* name */
Namjae Jeon64b39f42021-03-30 14:25:35 +09004001 if (req->InputBufferLength) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004002 ea_req = (struct smb2_ea_info_req *)req->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004003 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09004004 /* need to send all EAs, if no specific EA is requested*/
4005 if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY)
4006 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09004007 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n",
4008 le32_to_cpu(req->Flags));
Namjae Jeone2f34482021-03-16 10:49:09 +09004009 }
4010
4011 buf_free_len = work->response_sz -
4012 (get_rfc1002_len(rsp_org) + 4) -
4013 sizeof(struct smb2_query_info_rsp);
4014
4015 if (le32_to_cpu(req->OutputBufferLength) < buf_free_len)
4016 buf_free_len = le32_to_cpu(req->OutputBufferLength);
4017
4018 rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4019 if (rc < 0) {
4020 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4021 goto out;
4022 } else if (!rc) { /* there is no EA in the file */
4023 ksmbd_debug(SMB, "no ea data in the file\n");
4024 goto done;
4025 }
4026 xattr_list_len = rc;
4027
4028 ptr = (char *)rsp->Buffer;
4029 eainfo = (struct smb2_ea_info *)ptr;
4030 prev_eainfo = eainfo;
4031 idx = 0;
4032
4033 while (idx < xattr_list_len) {
4034 name = xattr_list + idx;
4035 name_len = strlen(name);
4036
4037 ksmbd_debug(SMB, "%s, len %d\n", name, name_len);
4038 idx += name_len + 1;
4039
4040 /*
4041 * CIFS does not support EA other than user.* namespace,
4042 * still keep the framework generic, to list other attrs
4043 * in future.
4044 */
4045 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4046 continue;
4047
4048 if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004049 STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004050 continue;
4051
4052 if (req->InputBufferLength &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09004053 strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name,
4054 ea_req->EaNameLength))
Namjae Jeone2f34482021-03-16 10:49:09 +09004055 continue;
4056
4057 if (!strncmp(&name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004058 DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004059 continue;
4060
4061 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4062 name_len -= XATTR_USER_PREFIX_LEN;
4063
4064 ptr = (char *)(&eainfo->name + name_len + 1);
4065 buf_free_len -= (offsetof(struct smb2_ea_info, name) +
4066 name_len + 1);
4067 /* bailout if xattr can't fit in buf_free_len */
Hyunchul Lee465d7202021-07-03 12:10:36 +09004068 value_len = ksmbd_vfs_getxattr(user_ns, path->dentry,
4069 name, &buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004070 if (value_len <= 0) {
4071 rc = -ENOENT;
4072 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4073 goto out;
4074 }
4075
4076 buf_free_len -= value_len;
4077 if (buf_free_len < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09004078 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004079 break;
4080 }
4081
4082 memcpy(ptr, buf, value_len);
Namjae Jeon79f6b112021-04-02 12:47:14 +09004083 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004084
4085 ptr += value_len;
4086 eainfo->Flags = 0;
4087 eainfo->EaNameLength = name_len;
4088
Namjae Jeon64b39f42021-03-30 14:25:35 +09004089 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004090 memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN],
Namjae Jeon070fb212021-05-26 17:57:12 +09004091 name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09004092 else
4093 memcpy(eainfo->name, name, name_len);
4094
4095 eainfo->name[name_len] = '\0';
4096 eainfo->EaValueLength = cpu_to_le16(value_len);
4097 next_offset = offsetof(struct smb2_ea_info, name) +
4098 name_len + 1 + value_len;
4099
4100 /* align next xattr entry at 4 byte bundary */
4101 alignment_bytes = ((next_offset + 3) & ~3) - next_offset;
4102 if (alignment_bytes) {
4103 memset(ptr, '\0', alignment_bytes);
4104 ptr += alignment_bytes;
4105 next_offset += alignment_bytes;
4106 buf_free_len -= alignment_bytes;
4107 }
4108 eainfo->NextEntryOffset = cpu_to_le32(next_offset);
4109 prev_eainfo = eainfo;
4110 eainfo = (struct smb2_ea_info *)ptr;
4111 rsp_data_cnt += next_offset;
4112
4113 if (req->InputBufferLength) {
4114 ksmbd_debug(SMB, "single entry requested\n");
4115 break;
4116 }
4117 }
4118
4119 /* no more ea entries */
4120 prev_eainfo->NextEntryOffset = 0;
4121done:
4122 rc = 0;
4123 if (rsp_data_cnt == 0)
4124 rsp->hdr.Status = STATUS_NO_EAS_ON_FILE;
4125 rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt);
4126 inc_rfc1001_len(rsp_org, rsp_data_cnt);
4127out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004128 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004129 return rc;
4130}
4131
4132static void get_file_access_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004133 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004134{
4135 struct smb2_file_access_info *file_info;
4136
4137 file_info = (struct smb2_file_access_info *)rsp->Buffer;
4138 file_info->AccessFlags = fp->daccess;
4139 rsp->OutputBufferLength =
4140 cpu_to_le32(sizeof(struct smb2_file_access_info));
4141 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info));
4142}
4143
4144static int get_file_basic_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004145 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004146{
4147 struct smb2_file_all_info *basic_info;
4148 struct kstat stat;
4149 u64 time;
4150
4151 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004152 pr_err("no right to read the attributes : 0x%x\n",
4153 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004154 return -EACCES;
4155 }
4156
4157 basic_info = (struct smb2_file_all_info *)rsp->Buffer;
Hyunchul Leeaf349832021-06-30 18:25:53 +09004158 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4159 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004160 basic_info->CreationTime = cpu_to_le64(fp->create_time);
4161 time = ksmbd_UnixTimeToNT(stat.atime);
4162 basic_info->LastAccessTime = cpu_to_le64(time);
4163 time = ksmbd_UnixTimeToNT(stat.mtime);
4164 basic_info->LastWriteTime = cpu_to_le64(time);
4165 time = ksmbd_UnixTimeToNT(stat.ctime);
4166 basic_info->ChangeTime = cpu_to_le64(time);
4167 basic_info->Attributes = fp->f_ci->m_fattr;
4168 basic_info->Pad1 = 0;
4169 rsp->OutputBufferLength =
Namjae Jeon64b39f42021-03-30 14:25:35 +09004170 cpu_to_le32(offsetof(struct smb2_file_all_info, AllocationSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09004171 inc_rfc1001_len(rsp_org, offsetof(struct smb2_file_all_info,
4172 AllocationSize));
4173 return 0;
4174}
4175
4176static unsigned long long get_allocation_size(struct inode *inode,
Namjae Jeon070fb212021-05-26 17:57:12 +09004177 struct kstat *stat)
Namjae Jeone2f34482021-03-16 10:49:09 +09004178{
4179 unsigned long long alloc_size = 0;
4180
4181 if (!S_ISDIR(stat->mode)) {
4182 if ((inode->i_blocks << 9) <= stat->size)
4183 alloc_size = stat->size;
4184 else
4185 alloc_size = inode->i_blocks << 9;
Namjae Jeone2f34482021-03-16 10:49:09 +09004186 }
4187
4188 return alloc_size;
4189}
4190
4191static void get_file_standard_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004192 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004193{
4194 struct smb2_file_standard_info *sinfo;
4195 unsigned int delete_pending;
4196 struct inode *inode;
4197 struct kstat stat;
4198
Namjae Jeonab0b2632021-06-29 09:20:13 +09004199 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004200 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004201
4202 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4203 delete_pending = ksmbd_inode_pending_delete(fp);
4204
4205 sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat));
4206 sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4207 sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending);
4208 sinfo->DeletePending = delete_pending;
4209 sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4210 rsp->OutputBufferLength =
4211 cpu_to_le32(sizeof(struct smb2_file_standard_info));
4212 inc_rfc1001_len(rsp_org,
4213 sizeof(struct smb2_file_standard_info));
4214}
4215
4216static void get_file_alignment_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004217 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004218{
4219 struct smb2_file_alignment_info *file_info;
4220
4221 file_info = (struct smb2_file_alignment_info *)rsp->Buffer;
4222 file_info->AlignmentRequirement = 0;
4223 rsp->OutputBufferLength =
4224 cpu_to_le32(sizeof(struct smb2_file_alignment_info));
4225 inc_rfc1001_len(rsp_org,
4226 sizeof(struct smb2_file_alignment_info));
4227}
4228
4229static int get_file_all_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004230 struct smb2_query_info_rsp *rsp,
4231 struct ksmbd_file *fp,
4232 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004233{
4234 struct ksmbd_conn *conn = work->conn;
4235 struct smb2_file_all_info *file_info;
4236 unsigned int delete_pending;
4237 struct inode *inode;
4238 struct kstat stat;
4239 int conv_len;
4240 char *filename;
4241 u64 time;
4242
4243 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4244 ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004245 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004246 return -EACCES;
4247 }
4248
4249 filename = convert_to_nt_pathname(fp->filename,
4250 work->tcon->share_conf->path);
4251 if (!filename)
4252 return -ENOMEM;
4253
Namjae Jeonab0b2632021-06-29 09:20:13 +09004254 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004255 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004256
4257 ksmbd_debug(SMB, "filename = %s\n", filename);
4258 delete_pending = ksmbd_inode_pending_delete(fp);
4259 file_info = (struct smb2_file_all_info *)rsp->Buffer;
4260
4261 file_info->CreationTime = cpu_to_le64(fp->create_time);
4262 time = ksmbd_UnixTimeToNT(stat.atime);
4263 file_info->LastAccessTime = cpu_to_le64(time);
4264 time = ksmbd_UnixTimeToNT(stat.mtime);
4265 file_info->LastWriteTime = cpu_to_le64(time);
4266 time = ksmbd_UnixTimeToNT(stat.ctime);
4267 file_info->ChangeTime = cpu_to_le64(time);
4268 file_info->Attributes = fp->f_ci->m_fattr;
4269 file_info->Pad1 = 0;
4270 file_info->AllocationSize =
4271 cpu_to_le64(get_allocation_size(inode, &stat));
4272 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4273 file_info->NumberOfLinks =
4274 cpu_to_le32(get_nlink(&stat) - delete_pending);
4275 file_info->DeletePending = delete_pending;
4276 file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4277 file_info->Pad2 = 0;
4278 file_info->IndexNumber = cpu_to_le64(stat.ino);
4279 file_info->EASize = 0;
4280 file_info->AccessFlags = fp->daccess;
4281 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4282 file_info->Mode = fp->coption;
4283 file_info->AlignmentRequirement = 0;
Namjae Jeon070fb212021-05-26 17:57:12 +09004284 conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename,
4285 PATH_MAX, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004286 conv_len *= 2;
4287 file_info->FileNameLength = cpu_to_le32(conv_len);
4288 rsp->OutputBufferLength =
4289 cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1);
4290 kfree(filename);
4291 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4292 return 0;
4293}
4294
4295static void get_file_alternate_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004296 struct smb2_query_info_rsp *rsp,
4297 struct ksmbd_file *fp,
4298 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004299{
4300 struct ksmbd_conn *conn = work->conn;
4301 struct smb2_file_alt_name_info *file_info;
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004302 struct dentry *dentry = fp->filp->f_path.dentry;
Namjae Jeone2f34482021-03-16 10:49:09 +09004303 int conv_len;
Namjae Jeone2f34482021-03-16 10:49:09 +09004304
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004305 spin_lock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004306 file_info = (struct smb2_file_alt_name_info *)rsp->Buffer;
4307 conv_len = ksmbd_extract_shortname(conn,
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004308 dentry->d_name.name,
Namjae Jeone2f34482021-03-16 10:49:09 +09004309 file_info->FileName);
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004310 spin_unlock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004311 file_info->FileNameLength = cpu_to_le32(conv_len);
4312 rsp->OutputBufferLength =
4313 cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len);
4314 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4315}
4316
4317static void get_file_stream_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004318 struct smb2_query_info_rsp *rsp,
4319 struct ksmbd_file *fp,
4320 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004321{
4322 struct ksmbd_conn *conn = work->conn;
4323 struct smb2_file_stream_info *file_info;
4324 char *stream_name, *xattr_list = NULL, *stream_buf;
4325 struct kstat stat;
4326 struct path *path = &fp->filp->f_path;
4327 ssize_t xattr_list_len;
4328 int nbytes = 0, streamlen, stream_name_len, next, idx = 0;
4329
Hyunchul Leeaf349832021-06-30 18:25:53 +09004330 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4331 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004332 file_info = (struct smb2_file_stream_info *)rsp->Buffer;
4333
4334 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4335 if (xattr_list_len < 0) {
4336 goto out;
4337 } else if (!xattr_list_len) {
4338 ksmbd_debug(SMB, "empty xattr in the file\n");
4339 goto out;
4340 }
4341
4342 while (idx < xattr_list_len) {
4343 stream_name = xattr_list + idx;
4344 streamlen = strlen(stream_name);
4345 idx += streamlen + 1;
4346
4347 ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen);
4348
4349 if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004350 STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004351 continue;
4352
4353 stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN +
4354 STREAM_PREFIX_LEN);
4355 streamlen = stream_name_len;
4356
4357 /* plus : size */
4358 streamlen += 1;
4359 stream_buf = kmalloc(streamlen + 1, GFP_KERNEL);
4360 if (!stream_buf)
4361 break;
4362
4363 streamlen = snprintf(stream_buf, streamlen + 1,
Namjae Jeon070fb212021-05-26 17:57:12 +09004364 ":%s", &stream_name[XATTR_NAME_STREAM_LEN]);
Namjae Jeone2f34482021-03-16 10:49:09 +09004365
Namjae Jeon070fb212021-05-26 17:57:12 +09004366 file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes];
Namjae Jeone2f34482021-03-16 10:49:09 +09004367 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004368 stream_buf, streamlen,
4369 conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004370 streamlen *= 2;
4371 kfree(stream_buf);
4372 file_info->StreamNameLength = cpu_to_le32(streamlen);
4373 file_info->StreamSize = cpu_to_le64(stream_name_len);
4374 file_info->StreamAllocationSize = cpu_to_le64(stream_name_len);
4375
4376 next = sizeof(struct smb2_file_stream_info) + streamlen;
4377 nbytes += next;
4378 file_info->NextEntryOffset = cpu_to_le32(next);
4379 }
4380
4381 if (nbytes) {
4382 file_info = (struct smb2_file_stream_info *)
4383 &rsp->Buffer[nbytes];
4384 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004385 "::$DATA", 7, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004386 streamlen *= 2;
4387 file_info->StreamNameLength = cpu_to_le32(streamlen);
4388 file_info->StreamSize = S_ISDIR(stat.mode) ? 0 :
4389 cpu_to_le64(stat.size);
4390 file_info->StreamAllocationSize = S_ISDIR(stat.mode) ? 0 :
4391 cpu_to_le64(stat.size);
4392 nbytes += sizeof(struct smb2_file_stream_info) + streamlen;
4393 }
4394
4395 /* last entry offset should be 0 */
4396 file_info->NextEntryOffset = 0;
4397out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004398 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004399
4400 rsp->OutputBufferLength = cpu_to_le32(nbytes);
4401 inc_rfc1001_len(rsp_org, nbytes);
4402}
4403
4404static void get_file_internal_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004405 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004406{
4407 struct smb2_file_internal_info *file_info;
4408 struct kstat stat;
4409
Hyunchul Leeaf349832021-06-30 18:25:53 +09004410 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4411 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004412 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4413 file_info->IndexNumber = cpu_to_le64(stat.ino);
4414 rsp->OutputBufferLength =
4415 cpu_to_le32(sizeof(struct smb2_file_internal_info));
4416 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
4417}
4418
4419static int get_file_network_open_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004420 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004421{
4422 struct smb2_file_ntwrk_info *file_info;
4423 struct inode *inode;
4424 struct kstat stat;
4425 u64 time;
4426
4427 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004428 pr_err("no right to read the attributes : 0x%x\n",
4429 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004430 return -EACCES;
4431 }
4432
4433 file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer;
4434
Namjae Jeonab0b2632021-06-29 09:20:13 +09004435 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004436 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004437
4438 file_info->CreationTime = cpu_to_le64(fp->create_time);
4439 time = ksmbd_UnixTimeToNT(stat.atime);
4440 file_info->LastAccessTime = cpu_to_le64(time);
4441 time = ksmbd_UnixTimeToNT(stat.mtime);
4442 file_info->LastWriteTime = cpu_to_le64(time);
4443 time = ksmbd_UnixTimeToNT(stat.ctime);
4444 file_info->ChangeTime = cpu_to_le64(time);
4445 file_info->Attributes = fp->f_ci->m_fattr;
4446 file_info->AllocationSize =
4447 cpu_to_le64(get_allocation_size(inode, &stat));
4448 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4449 file_info->Reserved = cpu_to_le32(0);
4450 rsp->OutputBufferLength =
4451 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info));
4452 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info));
4453 return 0;
4454}
4455
Namjae Jeon64b39f42021-03-30 14:25:35 +09004456static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004457{
4458 struct smb2_file_ea_info *file_info;
4459
4460 file_info = (struct smb2_file_ea_info *)rsp->Buffer;
4461 file_info->EASize = 0;
4462 rsp->OutputBufferLength =
4463 cpu_to_le32(sizeof(struct smb2_file_ea_info));
4464 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info));
4465}
4466
4467static void get_file_position_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004468 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004469{
4470 struct smb2_file_pos_info *file_info;
4471
4472 file_info = (struct smb2_file_pos_info *)rsp->Buffer;
4473 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4474 rsp->OutputBufferLength =
4475 cpu_to_le32(sizeof(struct smb2_file_pos_info));
4476 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info));
4477}
4478
4479static void get_file_mode_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004480 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004481{
4482 struct smb2_file_mode_info *file_info;
4483
4484 file_info = (struct smb2_file_mode_info *)rsp->Buffer;
4485 file_info->Mode = fp->coption & FILE_MODE_INFO_MASK;
4486 rsp->OutputBufferLength =
4487 cpu_to_le32(sizeof(struct smb2_file_mode_info));
4488 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info));
4489}
4490
4491static void get_file_compression_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004492 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004493{
4494 struct smb2_file_comp_info *file_info;
4495 struct kstat stat;
4496
Hyunchul Leeaf349832021-06-30 18:25:53 +09004497 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4498 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004499
4500 file_info = (struct smb2_file_comp_info *)rsp->Buffer;
4501 file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9);
4502 file_info->CompressionFormat = COMPRESSION_FORMAT_NONE;
4503 file_info->CompressionUnitShift = 0;
4504 file_info->ChunkShift = 0;
4505 file_info->ClusterShift = 0;
4506 memset(&file_info->Reserved[0], 0, 3);
4507
4508 rsp->OutputBufferLength =
4509 cpu_to_le32(sizeof(struct smb2_file_comp_info));
4510 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info));
4511}
4512
4513static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004514 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004515{
4516 struct smb2_file_attr_tag_info *file_info;
4517
4518 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004519 pr_err("no right to read the attributes : 0x%x\n",
4520 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004521 return -EACCES;
4522 }
4523
4524 file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer;
4525 file_info->FileAttributes = fp->f_ci->m_fattr;
4526 file_info->ReparseTag = 0;
4527 rsp->OutputBufferLength =
4528 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info));
Namjae Jeon070fb212021-05-26 17:57:12 +09004529 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004530 return 0;
4531}
4532
4533static int find_file_posix_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004534 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004535{
4536 struct smb311_posix_qinfo *file_info;
Namjae Jeonab0b2632021-06-29 09:20:13 +09004537 struct inode *inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09004538 u64 time;
4539
4540 file_info = (struct smb311_posix_qinfo *)rsp->Buffer;
4541 file_info->CreationTime = cpu_to_le64(fp->create_time);
4542 time = ksmbd_UnixTimeToNT(inode->i_atime);
4543 file_info->LastAccessTime = cpu_to_le64(time);
4544 time = ksmbd_UnixTimeToNT(inode->i_mtime);
4545 file_info->LastWriteTime = cpu_to_le64(time);
4546 time = ksmbd_UnixTimeToNT(inode->i_ctime);
4547 file_info->ChangeTime = cpu_to_le64(time);
4548 file_info->DosAttributes = fp->f_ci->m_fattr;
4549 file_info->Inode = cpu_to_le64(inode->i_ino);
4550 file_info->EndOfFile = cpu_to_le64(inode->i_size);
4551 file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9);
4552 file_info->HardLinks = cpu_to_le32(inode->i_nlink);
4553 file_info->Mode = cpu_to_le32(inode->i_mode);
4554 file_info->DeviceId = cpu_to_le32(inode->i_rdev);
4555 rsp->OutputBufferLength =
4556 cpu_to_le32(sizeof(struct smb311_posix_qinfo));
Namjae Jeon64b39f42021-03-30 14:25:35 +09004557 inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo));
Namjae Jeone2f34482021-03-16 10:49:09 +09004558 return 0;
4559}
4560
Namjae Jeone2f34482021-03-16 10:49:09 +09004561static int smb2_get_info_file(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004562 struct smb2_query_info_req *req,
4563 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004564{
4565 struct ksmbd_file *fp;
4566 int fileinfoclass = 0;
4567 int rc = 0;
4568 int file_infoclass_size;
4569 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4570
4571 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004572 KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004573 /* smb2 info file called for pipe */
4574 return smb2_get_info_file_pipe(work->sess, req, rsp);
4575 }
4576
4577 if (work->next_smb2_rcv_hdr_off) {
4578 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
4579 ksmbd_debug(SMB, "Compound request set FID = %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004580 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004581 id = work->compound_fid;
4582 pid = work->compound_pfid;
4583 }
4584 }
4585
4586 if (!HAS_FILE_ID(id)) {
4587 id = le64_to_cpu(req->VolatileFileId);
4588 pid = le64_to_cpu(req->PersistentFileId);
4589 }
4590
4591 fp = ksmbd_lookup_fd_slow(work, id, pid);
4592 if (!fp)
4593 return -ENOENT;
4594
4595 fileinfoclass = req->FileInfoClass;
4596
4597 switch (fileinfoclass) {
4598 case FILE_ACCESS_INFORMATION:
4599 get_file_access_info(rsp, fp, rsp_org);
4600 file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE;
4601 break;
4602
4603 case FILE_BASIC_INFORMATION:
4604 rc = get_file_basic_info(rsp, fp, rsp_org);
4605 file_infoclass_size = FILE_BASIC_INFORMATION_SIZE;
4606 break;
4607
4608 case FILE_STANDARD_INFORMATION:
4609 get_file_standard_info(rsp, fp, rsp_org);
4610 file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE;
4611 break;
4612
4613 case FILE_ALIGNMENT_INFORMATION:
4614 get_file_alignment_info(rsp, rsp_org);
4615 file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE;
4616 break;
4617
4618 case FILE_ALL_INFORMATION:
4619 rc = get_file_all_info(work, rsp, fp, rsp_org);
4620 file_infoclass_size = FILE_ALL_INFORMATION_SIZE;
4621 break;
4622
4623 case FILE_ALTERNATE_NAME_INFORMATION:
4624 get_file_alternate_info(work, rsp, fp, rsp_org);
4625 file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE;
4626 break;
4627
4628 case FILE_STREAM_INFORMATION:
4629 get_file_stream_info(work, rsp, fp, rsp_org);
4630 file_infoclass_size = FILE_STREAM_INFORMATION_SIZE;
4631 break;
4632
4633 case FILE_INTERNAL_INFORMATION:
4634 get_file_internal_info(rsp, fp, rsp_org);
4635 file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE;
4636 break;
4637
4638 case FILE_NETWORK_OPEN_INFORMATION:
4639 rc = get_file_network_open_info(rsp, fp, rsp_org);
4640 file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE;
4641 break;
4642
4643 case FILE_EA_INFORMATION:
4644 get_file_ea_info(rsp, rsp_org);
4645 file_infoclass_size = FILE_EA_INFORMATION_SIZE;
4646 break;
4647
4648 case FILE_FULL_EA_INFORMATION:
4649 rc = smb2_get_ea(work, fp, req, rsp, rsp_org);
4650 file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE;
4651 break;
4652
4653 case FILE_POSITION_INFORMATION:
4654 get_file_position_info(rsp, fp, rsp_org);
4655 file_infoclass_size = FILE_POSITION_INFORMATION_SIZE;
4656 break;
4657
4658 case FILE_MODE_INFORMATION:
4659 get_file_mode_info(rsp, fp, rsp_org);
4660 file_infoclass_size = FILE_MODE_INFORMATION_SIZE;
4661 break;
4662
4663 case FILE_COMPRESSION_INFORMATION:
4664 get_file_compression_info(rsp, fp, rsp_org);
4665 file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE;
4666 break;
4667
4668 case FILE_ATTRIBUTE_TAG_INFORMATION:
4669 rc = get_file_attribute_tag_info(rsp, fp, rsp_org);
4670 file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE;
4671 break;
4672 case SMB_FIND_FILE_POSIX_INFO:
4673 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004674 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004675 rc = -EOPNOTSUPP;
4676 } else {
4677 rc = find_file_posix_info(rsp, fp, rsp_org);
4678 file_infoclass_size = sizeof(struct smb311_posix_qinfo);
4679 }
4680 break;
4681 default:
4682 ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n",
4683 fileinfoclass);
4684 rc = -EOPNOTSUPP;
4685 }
4686 if (!rc)
4687 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4688 rsp,
4689 file_infoclass_size);
4690 ksmbd_fd_put(work, fp);
4691 return rc;
4692}
4693
Namjae Jeone2f34482021-03-16 10:49:09 +09004694static int smb2_get_info_filesystem(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004695 struct smb2_query_info_req *req,
4696 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004697{
4698 struct ksmbd_session *sess = work->sess;
4699 struct ksmbd_conn *conn = sess->conn;
4700 struct ksmbd_share_config *share = work->tcon->share_conf;
4701 int fsinfoclass = 0;
4702 struct kstatfs stfs;
4703 struct path path;
4704 int rc = 0, len;
4705 int fs_infoclass_size = 0;
Hyunchul Leea6a5fa72021-05-26 18:59:06 +09004706 int lookup_flags = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09004707
Hyunchul Leea6a5fa72021-05-26 18:59:06 +09004708 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_FOLLOW_SYMLINKS))
4709 lookup_flags = LOOKUP_FOLLOW;
4710
4711 rc = ksmbd_vfs_kern_path(share->path, lookup_flags, &path, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004712 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004713 pr_err("cannot create vfs path\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004714 return -EIO;
4715 }
4716
4717 rc = vfs_statfs(&path, &stfs);
4718 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004719 pr_err("cannot do stat of path %s\n", share->path);
Namjae Jeone2f34482021-03-16 10:49:09 +09004720 path_put(&path);
4721 return -EIO;
4722 }
4723
4724 fsinfoclass = req->FileInfoClass;
4725
4726 switch (fsinfoclass) {
4727 case FS_DEVICE_INFORMATION:
4728 {
4729 struct filesystem_device_info *info;
4730
4731 info = (struct filesystem_device_info *)rsp->Buffer;
4732
4733 info->DeviceType = cpu_to_le32(stfs.f_type);
4734 info->DeviceCharacteristics = cpu_to_le32(0x00000020);
4735 rsp->OutputBufferLength = cpu_to_le32(8);
4736 inc_rfc1001_len(rsp_org, 8);
4737 fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE;
4738 break;
4739 }
4740 case FS_ATTRIBUTE_INFORMATION:
4741 {
4742 struct filesystem_attribute_info *info;
4743 size_t sz;
4744
4745 info = (struct filesystem_attribute_info *)rsp->Buffer;
4746 info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS |
4747 FILE_PERSISTENT_ACLS |
4748 FILE_UNICODE_ON_DISK |
4749 FILE_CASE_PRESERVED_NAMES |
Namjae Jeoneb817362021-05-18 10:37:59 +09004750 FILE_CASE_SENSITIVE_SEARCH |
4751 FILE_SUPPORTS_BLOCK_REFCOUNTING);
Namjae Jeone2f34482021-03-16 10:49:09 +09004752
4753 info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps);
4754
4755 info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen);
4756 len = smbConvertToUTF16((__le16 *)info->FileSystemName,
4757 "NTFS", PATH_MAX, conn->local_nls, 0);
4758 len = len * 2;
4759 info->FileSystemNameLen = cpu_to_le32(len);
4760 sz = sizeof(struct filesystem_attribute_info) - 2 + len;
4761 rsp->OutputBufferLength = cpu_to_le32(sz);
4762 inc_rfc1001_len(rsp_org, sz);
4763 fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE;
4764 break;
4765 }
4766 case FS_VOLUME_INFORMATION:
4767 {
4768 struct filesystem_vol_info *info;
4769 size_t sz;
4770
4771 info = (struct filesystem_vol_info *)(rsp->Buffer);
4772 info->VolumeCreationTime = 0;
4773 /* Taking dummy value of serial number*/
4774 info->SerialNumber = cpu_to_le32(0xbc3ac512);
4775 len = smbConvertToUTF16((__le16 *)info->VolumeLabel,
4776 share->name, PATH_MAX,
4777 conn->local_nls, 0);
4778 len = len * 2;
4779 info->VolumeLabelSize = cpu_to_le32(len);
4780 info->Reserved = 0;
4781 sz = sizeof(struct filesystem_vol_info) - 2 + len;
4782 rsp->OutputBufferLength = cpu_to_le32(sz);
4783 inc_rfc1001_len(rsp_org, sz);
4784 fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE;
4785 break;
4786 }
4787 case FS_SIZE_INFORMATION:
4788 {
4789 struct filesystem_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004790
4791 info = (struct filesystem_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004792 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4793 info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004794 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4795 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004796 rsp->OutputBufferLength = cpu_to_le32(24);
4797 inc_rfc1001_len(rsp_org, 24);
4798 fs_infoclass_size = FS_SIZE_INFORMATION_SIZE;
4799 break;
4800 }
4801 case FS_FULL_SIZE_INFORMATION:
4802 {
4803 struct smb2_fs_full_size_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004804
4805 info = (struct smb2_fs_full_size_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004806 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4807 info->CallerAvailableAllocationUnits =
4808 cpu_to_le64(stfs.f_bavail);
4809 info->ActualAvailableAllocationUnits =
4810 cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004811 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4812 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004813 rsp->OutputBufferLength = cpu_to_le32(32);
4814 inc_rfc1001_len(rsp_org, 32);
4815 fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE;
4816 break;
4817 }
4818 case FS_OBJECT_ID_INFORMATION:
4819 {
4820 struct object_id_info *info;
4821
4822 info = (struct object_id_info *)(rsp->Buffer);
4823
4824 if (!user_guest(sess->user))
4825 memcpy(info->objid, user_passkey(sess->user), 16);
4826 else
4827 memset(info->objid, 0, 16);
4828
4829 info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC);
4830 info->extended_info.version = cpu_to_le32(1);
4831 info->extended_info.release = cpu_to_le32(1);
4832 info->extended_info.rel_date = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004833 memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0"));
Namjae Jeone2f34482021-03-16 10:49:09 +09004834 rsp->OutputBufferLength = cpu_to_le32(64);
4835 inc_rfc1001_len(rsp_org, 64);
4836 fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE;
4837 break;
4838 }
4839 case FS_SECTOR_SIZE_INFORMATION:
4840 {
4841 struct smb3_fs_ss_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004842
4843 info = (struct smb3_fs_ss_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004844
Namjae Jeon131bac12021-06-22 16:20:47 +09004845 info->LogicalBytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004846 info->PhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09004847 cpu_to_le32(stfs.f_bsize);
4848 info->PhysicalBytesPerSectorForPerf = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004849 info->FSEffPhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09004850 cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004851 info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE |
4852 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE);
4853 info->ByteOffsetForSectorAlignment = 0;
4854 info->ByteOffsetForPartitionAlignment = 0;
4855 rsp->OutputBufferLength = cpu_to_le32(28);
4856 inc_rfc1001_len(rsp_org, 28);
4857 fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE;
4858 break;
4859 }
4860 case FS_CONTROL_INFORMATION:
4861 {
4862 /*
4863 * TODO : The current implementation is based on
4864 * test result with win7(NTFS) server. It's need to
4865 * modify this to get valid Quota values
4866 * from Linux kernel
4867 */
4868 struct smb2_fs_control_info *info;
4869
4870 info = (struct smb2_fs_control_info *)(rsp->Buffer);
4871 info->FreeSpaceStartFiltering = 0;
4872 info->FreeSpaceThreshold = 0;
4873 info->FreeSpaceStopFiltering = 0;
4874 info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID);
4875 info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID);
4876 info->Padding = 0;
4877 rsp->OutputBufferLength = cpu_to_le32(48);
4878 inc_rfc1001_len(rsp_org, 48);
4879 fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE;
4880 break;
4881 }
4882 case FS_POSIX_INFORMATION:
4883 {
4884 struct filesystem_posix_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004885
4886 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004887 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004888 rc = -EOPNOTSUPP;
4889 } else {
4890 info = (struct filesystem_posix_info *)(rsp->Buffer);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004891 info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004892 info->BlockSize = cpu_to_le32(stfs.f_bsize);
4893 info->TotalBlocks = cpu_to_le64(stfs.f_blocks);
4894 info->BlocksAvail = cpu_to_le64(stfs.f_bfree);
4895 info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail);
4896 info->TotalFileNodes = cpu_to_le64(stfs.f_files);
4897 info->FreeFileNodes = cpu_to_le64(stfs.f_ffree);
4898 rsp->OutputBufferLength = cpu_to_le32(56);
4899 inc_rfc1001_len(rsp_org, 56);
4900 fs_infoclass_size = FS_POSIX_INFORMATION_SIZE;
4901 }
4902 break;
4903 }
4904 default:
4905 path_put(&path);
4906 return -EOPNOTSUPP;
4907 }
4908 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4909 rsp,
4910 fs_infoclass_size);
4911 path_put(&path);
4912 return rc;
4913}
4914
4915static int smb2_get_info_sec(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004916 struct smb2_query_info_req *req,
4917 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004918{
4919 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09004920 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09004921 struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL;
4922 struct smb_fattr fattr = {{0}};
4923 struct inode *inode;
4924 __u32 secdesclen;
4925 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4926 int addition_info = le32_to_cpu(req->AdditionalInformation);
4927 int rc;
4928
Namjae Jeone294f782021-06-28 15:26:37 +09004929 if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
4930 PROTECTED_DACL_SECINFO |
4931 UNPROTECTED_DACL_SECINFO)) {
4932 pr_err("Unsupported addition info: 0x%x)\n",
4933 addition_info);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09004934
4935 pntsd->revision = cpu_to_le16(1);
4936 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED);
4937 pntsd->osidoffset = 0;
4938 pntsd->gsidoffset = 0;
4939 pntsd->sacloffset = 0;
4940 pntsd->dacloffset = 0;
4941
4942 secdesclen = sizeof(struct smb_ntsd);
4943 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
4944 inc_rfc1001_len(rsp_org, secdesclen);
4945
4946 return 0;
4947 }
4948
Namjae Jeone2f34482021-03-16 10:49:09 +09004949 if (work->next_smb2_rcv_hdr_off) {
4950 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
4951 ksmbd_debug(SMB, "Compound request set FID = %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004952 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004953 id = work->compound_fid;
4954 pid = work->compound_pfid;
4955 }
4956 }
4957
4958 if (!HAS_FILE_ID(id)) {
4959 id = le64_to_cpu(req->VolatileFileId);
4960 pid = le64_to_cpu(req->PersistentFileId);
4961 }
4962
4963 fp = ksmbd_lookup_fd_slow(work, id, pid);
4964 if (!fp)
4965 return -ENOENT;
4966
Hyunchul Lee465d7202021-07-03 12:10:36 +09004967 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeonab0b2632021-06-29 09:20:13 +09004968 inode = file_inode(fp->filp);
Namjae Jeon3d47e542021-04-20 14:25:35 +09004969 ksmbd_acls_fattr(&fattr, inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09004970
4971 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004972 KSMBD_SHARE_FLAG_ACL_XATTR))
Hyunchul Lee465d7202021-07-03 12:10:36 +09004973 ksmbd_vfs_get_sd_xattr(work->conn, user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09004974 fp->filp->f_path.dentry, &ppntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09004975
Hyunchul Lee465d7202021-07-03 12:10:36 +09004976 rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info,
4977 &secdesclen, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09004978 posix_acl_release(fattr.cf_acls);
4979 posix_acl_release(fattr.cf_dacls);
4980 kfree(ppntsd);
4981 ksmbd_fd_put(work, fp);
4982 if (rc)
4983 return rc;
4984
4985 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
4986 inc_rfc1001_len(rsp_org, secdesclen);
4987 return 0;
4988}
4989
4990/**
4991 * smb2_query_info() - handler for smb2 query info command
4992 * @work: smb work containing query info request buffer
4993 *
4994 * Return: 0 on success, otherwise error
4995 */
4996int smb2_query_info(struct ksmbd_work *work)
4997{
4998 struct smb2_query_info_req *req;
4999 struct smb2_query_info_rsp *rsp, *rsp_org;
5000 int rc = 0;
5001
Namjae Jeone5066492021-03-30 12:35:23 +09005002 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005003 WORK_BUFFERS(work, req, rsp);
5004
5005 ksmbd_debug(SMB, "GOT query info request\n");
5006
5007 switch (req->InfoType) {
5008 case SMB2_O_INFO_FILE:
5009 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
5010 rc = smb2_get_info_file(work, req, rsp, (void *)rsp_org);
5011 break;
5012 case SMB2_O_INFO_FILESYSTEM:
5013 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n");
5014 rc = smb2_get_info_filesystem(work, req, rsp, (void *)rsp_org);
5015 break;
5016 case SMB2_O_INFO_SECURITY:
5017 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
5018 rc = smb2_get_info_sec(work, req, rsp, (void *)rsp_org);
5019 break;
5020 default:
5021 ksmbd_debug(SMB, "InfoType %d not supported yet\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005022 req->InfoType);
Namjae Jeone2f34482021-03-16 10:49:09 +09005023 rc = -EOPNOTSUPP;
5024 }
5025
5026 if (rc < 0) {
5027 if (rc == -EACCES)
5028 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5029 else if (rc == -ENOENT)
5030 rsp->hdr.Status = STATUS_FILE_CLOSED;
5031 else if (rc == -EIO)
5032 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5033 else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0)
5034 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5035 smb2_set_err_rsp(work);
5036
5037 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005038 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005039 return rc;
5040 }
5041 rsp->StructureSize = cpu_to_le16(9);
5042 rsp->OutputBufferOffset = cpu_to_le16(72);
5043 inc_rfc1001_len(rsp_org, 8);
5044 return 0;
5045}
5046
5047/**
5048 * smb2_close_pipe() - handler for closing IPC pipe
5049 * @work: smb work containing close request buffer
5050 *
5051 * Return: 0
5052 */
5053static noinline int smb2_close_pipe(struct ksmbd_work *work)
5054{
Namjae Jeon64b39f42021-03-30 14:25:35 +09005055 u64 id;
Namjae Jeone5066492021-03-30 12:35:23 +09005056 struct smb2_close_req *req = work->request_buf;
5057 struct smb2_close_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005058
5059 id = le64_to_cpu(req->VolatileFileId);
5060 ksmbd_session_rpc_close(work->sess, id);
5061
5062 rsp->StructureSize = cpu_to_le16(60);
5063 rsp->Flags = 0;
5064 rsp->Reserved = 0;
5065 rsp->CreationTime = 0;
5066 rsp->LastAccessTime = 0;
5067 rsp->LastWriteTime = 0;
5068 rsp->ChangeTime = 0;
5069 rsp->AllocationSize = 0;
5070 rsp->EndOfFile = 0;
5071 rsp->Attributes = 0;
5072 inc_rfc1001_len(rsp, 60);
5073 return 0;
5074}
5075
5076/**
5077 * smb2_close() - handler for smb2 close file command
5078 * @work: smb work containing close request buffer
5079 *
5080 * Return: 0
5081 */
5082int smb2_close(struct ksmbd_work *work)
5083{
5084 unsigned int volatile_id = KSMBD_NO_FID;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005085 u64 sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09005086 struct smb2_close_req *req;
5087 struct smb2_close_rsp *rsp;
5088 struct smb2_close_rsp *rsp_org;
5089 struct ksmbd_conn *conn = work->conn;
5090 struct ksmbd_file *fp;
5091 struct inode *inode;
5092 u64 time;
5093 int err = 0;
5094
Namjae Jeone5066492021-03-30 12:35:23 +09005095 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005096 WORK_BUFFERS(work, req, rsp);
5097
5098 if (test_share_config_flag(work->tcon->share_conf,
5099 KSMBD_SHARE_FLAG_PIPE)) {
5100 ksmbd_debug(SMB, "IPC pipe close request\n");
5101 return smb2_close_pipe(work);
5102 }
5103
5104 sess_id = le64_to_cpu(req->hdr.SessionId);
5105 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5106 sess_id = work->compound_sid;
5107
5108 work->compound_sid = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005109 if (check_session_id(conn, sess_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005110 work->compound_sid = sess_id;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005111 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005112 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
5113 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5114 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5115 err = -EBADF;
5116 goto out;
5117 }
5118
5119 if (work->next_smb2_rcv_hdr_off &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005120 !HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005121 if (!HAS_FILE_ID(work->compound_fid)) {
5122 /* file already closed, return FILE_CLOSED */
5123 ksmbd_debug(SMB, "file already closed\n");
5124 rsp->hdr.Status = STATUS_FILE_CLOSED;
5125 err = -EBADF;
5126 goto out;
5127 } else {
5128 ksmbd_debug(SMB, "Compound request set FID = %u:%u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005129 work->compound_fid,
5130 work->compound_pfid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005131 volatile_id = work->compound_fid;
5132
5133 /* file closed, stored id is not valid anymore */
5134 work->compound_fid = KSMBD_NO_FID;
5135 work->compound_pfid = KSMBD_NO_FID;
5136 }
5137 } else {
5138 volatile_id = le64_to_cpu(req->VolatileFileId);
5139 }
5140 ksmbd_debug(SMB, "volatile_id = %u\n", volatile_id);
5141
5142 rsp->StructureSize = cpu_to_le16(60);
5143 rsp->Reserved = 0;
5144
5145 if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) {
5146 fp = ksmbd_lookup_fd_fast(work, volatile_id);
5147 if (!fp) {
5148 err = -ENOENT;
5149 goto out;
5150 }
5151
Namjae Jeonab0b2632021-06-29 09:20:13 +09005152 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005153 rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
5154 rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 :
5155 cpu_to_le64(inode->i_blocks << 9);
5156 rsp->EndOfFile = cpu_to_le64(inode->i_size);
5157 rsp->Attributes = fp->f_ci->m_fattr;
5158 rsp->CreationTime = cpu_to_le64(fp->create_time);
5159 time = ksmbd_UnixTimeToNT(inode->i_atime);
5160 rsp->LastAccessTime = cpu_to_le64(time);
5161 time = ksmbd_UnixTimeToNT(inode->i_mtime);
5162 rsp->LastWriteTime = cpu_to_le64(time);
5163 time = ksmbd_UnixTimeToNT(inode->i_ctime);
5164 rsp->ChangeTime = cpu_to_le64(time);
5165 ksmbd_fd_put(work, fp);
5166 } else {
5167 rsp->Flags = 0;
5168 rsp->AllocationSize = 0;
5169 rsp->EndOfFile = 0;
5170 rsp->Attributes = 0;
5171 rsp->CreationTime = 0;
5172 rsp->LastAccessTime = 0;
5173 rsp->LastWriteTime = 0;
5174 rsp->ChangeTime = 0;
5175 }
5176
5177 err = ksmbd_close_fd(work, volatile_id);
5178out:
5179 if (err) {
5180 if (rsp->hdr.Status == 0)
5181 rsp->hdr.Status = STATUS_FILE_CLOSED;
5182 smb2_set_err_rsp(work);
5183 } else {
5184 inc_rfc1001_len(rsp_org, 60);
5185 }
5186
5187 return 0;
5188}
5189
5190/**
5191 * smb2_echo() - handler for smb2 echo(ping) command
5192 * @work: smb work containing echo request buffer
5193 *
5194 * Return: 0
5195 */
5196int smb2_echo(struct ksmbd_work *work)
5197{
Namjae Jeone5066492021-03-30 12:35:23 +09005198 struct smb2_echo_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005199
5200 rsp->StructureSize = cpu_to_le16(4);
5201 rsp->Reserved = 0;
5202 inc_rfc1001_len(rsp, 4);
5203 return 0;
5204}
5205
Namjae Jeone2f34482021-03-16 10:49:09 +09005206static int smb2_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005207 struct smb2_file_rename_info *file_info,
5208 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005209{
5210 struct ksmbd_share_config *share = fp->tcon->share_conf;
5211 char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL;
5212 char *pathname = NULL;
5213 struct path path;
5214 bool file_present = true;
5215 int rc;
5216
5217 ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n");
5218 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5219 if (!pathname)
5220 return -ENOMEM;
5221
5222 abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
5223 if (IS_ERR(abs_oldname)) {
5224 rc = -EINVAL;
5225 goto out;
5226 }
5227 old_name = strrchr(abs_oldname, '/');
Namjae Jeon64b39f42021-03-30 14:25:35 +09005228 if (old_name && old_name[1] != '\0') {
Namjae Jeone2f34482021-03-16 10:49:09 +09005229 old_name++;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005230 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005231 ksmbd_debug(SMB, "can't get last component in path %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005232 abs_oldname);
Namjae Jeone2f34482021-03-16 10:49:09 +09005233 rc = -ENOENT;
5234 goto out;
5235 }
5236
5237 new_name = smb2_get_name(share,
5238 file_info->FileName,
5239 le32_to_cpu(file_info->FileNameLength),
5240 local_nls);
5241 if (IS_ERR(new_name)) {
5242 rc = PTR_ERR(new_name);
5243 goto out;
5244 }
5245
5246 if (strchr(new_name, ':')) {
5247 int s_type;
5248 char *xattr_stream_name, *stream_name = NULL;
5249 size_t xattr_stream_size;
5250 int len;
5251
5252 rc = parse_stream_name(new_name, &stream_name, &s_type);
5253 if (rc < 0)
5254 goto out;
5255
5256 len = strlen(new_name);
5257 if (new_name[len - 1] != '/') {
Namjae Jeonbde16942021-06-28 15:23:19 +09005258 pr_err("not allow base filename in rename\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005259 rc = -ESHARE;
5260 goto out;
5261 }
5262
5263 rc = ksmbd_vfs_xattr_stream_name(stream_name,
5264 &xattr_stream_name,
5265 &xattr_stream_size,
5266 s_type);
5267 if (rc)
5268 goto out;
5269
Hyunchul Leeaf349832021-06-30 18:25:53 +09005270 rc = ksmbd_vfs_setxattr(file_mnt_user_ns(fp->filp),
5271 fp->filp->f_path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09005272 xattr_stream_name,
5273 NULL, 0, 0);
5274 if (rc < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005275 pr_err("failed to store stream name in xattr: %d\n",
5276 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005277 rc = -EINVAL;
5278 goto out;
5279 }
5280
5281 goto out;
5282 }
5283
5284 ksmbd_debug(SMB, "new name %s\n", new_name);
5285 rc = ksmbd_vfs_kern_path(new_name, 0, &path, 1);
5286 if (rc)
5287 file_present = false;
5288 else
5289 path_put(&path);
5290
5291 if (ksmbd_share_veto_filename(share, new_name)) {
5292 rc = -ENOENT;
5293 ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name);
5294 goto out;
5295 }
5296
5297 if (file_info->ReplaceIfExists) {
5298 if (file_present) {
5299 rc = ksmbd_vfs_remove_file(work, new_name);
5300 if (rc) {
5301 if (rc != -ENOTEMPTY)
5302 rc = -EINVAL;
5303 ksmbd_debug(SMB, "cannot delete %s, rc %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005304 new_name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005305 goto out;
5306 }
5307 }
5308 } else {
5309 if (file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005310 strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005311 rc = -EEXIST;
5312 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005313 "cannot rename already existing file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005314 goto out;
5315 }
5316 }
5317
5318 rc = ksmbd_vfs_fp_rename(work, fp, new_name);
5319out:
5320 kfree(pathname);
5321 if (!IS_ERR(new_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005322 kfree(new_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005323 return rc;
5324}
5325
Namjae Jeone2f34482021-03-16 10:49:09 +09005326static int smb2_create_link(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005327 struct ksmbd_share_config *share,
5328 struct smb2_file_link_info *file_info,
5329 struct file *filp,
5330 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005331{
5332 char *link_name = NULL, *target_name = NULL, *pathname = NULL;
5333 struct path path;
5334 bool file_present = true;
5335 int rc;
5336
5337 ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n");
5338 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5339 if (!pathname)
5340 return -ENOMEM;
5341
5342 link_name = smb2_get_name(share,
5343 file_info->FileName,
5344 le32_to_cpu(file_info->FileNameLength),
5345 local_nls);
5346 if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) {
5347 rc = -EINVAL;
5348 goto out;
5349 }
5350
5351 ksmbd_debug(SMB, "link name is %s\n", link_name);
5352 target_name = d_path(&filp->f_path, pathname, PATH_MAX);
5353 if (IS_ERR(target_name)) {
5354 rc = -EINVAL;
5355 goto out;
5356 }
5357
5358 ksmbd_debug(SMB, "target name is %s\n", target_name);
5359 rc = ksmbd_vfs_kern_path(link_name, 0, &path, 0);
5360 if (rc)
5361 file_present = false;
5362 else
5363 path_put(&path);
5364
5365 if (file_info->ReplaceIfExists) {
5366 if (file_present) {
5367 rc = ksmbd_vfs_remove_file(work, link_name);
5368 if (rc) {
5369 rc = -EINVAL;
5370 ksmbd_debug(SMB, "cannot delete %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005371 link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005372 goto out;
5373 }
5374 }
5375 } else {
5376 if (file_present) {
5377 rc = -EEXIST;
5378 ksmbd_debug(SMB, "link already exists\n");
5379 goto out;
5380 }
5381 }
5382
5383 rc = ksmbd_vfs_link(work, target_name, link_name);
5384 if (rc)
5385 rc = -EINVAL;
5386out:
5387 if (!IS_ERR(link_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005388 kfree(link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005389 kfree(pathname);
5390 return rc;
5391}
5392
Namjae Jeon64b39f42021-03-30 14:25:35 +09005393static int set_file_basic_info(struct ksmbd_file *fp, char *buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09005394 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005395{
5396 struct smb2_file_all_info *file_info;
5397 struct iattr attrs;
5398 struct iattr temp_attrs;
5399 struct file *filp;
5400 struct inode *inode;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005401 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09005402 int rc;
5403
Marios Makassikis7adfd4f2021-04-27 15:30:22 +09005404 if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005405 return -EACCES;
5406
5407 file_info = (struct smb2_file_all_info *)buf;
5408 attrs.ia_valid = 0;
5409 filp = fp->filp;
5410 inode = file_inode(filp);
Hyunchul Lee465d7202021-07-03 12:10:36 +09005411 user_ns = file_mnt_user_ns(filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005412
5413 if (file_info->CreationTime)
5414 fp->create_time = le64_to_cpu(file_info->CreationTime);
5415
5416 if (file_info->LastAccessTime) {
5417 attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime);
5418 attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
5419 }
5420
5421 if (file_info->ChangeTime) {
5422 temp_attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime);
5423 attrs.ia_ctime = temp_attrs.ia_ctime;
5424 attrs.ia_valid |= ATTR_CTIME;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005425 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005426 temp_attrs.ia_ctime = inode->i_ctime;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005427 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005428
5429 if (file_info->LastWriteTime) {
5430 attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime);
5431 attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
5432 }
5433
5434 if (file_info->Attributes) {
5435 if (!S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005436 file_info->Attributes & ATTR_DIRECTORY_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005437 pr_err("can't change a file to a directory\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005438 return -EINVAL;
5439 }
5440
5441 if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == ATTR_NORMAL_LE))
5442 fp->f_ci->m_fattr = file_info->Attributes |
5443 (fp->f_ci->m_fattr & ATTR_DIRECTORY_LE);
5444 }
5445
5446 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
5447 (file_info->CreationTime || file_info->Attributes)) {
5448 struct xattr_dos_attrib da = {0};
5449
5450 da.version = 4;
5451 da.itime = fp->itime;
5452 da.create_time = fp->create_time;
5453 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
5454 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
5455 XATTR_DOSINFO_ITIME;
5456
Hyunchul Lee465d7202021-07-03 12:10:36 +09005457 rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005458 filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09005459 if (rc)
5460 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005461 "failed to restore file attribute in EA\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005462 rc = 0;
5463 }
5464
5465 /*
5466 * HACK : set ctime here to avoid ctime changed
5467 * when file_info->ChangeTime is zero.
5468 */
5469 attrs.ia_ctime = temp_attrs.ia_ctime;
5470 attrs.ia_valid |= ATTR_CTIME;
5471
5472 if (attrs.ia_valid) {
5473 struct dentry *dentry = filp->f_path.dentry;
5474 struct inode *inode = d_inode(dentry);
5475
5476 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
5477 return -EACCES;
5478
Hyunchul Lee465d7202021-07-03 12:10:36 +09005479 rc = setattr_prepare(user_ns, dentry, &attrs);
Namjae Jeone2f34482021-03-16 10:49:09 +09005480 if (rc)
5481 return -EINVAL;
5482
5483 inode_lock(inode);
Hyunchul Lee465d7202021-07-03 12:10:36 +09005484 setattr_copy(user_ns, inode, &attrs);
Namjae Jeone2f34482021-03-16 10:49:09 +09005485 attrs.ia_valid &= ~ATTR_CTIME;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005486 rc = notify_change(user_ns, dentry, &attrs, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09005487 inode_unlock(inode);
5488 }
5489 return 0;
5490}
5491
5492static int set_file_allocation_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005493 struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005494{
5495 /*
5496 * TODO : It's working fine only when store dos attributes
5497 * is not yes. need to implement a logic which works
5498 * properly with any smb.conf option
5499 */
5500
5501 struct smb2_file_alloc_info *file_alloc_info;
5502 loff_t alloc_blks;
5503 struct inode *inode;
5504 int rc;
5505
Marios Makassikisa2996692021-04-27 15:29:01 +09005506 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005507 return -EACCES;
5508
5509 file_alloc_info = (struct smb2_file_alloc_info *)buf;
5510 alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9;
5511 inode = file_inode(fp->filp);
5512
5513 if (alloc_blks > inode->i_blocks) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005514 smb_break_all_levII_oplock(work, fp, 1);
5515 rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
5516 alloc_blks * 512);
Namjae Jeone2f34482021-03-16 10:49:09 +09005517 if (rc && rc != -EOPNOTSUPP) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005518 pr_err("vfs_fallocate is failed : %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005519 return rc;
5520 }
5521 } else if (alloc_blks < inode->i_blocks) {
5522 loff_t size;
5523
5524 /*
5525 * Allocation size could be smaller than original one
5526 * which means allocated blocks in file should be
5527 * deallocated. use truncate to cut out it, but inode
5528 * size is also updated with truncate offset.
5529 * inode size is retained by backup inode size.
5530 */
5531 size = i_size_read(inode);
5532 rc = ksmbd_vfs_truncate(work, NULL, fp, alloc_blks * 512);
5533 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005534 pr_err("truncate failed! filename : %s, err %d\n",
5535 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005536 return rc;
5537 }
5538 if (size < alloc_blks * 512)
5539 i_size_write(inode, size);
5540 }
5541 return 0;
5542}
5543
Namjae Jeon64b39f42021-03-30 14:25:35 +09005544static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005545 char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005546{
5547 struct smb2_file_eof_info *file_eof_info;
5548 loff_t newsize;
5549 struct inode *inode;
5550 int rc;
5551
Marios Makassikisa2996692021-04-27 15:29:01 +09005552 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005553 return -EACCES;
5554
5555 file_eof_info = (struct smb2_file_eof_info *)buf;
5556 newsize = le64_to_cpu(file_eof_info->EndOfFile);
5557 inode = file_inode(fp->filp);
5558
5559 /*
5560 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called
5561 * on FAT32 shared device, truncate execution time is too long
5562 * and network error could cause from windows client. because
5563 * truncate of some filesystem like FAT32 fill zero data in
5564 * truncated range.
5565 */
5566 if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) {
5567 ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005568 fp->filename, newsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005569 rc = ksmbd_vfs_truncate(work, NULL, fp, newsize);
5570 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005571 ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005572 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005573 if (rc != -EAGAIN)
5574 rc = -EBADF;
5575 return rc;
5576 }
5577 }
5578 return 0;
5579}
5580
Namjae Jeon64b39f42021-03-30 14:25:35 +09005581static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005582 char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005583{
5584 struct ksmbd_file *parent_fp;
Namjae Jeon12202c02021-06-29 09:23:56 +09005585 struct dentry *parent;
5586 struct dentry *dentry = fp->filp->f_path.dentry;
5587 int ret;
Namjae Jeone2f34482021-03-16 10:49:09 +09005588
5589 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005590 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005591 return -EACCES;
5592 }
5593
5594 if (ksmbd_stream_fd(fp))
5595 goto next;
5596
Namjae Jeon12202c02021-06-29 09:23:56 +09005597 parent = dget_parent(dentry);
5598 ret = ksmbd_vfs_lock_parent(parent, dentry);
5599 if (ret) {
5600 dput(parent);
5601 return ret;
5602 }
5603
5604 parent_fp = ksmbd_lookup_fd_inode(d_inode(parent));
5605 inode_unlock(d_inode(parent));
5606 dput(parent);
5607
Namjae Jeone2f34482021-03-16 10:49:09 +09005608 if (parent_fp) {
5609 if (parent_fp->daccess & FILE_DELETE_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005610 pr_err("parent dir is opened with delete access\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005611 return -ESHARE;
5612 }
5613 }
5614next:
5615 return smb2_rename(work, fp,
5616 (struct smb2_file_rename_info *)buf,
5617 work->sess->conn->local_nls);
5618}
5619
Namjae Jeon64b39f42021-03-30 14:25:35 +09005620static int set_file_disposition_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005621{
5622 struct smb2_file_disposition_info *file_info;
5623 struct inode *inode;
5624
5625 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005626 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005627 return -EACCES;
5628 }
5629
5630 inode = file_inode(fp->filp);
5631 file_info = (struct smb2_file_disposition_info *)buf;
5632 if (file_info->DeletePending) {
5633 if (S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005634 ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY)
Namjae Jeone2f34482021-03-16 10:49:09 +09005635 return -EBUSY;
5636 ksmbd_set_inode_pending_delete(fp);
5637 } else {
5638 ksmbd_clear_inode_pending_delete(fp);
5639 }
5640 return 0;
5641}
5642
Namjae Jeon64b39f42021-03-30 14:25:35 +09005643static int set_file_position_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005644{
5645 struct smb2_file_pos_info *file_info;
5646 loff_t current_byte_offset;
Namjae Jeonee81cae2021-06-26 22:32:34 +09005647 unsigned long sector_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09005648 struct inode *inode;
5649
5650 inode = file_inode(fp->filp);
5651 file_info = (struct smb2_file_pos_info *)buf;
5652 current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset);
Namjae Jeonee81cae2021-06-26 22:32:34 +09005653 sector_size = inode->i_sb->s_blocksize;
Namjae Jeone2f34482021-03-16 10:49:09 +09005654
5655 if (current_byte_offset < 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09005656 (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE &&
5657 current_byte_offset & (sector_size - 1))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005658 pr_err("CurrentByteOffset is not valid : %llu\n",
5659 current_byte_offset);
Namjae Jeone2f34482021-03-16 10:49:09 +09005660 return -EINVAL;
5661 }
5662
5663 fp->filp->f_pos = current_byte_offset;
5664 return 0;
5665}
5666
Namjae Jeon64b39f42021-03-30 14:25:35 +09005667static int set_file_mode_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005668{
5669 struct smb2_file_mode_info *file_info;
5670 __le32 mode;
5671
5672 file_info = (struct smb2_file_mode_info *)buf;
5673 mode = file_info->Mode;
5674
Namjae Jeon64b39f42021-03-30 14:25:35 +09005675 if ((mode & ~FILE_MODE_INFO_MASK) ||
5676 (mode & FILE_SYNCHRONOUS_IO_ALERT_LE &&
5677 mode & FILE_SYNCHRONOUS_IO_NONALERT_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005678 pr_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode));
Namjae Jeone2f34482021-03-16 10:49:09 +09005679 return -EINVAL;
5680 }
5681
5682 /*
5683 * TODO : need to implement consideration for
5684 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT
5685 */
5686 ksmbd_vfs_set_fadvise(fp->filp, mode);
5687 fp->coption = mode;
5688 return 0;
5689}
5690
5691/**
5692 * smb2_set_info_file() - handler for smb2 set info command
5693 * @work: smb work containing set info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09005694 * @fp: ksmbd_file pointer
5695 * @info_class: smb2 set info class
5696 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09005697 *
5698 * Return: 0 on success, otherwise error
5699 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH
5700 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09005701static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005702 int info_class, char *buf,
5703 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005704{
5705 switch (info_class) {
5706 case FILE_BASIC_INFORMATION:
5707 return set_file_basic_info(fp, buf, share);
5708
5709 case FILE_ALLOCATION_INFORMATION:
5710 return set_file_allocation_info(work, fp, buf);
5711
5712 case FILE_END_OF_FILE_INFORMATION:
5713 return set_end_of_file_info(work, fp, buf);
5714
5715 case FILE_RENAME_INFORMATION:
Namjae Jeon64b39f42021-03-30 14:25:35 +09005716 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005717 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005718 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005719 return -EACCES;
5720 }
5721 return set_rename_info(work, fp, buf);
5722
5723 case FILE_LINK_INFORMATION:
5724 return smb2_create_link(work, work->tcon->share_conf,
Namjae Jeon070fb212021-05-26 17:57:12 +09005725 (struct smb2_file_link_info *)buf, fp->filp,
5726 work->sess->conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09005727
5728 case FILE_DISPOSITION_INFORMATION:
Namjae Jeon64b39f42021-03-30 14:25:35 +09005729 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005730 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005731 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005732 return -EACCES;
5733 }
5734 return set_file_disposition_info(fp, buf);
5735
5736 case FILE_FULL_EA_INFORMATION:
5737 {
5738 if (!(fp->daccess & FILE_WRITE_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005739 pr_err("Not permitted to write ext attr: 0x%x\n",
5740 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005741 return -EACCES;
5742 }
5743
5744 return smb2_set_ea((struct smb2_ea_info *)buf,
5745 &fp->filp->f_path);
5746 }
5747
5748 case FILE_POSITION_INFORMATION:
5749 return set_file_position_info(fp, buf);
5750
5751 case FILE_MODE_INFORMATION:
5752 return set_file_mode_info(fp, buf);
5753 }
5754
Namjae Jeonbde16942021-06-28 15:23:19 +09005755 pr_err("Unimplemented Fileinfoclass :%d\n", info_class);
Namjae Jeone2f34482021-03-16 10:49:09 +09005756 return -EOPNOTSUPP;
5757}
5758
Namjae Jeon64b39f42021-03-30 14:25:35 +09005759static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005760 char *buffer, int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005761{
5762 struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer;
5763
5764 fp->saccess |= FILE_SHARE_DELETE_LE;
5765
Hyunchul Leeef24c962021-06-30 18:25:52 +09005766 return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd,
Namjae Jeone2f34482021-03-16 10:49:09 +09005767 buf_len, false);
5768}
5769
5770/**
5771 * smb2_set_info() - handler for smb2 set info command handler
5772 * @work: smb work containing set info request buffer
5773 *
5774 * Return: 0 on success, otherwise error
5775 */
5776int smb2_set_info(struct ksmbd_work *work)
5777{
5778 struct smb2_set_info_req *req;
5779 struct smb2_set_info_rsp *rsp, *rsp_org;
5780 struct ksmbd_file *fp;
5781 int rc = 0;
5782 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5783
5784 ksmbd_debug(SMB, "Received set info request\n");
5785
Namjae Jeone5066492021-03-30 12:35:23 +09005786 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005787 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09005788 req = ksmbd_req_buf_next(work);
5789 rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09005790 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
5791 ksmbd_debug(SMB, "Compound request set FID = %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005792 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005793 id = work->compound_fid;
5794 pid = work->compound_pfid;
5795 }
5796 } else {
Namjae Jeone5066492021-03-30 12:35:23 +09005797 req = work->request_buf;
5798 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005799 }
5800
5801 if (!HAS_FILE_ID(id)) {
5802 id = le64_to_cpu(req->VolatileFileId);
5803 pid = le64_to_cpu(req->PersistentFileId);
5804 }
5805
5806 fp = ksmbd_lookup_fd_slow(work, id, pid);
5807 if (!fp) {
5808 ksmbd_debug(SMB, "Invalid id for close: %u\n", id);
5809 rc = -ENOENT;
5810 goto err_out;
5811 }
5812
5813 switch (req->InfoType) {
5814 case SMB2_O_INFO_FILE:
5815 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
5816 rc = smb2_set_info_file(work, fp, req->FileInfoClass,
5817 req->Buffer, work->tcon->share_conf);
5818 break;
5819 case SMB2_O_INFO_SECURITY:
5820 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
5821 rc = smb2_set_info_sec(fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005822 le32_to_cpu(req->AdditionalInformation),
5823 req->Buffer,
5824 le32_to_cpu(req->BufferLength));
Namjae Jeone2f34482021-03-16 10:49:09 +09005825 break;
5826 default:
5827 rc = -EOPNOTSUPP;
5828 }
5829
5830 if (rc < 0)
5831 goto err_out;
5832
5833 rsp->StructureSize = cpu_to_le16(2);
5834 inc_rfc1001_len(rsp_org, 2);
5835 ksmbd_fd_put(work, fp);
5836 return 0;
5837
5838err_out:
5839 if (rc == -EACCES || rc == -EPERM)
5840 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5841 else if (rc == -EINVAL)
5842 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5843 else if (rc == -ESHARE)
5844 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
5845 else if (rc == -ENOENT)
5846 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
5847 else if (rc == -EBUSY || rc == -ENOTEMPTY)
5848 rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY;
5849 else if (rc == -EAGAIN)
5850 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
Namjae Jeonff1d5722021-04-13 13:18:10 +09005851 else if (rc == -EBADF || rc == -ESTALE)
Namjae Jeone2f34482021-03-16 10:49:09 +09005852 rsp->hdr.Status = STATUS_INVALID_HANDLE;
5853 else if (rc == -EEXIST)
5854 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
5855 else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP)
5856 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5857 smb2_set_err_rsp(work);
5858 ksmbd_fd_put(work, fp);
Namjae Jeon070fb212021-05-26 17:57:12 +09005859 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005860 return rc;
5861}
5862
5863/**
5864 * smb2_read_pipe() - handler for smb2 read from IPC pipe
5865 * @work: smb work containing read IPC pipe command buffer
5866 *
5867 * Return: 0 on success, otherwise error
5868 */
5869static noinline int smb2_read_pipe(struct ksmbd_work *work)
5870{
5871 int nbytes = 0, err;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005872 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09005873 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeone5066492021-03-30 12:35:23 +09005874 struct smb2_read_req *req = work->request_buf;
5875 struct smb2_read_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005876
5877 id = le64_to_cpu(req->VolatileFileId);
5878
5879 inc_rfc1001_len(rsp, 16);
5880 rpc_resp = ksmbd_rpc_read(work->sess, id);
5881 if (rpc_resp) {
5882 if (rpc_resp->flags != KSMBD_RPC_OK) {
5883 err = -EINVAL;
5884 goto out;
5885 }
5886
5887 work->aux_payload_buf =
Namjae Jeon79f6b112021-04-02 12:47:14 +09005888 kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09005889 if (!work->aux_payload_buf) {
5890 err = -ENOMEM;
5891 goto out;
5892 }
5893
5894 memcpy(work->aux_payload_buf, rpc_resp->payload,
Namjae Jeon070fb212021-05-26 17:57:12 +09005895 rpc_resp->payload_sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09005896
5897 nbytes = rpc_resp->payload_sz;
5898 work->resp_hdr_sz = get_rfc1002_len(rsp) + 4;
5899 work->aux_payload_sz = nbytes;
Namjae Jeon79f6b112021-04-02 12:47:14 +09005900 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005901 }
5902
5903 rsp->StructureSize = cpu_to_le16(17);
5904 rsp->DataOffset = 80;
5905 rsp->Reserved = 0;
5906 rsp->DataLength = cpu_to_le32(nbytes);
5907 rsp->DataRemaining = 0;
5908 rsp->Reserved2 = 0;
5909 inc_rfc1001_len(rsp, nbytes);
5910 return 0;
5911
5912out:
5913 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5914 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09005915 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005916 return err;
5917}
5918
5919static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005920 struct smb2_read_req *req, void *data_buf,
5921 size_t length)
Namjae Jeone2f34482021-03-16 10:49:09 +09005922{
5923 struct smb2_buffer_desc_v1 *desc =
5924 (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
5925 int err;
5926
Namjae Jeon64b39f42021-03-30 14:25:35 +09005927 if (work->conn->dialect == SMB30_PROT_ID &&
5928 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09005929 return -EINVAL;
5930
Namjae Jeon64b39f42021-03-30 14:25:35 +09005931 if (req->ReadChannelInfoOffset == 0 ||
5932 le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09005933 return -EINVAL;
5934
5935 work->need_invalidate_rkey =
5936 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
5937 work->remote_key = le32_to_cpu(desc->token);
5938
Namjae Jeon64b39f42021-03-30 14:25:35 +09005939 err = ksmbd_conn_rdma_write(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09005940 le32_to_cpu(desc->token),
5941 le64_to_cpu(desc->offset),
5942 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09005943 if (err)
5944 return err;
5945
5946 return length;
5947}
5948
5949/**
5950 * smb2_read() - handler for smb2 read from file
5951 * @work: smb work containing read command buffer
5952 *
5953 * Return: 0 on success, otherwise error
5954 */
5955int smb2_read(struct ksmbd_work *work)
5956{
5957 struct ksmbd_conn *conn = work->conn;
5958 struct smb2_read_req *req;
5959 struct smb2_read_rsp *rsp, *rsp_org;
5960 struct ksmbd_file *fp;
5961 loff_t offset;
5962 size_t length, mincount;
5963 ssize_t nbytes = 0, remain_bytes = 0;
5964 int err = 0;
5965
Namjae Jeone5066492021-03-30 12:35:23 +09005966 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005967 WORK_BUFFERS(work, req, rsp);
5968
5969 if (test_share_config_flag(work->tcon->share_conf,
5970 KSMBD_SHARE_FLAG_PIPE)) {
5971 ksmbd_debug(SMB, "IPC pipe read request\n");
5972 return smb2_read_pipe(work);
5973 }
5974
Namjae Jeon070fb212021-05-26 17:57:12 +09005975 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
5976 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09005977 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09005978 err = -ENOENT;
5979 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005980 }
5981
5982 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005983 pr_err("Not permitted to read : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005984 err = -EACCES;
5985 goto out;
5986 }
5987
5988 offset = le64_to_cpu(req->Offset);
5989 length = le32_to_cpu(req->Length);
5990 mincount = le32_to_cpu(req->MinimumCount);
5991
5992 if (length > conn->vals->max_read_size) {
5993 ksmbd_debug(SMB, "limiting read size to max size(%u)\n",
5994 conn->vals->max_read_size);
5995 err = -EINVAL;
5996 goto out;
5997 }
5998
Namjae Jeon493fa2f2021-06-29 09:22:16 +09005999 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6000 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006001
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006002 work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006003 if (!work->aux_payload_buf) {
Dan Carpenterc1ea1112021-03-22 17:50:11 +03006004 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09006005 goto out;
6006 }
6007
6008 nbytes = ksmbd_vfs_read(work, fp, length, &offset);
6009 if (nbytes < 0) {
6010 err = nbytes;
6011 goto out;
6012 }
6013
6014 if ((nbytes == 0 && length != 0) || nbytes < mincount) {
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006015 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006016 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006017 rsp->hdr.Status = STATUS_END_OF_FILE;
6018 smb2_set_err_rsp(work);
6019 ksmbd_fd_put(work, fp);
6020 return 0;
6021 }
6022
6023 ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006024 nbytes, offset, mincount);
Namjae Jeone2f34482021-03-16 10:49:09 +09006025
6026 if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006027 req->Channel == SMB2_CHANNEL_RDMA_V1) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006028 /* write data to the client using rdma channel */
6029 remain_bytes = smb2_read_rdma_channel(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09006030 work->aux_payload_buf,
6031 nbytes);
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006032 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006033 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006034
6035 nbytes = 0;
6036 if (remain_bytes < 0) {
6037 err = (int)remain_bytes;
6038 goto out;
6039 }
6040 }
6041
6042 rsp->StructureSize = cpu_to_le16(17);
6043 rsp->DataOffset = 80;
6044 rsp->Reserved = 0;
6045 rsp->DataLength = cpu_to_le32(nbytes);
6046 rsp->DataRemaining = cpu_to_le32(remain_bytes);
6047 rsp->Reserved2 = 0;
6048 inc_rfc1001_len(rsp_org, 16);
6049 work->resp_hdr_sz = get_rfc1002_len(rsp_org) + 4;
6050 work->aux_payload_sz = nbytes;
6051 inc_rfc1001_len(rsp_org, nbytes);
6052 ksmbd_fd_put(work, fp);
6053 return 0;
6054
6055out:
6056 if (err) {
6057 if (err == -EISDIR)
6058 rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST;
6059 else if (err == -EAGAIN)
6060 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6061 else if (err == -ENOENT)
6062 rsp->hdr.Status = STATUS_FILE_CLOSED;
6063 else if (err == -EACCES)
6064 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6065 else if (err == -ESHARE)
6066 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6067 else if (err == -EINVAL)
6068 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6069 else
6070 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6071
6072 smb2_set_err_rsp(work);
6073 }
6074 ksmbd_fd_put(work, fp);
6075 return err;
6076}
6077
6078/**
6079 * smb2_write_pipe() - handler for smb2 write on IPC pipe
6080 * @work: smb work containing write IPC pipe command buffer
6081 *
6082 * Return: 0 on success, otherwise error
6083 */
6084static noinline int smb2_write_pipe(struct ksmbd_work *work)
6085{
Namjae Jeone5066492021-03-30 12:35:23 +09006086 struct smb2_write_req *req = work->request_buf;
6087 struct smb2_write_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006088 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006089 u64 id = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09006090 int err = 0, ret = 0;
6091 char *data_buf;
6092 size_t length;
6093
6094 length = le32_to_cpu(req->Length);
6095 id = le64_to_cpu(req->VolatileFileId);
6096
6097 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09006098 (offsetof(struct smb2_write_req, Buffer) - 4)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006099 data_buf = (char *)&req->Buffer[0];
6100 } else {
6101 if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006102 (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006103 pr_err("invalid write data offset %u, smb_len %u\n",
6104 le16_to_cpu(req->DataOffset),
6105 get_rfc1002_len(req));
Namjae Jeone2f34482021-03-16 10:49:09 +09006106 err = -EINVAL;
6107 goto out;
6108 }
6109
6110 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6111 le16_to_cpu(req->DataOffset));
6112 }
6113
6114 rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length);
6115 if (rpc_resp) {
6116 if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
6117 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006118 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006119 smb2_set_err_rsp(work);
6120 return -EOPNOTSUPP;
6121 }
6122 if (rpc_resp->flags != KSMBD_RPC_OK) {
6123 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6124 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006125 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006126 return ret;
6127 }
Namjae Jeon79f6b112021-04-02 12:47:14 +09006128 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006129 }
6130
6131 rsp->StructureSize = cpu_to_le16(17);
6132 rsp->DataOffset = 0;
6133 rsp->Reserved = 0;
6134 rsp->DataLength = cpu_to_le32(length);
6135 rsp->DataRemaining = 0;
6136 rsp->Reserved2 = 0;
6137 inc_rfc1001_len(rsp, 16);
6138 return 0;
6139out:
6140 if (err) {
6141 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6142 smb2_set_err_rsp(work);
6143 }
6144
6145 return err;
6146}
6147
6148static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006149 struct smb2_write_req *req,
6150 struct ksmbd_file *fp,
6151 loff_t offset, size_t length, bool sync)
Namjae Jeone2f34482021-03-16 10:49:09 +09006152{
6153 struct smb2_buffer_desc_v1 *desc;
6154 char *data_buf;
6155 int ret;
6156 ssize_t nbytes;
6157
6158 desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6159
6160 if (work->conn->dialect == SMB30_PROT_ID &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006161 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09006162 return -EINVAL;
6163
6164 if (req->Length != 0 || req->DataOffset != 0)
6165 return -EINVAL;
6166
Namjae Jeon64b39f42021-03-30 14:25:35 +09006167 if (req->WriteChannelInfoOffset == 0 ||
6168 le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09006169 return -EINVAL;
6170
6171 work->need_invalidate_rkey =
6172 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6173 work->remote_key = le32_to_cpu(desc->token);
6174
Namjae Jeon79f6b112021-04-02 12:47:14 +09006175 data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006176 if (!data_buf)
6177 return -ENOMEM;
6178
6179 ret = ksmbd_conn_rdma_read(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006180 le32_to_cpu(desc->token),
6181 le64_to_cpu(desc->offset),
6182 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006183 if (ret < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09006184 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006185 return ret;
6186 }
6187
Namjae Jeon64b39f42021-03-30 14:25:35 +09006188 ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006189 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006190 if (ret < 0)
6191 return ret;
6192
6193 return nbytes;
6194}
6195
6196/**
6197 * smb2_write() - handler for smb2 write from file
6198 * @work: smb work containing write command buffer
6199 *
6200 * Return: 0 on success, otherwise error
6201 */
6202int smb2_write(struct ksmbd_work *work)
6203{
6204 struct smb2_write_req *req;
6205 struct smb2_write_rsp *rsp, *rsp_org;
Namjae Jeonbcd62a32021-05-10 09:08:19 +09006206 struct ksmbd_file *fp = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006207 loff_t offset;
6208 size_t length;
6209 ssize_t nbytes;
6210 char *data_buf;
6211 bool writethrough = false;
6212 int err = 0;
6213
Namjae Jeone5066492021-03-30 12:35:23 +09006214 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006215 WORK_BUFFERS(work, req, rsp);
6216
Namjae Jeon64b39f42021-03-30 14:25:35 +09006217 if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006218 ksmbd_debug(SMB, "IPC pipe write request\n");
6219 return smb2_write_pipe(work);
6220 }
6221
6222 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
6223 ksmbd_debug(SMB, "User does not have write permission\n");
6224 err = -EACCES;
6225 goto out;
6226 }
6227
Namjae Jeon64b39f42021-03-30 14:25:35 +09006228 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
Namjae Jeon070fb212021-05-26 17:57:12 +09006229 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006230 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006231 err = -ENOENT;
6232 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006233 }
6234
6235 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006236 pr_err("Not permitted to write : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09006237 err = -EACCES;
6238 goto out;
6239 }
6240
6241 offset = le64_to_cpu(req->Offset);
6242 length = le32_to_cpu(req->Length);
6243
6244 if (length > work->conn->vals->max_write_size) {
6245 ksmbd_debug(SMB, "limiting write size to max size(%u)\n",
6246 work->conn->vals->max_write_size);
6247 err = -EINVAL;
6248 goto out;
6249 }
6250
6251 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6252 writethrough = true;
6253
6254 if (req->Channel != SMB2_CHANNEL_RDMA_V1 &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006255 req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006256 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeon070fb212021-05-26 17:57:12 +09006257 (offsetof(struct smb2_write_req, Buffer) - 4)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006258 data_buf = (char *)&req->Buffer[0];
6259 } else {
Namjae Jeon64b39f42021-03-30 14:25:35 +09006260 if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) ||
6261 (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006262 pr_err("invalid write data offset %u, smb_len %u\n",
6263 le16_to_cpu(req->DataOffset),
6264 get_rfc1002_len(req));
Namjae Jeone2f34482021-03-16 10:49:09 +09006265 err = -EINVAL;
6266 goto out;
6267 }
6268
6269 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6270 le16_to_cpu(req->DataOffset));
6271 }
6272
6273 ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags));
6274 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6275 writethrough = true;
6276
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006277 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6278 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006279 err = ksmbd_vfs_write(work, fp, data_buf, length, &offset,
6280 writethrough, &nbytes);
6281 if (err < 0)
6282 goto out;
6283 } else {
6284 /* read data from the client using rdma channel, and
6285 * write the data.
6286 */
6287 nbytes = smb2_write_rdma_channel(work, req, fp, offset,
Namjae Jeon070fb212021-05-26 17:57:12 +09006288 le32_to_cpu(req->RemainingBytes),
6289 writethrough);
Namjae Jeone2f34482021-03-16 10:49:09 +09006290 if (nbytes < 0) {
6291 err = (int)nbytes;
6292 goto out;
6293 }
6294 }
6295
6296 rsp->StructureSize = cpu_to_le16(17);
6297 rsp->DataOffset = 0;
6298 rsp->Reserved = 0;
6299 rsp->DataLength = cpu_to_le32(nbytes);
6300 rsp->DataRemaining = 0;
6301 rsp->Reserved2 = 0;
6302 inc_rfc1001_len(rsp_org, 16);
6303 ksmbd_fd_put(work, fp);
6304 return 0;
6305
6306out:
6307 if (err == -EAGAIN)
6308 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6309 else if (err == -ENOSPC || err == -EFBIG)
6310 rsp->hdr.Status = STATUS_DISK_FULL;
6311 else if (err == -ENOENT)
6312 rsp->hdr.Status = STATUS_FILE_CLOSED;
6313 else if (err == -EACCES)
6314 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6315 else if (err == -ESHARE)
6316 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6317 else if (err == -EINVAL)
6318 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6319 else
6320 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6321
6322 smb2_set_err_rsp(work);
6323 ksmbd_fd_put(work, fp);
6324 return err;
6325}
6326
6327/**
6328 * smb2_flush() - handler for smb2 flush file - fsync
6329 * @work: smb work containing flush command buffer
6330 *
6331 * Return: 0 on success, otherwise error
6332 */
6333int smb2_flush(struct ksmbd_work *work)
6334{
6335 struct smb2_flush_req *req;
6336 struct smb2_flush_rsp *rsp, *rsp_org;
6337 int err;
6338
Namjae Jeone5066492021-03-30 12:35:23 +09006339 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006340 WORK_BUFFERS(work, req, rsp);
6341
6342 ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006343 le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006344
6345 err = ksmbd_vfs_fsync(work,
6346 le64_to_cpu(req->VolatileFileId),
6347 le64_to_cpu(req->PersistentFileId));
6348 if (err)
6349 goto out;
6350
6351 rsp->StructureSize = cpu_to_le16(4);
6352 rsp->Reserved = 0;
6353 inc_rfc1001_len(rsp_org, 4);
6354 return 0;
6355
6356out:
6357 if (err) {
6358 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6359 smb2_set_err_rsp(work);
6360 }
6361
6362 return err;
6363}
6364
6365/**
6366 * smb2_cancel() - handler for smb2 cancel command
6367 * @work: smb work containing cancel command buffer
6368 *
6369 * Return: 0 on success, otherwise error
6370 */
6371int smb2_cancel(struct ksmbd_work *work)
6372{
6373 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09006374 struct smb2_hdr *hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006375 struct smb2_hdr *chdr;
6376 struct ksmbd_work *cancel_work = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006377 int canceled = 0;
6378 struct list_head *command_list;
6379
6380 ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006381 hdr->MessageId, hdr->Flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09006382
6383 if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) {
6384 command_list = &conn->async_requests;
6385
6386 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006387 list_for_each_entry(cancel_work, command_list,
6388 async_request_entry) {
Namjae Jeone5066492021-03-30 12:35:23 +09006389 chdr = cancel_work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006390
6391 if (cancel_work->async_id !=
Namjae Jeon64b39f42021-03-30 14:25:35 +09006392 le64_to_cpu(hdr->Id.AsyncId))
Namjae Jeone2f34482021-03-16 10:49:09 +09006393 continue;
6394
6395 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006396 "smb2 with AsyncId %llu cancelled command = 0x%x\n",
6397 le64_to_cpu(hdr->Id.AsyncId),
6398 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006399 canceled = 1;
6400 break;
6401 }
6402 spin_unlock(&conn->request_lock);
6403 } else {
6404 command_list = &conn->requests;
6405
6406 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006407 list_for_each_entry(cancel_work, command_list, request_entry) {
Namjae Jeone5066492021-03-30 12:35:23 +09006408 chdr = cancel_work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006409
6410 if (chdr->MessageId != hdr->MessageId ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006411 cancel_work == work)
Namjae Jeone2f34482021-03-16 10:49:09 +09006412 continue;
6413
6414 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006415 "smb2 with mid %llu cancelled command = 0x%x\n",
6416 le64_to_cpu(hdr->MessageId),
6417 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006418 canceled = 1;
6419 break;
6420 }
6421 spin_unlock(&conn->request_lock);
6422 }
6423
6424 if (canceled) {
6425 cancel_work->state = KSMBD_WORK_CANCELLED;
6426 if (cancel_work->cancel_fn)
6427 cancel_work->cancel_fn(cancel_work->cancel_argv);
6428 }
6429
6430 /* For SMB2_CANCEL command itself send no response*/
6431 work->send_no_response = 1;
6432 return 0;
6433}
6434
6435struct file_lock *smb_flock_init(struct file *f)
6436{
6437 struct file_lock *fl;
6438
6439 fl = locks_alloc_lock();
6440 if (!fl)
6441 goto out;
6442
6443 locks_init_lock(fl);
6444
6445 fl->fl_owner = f;
6446 fl->fl_pid = current->tgid;
6447 fl->fl_file = f;
6448 fl->fl_flags = FL_POSIX;
6449 fl->fl_ops = NULL;
6450 fl->fl_lmops = NULL;
6451
6452out:
6453 return fl;
6454}
6455
6456static int smb2_set_flock_flags(struct file_lock *flock, int flags)
6457{
6458 int cmd = -EINVAL;
6459
6460 /* Checking for wrong flag combination during lock request*/
6461 switch (flags) {
6462 case SMB2_LOCKFLAG_SHARED:
6463 ksmbd_debug(SMB, "received shared request\n");
6464 cmd = F_SETLKW;
6465 flock->fl_type = F_RDLCK;
6466 flock->fl_flags |= FL_SLEEP;
6467 break;
6468 case SMB2_LOCKFLAG_EXCLUSIVE:
6469 ksmbd_debug(SMB, "received exclusive request\n");
6470 cmd = F_SETLKW;
6471 flock->fl_type = F_WRLCK;
6472 flock->fl_flags |= FL_SLEEP;
6473 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006474 case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006475 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006476 "received shared & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006477 cmd = F_SETLK;
6478 flock->fl_type = F_RDLCK;
6479 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006480 case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006481 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006482 "received exclusive & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006483 cmd = F_SETLK;
6484 flock->fl_type = F_WRLCK;
6485 break;
6486 case SMB2_LOCKFLAG_UNLOCK:
6487 ksmbd_debug(SMB, "received unlock request\n");
6488 flock->fl_type = F_UNLCK;
6489 cmd = 0;
6490 break;
6491 }
6492
6493 return cmd;
6494}
6495
6496static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock,
Namjae Jeon070fb212021-05-26 17:57:12 +09006497 unsigned int cmd, int flags,
6498 struct list_head *lock_list)
Namjae Jeone2f34482021-03-16 10:49:09 +09006499{
6500 struct ksmbd_lock *lock;
6501
6502 lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL);
6503 if (!lock)
6504 return NULL;
6505
6506 lock->cmd = cmd;
6507 lock->fl = flock;
6508 lock->start = flock->fl_start;
6509 lock->end = flock->fl_end;
6510 lock->flags = flags;
6511 if (lock->start == lock->end)
6512 lock->zero_len = 1;
6513 INIT_LIST_HEAD(&lock->llist);
6514 INIT_LIST_HEAD(&lock->glist);
6515 list_add_tail(&lock->llist, lock_list);
6516
6517 return lock;
6518}
6519
6520static void smb2_remove_blocked_lock(void **argv)
6521{
6522 struct file_lock *flock = (struct file_lock *)argv[0];
6523
6524 ksmbd_vfs_posix_lock_unblock(flock);
6525 wake_up(&flock->fl_wait);
6526}
6527
6528static inline bool lock_defer_pending(struct file_lock *fl)
6529{
6530 /* check pending lock waiters */
6531 return waitqueue_active(&fl->fl_wait);
6532}
6533
6534/**
6535 * smb2_lock() - handler for smb2 file lock command
6536 * @work: smb work containing lock command buffer
6537 *
6538 * Return: 0 on success, otherwise error
6539 */
6540int smb2_lock(struct ksmbd_work *work)
6541{
Namjae Jeone5066492021-03-30 12:35:23 +09006542 struct smb2_lock_req *req = work->request_buf;
6543 struct smb2_lock_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006544 struct smb2_lock_element *lock_ele;
6545 struct ksmbd_file *fp = NULL;
6546 struct file_lock *flock = NULL;
6547 struct file *filp = NULL;
6548 int lock_count;
6549 int flags = 0;
6550 int cmd = 0;
6551 int err = 0, i;
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006552 u64 lock_start, lock_length;
Namjae Jeone2f34482021-03-16 10:49:09 +09006553 struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp;
6554 int nolock = 0;
6555 LIST_HEAD(lock_list);
6556 LIST_HEAD(rollback_list);
6557 int prior_lock = 0;
6558
6559 ksmbd_debug(SMB, "Received lock request\n");
6560 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006561 le64_to_cpu(req->VolatileFileId),
6562 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006563 if (!fp) {
6564 ksmbd_debug(SMB, "Invalid file id for lock : %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006565 le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006566 rsp->hdr.Status = STATUS_FILE_CLOSED;
6567 goto out2;
6568 }
6569
6570 filp = fp->filp;
6571 lock_count = le16_to_cpu(req->LockCount);
6572 lock_ele = req->locks;
6573
6574 ksmbd_debug(SMB, "lock count is %d\n", lock_count);
Namjae Jeon070fb212021-05-26 17:57:12 +09006575 if (!lock_count) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006576 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6577 goto out2;
6578 }
6579
6580 for (i = 0; i < lock_count; i++) {
6581 flags = le32_to_cpu(lock_ele[i].Flags);
6582
6583 flock = smb_flock_init(filp);
6584 if (!flock) {
6585 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6586 goto out;
6587 }
6588
6589 cmd = smb2_set_flock_flags(flock, flags);
6590
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006591 lock_start = le64_to_cpu(lock_ele[i].Offset);
6592 lock_length = le64_to_cpu(lock_ele[i].Length);
6593 if (lock_start > U64_MAX - lock_length) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006594 pr_err("Invalid lock range requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006595 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6596 goto out;
6597 }
6598
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006599 if (lock_start > OFFSET_MAX)
6600 flock->fl_start = OFFSET_MAX;
6601 else
6602 flock->fl_start = lock_start;
6603
Namjae Jeone2f34482021-03-16 10:49:09 +09006604 lock_length = le64_to_cpu(lock_ele[i].Length);
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006605 if (lock_length > OFFSET_MAX - flock->fl_start)
6606 lock_length = OFFSET_MAX - flock->fl_start;
Namjae Jeone2f34482021-03-16 10:49:09 +09006607
6608 flock->fl_end = flock->fl_start + lock_length;
6609
6610 if (flock->fl_end < flock->fl_start) {
6611 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006612 "the end offset(%llx) is smaller than the start offset(%llx)\n",
6613 flock->fl_end, flock->fl_start);
Namjae Jeone2f34482021-03-16 10:49:09 +09006614 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6615 goto out;
6616 }
6617
6618 /* Check conflict locks in one request */
6619 list_for_each_entry(cmp_lock, &lock_list, llist) {
6620 if (cmp_lock->fl->fl_start <= flock->fl_start &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006621 cmp_lock->fl->fl_end >= flock->fl_end) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006622 if (cmp_lock->fl->fl_type != F_UNLCK &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006623 flock->fl_type != F_UNLCK) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006624 pr_err("conflict two locks in one request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006625 rsp->hdr.Status =
6626 STATUS_INVALID_PARAMETER;
6627 goto out;
6628 }
6629 }
6630 }
6631
6632 smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list);
6633 if (!smb_lock) {
6634 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6635 goto out;
6636 }
6637 }
6638
6639 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6640 if (smb_lock->cmd < 0) {
6641 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6642 goto out;
6643 }
6644
6645 if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) {
6646 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6647 goto out;
6648 }
6649
Namjae Jeon64b39f42021-03-30 14:25:35 +09006650 if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) &&
6651 smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) ||
6652 (prior_lock == SMB2_LOCKFLAG_UNLOCK &&
6653 !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006654 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6655 goto out;
6656 }
6657
6658 prior_lock = smb_lock->flags;
6659
6660 if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006661 !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY))
Namjae Jeone2f34482021-03-16 10:49:09 +09006662 goto no_check_gl;
6663
6664 nolock = 1;
6665 /* check locks in global list */
6666 list_for_each_entry(cmp_lock, &global_lock_list, glist) {
6667 if (file_inode(cmp_lock->fl->fl_file) !=
Namjae Jeon64b39f42021-03-30 14:25:35 +09006668 file_inode(smb_lock->fl->fl_file))
Namjae Jeone2f34482021-03-16 10:49:09 +09006669 continue;
6670
6671 if (smb_lock->fl->fl_type == F_UNLCK) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09006672 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file &&
6673 cmp_lock->start == smb_lock->start &&
6674 cmp_lock->end == smb_lock->end &&
6675 !lock_defer_pending(cmp_lock->fl)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006676 nolock = 0;
6677 locks_free_lock(cmp_lock->fl);
6678 list_del(&cmp_lock->glist);
6679 kfree(cmp_lock);
6680 break;
6681 }
6682 continue;
6683 }
6684
6685 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) {
6686 if (smb_lock->flags & SMB2_LOCKFLAG_SHARED)
6687 continue;
6688 } else {
6689 if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED)
6690 continue;
6691 }
6692
6693 /* check zero byte lock range */
6694 if (cmp_lock->zero_len && !smb_lock->zero_len &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006695 cmp_lock->start > smb_lock->start &&
6696 cmp_lock->start < smb_lock->end) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006697 pr_err("previous lock conflict with zero byte lock range\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006698 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6699 goto out;
6700 }
6701
6702 if (smb_lock->zero_len && !cmp_lock->zero_len &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006703 smb_lock->start > cmp_lock->start &&
6704 smb_lock->start < cmp_lock->end) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006705 pr_err("current lock conflict with zero byte lock range\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006706 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6707 goto out;
6708 }
6709
6710 if (((cmp_lock->start <= smb_lock->start &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006711 cmp_lock->end > smb_lock->start) ||
6712 (cmp_lock->start < smb_lock->end && cmp_lock->end >= smb_lock->end)) &&
6713 !cmp_lock->zero_len && !smb_lock->zero_len) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006714 pr_err("Not allow lock operation on exclusive lock range\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006715 rsp->hdr.Status =
6716 STATUS_LOCK_NOT_GRANTED;
6717 goto out;
6718 }
6719 }
6720
6721 if (smb_lock->fl->fl_type == F_UNLCK && nolock) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006722 pr_err("Try to unlock nolocked range\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006723 rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED;
6724 goto out;
6725 }
6726
6727no_check_gl:
6728 if (smb_lock->zero_len) {
6729 err = 0;
6730 goto skip;
6731 }
6732
6733 flock = smb_lock->fl;
6734 list_del(&smb_lock->llist);
6735retry:
Namjae Jeone8c06192021-06-22 11:06:11 +09006736 err = vfs_lock_file(filp, smb_lock->cmd, flock, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09006737skip:
6738 if (flags & SMB2_LOCKFLAG_UNLOCK) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09006739 if (!err) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006740 ksmbd_debug(SMB, "File unlocked\n");
Namjae Jeon64b39f42021-03-30 14:25:35 +09006741 } else if (err == -ENOENT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006742 rsp->hdr.Status = STATUS_NOT_LOCKED;
6743 goto out;
6744 }
6745 locks_free_lock(flock);
6746 kfree(smb_lock);
6747 } else {
6748 if (err == FILE_LOCK_DEFERRED) {
6749 void **argv;
6750
6751 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006752 "would have to wait for getting lock\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006753 list_add_tail(&smb_lock->glist,
Namjae Jeon070fb212021-05-26 17:57:12 +09006754 &global_lock_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09006755 list_add(&smb_lock->llist, &rollback_list);
6756
6757 argv = kmalloc(sizeof(void *), GFP_KERNEL);
6758 if (!argv) {
6759 err = -ENOMEM;
6760 goto out;
6761 }
6762 argv[0] = flock;
6763
6764 err = setup_async_work(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006765 smb2_remove_blocked_lock,
6766 argv);
Namjae Jeone2f34482021-03-16 10:49:09 +09006767 if (err) {
6768 rsp->hdr.Status =
6769 STATUS_INSUFFICIENT_RESOURCES;
6770 goto out;
6771 }
6772 spin_lock(&fp->f_lock);
6773 list_add(&work->fp_entry, &fp->blocked_works);
6774 spin_unlock(&fp->f_lock);
6775
6776 smb2_send_interim_resp(work, STATUS_PENDING);
6777
6778 err = ksmbd_vfs_posix_lock_wait(flock);
6779
Hyunchul Leed4075ab2021-06-25 07:02:10 +09006780 if (work->state != KSMBD_WORK_ACTIVE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006781 list_del(&smb_lock->llist);
6782 list_del(&smb_lock->glist);
6783 locks_free_lock(flock);
6784
Hyunchul Leed4075ab2021-06-25 07:02:10 +09006785 if (work->state == KSMBD_WORK_CANCELLED) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006786 spin_lock(&fp->f_lock);
6787 list_del(&work->fp_entry);
6788 spin_unlock(&fp->f_lock);
6789 rsp->hdr.Status =
6790 STATUS_CANCELLED;
6791 kfree(smb_lock);
6792 smb2_send_interim_resp(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006793 STATUS_CANCELLED);
Namjae Jeone2f34482021-03-16 10:49:09 +09006794 work->send_no_response = 1;
6795 goto out;
6796 }
6797 init_smb2_rsp_hdr(work);
6798 smb2_set_err_rsp(work);
6799 rsp->hdr.Status =
6800 STATUS_RANGE_NOT_LOCKED;
6801 kfree(smb_lock);
6802 goto out2;
6803 }
6804
6805 list_del(&smb_lock->llist);
6806 list_del(&smb_lock->glist);
6807 spin_lock(&fp->f_lock);
6808 list_del(&work->fp_entry);
6809 spin_unlock(&fp->f_lock);
6810 goto retry;
6811 } else if (!err) {
6812 list_add_tail(&smb_lock->glist,
Namjae Jeon070fb212021-05-26 17:57:12 +09006813 &global_lock_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09006814 list_add(&smb_lock->llist, &rollback_list);
6815 ksmbd_debug(SMB, "successful in taking lock\n");
6816 } else {
6817 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6818 goto out;
6819 }
6820 }
6821 }
6822
6823 if (atomic_read(&fp->f_ci->op_count) > 1)
6824 smb_break_all_oplock(work, fp);
6825
6826 rsp->StructureSize = cpu_to_le16(4);
6827 ksmbd_debug(SMB, "successful in taking lock\n");
6828 rsp->hdr.Status = STATUS_SUCCESS;
6829 rsp->Reserved = 0;
6830 inc_rfc1001_len(rsp, 4);
6831 ksmbd_fd_put(work, fp);
6832 return err;
6833
6834out:
6835 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6836 locks_free_lock(smb_lock->fl);
6837 list_del(&smb_lock->llist);
6838 kfree(smb_lock);
6839 }
6840
6841 list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) {
6842 struct file_lock *rlock = NULL;
6843
6844 rlock = smb_flock_init(filp);
6845 rlock->fl_type = F_UNLCK;
6846 rlock->fl_start = smb_lock->start;
6847 rlock->fl_end = smb_lock->end;
6848
Namjae Jeone8c06192021-06-22 11:06:11 +09006849 err = vfs_lock_file(filp, 0, rlock, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09006850 if (err)
Namjae Jeonbde16942021-06-28 15:23:19 +09006851 pr_err("rollback unlock fail : %d\n", err);
Namjae Jeone2f34482021-03-16 10:49:09 +09006852 list_del(&smb_lock->llist);
6853 list_del(&smb_lock->glist);
6854 locks_free_lock(smb_lock->fl);
6855 locks_free_lock(rlock);
6856 kfree(smb_lock);
6857 }
6858out2:
6859 ksmbd_debug(SMB, "failed in taking lock(flags : %x)\n", flags);
6860 smb2_set_err_rsp(work);
6861 ksmbd_fd_put(work, fp);
6862 return 0;
6863}
6864
Namjae Jeon64b39f42021-03-30 14:25:35 +09006865static int fsctl_copychunk(struct ksmbd_work *work, struct smb2_ioctl_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09006866 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09006867{
6868 struct copychunk_ioctl_req *ci_req;
6869 struct copychunk_ioctl_rsp *ci_rsp;
6870 struct ksmbd_file *src_fp = NULL, *dst_fp = NULL;
6871 struct srv_copychunk *chunks;
6872 unsigned int i, chunk_count, chunk_count_written = 0;
6873 unsigned int chunk_size_written = 0;
6874 loff_t total_size_written = 0;
6875 int ret, cnt_code;
6876
6877 cnt_code = le32_to_cpu(req->CntCode);
6878 ci_req = (struct copychunk_ioctl_req *)&req->Buffer[0];
6879 ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0];
6880
6881 rsp->VolatileFileId = req->VolatileFileId;
6882 rsp->PersistentFileId = req->PersistentFileId;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006883 ci_rsp->ChunksWritten =
6884 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count());
6885 ci_rsp->ChunkBytesWritten =
6886 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size());
6887 ci_rsp->TotalBytesWritten =
6888 cpu_to_le32(ksmbd_server_side_copy_max_total_size());
Namjae Jeone2f34482021-03-16 10:49:09 +09006889
6890 chunks = (struct srv_copychunk *)&ci_req->Chunks[0];
6891 chunk_count = le32_to_cpu(ci_req->ChunkCount);
6892 total_size_written = 0;
6893
6894 /* verify the SRV_COPYCHUNK_COPY packet */
6895 if (chunk_count > ksmbd_server_side_copy_max_chunk_count() ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006896 le32_to_cpu(req->InputCount) <
6897 offsetof(struct copychunk_ioctl_req, Chunks) +
6898 chunk_count * sizeof(struct srv_copychunk)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006899 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6900 return -EINVAL;
6901 }
6902
6903 for (i = 0; i < chunk_count; i++) {
6904 if (le32_to_cpu(chunks[i].Length) == 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006905 le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size())
Namjae Jeone2f34482021-03-16 10:49:09 +09006906 break;
6907 total_size_written += le32_to_cpu(chunks[i].Length);
6908 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09006909
6910 if (i < chunk_count ||
6911 total_size_written > ksmbd_server_side_copy_max_total_size()) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006912 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6913 return -EINVAL;
6914 }
6915
6916 src_fp = ksmbd_lookup_foreign_fd(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006917 le64_to_cpu(ci_req->ResumeKey[0]));
Namjae Jeone2f34482021-03-16 10:49:09 +09006918 dst_fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006919 le64_to_cpu(req->VolatileFileId),
6920 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006921 ret = -EINVAL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006922 if (!src_fp ||
6923 src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006924 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
6925 goto out;
6926 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09006927
Namjae Jeone2f34482021-03-16 10:49:09 +09006928 if (!dst_fp) {
6929 rsp->hdr.Status = STATUS_FILE_CLOSED;
6930 goto out;
6931 }
6932
6933 /*
6934 * FILE_READ_DATA should only be included in
6935 * the FSCTL_COPYCHUNK case
6936 */
Namjae Jeon070fb212021-05-26 17:57:12 +09006937 if (cnt_code == FSCTL_COPYCHUNK &&
6938 !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006939 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6940 goto out;
6941 }
6942
6943 ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09006944 chunks, chunk_count,
6945 &chunk_count_written,
6946 &chunk_size_written,
6947 &total_size_written);
Namjae Jeone2f34482021-03-16 10:49:09 +09006948 if (ret < 0) {
6949 if (ret == -EACCES)
6950 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6951 if (ret == -EAGAIN)
6952 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6953 else if (ret == -EBADF)
6954 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6955 else if (ret == -EFBIG || ret == -ENOSPC)
6956 rsp->hdr.Status = STATUS_DISK_FULL;
6957 else if (ret == -EINVAL)
6958 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6959 else if (ret == -EISDIR)
6960 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
6961 else if (ret == -E2BIG)
6962 rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE;
6963 else
6964 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
6965 }
6966
6967 ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written);
6968 ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written);
6969 ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written);
6970out:
6971 ksmbd_fd_put(work, src_fp);
6972 ksmbd_fd_put(work, dst_fp);
6973 return ret;
6974}
6975
6976static __be32 idev_ipv4_address(struct in_device *idev)
6977{
6978 __be32 addr = 0;
6979
6980 struct in_ifaddr *ifa;
6981
6982 rcu_read_lock();
6983 in_dev_for_each_ifa_rcu(ifa, idev) {
6984 if (ifa->ifa_flags & IFA_F_SECONDARY)
6985 continue;
6986
6987 addr = ifa->ifa_address;
6988 break;
6989 }
6990 rcu_read_unlock();
6991 return addr;
6992}
6993
6994static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09006995 struct smb2_ioctl_req *req,
6996 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09006997{
6998 struct network_interface_info_ioctl_rsp *nii_rsp = NULL;
6999 int nbytes = 0;
7000 struct net_device *netdev;
7001 struct sockaddr_storage_rsp *sockaddr_storage;
7002 unsigned int flags;
7003 unsigned long long speed;
7004
7005 rtnl_lock();
7006 for_each_netdev(&init_net, netdev) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007007 if (netdev->type == ARPHRD_LOOPBACK)
7008 continue;
7009
7010 flags = dev_get_flags(netdev);
7011 if (!(flags & IFF_RUNNING))
7012 continue;
7013
7014 nii_rsp = (struct network_interface_info_ioctl_rsp *)
7015 &rsp->Buffer[nbytes];
7016 nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex);
7017
7018 /* TODO: specify the RDMA capabilities */
7019 if (netdev->num_tx_queues > 1)
7020 nii_rsp->Capability = cpu_to_le32(RSS_CAPABLE);
7021 else
7022 nii_rsp->Capability = 0;
7023
7024 nii_rsp->Next = cpu_to_le32(152);
7025 nii_rsp->Reserved = 0;
7026
7027 if (netdev->ethtool_ops->get_link_ksettings) {
7028 struct ethtool_link_ksettings cmd;
7029
7030 netdev->ethtool_ops->get_link_ksettings(netdev, &cmd);
7031 speed = cmd.base.speed;
7032 } else {
Namjae Jeonbde16942021-06-28 15:23:19 +09007033 pr_err("%s %s\n", netdev->name,
7034 "speed is unknown, defaulting to 1Gb/sec");
Namjae Jeone2f34482021-03-16 10:49:09 +09007035 speed = SPEED_1000;
7036 }
7037
7038 speed *= 1000000;
7039 nii_rsp->LinkSpeed = cpu_to_le64(speed);
7040
7041 sockaddr_storage = (struct sockaddr_storage_rsp *)
7042 nii_rsp->SockAddr_Storage;
7043 memset(sockaddr_storage, 0, 128);
7044
7045 if (conn->peer_addr.ss_family == PF_INET) {
7046 struct in_device *idev;
7047
7048 sockaddr_storage->Family = cpu_to_le16(INTERNETWORK);
7049 sockaddr_storage->addr4.Port = 0;
7050
7051 idev = __in_dev_get_rtnl(netdev);
7052 if (!idev)
7053 continue;
7054 sockaddr_storage->addr4.IPv4address =
7055 idev_ipv4_address(idev);
7056 } else {
7057 struct inet6_dev *idev6;
7058 struct inet6_ifaddr *ifa;
7059 __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address;
7060
7061 sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6);
7062 sockaddr_storage->addr6.Port = 0;
7063 sockaddr_storage->addr6.FlowInfo = 0;
7064
7065 idev6 = __in6_dev_get(netdev);
7066 if (!idev6)
7067 continue;
7068
7069 list_for_each_entry(ifa, &idev6->addr_list, if_list) {
7070 if (ifa->flags & (IFA_F_TENTATIVE |
7071 IFA_F_DEPRECATED))
7072 continue;
7073 memcpy(ipv6_addr, ifa->addr.s6_addr, 16);
7074 break;
7075 }
7076 sockaddr_storage->addr6.ScopeId = 0;
7077 }
7078
7079 nbytes += sizeof(struct network_interface_info_ioctl_rsp);
7080 }
7081 rtnl_unlock();
7082
7083 /* zero if this is last one */
7084 if (nii_rsp)
7085 nii_rsp->Next = 0;
7086
7087 if (!nbytes) {
7088 rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL;
7089 return -EINVAL;
7090 }
7091
7092 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7093 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7094 return nbytes;
7095}
7096
Namjae Jeone2f34482021-03-16 10:49:09 +09007097static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09007098 struct validate_negotiate_info_req *neg_req,
7099 struct validate_negotiate_info_rsp *neg_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007100{
7101 int ret = 0;
7102 int dialect;
7103
7104 dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects,
Namjae Jeon070fb212021-05-26 17:57:12 +09007105 neg_req->DialectCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09007106 if (dialect == BAD_PROT_ID || dialect != conn->dialect) {
7107 ret = -EINVAL;
7108 goto err_out;
7109 }
7110
7111 if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) {
7112 ret = -EINVAL;
7113 goto err_out;
7114 }
7115
7116 if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) {
7117 ret = -EINVAL;
7118 goto err_out;
7119 }
7120
7121 if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) {
7122 ret = -EINVAL;
7123 goto err_out;
7124 }
7125
7126 neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
7127 memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE);
7128 neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode);
7129 neg_rsp->Dialect = cpu_to_le16(conn->dialect);
7130err_out:
7131 return ret;
7132}
7133
Namjae Jeon64b39f42021-03-30 14:25:35 +09007134static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007135 struct file_allocated_range_buffer *qar_req,
7136 struct file_allocated_range_buffer *qar_rsp,
7137 int in_count, int *out_count)
Namjae Jeone2f34482021-03-16 10:49:09 +09007138{
7139 struct ksmbd_file *fp;
7140 loff_t start, length;
7141 int ret = 0;
7142
7143 *out_count = 0;
7144 if (in_count == 0)
7145 return -EINVAL;
7146
7147 fp = ksmbd_lookup_fd_fast(work, id);
7148 if (!fp)
7149 return -ENOENT;
7150
7151 start = le64_to_cpu(qar_req->file_offset);
7152 length = le64_to_cpu(qar_req->length);
7153
7154 ret = ksmbd_vfs_fqar_lseek(fp, start, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09007155 qar_rsp, in_count, out_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09007156 if (ret && ret != -E2BIG)
7157 *out_count = 0;
7158
7159 ksmbd_fd_put(work, fp);
7160 return ret;
7161}
7162
Namjae Jeon64b39f42021-03-30 14:25:35 +09007163static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007164 int out_buf_len, struct smb2_ioctl_req *req,
7165 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007166{
7167 struct ksmbd_rpc_command *rpc_resp;
7168 char *data_buf = (char *)&req->Buffer[0];
7169 int nbytes = 0;
7170
Namjae Jeon64b39f42021-03-30 14:25:35 +09007171 rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09007172 le32_to_cpu(req->InputCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09007173 if (rpc_resp) {
7174 if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) {
7175 /*
7176 * set STATUS_SOME_NOT_MAPPED response
7177 * for unknown domain sid.
7178 */
7179 rsp->hdr.Status = STATUS_SOME_NOT_MAPPED;
7180 } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
7181 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7182 goto out;
7183 } else if (rpc_resp->flags != KSMBD_RPC_OK) {
7184 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7185 goto out;
7186 }
7187
7188 nbytes = rpc_resp->payload_sz;
7189 if (rpc_resp->payload_sz > out_buf_len) {
7190 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7191 nbytes = out_buf_len;
7192 }
7193
7194 if (!rpc_resp->payload_sz) {
7195 rsp->hdr.Status =
7196 STATUS_UNEXPECTED_IO_ERROR;
7197 goto out;
7198 }
7199
7200 memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes);
7201 }
7202out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09007203 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007204 return nbytes;
7205}
7206
Namjae Jeon64b39f42021-03-30 14:25:35 +09007207static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007208 struct file_sparse *sparse)
Namjae Jeone2f34482021-03-16 10:49:09 +09007209{
7210 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007211 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09007212 int ret = 0;
7213 __le32 old_fattr;
7214
7215 fp = ksmbd_lookup_fd_fast(work, id);
7216 if (!fp)
7217 return -ENOENT;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007218 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007219
7220 old_fattr = fp->f_ci->m_fattr;
7221 if (sparse->SetSparse)
7222 fp->f_ci->m_fattr |= ATTR_SPARSE_FILE_LE;
7223 else
7224 fp->f_ci->m_fattr &= ~ATTR_SPARSE_FILE_LE;
7225
7226 if (fp->f_ci->m_fattr != old_fattr &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007227 test_share_config_flag(work->tcon->share_conf,
7228 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007229 struct xattr_dos_attrib da;
7230
Hyunchul Lee465d7202021-07-03 12:10:36 +09007231 ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007232 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007233 if (ret <= 0)
7234 goto out;
7235
7236 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
Hyunchul Lee465d7202021-07-03 12:10:36 +09007237 ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007238 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007239 if (ret)
7240 fp->f_ci->m_fattr = old_fattr;
7241 }
7242
7243out:
7244 ksmbd_fd_put(work, fp);
7245 return ret;
7246}
7247
7248static int fsctl_request_resume_key(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007249 struct smb2_ioctl_req *req,
7250 struct resume_key_ioctl_rsp *key_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007251{
7252 struct ksmbd_file *fp;
7253
7254 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007255 le64_to_cpu(req->VolatileFileId),
7256 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09007257 if (!fp)
7258 return -ENOENT;
7259
7260 memset(key_rsp, 0, sizeof(*key_rsp));
7261 key_rsp->ResumeKey[0] = req->VolatileFileId;
7262 key_rsp->ResumeKey[1] = req->PersistentFileId;
7263 ksmbd_fd_put(work, fp);
7264
7265 return 0;
7266}
7267
7268/**
7269 * smb2_ioctl() - handler for smb2 ioctl command
7270 * @work: smb work containing ioctl command buffer
7271 *
7272 * Return: 0 on success, otherwise error
7273 */
7274int smb2_ioctl(struct ksmbd_work *work)
7275{
7276 struct smb2_ioctl_req *req;
7277 struct smb2_ioctl_rsp *rsp, *rsp_org;
7278 int cnt_code, nbytes = 0;
7279 int out_buf_len;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007280 u64 id = KSMBD_NO_FID;
Namjae Jeone2f34482021-03-16 10:49:09 +09007281 struct ksmbd_conn *conn = work->conn;
7282 int ret = 0;
7283
Namjae Jeone5066492021-03-30 12:35:23 +09007284 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007285 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09007286 req = ksmbd_req_buf_next(work);
7287 rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09007288 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
7289 ksmbd_debug(SMB, "Compound request set FID = %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007290 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09007291 id = work->compound_fid;
7292 }
7293 } else {
Namjae Jeone5066492021-03-30 12:35:23 +09007294 req = work->request_buf;
7295 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007296 }
7297
7298 if (!HAS_FILE_ID(id))
7299 id = le64_to_cpu(req->VolatileFileId);
7300
7301 if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) {
7302 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7303 goto out;
7304 }
7305
7306 cnt_code = le32_to_cpu(req->CntCode);
7307 out_buf_len = le32_to_cpu(req->MaxOutputResponse);
7308 out_buf_len = min(KSMBD_IPC_MAX_PAYLOAD, out_buf_len);
7309
7310 switch (cnt_code) {
7311 case FSCTL_DFS_GET_REFERRALS:
7312 case FSCTL_DFS_GET_REFERRALS_EX:
7313 /* Not support DFS yet */
7314 rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED;
7315 goto out;
7316 case FSCTL_CREATE_OR_GET_OBJECT_ID:
7317 {
7318 struct file_object_buf_type1_ioctl_rsp *obj_buf;
7319
7320 nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp);
7321 obj_buf = (struct file_object_buf_type1_ioctl_rsp *)
7322 &rsp->Buffer[0];
7323
7324 /*
7325 * TODO: This is dummy implementation to pass smbtorture
7326 * Need to check correct response later
7327 */
7328 memset(obj_buf->ObjectId, 0x0, 16);
7329 memset(obj_buf->BirthVolumeId, 0x0, 16);
7330 memset(obj_buf->BirthObjectId, 0x0, 16);
7331 memset(obj_buf->DomainId, 0x0, 16);
7332
7333 break;
7334 }
7335 case FSCTL_PIPE_TRANSCEIVE:
7336 nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp);
7337 break;
7338 case FSCTL_VALIDATE_NEGOTIATE_INFO:
7339 if (conn->dialect < SMB30_PROT_ID) {
7340 ret = -EOPNOTSUPP;
7341 goto out;
7342 }
7343
7344 ret = fsctl_validate_negotiate_info(conn,
7345 (struct validate_negotiate_info_req *)&req->Buffer[0],
7346 (struct validate_negotiate_info_rsp *)&rsp->Buffer[0]);
7347 if (ret < 0)
7348 goto out;
7349
7350 nbytes = sizeof(struct validate_negotiate_info_rsp);
7351 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7352 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7353 break;
7354 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
7355 nbytes = fsctl_query_iface_info_ioctl(conn, req, rsp);
7356 if (nbytes < 0)
7357 goto out;
7358 break;
7359 case FSCTL_REQUEST_RESUME_KEY:
7360 if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) {
7361 ret = -EINVAL;
7362 goto out;
7363 }
7364
7365 ret = fsctl_request_resume_key(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007366 (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007367 if (ret < 0)
7368 goto out;
7369 rsp->PersistentFileId = req->PersistentFileId;
7370 rsp->VolatileFileId = req->VolatileFileId;
7371 nbytes = sizeof(struct resume_key_ioctl_rsp);
7372 break;
7373 case FSCTL_COPYCHUNK:
7374 case FSCTL_COPYCHUNK_WRITE:
Namjae Jeon64b39f42021-03-30 14:25:35 +09007375 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007376 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007377 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007378 ret = -EACCES;
7379 goto out;
7380 }
7381
7382 if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) {
7383 ret = -EINVAL;
7384 goto out;
7385 }
7386
7387 nbytes = sizeof(struct copychunk_ioctl_rsp);
7388 fsctl_copychunk(work, req, rsp);
7389 break;
7390 case FSCTL_SET_SPARSE:
7391 ret = fsctl_set_sparse(work, id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007392 (struct file_sparse *)&req->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007393 if (ret < 0)
7394 goto out;
7395 break;
7396 case FSCTL_SET_ZERO_DATA:
7397 {
7398 struct file_zero_data_information *zero_data;
7399 struct ksmbd_file *fp;
7400 loff_t off, len;
7401
Namjae Jeon64b39f42021-03-30 14:25:35 +09007402 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007403 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007404 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007405 ret = -EACCES;
7406 goto out;
7407 }
7408
7409 zero_data =
7410 (struct file_zero_data_information *)&req->Buffer[0];
7411
7412 fp = ksmbd_lookup_fd_fast(work, id);
7413 if (!fp) {
7414 ret = -ENOENT;
7415 goto out;
7416 }
7417
7418 off = le64_to_cpu(zero_data->FileOffset);
7419 len = le64_to_cpu(zero_data->BeyondFinalZero) - off;
7420
7421 ret = ksmbd_vfs_zero_data(work, fp, off, len);
7422 ksmbd_fd_put(work, fp);
7423 if (ret < 0)
7424 goto out;
7425 break;
7426 }
7427 case FSCTL_QUERY_ALLOCATED_RANGES:
7428 ret = fsctl_query_allocated_ranges(work, id,
7429 (struct file_allocated_range_buffer *)&req->Buffer[0],
7430 (struct file_allocated_range_buffer *)&rsp->Buffer[0],
7431 out_buf_len /
7432 sizeof(struct file_allocated_range_buffer), &nbytes);
7433 if (ret == -E2BIG) {
7434 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7435 } else if (ret < 0) {
7436 nbytes = 0;
7437 goto out;
7438 }
7439
7440 nbytes *= sizeof(struct file_allocated_range_buffer);
7441 break;
7442 case FSCTL_GET_REPARSE_POINT:
7443 {
7444 struct reparse_data_buffer *reparse_ptr;
7445 struct ksmbd_file *fp;
7446
7447 reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0];
7448 fp = ksmbd_lookup_fd_fast(work, id);
7449 if (!fp) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007450 pr_err("not found fp!!\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007451 ret = -ENOENT;
7452 goto out;
7453 }
7454
7455 reparse_ptr->ReparseTag =
Namjae Jeonab0b2632021-06-29 09:20:13 +09007456 smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode);
Namjae Jeone2f34482021-03-16 10:49:09 +09007457 reparse_ptr->ReparseDataLength = 0;
7458 ksmbd_fd_put(work, fp);
7459 nbytes = sizeof(struct reparse_data_buffer);
7460 break;
7461 }
Namjae Jeoneb817362021-05-18 10:37:59 +09007462 case FSCTL_DUPLICATE_EXTENTS_TO_FILE:
7463 {
7464 struct ksmbd_file *fp_in, *fp_out = NULL;
7465 struct duplicate_extents_to_file *dup_ext;
7466 loff_t src_off, dst_off, length, cloned;
7467
7468 dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0];
7469
7470 fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle,
Namjae Jeon070fb212021-05-26 17:57:12 +09007471 dup_ext->PersistentFileHandle);
Namjae Jeoneb817362021-05-18 10:37:59 +09007472 if (!fp_in) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007473 pr_err("not found file handle in duplicate extent to file\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007474 ret = -ENOENT;
7475 goto out;
7476 }
7477
7478 fp_out = ksmbd_lookup_fd_fast(work, id);
7479 if (!fp_out) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007480 pr_err("not found fp\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007481 ret = -ENOENT;
7482 goto dup_ext_out;
7483 }
7484
7485 src_off = le64_to_cpu(dup_ext->SourceFileOffset);
7486 dst_off = le64_to_cpu(dup_ext->TargetFileOffset);
7487 length = le64_to_cpu(dup_ext->ByteCount);
7488 cloned = vfs_clone_file_range(fp_in->filp, src_off, fp_out->filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007489 dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007490 if (cloned == -EXDEV || cloned == -EOPNOTSUPP) {
7491 ret = -EOPNOTSUPP;
7492 goto dup_ext_out;
7493 } else if (cloned != length) {
Namjae Jeonf8524772021-06-18 10:28:00 +09007494 cloned = vfs_copy_file_range(fp_in->filp, src_off,
7495 fp_out->filp, dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007496 if (cloned != length) {
7497 if (cloned < 0)
7498 ret = cloned;
7499 else
7500 ret = -EINVAL;
7501 }
7502 }
7503
7504dup_ext_out:
7505 ksmbd_fd_put(work, fp_in);
7506 ksmbd_fd_put(work, fp_out);
7507 if (ret < 0)
7508 goto out;
7509 break;
7510 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007511 default:
7512 ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007513 cnt_code);
Namjae Jeone2f34482021-03-16 10:49:09 +09007514 ret = -EOPNOTSUPP;
7515 goto out;
7516 }
7517
7518 rsp->CntCode = cpu_to_le32(cnt_code);
7519 rsp->InputCount = cpu_to_le32(0);
7520 rsp->InputOffset = cpu_to_le32(112);
7521 rsp->OutputOffset = cpu_to_le32(112);
7522 rsp->OutputCount = cpu_to_le32(nbytes);
7523 rsp->StructureSize = cpu_to_le16(49);
7524 rsp->Reserved = cpu_to_le16(0);
7525 rsp->Flags = cpu_to_le32(0);
7526 rsp->Reserved2 = cpu_to_le32(0);
7527 inc_rfc1001_len(rsp_org, 48 + nbytes);
7528
7529 return 0;
7530
7531out:
7532 if (ret == -EACCES)
7533 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7534 else if (ret == -ENOENT)
7535 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7536 else if (ret == -EOPNOTSUPP)
7537 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7538 else if (ret < 0 || rsp->hdr.Status == 0)
7539 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7540 smb2_set_err_rsp(work);
7541 return 0;
7542}
7543
7544/**
7545 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command
7546 * @work: smb work containing oplock break command buffer
7547 *
7548 * Return: 0
7549 */
7550static void smb20_oplock_break_ack(struct ksmbd_work *work)
7551{
Namjae Jeone5066492021-03-30 12:35:23 +09007552 struct smb2_oplock_break *req = work->request_buf;
7553 struct smb2_oplock_break *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007554 struct ksmbd_file *fp;
7555 struct oplock_info *opinfo = NULL;
7556 __le32 err = 0;
7557 int ret = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007558 u64 volatile_id, persistent_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09007559 char req_oplevel = 0, rsp_oplevel = 0;
7560 unsigned int oplock_change_type;
7561
7562 volatile_id = le64_to_cpu(req->VolatileFid);
7563 persistent_id = le64_to_cpu(req->PersistentFid);
7564 req_oplevel = req->OplockLevel;
7565 ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n",
7566 volatile_id, persistent_id, req_oplevel);
7567
7568 fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
7569 if (!fp) {
7570 rsp->hdr.Status = STATUS_FILE_CLOSED;
7571 smb2_set_err_rsp(work);
7572 return;
7573 }
7574
7575 opinfo = opinfo_get(fp);
7576 if (!opinfo) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007577 pr_err("unexpected null oplock_info\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007578 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7579 smb2_set_err_rsp(work);
7580 ksmbd_fd_put(work, fp);
7581 return;
7582 }
7583
7584 if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) {
7585 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7586 goto err_out;
7587 }
7588
7589 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7590 ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state);
7591 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7592 goto err_out;
7593 }
7594
Namjae Jeon64b39f42021-03-30 14:25:35 +09007595 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7596 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7597 (req_oplevel != SMB2_OPLOCK_LEVEL_II &&
7598 req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007599 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7600 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007601 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7602 req_oplevel != SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007603 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7604 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007605 } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II ||
7606 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007607 err = STATUS_INVALID_DEVICE_STATE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007608 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7609 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7610 req_oplevel == SMB2_OPLOCK_LEVEL_II) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007611 oplock_change_type = OPLOCK_WRITE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007612 } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7613 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7614 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007615 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007616 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7617 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007618 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007619 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007620 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007621 }
7622 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007623 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007624 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007625
7626 switch (oplock_change_type) {
7627 case OPLOCK_WRITE_TO_READ:
7628 ret = opinfo_write_to_read(opinfo);
7629 rsp_oplevel = SMB2_OPLOCK_LEVEL_II;
7630 break;
7631 case OPLOCK_WRITE_TO_NONE:
7632 ret = opinfo_write_to_none(opinfo);
7633 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7634 break;
7635 case OPLOCK_READ_TO_NONE:
7636 ret = opinfo_read_to_none(opinfo);
7637 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7638 break;
7639 default:
Namjae Jeonbde16942021-06-28 15:23:19 +09007640 pr_err("unknown oplock change 0x%x -> 0x%x\n",
7641 opinfo->level, rsp_oplevel);
Namjae Jeone2f34482021-03-16 10:49:09 +09007642 }
7643
7644 if (ret < 0) {
7645 rsp->hdr.Status = err;
7646 goto err_out;
7647 }
7648
7649 opinfo_put(opinfo);
7650 ksmbd_fd_put(work, fp);
7651 opinfo->op_state = OPLOCK_STATE_NONE;
7652 wake_up_interruptible_all(&opinfo->oplock_q);
7653
7654 rsp->StructureSize = cpu_to_le16(24);
7655 rsp->OplockLevel = rsp_oplevel;
7656 rsp->Reserved = 0;
7657 rsp->Reserved2 = 0;
7658 rsp->VolatileFid = cpu_to_le64(volatile_id);
7659 rsp->PersistentFid = cpu_to_le64(persistent_id);
7660 inc_rfc1001_len(rsp, 24);
7661 return;
7662
7663err_out:
7664 opinfo->op_state = OPLOCK_STATE_NONE;
7665 wake_up_interruptible_all(&opinfo->oplock_q);
7666
7667 opinfo_put(opinfo);
7668 ksmbd_fd_put(work, fp);
7669 smb2_set_err_rsp(work);
7670}
7671
7672static int check_lease_state(struct lease *lease, __le32 req_state)
7673{
7674 if ((lease->new_state ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09007675 (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) &&
7676 !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007677 lease->new_state = req_state;
7678 return 0;
7679 }
7680
7681 if (lease->new_state == req_state)
7682 return 0;
7683
7684 return 1;
7685}
7686
7687/**
7688 * smb21_lease_break_ack() - handler for smb2.1 lease break command
7689 * @work: smb work containing lease break command buffer
7690 *
7691 * Return: 0
7692 */
7693static void smb21_lease_break_ack(struct ksmbd_work *work)
7694{
7695 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09007696 struct smb2_lease_ack *req = work->request_buf;
7697 struct smb2_lease_ack *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007698 struct oplock_info *opinfo;
7699 __le32 err = 0;
7700 int ret = 0;
7701 unsigned int lease_change_type;
7702 __le32 lease_state;
7703 struct lease *lease;
7704
7705 ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007706 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007707 opinfo = lookup_lease_in_table(conn, req->LeaseKey);
7708 if (!opinfo) {
7709 ksmbd_debug(OPLOCK, "file not opened\n");
7710 smb2_set_err_rsp(work);
7711 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7712 return;
7713 }
7714 lease = opinfo->o_lease;
7715
7716 if (opinfo->op_state == OPLOCK_STATE_NONE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007717 pr_err("unexpected lease break state 0x%x\n",
7718 opinfo->op_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09007719 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7720 goto err_out;
7721 }
7722
7723 if (check_lease_state(lease, req->LeaseState)) {
7724 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
7725 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +09007726 "req lease state: 0x%x, expected state: 0x%x\n",
7727 req->LeaseState, lease->new_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09007728 goto err_out;
7729 }
7730
7731 if (!atomic_read(&opinfo->breaking_cnt)) {
7732 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7733 goto err_out;
7734 }
7735
7736 /* check for bad lease state */
Namjae Jeon070fb212021-05-26 17:57:12 +09007737 if (req->LeaseState &
7738 (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007739 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7740 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7741 lease_change_type = OPLOCK_WRITE_TO_NONE;
7742 else
7743 lease_change_type = OPLOCK_READ_TO_NONE;
7744 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007745 le32_to_cpu(lease->state),
7746 le32_to_cpu(req->LeaseState));
Namjae Jeon64b39f42021-03-30 14:25:35 +09007747 } else if (lease->state == SMB2_LEASE_READ_CACHING_LE &&
7748 req->LeaseState != SMB2_LEASE_NONE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007749 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7750 lease_change_type = OPLOCK_READ_TO_NONE;
7751 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007752 le32_to_cpu(lease->state),
7753 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007754 } else {
7755 /* valid lease state changes */
7756 err = STATUS_INVALID_DEVICE_STATE;
7757 if (req->LeaseState == SMB2_LEASE_NONE_LE) {
7758 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7759 lease_change_type = OPLOCK_WRITE_TO_NONE;
7760 else
7761 lease_change_type = OPLOCK_READ_TO_NONE;
7762 } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) {
7763 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7764 lease_change_type = OPLOCK_WRITE_TO_READ;
7765 else
7766 lease_change_type = OPLOCK_READ_HANDLE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007767 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007768 lease_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007769 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007770 }
7771
7772 switch (lease_change_type) {
7773 case OPLOCK_WRITE_TO_READ:
7774 ret = opinfo_write_to_read(opinfo);
7775 break;
7776 case OPLOCK_READ_HANDLE_TO_READ:
7777 ret = opinfo_read_handle_to_read(opinfo);
7778 break;
7779 case OPLOCK_WRITE_TO_NONE:
7780 ret = opinfo_write_to_none(opinfo);
7781 break;
7782 case OPLOCK_READ_TO_NONE:
7783 ret = opinfo_read_to_none(opinfo);
7784 break;
7785 default:
7786 ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007787 le32_to_cpu(lease->state),
7788 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007789 }
7790
7791 lease_state = lease->state;
7792 opinfo->op_state = OPLOCK_STATE_NONE;
7793 wake_up_interruptible_all(&opinfo->oplock_q);
7794 atomic_dec(&opinfo->breaking_cnt);
7795 wake_up_interruptible_all(&opinfo->oplock_brk);
7796 opinfo_put(opinfo);
7797
7798 if (ret < 0) {
7799 rsp->hdr.Status = err;
7800 goto err_out;
7801 }
7802
7803 rsp->StructureSize = cpu_to_le16(36);
7804 rsp->Reserved = 0;
7805 rsp->Flags = 0;
7806 memcpy(rsp->LeaseKey, req->LeaseKey, 16);
7807 rsp->LeaseState = lease_state;
7808 rsp->LeaseDuration = 0;
7809 inc_rfc1001_len(rsp, 36);
7810 return;
7811
7812err_out:
7813 opinfo->op_state = OPLOCK_STATE_NONE;
7814 wake_up_interruptible_all(&opinfo->oplock_q);
7815 atomic_dec(&opinfo->breaking_cnt);
7816 wake_up_interruptible_all(&opinfo->oplock_brk);
7817
7818 opinfo_put(opinfo);
7819 smb2_set_err_rsp(work);
7820}
7821
7822/**
7823 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break
7824 * @work: smb work containing oplock/lease break command buffer
7825 *
7826 * Return: 0
7827 */
7828int smb2_oplock_break(struct ksmbd_work *work)
7829{
Namjae Jeone5066492021-03-30 12:35:23 +09007830 struct smb2_oplock_break *req = work->request_buf;
7831 struct smb2_oplock_break *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007832
7833 switch (le16_to_cpu(req->StructureSize)) {
7834 case OP_BREAK_STRUCT_SIZE_20:
7835 smb20_oplock_break_ack(work);
7836 break;
7837 case OP_BREAK_STRUCT_SIZE_21:
7838 smb21_lease_break_ack(work);
7839 break;
7840 default:
7841 ksmbd_debug(OPLOCK, "invalid break cmd %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007842 le16_to_cpu(req->StructureSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09007843 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7844 smb2_set_err_rsp(work);
7845 }
7846
7847 return 0;
7848}
7849
7850/**
7851 * smb2_notify() - handler for smb2 notify request
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09007852 * @work: smb work containing notify command buffer
Namjae Jeone2f34482021-03-16 10:49:09 +09007853 *
7854 * Return: 0
7855 */
7856int smb2_notify(struct ksmbd_work *work)
7857{
7858 struct smb2_notify_req *req;
7859 struct smb2_notify_rsp *rsp;
7860
7861 WORK_BUFFERS(work, req, rsp);
7862
7863 if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) {
7864 rsp->hdr.Status = STATUS_INTERNAL_ERROR;
7865 smb2_set_err_rsp(work);
7866 return 0;
7867 }
7868
7869 smb2_set_err_rsp(work);
7870 rsp->hdr.Status = STATUS_NOT_IMPLEMENTED;
7871 return 0;
7872}
7873
7874/**
7875 * smb2_is_sign_req() - handler for checking packet signing status
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09007876 * @work: smb work containing notify command buffer
7877 * @command: SMB2 command id
Namjae Jeone2f34482021-03-16 10:49:09 +09007878 *
7879 * Return: true if packed is signed, false otherwise
7880 */
7881bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command)
7882{
Namjae Jeone5066492021-03-30 12:35:23 +09007883 struct smb2_hdr *rcv_hdr2 = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007884
7885 if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007886 command != SMB2_NEGOTIATE_HE &&
7887 command != SMB2_SESSION_SETUP_HE &&
7888 command != SMB2_OPLOCK_BREAK_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +09007889 return true;
7890
kernel test robot56160152021-05-12 09:24:37 +09007891 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +09007892}
7893
7894/**
7895 * smb2_check_sign_req() - handler for req packet sign processing
7896 * @work: smb work containing notify command buffer
7897 *
7898 * Return: 1 on success, 0 otherwise
7899 */
7900int smb2_check_sign_req(struct ksmbd_work *work)
7901{
7902 struct smb2_hdr *hdr, *hdr_org;
7903 char signature_req[SMB2_SIGNATURE_SIZE];
7904 char signature[SMB2_HMACSHA256_SIZE];
7905 struct kvec iov[1];
7906 size_t len;
7907
Namjae Jeone5066492021-03-30 12:35:23 +09007908 hdr_org = hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007909 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09007910 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09007911
7912 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
7913 len = be32_to_cpu(hdr_org->smb2_buf_length);
7914 else if (hdr->NextCommand)
7915 len = le32_to_cpu(hdr->NextCommand);
7916 else
7917 len = be32_to_cpu(hdr_org->smb2_buf_length) -
7918 work->next_smb2_rcv_hdr_off;
7919
7920 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
7921 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7922
7923 iov[0].iov_base = (char *)&hdr->ProtocolId;
7924 iov[0].iov_len = len;
7925
7926 if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1,
Namjae Jeon64b39f42021-03-30 14:25:35 +09007927 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09007928 return 0;
7929
7930 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007931 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007932 return 0;
7933 }
7934
7935 return 1;
7936}
7937
7938/**
7939 * smb2_set_sign_rsp() - handler for rsp packet sign processing
7940 * @work: smb work containing notify command buffer
7941 *
7942 */
7943void smb2_set_sign_rsp(struct ksmbd_work *work)
7944{
7945 struct smb2_hdr *hdr, *hdr_org;
7946 struct smb2_hdr *req_hdr;
7947 char signature[SMB2_HMACSHA256_SIZE];
7948 struct kvec iov[2];
7949 size_t len;
7950 int n_vec = 1;
7951
Namjae Jeone5066492021-03-30 12:35:23 +09007952 hdr_org = hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007953 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09007954 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09007955
Namjae Jeon8a893312021-06-25 13:43:37 +09007956 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09007957
7958 if (!work->next_smb2_rsp_hdr_off) {
7959 len = get_rfc1002_len(hdr_org);
7960 if (req_hdr->NextCommand)
7961 len = ALIGN(len, 8);
7962 } else {
7963 len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off;
7964 len = ALIGN(len, 8);
7965 }
7966
7967 if (req_hdr->NextCommand)
7968 hdr->NextCommand = cpu_to_le32(len);
7969
7970 hdr->Flags |= SMB2_FLAGS_SIGNED;
7971 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7972
7973 iov[0].iov_base = (char *)&hdr->ProtocolId;
7974 iov[0].iov_len = len;
7975
Namjae Jeone5066492021-03-30 12:35:23 +09007976 if (work->aux_payload_sz) {
7977 iov[0].iov_len -= work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09007978
Namjae Jeone5066492021-03-30 12:35:23 +09007979 iov[1].iov_base = work->aux_payload_buf;
7980 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09007981 n_vec++;
7982 }
7983
7984 if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec,
Namjae Jeon64b39f42021-03-30 14:25:35 +09007985 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09007986 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
7987}
7988
7989/**
7990 * smb3_check_sign_req() - handler for req packet sign processing
7991 * @work: smb work containing notify command buffer
7992 *
7993 * Return: 1 on success, 0 otherwise
7994 */
7995int smb3_check_sign_req(struct ksmbd_work *work)
7996{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09007997 struct ksmbd_conn *conn = work->conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09007998 char *signing_key;
7999 struct smb2_hdr *hdr, *hdr_org;
8000 struct channel *chann;
8001 char signature_req[SMB2_SIGNATURE_SIZE];
8002 char signature[SMB2_CMACAES_SIZE];
8003 struct kvec iov[1];
8004 size_t len;
8005
Namjae Jeone5066492021-03-30 12:35:23 +09008006 hdr_org = hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008007 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008008 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008009
8010 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
8011 len = be32_to_cpu(hdr_org->smb2_buf_length);
8012 else if (hdr->NextCommand)
8013 len = le32_to_cpu(hdr->NextCommand);
8014 else
8015 len = be32_to_cpu(hdr_org->smb2_buf_length) -
8016 work->next_smb2_rcv_hdr_off;
8017
8018 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
8019 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008020 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008021 chann = lookup_chann_list(work->sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008022 if (!chann)
8023 return 0;
8024 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008025 }
8026
8027 if (!signing_key) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008028 pr_err("SMB3 signing key is not generated\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008029 return 0;
8030 }
8031
8032 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8033 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8034 iov[0].iov_base = (char *)&hdr->ProtocolId;
8035 iov[0].iov_len = len;
8036
8037 if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature))
8038 return 0;
8039
8040 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008041 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008042 return 0;
8043 }
8044
8045 return 1;
8046}
8047
8048/**
8049 * smb3_set_sign_rsp() - handler for rsp packet sign processing
8050 * @work: smb work containing notify command buffer
8051 *
8052 */
8053void smb3_set_sign_rsp(struct ksmbd_work *work)
8054{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008055 struct ksmbd_conn *conn = work->conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09008056 struct smb2_hdr *req_hdr;
8057 struct smb2_hdr *hdr, *hdr_org;
8058 struct channel *chann;
8059 char signature[SMB2_CMACAES_SIZE];
8060 struct kvec iov[2];
8061 int n_vec = 1;
8062 size_t len;
8063 char *signing_key;
8064
Namjae Jeone5066492021-03-30 12:35:23 +09008065 hdr_org = hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008066 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008067 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008068
Namjae Jeon8a893312021-06-25 13:43:37 +09008069 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008070
8071 if (!work->next_smb2_rsp_hdr_off) {
8072 len = get_rfc1002_len(hdr_org);
8073 if (req_hdr->NextCommand)
8074 len = ALIGN(len, 8);
8075 } else {
8076 len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off;
8077 len = ALIGN(len, 8);
8078 }
8079
8080 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
8081 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008082 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008083 chann = lookup_chann_list(work->sess, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008084 if (!chann)
8085 return;
8086 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008087 }
8088
8089 if (!signing_key)
8090 return;
8091
8092 if (req_hdr->NextCommand)
8093 hdr->NextCommand = cpu_to_le32(len);
8094
8095 hdr->Flags |= SMB2_FLAGS_SIGNED;
8096 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8097 iov[0].iov_base = (char *)&hdr->ProtocolId;
8098 iov[0].iov_len = len;
Namjae Jeone5066492021-03-30 12:35:23 +09008099 if (work->aux_payload_sz) {
8100 iov[0].iov_len -= work->aux_payload_sz;
8101 iov[1].iov_base = work->aux_payload_buf;
8102 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008103 n_vec++;
8104 }
8105
8106 if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature))
8107 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8108}
8109
8110/**
8111 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response
8112 * @work: smb work containing response buffer
8113 *
8114 */
8115void smb3_preauth_hash_rsp(struct ksmbd_work *work)
8116{
8117 struct ksmbd_conn *conn = work->conn;
8118 struct ksmbd_session *sess = work->sess;
8119 struct smb2_hdr *req, *rsp;
8120
8121 if (conn->dialect != SMB311_PROT_ID)
8122 return;
8123
8124 WORK_BUFFERS(work, req, rsp);
8125
8126 if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE)
8127 ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09008128 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09008129
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008130 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008131 __u8 *hash_value;
8132
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008133 if (conn->binding) {
8134 struct preauth_session *preauth_sess;
8135
8136 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
8137 if (!preauth_sess)
8138 return;
8139 hash_value = preauth_sess->Preauth_HashValue;
8140 } else {
8141 hash_value = sess->Preauth_HashValue;
8142 if (!hash_value)
8143 return;
8144 }
Namjae Jeone2f34482021-03-16 10:49:09 +09008145 ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09008146 hash_value);
Namjae Jeone2f34482021-03-16 10:49:09 +09008147 }
8148}
8149
Namjae Jeon64b39f42021-03-30 14:25:35 +09008150static void fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, char *old_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008151 __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09008152{
8153 struct smb2_hdr *hdr = (struct smb2_hdr *)old_buf;
8154 unsigned int orig_len = get_rfc1002_len(old_buf);
8155
8156 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
8157 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
8158 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
8159 tr_hdr->Flags = cpu_to_le16(0x01);
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008160 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
8161 cipher_type == SMB2_ENCRYPTION_AES256_GCM)
8162 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008163 else
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008164 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008165 memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8);
8166 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
8167 inc_rfc1001_len(tr_hdr, orig_len);
8168}
8169
8170int smb3_encrypt_resp(struct ksmbd_work *work)
8171{
Namjae Jeone5066492021-03-30 12:35:23 +09008172 char *buf = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008173 struct smb2_transform_hdr *tr_hdr;
8174 struct kvec iov[3];
8175 int rc = -ENOMEM;
Namjae Jeone5066492021-03-30 12:35:23 +09008176 int buf_size = 0, rq_nvec = 2 + (work->aux_payload_sz ? 1 : 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09008177
8178 if (ARRAY_SIZE(iov) < rq_nvec)
8179 return -ENOMEM;
8180
Namjae Jeon20ea7fd2021-03-30 12:40:47 +09008181 tr_hdr = kzalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09008182 if (!tr_hdr)
8183 return rc;
8184
8185 /* fill transform header */
8186 fill_transform_hdr(tr_hdr, buf, work->conn->cipher_type);
8187
8188 iov[0].iov_base = tr_hdr;
8189 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8190 buf_size += iov[0].iov_len - 4;
8191
8192 iov[1].iov_base = buf + 4;
8193 iov[1].iov_len = get_rfc1002_len(buf);
Namjae Jeone5066492021-03-30 12:35:23 +09008194 if (work->aux_payload_sz) {
8195 iov[1].iov_len = work->resp_hdr_sz - 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008196
Namjae Jeone5066492021-03-30 12:35:23 +09008197 iov[2].iov_base = work->aux_payload_buf;
8198 iov[2].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008199 buf_size += iov[2].iov_len;
8200 }
8201 buf_size += iov[1].iov_len;
8202 work->resp_hdr_sz = iov[1].iov_len;
8203
8204 rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1);
8205 if (rc)
8206 return rc;
8207
8208 memmove(buf, iov[1].iov_base, iov[1].iov_len);
8209 tr_hdr->smb2_buf_length = cpu_to_be32(buf_size);
8210 work->tr_buf = tr_hdr;
8211
8212 return rc;
8213}
8214
8215int smb3_is_transform_hdr(void *buf)
8216{
8217 struct smb2_transform_hdr *trhdr = buf;
8218
8219 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
8220}
8221
8222int smb3_decrypt_req(struct ksmbd_work *work)
8223{
8224 struct ksmbd_conn *conn = work->conn;
8225 struct ksmbd_session *sess;
Namjae Jeone5066492021-03-30 12:35:23 +09008226 char *buf = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008227 struct smb2_hdr *hdr;
8228 unsigned int pdu_length = get_rfc1002_len(buf);
8229 struct kvec iov[2];
8230 unsigned int buf_data_size = pdu_length + 4 -
8231 sizeof(struct smb2_transform_hdr);
8232 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
8233 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
8234 int rc = 0;
8235
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008236 sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09008237 if (!sess) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008238 pr_err("invalid session id(%llx) in transform header\n",
8239 le64_to_cpu(tr_hdr->SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09008240 return -ECONNABORTED;
8241 }
8242
Namjae Jeon070fb212021-05-26 17:57:12 +09008243 if (pdu_length + 4 <
8244 sizeof(struct smb2_transform_hdr) + sizeof(struct smb2_hdr)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008245 pr_err("Transform message is too small (%u)\n",
8246 pdu_length);
Namjae Jeone2f34482021-03-16 10:49:09 +09008247 return -ECONNABORTED;
8248 }
8249
8250 if (pdu_length + 4 < orig_len + sizeof(struct smb2_transform_hdr)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008251 pr_err("Transform message is broken\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008252 return -ECONNABORTED;
8253 }
8254
8255 iov[0].iov_base = buf;
8256 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8257 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
8258 iov[1].iov_len = buf_data_size;
8259 rc = ksmbd_crypt_message(conn, iov, 2, 0);
8260 if (rc)
8261 return rc;
8262
8263 memmove(buf + 4, iov[1].iov_base, buf_data_size);
8264 hdr = (struct smb2_hdr *)buf;
8265 hdr->smb2_buf_length = cpu_to_be32(buf_data_size);
8266
8267 return rc;
8268}
8269
8270bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work)
8271{
8272 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09008273 struct smb2_hdr *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008274
8275 if (conn->dialect < SMB30_PROT_ID)
8276 return false;
8277
8278 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008279 rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008280
8281 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008282 rsp->Status == STATUS_SUCCESS)
Namjae Jeone2f34482021-03-16 10:49:09 +09008283 return true;
8284 return false;
8285}