blob: 125590d5e9402486eff8d2f992cdf811ef3e2183 [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)) {
Ronnie Sahlberg26a27872021-11-03 08:45:52 +0900716 attr = FILE_ATTRIBUTE_DIRECTORY |
717 (attribute & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM));
Namjae Jeon64b39f42021-03-30 14:25:35 +0900718 } else {
Ronnie Sahlberg26a27872021-11-03 08:45:52 +0900719 attr = (attribute & 0x00005137) | FILE_ATTRIBUTE_ARCHIVE;
720 attr &= ~(FILE_ATTRIBUTE_DIRECTORY);
Namjae Jeone2f34482021-03-16 10:49:09 +0900721 if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps &
722 FILE_SUPPORTS_SPARSE_FILES))
Ronnie Sahlberg26a27872021-11-03 08:45:52 +0900723 attr |= FILE_ATTRIBUTE_SPARSE_FILE;
Namjae Jeone2f34482021-03-16 10:49:09 +0900724
725 if (smb2_get_reparse_tag_special_file(stat->mode))
Ronnie Sahlberg26a27872021-11-03 08:45:52 +0900726 attr |= FILE_ATTRIBUTE_REPARSE_POINT;
Namjae Jeone2f34482021-03-16 10:49:09 +0900727 }
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) ||
Christophe JAILLETf8fbfd82021-11-07 16:22:57 +01001700 negblob_len < offsetof(struct negotiate_message, NegotiateFlags)) {
1701 rc = -EINVAL;
1702 goto out_err;
1703 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001704
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001705 negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId +
1706 negblob_off);
1707
1708 if (decode_negotiation_token(conn, negblob, negblob_len) == 0) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001709 if (conn->mechToken)
1710 negblob = (struct negotiate_message *)conn->mechToken;
1711 }
1712
1713 if (server_conf.auth_mechs & conn->auth_mechs) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001714 rc = generate_preauth_hash(work);
1715 if (rc)
1716 goto out_err;
1717
Namjae Jeone2f34482021-03-16 10:49:09 +09001718 if (conn->preferred_auth_mech &
1719 (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001720 rc = krb5_authenticate(work);
1721 if (rc) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001722 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001723 goto out_err;
1724 }
1725
1726 ksmbd_conn_set_good(work);
1727 sess->state = SMB2_SESSION_VALID;
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001728 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001729 sess->Preauth_HashValue = NULL;
1730 } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001731 if (negblob->MessageType == NtLmNegotiate) {
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001732 rc = ntlm_negotiate(work, negblob, negblob_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001733 if (rc)
1734 goto out_err;
1735 rsp->hdr.Status =
1736 STATUS_MORE_PROCESSING_REQUIRED;
1737 /*
1738 * Note: here total size -1 is done as an
1739 * adjustment for 0 size blob
1740 */
Namjae Jeoncb451722021-11-03 08:08:44 +09001741 inc_rfc1001_len(work->response_buf,
1742 le16_to_cpu(rsp->SecurityBufferLength) - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001743
1744 } else if (negblob->MessageType == NtLmAuthenticate) {
1745 rc = ntlm_authenticate(work);
1746 if (rc)
1747 goto out_err;
1748
1749 ksmbd_conn_set_good(work);
1750 sess->state = SMB2_SESSION_VALID;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001751 if (conn->binding) {
1752 struct preauth_session *preauth_sess;
1753
1754 preauth_sess =
1755 ksmbd_preauth_session_lookup(conn, sess->id);
1756 if (preauth_sess) {
1757 list_del(&preauth_sess->preauth_entry);
1758 kfree(preauth_sess);
1759 }
1760 }
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001761 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001762 sess->Preauth_HashValue = NULL;
1763 }
1764 } else {
1765 /* TODO: need one more negotiation */
Namjae Jeonbde16942021-06-28 15:23:19 +09001766 pr_err("Not support the preferred authentication\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001767 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001768 }
1769 } else {
Namjae Jeonbde16942021-06-28 15:23:19 +09001770 pr_err("Not support authentication\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001771 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001772 }
1773
1774out_err:
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001775 if (rc == -EINVAL)
1776 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1777 else if (rc == -ENOENT)
1778 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1779 else if (rc == -EACCES)
1780 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
1781 else if (rc == -EFAULT)
1782 rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED;
Namjae Jeon58090b12021-07-16 14:52:09 +09001783 else if (rc == -ENOMEM)
1784 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001785 else if (rc)
1786 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1787
Namjae Jeone2f34482021-03-16 10:49:09 +09001788 if (conn->use_spnego && conn->mechToken) {
1789 kfree(conn->mechToken);
1790 conn->mechToken = NULL;
1791 }
1792
Namjae Jeon621be842021-10-13 17:28:31 +09001793 if (rc < 0) {
1794 /*
1795 * SecurityBufferOffset should be set to zero
1796 * in session setup error response.
1797 */
1798 rsp->SecurityBufferOffset = 0;
1799
1800 if (sess) {
1801 bool try_delay = false;
1802
1803 /*
1804 * To avoid dictionary attacks (repeated session setups rapidly sent) to
1805 * connect to server, ksmbd make a delay of a 5 seconds on session setup
1806 * failure to make it harder to send enough random connection requests
1807 * to break into a server.
1808 */
1809 if (sess->user && sess->user->flags & KSMBD_USER_FLAG_DELAY_SESSION)
1810 try_delay = true;
1811
1812 ksmbd_session_destroy(sess);
1813 work->sess = NULL;
1814 if (try_delay)
1815 ssleep(5);
1816 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001817 }
1818
1819 return rc;
1820}
1821
1822/**
1823 * smb2_tree_connect() - handler for smb2 tree connect command
1824 * @work: smb work containing smb request buffer
1825 *
1826 * Return: 0 on success, otherwise error
1827 */
1828int smb2_tree_connect(struct ksmbd_work *work)
1829{
1830 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09001831 struct smb2_tree_connect_req *req = smb2_get_msg(work->request_buf);
1832 struct smb2_tree_connect_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001833 struct ksmbd_session *sess = work->sess;
1834 char *treename = NULL, *name = NULL;
1835 struct ksmbd_tree_conn_status status;
1836 struct ksmbd_share_config *share;
1837 int rc = -EINVAL;
1838
1839 treename = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09001840 le16_to_cpu(req->PathLength), true,
1841 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09001842 if (IS_ERR(treename)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001843 pr_err("treename is NULL\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001844 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1845 goto out_err1;
1846 }
1847
Stephen Rothwell36ba3862021-03-17 17:01:15 +09001848 name = ksmbd_extract_sharename(treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001849 if (IS_ERR(name)) {
1850 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1851 goto out_err1;
1852 }
1853
1854 ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001855 name, treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001856
1857 status = ksmbd_tree_conn_connect(sess, name);
1858 if (status.ret == KSMBD_TREE_CONN_STATUS_OK)
1859 rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id);
1860 else
1861 goto out_err1;
1862
1863 share = status.tree_conn->share_conf;
1864 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
1865 ksmbd_debug(SMB, "IPC share path request\n");
1866 rsp->ShareType = SMB2_SHARE_TYPE_PIPE;
1867 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1868 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE |
1869 FILE_DELETE_LE | FILE_READ_CONTROL_LE |
1870 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1871 FILE_SYNCHRONIZE_LE;
1872 } else {
1873 rsp->ShareType = SMB2_SHARE_TYPE_DISK;
1874 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1875 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE;
1876 if (test_tree_conn_flag(status.tree_conn,
1877 KSMBD_TREE_CONN_FLAG_WRITABLE)) {
1878 rsp->MaximalAccess |= FILE_WRITE_DATA_LE |
1879 FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE |
Wan Jiabing3aefd542021-06-07 12:54:32 +08001880 FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE |
1881 FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE |
1882 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1883 FILE_SYNCHRONIZE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09001884 }
1885 }
1886
1887 status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1888 if (conn->posix_ext_supported)
1889 status.tree_conn->posix_extensions = true;
1890
1891out_err1:
1892 rsp->StructureSize = cpu_to_le16(16);
1893 rsp->Capabilities = 0;
1894 rsp->Reserved = 0;
1895 /* default manual caching */
1896 rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING;
Namjae Jeoncb451722021-11-03 08:08:44 +09001897 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09001898
1899 if (!IS_ERR(treename))
1900 kfree(treename);
1901 if (!IS_ERR(name))
1902 kfree(name);
1903
1904 switch (status.ret) {
1905 case KSMBD_TREE_CONN_STATUS_OK:
1906 rsp->hdr.Status = STATUS_SUCCESS;
1907 rc = 0;
1908 break;
1909 case KSMBD_TREE_CONN_STATUS_NO_SHARE:
1910 rsp->hdr.Status = STATUS_BAD_NETWORK_PATH;
1911 break;
1912 case -ENOMEM:
1913 case KSMBD_TREE_CONN_STATUS_NOMEM:
1914 rsp->hdr.Status = STATUS_NO_MEMORY;
1915 break;
1916 case KSMBD_TREE_CONN_STATUS_ERROR:
1917 case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS:
1918 case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS:
1919 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1920 break;
1921 case -EINVAL:
1922 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1923 break;
1924 default:
1925 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1926 }
1927
1928 return rc;
1929}
1930
1931/**
1932 * smb2_create_open_flags() - convert smb open flags to unix open flags
1933 * @file_present: is file already present
1934 * @access: file access flags
1935 * @disposition: file disposition flags
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001936 * @may_flags: set with MAY_ flags
Namjae Jeone2f34482021-03-16 10:49:09 +09001937 *
1938 * Return: file open flags
1939 */
1940static int smb2_create_open_flags(bool file_present, __le32 access,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001941 __le32 disposition,
1942 int *may_flags)
Namjae Jeone2f34482021-03-16 10:49:09 +09001943{
1944 int oflags = O_NONBLOCK | O_LARGEFILE;
1945
1946 if (access & FILE_READ_DESIRED_ACCESS_LE &&
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001947 access & FILE_WRITE_DESIRE_ACCESS_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001948 oflags |= O_RDWR;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001949 *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE;
1950 } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001951 oflags |= O_WRONLY;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001952 *may_flags = MAY_OPEN | MAY_WRITE;
1953 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09001954 oflags |= O_RDONLY;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001955 *may_flags = MAY_OPEN | MAY_READ;
1956 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001957
1958 if (access == FILE_READ_ATTRIBUTES_LE)
1959 oflags |= O_PATH;
1960
1961 if (file_present) {
1962 switch (disposition & FILE_CREATE_MASK_LE) {
1963 case FILE_OPEN_LE:
1964 case FILE_CREATE_LE:
1965 break;
1966 case FILE_SUPERSEDE_LE:
1967 case FILE_OVERWRITE_LE:
1968 case FILE_OVERWRITE_IF_LE:
1969 oflags |= O_TRUNC;
1970 break;
1971 default:
1972 break;
1973 }
1974 } else {
1975 switch (disposition & FILE_CREATE_MASK_LE) {
1976 case FILE_SUPERSEDE_LE:
1977 case FILE_CREATE_LE:
1978 case FILE_OPEN_IF_LE:
1979 case FILE_OVERWRITE_IF_LE:
1980 oflags |= O_CREAT;
1981 break;
1982 case FILE_OPEN_LE:
1983 case FILE_OVERWRITE_LE:
1984 oflags &= ~O_CREAT;
1985 break;
1986 default:
1987 break;
1988 }
1989 }
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001990
Namjae Jeone2f34482021-03-16 10:49:09 +09001991 return oflags;
1992}
1993
1994/**
1995 * smb2_tree_disconnect() - handler for smb tree connect request
1996 * @work: smb work containing request buffer
1997 *
1998 * Return: 0
1999 */
2000int smb2_tree_disconnect(struct ksmbd_work *work)
2001{
Namjae Jeoncb451722021-11-03 08:08:44 +09002002 struct smb2_tree_disconnect_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002003 struct ksmbd_session *sess = work->sess;
2004 struct ksmbd_tree_connect *tcon = work->tcon;
2005
2006 rsp->StructureSize = cpu_to_le16(4);
Namjae Jeoncb451722021-11-03 08:08:44 +09002007 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09002008
2009 ksmbd_debug(SMB, "request\n");
2010
2011 if (!tcon) {
Namjae Jeoncb451722021-11-03 08:08:44 +09002012 struct smb2_tree_disconnect_req *req =
2013 smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002014
2015 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2016 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2017 smb2_set_err_rsp(work);
2018 return 0;
2019 }
2020
2021 ksmbd_close_tree_conn_fds(work);
2022 ksmbd_tree_conn_disconnect(sess, tcon);
2023 return 0;
2024}
2025
2026/**
2027 * smb2_session_logoff() - handler for session log off request
2028 * @work: smb work containing request buffer
2029 *
2030 * Return: 0
2031 */
2032int smb2_session_logoff(struct ksmbd_work *work)
2033{
2034 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09002035 struct smb2_logoff_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002036 struct ksmbd_session *sess = work->sess;
2037
2038 rsp->StructureSize = cpu_to_le16(4);
Namjae Jeoncb451722021-11-03 08:08:44 +09002039 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09002040
2041 ksmbd_debug(SMB, "request\n");
2042
2043 /* Got a valid session, set connection state */
2044 WARN_ON(sess->conn != conn);
2045
2046 /* setting CifsExiting here may race with start_tcp_sess */
2047 ksmbd_conn_set_need_reconnect(work);
2048 ksmbd_close_session_fds(work);
2049 ksmbd_conn_wait_idle(conn);
2050
2051 if (ksmbd_tree_conn_session_logoff(sess)) {
Namjae Jeoncb451722021-11-03 08:08:44 +09002052 struct smb2_logoff_req *req = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002053
2054 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2055 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2056 smb2_set_err_rsp(work);
2057 return 0;
2058 }
2059
2060 ksmbd_destroy_file_table(&sess->file_table);
2061 sess->state = SMB2_SESSION_EXPIRED;
2062
2063 ksmbd_free_user(sess->user);
2064 sess->user = NULL;
2065
2066 /* let start_tcp_sess free connection info now */
2067 ksmbd_conn_set_need_negotiate(work);
2068 return 0;
2069}
2070
2071/**
2072 * create_smb2_pipe() - create IPC pipe
2073 * @work: smb work containing request buffer
2074 *
2075 * Return: 0 on success, otherwise error
2076 */
2077static noinline int create_smb2_pipe(struct ksmbd_work *work)
2078{
Namjae Jeoncb451722021-11-03 08:08:44 +09002079 struct smb2_create_rsp *rsp = smb2_get_msg(work->response_buf);
2080 struct smb2_create_req *req = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002081 int id;
2082 int err;
2083 char *name;
2084
2085 name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09002086 1, work->conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09002087 if (IS_ERR(name)) {
2088 rsp->hdr.Status = STATUS_NO_MEMORY;
2089 err = PTR_ERR(name);
2090 goto out;
2091 }
2092
2093 id = ksmbd_session_rpc_open(work->sess, name);
Marios Makassikis79caa962021-05-06 11:38:35 +09002094 if (id < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002095 pr_err("Unable to open RPC pipe: %d\n", id);
Marios Makassikis79caa962021-05-06 11:38:35 +09002096 err = id;
2097 goto out;
2098 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002099
Marios Makassikis79caa962021-05-06 11:38:35 +09002100 rsp->hdr.Status = STATUS_SUCCESS;
Namjae Jeone2f34482021-03-16 10:49:09 +09002101 rsp->StructureSize = cpu_to_le16(89);
2102 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002103 rsp->Flags = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002104 rsp->CreateAction = cpu_to_le32(FILE_OPENED);
2105
2106 rsp->CreationTime = cpu_to_le64(0);
2107 rsp->LastAccessTime = cpu_to_le64(0);
2108 rsp->ChangeTime = cpu_to_le64(0);
2109 rsp->AllocationSize = cpu_to_le64(0);
2110 rsp->EndofFile = cpu_to_le64(0);
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002111 rsp->FileAttributes = FILE_ATTRIBUTE_NORMAL_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09002112 rsp->Reserved2 = 0;
2113 rsp->VolatileFileId = cpu_to_le64(id);
2114 rsp->PersistentFileId = 0;
2115 rsp->CreateContextsOffset = 0;
2116 rsp->CreateContextsLength = 0;
2117
Namjae Jeoncb451722021-11-03 08:08:44 +09002118 inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/
Namjae Jeone2f34482021-03-16 10:49:09 +09002119 kfree(name);
2120 return 0;
2121
2122out:
Marios Makassikis79caa962021-05-06 11:38:35 +09002123 switch (err) {
2124 case -EINVAL:
2125 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2126 break;
2127 case -ENOSPC:
2128 case -ENOMEM:
2129 rsp->hdr.Status = STATUS_NO_MEMORY;
2130 break;
2131 }
2132
2133 if (!IS_ERR(name))
2134 kfree(name);
2135
Namjae Jeone2f34482021-03-16 10:49:09 +09002136 smb2_set_err_rsp(work);
2137 return err;
2138}
2139
Namjae Jeone2f34482021-03-16 10:49:09 +09002140/**
2141 * smb2_set_ea() - handler for setting extended attributes using set
2142 * info command
2143 * @eabuf: set info command buffer
Namjae Jeon9496e262021-09-29 15:41:48 +09002144 * @buf_len: set info command buffer length
Namjae Jeone2f34482021-03-16 10:49:09 +09002145 * @path: dentry path for get ea
2146 *
2147 * Return: 0 on success, otherwise error
2148 */
Namjae Jeon9496e262021-09-29 15:41:48 +09002149static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len,
2150 struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002151{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002152 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002153 char *attr_name = NULL, *value;
2154 int rc = 0;
Namjae Jeon9496e262021-09-29 15:41:48 +09002155 unsigned int next = 0;
2156
2157 if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength +
2158 le16_to_cpu(eabuf->EaValueLength))
2159 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002160
2161 attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL);
2162 if (!attr_name)
2163 return -ENOMEM;
2164
2165 do {
2166 if (!eabuf->EaNameLength)
2167 goto next;
2168
2169 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002170 "name : <%s>, name_len : %u, value_len : %u, next : %u\n",
2171 eabuf->name, eabuf->EaNameLength,
2172 le16_to_cpu(eabuf->EaValueLength),
2173 le32_to_cpu(eabuf->NextEntryOffset));
Namjae Jeone2f34482021-03-16 10:49:09 +09002174
2175 if (eabuf->EaNameLength >
Namjae Jeon070fb212021-05-26 17:57:12 +09002176 (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002177 rc = -EINVAL;
2178 break;
2179 }
2180
2181 memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2182 memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002183 eabuf->EaNameLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09002184 attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0';
2185 value = (char *)&eabuf->name + eabuf->EaNameLength + 1;
2186
2187 if (!eabuf->EaValueLength) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002188 rc = ksmbd_vfs_casexattr_len(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002189 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002190 attr_name,
2191 XATTR_USER_PREFIX_LEN +
2192 eabuf->EaNameLength);
2193
2194 /* delete the EA only when it exits */
2195 if (rc > 0) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002196 rc = ksmbd_vfs_remove_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002197 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002198 attr_name);
2199
2200 if (rc < 0) {
2201 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002202 "remove xattr failed(%d)\n",
2203 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002204 break;
2205 }
2206 }
2207
2208 /* if the EA doesn't exist, just do nothing. */
2209 rc = 0;
2210 } else {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002211 rc = ksmbd_vfs_setxattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002212 path->dentry, attr_name, value,
Namjae Jeon070fb212021-05-26 17:57:12 +09002213 le16_to_cpu(eabuf->EaValueLength), 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002214 if (rc < 0) {
2215 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002216 "ksmbd_vfs_setxattr is failed(%d)\n",
2217 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002218 break;
2219 }
2220 }
2221
2222next:
2223 next = le32_to_cpu(eabuf->NextEntryOffset);
Namjae Jeon9496e262021-09-29 15:41:48 +09002224 if (next == 0 || buf_len < next)
2225 break;
2226 buf_len -= next;
Namjae Jeone2f34482021-03-16 10:49:09 +09002227 eabuf = (struct smb2_ea_info *)((char *)eabuf + next);
Namjae Jeon9496e262021-09-29 15:41:48 +09002228 if (next < (u32)eabuf->EaNameLength + le16_to_cpu(eabuf->EaValueLength))
2229 break;
2230
Namjae Jeone2f34482021-03-16 10:49:09 +09002231 } while (next != 0);
2232
2233 kfree(attr_name);
2234 return rc;
2235}
2236
Namjae Jeone2f34482021-03-16 10:49:09 +09002237static noinline int smb2_set_stream_name_xattr(struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002238 struct ksmbd_file *fp,
2239 char *stream_name, int s_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09002240{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002241 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002242 size_t xattr_stream_size;
2243 char *xattr_stream_name;
2244 int rc;
2245
2246 rc = ksmbd_vfs_xattr_stream_name(stream_name,
2247 &xattr_stream_name,
2248 &xattr_stream_size,
2249 s_type);
2250 if (rc)
2251 return rc;
2252
2253 fp->stream.name = xattr_stream_name;
2254 fp->stream.size = xattr_stream_size;
2255
2256 /* Check if there is stream prefix in xattr space */
Hyunchul Lee465d7202021-07-03 12:10:36 +09002257 rc = ksmbd_vfs_casexattr_len(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002258 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002259 xattr_stream_name,
2260 xattr_stream_size);
2261 if (rc >= 0)
2262 return 0;
2263
2264 if (fp->cdoption == FILE_OPEN_LE) {
2265 ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc);
2266 return -EBADF;
2267 }
2268
Hyunchul Lee465d7202021-07-03 12:10:36 +09002269 rc = ksmbd_vfs_setxattr(user_ns, path->dentry,
2270 xattr_stream_name, NULL, 0, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002271 if (rc < 0)
Namjae Jeonbde16942021-06-28 15:23:19 +09002272 pr_err("Failed to store XATTR stream name :%d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002273 return 0;
2274}
2275
Hyunchul Leeef24c962021-06-30 18:25:52 +09002276static int smb2_remove_smb_xattrs(struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002277{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002278 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002279 char *name, *xattr_list = NULL;
2280 ssize_t xattr_list_len;
2281 int err = 0;
2282
Hyunchul Leeef24c962021-06-30 18:25:52 +09002283 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002284 if (xattr_list_len < 0) {
2285 goto out;
2286 } else if (!xattr_list_len) {
2287 ksmbd_debug(SMB, "empty xattr in the file\n");
2288 goto out;
2289 }
2290
2291 for (name = xattr_list; name - xattr_list < xattr_list_len;
2292 name += strlen(name) + 1) {
2293 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
2294
2295 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002296 strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX,
2297 DOS_ATTRIBUTE_PREFIX_LEN) &&
2298 strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09002299 continue;
2300
Hyunchul Lee465d7202021-07-03 12:10:36 +09002301 err = ksmbd_vfs_remove_xattr(user_ns, path->dentry, name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002302 if (err)
2303 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
2304 }
2305out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09002306 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002307 return err;
2308}
2309
2310static int smb2_create_truncate(struct path *path)
2311{
2312 int rc = vfs_truncate(path, 0);
2313
2314 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002315 pr_err("vfs_truncate failed, rc %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002316 return rc;
2317 }
2318
Hyunchul Leeef24c962021-06-30 18:25:52 +09002319 rc = smb2_remove_smb_xattrs(path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002320 if (rc == -EOPNOTSUPP)
2321 rc = 0;
2322 if (rc)
2323 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002324 "ksmbd_truncate_stream_name_xattr failed, rc %d\n",
2325 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002326 return rc;
2327}
2328
Namjae Jeon64b39f42021-03-30 14:25:35 +09002329static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002330 struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002331{
2332 struct xattr_dos_attrib da = {0};
2333 int rc;
2334
2335 if (!test_share_config_flag(tcon->share_conf,
2336 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
2337 return;
2338
2339 da.version = 4;
2340 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
2341 da.itime = da.create_time = fp->create_time;
2342 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
2343 XATTR_DOSINFO_ITIME;
2344
Hyunchul Leeaf349832021-06-30 18:25:53 +09002345 rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt),
2346 path->dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09002347 if (rc)
2348 ksmbd_debug(SMB, "failed to store file attribute into xattr\n");
2349}
2350
2351static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon,
Namjae Jeon070fb212021-05-26 17:57:12 +09002352 struct path *path, struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002353{
2354 struct xattr_dos_attrib da;
2355 int rc;
2356
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002357 fp->f_ci->m_fattr &= ~(FILE_ATTRIBUTE_HIDDEN_LE | FILE_ATTRIBUTE_SYSTEM_LE);
Namjae Jeone2f34482021-03-16 10:49:09 +09002358
2359 /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */
2360 if (!test_share_config_flag(tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002361 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
Namjae Jeone2f34482021-03-16 10:49:09 +09002362 return;
2363
Hyunchul Leeaf349832021-06-30 18:25:53 +09002364 rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_user_ns(path->mnt),
2365 path->dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09002366 if (rc > 0) {
2367 fp->f_ci->m_fattr = cpu_to_le32(da.attr);
2368 fp->create_time = da.create_time;
2369 fp->itime = da.itime;
2370 }
2371}
2372
Namjae Jeon64b39f42021-03-30 14:25:35 +09002373static int smb2_creat(struct ksmbd_work *work, struct path *path, char *name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002374 int open_flags, umode_t posix_mode, bool is_dir)
Namjae Jeone2f34482021-03-16 10:49:09 +09002375{
2376 struct ksmbd_tree_connect *tcon = work->tcon;
2377 struct ksmbd_share_config *share = tcon->share_conf;
2378 umode_t mode;
2379 int rc;
2380
2381 if (!(open_flags & O_CREAT))
2382 return -EBADF;
2383
2384 ksmbd_debug(SMB, "file does not exist, so creating\n");
2385 if (is_dir == true) {
2386 ksmbd_debug(SMB, "creating directory\n");
2387
2388 mode = share_config_directory_mode(share, posix_mode);
2389 rc = ksmbd_vfs_mkdir(work, name, mode);
2390 if (rc)
2391 return rc;
2392 } else {
2393 ksmbd_debug(SMB, "creating regular file\n");
2394
2395 mode = share_config_create_mode(share, posix_mode);
2396 rc = ksmbd_vfs_create(work, name, mode);
2397 if (rc)
2398 return rc;
2399 }
2400
Hyunchul Lee265fd192021-09-25 00:06:16 +09002401 rc = ksmbd_vfs_kern_path(work, name, 0, path, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002402 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002403 pr_err("cannot get linux path (%s), err = %d\n",
2404 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002405 return rc;
2406 }
2407 return 0;
2408}
2409
2410static int smb2_create_sd_buffer(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09002411 struct smb2_create_req *req,
Hyunchul Leeef24c962021-06-30 18:25:52 +09002412 struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002413{
2414 struct create_context *context;
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002415 struct create_sd_buf_req *sd_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09002416
2417 if (!req->CreateContextsOffset)
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002418 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09002419
2420 /* Parse SD BUFFER create contexts */
2421 context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER);
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002422 if (!context)
2423 return -ENOENT;
2424 else if (IS_ERR(context))
2425 return PTR_ERR(context);
Namjae Jeone2f34482021-03-16 10:49:09 +09002426
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002427 ksmbd_debug(SMB,
2428 "Set ACLs using SMB2_CREATE_SD_BUFFER context\n");
2429 sd_buf = (struct create_sd_buf_req *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002430 if (le16_to_cpu(context->DataOffset) +
2431 le32_to_cpu(context->DataLength) <
2432 sizeof(struct create_sd_buf_req))
2433 return -EINVAL;
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002434 return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd,
2435 le32_to_cpu(sd_buf->ccontext.DataLength), true);
Namjae Jeone2f34482021-03-16 10:49:09 +09002436}
2437
Christian Brauner43205ca2021-08-23 17:13:50 +02002438static void ksmbd_acls_fattr(struct smb_fattr *fattr,
2439 struct user_namespace *mnt_userns,
2440 struct inode *inode)
Namjae Jeon3d47e542021-04-20 14:25:35 +09002441{
Christian Brauner43205ca2021-08-23 17:13:50 +02002442 fattr->cf_uid = i_uid_into_mnt(mnt_userns, inode);
2443 fattr->cf_gid = i_gid_into_mnt(mnt_userns, inode);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002444 fattr->cf_mode = inode->i_mode;
Namjae Jeon777cad12021-08-13 08:15:33 +09002445 fattr->cf_acls = NULL;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002446 fattr->cf_dacls = NULL;
2447
Namjae Jeon777cad12021-08-13 08:15:33 +09002448 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
2449 fattr->cf_acls = get_acl(inode, ACL_TYPE_ACCESS);
2450 if (S_ISDIR(inode->i_mode))
2451 fattr->cf_dacls = get_acl(inode, ACL_TYPE_DEFAULT);
2452 }
Namjae Jeon3d47e542021-04-20 14:25:35 +09002453}
2454
Namjae Jeone2f34482021-03-16 10:49:09 +09002455/**
2456 * smb2_open() - handler for smb file open request
2457 * @work: smb work containing request buffer
2458 *
2459 * Return: 0 on success, otherwise error
2460 */
2461int smb2_open(struct ksmbd_work *work)
2462{
2463 struct ksmbd_conn *conn = work->conn;
2464 struct ksmbd_session *sess = work->sess;
2465 struct ksmbd_tree_connect *tcon = work->tcon;
2466 struct smb2_create_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09002467 struct smb2_create_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09002468 struct path path;
2469 struct ksmbd_share_config *share = tcon->share_conf;
2470 struct ksmbd_file *fp = NULL;
2471 struct file *filp = NULL;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002472 struct user_namespace *user_ns = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002473 struct kstat stat;
2474 struct create_context *context;
2475 struct lease_ctx_info *lc = NULL;
2476 struct create_ea_buf_req *ea_buf = NULL;
2477 struct oplock_info *opinfo;
2478 __le32 *next_ptr = NULL;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002479 int req_op_level = 0, open_flags = 0, may_flags = 0, file_info = 0;
Hyunchul Lee265fd192021-09-25 00:06:16 +09002480 int rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002481 int contxt_cnt = 0, query_disk_id = 0;
2482 int maximal_access_ctxt = 0, posix_ctxt = 0;
2483 int s_type = 0;
2484 int next_off = 0;
2485 char *name = NULL;
2486 char *stream_name = NULL;
2487 bool file_present = false, created = false, already_permitted = false;
Namjae Jeone2f34482021-03-16 10:49:09 +09002488 int share_ret, need_truncate = 0;
2489 u64 time;
2490 umode_t posix_mode = 0;
2491 __le32 daccess, maximal_access = 0;
2492
Namjae Jeone2f34482021-03-16 10:49:09 +09002493 WORK_BUFFERS(work, req, rsp);
2494
2495 if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002496 (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002497 ksmbd_debug(SMB, "invalid flag in chained command\n");
2498 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2499 smb2_set_err_rsp(work);
2500 return -EINVAL;
2501 }
2502
2503 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
2504 ksmbd_debug(SMB, "IPC pipe create request\n");
2505 return create_smb2_pipe(work);
2506 }
2507
2508 if (req->NameLength) {
2509 if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002510 *(char *)req->Buffer == '\\') {
Namjae Jeonbde16942021-06-28 15:23:19 +09002511 pr_err("not allow directory name included leading slash\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002512 rc = -EINVAL;
2513 goto err_out1;
2514 }
2515
2516 name = smb2_get_name(share,
2517 req->Buffer,
2518 le16_to_cpu(req->NameLength),
2519 work->conn->local_nls);
2520 if (IS_ERR(name)) {
2521 rc = PTR_ERR(name);
2522 if (rc != -ENOMEM)
2523 rc = -ENOENT;
Dan Carpenter8b99f352021-08-02 08:14:03 +09002524 name = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002525 goto err_out1;
2526 }
2527
2528 ksmbd_debug(SMB, "converted name = %s\n", name);
2529 if (strchr(name, ':')) {
2530 if (!test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002531 KSMBD_SHARE_FLAG_STREAMS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002532 rc = -EBADF;
2533 goto err_out1;
2534 }
2535 rc = parse_stream_name(name, &stream_name, &s_type);
2536 if (rc < 0)
2537 goto err_out1;
2538 }
2539
2540 rc = ksmbd_validate_filename(name);
2541 if (rc < 0)
2542 goto err_out1;
2543
2544 if (ksmbd_share_veto_filename(share, name)) {
2545 rc = -ENOENT;
2546 ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002547 name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002548 goto err_out1;
2549 }
2550 } else {
Hyunchul Lee265fd192021-09-25 00:06:16 +09002551 name = kstrdup("", GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09002552 if (!name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002553 rc = -ENOMEM;
2554 goto err_out1;
2555 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002556 }
2557
2558 req_op_level = req->RequestedOplockLevel;
Namjae Jeon73f9dad2021-04-16 14:12:06 +09002559 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002560 lc = parse_lease_state(req);
Namjae Jeone2f34482021-03-16 10:49:09 +09002561
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002562 if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002563 pr_err("Invalid impersonationlevel : 0x%x\n",
2564 le32_to_cpu(req->ImpersonationLevel));
Namjae Jeone2f34482021-03-16 10:49:09 +09002565 rc = -EIO;
2566 rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL;
2567 goto err_out1;
2568 }
2569
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002570 if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002571 pr_err("Invalid create options : 0x%x\n",
2572 le32_to_cpu(req->CreateOptions));
Namjae Jeone2f34482021-03-16 10:49:09 +09002573 rc = -EINVAL;
2574 goto err_out1;
2575 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09002576 if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002577 req->CreateOptions & FILE_RANDOM_ACCESS_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002578 req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE);
2579
Namjae Jeon070fb212021-05-26 17:57:12 +09002580 if (req->CreateOptions &
2581 (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION |
2582 FILE_RESERVE_OPFILTER_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002583 rc = -EOPNOTSUPP;
2584 goto err_out1;
2585 }
2586
2587 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2588 if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) {
2589 rc = -EINVAL;
2590 goto err_out1;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002591 } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002592 req->CreateOptions = ~(FILE_NO_COMPRESSION_LE);
Namjae Jeon64b39f42021-03-30 14:25:35 +09002593 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002594 }
2595 }
2596
2597 if (le32_to_cpu(req->CreateDisposition) >
Namjae Jeon070fb212021-05-26 17:57:12 +09002598 le32_to_cpu(FILE_OVERWRITE_IF_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002599 pr_err("Invalid create disposition : 0x%x\n",
2600 le32_to_cpu(req->CreateDisposition));
Namjae Jeone2f34482021-03-16 10:49:09 +09002601 rc = -EINVAL;
2602 goto err_out1;
2603 }
2604
2605 if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002606 pr_err("Invalid desired access : 0x%x\n",
2607 le32_to_cpu(req->DesiredAccess));
Namjae Jeone2f34482021-03-16 10:49:09 +09002608 rc = -EACCES;
2609 goto err_out1;
2610 }
2611
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002612 if (req->FileAttributes && !(req->FileAttributes & FILE_ATTRIBUTE_MASK_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002613 pr_err("Invalid file attribute : 0x%x\n",
2614 le32_to_cpu(req->FileAttributes));
Namjae Jeone2f34482021-03-16 10:49:09 +09002615 rc = -EINVAL;
2616 goto err_out1;
2617 }
2618
2619 if (req->CreateContextsOffset) {
2620 /* Parse non-durable handle create contexts */
2621 context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002622 if (IS_ERR(context)) {
2623 rc = PTR_ERR(context);
2624 goto err_out1;
2625 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002626 ea_buf = (struct create_ea_buf_req *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002627 if (le16_to_cpu(context->DataOffset) +
2628 le32_to_cpu(context->DataLength) <
2629 sizeof(struct create_ea_buf_req)) {
2630 rc = -EINVAL;
2631 goto err_out1;
2632 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002633 if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) {
2634 rsp->hdr.Status = STATUS_ACCESS_DENIED;
2635 rc = -EACCES;
2636 goto err_out1;
2637 }
2638 }
2639
2640 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002641 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002642 if (IS_ERR(context)) {
2643 rc = PTR_ERR(context);
2644 goto err_out1;
2645 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002646 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002647 "get query maximal access context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002648 maximal_access_ctxt = 1;
2649 }
2650
2651 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002652 SMB2_CREATE_TIMEWARP_REQUEST);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002653 if (IS_ERR(context)) {
2654 rc = PTR_ERR(context);
2655 goto err_out1;
2656 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002657 ksmbd_debug(SMB, "get timewarp context\n");
2658 rc = -EBADF;
2659 goto err_out1;
2660 }
2661
2662 if (tcon->posix_extensions) {
2663 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002664 SMB2_CREATE_TAG_POSIX);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002665 if (IS_ERR(context)) {
2666 rc = PTR_ERR(context);
2667 goto err_out1;
2668 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002669 struct create_posix *posix =
2670 (struct create_posix *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002671 if (le16_to_cpu(context->DataOffset) +
2672 le32_to_cpu(context->DataLength) <
2673 sizeof(struct create_posix)) {
2674 rc = -EINVAL;
2675 goto err_out1;
2676 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002677 ksmbd_debug(SMB, "get posix context\n");
2678
2679 posix_mode = le32_to_cpu(posix->Mode);
2680 posix_ctxt = 1;
2681 }
2682 }
2683 }
2684
2685 if (ksmbd_override_fsids(work)) {
2686 rc = -ENOMEM;
2687 goto err_out1;
2688 }
2689
Hyunchul Lee265fd192021-09-25 00:06:16 +09002690 rc = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, 1);
Namjae Jeon4ea47792021-09-21 14:19:33 +09002691 if (!rc) {
2692 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002693 /*
2694 * If file exists with under flags, return access
2695 * denied error.
2696 */
2697 if (req->CreateDisposition == FILE_OVERWRITE_IF_LE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09002698 req->CreateDisposition == FILE_OPEN_IF_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002699 rc = -EACCES;
2700 path_put(&path);
2701 goto err_out;
2702 }
2703
Namjae Jeon64b39f42021-03-30 14:25:35 +09002704 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002705 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002706 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002707 rc = -EACCES;
2708 path_put(&path);
2709 goto err_out;
2710 }
Namjae Jeon4ea47792021-09-21 14:19:33 +09002711 } else if (d_is_symlink(path.dentry)) {
2712 rc = -EACCES;
2713 path_put(&path);
2714 goto err_out;
Namjae Jeone2f34482021-03-16 10:49:09 +09002715 }
2716 }
2717
2718 if (rc) {
Hyunchul Lee265fd192021-09-25 00:06:16 +09002719 if (rc != -ENOENT)
Namjae Jeone2f34482021-03-16 10:49:09 +09002720 goto err_out;
Namjae Jeone2f34482021-03-16 10:49:09 +09002721 ksmbd_debug(SMB, "can not get linux path for %s, rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002722 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002723 rc = 0;
2724 } else {
2725 file_present = true;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002726 user_ns = mnt_user_ns(path.mnt);
2727 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09002728 }
2729 if (stream_name) {
2730 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2731 if (s_type == DATA_STREAM) {
2732 rc = -EIO;
2733 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2734 }
2735 } else {
2736 if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) {
2737 rc = -EIO;
2738 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2739 }
2740 }
2741
2742 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE &&
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002743 req->FileAttributes & FILE_ATTRIBUTE_NORMAL_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002744 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2745 rc = -EIO;
2746 }
2747
2748 if (rc < 0)
2749 goto err_out;
2750 }
2751
Namjae Jeon64b39f42021-03-30 14:25:35 +09002752 if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE &&
2753 S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002754 ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002755 name, req->CreateOptions);
Namjae Jeone2f34482021-03-16 10:49:09 +09002756 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2757 rc = -EIO;
2758 goto err_out;
2759 }
2760
2761 if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002762 !(req->CreateDisposition == FILE_CREATE_LE) &&
2763 !S_ISDIR(stat.mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002764 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2765 rc = -EIO;
2766 goto err_out;
2767 }
2768
2769 if (!stream_name && file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002770 req->CreateDisposition == FILE_CREATE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002771 rc = -EEXIST;
2772 goto err_out;
2773 }
2774
Namjae Jeone2f34482021-03-16 10:49:09 +09002775 daccess = smb_map_generic_desired_access(req->DesiredAccess);
2776
2777 if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002778 rc = smb_check_perm_dacl(conn, &path, &daccess,
Namjae Jeon070fb212021-05-26 17:57:12 +09002779 sess->user->uid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002780 if (rc)
2781 goto err_out;
2782 }
2783
2784 if (daccess & FILE_MAXIMAL_ACCESS_LE) {
2785 if (!file_present) {
2786 daccess = cpu_to_le32(GENERIC_ALL_FLAGS);
2787 } else {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002788 rc = ksmbd_vfs_query_maximal_access(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002789 path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002790 &daccess);
2791 if (rc)
2792 goto err_out;
2793 already_permitted = true;
2794 }
2795 maximal_access = daccess;
2796 }
2797
Namjae Jeon070fb212021-05-26 17:57:12 +09002798 open_flags = smb2_create_open_flags(file_present, daccess,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002799 req->CreateDisposition,
2800 &may_flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09002801
2802 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
2803 if (open_flags & O_CREAT) {
2804 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002805 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002806 rc = -EACCES;
2807 goto err_out;
2808 }
2809 }
2810
2811 /*create file if not present */
2812 if (!file_present) {
2813 rc = smb2_creat(work, &path, name, open_flags, posix_mode,
Namjae Jeon070fb212021-05-26 17:57:12 +09002814 req->CreateOptions & FILE_DIRECTORY_FILE_LE);
Marios Makassikisd337a442021-07-27 09:24:51 +09002815 if (rc) {
2816 if (rc == -ENOENT) {
2817 rc = -EIO;
2818 rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND;
2819 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002820 goto err_out;
Marios Makassikisd337a442021-07-27 09:24:51 +09002821 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002822
2823 created = true;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002824 user_ns = mnt_user_ns(path.mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002825 if (ea_buf) {
Namjae Jeon9496e262021-09-29 15:41:48 +09002826 if (le32_to_cpu(ea_buf->ccontext.DataLength) <
2827 sizeof(struct smb2_ea_info)) {
2828 rc = -EINVAL;
2829 goto err_out;
2830 }
2831
2832 rc = smb2_set_ea(&ea_buf->ea,
2833 le32_to_cpu(ea_buf->ccontext.DataLength),
2834 &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002835 if (rc == -EOPNOTSUPP)
2836 rc = 0;
2837 else if (rc)
2838 goto err_out;
2839 }
2840 } else if (!already_permitted) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002841 /* FILE_READ_ATTRIBUTE is allowed without inode_permission,
2842 * because execute(search) permission on a parent directory,
2843 * is already granted.
2844 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09002845 if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002846 rc = inode_permission(user_ns,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002847 d_inode(path.dentry),
2848 may_flags);
Namjae Jeonff1d5722021-04-13 13:18:10 +09002849 if (rc)
Namjae Jeone2f34482021-03-16 10:49:09 +09002850 goto err_out;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002851
2852 if ((daccess & FILE_DELETE_LE) ||
2853 (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002854 rc = ksmbd_vfs_may_delete(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002855 path.dentry);
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002856 if (rc)
2857 goto err_out;
2858 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002859 }
2860 }
2861
2862 rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent));
2863 if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) {
2864 rc = -EBUSY;
2865 goto err_out;
2866 }
2867
2868 rc = 0;
2869 filp = dentry_open(&path, open_flags, current_cred());
2870 if (IS_ERR(filp)) {
2871 rc = PTR_ERR(filp);
Namjae Jeonbde16942021-06-28 15:23:19 +09002872 pr_err("dentry open for dir failed, rc %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002873 goto err_out;
2874 }
2875
2876 if (file_present) {
2877 if (!(open_flags & O_TRUNC))
2878 file_info = FILE_OPENED;
2879 else
2880 file_info = FILE_OVERWRITTEN;
2881
Namjae Jeon070fb212021-05-26 17:57:12 +09002882 if ((req->CreateDisposition & FILE_CREATE_MASK_LE) ==
2883 FILE_SUPERSEDE_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002884 file_info = FILE_SUPERSEDED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002885 } else if (open_flags & O_CREAT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002886 file_info = FILE_CREATED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002887 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002888
2889 ksmbd_vfs_set_fadvise(filp, req->CreateOptions);
2890
2891 /* Obtain Volatile-ID */
2892 fp = ksmbd_open_fd(work, filp);
2893 if (IS_ERR(fp)) {
2894 fput(filp);
2895 rc = PTR_ERR(fp);
2896 fp = NULL;
2897 goto err_out;
2898 }
2899
2900 /* Get Persistent-ID */
2901 ksmbd_open_durable_fd(fp);
Namjae Jeon38673692021-07-08 12:32:27 +09002902 if (!has_file_id(fp->persistent_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002903 rc = -ENOMEM;
2904 goto err_out;
2905 }
2906
2907 fp->filename = name;
2908 fp->cdoption = req->CreateDisposition;
2909 fp->daccess = daccess;
2910 fp->saccess = req->ShareAccess;
2911 fp->coption = req->CreateOptions;
2912
2913 /* Set default windows and posix acls if creating new file */
2914 if (created) {
2915 int posix_acl_rc;
Namjae Jeonfba08fa2021-04-15 10:29:39 +09002916 struct inode *inode = d_inode(path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09002917
Hyunchul Lee465d7202021-07-03 12:10:36 +09002918 posix_acl_rc = ksmbd_vfs_inherit_posix_acl(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002919 inode,
2920 d_inode(path.dentry->d_parent));
Namjae Jeone2f34482021-03-16 10:49:09 +09002921 if (posix_acl_rc)
2922 ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc);
2923
2924 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002925 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002926 rc = smb_inherit_dacl(conn, &path, sess->user->uid,
Namjae Jeon070fb212021-05-26 17:57:12 +09002927 sess->user->gid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002928 }
2929
2930 if (rc) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002931 rc = smb2_create_sd_buffer(work, req, &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002932 if (rc) {
2933 if (posix_acl_rc)
Hyunchul Lee465d7202021-07-03 12:10:36 +09002934 ksmbd_vfs_set_init_posix_acl(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002935 inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09002936
2937 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002938 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002939 struct smb_fattr fattr;
2940 struct smb_ntsd *pntsd;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002941 int pntsd_size, ace_num = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002942
Christian Brauner43205ca2021-08-23 17:13:50 +02002943 ksmbd_acls_fattr(&fattr, user_ns, inode);
Marios Makassikise6b10592021-04-15 10:24:56 +09002944 if (fattr.cf_acls)
2945 ace_num = fattr.cf_acls->a_count;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002946 if (fattr.cf_dacls)
2947 ace_num += fattr.cf_dacls->a_count;
Namjae Jeone2f34482021-03-16 10:49:09 +09002948
2949 pntsd = kmalloc(sizeof(struct smb_ntsd) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002950 sizeof(struct smb_sid) * 3 +
Namjae Jeone2f34482021-03-16 10:49:09 +09002951 sizeof(struct smb_acl) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002952 sizeof(struct smb_ace) * ace_num * 2,
Namjae Jeone2f34482021-03-16 10:49:09 +09002953 GFP_KERNEL);
2954 if (!pntsd)
2955 goto err_out;
2956
Hyunchul Lee465d7202021-07-03 12:10:36 +09002957 rc = build_sec_desc(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002958 pntsd, NULL,
Namjae Jeon070fb212021-05-26 17:57:12 +09002959 OWNER_SECINFO |
Hyunchul Leeaf349832021-06-30 18:25:53 +09002960 GROUP_SECINFO |
2961 DACL_SECINFO,
Namjae Jeon070fb212021-05-26 17:57:12 +09002962 &pntsd_size, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09002963 posix_acl_release(fattr.cf_acls);
2964 posix_acl_release(fattr.cf_dacls);
Namjae Jeonf2e78af2021-12-01 10:12:39 +09002965 if (rc) {
2966 kfree(pntsd);
2967 goto err_out;
2968 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002969
2970 rc = ksmbd_vfs_set_sd_xattr(conn,
Hyunchul Lee465d7202021-07-03 12:10:36 +09002971 user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +09002972 path.dentry,
2973 pntsd,
2974 pntsd_size);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002975 kfree(pntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09002976 if (rc)
Namjae Jeonbde16942021-06-28 15:23:19 +09002977 pr_err("failed to store ntacl in xattr : %d\n",
2978 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002979 }
2980 }
2981 }
2982 rc = 0;
2983 }
2984
2985 if (stream_name) {
2986 rc = smb2_set_stream_name_xattr(&path,
2987 fp,
2988 stream_name,
2989 s_type);
2990 if (rc)
2991 goto err_out;
2992 file_info = FILE_CREATED;
2993 }
2994
2995 fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE |
2996 FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE));
Namjae Jeon64b39f42021-03-30 14:25:35 +09002997 if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC &&
2998 !fp->attrib_only && !stream_name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002999 smb_break_all_oplock(work, fp);
3000 need_truncate = 1;
3001 }
3002
3003 /* fp should be searchable through ksmbd_inode.m_fp_list
3004 * after daccess, saccess, attrib_only, and stream are
3005 * initialized.
3006 */
3007 write_lock(&fp->f_ci->m_lock);
3008 list_add(&fp->node, &fp->f_ci->m_fp_list);
3009 write_unlock(&fp->f_ci->m_lock);
3010
3011 rc = ksmbd_vfs_getattr(&path, &stat);
3012 if (rc) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09003013 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09003014 rc = 0;
3015 }
3016
3017 /* Check delete pending among previous fp before oplock break */
3018 if (ksmbd_inode_pending_delete(fp)) {
3019 rc = -EBUSY;
3020 goto err_out;
3021 }
3022
3023 share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp);
Namjae Jeon64b39f42021-03-30 14:25:35 +09003024 if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) ||
3025 (req_op_level == SMB2_OPLOCK_LEVEL_LEASE &&
3026 !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) {
Namjae Jeonab0b2632021-06-29 09:20:13 +09003027 if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003028 rc = share_ret;
3029 goto err_out;
3030 }
3031 } else {
3032 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) {
3033 req_op_level = smb2_map_lease_to_oplock(lc->req_state);
3034 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003035 "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n",
3036 name, req_op_level, lc->req_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09003037 rc = find_same_lease_key(sess, fp->f_ci, lc);
3038 if (rc)
3039 goto err_out;
3040 } else if (open_flags == O_RDONLY &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09003041 (req_op_level == SMB2_OPLOCK_LEVEL_BATCH ||
3042 req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
Namjae Jeone2f34482021-03-16 10:49:09 +09003043 req_op_level = SMB2_OPLOCK_LEVEL_II;
3044
3045 rc = smb_grant_oplock(work, req_op_level,
3046 fp->persistent_id, fp,
3047 le32_to_cpu(req->hdr.Id.SyncId.TreeId),
3048 lc, share_ret);
3049 if (rc < 0)
3050 goto err_out;
3051 }
3052
3053 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
3054 ksmbd_fd_set_delete_on_close(fp, file_info);
3055
3056 if (need_truncate) {
3057 rc = smb2_create_truncate(&path);
3058 if (rc)
3059 goto err_out;
3060 }
3061
3062 if (req->CreateContextsOffset) {
3063 struct create_alloc_size_req *az_req;
3064
Namjae Jeon070fb212021-05-26 17:57:12 +09003065 az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req,
3066 SMB2_CREATE_ALLOCATION_SIZE);
Namjae Jeonf19b3962021-07-13 09:59:34 +09003067 if (IS_ERR(az_req)) {
3068 rc = PTR_ERR(az_req);
3069 goto err_out;
3070 } else if (az_req) {
Hyunchul Lee8f771502021-09-24 22:22:22 +09003071 loff_t alloc_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09003072 int err;
3073
Hyunchul Lee8f771502021-09-24 22:22:22 +09003074 if (le16_to_cpu(az_req->ccontext.DataOffset) +
3075 le32_to_cpu(az_req->ccontext.DataLength) <
3076 sizeof(struct create_alloc_size_req)) {
3077 rc = -EINVAL;
3078 goto err_out;
3079 }
3080 alloc_size = le64_to_cpu(az_req->AllocationSize);
Namjae Jeone2f34482021-03-16 10:49:09 +09003081 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003082 "request smb2 create allocate size : %llu\n",
3083 alloc_size);
Namjae Jeone8c06192021-06-22 11:06:11 +09003084 smb_break_all_levII_oplock(work, fp, 1);
3085 err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
3086 alloc_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003087 if (err < 0)
3088 ksmbd_debug(SMB,
Namjae Jeone8c06192021-06-22 11:06:11 +09003089 "vfs_fallocate is failed : %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003090 err);
Namjae Jeone2f34482021-03-16 10:49:09 +09003091 }
3092
Namjae Jeon64b39f42021-03-30 14:25:35 +09003093 context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID);
Namjae Jeonf19b3962021-07-13 09:59:34 +09003094 if (IS_ERR(context)) {
3095 rc = PTR_ERR(context);
3096 goto err_out;
3097 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003098 ksmbd_debug(SMB, "get query on disk id context\n");
3099 query_disk_id = 1;
3100 }
3101 }
3102
3103 if (stat.result_mask & STATX_BTIME)
3104 fp->create_time = ksmbd_UnixTimeToNT(stat.btime);
3105 else
3106 fp->create_time = ksmbd_UnixTimeToNT(stat.ctime);
3107 if (req->FileAttributes || fp->f_ci->m_fattr == 0)
Namjae Jeon070fb212021-05-26 17:57:12 +09003108 fp->f_ci->m_fattr =
3109 cpu_to_le32(smb2_get_dos_mode(&stat, le32_to_cpu(req->FileAttributes)));
Namjae Jeone2f34482021-03-16 10:49:09 +09003110
3111 if (!created)
3112 smb2_update_xattrs(tcon, &path, fp);
3113 else
3114 smb2_new_xattrs(tcon, &path, fp);
3115
3116 memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE);
3117
Hyunchul Lee465d7202021-07-03 12:10:36 +09003118 generic_fillattr(user_ns, file_inode(fp->filp),
Hyunchul Leeaf349832021-06-30 18:25:53 +09003119 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09003120
3121 rsp->StructureSize = cpu_to_le16(89);
3122 rcu_read_lock();
3123 opinfo = rcu_dereference(fp->f_opinfo);
3124 rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0;
3125 rcu_read_unlock();
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003126 rsp->Flags = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09003127 rsp->CreateAction = cpu_to_le32(file_info);
3128 rsp->CreationTime = cpu_to_le64(fp->create_time);
3129 time = ksmbd_UnixTimeToNT(stat.atime);
3130 rsp->LastAccessTime = cpu_to_le64(time);
3131 time = ksmbd_UnixTimeToNT(stat.mtime);
3132 rsp->LastWriteTime = cpu_to_le64(time);
3133 time = ksmbd_UnixTimeToNT(stat.ctime);
3134 rsp->ChangeTime = cpu_to_le64(time);
3135 rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 :
3136 cpu_to_le64(stat.blocks << 9);
3137 rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
3138 rsp->FileAttributes = fp->f_ci->m_fattr;
3139
3140 rsp->Reserved2 = 0;
3141
3142 rsp->PersistentFileId = cpu_to_le64(fp->persistent_id);
3143 rsp->VolatileFileId = cpu_to_le64(fp->volatile_id);
3144
3145 rsp->CreateContextsOffset = 0;
3146 rsp->CreateContextsLength = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09003147 inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/
Namjae Jeone2f34482021-03-16 10:49:09 +09003148
3149 /* If lease is request send lease context response */
3150 if (opinfo && opinfo->is_lease) {
3151 struct create_context *lease_ccontext;
3152
3153 ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003154 name, opinfo->o_lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +09003155 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
3156
3157 lease_ccontext = (struct create_context *)rsp->Buffer;
3158 contxt_cnt++;
3159 create_lease_buf(rsp->Buffer, opinfo->o_lease);
3160 le32_add_cpu(&rsp->CreateContextsLength,
3161 conn->vals->create_lease_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003162 inc_rfc1001_len(work->response_buf,
3163 conn->vals->create_lease_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003164 next_ptr = &lease_ccontext->Next;
3165 next_off = conn->vals->create_lease_size;
3166 }
3167
Namjae Jeone2f34482021-03-16 10:49:09 +09003168 if (maximal_access_ctxt) {
3169 struct create_context *mxac_ccontext;
3170
3171 if (maximal_access == 0)
Hyunchul Lee465d7202021-07-03 12:10:36 +09003172 ksmbd_vfs_query_maximal_access(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09003173 path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09003174 &maximal_access);
3175 mxac_ccontext = (struct create_context *)(rsp->Buffer +
3176 le32_to_cpu(rsp->CreateContextsLength));
3177 contxt_cnt++;
3178 create_mxac_rsp_buf(rsp->Buffer +
3179 le32_to_cpu(rsp->CreateContextsLength),
3180 le32_to_cpu(maximal_access));
3181 le32_add_cpu(&rsp->CreateContextsLength,
3182 conn->vals->create_mxac_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003183 inc_rfc1001_len(work->response_buf,
3184 conn->vals->create_mxac_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003185 if (next_ptr)
3186 *next_ptr = cpu_to_le32(next_off);
3187 next_ptr = &mxac_ccontext->Next;
3188 next_off = conn->vals->create_mxac_size;
3189 }
3190
3191 if (query_disk_id) {
3192 struct create_context *disk_id_ccontext;
3193
3194 disk_id_ccontext = (struct create_context *)(rsp->Buffer +
3195 le32_to_cpu(rsp->CreateContextsLength));
3196 contxt_cnt++;
3197 create_disk_id_rsp_buf(rsp->Buffer +
3198 le32_to_cpu(rsp->CreateContextsLength),
3199 stat.ino, tcon->id);
3200 le32_add_cpu(&rsp->CreateContextsLength,
3201 conn->vals->create_disk_id_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003202 inc_rfc1001_len(work->response_buf,
3203 conn->vals->create_disk_id_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003204 if (next_ptr)
3205 *next_ptr = cpu_to_le32(next_off);
3206 next_ptr = &disk_id_ccontext->Next;
3207 next_off = conn->vals->create_disk_id_size;
3208 }
3209
3210 if (posix_ctxt) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003211 contxt_cnt++;
3212 create_posix_rsp_buf(rsp->Buffer +
3213 le32_to_cpu(rsp->CreateContextsLength),
3214 fp);
3215 le32_add_cpu(&rsp->CreateContextsLength,
3216 conn->vals->create_posix_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003217 inc_rfc1001_len(work->response_buf,
3218 conn->vals->create_posix_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003219 if (next_ptr)
3220 *next_ptr = cpu_to_le32(next_off);
3221 }
3222
3223 if (contxt_cnt > 0) {
3224 rsp->CreateContextsOffset =
Namjae Jeoncb451722021-11-03 08:08:44 +09003225 cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer));
Namjae Jeone2f34482021-03-16 10:49:09 +09003226 }
3227
3228err_out:
3229 if (file_present || created)
3230 path_put(&path);
3231 ksmbd_revert_fsids(work);
3232err_out1:
3233 if (rc) {
3234 if (rc == -EINVAL)
3235 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3236 else if (rc == -EOPNOTSUPP)
3237 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Hyunchul Lee265fd192021-09-25 00:06:16 +09003238 else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV)
Namjae Jeone2f34482021-03-16 10:49:09 +09003239 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3240 else if (rc == -ENOENT)
3241 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
3242 else if (rc == -EPERM)
3243 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
3244 else if (rc == -EBUSY)
3245 rsp->hdr.Status = STATUS_DELETE_PENDING;
3246 else if (rc == -EBADF)
3247 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
3248 else if (rc == -ENOEXEC)
3249 rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID;
3250 else if (rc == -ENXIO)
3251 rsp->hdr.Status = STATUS_NO_SUCH_DEVICE;
3252 else if (rc == -EEXIST)
3253 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
3254 else if (rc == -EMFILE)
3255 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
3256 if (!rsp->hdr.Status)
3257 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3258
3259 if (!fp || !fp->filename)
3260 kfree(name);
3261 if (fp)
3262 ksmbd_fd_put(work, fp);
3263 smb2_set_err_rsp(work);
3264 ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status);
3265 }
3266
3267 kfree(lc);
3268
3269 return 0;
3270}
3271
3272static int readdir_info_level_struct_sz(int info_level)
3273{
3274 switch (info_level) {
3275 case FILE_FULL_DIRECTORY_INFORMATION:
3276 return sizeof(struct file_full_directory_info);
3277 case FILE_BOTH_DIRECTORY_INFORMATION:
3278 return sizeof(struct file_both_directory_info);
3279 case FILE_DIRECTORY_INFORMATION:
3280 return sizeof(struct file_directory_info);
3281 case FILE_NAMES_INFORMATION:
3282 return sizeof(struct file_names_info);
3283 case FILEID_FULL_DIRECTORY_INFORMATION:
3284 return sizeof(struct file_id_full_dir_info);
3285 case FILEID_BOTH_DIRECTORY_INFORMATION:
3286 return sizeof(struct file_id_both_directory_info);
3287 case SMB_FIND_FILE_POSIX_INFO:
3288 return sizeof(struct smb2_posix_info);
3289 default:
3290 return -EOPNOTSUPP;
3291 }
3292}
3293
3294static int dentry_name(struct ksmbd_dir_info *d_info, int info_level)
3295{
3296 switch (info_level) {
3297 case FILE_FULL_DIRECTORY_INFORMATION:
3298 {
3299 struct file_full_directory_info *ffdinfo;
3300
3301 ffdinfo = (struct file_full_directory_info *)d_info->rptr;
3302 d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset);
3303 d_info->name = ffdinfo->FileName;
3304 d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength);
3305 return 0;
3306 }
3307 case FILE_BOTH_DIRECTORY_INFORMATION:
3308 {
3309 struct file_both_directory_info *fbdinfo;
3310
3311 fbdinfo = (struct file_both_directory_info *)d_info->rptr;
3312 d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset);
3313 d_info->name = fbdinfo->FileName;
3314 d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength);
3315 return 0;
3316 }
3317 case FILE_DIRECTORY_INFORMATION:
3318 {
3319 struct file_directory_info *fdinfo;
3320
3321 fdinfo = (struct file_directory_info *)d_info->rptr;
3322 d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset);
3323 d_info->name = fdinfo->FileName;
3324 d_info->name_len = le32_to_cpu(fdinfo->FileNameLength);
3325 return 0;
3326 }
3327 case FILE_NAMES_INFORMATION:
3328 {
3329 struct file_names_info *fninfo;
3330
3331 fninfo = (struct file_names_info *)d_info->rptr;
3332 d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset);
3333 d_info->name = fninfo->FileName;
3334 d_info->name_len = le32_to_cpu(fninfo->FileNameLength);
3335 return 0;
3336 }
3337 case FILEID_FULL_DIRECTORY_INFORMATION:
3338 {
3339 struct file_id_full_dir_info *dinfo;
3340
3341 dinfo = (struct file_id_full_dir_info *)d_info->rptr;
3342 d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset);
3343 d_info->name = dinfo->FileName;
3344 d_info->name_len = le32_to_cpu(dinfo->FileNameLength);
3345 return 0;
3346 }
3347 case FILEID_BOTH_DIRECTORY_INFORMATION:
3348 {
3349 struct file_id_both_directory_info *fibdinfo;
3350
3351 fibdinfo = (struct file_id_both_directory_info *)d_info->rptr;
3352 d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset);
3353 d_info->name = fibdinfo->FileName;
3354 d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength);
3355 return 0;
3356 }
3357 case SMB_FIND_FILE_POSIX_INFO:
3358 {
3359 struct smb2_posix_info *posix_info;
3360
3361 posix_info = (struct smb2_posix_info *)d_info->rptr;
3362 d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset);
3363 d_info->name = posix_info->name;
3364 d_info->name_len = le32_to_cpu(posix_info->name_len);
3365 return 0;
3366 }
3367 default:
3368 return -EINVAL;
3369 }
3370}
3371
3372/**
3373 * smb2_populate_readdir_entry() - encode directory entry in smb2 response
3374 * buffer
3375 * @conn: connection instance
3376 * @info_level: smb information level
3377 * @d_info: structure included variables for query dir
Hyunchul Leeaf349832021-06-30 18:25:53 +09003378 * @user_ns: user namespace
Namjae Jeone2f34482021-03-16 10:49:09 +09003379 * @ksmbd_kstat: ksmbd wrapper of dirent stat information
3380 *
3381 * if directory has many entries, find first can't read it fully.
3382 * find next might be called multiple times to read remaining dir entries
3383 *
3384 * Return: 0 on success, otherwise error
3385 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003386static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level,
Namjae Jeon070fb212021-05-26 17:57:12 +09003387 struct ksmbd_dir_info *d_info,
3388 struct ksmbd_kstat *ksmbd_kstat)
Namjae Jeone2f34482021-03-16 10:49:09 +09003389{
3390 int next_entry_offset = 0;
3391 char *conv_name;
3392 int conv_len;
3393 void *kstat;
Namjae Jeondac0ec62021-07-07 14:57:24 +09003394 int struct_sz, rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09003395
3396 conv_name = ksmbd_convert_dir_info_name(d_info,
3397 conn->local_nls,
3398 &conv_len);
3399 if (!conv_name)
3400 return -ENOMEM;
3401
3402 /* Somehow the name has only terminating NULL bytes */
3403 if (conv_len < 0) {
Namjae Jeondac0ec62021-07-07 14:57:24 +09003404 rc = -EINVAL;
3405 goto free_conv_name;
Namjae Jeone2f34482021-03-16 10:49:09 +09003406 }
3407
3408 struct_sz = readdir_info_level_struct_sz(info_level);
3409 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3410 KSMBD_DIR_INFO_ALIGNMENT);
3411
3412 if (next_entry_offset > d_info->out_buf_len) {
3413 d_info->out_buf_len = 0;
Namjae Jeondac0ec62021-07-07 14:57:24 +09003414 rc = -ENOSPC;
3415 goto free_conv_name;
Namjae Jeone2f34482021-03-16 10:49:09 +09003416 }
3417
3418 kstat = d_info->wptr;
3419 if (info_level != FILE_NAMES_INFORMATION)
3420 kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat);
3421
3422 switch (info_level) {
3423 case FILE_FULL_DIRECTORY_INFORMATION:
3424 {
3425 struct file_full_directory_info *ffdinfo;
3426
3427 ffdinfo = (struct file_full_directory_info *)kstat;
3428 ffdinfo->FileNameLength = cpu_to_le32(conv_len);
3429 ffdinfo->EaSize =
3430 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3431 if (ffdinfo->EaSize)
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003432 ffdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003433 if (d_info->hide_dot_file && d_info->name[0] == '.')
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003434 ffdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003435 memcpy(ffdinfo->FileName, conv_name, conv_len);
3436 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3437 break;
3438 }
3439 case FILE_BOTH_DIRECTORY_INFORMATION:
3440 {
3441 struct file_both_directory_info *fbdinfo;
3442
3443 fbdinfo = (struct file_both_directory_info *)kstat;
3444 fbdinfo->FileNameLength = cpu_to_le32(conv_len);
3445 fbdinfo->EaSize =
3446 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3447 if (fbdinfo->EaSize)
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003448 fbdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003449 fbdinfo->ShortNameLength = 0;
3450 fbdinfo->Reserved = 0;
3451 if (d_info->hide_dot_file && d_info->name[0] == '.')
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003452 fbdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003453 memcpy(fbdinfo->FileName, conv_name, conv_len);
3454 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3455 break;
3456 }
3457 case FILE_DIRECTORY_INFORMATION:
3458 {
3459 struct file_directory_info *fdinfo;
3460
3461 fdinfo = (struct file_directory_info *)kstat;
3462 fdinfo->FileNameLength = cpu_to_le32(conv_len);
3463 if (d_info->hide_dot_file && d_info->name[0] == '.')
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003464 fdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003465 memcpy(fdinfo->FileName, conv_name, conv_len);
3466 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3467 break;
3468 }
3469 case FILE_NAMES_INFORMATION:
3470 {
3471 struct file_names_info *fninfo;
3472
3473 fninfo = (struct file_names_info *)kstat;
3474 fninfo->FileNameLength = cpu_to_le32(conv_len);
3475 memcpy(fninfo->FileName, conv_name, conv_len);
3476 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3477 break;
3478 }
3479 case FILEID_FULL_DIRECTORY_INFORMATION:
3480 {
3481 struct file_id_full_dir_info *dinfo;
3482
3483 dinfo = (struct file_id_full_dir_info *)kstat;
3484 dinfo->FileNameLength = cpu_to_le32(conv_len);
3485 dinfo->EaSize =
3486 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3487 if (dinfo->EaSize)
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003488 dinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003489 dinfo->Reserved = 0;
3490 dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3491 if (d_info->hide_dot_file && d_info->name[0] == '.')
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003492 dinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003493 memcpy(dinfo->FileName, conv_name, conv_len);
3494 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3495 break;
3496 }
3497 case FILEID_BOTH_DIRECTORY_INFORMATION:
3498 {
3499 struct file_id_both_directory_info *fibdinfo;
3500
3501 fibdinfo = (struct file_id_both_directory_info *)kstat;
3502 fibdinfo->FileNameLength = cpu_to_le32(conv_len);
3503 fibdinfo->EaSize =
3504 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3505 if (fibdinfo->EaSize)
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003506 fibdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003507 fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3508 fibdinfo->ShortNameLength = 0;
3509 fibdinfo->Reserved = 0;
3510 fibdinfo->Reserved2 = cpu_to_le16(0);
3511 if (d_info->hide_dot_file && d_info->name[0] == '.')
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003512 fibdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003513 memcpy(fibdinfo->FileName, conv_name, conv_len);
3514 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3515 break;
3516 }
3517 case SMB_FIND_FILE_POSIX_INFO:
3518 {
3519 struct smb2_posix_info *posix_info;
3520 u64 time;
3521
3522 posix_info = (struct smb2_posix_info *)kstat;
3523 posix_info->Ignored = 0;
3524 posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
3525 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
3526 posix_info->ChangeTime = cpu_to_le64(time);
3527 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime);
3528 posix_info->LastAccessTime = cpu_to_le64(time);
3529 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime);
3530 posix_info->LastWriteTime = cpu_to_le64(time);
3531 posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size);
3532 posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9);
3533 posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev);
3534 posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink);
3535 posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode);
3536 posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino);
3537 posix_info->DosAttributes =
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003538 S_ISDIR(ksmbd_kstat->kstat->mode) ?
3539 FILE_ATTRIBUTE_DIRECTORY_LE : FILE_ATTRIBUTE_ARCHIVE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003540 if (d_info->hide_dot_file && d_info->name[0] == '.')
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003541 posix_info->DosAttributes |= FILE_ATTRIBUTE_HIDDEN_LE;
Christian Brauner475d6f92021-08-23 17:13:48 +02003542 id_to_sid(from_kuid_munged(&init_user_ns, ksmbd_kstat->kstat->uid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003543 SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]);
Christian Brauner475d6f92021-08-23 17:13:48 +02003544 id_to_sid(from_kgid_munged(&init_user_ns, ksmbd_kstat->kstat->gid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003545 SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]);
Namjae Jeone2f34482021-03-16 10:49:09 +09003546 memcpy(posix_info->name, conv_name, conv_len);
3547 posix_info->name_len = cpu_to_le32(conv_len);
3548 posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset);
3549 break;
3550 }
3551
3552 } /* switch (info_level) */
3553
3554 d_info->last_entry_offset = d_info->data_count;
3555 d_info->data_count += next_entry_offset;
Marios Makassikise7735c82021-05-06 11:40:02 +09003556 d_info->out_buf_len -= next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003557 d_info->wptr += next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003558
3559 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003560 "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n",
3561 info_level, d_info->out_buf_len,
3562 next_entry_offset, d_info->data_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09003563
Namjae Jeondac0ec62021-07-07 14:57:24 +09003564free_conv_name:
3565 kfree(conv_name);
3566 return rc;
Namjae Jeone2f34482021-03-16 10:49:09 +09003567}
3568
3569struct smb2_query_dir_private {
3570 struct ksmbd_work *work;
3571 char *search_pattern;
3572 struct ksmbd_file *dir_fp;
3573
3574 struct ksmbd_dir_info *d_info;
3575 int info_level;
3576};
3577
3578static void lock_dir(struct ksmbd_file *dir_fp)
3579{
3580 struct dentry *dir = dir_fp->filp->f_path.dentry;
3581
3582 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
3583}
3584
3585static void unlock_dir(struct ksmbd_file *dir_fp)
3586{
3587 struct dentry *dir = dir_fp->filp->f_path.dentry;
3588
3589 inode_unlock(d_inode(dir));
3590}
3591
3592static int process_query_dir_entries(struct smb2_query_dir_private *priv)
3593{
Hyunchul Lee465d7202021-07-03 12:10:36 +09003594 struct user_namespace *user_ns = file_mnt_user_ns(priv->dir_fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003595 struct kstat kstat;
3596 struct ksmbd_kstat ksmbd_kstat;
3597 int rc;
3598 int i;
3599
3600 for (i = 0; i < priv->d_info->num_entry; i++) {
3601 struct dentry *dent;
3602
3603 if (dentry_name(priv->d_info, priv->info_level))
3604 return -EINVAL;
3605
3606 lock_dir(priv->dir_fp);
Christian Braunerda1e7ad2021-08-23 17:13:47 +02003607 dent = lookup_one(user_ns, priv->d_info->name,
3608 priv->dir_fp->filp->f_path.dentry,
3609 priv->d_info->name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09003610 unlock_dir(priv->dir_fp);
3611
3612 if (IS_ERR(dent)) {
3613 ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003614 priv->d_info->name,
3615 PTR_ERR(dent));
Namjae Jeone2f34482021-03-16 10:49:09 +09003616 continue;
3617 }
3618 if (unlikely(d_is_negative(dent))) {
3619 dput(dent);
3620 ksmbd_debug(SMB, "Negative dentry `%s'\n",
3621 priv->d_info->name);
3622 continue;
3623 }
3624
3625 ksmbd_kstat.kstat = &kstat;
3626 if (priv->info_level != FILE_NAMES_INFORMATION)
3627 ksmbd_vfs_fill_dentry_attrs(priv->work,
Hyunchul Lee465d7202021-07-03 12:10:36 +09003628 user_ns,
Namjae Jeone2f34482021-03-16 10:49:09 +09003629 dent,
3630 &ksmbd_kstat);
3631
3632 rc = smb2_populate_readdir_entry(priv->work->conn,
3633 priv->info_level,
3634 priv->d_info,
3635 &ksmbd_kstat);
3636 dput(dent);
3637 if (rc)
3638 return rc;
3639 }
3640 return 0;
3641}
3642
3643static int reserve_populate_dentry(struct ksmbd_dir_info *d_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09003644 int info_level)
Namjae Jeone2f34482021-03-16 10:49:09 +09003645{
3646 int struct_sz;
3647 int conv_len;
3648 int next_entry_offset;
3649
3650 struct_sz = readdir_info_level_struct_sz(info_level);
3651 if (struct_sz == -EOPNOTSUPP)
3652 return -EOPNOTSUPP;
3653
3654 conv_len = (d_info->name_len + 1) * 2;
3655 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3656 KSMBD_DIR_INFO_ALIGNMENT);
3657
3658 if (next_entry_offset > d_info->out_buf_len) {
3659 d_info->out_buf_len = 0;
3660 return -ENOSPC;
3661 }
3662
3663 switch (info_level) {
3664 case FILE_FULL_DIRECTORY_INFORMATION:
3665 {
3666 struct file_full_directory_info *ffdinfo;
3667
3668 ffdinfo = (struct file_full_directory_info *)d_info->wptr;
3669 memcpy(ffdinfo->FileName, d_info->name, d_info->name_len);
3670 ffdinfo->FileName[d_info->name_len] = 0x00;
3671 ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3672 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3673 break;
3674 }
3675 case FILE_BOTH_DIRECTORY_INFORMATION:
3676 {
3677 struct file_both_directory_info *fbdinfo;
3678
3679 fbdinfo = (struct file_both_directory_info *)d_info->wptr;
3680 memcpy(fbdinfo->FileName, d_info->name, d_info->name_len);
3681 fbdinfo->FileName[d_info->name_len] = 0x00;
3682 fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3683 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3684 break;
3685 }
3686 case FILE_DIRECTORY_INFORMATION:
3687 {
3688 struct file_directory_info *fdinfo;
3689
3690 fdinfo = (struct file_directory_info *)d_info->wptr;
3691 memcpy(fdinfo->FileName, d_info->name, d_info->name_len);
3692 fdinfo->FileName[d_info->name_len] = 0x00;
3693 fdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3694 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3695 break;
3696 }
3697 case FILE_NAMES_INFORMATION:
3698 {
3699 struct file_names_info *fninfo;
3700
3701 fninfo = (struct file_names_info *)d_info->wptr;
3702 memcpy(fninfo->FileName, d_info->name, d_info->name_len);
3703 fninfo->FileName[d_info->name_len] = 0x00;
3704 fninfo->FileNameLength = cpu_to_le32(d_info->name_len);
3705 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3706 break;
3707 }
3708 case FILEID_FULL_DIRECTORY_INFORMATION:
3709 {
3710 struct file_id_full_dir_info *dinfo;
3711
3712 dinfo = (struct file_id_full_dir_info *)d_info->wptr;
3713 memcpy(dinfo->FileName, d_info->name, d_info->name_len);
3714 dinfo->FileName[d_info->name_len] = 0x00;
3715 dinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3716 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3717 break;
3718 }
3719 case FILEID_BOTH_DIRECTORY_INFORMATION:
3720 {
3721 struct file_id_both_directory_info *fibdinfo;
3722
3723 fibdinfo = (struct file_id_both_directory_info *)d_info->wptr;
3724 memcpy(fibdinfo->FileName, d_info->name, d_info->name_len);
3725 fibdinfo->FileName[d_info->name_len] = 0x00;
3726 fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3727 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3728 break;
3729 }
3730 case SMB_FIND_FILE_POSIX_INFO:
3731 {
3732 struct smb2_posix_info *posix_info;
3733
3734 posix_info = (struct smb2_posix_info *)d_info->wptr;
3735 memcpy(posix_info->name, d_info->name, d_info->name_len);
3736 posix_info->name[d_info->name_len] = 0x00;
3737 posix_info->name_len = cpu_to_le32(d_info->name_len);
3738 posix_info->NextEntryOffset =
3739 cpu_to_le32(next_entry_offset);
3740 break;
3741 }
3742 } /* switch (info_level) */
3743
3744 d_info->num_entry++;
3745 d_info->out_buf_len -= next_entry_offset;
3746 d_info->wptr += next_entry_offset;
3747 return 0;
3748}
3749
Namjae Jeon64b39f42021-03-30 14:25:35 +09003750static int __query_dir(struct dir_context *ctx, const char *name, int namlen,
Namjae Jeon070fb212021-05-26 17:57:12 +09003751 loff_t offset, u64 ino, unsigned int d_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09003752{
3753 struct ksmbd_readdir_data *buf;
3754 struct smb2_query_dir_private *priv;
3755 struct ksmbd_dir_info *d_info;
3756 int rc;
3757
3758 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
3759 priv = buf->private;
3760 d_info = priv->d_info;
3761
3762 /* dot and dotdot entries are already reserved */
3763 if (!strcmp(".", name) || !strcmp("..", name))
3764 return 0;
3765 if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name))
3766 return 0;
Namjae Jeonb24c9332021-03-21 17:32:19 +09003767 if (!match_pattern(name, namlen, priv->search_pattern))
Namjae Jeone2f34482021-03-16 10:49:09 +09003768 return 0;
3769
3770 d_info->name = name;
3771 d_info->name_len = namlen;
3772 rc = reserve_populate_dentry(d_info, priv->info_level);
3773 if (rc)
3774 return rc;
3775 if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
3776 d_info->out_buf_len = 0;
3777 return 0;
3778 }
3779 return 0;
3780}
3781
3782static void restart_ctx(struct dir_context *ctx)
3783{
3784 ctx->pos = 0;
3785}
3786
3787static int verify_info_level(int info_level)
3788{
3789 switch (info_level) {
3790 case FILE_FULL_DIRECTORY_INFORMATION:
3791 case FILE_BOTH_DIRECTORY_INFORMATION:
3792 case FILE_DIRECTORY_INFORMATION:
3793 case FILE_NAMES_INFORMATION:
3794 case FILEID_FULL_DIRECTORY_INFORMATION:
3795 case FILEID_BOTH_DIRECTORY_INFORMATION:
3796 case SMB_FIND_FILE_POSIX_INFO:
3797 break;
3798 default:
3799 return -EOPNOTSUPP;
3800 }
3801
3802 return 0;
3803}
3804
Hyunchul Lee34061d62021-10-16 08:39:54 +09003805static int smb2_calc_max_out_buf_len(struct ksmbd_work *work,
3806 unsigned short hdr2_len,
3807 unsigned int out_buf_len)
3808{
3809 int free_len;
3810
3811 if (out_buf_len > work->conn->vals->max_trans_size)
3812 return -EINVAL;
3813
3814 free_len = (int)(work->response_sz -
3815 (get_rfc1002_len(work->response_buf) + 4)) -
3816 hdr2_len;
3817 if (free_len < 0)
3818 return -EINVAL;
3819
3820 return min_t(int, out_buf_len, free_len);
3821}
3822
Namjae Jeone2f34482021-03-16 10:49:09 +09003823int smb2_query_dir(struct ksmbd_work *work)
3824{
3825 struct ksmbd_conn *conn = work->conn;
3826 struct smb2_query_directory_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09003827 struct smb2_query_directory_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09003828 struct ksmbd_share_config *share = work->tcon->share_conf;
3829 struct ksmbd_file *dir_fp = NULL;
3830 struct ksmbd_dir_info d_info;
3831 int rc = 0;
3832 char *srch_ptr = NULL;
3833 unsigned char srch_flag;
3834 int buffer_sz;
3835 struct smb2_query_dir_private query_dir_private = {NULL, };
3836
Namjae Jeone2f34482021-03-16 10:49:09 +09003837 WORK_BUFFERS(work, req, rsp);
3838
3839 if (ksmbd_override_fsids(work)) {
3840 rsp->hdr.Status = STATUS_NO_MEMORY;
3841 smb2_set_err_rsp(work);
3842 return -ENOMEM;
3843 }
3844
3845 rc = verify_info_level(req->FileInformationClass);
3846 if (rc) {
3847 rc = -EFAULT;
3848 goto err_out2;
3849 }
3850
3851 dir_fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09003852 le64_to_cpu(req->VolatileFileId),
3853 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09003854 if (!dir_fp) {
3855 rc = -EBADF;
3856 goto err_out2;
3857 }
3858
3859 if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) ||
Hyunchul Leeaf349832021-06-30 18:25:53 +09003860 inode_permission(file_mnt_user_ns(dir_fp->filp),
3861 file_inode(dir_fp->filp),
Namjae Jeon070fb212021-05-26 17:57:12 +09003862 MAY_READ | MAY_EXEC)) {
Namjae Jeon493fa2f2021-06-29 09:22:16 +09003863 pr_err("no right to enumerate directory (%pd)\n",
3864 dir_fp->filp->f_path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003865 rc = -EACCES;
3866 goto err_out2;
3867 }
3868
3869 if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09003870 pr_err("can't do query dir for a file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09003871 rc = -EINVAL;
3872 goto err_out2;
3873 }
3874
3875 srch_flag = req->Flags;
3876 srch_ptr = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09003877 le16_to_cpu(req->FileNameLength), 1,
3878 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09003879 if (IS_ERR(srch_ptr)) {
3880 ksmbd_debug(SMB, "Search Pattern not found\n");
3881 rc = -EINVAL;
3882 goto err_out2;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003883 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003884 ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr);
Namjae Jeon64b39f42021-03-30 14:25:35 +09003885 }
Namjae Jeone2f34482021-03-16 10:49:09 +09003886
3887 ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename);
3888
3889 if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) {
3890 ksmbd_debug(SMB, "Restart directory scan\n");
3891 generic_file_llseek(dir_fp->filp, 0, SEEK_SET);
3892 restart_ctx(&dir_fp->readdir_data.ctx);
3893 }
3894
3895 memset(&d_info, 0, sizeof(struct ksmbd_dir_info));
3896 d_info.wptr = (char *)rsp->Buffer;
3897 d_info.rptr = (char *)rsp->Buffer;
Hyunchul Lee34061d62021-10-16 08:39:54 +09003898 d_info.out_buf_len =
3899 smb2_calc_max_out_buf_len(work, 8,
3900 le32_to_cpu(req->OutputBufferLength));
3901 if (d_info.out_buf_len < 0) {
3902 rc = -EINVAL;
3903 goto err_out;
3904 }
Namjae Jeone2f34482021-03-16 10:49:09 +09003905 d_info.flags = srch_flag;
3906
3907 /*
3908 * reserve dot and dotdot entries in head of buffer
3909 * in first response
3910 */
3911 rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass,
Namjae Jeon070fb212021-05-26 17:57:12 +09003912 dir_fp, &d_info, srch_ptr,
3913 smb2_populate_readdir_entry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003914 if (rc == -ENOSPC)
3915 rc = 0;
3916 else if (rc)
3917 goto err_out;
3918
3919 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES))
3920 d_info.hide_dot_file = true;
3921
3922 buffer_sz = d_info.out_buf_len;
3923 d_info.rptr = d_info.wptr;
3924 query_dir_private.work = work;
3925 query_dir_private.search_pattern = srch_ptr;
3926 query_dir_private.dir_fp = dir_fp;
3927 query_dir_private.d_info = &d_info;
3928 query_dir_private.info_level = req->FileInformationClass;
3929 dir_fp->readdir_data.private = &query_dir_private;
3930 set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir);
3931
Namjae Jeone8c06192021-06-22 11:06:11 +09003932 rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx);
Namjae Jeone2f34482021-03-16 10:49:09 +09003933 if (rc == 0)
3934 restart_ctx(&dir_fp->readdir_data.ctx);
3935 if (rc == -ENOSPC)
3936 rc = 0;
3937 if (rc)
3938 goto err_out;
3939
3940 d_info.wptr = d_info.rptr;
3941 d_info.out_buf_len = buffer_sz;
3942 rc = process_query_dir_entries(&query_dir_private);
3943 if (rc)
3944 goto err_out;
3945
3946 if (!d_info.data_count && d_info.out_buf_len >= 0) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09003947 if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003948 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003949 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003950 dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0;
3951 rsp->hdr.Status = STATUS_NO_MORE_FILES;
3952 }
3953 rsp->StructureSize = cpu_to_le16(9);
3954 rsp->OutputBufferOffset = cpu_to_le16(0);
3955 rsp->OutputBufferLength = cpu_to_le32(0);
3956 rsp->Buffer[0] = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09003957 inc_rfc1001_len(work->response_buf, 9);
Namjae Jeone2f34482021-03-16 10:49:09 +09003958 } else {
3959 ((struct file_directory_info *)
3960 ((char *)rsp->Buffer + d_info.last_entry_offset))
3961 ->NextEntryOffset = 0;
3962
3963 rsp->StructureSize = cpu_to_le16(9);
3964 rsp->OutputBufferOffset = cpu_to_le16(72);
3965 rsp->OutputBufferLength = cpu_to_le32(d_info.data_count);
Namjae Jeoncb451722021-11-03 08:08:44 +09003966 inc_rfc1001_len(work->response_buf, 8 + d_info.data_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09003967 }
3968
3969 kfree(srch_ptr);
3970 ksmbd_fd_put(work, dir_fp);
3971 ksmbd_revert_fsids(work);
3972 return 0;
3973
3974err_out:
Namjae Jeonbde16942021-06-28 15:23:19 +09003975 pr_err("error while processing smb2 query dir rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09003976 kfree(srch_ptr);
3977
3978err_out2:
3979 if (rc == -EINVAL)
3980 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3981 else if (rc == -EACCES)
3982 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3983 else if (rc == -ENOENT)
3984 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
3985 else if (rc == -EBADF)
3986 rsp->hdr.Status = STATUS_FILE_CLOSED;
3987 else if (rc == -ENOMEM)
3988 rsp->hdr.Status = STATUS_NO_MEMORY;
3989 else if (rc == -EFAULT)
3990 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
3991 if (!rsp->hdr.Status)
3992 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3993
3994 smb2_set_err_rsp(work);
3995 ksmbd_fd_put(work, dir_fp);
3996 ksmbd_revert_fsids(work);
3997 return 0;
3998}
3999
4000/**
4001 * buffer_check_err() - helper function to check buffer errors
4002 * @reqOutputBufferLength: max buffer length expected in command response
4003 * @rsp: query info response buffer contains output buffer length
4004 * @infoclass_size: query info class response buffer size
4005 *
4006 * Return: 0 on success, otherwise error
4007 */
4008static int buffer_check_err(int reqOutputBufferLength,
Namjae Jeoncb451722021-11-03 08:08:44 +09004009 struct smb2_query_info_rsp *rsp,
4010 void *rsp_org, int infoclass_size)
Namjae Jeone2f34482021-03-16 10:49:09 +09004011{
4012 if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) {
4013 if (reqOutputBufferLength < infoclass_size) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004014 pr_err("Invalid Buffer Size Requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004015 rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH;
Namjae Jeoncb451722021-11-03 08:08:44 +09004016 *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr));
Namjae Jeone2f34482021-03-16 10:49:09 +09004017 return -EINVAL;
4018 }
4019
4020 ksmbd_debug(SMB, "Buffer Overflow\n");
4021 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
Namjae Jeoncb451722021-11-03 08:08:44 +09004022 *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09004023 reqOutputBufferLength);
4024 rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09004025 }
4026 return 0;
4027}
4028
Namjae Jeoncb451722021-11-03 08:08:44 +09004029static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp,
4030 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004031{
4032 struct smb2_file_standard_info *sinfo;
4033
4034 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4035
4036 sinfo->AllocationSize = cpu_to_le64(4096);
4037 sinfo->EndOfFile = cpu_to_le64(0);
4038 sinfo->NumberOfLinks = cpu_to_le32(1);
4039 sinfo->DeletePending = 1;
4040 sinfo->Directory = 0;
4041 rsp->OutputBufferLength =
4042 cpu_to_le32(sizeof(struct smb2_file_standard_info));
Namjae Jeoncb451722021-11-03 08:08:44 +09004043 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_standard_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004044}
4045
Namjae Jeoncb451722021-11-03 08:08:44 +09004046static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num,
4047 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004048{
4049 struct smb2_file_internal_info *file_info;
4050
4051 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4052
4053 /* any unique number */
4054 file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63));
4055 rsp->OutputBufferLength =
4056 cpu_to_le32(sizeof(struct smb2_file_internal_info));
Namjae Jeoncb451722021-11-03 08:08:44 +09004057 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004058}
4059
Namjae Jeone2f34482021-03-16 10:49:09 +09004060static int smb2_get_info_file_pipe(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09004061 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004062 struct smb2_query_info_rsp *rsp,
4063 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004064{
Namjae Jeon64b39f42021-03-30 14:25:35 +09004065 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09004066 int rc;
4067
4068 /*
4069 * Windows can sometime send query file info request on
4070 * pipe without opening it, checking error condition here
4071 */
4072 id = le64_to_cpu(req->VolatileFileId);
4073 if (!ksmbd_session_rpc_method(sess, id))
4074 return -ENOENT;
4075
4076 ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004077 req->FileInfoClass, le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09004078
4079 switch (req->FileInfoClass) {
4080 case FILE_STANDARD_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004081 get_standard_info_pipe(rsp, rsp_org);
Namjae Jeone2f34482021-03-16 10:49:09 +09004082 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004083 rsp, rsp_org,
4084 FILE_STANDARD_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09004085 break;
4086 case FILE_INTERNAL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004087 get_internal_info_pipe(rsp, id, rsp_org);
Namjae Jeone2f34482021-03-16 10:49:09 +09004088 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004089 rsp, rsp_org,
4090 FILE_INTERNAL_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09004091 break;
4092 default:
4093 ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004094 req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09004095 rc = -EOPNOTSUPP;
4096 }
4097 return rc;
4098}
4099
4100/**
4101 * smb2_get_ea() - handler for smb2 get extended attribute command
4102 * @work: smb work containing query info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09004103 * @fp: ksmbd_file pointer
4104 * @req: get extended attribute request
4105 * @rsp: response buffer pointer
4106 * @rsp_org: base response buffer pointer in case of chained response
Namjae Jeone2f34482021-03-16 10:49:09 +09004107 *
4108 * Return: 0 on success, otherwise error
4109 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09004110static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004111 struct smb2_query_info_req *req,
4112 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004113{
4114 struct smb2_ea_info *eainfo, *prev_eainfo;
4115 char *name, *ptr, *xattr_list = NULL, *buf;
4116 int rc, name_len, value_len, xattr_list_len, idx;
4117 ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0;
4118 struct smb2_ea_info_req *ea_req = NULL;
4119 struct path *path;
Hyunchul Lee465d7202021-07-03 12:10:36 +09004120 struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09004121
4122 if (!(fp->daccess & FILE_READ_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004123 pr_err("Not permitted to read ext attr : 0x%x\n",
4124 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004125 return -EACCES;
4126 }
4127
4128 path = &fp->filp->f_path;
4129 /* single EA entry is requested with given user.* name */
Namjae Jeon64b39f42021-03-30 14:25:35 +09004130 if (req->InputBufferLength) {
Namjae Jeon6d562622021-09-18 18:45:12 +09004131 if (le32_to_cpu(req->InputBufferLength) <
4132 sizeof(struct smb2_ea_info_req))
4133 return -EINVAL;
4134
Namjae Jeone2f34482021-03-16 10:49:09 +09004135 ea_req = (struct smb2_ea_info_req *)req->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004136 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09004137 /* need to send all EAs, if no specific EA is requested*/
4138 if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY)
4139 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09004140 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n",
4141 le32_to_cpu(req->Flags));
Namjae Jeone2f34482021-03-16 10:49:09 +09004142 }
4143
Hyunchul Lee34061d62021-10-16 08:39:54 +09004144 buf_free_len =
4145 smb2_calc_max_out_buf_len(work, 8,
4146 le32_to_cpu(req->OutputBufferLength));
4147 if (buf_free_len < 0)
4148 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09004149
4150 rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4151 if (rc < 0) {
4152 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4153 goto out;
4154 } else if (!rc) { /* there is no EA in the file */
4155 ksmbd_debug(SMB, "no ea data in the file\n");
4156 goto done;
4157 }
4158 xattr_list_len = rc;
4159
4160 ptr = (char *)rsp->Buffer;
4161 eainfo = (struct smb2_ea_info *)ptr;
4162 prev_eainfo = eainfo;
4163 idx = 0;
4164
4165 while (idx < xattr_list_len) {
4166 name = xattr_list + idx;
4167 name_len = strlen(name);
4168
4169 ksmbd_debug(SMB, "%s, len %d\n", name, name_len);
4170 idx += name_len + 1;
4171
4172 /*
4173 * CIFS does not support EA other than user.* namespace,
4174 * still keep the framework generic, to list other attrs
4175 * in future.
4176 */
4177 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4178 continue;
4179
4180 if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004181 STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004182 continue;
4183
4184 if (req->InputBufferLength &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09004185 strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name,
4186 ea_req->EaNameLength))
Namjae Jeone2f34482021-03-16 10:49:09 +09004187 continue;
4188
4189 if (!strncmp(&name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004190 DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004191 continue;
4192
4193 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4194 name_len -= XATTR_USER_PREFIX_LEN;
4195
4196 ptr = (char *)(&eainfo->name + name_len + 1);
4197 buf_free_len -= (offsetof(struct smb2_ea_info, name) +
4198 name_len + 1);
4199 /* bailout if xattr can't fit in buf_free_len */
Hyunchul Lee465d7202021-07-03 12:10:36 +09004200 value_len = ksmbd_vfs_getxattr(user_ns, path->dentry,
4201 name, &buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004202 if (value_len <= 0) {
4203 rc = -ENOENT;
4204 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4205 goto out;
4206 }
4207
4208 buf_free_len -= value_len;
4209 if (buf_free_len < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09004210 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004211 break;
4212 }
4213
4214 memcpy(ptr, buf, value_len);
Namjae Jeon79f6b112021-04-02 12:47:14 +09004215 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004216
4217 ptr += value_len;
4218 eainfo->Flags = 0;
4219 eainfo->EaNameLength = name_len;
4220
Namjae Jeon64b39f42021-03-30 14:25:35 +09004221 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004222 memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN],
Namjae Jeon070fb212021-05-26 17:57:12 +09004223 name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09004224 else
4225 memcpy(eainfo->name, name, name_len);
4226
4227 eainfo->name[name_len] = '\0';
4228 eainfo->EaValueLength = cpu_to_le16(value_len);
4229 next_offset = offsetof(struct smb2_ea_info, name) +
4230 name_len + 1 + value_len;
4231
4232 /* align next xattr entry at 4 byte bundary */
4233 alignment_bytes = ((next_offset + 3) & ~3) - next_offset;
4234 if (alignment_bytes) {
4235 memset(ptr, '\0', alignment_bytes);
4236 ptr += alignment_bytes;
4237 next_offset += alignment_bytes;
4238 buf_free_len -= alignment_bytes;
4239 }
4240 eainfo->NextEntryOffset = cpu_to_le32(next_offset);
4241 prev_eainfo = eainfo;
4242 eainfo = (struct smb2_ea_info *)ptr;
4243 rsp_data_cnt += next_offset;
4244
4245 if (req->InputBufferLength) {
4246 ksmbd_debug(SMB, "single entry requested\n");
4247 break;
4248 }
4249 }
4250
4251 /* no more ea entries */
4252 prev_eainfo->NextEntryOffset = 0;
4253done:
4254 rc = 0;
4255 if (rsp_data_cnt == 0)
4256 rsp->hdr.Status = STATUS_NO_EAS_ON_FILE;
4257 rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt);
4258 inc_rfc1001_len(rsp_org, rsp_data_cnt);
4259out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004260 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004261 return rc;
4262}
4263
4264static void get_file_access_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004265 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004266{
4267 struct smb2_file_access_info *file_info;
4268
4269 file_info = (struct smb2_file_access_info *)rsp->Buffer;
4270 file_info->AccessFlags = fp->daccess;
4271 rsp->OutputBufferLength =
4272 cpu_to_le32(sizeof(struct smb2_file_access_info));
4273 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info));
4274}
4275
4276static int get_file_basic_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004277 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004278{
Namjae Jeon88d30052021-09-29 15:37:18 +09004279 struct smb2_file_basic_info *basic_info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004280 struct kstat stat;
4281 u64 time;
4282
4283 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004284 pr_err("no right to read the attributes : 0x%x\n",
4285 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004286 return -EACCES;
4287 }
4288
Namjae Jeon88d30052021-09-29 15:37:18 +09004289 basic_info = (struct smb2_file_basic_info *)rsp->Buffer;
Hyunchul Leeaf349832021-06-30 18:25:53 +09004290 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4291 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004292 basic_info->CreationTime = cpu_to_le64(fp->create_time);
4293 time = ksmbd_UnixTimeToNT(stat.atime);
4294 basic_info->LastAccessTime = cpu_to_le64(time);
4295 time = ksmbd_UnixTimeToNT(stat.mtime);
4296 basic_info->LastWriteTime = cpu_to_le64(time);
4297 time = ksmbd_UnixTimeToNT(stat.ctime);
4298 basic_info->ChangeTime = cpu_to_le64(time);
4299 basic_info->Attributes = fp->f_ci->m_fattr;
4300 basic_info->Pad1 = 0;
4301 rsp->OutputBufferLength =
Namjae Jeon88d30052021-09-29 15:37:18 +09004302 cpu_to_le32(sizeof(struct smb2_file_basic_info));
4303 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_basic_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004304 return 0;
4305}
4306
4307static unsigned long long get_allocation_size(struct inode *inode,
Namjae Jeon070fb212021-05-26 17:57:12 +09004308 struct kstat *stat)
Namjae Jeone2f34482021-03-16 10:49:09 +09004309{
4310 unsigned long long alloc_size = 0;
4311
4312 if (!S_ISDIR(stat->mode)) {
4313 if ((inode->i_blocks << 9) <= stat->size)
4314 alloc_size = stat->size;
4315 else
4316 alloc_size = inode->i_blocks << 9;
Namjae Jeone2f34482021-03-16 10:49:09 +09004317 }
4318
4319 return alloc_size;
4320}
4321
4322static void get_file_standard_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004323 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004324{
4325 struct smb2_file_standard_info *sinfo;
4326 unsigned int delete_pending;
4327 struct inode *inode;
4328 struct kstat stat;
4329
Namjae Jeonab0b2632021-06-29 09:20:13 +09004330 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004331 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004332
4333 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4334 delete_pending = ksmbd_inode_pending_delete(fp);
4335
4336 sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat));
4337 sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4338 sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending);
4339 sinfo->DeletePending = delete_pending;
4340 sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4341 rsp->OutputBufferLength =
4342 cpu_to_le32(sizeof(struct smb2_file_standard_info));
4343 inc_rfc1001_len(rsp_org,
4344 sizeof(struct smb2_file_standard_info));
4345}
4346
4347static void get_file_alignment_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004348 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004349{
4350 struct smb2_file_alignment_info *file_info;
4351
4352 file_info = (struct smb2_file_alignment_info *)rsp->Buffer;
4353 file_info->AlignmentRequirement = 0;
4354 rsp->OutputBufferLength =
4355 cpu_to_le32(sizeof(struct smb2_file_alignment_info));
4356 inc_rfc1001_len(rsp_org,
4357 sizeof(struct smb2_file_alignment_info));
4358}
4359
4360static int get_file_all_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004361 struct smb2_query_info_rsp *rsp,
4362 struct ksmbd_file *fp,
4363 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004364{
4365 struct ksmbd_conn *conn = work->conn;
4366 struct smb2_file_all_info *file_info;
4367 unsigned int delete_pending;
4368 struct inode *inode;
4369 struct kstat stat;
4370 int conv_len;
4371 char *filename;
4372 u64 time;
4373
4374 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4375 ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004376 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004377 return -EACCES;
4378 }
4379
Hyunchul Lee265fd192021-09-25 00:06:16 +09004380 filename = convert_to_nt_pathname(fp->filename);
Namjae Jeone2f34482021-03-16 10:49:09 +09004381 if (!filename)
4382 return -ENOMEM;
4383
Namjae Jeonab0b2632021-06-29 09:20:13 +09004384 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004385 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004386
4387 ksmbd_debug(SMB, "filename = %s\n", filename);
4388 delete_pending = ksmbd_inode_pending_delete(fp);
4389 file_info = (struct smb2_file_all_info *)rsp->Buffer;
4390
4391 file_info->CreationTime = cpu_to_le64(fp->create_time);
4392 time = ksmbd_UnixTimeToNT(stat.atime);
4393 file_info->LastAccessTime = cpu_to_le64(time);
4394 time = ksmbd_UnixTimeToNT(stat.mtime);
4395 file_info->LastWriteTime = cpu_to_le64(time);
4396 time = ksmbd_UnixTimeToNT(stat.ctime);
4397 file_info->ChangeTime = cpu_to_le64(time);
4398 file_info->Attributes = fp->f_ci->m_fattr;
4399 file_info->Pad1 = 0;
4400 file_info->AllocationSize =
4401 cpu_to_le64(get_allocation_size(inode, &stat));
4402 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4403 file_info->NumberOfLinks =
4404 cpu_to_le32(get_nlink(&stat) - delete_pending);
4405 file_info->DeletePending = delete_pending;
4406 file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4407 file_info->Pad2 = 0;
4408 file_info->IndexNumber = cpu_to_le64(stat.ino);
4409 file_info->EASize = 0;
4410 file_info->AccessFlags = fp->daccess;
4411 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4412 file_info->Mode = fp->coption;
4413 file_info->AlignmentRequirement = 0;
Namjae Jeon070fb212021-05-26 17:57:12 +09004414 conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename,
4415 PATH_MAX, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004416 conv_len *= 2;
4417 file_info->FileNameLength = cpu_to_le32(conv_len);
4418 rsp->OutputBufferLength =
4419 cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1);
4420 kfree(filename);
4421 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4422 return 0;
4423}
4424
4425static void get_file_alternate_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004426 struct smb2_query_info_rsp *rsp,
4427 struct ksmbd_file *fp,
4428 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004429{
4430 struct ksmbd_conn *conn = work->conn;
4431 struct smb2_file_alt_name_info *file_info;
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004432 struct dentry *dentry = fp->filp->f_path.dentry;
Namjae Jeone2f34482021-03-16 10:49:09 +09004433 int conv_len;
Namjae Jeone2f34482021-03-16 10:49:09 +09004434
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004435 spin_lock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004436 file_info = (struct smb2_file_alt_name_info *)rsp->Buffer;
4437 conv_len = ksmbd_extract_shortname(conn,
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004438 dentry->d_name.name,
Namjae Jeone2f34482021-03-16 10:49:09 +09004439 file_info->FileName);
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004440 spin_unlock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004441 file_info->FileNameLength = cpu_to_le32(conv_len);
4442 rsp->OutputBufferLength =
4443 cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len);
4444 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4445}
4446
4447static void get_file_stream_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004448 struct smb2_query_info_rsp *rsp,
4449 struct ksmbd_file *fp,
4450 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004451{
4452 struct ksmbd_conn *conn = work->conn;
4453 struct smb2_file_stream_info *file_info;
4454 char *stream_name, *xattr_list = NULL, *stream_buf;
4455 struct kstat stat;
4456 struct path *path = &fp->filp->f_path;
4457 ssize_t xattr_list_len;
4458 int nbytes = 0, streamlen, stream_name_len, next, idx = 0;
Hyunchul Lee34061d62021-10-16 08:39:54 +09004459 int buf_free_len;
4460 struct smb2_query_info_req *req = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09004461
Hyunchul Leeaf349832021-06-30 18:25:53 +09004462 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4463 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004464 file_info = (struct smb2_file_stream_info *)rsp->Buffer;
4465
Namjae Jeon1ec721532021-11-21 11:32:39 +09004466 buf_free_len =
4467 smb2_calc_max_out_buf_len(work, 8,
4468 le32_to_cpu(req->OutputBufferLength));
4469 if (buf_free_len < 0)
4470 goto out;
4471
Namjae Jeone2f34482021-03-16 10:49:09 +09004472 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4473 if (xattr_list_len < 0) {
4474 goto out;
4475 } else if (!xattr_list_len) {
4476 ksmbd_debug(SMB, "empty xattr in the file\n");
4477 goto out;
4478 }
4479
4480 while (idx < xattr_list_len) {
4481 stream_name = xattr_list + idx;
4482 streamlen = strlen(stream_name);
4483 idx += streamlen + 1;
4484
4485 ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen);
4486
4487 if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004488 STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004489 continue;
4490
4491 stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN +
4492 STREAM_PREFIX_LEN);
4493 streamlen = stream_name_len;
4494
4495 /* plus : size */
4496 streamlen += 1;
4497 stream_buf = kmalloc(streamlen + 1, GFP_KERNEL);
4498 if (!stream_buf)
4499 break;
4500
4501 streamlen = snprintf(stream_buf, streamlen + 1,
Namjae Jeon070fb212021-05-26 17:57:12 +09004502 ":%s", &stream_name[XATTR_NAME_STREAM_LEN]);
Namjae Jeone2f34482021-03-16 10:49:09 +09004503
Hyunchul Lee34061d62021-10-16 08:39:54 +09004504 next = sizeof(struct smb2_file_stream_info) + streamlen * 2;
Namjae Jeon178ca6f2021-11-24 10:23:02 +09004505 if (next > buf_free_len) {
4506 kfree(stream_buf);
Hyunchul Lee34061d62021-10-16 08:39:54 +09004507 break;
Namjae Jeon178ca6f2021-11-24 10:23:02 +09004508 }
Hyunchul Lee34061d62021-10-16 08:39:54 +09004509
Namjae Jeon070fb212021-05-26 17:57:12 +09004510 file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes];
Namjae Jeone2f34482021-03-16 10:49:09 +09004511 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004512 stream_buf, streamlen,
4513 conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004514 streamlen *= 2;
4515 kfree(stream_buf);
4516 file_info->StreamNameLength = cpu_to_le32(streamlen);
4517 file_info->StreamSize = cpu_to_le64(stream_name_len);
4518 file_info->StreamAllocationSize = cpu_to_le64(stream_name_len);
4519
Namjae Jeone2f34482021-03-16 10:49:09 +09004520 nbytes += next;
Hyunchul Lee34061d62021-10-16 08:39:54 +09004521 buf_free_len -= next;
Namjae Jeone2f34482021-03-16 10:49:09 +09004522 file_info->NextEntryOffset = cpu_to_le32(next);
4523 }
4524
Namjae Jeon1ec721532021-11-21 11:32:39 +09004525out:
Hyunchul Lee34061d62021-10-16 08:39:54 +09004526 if (!S_ISDIR(stat.mode) &&
4527 buf_free_len >= sizeof(struct smb2_file_stream_info) + 7 * 2) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004528 file_info = (struct smb2_file_stream_info *)
4529 &rsp->Buffer[nbytes];
4530 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004531 "::$DATA", 7, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004532 streamlen *= 2;
4533 file_info->StreamNameLength = cpu_to_le32(streamlen);
Namjae Jeon1ec721532021-11-21 11:32:39 +09004534 file_info->StreamSize = cpu_to_le64(stat.size);
4535 file_info->StreamAllocationSize = cpu_to_le64(stat.blocks << 9);
Namjae Jeone2f34482021-03-16 10:49:09 +09004536 nbytes += sizeof(struct smb2_file_stream_info) + streamlen;
4537 }
4538
4539 /* last entry offset should be 0 */
4540 file_info->NextEntryOffset = 0;
Namjae Jeon79f6b112021-04-02 12:47:14 +09004541 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004542
4543 rsp->OutputBufferLength = cpu_to_le32(nbytes);
4544 inc_rfc1001_len(rsp_org, nbytes);
4545}
4546
4547static void get_file_internal_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004548 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004549{
4550 struct smb2_file_internal_info *file_info;
4551 struct kstat stat;
4552
Hyunchul Leeaf349832021-06-30 18:25:53 +09004553 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4554 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004555 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4556 file_info->IndexNumber = cpu_to_le64(stat.ino);
4557 rsp->OutputBufferLength =
4558 cpu_to_le32(sizeof(struct smb2_file_internal_info));
4559 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
4560}
4561
4562static int get_file_network_open_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004563 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004564{
4565 struct smb2_file_ntwrk_info *file_info;
4566 struct inode *inode;
4567 struct kstat stat;
4568 u64 time;
4569
4570 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004571 pr_err("no right to read the attributes : 0x%x\n",
4572 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004573 return -EACCES;
4574 }
4575
4576 file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer;
4577
Namjae Jeonab0b2632021-06-29 09:20:13 +09004578 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004579 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004580
4581 file_info->CreationTime = cpu_to_le64(fp->create_time);
4582 time = ksmbd_UnixTimeToNT(stat.atime);
4583 file_info->LastAccessTime = cpu_to_le64(time);
4584 time = ksmbd_UnixTimeToNT(stat.mtime);
4585 file_info->LastWriteTime = cpu_to_le64(time);
4586 time = ksmbd_UnixTimeToNT(stat.ctime);
4587 file_info->ChangeTime = cpu_to_le64(time);
4588 file_info->Attributes = fp->f_ci->m_fattr;
4589 file_info->AllocationSize =
4590 cpu_to_le64(get_allocation_size(inode, &stat));
4591 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4592 file_info->Reserved = cpu_to_le32(0);
4593 rsp->OutputBufferLength =
4594 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info));
4595 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info));
4596 return 0;
4597}
4598
Namjae Jeon64b39f42021-03-30 14:25:35 +09004599static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004600{
4601 struct smb2_file_ea_info *file_info;
4602
4603 file_info = (struct smb2_file_ea_info *)rsp->Buffer;
4604 file_info->EASize = 0;
4605 rsp->OutputBufferLength =
4606 cpu_to_le32(sizeof(struct smb2_file_ea_info));
4607 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info));
4608}
4609
4610static void get_file_position_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004611 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004612{
4613 struct smb2_file_pos_info *file_info;
4614
4615 file_info = (struct smb2_file_pos_info *)rsp->Buffer;
4616 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4617 rsp->OutputBufferLength =
4618 cpu_to_le32(sizeof(struct smb2_file_pos_info));
4619 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info));
4620}
4621
4622static void get_file_mode_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004623 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004624{
4625 struct smb2_file_mode_info *file_info;
4626
4627 file_info = (struct smb2_file_mode_info *)rsp->Buffer;
4628 file_info->Mode = fp->coption & FILE_MODE_INFO_MASK;
4629 rsp->OutputBufferLength =
4630 cpu_to_le32(sizeof(struct smb2_file_mode_info));
4631 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info));
4632}
4633
4634static void get_file_compression_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004635 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004636{
4637 struct smb2_file_comp_info *file_info;
4638 struct kstat stat;
4639
Hyunchul Leeaf349832021-06-30 18:25:53 +09004640 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4641 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004642
4643 file_info = (struct smb2_file_comp_info *)rsp->Buffer;
4644 file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9);
4645 file_info->CompressionFormat = COMPRESSION_FORMAT_NONE;
4646 file_info->CompressionUnitShift = 0;
4647 file_info->ChunkShift = 0;
4648 file_info->ClusterShift = 0;
4649 memset(&file_info->Reserved[0], 0, 3);
4650
4651 rsp->OutputBufferLength =
4652 cpu_to_le32(sizeof(struct smb2_file_comp_info));
4653 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info));
4654}
4655
4656static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004657 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004658{
4659 struct smb2_file_attr_tag_info *file_info;
4660
4661 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004662 pr_err("no right to read the attributes : 0x%x\n",
4663 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004664 return -EACCES;
4665 }
4666
4667 file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer;
4668 file_info->FileAttributes = fp->f_ci->m_fattr;
4669 file_info->ReparseTag = 0;
4670 rsp->OutputBufferLength =
4671 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info));
Namjae Jeon070fb212021-05-26 17:57:12 +09004672 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004673 return 0;
4674}
4675
4676static int find_file_posix_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004677 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004678{
4679 struct smb311_posix_qinfo *file_info;
Namjae Jeonab0b2632021-06-29 09:20:13 +09004680 struct inode *inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09004681 u64 time;
4682
4683 file_info = (struct smb311_posix_qinfo *)rsp->Buffer;
4684 file_info->CreationTime = cpu_to_le64(fp->create_time);
4685 time = ksmbd_UnixTimeToNT(inode->i_atime);
4686 file_info->LastAccessTime = cpu_to_le64(time);
4687 time = ksmbd_UnixTimeToNT(inode->i_mtime);
4688 file_info->LastWriteTime = cpu_to_le64(time);
4689 time = ksmbd_UnixTimeToNT(inode->i_ctime);
4690 file_info->ChangeTime = cpu_to_le64(time);
4691 file_info->DosAttributes = fp->f_ci->m_fattr;
4692 file_info->Inode = cpu_to_le64(inode->i_ino);
4693 file_info->EndOfFile = cpu_to_le64(inode->i_size);
4694 file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9);
4695 file_info->HardLinks = cpu_to_le32(inode->i_nlink);
4696 file_info->Mode = cpu_to_le32(inode->i_mode);
4697 file_info->DeviceId = cpu_to_le32(inode->i_rdev);
4698 rsp->OutputBufferLength =
4699 cpu_to_le32(sizeof(struct smb311_posix_qinfo));
Namjae Jeon64b39f42021-03-30 14:25:35 +09004700 inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo));
Namjae Jeone2f34482021-03-16 10:49:09 +09004701 return 0;
4702}
4703
Namjae Jeone2f34482021-03-16 10:49:09 +09004704static int smb2_get_info_file(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004705 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004706 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09004707{
4708 struct ksmbd_file *fp;
4709 int fileinfoclass = 0;
4710 int rc = 0;
4711 int file_infoclass_size;
4712 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4713
4714 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004715 KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004716 /* smb2 info file called for pipe */
Namjae Jeoncb451722021-11-03 08:08:44 +09004717 return smb2_get_info_file_pipe(work->sess, req, rsp,
4718 work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004719 }
4720
4721 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon38673692021-07-08 12:32:27 +09004722 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
4723 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004724 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004725 id = work->compound_fid;
4726 pid = work->compound_pfid;
4727 }
4728 }
4729
Namjae Jeon38673692021-07-08 12:32:27 +09004730 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004731 id = le64_to_cpu(req->VolatileFileId);
4732 pid = le64_to_cpu(req->PersistentFileId);
4733 }
4734
4735 fp = ksmbd_lookup_fd_slow(work, id, pid);
4736 if (!fp)
4737 return -ENOENT;
4738
4739 fileinfoclass = req->FileInfoClass;
4740
4741 switch (fileinfoclass) {
4742 case FILE_ACCESS_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004743 get_file_access_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004744 file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE;
4745 break;
4746
4747 case FILE_BASIC_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004748 rc = get_file_basic_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004749 file_infoclass_size = FILE_BASIC_INFORMATION_SIZE;
4750 break;
4751
4752 case FILE_STANDARD_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004753 get_file_standard_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004754 file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE;
4755 break;
4756
4757 case FILE_ALIGNMENT_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004758 get_file_alignment_info(rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004759 file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE;
4760 break;
4761
4762 case FILE_ALL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004763 rc = get_file_all_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004764 file_infoclass_size = FILE_ALL_INFORMATION_SIZE;
4765 break;
4766
4767 case FILE_ALTERNATE_NAME_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004768 get_file_alternate_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004769 file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE;
4770 break;
4771
4772 case FILE_STREAM_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004773 get_file_stream_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004774 file_infoclass_size = FILE_STREAM_INFORMATION_SIZE;
4775 break;
4776
4777 case FILE_INTERNAL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004778 get_file_internal_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004779 file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE;
4780 break;
4781
4782 case FILE_NETWORK_OPEN_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004783 rc = get_file_network_open_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004784 file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE;
4785 break;
4786
4787 case FILE_EA_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004788 get_file_ea_info(rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004789 file_infoclass_size = FILE_EA_INFORMATION_SIZE;
4790 break;
4791
4792 case FILE_FULL_EA_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004793 rc = smb2_get_ea(work, fp, req, rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004794 file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE;
4795 break;
4796
4797 case FILE_POSITION_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004798 get_file_position_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004799 file_infoclass_size = FILE_POSITION_INFORMATION_SIZE;
4800 break;
4801
4802 case FILE_MODE_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004803 get_file_mode_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004804 file_infoclass_size = FILE_MODE_INFORMATION_SIZE;
4805 break;
4806
4807 case FILE_COMPRESSION_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004808 get_file_compression_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004809 file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE;
4810 break;
4811
4812 case FILE_ATTRIBUTE_TAG_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004813 rc = get_file_attribute_tag_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004814 file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE;
4815 break;
4816 case SMB_FIND_FILE_POSIX_INFO:
4817 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004818 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004819 rc = -EOPNOTSUPP;
4820 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09004821 rc = find_file_posix_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004822 file_infoclass_size = sizeof(struct smb311_posix_qinfo);
4823 }
4824 break;
4825 default:
4826 ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n",
4827 fileinfoclass);
4828 rc = -EOPNOTSUPP;
4829 }
4830 if (!rc)
4831 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004832 rsp, work->response_buf,
Namjae Jeone2f34482021-03-16 10:49:09 +09004833 file_infoclass_size);
4834 ksmbd_fd_put(work, fp);
4835 return rc;
4836}
4837
Namjae Jeone2f34482021-03-16 10:49:09 +09004838static int smb2_get_info_filesystem(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004839 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004840 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09004841{
4842 struct ksmbd_session *sess = work->sess;
4843 struct ksmbd_conn *conn = sess->conn;
4844 struct ksmbd_share_config *share = work->tcon->share_conf;
4845 int fsinfoclass = 0;
4846 struct kstatfs stfs;
4847 struct path path;
4848 int rc = 0, len;
4849 int fs_infoclass_size = 0;
4850
Hyunchul Lee265fd192021-09-25 00:06:16 +09004851 rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09004852 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004853 pr_err("cannot create vfs path\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004854 return -EIO;
4855 }
4856
4857 rc = vfs_statfs(&path, &stfs);
4858 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004859 pr_err("cannot do stat of path %s\n", share->path);
Namjae Jeone2f34482021-03-16 10:49:09 +09004860 path_put(&path);
4861 return -EIO;
4862 }
4863
4864 fsinfoclass = req->FileInfoClass;
4865
4866 switch (fsinfoclass) {
4867 case FS_DEVICE_INFORMATION:
4868 {
4869 struct filesystem_device_info *info;
4870
4871 info = (struct filesystem_device_info *)rsp->Buffer;
4872
4873 info->DeviceType = cpu_to_le32(stfs.f_type);
4874 info->DeviceCharacteristics = cpu_to_le32(0x00000020);
4875 rsp->OutputBufferLength = cpu_to_le32(8);
Namjae Jeoncb451722021-11-03 08:08:44 +09004876 inc_rfc1001_len(work->response_buf, 8);
Namjae Jeone2f34482021-03-16 10:49:09 +09004877 fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE;
4878 break;
4879 }
4880 case FS_ATTRIBUTE_INFORMATION:
4881 {
4882 struct filesystem_attribute_info *info;
4883 size_t sz;
4884
4885 info = (struct filesystem_attribute_info *)rsp->Buffer;
4886 info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS |
4887 FILE_PERSISTENT_ACLS |
4888 FILE_UNICODE_ON_DISK |
4889 FILE_CASE_PRESERVED_NAMES |
Namjae Jeoneb817362021-05-18 10:37:59 +09004890 FILE_CASE_SENSITIVE_SEARCH |
4891 FILE_SUPPORTS_BLOCK_REFCOUNTING);
Namjae Jeone2f34482021-03-16 10:49:09 +09004892
4893 info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps);
4894
4895 info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen);
4896 len = smbConvertToUTF16((__le16 *)info->FileSystemName,
4897 "NTFS", PATH_MAX, conn->local_nls, 0);
4898 len = len * 2;
4899 info->FileSystemNameLen = cpu_to_le32(len);
4900 sz = sizeof(struct filesystem_attribute_info) - 2 + len;
4901 rsp->OutputBufferLength = cpu_to_le32(sz);
Namjae Jeoncb451722021-11-03 08:08:44 +09004902 inc_rfc1001_len(work->response_buf, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09004903 fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE;
4904 break;
4905 }
4906 case FS_VOLUME_INFORMATION:
4907 {
4908 struct filesystem_vol_info *info;
4909 size_t sz;
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004910 unsigned int serial_crc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09004911
4912 info = (struct filesystem_vol_info *)(rsp->Buffer);
4913 info->VolumeCreationTime = 0;
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004914 serial_crc = crc32_le(serial_crc, share->name,
4915 strlen(share->name));
4916 serial_crc = crc32_le(serial_crc, share->path,
4917 strlen(share->path));
4918 serial_crc = crc32_le(serial_crc, ksmbd_netbios_name(),
4919 strlen(ksmbd_netbios_name()));
Namjae Jeone2f34482021-03-16 10:49:09 +09004920 /* Taking dummy value of serial number*/
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004921 info->SerialNumber = cpu_to_le32(serial_crc);
Namjae Jeone2f34482021-03-16 10:49:09 +09004922 len = smbConvertToUTF16((__le16 *)info->VolumeLabel,
4923 share->name, PATH_MAX,
4924 conn->local_nls, 0);
4925 len = len * 2;
4926 info->VolumeLabelSize = cpu_to_le32(len);
4927 info->Reserved = 0;
4928 sz = sizeof(struct filesystem_vol_info) - 2 + len;
4929 rsp->OutputBufferLength = cpu_to_le32(sz);
Namjae Jeoncb451722021-11-03 08:08:44 +09004930 inc_rfc1001_len(work->response_buf, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09004931 fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE;
4932 break;
4933 }
4934 case FS_SIZE_INFORMATION:
4935 {
4936 struct filesystem_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004937
4938 info = (struct filesystem_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004939 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4940 info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004941 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4942 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004943 rsp->OutputBufferLength = cpu_to_le32(24);
Namjae Jeoncb451722021-11-03 08:08:44 +09004944 inc_rfc1001_len(work->response_buf, 24);
Namjae Jeone2f34482021-03-16 10:49:09 +09004945 fs_infoclass_size = FS_SIZE_INFORMATION_SIZE;
4946 break;
4947 }
4948 case FS_FULL_SIZE_INFORMATION:
4949 {
4950 struct smb2_fs_full_size_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004951
4952 info = (struct smb2_fs_full_size_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004953 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4954 info->CallerAvailableAllocationUnits =
4955 cpu_to_le64(stfs.f_bavail);
4956 info->ActualAvailableAllocationUnits =
4957 cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004958 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4959 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004960 rsp->OutputBufferLength = cpu_to_le32(32);
Namjae Jeoncb451722021-11-03 08:08:44 +09004961 inc_rfc1001_len(work->response_buf, 32);
Namjae Jeone2f34482021-03-16 10:49:09 +09004962 fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE;
4963 break;
4964 }
4965 case FS_OBJECT_ID_INFORMATION:
4966 {
4967 struct object_id_info *info;
4968
4969 info = (struct object_id_info *)(rsp->Buffer);
4970
4971 if (!user_guest(sess->user))
4972 memcpy(info->objid, user_passkey(sess->user), 16);
4973 else
4974 memset(info->objid, 0, 16);
4975
4976 info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC);
4977 info->extended_info.version = cpu_to_le32(1);
4978 info->extended_info.release = cpu_to_le32(1);
4979 info->extended_info.rel_date = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004980 memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0"));
Namjae Jeone2f34482021-03-16 10:49:09 +09004981 rsp->OutputBufferLength = cpu_to_le32(64);
Namjae Jeoncb451722021-11-03 08:08:44 +09004982 inc_rfc1001_len(work->response_buf, 64);
Namjae Jeone2f34482021-03-16 10:49:09 +09004983 fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE;
4984 break;
4985 }
4986 case FS_SECTOR_SIZE_INFORMATION:
4987 {
4988 struct smb3_fs_ss_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004989
4990 info = (struct smb3_fs_ss_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004991
Namjae Jeon131bac12021-06-22 16:20:47 +09004992 info->LogicalBytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004993 info->PhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09004994 cpu_to_le32(stfs.f_bsize);
4995 info->PhysicalBytesPerSectorForPerf = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004996 info->FSEffPhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09004997 cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004998 info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE |
4999 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE);
5000 info->ByteOffsetForSectorAlignment = 0;
5001 info->ByteOffsetForPartitionAlignment = 0;
5002 rsp->OutputBufferLength = cpu_to_le32(28);
Namjae Jeoncb451722021-11-03 08:08:44 +09005003 inc_rfc1001_len(work->response_buf, 28);
Namjae Jeone2f34482021-03-16 10:49:09 +09005004 fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE;
5005 break;
5006 }
5007 case FS_CONTROL_INFORMATION:
5008 {
5009 /*
5010 * TODO : The current implementation is based on
5011 * test result with win7(NTFS) server. It's need to
5012 * modify this to get valid Quota values
5013 * from Linux kernel
5014 */
5015 struct smb2_fs_control_info *info;
5016
5017 info = (struct smb2_fs_control_info *)(rsp->Buffer);
5018 info->FreeSpaceStartFiltering = 0;
5019 info->FreeSpaceThreshold = 0;
5020 info->FreeSpaceStopFiltering = 0;
5021 info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID);
5022 info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID);
5023 info->Padding = 0;
5024 rsp->OutputBufferLength = cpu_to_le32(48);
Namjae Jeoncb451722021-11-03 08:08:44 +09005025 inc_rfc1001_len(work->response_buf, 48);
Namjae Jeone2f34482021-03-16 10:49:09 +09005026 fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE;
5027 break;
5028 }
5029 case FS_POSIX_INFORMATION:
5030 {
5031 struct filesystem_posix_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09005032
5033 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005034 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005035 rc = -EOPNOTSUPP;
5036 } else {
5037 info = (struct filesystem_posix_info *)(rsp->Buffer);
Namjae Jeonee81cae2021-06-26 22:32:34 +09005038 info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005039 info->BlockSize = cpu_to_le32(stfs.f_bsize);
5040 info->TotalBlocks = cpu_to_le64(stfs.f_blocks);
5041 info->BlocksAvail = cpu_to_le64(stfs.f_bfree);
5042 info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail);
5043 info->TotalFileNodes = cpu_to_le64(stfs.f_files);
5044 info->FreeFileNodes = cpu_to_le64(stfs.f_ffree);
5045 rsp->OutputBufferLength = cpu_to_le32(56);
Namjae Jeoncb451722021-11-03 08:08:44 +09005046 inc_rfc1001_len(work->response_buf, 56);
Namjae Jeone2f34482021-03-16 10:49:09 +09005047 fs_infoclass_size = FS_POSIX_INFORMATION_SIZE;
5048 }
5049 break;
5050 }
5051 default:
5052 path_put(&path);
5053 return -EOPNOTSUPP;
5054 }
5055 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09005056 rsp, work->response_buf,
Namjae Jeone2f34482021-03-16 10:49:09 +09005057 fs_infoclass_size);
5058 path_put(&path);
5059 return rc;
5060}
5061
5062static int smb2_get_info_sec(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005063 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09005064 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09005065{
5066 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005067 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09005068 struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL;
5069 struct smb_fattr fattr = {{0}};
5070 struct inode *inode;
5071 __u32 secdesclen;
5072 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5073 int addition_info = le32_to_cpu(req->AdditionalInformation);
5074 int rc;
5075
Namjae Jeone294f782021-06-28 15:26:37 +09005076 if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
5077 PROTECTED_DACL_SECINFO |
5078 UNPROTECTED_DACL_SECINFO)) {
Namjae Jeon8e537d12021-11-21 07:48:45 +09005079 ksmbd_debug(SMB, "Unsupported addition info: 0x%x)\n",
Namjae Jeone294f782021-06-28 15:26:37 +09005080 addition_info);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09005081
5082 pntsd->revision = cpu_to_le16(1);
5083 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED);
5084 pntsd->osidoffset = 0;
5085 pntsd->gsidoffset = 0;
5086 pntsd->sacloffset = 0;
5087 pntsd->dacloffset = 0;
5088
5089 secdesclen = sizeof(struct smb_ntsd);
5090 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
Namjae Jeoncb451722021-11-03 08:08:44 +09005091 inc_rfc1001_len(work->response_buf, secdesclen);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09005092
5093 return 0;
5094 }
5095
Namjae Jeone2f34482021-03-16 10:49:09 +09005096 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon38673692021-07-08 12:32:27 +09005097 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
5098 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005099 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005100 id = work->compound_fid;
5101 pid = work->compound_pfid;
5102 }
5103 }
5104
Namjae Jeon38673692021-07-08 12:32:27 +09005105 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005106 id = le64_to_cpu(req->VolatileFileId);
5107 pid = le64_to_cpu(req->PersistentFileId);
5108 }
5109
5110 fp = ksmbd_lookup_fd_slow(work, id, pid);
5111 if (!fp)
5112 return -ENOENT;
5113
Hyunchul Lee465d7202021-07-03 12:10:36 +09005114 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeonab0b2632021-06-29 09:20:13 +09005115 inode = file_inode(fp->filp);
Christian Brauner43205ca2021-08-23 17:13:50 +02005116 ksmbd_acls_fattr(&fattr, user_ns, inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09005117
5118 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09005119 KSMBD_SHARE_FLAG_ACL_XATTR))
Hyunchul Lee465d7202021-07-03 12:10:36 +09005120 ksmbd_vfs_get_sd_xattr(work->conn, user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005121 fp->filp->f_path.dentry, &ppntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09005122
Hyunchul Lee465d7202021-07-03 12:10:36 +09005123 rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info,
5124 &secdesclen, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09005125 posix_acl_release(fattr.cf_acls);
5126 posix_acl_release(fattr.cf_dacls);
5127 kfree(ppntsd);
5128 ksmbd_fd_put(work, fp);
5129 if (rc)
5130 return rc;
5131
5132 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
Namjae Jeoncb451722021-11-03 08:08:44 +09005133 inc_rfc1001_len(work->response_buf, secdesclen);
Namjae Jeone2f34482021-03-16 10:49:09 +09005134 return 0;
5135}
5136
5137/**
5138 * smb2_query_info() - handler for smb2 query info command
5139 * @work: smb work containing query info request buffer
5140 *
5141 * Return: 0 on success, otherwise error
5142 */
5143int smb2_query_info(struct ksmbd_work *work)
5144{
5145 struct smb2_query_info_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09005146 struct smb2_query_info_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005147 int rc = 0;
5148
Namjae Jeone2f34482021-03-16 10:49:09 +09005149 WORK_BUFFERS(work, req, rsp);
5150
5151 ksmbd_debug(SMB, "GOT query info request\n");
5152
5153 switch (req->InfoType) {
5154 case SMB2_O_INFO_FILE:
5155 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005156 rc = smb2_get_info_file(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005157 break;
5158 case SMB2_O_INFO_FILESYSTEM:
5159 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005160 rc = smb2_get_info_filesystem(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005161 break;
5162 case SMB2_O_INFO_SECURITY:
5163 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005164 rc = smb2_get_info_sec(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005165 break;
5166 default:
5167 ksmbd_debug(SMB, "InfoType %d not supported yet\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005168 req->InfoType);
Namjae Jeone2f34482021-03-16 10:49:09 +09005169 rc = -EOPNOTSUPP;
5170 }
5171
5172 if (rc < 0) {
5173 if (rc == -EACCES)
5174 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5175 else if (rc == -ENOENT)
5176 rsp->hdr.Status = STATUS_FILE_CLOSED;
5177 else if (rc == -EIO)
5178 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5179 else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0)
5180 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5181 smb2_set_err_rsp(work);
5182
5183 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005184 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005185 return rc;
5186 }
5187 rsp->StructureSize = cpu_to_le16(9);
5188 rsp->OutputBufferOffset = cpu_to_le16(72);
Namjae Jeoncb451722021-11-03 08:08:44 +09005189 inc_rfc1001_len(work->response_buf, 8);
Namjae Jeone2f34482021-03-16 10:49:09 +09005190 return 0;
5191}
5192
5193/**
5194 * smb2_close_pipe() - handler for closing IPC pipe
5195 * @work: smb work containing close request buffer
5196 *
5197 * Return: 0
5198 */
5199static noinline int smb2_close_pipe(struct ksmbd_work *work)
5200{
Namjae Jeon64b39f42021-03-30 14:25:35 +09005201 u64 id;
Namjae Jeoncb451722021-11-03 08:08:44 +09005202 struct smb2_close_req *req = smb2_get_msg(work->request_buf);
5203 struct smb2_close_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005204
5205 id = le64_to_cpu(req->VolatileFileId);
5206 ksmbd_session_rpc_close(work->sess, id);
5207
5208 rsp->StructureSize = cpu_to_le16(60);
5209 rsp->Flags = 0;
5210 rsp->Reserved = 0;
5211 rsp->CreationTime = 0;
5212 rsp->LastAccessTime = 0;
5213 rsp->LastWriteTime = 0;
5214 rsp->ChangeTime = 0;
5215 rsp->AllocationSize = 0;
5216 rsp->EndOfFile = 0;
5217 rsp->Attributes = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09005218 inc_rfc1001_len(work->response_buf, 60);
Namjae Jeone2f34482021-03-16 10:49:09 +09005219 return 0;
5220}
5221
5222/**
5223 * smb2_close() - handler for smb2 close file command
5224 * @work: smb work containing close request buffer
5225 *
5226 * Return: 0
5227 */
5228int smb2_close(struct ksmbd_work *work)
5229{
Namjae Jeon38673692021-07-08 12:32:27 +09005230 u64 volatile_id = KSMBD_NO_FID;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005231 u64 sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09005232 struct smb2_close_req *req;
5233 struct smb2_close_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005234 struct ksmbd_conn *conn = work->conn;
5235 struct ksmbd_file *fp;
5236 struct inode *inode;
5237 u64 time;
5238 int err = 0;
5239
Namjae Jeone2f34482021-03-16 10:49:09 +09005240 WORK_BUFFERS(work, req, rsp);
5241
5242 if (test_share_config_flag(work->tcon->share_conf,
5243 KSMBD_SHARE_FLAG_PIPE)) {
5244 ksmbd_debug(SMB, "IPC pipe close request\n");
5245 return smb2_close_pipe(work);
5246 }
5247
5248 sess_id = le64_to_cpu(req->hdr.SessionId);
5249 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5250 sess_id = work->compound_sid;
5251
5252 work->compound_sid = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005253 if (check_session_id(conn, sess_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005254 work->compound_sid = sess_id;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005255 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005256 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
5257 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5258 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5259 err = -EBADF;
5260 goto out;
5261 }
5262
5263 if (work->next_smb2_rcv_hdr_off &&
Namjae Jeon38673692021-07-08 12:32:27 +09005264 !has_file_id(le64_to_cpu(req->VolatileFileId))) {
5265 if (!has_file_id(work->compound_fid)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005266 /* file already closed, return FILE_CLOSED */
5267 ksmbd_debug(SMB, "file already closed\n");
5268 rsp->hdr.Status = STATUS_FILE_CLOSED;
5269 err = -EBADF;
5270 goto out;
5271 } else {
Namjae Jeon38673692021-07-08 12:32:27 +09005272 ksmbd_debug(SMB,
5273 "Compound request set FID = %llu:%llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005274 work->compound_fid,
5275 work->compound_pfid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005276 volatile_id = work->compound_fid;
5277
5278 /* file closed, stored id is not valid anymore */
5279 work->compound_fid = KSMBD_NO_FID;
5280 work->compound_pfid = KSMBD_NO_FID;
5281 }
5282 } else {
5283 volatile_id = le64_to_cpu(req->VolatileFileId);
5284 }
Namjae Jeon38673692021-07-08 12:32:27 +09005285 ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id);
Namjae Jeone2f34482021-03-16 10:49:09 +09005286
5287 rsp->StructureSize = cpu_to_le16(60);
5288 rsp->Reserved = 0;
5289
5290 if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) {
5291 fp = ksmbd_lookup_fd_fast(work, volatile_id);
5292 if (!fp) {
5293 err = -ENOENT;
5294 goto out;
5295 }
5296
Namjae Jeonab0b2632021-06-29 09:20:13 +09005297 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005298 rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
5299 rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 :
5300 cpu_to_le64(inode->i_blocks << 9);
5301 rsp->EndOfFile = cpu_to_le64(inode->i_size);
5302 rsp->Attributes = fp->f_ci->m_fattr;
5303 rsp->CreationTime = cpu_to_le64(fp->create_time);
5304 time = ksmbd_UnixTimeToNT(inode->i_atime);
5305 rsp->LastAccessTime = cpu_to_le64(time);
5306 time = ksmbd_UnixTimeToNT(inode->i_mtime);
5307 rsp->LastWriteTime = cpu_to_le64(time);
5308 time = ksmbd_UnixTimeToNT(inode->i_ctime);
5309 rsp->ChangeTime = cpu_to_le64(time);
5310 ksmbd_fd_put(work, fp);
5311 } else {
5312 rsp->Flags = 0;
5313 rsp->AllocationSize = 0;
5314 rsp->EndOfFile = 0;
5315 rsp->Attributes = 0;
5316 rsp->CreationTime = 0;
5317 rsp->LastAccessTime = 0;
5318 rsp->LastWriteTime = 0;
5319 rsp->ChangeTime = 0;
5320 }
5321
5322 err = ksmbd_close_fd(work, volatile_id);
5323out:
5324 if (err) {
5325 if (rsp->hdr.Status == 0)
5326 rsp->hdr.Status = STATUS_FILE_CLOSED;
5327 smb2_set_err_rsp(work);
5328 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09005329 inc_rfc1001_len(work->response_buf, 60);
Namjae Jeone2f34482021-03-16 10:49:09 +09005330 }
5331
5332 return 0;
5333}
5334
5335/**
5336 * smb2_echo() - handler for smb2 echo(ping) command
5337 * @work: smb work containing echo request buffer
5338 *
5339 * Return: 0
5340 */
5341int smb2_echo(struct ksmbd_work *work)
5342{
Namjae Jeoncb451722021-11-03 08:08:44 +09005343 struct smb2_echo_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005344
5345 rsp->StructureSize = cpu_to_le16(4);
5346 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09005347 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09005348 return 0;
5349}
5350
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005351static int smb2_rename(struct ksmbd_work *work,
5352 struct ksmbd_file *fp,
5353 struct user_namespace *user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +09005354 struct smb2_file_rename_info *file_info,
5355 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005356{
5357 struct ksmbd_share_config *share = fp->tcon->share_conf;
5358 char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL;
5359 char *pathname = NULL;
5360 struct path path;
5361 bool file_present = true;
5362 int rc;
5363
5364 ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n");
5365 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5366 if (!pathname)
5367 return -ENOMEM;
5368
5369 abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
5370 if (IS_ERR(abs_oldname)) {
5371 rc = -EINVAL;
5372 goto out;
5373 }
5374 old_name = strrchr(abs_oldname, '/');
Namjae Jeon64b39f42021-03-30 14:25:35 +09005375 if (old_name && old_name[1] != '\0') {
Namjae Jeone2f34482021-03-16 10:49:09 +09005376 old_name++;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005377 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005378 ksmbd_debug(SMB, "can't get last component in path %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005379 abs_oldname);
Namjae Jeone2f34482021-03-16 10:49:09 +09005380 rc = -ENOENT;
5381 goto out;
5382 }
5383
5384 new_name = smb2_get_name(share,
5385 file_info->FileName,
5386 le32_to_cpu(file_info->FileNameLength),
5387 local_nls);
5388 if (IS_ERR(new_name)) {
5389 rc = PTR_ERR(new_name);
5390 goto out;
5391 }
5392
5393 if (strchr(new_name, ':')) {
5394 int s_type;
5395 char *xattr_stream_name, *stream_name = NULL;
5396 size_t xattr_stream_size;
5397 int len;
5398
5399 rc = parse_stream_name(new_name, &stream_name, &s_type);
5400 if (rc < 0)
5401 goto out;
5402
5403 len = strlen(new_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005404 if (len > 0 && new_name[len - 1] != '/') {
Namjae Jeonbde16942021-06-28 15:23:19 +09005405 pr_err("not allow base filename in rename\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005406 rc = -ESHARE;
5407 goto out;
5408 }
5409
5410 rc = ksmbd_vfs_xattr_stream_name(stream_name,
5411 &xattr_stream_name,
5412 &xattr_stream_size,
5413 s_type);
5414 if (rc)
5415 goto out;
5416
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005417 rc = ksmbd_vfs_setxattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005418 fp->filp->f_path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09005419 xattr_stream_name,
5420 NULL, 0, 0);
5421 if (rc < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005422 pr_err("failed to store stream name in xattr: %d\n",
5423 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005424 rc = -EINVAL;
5425 goto out;
5426 }
5427
5428 goto out;
5429 }
5430
5431 ksmbd_debug(SMB, "new name %s\n", new_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005432 rc = ksmbd_vfs_kern_path(work, new_name, LOOKUP_NO_SYMLINKS, &path, 1);
5433 if (rc) {
5434 if (rc != -ENOENT)
5435 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005436 file_present = false;
Hyunchul Lee265fd192021-09-25 00:06:16 +09005437 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005438 path_put(&path);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005439 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005440
5441 if (ksmbd_share_veto_filename(share, new_name)) {
5442 rc = -ENOENT;
5443 ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name);
5444 goto out;
5445 }
5446
5447 if (file_info->ReplaceIfExists) {
5448 if (file_present) {
5449 rc = ksmbd_vfs_remove_file(work, new_name);
5450 if (rc) {
5451 if (rc != -ENOTEMPTY)
5452 rc = -EINVAL;
5453 ksmbd_debug(SMB, "cannot delete %s, rc %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005454 new_name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005455 goto out;
5456 }
5457 }
5458 } else {
5459 if (file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005460 strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005461 rc = -EEXIST;
5462 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005463 "cannot rename already existing file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005464 goto out;
5465 }
5466 }
5467
5468 rc = ksmbd_vfs_fp_rename(work, fp, new_name);
5469out:
5470 kfree(pathname);
5471 if (!IS_ERR(new_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005472 kfree(new_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005473 return rc;
5474}
5475
Namjae Jeone2f34482021-03-16 10:49:09 +09005476static int smb2_create_link(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005477 struct ksmbd_share_config *share,
5478 struct smb2_file_link_info *file_info,
Namjae Jeon9496e262021-09-29 15:41:48 +09005479 unsigned int buf_len, struct file *filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005480 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005481{
5482 char *link_name = NULL, *target_name = NULL, *pathname = NULL;
5483 struct path path;
5484 bool file_present = true;
5485 int rc;
5486
Namjae Jeon9496e262021-09-29 15:41:48 +09005487 if (buf_len < (u64)sizeof(struct smb2_file_link_info) +
5488 le32_to_cpu(file_info->FileNameLength))
5489 return -EINVAL;
5490
Namjae Jeone2f34482021-03-16 10:49:09 +09005491 ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n");
5492 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5493 if (!pathname)
5494 return -ENOMEM;
5495
5496 link_name = smb2_get_name(share,
5497 file_info->FileName,
5498 le32_to_cpu(file_info->FileNameLength),
5499 local_nls);
5500 if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) {
5501 rc = -EINVAL;
5502 goto out;
5503 }
5504
5505 ksmbd_debug(SMB, "link name is %s\n", link_name);
5506 target_name = d_path(&filp->f_path, pathname, PATH_MAX);
5507 if (IS_ERR(target_name)) {
5508 rc = -EINVAL;
5509 goto out;
5510 }
5511
5512 ksmbd_debug(SMB, "target name is %s\n", target_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005513 rc = ksmbd_vfs_kern_path(work, link_name, LOOKUP_NO_SYMLINKS, &path, 0);
5514 if (rc) {
5515 if (rc != -ENOENT)
5516 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005517 file_present = false;
Hyunchul Lee265fd192021-09-25 00:06:16 +09005518 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005519 path_put(&path);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005520 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005521
5522 if (file_info->ReplaceIfExists) {
5523 if (file_present) {
5524 rc = ksmbd_vfs_remove_file(work, link_name);
5525 if (rc) {
5526 rc = -EINVAL;
5527 ksmbd_debug(SMB, "cannot delete %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005528 link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005529 goto out;
5530 }
5531 }
5532 } else {
5533 if (file_present) {
5534 rc = -EEXIST;
5535 ksmbd_debug(SMB, "link already exists\n");
5536 goto out;
5537 }
5538 }
5539
5540 rc = ksmbd_vfs_link(work, target_name, link_name);
5541 if (rc)
5542 rc = -EINVAL;
5543out:
5544 if (!IS_ERR(link_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005545 kfree(link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005546 kfree(pathname);
5547 return rc;
5548}
5549
Namjae Jeon9496e262021-09-29 15:41:48 +09005550static int set_file_basic_info(struct ksmbd_file *fp,
5551 struct smb2_file_basic_info *file_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005552 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005553{
Namjae Jeone2f34482021-03-16 10:49:09 +09005554 struct iattr attrs;
Namjae Jeone2f34482021-03-16 10:49:09 +09005555 struct file *filp;
5556 struct inode *inode;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005557 struct user_namespace *user_ns;
Namjae Jeon4ffd5262021-09-07 08:15:21 +09005558 int rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09005559
Marios Makassikis7adfd4f2021-04-27 15:30:22 +09005560 if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005561 return -EACCES;
5562
Namjae Jeone2f34482021-03-16 10:49:09 +09005563 attrs.ia_valid = 0;
5564 filp = fp->filp;
5565 inode = file_inode(filp);
Hyunchul Lee465d7202021-07-03 12:10:36 +09005566 user_ns = file_mnt_user_ns(filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005567
5568 if (file_info->CreationTime)
5569 fp->create_time = le64_to_cpu(file_info->CreationTime);
5570
5571 if (file_info->LastAccessTime) {
5572 attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime);
5573 attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
5574 }
5575
Namjae Jeon64e78752021-10-03 13:19:00 +09005576 attrs.ia_valid |= ATTR_CTIME;
5577 if (file_info->ChangeTime)
Christian Braunerdb7fb6f2021-08-26 10:07:05 +09005578 attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime);
Namjae Jeon64e78752021-10-03 13:19:00 +09005579 else
5580 attrs.ia_ctime = inode->i_ctime;
Namjae Jeone2f34482021-03-16 10:49:09 +09005581
5582 if (file_info->LastWriteTime) {
5583 attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime);
5584 attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
5585 }
5586
5587 if (file_info->Attributes) {
5588 if (!S_ISDIR(inode->i_mode) &&
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09005589 file_info->Attributes & FILE_ATTRIBUTE_DIRECTORY_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005590 pr_err("can't change a file to a directory\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005591 return -EINVAL;
5592 }
5593
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09005594 if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == FILE_ATTRIBUTE_NORMAL_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005595 fp->f_ci->m_fattr = file_info->Attributes |
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09005596 (fp->f_ci->m_fattr & FILE_ATTRIBUTE_DIRECTORY_LE);
Namjae Jeone2f34482021-03-16 10:49:09 +09005597 }
5598
5599 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
5600 (file_info->CreationTime || file_info->Attributes)) {
5601 struct xattr_dos_attrib da = {0};
5602
5603 da.version = 4;
5604 da.itime = fp->itime;
5605 da.create_time = fp->create_time;
5606 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
5607 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
5608 XATTR_DOSINFO_ITIME;
5609
Hyunchul Lee465d7202021-07-03 12:10:36 +09005610 rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005611 filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09005612 if (rc)
5613 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005614 "failed to restore file attribute in EA\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005615 rc = 0;
5616 }
5617
Namjae Jeone2f34482021-03-16 10:49:09 +09005618 if (attrs.ia_valid) {
5619 struct dentry *dentry = filp->f_path.dentry;
5620 struct inode *inode = d_inode(dentry);
5621
5622 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
5623 return -EACCES;
5624
Namjae Jeone2f34482021-03-16 10:49:09 +09005625 inode_lock(inode);
Namjae Jeon64e78752021-10-03 13:19:00 +09005626 inode->i_ctime = attrs.ia_ctime;
5627 attrs.ia_valid &= ~ATTR_CTIME;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005628 rc = notify_change(user_ns, dentry, &attrs, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09005629 inode_unlock(inode);
5630 }
Christian Braunereb5784f2021-08-23 17:13:55 +02005631 return rc;
Namjae Jeone2f34482021-03-16 10:49:09 +09005632}
5633
5634static int set_file_allocation_info(struct ksmbd_work *work,
Namjae Jeon9496e262021-09-29 15:41:48 +09005635 struct ksmbd_file *fp,
5636 struct smb2_file_alloc_info *file_alloc_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005637{
5638 /*
5639 * TODO : It's working fine only when store dos attributes
5640 * is not yes. need to implement a logic which works
5641 * properly with any smb.conf option
5642 */
5643
Namjae Jeone2f34482021-03-16 10:49:09 +09005644 loff_t alloc_blks;
5645 struct inode *inode;
5646 int rc;
5647
Marios Makassikisa2996692021-04-27 15:29:01 +09005648 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005649 return -EACCES;
5650
Namjae Jeone2f34482021-03-16 10:49:09 +09005651 alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9;
5652 inode = file_inode(fp->filp);
5653
5654 if (alloc_blks > inode->i_blocks) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005655 smb_break_all_levII_oplock(work, fp, 1);
5656 rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
5657 alloc_blks * 512);
Namjae Jeone2f34482021-03-16 10:49:09 +09005658 if (rc && rc != -EOPNOTSUPP) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005659 pr_err("vfs_fallocate is failed : %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005660 return rc;
5661 }
5662 } else if (alloc_blks < inode->i_blocks) {
5663 loff_t size;
5664
5665 /*
5666 * Allocation size could be smaller than original one
5667 * which means allocated blocks in file should be
5668 * deallocated. use truncate to cut out it, but inode
5669 * size is also updated with truncate offset.
5670 * inode size is retained by backup inode size.
5671 */
5672 size = i_size_read(inode);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005673 rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512);
Namjae Jeone2f34482021-03-16 10:49:09 +09005674 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005675 pr_err("truncate failed! filename : %s, err %d\n",
5676 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005677 return rc;
5678 }
5679 if (size < alloc_blks * 512)
5680 i_size_write(inode, size);
5681 }
5682 return 0;
5683}
5684
Namjae Jeon64b39f42021-03-30 14:25:35 +09005685static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005686 struct smb2_file_eof_info *file_eof_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005687{
Namjae Jeone2f34482021-03-16 10:49:09 +09005688 loff_t newsize;
5689 struct inode *inode;
5690 int rc;
5691
Marios Makassikisa2996692021-04-27 15:29:01 +09005692 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005693 return -EACCES;
5694
Namjae Jeone2f34482021-03-16 10:49:09 +09005695 newsize = le64_to_cpu(file_eof_info->EndOfFile);
5696 inode = file_inode(fp->filp);
5697
5698 /*
5699 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called
5700 * on FAT32 shared device, truncate execution time is too long
5701 * and network error could cause from windows client. because
5702 * truncate of some filesystem like FAT32 fill zero data in
5703 * truncated range.
5704 */
5705 if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) {
5706 ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005707 fp->filename, newsize);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005708 rc = ksmbd_vfs_truncate(work, fp, newsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005709 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005710 ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005711 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005712 if (rc != -EAGAIN)
5713 rc = -EBADF;
5714 return rc;
5715 }
5716 }
5717 return 0;
5718}
5719
Namjae Jeon64b39f42021-03-30 14:25:35 +09005720static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005721 struct smb2_file_rename_info *rename_info,
5722 unsigned int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005723{
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005724 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09005725 struct ksmbd_file *parent_fp;
Namjae Jeon12202c02021-06-29 09:23:56 +09005726 struct dentry *parent;
5727 struct dentry *dentry = fp->filp->f_path.dentry;
5728 int ret;
Namjae Jeone2f34482021-03-16 10:49:09 +09005729
5730 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005731 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005732 return -EACCES;
5733 }
5734
Namjae Jeon9496e262021-09-29 15:41:48 +09005735 if (buf_len < (u64)sizeof(struct smb2_file_rename_info) +
5736 le32_to_cpu(rename_info->FileNameLength))
5737 return -EINVAL;
5738
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005739 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005740 if (ksmbd_stream_fd(fp))
5741 goto next;
5742
Namjae Jeon12202c02021-06-29 09:23:56 +09005743 parent = dget_parent(dentry);
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005744 ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
Namjae Jeon12202c02021-06-29 09:23:56 +09005745 if (ret) {
5746 dput(parent);
5747 return ret;
5748 }
5749
5750 parent_fp = ksmbd_lookup_fd_inode(d_inode(parent));
5751 inode_unlock(d_inode(parent));
5752 dput(parent);
5753
Namjae Jeone2f34482021-03-16 10:49:09 +09005754 if (parent_fp) {
5755 if (parent_fp->daccess & FILE_DELETE_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005756 pr_err("parent dir is opened with delete access\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005757 return -ESHARE;
5758 }
5759 }
5760next:
Namjae Jeon9496e262021-09-29 15:41:48 +09005761 return smb2_rename(work, fp, user_ns, rename_info,
Namjae Jeone2f34482021-03-16 10:49:09 +09005762 work->sess->conn->local_nls);
5763}
5764
Namjae Jeon9496e262021-09-29 15:41:48 +09005765static int set_file_disposition_info(struct ksmbd_file *fp,
5766 struct smb2_file_disposition_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005767{
Namjae Jeone2f34482021-03-16 10:49:09 +09005768 struct inode *inode;
5769
5770 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005771 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005772 return -EACCES;
5773 }
5774
5775 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005776 if (file_info->DeletePending) {
5777 if (S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005778 ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY)
Namjae Jeone2f34482021-03-16 10:49:09 +09005779 return -EBUSY;
5780 ksmbd_set_inode_pending_delete(fp);
5781 } else {
5782 ksmbd_clear_inode_pending_delete(fp);
5783 }
5784 return 0;
5785}
5786
Namjae Jeon9496e262021-09-29 15:41:48 +09005787static int set_file_position_info(struct ksmbd_file *fp,
5788 struct smb2_file_pos_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005789{
Namjae Jeone2f34482021-03-16 10:49:09 +09005790 loff_t current_byte_offset;
Namjae Jeonee81cae2021-06-26 22:32:34 +09005791 unsigned long sector_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09005792 struct inode *inode;
5793
5794 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005795 current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset);
Namjae Jeonee81cae2021-06-26 22:32:34 +09005796 sector_size = inode->i_sb->s_blocksize;
Namjae Jeone2f34482021-03-16 10:49:09 +09005797
5798 if (current_byte_offset < 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09005799 (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE &&
5800 current_byte_offset & (sector_size - 1))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005801 pr_err("CurrentByteOffset is not valid : %llu\n",
5802 current_byte_offset);
Namjae Jeone2f34482021-03-16 10:49:09 +09005803 return -EINVAL;
5804 }
5805
5806 fp->filp->f_pos = current_byte_offset;
5807 return 0;
5808}
5809
Namjae Jeon9496e262021-09-29 15:41:48 +09005810static int set_file_mode_info(struct ksmbd_file *fp,
5811 struct smb2_file_mode_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005812{
Namjae Jeone2f34482021-03-16 10:49:09 +09005813 __le32 mode;
5814
Namjae Jeone2f34482021-03-16 10:49:09 +09005815 mode = file_info->Mode;
5816
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09005817 if ((mode & ~FILE_MODE_INFO_MASK)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005818 pr_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode));
Namjae Jeone2f34482021-03-16 10:49:09 +09005819 return -EINVAL;
5820 }
5821
5822 /*
5823 * TODO : need to implement consideration for
5824 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT
5825 */
5826 ksmbd_vfs_set_fadvise(fp->filp, mode);
5827 fp->coption = mode;
5828 return 0;
5829}
5830
5831/**
5832 * smb2_set_info_file() - handler for smb2 set info command
5833 * @work: smb work containing set info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09005834 * @fp: ksmbd_file pointer
5835 * @info_class: smb2 set info class
5836 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09005837 *
5838 * Return: 0 on success, otherwise error
5839 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH
5840 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09005841static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005842 struct smb2_set_info_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09005843 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005844{
Namjae Jeon9496e262021-09-29 15:41:48 +09005845 unsigned int buf_len = le32_to_cpu(req->BufferLength);
5846
5847 switch (req->FileInfoClass) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005848 case FILE_BASIC_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005849 {
5850 if (buf_len < sizeof(struct smb2_file_basic_info))
5851 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005852
Namjae Jeon9496e262021-09-29 15:41:48 +09005853 return set_file_basic_info(fp, (struct smb2_file_basic_info *)req->Buffer, share);
5854 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005855 case FILE_ALLOCATION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005856 {
5857 if (buf_len < sizeof(struct smb2_file_alloc_info))
5858 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005859
Namjae Jeon9496e262021-09-29 15:41:48 +09005860 return set_file_allocation_info(work, fp,
5861 (struct smb2_file_alloc_info *)req->Buffer);
5862 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005863 case FILE_END_OF_FILE_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005864 {
5865 if (buf_len < sizeof(struct smb2_file_eof_info))
5866 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005867
Namjae Jeon9496e262021-09-29 15:41:48 +09005868 return set_end_of_file_info(work, fp,
5869 (struct smb2_file_eof_info *)req->Buffer);
5870 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005871 case FILE_RENAME_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005872 {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005873 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005874 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005875 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005876 return -EACCES;
5877 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005878
Namjae Jeon9496e262021-09-29 15:41:48 +09005879 if (buf_len < sizeof(struct smb2_file_rename_info))
5880 return -EINVAL;
5881
5882 return set_rename_info(work, fp,
5883 (struct smb2_file_rename_info *)req->Buffer,
5884 buf_len);
5885 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005886 case FILE_LINK_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005887 {
5888 if (buf_len < sizeof(struct smb2_file_link_info))
5889 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005890
Namjae Jeon9496e262021-09-29 15:41:48 +09005891 return smb2_create_link(work, work->tcon->share_conf,
5892 (struct smb2_file_link_info *)req->Buffer,
5893 buf_len, fp->filp,
5894 work->sess->conn->local_nls);
5895 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005896 case FILE_DISPOSITION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005897 {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005898 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005899 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005900 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005901 return -EACCES;
5902 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005903
Namjae Jeon9496e262021-09-29 15:41:48 +09005904 if (buf_len < sizeof(struct smb2_file_disposition_info))
5905 return -EINVAL;
5906
5907 return set_file_disposition_info(fp,
5908 (struct smb2_file_disposition_info *)req->Buffer);
5909 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005910 case FILE_FULL_EA_INFORMATION:
5911 {
5912 if (!(fp->daccess & FILE_WRITE_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005913 pr_err("Not permitted to write ext attr: 0x%x\n",
5914 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005915 return -EACCES;
5916 }
5917
Namjae Jeon9496e262021-09-29 15:41:48 +09005918 if (buf_len < sizeof(struct smb2_ea_info))
5919 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005920
Namjae Jeon9496e262021-09-29 15:41:48 +09005921 return smb2_set_ea((struct smb2_ea_info *)req->Buffer,
5922 buf_len, &fp->filp->f_path);
5923 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005924 case FILE_POSITION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005925 {
5926 if (buf_len < sizeof(struct smb2_file_pos_info))
5927 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005928
Namjae Jeon9496e262021-09-29 15:41:48 +09005929 return set_file_position_info(fp, (struct smb2_file_pos_info *)req->Buffer);
5930 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005931 case FILE_MODE_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005932 {
5933 if (buf_len < sizeof(struct smb2_file_mode_info))
5934 return -EINVAL;
5935
5936 return set_file_mode_info(fp, (struct smb2_file_mode_info *)req->Buffer);
5937 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005938 }
5939
Namjae Jeon9496e262021-09-29 15:41:48 +09005940 pr_err("Unimplemented Fileinfoclass :%d\n", req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09005941 return -EOPNOTSUPP;
5942}
5943
Namjae Jeon64b39f42021-03-30 14:25:35 +09005944static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005945 char *buffer, int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005946{
5947 struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer;
5948
5949 fp->saccess |= FILE_SHARE_DELETE_LE;
5950
Hyunchul Leeef24c962021-06-30 18:25:52 +09005951 return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd,
Namjae Jeone2f34482021-03-16 10:49:09 +09005952 buf_len, false);
5953}
5954
5955/**
5956 * smb2_set_info() - handler for smb2 set info command handler
5957 * @work: smb work containing set info request buffer
5958 *
5959 * Return: 0 on success, otherwise error
5960 */
5961int smb2_set_info(struct ksmbd_work *work)
5962{
5963 struct smb2_set_info_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09005964 struct smb2_set_info_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005965 struct ksmbd_file *fp;
5966 int rc = 0;
5967 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5968
5969 ksmbd_debug(SMB, "Received set info request\n");
5970
Namjae Jeone2f34482021-03-16 10:49:09 +09005971 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09005972 req = ksmbd_req_buf_next(work);
5973 rsp = ksmbd_resp_buf_next(work);
Namjae Jeon38673692021-07-08 12:32:27 +09005974 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
5975 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005976 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005977 id = work->compound_fid;
5978 pid = work->compound_pfid;
5979 }
5980 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09005981 req = smb2_get_msg(work->request_buf);
5982 rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005983 }
5984
Namjae Jeon38673692021-07-08 12:32:27 +09005985 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005986 id = le64_to_cpu(req->VolatileFileId);
5987 pid = le64_to_cpu(req->PersistentFileId);
5988 }
5989
5990 fp = ksmbd_lookup_fd_slow(work, id, pid);
5991 if (!fp) {
5992 ksmbd_debug(SMB, "Invalid id for close: %u\n", id);
5993 rc = -ENOENT;
5994 goto err_out;
5995 }
5996
5997 switch (req->InfoType) {
5998 case SMB2_O_INFO_FILE:
5999 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
Namjae Jeon9496e262021-09-29 15:41:48 +09006000 rc = smb2_set_info_file(work, fp, req, work->tcon->share_conf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006001 break;
6002 case SMB2_O_INFO_SECURITY:
6003 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
Namjae Jeone70e3922021-08-21 23:26:01 +09006004 if (ksmbd_override_fsids(work)) {
6005 rc = -ENOMEM;
6006 goto err_out;
6007 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006008 rc = smb2_set_info_sec(fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09006009 le32_to_cpu(req->AdditionalInformation),
6010 req->Buffer,
6011 le32_to_cpu(req->BufferLength));
Namjae Jeone70e3922021-08-21 23:26:01 +09006012 ksmbd_revert_fsids(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09006013 break;
6014 default:
6015 rc = -EOPNOTSUPP;
6016 }
6017
6018 if (rc < 0)
6019 goto err_out;
6020
6021 rsp->StructureSize = cpu_to_le16(2);
Namjae Jeoncb451722021-11-03 08:08:44 +09006022 inc_rfc1001_len(work->response_buf, 2);
Namjae Jeone2f34482021-03-16 10:49:09 +09006023 ksmbd_fd_put(work, fp);
6024 return 0;
6025
6026err_out:
Hyunchul Lee265fd192021-09-25 00:06:16 +09006027 if (rc == -EACCES || rc == -EPERM || rc == -EXDEV)
Namjae Jeone2f34482021-03-16 10:49:09 +09006028 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6029 else if (rc == -EINVAL)
6030 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6031 else if (rc == -ESHARE)
6032 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6033 else if (rc == -ENOENT)
6034 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
6035 else if (rc == -EBUSY || rc == -ENOTEMPTY)
6036 rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY;
6037 else if (rc == -EAGAIN)
6038 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
Namjae Jeonff1d5722021-04-13 13:18:10 +09006039 else if (rc == -EBADF || rc == -ESTALE)
Namjae Jeone2f34482021-03-16 10:49:09 +09006040 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6041 else if (rc == -EEXIST)
6042 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
6043 else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP)
6044 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
6045 smb2_set_err_rsp(work);
6046 ksmbd_fd_put(work, fp);
Namjae Jeon070fb212021-05-26 17:57:12 +09006047 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09006048 return rc;
6049}
6050
6051/**
6052 * smb2_read_pipe() - handler for smb2 read from IPC pipe
6053 * @work: smb work containing read IPC pipe command buffer
6054 *
6055 * Return: 0 on success, otherwise error
6056 */
6057static noinline int smb2_read_pipe(struct ksmbd_work *work)
6058{
6059 int nbytes = 0, err;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006060 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09006061 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeoncb451722021-11-03 08:08:44 +09006062 struct smb2_read_req *req = smb2_get_msg(work->request_buf);
6063 struct smb2_read_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006064
6065 id = le64_to_cpu(req->VolatileFileId);
6066
Namjae Jeoncb451722021-11-03 08:08:44 +09006067 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006068 rpc_resp = ksmbd_rpc_read(work->sess, id);
6069 if (rpc_resp) {
6070 if (rpc_resp->flags != KSMBD_RPC_OK) {
6071 err = -EINVAL;
6072 goto out;
6073 }
6074
6075 work->aux_payload_buf =
Namjae Jeon79f6b112021-04-02 12:47:14 +09006076 kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006077 if (!work->aux_payload_buf) {
6078 err = -ENOMEM;
6079 goto out;
6080 }
6081
6082 memcpy(work->aux_payload_buf, rpc_resp->payload,
Namjae Jeon070fb212021-05-26 17:57:12 +09006083 rpc_resp->payload_sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09006084
6085 nbytes = rpc_resp->payload_sz;
Namjae Jeoncb451722021-11-03 08:08:44 +09006086 work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09006087 work->aux_payload_sz = nbytes;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006088 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006089 }
6090
6091 rsp->StructureSize = cpu_to_le16(17);
6092 rsp->DataOffset = 80;
6093 rsp->Reserved = 0;
6094 rsp->DataLength = cpu_to_le32(nbytes);
6095 rsp->DataRemaining = 0;
Ronnie Sahlberg699230f2021-09-09 12:26:12 +09006096 rsp->Flags = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006097 inc_rfc1001_len(work->response_buf, nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09006098 return 0;
6099
6100out:
6101 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
6102 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006103 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006104 return err;
6105}
6106
6107static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006108 struct smb2_read_req *req, void *data_buf,
6109 size_t length)
Namjae Jeone2f34482021-03-16 10:49:09 +09006110{
6111 struct smb2_buffer_desc_v1 *desc =
6112 (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6113 int err;
6114
Namjae Jeon64b39f42021-03-30 14:25:35 +09006115 if (work->conn->dialect == SMB30_PROT_ID &&
6116 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09006117 return -EINVAL;
6118
Namjae Jeon64b39f42021-03-30 14:25:35 +09006119 if (req->ReadChannelInfoOffset == 0 ||
6120 le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09006121 return -EINVAL;
6122
6123 work->need_invalidate_rkey =
6124 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6125 work->remote_key = le32_to_cpu(desc->token);
6126
Namjae Jeon64b39f42021-03-30 14:25:35 +09006127 err = ksmbd_conn_rdma_write(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006128 le32_to_cpu(desc->token),
6129 le64_to_cpu(desc->offset),
6130 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006131 if (err)
6132 return err;
6133
6134 return length;
6135}
6136
6137/**
6138 * smb2_read() - handler for smb2 read from file
6139 * @work: smb work containing read command buffer
6140 *
6141 * Return: 0 on success, otherwise error
6142 */
6143int smb2_read(struct ksmbd_work *work)
6144{
6145 struct ksmbd_conn *conn = work->conn;
6146 struct smb2_read_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006147 struct smb2_read_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09006148 struct ksmbd_file *fp;
6149 loff_t offset;
6150 size_t length, mincount;
6151 ssize_t nbytes = 0, remain_bytes = 0;
6152 int err = 0;
6153
Namjae Jeone2f34482021-03-16 10:49:09 +09006154 WORK_BUFFERS(work, req, rsp);
6155
6156 if (test_share_config_flag(work->tcon->share_conf,
6157 KSMBD_SHARE_FLAG_PIPE)) {
6158 ksmbd_debug(SMB, "IPC pipe read request\n");
6159 return smb2_read_pipe(work);
6160 }
6161
Namjae Jeon070fb212021-05-26 17:57:12 +09006162 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
6163 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006164 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006165 err = -ENOENT;
6166 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006167 }
6168
6169 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006170 pr_err("Not permitted to read : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09006171 err = -EACCES;
6172 goto out;
6173 }
6174
6175 offset = le64_to_cpu(req->Offset);
6176 length = le32_to_cpu(req->Length);
6177 mincount = le32_to_cpu(req->MinimumCount);
6178
6179 if (length > conn->vals->max_read_size) {
6180 ksmbd_debug(SMB, "limiting read size to max size(%u)\n",
6181 conn->vals->max_read_size);
6182 err = -EINVAL;
6183 goto out;
6184 }
6185
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006186 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6187 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006188
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006189 work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006190 if (!work->aux_payload_buf) {
Dan Carpenterc1ea1112021-03-22 17:50:11 +03006191 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09006192 goto out;
6193 }
6194
6195 nbytes = ksmbd_vfs_read(work, fp, length, &offset);
6196 if (nbytes < 0) {
6197 err = nbytes;
6198 goto out;
6199 }
6200
6201 if ((nbytes == 0 && length != 0) || nbytes < mincount) {
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006202 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006203 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006204 rsp->hdr.Status = STATUS_END_OF_FILE;
6205 smb2_set_err_rsp(work);
6206 ksmbd_fd_put(work, fp);
6207 return 0;
6208 }
6209
6210 ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006211 nbytes, offset, mincount);
Namjae Jeone2f34482021-03-16 10:49:09 +09006212
6213 if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006214 req->Channel == SMB2_CHANNEL_RDMA_V1) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006215 /* write data to the client using rdma channel */
6216 remain_bytes = smb2_read_rdma_channel(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09006217 work->aux_payload_buf,
6218 nbytes);
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006219 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006220 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006221
6222 nbytes = 0;
6223 if (remain_bytes < 0) {
6224 err = (int)remain_bytes;
6225 goto out;
6226 }
6227 }
6228
6229 rsp->StructureSize = cpu_to_le16(17);
6230 rsp->DataOffset = 80;
6231 rsp->Reserved = 0;
6232 rsp->DataLength = cpu_to_le32(nbytes);
6233 rsp->DataRemaining = cpu_to_le32(remain_bytes);
Ronnie Sahlberg699230f2021-09-09 12:26:12 +09006234 rsp->Flags = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006235 inc_rfc1001_len(work->response_buf, 16);
6236 work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09006237 work->aux_payload_sz = nbytes;
Namjae Jeoncb451722021-11-03 08:08:44 +09006238 inc_rfc1001_len(work->response_buf, nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09006239 ksmbd_fd_put(work, fp);
6240 return 0;
6241
6242out:
6243 if (err) {
6244 if (err == -EISDIR)
6245 rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST;
6246 else if (err == -EAGAIN)
6247 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6248 else if (err == -ENOENT)
6249 rsp->hdr.Status = STATUS_FILE_CLOSED;
6250 else if (err == -EACCES)
6251 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6252 else if (err == -ESHARE)
6253 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6254 else if (err == -EINVAL)
6255 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6256 else
6257 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6258
6259 smb2_set_err_rsp(work);
6260 }
6261 ksmbd_fd_put(work, fp);
6262 return err;
6263}
6264
6265/**
6266 * smb2_write_pipe() - handler for smb2 write on IPC pipe
6267 * @work: smb work containing write IPC pipe command buffer
6268 *
6269 * Return: 0 on success, otherwise error
6270 */
6271static noinline int smb2_write_pipe(struct ksmbd_work *work)
6272{
Namjae Jeoncb451722021-11-03 08:08:44 +09006273 struct smb2_write_req *req = smb2_get_msg(work->request_buf);
6274 struct smb2_write_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006275 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006276 u64 id = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09006277 int err = 0, ret = 0;
6278 char *data_buf;
6279 size_t length;
6280
6281 length = le32_to_cpu(req->Length);
6282 id = le64_to_cpu(req->VolatileFileId);
6283
6284 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeoncb451722021-11-03 08:08:44 +09006285 offsetof(struct smb2_write_req, Buffer)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006286 data_buf = (char *)&req->Buffer[0];
6287 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09006288 if ((u64)le16_to_cpu(req->DataOffset) + length >
6289 get_rfc1002_len(work->request_buf)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006290 pr_err("invalid write data offset %u, smb_len %u\n",
6291 le16_to_cpu(req->DataOffset),
Namjae Jeoncb451722021-11-03 08:08:44 +09006292 get_rfc1002_len(work->request_buf));
Namjae Jeone2f34482021-03-16 10:49:09 +09006293 err = -EINVAL;
6294 goto out;
6295 }
6296
6297 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6298 le16_to_cpu(req->DataOffset));
6299 }
6300
6301 rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length);
6302 if (rpc_resp) {
6303 if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
6304 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006305 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006306 smb2_set_err_rsp(work);
6307 return -EOPNOTSUPP;
6308 }
6309 if (rpc_resp->flags != KSMBD_RPC_OK) {
6310 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6311 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006312 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006313 return ret;
6314 }
Namjae Jeon79f6b112021-04-02 12:47:14 +09006315 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006316 }
6317
6318 rsp->StructureSize = cpu_to_le16(17);
6319 rsp->DataOffset = 0;
6320 rsp->Reserved = 0;
6321 rsp->DataLength = cpu_to_le32(length);
6322 rsp->DataRemaining = 0;
6323 rsp->Reserved2 = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006324 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006325 return 0;
6326out:
6327 if (err) {
6328 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6329 smb2_set_err_rsp(work);
6330 }
6331
6332 return err;
6333}
6334
6335static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006336 struct smb2_write_req *req,
6337 struct ksmbd_file *fp,
6338 loff_t offset, size_t length, bool sync)
Namjae Jeone2f34482021-03-16 10:49:09 +09006339{
6340 struct smb2_buffer_desc_v1 *desc;
6341 char *data_buf;
6342 int ret;
6343 ssize_t nbytes;
6344
6345 desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6346
6347 if (work->conn->dialect == SMB30_PROT_ID &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006348 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09006349 return -EINVAL;
6350
6351 if (req->Length != 0 || req->DataOffset != 0)
6352 return -EINVAL;
6353
Namjae Jeon64b39f42021-03-30 14:25:35 +09006354 if (req->WriteChannelInfoOffset == 0 ||
6355 le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09006356 return -EINVAL;
6357
6358 work->need_invalidate_rkey =
6359 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6360 work->remote_key = le32_to_cpu(desc->token);
6361
Namjae Jeon79f6b112021-04-02 12:47:14 +09006362 data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006363 if (!data_buf)
6364 return -ENOMEM;
6365
6366 ret = ksmbd_conn_rdma_read(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006367 le32_to_cpu(desc->token),
6368 le64_to_cpu(desc->offset),
6369 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006370 if (ret < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09006371 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006372 return ret;
6373 }
6374
Namjae Jeon64b39f42021-03-30 14:25:35 +09006375 ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006376 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006377 if (ret < 0)
6378 return ret;
6379
6380 return nbytes;
6381}
6382
6383/**
6384 * smb2_write() - handler for smb2 write from file
6385 * @work: smb work containing write command buffer
6386 *
6387 * Return: 0 on success, otherwise error
6388 */
6389int smb2_write(struct ksmbd_work *work)
6390{
6391 struct smb2_write_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006392 struct smb2_write_rsp *rsp;
Namjae Jeonbcd62a32021-05-10 09:08:19 +09006393 struct ksmbd_file *fp = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006394 loff_t offset;
6395 size_t length;
6396 ssize_t nbytes;
6397 char *data_buf;
6398 bool writethrough = false;
6399 int err = 0;
6400
Namjae Jeone2f34482021-03-16 10:49:09 +09006401 WORK_BUFFERS(work, req, rsp);
6402
Namjae Jeon64b39f42021-03-30 14:25:35 +09006403 if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006404 ksmbd_debug(SMB, "IPC pipe write request\n");
6405 return smb2_write_pipe(work);
6406 }
6407
6408 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
6409 ksmbd_debug(SMB, "User does not have write permission\n");
6410 err = -EACCES;
6411 goto out;
6412 }
6413
Namjae Jeon64b39f42021-03-30 14:25:35 +09006414 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
Namjae Jeon070fb212021-05-26 17:57:12 +09006415 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006416 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006417 err = -ENOENT;
6418 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006419 }
6420
6421 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006422 pr_err("Not permitted to write : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09006423 err = -EACCES;
6424 goto out;
6425 }
6426
6427 offset = le64_to_cpu(req->Offset);
6428 length = le32_to_cpu(req->Length);
6429
6430 if (length > work->conn->vals->max_write_size) {
6431 ksmbd_debug(SMB, "limiting write size to max size(%u)\n",
6432 work->conn->vals->max_write_size);
6433 err = -EINVAL;
6434 goto out;
6435 }
6436
6437 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6438 writethrough = true;
6439
6440 if (req->Channel != SMB2_CHANNEL_RDMA_V1 &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006441 req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006442 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeoncb451722021-11-03 08:08:44 +09006443 offsetof(struct smb2_write_req, Buffer)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006444 data_buf = (char *)&req->Buffer[0];
6445 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09006446 if ((u64)le16_to_cpu(req->DataOffset) + length >
6447 get_rfc1002_len(work->request_buf)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006448 pr_err("invalid write data offset %u, smb_len %u\n",
6449 le16_to_cpu(req->DataOffset),
Namjae Jeoncb451722021-11-03 08:08:44 +09006450 get_rfc1002_len(work->request_buf));
Namjae Jeone2f34482021-03-16 10:49:09 +09006451 err = -EINVAL;
6452 goto out;
6453 }
6454
6455 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6456 le16_to_cpu(req->DataOffset));
6457 }
6458
6459 ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags));
6460 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6461 writethrough = true;
6462
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006463 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6464 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006465 err = ksmbd_vfs_write(work, fp, data_buf, length, &offset,
6466 writethrough, &nbytes);
6467 if (err < 0)
6468 goto out;
6469 } else {
6470 /* read data from the client using rdma channel, and
6471 * write the data.
6472 */
6473 nbytes = smb2_write_rdma_channel(work, req, fp, offset,
Namjae Jeon070fb212021-05-26 17:57:12 +09006474 le32_to_cpu(req->RemainingBytes),
6475 writethrough);
Namjae Jeone2f34482021-03-16 10:49:09 +09006476 if (nbytes < 0) {
6477 err = (int)nbytes;
6478 goto out;
6479 }
6480 }
6481
6482 rsp->StructureSize = cpu_to_le16(17);
6483 rsp->DataOffset = 0;
6484 rsp->Reserved = 0;
6485 rsp->DataLength = cpu_to_le32(nbytes);
6486 rsp->DataRemaining = 0;
6487 rsp->Reserved2 = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006488 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006489 ksmbd_fd_put(work, fp);
6490 return 0;
6491
6492out:
6493 if (err == -EAGAIN)
6494 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6495 else if (err == -ENOSPC || err == -EFBIG)
6496 rsp->hdr.Status = STATUS_DISK_FULL;
6497 else if (err == -ENOENT)
6498 rsp->hdr.Status = STATUS_FILE_CLOSED;
6499 else if (err == -EACCES)
6500 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6501 else if (err == -ESHARE)
6502 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6503 else if (err == -EINVAL)
6504 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6505 else
6506 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6507
6508 smb2_set_err_rsp(work);
6509 ksmbd_fd_put(work, fp);
6510 return err;
6511}
6512
6513/**
6514 * smb2_flush() - handler for smb2 flush file - fsync
6515 * @work: smb work containing flush command buffer
6516 *
6517 * Return: 0 on success, otherwise error
6518 */
6519int smb2_flush(struct ksmbd_work *work)
6520{
6521 struct smb2_flush_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006522 struct smb2_flush_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09006523 int err;
6524
Namjae Jeone2f34482021-03-16 10:49:09 +09006525 WORK_BUFFERS(work, req, rsp);
6526
6527 ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006528 le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006529
6530 err = ksmbd_vfs_fsync(work,
6531 le64_to_cpu(req->VolatileFileId),
6532 le64_to_cpu(req->PersistentFileId));
6533 if (err)
6534 goto out;
6535
6536 rsp->StructureSize = cpu_to_le16(4);
6537 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006538 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09006539 return 0;
6540
6541out:
6542 if (err) {
6543 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6544 smb2_set_err_rsp(work);
6545 }
6546
6547 return err;
6548}
6549
6550/**
6551 * smb2_cancel() - handler for smb2 cancel command
6552 * @work: smb work containing cancel command buffer
6553 *
6554 * Return: 0 on success, otherwise error
6555 */
6556int smb2_cancel(struct ksmbd_work *work)
6557{
6558 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09006559 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006560 struct smb2_hdr *chdr;
6561 struct ksmbd_work *cancel_work = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006562 int canceled = 0;
6563 struct list_head *command_list;
6564
6565 ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006566 hdr->MessageId, hdr->Flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09006567
6568 if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) {
6569 command_list = &conn->async_requests;
6570
6571 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006572 list_for_each_entry(cancel_work, command_list,
6573 async_request_entry) {
Namjae Jeoncb451722021-11-03 08:08:44 +09006574 chdr = smb2_get_msg(cancel_work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006575
6576 if (cancel_work->async_id !=
Namjae Jeon64b39f42021-03-30 14:25:35 +09006577 le64_to_cpu(hdr->Id.AsyncId))
Namjae Jeone2f34482021-03-16 10:49:09 +09006578 continue;
6579
6580 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006581 "smb2 with AsyncId %llu cancelled command = 0x%x\n",
6582 le64_to_cpu(hdr->Id.AsyncId),
6583 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006584 canceled = 1;
6585 break;
6586 }
6587 spin_unlock(&conn->request_lock);
6588 } else {
6589 command_list = &conn->requests;
6590
6591 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006592 list_for_each_entry(cancel_work, command_list, request_entry) {
Namjae Jeoncb451722021-11-03 08:08:44 +09006593 chdr = smb2_get_msg(cancel_work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006594
6595 if (chdr->MessageId != hdr->MessageId ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006596 cancel_work == work)
Namjae Jeone2f34482021-03-16 10:49:09 +09006597 continue;
6598
6599 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006600 "smb2 with mid %llu cancelled command = 0x%x\n",
6601 le64_to_cpu(hdr->MessageId),
6602 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006603 canceled = 1;
6604 break;
6605 }
6606 spin_unlock(&conn->request_lock);
6607 }
6608
6609 if (canceled) {
6610 cancel_work->state = KSMBD_WORK_CANCELLED;
6611 if (cancel_work->cancel_fn)
6612 cancel_work->cancel_fn(cancel_work->cancel_argv);
6613 }
6614
6615 /* For SMB2_CANCEL command itself send no response*/
6616 work->send_no_response = 1;
6617 return 0;
6618}
6619
6620struct file_lock *smb_flock_init(struct file *f)
6621{
6622 struct file_lock *fl;
6623
6624 fl = locks_alloc_lock();
6625 if (!fl)
6626 goto out;
6627
6628 locks_init_lock(fl);
6629
6630 fl->fl_owner = f;
6631 fl->fl_pid = current->tgid;
6632 fl->fl_file = f;
6633 fl->fl_flags = FL_POSIX;
6634 fl->fl_ops = NULL;
6635 fl->fl_lmops = NULL;
6636
6637out:
6638 return fl;
6639}
6640
6641static int smb2_set_flock_flags(struct file_lock *flock, int flags)
6642{
6643 int cmd = -EINVAL;
6644
6645 /* Checking for wrong flag combination during lock request*/
6646 switch (flags) {
6647 case SMB2_LOCKFLAG_SHARED:
6648 ksmbd_debug(SMB, "received shared request\n");
6649 cmd = F_SETLKW;
6650 flock->fl_type = F_RDLCK;
6651 flock->fl_flags |= FL_SLEEP;
6652 break;
6653 case SMB2_LOCKFLAG_EXCLUSIVE:
6654 ksmbd_debug(SMB, "received exclusive request\n");
6655 cmd = F_SETLKW;
6656 flock->fl_type = F_WRLCK;
6657 flock->fl_flags |= FL_SLEEP;
6658 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006659 case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006660 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006661 "received shared & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006662 cmd = F_SETLK;
6663 flock->fl_type = F_RDLCK;
6664 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006665 case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006666 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006667 "received exclusive & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006668 cmd = F_SETLK;
6669 flock->fl_type = F_WRLCK;
6670 break;
6671 case SMB2_LOCKFLAG_UNLOCK:
6672 ksmbd_debug(SMB, "received unlock request\n");
6673 flock->fl_type = F_UNLCK;
6674 cmd = 0;
6675 break;
6676 }
6677
6678 return cmd;
6679}
6680
6681static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock,
Namjae Jeon070fb212021-05-26 17:57:12 +09006682 unsigned int cmd, int flags,
6683 struct list_head *lock_list)
Namjae Jeone2f34482021-03-16 10:49:09 +09006684{
6685 struct ksmbd_lock *lock;
6686
6687 lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL);
6688 if (!lock)
6689 return NULL;
6690
6691 lock->cmd = cmd;
6692 lock->fl = flock;
6693 lock->start = flock->fl_start;
6694 lock->end = flock->fl_end;
6695 lock->flags = flags;
6696 if (lock->start == lock->end)
6697 lock->zero_len = 1;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006698 INIT_LIST_HEAD(&lock->clist);
6699 INIT_LIST_HEAD(&lock->flist);
Namjae Jeone2f34482021-03-16 10:49:09 +09006700 INIT_LIST_HEAD(&lock->llist);
Namjae Jeone2f34482021-03-16 10:49:09 +09006701 list_add_tail(&lock->llist, lock_list);
6702
6703 return lock;
6704}
6705
6706static void smb2_remove_blocked_lock(void **argv)
6707{
6708 struct file_lock *flock = (struct file_lock *)argv[0];
6709
6710 ksmbd_vfs_posix_lock_unblock(flock);
6711 wake_up(&flock->fl_wait);
6712}
6713
6714static inline bool lock_defer_pending(struct file_lock *fl)
6715{
6716 /* check pending lock waiters */
6717 return waitqueue_active(&fl->fl_wait);
6718}
6719
6720/**
6721 * smb2_lock() - handler for smb2 file lock command
6722 * @work: smb work containing lock command buffer
6723 *
6724 * Return: 0 on success, otherwise error
6725 */
6726int smb2_lock(struct ksmbd_work *work)
6727{
Namjae Jeoncb451722021-11-03 08:08:44 +09006728 struct smb2_lock_req *req = smb2_get_msg(work->request_buf);
6729 struct smb2_lock_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006730 struct smb2_lock_element *lock_ele;
6731 struct ksmbd_file *fp = NULL;
6732 struct file_lock *flock = NULL;
6733 struct file *filp = NULL;
6734 int lock_count;
6735 int flags = 0;
6736 int cmd = 0;
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006737 int err = -EIO, i, rc = 0;
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006738 u64 lock_start, lock_length;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006739 struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2;
6740 struct ksmbd_conn *conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09006741 int nolock = 0;
6742 LIST_HEAD(lock_list);
6743 LIST_HEAD(rollback_list);
6744 int prior_lock = 0;
6745
6746 ksmbd_debug(SMB, "Received lock request\n");
6747 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006748 le64_to_cpu(req->VolatileFileId),
6749 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006750 if (!fp) {
6751 ksmbd_debug(SMB, "Invalid file id for lock : %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006752 le64_to_cpu(req->VolatileFileId));
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006753 err = -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09006754 goto out2;
6755 }
6756
6757 filp = fp->filp;
6758 lock_count = le16_to_cpu(req->LockCount);
6759 lock_ele = req->locks;
6760
6761 ksmbd_debug(SMB, "lock count is %d\n", lock_count);
Namjae Jeon070fb212021-05-26 17:57:12 +09006762 if (!lock_count) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006763 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006764 goto out2;
6765 }
6766
6767 for (i = 0; i < lock_count; i++) {
6768 flags = le32_to_cpu(lock_ele[i].Flags);
6769
6770 flock = smb_flock_init(filp);
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006771 if (!flock)
Namjae Jeone2f34482021-03-16 10:49:09 +09006772 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006773
6774 cmd = smb2_set_flock_flags(flock, flags);
6775
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006776 lock_start = le64_to_cpu(lock_ele[i].Offset);
6777 lock_length = le64_to_cpu(lock_ele[i].Length);
6778 if (lock_start > U64_MAX - lock_length) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006779 pr_err("Invalid lock range requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006780 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6781 goto out;
6782 }
6783
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006784 if (lock_start > OFFSET_MAX)
6785 flock->fl_start = OFFSET_MAX;
6786 else
6787 flock->fl_start = lock_start;
6788
Namjae Jeone2f34482021-03-16 10:49:09 +09006789 lock_length = le64_to_cpu(lock_ele[i].Length);
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006790 if (lock_length > OFFSET_MAX - flock->fl_start)
6791 lock_length = OFFSET_MAX - flock->fl_start;
Namjae Jeone2f34482021-03-16 10:49:09 +09006792
6793 flock->fl_end = flock->fl_start + lock_length;
6794
6795 if (flock->fl_end < flock->fl_start) {
6796 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006797 "the end offset(%llx) is smaller than the start offset(%llx)\n",
6798 flock->fl_end, flock->fl_start);
Namjae Jeone2f34482021-03-16 10:49:09 +09006799 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6800 goto out;
6801 }
6802
6803 /* Check conflict locks in one request */
6804 list_for_each_entry(cmp_lock, &lock_list, llist) {
6805 if (cmp_lock->fl->fl_start <= flock->fl_start &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006806 cmp_lock->fl->fl_end >= flock->fl_end) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006807 if (cmp_lock->fl->fl_type != F_UNLCK &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006808 flock->fl_type != F_UNLCK) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006809 pr_err("conflict two locks in one request\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006810 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006811 goto out;
6812 }
6813 }
6814 }
6815
6816 smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list);
6817 if (!smb_lock) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006818 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006819 goto out;
6820 }
6821 }
6822
6823 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6824 if (smb_lock->cmd < 0) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006825 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006826 goto out;
6827 }
6828
6829 if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006830 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006831 goto out;
6832 }
6833
Namjae Jeon64b39f42021-03-30 14:25:35 +09006834 if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) &&
6835 smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) ||
6836 (prior_lock == SMB2_LOCKFLAG_UNLOCK &&
6837 !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006838 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006839 goto out;
6840 }
6841
6842 prior_lock = smb_lock->flags;
6843
6844 if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006845 !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY))
Hyunchul Leed63528e2021-07-10 16:22:41 +09006846 goto no_check_cl;
Namjae Jeone2f34482021-03-16 10:49:09 +09006847
6848 nolock = 1;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006849 /* check locks in connection list */
6850 read_lock(&conn_list_lock);
6851 list_for_each_entry(conn, &conn_list, conns_list) {
6852 spin_lock(&conn->llist_lock);
6853 list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) {
6854 if (file_inode(cmp_lock->fl->fl_file) !=
6855 file_inode(smb_lock->fl->fl_file))
6856 continue;
Namjae Jeone2f34482021-03-16 10:49:09 +09006857
Hyunchul Leed63528e2021-07-10 16:22:41 +09006858 if (smb_lock->fl->fl_type == F_UNLCK) {
6859 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file &&
6860 cmp_lock->start == smb_lock->start &&
6861 cmp_lock->end == smb_lock->end &&
6862 !lock_defer_pending(cmp_lock->fl)) {
6863 nolock = 0;
6864 list_del(&cmp_lock->flist);
6865 list_del(&cmp_lock->clist);
6866 spin_unlock(&conn->llist_lock);
6867 read_unlock(&conn_list_lock);
6868
6869 locks_free_lock(cmp_lock->fl);
6870 kfree(cmp_lock);
6871 goto out_check_cl;
6872 }
6873 continue;
Namjae Jeone2f34482021-03-16 10:49:09 +09006874 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006875
Hyunchul Leed63528e2021-07-10 16:22:41 +09006876 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) {
6877 if (smb_lock->flags & SMB2_LOCKFLAG_SHARED)
6878 continue;
6879 } else {
6880 if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED)
6881 continue;
6882 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006883
Hyunchul Leed63528e2021-07-10 16:22:41 +09006884 /* check zero byte lock range */
6885 if (cmp_lock->zero_len && !smb_lock->zero_len &&
6886 cmp_lock->start > smb_lock->start &&
6887 cmp_lock->start < smb_lock->end) {
6888 spin_unlock(&conn->llist_lock);
6889 read_unlock(&conn_list_lock);
6890 pr_err("previous lock conflict with zero byte lock range\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006891 goto out;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006892 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006893
Hyunchul Leed63528e2021-07-10 16:22:41 +09006894 if (smb_lock->zero_len && !cmp_lock->zero_len &&
6895 smb_lock->start > cmp_lock->start &&
6896 smb_lock->start < cmp_lock->end) {
6897 spin_unlock(&conn->llist_lock);
6898 read_unlock(&conn_list_lock);
6899 pr_err("current lock conflict with zero byte lock range\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006900 goto out;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006901 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006902
Hyunchul Leed63528e2021-07-10 16:22:41 +09006903 if (((cmp_lock->start <= smb_lock->start &&
6904 cmp_lock->end > smb_lock->start) ||
6905 (cmp_lock->start < smb_lock->end &&
6906 cmp_lock->end >= smb_lock->end)) &&
6907 !cmp_lock->zero_len && !smb_lock->zero_len) {
6908 spin_unlock(&conn->llist_lock);
6909 read_unlock(&conn_list_lock);
6910 pr_err("Not allow lock operation on exclusive lock range\n");
Hyunchul Leed63528e2021-07-10 16:22:41 +09006911 goto out;
6912 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006913 }
Hyunchul Leed63528e2021-07-10 16:22:41 +09006914 spin_unlock(&conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006915 }
Hyunchul Leed63528e2021-07-10 16:22:41 +09006916 read_unlock(&conn_list_lock);
6917out_check_cl:
Namjae Jeone2f34482021-03-16 10:49:09 +09006918 if (smb_lock->fl->fl_type == F_UNLCK && nolock) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006919 pr_err("Try to unlock nolocked range\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006920 rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED;
6921 goto out;
6922 }
6923
Hyunchul Leed63528e2021-07-10 16:22:41 +09006924no_check_cl:
Namjae Jeone2f34482021-03-16 10:49:09 +09006925 if (smb_lock->zero_len) {
6926 err = 0;
6927 goto skip;
6928 }
6929
6930 flock = smb_lock->fl;
6931 list_del(&smb_lock->llist);
6932retry:
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006933 rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09006934skip:
6935 if (flags & SMB2_LOCKFLAG_UNLOCK) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006936 if (!rc) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006937 ksmbd_debug(SMB, "File unlocked\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006938 } else if (rc == -ENOENT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006939 rsp->hdr.Status = STATUS_NOT_LOCKED;
6940 goto out;
6941 }
6942 locks_free_lock(flock);
6943 kfree(smb_lock);
6944 } else {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006945 if (rc == FILE_LOCK_DEFERRED) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006946 void **argv;
6947
6948 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006949 "would have to wait for getting lock\n");
Hyunchul Leed63528e2021-07-10 16:22:41 +09006950 spin_lock(&work->conn->llist_lock);
6951 list_add_tail(&smb_lock->clist,
6952 &work->conn->lock_list);
6953 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006954 list_add(&smb_lock->llist, &rollback_list);
6955
6956 argv = kmalloc(sizeof(void *), GFP_KERNEL);
6957 if (!argv) {
6958 err = -ENOMEM;
6959 goto out;
6960 }
6961 argv[0] = flock;
6962
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006963 rc = setup_async_work(work,
6964 smb2_remove_blocked_lock,
6965 argv);
6966 if (rc) {
6967 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09006968 goto out;
6969 }
6970 spin_lock(&fp->f_lock);
6971 list_add(&work->fp_entry, &fp->blocked_works);
6972 spin_unlock(&fp->f_lock);
6973
6974 smb2_send_interim_resp(work, STATUS_PENDING);
6975
Hyunchul Lee45a64e82021-07-10 09:34:20 +09006976 ksmbd_vfs_posix_lock_wait(flock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006977
Hyunchul Leed4075ab2021-06-25 07:02:10 +09006978 if (work->state != KSMBD_WORK_ACTIVE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006979 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09006980 spin_lock(&work->conn->llist_lock);
6981 list_del(&smb_lock->clist);
6982 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006983 locks_free_lock(flock);
6984
Hyunchul Leed4075ab2021-06-25 07:02:10 +09006985 if (work->state == KSMBD_WORK_CANCELLED) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006986 spin_lock(&fp->f_lock);
6987 list_del(&work->fp_entry);
6988 spin_unlock(&fp->f_lock);
6989 rsp->hdr.Status =
6990 STATUS_CANCELLED;
6991 kfree(smb_lock);
6992 smb2_send_interim_resp(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006993 STATUS_CANCELLED);
Namjae Jeone2f34482021-03-16 10:49:09 +09006994 work->send_no_response = 1;
6995 goto out;
6996 }
6997 init_smb2_rsp_hdr(work);
6998 smb2_set_err_rsp(work);
6999 rsp->hdr.Status =
7000 STATUS_RANGE_NOT_LOCKED;
7001 kfree(smb_lock);
7002 goto out2;
7003 }
7004
7005 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007006 spin_lock(&work->conn->llist_lock);
7007 list_del(&smb_lock->clist);
7008 spin_unlock(&work->conn->llist_lock);
7009
Namjae Jeone2f34482021-03-16 10:49:09 +09007010 spin_lock(&fp->f_lock);
7011 list_del(&work->fp_entry);
7012 spin_unlock(&fp->f_lock);
7013 goto retry;
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09007014 } else if (!rc) {
Hyunchul Leed63528e2021-07-10 16:22:41 +09007015 spin_lock(&work->conn->llist_lock);
7016 list_add_tail(&smb_lock->clist,
7017 &work->conn->lock_list);
7018 list_add_tail(&smb_lock->flist,
7019 &fp->lock_list);
7020 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09007021 list_add(&smb_lock->llist, &rollback_list);
7022 ksmbd_debug(SMB, "successful in taking lock\n");
7023 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007024 goto out;
7025 }
7026 }
7027 }
7028
7029 if (atomic_read(&fp->f_ci->op_count) > 1)
7030 smb_break_all_oplock(work, fp);
7031
7032 rsp->StructureSize = cpu_to_le16(4);
7033 ksmbd_debug(SMB, "successful in taking lock\n");
7034 rsp->hdr.Status = STATUS_SUCCESS;
7035 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09007036 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09007037 ksmbd_fd_put(work, fp);
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007038 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09007039
7040out:
7041 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
7042 locks_free_lock(smb_lock->fl);
7043 list_del(&smb_lock->llist);
7044 kfree(smb_lock);
7045 }
7046
7047 list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) {
7048 struct file_lock *rlock = NULL;
7049
7050 rlock = smb_flock_init(filp);
7051 rlock->fl_type = F_UNLCK;
7052 rlock->fl_start = smb_lock->start;
7053 rlock->fl_end = smb_lock->end;
7054
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007055 rc = vfs_lock_file(filp, 0, rlock, NULL);
7056 if (rc)
7057 pr_err("rollback unlock fail : %d\n", rc);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007058
Namjae Jeone2f34482021-03-16 10:49:09 +09007059 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007060 spin_lock(&work->conn->llist_lock);
7061 if (!list_empty(&smb_lock->flist))
7062 list_del(&smb_lock->flist);
7063 list_del(&smb_lock->clist);
7064 spin_unlock(&work->conn->llist_lock);
7065
Namjae Jeone2f34482021-03-16 10:49:09 +09007066 locks_free_lock(smb_lock->fl);
7067 locks_free_lock(rlock);
7068 kfree(smb_lock);
7069 }
7070out2:
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09007071 ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err);
7072
7073 if (!rsp->hdr.Status) {
7074 if (err == -EINVAL)
7075 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7076 else if (err == -ENOMEM)
7077 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
7078 else if (err == -ENOENT)
7079 rsp->hdr.Status = STATUS_FILE_CLOSED;
7080 else
7081 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
7082 }
7083
Namjae Jeone2f34482021-03-16 10:49:09 +09007084 smb2_set_err_rsp(work);
7085 ksmbd_fd_put(work, fp);
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007086 return err;
Namjae Jeone2f34482021-03-16 10:49:09 +09007087}
7088
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007089static int fsctl_copychunk(struct ksmbd_work *work,
7090 struct copychunk_ioctl_req *ci_req,
7091 unsigned int cnt_code,
7092 unsigned int input_count,
7093 unsigned long long volatile_id,
7094 unsigned long long persistent_id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007095 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007096{
Namjae Jeone2f34482021-03-16 10:49:09 +09007097 struct copychunk_ioctl_rsp *ci_rsp;
7098 struct ksmbd_file *src_fp = NULL, *dst_fp = NULL;
7099 struct srv_copychunk *chunks;
7100 unsigned int i, chunk_count, chunk_count_written = 0;
7101 unsigned int chunk_size_written = 0;
7102 loff_t total_size_written = 0;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007103 int ret = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09007104
Namjae Jeone2f34482021-03-16 10:49:09 +09007105 ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0];
7106
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007107 rsp->VolatileFileId = cpu_to_le64(volatile_id);
7108 rsp->PersistentFileId = cpu_to_le64(persistent_id);
Namjae Jeon64b39f42021-03-30 14:25:35 +09007109 ci_rsp->ChunksWritten =
7110 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count());
7111 ci_rsp->ChunkBytesWritten =
7112 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size());
7113 ci_rsp->TotalBytesWritten =
7114 cpu_to_le32(ksmbd_server_side_copy_max_total_size());
Namjae Jeone2f34482021-03-16 10:49:09 +09007115
7116 chunks = (struct srv_copychunk *)&ci_req->Chunks[0];
7117 chunk_count = le32_to_cpu(ci_req->ChunkCount);
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007118 if (chunk_count == 0)
7119 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09007120 total_size_written = 0;
7121
7122 /* verify the SRV_COPYCHUNK_COPY packet */
7123 if (chunk_count > ksmbd_server_side_copy_max_chunk_count() ||
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007124 input_count < offsetof(struct copychunk_ioctl_req, Chunks) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09007125 chunk_count * sizeof(struct srv_copychunk)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007126 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7127 return -EINVAL;
7128 }
7129
7130 for (i = 0; i < chunk_count; i++) {
7131 if (le32_to_cpu(chunks[i].Length) == 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09007132 le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size())
Namjae Jeone2f34482021-03-16 10:49:09 +09007133 break;
7134 total_size_written += le32_to_cpu(chunks[i].Length);
7135 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09007136
7137 if (i < chunk_count ||
7138 total_size_written > ksmbd_server_side_copy_max_total_size()) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007139 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7140 return -EINVAL;
7141 }
7142
7143 src_fp = ksmbd_lookup_foreign_fd(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007144 le64_to_cpu(ci_req->ResumeKey[0]));
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007145 dst_fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
Namjae Jeone2f34482021-03-16 10:49:09 +09007146 ret = -EINVAL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007147 if (!src_fp ||
7148 src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007149 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7150 goto out;
7151 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09007152
Namjae Jeone2f34482021-03-16 10:49:09 +09007153 if (!dst_fp) {
7154 rsp->hdr.Status = STATUS_FILE_CLOSED;
7155 goto out;
7156 }
7157
7158 /*
7159 * FILE_READ_DATA should only be included in
7160 * the FSCTL_COPYCHUNK case
7161 */
Namjae Jeon070fb212021-05-26 17:57:12 +09007162 if (cnt_code == FSCTL_COPYCHUNK &&
7163 !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007164 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7165 goto out;
7166 }
7167
7168 ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007169 chunks, chunk_count,
7170 &chunk_count_written,
7171 &chunk_size_written,
7172 &total_size_written);
Namjae Jeone2f34482021-03-16 10:49:09 +09007173 if (ret < 0) {
7174 if (ret == -EACCES)
7175 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7176 if (ret == -EAGAIN)
7177 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
7178 else if (ret == -EBADF)
7179 rsp->hdr.Status = STATUS_INVALID_HANDLE;
7180 else if (ret == -EFBIG || ret == -ENOSPC)
7181 rsp->hdr.Status = STATUS_DISK_FULL;
7182 else if (ret == -EINVAL)
7183 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7184 else if (ret == -EISDIR)
7185 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
7186 else if (ret == -E2BIG)
7187 rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE;
7188 else
7189 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
7190 }
7191
7192 ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written);
7193 ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written);
7194 ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written);
7195out:
7196 ksmbd_fd_put(work, src_fp);
7197 ksmbd_fd_put(work, dst_fp);
7198 return ret;
7199}
7200
7201static __be32 idev_ipv4_address(struct in_device *idev)
7202{
7203 __be32 addr = 0;
7204
7205 struct in_ifaddr *ifa;
7206
7207 rcu_read_lock();
7208 in_dev_for_each_ifa_rcu(ifa, idev) {
7209 if (ifa->ifa_flags & IFA_F_SECONDARY)
7210 continue;
7211
7212 addr = ifa->ifa_address;
7213 break;
7214 }
7215 rcu_read_unlock();
7216 return addr;
7217}
7218
7219static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007220 struct smb2_ioctl_rsp *rsp,
7221 unsigned int out_buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09007222{
7223 struct network_interface_info_ioctl_rsp *nii_rsp = NULL;
7224 int nbytes = 0;
7225 struct net_device *netdev;
7226 struct sockaddr_storage_rsp *sockaddr_storage;
7227 unsigned int flags;
7228 unsigned long long speed;
Namjae Jeonf1abdb72021-07-27 13:25:13 +09007229 struct sockaddr_in6 *csin6 = (struct sockaddr_in6 *)&conn->peer_addr;
Namjae Jeone2f34482021-03-16 10:49:09 +09007230
7231 rtnl_lock();
7232 for_each_netdev(&init_net, netdev) {
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007233 if (out_buf_len <
7234 nbytes + sizeof(struct network_interface_info_ioctl_rsp)) {
7235 rtnl_unlock();
7236 return -ENOSPC;
7237 }
7238
Namjae Jeone2f34482021-03-16 10:49:09 +09007239 if (netdev->type == ARPHRD_LOOPBACK)
7240 continue;
7241
7242 flags = dev_get_flags(netdev);
7243 if (!(flags & IFF_RUNNING))
7244 continue;
7245
7246 nii_rsp = (struct network_interface_info_ioctl_rsp *)
7247 &rsp->Buffer[nbytes];
7248 nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex);
7249
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09007250 nii_rsp->Capability = 0;
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09007251 if (ksmbd_rdma_capable_netdev(netdev))
7252 nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE);
Namjae Jeone2f34482021-03-16 10:49:09 +09007253
7254 nii_rsp->Next = cpu_to_le32(152);
7255 nii_rsp->Reserved = 0;
7256
7257 if (netdev->ethtool_ops->get_link_ksettings) {
7258 struct ethtool_link_ksettings cmd;
7259
7260 netdev->ethtool_ops->get_link_ksettings(netdev, &cmd);
7261 speed = cmd.base.speed;
7262 } else {
Per Forlind4758662021-08-30 13:23:04 +09007263 ksmbd_debug(SMB, "%s %s\n", netdev->name,
7264 "speed is unknown, defaulting to 1Gb/sec");
Namjae Jeone2f34482021-03-16 10:49:09 +09007265 speed = SPEED_1000;
7266 }
7267
7268 speed *= 1000000;
7269 nii_rsp->LinkSpeed = cpu_to_le64(speed);
7270
7271 sockaddr_storage = (struct sockaddr_storage_rsp *)
7272 nii_rsp->SockAddr_Storage;
7273 memset(sockaddr_storage, 0, 128);
7274
Namjae Jeonf1abdb72021-07-27 13:25:13 +09007275 if (conn->peer_addr.ss_family == PF_INET ||
7276 ipv6_addr_v4mapped(&csin6->sin6_addr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007277 struct in_device *idev;
7278
7279 sockaddr_storage->Family = cpu_to_le16(INTERNETWORK);
7280 sockaddr_storage->addr4.Port = 0;
7281
7282 idev = __in_dev_get_rtnl(netdev);
7283 if (!idev)
7284 continue;
7285 sockaddr_storage->addr4.IPv4address =
7286 idev_ipv4_address(idev);
7287 } else {
7288 struct inet6_dev *idev6;
7289 struct inet6_ifaddr *ifa;
7290 __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address;
7291
7292 sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6);
7293 sockaddr_storage->addr6.Port = 0;
7294 sockaddr_storage->addr6.FlowInfo = 0;
7295
7296 idev6 = __in6_dev_get(netdev);
7297 if (!idev6)
7298 continue;
7299
7300 list_for_each_entry(ifa, &idev6->addr_list, if_list) {
7301 if (ifa->flags & (IFA_F_TENTATIVE |
7302 IFA_F_DEPRECATED))
7303 continue;
7304 memcpy(ipv6_addr, ifa->addr.s6_addr, 16);
7305 break;
7306 }
7307 sockaddr_storage->addr6.ScopeId = 0;
7308 }
7309
7310 nbytes += sizeof(struct network_interface_info_ioctl_rsp);
7311 }
7312 rtnl_unlock();
7313
7314 /* zero if this is last one */
7315 if (nii_rsp)
7316 nii_rsp->Next = 0;
7317
Namjae Jeone2f34482021-03-16 10:49:09 +09007318 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7319 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7320 return nbytes;
7321}
7322
Namjae Jeone2f34482021-03-16 10:49:09 +09007323static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09007324 struct validate_negotiate_info_req *neg_req,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007325 struct validate_negotiate_info_rsp *neg_rsp,
7326 unsigned int in_buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09007327{
7328 int ret = 0;
7329 int dialect;
7330
Marios Makassikis78f16882021-10-28 21:01:27 +02007331 if (in_buf_len < offsetof(struct validate_negotiate_info_req, Dialects) +
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007332 le16_to_cpu(neg_req->DialectCount) * sizeof(__le16))
7333 return -EINVAL;
7334
Namjae Jeone2f34482021-03-16 10:49:09 +09007335 dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects,
Namjae Jeon070fb212021-05-26 17:57:12 +09007336 neg_req->DialectCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09007337 if (dialect == BAD_PROT_ID || dialect != conn->dialect) {
7338 ret = -EINVAL;
7339 goto err_out;
7340 }
7341
7342 if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) {
7343 ret = -EINVAL;
7344 goto err_out;
7345 }
7346
7347 if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) {
7348 ret = -EINVAL;
7349 goto err_out;
7350 }
7351
7352 if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) {
7353 ret = -EINVAL;
7354 goto err_out;
7355 }
7356
7357 neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
7358 memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE);
7359 neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode);
7360 neg_rsp->Dialect = cpu_to_le16(conn->dialect);
7361err_out:
7362 return ret;
7363}
7364
Namjae Jeon64b39f42021-03-30 14:25:35 +09007365static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007366 struct file_allocated_range_buffer *qar_req,
7367 struct file_allocated_range_buffer *qar_rsp,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007368 unsigned int in_count, unsigned int *out_count)
Namjae Jeone2f34482021-03-16 10:49:09 +09007369{
7370 struct ksmbd_file *fp;
7371 loff_t start, length;
7372 int ret = 0;
7373
7374 *out_count = 0;
7375 if (in_count == 0)
7376 return -EINVAL;
7377
7378 fp = ksmbd_lookup_fd_fast(work, id);
7379 if (!fp)
7380 return -ENOENT;
7381
7382 start = le64_to_cpu(qar_req->file_offset);
7383 length = le64_to_cpu(qar_req->length);
7384
7385 ret = ksmbd_vfs_fqar_lseek(fp, start, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09007386 qar_rsp, in_count, out_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09007387 if (ret && ret != -E2BIG)
7388 *out_count = 0;
7389
7390 ksmbd_fd_put(work, fp);
7391 return ret;
7392}
7393
Namjae Jeon64b39f42021-03-30 14:25:35 +09007394static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007395 unsigned int out_buf_len,
7396 struct smb2_ioctl_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007397 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007398{
7399 struct ksmbd_rpc_command *rpc_resp;
7400 char *data_buf = (char *)&req->Buffer[0];
7401 int nbytes = 0;
7402
Namjae Jeon64b39f42021-03-30 14:25:35 +09007403 rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09007404 le32_to_cpu(req->InputCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09007405 if (rpc_resp) {
7406 if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) {
7407 /*
7408 * set STATUS_SOME_NOT_MAPPED response
7409 * for unknown domain sid.
7410 */
7411 rsp->hdr.Status = STATUS_SOME_NOT_MAPPED;
7412 } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
7413 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7414 goto out;
7415 } else if (rpc_resp->flags != KSMBD_RPC_OK) {
7416 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7417 goto out;
7418 }
7419
7420 nbytes = rpc_resp->payload_sz;
7421 if (rpc_resp->payload_sz > out_buf_len) {
7422 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7423 nbytes = out_buf_len;
7424 }
7425
7426 if (!rpc_resp->payload_sz) {
7427 rsp->hdr.Status =
7428 STATUS_UNEXPECTED_IO_ERROR;
7429 goto out;
7430 }
7431
7432 memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes);
7433 }
7434out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09007435 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007436 return nbytes;
7437}
7438
Namjae Jeon64b39f42021-03-30 14:25:35 +09007439static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007440 struct file_sparse *sparse)
Namjae Jeone2f34482021-03-16 10:49:09 +09007441{
7442 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007443 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09007444 int ret = 0;
7445 __le32 old_fattr;
7446
7447 fp = ksmbd_lookup_fd_fast(work, id);
7448 if (!fp)
7449 return -ENOENT;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007450 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007451
7452 old_fattr = fp->f_ci->m_fattr;
7453 if (sparse->SetSparse)
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09007454 fp->f_ci->m_fattr |= FILE_ATTRIBUTE_SPARSE_FILE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09007455 else
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09007456 fp->f_ci->m_fattr &= ~FILE_ATTRIBUTE_SPARSE_FILE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09007457
7458 if (fp->f_ci->m_fattr != old_fattr &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007459 test_share_config_flag(work->tcon->share_conf,
7460 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007461 struct xattr_dos_attrib da;
7462
Hyunchul Lee465d7202021-07-03 12:10:36 +09007463 ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007464 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007465 if (ret <= 0)
7466 goto out;
7467
7468 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
Hyunchul Lee465d7202021-07-03 12:10:36 +09007469 ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007470 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007471 if (ret)
7472 fp->f_ci->m_fattr = old_fattr;
7473 }
7474
7475out:
7476 ksmbd_fd_put(work, fp);
7477 return ret;
7478}
7479
7480static int fsctl_request_resume_key(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007481 struct smb2_ioctl_req *req,
7482 struct resume_key_ioctl_rsp *key_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007483{
7484 struct ksmbd_file *fp;
7485
7486 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007487 le64_to_cpu(req->VolatileFileId),
7488 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09007489 if (!fp)
7490 return -ENOENT;
7491
7492 memset(key_rsp, 0, sizeof(*key_rsp));
7493 key_rsp->ResumeKey[0] = req->VolatileFileId;
7494 key_rsp->ResumeKey[1] = req->PersistentFileId;
7495 ksmbd_fd_put(work, fp);
7496
7497 return 0;
7498}
7499
7500/**
7501 * smb2_ioctl() - handler for smb2 ioctl command
7502 * @work: smb work containing ioctl command buffer
7503 *
7504 * Return: 0 on success, otherwise error
7505 */
7506int smb2_ioctl(struct ksmbd_work *work)
7507{
7508 struct smb2_ioctl_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09007509 struct smb2_ioctl_rsp *rsp;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007510 unsigned int cnt_code, nbytes = 0, out_buf_len, in_buf_len;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007511 u64 id = KSMBD_NO_FID;
Namjae Jeone2f34482021-03-16 10:49:09 +09007512 struct ksmbd_conn *conn = work->conn;
7513 int ret = 0;
7514
Namjae Jeone2f34482021-03-16 10:49:09 +09007515 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09007516 req = ksmbd_req_buf_next(work);
7517 rsp = ksmbd_resp_buf_next(work);
Namjae Jeon38673692021-07-08 12:32:27 +09007518 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
7519 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007520 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09007521 id = work->compound_fid;
7522 }
7523 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09007524 req = smb2_get_msg(work->request_buf);
7525 rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09007526 }
7527
Namjae Jeon38673692021-07-08 12:32:27 +09007528 if (!has_file_id(id))
Namjae Jeone2f34482021-03-16 10:49:09 +09007529 id = le64_to_cpu(req->VolatileFileId);
7530
7531 if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) {
7532 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7533 goto out;
7534 }
7535
7536 cnt_code = le32_to_cpu(req->CntCode);
Hyunchul Lee34061d62021-10-16 08:39:54 +09007537 ret = smb2_calc_max_out_buf_len(work, 48,
7538 le32_to_cpu(req->MaxOutputResponse));
7539 if (ret < 0) {
7540 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7541 goto out;
7542 }
7543 out_buf_len = (unsigned int)ret;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007544 in_buf_len = le32_to_cpu(req->InputCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09007545
7546 switch (cnt_code) {
7547 case FSCTL_DFS_GET_REFERRALS:
7548 case FSCTL_DFS_GET_REFERRALS_EX:
7549 /* Not support DFS yet */
7550 rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED;
7551 goto out;
7552 case FSCTL_CREATE_OR_GET_OBJECT_ID:
7553 {
7554 struct file_object_buf_type1_ioctl_rsp *obj_buf;
7555
7556 nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp);
7557 obj_buf = (struct file_object_buf_type1_ioctl_rsp *)
7558 &rsp->Buffer[0];
7559
7560 /*
7561 * TODO: This is dummy implementation to pass smbtorture
7562 * Need to check correct response later
7563 */
7564 memset(obj_buf->ObjectId, 0x0, 16);
7565 memset(obj_buf->BirthVolumeId, 0x0, 16);
7566 memset(obj_buf->BirthObjectId, 0x0, 16);
7567 memset(obj_buf->DomainId, 0x0, 16);
7568
7569 break;
7570 }
7571 case FSCTL_PIPE_TRANSCEIVE:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007572 out_buf_len = min_t(u32, KSMBD_IPC_MAX_PAYLOAD, out_buf_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09007573 nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp);
7574 break;
7575 case FSCTL_VALIDATE_NEGOTIATE_INFO:
7576 if (conn->dialect < SMB30_PROT_ID) {
7577 ret = -EOPNOTSUPP;
7578 goto out;
7579 }
7580
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007581 if (in_buf_len < sizeof(struct validate_negotiate_info_req))
7582 return -EINVAL;
7583
7584 if (out_buf_len < sizeof(struct validate_negotiate_info_rsp))
7585 return -EINVAL;
7586
Namjae Jeone2f34482021-03-16 10:49:09 +09007587 ret = fsctl_validate_negotiate_info(conn,
7588 (struct validate_negotiate_info_req *)&req->Buffer[0],
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007589 (struct validate_negotiate_info_rsp *)&rsp->Buffer[0],
7590 in_buf_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09007591 if (ret < 0)
7592 goto out;
7593
7594 nbytes = sizeof(struct validate_negotiate_info_rsp);
7595 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7596 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7597 break;
7598 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007599 ret = fsctl_query_iface_info_ioctl(conn, rsp, out_buf_len);
7600 if (ret < 0)
Namjae Jeone2f34482021-03-16 10:49:09 +09007601 goto out;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007602 nbytes = ret;
Namjae Jeone2f34482021-03-16 10:49:09 +09007603 break;
7604 case FSCTL_REQUEST_RESUME_KEY:
7605 if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) {
7606 ret = -EINVAL;
7607 goto out;
7608 }
7609
7610 ret = fsctl_request_resume_key(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007611 (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007612 if (ret < 0)
7613 goto out;
7614 rsp->PersistentFileId = req->PersistentFileId;
7615 rsp->VolatileFileId = req->VolatileFileId;
7616 nbytes = sizeof(struct resume_key_ioctl_rsp);
7617 break;
7618 case FSCTL_COPYCHUNK:
7619 case FSCTL_COPYCHUNK_WRITE:
Namjae Jeon64b39f42021-03-30 14:25:35 +09007620 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007621 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007622 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007623 ret = -EACCES;
7624 goto out;
7625 }
7626
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007627 if (in_buf_len < sizeof(struct copychunk_ioctl_req)) {
7628 ret = -EINVAL;
7629 goto out;
7630 }
7631
Namjae Jeone2f34482021-03-16 10:49:09 +09007632 if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) {
7633 ret = -EINVAL;
7634 goto out;
7635 }
7636
7637 nbytes = sizeof(struct copychunk_ioctl_rsp);
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007638 rsp->VolatileFileId = req->VolatileFileId;
7639 rsp->PersistentFileId = req->PersistentFileId;
7640 fsctl_copychunk(work,
7641 (struct copychunk_ioctl_req *)&req->Buffer[0],
7642 le32_to_cpu(req->CntCode),
7643 le32_to_cpu(req->InputCount),
7644 le64_to_cpu(req->VolatileFileId),
7645 le64_to_cpu(req->PersistentFileId),
7646 rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007647 break;
7648 case FSCTL_SET_SPARSE:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007649 if (in_buf_len < sizeof(struct file_sparse)) {
7650 ret = -EINVAL;
7651 goto out;
7652 }
7653
Namjae Jeone2f34482021-03-16 10:49:09 +09007654 ret = fsctl_set_sparse(work, id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007655 (struct file_sparse *)&req->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007656 if (ret < 0)
7657 goto out;
7658 break;
7659 case FSCTL_SET_ZERO_DATA:
7660 {
7661 struct file_zero_data_information *zero_data;
7662 struct ksmbd_file *fp;
7663 loff_t off, len;
7664
Namjae Jeon64b39f42021-03-30 14:25:35 +09007665 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007666 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007667 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007668 ret = -EACCES;
7669 goto out;
7670 }
7671
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007672 if (in_buf_len < sizeof(struct file_zero_data_information)) {
7673 ret = -EINVAL;
7674 goto out;
7675 }
7676
Namjae Jeone2f34482021-03-16 10:49:09 +09007677 zero_data =
7678 (struct file_zero_data_information *)&req->Buffer[0];
7679
7680 fp = ksmbd_lookup_fd_fast(work, id);
7681 if (!fp) {
7682 ret = -ENOENT;
7683 goto out;
7684 }
7685
7686 off = le64_to_cpu(zero_data->FileOffset);
7687 len = le64_to_cpu(zero_data->BeyondFinalZero) - off;
7688
7689 ret = ksmbd_vfs_zero_data(work, fp, off, len);
7690 ksmbd_fd_put(work, fp);
7691 if (ret < 0)
7692 goto out;
7693 break;
7694 }
7695 case FSCTL_QUERY_ALLOCATED_RANGES:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007696 if (in_buf_len < sizeof(struct file_allocated_range_buffer)) {
7697 ret = -EINVAL;
7698 goto out;
7699 }
7700
Namjae Jeone2f34482021-03-16 10:49:09 +09007701 ret = fsctl_query_allocated_ranges(work, id,
7702 (struct file_allocated_range_buffer *)&req->Buffer[0],
7703 (struct file_allocated_range_buffer *)&rsp->Buffer[0],
7704 out_buf_len /
7705 sizeof(struct file_allocated_range_buffer), &nbytes);
7706 if (ret == -E2BIG) {
7707 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7708 } else if (ret < 0) {
7709 nbytes = 0;
7710 goto out;
7711 }
7712
7713 nbytes *= sizeof(struct file_allocated_range_buffer);
7714 break;
7715 case FSCTL_GET_REPARSE_POINT:
7716 {
7717 struct reparse_data_buffer *reparse_ptr;
7718 struct ksmbd_file *fp;
7719
7720 reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0];
7721 fp = ksmbd_lookup_fd_fast(work, id);
7722 if (!fp) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007723 pr_err("not found fp!!\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007724 ret = -ENOENT;
7725 goto out;
7726 }
7727
7728 reparse_ptr->ReparseTag =
Namjae Jeonab0b2632021-06-29 09:20:13 +09007729 smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode);
Namjae Jeone2f34482021-03-16 10:49:09 +09007730 reparse_ptr->ReparseDataLength = 0;
7731 ksmbd_fd_put(work, fp);
7732 nbytes = sizeof(struct reparse_data_buffer);
7733 break;
7734 }
Namjae Jeoneb817362021-05-18 10:37:59 +09007735 case FSCTL_DUPLICATE_EXTENTS_TO_FILE:
7736 {
7737 struct ksmbd_file *fp_in, *fp_out = NULL;
7738 struct duplicate_extents_to_file *dup_ext;
7739 loff_t src_off, dst_off, length, cloned;
7740
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007741 if (in_buf_len < sizeof(struct duplicate_extents_to_file)) {
7742 ret = -EINVAL;
7743 goto out;
7744 }
7745
Namjae Jeoneb817362021-05-18 10:37:59 +09007746 dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0];
7747
7748 fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle,
Namjae Jeon070fb212021-05-26 17:57:12 +09007749 dup_ext->PersistentFileHandle);
Namjae Jeoneb817362021-05-18 10:37:59 +09007750 if (!fp_in) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007751 pr_err("not found file handle in duplicate extent to file\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007752 ret = -ENOENT;
7753 goto out;
7754 }
7755
7756 fp_out = ksmbd_lookup_fd_fast(work, id);
7757 if (!fp_out) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007758 pr_err("not found fp\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007759 ret = -ENOENT;
7760 goto dup_ext_out;
7761 }
7762
7763 src_off = le64_to_cpu(dup_ext->SourceFileOffset);
7764 dst_off = le64_to_cpu(dup_ext->TargetFileOffset);
7765 length = le64_to_cpu(dup_ext->ByteCount);
7766 cloned = vfs_clone_file_range(fp_in->filp, src_off, fp_out->filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007767 dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007768 if (cloned == -EXDEV || cloned == -EOPNOTSUPP) {
7769 ret = -EOPNOTSUPP;
7770 goto dup_ext_out;
7771 } else if (cloned != length) {
Namjae Jeonf8524772021-06-18 10:28:00 +09007772 cloned = vfs_copy_file_range(fp_in->filp, src_off,
7773 fp_out->filp, dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007774 if (cloned != length) {
7775 if (cloned < 0)
7776 ret = cloned;
7777 else
7778 ret = -EINVAL;
7779 }
7780 }
7781
7782dup_ext_out:
7783 ksmbd_fd_put(work, fp_in);
7784 ksmbd_fd_put(work, fp_out);
7785 if (ret < 0)
7786 goto out;
7787 break;
7788 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007789 default:
7790 ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007791 cnt_code);
Namjae Jeone2f34482021-03-16 10:49:09 +09007792 ret = -EOPNOTSUPP;
7793 goto out;
7794 }
7795
7796 rsp->CntCode = cpu_to_le32(cnt_code);
7797 rsp->InputCount = cpu_to_le32(0);
7798 rsp->InputOffset = cpu_to_le32(112);
7799 rsp->OutputOffset = cpu_to_le32(112);
7800 rsp->OutputCount = cpu_to_le32(nbytes);
7801 rsp->StructureSize = cpu_to_le16(49);
7802 rsp->Reserved = cpu_to_le16(0);
7803 rsp->Flags = cpu_to_le32(0);
7804 rsp->Reserved2 = cpu_to_le32(0);
Namjae Jeoncb451722021-11-03 08:08:44 +09007805 inc_rfc1001_len(work->response_buf, 48 + nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09007806
7807 return 0;
7808
7809out:
7810 if (ret == -EACCES)
7811 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7812 else if (ret == -ENOENT)
7813 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7814 else if (ret == -EOPNOTSUPP)
7815 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007816 else if (ret == -ENOSPC)
7817 rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL;
Namjae Jeone2f34482021-03-16 10:49:09 +09007818 else if (ret < 0 || rsp->hdr.Status == 0)
7819 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7820 smb2_set_err_rsp(work);
7821 return 0;
7822}
7823
7824/**
7825 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command
7826 * @work: smb work containing oplock break command buffer
7827 *
7828 * Return: 0
7829 */
7830static void smb20_oplock_break_ack(struct ksmbd_work *work)
7831{
Namjae Jeoncb451722021-11-03 08:08:44 +09007832 struct smb2_oplock_break *req = smb2_get_msg(work->request_buf);
7833 struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09007834 struct ksmbd_file *fp;
7835 struct oplock_info *opinfo = NULL;
7836 __le32 err = 0;
7837 int ret = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007838 u64 volatile_id, persistent_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09007839 char req_oplevel = 0, rsp_oplevel = 0;
7840 unsigned int oplock_change_type;
7841
7842 volatile_id = le64_to_cpu(req->VolatileFid);
7843 persistent_id = le64_to_cpu(req->PersistentFid);
7844 req_oplevel = req->OplockLevel;
7845 ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n",
7846 volatile_id, persistent_id, req_oplevel);
7847
7848 fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
7849 if (!fp) {
7850 rsp->hdr.Status = STATUS_FILE_CLOSED;
7851 smb2_set_err_rsp(work);
7852 return;
7853 }
7854
7855 opinfo = opinfo_get(fp);
7856 if (!opinfo) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007857 pr_err("unexpected null oplock_info\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007858 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7859 smb2_set_err_rsp(work);
7860 ksmbd_fd_put(work, fp);
7861 return;
7862 }
7863
7864 if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) {
7865 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7866 goto err_out;
7867 }
7868
7869 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7870 ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state);
7871 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7872 goto err_out;
7873 }
7874
Namjae Jeon64b39f42021-03-30 14:25:35 +09007875 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7876 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7877 (req_oplevel != SMB2_OPLOCK_LEVEL_II &&
7878 req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007879 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7880 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007881 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7882 req_oplevel != SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007883 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7884 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007885 } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II ||
7886 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007887 err = STATUS_INVALID_DEVICE_STATE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007888 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7889 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7890 req_oplevel == SMB2_OPLOCK_LEVEL_II) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007891 oplock_change_type = OPLOCK_WRITE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007892 } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7893 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7894 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007895 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007896 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7897 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007898 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007899 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007900 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007901 }
7902 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007903 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007904 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007905
7906 switch (oplock_change_type) {
7907 case OPLOCK_WRITE_TO_READ:
7908 ret = opinfo_write_to_read(opinfo);
7909 rsp_oplevel = SMB2_OPLOCK_LEVEL_II;
7910 break;
7911 case OPLOCK_WRITE_TO_NONE:
7912 ret = opinfo_write_to_none(opinfo);
7913 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7914 break;
7915 case OPLOCK_READ_TO_NONE:
7916 ret = opinfo_read_to_none(opinfo);
7917 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7918 break;
7919 default:
Namjae Jeonbde16942021-06-28 15:23:19 +09007920 pr_err("unknown oplock change 0x%x -> 0x%x\n",
7921 opinfo->level, rsp_oplevel);
Namjae Jeone2f34482021-03-16 10:49:09 +09007922 }
7923
7924 if (ret < 0) {
7925 rsp->hdr.Status = err;
7926 goto err_out;
7927 }
7928
7929 opinfo_put(opinfo);
7930 ksmbd_fd_put(work, fp);
7931 opinfo->op_state = OPLOCK_STATE_NONE;
7932 wake_up_interruptible_all(&opinfo->oplock_q);
7933
7934 rsp->StructureSize = cpu_to_le16(24);
7935 rsp->OplockLevel = rsp_oplevel;
7936 rsp->Reserved = 0;
7937 rsp->Reserved2 = 0;
7938 rsp->VolatileFid = cpu_to_le64(volatile_id);
7939 rsp->PersistentFid = cpu_to_le64(persistent_id);
Namjae Jeoncb451722021-11-03 08:08:44 +09007940 inc_rfc1001_len(work->response_buf, 24);
Namjae Jeone2f34482021-03-16 10:49:09 +09007941 return;
7942
7943err_out:
7944 opinfo->op_state = OPLOCK_STATE_NONE;
7945 wake_up_interruptible_all(&opinfo->oplock_q);
7946
7947 opinfo_put(opinfo);
7948 ksmbd_fd_put(work, fp);
7949 smb2_set_err_rsp(work);
7950}
7951
7952static int check_lease_state(struct lease *lease, __le32 req_state)
7953{
7954 if ((lease->new_state ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09007955 (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) &&
7956 !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007957 lease->new_state = req_state;
7958 return 0;
7959 }
7960
7961 if (lease->new_state == req_state)
7962 return 0;
7963
7964 return 1;
7965}
7966
7967/**
7968 * smb21_lease_break_ack() - handler for smb2.1 lease break command
7969 * @work: smb work containing lease break command buffer
7970 *
7971 * Return: 0
7972 */
7973static void smb21_lease_break_ack(struct ksmbd_work *work)
7974{
7975 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09007976 struct smb2_lease_ack *req = smb2_get_msg(work->request_buf);
7977 struct smb2_lease_ack *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09007978 struct oplock_info *opinfo;
7979 __le32 err = 0;
7980 int ret = 0;
7981 unsigned int lease_change_type;
7982 __le32 lease_state;
7983 struct lease *lease;
7984
7985 ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007986 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007987 opinfo = lookup_lease_in_table(conn, req->LeaseKey);
7988 if (!opinfo) {
7989 ksmbd_debug(OPLOCK, "file not opened\n");
7990 smb2_set_err_rsp(work);
7991 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7992 return;
7993 }
7994 lease = opinfo->o_lease;
7995
7996 if (opinfo->op_state == OPLOCK_STATE_NONE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007997 pr_err("unexpected lease break state 0x%x\n",
7998 opinfo->op_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09007999 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
8000 goto err_out;
8001 }
8002
8003 if (check_lease_state(lease, req->LeaseState)) {
8004 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
8005 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +09008006 "req lease state: 0x%x, expected state: 0x%x\n",
8007 req->LeaseState, lease->new_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09008008 goto err_out;
8009 }
8010
8011 if (!atomic_read(&opinfo->breaking_cnt)) {
8012 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
8013 goto err_out;
8014 }
8015
8016 /* check for bad lease state */
Namjae Jeon070fb212021-05-26 17:57:12 +09008017 if (req->LeaseState &
8018 (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008019 err = STATUS_INVALID_OPLOCK_PROTOCOL;
8020 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8021 lease_change_type = OPLOCK_WRITE_TO_NONE;
8022 else
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 Jeon64b39f42021-03-30 14:25:35 +09008027 } else if (lease->state == SMB2_LEASE_READ_CACHING_LE &&
8028 req->LeaseState != SMB2_LEASE_NONE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008029 err = STATUS_INVALID_OPLOCK_PROTOCOL;
8030 lease_change_type = OPLOCK_READ_TO_NONE;
8031 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008032 le32_to_cpu(lease->state),
8033 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09008034 } else {
8035 /* valid lease state changes */
8036 err = STATUS_INVALID_DEVICE_STATE;
8037 if (req->LeaseState == SMB2_LEASE_NONE_LE) {
8038 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8039 lease_change_type = OPLOCK_WRITE_TO_NONE;
8040 else
8041 lease_change_type = OPLOCK_READ_TO_NONE;
8042 } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) {
8043 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8044 lease_change_type = OPLOCK_WRITE_TO_READ;
8045 else
8046 lease_change_type = OPLOCK_READ_HANDLE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09008047 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09008048 lease_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09008049 }
Namjae Jeone2f34482021-03-16 10:49:09 +09008050 }
8051
8052 switch (lease_change_type) {
8053 case OPLOCK_WRITE_TO_READ:
8054 ret = opinfo_write_to_read(opinfo);
8055 break;
8056 case OPLOCK_READ_HANDLE_TO_READ:
8057 ret = opinfo_read_handle_to_read(opinfo);
8058 break;
8059 case OPLOCK_WRITE_TO_NONE:
8060 ret = opinfo_write_to_none(opinfo);
8061 break;
8062 case OPLOCK_READ_TO_NONE:
8063 ret = opinfo_read_to_none(opinfo);
8064 break;
8065 default:
8066 ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008067 le32_to_cpu(lease->state),
8068 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09008069 }
8070
8071 lease_state = lease->state;
8072 opinfo->op_state = OPLOCK_STATE_NONE;
8073 wake_up_interruptible_all(&opinfo->oplock_q);
8074 atomic_dec(&opinfo->breaking_cnt);
8075 wake_up_interruptible_all(&opinfo->oplock_brk);
8076 opinfo_put(opinfo);
8077
8078 if (ret < 0) {
8079 rsp->hdr.Status = err;
8080 goto err_out;
8081 }
8082
8083 rsp->StructureSize = cpu_to_le16(36);
8084 rsp->Reserved = 0;
8085 rsp->Flags = 0;
8086 memcpy(rsp->LeaseKey, req->LeaseKey, 16);
8087 rsp->LeaseState = lease_state;
8088 rsp->LeaseDuration = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09008089 inc_rfc1001_len(work->response_buf, 36);
Namjae Jeone2f34482021-03-16 10:49:09 +09008090 return;
8091
8092err_out:
8093 opinfo->op_state = OPLOCK_STATE_NONE;
8094 wake_up_interruptible_all(&opinfo->oplock_q);
8095 atomic_dec(&opinfo->breaking_cnt);
8096 wake_up_interruptible_all(&opinfo->oplock_brk);
8097
8098 opinfo_put(opinfo);
8099 smb2_set_err_rsp(work);
8100}
8101
8102/**
8103 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break
8104 * @work: smb work containing oplock/lease break command buffer
8105 *
8106 * Return: 0
8107 */
8108int smb2_oplock_break(struct ksmbd_work *work)
8109{
Namjae Jeoncb451722021-11-03 08:08:44 +09008110 struct smb2_oplock_break *req = smb2_get_msg(work->request_buf);
8111 struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008112
8113 switch (le16_to_cpu(req->StructureSize)) {
8114 case OP_BREAK_STRUCT_SIZE_20:
8115 smb20_oplock_break_ack(work);
8116 break;
8117 case OP_BREAK_STRUCT_SIZE_21:
8118 smb21_lease_break_ack(work);
8119 break;
8120 default:
8121 ksmbd_debug(OPLOCK, "invalid break cmd %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008122 le16_to_cpu(req->StructureSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09008123 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
8124 smb2_set_err_rsp(work);
8125 }
8126
8127 return 0;
8128}
8129
8130/**
8131 * smb2_notify() - handler for smb2 notify request
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09008132 * @work: smb work containing notify command buffer
Namjae Jeone2f34482021-03-16 10:49:09 +09008133 *
8134 * Return: 0
8135 */
8136int smb2_notify(struct ksmbd_work *work)
8137{
Ronnie Sahlberg699230f2021-09-09 12:26:12 +09008138 struct smb2_change_notify_req *req;
8139 struct smb2_change_notify_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09008140
8141 WORK_BUFFERS(work, req, rsp);
8142
8143 if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) {
8144 rsp->hdr.Status = STATUS_INTERNAL_ERROR;
8145 smb2_set_err_rsp(work);
8146 return 0;
8147 }
8148
8149 smb2_set_err_rsp(work);
8150 rsp->hdr.Status = STATUS_NOT_IMPLEMENTED;
8151 return 0;
8152}
8153
8154/**
8155 * smb2_is_sign_req() - handler for checking packet signing status
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09008156 * @work: smb work containing notify command buffer
8157 * @command: SMB2 command id
Namjae Jeone2f34482021-03-16 10:49:09 +09008158 *
8159 * Return: true if packed is signed, false otherwise
8160 */
8161bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command)
8162{
Namjae Jeoncb451722021-11-03 08:08:44 +09008163 struct smb2_hdr *rcv_hdr2 = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008164
8165 if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008166 command != SMB2_NEGOTIATE_HE &&
8167 command != SMB2_SESSION_SETUP_HE &&
8168 command != SMB2_OPLOCK_BREAK_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +09008169 return true;
8170
kernel test robot56160152021-05-12 09:24:37 +09008171 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +09008172}
8173
8174/**
8175 * smb2_check_sign_req() - handler for req packet sign processing
8176 * @work: smb work containing notify command buffer
8177 *
8178 * Return: 1 on success, 0 otherwise
8179 */
8180int smb2_check_sign_req(struct ksmbd_work *work)
8181{
Namjae Jeoncb451722021-11-03 08:08:44 +09008182 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008183 char signature_req[SMB2_SIGNATURE_SIZE];
8184 char signature[SMB2_HMACSHA256_SIZE];
8185 struct kvec iov[1];
8186 size_t len;
8187
Namjae Jeoncb451722021-11-03 08:08:44 +09008188 hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008189 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008190 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008191
8192 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
Namjae Jeoncb451722021-11-03 08:08:44 +09008193 len = get_rfc1002_len(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008194 else if (hdr->NextCommand)
8195 len = le32_to_cpu(hdr->NextCommand);
8196 else
Namjae Jeoncb451722021-11-03 08:08:44 +09008197 len = get_rfc1002_len(work->request_buf) -
Namjae Jeone2f34482021-03-16 10:49:09 +09008198 work->next_smb2_rcv_hdr_off;
8199
8200 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8201 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8202
8203 iov[0].iov_base = (char *)&hdr->ProtocolId;
8204 iov[0].iov_len = len;
8205
8206 if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1,
Namjae Jeon64b39f42021-03-30 14:25:35 +09008207 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09008208 return 0;
8209
8210 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008211 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008212 return 0;
8213 }
8214
8215 return 1;
8216}
8217
8218/**
8219 * smb2_set_sign_rsp() - handler for rsp packet sign processing
8220 * @work: smb work containing notify command buffer
8221 *
8222 */
8223void smb2_set_sign_rsp(struct ksmbd_work *work)
8224{
Namjae Jeoncb451722021-11-03 08:08:44 +09008225 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008226 struct smb2_hdr *req_hdr;
8227 char signature[SMB2_HMACSHA256_SIZE];
8228 struct kvec iov[2];
8229 size_t len;
8230 int n_vec = 1;
8231
Namjae Jeoncb451722021-11-03 08:08:44 +09008232 hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008233 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008234 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008235
Namjae Jeon8a893312021-06-25 13:43:37 +09008236 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008237
8238 if (!work->next_smb2_rsp_hdr_off) {
Namjae Jeoncb451722021-11-03 08:08:44 +09008239 len = get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008240 if (req_hdr->NextCommand)
8241 len = ALIGN(len, 8);
8242 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09008243 len = get_rfc1002_len(work->response_buf) -
8244 work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +09008245 len = ALIGN(len, 8);
8246 }
8247
8248 if (req_hdr->NextCommand)
8249 hdr->NextCommand = cpu_to_le32(len);
8250
8251 hdr->Flags |= SMB2_FLAGS_SIGNED;
8252 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8253
8254 iov[0].iov_base = (char *)&hdr->ProtocolId;
8255 iov[0].iov_len = len;
8256
Namjae Jeone5066492021-03-30 12:35:23 +09008257 if (work->aux_payload_sz) {
8258 iov[0].iov_len -= work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008259
Namjae Jeone5066492021-03-30 12:35:23 +09008260 iov[1].iov_base = work->aux_payload_buf;
8261 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008262 n_vec++;
8263 }
8264
8265 if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec,
Namjae Jeon64b39f42021-03-30 14:25:35 +09008266 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09008267 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8268}
8269
8270/**
8271 * smb3_check_sign_req() - handler for req packet sign processing
8272 * @work: smb work containing notify command buffer
8273 *
8274 * Return: 1 on success, 0 otherwise
8275 */
8276int smb3_check_sign_req(struct ksmbd_work *work)
8277{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008278 struct ksmbd_conn *conn = work->conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09008279 char *signing_key;
Namjae Jeoncb451722021-11-03 08:08:44 +09008280 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008281 struct channel *chann;
8282 char signature_req[SMB2_SIGNATURE_SIZE];
8283 char signature[SMB2_CMACAES_SIZE];
8284 struct kvec iov[1];
8285 size_t len;
8286
Namjae Jeoncb451722021-11-03 08:08:44 +09008287 hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008288 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008289 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008290
8291 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
Namjae Jeoncb451722021-11-03 08:08:44 +09008292 len = get_rfc1002_len(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008293 else if (hdr->NextCommand)
8294 len = le32_to_cpu(hdr->NextCommand);
8295 else
Namjae Jeoncb451722021-11-03 08:08:44 +09008296 len = get_rfc1002_len(work->request_buf) -
Namjae Jeone2f34482021-03-16 10:49:09 +09008297 work->next_smb2_rcv_hdr_off;
8298
8299 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
8300 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008301 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008302 chann = lookup_chann_list(work->sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008303 if (!chann)
8304 return 0;
8305 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008306 }
8307
8308 if (!signing_key) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008309 pr_err("SMB3 signing key is not generated\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008310 return 0;
8311 }
8312
8313 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8314 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8315 iov[0].iov_base = (char *)&hdr->ProtocolId;
8316 iov[0].iov_len = len;
8317
8318 if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature))
8319 return 0;
8320
8321 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008322 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008323 return 0;
8324 }
8325
8326 return 1;
8327}
8328
8329/**
8330 * smb3_set_sign_rsp() - handler for rsp packet sign processing
8331 * @work: smb work containing notify command buffer
8332 *
8333 */
8334void smb3_set_sign_rsp(struct ksmbd_work *work)
8335{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008336 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09008337 struct smb2_hdr *req_hdr, *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008338 struct channel *chann;
8339 char signature[SMB2_CMACAES_SIZE];
8340 struct kvec iov[2];
8341 int n_vec = 1;
8342 size_t len;
8343 char *signing_key;
8344
Namjae Jeoncb451722021-11-03 08:08:44 +09008345 hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008346 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008347 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008348
Namjae Jeon8a893312021-06-25 13:43:37 +09008349 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008350
8351 if (!work->next_smb2_rsp_hdr_off) {
Namjae Jeoncb451722021-11-03 08:08:44 +09008352 len = get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008353 if (req_hdr->NextCommand)
8354 len = ALIGN(len, 8);
8355 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09008356 len = get_rfc1002_len(work->response_buf) -
8357 work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +09008358 len = ALIGN(len, 8);
8359 }
8360
Namjae Jeon08bdbc62021-07-27 09:30:29 +09008361 if (conn->binding == false &&
8362 le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008363 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008364 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008365 chann = lookup_chann_list(work->sess, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008366 if (!chann)
8367 return;
8368 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008369 }
8370
8371 if (!signing_key)
8372 return;
8373
8374 if (req_hdr->NextCommand)
8375 hdr->NextCommand = cpu_to_le32(len);
8376
8377 hdr->Flags |= SMB2_FLAGS_SIGNED;
8378 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8379 iov[0].iov_base = (char *)&hdr->ProtocolId;
8380 iov[0].iov_len = len;
Namjae Jeone5066492021-03-30 12:35:23 +09008381 if (work->aux_payload_sz) {
8382 iov[0].iov_len -= work->aux_payload_sz;
8383 iov[1].iov_base = work->aux_payload_buf;
8384 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008385 n_vec++;
8386 }
8387
8388 if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature))
8389 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8390}
8391
8392/**
8393 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response
8394 * @work: smb work containing response buffer
8395 *
8396 */
8397void smb3_preauth_hash_rsp(struct ksmbd_work *work)
8398{
8399 struct ksmbd_conn *conn = work->conn;
8400 struct ksmbd_session *sess = work->sess;
8401 struct smb2_hdr *req, *rsp;
8402
8403 if (conn->dialect != SMB311_PROT_ID)
8404 return;
8405
8406 WORK_BUFFERS(work, req, rsp);
8407
Namjae Jeon442ff9e2021-09-29 15:44:32 +09008408 if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE &&
8409 conn->preauth_info)
Namjae Jeoncb451722021-11-03 08:08:44 +09008410 ksmbd_gen_preauth_integrity_hash(conn, work->response_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008411 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09008412
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008413 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008414 __u8 *hash_value;
8415
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008416 if (conn->binding) {
8417 struct preauth_session *preauth_sess;
8418
8419 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
8420 if (!preauth_sess)
8421 return;
8422 hash_value = preauth_sess->Preauth_HashValue;
8423 } else {
8424 hash_value = sess->Preauth_HashValue;
8425 if (!hash_value)
8426 return;
8427 }
Namjae Jeoncb451722021-11-03 08:08:44 +09008428 ksmbd_gen_preauth_integrity_hash(conn, work->response_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008429 hash_value);
Namjae Jeone2f34482021-03-16 10:49:09 +09008430 }
8431}
8432
Namjae Jeon2dd91292021-11-03 08:25:54 +09008433static void fill_transform_hdr(void *tr_buf, char *old_buf, __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09008434{
Namjae Jeon2dd91292021-11-03 08:25:54 +09008435 struct smb2_transform_hdr *tr_hdr = tr_buf + 4;
8436 struct smb2_hdr *hdr = smb2_get_msg(old_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008437 unsigned int orig_len = get_rfc1002_len(old_buf);
8438
Namjae Jeon2dd91292021-11-03 08:25:54 +09008439 memset(tr_buf, 0, sizeof(struct smb2_transform_hdr) + 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09008440 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
8441 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
Ronnie Sahlberg4355a8f2021-11-03 08:43:42 +09008442 tr_hdr->Flags = cpu_to_le16(TRANSFORM_FLAG_ENCRYPTED);
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008443 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
8444 cipher_type == SMB2_ENCRYPTION_AES256_GCM)
8445 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008446 else
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008447 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008448 memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8);
Namjae Jeon2dd91292021-11-03 08:25:54 +09008449 inc_rfc1001_len(tr_buf, sizeof(struct smb2_transform_hdr));
8450 inc_rfc1001_len(tr_buf, orig_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09008451}
8452
8453int smb3_encrypt_resp(struct ksmbd_work *work)
8454{
Namjae Jeone5066492021-03-30 12:35:23 +09008455 char *buf = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008456 struct kvec iov[3];
8457 int rc = -ENOMEM;
Namjae Jeone5066492021-03-30 12:35:23 +09008458 int buf_size = 0, rq_nvec = 2 + (work->aux_payload_sz ? 1 : 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09008459
8460 if (ARRAY_SIZE(iov) < rq_nvec)
8461 return -ENOMEM;
8462
Namjae Jeon2dd91292021-11-03 08:25:54 +09008463 work->tr_buf = kzalloc(sizeof(struct smb2_transform_hdr) + 4, GFP_KERNEL);
8464 if (!work->tr_buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09008465 return rc;
8466
8467 /* fill transform header */
Namjae Jeon2dd91292021-11-03 08:25:54 +09008468 fill_transform_hdr(work->tr_buf, buf, work->conn->cipher_type);
Namjae Jeone2f34482021-03-16 10:49:09 +09008469
Namjae Jeon2dd91292021-11-03 08:25:54 +09008470 iov[0].iov_base = work->tr_buf;
8471 iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008472 buf_size += iov[0].iov_len - 4;
8473
8474 iov[1].iov_base = buf + 4;
8475 iov[1].iov_len = get_rfc1002_len(buf);
Namjae Jeone5066492021-03-30 12:35:23 +09008476 if (work->aux_payload_sz) {
8477 iov[1].iov_len = work->resp_hdr_sz - 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008478
Namjae Jeone5066492021-03-30 12:35:23 +09008479 iov[2].iov_base = work->aux_payload_buf;
8480 iov[2].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008481 buf_size += iov[2].iov_len;
8482 }
8483 buf_size += iov[1].iov_len;
8484 work->resp_hdr_sz = iov[1].iov_len;
8485
8486 rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1);
8487 if (rc)
8488 return rc;
8489
8490 memmove(buf, iov[1].iov_base, iov[1].iov_len);
Namjae Jeon2dd91292021-11-03 08:25:54 +09008491 *(__be32 *)work->tr_buf = cpu_to_be32(buf_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09008492
8493 return rc;
8494}
8495
Namjae Jeonf4228b62021-08-12 10:16:40 +09008496bool smb3_is_transform_hdr(void *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09008497{
Namjae Jeon2dd91292021-11-03 08:25:54 +09008498 struct smb2_transform_hdr *trhdr = smb2_get_msg(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008499
8500 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
8501}
8502
8503int smb3_decrypt_req(struct ksmbd_work *work)
8504{
8505 struct ksmbd_conn *conn = work->conn;
8506 struct ksmbd_session *sess;
Namjae Jeone5066492021-03-30 12:35:23 +09008507 char *buf = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008508 unsigned int pdu_length = get_rfc1002_len(buf);
8509 struct kvec iov[2];
Namjae Jeon2dd91292021-11-03 08:25:54 +09008510 int buf_data_size = pdu_length - sizeof(struct smb2_transform_hdr);
8511 struct smb2_transform_hdr *tr_hdr = smb2_get_msg(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008512 int rc = 0;
8513
Namjae Jeonc7705ee2021-10-04 20:44:52 +09008514 if (buf_data_size < sizeof(struct smb2_hdr)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008515 pr_err("Transform message is too small (%u)\n",
8516 pdu_length);
Namjae Jeone2f34482021-03-16 10:49:09 +09008517 return -ECONNABORTED;
8518 }
8519
Namjae Jeonc7705ee2021-10-04 20:44:52 +09008520 if (buf_data_size < le32_to_cpu(tr_hdr->OriginalMessageSize)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008521 pr_err("Transform message is broken\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008522 return -ECONNABORTED;
8523 }
8524
Namjae Jeon4227f812021-09-29 19:52:51 +09008525 sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId));
8526 if (!sess) {
8527 pr_err("invalid session id(%llx) in transform header\n",
8528 le64_to_cpu(tr_hdr->SessionId));
8529 return -ECONNABORTED;
8530 }
8531
Namjae Jeone2f34482021-03-16 10:49:09 +09008532 iov[0].iov_base = buf;
Namjae Jeon2dd91292021-11-03 08:25:54 +09008533 iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4;
8534 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008535 iov[1].iov_len = buf_data_size;
8536 rc = ksmbd_crypt_message(conn, iov, 2, 0);
8537 if (rc)
8538 return rc;
8539
8540 memmove(buf + 4, iov[1].iov_base, buf_data_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09008541 *(__be32 *)buf = cpu_to_be32(buf_data_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09008542
8543 return rc;
8544}
8545
8546bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work)
8547{
8548 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09008549 struct smb2_hdr *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008550
8551 if (conn->dialect < SMB30_PROT_ID)
8552 return false;
8553
8554 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008555 rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008556
8557 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008558 rsp->Status == STATUS_SUCCESS)
Namjae Jeone2f34482021-03-16 10:49:09 +09008559 return true;
8560 return false;
8561}