blob: f3c3ff3eab2e09800a5de80594b653c1ae151364 [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"
Namjae Jeone2f34482021-03-16 10:49:09 +090016#include "smbfsctl.h"
17#include "oplock.h"
18#include "smbacl.h"
19
20#include "auth.h"
21#include "asn1.h"
Namjae Jeone2f34482021-03-16 10:49:09 +090022#include "connection.h"
23#include "transport_ipc.h"
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +090024#include "transport_rdma.h"
Namjae Jeone2f34482021-03-16 10:49:09 +090025#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 Jeoncb451722021-11-03 08:08:44 +090046 *req = smb2_get_msg(work->request_buf);
47 *rsp = smb2_get_msg(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 Jeonf4228b62021-08-12 10:16:40 +090060static inline bool 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)
Namjae Jeonf4228b62021-08-12 10:16:40 +090065 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +090066
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)
Namjae Jeonf4228b62021-08-12 10:16:40 +090069 return true;
Namjae Jeonbde16942021-06-28 15:23:19 +090070 pr_err("Invalid user session id: %llu\n", id);
Namjae Jeonf4228b62021-08-12 10:16:40 +090071 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +090072}
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/**
Namjae Jeon5ec3df8e2021-08-12 10:17:39 +090087 * smb2_get_ksmbd_tcon() - get tree connection information using a tree id.
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +090088 * @work: smb work
Namjae Jeone2f34482021-03-16 10:49:09 +090089 *
Namjae Jeon5ec3df8e2021-08-12 10:17:39 +090090 * Return: 0 if there is a tree connection matched or these are
91 * skipable commands, otherwise error
Namjae Jeone2f34482021-03-16 10:49:09 +090092 */
93int smb2_get_ksmbd_tcon(struct ksmbd_work *work)
94{
Namjae Jeoncb451722021-11-03 08:08:44 +090095 struct smb2_hdr *req_hdr = smb2_get_msg(work->request_buf);
Ralph Boehme341b1602021-10-05 07:03:40 +020096 unsigned int cmd = le16_to_cpu(req_hdr->Command);
Namjae Jeone2f34482021-03-16 10:49:09 +090097 int tree_id;
98
99 work->tcon = NULL;
Ralph Boehme341b1602021-10-05 07:03:40 +0200100 if (cmd == SMB2_TREE_CONNECT_HE ||
101 cmd == SMB2_CANCEL_HE ||
102 cmd == SMB2_LOGOFF_HE) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900103 ksmbd_debug(SMB, "skip to check tree connect request\n");
104 return 0;
105 }
106
Namjae Jeon02b68b22021-04-01 17:45:33 +0900107 if (xa_empty(&work->sess->tree_conns)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900108 ksmbd_debug(SMB, "NO tree connected\n");
Namjae Jeonc6ce2b52021-08-12 10:18:18 +0900109 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +0900110 }
111
112 tree_id = le32_to_cpu(req_hdr->Id.SyncId.TreeId);
113 work->tcon = ksmbd_tree_conn_lookup(work->sess, tree_id);
114 if (!work->tcon) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900115 pr_err("Invalid tid %d\n", tree_id);
Namjae Jeonc6ce2b52021-08-12 10:18:18 +0900116 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +0900117 }
118
119 return 1;
120}
121
122/**
123 * smb2_set_err_rsp() - set error response code on smb response
124 * @work: smb work containing response buffer
125 */
126void smb2_set_err_rsp(struct ksmbd_work *work)
127{
128 struct smb2_err_rsp *err_rsp;
129
130 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +0900131 err_rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900132 else
Namjae Jeoncb451722021-11-03 08:08:44 +0900133 err_rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900134
135 if (err_rsp->hdr.Status != STATUS_STOPPED_ON_SYMLINK) {
136 err_rsp->StructureSize = SMB2_ERROR_STRUCTURE_SIZE2_LE;
137 err_rsp->ErrorContextCount = 0;
138 err_rsp->Reserved = 0;
139 err_rsp->ByteCount = 0;
140 err_rsp->ErrorData[0] = 0;
Namjae Jeone5066492021-03-30 12:35:23 +0900141 inc_rfc1001_len(work->response_buf, SMB2_ERROR_STRUCTURE_SIZE2);
Namjae Jeone2f34482021-03-16 10:49:09 +0900142 }
143}
144
145/**
146 * is_smb2_neg_cmd() - is it smb2 negotiation command
147 * @work: smb work containing smb header
148 *
Namjae Jeonf4228b62021-08-12 10:16:40 +0900149 * Return: true if smb2 negotiation command, otherwise false
Namjae Jeone2f34482021-03-16 10:49:09 +0900150 */
Namjae Jeonf4228b62021-08-12 10:16:40 +0900151bool is_smb2_neg_cmd(struct ksmbd_work *work)
Namjae Jeone2f34482021-03-16 10:49:09 +0900152{
Namjae Jeoncb451722021-11-03 08:08:44 +0900153 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900154
155 /* is it SMB2 header ? */
156 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
Namjae Jeonf4228b62021-08-12 10:16:40 +0900157 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900158
159 /* make sure it is request not response message */
160 if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
Namjae Jeonf4228b62021-08-12 10:16:40 +0900161 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900162
163 if (hdr->Command != SMB2_NEGOTIATE)
Namjae Jeonf4228b62021-08-12 10:16:40 +0900164 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900165
Namjae Jeonf4228b62021-08-12 10:16:40 +0900166 return true;
Namjae Jeone2f34482021-03-16 10:49:09 +0900167}
168
169/**
170 * is_smb2_rsp() - is it smb2 response
171 * @work: smb work containing smb response buffer
172 *
Namjae Jeonf4228b62021-08-12 10:16:40 +0900173 * Return: true if smb2 response, otherwise false
Namjae Jeone2f34482021-03-16 10:49:09 +0900174 */
Namjae Jeonf4228b62021-08-12 10:16:40 +0900175bool is_smb2_rsp(struct ksmbd_work *work)
Namjae Jeone2f34482021-03-16 10:49:09 +0900176{
Namjae Jeoncb451722021-11-03 08:08:44 +0900177 struct smb2_hdr *hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900178
179 /* is it SMB2 header ? */
180 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
Namjae Jeonf4228b62021-08-12 10:16:40 +0900181 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900182
183 /* make sure it is response not request message */
184 if (!(hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR))
Namjae Jeonf4228b62021-08-12 10:16:40 +0900185 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900186
Namjae Jeonf4228b62021-08-12 10:16:40 +0900187 return true;
Namjae Jeone2f34482021-03-16 10:49:09 +0900188}
189
190/**
191 * get_smb2_cmd_val() - get smb command code from smb header
192 * @work: smb work containing smb request buffer
193 *
194 * Return: smb2 request command value
195 */
Namjae Jeonfc2d1b52021-05-26 18:01:08 +0900196u16 get_smb2_cmd_val(struct ksmbd_work *work)
Namjae Jeone2f34482021-03-16 10:49:09 +0900197{
198 struct smb2_hdr *rcv_hdr;
199
200 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +0900201 rcv_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900202 else
Namjae Jeoncb451722021-11-03 08:08:44 +0900203 rcv_hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900204 return le16_to_cpu(rcv_hdr->Command);
205}
206
207/**
208 * set_smb2_rsp_status() - set error response code on smb2 header
209 * @work: smb work containing response buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900210 * @err: error response code
Namjae Jeone2f34482021-03-16 10:49:09 +0900211 */
212void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err)
213{
214 struct smb2_hdr *rsp_hdr;
215
216 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +0900217 rsp_hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900218 else
Namjae Jeoncb451722021-11-03 08:08:44 +0900219 rsp_hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900220 rsp_hdr->Status = err;
221 smb2_set_err_rsp(work);
222}
223
224/**
225 * init_smb2_neg_rsp() - initialize smb2 response for negotiate command
226 * @work: smb work containing smb request buffer
227 *
228 * smb2 negotiate response is sent in reply of smb1 negotiate command for
229 * dialect auto-negotiation.
230 */
231int init_smb2_neg_rsp(struct ksmbd_work *work)
232{
233 struct smb2_hdr *rsp_hdr;
234 struct smb2_negotiate_rsp *rsp;
235 struct ksmbd_conn *conn = work->conn;
236
237 if (conn->need_neg == false)
238 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +0900239
Namjae Jeoncb451722021-11-03 08:08:44 +0900240 *(__be32 *)work->response_buf =
241 cpu_to_be32(conn->vals->header_size);
Namjae Jeone2f34482021-03-16 10:49:09 +0900242
Namjae Jeoncb451722021-11-03 08:08:44 +0900243 rsp_hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900244 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
Namjae Jeone2f34482021-03-16 10:49:09 +0900245 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
246 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
247 rsp_hdr->CreditRequest = cpu_to_le16(2);
248 rsp_hdr->Command = SMB2_NEGOTIATE;
249 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
250 rsp_hdr->NextCommand = 0;
251 rsp_hdr->MessageId = 0;
252 rsp_hdr->Id.SyncId.ProcessId = 0;
253 rsp_hdr->Id.SyncId.TreeId = 0;
254 rsp_hdr->SessionId = 0;
255 memset(rsp_hdr->Signature, 0, 16);
256
Namjae Jeoncb451722021-11-03 08:08:44 +0900257 rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900258
259 WARN_ON(ksmbd_conn_good(work));
260
261 rsp->StructureSize = cpu_to_le16(65);
262 ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect);
263 rsp->DialectRevision = cpu_to_le16(conn->dialect);
264 /* Not setting conn guid rsp->ServerGUID, as it
265 * not used by client for identifying connection
266 */
267 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
268 /* Default Max Message Size till SMB2.0, 64K*/
269 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
270 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
271 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
272
273 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
274 rsp->ServerStartTime = 0;
275
276 rsp->SecurityBufferOffset = cpu_to_le16(128);
277 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
Namjae Jeoncb451722021-11-03 08:08:44 +0900278 ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) +
Namjae Jeone2f34482021-03-16 10:49:09 +0900279 le16_to_cpu(rsp->SecurityBufferOffset));
Namjae Jeoncb451722021-11-03 08:08:44 +0900280 inc_rfc1001_len(work->response_buf,
281 sizeof(struct smb2_negotiate_rsp) -
282 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
283 AUTH_GSS_LENGTH);
Namjae Jeone2f34482021-03-16 10:49:09 +0900284 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
285 if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY)
286 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
287 conn->use_spnego = true;
288
289 ksmbd_conn_set_need_negotiate(work);
290 return 0;
291}
292
Namjae Jeone2f34482021-03-16 10:49:09 +0900293/**
294 * smb2_set_rsp_credits() - set number of credits in response buffer
295 * @work: smb work containing smb response buffer
296 */
297int smb2_set_rsp_credits(struct ksmbd_work *work)
298{
Namjae Jeon8a893312021-06-25 13:43:37 +0900299 struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work);
300 struct smb2_hdr *hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900301 struct ksmbd_conn *conn = work->conn;
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900302 unsigned short credits_requested;
303 unsigned short credit_charge, credits_granted = 0;
304 unsigned short aux_max, aux_credits;
Namjae Jeone2f34482021-03-16 10:49:09 +0900305
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900306 if (work->send_no_response)
307 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +0900308
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900309 hdr->CreditCharge = req_hdr->CreditCharge;
Namjae Jeone2f34482021-03-16 10:49:09 +0900310
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900311 if (conn->total_credits > conn->max_credits) {
312 hdr->CreditRequest = 0;
Namjae Jeonbde16942021-06-28 15:23:19 +0900313 pr_err("Total credits overflow: %d\n", conn->total_credits);
Namjae Jeone2f34482021-03-16 10:49:09 +0900314 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +0900315 }
316
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900317 credit_charge = max_t(unsigned short,
318 le16_to_cpu(req_hdr->CreditCharge), 1);
319 credits_requested = max_t(unsigned short,
320 le16_to_cpu(req_hdr->CreditRequest), 1);
321
322 /* according to smb2.credits smbtorture, Windows server
323 * 2016 or later grant up to 8192 credits at once.
324 *
325 * TODO: Need to adjuct CreditRequest value according to
326 * current cpu load
327 */
328 aux_credits = credits_requested - 1;
329 if (hdr->Command == SMB2_NEGOTIATE)
330 aux_max = 0;
331 else
332 aux_max = conn->max_credits - credit_charge;
333 aux_credits = min_t(unsigned short, aux_credits, aux_max);
334 credits_granted = credit_charge + aux_credits;
335
336 if (conn->max_credits - conn->total_credits < credits_granted)
337 credits_granted = conn->max_credits -
338 conn->total_credits;
339
Namjae Jeone2f34482021-03-16 10:49:09 +0900340 conn->total_credits += credits_granted;
341 work->credits_granted += credits_granted;
342
343 if (!req_hdr->NextCommand) {
344 /* Update CreditRequest in last request */
345 hdr->CreditRequest = cpu_to_le16(work->credits_granted);
346 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900347 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900348 "credits: requested[%d] granted[%d] total_granted[%d]\n",
349 credits_requested, credits_granted,
350 conn->total_credits);
Namjae Jeone2f34482021-03-16 10:49:09 +0900351 return 0;
352}
353
354/**
355 * init_chained_smb2_rsp() - initialize smb2 chained response
356 * @work: smb work containing smb response buffer
357 */
358static void init_chained_smb2_rsp(struct ksmbd_work *work)
359{
Namjae Jeon8a893312021-06-25 13:43:37 +0900360 struct smb2_hdr *req = ksmbd_req_buf_next(work);
361 struct smb2_hdr *rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900362 struct smb2_hdr *rsp_hdr;
363 struct smb2_hdr *rcv_hdr;
364 int next_hdr_offset = 0;
365 int len, new_len;
366
367 /* Len of this response = updated RFC len - offset of previous cmd
368 * in the compound rsp
369 */
370
371 /* Storing the current local FID which may be needed by subsequent
372 * command in the compound request
373 */
374 if (req->Command == SMB2_CREATE && rsp->Status == STATUS_SUCCESS) {
375 work->compound_fid =
376 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
377 VolatileFileId);
378 work->compound_pfid =
379 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
380 PersistentFileId);
381 work->compound_sid = le64_to_cpu(rsp->SessionId);
382 }
383
Namjae Jeone5066492021-03-30 12:35:23 +0900384 len = get_rfc1002_len(work->response_buf) - work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +0900385 next_hdr_offset = le32_to_cpu(req->NextCommand);
386
387 new_len = ALIGN(len, 8);
Namjae Jeoncb451722021-11-03 08:08:44 +0900388 inc_rfc1001_len(work->response_buf,
389 sizeof(struct smb2_hdr) + new_len - len);
Namjae Jeone2f34482021-03-16 10:49:09 +0900390 rsp->NextCommand = cpu_to_le32(new_len);
391
392 work->next_smb2_rcv_hdr_off += next_hdr_offset;
393 work->next_smb2_rsp_hdr_off += new_len;
394 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900395 "Compound req new_len = %d rcv off = %d rsp off = %d\n",
396 new_len, work->next_smb2_rcv_hdr_off,
397 work->next_smb2_rsp_hdr_off);
Namjae Jeone2f34482021-03-16 10:49:09 +0900398
Namjae Jeon8a893312021-06-25 13:43:37 +0900399 rsp_hdr = ksmbd_resp_buf_next(work);
400 rcv_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900401
402 if (!(rcv_hdr->Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
403 ksmbd_debug(SMB, "related flag should be set\n");
404 work->compound_fid = KSMBD_NO_FID;
405 work->compound_pfid = KSMBD_NO_FID;
406 }
Namjae Jeoncb451722021-11-03 08:08:44 +0900407 memset((char *)rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
Namjae Jeon18a015b2021-09-22 21:00:57 +0900408 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
Namjae Jeone2f34482021-03-16 10:49:09 +0900409 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
410 rsp_hdr->Command = rcv_hdr->Command;
411
412 /*
413 * Message is response. We don't grant oplock yet.
414 */
415 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR |
416 SMB2_FLAGS_RELATED_OPERATIONS);
417 rsp_hdr->NextCommand = 0;
418 rsp_hdr->MessageId = rcv_hdr->MessageId;
419 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
420 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
421 rsp_hdr->SessionId = rcv_hdr->SessionId;
422 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
423}
424
425/**
426 * is_chained_smb2_message() - check for chained command
427 * @work: smb work containing smb request buffer
428 *
429 * Return: true if chained request, otherwise false
430 */
431bool is_chained_smb2_message(struct ksmbd_work *work)
432{
Namjae Jeoncb451722021-11-03 08:08:44 +0900433 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
Namjae Jeond72a9c12021-09-24 09:24:08 +0900434 unsigned int len, next_cmd;
Namjae Jeone2f34482021-03-16 10:49:09 +0900435
436 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
437 return false;
438
Namjae Jeon8a893312021-06-25 13:43:37 +0900439 hdr = ksmbd_req_buf_next(work);
Namjae Jeond72a9c12021-09-24 09:24:08 +0900440 next_cmd = le32_to_cpu(hdr->NextCommand);
441 if (next_cmd > 0) {
442 if ((u64)work->next_smb2_rcv_hdr_off + next_cmd +
443 __SMB2_HEADER_STRUCTURE_SIZE >
444 get_rfc1002_len(work->request_buf)) {
445 pr_err("next command(%u) offset exceeds smb msg size\n",
446 next_cmd);
447 return false;
448 }
449
Namjae Jeondbad6302021-10-11 19:15:25 +0900450 if ((u64)get_rfc1002_len(work->response_buf) + MAX_CIFS_SMALL_BUFFER_SIZE >
451 work->response_sz) {
452 pr_err("next response offset exceeds response buffer size\n");
453 return false;
454 }
455
Namjae Jeone2f34482021-03-16 10:49:09 +0900456 ksmbd_debug(SMB, "got SMB2 chained command\n");
457 init_chained_smb2_rsp(work);
458 return true;
459 } else if (work->next_smb2_rcv_hdr_off) {
460 /*
461 * This is last request in chained command,
462 * align response to 8 byte
463 */
Namjae Jeone5066492021-03-30 12:35:23 +0900464 len = ALIGN(get_rfc1002_len(work->response_buf), 8);
465 len = len - get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900466 if (len) {
467 ksmbd_debug(SMB, "padding len %u\n", len);
Namjae Jeone5066492021-03-30 12:35:23 +0900468 inc_rfc1001_len(work->response_buf, len);
469 if (work->aux_payload_sz)
Namjae Jeone2f34482021-03-16 10:49:09 +0900470 work->aux_payload_sz += len;
471 }
472 }
473 return false;
474}
475
476/**
477 * init_smb2_rsp_hdr() - initialize smb2 response
478 * @work: smb work containing smb request buffer
479 *
480 * Return: 0
481 */
482int init_smb2_rsp_hdr(struct ksmbd_work *work)
483{
Namjae Jeoncb451722021-11-03 08:08:44 +0900484 struct smb2_hdr *rsp_hdr = smb2_get_msg(work->response_buf);
485 struct smb2_hdr *rcv_hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900486 struct ksmbd_conn *conn = work->conn;
487
488 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
Namjae Jeoncb451722021-11-03 08:08:44 +0900489 *(__be32 *)work->response_buf =
490 cpu_to_be32(conn->vals->header_size);
Namjae Jeone2f34482021-03-16 10:49:09 +0900491 rsp_hdr->ProtocolId = rcv_hdr->ProtocolId;
492 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
493 rsp_hdr->Command = rcv_hdr->Command;
494
495 /*
496 * Message is response. We don't grant oplock yet.
497 */
498 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
499 rsp_hdr->NextCommand = 0;
500 rsp_hdr->MessageId = rcv_hdr->MessageId;
501 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
502 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
503 rsp_hdr->SessionId = rcv_hdr->SessionId;
504 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
505
506 work->syncronous = true;
507 if (work->async_id) {
Namjae Jeond40012a2021-04-13 13:06:30 +0900508 ksmbd_release_id(&conn->async_ida, work->async_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900509 work->async_id = 0;
510 }
511
512 return 0;
513}
514
515/**
516 * smb2_allocate_rsp_buf() - allocate smb2 response buffer
517 * @work: smb work containing smb request buffer
518 *
519 * Return: 0 on success, otherwise -ENOMEM
520 */
521int smb2_allocate_rsp_buf(struct ksmbd_work *work)
522{
Namjae Jeoncb451722021-11-03 08:08:44 +0900523 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900524 size_t small_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
Namjae Jeon4bc59472021-10-15 17:14:02 +0900525 size_t large_sz = small_sz + work->conn->vals->max_trans_size;
Namjae Jeone2f34482021-03-16 10:49:09 +0900526 size_t sz = small_sz;
527 int cmd = le16_to_cpu(hdr->Command);
528
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900529 if (cmd == SMB2_IOCTL_HE || cmd == SMB2_QUERY_DIRECTORY_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900530 sz = large_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +0900531
532 if (cmd == SMB2_QUERY_INFO_HE) {
533 struct smb2_query_info_req *req;
534
Namjae Jeoncb451722021-11-03 08:08:44 +0900535 req = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900536 if (req->InfoType == SMB2_O_INFO_FILE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +0900537 (req->FileInfoClass == FILE_FULL_EA_INFORMATION ||
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900538 req->FileInfoClass == FILE_ALL_INFORMATION))
Namjae Jeone2f34482021-03-16 10:49:09 +0900539 sz = large_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +0900540 }
541
542 /* allocate large response buf for chained commands */
543 if (le32_to_cpu(hdr->NextCommand) > 0)
544 sz = large_sz;
545
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900546 work->response_buf = kvmalloc(sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeon63c454f2021-04-20 14:24:28 +0900547 if (!work->response_buf)
Namjae Jeone2f34482021-03-16 10:49:09 +0900548 return -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +0900549
550 work->response_sz = sz;
551 return 0;
552}
553
554/**
555 * smb2_check_user_session() - check for valid session for a user
556 * @work: smb work containing smb request buffer
557 *
558 * Return: 0 on success, otherwise error
559 */
560int smb2_check_user_session(struct ksmbd_work *work)
561{
Namjae Jeoncb451722021-11-03 08:08:44 +0900562 struct smb2_hdr *req_hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900563 struct ksmbd_conn *conn = work->conn;
564 unsigned int cmd = conn->ops->get_cmd_val(work);
565 unsigned long long sess_id;
566
567 work->sess = NULL;
568 /*
569 * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not
570 * require a session id, so no need to validate user session's for
571 * these commands.
572 */
573 if (cmd == SMB2_ECHO_HE || cmd == SMB2_NEGOTIATE_HE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +0900574 cmd == SMB2_SESSION_SETUP_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900575 return 0;
576
577 if (!ksmbd_conn_good(work))
578 return -EINVAL;
579
580 sess_id = le64_to_cpu(req_hdr->SessionId);
581 /* Check for validity of user session */
Namjae Jeonf5a544e2021-06-18 10:04:19 +0900582 work->sess = ksmbd_session_lookup_all(conn, sess_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900583 if (work->sess)
584 return 1;
585 ksmbd_debug(SMB, "Invalid user session, Uid %llu\n", sess_id);
586 return -EINVAL;
587}
588
Namjae Jeon64b39f42021-03-30 14:25:35 +0900589static void destroy_previous_session(struct ksmbd_user *user, u64 id)
Namjae Jeone2f34482021-03-16 10:49:09 +0900590{
591 struct ksmbd_session *prev_sess = ksmbd_session_lookup_slowpath(id);
592 struct ksmbd_user *prev_user;
593
594 if (!prev_sess)
595 return;
596
597 prev_user = prev_sess->user;
598
Marios Makassikis1fca8032021-05-06 11:41:54 +0900599 if (!prev_user ||
600 strcmp(user->name, prev_user->name) ||
Namjae Jeone2f34482021-03-16 10:49:09 +0900601 user->passkey_sz != prev_user->passkey_sz ||
602 memcmp(user->passkey, prev_user->passkey, user->passkey_sz)) {
603 put_session(prev_sess);
604 return;
605 }
606
607 put_session(prev_sess);
608 ksmbd_session_destroy(prev_sess);
609}
610
611/**
612 * smb2_get_name() - get filename string from on the wire smb format
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900613 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +0900614 * @src: source buffer
615 * @maxlen: maxlen of source string
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900616 * @nls_table: nls_table pointer
Namjae Jeone2f34482021-03-16 10:49:09 +0900617 *
618 * Return: matching converted filename on success, otherwise error ptr
619 */
620static char *
Namjae Jeon64b39f42021-03-30 14:25:35 +0900621smb2_get_name(struct ksmbd_share_config *share, const char *src,
Namjae Jeon070fb212021-05-26 17:57:12 +0900622 const int maxlen, struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +0900623{
Hyunchul Lee265fd192021-09-25 00:06:16 +0900624 char *name;
Namjae Jeone2f34482021-03-16 10:49:09 +0900625
Namjae Jeon64b39f42021-03-30 14:25:35 +0900626 name = smb_strndup_from_utf16(src, maxlen, 1, local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +0900627 if (IS_ERR(name)) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900628 pr_err("failed to get name %ld\n", PTR_ERR(name));
Namjae Jeone2f34482021-03-16 10:49:09 +0900629 return name;
630 }
631
Hyunchul Lee265fd192021-09-25 00:06:16 +0900632 ksmbd_conv_path_to_unix(name);
633 ksmbd_strip_last_slash(name);
634 return name;
Namjae Jeone2f34482021-03-16 10:49:09 +0900635}
636
Namjae Jeone2f34482021-03-16 10:49:09 +0900637int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg)
638{
639 struct smb2_hdr *rsp_hdr;
640 struct ksmbd_conn *conn = work->conn;
641 int id;
642
Namjae Jeoncb451722021-11-03 08:08:44 +0900643 rsp_hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900644 rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND;
645
Namjae Jeond40012a2021-04-13 13:06:30 +0900646 id = ksmbd_acquire_async_msg_id(&conn->async_ida);
Namjae Jeone2f34482021-03-16 10:49:09 +0900647 if (id < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900648 pr_err("Failed to alloc async message id\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900649 return id;
650 }
651 work->syncronous = false;
652 work->async_id = id;
653 rsp_hdr->Id.AsyncId = cpu_to_le64(id);
654
655 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900656 "Send interim Response to inform async request id : %d\n",
657 work->async_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900658
659 work->cancel_fn = fn;
660 work->cancel_argv = arg;
661
Namjae Jeon6c4e6752021-06-07 09:08:45 +0900662 if (list_empty(&work->async_request_entry)) {
663 spin_lock(&conn->request_lock);
664 list_add_tail(&work->async_request_entry, &conn->async_requests);
665 spin_unlock(&conn->request_lock);
666 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900667
668 return 0;
669}
670
671void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
672{
673 struct smb2_hdr *rsp_hdr;
674
Namjae Jeoncb451722021-11-03 08:08:44 +0900675 rsp_hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900676 smb2_set_err_rsp(work);
677 rsp_hdr->Status = status;
678
679 work->multiRsp = 1;
680 ksmbd_conn_write(work);
681 rsp_hdr->Status = 0;
682 work->multiRsp = 0;
683}
684
685static __le32 smb2_get_reparse_tag_special_file(umode_t mode)
686{
687 if (S_ISDIR(mode) || S_ISREG(mode))
688 return 0;
689
690 if (S_ISLNK(mode))
691 return IO_REPARSE_TAG_LX_SYMLINK_LE;
692 else if (S_ISFIFO(mode))
693 return IO_REPARSE_TAG_LX_FIFO_LE;
694 else if (S_ISSOCK(mode))
695 return IO_REPARSE_TAG_AF_UNIX_LE;
696 else if (S_ISCHR(mode))
697 return IO_REPARSE_TAG_LX_CHR_LE;
698 else if (S_ISBLK(mode))
699 return IO_REPARSE_TAG_LX_BLK_LE;
700
701 return 0;
702}
703
704/**
705 * smb2_get_dos_mode() - get file mode in dos format from unix mode
706 * @stat: kstat containing file mode
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900707 * @attribute: attribute flags
Namjae Jeone2f34482021-03-16 10:49:09 +0900708 *
709 * Return: converted dos mode
710 */
711static int smb2_get_dos_mode(struct kstat *stat, int attribute)
712{
713 int attr = 0;
714
Namjae Jeon64b39f42021-03-30 14:25:35 +0900715 if (S_ISDIR(stat->mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900716 attr = ATTR_DIRECTORY |
717 (attribute & (ATTR_HIDDEN | ATTR_SYSTEM));
Namjae Jeon64b39f42021-03-30 14:25:35 +0900718 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +0900719 attr = (attribute & 0x00005137) | ATTR_ARCHIVE;
720 attr &= ~(ATTR_DIRECTORY);
721 if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps &
722 FILE_SUPPORTS_SPARSE_FILES))
723 attr |= ATTR_SPARSE;
724
725 if (smb2_get_reparse_tag_special_file(stat->mode))
726 attr |= ATTR_REPARSE;
727 }
728
729 return attr;
730}
731
732static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900733 __le16 hash_id)
Namjae Jeone2f34482021-03-16 10:49:09 +0900734{
735 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
736 pneg_ctxt->DataLength = cpu_to_le16(38);
737 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
738 pneg_ctxt->Reserved = cpu_to_le32(0);
739 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
740 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
741 pneg_ctxt->HashAlgorithms = hash_id;
742}
743
744static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900745 __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +0900746{
747 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
748 pneg_ctxt->DataLength = cpu_to_le16(4);
749 pneg_ctxt->Reserved = cpu_to_le32(0);
750 pneg_ctxt->CipherCount = cpu_to_le16(1);
751 pneg_ctxt->Ciphers[0] = cipher_type;
752}
753
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900754static void build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900755 __le16 comp_algo)
Namjae Jeone2f34482021-03-16 10:49:09 +0900756{
757 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
758 pneg_ctxt->DataLength =
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900759 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
Namjae Jeone2f34482021-03-16 10:49:09 +0900760 - sizeof(struct smb2_neg_context));
761 pneg_ctxt->Reserved = cpu_to_le32(0);
762 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(1);
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900763 pneg_ctxt->Flags = cpu_to_le32(0);
Namjae Jeone2f34482021-03-16 10:49:09 +0900764 pneg_ctxt->CompressionAlgorithms[0] = comp_algo;
765}
766
Namjae Jeon378087c2021-07-21 10:05:53 +0900767static void build_sign_cap_ctxt(struct smb2_signing_capabilities *pneg_ctxt,
768 __le16 sign_algo)
769{
770 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
771 pneg_ctxt->DataLength =
772 cpu_to_le16((sizeof(struct smb2_signing_capabilities) + 2)
773 - sizeof(struct smb2_neg_context));
774 pneg_ctxt->Reserved = cpu_to_le32(0);
775 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(1);
776 pneg_ctxt->SigningAlgorithms[0] = sign_algo;
777}
778
Namjae Jeon64b39f42021-03-30 14:25:35 +0900779static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900780{
781 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
782 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
783 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
784 pneg_ctxt->Name[0] = 0x93;
785 pneg_ctxt->Name[1] = 0xAD;
786 pneg_ctxt->Name[2] = 0x25;
787 pneg_ctxt->Name[3] = 0x50;
788 pneg_ctxt->Name[4] = 0x9C;
789 pneg_ctxt->Name[5] = 0xB4;
790 pneg_ctxt->Name[6] = 0x11;
791 pneg_ctxt->Name[7] = 0xE7;
792 pneg_ctxt->Name[8] = 0xB4;
793 pneg_ctxt->Name[9] = 0x23;
794 pneg_ctxt->Name[10] = 0x83;
795 pneg_ctxt->Name[11] = 0xDE;
796 pneg_ctxt->Name[12] = 0x96;
797 pneg_ctxt->Name[13] = 0x8B;
798 pneg_ctxt->Name[14] = 0xCD;
799 pneg_ctxt->Name[15] = 0x7C;
800}
801
Namjae Jeon64b39f42021-03-30 14:25:35 +0900802static void assemble_neg_contexts(struct ksmbd_conn *conn,
Namjae Jeoncb451722021-11-03 08:08:44 +0900803 struct smb2_negotiate_rsp *rsp,
804 void *smb2_buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +0900805{
Namjae Jeone2f34482021-03-16 10:49:09 +0900806 char *pneg_ctxt = (char *)rsp +
Namjae Jeoncb451722021-11-03 08:08:44 +0900807 le32_to_cpu(rsp->NegotiateContextOffset);
Namjae Jeone2f34482021-03-16 10:49:09 +0900808 int neg_ctxt_cnt = 1;
809 int ctxt_size;
810
811 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900812 "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900813 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900814 conn->preauth_info->Preauth_HashId);
Namjae Jeone2f34482021-03-16 10:49:09 +0900815 rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt);
Namjae Jeoncb451722021-11-03 08:08:44 +0900816 inc_rfc1001_len(smb2_buf_len, AUTH_GSS_PADDING);
Namjae Jeone2f34482021-03-16 10:49:09 +0900817 ctxt_size = sizeof(struct smb2_preauth_neg_context);
818 /* Round to 8 byte boundary */
819 pneg_ctxt += round_up(sizeof(struct smb2_preauth_neg_context), 8);
820
821 if (conn->cipher_type) {
822 ctxt_size = round_up(ctxt_size, 8);
823 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900824 "assemble SMB2_ENCRYPTION_CAPABILITIES context\n");
Namjae Jeon64b39f42021-03-30 14:25:35 +0900825 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900826 conn->cipher_type);
Namjae Jeone2f34482021-03-16 10:49:09 +0900827 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900828 ctxt_size += sizeof(struct smb2_encryption_neg_context) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900829 /* Round to 8 byte boundary */
830 pneg_ctxt +=
Namjae Jeonaf320a72021-07-21 10:03:19 +0900831 round_up(sizeof(struct smb2_encryption_neg_context) + 2,
Namjae Jeone2f34482021-03-16 10:49:09 +0900832 8);
833 }
834
835 if (conn->compress_algorithm) {
836 ctxt_size = round_up(ctxt_size, 8);
837 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900838 "assemble SMB2_COMPRESSION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900839 /* Temporarily set to SMB3_COMPRESS_NONE */
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900840 build_compression_ctxt((struct smb2_compression_capabilities_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900841 conn->compress_algorithm);
Namjae Jeone2f34482021-03-16 10:49:09 +0900842 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900843 ctxt_size += sizeof(struct smb2_compression_capabilities_context) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900844 /* Round to 8 byte boundary */
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900845 pneg_ctxt += round_up(sizeof(struct smb2_compression_capabilities_context) + 2,
Namjae Jeonaf320a72021-07-21 10:03:19 +0900846 8);
Namjae Jeone2f34482021-03-16 10:49:09 +0900847 }
848
849 if (conn->posix_ext_supported) {
850 ctxt_size = round_up(ctxt_size, 8);
851 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900852 "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900853 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
854 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
855 ctxt_size += sizeof(struct smb2_posix_neg_context);
Namjae Jeon378087c2021-07-21 10:05:53 +0900856 /* Round to 8 byte boundary */
857 pneg_ctxt += round_up(sizeof(struct smb2_posix_neg_context), 8);
858 }
859
860 if (conn->signing_negotiated) {
861 ctxt_size = round_up(ctxt_size, 8);
862 ksmbd_debug(SMB,
863 "assemble SMB2_SIGNING_CAPABILITIES context\n");
864 build_sign_cap_ctxt((struct smb2_signing_capabilities *)pneg_ctxt,
865 conn->signing_algorithm);
866 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
867 ctxt_size += sizeof(struct smb2_signing_capabilities) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900868 }
869
Namjae Jeoncb451722021-11-03 08:08:44 +0900870 inc_rfc1001_len(smb2_buf_len, ctxt_size);
Namjae Jeone2f34482021-03-16 10:49:09 +0900871}
872
Namjae Jeon64b39f42021-03-30 14:25:35 +0900873static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900874 struct smb2_preauth_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900875{
876 __le32 err = STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP;
877
Namjae Jeon070fb212021-05-26 17:57:12 +0900878 if (pneg_ctxt->HashAlgorithms == SMB2_PREAUTH_INTEGRITY_SHA512) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900879 conn->preauth_info->Preauth_HashId =
880 SMB2_PREAUTH_INTEGRITY_SHA512;
881 err = STATUS_SUCCESS;
882 }
883
884 return err;
885}
886
Namjae Jeonaf320a72021-07-21 10:03:19 +0900887static void decode_encrypt_ctxt(struct ksmbd_conn *conn,
888 struct smb2_encryption_neg_context *pneg_ctxt,
889 int len_of_ctxts)
Namjae Jeone2f34482021-03-16 10:49:09 +0900890{
Namjae Jeone2f34482021-03-16 10:49:09 +0900891 int cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900892 int i, cphs_size = cph_cnt * sizeof(__le16);
Namjae Jeone2f34482021-03-16 10:49:09 +0900893
894 conn->cipher_type = 0;
895
Namjae Jeonaf320a72021-07-21 10:03:19 +0900896 if (sizeof(struct smb2_encryption_neg_context) + cphs_size >
897 len_of_ctxts) {
898 pr_err("Invalid cipher count(%d)\n", cph_cnt);
899 return;
900 }
901
Namjae Jeone2f34482021-03-16 10:49:09 +0900902 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION))
Namjae Jeonaf320a72021-07-21 10:03:19 +0900903 return;
Namjae Jeone2f34482021-03-16 10:49:09 +0900904
905 for (i = 0; i < cph_cnt; i++) {
906 if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM ||
Namjae Jeon5a0ca772021-05-06 11:43:37 +0900907 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM ||
908 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM ||
909 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900910 ksmbd_debug(SMB, "Cipher ID = 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +0900911 pneg_ctxt->Ciphers[i]);
Namjae Jeone2f34482021-03-16 10:49:09 +0900912 conn->cipher_type = pneg_ctxt->Ciphers[i];
913 break;
914 }
915 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900916}
917
Namjae Jeonaf320a72021-07-21 10:03:19 +0900918static void decode_compress_ctxt(struct ksmbd_conn *conn,
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900919 struct smb2_compression_capabilities_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900920{
Namjae Jeone2f34482021-03-16 10:49:09 +0900921 conn->compress_algorithm = SMB3_COMPRESS_NONE;
Namjae Jeone2f34482021-03-16 10:49:09 +0900922}
923
Namjae Jeon378087c2021-07-21 10:05:53 +0900924static void decode_sign_cap_ctxt(struct ksmbd_conn *conn,
925 struct smb2_signing_capabilities *pneg_ctxt,
926 int len_of_ctxts)
927{
928 int sign_algo_cnt = le16_to_cpu(pneg_ctxt->SigningAlgorithmCount);
929 int i, sign_alos_size = sign_algo_cnt * sizeof(__le16);
930
931 conn->signing_negotiated = false;
932
933 if (sizeof(struct smb2_signing_capabilities) + sign_alos_size >
934 len_of_ctxts) {
935 pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt);
936 return;
937 }
938
939 for (i = 0; i < sign_algo_cnt; i++) {
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900940 if (pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_HMAC_SHA256_LE ||
941 pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_AES_CMAC_LE) {
Namjae Jeon378087c2021-07-21 10:05:53 +0900942 ksmbd_debug(SMB, "Signing Algorithm ID = 0x%x\n",
943 pneg_ctxt->SigningAlgorithms[i]);
944 conn->signing_negotiated = true;
945 conn->signing_algorithm =
946 pneg_ctxt->SigningAlgorithms[i];
947 break;
948 }
949 }
950}
951
Namjae Jeone2f34482021-03-16 10:49:09 +0900952static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn,
Namjae Jeoncb451722021-11-03 08:08:44 +0900953 struct smb2_negotiate_req *req,
954 int len_of_smb)
Namjae Jeone2f34482021-03-16 10:49:09 +0900955{
Namjae Jeone2f34482021-03-16 10:49:09 +0900956 /* +4 is to account for the RFC1001 len field */
Namjae Jeoncb451722021-11-03 08:08:44 +0900957 struct smb2_neg_context *pctx = (struct smb2_neg_context *)req;
Namjae Jeonaf320a72021-07-21 10:03:19 +0900958 int i = 0, len_of_ctxts;
959 int offset = le32_to_cpu(req->NegotiateContextOffset);
Namjae Jeone2f34482021-03-16 10:49:09 +0900960 int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900961 __le32 status = STATUS_INVALID_PARAMETER;
Namjae Jeone2f34482021-03-16 10:49:09 +0900962
Namjae Jeonaf320a72021-07-21 10:03:19 +0900963 ksmbd_debug(SMB, "decoding %d negotiate contexts\n", neg_ctxt_cnt);
964 if (len_of_smb <= offset) {
965 ksmbd_debug(SMB, "Invalid response: negotiate context offset\n");
966 return status;
967 }
968
969 len_of_ctxts = len_of_smb - offset;
970
Namjae Jeone2f34482021-03-16 10:49:09 +0900971 while (i++ < neg_ctxt_cnt) {
Namjae Jeonaf320a72021-07-21 10:03:19 +0900972 int clen;
973
974 /* check that offset is not beyond end of SMB */
975 if (len_of_ctxts == 0)
976 break;
977
978 if (len_of_ctxts < sizeof(struct smb2_neg_context))
979 break;
980
981 pctx = (struct smb2_neg_context *)((char *)pctx + offset);
982 clen = le16_to_cpu(pctx->DataLength);
983 if (clen + sizeof(struct smb2_neg_context) > len_of_ctxts)
984 break;
985
986 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900987 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900988 "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900989 if (conn->preauth_info->Preauth_HashId)
990 break;
991
992 status = decode_preauth_ctxt(conn,
Namjae Jeonaf320a72021-07-21 10:03:19 +0900993 (struct smb2_preauth_neg_context *)pctx);
994 if (status != STATUS_SUCCESS)
995 break;
996 } else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900997 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900998 "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900999 if (conn->cipher_type)
1000 break;
1001
Namjae Jeonaf320a72021-07-21 10:03:19 +09001002 decode_encrypt_ctxt(conn,
1003 (struct smb2_encryption_neg_context *)pctx,
1004 len_of_ctxts);
1005 } else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001006 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001007 "deassemble SMB2_COMPRESSION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001008 if (conn->compress_algorithm)
1009 break;
1010
Namjae Jeonaf320a72021-07-21 10:03:19 +09001011 decode_compress_ctxt(conn,
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +09001012 (struct smb2_compression_capabilities_context *)pctx);
Namjae Jeonaf320a72021-07-21 10:03:19 +09001013 } else if (pctx->ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001014 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001015 "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n");
Namjae Jeonaf320a72021-07-21 10:03:19 +09001016 } else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001017 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001018 "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001019 conn->posix_ext_supported = true;
Namjae Jeon378087c2021-07-21 10:05:53 +09001020 } else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES) {
1021 ksmbd_debug(SMB,
1022 "deassemble SMB2_SIGNING_CAPABILITIES context\n");
1023 decode_sign_cap_ctxt(conn,
1024 (struct smb2_signing_capabilities *)pctx,
1025 len_of_ctxts);
Namjae Jeone2f34482021-03-16 10:49:09 +09001026 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001027
Namjae Jeonaf320a72021-07-21 10:03:19 +09001028 /* offsets must be 8 byte aligned */
1029 clen = (clen + 7) & ~0x7;
1030 offset = clen + sizeof(struct smb2_neg_context);
1031 len_of_ctxts -= clen + sizeof(struct smb2_neg_context);
Namjae Jeone2f34482021-03-16 10:49:09 +09001032 }
1033 return status;
1034}
1035
1036/**
1037 * smb2_handle_negotiate() - handler for smb2 negotiate command
1038 * @work: smb work containing smb request buffer
1039 *
1040 * Return: 0
1041 */
1042int smb2_handle_negotiate(struct ksmbd_work *work)
1043{
1044 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09001045 struct smb2_negotiate_req *req = smb2_get_msg(work->request_buf);
1046 struct smb2_negotiate_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001047 int rc = 0;
Namjae Jeon442ff9e2021-09-29 15:44:32 +09001048 unsigned int smb2_buf_len, smb2_neg_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09001049 __le32 status;
1050
1051 ksmbd_debug(SMB, "Received negotiate request\n");
1052 conn->need_neg = false;
1053 if (ksmbd_conn_good(work)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001054 pr_err("conn->tcp_status is already in CifsGood State\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001055 work->send_no_response = 1;
1056 return rc;
1057 }
1058
1059 if (req->DialectCount == 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001060 pr_err("malformed packet\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001061 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1062 rc = -EINVAL;
1063 goto err_out;
1064 }
1065
Namjae Jeon442ff9e2021-09-29 15:44:32 +09001066 smb2_buf_len = get_rfc1002_len(work->request_buf);
Namjae Jeoncb451722021-11-03 08:08:44 +09001067 smb2_neg_size = offsetof(struct smb2_negotiate_req, Dialects);
Namjae Jeon442ff9e2021-09-29 15:44:32 +09001068 if (smb2_neg_size > smb2_buf_len) {
1069 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1070 rc = -EINVAL;
1071 goto err_out;
1072 }
1073
1074 if (conn->dialect == SMB311_PROT_ID) {
1075 unsigned int nego_ctxt_off = le32_to_cpu(req->NegotiateContextOffset);
1076
1077 if (smb2_buf_len < nego_ctxt_off) {
1078 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1079 rc = -EINVAL;
1080 goto err_out;
1081 }
1082
1083 if (smb2_neg_size > nego_ctxt_off) {
1084 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1085 rc = -EINVAL;
1086 goto err_out;
1087 }
1088
1089 if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) >
1090 nego_ctxt_off) {
1091 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1092 rc = -EINVAL;
1093 goto err_out;
1094 }
1095 } else {
1096 if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) >
1097 smb2_buf_len) {
1098 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1099 rc = -EINVAL;
1100 goto err_out;
1101 }
1102 }
1103
Namjae Jeone2f34482021-03-16 10:49:09 +09001104 conn->cli_cap = le32_to_cpu(req->Capabilities);
1105 switch (conn->dialect) {
1106 case SMB311_PROT_ID:
1107 conn->preauth_info =
1108 kzalloc(sizeof(struct preauth_integrity_info),
Namjae Jeon070fb212021-05-26 17:57:12 +09001109 GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09001110 if (!conn->preauth_info) {
1111 rc = -ENOMEM;
1112 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1113 goto err_out;
1114 }
1115
Namjae Jeoncb451722021-11-03 08:08:44 +09001116 status = deassemble_neg_contexts(conn, req,
1117 get_rfc1002_len(work->request_buf));
Namjae Jeone2f34482021-03-16 10:49:09 +09001118 if (status != STATUS_SUCCESS) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001119 pr_err("deassemble_neg_contexts error(0x%x)\n",
1120 status);
Namjae Jeone2f34482021-03-16 10:49:09 +09001121 rsp->hdr.Status = status;
1122 rc = -EINVAL;
1123 goto err_out;
1124 }
1125
1126 rc = init_smb3_11_server(conn);
1127 if (rc < 0) {
1128 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1129 goto err_out;
1130 }
1131
1132 ksmbd_gen_preauth_integrity_hash(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001133 work->request_buf,
1134 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001135 rsp->NegotiateContextOffset =
1136 cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
Namjae Jeoncb451722021-11-03 08:08:44 +09001137 assemble_neg_contexts(conn, rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001138 break;
1139 case SMB302_PROT_ID:
1140 init_smb3_02_server(conn);
1141 break;
1142 case SMB30_PROT_ID:
1143 init_smb3_0_server(conn);
1144 break;
1145 case SMB21_PROT_ID:
1146 init_smb2_1_server(conn);
1147 break;
Namjae Jeone2f34482021-03-16 10:49:09 +09001148 case SMB2X_PROT_ID:
1149 case BAD_PROT_ID:
1150 default:
1151 ksmbd_debug(SMB, "Server dialect :0x%x not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001152 conn->dialect);
Namjae Jeone2f34482021-03-16 10:49:09 +09001153 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
1154 rc = -EINVAL;
1155 goto err_out;
1156 }
1157 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
1158
1159 /* For stats */
1160 conn->connection_type = conn->dialect;
1161
1162 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
1163 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
1164 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
1165
Namjae Jeon51a13872021-09-29 13:09:24 +09001166 memcpy(conn->ClientGUID, req->ClientGUID,
1167 SMB2_CLIENT_GUID_SIZE);
1168 conn->cli_sec_mode = le16_to_cpu(req->SecurityMode);
Namjae Jeone2f34482021-03-16 10:49:09 +09001169
1170 rsp->StructureSize = cpu_to_le16(65);
1171 rsp->DialectRevision = cpu_to_le16(conn->dialect);
1172 /* Not setting conn guid rsp->ServerGUID, as it
1173 * not used by client for identifying server
1174 */
1175 memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE);
1176
1177 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
1178 rsp->ServerStartTime = 0;
1179 ksmbd_debug(SMB, "negotiate context offset %d, count %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001180 le32_to_cpu(rsp->NegotiateContextOffset),
1181 le16_to_cpu(rsp->NegotiateContextCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09001182
1183 rsp->SecurityBufferOffset = cpu_to_le16(128);
1184 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
Namjae Jeoncb451722021-11-03 08:08:44 +09001185 ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) +
1186 le16_to_cpu(rsp->SecurityBufferOffset));
1187 inc_rfc1001_len(work->response_buf, sizeof(struct smb2_negotiate_rsp) -
Namjae Jeon070fb212021-05-26 17:57:12 +09001188 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
1189 AUTH_GSS_LENGTH);
Namjae Jeone2f34482021-03-16 10:49:09 +09001190 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
1191 conn->use_spnego = true;
1192
1193 if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001194 server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) &&
1195 req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09001196 conn->sign = true;
1197 else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) {
1198 server_conf.enforced_signing = true;
1199 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
1200 conn->sign = true;
1201 }
1202
1203 conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode);
1204 ksmbd_conn_set_need_negotiate(work);
1205
1206err_out:
1207 if (rc < 0)
1208 smb2_set_err_rsp(work);
1209
1210 return rc;
1211}
1212
1213static int alloc_preauth_hash(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09001214 struct ksmbd_conn *conn)
Namjae Jeone2f34482021-03-16 10:49:09 +09001215{
1216 if (sess->Preauth_HashValue)
1217 return 0;
1218
kernel test robot86f52972021-04-02 12:17:24 +09001219 sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue,
Namjae Jeon070fb212021-05-26 17:57:12 +09001220 PREAUTH_HASHVALUE_SIZE, GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09001221 if (!sess->Preauth_HashValue)
1222 return -ENOMEM;
1223
Namjae Jeone2f34482021-03-16 10:49:09 +09001224 return 0;
1225}
1226
1227static int generate_preauth_hash(struct ksmbd_work *work)
1228{
1229 struct ksmbd_conn *conn = work->conn;
1230 struct ksmbd_session *sess = work->sess;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001231 u8 *preauth_hash;
Namjae Jeone2f34482021-03-16 10:49:09 +09001232
1233 if (conn->dialect != SMB311_PROT_ID)
1234 return 0;
1235
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001236 if (conn->binding) {
1237 struct preauth_session *preauth_sess;
1238
1239 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
1240 if (!preauth_sess) {
1241 preauth_sess = ksmbd_preauth_session_alloc(conn, sess->id);
1242 if (!preauth_sess)
1243 return -ENOMEM;
1244 }
1245
1246 preauth_hash = preauth_sess->Preauth_HashValue;
1247 } else {
1248 if (!sess->Preauth_HashValue)
1249 if (alloc_preauth_hash(sess, conn))
1250 return -ENOMEM;
1251 preauth_hash = sess->Preauth_HashValue;
Namjae Jeone2f34482021-03-16 10:49:09 +09001252 }
1253
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001254 ksmbd_gen_preauth_integrity_hash(conn, work->request_buf, preauth_hash);
Namjae Jeone2f34482021-03-16 10:49:09 +09001255 return 0;
1256}
1257
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001258static int decode_negotiation_token(struct ksmbd_conn *conn,
1259 struct negotiate_message *negblob,
1260 size_t sz)
Namjae Jeone2f34482021-03-16 10:49:09 +09001261{
Namjae Jeone2f34482021-03-16 10:49:09 +09001262 if (!conn->use_spnego)
1263 return -EINVAL;
1264
Hyunchul Leefad41612021-04-19 17:26:15 +09001265 if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) {
1266 if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001267 conn->auth_mechs |= KSMBD_AUTH_NTLMSSP;
1268 conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP;
1269 conn->use_spnego = false;
1270 }
1271 }
1272 return 0;
1273}
1274
1275static int ntlm_negotiate(struct ksmbd_work *work,
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001276 struct negotiate_message *negblob,
1277 size_t negblob_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09001278{
Namjae Jeoncb451722021-11-03 08:08:44 +09001279 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001280 struct challenge_message *chgblob;
1281 unsigned char *spnego_blob = NULL;
1282 u16 spnego_blob_len;
1283 char *neg_blob;
1284 int sz, rc;
1285
1286 ksmbd_debug(SMB, "negotiate phase\n");
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001287 rc = ksmbd_decode_ntlmssp_neg_blob(negblob, negblob_len, work->sess);
Namjae Jeone2f34482021-03-16 10:49:09 +09001288 if (rc)
1289 return rc;
1290
1291 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1292 chgblob =
1293 (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz);
1294 memset(chgblob, 0, sizeof(struct challenge_message));
1295
1296 if (!work->conn->use_spnego) {
1297 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess);
1298 if (sz < 0)
1299 return -ENOMEM;
1300
1301 rsp->SecurityBufferLength = cpu_to_le16(sz);
1302 return 0;
1303 }
1304
1305 sz = sizeof(struct challenge_message);
1306 sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6;
1307
1308 neg_blob = kzalloc(sz, GFP_KERNEL);
1309 if (!neg_blob)
1310 return -ENOMEM;
1311
1312 chgblob = (struct challenge_message *)neg_blob;
1313 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess);
1314 if (sz < 0) {
1315 rc = -ENOMEM;
1316 goto out;
1317 }
1318
Namjae Jeon070fb212021-05-26 17:57:12 +09001319 rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len,
1320 neg_blob, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09001321 if (rc) {
1322 rc = -ENOMEM;
1323 goto out;
1324 }
1325
1326 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1327 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
1328 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1329
1330out:
1331 kfree(spnego_blob);
1332 kfree(neg_blob);
1333 return rc;
1334}
1335
1336static struct authenticate_message *user_authblob(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001337 struct smb2_sess_setup_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +09001338{
1339 int sz;
1340
1341 if (conn->use_spnego && conn->mechToken)
1342 return (struct authenticate_message *)conn->mechToken;
1343
1344 sz = le16_to_cpu(req->SecurityBufferOffset);
1345 return (struct authenticate_message *)((char *)&req->hdr.ProtocolId
1346 + sz);
1347}
1348
1349static struct ksmbd_user *session_user(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001350 struct smb2_sess_setup_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +09001351{
1352 struct authenticate_message *authblob;
1353 struct ksmbd_user *user;
1354 char *name;
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001355 unsigned int auth_msg_len, name_off, name_len, secbuf_len;
Namjae Jeone2f34482021-03-16 10:49:09 +09001356
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001357 secbuf_len = le16_to_cpu(req->SecurityBufferLength);
1358 if (secbuf_len < sizeof(struct authenticate_message)) {
1359 ksmbd_debug(SMB, "blob len %d too small\n", secbuf_len);
1360 return NULL;
1361 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001362 authblob = user_authblob(conn, req);
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001363 name_off = le32_to_cpu(authblob->UserName.BufferOffset);
1364 name_len = le16_to_cpu(authblob->UserName.Length);
1365 auth_msg_len = le16_to_cpu(req->SecurityBufferOffset) + secbuf_len;
1366
1367 if (auth_msg_len < (u64)name_off + name_len)
1368 return NULL;
1369
1370 name = smb_strndup_from_utf16((const char *)authblob + name_off,
1371 name_len,
Namjae Jeone2f34482021-03-16 10:49:09 +09001372 true,
1373 conn->local_nls);
1374 if (IS_ERR(name)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001375 pr_err("cannot allocate memory\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001376 return NULL;
1377 }
1378
1379 ksmbd_debug(SMB, "session setup request for user %s\n", name);
1380 user = ksmbd_login_user(name);
1381 kfree(name);
1382 return user;
1383}
1384
1385static int ntlm_authenticate(struct ksmbd_work *work)
1386{
Namjae Jeoncb451722021-11-03 08:08:44 +09001387 struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf);
1388 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001389 struct ksmbd_conn *conn = work->conn;
1390 struct ksmbd_session *sess = work->sess;
1391 struct channel *chann = NULL;
1392 struct ksmbd_user *user;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001393 u64 prev_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09001394 int sz, rc;
1395
1396 ksmbd_debug(SMB, "authenticate phase\n");
1397 if (conn->use_spnego) {
1398 unsigned char *spnego_blob;
1399 u16 spnego_blob_len;
1400
1401 rc = build_spnego_ntlmssp_auth_blob(&spnego_blob,
1402 &spnego_blob_len,
1403 0);
1404 if (rc)
1405 return -ENOMEM;
1406
1407 sz = le16_to_cpu(rsp->SecurityBufferOffset);
Namjae Jeon64b39f42021-03-30 14:25:35 +09001408 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001409 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1410 kfree(spnego_blob);
Namjae Jeoncb451722021-11-03 08:08:44 +09001411 inc_rfc1001_len(work->response_buf, spnego_blob_len - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001412 }
1413
1414 user = session_user(conn, req);
1415 if (!user) {
1416 ksmbd_debug(SMB, "Unknown user name or an error\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001417 return -EPERM;
Namjae Jeone2f34482021-03-16 10:49:09 +09001418 }
1419
1420 /* Check for previous session */
1421 prev_id = le64_to_cpu(req->PreviousSessionId);
1422 if (prev_id && prev_id != sess->id)
1423 destroy_previous_session(user, prev_id);
1424
1425 if (sess->state == SMB2_SESSION_VALID) {
1426 /*
1427 * Reuse session if anonymous try to connect
1428 * on reauthetication.
1429 */
1430 if (ksmbd_anonymous_user(user)) {
1431 ksmbd_free_user(user);
1432 return 0;
1433 }
1434 ksmbd_free_user(sess->user);
1435 }
1436
1437 sess->user = user;
1438 if (user_guest(sess->user)) {
1439 if (conn->sign) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001440 ksmbd_debug(SMB, "Guest login not allowed when signing enabled\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001441 return -EPERM;
Namjae Jeone2f34482021-03-16 10:49:09 +09001442 }
1443
1444 rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE;
1445 } else {
1446 struct authenticate_message *authblob;
1447
1448 authblob = user_authblob(conn, req);
1449 sz = le16_to_cpu(req->SecurityBufferLength);
1450 rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, sess);
1451 if (rc) {
1452 set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD);
1453 ksmbd_debug(SMB, "authentication failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001454 return -EPERM;
Namjae Jeone2f34482021-03-16 10:49:09 +09001455 }
1456
1457 /*
1458 * If session state is SMB2_SESSION_VALID, We can assume
1459 * that it is reauthentication. And the user/password
1460 * has been verified, so return it here.
1461 */
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001462 if (sess->state == SMB2_SESSION_VALID) {
1463 if (conn->binding)
1464 goto binding_session;
Namjae Jeone2f34482021-03-16 10:49:09 +09001465 return 0;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001466 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001467
1468 if ((conn->sign || server_conf.enforced_signing) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001469 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
Namjae Jeone2f34482021-03-16 10:49:09 +09001470 sess->sign = true;
1471
1472 if (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION &&
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001473 conn->ops->generate_encryptionkey &&
1474 !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001475 rc = conn->ops->generate_encryptionkey(sess);
1476 if (rc) {
1477 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001478 "SMB3 encryption key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001479 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001480 }
1481 sess->enc = true;
1482 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1483 /*
1484 * signing is disable if encryption is enable
1485 * on this session
1486 */
1487 sess->sign = false;
1488 }
1489 }
1490
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001491binding_session:
Namjae Jeone2f34482021-03-16 10:49:09 +09001492 if (conn->dialect >= SMB30_PROT_ID) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001493 chann = lookup_chann_list(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001494 if (!chann) {
1495 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1496 if (!chann)
1497 return -ENOMEM;
1498
1499 chann->conn = conn;
1500 INIT_LIST_HEAD(&chann->chann_list);
1501 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1502 }
1503 }
1504
1505 if (conn->ops->generate_signingkey) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001506 rc = conn->ops->generate_signingkey(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001507 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001508 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001509 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001510 }
1511 }
1512
Namjae Jeon51a13872021-09-29 13:09:24 +09001513 if (!ksmbd_conn_lookup_dialect(conn)) {
1514 pr_err("fail to verify the dialect\n");
1515 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001516 }
1517 return 0;
1518}
1519
1520#ifdef CONFIG_SMB_SERVER_KERBEROS5
1521static int krb5_authenticate(struct ksmbd_work *work)
1522{
Namjae Jeoncb451722021-11-03 08:08:44 +09001523 struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf);
1524 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001525 struct ksmbd_conn *conn = work->conn;
1526 struct ksmbd_session *sess = work->sess;
1527 char *in_blob, *out_blob;
1528 struct channel *chann = NULL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001529 u64 prev_sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09001530 int in_len, out_len;
1531 int retval;
1532
1533 in_blob = (char *)&req->hdr.ProtocolId +
1534 le16_to_cpu(req->SecurityBufferOffset);
1535 in_len = le16_to_cpu(req->SecurityBufferLength);
1536 out_blob = (char *)&rsp->hdr.ProtocolId +
1537 le16_to_cpu(rsp->SecurityBufferOffset);
1538 out_len = work->response_sz -
Namjae Jeoncb451722021-11-03 08:08:44 +09001539 (le16_to_cpu(rsp->SecurityBufferOffset) + 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09001540
1541 /* Check previous session */
1542 prev_sess_id = le64_to_cpu(req->PreviousSessionId);
1543 if (prev_sess_id && prev_sess_id != sess->id)
1544 destroy_previous_session(sess->user, prev_sess_id);
1545
1546 if (sess->state == SMB2_SESSION_VALID)
1547 ksmbd_free_user(sess->user);
1548
1549 retval = ksmbd_krb5_authenticate(sess, in_blob, in_len,
Namjae Jeon070fb212021-05-26 17:57:12 +09001550 out_blob, &out_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001551 if (retval) {
1552 ksmbd_debug(SMB, "krb5 authentication failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001553 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001554 }
1555 rsp->SecurityBufferLength = cpu_to_le16(out_len);
Namjae Jeoncb451722021-11-03 08:08:44 +09001556 inc_rfc1001_len(work->response_buf, out_len - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001557
1558 if ((conn->sign || server_conf.enforced_signing) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001559 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
Namjae Jeone2f34482021-03-16 10:49:09 +09001560 sess->sign = true;
1561
1562 if ((conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09001563 conn->ops->generate_encryptionkey) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001564 retval = conn->ops->generate_encryptionkey(sess);
1565 if (retval) {
1566 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001567 "SMB3 encryption key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001568 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001569 }
1570 sess->enc = true;
1571 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1572 sess->sign = false;
1573 }
1574
1575 if (conn->dialect >= SMB30_PROT_ID) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001576 chann = lookup_chann_list(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001577 if (!chann) {
1578 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1579 if (!chann)
1580 return -ENOMEM;
1581
1582 chann->conn = conn;
1583 INIT_LIST_HEAD(&chann->chann_list);
1584 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1585 }
1586 }
1587
1588 if (conn->ops->generate_signingkey) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001589 retval = conn->ops->generate_signingkey(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001590 if (retval) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001591 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001592 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001593 }
1594 }
1595
Namjae Jeon51a13872021-09-29 13:09:24 +09001596 if (!ksmbd_conn_lookup_dialect(conn)) {
1597 pr_err("fail to verify the dialect\n");
1598 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001599 }
1600 return 0;
1601}
1602#else
1603static int krb5_authenticate(struct ksmbd_work *work)
1604{
1605 return -EOPNOTSUPP;
1606}
1607#endif
1608
1609int smb2_sess_setup(struct ksmbd_work *work)
1610{
1611 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09001612 struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf);
1613 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001614 struct ksmbd_session *sess;
1615 struct negotiate_message *negblob;
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001616 unsigned int negblob_len, negblob_off;
Namjae Jeone2f34482021-03-16 10:49:09 +09001617 int rc = 0;
1618
1619 ksmbd_debug(SMB, "Received request for session setup\n");
1620
1621 rsp->StructureSize = cpu_to_le16(9);
1622 rsp->SessionFlags = 0;
1623 rsp->SecurityBufferOffset = cpu_to_le16(72);
1624 rsp->SecurityBufferLength = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09001625 inc_rfc1001_len(work->response_buf, 9);
Namjae Jeone2f34482021-03-16 10:49:09 +09001626
1627 if (!req->hdr.SessionId) {
1628 sess = ksmbd_smb2_session_create();
1629 if (!sess) {
1630 rc = -ENOMEM;
1631 goto out_err;
1632 }
1633 rsp->hdr.SessionId = cpu_to_le64(sess->id);
1634 ksmbd_session_register(conn, sess);
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001635 } else if (conn->dialect >= SMB30_PROT_ID &&
1636 (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
1637 req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) {
1638 u64 sess_id = le64_to_cpu(req->hdr.SessionId);
1639
1640 sess = ksmbd_session_lookup_slowpath(sess_id);
1641 if (!sess) {
1642 rc = -ENOENT;
1643 goto out_err;
1644 }
1645
1646 if (conn->dialect != sess->conn->dialect) {
1647 rc = -EINVAL;
1648 goto out_err;
1649 }
1650
1651 if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) {
1652 rc = -EINVAL;
1653 goto out_err;
1654 }
1655
1656 if (strncmp(conn->ClientGUID, sess->conn->ClientGUID,
1657 SMB2_CLIENT_GUID_SIZE)) {
1658 rc = -ENOENT;
1659 goto out_err;
1660 }
1661
1662 if (sess->state == SMB2_SESSION_IN_PROGRESS) {
1663 rc = -EACCES;
1664 goto out_err;
1665 }
1666
1667 if (sess->state == SMB2_SESSION_EXPIRED) {
1668 rc = -EFAULT;
1669 goto out_err;
1670 }
1671
1672 if (ksmbd_session_lookup(conn, sess_id)) {
1673 rc = -EACCES;
1674 goto out_err;
1675 }
1676
1677 conn->binding = true;
1678 } else if ((conn->dialect < SMB30_PROT_ID ||
1679 server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
1680 (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
Colin Ian King4951a842021-07-06 13:05:01 +01001681 sess = NULL;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001682 rc = -EACCES;
1683 goto out_err;
Namjae Jeone2f34482021-03-16 10:49:09 +09001684 } else {
1685 sess = ksmbd_session_lookup(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001686 le64_to_cpu(req->hdr.SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09001687 if (!sess) {
1688 rc = -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001689 goto out_err;
1690 }
1691 }
1692 work->sess = sess;
1693
1694 if (sess->state == SMB2_SESSION_EXPIRED)
1695 sess->state = SMB2_SESSION_IN_PROGRESS;
1696
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001697 negblob_off = le16_to_cpu(req->SecurityBufferOffset);
1698 negblob_len = le16_to_cpu(req->SecurityBufferLength);
Namjae Jeoncb451722021-11-03 08:08:44 +09001699 if (negblob_off < offsetof(struct smb2_sess_setup_req, Buffer) ||
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001700 negblob_len < offsetof(struct negotiate_message, NegotiateFlags))
1701 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001702
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001703 negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId +
1704 negblob_off);
1705
1706 if (decode_negotiation_token(conn, negblob, negblob_len) == 0) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001707 if (conn->mechToken)
1708 negblob = (struct negotiate_message *)conn->mechToken;
1709 }
1710
1711 if (server_conf.auth_mechs & conn->auth_mechs) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001712 rc = generate_preauth_hash(work);
1713 if (rc)
1714 goto out_err;
1715
Namjae Jeone2f34482021-03-16 10:49:09 +09001716 if (conn->preferred_auth_mech &
1717 (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001718 rc = krb5_authenticate(work);
1719 if (rc) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001720 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001721 goto out_err;
1722 }
1723
1724 ksmbd_conn_set_good(work);
1725 sess->state = SMB2_SESSION_VALID;
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001726 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001727 sess->Preauth_HashValue = NULL;
1728 } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001729 if (negblob->MessageType == NtLmNegotiate) {
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001730 rc = ntlm_negotiate(work, negblob, negblob_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001731 if (rc)
1732 goto out_err;
1733 rsp->hdr.Status =
1734 STATUS_MORE_PROCESSING_REQUIRED;
1735 /*
1736 * Note: here total size -1 is done as an
1737 * adjustment for 0 size blob
1738 */
Namjae Jeoncb451722021-11-03 08:08:44 +09001739 inc_rfc1001_len(work->response_buf,
1740 le16_to_cpu(rsp->SecurityBufferLength) - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001741
1742 } else if (negblob->MessageType == NtLmAuthenticate) {
1743 rc = ntlm_authenticate(work);
1744 if (rc)
1745 goto out_err;
1746
1747 ksmbd_conn_set_good(work);
1748 sess->state = SMB2_SESSION_VALID;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001749 if (conn->binding) {
1750 struct preauth_session *preauth_sess;
1751
1752 preauth_sess =
1753 ksmbd_preauth_session_lookup(conn, sess->id);
1754 if (preauth_sess) {
1755 list_del(&preauth_sess->preauth_entry);
1756 kfree(preauth_sess);
1757 }
1758 }
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001759 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001760 sess->Preauth_HashValue = NULL;
1761 }
1762 } else {
1763 /* TODO: need one more negotiation */
Namjae Jeonbde16942021-06-28 15:23:19 +09001764 pr_err("Not support the preferred authentication\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001765 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001766 }
1767 } else {
Namjae Jeonbde16942021-06-28 15:23:19 +09001768 pr_err("Not support authentication\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001769 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001770 }
1771
1772out_err:
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001773 if (rc == -EINVAL)
1774 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1775 else if (rc == -ENOENT)
1776 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1777 else if (rc == -EACCES)
1778 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
1779 else if (rc == -EFAULT)
1780 rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED;
Namjae Jeon58090b12021-07-16 14:52:09 +09001781 else if (rc == -ENOMEM)
1782 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001783 else if (rc)
1784 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1785
Namjae Jeone2f34482021-03-16 10:49:09 +09001786 if (conn->use_spnego && conn->mechToken) {
1787 kfree(conn->mechToken);
1788 conn->mechToken = NULL;
1789 }
1790
Namjae Jeon621be842021-10-13 17:28:31 +09001791 if (rc < 0) {
1792 /*
1793 * SecurityBufferOffset should be set to zero
1794 * in session setup error response.
1795 */
1796 rsp->SecurityBufferOffset = 0;
1797
1798 if (sess) {
1799 bool try_delay = false;
1800
1801 /*
1802 * To avoid dictionary attacks (repeated session setups rapidly sent) to
1803 * connect to server, ksmbd make a delay of a 5 seconds on session setup
1804 * failure to make it harder to send enough random connection requests
1805 * to break into a server.
1806 */
1807 if (sess->user && sess->user->flags & KSMBD_USER_FLAG_DELAY_SESSION)
1808 try_delay = true;
1809
1810 ksmbd_session_destroy(sess);
1811 work->sess = NULL;
1812 if (try_delay)
1813 ssleep(5);
1814 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001815 }
1816
1817 return rc;
1818}
1819
1820/**
1821 * smb2_tree_connect() - handler for smb2 tree connect command
1822 * @work: smb work containing smb request buffer
1823 *
1824 * Return: 0 on success, otherwise error
1825 */
1826int smb2_tree_connect(struct ksmbd_work *work)
1827{
1828 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09001829 struct smb2_tree_connect_req *req = smb2_get_msg(work->request_buf);
1830 struct smb2_tree_connect_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001831 struct ksmbd_session *sess = work->sess;
1832 char *treename = NULL, *name = NULL;
1833 struct ksmbd_tree_conn_status status;
1834 struct ksmbd_share_config *share;
1835 int rc = -EINVAL;
1836
1837 treename = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09001838 le16_to_cpu(req->PathLength), true,
1839 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09001840 if (IS_ERR(treename)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001841 pr_err("treename is NULL\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001842 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1843 goto out_err1;
1844 }
1845
Stephen Rothwell36ba3862021-03-17 17:01:15 +09001846 name = ksmbd_extract_sharename(treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001847 if (IS_ERR(name)) {
1848 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1849 goto out_err1;
1850 }
1851
1852 ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001853 name, treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001854
1855 status = ksmbd_tree_conn_connect(sess, name);
1856 if (status.ret == KSMBD_TREE_CONN_STATUS_OK)
1857 rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id);
1858 else
1859 goto out_err1;
1860
1861 share = status.tree_conn->share_conf;
1862 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
1863 ksmbd_debug(SMB, "IPC share path request\n");
1864 rsp->ShareType = SMB2_SHARE_TYPE_PIPE;
1865 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1866 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE |
1867 FILE_DELETE_LE | FILE_READ_CONTROL_LE |
1868 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1869 FILE_SYNCHRONIZE_LE;
1870 } else {
1871 rsp->ShareType = SMB2_SHARE_TYPE_DISK;
1872 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1873 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE;
1874 if (test_tree_conn_flag(status.tree_conn,
1875 KSMBD_TREE_CONN_FLAG_WRITABLE)) {
1876 rsp->MaximalAccess |= FILE_WRITE_DATA_LE |
1877 FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE |
Wan Jiabing3aefd542021-06-07 12:54:32 +08001878 FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE |
1879 FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE |
1880 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1881 FILE_SYNCHRONIZE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09001882 }
1883 }
1884
1885 status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1886 if (conn->posix_ext_supported)
1887 status.tree_conn->posix_extensions = true;
1888
1889out_err1:
1890 rsp->StructureSize = cpu_to_le16(16);
1891 rsp->Capabilities = 0;
1892 rsp->Reserved = 0;
1893 /* default manual caching */
1894 rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING;
Namjae Jeoncb451722021-11-03 08:08:44 +09001895 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09001896
1897 if (!IS_ERR(treename))
1898 kfree(treename);
1899 if (!IS_ERR(name))
1900 kfree(name);
1901
1902 switch (status.ret) {
1903 case KSMBD_TREE_CONN_STATUS_OK:
1904 rsp->hdr.Status = STATUS_SUCCESS;
1905 rc = 0;
1906 break;
1907 case KSMBD_TREE_CONN_STATUS_NO_SHARE:
1908 rsp->hdr.Status = STATUS_BAD_NETWORK_PATH;
1909 break;
1910 case -ENOMEM:
1911 case KSMBD_TREE_CONN_STATUS_NOMEM:
1912 rsp->hdr.Status = STATUS_NO_MEMORY;
1913 break;
1914 case KSMBD_TREE_CONN_STATUS_ERROR:
1915 case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS:
1916 case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS:
1917 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1918 break;
1919 case -EINVAL:
1920 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1921 break;
1922 default:
1923 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1924 }
1925
1926 return rc;
1927}
1928
1929/**
1930 * smb2_create_open_flags() - convert smb open flags to unix open flags
1931 * @file_present: is file already present
1932 * @access: file access flags
1933 * @disposition: file disposition flags
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001934 * @may_flags: set with MAY_ flags
Namjae Jeone2f34482021-03-16 10:49:09 +09001935 *
1936 * Return: file open flags
1937 */
1938static int smb2_create_open_flags(bool file_present, __le32 access,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001939 __le32 disposition,
1940 int *may_flags)
Namjae Jeone2f34482021-03-16 10:49:09 +09001941{
1942 int oflags = O_NONBLOCK | O_LARGEFILE;
1943
1944 if (access & FILE_READ_DESIRED_ACCESS_LE &&
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001945 access & FILE_WRITE_DESIRE_ACCESS_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001946 oflags |= O_RDWR;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001947 *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE;
1948 } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001949 oflags |= O_WRONLY;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001950 *may_flags = MAY_OPEN | MAY_WRITE;
1951 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09001952 oflags |= O_RDONLY;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001953 *may_flags = MAY_OPEN | MAY_READ;
1954 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001955
1956 if (access == FILE_READ_ATTRIBUTES_LE)
1957 oflags |= O_PATH;
1958
1959 if (file_present) {
1960 switch (disposition & FILE_CREATE_MASK_LE) {
1961 case FILE_OPEN_LE:
1962 case FILE_CREATE_LE:
1963 break;
1964 case FILE_SUPERSEDE_LE:
1965 case FILE_OVERWRITE_LE:
1966 case FILE_OVERWRITE_IF_LE:
1967 oflags |= O_TRUNC;
1968 break;
1969 default:
1970 break;
1971 }
1972 } else {
1973 switch (disposition & FILE_CREATE_MASK_LE) {
1974 case FILE_SUPERSEDE_LE:
1975 case FILE_CREATE_LE:
1976 case FILE_OPEN_IF_LE:
1977 case FILE_OVERWRITE_IF_LE:
1978 oflags |= O_CREAT;
1979 break;
1980 case FILE_OPEN_LE:
1981 case FILE_OVERWRITE_LE:
1982 oflags &= ~O_CREAT;
1983 break;
1984 default:
1985 break;
1986 }
1987 }
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001988
Namjae Jeone2f34482021-03-16 10:49:09 +09001989 return oflags;
1990}
1991
1992/**
1993 * smb2_tree_disconnect() - handler for smb tree connect request
1994 * @work: smb work containing request buffer
1995 *
1996 * Return: 0
1997 */
1998int smb2_tree_disconnect(struct ksmbd_work *work)
1999{
Namjae Jeoncb451722021-11-03 08:08:44 +09002000 struct smb2_tree_disconnect_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002001 struct ksmbd_session *sess = work->sess;
2002 struct ksmbd_tree_connect *tcon = work->tcon;
2003
2004 rsp->StructureSize = cpu_to_le16(4);
Namjae Jeoncb451722021-11-03 08:08:44 +09002005 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09002006
2007 ksmbd_debug(SMB, "request\n");
2008
2009 if (!tcon) {
Namjae Jeoncb451722021-11-03 08:08:44 +09002010 struct smb2_tree_disconnect_req *req =
2011 smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002012
2013 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2014 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2015 smb2_set_err_rsp(work);
2016 return 0;
2017 }
2018
2019 ksmbd_close_tree_conn_fds(work);
2020 ksmbd_tree_conn_disconnect(sess, tcon);
2021 return 0;
2022}
2023
2024/**
2025 * smb2_session_logoff() - handler for session log off request
2026 * @work: smb work containing request buffer
2027 *
2028 * Return: 0
2029 */
2030int smb2_session_logoff(struct ksmbd_work *work)
2031{
2032 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09002033 struct smb2_logoff_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002034 struct ksmbd_session *sess = work->sess;
2035
2036 rsp->StructureSize = cpu_to_le16(4);
Namjae Jeoncb451722021-11-03 08:08:44 +09002037 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09002038
2039 ksmbd_debug(SMB, "request\n");
2040
2041 /* Got a valid session, set connection state */
2042 WARN_ON(sess->conn != conn);
2043
2044 /* setting CifsExiting here may race with start_tcp_sess */
2045 ksmbd_conn_set_need_reconnect(work);
2046 ksmbd_close_session_fds(work);
2047 ksmbd_conn_wait_idle(conn);
2048
2049 if (ksmbd_tree_conn_session_logoff(sess)) {
Namjae Jeoncb451722021-11-03 08:08:44 +09002050 struct smb2_logoff_req *req = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002051
2052 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2053 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2054 smb2_set_err_rsp(work);
2055 return 0;
2056 }
2057
2058 ksmbd_destroy_file_table(&sess->file_table);
2059 sess->state = SMB2_SESSION_EXPIRED;
2060
2061 ksmbd_free_user(sess->user);
2062 sess->user = NULL;
2063
2064 /* let start_tcp_sess free connection info now */
2065 ksmbd_conn_set_need_negotiate(work);
2066 return 0;
2067}
2068
2069/**
2070 * create_smb2_pipe() - create IPC pipe
2071 * @work: smb work containing request buffer
2072 *
2073 * Return: 0 on success, otherwise error
2074 */
2075static noinline int create_smb2_pipe(struct ksmbd_work *work)
2076{
Namjae Jeoncb451722021-11-03 08:08:44 +09002077 struct smb2_create_rsp *rsp = smb2_get_msg(work->response_buf);
2078 struct smb2_create_req *req = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002079 int id;
2080 int err;
2081 char *name;
2082
2083 name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09002084 1, work->conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09002085 if (IS_ERR(name)) {
2086 rsp->hdr.Status = STATUS_NO_MEMORY;
2087 err = PTR_ERR(name);
2088 goto out;
2089 }
2090
2091 id = ksmbd_session_rpc_open(work->sess, name);
Marios Makassikis79caa962021-05-06 11:38:35 +09002092 if (id < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002093 pr_err("Unable to open RPC pipe: %d\n", id);
Marios Makassikis79caa962021-05-06 11:38:35 +09002094 err = id;
2095 goto out;
2096 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002097
Marios Makassikis79caa962021-05-06 11:38:35 +09002098 rsp->hdr.Status = STATUS_SUCCESS;
Namjae Jeone2f34482021-03-16 10:49:09 +09002099 rsp->StructureSize = cpu_to_le16(89);
2100 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2101 rsp->Reserved = 0;
2102 rsp->CreateAction = cpu_to_le32(FILE_OPENED);
2103
2104 rsp->CreationTime = cpu_to_le64(0);
2105 rsp->LastAccessTime = cpu_to_le64(0);
2106 rsp->ChangeTime = cpu_to_le64(0);
2107 rsp->AllocationSize = cpu_to_le64(0);
2108 rsp->EndofFile = cpu_to_le64(0);
2109 rsp->FileAttributes = ATTR_NORMAL_LE;
2110 rsp->Reserved2 = 0;
2111 rsp->VolatileFileId = cpu_to_le64(id);
2112 rsp->PersistentFileId = 0;
2113 rsp->CreateContextsOffset = 0;
2114 rsp->CreateContextsLength = 0;
2115
Namjae Jeoncb451722021-11-03 08:08:44 +09002116 inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/
Namjae Jeone2f34482021-03-16 10:49:09 +09002117 kfree(name);
2118 return 0;
2119
2120out:
Marios Makassikis79caa962021-05-06 11:38:35 +09002121 switch (err) {
2122 case -EINVAL:
2123 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2124 break;
2125 case -ENOSPC:
2126 case -ENOMEM:
2127 rsp->hdr.Status = STATUS_NO_MEMORY;
2128 break;
2129 }
2130
2131 if (!IS_ERR(name))
2132 kfree(name);
2133
Namjae Jeone2f34482021-03-16 10:49:09 +09002134 smb2_set_err_rsp(work);
2135 return err;
2136}
2137
Namjae Jeone2f34482021-03-16 10:49:09 +09002138/**
2139 * smb2_set_ea() - handler for setting extended attributes using set
2140 * info command
2141 * @eabuf: set info command buffer
Namjae Jeon9496e262021-09-29 15:41:48 +09002142 * @buf_len: set info command buffer length
Namjae Jeone2f34482021-03-16 10:49:09 +09002143 * @path: dentry path for get ea
2144 *
2145 * Return: 0 on success, otherwise error
2146 */
Namjae Jeon9496e262021-09-29 15:41:48 +09002147static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len,
2148 struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002149{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002150 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002151 char *attr_name = NULL, *value;
2152 int rc = 0;
Namjae Jeon9496e262021-09-29 15:41:48 +09002153 unsigned int next = 0;
2154
2155 if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength +
2156 le16_to_cpu(eabuf->EaValueLength))
2157 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002158
2159 attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL);
2160 if (!attr_name)
2161 return -ENOMEM;
2162
2163 do {
2164 if (!eabuf->EaNameLength)
2165 goto next;
2166
2167 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002168 "name : <%s>, name_len : %u, value_len : %u, next : %u\n",
2169 eabuf->name, eabuf->EaNameLength,
2170 le16_to_cpu(eabuf->EaValueLength),
2171 le32_to_cpu(eabuf->NextEntryOffset));
Namjae Jeone2f34482021-03-16 10:49:09 +09002172
2173 if (eabuf->EaNameLength >
Namjae Jeon070fb212021-05-26 17:57:12 +09002174 (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002175 rc = -EINVAL;
2176 break;
2177 }
2178
2179 memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2180 memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002181 eabuf->EaNameLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09002182 attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0';
2183 value = (char *)&eabuf->name + eabuf->EaNameLength + 1;
2184
2185 if (!eabuf->EaValueLength) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002186 rc = ksmbd_vfs_casexattr_len(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002187 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002188 attr_name,
2189 XATTR_USER_PREFIX_LEN +
2190 eabuf->EaNameLength);
2191
2192 /* delete the EA only when it exits */
2193 if (rc > 0) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002194 rc = ksmbd_vfs_remove_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002195 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002196 attr_name);
2197
2198 if (rc < 0) {
2199 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002200 "remove xattr failed(%d)\n",
2201 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002202 break;
2203 }
2204 }
2205
2206 /* if the EA doesn't exist, just do nothing. */
2207 rc = 0;
2208 } else {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002209 rc = ksmbd_vfs_setxattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002210 path->dentry, attr_name, value,
Namjae Jeon070fb212021-05-26 17:57:12 +09002211 le16_to_cpu(eabuf->EaValueLength), 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002212 if (rc < 0) {
2213 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002214 "ksmbd_vfs_setxattr is failed(%d)\n",
2215 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002216 break;
2217 }
2218 }
2219
2220next:
2221 next = le32_to_cpu(eabuf->NextEntryOffset);
Namjae Jeon9496e262021-09-29 15:41:48 +09002222 if (next == 0 || buf_len < next)
2223 break;
2224 buf_len -= next;
Namjae Jeone2f34482021-03-16 10:49:09 +09002225 eabuf = (struct smb2_ea_info *)((char *)eabuf + next);
Namjae Jeon9496e262021-09-29 15:41:48 +09002226 if (next < (u32)eabuf->EaNameLength + le16_to_cpu(eabuf->EaValueLength))
2227 break;
2228
Namjae Jeone2f34482021-03-16 10:49:09 +09002229 } while (next != 0);
2230
2231 kfree(attr_name);
2232 return rc;
2233}
2234
Namjae Jeone2f34482021-03-16 10:49:09 +09002235static noinline int smb2_set_stream_name_xattr(struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002236 struct ksmbd_file *fp,
2237 char *stream_name, int s_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09002238{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002239 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002240 size_t xattr_stream_size;
2241 char *xattr_stream_name;
2242 int rc;
2243
2244 rc = ksmbd_vfs_xattr_stream_name(stream_name,
2245 &xattr_stream_name,
2246 &xattr_stream_size,
2247 s_type);
2248 if (rc)
2249 return rc;
2250
2251 fp->stream.name = xattr_stream_name;
2252 fp->stream.size = xattr_stream_size;
2253
2254 /* Check if there is stream prefix in xattr space */
Hyunchul Lee465d7202021-07-03 12:10:36 +09002255 rc = ksmbd_vfs_casexattr_len(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002256 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002257 xattr_stream_name,
2258 xattr_stream_size);
2259 if (rc >= 0)
2260 return 0;
2261
2262 if (fp->cdoption == FILE_OPEN_LE) {
2263 ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc);
2264 return -EBADF;
2265 }
2266
Hyunchul Lee465d7202021-07-03 12:10:36 +09002267 rc = ksmbd_vfs_setxattr(user_ns, path->dentry,
2268 xattr_stream_name, NULL, 0, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002269 if (rc < 0)
Namjae Jeonbde16942021-06-28 15:23:19 +09002270 pr_err("Failed to store XATTR stream name :%d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002271 return 0;
2272}
2273
Hyunchul Leeef24c962021-06-30 18:25:52 +09002274static int smb2_remove_smb_xattrs(struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002275{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002276 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002277 char *name, *xattr_list = NULL;
2278 ssize_t xattr_list_len;
2279 int err = 0;
2280
Hyunchul Leeef24c962021-06-30 18:25:52 +09002281 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002282 if (xattr_list_len < 0) {
2283 goto out;
2284 } else if (!xattr_list_len) {
2285 ksmbd_debug(SMB, "empty xattr in the file\n");
2286 goto out;
2287 }
2288
2289 for (name = xattr_list; name - xattr_list < xattr_list_len;
2290 name += strlen(name) + 1) {
2291 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
2292
2293 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002294 strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX,
2295 DOS_ATTRIBUTE_PREFIX_LEN) &&
2296 strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09002297 continue;
2298
Hyunchul Lee465d7202021-07-03 12:10:36 +09002299 err = ksmbd_vfs_remove_xattr(user_ns, path->dentry, name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002300 if (err)
2301 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
2302 }
2303out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09002304 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002305 return err;
2306}
2307
2308static int smb2_create_truncate(struct path *path)
2309{
2310 int rc = vfs_truncate(path, 0);
2311
2312 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002313 pr_err("vfs_truncate failed, rc %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002314 return rc;
2315 }
2316
Hyunchul Leeef24c962021-06-30 18:25:52 +09002317 rc = smb2_remove_smb_xattrs(path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002318 if (rc == -EOPNOTSUPP)
2319 rc = 0;
2320 if (rc)
2321 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002322 "ksmbd_truncate_stream_name_xattr failed, rc %d\n",
2323 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002324 return rc;
2325}
2326
Namjae Jeon64b39f42021-03-30 14:25:35 +09002327static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002328 struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002329{
2330 struct xattr_dos_attrib da = {0};
2331 int rc;
2332
2333 if (!test_share_config_flag(tcon->share_conf,
2334 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
2335 return;
2336
2337 da.version = 4;
2338 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
2339 da.itime = da.create_time = fp->create_time;
2340 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
2341 XATTR_DOSINFO_ITIME;
2342
Hyunchul Leeaf349832021-06-30 18:25:53 +09002343 rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt),
2344 path->dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09002345 if (rc)
2346 ksmbd_debug(SMB, "failed to store file attribute into xattr\n");
2347}
2348
2349static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon,
Namjae Jeon070fb212021-05-26 17:57:12 +09002350 struct path *path, struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002351{
2352 struct xattr_dos_attrib da;
2353 int rc;
2354
2355 fp->f_ci->m_fattr &= ~(ATTR_HIDDEN_LE | ATTR_SYSTEM_LE);
2356
2357 /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */
2358 if (!test_share_config_flag(tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002359 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
Namjae Jeone2f34482021-03-16 10:49:09 +09002360 return;
2361
Hyunchul Leeaf349832021-06-30 18:25:53 +09002362 rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_user_ns(path->mnt),
2363 path->dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09002364 if (rc > 0) {
2365 fp->f_ci->m_fattr = cpu_to_le32(da.attr);
2366 fp->create_time = da.create_time;
2367 fp->itime = da.itime;
2368 }
2369}
2370
Namjae Jeon64b39f42021-03-30 14:25:35 +09002371static int smb2_creat(struct ksmbd_work *work, struct path *path, char *name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002372 int open_flags, umode_t posix_mode, bool is_dir)
Namjae Jeone2f34482021-03-16 10:49:09 +09002373{
2374 struct ksmbd_tree_connect *tcon = work->tcon;
2375 struct ksmbd_share_config *share = tcon->share_conf;
2376 umode_t mode;
2377 int rc;
2378
2379 if (!(open_flags & O_CREAT))
2380 return -EBADF;
2381
2382 ksmbd_debug(SMB, "file does not exist, so creating\n");
2383 if (is_dir == true) {
2384 ksmbd_debug(SMB, "creating directory\n");
2385
2386 mode = share_config_directory_mode(share, posix_mode);
2387 rc = ksmbd_vfs_mkdir(work, name, mode);
2388 if (rc)
2389 return rc;
2390 } else {
2391 ksmbd_debug(SMB, "creating regular file\n");
2392
2393 mode = share_config_create_mode(share, posix_mode);
2394 rc = ksmbd_vfs_create(work, name, mode);
2395 if (rc)
2396 return rc;
2397 }
2398
Hyunchul Lee265fd192021-09-25 00:06:16 +09002399 rc = ksmbd_vfs_kern_path(work, name, 0, path, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002400 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002401 pr_err("cannot get linux path (%s), err = %d\n",
2402 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002403 return rc;
2404 }
2405 return 0;
2406}
2407
2408static int smb2_create_sd_buffer(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09002409 struct smb2_create_req *req,
Hyunchul Leeef24c962021-06-30 18:25:52 +09002410 struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002411{
2412 struct create_context *context;
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002413 struct create_sd_buf_req *sd_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09002414
2415 if (!req->CreateContextsOffset)
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002416 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09002417
2418 /* Parse SD BUFFER create contexts */
2419 context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER);
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002420 if (!context)
2421 return -ENOENT;
2422 else if (IS_ERR(context))
2423 return PTR_ERR(context);
Namjae Jeone2f34482021-03-16 10:49:09 +09002424
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002425 ksmbd_debug(SMB,
2426 "Set ACLs using SMB2_CREATE_SD_BUFFER context\n");
2427 sd_buf = (struct create_sd_buf_req *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002428 if (le16_to_cpu(context->DataOffset) +
2429 le32_to_cpu(context->DataLength) <
2430 sizeof(struct create_sd_buf_req))
2431 return -EINVAL;
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002432 return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd,
2433 le32_to_cpu(sd_buf->ccontext.DataLength), true);
Namjae Jeone2f34482021-03-16 10:49:09 +09002434}
2435
Christian Brauner43205ca2021-08-23 17:13:50 +02002436static void ksmbd_acls_fattr(struct smb_fattr *fattr,
2437 struct user_namespace *mnt_userns,
2438 struct inode *inode)
Namjae Jeon3d47e542021-04-20 14:25:35 +09002439{
Christian Brauner43205ca2021-08-23 17:13:50 +02002440 fattr->cf_uid = i_uid_into_mnt(mnt_userns, inode);
2441 fattr->cf_gid = i_gid_into_mnt(mnt_userns, inode);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002442 fattr->cf_mode = inode->i_mode;
Namjae Jeon777cad12021-08-13 08:15:33 +09002443 fattr->cf_acls = NULL;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002444 fattr->cf_dacls = NULL;
2445
Namjae Jeon777cad12021-08-13 08:15:33 +09002446 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
2447 fattr->cf_acls = get_acl(inode, ACL_TYPE_ACCESS);
2448 if (S_ISDIR(inode->i_mode))
2449 fattr->cf_dacls = get_acl(inode, ACL_TYPE_DEFAULT);
2450 }
Namjae Jeon3d47e542021-04-20 14:25:35 +09002451}
2452
Namjae Jeone2f34482021-03-16 10:49:09 +09002453/**
2454 * smb2_open() - handler for smb file open request
2455 * @work: smb work containing request buffer
2456 *
2457 * Return: 0 on success, otherwise error
2458 */
2459int smb2_open(struct ksmbd_work *work)
2460{
2461 struct ksmbd_conn *conn = work->conn;
2462 struct ksmbd_session *sess = work->sess;
2463 struct ksmbd_tree_connect *tcon = work->tcon;
2464 struct smb2_create_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09002465 struct smb2_create_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09002466 struct path path;
2467 struct ksmbd_share_config *share = tcon->share_conf;
2468 struct ksmbd_file *fp = NULL;
2469 struct file *filp = NULL;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002470 struct user_namespace *user_ns = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002471 struct kstat stat;
2472 struct create_context *context;
2473 struct lease_ctx_info *lc = NULL;
2474 struct create_ea_buf_req *ea_buf = NULL;
2475 struct oplock_info *opinfo;
2476 __le32 *next_ptr = NULL;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002477 int req_op_level = 0, open_flags = 0, may_flags = 0, file_info = 0;
Hyunchul Lee265fd192021-09-25 00:06:16 +09002478 int rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002479 int contxt_cnt = 0, query_disk_id = 0;
2480 int maximal_access_ctxt = 0, posix_ctxt = 0;
2481 int s_type = 0;
2482 int next_off = 0;
2483 char *name = NULL;
2484 char *stream_name = NULL;
2485 bool file_present = false, created = false, already_permitted = false;
Namjae Jeone2f34482021-03-16 10:49:09 +09002486 int share_ret, need_truncate = 0;
2487 u64 time;
2488 umode_t posix_mode = 0;
2489 __le32 daccess, maximal_access = 0;
2490
Namjae Jeone2f34482021-03-16 10:49:09 +09002491 WORK_BUFFERS(work, req, rsp);
2492
2493 if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002494 (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002495 ksmbd_debug(SMB, "invalid flag in chained command\n");
2496 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2497 smb2_set_err_rsp(work);
2498 return -EINVAL;
2499 }
2500
2501 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
2502 ksmbd_debug(SMB, "IPC pipe create request\n");
2503 return create_smb2_pipe(work);
2504 }
2505
2506 if (req->NameLength) {
2507 if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002508 *(char *)req->Buffer == '\\') {
Namjae Jeonbde16942021-06-28 15:23:19 +09002509 pr_err("not allow directory name included leading slash\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002510 rc = -EINVAL;
2511 goto err_out1;
2512 }
2513
2514 name = smb2_get_name(share,
2515 req->Buffer,
2516 le16_to_cpu(req->NameLength),
2517 work->conn->local_nls);
2518 if (IS_ERR(name)) {
2519 rc = PTR_ERR(name);
2520 if (rc != -ENOMEM)
2521 rc = -ENOENT;
Dan Carpenter8b99f352021-08-02 08:14:03 +09002522 name = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002523 goto err_out1;
2524 }
2525
2526 ksmbd_debug(SMB, "converted name = %s\n", name);
2527 if (strchr(name, ':')) {
2528 if (!test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002529 KSMBD_SHARE_FLAG_STREAMS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002530 rc = -EBADF;
2531 goto err_out1;
2532 }
2533 rc = parse_stream_name(name, &stream_name, &s_type);
2534 if (rc < 0)
2535 goto err_out1;
2536 }
2537
2538 rc = ksmbd_validate_filename(name);
2539 if (rc < 0)
2540 goto err_out1;
2541
2542 if (ksmbd_share_veto_filename(share, name)) {
2543 rc = -ENOENT;
2544 ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002545 name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002546 goto err_out1;
2547 }
2548 } else {
Hyunchul Lee265fd192021-09-25 00:06:16 +09002549 name = kstrdup("", GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09002550 if (!name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002551 rc = -ENOMEM;
2552 goto err_out1;
2553 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002554 }
2555
2556 req_op_level = req->RequestedOplockLevel;
Namjae Jeon73f9dad2021-04-16 14:12:06 +09002557 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002558 lc = parse_lease_state(req);
Namjae Jeone2f34482021-03-16 10:49:09 +09002559
Namjae Jeon64b39f42021-03-30 14:25:35 +09002560 if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002561 pr_err("Invalid impersonationlevel : 0x%x\n",
2562 le32_to_cpu(req->ImpersonationLevel));
Namjae Jeone2f34482021-03-16 10:49:09 +09002563 rc = -EIO;
2564 rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL;
2565 goto err_out1;
2566 }
2567
2568 if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002569 pr_err("Invalid create options : 0x%x\n",
2570 le32_to_cpu(req->CreateOptions));
Namjae Jeone2f34482021-03-16 10:49:09 +09002571 rc = -EINVAL;
2572 goto err_out1;
2573 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09002574 if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002575 req->CreateOptions & FILE_RANDOM_ACCESS_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002576 req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE);
2577
Namjae Jeon070fb212021-05-26 17:57:12 +09002578 if (req->CreateOptions &
2579 (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION |
2580 FILE_RESERVE_OPFILTER_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002581 rc = -EOPNOTSUPP;
2582 goto err_out1;
2583 }
2584
2585 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2586 if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) {
2587 rc = -EINVAL;
2588 goto err_out1;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002589 } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002590 req->CreateOptions = ~(FILE_NO_COMPRESSION_LE);
Namjae Jeon64b39f42021-03-30 14:25:35 +09002591 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002592 }
2593 }
2594
2595 if (le32_to_cpu(req->CreateDisposition) >
Namjae Jeon070fb212021-05-26 17:57:12 +09002596 le32_to_cpu(FILE_OVERWRITE_IF_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002597 pr_err("Invalid create disposition : 0x%x\n",
2598 le32_to_cpu(req->CreateDisposition));
Namjae Jeone2f34482021-03-16 10:49:09 +09002599 rc = -EINVAL;
2600 goto err_out1;
2601 }
2602
2603 if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002604 pr_err("Invalid desired access : 0x%x\n",
2605 le32_to_cpu(req->DesiredAccess));
Namjae Jeone2f34482021-03-16 10:49:09 +09002606 rc = -EACCES;
2607 goto err_out1;
2608 }
2609
Namjae Jeon64b39f42021-03-30 14:25:35 +09002610 if (req->FileAttributes && !(req->FileAttributes & ATTR_MASK_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002611 pr_err("Invalid file attribute : 0x%x\n",
2612 le32_to_cpu(req->FileAttributes));
Namjae Jeone2f34482021-03-16 10:49:09 +09002613 rc = -EINVAL;
2614 goto err_out1;
2615 }
2616
2617 if (req->CreateContextsOffset) {
2618 /* Parse non-durable handle create contexts */
2619 context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002620 if (IS_ERR(context)) {
2621 rc = PTR_ERR(context);
2622 goto err_out1;
2623 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002624 ea_buf = (struct create_ea_buf_req *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002625 if (le16_to_cpu(context->DataOffset) +
2626 le32_to_cpu(context->DataLength) <
2627 sizeof(struct create_ea_buf_req)) {
2628 rc = -EINVAL;
2629 goto err_out1;
2630 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002631 if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) {
2632 rsp->hdr.Status = STATUS_ACCESS_DENIED;
2633 rc = -EACCES;
2634 goto err_out1;
2635 }
2636 }
2637
2638 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002639 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002640 if (IS_ERR(context)) {
2641 rc = PTR_ERR(context);
2642 goto err_out1;
2643 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002644 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002645 "get query maximal access context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002646 maximal_access_ctxt = 1;
2647 }
2648
2649 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002650 SMB2_CREATE_TIMEWARP_REQUEST);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002651 if (IS_ERR(context)) {
2652 rc = PTR_ERR(context);
2653 goto err_out1;
2654 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002655 ksmbd_debug(SMB, "get timewarp context\n");
2656 rc = -EBADF;
2657 goto err_out1;
2658 }
2659
2660 if (tcon->posix_extensions) {
2661 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002662 SMB2_CREATE_TAG_POSIX);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002663 if (IS_ERR(context)) {
2664 rc = PTR_ERR(context);
2665 goto err_out1;
2666 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002667 struct create_posix *posix =
2668 (struct create_posix *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002669 if (le16_to_cpu(context->DataOffset) +
2670 le32_to_cpu(context->DataLength) <
2671 sizeof(struct create_posix)) {
2672 rc = -EINVAL;
2673 goto err_out1;
2674 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002675 ksmbd_debug(SMB, "get posix context\n");
2676
2677 posix_mode = le32_to_cpu(posix->Mode);
2678 posix_ctxt = 1;
2679 }
2680 }
2681 }
2682
2683 if (ksmbd_override_fsids(work)) {
2684 rc = -ENOMEM;
2685 goto err_out1;
2686 }
2687
Hyunchul Lee265fd192021-09-25 00:06:16 +09002688 rc = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, 1);
Namjae Jeon4ea47792021-09-21 14:19:33 +09002689 if (!rc) {
2690 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002691 /*
2692 * If file exists with under flags, return access
2693 * denied error.
2694 */
2695 if (req->CreateDisposition == FILE_OVERWRITE_IF_LE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09002696 req->CreateDisposition == FILE_OPEN_IF_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002697 rc = -EACCES;
2698 path_put(&path);
2699 goto err_out;
2700 }
2701
Namjae Jeon64b39f42021-03-30 14:25:35 +09002702 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002703 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002704 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002705 rc = -EACCES;
2706 path_put(&path);
2707 goto err_out;
2708 }
Namjae Jeon4ea47792021-09-21 14:19:33 +09002709 } else if (d_is_symlink(path.dentry)) {
2710 rc = -EACCES;
2711 path_put(&path);
2712 goto err_out;
Namjae Jeone2f34482021-03-16 10:49:09 +09002713 }
2714 }
2715
2716 if (rc) {
Hyunchul Lee265fd192021-09-25 00:06:16 +09002717 if (rc != -ENOENT)
Namjae Jeone2f34482021-03-16 10:49:09 +09002718 goto err_out;
Namjae Jeone2f34482021-03-16 10:49:09 +09002719 ksmbd_debug(SMB, "can not get linux path for %s, rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002720 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002721 rc = 0;
2722 } else {
2723 file_present = true;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002724 user_ns = mnt_user_ns(path.mnt);
2725 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09002726 }
2727 if (stream_name) {
2728 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2729 if (s_type == DATA_STREAM) {
2730 rc = -EIO;
2731 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2732 }
2733 } else {
2734 if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) {
2735 rc = -EIO;
2736 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2737 }
2738 }
2739
2740 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002741 req->FileAttributes & ATTR_NORMAL_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002742 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2743 rc = -EIO;
2744 }
2745
2746 if (rc < 0)
2747 goto err_out;
2748 }
2749
Namjae Jeon64b39f42021-03-30 14:25:35 +09002750 if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE &&
2751 S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002752 ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002753 name, req->CreateOptions);
Namjae Jeone2f34482021-03-16 10:49:09 +09002754 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2755 rc = -EIO;
2756 goto err_out;
2757 }
2758
2759 if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002760 !(req->CreateDisposition == FILE_CREATE_LE) &&
2761 !S_ISDIR(stat.mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002762 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2763 rc = -EIO;
2764 goto err_out;
2765 }
2766
2767 if (!stream_name && file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002768 req->CreateDisposition == FILE_CREATE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002769 rc = -EEXIST;
2770 goto err_out;
2771 }
2772
Namjae Jeone2f34482021-03-16 10:49:09 +09002773 daccess = smb_map_generic_desired_access(req->DesiredAccess);
2774
2775 if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002776 rc = smb_check_perm_dacl(conn, &path, &daccess,
Namjae Jeon070fb212021-05-26 17:57:12 +09002777 sess->user->uid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002778 if (rc)
2779 goto err_out;
2780 }
2781
2782 if (daccess & FILE_MAXIMAL_ACCESS_LE) {
2783 if (!file_present) {
2784 daccess = cpu_to_le32(GENERIC_ALL_FLAGS);
2785 } else {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002786 rc = ksmbd_vfs_query_maximal_access(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002787 path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002788 &daccess);
2789 if (rc)
2790 goto err_out;
2791 already_permitted = true;
2792 }
2793 maximal_access = daccess;
2794 }
2795
Namjae Jeon070fb212021-05-26 17:57:12 +09002796 open_flags = smb2_create_open_flags(file_present, daccess,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002797 req->CreateDisposition,
2798 &may_flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09002799
2800 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
2801 if (open_flags & O_CREAT) {
2802 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002803 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002804 rc = -EACCES;
2805 goto err_out;
2806 }
2807 }
2808
2809 /*create file if not present */
2810 if (!file_present) {
2811 rc = smb2_creat(work, &path, name, open_flags, posix_mode,
Namjae Jeon070fb212021-05-26 17:57:12 +09002812 req->CreateOptions & FILE_DIRECTORY_FILE_LE);
Marios Makassikisd337a442021-07-27 09:24:51 +09002813 if (rc) {
2814 if (rc == -ENOENT) {
2815 rc = -EIO;
2816 rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND;
2817 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002818 goto err_out;
Marios Makassikisd337a442021-07-27 09:24:51 +09002819 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002820
2821 created = true;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002822 user_ns = mnt_user_ns(path.mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002823 if (ea_buf) {
Namjae Jeon9496e262021-09-29 15:41:48 +09002824 if (le32_to_cpu(ea_buf->ccontext.DataLength) <
2825 sizeof(struct smb2_ea_info)) {
2826 rc = -EINVAL;
2827 goto err_out;
2828 }
2829
2830 rc = smb2_set_ea(&ea_buf->ea,
2831 le32_to_cpu(ea_buf->ccontext.DataLength),
2832 &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002833 if (rc == -EOPNOTSUPP)
2834 rc = 0;
2835 else if (rc)
2836 goto err_out;
2837 }
2838 } else if (!already_permitted) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002839 /* FILE_READ_ATTRIBUTE is allowed without inode_permission,
2840 * because execute(search) permission on a parent directory,
2841 * is already granted.
2842 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09002843 if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002844 rc = inode_permission(user_ns,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002845 d_inode(path.dentry),
2846 may_flags);
Namjae Jeonff1d5722021-04-13 13:18:10 +09002847 if (rc)
Namjae Jeone2f34482021-03-16 10:49:09 +09002848 goto err_out;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002849
2850 if ((daccess & FILE_DELETE_LE) ||
2851 (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002852 rc = ksmbd_vfs_may_delete(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002853 path.dentry);
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002854 if (rc)
2855 goto err_out;
2856 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002857 }
2858 }
2859
2860 rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent));
2861 if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) {
2862 rc = -EBUSY;
2863 goto err_out;
2864 }
2865
2866 rc = 0;
2867 filp = dentry_open(&path, open_flags, current_cred());
2868 if (IS_ERR(filp)) {
2869 rc = PTR_ERR(filp);
Namjae Jeonbde16942021-06-28 15:23:19 +09002870 pr_err("dentry open for dir failed, rc %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002871 goto err_out;
2872 }
2873
2874 if (file_present) {
2875 if (!(open_flags & O_TRUNC))
2876 file_info = FILE_OPENED;
2877 else
2878 file_info = FILE_OVERWRITTEN;
2879
Namjae Jeon070fb212021-05-26 17:57:12 +09002880 if ((req->CreateDisposition & FILE_CREATE_MASK_LE) ==
2881 FILE_SUPERSEDE_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002882 file_info = FILE_SUPERSEDED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002883 } else if (open_flags & O_CREAT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002884 file_info = FILE_CREATED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002885 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002886
2887 ksmbd_vfs_set_fadvise(filp, req->CreateOptions);
2888
2889 /* Obtain Volatile-ID */
2890 fp = ksmbd_open_fd(work, filp);
2891 if (IS_ERR(fp)) {
2892 fput(filp);
2893 rc = PTR_ERR(fp);
2894 fp = NULL;
2895 goto err_out;
2896 }
2897
2898 /* Get Persistent-ID */
2899 ksmbd_open_durable_fd(fp);
Namjae Jeon38673692021-07-08 12:32:27 +09002900 if (!has_file_id(fp->persistent_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002901 rc = -ENOMEM;
2902 goto err_out;
2903 }
2904
2905 fp->filename = name;
2906 fp->cdoption = req->CreateDisposition;
2907 fp->daccess = daccess;
2908 fp->saccess = req->ShareAccess;
2909 fp->coption = req->CreateOptions;
2910
2911 /* Set default windows and posix acls if creating new file */
2912 if (created) {
2913 int posix_acl_rc;
Namjae Jeonfba08fa2021-04-15 10:29:39 +09002914 struct inode *inode = d_inode(path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09002915
Hyunchul Lee465d7202021-07-03 12:10:36 +09002916 posix_acl_rc = ksmbd_vfs_inherit_posix_acl(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002917 inode,
2918 d_inode(path.dentry->d_parent));
Namjae Jeone2f34482021-03-16 10:49:09 +09002919 if (posix_acl_rc)
2920 ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc);
2921
2922 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002923 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002924 rc = smb_inherit_dacl(conn, &path, sess->user->uid,
Namjae Jeon070fb212021-05-26 17:57:12 +09002925 sess->user->gid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002926 }
2927
2928 if (rc) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002929 rc = smb2_create_sd_buffer(work, req, &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002930 if (rc) {
2931 if (posix_acl_rc)
Hyunchul Lee465d7202021-07-03 12:10:36 +09002932 ksmbd_vfs_set_init_posix_acl(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002933 inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09002934
2935 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002936 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002937 struct smb_fattr fattr;
2938 struct smb_ntsd *pntsd;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002939 int pntsd_size, ace_num = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002940
Christian Brauner43205ca2021-08-23 17:13:50 +02002941 ksmbd_acls_fattr(&fattr, user_ns, inode);
Marios Makassikise6b10592021-04-15 10:24:56 +09002942 if (fattr.cf_acls)
2943 ace_num = fattr.cf_acls->a_count;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002944 if (fattr.cf_dacls)
2945 ace_num += fattr.cf_dacls->a_count;
Namjae Jeone2f34482021-03-16 10:49:09 +09002946
2947 pntsd = kmalloc(sizeof(struct smb_ntsd) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002948 sizeof(struct smb_sid) * 3 +
Namjae Jeone2f34482021-03-16 10:49:09 +09002949 sizeof(struct smb_acl) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002950 sizeof(struct smb_ace) * ace_num * 2,
Namjae Jeone2f34482021-03-16 10:49:09 +09002951 GFP_KERNEL);
2952 if (!pntsd)
2953 goto err_out;
2954
Hyunchul Lee465d7202021-07-03 12:10:36 +09002955 rc = build_sec_desc(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002956 pntsd, NULL,
Namjae Jeon070fb212021-05-26 17:57:12 +09002957 OWNER_SECINFO |
Hyunchul Leeaf349832021-06-30 18:25:53 +09002958 GROUP_SECINFO |
2959 DACL_SECINFO,
Namjae Jeon070fb212021-05-26 17:57:12 +09002960 &pntsd_size, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09002961 posix_acl_release(fattr.cf_acls);
2962 posix_acl_release(fattr.cf_dacls);
2963
2964 rc = ksmbd_vfs_set_sd_xattr(conn,
Hyunchul Lee465d7202021-07-03 12:10:36 +09002965 user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +09002966 path.dentry,
2967 pntsd,
2968 pntsd_size);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002969 kfree(pntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09002970 if (rc)
Namjae Jeonbde16942021-06-28 15:23:19 +09002971 pr_err("failed to store ntacl in xattr : %d\n",
2972 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002973 }
2974 }
2975 }
2976 rc = 0;
2977 }
2978
2979 if (stream_name) {
2980 rc = smb2_set_stream_name_xattr(&path,
2981 fp,
2982 stream_name,
2983 s_type);
2984 if (rc)
2985 goto err_out;
2986 file_info = FILE_CREATED;
2987 }
2988
2989 fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE |
2990 FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE));
Namjae Jeon64b39f42021-03-30 14:25:35 +09002991 if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC &&
2992 !fp->attrib_only && !stream_name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002993 smb_break_all_oplock(work, fp);
2994 need_truncate = 1;
2995 }
2996
2997 /* fp should be searchable through ksmbd_inode.m_fp_list
2998 * after daccess, saccess, attrib_only, and stream are
2999 * initialized.
3000 */
3001 write_lock(&fp->f_ci->m_lock);
3002 list_add(&fp->node, &fp->f_ci->m_fp_list);
3003 write_unlock(&fp->f_ci->m_lock);
3004
3005 rc = ksmbd_vfs_getattr(&path, &stat);
3006 if (rc) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09003007 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09003008 rc = 0;
3009 }
3010
3011 /* Check delete pending among previous fp before oplock break */
3012 if (ksmbd_inode_pending_delete(fp)) {
3013 rc = -EBUSY;
3014 goto err_out;
3015 }
3016
3017 share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp);
Namjae Jeon64b39f42021-03-30 14:25:35 +09003018 if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) ||
3019 (req_op_level == SMB2_OPLOCK_LEVEL_LEASE &&
3020 !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) {
Namjae Jeonab0b2632021-06-29 09:20:13 +09003021 if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003022 rc = share_ret;
3023 goto err_out;
3024 }
3025 } else {
3026 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) {
3027 req_op_level = smb2_map_lease_to_oplock(lc->req_state);
3028 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003029 "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n",
3030 name, req_op_level, lc->req_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09003031 rc = find_same_lease_key(sess, fp->f_ci, lc);
3032 if (rc)
3033 goto err_out;
3034 } else if (open_flags == O_RDONLY &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09003035 (req_op_level == SMB2_OPLOCK_LEVEL_BATCH ||
3036 req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
Namjae Jeone2f34482021-03-16 10:49:09 +09003037 req_op_level = SMB2_OPLOCK_LEVEL_II;
3038
3039 rc = smb_grant_oplock(work, req_op_level,
3040 fp->persistent_id, fp,
3041 le32_to_cpu(req->hdr.Id.SyncId.TreeId),
3042 lc, share_ret);
3043 if (rc < 0)
3044 goto err_out;
3045 }
3046
3047 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
3048 ksmbd_fd_set_delete_on_close(fp, file_info);
3049
3050 if (need_truncate) {
3051 rc = smb2_create_truncate(&path);
3052 if (rc)
3053 goto err_out;
3054 }
3055
3056 if (req->CreateContextsOffset) {
3057 struct create_alloc_size_req *az_req;
3058
Namjae Jeon070fb212021-05-26 17:57:12 +09003059 az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req,
3060 SMB2_CREATE_ALLOCATION_SIZE);
Namjae Jeonf19b3962021-07-13 09:59:34 +09003061 if (IS_ERR(az_req)) {
3062 rc = PTR_ERR(az_req);
3063 goto err_out;
3064 } else if (az_req) {
Hyunchul Lee8f771502021-09-24 22:22:22 +09003065 loff_t alloc_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09003066 int err;
3067
Hyunchul Lee8f771502021-09-24 22:22:22 +09003068 if (le16_to_cpu(az_req->ccontext.DataOffset) +
3069 le32_to_cpu(az_req->ccontext.DataLength) <
3070 sizeof(struct create_alloc_size_req)) {
3071 rc = -EINVAL;
3072 goto err_out;
3073 }
3074 alloc_size = le64_to_cpu(az_req->AllocationSize);
Namjae Jeone2f34482021-03-16 10:49:09 +09003075 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003076 "request smb2 create allocate size : %llu\n",
3077 alloc_size);
Namjae Jeone8c06192021-06-22 11:06:11 +09003078 smb_break_all_levII_oplock(work, fp, 1);
3079 err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
3080 alloc_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003081 if (err < 0)
3082 ksmbd_debug(SMB,
Namjae Jeone8c06192021-06-22 11:06:11 +09003083 "vfs_fallocate is failed : %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003084 err);
Namjae Jeone2f34482021-03-16 10:49:09 +09003085 }
3086
Namjae Jeon64b39f42021-03-30 14:25:35 +09003087 context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID);
Namjae Jeonf19b3962021-07-13 09:59:34 +09003088 if (IS_ERR(context)) {
3089 rc = PTR_ERR(context);
3090 goto err_out;
3091 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003092 ksmbd_debug(SMB, "get query on disk id context\n");
3093 query_disk_id = 1;
3094 }
3095 }
3096
3097 if (stat.result_mask & STATX_BTIME)
3098 fp->create_time = ksmbd_UnixTimeToNT(stat.btime);
3099 else
3100 fp->create_time = ksmbd_UnixTimeToNT(stat.ctime);
3101 if (req->FileAttributes || fp->f_ci->m_fattr == 0)
Namjae Jeon070fb212021-05-26 17:57:12 +09003102 fp->f_ci->m_fattr =
3103 cpu_to_le32(smb2_get_dos_mode(&stat, le32_to_cpu(req->FileAttributes)));
Namjae Jeone2f34482021-03-16 10:49:09 +09003104
3105 if (!created)
3106 smb2_update_xattrs(tcon, &path, fp);
3107 else
3108 smb2_new_xattrs(tcon, &path, fp);
3109
3110 memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE);
3111
Hyunchul Lee465d7202021-07-03 12:10:36 +09003112 generic_fillattr(user_ns, file_inode(fp->filp),
Hyunchul Leeaf349832021-06-30 18:25:53 +09003113 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09003114
3115 rsp->StructureSize = cpu_to_le16(89);
3116 rcu_read_lock();
3117 opinfo = rcu_dereference(fp->f_opinfo);
3118 rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0;
3119 rcu_read_unlock();
3120 rsp->Reserved = 0;
3121 rsp->CreateAction = cpu_to_le32(file_info);
3122 rsp->CreationTime = cpu_to_le64(fp->create_time);
3123 time = ksmbd_UnixTimeToNT(stat.atime);
3124 rsp->LastAccessTime = cpu_to_le64(time);
3125 time = ksmbd_UnixTimeToNT(stat.mtime);
3126 rsp->LastWriteTime = cpu_to_le64(time);
3127 time = ksmbd_UnixTimeToNT(stat.ctime);
3128 rsp->ChangeTime = cpu_to_le64(time);
3129 rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 :
3130 cpu_to_le64(stat.blocks << 9);
3131 rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
3132 rsp->FileAttributes = fp->f_ci->m_fattr;
3133
3134 rsp->Reserved2 = 0;
3135
3136 rsp->PersistentFileId = cpu_to_le64(fp->persistent_id);
3137 rsp->VolatileFileId = cpu_to_le64(fp->volatile_id);
3138
3139 rsp->CreateContextsOffset = 0;
3140 rsp->CreateContextsLength = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09003141 inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/
Namjae Jeone2f34482021-03-16 10:49:09 +09003142
3143 /* If lease is request send lease context response */
3144 if (opinfo && opinfo->is_lease) {
3145 struct create_context *lease_ccontext;
3146
3147 ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003148 name, opinfo->o_lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +09003149 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
3150
3151 lease_ccontext = (struct create_context *)rsp->Buffer;
3152 contxt_cnt++;
3153 create_lease_buf(rsp->Buffer, opinfo->o_lease);
3154 le32_add_cpu(&rsp->CreateContextsLength,
3155 conn->vals->create_lease_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003156 inc_rfc1001_len(work->response_buf,
3157 conn->vals->create_lease_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003158 next_ptr = &lease_ccontext->Next;
3159 next_off = conn->vals->create_lease_size;
3160 }
3161
Namjae Jeone2f34482021-03-16 10:49:09 +09003162 if (maximal_access_ctxt) {
3163 struct create_context *mxac_ccontext;
3164
3165 if (maximal_access == 0)
Hyunchul Lee465d7202021-07-03 12:10:36 +09003166 ksmbd_vfs_query_maximal_access(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09003167 path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09003168 &maximal_access);
3169 mxac_ccontext = (struct create_context *)(rsp->Buffer +
3170 le32_to_cpu(rsp->CreateContextsLength));
3171 contxt_cnt++;
3172 create_mxac_rsp_buf(rsp->Buffer +
3173 le32_to_cpu(rsp->CreateContextsLength),
3174 le32_to_cpu(maximal_access));
3175 le32_add_cpu(&rsp->CreateContextsLength,
3176 conn->vals->create_mxac_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003177 inc_rfc1001_len(work->response_buf,
3178 conn->vals->create_mxac_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003179 if (next_ptr)
3180 *next_ptr = cpu_to_le32(next_off);
3181 next_ptr = &mxac_ccontext->Next;
3182 next_off = conn->vals->create_mxac_size;
3183 }
3184
3185 if (query_disk_id) {
3186 struct create_context *disk_id_ccontext;
3187
3188 disk_id_ccontext = (struct create_context *)(rsp->Buffer +
3189 le32_to_cpu(rsp->CreateContextsLength));
3190 contxt_cnt++;
3191 create_disk_id_rsp_buf(rsp->Buffer +
3192 le32_to_cpu(rsp->CreateContextsLength),
3193 stat.ino, tcon->id);
3194 le32_add_cpu(&rsp->CreateContextsLength,
3195 conn->vals->create_disk_id_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003196 inc_rfc1001_len(work->response_buf,
3197 conn->vals->create_disk_id_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003198 if (next_ptr)
3199 *next_ptr = cpu_to_le32(next_off);
3200 next_ptr = &disk_id_ccontext->Next;
3201 next_off = conn->vals->create_disk_id_size;
3202 }
3203
3204 if (posix_ctxt) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003205 contxt_cnt++;
3206 create_posix_rsp_buf(rsp->Buffer +
3207 le32_to_cpu(rsp->CreateContextsLength),
3208 fp);
3209 le32_add_cpu(&rsp->CreateContextsLength,
3210 conn->vals->create_posix_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003211 inc_rfc1001_len(work->response_buf,
3212 conn->vals->create_posix_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003213 if (next_ptr)
3214 *next_ptr = cpu_to_le32(next_off);
3215 }
3216
3217 if (contxt_cnt > 0) {
3218 rsp->CreateContextsOffset =
Namjae Jeoncb451722021-11-03 08:08:44 +09003219 cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer));
Namjae Jeone2f34482021-03-16 10:49:09 +09003220 }
3221
3222err_out:
3223 if (file_present || created)
3224 path_put(&path);
3225 ksmbd_revert_fsids(work);
3226err_out1:
3227 if (rc) {
3228 if (rc == -EINVAL)
3229 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3230 else if (rc == -EOPNOTSUPP)
3231 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Hyunchul Lee265fd192021-09-25 00:06:16 +09003232 else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV)
Namjae Jeone2f34482021-03-16 10:49:09 +09003233 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3234 else if (rc == -ENOENT)
3235 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
3236 else if (rc == -EPERM)
3237 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
3238 else if (rc == -EBUSY)
3239 rsp->hdr.Status = STATUS_DELETE_PENDING;
3240 else if (rc == -EBADF)
3241 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
3242 else if (rc == -ENOEXEC)
3243 rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID;
3244 else if (rc == -ENXIO)
3245 rsp->hdr.Status = STATUS_NO_SUCH_DEVICE;
3246 else if (rc == -EEXIST)
3247 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
3248 else if (rc == -EMFILE)
3249 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
3250 if (!rsp->hdr.Status)
3251 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3252
3253 if (!fp || !fp->filename)
3254 kfree(name);
3255 if (fp)
3256 ksmbd_fd_put(work, fp);
3257 smb2_set_err_rsp(work);
3258 ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status);
3259 }
3260
3261 kfree(lc);
3262
3263 return 0;
3264}
3265
3266static int readdir_info_level_struct_sz(int info_level)
3267{
3268 switch (info_level) {
3269 case FILE_FULL_DIRECTORY_INFORMATION:
3270 return sizeof(struct file_full_directory_info);
3271 case FILE_BOTH_DIRECTORY_INFORMATION:
3272 return sizeof(struct file_both_directory_info);
3273 case FILE_DIRECTORY_INFORMATION:
3274 return sizeof(struct file_directory_info);
3275 case FILE_NAMES_INFORMATION:
3276 return sizeof(struct file_names_info);
3277 case FILEID_FULL_DIRECTORY_INFORMATION:
3278 return sizeof(struct file_id_full_dir_info);
3279 case FILEID_BOTH_DIRECTORY_INFORMATION:
3280 return sizeof(struct file_id_both_directory_info);
3281 case SMB_FIND_FILE_POSIX_INFO:
3282 return sizeof(struct smb2_posix_info);
3283 default:
3284 return -EOPNOTSUPP;
3285 }
3286}
3287
3288static int dentry_name(struct ksmbd_dir_info *d_info, int info_level)
3289{
3290 switch (info_level) {
3291 case FILE_FULL_DIRECTORY_INFORMATION:
3292 {
3293 struct file_full_directory_info *ffdinfo;
3294
3295 ffdinfo = (struct file_full_directory_info *)d_info->rptr;
3296 d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset);
3297 d_info->name = ffdinfo->FileName;
3298 d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength);
3299 return 0;
3300 }
3301 case FILE_BOTH_DIRECTORY_INFORMATION:
3302 {
3303 struct file_both_directory_info *fbdinfo;
3304
3305 fbdinfo = (struct file_both_directory_info *)d_info->rptr;
3306 d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset);
3307 d_info->name = fbdinfo->FileName;
3308 d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength);
3309 return 0;
3310 }
3311 case FILE_DIRECTORY_INFORMATION:
3312 {
3313 struct file_directory_info *fdinfo;
3314
3315 fdinfo = (struct file_directory_info *)d_info->rptr;
3316 d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset);
3317 d_info->name = fdinfo->FileName;
3318 d_info->name_len = le32_to_cpu(fdinfo->FileNameLength);
3319 return 0;
3320 }
3321 case FILE_NAMES_INFORMATION:
3322 {
3323 struct file_names_info *fninfo;
3324
3325 fninfo = (struct file_names_info *)d_info->rptr;
3326 d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset);
3327 d_info->name = fninfo->FileName;
3328 d_info->name_len = le32_to_cpu(fninfo->FileNameLength);
3329 return 0;
3330 }
3331 case FILEID_FULL_DIRECTORY_INFORMATION:
3332 {
3333 struct file_id_full_dir_info *dinfo;
3334
3335 dinfo = (struct file_id_full_dir_info *)d_info->rptr;
3336 d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset);
3337 d_info->name = dinfo->FileName;
3338 d_info->name_len = le32_to_cpu(dinfo->FileNameLength);
3339 return 0;
3340 }
3341 case FILEID_BOTH_DIRECTORY_INFORMATION:
3342 {
3343 struct file_id_both_directory_info *fibdinfo;
3344
3345 fibdinfo = (struct file_id_both_directory_info *)d_info->rptr;
3346 d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset);
3347 d_info->name = fibdinfo->FileName;
3348 d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength);
3349 return 0;
3350 }
3351 case SMB_FIND_FILE_POSIX_INFO:
3352 {
3353 struct smb2_posix_info *posix_info;
3354
3355 posix_info = (struct smb2_posix_info *)d_info->rptr;
3356 d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset);
3357 d_info->name = posix_info->name;
3358 d_info->name_len = le32_to_cpu(posix_info->name_len);
3359 return 0;
3360 }
3361 default:
3362 return -EINVAL;
3363 }
3364}
3365
3366/**
3367 * smb2_populate_readdir_entry() - encode directory entry in smb2 response
3368 * buffer
3369 * @conn: connection instance
3370 * @info_level: smb information level
3371 * @d_info: structure included variables for query dir
Hyunchul Leeaf349832021-06-30 18:25:53 +09003372 * @user_ns: user namespace
Namjae Jeone2f34482021-03-16 10:49:09 +09003373 * @ksmbd_kstat: ksmbd wrapper of dirent stat information
3374 *
3375 * if directory has many entries, find first can't read it fully.
3376 * find next might be called multiple times to read remaining dir entries
3377 *
3378 * Return: 0 on success, otherwise error
3379 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003380static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level,
Namjae Jeon070fb212021-05-26 17:57:12 +09003381 struct ksmbd_dir_info *d_info,
3382 struct ksmbd_kstat *ksmbd_kstat)
Namjae Jeone2f34482021-03-16 10:49:09 +09003383{
3384 int next_entry_offset = 0;
3385 char *conv_name;
3386 int conv_len;
3387 void *kstat;
Namjae Jeondac0ec62021-07-07 14:57:24 +09003388 int struct_sz, rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09003389
3390 conv_name = ksmbd_convert_dir_info_name(d_info,
3391 conn->local_nls,
3392 &conv_len);
3393 if (!conv_name)
3394 return -ENOMEM;
3395
3396 /* Somehow the name has only terminating NULL bytes */
3397 if (conv_len < 0) {
Namjae Jeondac0ec62021-07-07 14:57:24 +09003398 rc = -EINVAL;
3399 goto free_conv_name;
Namjae Jeone2f34482021-03-16 10:49:09 +09003400 }
3401
3402 struct_sz = readdir_info_level_struct_sz(info_level);
3403 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3404 KSMBD_DIR_INFO_ALIGNMENT);
3405
3406 if (next_entry_offset > d_info->out_buf_len) {
3407 d_info->out_buf_len = 0;
Namjae Jeondac0ec62021-07-07 14:57:24 +09003408 rc = -ENOSPC;
3409 goto free_conv_name;
Namjae Jeone2f34482021-03-16 10:49:09 +09003410 }
3411
3412 kstat = d_info->wptr;
3413 if (info_level != FILE_NAMES_INFORMATION)
3414 kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat);
3415
3416 switch (info_level) {
3417 case FILE_FULL_DIRECTORY_INFORMATION:
3418 {
3419 struct file_full_directory_info *ffdinfo;
3420
3421 ffdinfo = (struct file_full_directory_info *)kstat;
3422 ffdinfo->FileNameLength = cpu_to_le32(conv_len);
3423 ffdinfo->EaSize =
3424 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3425 if (ffdinfo->EaSize)
3426 ffdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3427 if (d_info->hide_dot_file && d_info->name[0] == '.')
3428 ffdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3429 memcpy(ffdinfo->FileName, conv_name, conv_len);
3430 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3431 break;
3432 }
3433 case FILE_BOTH_DIRECTORY_INFORMATION:
3434 {
3435 struct file_both_directory_info *fbdinfo;
3436
3437 fbdinfo = (struct file_both_directory_info *)kstat;
3438 fbdinfo->FileNameLength = cpu_to_le32(conv_len);
3439 fbdinfo->EaSize =
3440 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3441 if (fbdinfo->EaSize)
3442 fbdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3443 fbdinfo->ShortNameLength = 0;
3444 fbdinfo->Reserved = 0;
3445 if (d_info->hide_dot_file && d_info->name[0] == '.')
3446 fbdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3447 memcpy(fbdinfo->FileName, conv_name, conv_len);
3448 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3449 break;
3450 }
3451 case FILE_DIRECTORY_INFORMATION:
3452 {
3453 struct file_directory_info *fdinfo;
3454
3455 fdinfo = (struct file_directory_info *)kstat;
3456 fdinfo->FileNameLength = cpu_to_le32(conv_len);
3457 if (d_info->hide_dot_file && d_info->name[0] == '.')
3458 fdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3459 memcpy(fdinfo->FileName, conv_name, conv_len);
3460 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3461 break;
3462 }
3463 case FILE_NAMES_INFORMATION:
3464 {
3465 struct file_names_info *fninfo;
3466
3467 fninfo = (struct file_names_info *)kstat;
3468 fninfo->FileNameLength = cpu_to_le32(conv_len);
3469 memcpy(fninfo->FileName, conv_name, conv_len);
3470 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3471 break;
3472 }
3473 case FILEID_FULL_DIRECTORY_INFORMATION:
3474 {
3475 struct file_id_full_dir_info *dinfo;
3476
3477 dinfo = (struct file_id_full_dir_info *)kstat;
3478 dinfo->FileNameLength = cpu_to_le32(conv_len);
3479 dinfo->EaSize =
3480 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3481 if (dinfo->EaSize)
3482 dinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3483 dinfo->Reserved = 0;
3484 dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3485 if (d_info->hide_dot_file && d_info->name[0] == '.')
3486 dinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3487 memcpy(dinfo->FileName, conv_name, conv_len);
3488 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3489 break;
3490 }
3491 case FILEID_BOTH_DIRECTORY_INFORMATION:
3492 {
3493 struct file_id_both_directory_info *fibdinfo;
3494
3495 fibdinfo = (struct file_id_both_directory_info *)kstat;
3496 fibdinfo->FileNameLength = cpu_to_le32(conv_len);
3497 fibdinfo->EaSize =
3498 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3499 if (fibdinfo->EaSize)
3500 fibdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3501 fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3502 fibdinfo->ShortNameLength = 0;
3503 fibdinfo->Reserved = 0;
3504 fibdinfo->Reserved2 = cpu_to_le16(0);
3505 if (d_info->hide_dot_file && d_info->name[0] == '.')
3506 fibdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3507 memcpy(fibdinfo->FileName, conv_name, conv_len);
3508 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3509 break;
3510 }
3511 case SMB_FIND_FILE_POSIX_INFO:
3512 {
3513 struct smb2_posix_info *posix_info;
3514 u64 time;
3515
3516 posix_info = (struct smb2_posix_info *)kstat;
3517 posix_info->Ignored = 0;
3518 posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
3519 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
3520 posix_info->ChangeTime = cpu_to_le64(time);
3521 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime);
3522 posix_info->LastAccessTime = cpu_to_le64(time);
3523 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime);
3524 posix_info->LastWriteTime = cpu_to_le64(time);
3525 posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size);
3526 posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9);
3527 posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev);
3528 posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink);
3529 posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode);
3530 posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino);
3531 posix_info->DosAttributes =
3532 S_ISDIR(ksmbd_kstat->kstat->mode) ? ATTR_DIRECTORY_LE : ATTR_ARCHIVE_LE;
3533 if (d_info->hide_dot_file && d_info->name[0] == '.')
3534 posix_info->DosAttributes |= ATTR_HIDDEN_LE;
Christian Brauner475d6f92021-08-23 17:13:48 +02003535 id_to_sid(from_kuid_munged(&init_user_ns, ksmbd_kstat->kstat->uid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003536 SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]);
Christian Brauner475d6f92021-08-23 17:13:48 +02003537 id_to_sid(from_kgid_munged(&init_user_ns, ksmbd_kstat->kstat->gid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003538 SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]);
Namjae Jeone2f34482021-03-16 10:49:09 +09003539 memcpy(posix_info->name, conv_name, conv_len);
3540 posix_info->name_len = cpu_to_le32(conv_len);
3541 posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset);
3542 break;
3543 }
3544
3545 } /* switch (info_level) */
3546
3547 d_info->last_entry_offset = d_info->data_count;
3548 d_info->data_count += next_entry_offset;
Marios Makassikise7735c82021-05-06 11:40:02 +09003549 d_info->out_buf_len -= next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003550 d_info->wptr += next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003551
3552 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003553 "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n",
3554 info_level, d_info->out_buf_len,
3555 next_entry_offset, d_info->data_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09003556
Namjae Jeondac0ec62021-07-07 14:57:24 +09003557free_conv_name:
3558 kfree(conv_name);
3559 return rc;
Namjae Jeone2f34482021-03-16 10:49:09 +09003560}
3561
3562struct smb2_query_dir_private {
3563 struct ksmbd_work *work;
3564 char *search_pattern;
3565 struct ksmbd_file *dir_fp;
3566
3567 struct ksmbd_dir_info *d_info;
3568 int info_level;
3569};
3570
3571static void lock_dir(struct ksmbd_file *dir_fp)
3572{
3573 struct dentry *dir = dir_fp->filp->f_path.dentry;
3574
3575 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
3576}
3577
3578static void unlock_dir(struct ksmbd_file *dir_fp)
3579{
3580 struct dentry *dir = dir_fp->filp->f_path.dentry;
3581
3582 inode_unlock(d_inode(dir));
3583}
3584
3585static int process_query_dir_entries(struct smb2_query_dir_private *priv)
3586{
Hyunchul Lee465d7202021-07-03 12:10:36 +09003587 struct user_namespace *user_ns = file_mnt_user_ns(priv->dir_fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003588 struct kstat kstat;
3589 struct ksmbd_kstat ksmbd_kstat;
3590 int rc;
3591 int i;
3592
3593 for (i = 0; i < priv->d_info->num_entry; i++) {
3594 struct dentry *dent;
3595
3596 if (dentry_name(priv->d_info, priv->info_level))
3597 return -EINVAL;
3598
3599 lock_dir(priv->dir_fp);
Christian Braunerda1e7ad2021-08-23 17:13:47 +02003600 dent = lookup_one(user_ns, priv->d_info->name,
3601 priv->dir_fp->filp->f_path.dentry,
3602 priv->d_info->name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09003603 unlock_dir(priv->dir_fp);
3604
3605 if (IS_ERR(dent)) {
3606 ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003607 priv->d_info->name,
3608 PTR_ERR(dent));
Namjae Jeone2f34482021-03-16 10:49:09 +09003609 continue;
3610 }
3611 if (unlikely(d_is_negative(dent))) {
3612 dput(dent);
3613 ksmbd_debug(SMB, "Negative dentry `%s'\n",
3614 priv->d_info->name);
3615 continue;
3616 }
3617
3618 ksmbd_kstat.kstat = &kstat;
3619 if (priv->info_level != FILE_NAMES_INFORMATION)
3620 ksmbd_vfs_fill_dentry_attrs(priv->work,
Hyunchul Lee465d7202021-07-03 12:10:36 +09003621 user_ns,
Namjae Jeone2f34482021-03-16 10:49:09 +09003622 dent,
3623 &ksmbd_kstat);
3624
3625 rc = smb2_populate_readdir_entry(priv->work->conn,
3626 priv->info_level,
3627 priv->d_info,
3628 &ksmbd_kstat);
3629 dput(dent);
3630 if (rc)
3631 return rc;
3632 }
3633 return 0;
3634}
3635
3636static int reserve_populate_dentry(struct ksmbd_dir_info *d_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09003637 int info_level)
Namjae Jeone2f34482021-03-16 10:49:09 +09003638{
3639 int struct_sz;
3640 int conv_len;
3641 int next_entry_offset;
3642
3643 struct_sz = readdir_info_level_struct_sz(info_level);
3644 if (struct_sz == -EOPNOTSUPP)
3645 return -EOPNOTSUPP;
3646
3647 conv_len = (d_info->name_len + 1) * 2;
3648 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3649 KSMBD_DIR_INFO_ALIGNMENT);
3650
3651 if (next_entry_offset > d_info->out_buf_len) {
3652 d_info->out_buf_len = 0;
3653 return -ENOSPC;
3654 }
3655
3656 switch (info_level) {
3657 case FILE_FULL_DIRECTORY_INFORMATION:
3658 {
3659 struct file_full_directory_info *ffdinfo;
3660
3661 ffdinfo = (struct file_full_directory_info *)d_info->wptr;
3662 memcpy(ffdinfo->FileName, d_info->name, d_info->name_len);
3663 ffdinfo->FileName[d_info->name_len] = 0x00;
3664 ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3665 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3666 break;
3667 }
3668 case FILE_BOTH_DIRECTORY_INFORMATION:
3669 {
3670 struct file_both_directory_info *fbdinfo;
3671
3672 fbdinfo = (struct file_both_directory_info *)d_info->wptr;
3673 memcpy(fbdinfo->FileName, d_info->name, d_info->name_len);
3674 fbdinfo->FileName[d_info->name_len] = 0x00;
3675 fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3676 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3677 break;
3678 }
3679 case FILE_DIRECTORY_INFORMATION:
3680 {
3681 struct file_directory_info *fdinfo;
3682
3683 fdinfo = (struct file_directory_info *)d_info->wptr;
3684 memcpy(fdinfo->FileName, d_info->name, d_info->name_len);
3685 fdinfo->FileName[d_info->name_len] = 0x00;
3686 fdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3687 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3688 break;
3689 }
3690 case FILE_NAMES_INFORMATION:
3691 {
3692 struct file_names_info *fninfo;
3693
3694 fninfo = (struct file_names_info *)d_info->wptr;
3695 memcpy(fninfo->FileName, d_info->name, d_info->name_len);
3696 fninfo->FileName[d_info->name_len] = 0x00;
3697 fninfo->FileNameLength = cpu_to_le32(d_info->name_len);
3698 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3699 break;
3700 }
3701 case FILEID_FULL_DIRECTORY_INFORMATION:
3702 {
3703 struct file_id_full_dir_info *dinfo;
3704
3705 dinfo = (struct file_id_full_dir_info *)d_info->wptr;
3706 memcpy(dinfo->FileName, d_info->name, d_info->name_len);
3707 dinfo->FileName[d_info->name_len] = 0x00;
3708 dinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3709 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3710 break;
3711 }
3712 case FILEID_BOTH_DIRECTORY_INFORMATION:
3713 {
3714 struct file_id_both_directory_info *fibdinfo;
3715
3716 fibdinfo = (struct file_id_both_directory_info *)d_info->wptr;
3717 memcpy(fibdinfo->FileName, d_info->name, d_info->name_len);
3718 fibdinfo->FileName[d_info->name_len] = 0x00;
3719 fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3720 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3721 break;
3722 }
3723 case SMB_FIND_FILE_POSIX_INFO:
3724 {
3725 struct smb2_posix_info *posix_info;
3726
3727 posix_info = (struct smb2_posix_info *)d_info->wptr;
3728 memcpy(posix_info->name, d_info->name, d_info->name_len);
3729 posix_info->name[d_info->name_len] = 0x00;
3730 posix_info->name_len = cpu_to_le32(d_info->name_len);
3731 posix_info->NextEntryOffset =
3732 cpu_to_le32(next_entry_offset);
3733 break;
3734 }
3735 } /* switch (info_level) */
3736
3737 d_info->num_entry++;
3738 d_info->out_buf_len -= next_entry_offset;
3739 d_info->wptr += next_entry_offset;
3740 return 0;
3741}
3742
Namjae Jeon64b39f42021-03-30 14:25:35 +09003743static int __query_dir(struct dir_context *ctx, const char *name, int namlen,
Namjae Jeon070fb212021-05-26 17:57:12 +09003744 loff_t offset, u64 ino, unsigned int d_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09003745{
3746 struct ksmbd_readdir_data *buf;
3747 struct smb2_query_dir_private *priv;
3748 struct ksmbd_dir_info *d_info;
3749 int rc;
3750
3751 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
3752 priv = buf->private;
3753 d_info = priv->d_info;
3754
3755 /* dot and dotdot entries are already reserved */
3756 if (!strcmp(".", name) || !strcmp("..", name))
3757 return 0;
3758 if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name))
3759 return 0;
Namjae Jeonb24c9332021-03-21 17:32:19 +09003760 if (!match_pattern(name, namlen, priv->search_pattern))
Namjae Jeone2f34482021-03-16 10:49:09 +09003761 return 0;
3762
3763 d_info->name = name;
3764 d_info->name_len = namlen;
3765 rc = reserve_populate_dentry(d_info, priv->info_level);
3766 if (rc)
3767 return rc;
3768 if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
3769 d_info->out_buf_len = 0;
3770 return 0;
3771 }
3772 return 0;
3773}
3774
3775static void restart_ctx(struct dir_context *ctx)
3776{
3777 ctx->pos = 0;
3778}
3779
3780static int verify_info_level(int info_level)
3781{
3782 switch (info_level) {
3783 case FILE_FULL_DIRECTORY_INFORMATION:
3784 case FILE_BOTH_DIRECTORY_INFORMATION:
3785 case FILE_DIRECTORY_INFORMATION:
3786 case FILE_NAMES_INFORMATION:
3787 case FILEID_FULL_DIRECTORY_INFORMATION:
3788 case FILEID_BOTH_DIRECTORY_INFORMATION:
3789 case SMB_FIND_FILE_POSIX_INFO:
3790 break;
3791 default:
3792 return -EOPNOTSUPP;
3793 }
3794
3795 return 0;
3796}
3797
Hyunchul Lee34061d62021-10-16 08:39:54 +09003798static int smb2_calc_max_out_buf_len(struct ksmbd_work *work,
3799 unsigned short hdr2_len,
3800 unsigned int out_buf_len)
3801{
3802 int free_len;
3803
3804 if (out_buf_len > work->conn->vals->max_trans_size)
3805 return -EINVAL;
3806
3807 free_len = (int)(work->response_sz -
3808 (get_rfc1002_len(work->response_buf) + 4)) -
3809 hdr2_len;
3810 if (free_len < 0)
3811 return -EINVAL;
3812
3813 return min_t(int, out_buf_len, free_len);
3814}
3815
Namjae Jeone2f34482021-03-16 10:49:09 +09003816int smb2_query_dir(struct ksmbd_work *work)
3817{
3818 struct ksmbd_conn *conn = work->conn;
3819 struct smb2_query_directory_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09003820 struct smb2_query_directory_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09003821 struct ksmbd_share_config *share = work->tcon->share_conf;
3822 struct ksmbd_file *dir_fp = NULL;
3823 struct ksmbd_dir_info d_info;
3824 int rc = 0;
3825 char *srch_ptr = NULL;
3826 unsigned char srch_flag;
3827 int buffer_sz;
3828 struct smb2_query_dir_private query_dir_private = {NULL, };
3829
Namjae Jeone2f34482021-03-16 10:49:09 +09003830 WORK_BUFFERS(work, req, rsp);
3831
3832 if (ksmbd_override_fsids(work)) {
3833 rsp->hdr.Status = STATUS_NO_MEMORY;
3834 smb2_set_err_rsp(work);
3835 return -ENOMEM;
3836 }
3837
3838 rc = verify_info_level(req->FileInformationClass);
3839 if (rc) {
3840 rc = -EFAULT;
3841 goto err_out2;
3842 }
3843
3844 dir_fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09003845 le64_to_cpu(req->VolatileFileId),
3846 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09003847 if (!dir_fp) {
3848 rc = -EBADF;
3849 goto err_out2;
3850 }
3851
3852 if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) ||
Hyunchul Leeaf349832021-06-30 18:25:53 +09003853 inode_permission(file_mnt_user_ns(dir_fp->filp),
3854 file_inode(dir_fp->filp),
Namjae Jeon070fb212021-05-26 17:57:12 +09003855 MAY_READ | MAY_EXEC)) {
Namjae Jeon493fa2f2021-06-29 09:22:16 +09003856 pr_err("no right to enumerate directory (%pd)\n",
3857 dir_fp->filp->f_path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003858 rc = -EACCES;
3859 goto err_out2;
3860 }
3861
3862 if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09003863 pr_err("can't do query dir for a file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09003864 rc = -EINVAL;
3865 goto err_out2;
3866 }
3867
3868 srch_flag = req->Flags;
3869 srch_ptr = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09003870 le16_to_cpu(req->FileNameLength), 1,
3871 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09003872 if (IS_ERR(srch_ptr)) {
3873 ksmbd_debug(SMB, "Search Pattern not found\n");
3874 rc = -EINVAL;
3875 goto err_out2;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003876 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003877 ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr);
Namjae Jeon64b39f42021-03-30 14:25:35 +09003878 }
Namjae Jeone2f34482021-03-16 10:49:09 +09003879
3880 ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename);
3881
3882 if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) {
3883 ksmbd_debug(SMB, "Restart directory scan\n");
3884 generic_file_llseek(dir_fp->filp, 0, SEEK_SET);
3885 restart_ctx(&dir_fp->readdir_data.ctx);
3886 }
3887
3888 memset(&d_info, 0, sizeof(struct ksmbd_dir_info));
3889 d_info.wptr = (char *)rsp->Buffer;
3890 d_info.rptr = (char *)rsp->Buffer;
Hyunchul Lee34061d62021-10-16 08:39:54 +09003891 d_info.out_buf_len =
3892 smb2_calc_max_out_buf_len(work, 8,
3893 le32_to_cpu(req->OutputBufferLength));
3894 if (d_info.out_buf_len < 0) {
3895 rc = -EINVAL;
3896 goto err_out;
3897 }
Namjae Jeone2f34482021-03-16 10:49:09 +09003898 d_info.flags = srch_flag;
3899
3900 /*
3901 * reserve dot and dotdot entries in head of buffer
3902 * in first response
3903 */
3904 rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass,
Namjae Jeon070fb212021-05-26 17:57:12 +09003905 dir_fp, &d_info, srch_ptr,
3906 smb2_populate_readdir_entry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003907 if (rc == -ENOSPC)
3908 rc = 0;
3909 else if (rc)
3910 goto err_out;
3911
3912 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES))
3913 d_info.hide_dot_file = true;
3914
3915 buffer_sz = d_info.out_buf_len;
3916 d_info.rptr = d_info.wptr;
3917 query_dir_private.work = work;
3918 query_dir_private.search_pattern = srch_ptr;
3919 query_dir_private.dir_fp = dir_fp;
3920 query_dir_private.d_info = &d_info;
3921 query_dir_private.info_level = req->FileInformationClass;
3922 dir_fp->readdir_data.private = &query_dir_private;
3923 set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir);
3924
Namjae Jeone8c06192021-06-22 11:06:11 +09003925 rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx);
Namjae Jeone2f34482021-03-16 10:49:09 +09003926 if (rc == 0)
3927 restart_ctx(&dir_fp->readdir_data.ctx);
3928 if (rc == -ENOSPC)
3929 rc = 0;
3930 if (rc)
3931 goto err_out;
3932
3933 d_info.wptr = d_info.rptr;
3934 d_info.out_buf_len = buffer_sz;
3935 rc = process_query_dir_entries(&query_dir_private);
3936 if (rc)
3937 goto err_out;
3938
3939 if (!d_info.data_count && d_info.out_buf_len >= 0) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09003940 if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003941 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003942 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003943 dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0;
3944 rsp->hdr.Status = STATUS_NO_MORE_FILES;
3945 }
3946 rsp->StructureSize = cpu_to_le16(9);
3947 rsp->OutputBufferOffset = cpu_to_le16(0);
3948 rsp->OutputBufferLength = cpu_to_le32(0);
3949 rsp->Buffer[0] = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09003950 inc_rfc1001_len(work->response_buf, 9);
Namjae Jeone2f34482021-03-16 10:49:09 +09003951 } else {
3952 ((struct file_directory_info *)
3953 ((char *)rsp->Buffer + d_info.last_entry_offset))
3954 ->NextEntryOffset = 0;
3955
3956 rsp->StructureSize = cpu_to_le16(9);
3957 rsp->OutputBufferOffset = cpu_to_le16(72);
3958 rsp->OutputBufferLength = cpu_to_le32(d_info.data_count);
Namjae Jeoncb451722021-11-03 08:08:44 +09003959 inc_rfc1001_len(work->response_buf, 8 + d_info.data_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09003960 }
3961
3962 kfree(srch_ptr);
3963 ksmbd_fd_put(work, dir_fp);
3964 ksmbd_revert_fsids(work);
3965 return 0;
3966
3967err_out:
Namjae Jeonbde16942021-06-28 15:23:19 +09003968 pr_err("error while processing smb2 query dir rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09003969 kfree(srch_ptr);
3970
3971err_out2:
3972 if (rc == -EINVAL)
3973 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3974 else if (rc == -EACCES)
3975 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3976 else if (rc == -ENOENT)
3977 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
3978 else if (rc == -EBADF)
3979 rsp->hdr.Status = STATUS_FILE_CLOSED;
3980 else if (rc == -ENOMEM)
3981 rsp->hdr.Status = STATUS_NO_MEMORY;
3982 else if (rc == -EFAULT)
3983 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
3984 if (!rsp->hdr.Status)
3985 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3986
3987 smb2_set_err_rsp(work);
3988 ksmbd_fd_put(work, dir_fp);
3989 ksmbd_revert_fsids(work);
3990 return 0;
3991}
3992
3993/**
3994 * buffer_check_err() - helper function to check buffer errors
3995 * @reqOutputBufferLength: max buffer length expected in command response
3996 * @rsp: query info response buffer contains output buffer length
3997 * @infoclass_size: query info class response buffer size
3998 *
3999 * Return: 0 on success, otherwise error
4000 */
4001static int buffer_check_err(int reqOutputBufferLength,
Namjae Jeoncb451722021-11-03 08:08:44 +09004002 struct smb2_query_info_rsp *rsp,
4003 void *rsp_org, int infoclass_size)
Namjae Jeone2f34482021-03-16 10:49:09 +09004004{
4005 if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) {
4006 if (reqOutputBufferLength < infoclass_size) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004007 pr_err("Invalid Buffer Size Requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004008 rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH;
Namjae Jeoncb451722021-11-03 08:08:44 +09004009 *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr));
Namjae Jeone2f34482021-03-16 10:49:09 +09004010 return -EINVAL;
4011 }
4012
4013 ksmbd_debug(SMB, "Buffer Overflow\n");
4014 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
Namjae Jeoncb451722021-11-03 08:08:44 +09004015 *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09004016 reqOutputBufferLength);
4017 rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09004018 }
4019 return 0;
4020}
4021
Namjae Jeoncb451722021-11-03 08:08:44 +09004022static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp,
4023 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004024{
4025 struct smb2_file_standard_info *sinfo;
4026
4027 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4028
4029 sinfo->AllocationSize = cpu_to_le64(4096);
4030 sinfo->EndOfFile = cpu_to_le64(0);
4031 sinfo->NumberOfLinks = cpu_to_le32(1);
4032 sinfo->DeletePending = 1;
4033 sinfo->Directory = 0;
4034 rsp->OutputBufferLength =
4035 cpu_to_le32(sizeof(struct smb2_file_standard_info));
Namjae Jeoncb451722021-11-03 08:08:44 +09004036 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_standard_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004037}
4038
Namjae Jeoncb451722021-11-03 08:08:44 +09004039static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num,
4040 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004041{
4042 struct smb2_file_internal_info *file_info;
4043
4044 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4045
4046 /* any unique number */
4047 file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63));
4048 rsp->OutputBufferLength =
4049 cpu_to_le32(sizeof(struct smb2_file_internal_info));
Namjae Jeoncb451722021-11-03 08:08:44 +09004050 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004051}
4052
Namjae Jeone2f34482021-03-16 10:49:09 +09004053static int smb2_get_info_file_pipe(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09004054 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004055 struct smb2_query_info_rsp *rsp,
4056 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004057{
Namjae Jeon64b39f42021-03-30 14:25:35 +09004058 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09004059 int rc;
4060
4061 /*
4062 * Windows can sometime send query file info request on
4063 * pipe without opening it, checking error condition here
4064 */
4065 id = le64_to_cpu(req->VolatileFileId);
4066 if (!ksmbd_session_rpc_method(sess, id))
4067 return -ENOENT;
4068
4069 ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004070 req->FileInfoClass, le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09004071
4072 switch (req->FileInfoClass) {
4073 case FILE_STANDARD_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004074 get_standard_info_pipe(rsp, rsp_org);
Namjae Jeone2f34482021-03-16 10:49:09 +09004075 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004076 rsp, rsp_org,
4077 FILE_STANDARD_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09004078 break;
4079 case FILE_INTERNAL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004080 get_internal_info_pipe(rsp, id, rsp_org);
Namjae Jeone2f34482021-03-16 10:49:09 +09004081 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004082 rsp, rsp_org,
4083 FILE_INTERNAL_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09004084 break;
4085 default:
4086 ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004087 req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09004088 rc = -EOPNOTSUPP;
4089 }
4090 return rc;
4091}
4092
4093/**
4094 * smb2_get_ea() - handler for smb2 get extended attribute command
4095 * @work: smb work containing query info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09004096 * @fp: ksmbd_file pointer
4097 * @req: get extended attribute request
4098 * @rsp: response buffer pointer
4099 * @rsp_org: base response buffer pointer in case of chained response
Namjae Jeone2f34482021-03-16 10:49:09 +09004100 *
4101 * Return: 0 on success, otherwise error
4102 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09004103static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004104 struct smb2_query_info_req *req,
4105 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004106{
4107 struct smb2_ea_info *eainfo, *prev_eainfo;
4108 char *name, *ptr, *xattr_list = NULL, *buf;
4109 int rc, name_len, value_len, xattr_list_len, idx;
4110 ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0;
4111 struct smb2_ea_info_req *ea_req = NULL;
4112 struct path *path;
Hyunchul Lee465d7202021-07-03 12:10:36 +09004113 struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09004114
4115 if (!(fp->daccess & FILE_READ_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004116 pr_err("Not permitted to read ext attr : 0x%x\n",
4117 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004118 return -EACCES;
4119 }
4120
4121 path = &fp->filp->f_path;
4122 /* single EA entry is requested with given user.* name */
Namjae Jeon64b39f42021-03-30 14:25:35 +09004123 if (req->InputBufferLength) {
Namjae Jeon6d562622021-09-18 18:45:12 +09004124 if (le32_to_cpu(req->InputBufferLength) <
4125 sizeof(struct smb2_ea_info_req))
4126 return -EINVAL;
4127
Namjae Jeone2f34482021-03-16 10:49:09 +09004128 ea_req = (struct smb2_ea_info_req *)req->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004129 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09004130 /* need to send all EAs, if no specific EA is requested*/
4131 if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY)
4132 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09004133 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n",
4134 le32_to_cpu(req->Flags));
Namjae Jeone2f34482021-03-16 10:49:09 +09004135 }
4136
Hyunchul Lee34061d62021-10-16 08:39:54 +09004137 buf_free_len =
4138 smb2_calc_max_out_buf_len(work, 8,
4139 le32_to_cpu(req->OutputBufferLength));
4140 if (buf_free_len < 0)
4141 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09004142
4143 rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4144 if (rc < 0) {
4145 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4146 goto out;
4147 } else if (!rc) { /* there is no EA in the file */
4148 ksmbd_debug(SMB, "no ea data in the file\n");
4149 goto done;
4150 }
4151 xattr_list_len = rc;
4152
4153 ptr = (char *)rsp->Buffer;
4154 eainfo = (struct smb2_ea_info *)ptr;
4155 prev_eainfo = eainfo;
4156 idx = 0;
4157
4158 while (idx < xattr_list_len) {
4159 name = xattr_list + idx;
4160 name_len = strlen(name);
4161
4162 ksmbd_debug(SMB, "%s, len %d\n", name, name_len);
4163 idx += name_len + 1;
4164
4165 /*
4166 * CIFS does not support EA other than user.* namespace,
4167 * still keep the framework generic, to list other attrs
4168 * in future.
4169 */
4170 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4171 continue;
4172
4173 if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004174 STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004175 continue;
4176
4177 if (req->InputBufferLength &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09004178 strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name,
4179 ea_req->EaNameLength))
Namjae Jeone2f34482021-03-16 10:49:09 +09004180 continue;
4181
4182 if (!strncmp(&name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004183 DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004184 continue;
4185
4186 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4187 name_len -= XATTR_USER_PREFIX_LEN;
4188
4189 ptr = (char *)(&eainfo->name + name_len + 1);
4190 buf_free_len -= (offsetof(struct smb2_ea_info, name) +
4191 name_len + 1);
4192 /* bailout if xattr can't fit in buf_free_len */
Hyunchul Lee465d7202021-07-03 12:10:36 +09004193 value_len = ksmbd_vfs_getxattr(user_ns, path->dentry,
4194 name, &buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004195 if (value_len <= 0) {
4196 rc = -ENOENT;
4197 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4198 goto out;
4199 }
4200
4201 buf_free_len -= value_len;
4202 if (buf_free_len < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09004203 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004204 break;
4205 }
4206
4207 memcpy(ptr, buf, value_len);
Namjae Jeon79f6b112021-04-02 12:47:14 +09004208 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004209
4210 ptr += value_len;
4211 eainfo->Flags = 0;
4212 eainfo->EaNameLength = name_len;
4213
Namjae Jeon64b39f42021-03-30 14:25:35 +09004214 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004215 memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN],
Namjae Jeon070fb212021-05-26 17:57:12 +09004216 name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09004217 else
4218 memcpy(eainfo->name, name, name_len);
4219
4220 eainfo->name[name_len] = '\0';
4221 eainfo->EaValueLength = cpu_to_le16(value_len);
4222 next_offset = offsetof(struct smb2_ea_info, name) +
4223 name_len + 1 + value_len;
4224
4225 /* align next xattr entry at 4 byte bundary */
4226 alignment_bytes = ((next_offset + 3) & ~3) - next_offset;
4227 if (alignment_bytes) {
4228 memset(ptr, '\0', alignment_bytes);
4229 ptr += alignment_bytes;
4230 next_offset += alignment_bytes;
4231 buf_free_len -= alignment_bytes;
4232 }
4233 eainfo->NextEntryOffset = cpu_to_le32(next_offset);
4234 prev_eainfo = eainfo;
4235 eainfo = (struct smb2_ea_info *)ptr;
4236 rsp_data_cnt += next_offset;
4237
4238 if (req->InputBufferLength) {
4239 ksmbd_debug(SMB, "single entry requested\n");
4240 break;
4241 }
4242 }
4243
4244 /* no more ea entries */
4245 prev_eainfo->NextEntryOffset = 0;
4246done:
4247 rc = 0;
4248 if (rsp_data_cnt == 0)
4249 rsp->hdr.Status = STATUS_NO_EAS_ON_FILE;
4250 rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt);
4251 inc_rfc1001_len(rsp_org, rsp_data_cnt);
4252out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004253 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004254 return rc;
4255}
4256
4257static void get_file_access_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004258 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004259{
4260 struct smb2_file_access_info *file_info;
4261
4262 file_info = (struct smb2_file_access_info *)rsp->Buffer;
4263 file_info->AccessFlags = fp->daccess;
4264 rsp->OutputBufferLength =
4265 cpu_to_le32(sizeof(struct smb2_file_access_info));
4266 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info));
4267}
4268
4269static int get_file_basic_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004270 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004271{
Namjae Jeon88d30052021-09-29 15:37:18 +09004272 struct smb2_file_basic_info *basic_info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004273 struct kstat stat;
4274 u64 time;
4275
4276 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004277 pr_err("no right to read the attributes : 0x%x\n",
4278 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004279 return -EACCES;
4280 }
4281
Namjae Jeon88d30052021-09-29 15:37:18 +09004282 basic_info = (struct smb2_file_basic_info *)rsp->Buffer;
Hyunchul Leeaf349832021-06-30 18:25:53 +09004283 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4284 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004285 basic_info->CreationTime = cpu_to_le64(fp->create_time);
4286 time = ksmbd_UnixTimeToNT(stat.atime);
4287 basic_info->LastAccessTime = cpu_to_le64(time);
4288 time = ksmbd_UnixTimeToNT(stat.mtime);
4289 basic_info->LastWriteTime = cpu_to_le64(time);
4290 time = ksmbd_UnixTimeToNT(stat.ctime);
4291 basic_info->ChangeTime = cpu_to_le64(time);
4292 basic_info->Attributes = fp->f_ci->m_fattr;
4293 basic_info->Pad1 = 0;
4294 rsp->OutputBufferLength =
Namjae Jeon88d30052021-09-29 15:37:18 +09004295 cpu_to_le32(sizeof(struct smb2_file_basic_info));
4296 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_basic_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004297 return 0;
4298}
4299
4300static unsigned long long get_allocation_size(struct inode *inode,
Namjae Jeon070fb212021-05-26 17:57:12 +09004301 struct kstat *stat)
Namjae Jeone2f34482021-03-16 10:49:09 +09004302{
4303 unsigned long long alloc_size = 0;
4304
4305 if (!S_ISDIR(stat->mode)) {
4306 if ((inode->i_blocks << 9) <= stat->size)
4307 alloc_size = stat->size;
4308 else
4309 alloc_size = inode->i_blocks << 9;
Namjae Jeone2f34482021-03-16 10:49:09 +09004310 }
4311
4312 return alloc_size;
4313}
4314
4315static void get_file_standard_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004316 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004317{
4318 struct smb2_file_standard_info *sinfo;
4319 unsigned int delete_pending;
4320 struct inode *inode;
4321 struct kstat stat;
4322
Namjae Jeonab0b2632021-06-29 09:20:13 +09004323 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004324 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004325
4326 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4327 delete_pending = ksmbd_inode_pending_delete(fp);
4328
4329 sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat));
4330 sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4331 sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending);
4332 sinfo->DeletePending = delete_pending;
4333 sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4334 rsp->OutputBufferLength =
4335 cpu_to_le32(sizeof(struct smb2_file_standard_info));
4336 inc_rfc1001_len(rsp_org,
4337 sizeof(struct smb2_file_standard_info));
4338}
4339
4340static void get_file_alignment_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004341 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004342{
4343 struct smb2_file_alignment_info *file_info;
4344
4345 file_info = (struct smb2_file_alignment_info *)rsp->Buffer;
4346 file_info->AlignmentRequirement = 0;
4347 rsp->OutputBufferLength =
4348 cpu_to_le32(sizeof(struct smb2_file_alignment_info));
4349 inc_rfc1001_len(rsp_org,
4350 sizeof(struct smb2_file_alignment_info));
4351}
4352
4353static int get_file_all_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004354 struct smb2_query_info_rsp *rsp,
4355 struct ksmbd_file *fp,
4356 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004357{
4358 struct ksmbd_conn *conn = work->conn;
4359 struct smb2_file_all_info *file_info;
4360 unsigned int delete_pending;
4361 struct inode *inode;
4362 struct kstat stat;
4363 int conv_len;
4364 char *filename;
4365 u64 time;
4366
4367 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4368 ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004369 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004370 return -EACCES;
4371 }
4372
Hyunchul Lee265fd192021-09-25 00:06:16 +09004373 filename = convert_to_nt_pathname(fp->filename);
Namjae Jeone2f34482021-03-16 10:49:09 +09004374 if (!filename)
4375 return -ENOMEM;
4376
Namjae Jeonab0b2632021-06-29 09:20:13 +09004377 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004378 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004379
4380 ksmbd_debug(SMB, "filename = %s\n", filename);
4381 delete_pending = ksmbd_inode_pending_delete(fp);
4382 file_info = (struct smb2_file_all_info *)rsp->Buffer;
4383
4384 file_info->CreationTime = cpu_to_le64(fp->create_time);
4385 time = ksmbd_UnixTimeToNT(stat.atime);
4386 file_info->LastAccessTime = cpu_to_le64(time);
4387 time = ksmbd_UnixTimeToNT(stat.mtime);
4388 file_info->LastWriteTime = cpu_to_le64(time);
4389 time = ksmbd_UnixTimeToNT(stat.ctime);
4390 file_info->ChangeTime = cpu_to_le64(time);
4391 file_info->Attributes = fp->f_ci->m_fattr;
4392 file_info->Pad1 = 0;
4393 file_info->AllocationSize =
4394 cpu_to_le64(get_allocation_size(inode, &stat));
4395 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4396 file_info->NumberOfLinks =
4397 cpu_to_le32(get_nlink(&stat) - delete_pending);
4398 file_info->DeletePending = delete_pending;
4399 file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4400 file_info->Pad2 = 0;
4401 file_info->IndexNumber = cpu_to_le64(stat.ino);
4402 file_info->EASize = 0;
4403 file_info->AccessFlags = fp->daccess;
4404 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4405 file_info->Mode = fp->coption;
4406 file_info->AlignmentRequirement = 0;
Namjae Jeon070fb212021-05-26 17:57:12 +09004407 conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename,
4408 PATH_MAX, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004409 conv_len *= 2;
4410 file_info->FileNameLength = cpu_to_le32(conv_len);
4411 rsp->OutputBufferLength =
4412 cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1);
4413 kfree(filename);
4414 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4415 return 0;
4416}
4417
4418static void get_file_alternate_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004419 struct smb2_query_info_rsp *rsp,
4420 struct ksmbd_file *fp,
4421 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004422{
4423 struct ksmbd_conn *conn = work->conn;
4424 struct smb2_file_alt_name_info *file_info;
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004425 struct dentry *dentry = fp->filp->f_path.dentry;
Namjae Jeone2f34482021-03-16 10:49:09 +09004426 int conv_len;
Namjae Jeone2f34482021-03-16 10:49:09 +09004427
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004428 spin_lock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004429 file_info = (struct smb2_file_alt_name_info *)rsp->Buffer;
4430 conv_len = ksmbd_extract_shortname(conn,
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004431 dentry->d_name.name,
Namjae Jeone2f34482021-03-16 10:49:09 +09004432 file_info->FileName);
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004433 spin_unlock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004434 file_info->FileNameLength = cpu_to_le32(conv_len);
4435 rsp->OutputBufferLength =
4436 cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len);
4437 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4438}
4439
4440static void get_file_stream_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004441 struct smb2_query_info_rsp *rsp,
4442 struct ksmbd_file *fp,
4443 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004444{
4445 struct ksmbd_conn *conn = work->conn;
4446 struct smb2_file_stream_info *file_info;
4447 char *stream_name, *xattr_list = NULL, *stream_buf;
4448 struct kstat stat;
4449 struct path *path = &fp->filp->f_path;
4450 ssize_t xattr_list_len;
4451 int nbytes = 0, streamlen, stream_name_len, next, idx = 0;
Hyunchul Lee34061d62021-10-16 08:39:54 +09004452 int buf_free_len;
4453 struct smb2_query_info_req *req = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09004454
Hyunchul Leeaf349832021-06-30 18:25:53 +09004455 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4456 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004457 file_info = (struct smb2_file_stream_info *)rsp->Buffer;
4458
4459 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4460 if (xattr_list_len < 0) {
4461 goto out;
4462 } else if (!xattr_list_len) {
4463 ksmbd_debug(SMB, "empty xattr in the file\n");
4464 goto out;
4465 }
4466
Hyunchul Lee34061d62021-10-16 08:39:54 +09004467 buf_free_len =
4468 smb2_calc_max_out_buf_len(work, 8,
4469 le32_to_cpu(req->OutputBufferLength));
4470 if (buf_free_len < 0)
4471 goto out;
4472
Namjae Jeone2f34482021-03-16 10:49:09 +09004473 while (idx < xattr_list_len) {
4474 stream_name = xattr_list + idx;
4475 streamlen = strlen(stream_name);
4476 idx += streamlen + 1;
4477
4478 ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen);
4479
4480 if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004481 STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004482 continue;
4483
4484 stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN +
4485 STREAM_PREFIX_LEN);
4486 streamlen = stream_name_len;
4487
4488 /* plus : size */
4489 streamlen += 1;
4490 stream_buf = kmalloc(streamlen + 1, GFP_KERNEL);
4491 if (!stream_buf)
4492 break;
4493
4494 streamlen = snprintf(stream_buf, streamlen + 1,
Namjae Jeon070fb212021-05-26 17:57:12 +09004495 ":%s", &stream_name[XATTR_NAME_STREAM_LEN]);
Namjae Jeone2f34482021-03-16 10:49:09 +09004496
Hyunchul Lee34061d62021-10-16 08:39:54 +09004497 next = sizeof(struct smb2_file_stream_info) + streamlen * 2;
4498 if (next > buf_free_len)
4499 break;
4500
Namjae Jeon070fb212021-05-26 17:57:12 +09004501 file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes];
Namjae Jeone2f34482021-03-16 10:49:09 +09004502 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004503 stream_buf, streamlen,
4504 conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004505 streamlen *= 2;
4506 kfree(stream_buf);
4507 file_info->StreamNameLength = cpu_to_le32(streamlen);
4508 file_info->StreamSize = cpu_to_le64(stream_name_len);
4509 file_info->StreamAllocationSize = cpu_to_le64(stream_name_len);
4510
Namjae Jeone2f34482021-03-16 10:49:09 +09004511 nbytes += next;
Hyunchul Lee34061d62021-10-16 08:39:54 +09004512 buf_free_len -= next;
Namjae Jeone2f34482021-03-16 10:49:09 +09004513 file_info->NextEntryOffset = cpu_to_le32(next);
4514 }
4515
Hyunchul Lee34061d62021-10-16 08:39:54 +09004516 if (!S_ISDIR(stat.mode) &&
4517 buf_free_len >= sizeof(struct smb2_file_stream_info) + 7 * 2) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004518 file_info = (struct smb2_file_stream_info *)
4519 &rsp->Buffer[nbytes];
4520 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004521 "::$DATA", 7, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004522 streamlen *= 2;
4523 file_info->StreamNameLength = cpu_to_le32(streamlen);
Namjae Jeon9f6323312021-09-18 21:02:39 +09004524 file_info->StreamSize = 0;
4525 file_info->StreamAllocationSize = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09004526 nbytes += sizeof(struct smb2_file_stream_info) + streamlen;
4527 }
4528
4529 /* last entry offset should be 0 */
4530 file_info->NextEntryOffset = 0;
4531out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004532 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004533
4534 rsp->OutputBufferLength = cpu_to_le32(nbytes);
4535 inc_rfc1001_len(rsp_org, nbytes);
4536}
4537
4538static void get_file_internal_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004539 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004540{
4541 struct smb2_file_internal_info *file_info;
4542 struct kstat stat;
4543
Hyunchul Leeaf349832021-06-30 18:25:53 +09004544 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4545 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004546 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4547 file_info->IndexNumber = cpu_to_le64(stat.ino);
4548 rsp->OutputBufferLength =
4549 cpu_to_le32(sizeof(struct smb2_file_internal_info));
4550 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
4551}
4552
4553static int get_file_network_open_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004554 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004555{
4556 struct smb2_file_ntwrk_info *file_info;
4557 struct inode *inode;
4558 struct kstat stat;
4559 u64 time;
4560
4561 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004562 pr_err("no right to read the attributes : 0x%x\n",
4563 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004564 return -EACCES;
4565 }
4566
4567 file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer;
4568
Namjae Jeonab0b2632021-06-29 09:20:13 +09004569 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004570 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004571
4572 file_info->CreationTime = cpu_to_le64(fp->create_time);
4573 time = ksmbd_UnixTimeToNT(stat.atime);
4574 file_info->LastAccessTime = cpu_to_le64(time);
4575 time = ksmbd_UnixTimeToNT(stat.mtime);
4576 file_info->LastWriteTime = cpu_to_le64(time);
4577 time = ksmbd_UnixTimeToNT(stat.ctime);
4578 file_info->ChangeTime = cpu_to_le64(time);
4579 file_info->Attributes = fp->f_ci->m_fattr;
4580 file_info->AllocationSize =
4581 cpu_to_le64(get_allocation_size(inode, &stat));
4582 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4583 file_info->Reserved = cpu_to_le32(0);
4584 rsp->OutputBufferLength =
4585 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info));
4586 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info));
4587 return 0;
4588}
4589
Namjae Jeon64b39f42021-03-30 14:25:35 +09004590static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004591{
4592 struct smb2_file_ea_info *file_info;
4593
4594 file_info = (struct smb2_file_ea_info *)rsp->Buffer;
4595 file_info->EASize = 0;
4596 rsp->OutputBufferLength =
4597 cpu_to_le32(sizeof(struct smb2_file_ea_info));
4598 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info));
4599}
4600
4601static void get_file_position_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004602 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004603{
4604 struct smb2_file_pos_info *file_info;
4605
4606 file_info = (struct smb2_file_pos_info *)rsp->Buffer;
4607 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4608 rsp->OutputBufferLength =
4609 cpu_to_le32(sizeof(struct smb2_file_pos_info));
4610 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info));
4611}
4612
4613static void get_file_mode_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004614 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004615{
4616 struct smb2_file_mode_info *file_info;
4617
4618 file_info = (struct smb2_file_mode_info *)rsp->Buffer;
4619 file_info->Mode = fp->coption & FILE_MODE_INFO_MASK;
4620 rsp->OutputBufferLength =
4621 cpu_to_le32(sizeof(struct smb2_file_mode_info));
4622 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info));
4623}
4624
4625static void get_file_compression_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004626 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004627{
4628 struct smb2_file_comp_info *file_info;
4629 struct kstat stat;
4630
Hyunchul Leeaf349832021-06-30 18:25:53 +09004631 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4632 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004633
4634 file_info = (struct smb2_file_comp_info *)rsp->Buffer;
4635 file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9);
4636 file_info->CompressionFormat = COMPRESSION_FORMAT_NONE;
4637 file_info->CompressionUnitShift = 0;
4638 file_info->ChunkShift = 0;
4639 file_info->ClusterShift = 0;
4640 memset(&file_info->Reserved[0], 0, 3);
4641
4642 rsp->OutputBufferLength =
4643 cpu_to_le32(sizeof(struct smb2_file_comp_info));
4644 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info));
4645}
4646
4647static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004648 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004649{
4650 struct smb2_file_attr_tag_info *file_info;
4651
4652 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004653 pr_err("no right to read the attributes : 0x%x\n",
4654 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004655 return -EACCES;
4656 }
4657
4658 file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer;
4659 file_info->FileAttributes = fp->f_ci->m_fattr;
4660 file_info->ReparseTag = 0;
4661 rsp->OutputBufferLength =
4662 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info));
Namjae Jeon070fb212021-05-26 17:57:12 +09004663 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004664 return 0;
4665}
4666
4667static int find_file_posix_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004668 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004669{
4670 struct smb311_posix_qinfo *file_info;
Namjae Jeonab0b2632021-06-29 09:20:13 +09004671 struct inode *inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09004672 u64 time;
4673
4674 file_info = (struct smb311_posix_qinfo *)rsp->Buffer;
4675 file_info->CreationTime = cpu_to_le64(fp->create_time);
4676 time = ksmbd_UnixTimeToNT(inode->i_atime);
4677 file_info->LastAccessTime = cpu_to_le64(time);
4678 time = ksmbd_UnixTimeToNT(inode->i_mtime);
4679 file_info->LastWriteTime = cpu_to_le64(time);
4680 time = ksmbd_UnixTimeToNT(inode->i_ctime);
4681 file_info->ChangeTime = cpu_to_le64(time);
4682 file_info->DosAttributes = fp->f_ci->m_fattr;
4683 file_info->Inode = cpu_to_le64(inode->i_ino);
4684 file_info->EndOfFile = cpu_to_le64(inode->i_size);
4685 file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9);
4686 file_info->HardLinks = cpu_to_le32(inode->i_nlink);
4687 file_info->Mode = cpu_to_le32(inode->i_mode);
4688 file_info->DeviceId = cpu_to_le32(inode->i_rdev);
4689 rsp->OutputBufferLength =
4690 cpu_to_le32(sizeof(struct smb311_posix_qinfo));
Namjae Jeon64b39f42021-03-30 14:25:35 +09004691 inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo));
Namjae Jeone2f34482021-03-16 10:49:09 +09004692 return 0;
4693}
4694
Namjae Jeone2f34482021-03-16 10:49:09 +09004695static int smb2_get_info_file(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004696 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004697 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09004698{
4699 struct ksmbd_file *fp;
4700 int fileinfoclass = 0;
4701 int rc = 0;
4702 int file_infoclass_size;
4703 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4704
4705 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004706 KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004707 /* smb2 info file called for pipe */
Namjae Jeoncb451722021-11-03 08:08:44 +09004708 return smb2_get_info_file_pipe(work->sess, req, rsp,
4709 work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004710 }
4711
4712 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon38673692021-07-08 12:32:27 +09004713 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
4714 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004715 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004716 id = work->compound_fid;
4717 pid = work->compound_pfid;
4718 }
4719 }
4720
Namjae Jeon38673692021-07-08 12:32:27 +09004721 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004722 id = le64_to_cpu(req->VolatileFileId);
4723 pid = le64_to_cpu(req->PersistentFileId);
4724 }
4725
4726 fp = ksmbd_lookup_fd_slow(work, id, pid);
4727 if (!fp)
4728 return -ENOENT;
4729
4730 fileinfoclass = req->FileInfoClass;
4731
4732 switch (fileinfoclass) {
4733 case FILE_ACCESS_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004734 get_file_access_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004735 file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE;
4736 break;
4737
4738 case FILE_BASIC_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004739 rc = get_file_basic_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004740 file_infoclass_size = FILE_BASIC_INFORMATION_SIZE;
4741 break;
4742
4743 case FILE_STANDARD_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004744 get_file_standard_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004745 file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE;
4746 break;
4747
4748 case FILE_ALIGNMENT_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004749 get_file_alignment_info(rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004750 file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE;
4751 break;
4752
4753 case FILE_ALL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004754 rc = get_file_all_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004755 file_infoclass_size = FILE_ALL_INFORMATION_SIZE;
4756 break;
4757
4758 case FILE_ALTERNATE_NAME_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004759 get_file_alternate_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004760 file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE;
4761 break;
4762
4763 case FILE_STREAM_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004764 get_file_stream_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004765 file_infoclass_size = FILE_STREAM_INFORMATION_SIZE;
4766 break;
4767
4768 case FILE_INTERNAL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004769 get_file_internal_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004770 file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE;
4771 break;
4772
4773 case FILE_NETWORK_OPEN_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004774 rc = get_file_network_open_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004775 file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE;
4776 break;
4777
4778 case FILE_EA_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004779 get_file_ea_info(rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004780 file_infoclass_size = FILE_EA_INFORMATION_SIZE;
4781 break;
4782
4783 case FILE_FULL_EA_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004784 rc = smb2_get_ea(work, fp, req, rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004785 file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE;
4786 break;
4787
4788 case FILE_POSITION_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004789 get_file_position_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004790 file_infoclass_size = FILE_POSITION_INFORMATION_SIZE;
4791 break;
4792
4793 case FILE_MODE_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004794 get_file_mode_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004795 file_infoclass_size = FILE_MODE_INFORMATION_SIZE;
4796 break;
4797
4798 case FILE_COMPRESSION_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004799 get_file_compression_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004800 file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE;
4801 break;
4802
4803 case FILE_ATTRIBUTE_TAG_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004804 rc = get_file_attribute_tag_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004805 file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE;
4806 break;
4807 case SMB_FIND_FILE_POSIX_INFO:
4808 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004809 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004810 rc = -EOPNOTSUPP;
4811 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09004812 rc = find_file_posix_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004813 file_infoclass_size = sizeof(struct smb311_posix_qinfo);
4814 }
4815 break;
4816 default:
4817 ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n",
4818 fileinfoclass);
4819 rc = -EOPNOTSUPP;
4820 }
4821 if (!rc)
4822 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004823 rsp, work->response_buf,
Namjae Jeone2f34482021-03-16 10:49:09 +09004824 file_infoclass_size);
4825 ksmbd_fd_put(work, fp);
4826 return rc;
4827}
4828
Namjae Jeone2f34482021-03-16 10:49:09 +09004829static int smb2_get_info_filesystem(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004830 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004831 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09004832{
4833 struct ksmbd_session *sess = work->sess;
4834 struct ksmbd_conn *conn = sess->conn;
4835 struct ksmbd_share_config *share = work->tcon->share_conf;
4836 int fsinfoclass = 0;
4837 struct kstatfs stfs;
4838 struct path path;
4839 int rc = 0, len;
4840 int fs_infoclass_size = 0;
4841
Hyunchul Lee265fd192021-09-25 00:06:16 +09004842 rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09004843 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004844 pr_err("cannot create vfs path\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004845 return -EIO;
4846 }
4847
4848 rc = vfs_statfs(&path, &stfs);
4849 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004850 pr_err("cannot do stat of path %s\n", share->path);
Namjae Jeone2f34482021-03-16 10:49:09 +09004851 path_put(&path);
4852 return -EIO;
4853 }
4854
4855 fsinfoclass = req->FileInfoClass;
4856
4857 switch (fsinfoclass) {
4858 case FS_DEVICE_INFORMATION:
4859 {
4860 struct filesystem_device_info *info;
4861
4862 info = (struct filesystem_device_info *)rsp->Buffer;
4863
4864 info->DeviceType = cpu_to_le32(stfs.f_type);
4865 info->DeviceCharacteristics = cpu_to_le32(0x00000020);
4866 rsp->OutputBufferLength = cpu_to_le32(8);
Namjae Jeoncb451722021-11-03 08:08:44 +09004867 inc_rfc1001_len(work->response_buf, 8);
Namjae Jeone2f34482021-03-16 10:49:09 +09004868 fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE;
4869 break;
4870 }
4871 case FS_ATTRIBUTE_INFORMATION:
4872 {
4873 struct filesystem_attribute_info *info;
4874 size_t sz;
4875
4876 info = (struct filesystem_attribute_info *)rsp->Buffer;
4877 info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS |
4878 FILE_PERSISTENT_ACLS |
4879 FILE_UNICODE_ON_DISK |
4880 FILE_CASE_PRESERVED_NAMES |
Namjae Jeoneb817362021-05-18 10:37:59 +09004881 FILE_CASE_SENSITIVE_SEARCH |
4882 FILE_SUPPORTS_BLOCK_REFCOUNTING);
Namjae Jeone2f34482021-03-16 10:49:09 +09004883
4884 info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps);
4885
4886 info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen);
4887 len = smbConvertToUTF16((__le16 *)info->FileSystemName,
4888 "NTFS", PATH_MAX, conn->local_nls, 0);
4889 len = len * 2;
4890 info->FileSystemNameLen = cpu_to_le32(len);
4891 sz = sizeof(struct filesystem_attribute_info) - 2 + len;
4892 rsp->OutputBufferLength = cpu_to_le32(sz);
Namjae Jeoncb451722021-11-03 08:08:44 +09004893 inc_rfc1001_len(work->response_buf, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09004894 fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE;
4895 break;
4896 }
4897 case FS_VOLUME_INFORMATION:
4898 {
4899 struct filesystem_vol_info *info;
4900 size_t sz;
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004901 unsigned int serial_crc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09004902
4903 info = (struct filesystem_vol_info *)(rsp->Buffer);
4904 info->VolumeCreationTime = 0;
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004905 serial_crc = crc32_le(serial_crc, share->name,
4906 strlen(share->name));
4907 serial_crc = crc32_le(serial_crc, share->path,
4908 strlen(share->path));
4909 serial_crc = crc32_le(serial_crc, ksmbd_netbios_name(),
4910 strlen(ksmbd_netbios_name()));
Namjae Jeone2f34482021-03-16 10:49:09 +09004911 /* Taking dummy value of serial number*/
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004912 info->SerialNumber = cpu_to_le32(serial_crc);
Namjae Jeone2f34482021-03-16 10:49:09 +09004913 len = smbConvertToUTF16((__le16 *)info->VolumeLabel,
4914 share->name, PATH_MAX,
4915 conn->local_nls, 0);
4916 len = len * 2;
4917 info->VolumeLabelSize = cpu_to_le32(len);
4918 info->Reserved = 0;
4919 sz = sizeof(struct filesystem_vol_info) - 2 + len;
4920 rsp->OutputBufferLength = cpu_to_le32(sz);
Namjae Jeoncb451722021-11-03 08:08:44 +09004921 inc_rfc1001_len(work->response_buf, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09004922 fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE;
4923 break;
4924 }
4925 case FS_SIZE_INFORMATION:
4926 {
4927 struct filesystem_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004928
4929 info = (struct filesystem_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004930 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4931 info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004932 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4933 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004934 rsp->OutputBufferLength = cpu_to_le32(24);
Namjae Jeoncb451722021-11-03 08:08:44 +09004935 inc_rfc1001_len(work->response_buf, 24);
Namjae Jeone2f34482021-03-16 10:49:09 +09004936 fs_infoclass_size = FS_SIZE_INFORMATION_SIZE;
4937 break;
4938 }
4939 case FS_FULL_SIZE_INFORMATION:
4940 {
4941 struct smb2_fs_full_size_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004942
4943 info = (struct smb2_fs_full_size_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004944 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4945 info->CallerAvailableAllocationUnits =
4946 cpu_to_le64(stfs.f_bavail);
4947 info->ActualAvailableAllocationUnits =
4948 cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004949 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4950 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004951 rsp->OutputBufferLength = cpu_to_le32(32);
Namjae Jeoncb451722021-11-03 08:08:44 +09004952 inc_rfc1001_len(work->response_buf, 32);
Namjae Jeone2f34482021-03-16 10:49:09 +09004953 fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE;
4954 break;
4955 }
4956 case FS_OBJECT_ID_INFORMATION:
4957 {
4958 struct object_id_info *info;
4959
4960 info = (struct object_id_info *)(rsp->Buffer);
4961
4962 if (!user_guest(sess->user))
4963 memcpy(info->objid, user_passkey(sess->user), 16);
4964 else
4965 memset(info->objid, 0, 16);
4966
4967 info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC);
4968 info->extended_info.version = cpu_to_le32(1);
4969 info->extended_info.release = cpu_to_le32(1);
4970 info->extended_info.rel_date = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004971 memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0"));
Namjae Jeone2f34482021-03-16 10:49:09 +09004972 rsp->OutputBufferLength = cpu_to_le32(64);
Namjae Jeoncb451722021-11-03 08:08:44 +09004973 inc_rfc1001_len(work->response_buf, 64);
Namjae Jeone2f34482021-03-16 10:49:09 +09004974 fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE;
4975 break;
4976 }
4977 case FS_SECTOR_SIZE_INFORMATION:
4978 {
4979 struct smb3_fs_ss_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004980
4981 info = (struct smb3_fs_ss_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004982
Namjae Jeon131bac12021-06-22 16:20:47 +09004983 info->LogicalBytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004984 info->PhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09004985 cpu_to_le32(stfs.f_bsize);
4986 info->PhysicalBytesPerSectorForPerf = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004987 info->FSEffPhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09004988 cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004989 info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE |
4990 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE);
4991 info->ByteOffsetForSectorAlignment = 0;
4992 info->ByteOffsetForPartitionAlignment = 0;
4993 rsp->OutputBufferLength = cpu_to_le32(28);
Namjae Jeoncb451722021-11-03 08:08:44 +09004994 inc_rfc1001_len(work->response_buf, 28);
Namjae Jeone2f34482021-03-16 10:49:09 +09004995 fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE;
4996 break;
4997 }
4998 case FS_CONTROL_INFORMATION:
4999 {
5000 /*
5001 * TODO : The current implementation is based on
5002 * test result with win7(NTFS) server. It's need to
5003 * modify this to get valid Quota values
5004 * from Linux kernel
5005 */
5006 struct smb2_fs_control_info *info;
5007
5008 info = (struct smb2_fs_control_info *)(rsp->Buffer);
5009 info->FreeSpaceStartFiltering = 0;
5010 info->FreeSpaceThreshold = 0;
5011 info->FreeSpaceStopFiltering = 0;
5012 info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID);
5013 info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID);
5014 info->Padding = 0;
5015 rsp->OutputBufferLength = cpu_to_le32(48);
Namjae Jeoncb451722021-11-03 08:08:44 +09005016 inc_rfc1001_len(work->response_buf, 48);
Namjae Jeone2f34482021-03-16 10:49:09 +09005017 fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE;
5018 break;
5019 }
5020 case FS_POSIX_INFORMATION:
5021 {
5022 struct filesystem_posix_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09005023
5024 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005025 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005026 rc = -EOPNOTSUPP;
5027 } else {
5028 info = (struct filesystem_posix_info *)(rsp->Buffer);
Namjae Jeonee81cae2021-06-26 22:32:34 +09005029 info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005030 info->BlockSize = cpu_to_le32(stfs.f_bsize);
5031 info->TotalBlocks = cpu_to_le64(stfs.f_blocks);
5032 info->BlocksAvail = cpu_to_le64(stfs.f_bfree);
5033 info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail);
5034 info->TotalFileNodes = cpu_to_le64(stfs.f_files);
5035 info->FreeFileNodes = cpu_to_le64(stfs.f_ffree);
5036 rsp->OutputBufferLength = cpu_to_le32(56);
Namjae Jeoncb451722021-11-03 08:08:44 +09005037 inc_rfc1001_len(work->response_buf, 56);
Namjae Jeone2f34482021-03-16 10:49:09 +09005038 fs_infoclass_size = FS_POSIX_INFORMATION_SIZE;
5039 }
5040 break;
5041 }
5042 default:
5043 path_put(&path);
5044 return -EOPNOTSUPP;
5045 }
5046 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09005047 rsp, work->response_buf,
Namjae Jeone2f34482021-03-16 10:49:09 +09005048 fs_infoclass_size);
5049 path_put(&path);
5050 return rc;
5051}
5052
5053static int smb2_get_info_sec(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005054 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09005055 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09005056{
5057 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005058 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09005059 struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL;
5060 struct smb_fattr fattr = {{0}};
5061 struct inode *inode;
5062 __u32 secdesclen;
5063 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5064 int addition_info = le32_to_cpu(req->AdditionalInformation);
5065 int rc;
5066
Namjae Jeone294f782021-06-28 15:26:37 +09005067 if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
5068 PROTECTED_DACL_SECINFO |
5069 UNPROTECTED_DACL_SECINFO)) {
5070 pr_err("Unsupported addition info: 0x%x)\n",
5071 addition_info);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09005072
5073 pntsd->revision = cpu_to_le16(1);
5074 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED);
5075 pntsd->osidoffset = 0;
5076 pntsd->gsidoffset = 0;
5077 pntsd->sacloffset = 0;
5078 pntsd->dacloffset = 0;
5079
5080 secdesclen = sizeof(struct smb_ntsd);
5081 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
Namjae Jeoncb451722021-11-03 08:08:44 +09005082 inc_rfc1001_len(work->response_buf, secdesclen);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09005083
5084 return 0;
5085 }
5086
Namjae Jeone2f34482021-03-16 10:49:09 +09005087 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon38673692021-07-08 12:32:27 +09005088 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
5089 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005090 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005091 id = work->compound_fid;
5092 pid = work->compound_pfid;
5093 }
5094 }
5095
Namjae Jeon38673692021-07-08 12:32:27 +09005096 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005097 id = le64_to_cpu(req->VolatileFileId);
5098 pid = le64_to_cpu(req->PersistentFileId);
5099 }
5100
5101 fp = ksmbd_lookup_fd_slow(work, id, pid);
5102 if (!fp)
5103 return -ENOENT;
5104
Hyunchul Lee465d7202021-07-03 12:10:36 +09005105 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeonab0b2632021-06-29 09:20:13 +09005106 inode = file_inode(fp->filp);
Christian Brauner43205ca2021-08-23 17:13:50 +02005107 ksmbd_acls_fattr(&fattr, user_ns, inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09005108
5109 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09005110 KSMBD_SHARE_FLAG_ACL_XATTR))
Hyunchul Lee465d7202021-07-03 12:10:36 +09005111 ksmbd_vfs_get_sd_xattr(work->conn, user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005112 fp->filp->f_path.dentry, &ppntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09005113
Hyunchul Lee465d7202021-07-03 12:10:36 +09005114 rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info,
5115 &secdesclen, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09005116 posix_acl_release(fattr.cf_acls);
5117 posix_acl_release(fattr.cf_dacls);
5118 kfree(ppntsd);
5119 ksmbd_fd_put(work, fp);
5120 if (rc)
5121 return rc;
5122
5123 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
Namjae Jeoncb451722021-11-03 08:08:44 +09005124 inc_rfc1001_len(work->response_buf, secdesclen);
Namjae Jeone2f34482021-03-16 10:49:09 +09005125 return 0;
5126}
5127
5128/**
5129 * smb2_query_info() - handler for smb2 query info command
5130 * @work: smb work containing query info request buffer
5131 *
5132 * Return: 0 on success, otherwise error
5133 */
5134int smb2_query_info(struct ksmbd_work *work)
5135{
5136 struct smb2_query_info_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09005137 struct smb2_query_info_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005138 int rc = 0;
5139
Namjae Jeone2f34482021-03-16 10:49:09 +09005140 WORK_BUFFERS(work, req, rsp);
5141
5142 ksmbd_debug(SMB, "GOT query info request\n");
5143
5144 switch (req->InfoType) {
5145 case SMB2_O_INFO_FILE:
5146 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005147 rc = smb2_get_info_file(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005148 break;
5149 case SMB2_O_INFO_FILESYSTEM:
5150 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005151 rc = smb2_get_info_filesystem(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005152 break;
5153 case SMB2_O_INFO_SECURITY:
5154 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005155 rc = smb2_get_info_sec(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005156 break;
5157 default:
5158 ksmbd_debug(SMB, "InfoType %d not supported yet\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005159 req->InfoType);
Namjae Jeone2f34482021-03-16 10:49:09 +09005160 rc = -EOPNOTSUPP;
5161 }
5162
5163 if (rc < 0) {
5164 if (rc == -EACCES)
5165 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5166 else if (rc == -ENOENT)
5167 rsp->hdr.Status = STATUS_FILE_CLOSED;
5168 else if (rc == -EIO)
5169 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5170 else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0)
5171 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5172 smb2_set_err_rsp(work);
5173
5174 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005175 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005176 return rc;
5177 }
5178 rsp->StructureSize = cpu_to_le16(9);
5179 rsp->OutputBufferOffset = cpu_to_le16(72);
Namjae Jeoncb451722021-11-03 08:08:44 +09005180 inc_rfc1001_len(work->response_buf, 8);
Namjae Jeone2f34482021-03-16 10:49:09 +09005181 return 0;
5182}
5183
5184/**
5185 * smb2_close_pipe() - handler for closing IPC pipe
5186 * @work: smb work containing close request buffer
5187 *
5188 * Return: 0
5189 */
5190static noinline int smb2_close_pipe(struct ksmbd_work *work)
5191{
Namjae Jeon64b39f42021-03-30 14:25:35 +09005192 u64 id;
Namjae Jeoncb451722021-11-03 08:08:44 +09005193 struct smb2_close_req *req = smb2_get_msg(work->request_buf);
5194 struct smb2_close_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005195
5196 id = le64_to_cpu(req->VolatileFileId);
5197 ksmbd_session_rpc_close(work->sess, id);
5198
5199 rsp->StructureSize = cpu_to_le16(60);
5200 rsp->Flags = 0;
5201 rsp->Reserved = 0;
5202 rsp->CreationTime = 0;
5203 rsp->LastAccessTime = 0;
5204 rsp->LastWriteTime = 0;
5205 rsp->ChangeTime = 0;
5206 rsp->AllocationSize = 0;
5207 rsp->EndOfFile = 0;
5208 rsp->Attributes = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09005209 inc_rfc1001_len(work->response_buf, 60);
Namjae Jeone2f34482021-03-16 10:49:09 +09005210 return 0;
5211}
5212
5213/**
5214 * smb2_close() - handler for smb2 close file command
5215 * @work: smb work containing close request buffer
5216 *
5217 * Return: 0
5218 */
5219int smb2_close(struct ksmbd_work *work)
5220{
Namjae Jeon38673692021-07-08 12:32:27 +09005221 u64 volatile_id = KSMBD_NO_FID;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005222 u64 sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09005223 struct smb2_close_req *req;
5224 struct smb2_close_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005225 struct ksmbd_conn *conn = work->conn;
5226 struct ksmbd_file *fp;
5227 struct inode *inode;
5228 u64 time;
5229 int err = 0;
5230
Namjae Jeone2f34482021-03-16 10:49:09 +09005231 WORK_BUFFERS(work, req, rsp);
5232
5233 if (test_share_config_flag(work->tcon->share_conf,
5234 KSMBD_SHARE_FLAG_PIPE)) {
5235 ksmbd_debug(SMB, "IPC pipe close request\n");
5236 return smb2_close_pipe(work);
5237 }
5238
5239 sess_id = le64_to_cpu(req->hdr.SessionId);
5240 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5241 sess_id = work->compound_sid;
5242
5243 work->compound_sid = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005244 if (check_session_id(conn, sess_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005245 work->compound_sid = sess_id;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005246 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005247 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
5248 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5249 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5250 err = -EBADF;
5251 goto out;
5252 }
5253
5254 if (work->next_smb2_rcv_hdr_off &&
Namjae Jeon38673692021-07-08 12:32:27 +09005255 !has_file_id(le64_to_cpu(req->VolatileFileId))) {
5256 if (!has_file_id(work->compound_fid)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005257 /* file already closed, return FILE_CLOSED */
5258 ksmbd_debug(SMB, "file already closed\n");
5259 rsp->hdr.Status = STATUS_FILE_CLOSED;
5260 err = -EBADF;
5261 goto out;
5262 } else {
Namjae Jeon38673692021-07-08 12:32:27 +09005263 ksmbd_debug(SMB,
5264 "Compound request set FID = %llu:%llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005265 work->compound_fid,
5266 work->compound_pfid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005267 volatile_id = work->compound_fid;
5268
5269 /* file closed, stored id is not valid anymore */
5270 work->compound_fid = KSMBD_NO_FID;
5271 work->compound_pfid = KSMBD_NO_FID;
5272 }
5273 } else {
5274 volatile_id = le64_to_cpu(req->VolatileFileId);
5275 }
Namjae Jeon38673692021-07-08 12:32:27 +09005276 ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id);
Namjae Jeone2f34482021-03-16 10:49:09 +09005277
5278 rsp->StructureSize = cpu_to_le16(60);
5279 rsp->Reserved = 0;
5280
5281 if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) {
5282 fp = ksmbd_lookup_fd_fast(work, volatile_id);
5283 if (!fp) {
5284 err = -ENOENT;
5285 goto out;
5286 }
5287
Namjae Jeonab0b2632021-06-29 09:20:13 +09005288 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005289 rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
5290 rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 :
5291 cpu_to_le64(inode->i_blocks << 9);
5292 rsp->EndOfFile = cpu_to_le64(inode->i_size);
5293 rsp->Attributes = fp->f_ci->m_fattr;
5294 rsp->CreationTime = cpu_to_le64(fp->create_time);
5295 time = ksmbd_UnixTimeToNT(inode->i_atime);
5296 rsp->LastAccessTime = cpu_to_le64(time);
5297 time = ksmbd_UnixTimeToNT(inode->i_mtime);
5298 rsp->LastWriteTime = cpu_to_le64(time);
5299 time = ksmbd_UnixTimeToNT(inode->i_ctime);
5300 rsp->ChangeTime = cpu_to_le64(time);
5301 ksmbd_fd_put(work, fp);
5302 } else {
5303 rsp->Flags = 0;
5304 rsp->AllocationSize = 0;
5305 rsp->EndOfFile = 0;
5306 rsp->Attributes = 0;
5307 rsp->CreationTime = 0;
5308 rsp->LastAccessTime = 0;
5309 rsp->LastWriteTime = 0;
5310 rsp->ChangeTime = 0;
5311 }
5312
5313 err = ksmbd_close_fd(work, volatile_id);
5314out:
5315 if (err) {
5316 if (rsp->hdr.Status == 0)
5317 rsp->hdr.Status = STATUS_FILE_CLOSED;
5318 smb2_set_err_rsp(work);
5319 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09005320 inc_rfc1001_len(work->response_buf, 60);
Namjae Jeone2f34482021-03-16 10:49:09 +09005321 }
5322
5323 return 0;
5324}
5325
5326/**
5327 * smb2_echo() - handler for smb2 echo(ping) command
5328 * @work: smb work containing echo request buffer
5329 *
5330 * Return: 0
5331 */
5332int smb2_echo(struct ksmbd_work *work)
5333{
Namjae Jeoncb451722021-11-03 08:08:44 +09005334 struct smb2_echo_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005335
5336 rsp->StructureSize = cpu_to_le16(4);
5337 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09005338 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09005339 return 0;
5340}
5341
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005342static int smb2_rename(struct ksmbd_work *work,
5343 struct ksmbd_file *fp,
5344 struct user_namespace *user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +09005345 struct smb2_file_rename_info *file_info,
5346 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005347{
5348 struct ksmbd_share_config *share = fp->tcon->share_conf;
5349 char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL;
5350 char *pathname = NULL;
5351 struct path path;
5352 bool file_present = true;
5353 int rc;
5354
5355 ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n");
5356 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5357 if (!pathname)
5358 return -ENOMEM;
5359
5360 abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
5361 if (IS_ERR(abs_oldname)) {
5362 rc = -EINVAL;
5363 goto out;
5364 }
5365 old_name = strrchr(abs_oldname, '/');
Namjae Jeon64b39f42021-03-30 14:25:35 +09005366 if (old_name && old_name[1] != '\0') {
Namjae Jeone2f34482021-03-16 10:49:09 +09005367 old_name++;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005368 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005369 ksmbd_debug(SMB, "can't get last component in path %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005370 abs_oldname);
Namjae Jeone2f34482021-03-16 10:49:09 +09005371 rc = -ENOENT;
5372 goto out;
5373 }
5374
5375 new_name = smb2_get_name(share,
5376 file_info->FileName,
5377 le32_to_cpu(file_info->FileNameLength),
5378 local_nls);
5379 if (IS_ERR(new_name)) {
5380 rc = PTR_ERR(new_name);
5381 goto out;
5382 }
5383
5384 if (strchr(new_name, ':')) {
5385 int s_type;
5386 char *xattr_stream_name, *stream_name = NULL;
5387 size_t xattr_stream_size;
5388 int len;
5389
5390 rc = parse_stream_name(new_name, &stream_name, &s_type);
5391 if (rc < 0)
5392 goto out;
5393
5394 len = strlen(new_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005395 if (len > 0 && new_name[len - 1] != '/') {
Namjae Jeonbde16942021-06-28 15:23:19 +09005396 pr_err("not allow base filename in rename\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005397 rc = -ESHARE;
5398 goto out;
5399 }
5400
5401 rc = ksmbd_vfs_xattr_stream_name(stream_name,
5402 &xattr_stream_name,
5403 &xattr_stream_size,
5404 s_type);
5405 if (rc)
5406 goto out;
5407
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005408 rc = ksmbd_vfs_setxattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005409 fp->filp->f_path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09005410 xattr_stream_name,
5411 NULL, 0, 0);
5412 if (rc < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005413 pr_err("failed to store stream name in xattr: %d\n",
5414 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005415 rc = -EINVAL;
5416 goto out;
5417 }
5418
5419 goto out;
5420 }
5421
5422 ksmbd_debug(SMB, "new name %s\n", new_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005423 rc = ksmbd_vfs_kern_path(work, new_name, LOOKUP_NO_SYMLINKS, &path, 1);
5424 if (rc) {
5425 if (rc != -ENOENT)
5426 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005427 file_present = false;
Hyunchul Lee265fd192021-09-25 00:06:16 +09005428 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005429 path_put(&path);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005430 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005431
5432 if (ksmbd_share_veto_filename(share, new_name)) {
5433 rc = -ENOENT;
5434 ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name);
5435 goto out;
5436 }
5437
5438 if (file_info->ReplaceIfExists) {
5439 if (file_present) {
5440 rc = ksmbd_vfs_remove_file(work, new_name);
5441 if (rc) {
5442 if (rc != -ENOTEMPTY)
5443 rc = -EINVAL;
5444 ksmbd_debug(SMB, "cannot delete %s, rc %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005445 new_name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005446 goto out;
5447 }
5448 }
5449 } else {
5450 if (file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005451 strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005452 rc = -EEXIST;
5453 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005454 "cannot rename already existing file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005455 goto out;
5456 }
5457 }
5458
5459 rc = ksmbd_vfs_fp_rename(work, fp, new_name);
5460out:
5461 kfree(pathname);
5462 if (!IS_ERR(new_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005463 kfree(new_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005464 return rc;
5465}
5466
Namjae Jeone2f34482021-03-16 10:49:09 +09005467static int smb2_create_link(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005468 struct ksmbd_share_config *share,
5469 struct smb2_file_link_info *file_info,
Namjae Jeon9496e262021-09-29 15:41:48 +09005470 unsigned int buf_len, struct file *filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005471 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005472{
5473 char *link_name = NULL, *target_name = NULL, *pathname = NULL;
5474 struct path path;
5475 bool file_present = true;
5476 int rc;
5477
Namjae Jeon9496e262021-09-29 15:41:48 +09005478 if (buf_len < (u64)sizeof(struct smb2_file_link_info) +
5479 le32_to_cpu(file_info->FileNameLength))
5480 return -EINVAL;
5481
Namjae Jeone2f34482021-03-16 10:49:09 +09005482 ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n");
5483 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5484 if (!pathname)
5485 return -ENOMEM;
5486
5487 link_name = smb2_get_name(share,
5488 file_info->FileName,
5489 le32_to_cpu(file_info->FileNameLength),
5490 local_nls);
5491 if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) {
5492 rc = -EINVAL;
5493 goto out;
5494 }
5495
5496 ksmbd_debug(SMB, "link name is %s\n", link_name);
5497 target_name = d_path(&filp->f_path, pathname, PATH_MAX);
5498 if (IS_ERR(target_name)) {
5499 rc = -EINVAL;
5500 goto out;
5501 }
5502
5503 ksmbd_debug(SMB, "target name is %s\n", target_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005504 rc = ksmbd_vfs_kern_path(work, link_name, LOOKUP_NO_SYMLINKS, &path, 0);
5505 if (rc) {
5506 if (rc != -ENOENT)
5507 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005508 file_present = false;
Hyunchul Lee265fd192021-09-25 00:06:16 +09005509 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005510 path_put(&path);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005511 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005512
5513 if (file_info->ReplaceIfExists) {
5514 if (file_present) {
5515 rc = ksmbd_vfs_remove_file(work, link_name);
5516 if (rc) {
5517 rc = -EINVAL;
5518 ksmbd_debug(SMB, "cannot delete %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005519 link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005520 goto out;
5521 }
5522 }
5523 } else {
5524 if (file_present) {
5525 rc = -EEXIST;
5526 ksmbd_debug(SMB, "link already exists\n");
5527 goto out;
5528 }
5529 }
5530
5531 rc = ksmbd_vfs_link(work, target_name, link_name);
5532 if (rc)
5533 rc = -EINVAL;
5534out:
5535 if (!IS_ERR(link_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005536 kfree(link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005537 kfree(pathname);
5538 return rc;
5539}
5540
Namjae Jeon9496e262021-09-29 15:41:48 +09005541static int set_file_basic_info(struct ksmbd_file *fp,
5542 struct smb2_file_basic_info *file_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005543 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005544{
Namjae Jeone2f34482021-03-16 10:49:09 +09005545 struct iattr attrs;
Namjae Jeone2f34482021-03-16 10:49:09 +09005546 struct file *filp;
5547 struct inode *inode;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005548 struct user_namespace *user_ns;
Namjae Jeon4ffd5262021-09-07 08:15:21 +09005549 int rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09005550
Marios Makassikis7adfd4f2021-04-27 15:30:22 +09005551 if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005552 return -EACCES;
5553
Namjae Jeone2f34482021-03-16 10:49:09 +09005554 attrs.ia_valid = 0;
5555 filp = fp->filp;
5556 inode = file_inode(filp);
Hyunchul Lee465d7202021-07-03 12:10:36 +09005557 user_ns = file_mnt_user_ns(filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005558
5559 if (file_info->CreationTime)
5560 fp->create_time = le64_to_cpu(file_info->CreationTime);
5561
5562 if (file_info->LastAccessTime) {
5563 attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime);
5564 attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
5565 }
5566
Namjae Jeon64e78752021-10-03 13:19:00 +09005567 attrs.ia_valid |= ATTR_CTIME;
5568 if (file_info->ChangeTime)
Christian Braunerdb7fb6f2021-08-26 10:07:05 +09005569 attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime);
Namjae Jeon64e78752021-10-03 13:19:00 +09005570 else
5571 attrs.ia_ctime = inode->i_ctime;
Namjae Jeone2f34482021-03-16 10:49:09 +09005572
5573 if (file_info->LastWriteTime) {
5574 attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime);
5575 attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
5576 }
5577
5578 if (file_info->Attributes) {
5579 if (!S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005580 file_info->Attributes & ATTR_DIRECTORY_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005581 pr_err("can't change a file to a directory\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005582 return -EINVAL;
5583 }
5584
5585 if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == ATTR_NORMAL_LE))
5586 fp->f_ci->m_fattr = file_info->Attributes |
5587 (fp->f_ci->m_fattr & ATTR_DIRECTORY_LE);
5588 }
5589
5590 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
5591 (file_info->CreationTime || file_info->Attributes)) {
5592 struct xattr_dos_attrib da = {0};
5593
5594 da.version = 4;
5595 da.itime = fp->itime;
5596 da.create_time = fp->create_time;
5597 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
5598 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
5599 XATTR_DOSINFO_ITIME;
5600
Hyunchul Lee465d7202021-07-03 12:10:36 +09005601 rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005602 filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09005603 if (rc)
5604 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005605 "failed to restore file attribute in EA\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005606 rc = 0;
5607 }
5608
Namjae Jeone2f34482021-03-16 10:49:09 +09005609 if (attrs.ia_valid) {
5610 struct dentry *dentry = filp->f_path.dentry;
5611 struct inode *inode = d_inode(dentry);
5612
5613 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
5614 return -EACCES;
5615
Namjae Jeone2f34482021-03-16 10:49:09 +09005616 inode_lock(inode);
Namjae Jeon64e78752021-10-03 13:19:00 +09005617 inode->i_ctime = attrs.ia_ctime;
5618 attrs.ia_valid &= ~ATTR_CTIME;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005619 rc = notify_change(user_ns, dentry, &attrs, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09005620 inode_unlock(inode);
5621 }
Christian Braunereb5784f2021-08-23 17:13:55 +02005622 return rc;
Namjae Jeone2f34482021-03-16 10:49:09 +09005623}
5624
5625static int set_file_allocation_info(struct ksmbd_work *work,
Namjae Jeon9496e262021-09-29 15:41:48 +09005626 struct ksmbd_file *fp,
5627 struct smb2_file_alloc_info *file_alloc_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005628{
5629 /*
5630 * TODO : It's working fine only when store dos attributes
5631 * is not yes. need to implement a logic which works
5632 * properly with any smb.conf option
5633 */
5634
Namjae Jeone2f34482021-03-16 10:49:09 +09005635 loff_t alloc_blks;
5636 struct inode *inode;
5637 int rc;
5638
Marios Makassikisa2996692021-04-27 15:29:01 +09005639 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005640 return -EACCES;
5641
Namjae Jeone2f34482021-03-16 10:49:09 +09005642 alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9;
5643 inode = file_inode(fp->filp);
5644
5645 if (alloc_blks > inode->i_blocks) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005646 smb_break_all_levII_oplock(work, fp, 1);
5647 rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
5648 alloc_blks * 512);
Namjae Jeone2f34482021-03-16 10:49:09 +09005649 if (rc && rc != -EOPNOTSUPP) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005650 pr_err("vfs_fallocate is failed : %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005651 return rc;
5652 }
5653 } else if (alloc_blks < inode->i_blocks) {
5654 loff_t size;
5655
5656 /*
5657 * Allocation size could be smaller than original one
5658 * which means allocated blocks in file should be
5659 * deallocated. use truncate to cut out it, but inode
5660 * size is also updated with truncate offset.
5661 * inode size is retained by backup inode size.
5662 */
5663 size = i_size_read(inode);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005664 rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512);
Namjae Jeone2f34482021-03-16 10:49:09 +09005665 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005666 pr_err("truncate failed! filename : %s, err %d\n",
5667 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005668 return rc;
5669 }
5670 if (size < alloc_blks * 512)
5671 i_size_write(inode, size);
5672 }
5673 return 0;
5674}
5675
Namjae Jeon64b39f42021-03-30 14:25:35 +09005676static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005677 struct smb2_file_eof_info *file_eof_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005678{
Namjae Jeone2f34482021-03-16 10:49:09 +09005679 loff_t newsize;
5680 struct inode *inode;
5681 int rc;
5682
Marios Makassikisa2996692021-04-27 15:29:01 +09005683 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005684 return -EACCES;
5685
Namjae Jeone2f34482021-03-16 10:49:09 +09005686 newsize = le64_to_cpu(file_eof_info->EndOfFile);
5687 inode = file_inode(fp->filp);
5688
5689 /*
5690 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called
5691 * on FAT32 shared device, truncate execution time is too long
5692 * and network error could cause from windows client. because
5693 * truncate of some filesystem like FAT32 fill zero data in
5694 * truncated range.
5695 */
5696 if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) {
5697 ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005698 fp->filename, newsize);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005699 rc = ksmbd_vfs_truncate(work, fp, newsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005700 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005701 ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005702 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005703 if (rc != -EAGAIN)
5704 rc = -EBADF;
5705 return rc;
5706 }
5707 }
5708 return 0;
5709}
5710
Namjae Jeon64b39f42021-03-30 14:25:35 +09005711static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005712 struct smb2_file_rename_info *rename_info,
5713 unsigned int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005714{
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005715 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09005716 struct ksmbd_file *parent_fp;
Namjae Jeon12202c02021-06-29 09:23:56 +09005717 struct dentry *parent;
5718 struct dentry *dentry = fp->filp->f_path.dentry;
5719 int ret;
Namjae Jeone2f34482021-03-16 10:49:09 +09005720
5721 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005722 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005723 return -EACCES;
5724 }
5725
Namjae Jeon9496e262021-09-29 15:41:48 +09005726 if (buf_len < (u64)sizeof(struct smb2_file_rename_info) +
5727 le32_to_cpu(rename_info->FileNameLength))
5728 return -EINVAL;
5729
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005730 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005731 if (ksmbd_stream_fd(fp))
5732 goto next;
5733
Namjae Jeon12202c02021-06-29 09:23:56 +09005734 parent = dget_parent(dentry);
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005735 ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
Namjae Jeon12202c02021-06-29 09:23:56 +09005736 if (ret) {
5737 dput(parent);
5738 return ret;
5739 }
5740
5741 parent_fp = ksmbd_lookup_fd_inode(d_inode(parent));
5742 inode_unlock(d_inode(parent));
5743 dput(parent);
5744
Namjae Jeone2f34482021-03-16 10:49:09 +09005745 if (parent_fp) {
5746 if (parent_fp->daccess & FILE_DELETE_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005747 pr_err("parent dir is opened with delete access\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005748 return -ESHARE;
5749 }
5750 }
5751next:
Namjae Jeon9496e262021-09-29 15:41:48 +09005752 return smb2_rename(work, fp, user_ns, rename_info,
Namjae Jeone2f34482021-03-16 10:49:09 +09005753 work->sess->conn->local_nls);
5754}
5755
Namjae Jeon9496e262021-09-29 15:41:48 +09005756static int set_file_disposition_info(struct ksmbd_file *fp,
5757 struct smb2_file_disposition_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005758{
Namjae Jeone2f34482021-03-16 10:49:09 +09005759 struct inode *inode;
5760
5761 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005762 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005763 return -EACCES;
5764 }
5765
5766 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005767 if (file_info->DeletePending) {
5768 if (S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005769 ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY)
Namjae Jeone2f34482021-03-16 10:49:09 +09005770 return -EBUSY;
5771 ksmbd_set_inode_pending_delete(fp);
5772 } else {
5773 ksmbd_clear_inode_pending_delete(fp);
5774 }
5775 return 0;
5776}
5777
Namjae Jeon9496e262021-09-29 15:41:48 +09005778static int set_file_position_info(struct ksmbd_file *fp,
5779 struct smb2_file_pos_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005780{
Namjae Jeone2f34482021-03-16 10:49:09 +09005781 loff_t current_byte_offset;
Namjae Jeonee81cae2021-06-26 22:32:34 +09005782 unsigned long sector_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09005783 struct inode *inode;
5784
5785 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005786 current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset);
Namjae Jeonee81cae2021-06-26 22:32:34 +09005787 sector_size = inode->i_sb->s_blocksize;
Namjae Jeone2f34482021-03-16 10:49:09 +09005788
5789 if (current_byte_offset < 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09005790 (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE &&
5791 current_byte_offset & (sector_size - 1))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005792 pr_err("CurrentByteOffset is not valid : %llu\n",
5793 current_byte_offset);
Namjae Jeone2f34482021-03-16 10:49:09 +09005794 return -EINVAL;
5795 }
5796
5797 fp->filp->f_pos = current_byte_offset;
5798 return 0;
5799}
5800
Namjae Jeon9496e262021-09-29 15:41:48 +09005801static int set_file_mode_info(struct ksmbd_file *fp,
5802 struct smb2_file_mode_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005803{
Namjae Jeone2f34482021-03-16 10:49:09 +09005804 __le32 mode;
5805
Namjae Jeone2f34482021-03-16 10:49:09 +09005806 mode = file_info->Mode;
5807
Namjae Jeon64b39f42021-03-30 14:25:35 +09005808 if ((mode & ~FILE_MODE_INFO_MASK) ||
5809 (mode & FILE_SYNCHRONOUS_IO_ALERT_LE &&
5810 mode & FILE_SYNCHRONOUS_IO_NONALERT_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005811 pr_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode));
Namjae Jeone2f34482021-03-16 10:49:09 +09005812 return -EINVAL;
5813 }
5814
5815 /*
5816 * TODO : need to implement consideration for
5817 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT
5818 */
5819 ksmbd_vfs_set_fadvise(fp->filp, mode);
5820 fp->coption = mode;
5821 return 0;
5822}
5823
5824/**
5825 * smb2_set_info_file() - handler for smb2 set info command
5826 * @work: smb work containing set info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09005827 * @fp: ksmbd_file pointer
5828 * @info_class: smb2 set info class
5829 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09005830 *
5831 * Return: 0 on success, otherwise error
5832 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH
5833 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09005834static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005835 struct smb2_set_info_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09005836 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005837{
Namjae Jeon9496e262021-09-29 15:41:48 +09005838 unsigned int buf_len = le32_to_cpu(req->BufferLength);
5839
5840 switch (req->FileInfoClass) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005841 case FILE_BASIC_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005842 {
5843 if (buf_len < sizeof(struct smb2_file_basic_info))
5844 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005845
Namjae Jeon9496e262021-09-29 15:41:48 +09005846 return set_file_basic_info(fp, (struct smb2_file_basic_info *)req->Buffer, share);
5847 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005848 case FILE_ALLOCATION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005849 {
5850 if (buf_len < sizeof(struct smb2_file_alloc_info))
5851 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005852
Namjae Jeon9496e262021-09-29 15:41:48 +09005853 return set_file_allocation_info(work, fp,
5854 (struct smb2_file_alloc_info *)req->Buffer);
5855 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005856 case FILE_END_OF_FILE_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005857 {
5858 if (buf_len < sizeof(struct smb2_file_eof_info))
5859 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005860
Namjae Jeon9496e262021-09-29 15:41:48 +09005861 return set_end_of_file_info(work, fp,
5862 (struct smb2_file_eof_info *)req->Buffer);
5863 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005864 case FILE_RENAME_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005865 {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005866 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005867 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005868 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005869 return -EACCES;
5870 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005871
Namjae Jeon9496e262021-09-29 15:41:48 +09005872 if (buf_len < sizeof(struct smb2_file_rename_info))
5873 return -EINVAL;
5874
5875 return set_rename_info(work, fp,
5876 (struct smb2_file_rename_info *)req->Buffer,
5877 buf_len);
5878 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005879 case FILE_LINK_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005880 {
5881 if (buf_len < sizeof(struct smb2_file_link_info))
5882 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005883
Namjae Jeon9496e262021-09-29 15:41:48 +09005884 return smb2_create_link(work, work->tcon->share_conf,
5885 (struct smb2_file_link_info *)req->Buffer,
5886 buf_len, fp->filp,
5887 work->sess->conn->local_nls);
5888 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005889 case FILE_DISPOSITION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005890 {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005891 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005892 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005893 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005894 return -EACCES;
5895 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005896
Namjae Jeon9496e262021-09-29 15:41:48 +09005897 if (buf_len < sizeof(struct smb2_file_disposition_info))
5898 return -EINVAL;
5899
5900 return set_file_disposition_info(fp,
5901 (struct smb2_file_disposition_info *)req->Buffer);
5902 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005903 case FILE_FULL_EA_INFORMATION:
5904 {
5905 if (!(fp->daccess & FILE_WRITE_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005906 pr_err("Not permitted to write ext attr: 0x%x\n",
5907 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005908 return -EACCES;
5909 }
5910
Namjae Jeon9496e262021-09-29 15:41:48 +09005911 if (buf_len < sizeof(struct smb2_ea_info))
5912 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005913
Namjae Jeon9496e262021-09-29 15:41:48 +09005914 return smb2_set_ea((struct smb2_ea_info *)req->Buffer,
5915 buf_len, &fp->filp->f_path);
5916 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005917 case FILE_POSITION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005918 {
5919 if (buf_len < sizeof(struct smb2_file_pos_info))
5920 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005921
Namjae Jeon9496e262021-09-29 15:41:48 +09005922 return set_file_position_info(fp, (struct smb2_file_pos_info *)req->Buffer);
5923 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005924 case FILE_MODE_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005925 {
5926 if (buf_len < sizeof(struct smb2_file_mode_info))
5927 return -EINVAL;
5928
5929 return set_file_mode_info(fp, (struct smb2_file_mode_info *)req->Buffer);
5930 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005931 }
5932
Namjae Jeon9496e262021-09-29 15:41:48 +09005933 pr_err("Unimplemented Fileinfoclass :%d\n", req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09005934 return -EOPNOTSUPP;
5935}
5936
Namjae Jeon64b39f42021-03-30 14:25:35 +09005937static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005938 char *buffer, int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005939{
5940 struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer;
5941
5942 fp->saccess |= FILE_SHARE_DELETE_LE;
5943
Hyunchul Leeef24c962021-06-30 18:25:52 +09005944 return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd,
Namjae Jeone2f34482021-03-16 10:49:09 +09005945 buf_len, false);
5946}
5947
5948/**
5949 * smb2_set_info() - handler for smb2 set info command handler
5950 * @work: smb work containing set info request buffer
5951 *
5952 * Return: 0 on success, otherwise error
5953 */
5954int smb2_set_info(struct ksmbd_work *work)
5955{
5956 struct smb2_set_info_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09005957 struct smb2_set_info_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005958 struct ksmbd_file *fp;
5959 int rc = 0;
5960 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5961
5962 ksmbd_debug(SMB, "Received set info request\n");
5963
Namjae Jeone2f34482021-03-16 10:49:09 +09005964 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09005965 req = ksmbd_req_buf_next(work);
5966 rsp = ksmbd_resp_buf_next(work);
Namjae Jeon38673692021-07-08 12:32:27 +09005967 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
5968 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005969 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005970 id = work->compound_fid;
5971 pid = work->compound_pfid;
5972 }
5973 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09005974 req = smb2_get_msg(work->request_buf);
5975 rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005976 }
5977
Namjae Jeon38673692021-07-08 12:32:27 +09005978 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005979 id = le64_to_cpu(req->VolatileFileId);
5980 pid = le64_to_cpu(req->PersistentFileId);
5981 }
5982
5983 fp = ksmbd_lookup_fd_slow(work, id, pid);
5984 if (!fp) {
5985 ksmbd_debug(SMB, "Invalid id for close: %u\n", id);
5986 rc = -ENOENT;
5987 goto err_out;
5988 }
5989
5990 switch (req->InfoType) {
5991 case SMB2_O_INFO_FILE:
5992 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
Namjae Jeon9496e262021-09-29 15:41:48 +09005993 rc = smb2_set_info_file(work, fp, req, work->tcon->share_conf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005994 break;
5995 case SMB2_O_INFO_SECURITY:
5996 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
Namjae Jeone70e3922021-08-21 23:26:01 +09005997 if (ksmbd_override_fsids(work)) {
5998 rc = -ENOMEM;
5999 goto err_out;
6000 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006001 rc = smb2_set_info_sec(fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09006002 le32_to_cpu(req->AdditionalInformation),
6003 req->Buffer,
6004 le32_to_cpu(req->BufferLength));
Namjae Jeone70e3922021-08-21 23:26:01 +09006005 ksmbd_revert_fsids(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09006006 break;
6007 default:
6008 rc = -EOPNOTSUPP;
6009 }
6010
6011 if (rc < 0)
6012 goto err_out;
6013
6014 rsp->StructureSize = cpu_to_le16(2);
Namjae Jeoncb451722021-11-03 08:08:44 +09006015 inc_rfc1001_len(work->response_buf, 2);
Namjae Jeone2f34482021-03-16 10:49:09 +09006016 ksmbd_fd_put(work, fp);
6017 return 0;
6018
6019err_out:
Hyunchul Lee265fd192021-09-25 00:06:16 +09006020 if (rc == -EACCES || rc == -EPERM || rc == -EXDEV)
Namjae Jeone2f34482021-03-16 10:49:09 +09006021 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6022 else if (rc == -EINVAL)
6023 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6024 else if (rc == -ESHARE)
6025 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6026 else if (rc == -ENOENT)
6027 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
6028 else if (rc == -EBUSY || rc == -ENOTEMPTY)
6029 rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY;
6030 else if (rc == -EAGAIN)
6031 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
Namjae Jeonff1d5722021-04-13 13:18:10 +09006032 else if (rc == -EBADF || rc == -ESTALE)
Namjae Jeone2f34482021-03-16 10:49:09 +09006033 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6034 else if (rc == -EEXIST)
6035 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
6036 else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP)
6037 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
6038 smb2_set_err_rsp(work);
6039 ksmbd_fd_put(work, fp);
Namjae Jeon070fb212021-05-26 17:57:12 +09006040 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09006041 return rc;
6042}
6043
6044/**
6045 * smb2_read_pipe() - handler for smb2 read from IPC pipe
6046 * @work: smb work containing read IPC pipe command buffer
6047 *
6048 * Return: 0 on success, otherwise error
6049 */
6050static noinline int smb2_read_pipe(struct ksmbd_work *work)
6051{
6052 int nbytes = 0, err;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006053 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09006054 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeoncb451722021-11-03 08:08:44 +09006055 struct smb2_read_req *req = smb2_get_msg(work->request_buf);
6056 struct smb2_read_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006057
6058 id = le64_to_cpu(req->VolatileFileId);
6059
Namjae Jeoncb451722021-11-03 08:08:44 +09006060 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006061 rpc_resp = ksmbd_rpc_read(work->sess, id);
6062 if (rpc_resp) {
6063 if (rpc_resp->flags != KSMBD_RPC_OK) {
6064 err = -EINVAL;
6065 goto out;
6066 }
6067
6068 work->aux_payload_buf =
Namjae Jeon79f6b112021-04-02 12:47:14 +09006069 kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006070 if (!work->aux_payload_buf) {
6071 err = -ENOMEM;
6072 goto out;
6073 }
6074
6075 memcpy(work->aux_payload_buf, rpc_resp->payload,
Namjae Jeon070fb212021-05-26 17:57:12 +09006076 rpc_resp->payload_sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09006077
6078 nbytes = rpc_resp->payload_sz;
Namjae Jeoncb451722021-11-03 08:08:44 +09006079 work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09006080 work->aux_payload_sz = nbytes;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006081 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006082 }
6083
6084 rsp->StructureSize = cpu_to_le16(17);
6085 rsp->DataOffset = 80;
6086 rsp->Reserved = 0;
6087 rsp->DataLength = cpu_to_le32(nbytes);
6088 rsp->DataRemaining = 0;
Ronnie Sahlberg699230f2021-09-09 12:26:12 +09006089 rsp->Flags = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006090 inc_rfc1001_len(work->response_buf, nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09006091 return 0;
6092
6093out:
6094 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
6095 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006096 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006097 return err;
6098}
6099
6100static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006101 struct smb2_read_req *req, void *data_buf,
6102 size_t length)
Namjae Jeone2f34482021-03-16 10:49:09 +09006103{
6104 struct smb2_buffer_desc_v1 *desc =
6105 (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6106 int err;
6107
Namjae Jeon64b39f42021-03-30 14:25:35 +09006108 if (work->conn->dialect == SMB30_PROT_ID &&
6109 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09006110 return -EINVAL;
6111
Namjae Jeon64b39f42021-03-30 14:25:35 +09006112 if (req->ReadChannelInfoOffset == 0 ||
6113 le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09006114 return -EINVAL;
6115
6116 work->need_invalidate_rkey =
6117 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6118 work->remote_key = le32_to_cpu(desc->token);
6119
Namjae Jeon64b39f42021-03-30 14:25:35 +09006120 err = ksmbd_conn_rdma_write(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006121 le32_to_cpu(desc->token),
6122 le64_to_cpu(desc->offset),
6123 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006124 if (err)
6125 return err;
6126
6127 return length;
6128}
6129
6130/**
6131 * smb2_read() - handler for smb2 read from file
6132 * @work: smb work containing read command buffer
6133 *
6134 * Return: 0 on success, otherwise error
6135 */
6136int smb2_read(struct ksmbd_work *work)
6137{
6138 struct ksmbd_conn *conn = work->conn;
6139 struct smb2_read_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006140 struct smb2_read_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09006141 struct ksmbd_file *fp;
6142 loff_t offset;
6143 size_t length, mincount;
6144 ssize_t nbytes = 0, remain_bytes = 0;
6145 int err = 0;
6146
Namjae Jeone2f34482021-03-16 10:49:09 +09006147 WORK_BUFFERS(work, req, rsp);
6148
6149 if (test_share_config_flag(work->tcon->share_conf,
6150 KSMBD_SHARE_FLAG_PIPE)) {
6151 ksmbd_debug(SMB, "IPC pipe read request\n");
6152 return smb2_read_pipe(work);
6153 }
6154
Namjae Jeon070fb212021-05-26 17:57:12 +09006155 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
6156 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006157 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006158 err = -ENOENT;
6159 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006160 }
6161
6162 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006163 pr_err("Not permitted to read : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09006164 err = -EACCES;
6165 goto out;
6166 }
6167
6168 offset = le64_to_cpu(req->Offset);
6169 length = le32_to_cpu(req->Length);
6170 mincount = le32_to_cpu(req->MinimumCount);
6171
6172 if (length > conn->vals->max_read_size) {
6173 ksmbd_debug(SMB, "limiting read size to max size(%u)\n",
6174 conn->vals->max_read_size);
6175 err = -EINVAL;
6176 goto out;
6177 }
6178
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006179 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6180 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006181
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006182 work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006183 if (!work->aux_payload_buf) {
Dan Carpenterc1ea1112021-03-22 17:50:11 +03006184 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09006185 goto out;
6186 }
6187
6188 nbytes = ksmbd_vfs_read(work, fp, length, &offset);
6189 if (nbytes < 0) {
6190 err = nbytes;
6191 goto out;
6192 }
6193
6194 if ((nbytes == 0 && length != 0) || nbytes < mincount) {
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006195 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006196 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006197 rsp->hdr.Status = STATUS_END_OF_FILE;
6198 smb2_set_err_rsp(work);
6199 ksmbd_fd_put(work, fp);
6200 return 0;
6201 }
6202
6203 ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006204 nbytes, offset, mincount);
Namjae Jeone2f34482021-03-16 10:49:09 +09006205
6206 if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006207 req->Channel == SMB2_CHANNEL_RDMA_V1) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006208 /* write data to the client using rdma channel */
6209 remain_bytes = smb2_read_rdma_channel(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09006210 work->aux_payload_buf,
6211 nbytes);
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006212 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006213 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006214
6215 nbytes = 0;
6216 if (remain_bytes < 0) {
6217 err = (int)remain_bytes;
6218 goto out;
6219 }
6220 }
6221
6222 rsp->StructureSize = cpu_to_le16(17);
6223 rsp->DataOffset = 80;
6224 rsp->Reserved = 0;
6225 rsp->DataLength = cpu_to_le32(nbytes);
6226 rsp->DataRemaining = cpu_to_le32(remain_bytes);
Ronnie Sahlberg699230f2021-09-09 12:26:12 +09006227 rsp->Flags = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006228 inc_rfc1001_len(work->response_buf, 16);
6229 work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09006230 work->aux_payload_sz = nbytes;
Namjae Jeoncb451722021-11-03 08:08:44 +09006231 inc_rfc1001_len(work->response_buf, nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09006232 ksmbd_fd_put(work, fp);
6233 return 0;
6234
6235out:
6236 if (err) {
6237 if (err == -EISDIR)
6238 rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST;
6239 else if (err == -EAGAIN)
6240 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6241 else if (err == -ENOENT)
6242 rsp->hdr.Status = STATUS_FILE_CLOSED;
6243 else if (err == -EACCES)
6244 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6245 else if (err == -ESHARE)
6246 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6247 else if (err == -EINVAL)
6248 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6249 else
6250 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6251
6252 smb2_set_err_rsp(work);
6253 }
6254 ksmbd_fd_put(work, fp);
6255 return err;
6256}
6257
6258/**
6259 * smb2_write_pipe() - handler for smb2 write on IPC pipe
6260 * @work: smb work containing write IPC pipe command buffer
6261 *
6262 * Return: 0 on success, otherwise error
6263 */
6264static noinline int smb2_write_pipe(struct ksmbd_work *work)
6265{
Namjae Jeoncb451722021-11-03 08:08:44 +09006266 struct smb2_write_req *req = smb2_get_msg(work->request_buf);
6267 struct smb2_write_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006268 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006269 u64 id = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09006270 int err = 0, ret = 0;
6271 char *data_buf;
6272 size_t length;
6273
6274 length = le32_to_cpu(req->Length);
6275 id = le64_to_cpu(req->VolatileFileId);
6276
6277 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeoncb451722021-11-03 08:08:44 +09006278 offsetof(struct smb2_write_req, Buffer)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006279 data_buf = (char *)&req->Buffer[0];
6280 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09006281 if ((u64)le16_to_cpu(req->DataOffset) + length >
6282 get_rfc1002_len(work->request_buf)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006283 pr_err("invalid write data offset %u, smb_len %u\n",
6284 le16_to_cpu(req->DataOffset),
Namjae Jeoncb451722021-11-03 08:08:44 +09006285 get_rfc1002_len(work->request_buf));
Namjae Jeone2f34482021-03-16 10:49:09 +09006286 err = -EINVAL;
6287 goto out;
6288 }
6289
6290 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6291 le16_to_cpu(req->DataOffset));
6292 }
6293
6294 rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length);
6295 if (rpc_resp) {
6296 if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
6297 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006298 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006299 smb2_set_err_rsp(work);
6300 return -EOPNOTSUPP;
6301 }
6302 if (rpc_resp->flags != KSMBD_RPC_OK) {
6303 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6304 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006305 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006306 return ret;
6307 }
Namjae Jeon79f6b112021-04-02 12:47:14 +09006308 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006309 }
6310
6311 rsp->StructureSize = cpu_to_le16(17);
6312 rsp->DataOffset = 0;
6313 rsp->Reserved = 0;
6314 rsp->DataLength = cpu_to_le32(length);
6315 rsp->DataRemaining = 0;
6316 rsp->Reserved2 = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006317 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006318 return 0;
6319out:
6320 if (err) {
6321 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6322 smb2_set_err_rsp(work);
6323 }
6324
6325 return err;
6326}
6327
6328static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006329 struct smb2_write_req *req,
6330 struct ksmbd_file *fp,
6331 loff_t offset, size_t length, bool sync)
Namjae Jeone2f34482021-03-16 10:49:09 +09006332{
6333 struct smb2_buffer_desc_v1 *desc;
6334 char *data_buf;
6335 int ret;
6336 ssize_t nbytes;
6337
6338 desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6339
6340 if (work->conn->dialect == SMB30_PROT_ID &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006341 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09006342 return -EINVAL;
6343
6344 if (req->Length != 0 || req->DataOffset != 0)
6345 return -EINVAL;
6346
Namjae Jeon64b39f42021-03-30 14:25:35 +09006347 if (req->WriteChannelInfoOffset == 0 ||
6348 le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09006349 return -EINVAL;
6350
6351 work->need_invalidate_rkey =
6352 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6353 work->remote_key = le32_to_cpu(desc->token);
6354
Namjae Jeon79f6b112021-04-02 12:47:14 +09006355 data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006356 if (!data_buf)
6357 return -ENOMEM;
6358
6359 ret = ksmbd_conn_rdma_read(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006360 le32_to_cpu(desc->token),
6361 le64_to_cpu(desc->offset),
6362 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006363 if (ret < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09006364 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006365 return ret;
6366 }
6367
Namjae Jeon64b39f42021-03-30 14:25:35 +09006368 ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006369 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006370 if (ret < 0)
6371 return ret;
6372
6373 return nbytes;
6374}
6375
6376/**
6377 * smb2_write() - handler for smb2 write from file
6378 * @work: smb work containing write command buffer
6379 *
6380 * Return: 0 on success, otherwise error
6381 */
6382int smb2_write(struct ksmbd_work *work)
6383{
6384 struct smb2_write_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006385 struct smb2_write_rsp *rsp;
Namjae Jeonbcd62a32021-05-10 09:08:19 +09006386 struct ksmbd_file *fp = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006387 loff_t offset;
6388 size_t length;
6389 ssize_t nbytes;
6390 char *data_buf;
6391 bool writethrough = false;
6392 int err = 0;
6393
Namjae Jeone2f34482021-03-16 10:49:09 +09006394 WORK_BUFFERS(work, req, rsp);
6395
Namjae Jeon64b39f42021-03-30 14:25:35 +09006396 if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006397 ksmbd_debug(SMB, "IPC pipe write request\n");
6398 return smb2_write_pipe(work);
6399 }
6400
6401 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
6402 ksmbd_debug(SMB, "User does not have write permission\n");
6403 err = -EACCES;
6404 goto out;
6405 }
6406
Namjae Jeon64b39f42021-03-30 14:25:35 +09006407 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
Namjae Jeon070fb212021-05-26 17:57:12 +09006408 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006409 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006410 err = -ENOENT;
6411 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006412 }
6413
6414 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006415 pr_err("Not permitted to write : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09006416 err = -EACCES;
6417 goto out;
6418 }
6419
6420 offset = le64_to_cpu(req->Offset);
6421 length = le32_to_cpu(req->Length);
6422
6423 if (length > work->conn->vals->max_write_size) {
6424 ksmbd_debug(SMB, "limiting write size to max size(%u)\n",
6425 work->conn->vals->max_write_size);
6426 err = -EINVAL;
6427 goto out;
6428 }
6429
6430 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6431 writethrough = true;
6432
6433 if (req->Channel != SMB2_CHANNEL_RDMA_V1 &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006434 req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006435 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeoncb451722021-11-03 08:08:44 +09006436 offsetof(struct smb2_write_req, Buffer)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006437 data_buf = (char *)&req->Buffer[0];
6438 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09006439 if ((u64)le16_to_cpu(req->DataOffset) + length >
6440 get_rfc1002_len(work->request_buf)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006441 pr_err("invalid write data offset %u, smb_len %u\n",
6442 le16_to_cpu(req->DataOffset),
Namjae Jeoncb451722021-11-03 08:08:44 +09006443 get_rfc1002_len(work->request_buf));
Namjae Jeone2f34482021-03-16 10:49:09 +09006444 err = -EINVAL;
6445 goto out;
6446 }
6447
6448 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6449 le16_to_cpu(req->DataOffset));
6450 }
6451
6452 ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags));
6453 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6454 writethrough = true;
6455
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006456 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6457 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006458 err = ksmbd_vfs_write(work, fp, data_buf, length, &offset,
6459 writethrough, &nbytes);
6460 if (err < 0)
6461 goto out;
6462 } else {
6463 /* read data from the client using rdma channel, and
6464 * write the data.
6465 */
6466 nbytes = smb2_write_rdma_channel(work, req, fp, offset,
Namjae Jeon070fb212021-05-26 17:57:12 +09006467 le32_to_cpu(req->RemainingBytes),
6468 writethrough);
Namjae Jeone2f34482021-03-16 10:49:09 +09006469 if (nbytes < 0) {
6470 err = (int)nbytes;
6471 goto out;
6472 }
6473 }
6474
6475 rsp->StructureSize = cpu_to_le16(17);
6476 rsp->DataOffset = 0;
6477 rsp->Reserved = 0;
6478 rsp->DataLength = cpu_to_le32(nbytes);
6479 rsp->DataRemaining = 0;
6480 rsp->Reserved2 = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006481 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006482 ksmbd_fd_put(work, fp);
6483 return 0;
6484
6485out:
6486 if (err == -EAGAIN)
6487 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6488 else if (err == -ENOSPC || err == -EFBIG)
6489 rsp->hdr.Status = STATUS_DISK_FULL;
6490 else if (err == -ENOENT)
6491 rsp->hdr.Status = STATUS_FILE_CLOSED;
6492 else if (err == -EACCES)
6493 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6494 else if (err == -ESHARE)
6495 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6496 else if (err == -EINVAL)
6497 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6498 else
6499 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6500
6501 smb2_set_err_rsp(work);
6502 ksmbd_fd_put(work, fp);
6503 return err;
6504}
6505
6506/**
6507 * smb2_flush() - handler for smb2 flush file - fsync
6508 * @work: smb work containing flush command buffer
6509 *
6510 * Return: 0 on success, otherwise error
6511 */
6512int smb2_flush(struct ksmbd_work *work)
6513{
6514 struct smb2_flush_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006515 struct smb2_flush_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09006516 int err;
6517
Namjae Jeone2f34482021-03-16 10:49:09 +09006518 WORK_BUFFERS(work, req, rsp);
6519
6520 ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006521 le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006522
6523 err = ksmbd_vfs_fsync(work,
6524 le64_to_cpu(req->VolatileFileId),
6525 le64_to_cpu(req->PersistentFileId));
6526 if (err)
6527 goto out;
6528
6529 rsp->StructureSize = cpu_to_le16(4);
6530 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006531 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09006532 return 0;
6533
6534out:
6535 if (err) {
6536 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6537 smb2_set_err_rsp(work);
6538 }
6539
6540 return err;
6541}
6542
6543/**
6544 * smb2_cancel() - handler for smb2 cancel command
6545 * @work: smb work containing cancel command buffer
6546 *
6547 * Return: 0 on success, otherwise error
6548 */
6549int smb2_cancel(struct ksmbd_work *work)
6550{
6551 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09006552 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006553 struct smb2_hdr *chdr;
6554 struct ksmbd_work *cancel_work = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006555 int canceled = 0;
6556 struct list_head *command_list;
6557
6558 ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006559 hdr->MessageId, hdr->Flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09006560
6561 if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) {
6562 command_list = &conn->async_requests;
6563
6564 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006565 list_for_each_entry(cancel_work, command_list,
6566 async_request_entry) {
Namjae Jeoncb451722021-11-03 08:08:44 +09006567 chdr = smb2_get_msg(cancel_work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006568
6569 if (cancel_work->async_id !=
Namjae Jeon64b39f42021-03-30 14:25:35 +09006570 le64_to_cpu(hdr->Id.AsyncId))
Namjae Jeone2f34482021-03-16 10:49:09 +09006571 continue;
6572
6573 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006574 "smb2 with AsyncId %llu cancelled command = 0x%x\n",
6575 le64_to_cpu(hdr->Id.AsyncId),
6576 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006577 canceled = 1;
6578 break;
6579 }
6580 spin_unlock(&conn->request_lock);
6581 } else {
6582 command_list = &conn->requests;
6583
6584 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006585 list_for_each_entry(cancel_work, command_list, request_entry) {
Namjae Jeoncb451722021-11-03 08:08:44 +09006586 chdr = smb2_get_msg(cancel_work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006587
6588 if (chdr->MessageId != hdr->MessageId ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006589 cancel_work == work)
Namjae Jeone2f34482021-03-16 10:49:09 +09006590 continue;
6591
6592 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006593 "smb2 with mid %llu cancelled command = 0x%x\n",
6594 le64_to_cpu(hdr->MessageId),
6595 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006596 canceled = 1;
6597 break;
6598 }
6599 spin_unlock(&conn->request_lock);
6600 }
6601
6602 if (canceled) {
6603 cancel_work->state = KSMBD_WORK_CANCELLED;
6604 if (cancel_work->cancel_fn)
6605 cancel_work->cancel_fn(cancel_work->cancel_argv);
6606 }
6607
6608 /* For SMB2_CANCEL command itself send no response*/
6609 work->send_no_response = 1;
6610 return 0;
6611}
6612
6613struct file_lock *smb_flock_init(struct file *f)
6614{
6615 struct file_lock *fl;
6616
6617 fl = locks_alloc_lock();
6618 if (!fl)
6619 goto out;
6620
6621 locks_init_lock(fl);
6622
6623 fl->fl_owner = f;
6624 fl->fl_pid = current->tgid;
6625 fl->fl_file = f;
6626 fl->fl_flags = FL_POSIX;
6627 fl->fl_ops = NULL;
6628 fl->fl_lmops = NULL;
6629
6630out:
6631 return fl;
6632}
6633
6634static int smb2_set_flock_flags(struct file_lock *flock, int flags)
6635{
6636 int cmd = -EINVAL;
6637
6638 /* Checking for wrong flag combination during lock request*/
6639 switch (flags) {
6640 case SMB2_LOCKFLAG_SHARED:
6641 ksmbd_debug(SMB, "received shared request\n");
6642 cmd = F_SETLKW;
6643 flock->fl_type = F_RDLCK;
6644 flock->fl_flags |= FL_SLEEP;
6645 break;
6646 case SMB2_LOCKFLAG_EXCLUSIVE:
6647 ksmbd_debug(SMB, "received exclusive request\n");
6648 cmd = F_SETLKW;
6649 flock->fl_type = F_WRLCK;
6650 flock->fl_flags |= FL_SLEEP;
6651 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006652 case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006653 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006654 "received shared & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006655 cmd = F_SETLK;
6656 flock->fl_type = F_RDLCK;
6657 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006658 case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006659 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006660 "received exclusive & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006661 cmd = F_SETLK;
6662 flock->fl_type = F_WRLCK;
6663 break;
6664 case SMB2_LOCKFLAG_UNLOCK:
6665 ksmbd_debug(SMB, "received unlock request\n");
6666 flock->fl_type = F_UNLCK;
6667 cmd = 0;
6668 break;
6669 }
6670
6671 return cmd;
6672}
6673
6674static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock,
Namjae Jeon070fb212021-05-26 17:57:12 +09006675 unsigned int cmd, int flags,
6676 struct list_head *lock_list)
Namjae Jeone2f34482021-03-16 10:49:09 +09006677{
6678 struct ksmbd_lock *lock;
6679
6680 lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL);
6681 if (!lock)
6682 return NULL;
6683
6684 lock->cmd = cmd;
6685 lock->fl = flock;
6686 lock->start = flock->fl_start;
6687 lock->end = flock->fl_end;
6688 lock->flags = flags;
6689 if (lock->start == lock->end)
6690 lock->zero_len = 1;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006691 INIT_LIST_HEAD(&lock->clist);
6692 INIT_LIST_HEAD(&lock->flist);
Namjae Jeone2f34482021-03-16 10:49:09 +09006693 INIT_LIST_HEAD(&lock->llist);
Namjae Jeone2f34482021-03-16 10:49:09 +09006694 list_add_tail(&lock->llist, lock_list);
6695
6696 return lock;
6697}
6698
6699static void smb2_remove_blocked_lock(void **argv)
6700{
6701 struct file_lock *flock = (struct file_lock *)argv[0];
6702
6703 ksmbd_vfs_posix_lock_unblock(flock);
6704 wake_up(&flock->fl_wait);
6705}
6706
6707static inline bool lock_defer_pending(struct file_lock *fl)
6708{
6709 /* check pending lock waiters */
6710 return waitqueue_active(&fl->fl_wait);
6711}
6712
6713/**
6714 * smb2_lock() - handler for smb2 file lock command
6715 * @work: smb work containing lock command buffer
6716 *
6717 * Return: 0 on success, otherwise error
6718 */
6719int smb2_lock(struct ksmbd_work *work)
6720{
Namjae Jeoncb451722021-11-03 08:08:44 +09006721 struct smb2_lock_req *req = smb2_get_msg(work->request_buf);
6722 struct smb2_lock_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006723 struct smb2_lock_element *lock_ele;
6724 struct ksmbd_file *fp = NULL;
6725 struct file_lock *flock = NULL;
6726 struct file *filp = NULL;
6727 int lock_count;
6728 int flags = 0;
6729 int cmd = 0;
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006730 int err = -EIO, i, rc = 0;
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006731 u64 lock_start, lock_length;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006732 struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2;
6733 struct ksmbd_conn *conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09006734 int nolock = 0;
6735 LIST_HEAD(lock_list);
6736 LIST_HEAD(rollback_list);
6737 int prior_lock = 0;
6738
6739 ksmbd_debug(SMB, "Received lock request\n");
6740 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006741 le64_to_cpu(req->VolatileFileId),
6742 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006743 if (!fp) {
6744 ksmbd_debug(SMB, "Invalid file id for lock : %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006745 le64_to_cpu(req->VolatileFileId));
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006746 err = -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09006747 goto out2;
6748 }
6749
6750 filp = fp->filp;
6751 lock_count = le16_to_cpu(req->LockCount);
6752 lock_ele = req->locks;
6753
6754 ksmbd_debug(SMB, "lock count is %d\n", lock_count);
Namjae Jeon070fb212021-05-26 17:57:12 +09006755 if (!lock_count) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006756 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006757 goto out2;
6758 }
6759
6760 for (i = 0; i < lock_count; i++) {
6761 flags = le32_to_cpu(lock_ele[i].Flags);
6762
6763 flock = smb_flock_init(filp);
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006764 if (!flock)
Namjae Jeone2f34482021-03-16 10:49:09 +09006765 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006766
6767 cmd = smb2_set_flock_flags(flock, flags);
6768
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006769 lock_start = le64_to_cpu(lock_ele[i].Offset);
6770 lock_length = le64_to_cpu(lock_ele[i].Length);
6771 if (lock_start > U64_MAX - lock_length) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006772 pr_err("Invalid lock range requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006773 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6774 goto out;
6775 }
6776
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006777 if (lock_start > OFFSET_MAX)
6778 flock->fl_start = OFFSET_MAX;
6779 else
6780 flock->fl_start = lock_start;
6781
Namjae Jeone2f34482021-03-16 10:49:09 +09006782 lock_length = le64_to_cpu(lock_ele[i].Length);
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006783 if (lock_length > OFFSET_MAX - flock->fl_start)
6784 lock_length = OFFSET_MAX - flock->fl_start;
Namjae Jeone2f34482021-03-16 10:49:09 +09006785
6786 flock->fl_end = flock->fl_start + lock_length;
6787
6788 if (flock->fl_end < flock->fl_start) {
6789 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006790 "the end offset(%llx) is smaller than the start offset(%llx)\n",
6791 flock->fl_end, flock->fl_start);
Namjae Jeone2f34482021-03-16 10:49:09 +09006792 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6793 goto out;
6794 }
6795
6796 /* Check conflict locks in one request */
6797 list_for_each_entry(cmp_lock, &lock_list, llist) {
6798 if (cmp_lock->fl->fl_start <= flock->fl_start &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006799 cmp_lock->fl->fl_end >= flock->fl_end) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006800 if (cmp_lock->fl->fl_type != F_UNLCK &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006801 flock->fl_type != F_UNLCK) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006802 pr_err("conflict two locks in one request\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006803 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006804 goto out;
6805 }
6806 }
6807 }
6808
6809 smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list);
6810 if (!smb_lock) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006811 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006812 goto out;
6813 }
6814 }
6815
6816 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6817 if (smb_lock->cmd < 0) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006818 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006819 goto out;
6820 }
6821
6822 if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006823 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006824 goto out;
6825 }
6826
Namjae Jeon64b39f42021-03-30 14:25:35 +09006827 if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) &&
6828 smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) ||
6829 (prior_lock == SMB2_LOCKFLAG_UNLOCK &&
6830 !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006831 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006832 goto out;
6833 }
6834
6835 prior_lock = smb_lock->flags;
6836
6837 if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006838 !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY))
Hyunchul Leed63528e2021-07-10 16:22:41 +09006839 goto no_check_cl;
Namjae Jeone2f34482021-03-16 10:49:09 +09006840
6841 nolock = 1;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006842 /* check locks in connection list */
6843 read_lock(&conn_list_lock);
6844 list_for_each_entry(conn, &conn_list, conns_list) {
6845 spin_lock(&conn->llist_lock);
6846 list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) {
6847 if (file_inode(cmp_lock->fl->fl_file) !=
6848 file_inode(smb_lock->fl->fl_file))
6849 continue;
Namjae Jeone2f34482021-03-16 10:49:09 +09006850
Hyunchul Leed63528e2021-07-10 16:22:41 +09006851 if (smb_lock->fl->fl_type == F_UNLCK) {
6852 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file &&
6853 cmp_lock->start == smb_lock->start &&
6854 cmp_lock->end == smb_lock->end &&
6855 !lock_defer_pending(cmp_lock->fl)) {
6856 nolock = 0;
6857 list_del(&cmp_lock->flist);
6858 list_del(&cmp_lock->clist);
6859 spin_unlock(&conn->llist_lock);
6860 read_unlock(&conn_list_lock);
6861
6862 locks_free_lock(cmp_lock->fl);
6863 kfree(cmp_lock);
6864 goto out_check_cl;
6865 }
6866 continue;
Namjae Jeone2f34482021-03-16 10:49:09 +09006867 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006868
Hyunchul Leed63528e2021-07-10 16:22:41 +09006869 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) {
6870 if (smb_lock->flags & SMB2_LOCKFLAG_SHARED)
6871 continue;
6872 } else {
6873 if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED)
6874 continue;
6875 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006876
Hyunchul Leed63528e2021-07-10 16:22:41 +09006877 /* check zero byte lock range */
6878 if (cmp_lock->zero_len && !smb_lock->zero_len &&
6879 cmp_lock->start > smb_lock->start &&
6880 cmp_lock->start < smb_lock->end) {
6881 spin_unlock(&conn->llist_lock);
6882 read_unlock(&conn_list_lock);
6883 pr_err("previous lock conflict with zero byte lock range\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006884 goto out;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006885 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006886
Hyunchul Leed63528e2021-07-10 16:22:41 +09006887 if (smb_lock->zero_len && !cmp_lock->zero_len &&
6888 smb_lock->start > cmp_lock->start &&
6889 smb_lock->start < cmp_lock->end) {
6890 spin_unlock(&conn->llist_lock);
6891 read_unlock(&conn_list_lock);
6892 pr_err("current lock conflict with zero byte lock range\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006893 goto out;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006894 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006895
Hyunchul Leed63528e2021-07-10 16:22:41 +09006896 if (((cmp_lock->start <= smb_lock->start &&
6897 cmp_lock->end > smb_lock->start) ||
6898 (cmp_lock->start < smb_lock->end &&
6899 cmp_lock->end >= smb_lock->end)) &&
6900 !cmp_lock->zero_len && !smb_lock->zero_len) {
6901 spin_unlock(&conn->llist_lock);
6902 read_unlock(&conn_list_lock);
6903 pr_err("Not allow lock operation on exclusive lock range\n");
Hyunchul Leed63528e2021-07-10 16:22:41 +09006904 goto out;
6905 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006906 }
Hyunchul Leed63528e2021-07-10 16:22:41 +09006907 spin_unlock(&conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006908 }
Hyunchul Leed63528e2021-07-10 16:22:41 +09006909 read_unlock(&conn_list_lock);
6910out_check_cl:
Namjae Jeone2f34482021-03-16 10:49:09 +09006911 if (smb_lock->fl->fl_type == F_UNLCK && nolock) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006912 pr_err("Try to unlock nolocked range\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006913 rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED;
6914 goto out;
6915 }
6916
Hyunchul Leed63528e2021-07-10 16:22:41 +09006917no_check_cl:
Namjae Jeone2f34482021-03-16 10:49:09 +09006918 if (smb_lock->zero_len) {
6919 err = 0;
6920 goto skip;
6921 }
6922
6923 flock = smb_lock->fl;
6924 list_del(&smb_lock->llist);
6925retry:
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006926 rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09006927skip:
6928 if (flags & SMB2_LOCKFLAG_UNLOCK) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006929 if (!rc) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006930 ksmbd_debug(SMB, "File unlocked\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006931 } else if (rc == -ENOENT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006932 rsp->hdr.Status = STATUS_NOT_LOCKED;
6933 goto out;
6934 }
6935 locks_free_lock(flock);
6936 kfree(smb_lock);
6937 } else {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006938 if (rc == FILE_LOCK_DEFERRED) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006939 void **argv;
6940
6941 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006942 "would have to wait for getting lock\n");
Hyunchul Leed63528e2021-07-10 16:22:41 +09006943 spin_lock(&work->conn->llist_lock);
6944 list_add_tail(&smb_lock->clist,
6945 &work->conn->lock_list);
6946 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006947 list_add(&smb_lock->llist, &rollback_list);
6948
6949 argv = kmalloc(sizeof(void *), GFP_KERNEL);
6950 if (!argv) {
6951 err = -ENOMEM;
6952 goto out;
6953 }
6954 argv[0] = flock;
6955
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006956 rc = setup_async_work(work,
6957 smb2_remove_blocked_lock,
6958 argv);
6959 if (rc) {
6960 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09006961 goto out;
6962 }
6963 spin_lock(&fp->f_lock);
6964 list_add(&work->fp_entry, &fp->blocked_works);
6965 spin_unlock(&fp->f_lock);
6966
6967 smb2_send_interim_resp(work, STATUS_PENDING);
6968
Hyunchul Lee45a64e82021-07-10 09:34:20 +09006969 ksmbd_vfs_posix_lock_wait(flock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006970
Hyunchul Leed4075ab2021-06-25 07:02:10 +09006971 if (work->state != KSMBD_WORK_ACTIVE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006972 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09006973 spin_lock(&work->conn->llist_lock);
6974 list_del(&smb_lock->clist);
6975 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006976 locks_free_lock(flock);
6977
Hyunchul Leed4075ab2021-06-25 07:02:10 +09006978 if (work->state == KSMBD_WORK_CANCELLED) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006979 spin_lock(&fp->f_lock);
6980 list_del(&work->fp_entry);
6981 spin_unlock(&fp->f_lock);
6982 rsp->hdr.Status =
6983 STATUS_CANCELLED;
6984 kfree(smb_lock);
6985 smb2_send_interim_resp(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006986 STATUS_CANCELLED);
Namjae Jeone2f34482021-03-16 10:49:09 +09006987 work->send_no_response = 1;
6988 goto out;
6989 }
6990 init_smb2_rsp_hdr(work);
6991 smb2_set_err_rsp(work);
6992 rsp->hdr.Status =
6993 STATUS_RANGE_NOT_LOCKED;
6994 kfree(smb_lock);
6995 goto out2;
6996 }
6997
6998 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09006999 spin_lock(&work->conn->llist_lock);
7000 list_del(&smb_lock->clist);
7001 spin_unlock(&work->conn->llist_lock);
7002
Namjae Jeone2f34482021-03-16 10:49:09 +09007003 spin_lock(&fp->f_lock);
7004 list_del(&work->fp_entry);
7005 spin_unlock(&fp->f_lock);
7006 goto retry;
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09007007 } else if (!rc) {
Hyunchul Leed63528e2021-07-10 16:22:41 +09007008 spin_lock(&work->conn->llist_lock);
7009 list_add_tail(&smb_lock->clist,
7010 &work->conn->lock_list);
7011 list_add_tail(&smb_lock->flist,
7012 &fp->lock_list);
7013 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09007014 list_add(&smb_lock->llist, &rollback_list);
7015 ksmbd_debug(SMB, "successful in taking lock\n");
7016 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007017 goto out;
7018 }
7019 }
7020 }
7021
7022 if (atomic_read(&fp->f_ci->op_count) > 1)
7023 smb_break_all_oplock(work, fp);
7024
7025 rsp->StructureSize = cpu_to_le16(4);
7026 ksmbd_debug(SMB, "successful in taking lock\n");
7027 rsp->hdr.Status = STATUS_SUCCESS;
7028 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09007029 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09007030 ksmbd_fd_put(work, fp);
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007031 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09007032
7033out:
7034 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
7035 locks_free_lock(smb_lock->fl);
7036 list_del(&smb_lock->llist);
7037 kfree(smb_lock);
7038 }
7039
7040 list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) {
7041 struct file_lock *rlock = NULL;
7042
7043 rlock = smb_flock_init(filp);
7044 rlock->fl_type = F_UNLCK;
7045 rlock->fl_start = smb_lock->start;
7046 rlock->fl_end = smb_lock->end;
7047
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007048 rc = vfs_lock_file(filp, 0, rlock, NULL);
7049 if (rc)
7050 pr_err("rollback unlock fail : %d\n", rc);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007051
Namjae Jeone2f34482021-03-16 10:49:09 +09007052 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007053 spin_lock(&work->conn->llist_lock);
7054 if (!list_empty(&smb_lock->flist))
7055 list_del(&smb_lock->flist);
7056 list_del(&smb_lock->clist);
7057 spin_unlock(&work->conn->llist_lock);
7058
Namjae Jeone2f34482021-03-16 10:49:09 +09007059 locks_free_lock(smb_lock->fl);
7060 locks_free_lock(rlock);
7061 kfree(smb_lock);
7062 }
7063out2:
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09007064 ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err);
7065
7066 if (!rsp->hdr.Status) {
7067 if (err == -EINVAL)
7068 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7069 else if (err == -ENOMEM)
7070 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
7071 else if (err == -ENOENT)
7072 rsp->hdr.Status = STATUS_FILE_CLOSED;
7073 else
7074 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
7075 }
7076
Namjae Jeone2f34482021-03-16 10:49:09 +09007077 smb2_set_err_rsp(work);
7078 ksmbd_fd_put(work, fp);
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007079 return err;
Namjae Jeone2f34482021-03-16 10:49:09 +09007080}
7081
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007082static int fsctl_copychunk(struct ksmbd_work *work,
7083 struct copychunk_ioctl_req *ci_req,
7084 unsigned int cnt_code,
7085 unsigned int input_count,
7086 unsigned long long volatile_id,
7087 unsigned long long persistent_id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007088 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007089{
Namjae Jeone2f34482021-03-16 10:49:09 +09007090 struct copychunk_ioctl_rsp *ci_rsp;
7091 struct ksmbd_file *src_fp = NULL, *dst_fp = NULL;
7092 struct srv_copychunk *chunks;
7093 unsigned int i, chunk_count, chunk_count_written = 0;
7094 unsigned int chunk_size_written = 0;
7095 loff_t total_size_written = 0;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007096 int ret = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09007097
Namjae Jeone2f34482021-03-16 10:49:09 +09007098 ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0];
7099
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007100 rsp->VolatileFileId = cpu_to_le64(volatile_id);
7101 rsp->PersistentFileId = cpu_to_le64(persistent_id);
Namjae Jeon64b39f42021-03-30 14:25:35 +09007102 ci_rsp->ChunksWritten =
7103 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count());
7104 ci_rsp->ChunkBytesWritten =
7105 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size());
7106 ci_rsp->TotalBytesWritten =
7107 cpu_to_le32(ksmbd_server_side_copy_max_total_size());
Namjae Jeone2f34482021-03-16 10:49:09 +09007108
7109 chunks = (struct srv_copychunk *)&ci_req->Chunks[0];
7110 chunk_count = le32_to_cpu(ci_req->ChunkCount);
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007111 if (chunk_count == 0)
7112 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09007113 total_size_written = 0;
7114
7115 /* verify the SRV_COPYCHUNK_COPY packet */
7116 if (chunk_count > ksmbd_server_side_copy_max_chunk_count() ||
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007117 input_count < offsetof(struct copychunk_ioctl_req, Chunks) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09007118 chunk_count * sizeof(struct srv_copychunk)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007119 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7120 return -EINVAL;
7121 }
7122
7123 for (i = 0; i < chunk_count; i++) {
7124 if (le32_to_cpu(chunks[i].Length) == 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09007125 le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size())
Namjae Jeone2f34482021-03-16 10:49:09 +09007126 break;
7127 total_size_written += le32_to_cpu(chunks[i].Length);
7128 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09007129
7130 if (i < chunk_count ||
7131 total_size_written > ksmbd_server_side_copy_max_total_size()) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007132 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7133 return -EINVAL;
7134 }
7135
7136 src_fp = ksmbd_lookup_foreign_fd(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007137 le64_to_cpu(ci_req->ResumeKey[0]));
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007138 dst_fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
Namjae Jeone2f34482021-03-16 10:49:09 +09007139 ret = -EINVAL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007140 if (!src_fp ||
7141 src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007142 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7143 goto out;
7144 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09007145
Namjae Jeone2f34482021-03-16 10:49:09 +09007146 if (!dst_fp) {
7147 rsp->hdr.Status = STATUS_FILE_CLOSED;
7148 goto out;
7149 }
7150
7151 /*
7152 * FILE_READ_DATA should only be included in
7153 * the FSCTL_COPYCHUNK case
7154 */
Namjae Jeon070fb212021-05-26 17:57:12 +09007155 if (cnt_code == FSCTL_COPYCHUNK &&
7156 !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007157 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7158 goto out;
7159 }
7160
7161 ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007162 chunks, chunk_count,
7163 &chunk_count_written,
7164 &chunk_size_written,
7165 &total_size_written);
Namjae Jeone2f34482021-03-16 10:49:09 +09007166 if (ret < 0) {
7167 if (ret == -EACCES)
7168 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7169 if (ret == -EAGAIN)
7170 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
7171 else if (ret == -EBADF)
7172 rsp->hdr.Status = STATUS_INVALID_HANDLE;
7173 else if (ret == -EFBIG || ret == -ENOSPC)
7174 rsp->hdr.Status = STATUS_DISK_FULL;
7175 else if (ret == -EINVAL)
7176 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7177 else if (ret == -EISDIR)
7178 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
7179 else if (ret == -E2BIG)
7180 rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE;
7181 else
7182 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
7183 }
7184
7185 ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written);
7186 ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written);
7187 ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written);
7188out:
7189 ksmbd_fd_put(work, src_fp);
7190 ksmbd_fd_put(work, dst_fp);
7191 return ret;
7192}
7193
7194static __be32 idev_ipv4_address(struct in_device *idev)
7195{
7196 __be32 addr = 0;
7197
7198 struct in_ifaddr *ifa;
7199
7200 rcu_read_lock();
7201 in_dev_for_each_ifa_rcu(ifa, idev) {
7202 if (ifa->ifa_flags & IFA_F_SECONDARY)
7203 continue;
7204
7205 addr = ifa->ifa_address;
7206 break;
7207 }
7208 rcu_read_unlock();
7209 return addr;
7210}
7211
7212static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007213 struct smb2_ioctl_rsp *rsp,
7214 unsigned int out_buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09007215{
7216 struct network_interface_info_ioctl_rsp *nii_rsp = NULL;
7217 int nbytes = 0;
7218 struct net_device *netdev;
7219 struct sockaddr_storage_rsp *sockaddr_storage;
7220 unsigned int flags;
7221 unsigned long long speed;
Namjae Jeonf1abdb72021-07-27 13:25:13 +09007222 struct sockaddr_in6 *csin6 = (struct sockaddr_in6 *)&conn->peer_addr;
Namjae Jeone2f34482021-03-16 10:49:09 +09007223
7224 rtnl_lock();
7225 for_each_netdev(&init_net, netdev) {
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007226 if (out_buf_len <
7227 nbytes + sizeof(struct network_interface_info_ioctl_rsp)) {
7228 rtnl_unlock();
7229 return -ENOSPC;
7230 }
7231
Namjae Jeone2f34482021-03-16 10:49:09 +09007232 if (netdev->type == ARPHRD_LOOPBACK)
7233 continue;
7234
7235 flags = dev_get_flags(netdev);
7236 if (!(flags & IFF_RUNNING))
7237 continue;
7238
7239 nii_rsp = (struct network_interface_info_ioctl_rsp *)
7240 &rsp->Buffer[nbytes];
7241 nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex);
7242
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09007243 nii_rsp->Capability = 0;
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09007244 if (ksmbd_rdma_capable_netdev(netdev))
7245 nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE);
Namjae Jeone2f34482021-03-16 10:49:09 +09007246
7247 nii_rsp->Next = cpu_to_le32(152);
7248 nii_rsp->Reserved = 0;
7249
7250 if (netdev->ethtool_ops->get_link_ksettings) {
7251 struct ethtool_link_ksettings cmd;
7252
7253 netdev->ethtool_ops->get_link_ksettings(netdev, &cmd);
7254 speed = cmd.base.speed;
7255 } else {
Per Forlind4758662021-08-30 13:23:04 +09007256 ksmbd_debug(SMB, "%s %s\n", netdev->name,
7257 "speed is unknown, defaulting to 1Gb/sec");
Namjae Jeone2f34482021-03-16 10:49:09 +09007258 speed = SPEED_1000;
7259 }
7260
7261 speed *= 1000000;
7262 nii_rsp->LinkSpeed = cpu_to_le64(speed);
7263
7264 sockaddr_storage = (struct sockaddr_storage_rsp *)
7265 nii_rsp->SockAddr_Storage;
7266 memset(sockaddr_storage, 0, 128);
7267
Namjae Jeonf1abdb72021-07-27 13:25:13 +09007268 if (conn->peer_addr.ss_family == PF_INET ||
7269 ipv6_addr_v4mapped(&csin6->sin6_addr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007270 struct in_device *idev;
7271
7272 sockaddr_storage->Family = cpu_to_le16(INTERNETWORK);
7273 sockaddr_storage->addr4.Port = 0;
7274
7275 idev = __in_dev_get_rtnl(netdev);
7276 if (!idev)
7277 continue;
7278 sockaddr_storage->addr4.IPv4address =
7279 idev_ipv4_address(idev);
7280 } else {
7281 struct inet6_dev *idev6;
7282 struct inet6_ifaddr *ifa;
7283 __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address;
7284
7285 sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6);
7286 sockaddr_storage->addr6.Port = 0;
7287 sockaddr_storage->addr6.FlowInfo = 0;
7288
7289 idev6 = __in6_dev_get(netdev);
7290 if (!idev6)
7291 continue;
7292
7293 list_for_each_entry(ifa, &idev6->addr_list, if_list) {
7294 if (ifa->flags & (IFA_F_TENTATIVE |
7295 IFA_F_DEPRECATED))
7296 continue;
7297 memcpy(ipv6_addr, ifa->addr.s6_addr, 16);
7298 break;
7299 }
7300 sockaddr_storage->addr6.ScopeId = 0;
7301 }
7302
7303 nbytes += sizeof(struct network_interface_info_ioctl_rsp);
7304 }
7305 rtnl_unlock();
7306
7307 /* zero if this is last one */
7308 if (nii_rsp)
7309 nii_rsp->Next = 0;
7310
Namjae Jeone2f34482021-03-16 10:49:09 +09007311 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7312 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7313 return nbytes;
7314}
7315
Namjae Jeone2f34482021-03-16 10:49:09 +09007316static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09007317 struct validate_negotiate_info_req *neg_req,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007318 struct validate_negotiate_info_rsp *neg_rsp,
7319 unsigned int in_buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09007320{
7321 int ret = 0;
7322 int dialect;
7323
Marios Makassikis78f16882021-10-28 21:01:27 +02007324 if (in_buf_len < offsetof(struct validate_negotiate_info_req, Dialects) +
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007325 le16_to_cpu(neg_req->DialectCount) * sizeof(__le16))
7326 return -EINVAL;
7327
Namjae Jeone2f34482021-03-16 10:49:09 +09007328 dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects,
Namjae Jeon070fb212021-05-26 17:57:12 +09007329 neg_req->DialectCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09007330 if (dialect == BAD_PROT_ID || dialect != conn->dialect) {
7331 ret = -EINVAL;
7332 goto err_out;
7333 }
7334
7335 if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) {
7336 ret = -EINVAL;
7337 goto err_out;
7338 }
7339
7340 if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) {
7341 ret = -EINVAL;
7342 goto err_out;
7343 }
7344
7345 if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) {
7346 ret = -EINVAL;
7347 goto err_out;
7348 }
7349
7350 neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
7351 memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE);
7352 neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode);
7353 neg_rsp->Dialect = cpu_to_le16(conn->dialect);
7354err_out:
7355 return ret;
7356}
7357
Namjae Jeon64b39f42021-03-30 14:25:35 +09007358static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007359 struct file_allocated_range_buffer *qar_req,
7360 struct file_allocated_range_buffer *qar_rsp,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007361 unsigned int in_count, unsigned int *out_count)
Namjae Jeone2f34482021-03-16 10:49:09 +09007362{
7363 struct ksmbd_file *fp;
7364 loff_t start, length;
7365 int ret = 0;
7366
7367 *out_count = 0;
7368 if (in_count == 0)
7369 return -EINVAL;
7370
7371 fp = ksmbd_lookup_fd_fast(work, id);
7372 if (!fp)
7373 return -ENOENT;
7374
7375 start = le64_to_cpu(qar_req->file_offset);
7376 length = le64_to_cpu(qar_req->length);
7377
7378 ret = ksmbd_vfs_fqar_lseek(fp, start, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09007379 qar_rsp, in_count, out_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09007380 if (ret && ret != -E2BIG)
7381 *out_count = 0;
7382
7383 ksmbd_fd_put(work, fp);
7384 return ret;
7385}
7386
Namjae Jeon64b39f42021-03-30 14:25:35 +09007387static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007388 unsigned int out_buf_len,
7389 struct smb2_ioctl_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007390 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007391{
7392 struct ksmbd_rpc_command *rpc_resp;
7393 char *data_buf = (char *)&req->Buffer[0];
7394 int nbytes = 0;
7395
Namjae Jeon64b39f42021-03-30 14:25:35 +09007396 rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09007397 le32_to_cpu(req->InputCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09007398 if (rpc_resp) {
7399 if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) {
7400 /*
7401 * set STATUS_SOME_NOT_MAPPED response
7402 * for unknown domain sid.
7403 */
7404 rsp->hdr.Status = STATUS_SOME_NOT_MAPPED;
7405 } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
7406 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7407 goto out;
7408 } else if (rpc_resp->flags != KSMBD_RPC_OK) {
7409 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7410 goto out;
7411 }
7412
7413 nbytes = rpc_resp->payload_sz;
7414 if (rpc_resp->payload_sz > out_buf_len) {
7415 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7416 nbytes = out_buf_len;
7417 }
7418
7419 if (!rpc_resp->payload_sz) {
7420 rsp->hdr.Status =
7421 STATUS_UNEXPECTED_IO_ERROR;
7422 goto out;
7423 }
7424
7425 memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes);
7426 }
7427out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09007428 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007429 return nbytes;
7430}
7431
Namjae Jeon64b39f42021-03-30 14:25:35 +09007432static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007433 struct file_sparse *sparse)
Namjae Jeone2f34482021-03-16 10:49:09 +09007434{
7435 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007436 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09007437 int ret = 0;
7438 __le32 old_fattr;
7439
7440 fp = ksmbd_lookup_fd_fast(work, id);
7441 if (!fp)
7442 return -ENOENT;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007443 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007444
7445 old_fattr = fp->f_ci->m_fattr;
7446 if (sparse->SetSparse)
7447 fp->f_ci->m_fattr |= ATTR_SPARSE_FILE_LE;
7448 else
7449 fp->f_ci->m_fattr &= ~ATTR_SPARSE_FILE_LE;
7450
7451 if (fp->f_ci->m_fattr != old_fattr &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007452 test_share_config_flag(work->tcon->share_conf,
7453 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007454 struct xattr_dos_attrib da;
7455
Hyunchul Lee465d7202021-07-03 12:10:36 +09007456 ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007457 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007458 if (ret <= 0)
7459 goto out;
7460
7461 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
Hyunchul Lee465d7202021-07-03 12:10:36 +09007462 ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007463 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007464 if (ret)
7465 fp->f_ci->m_fattr = old_fattr;
7466 }
7467
7468out:
7469 ksmbd_fd_put(work, fp);
7470 return ret;
7471}
7472
7473static int fsctl_request_resume_key(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007474 struct smb2_ioctl_req *req,
7475 struct resume_key_ioctl_rsp *key_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007476{
7477 struct ksmbd_file *fp;
7478
7479 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007480 le64_to_cpu(req->VolatileFileId),
7481 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09007482 if (!fp)
7483 return -ENOENT;
7484
7485 memset(key_rsp, 0, sizeof(*key_rsp));
7486 key_rsp->ResumeKey[0] = req->VolatileFileId;
7487 key_rsp->ResumeKey[1] = req->PersistentFileId;
7488 ksmbd_fd_put(work, fp);
7489
7490 return 0;
7491}
7492
7493/**
7494 * smb2_ioctl() - handler for smb2 ioctl command
7495 * @work: smb work containing ioctl command buffer
7496 *
7497 * Return: 0 on success, otherwise error
7498 */
7499int smb2_ioctl(struct ksmbd_work *work)
7500{
7501 struct smb2_ioctl_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09007502 struct smb2_ioctl_rsp *rsp;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007503 unsigned int cnt_code, nbytes = 0, out_buf_len, in_buf_len;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007504 u64 id = KSMBD_NO_FID;
Namjae Jeone2f34482021-03-16 10:49:09 +09007505 struct ksmbd_conn *conn = work->conn;
7506 int ret = 0;
7507
Namjae Jeone2f34482021-03-16 10:49:09 +09007508 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09007509 req = ksmbd_req_buf_next(work);
7510 rsp = ksmbd_resp_buf_next(work);
Namjae Jeon38673692021-07-08 12:32:27 +09007511 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
7512 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007513 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09007514 id = work->compound_fid;
7515 }
7516 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09007517 req = smb2_get_msg(work->request_buf);
7518 rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09007519 }
7520
Namjae Jeon38673692021-07-08 12:32:27 +09007521 if (!has_file_id(id))
Namjae Jeone2f34482021-03-16 10:49:09 +09007522 id = le64_to_cpu(req->VolatileFileId);
7523
7524 if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) {
7525 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7526 goto out;
7527 }
7528
7529 cnt_code = le32_to_cpu(req->CntCode);
Hyunchul Lee34061d62021-10-16 08:39:54 +09007530 ret = smb2_calc_max_out_buf_len(work, 48,
7531 le32_to_cpu(req->MaxOutputResponse));
7532 if (ret < 0) {
7533 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7534 goto out;
7535 }
7536 out_buf_len = (unsigned int)ret;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007537 in_buf_len = le32_to_cpu(req->InputCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09007538
7539 switch (cnt_code) {
7540 case FSCTL_DFS_GET_REFERRALS:
7541 case FSCTL_DFS_GET_REFERRALS_EX:
7542 /* Not support DFS yet */
7543 rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED;
7544 goto out;
7545 case FSCTL_CREATE_OR_GET_OBJECT_ID:
7546 {
7547 struct file_object_buf_type1_ioctl_rsp *obj_buf;
7548
7549 nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp);
7550 obj_buf = (struct file_object_buf_type1_ioctl_rsp *)
7551 &rsp->Buffer[0];
7552
7553 /*
7554 * TODO: This is dummy implementation to pass smbtorture
7555 * Need to check correct response later
7556 */
7557 memset(obj_buf->ObjectId, 0x0, 16);
7558 memset(obj_buf->BirthVolumeId, 0x0, 16);
7559 memset(obj_buf->BirthObjectId, 0x0, 16);
7560 memset(obj_buf->DomainId, 0x0, 16);
7561
7562 break;
7563 }
7564 case FSCTL_PIPE_TRANSCEIVE:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007565 out_buf_len = min_t(u32, KSMBD_IPC_MAX_PAYLOAD, out_buf_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09007566 nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp);
7567 break;
7568 case FSCTL_VALIDATE_NEGOTIATE_INFO:
7569 if (conn->dialect < SMB30_PROT_ID) {
7570 ret = -EOPNOTSUPP;
7571 goto out;
7572 }
7573
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007574 if (in_buf_len < sizeof(struct validate_negotiate_info_req))
7575 return -EINVAL;
7576
7577 if (out_buf_len < sizeof(struct validate_negotiate_info_rsp))
7578 return -EINVAL;
7579
Namjae Jeone2f34482021-03-16 10:49:09 +09007580 ret = fsctl_validate_negotiate_info(conn,
7581 (struct validate_negotiate_info_req *)&req->Buffer[0],
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007582 (struct validate_negotiate_info_rsp *)&rsp->Buffer[0],
7583 in_buf_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09007584 if (ret < 0)
7585 goto out;
7586
7587 nbytes = sizeof(struct validate_negotiate_info_rsp);
7588 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7589 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7590 break;
7591 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007592 ret = fsctl_query_iface_info_ioctl(conn, rsp, out_buf_len);
7593 if (ret < 0)
Namjae Jeone2f34482021-03-16 10:49:09 +09007594 goto out;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007595 nbytes = ret;
Namjae Jeone2f34482021-03-16 10:49:09 +09007596 break;
7597 case FSCTL_REQUEST_RESUME_KEY:
7598 if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) {
7599 ret = -EINVAL;
7600 goto out;
7601 }
7602
7603 ret = fsctl_request_resume_key(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007604 (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007605 if (ret < 0)
7606 goto out;
7607 rsp->PersistentFileId = req->PersistentFileId;
7608 rsp->VolatileFileId = req->VolatileFileId;
7609 nbytes = sizeof(struct resume_key_ioctl_rsp);
7610 break;
7611 case FSCTL_COPYCHUNK:
7612 case FSCTL_COPYCHUNK_WRITE:
Namjae Jeon64b39f42021-03-30 14:25:35 +09007613 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007614 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007615 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007616 ret = -EACCES;
7617 goto out;
7618 }
7619
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007620 if (in_buf_len < sizeof(struct copychunk_ioctl_req)) {
7621 ret = -EINVAL;
7622 goto out;
7623 }
7624
Namjae Jeone2f34482021-03-16 10:49:09 +09007625 if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) {
7626 ret = -EINVAL;
7627 goto out;
7628 }
7629
7630 nbytes = sizeof(struct copychunk_ioctl_rsp);
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007631 rsp->VolatileFileId = req->VolatileFileId;
7632 rsp->PersistentFileId = req->PersistentFileId;
7633 fsctl_copychunk(work,
7634 (struct copychunk_ioctl_req *)&req->Buffer[0],
7635 le32_to_cpu(req->CntCode),
7636 le32_to_cpu(req->InputCount),
7637 le64_to_cpu(req->VolatileFileId),
7638 le64_to_cpu(req->PersistentFileId),
7639 rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007640 break;
7641 case FSCTL_SET_SPARSE:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007642 if (in_buf_len < sizeof(struct file_sparse)) {
7643 ret = -EINVAL;
7644 goto out;
7645 }
7646
Namjae Jeone2f34482021-03-16 10:49:09 +09007647 ret = fsctl_set_sparse(work, id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007648 (struct file_sparse *)&req->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007649 if (ret < 0)
7650 goto out;
7651 break;
7652 case FSCTL_SET_ZERO_DATA:
7653 {
7654 struct file_zero_data_information *zero_data;
7655 struct ksmbd_file *fp;
7656 loff_t off, len;
7657
Namjae Jeon64b39f42021-03-30 14:25:35 +09007658 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007659 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007660 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007661 ret = -EACCES;
7662 goto out;
7663 }
7664
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007665 if (in_buf_len < sizeof(struct file_zero_data_information)) {
7666 ret = -EINVAL;
7667 goto out;
7668 }
7669
Namjae Jeone2f34482021-03-16 10:49:09 +09007670 zero_data =
7671 (struct file_zero_data_information *)&req->Buffer[0];
7672
7673 fp = ksmbd_lookup_fd_fast(work, id);
7674 if (!fp) {
7675 ret = -ENOENT;
7676 goto out;
7677 }
7678
7679 off = le64_to_cpu(zero_data->FileOffset);
7680 len = le64_to_cpu(zero_data->BeyondFinalZero) - off;
7681
7682 ret = ksmbd_vfs_zero_data(work, fp, off, len);
7683 ksmbd_fd_put(work, fp);
7684 if (ret < 0)
7685 goto out;
7686 break;
7687 }
7688 case FSCTL_QUERY_ALLOCATED_RANGES:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007689 if (in_buf_len < sizeof(struct file_allocated_range_buffer)) {
7690 ret = -EINVAL;
7691 goto out;
7692 }
7693
Namjae Jeone2f34482021-03-16 10:49:09 +09007694 ret = fsctl_query_allocated_ranges(work, id,
7695 (struct file_allocated_range_buffer *)&req->Buffer[0],
7696 (struct file_allocated_range_buffer *)&rsp->Buffer[0],
7697 out_buf_len /
7698 sizeof(struct file_allocated_range_buffer), &nbytes);
7699 if (ret == -E2BIG) {
7700 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7701 } else if (ret < 0) {
7702 nbytes = 0;
7703 goto out;
7704 }
7705
7706 nbytes *= sizeof(struct file_allocated_range_buffer);
7707 break;
7708 case FSCTL_GET_REPARSE_POINT:
7709 {
7710 struct reparse_data_buffer *reparse_ptr;
7711 struct ksmbd_file *fp;
7712
7713 reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0];
7714 fp = ksmbd_lookup_fd_fast(work, id);
7715 if (!fp) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007716 pr_err("not found fp!!\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007717 ret = -ENOENT;
7718 goto out;
7719 }
7720
7721 reparse_ptr->ReparseTag =
Namjae Jeonab0b2632021-06-29 09:20:13 +09007722 smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode);
Namjae Jeone2f34482021-03-16 10:49:09 +09007723 reparse_ptr->ReparseDataLength = 0;
7724 ksmbd_fd_put(work, fp);
7725 nbytes = sizeof(struct reparse_data_buffer);
7726 break;
7727 }
Namjae Jeoneb817362021-05-18 10:37:59 +09007728 case FSCTL_DUPLICATE_EXTENTS_TO_FILE:
7729 {
7730 struct ksmbd_file *fp_in, *fp_out = NULL;
7731 struct duplicate_extents_to_file *dup_ext;
7732 loff_t src_off, dst_off, length, cloned;
7733
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007734 if (in_buf_len < sizeof(struct duplicate_extents_to_file)) {
7735 ret = -EINVAL;
7736 goto out;
7737 }
7738
Namjae Jeoneb817362021-05-18 10:37:59 +09007739 dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0];
7740
7741 fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle,
Namjae Jeon070fb212021-05-26 17:57:12 +09007742 dup_ext->PersistentFileHandle);
Namjae Jeoneb817362021-05-18 10:37:59 +09007743 if (!fp_in) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007744 pr_err("not found file handle in duplicate extent to file\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007745 ret = -ENOENT;
7746 goto out;
7747 }
7748
7749 fp_out = ksmbd_lookup_fd_fast(work, id);
7750 if (!fp_out) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007751 pr_err("not found fp\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007752 ret = -ENOENT;
7753 goto dup_ext_out;
7754 }
7755
7756 src_off = le64_to_cpu(dup_ext->SourceFileOffset);
7757 dst_off = le64_to_cpu(dup_ext->TargetFileOffset);
7758 length = le64_to_cpu(dup_ext->ByteCount);
7759 cloned = vfs_clone_file_range(fp_in->filp, src_off, fp_out->filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007760 dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007761 if (cloned == -EXDEV || cloned == -EOPNOTSUPP) {
7762 ret = -EOPNOTSUPP;
7763 goto dup_ext_out;
7764 } else if (cloned != length) {
Namjae Jeonf8524772021-06-18 10:28:00 +09007765 cloned = vfs_copy_file_range(fp_in->filp, src_off,
7766 fp_out->filp, dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007767 if (cloned != length) {
7768 if (cloned < 0)
7769 ret = cloned;
7770 else
7771 ret = -EINVAL;
7772 }
7773 }
7774
7775dup_ext_out:
7776 ksmbd_fd_put(work, fp_in);
7777 ksmbd_fd_put(work, fp_out);
7778 if (ret < 0)
7779 goto out;
7780 break;
7781 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007782 default:
7783 ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007784 cnt_code);
Namjae Jeone2f34482021-03-16 10:49:09 +09007785 ret = -EOPNOTSUPP;
7786 goto out;
7787 }
7788
7789 rsp->CntCode = cpu_to_le32(cnt_code);
7790 rsp->InputCount = cpu_to_le32(0);
7791 rsp->InputOffset = cpu_to_le32(112);
7792 rsp->OutputOffset = cpu_to_le32(112);
7793 rsp->OutputCount = cpu_to_le32(nbytes);
7794 rsp->StructureSize = cpu_to_le16(49);
7795 rsp->Reserved = cpu_to_le16(0);
7796 rsp->Flags = cpu_to_le32(0);
7797 rsp->Reserved2 = cpu_to_le32(0);
Namjae Jeoncb451722021-11-03 08:08:44 +09007798 inc_rfc1001_len(work->response_buf, 48 + nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09007799
7800 return 0;
7801
7802out:
7803 if (ret == -EACCES)
7804 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7805 else if (ret == -ENOENT)
7806 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7807 else if (ret == -EOPNOTSUPP)
7808 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007809 else if (ret == -ENOSPC)
7810 rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL;
Namjae Jeone2f34482021-03-16 10:49:09 +09007811 else if (ret < 0 || rsp->hdr.Status == 0)
7812 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7813 smb2_set_err_rsp(work);
7814 return 0;
7815}
7816
7817/**
7818 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command
7819 * @work: smb work containing oplock break command buffer
7820 *
7821 * Return: 0
7822 */
7823static void smb20_oplock_break_ack(struct ksmbd_work *work)
7824{
Namjae Jeoncb451722021-11-03 08:08:44 +09007825 struct smb2_oplock_break *req = smb2_get_msg(work->request_buf);
7826 struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09007827 struct ksmbd_file *fp;
7828 struct oplock_info *opinfo = NULL;
7829 __le32 err = 0;
7830 int ret = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007831 u64 volatile_id, persistent_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09007832 char req_oplevel = 0, rsp_oplevel = 0;
7833 unsigned int oplock_change_type;
7834
7835 volatile_id = le64_to_cpu(req->VolatileFid);
7836 persistent_id = le64_to_cpu(req->PersistentFid);
7837 req_oplevel = req->OplockLevel;
7838 ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n",
7839 volatile_id, persistent_id, req_oplevel);
7840
7841 fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
7842 if (!fp) {
7843 rsp->hdr.Status = STATUS_FILE_CLOSED;
7844 smb2_set_err_rsp(work);
7845 return;
7846 }
7847
7848 opinfo = opinfo_get(fp);
7849 if (!opinfo) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007850 pr_err("unexpected null oplock_info\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007851 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7852 smb2_set_err_rsp(work);
7853 ksmbd_fd_put(work, fp);
7854 return;
7855 }
7856
7857 if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) {
7858 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7859 goto err_out;
7860 }
7861
7862 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7863 ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state);
7864 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7865 goto err_out;
7866 }
7867
Namjae Jeon64b39f42021-03-30 14:25:35 +09007868 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7869 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7870 (req_oplevel != SMB2_OPLOCK_LEVEL_II &&
7871 req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007872 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7873 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007874 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7875 req_oplevel != SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007876 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7877 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007878 } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II ||
7879 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007880 err = STATUS_INVALID_DEVICE_STATE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007881 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7882 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7883 req_oplevel == SMB2_OPLOCK_LEVEL_II) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007884 oplock_change_type = OPLOCK_WRITE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007885 } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7886 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7887 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007888 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007889 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7890 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007891 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007892 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007893 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007894 }
7895 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007896 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007897 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007898
7899 switch (oplock_change_type) {
7900 case OPLOCK_WRITE_TO_READ:
7901 ret = opinfo_write_to_read(opinfo);
7902 rsp_oplevel = SMB2_OPLOCK_LEVEL_II;
7903 break;
7904 case OPLOCK_WRITE_TO_NONE:
7905 ret = opinfo_write_to_none(opinfo);
7906 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7907 break;
7908 case OPLOCK_READ_TO_NONE:
7909 ret = opinfo_read_to_none(opinfo);
7910 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7911 break;
7912 default:
Namjae Jeonbde16942021-06-28 15:23:19 +09007913 pr_err("unknown oplock change 0x%x -> 0x%x\n",
7914 opinfo->level, rsp_oplevel);
Namjae Jeone2f34482021-03-16 10:49:09 +09007915 }
7916
7917 if (ret < 0) {
7918 rsp->hdr.Status = err;
7919 goto err_out;
7920 }
7921
7922 opinfo_put(opinfo);
7923 ksmbd_fd_put(work, fp);
7924 opinfo->op_state = OPLOCK_STATE_NONE;
7925 wake_up_interruptible_all(&opinfo->oplock_q);
7926
7927 rsp->StructureSize = cpu_to_le16(24);
7928 rsp->OplockLevel = rsp_oplevel;
7929 rsp->Reserved = 0;
7930 rsp->Reserved2 = 0;
7931 rsp->VolatileFid = cpu_to_le64(volatile_id);
7932 rsp->PersistentFid = cpu_to_le64(persistent_id);
Namjae Jeoncb451722021-11-03 08:08:44 +09007933 inc_rfc1001_len(work->response_buf, 24);
Namjae Jeone2f34482021-03-16 10:49:09 +09007934 return;
7935
7936err_out:
7937 opinfo->op_state = OPLOCK_STATE_NONE;
7938 wake_up_interruptible_all(&opinfo->oplock_q);
7939
7940 opinfo_put(opinfo);
7941 ksmbd_fd_put(work, fp);
7942 smb2_set_err_rsp(work);
7943}
7944
7945static int check_lease_state(struct lease *lease, __le32 req_state)
7946{
7947 if ((lease->new_state ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09007948 (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) &&
7949 !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007950 lease->new_state = req_state;
7951 return 0;
7952 }
7953
7954 if (lease->new_state == req_state)
7955 return 0;
7956
7957 return 1;
7958}
7959
7960/**
7961 * smb21_lease_break_ack() - handler for smb2.1 lease break command
7962 * @work: smb work containing lease break command buffer
7963 *
7964 * Return: 0
7965 */
7966static void smb21_lease_break_ack(struct ksmbd_work *work)
7967{
7968 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09007969 struct smb2_lease_ack *req = smb2_get_msg(work->request_buf);
7970 struct smb2_lease_ack *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09007971 struct oplock_info *opinfo;
7972 __le32 err = 0;
7973 int ret = 0;
7974 unsigned int lease_change_type;
7975 __le32 lease_state;
7976 struct lease *lease;
7977
7978 ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007979 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007980 opinfo = lookup_lease_in_table(conn, req->LeaseKey);
7981 if (!opinfo) {
7982 ksmbd_debug(OPLOCK, "file not opened\n");
7983 smb2_set_err_rsp(work);
7984 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7985 return;
7986 }
7987 lease = opinfo->o_lease;
7988
7989 if (opinfo->op_state == OPLOCK_STATE_NONE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007990 pr_err("unexpected lease break state 0x%x\n",
7991 opinfo->op_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09007992 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7993 goto err_out;
7994 }
7995
7996 if (check_lease_state(lease, req->LeaseState)) {
7997 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
7998 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +09007999 "req lease state: 0x%x, expected state: 0x%x\n",
8000 req->LeaseState, lease->new_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09008001 goto err_out;
8002 }
8003
8004 if (!atomic_read(&opinfo->breaking_cnt)) {
8005 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
8006 goto err_out;
8007 }
8008
8009 /* check for bad lease state */
Namjae Jeon070fb212021-05-26 17:57:12 +09008010 if (req->LeaseState &
8011 (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008012 err = STATUS_INVALID_OPLOCK_PROTOCOL;
8013 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8014 lease_change_type = OPLOCK_WRITE_TO_NONE;
8015 else
8016 lease_change_type = OPLOCK_READ_TO_NONE;
8017 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008018 le32_to_cpu(lease->state),
8019 le32_to_cpu(req->LeaseState));
Namjae Jeon64b39f42021-03-30 14:25:35 +09008020 } else if (lease->state == SMB2_LEASE_READ_CACHING_LE &&
8021 req->LeaseState != SMB2_LEASE_NONE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008022 err = STATUS_INVALID_OPLOCK_PROTOCOL;
8023 lease_change_type = OPLOCK_READ_TO_NONE;
8024 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008025 le32_to_cpu(lease->state),
8026 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09008027 } else {
8028 /* valid lease state changes */
8029 err = STATUS_INVALID_DEVICE_STATE;
8030 if (req->LeaseState == SMB2_LEASE_NONE_LE) {
8031 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8032 lease_change_type = OPLOCK_WRITE_TO_NONE;
8033 else
8034 lease_change_type = OPLOCK_READ_TO_NONE;
8035 } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) {
8036 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8037 lease_change_type = OPLOCK_WRITE_TO_READ;
8038 else
8039 lease_change_type = OPLOCK_READ_HANDLE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09008040 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09008041 lease_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09008042 }
Namjae Jeone2f34482021-03-16 10:49:09 +09008043 }
8044
8045 switch (lease_change_type) {
8046 case OPLOCK_WRITE_TO_READ:
8047 ret = opinfo_write_to_read(opinfo);
8048 break;
8049 case OPLOCK_READ_HANDLE_TO_READ:
8050 ret = opinfo_read_handle_to_read(opinfo);
8051 break;
8052 case OPLOCK_WRITE_TO_NONE:
8053 ret = opinfo_write_to_none(opinfo);
8054 break;
8055 case OPLOCK_READ_TO_NONE:
8056 ret = opinfo_read_to_none(opinfo);
8057 break;
8058 default:
8059 ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008060 le32_to_cpu(lease->state),
8061 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09008062 }
8063
8064 lease_state = lease->state;
8065 opinfo->op_state = OPLOCK_STATE_NONE;
8066 wake_up_interruptible_all(&opinfo->oplock_q);
8067 atomic_dec(&opinfo->breaking_cnt);
8068 wake_up_interruptible_all(&opinfo->oplock_brk);
8069 opinfo_put(opinfo);
8070
8071 if (ret < 0) {
8072 rsp->hdr.Status = err;
8073 goto err_out;
8074 }
8075
8076 rsp->StructureSize = cpu_to_le16(36);
8077 rsp->Reserved = 0;
8078 rsp->Flags = 0;
8079 memcpy(rsp->LeaseKey, req->LeaseKey, 16);
8080 rsp->LeaseState = lease_state;
8081 rsp->LeaseDuration = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09008082 inc_rfc1001_len(work->response_buf, 36);
Namjae Jeone2f34482021-03-16 10:49:09 +09008083 return;
8084
8085err_out:
8086 opinfo->op_state = OPLOCK_STATE_NONE;
8087 wake_up_interruptible_all(&opinfo->oplock_q);
8088 atomic_dec(&opinfo->breaking_cnt);
8089 wake_up_interruptible_all(&opinfo->oplock_brk);
8090
8091 opinfo_put(opinfo);
8092 smb2_set_err_rsp(work);
8093}
8094
8095/**
8096 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break
8097 * @work: smb work containing oplock/lease break command buffer
8098 *
8099 * Return: 0
8100 */
8101int smb2_oplock_break(struct ksmbd_work *work)
8102{
Namjae Jeoncb451722021-11-03 08:08:44 +09008103 struct smb2_oplock_break *req = smb2_get_msg(work->request_buf);
8104 struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008105
8106 switch (le16_to_cpu(req->StructureSize)) {
8107 case OP_BREAK_STRUCT_SIZE_20:
8108 smb20_oplock_break_ack(work);
8109 break;
8110 case OP_BREAK_STRUCT_SIZE_21:
8111 smb21_lease_break_ack(work);
8112 break;
8113 default:
8114 ksmbd_debug(OPLOCK, "invalid break cmd %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008115 le16_to_cpu(req->StructureSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09008116 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
8117 smb2_set_err_rsp(work);
8118 }
8119
8120 return 0;
8121}
8122
8123/**
8124 * smb2_notify() - handler for smb2 notify request
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09008125 * @work: smb work containing notify command buffer
Namjae Jeone2f34482021-03-16 10:49:09 +09008126 *
8127 * Return: 0
8128 */
8129int smb2_notify(struct ksmbd_work *work)
8130{
Ronnie Sahlberg699230f2021-09-09 12:26:12 +09008131 struct smb2_change_notify_req *req;
8132 struct smb2_change_notify_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09008133
8134 WORK_BUFFERS(work, req, rsp);
8135
8136 if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) {
8137 rsp->hdr.Status = STATUS_INTERNAL_ERROR;
8138 smb2_set_err_rsp(work);
8139 return 0;
8140 }
8141
8142 smb2_set_err_rsp(work);
8143 rsp->hdr.Status = STATUS_NOT_IMPLEMENTED;
8144 return 0;
8145}
8146
8147/**
8148 * smb2_is_sign_req() - handler for checking packet signing status
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09008149 * @work: smb work containing notify command buffer
8150 * @command: SMB2 command id
Namjae Jeone2f34482021-03-16 10:49:09 +09008151 *
8152 * Return: true if packed is signed, false otherwise
8153 */
8154bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command)
8155{
Namjae Jeoncb451722021-11-03 08:08:44 +09008156 struct smb2_hdr *rcv_hdr2 = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008157
8158 if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008159 command != SMB2_NEGOTIATE_HE &&
8160 command != SMB2_SESSION_SETUP_HE &&
8161 command != SMB2_OPLOCK_BREAK_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +09008162 return true;
8163
kernel test robot56160152021-05-12 09:24:37 +09008164 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +09008165}
8166
8167/**
8168 * smb2_check_sign_req() - handler for req packet sign processing
8169 * @work: smb work containing notify command buffer
8170 *
8171 * Return: 1 on success, 0 otherwise
8172 */
8173int smb2_check_sign_req(struct ksmbd_work *work)
8174{
Namjae Jeoncb451722021-11-03 08:08:44 +09008175 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008176 char signature_req[SMB2_SIGNATURE_SIZE];
8177 char signature[SMB2_HMACSHA256_SIZE];
8178 struct kvec iov[1];
8179 size_t len;
8180
Namjae Jeoncb451722021-11-03 08:08:44 +09008181 hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008182 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008183 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008184
8185 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
Namjae Jeoncb451722021-11-03 08:08:44 +09008186 len = get_rfc1002_len(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008187 else if (hdr->NextCommand)
8188 len = le32_to_cpu(hdr->NextCommand);
8189 else
Namjae Jeoncb451722021-11-03 08:08:44 +09008190 len = get_rfc1002_len(work->request_buf) -
Namjae Jeone2f34482021-03-16 10:49:09 +09008191 work->next_smb2_rcv_hdr_off;
8192
8193 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8194 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8195
8196 iov[0].iov_base = (char *)&hdr->ProtocolId;
8197 iov[0].iov_len = len;
8198
8199 if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1,
Namjae Jeon64b39f42021-03-30 14:25:35 +09008200 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09008201 return 0;
8202
8203 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008204 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008205 return 0;
8206 }
8207
8208 return 1;
8209}
8210
8211/**
8212 * smb2_set_sign_rsp() - handler for rsp packet sign processing
8213 * @work: smb work containing notify command buffer
8214 *
8215 */
8216void smb2_set_sign_rsp(struct ksmbd_work *work)
8217{
Namjae Jeoncb451722021-11-03 08:08:44 +09008218 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008219 struct smb2_hdr *req_hdr;
8220 char signature[SMB2_HMACSHA256_SIZE];
8221 struct kvec iov[2];
8222 size_t len;
8223 int n_vec = 1;
8224
Namjae Jeoncb451722021-11-03 08:08:44 +09008225 hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008226 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008227 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008228
Namjae Jeon8a893312021-06-25 13:43:37 +09008229 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008230
8231 if (!work->next_smb2_rsp_hdr_off) {
Namjae Jeoncb451722021-11-03 08:08:44 +09008232 len = get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008233 if (req_hdr->NextCommand)
8234 len = ALIGN(len, 8);
8235 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09008236 len = get_rfc1002_len(work->response_buf) -
8237 work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +09008238 len = ALIGN(len, 8);
8239 }
8240
8241 if (req_hdr->NextCommand)
8242 hdr->NextCommand = cpu_to_le32(len);
8243
8244 hdr->Flags |= SMB2_FLAGS_SIGNED;
8245 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8246
8247 iov[0].iov_base = (char *)&hdr->ProtocolId;
8248 iov[0].iov_len = len;
8249
Namjae Jeone5066492021-03-30 12:35:23 +09008250 if (work->aux_payload_sz) {
8251 iov[0].iov_len -= work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008252
Namjae Jeone5066492021-03-30 12:35:23 +09008253 iov[1].iov_base = work->aux_payload_buf;
8254 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008255 n_vec++;
8256 }
8257
8258 if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec,
Namjae Jeon64b39f42021-03-30 14:25:35 +09008259 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09008260 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8261}
8262
8263/**
8264 * smb3_check_sign_req() - handler for req packet sign processing
8265 * @work: smb work containing notify command buffer
8266 *
8267 * Return: 1 on success, 0 otherwise
8268 */
8269int smb3_check_sign_req(struct ksmbd_work *work)
8270{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008271 struct ksmbd_conn *conn = work->conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09008272 char *signing_key;
Namjae Jeoncb451722021-11-03 08:08:44 +09008273 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008274 struct channel *chann;
8275 char signature_req[SMB2_SIGNATURE_SIZE];
8276 char signature[SMB2_CMACAES_SIZE];
8277 struct kvec iov[1];
8278 size_t len;
8279
Namjae Jeoncb451722021-11-03 08:08:44 +09008280 hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008281 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008282 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008283
8284 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
Namjae Jeoncb451722021-11-03 08:08:44 +09008285 len = get_rfc1002_len(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008286 else if (hdr->NextCommand)
8287 len = le32_to_cpu(hdr->NextCommand);
8288 else
Namjae Jeoncb451722021-11-03 08:08:44 +09008289 len = get_rfc1002_len(work->request_buf) -
Namjae Jeone2f34482021-03-16 10:49:09 +09008290 work->next_smb2_rcv_hdr_off;
8291
8292 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
8293 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008294 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008295 chann = lookup_chann_list(work->sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008296 if (!chann)
8297 return 0;
8298 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008299 }
8300
8301 if (!signing_key) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008302 pr_err("SMB3 signing key is not generated\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008303 return 0;
8304 }
8305
8306 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8307 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8308 iov[0].iov_base = (char *)&hdr->ProtocolId;
8309 iov[0].iov_len = len;
8310
8311 if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature))
8312 return 0;
8313
8314 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008315 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008316 return 0;
8317 }
8318
8319 return 1;
8320}
8321
8322/**
8323 * smb3_set_sign_rsp() - handler for rsp packet sign processing
8324 * @work: smb work containing notify command buffer
8325 *
8326 */
8327void smb3_set_sign_rsp(struct ksmbd_work *work)
8328{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008329 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09008330 struct smb2_hdr *req_hdr, *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008331 struct channel *chann;
8332 char signature[SMB2_CMACAES_SIZE];
8333 struct kvec iov[2];
8334 int n_vec = 1;
8335 size_t len;
8336 char *signing_key;
8337
Namjae Jeoncb451722021-11-03 08:08:44 +09008338 hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008339 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008340 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008341
Namjae Jeon8a893312021-06-25 13:43:37 +09008342 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008343
8344 if (!work->next_smb2_rsp_hdr_off) {
Namjae Jeoncb451722021-11-03 08:08:44 +09008345 len = get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008346 if (req_hdr->NextCommand)
8347 len = ALIGN(len, 8);
8348 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09008349 len = get_rfc1002_len(work->response_buf) -
8350 work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +09008351 len = ALIGN(len, 8);
8352 }
8353
Namjae Jeon08bdbc62021-07-27 09:30:29 +09008354 if (conn->binding == false &&
8355 le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008356 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008357 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008358 chann = lookup_chann_list(work->sess, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008359 if (!chann)
8360 return;
8361 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008362 }
8363
8364 if (!signing_key)
8365 return;
8366
8367 if (req_hdr->NextCommand)
8368 hdr->NextCommand = cpu_to_le32(len);
8369
8370 hdr->Flags |= SMB2_FLAGS_SIGNED;
8371 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8372 iov[0].iov_base = (char *)&hdr->ProtocolId;
8373 iov[0].iov_len = len;
Namjae Jeone5066492021-03-30 12:35:23 +09008374 if (work->aux_payload_sz) {
8375 iov[0].iov_len -= work->aux_payload_sz;
8376 iov[1].iov_base = work->aux_payload_buf;
8377 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008378 n_vec++;
8379 }
8380
8381 if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature))
8382 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8383}
8384
8385/**
8386 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response
8387 * @work: smb work containing response buffer
8388 *
8389 */
8390void smb3_preauth_hash_rsp(struct ksmbd_work *work)
8391{
8392 struct ksmbd_conn *conn = work->conn;
8393 struct ksmbd_session *sess = work->sess;
8394 struct smb2_hdr *req, *rsp;
8395
8396 if (conn->dialect != SMB311_PROT_ID)
8397 return;
8398
8399 WORK_BUFFERS(work, req, rsp);
8400
Namjae Jeon442ff9e2021-09-29 15:44:32 +09008401 if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE &&
8402 conn->preauth_info)
Namjae Jeoncb451722021-11-03 08:08:44 +09008403 ksmbd_gen_preauth_integrity_hash(conn, work->response_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008404 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09008405
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008406 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008407 __u8 *hash_value;
8408
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008409 if (conn->binding) {
8410 struct preauth_session *preauth_sess;
8411
8412 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
8413 if (!preauth_sess)
8414 return;
8415 hash_value = preauth_sess->Preauth_HashValue;
8416 } else {
8417 hash_value = sess->Preauth_HashValue;
8418 if (!hash_value)
8419 return;
8420 }
Namjae Jeoncb451722021-11-03 08:08:44 +09008421 ksmbd_gen_preauth_integrity_hash(conn, work->response_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008422 hash_value);
Namjae Jeone2f34482021-03-16 10:49:09 +09008423 }
8424}
8425
Namjae Jeon2dd91292021-11-03 08:25:54 +09008426static void fill_transform_hdr(void *tr_buf, char *old_buf, __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09008427{
Namjae Jeon2dd91292021-11-03 08:25:54 +09008428 struct smb2_transform_hdr *tr_hdr = tr_buf + 4;
8429 struct smb2_hdr *hdr = smb2_get_msg(old_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008430 unsigned int orig_len = get_rfc1002_len(old_buf);
8431
Namjae Jeon2dd91292021-11-03 08:25:54 +09008432 memset(tr_buf, 0, sizeof(struct smb2_transform_hdr) + 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09008433 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
8434 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
Ronnie Sahlberg4355a8f2021-11-03 08:43:42 +09008435 tr_hdr->Flags = cpu_to_le16(TRANSFORM_FLAG_ENCRYPTED);
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008436 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
8437 cipher_type == SMB2_ENCRYPTION_AES256_GCM)
8438 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008439 else
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008440 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008441 memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8);
Namjae Jeon2dd91292021-11-03 08:25:54 +09008442 inc_rfc1001_len(tr_buf, sizeof(struct smb2_transform_hdr));
8443 inc_rfc1001_len(tr_buf, orig_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09008444}
8445
8446int smb3_encrypt_resp(struct ksmbd_work *work)
8447{
Namjae Jeone5066492021-03-30 12:35:23 +09008448 char *buf = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008449 struct kvec iov[3];
8450 int rc = -ENOMEM;
Namjae Jeone5066492021-03-30 12:35:23 +09008451 int buf_size = 0, rq_nvec = 2 + (work->aux_payload_sz ? 1 : 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09008452
8453 if (ARRAY_SIZE(iov) < rq_nvec)
8454 return -ENOMEM;
8455
Namjae Jeon2dd91292021-11-03 08:25:54 +09008456 work->tr_buf = kzalloc(sizeof(struct smb2_transform_hdr) + 4, GFP_KERNEL);
8457 if (!work->tr_buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09008458 return rc;
8459
8460 /* fill transform header */
Namjae Jeon2dd91292021-11-03 08:25:54 +09008461 fill_transform_hdr(work->tr_buf, buf, work->conn->cipher_type);
Namjae Jeone2f34482021-03-16 10:49:09 +09008462
Namjae Jeon2dd91292021-11-03 08:25:54 +09008463 iov[0].iov_base = work->tr_buf;
8464 iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008465 buf_size += iov[0].iov_len - 4;
8466
8467 iov[1].iov_base = buf + 4;
8468 iov[1].iov_len = get_rfc1002_len(buf);
Namjae Jeone5066492021-03-30 12:35:23 +09008469 if (work->aux_payload_sz) {
8470 iov[1].iov_len = work->resp_hdr_sz - 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008471
Namjae Jeone5066492021-03-30 12:35:23 +09008472 iov[2].iov_base = work->aux_payload_buf;
8473 iov[2].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008474 buf_size += iov[2].iov_len;
8475 }
8476 buf_size += iov[1].iov_len;
8477 work->resp_hdr_sz = iov[1].iov_len;
8478
8479 rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1);
8480 if (rc)
8481 return rc;
8482
8483 memmove(buf, iov[1].iov_base, iov[1].iov_len);
Namjae Jeon2dd91292021-11-03 08:25:54 +09008484 *(__be32 *)work->tr_buf = cpu_to_be32(buf_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09008485
8486 return rc;
8487}
8488
Namjae Jeonf4228b62021-08-12 10:16:40 +09008489bool smb3_is_transform_hdr(void *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09008490{
Namjae Jeon2dd91292021-11-03 08:25:54 +09008491 struct smb2_transform_hdr *trhdr = smb2_get_msg(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008492
8493 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
8494}
8495
8496int smb3_decrypt_req(struct ksmbd_work *work)
8497{
8498 struct ksmbd_conn *conn = work->conn;
8499 struct ksmbd_session *sess;
Namjae Jeone5066492021-03-30 12:35:23 +09008500 char *buf = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008501 unsigned int pdu_length = get_rfc1002_len(buf);
8502 struct kvec iov[2];
Namjae Jeon2dd91292021-11-03 08:25:54 +09008503 int buf_data_size = pdu_length - sizeof(struct smb2_transform_hdr);
8504 struct smb2_transform_hdr *tr_hdr = smb2_get_msg(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008505 int rc = 0;
8506
Namjae Jeonc7705ee2021-10-04 20:44:52 +09008507 if (buf_data_size < sizeof(struct smb2_hdr)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008508 pr_err("Transform message is too small (%u)\n",
8509 pdu_length);
Namjae Jeone2f34482021-03-16 10:49:09 +09008510 return -ECONNABORTED;
8511 }
8512
Namjae Jeonc7705ee2021-10-04 20:44:52 +09008513 if (buf_data_size < le32_to_cpu(tr_hdr->OriginalMessageSize)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008514 pr_err("Transform message is broken\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008515 return -ECONNABORTED;
8516 }
8517
Namjae Jeon4227f812021-09-29 19:52:51 +09008518 sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId));
8519 if (!sess) {
8520 pr_err("invalid session id(%llx) in transform header\n",
8521 le64_to_cpu(tr_hdr->SessionId));
8522 return -ECONNABORTED;
8523 }
8524
Namjae Jeone2f34482021-03-16 10:49:09 +09008525 iov[0].iov_base = buf;
Namjae Jeon2dd91292021-11-03 08:25:54 +09008526 iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4;
8527 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008528 iov[1].iov_len = buf_data_size;
8529 rc = ksmbd_crypt_message(conn, iov, 2, 0);
8530 if (rc)
8531 return rc;
8532
8533 memmove(buf + 4, iov[1].iov_base, buf_data_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09008534 *(__be32 *)buf = cpu_to_be32(buf_data_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09008535
8536 return rc;
8537}
8538
8539bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work)
8540{
8541 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09008542 struct smb2_hdr *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008543
8544 if (conn->dialect < SMB30_PROT_ID)
8545 return false;
8546
8547 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008548 rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008549
8550 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008551 rsp->Status == STATUS_SUCCESS)
Namjae Jeone2f34482021-03-16 10:49:09 +09008552 return true;
8553 return false;
8554}