blob: 036a0dc868598cbfbfd30dd181620fa473ab8209 [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
613 * @src: source buffer
614 * @maxlen: maxlen of source string
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900615 * @nls_table: nls_table pointer
Namjae Jeone2f34482021-03-16 10:49:09 +0900616 *
617 * Return: matching converted filename on success, otherwise error ptr
618 */
619static char *
Marios Makassikis80917f12021-12-01 21:41:19 +0100620smb2_get_name(const char *src, const int maxlen, struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +0900621{
Hyunchul Lee265fd192021-09-25 00:06:16 +0900622 char *name;
Namjae Jeone2f34482021-03-16 10:49:09 +0900623
Namjae Jeon64b39f42021-03-30 14:25:35 +0900624 name = smb_strndup_from_utf16(src, maxlen, 1, local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +0900625 if (IS_ERR(name)) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900626 pr_err("failed to get name %ld\n", PTR_ERR(name));
Namjae Jeone2f34482021-03-16 10:49:09 +0900627 return name;
628 }
629
Hyunchul Lee265fd192021-09-25 00:06:16 +0900630 ksmbd_conv_path_to_unix(name);
631 ksmbd_strip_last_slash(name);
632 return name;
Namjae Jeone2f34482021-03-16 10:49:09 +0900633}
634
Namjae Jeone2f34482021-03-16 10:49:09 +0900635int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg)
636{
637 struct smb2_hdr *rsp_hdr;
638 struct ksmbd_conn *conn = work->conn;
639 int id;
640
Namjae Jeoncb451722021-11-03 08:08:44 +0900641 rsp_hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900642 rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND;
643
Namjae Jeond40012a2021-04-13 13:06:30 +0900644 id = ksmbd_acquire_async_msg_id(&conn->async_ida);
Namjae Jeone2f34482021-03-16 10:49:09 +0900645 if (id < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900646 pr_err("Failed to alloc async message id\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900647 return id;
648 }
649 work->syncronous = false;
650 work->async_id = id;
651 rsp_hdr->Id.AsyncId = cpu_to_le64(id);
652
653 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900654 "Send interim Response to inform async request id : %d\n",
655 work->async_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900656
657 work->cancel_fn = fn;
658 work->cancel_argv = arg;
659
Namjae Jeon6c4e6752021-06-07 09:08:45 +0900660 if (list_empty(&work->async_request_entry)) {
661 spin_lock(&conn->request_lock);
662 list_add_tail(&work->async_request_entry, &conn->async_requests);
663 spin_unlock(&conn->request_lock);
664 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900665
666 return 0;
667}
668
669void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
670{
671 struct smb2_hdr *rsp_hdr;
672
Namjae Jeoncb451722021-11-03 08:08:44 +0900673 rsp_hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900674 smb2_set_err_rsp(work);
675 rsp_hdr->Status = status;
676
677 work->multiRsp = 1;
678 ksmbd_conn_write(work);
679 rsp_hdr->Status = 0;
680 work->multiRsp = 0;
681}
682
683static __le32 smb2_get_reparse_tag_special_file(umode_t mode)
684{
685 if (S_ISDIR(mode) || S_ISREG(mode))
686 return 0;
687
688 if (S_ISLNK(mode))
689 return IO_REPARSE_TAG_LX_SYMLINK_LE;
690 else if (S_ISFIFO(mode))
691 return IO_REPARSE_TAG_LX_FIFO_LE;
692 else if (S_ISSOCK(mode))
693 return IO_REPARSE_TAG_AF_UNIX_LE;
694 else if (S_ISCHR(mode))
695 return IO_REPARSE_TAG_LX_CHR_LE;
696 else if (S_ISBLK(mode))
697 return IO_REPARSE_TAG_LX_BLK_LE;
698
699 return 0;
700}
701
702/**
703 * smb2_get_dos_mode() - get file mode in dos format from unix mode
704 * @stat: kstat containing file mode
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900705 * @attribute: attribute flags
Namjae Jeone2f34482021-03-16 10:49:09 +0900706 *
707 * Return: converted dos mode
708 */
709static int smb2_get_dos_mode(struct kstat *stat, int attribute)
710{
711 int attr = 0;
712
Namjae Jeon64b39f42021-03-30 14:25:35 +0900713 if (S_ISDIR(stat->mode)) {
Ronnie Sahlberg26a27872021-11-03 08:45:52 +0900714 attr = FILE_ATTRIBUTE_DIRECTORY |
715 (attribute & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM));
Namjae Jeon64b39f42021-03-30 14:25:35 +0900716 } else {
Ronnie Sahlberg26a27872021-11-03 08:45:52 +0900717 attr = (attribute & 0x00005137) | FILE_ATTRIBUTE_ARCHIVE;
718 attr &= ~(FILE_ATTRIBUTE_DIRECTORY);
Namjae Jeone2f34482021-03-16 10:49:09 +0900719 if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps &
720 FILE_SUPPORTS_SPARSE_FILES))
Ronnie Sahlberg26a27872021-11-03 08:45:52 +0900721 attr |= FILE_ATTRIBUTE_SPARSE_FILE;
Namjae Jeone2f34482021-03-16 10:49:09 +0900722
723 if (smb2_get_reparse_tag_special_file(stat->mode))
Ronnie Sahlberg26a27872021-11-03 08:45:52 +0900724 attr |= FILE_ATTRIBUTE_REPARSE_POINT;
Namjae Jeone2f34482021-03-16 10:49:09 +0900725 }
726
727 return attr;
728}
729
730static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900731 __le16 hash_id)
Namjae Jeone2f34482021-03-16 10:49:09 +0900732{
733 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
734 pneg_ctxt->DataLength = cpu_to_le16(38);
735 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
736 pneg_ctxt->Reserved = cpu_to_le32(0);
737 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
738 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
739 pneg_ctxt->HashAlgorithms = hash_id;
740}
741
742static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900743 __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +0900744{
745 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
746 pneg_ctxt->DataLength = cpu_to_le16(4);
747 pneg_ctxt->Reserved = cpu_to_le32(0);
748 pneg_ctxt->CipherCount = cpu_to_le16(1);
749 pneg_ctxt->Ciphers[0] = cipher_type;
750}
751
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900752static void build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900753 __le16 comp_algo)
Namjae Jeone2f34482021-03-16 10:49:09 +0900754{
755 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
756 pneg_ctxt->DataLength =
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900757 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
Namjae Jeone2f34482021-03-16 10:49:09 +0900758 - sizeof(struct smb2_neg_context));
759 pneg_ctxt->Reserved = cpu_to_le32(0);
760 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(1);
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900761 pneg_ctxt->Flags = cpu_to_le32(0);
Namjae Jeone2f34482021-03-16 10:49:09 +0900762 pneg_ctxt->CompressionAlgorithms[0] = comp_algo;
763}
764
Namjae Jeon378087c2021-07-21 10:05:53 +0900765static void build_sign_cap_ctxt(struct smb2_signing_capabilities *pneg_ctxt,
766 __le16 sign_algo)
767{
768 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
769 pneg_ctxt->DataLength =
770 cpu_to_le16((sizeof(struct smb2_signing_capabilities) + 2)
771 - sizeof(struct smb2_neg_context));
772 pneg_ctxt->Reserved = cpu_to_le32(0);
773 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(1);
774 pneg_ctxt->SigningAlgorithms[0] = sign_algo;
775}
776
Namjae Jeon64b39f42021-03-30 14:25:35 +0900777static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900778{
779 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
780 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
781 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
782 pneg_ctxt->Name[0] = 0x93;
783 pneg_ctxt->Name[1] = 0xAD;
784 pneg_ctxt->Name[2] = 0x25;
785 pneg_ctxt->Name[3] = 0x50;
786 pneg_ctxt->Name[4] = 0x9C;
787 pneg_ctxt->Name[5] = 0xB4;
788 pneg_ctxt->Name[6] = 0x11;
789 pneg_ctxt->Name[7] = 0xE7;
790 pneg_ctxt->Name[8] = 0xB4;
791 pneg_ctxt->Name[9] = 0x23;
792 pneg_ctxt->Name[10] = 0x83;
793 pneg_ctxt->Name[11] = 0xDE;
794 pneg_ctxt->Name[12] = 0x96;
795 pneg_ctxt->Name[13] = 0x8B;
796 pneg_ctxt->Name[14] = 0xCD;
797 pneg_ctxt->Name[15] = 0x7C;
798}
799
Namjae Jeon64b39f42021-03-30 14:25:35 +0900800static void assemble_neg_contexts(struct ksmbd_conn *conn,
Namjae Jeoncb451722021-11-03 08:08:44 +0900801 struct smb2_negotiate_rsp *rsp,
802 void *smb2_buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +0900803{
Namjae Jeone2f34482021-03-16 10:49:09 +0900804 char *pneg_ctxt = (char *)rsp +
Namjae Jeoncb451722021-11-03 08:08:44 +0900805 le32_to_cpu(rsp->NegotiateContextOffset);
Namjae Jeone2f34482021-03-16 10:49:09 +0900806 int neg_ctxt_cnt = 1;
807 int ctxt_size;
808
809 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900810 "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900811 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900812 conn->preauth_info->Preauth_HashId);
Namjae Jeone2f34482021-03-16 10:49:09 +0900813 rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt);
Namjae Jeoncb451722021-11-03 08:08:44 +0900814 inc_rfc1001_len(smb2_buf_len, AUTH_GSS_PADDING);
Namjae Jeone2f34482021-03-16 10:49:09 +0900815 ctxt_size = sizeof(struct smb2_preauth_neg_context);
816 /* Round to 8 byte boundary */
817 pneg_ctxt += round_up(sizeof(struct smb2_preauth_neg_context), 8);
818
819 if (conn->cipher_type) {
820 ctxt_size = round_up(ctxt_size, 8);
821 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900822 "assemble SMB2_ENCRYPTION_CAPABILITIES context\n");
Namjae Jeon64b39f42021-03-30 14:25:35 +0900823 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900824 conn->cipher_type);
Namjae Jeone2f34482021-03-16 10:49:09 +0900825 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900826 ctxt_size += sizeof(struct smb2_encryption_neg_context) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900827 /* Round to 8 byte boundary */
828 pneg_ctxt +=
Namjae Jeonaf320a72021-07-21 10:03:19 +0900829 round_up(sizeof(struct smb2_encryption_neg_context) + 2,
Namjae Jeone2f34482021-03-16 10:49:09 +0900830 8);
831 }
832
833 if (conn->compress_algorithm) {
834 ctxt_size = round_up(ctxt_size, 8);
835 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900836 "assemble SMB2_COMPRESSION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900837 /* Temporarily set to SMB3_COMPRESS_NONE */
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900838 build_compression_ctxt((struct smb2_compression_capabilities_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900839 conn->compress_algorithm);
Namjae Jeone2f34482021-03-16 10:49:09 +0900840 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900841 ctxt_size += sizeof(struct smb2_compression_capabilities_context) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900842 /* Round to 8 byte boundary */
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900843 pneg_ctxt += round_up(sizeof(struct smb2_compression_capabilities_context) + 2,
Namjae Jeonaf320a72021-07-21 10:03:19 +0900844 8);
Namjae Jeone2f34482021-03-16 10:49:09 +0900845 }
846
847 if (conn->posix_ext_supported) {
848 ctxt_size = round_up(ctxt_size, 8);
849 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900850 "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900851 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
852 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
853 ctxt_size += sizeof(struct smb2_posix_neg_context);
Namjae Jeon378087c2021-07-21 10:05:53 +0900854 /* Round to 8 byte boundary */
855 pneg_ctxt += round_up(sizeof(struct smb2_posix_neg_context), 8);
856 }
857
858 if (conn->signing_negotiated) {
859 ctxt_size = round_up(ctxt_size, 8);
860 ksmbd_debug(SMB,
861 "assemble SMB2_SIGNING_CAPABILITIES context\n");
862 build_sign_cap_ctxt((struct smb2_signing_capabilities *)pneg_ctxt,
863 conn->signing_algorithm);
864 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
865 ctxt_size += sizeof(struct smb2_signing_capabilities) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900866 }
867
Namjae Jeoncb451722021-11-03 08:08:44 +0900868 inc_rfc1001_len(smb2_buf_len, ctxt_size);
Namjae Jeone2f34482021-03-16 10:49:09 +0900869}
870
Namjae Jeon64b39f42021-03-30 14:25:35 +0900871static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900872 struct smb2_preauth_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900873{
874 __le32 err = STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP;
875
Namjae Jeon070fb212021-05-26 17:57:12 +0900876 if (pneg_ctxt->HashAlgorithms == SMB2_PREAUTH_INTEGRITY_SHA512) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900877 conn->preauth_info->Preauth_HashId =
878 SMB2_PREAUTH_INTEGRITY_SHA512;
879 err = STATUS_SUCCESS;
880 }
881
882 return err;
883}
884
Namjae Jeonaf320a72021-07-21 10:03:19 +0900885static void decode_encrypt_ctxt(struct ksmbd_conn *conn,
886 struct smb2_encryption_neg_context *pneg_ctxt,
887 int len_of_ctxts)
Namjae Jeone2f34482021-03-16 10:49:09 +0900888{
Namjae Jeone2f34482021-03-16 10:49:09 +0900889 int cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900890 int i, cphs_size = cph_cnt * sizeof(__le16);
Namjae Jeone2f34482021-03-16 10:49:09 +0900891
892 conn->cipher_type = 0;
893
Namjae Jeonaf320a72021-07-21 10:03:19 +0900894 if (sizeof(struct smb2_encryption_neg_context) + cphs_size >
895 len_of_ctxts) {
896 pr_err("Invalid cipher count(%d)\n", cph_cnt);
897 return;
898 }
899
Namjae Jeone2f34482021-03-16 10:49:09 +0900900 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION))
Namjae Jeonaf320a72021-07-21 10:03:19 +0900901 return;
Namjae Jeone2f34482021-03-16 10:49:09 +0900902
903 for (i = 0; i < cph_cnt; i++) {
904 if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM ||
Namjae Jeon5a0ca772021-05-06 11:43:37 +0900905 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM ||
906 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM ||
907 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900908 ksmbd_debug(SMB, "Cipher ID = 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +0900909 pneg_ctxt->Ciphers[i]);
Namjae Jeone2f34482021-03-16 10:49:09 +0900910 conn->cipher_type = pneg_ctxt->Ciphers[i];
911 break;
912 }
913 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900914}
915
Marcos Del Sol Vives83912d62021-12-16 11:37:22 +0100916/**
917 * smb3_encryption_negotiated() - checks if server and client agreed on enabling encryption
918 * @conn: smb connection
919 *
920 * Return: true if connection should be encrypted, else false
921 */
922static bool smb3_encryption_negotiated(struct ksmbd_conn *conn)
923{
924 if (!conn->ops->generate_encryptionkey)
925 return false;
926
927 /*
928 * SMB 3.0 and 3.0.2 dialects use the SMB2_GLOBAL_CAP_ENCRYPTION flag.
929 * SMB 3.1.1 uses the cipher_type field.
930 */
931 return (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) ||
932 conn->cipher_type;
933}
934
Namjae Jeonaf320a72021-07-21 10:03:19 +0900935static void decode_compress_ctxt(struct ksmbd_conn *conn,
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900936 struct smb2_compression_capabilities_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900937{
Namjae Jeone2f34482021-03-16 10:49:09 +0900938 conn->compress_algorithm = SMB3_COMPRESS_NONE;
Namjae Jeone2f34482021-03-16 10:49:09 +0900939}
940
Namjae Jeon378087c2021-07-21 10:05:53 +0900941static void decode_sign_cap_ctxt(struct ksmbd_conn *conn,
942 struct smb2_signing_capabilities *pneg_ctxt,
943 int len_of_ctxts)
944{
945 int sign_algo_cnt = le16_to_cpu(pneg_ctxt->SigningAlgorithmCount);
946 int i, sign_alos_size = sign_algo_cnt * sizeof(__le16);
947
948 conn->signing_negotiated = false;
949
950 if (sizeof(struct smb2_signing_capabilities) + sign_alos_size >
951 len_of_ctxts) {
952 pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt);
953 return;
954 }
955
956 for (i = 0; i < sign_algo_cnt; i++) {
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900957 if (pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_HMAC_SHA256_LE ||
958 pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_AES_CMAC_LE) {
Namjae Jeon378087c2021-07-21 10:05:53 +0900959 ksmbd_debug(SMB, "Signing Algorithm ID = 0x%x\n",
960 pneg_ctxt->SigningAlgorithms[i]);
961 conn->signing_negotiated = true;
962 conn->signing_algorithm =
963 pneg_ctxt->SigningAlgorithms[i];
964 break;
965 }
966 }
967}
968
Namjae Jeone2f34482021-03-16 10:49:09 +0900969static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn,
Namjae Jeoncb451722021-11-03 08:08:44 +0900970 struct smb2_negotiate_req *req,
971 int len_of_smb)
Namjae Jeone2f34482021-03-16 10:49:09 +0900972{
Namjae Jeone2f34482021-03-16 10:49:09 +0900973 /* +4 is to account for the RFC1001 len field */
Namjae Jeoncb451722021-11-03 08:08:44 +0900974 struct smb2_neg_context *pctx = (struct smb2_neg_context *)req;
Namjae Jeonaf320a72021-07-21 10:03:19 +0900975 int i = 0, len_of_ctxts;
976 int offset = le32_to_cpu(req->NegotiateContextOffset);
Namjae Jeone2f34482021-03-16 10:49:09 +0900977 int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900978 __le32 status = STATUS_INVALID_PARAMETER;
Namjae Jeone2f34482021-03-16 10:49:09 +0900979
Namjae Jeonaf320a72021-07-21 10:03:19 +0900980 ksmbd_debug(SMB, "decoding %d negotiate contexts\n", neg_ctxt_cnt);
981 if (len_of_smb <= offset) {
982 ksmbd_debug(SMB, "Invalid response: negotiate context offset\n");
983 return status;
984 }
985
986 len_of_ctxts = len_of_smb - offset;
987
Namjae Jeone2f34482021-03-16 10:49:09 +0900988 while (i++ < neg_ctxt_cnt) {
Namjae Jeonaf320a72021-07-21 10:03:19 +0900989 int clen;
990
991 /* check that offset is not beyond end of SMB */
992 if (len_of_ctxts == 0)
993 break;
994
995 if (len_of_ctxts < sizeof(struct smb2_neg_context))
996 break;
997
998 pctx = (struct smb2_neg_context *)((char *)pctx + offset);
999 clen = le16_to_cpu(pctx->DataLength);
1000 if (clen + sizeof(struct smb2_neg_context) > len_of_ctxts)
1001 break;
1002
1003 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001004 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001005 "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001006 if (conn->preauth_info->Preauth_HashId)
1007 break;
1008
1009 status = decode_preauth_ctxt(conn,
Namjae Jeonaf320a72021-07-21 10:03:19 +09001010 (struct smb2_preauth_neg_context *)pctx);
1011 if (status != STATUS_SUCCESS)
1012 break;
1013 } else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001014 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001015 "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001016 if (conn->cipher_type)
1017 break;
1018
Namjae Jeonaf320a72021-07-21 10:03:19 +09001019 decode_encrypt_ctxt(conn,
1020 (struct smb2_encryption_neg_context *)pctx,
1021 len_of_ctxts);
1022 } else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001023 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001024 "deassemble SMB2_COMPRESSION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001025 if (conn->compress_algorithm)
1026 break;
1027
Namjae Jeonaf320a72021-07-21 10:03:19 +09001028 decode_compress_ctxt(conn,
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +09001029 (struct smb2_compression_capabilities_context *)pctx);
Namjae Jeonaf320a72021-07-21 10:03:19 +09001030 } else if (pctx->ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001031 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001032 "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n");
Namjae Jeonaf320a72021-07-21 10:03:19 +09001033 } else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001034 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001035 "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001036 conn->posix_ext_supported = true;
Namjae Jeon378087c2021-07-21 10:05:53 +09001037 } else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES) {
1038 ksmbd_debug(SMB,
1039 "deassemble SMB2_SIGNING_CAPABILITIES context\n");
1040 decode_sign_cap_ctxt(conn,
1041 (struct smb2_signing_capabilities *)pctx,
1042 len_of_ctxts);
Namjae Jeone2f34482021-03-16 10:49:09 +09001043 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001044
Namjae Jeonaf320a72021-07-21 10:03:19 +09001045 /* offsets must be 8 byte aligned */
1046 clen = (clen + 7) & ~0x7;
1047 offset = clen + sizeof(struct smb2_neg_context);
1048 len_of_ctxts -= clen + sizeof(struct smb2_neg_context);
Namjae Jeone2f34482021-03-16 10:49:09 +09001049 }
1050 return status;
1051}
1052
1053/**
1054 * smb2_handle_negotiate() - handler for smb2 negotiate command
1055 * @work: smb work containing smb request buffer
1056 *
1057 * Return: 0
1058 */
1059int smb2_handle_negotiate(struct ksmbd_work *work)
1060{
1061 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09001062 struct smb2_negotiate_req *req = smb2_get_msg(work->request_buf);
1063 struct smb2_negotiate_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001064 int rc = 0;
Namjae Jeon442ff9e2021-09-29 15:44:32 +09001065 unsigned int smb2_buf_len, smb2_neg_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09001066 __le32 status;
1067
1068 ksmbd_debug(SMB, "Received negotiate request\n");
1069 conn->need_neg = false;
1070 if (ksmbd_conn_good(work)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001071 pr_err("conn->tcp_status is already in CifsGood State\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001072 work->send_no_response = 1;
1073 return rc;
1074 }
1075
1076 if (req->DialectCount == 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001077 pr_err("malformed packet\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001078 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1079 rc = -EINVAL;
1080 goto err_out;
1081 }
1082
Namjae Jeon442ff9e2021-09-29 15:44:32 +09001083 smb2_buf_len = get_rfc1002_len(work->request_buf);
Namjae Jeoncb451722021-11-03 08:08:44 +09001084 smb2_neg_size = offsetof(struct smb2_negotiate_req, Dialects);
Namjae Jeon442ff9e2021-09-29 15:44:32 +09001085 if (smb2_neg_size > smb2_buf_len) {
1086 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1087 rc = -EINVAL;
1088 goto err_out;
1089 }
1090
1091 if (conn->dialect == SMB311_PROT_ID) {
1092 unsigned int nego_ctxt_off = le32_to_cpu(req->NegotiateContextOffset);
1093
1094 if (smb2_buf_len < nego_ctxt_off) {
1095 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1096 rc = -EINVAL;
1097 goto err_out;
1098 }
1099
1100 if (smb2_neg_size > nego_ctxt_off) {
1101 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1102 rc = -EINVAL;
1103 goto err_out;
1104 }
1105
1106 if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) >
1107 nego_ctxt_off) {
1108 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1109 rc = -EINVAL;
1110 goto err_out;
1111 }
1112 } else {
1113 if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) >
1114 smb2_buf_len) {
1115 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1116 rc = -EINVAL;
1117 goto err_out;
1118 }
1119 }
1120
Namjae Jeone2f34482021-03-16 10:49:09 +09001121 conn->cli_cap = le32_to_cpu(req->Capabilities);
1122 switch (conn->dialect) {
1123 case SMB311_PROT_ID:
1124 conn->preauth_info =
1125 kzalloc(sizeof(struct preauth_integrity_info),
Namjae Jeon070fb212021-05-26 17:57:12 +09001126 GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09001127 if (!conn->preauth_info) {
1128 rc = -ENOMEM;
1129 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1130 goto err_out;
1131 }
1132
Namjae Jeoncb451722021-11-03 08:08:44 +09001133 status = deassemble_neg_contexts(conn, req,
1134 get_rfc1002_len(work->request_buf));
Namjae Jeone2f34482021-03-16 10:49:09 +09001135 if (status != STATUS_SUCCESS) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001136 pr_err("deassemble_neg_contexts error(0x%x)\n",
1137 status);
Namjae Jeone2f34482021-03-16 10:49:09 +09001138 rsp->hdr.Status = status;
1139 rc = -EINVAL;
1140 goto err_out;
1141 }
1142
1143 rc = init_smb3_11_server(conn);
1144 if (rc < 0) {
1145 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1146 goto err_out;
1147 }
1148
1149 ksmbd_gen_preauth_integrity_hash(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001150 work->request_buf,
1151 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001152 rsp->NegotiateContextOffset =
1153 cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
Namjae Jeoncb451722021-11-03 08:08:44 +09001154 assemble_neg_contexts(conn, rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001155 break;
1156 case SMB302_PROT_ID:
1157 init_smb3_02_server(conn);
1158 break;
1159 case SMB30_PROT_ID:
1160 init_smb3_0_server(conn);
1161 break;
1162 case SMB21_PROT_ID:
1163 init_smb2_1_server(conn);
1164 break;
Namjae Jeone2f34482021-03-16 10:49:09 +09001165 case SMB2X_PROT_ID:
1166 case BAD_PROT_ID:
1167 default:
1168 ksmbd_debug(SMB, "Server dialect :0x%x not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001169 conn->dialect);
Namjae Jeone2f34482021-03-16 10:49:09 +09001170 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
1171 rc = -EINVAL;
1172 goto err_out;
1173 }
1174 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
1175
1176 /* For stats */
1177 conn->connection_type = conn->dialect;
1178
1179 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
1180 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
1181 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
1182
Namjae Jeon51a13872021-09-29 13:09:24 +09001183 memcpy(conn->ClientGUID, req->ClientGUID,
1184 SMB2_CLIENT_GUID_SIZE);
1185 conn->cli_sec_mode = le16_to_cpu(req->SecurityMode);
Namjae Jeone2f34482021-03-16 10:49:09 +09001186
1187 rsp->StructureSize = cpu_to_le16(65);
1188 rsp->DialectRevision = cpu_to_le16(conn->dialect);
1189 /* Not setting conn guid rsp->ServerGUID, as it
1190 * not used by client for identifying server
1191 */
1192 memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE);
1193
1194 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
1195 rsp->ServerStartTime = 0;
1196 ksmbd_debug(SMB, "negotiate context offset %d, count %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001197 le32_to_cpu(rsp->NegotiateContextOffset),
1198 le16_to_cpu(rsp->NegotiateContextCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09001199
1200 rsp->SecurityBufferOffset = cpu_to_le16(128);
1201 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
Namjae Jeoncb451722021-11-03 08:08:44 +09001202 ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) +
1203 le16_to_cpu(rsp->SecurityBufferOffset));
1204 inc_rfc1001_len(work->response_buf, sizeof(struct smb2_negotiate_rsp) -
Namjae Jeon070fb212021-05-26 17:57:12 +09001205 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
1206 AUTH_GSS_LENGTH);
Namjae Jeone2f34482021-03-16 10:49:09 +09001207 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
1208 conn->use_spnego = true;
1209
1210 if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001211 server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) &&
1212 req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09001213 conn->sign = true;
1214 else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) {
1215 server_conf.enforced_signing = true;
1216 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
1217 conn->sign = true;
1218 }
1219
1220 conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode);
1221 ksmbd_conn_set_need_negotiate(work);
1222
1223err_out:
1224 if (rc < 0)
1225 smb2_set_err_rsp(work);
1226
1227 return rc;
1228}
1229
1230static int alloc_preauth_hash(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09001231 struct ksmbd_conn *conn)
Namjae Jeone2f34482021-03-16 10:49:09 +09001232{
1233 if (sess->Preauth_HashValue)
1234 return 0;
1235
kernel test robot86f52972021-04-02 12:17:24 +09001236 sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue,
Namjae Jeon070fb212021-05-26 17:57:12 +09001237 PREAUTH_HASHVALUE_SIZE, GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09001238 if (!sess->Preauth_HashValue)
1239 return -ENOMEM;
1240
Namjae Jeone2f34482021-03-16 10:49:09 +09001241 return 0;
1242}
1243
1244static int generate_preauth_hash(struct ksmbd_work *work)
1245{
1246 struct ksmbd_conn *conn = work->conn;
1247 struct ksmbd_session *sess = work->sess;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001248 u8 *preauth_hash;
Namjae Jeone2f34482021-03-16 10:49:09 +09001249
1250 if (conn->dialect != SMB311_PROT_ID)
1251 return 0;
1252
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001253 if (conn->binding) {
1254 struct preauth_session *preauth_sess;
1255
1256 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
1257 if (!preauth_sess) {
1258 preauth_sess = ksmbd_preauth_session_alloc(conn, sess->id);
1259 if (!preauth_sess)
1260 return -ENOMEM;
1261 }
1262
1263 preauth_hash = preauth_sess->Preauth_HashValue;
1264 } else {
1265 if (!sess->Preauth_HashValue)
1266 if (alloc_preauth_hash(sess, conn))
1267 return -ENOMEM;
1268 preauth_hash = sess->Preauth_HashValue;
Namjae Jeone2f34482021-03-16 10:49:09 +09001269 }
1270
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001271 ksmbd_gen_preauth_integrity_hash(conn, work->request_buf, preauth_hash);
Namjae Jeone2f34482021-03-16 10:49:09 +09001272 return 0;
1273}
1274
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001275static int decode_negotiation_token(struct ksmbd_conn *conn,
1276 struct negotiate_message *negblob,
1277 size_t sz)
Namjae Jeone2f34482021-03-16 10:49:09 +09001278{
Namjae Jeone2f34482021-03-16 10:49:09 +09001279 if (!conn->use_spnego)
1280 return -EINVAL;
1281
Hyunchul Leefad41612021-04-19 17:26:15 +09001282 if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) {
1283 if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001284 conn->auth_mechs |= KSMBD_AUTH_NTLMSSP;
1285 conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP;
1286 conn->use_spnego = false;
1287 }
1288 }
1289 return 0;
1290}
1291
1292static int ntlm_negotiate(struct ksmbd_work *work,
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001293 struct negotiate_message *negblob,
1294 size_t negblob_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09001295{
Namjae Jeoncb451722021-11-03 08:08:44 +09001296 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001297 struct challenge_message *chgblob;
1298 unsigned char *spnego_blob = NULL;
1299 u16 spnego_blob_len;
1300 char *neg_blob;
1301 int sz, rc;
1302
1303 ksmbd_debug(SMB, "negotiate phase\n");
Namjae Jeonce53d362021-12-15 14:57:27 +09001304 rc = ksmbd_decode_ntlmssp_neg_blob(negblob, negblob_len, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001305 if (rc)
1306 return rc;
1307
1308 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1309 chgblob =
1310 (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz);
1311 memset(chgblob, 0, sizeof(struct challenge_message));
1312
1313 if (!work->conn->use_spnego) {
Namjae Jeonce53d362021-12-15 14:57:27 +09001314 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001315 if (sz < 0)
1316 return -ENOMEM;
1317
1318 rsp->SecurityBufferLength = cpu_to_le16(sz);
1319 return 0;
1320 }
1321
1322 sz = sizeof(struct challenge_message);
1323 sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6;
1324
1325 neg_blob = kzalloc(sz, GFP_KERNEL);
1326 if (!neg_blob)
1327 return -ENOMEM;
1328
1329 chgblob = (struct challenge_message *)neg_blob;
Namjae Jeonce53d362021-12-15 14:57:27 +09001330 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001331 if (sz < 0) {
1332 rc = -ENOMEM;
1333 goto out;
1334 }
1335
Namjae Jeon070fb212021-05-26 17:57:12 +09001336 rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len,
1337 neg_blob, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09001338 if (rc) {
1339 rc = -ENOMEM;
1340 goto out;
1341 }
1342
1343 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1344 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
1345 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1346
1347out:
1348 kfree(spnego_blob);
1349 kfree(neg_blob);
1350 return rc;
1351}
1352
1353static struct authenticate_message *user_authblob(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001354 struct smb2_sess_setup_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +09001355{
1356 int sz;
1357
1358 if (conn->use_spnego && conn->mechToken)
1359 return (struct authenticate_message *)conn->mechToken;
1360
1361 sz = le16_to_cpu(req->SecurityBufferOffset);
1362 return (struct authenticate_message *)((char *)&req->hdr.ProtocolId
1363 + sz);
1364}
1365
1366static struct ksmbd_user *session_user(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001367 struct smb2_sess_setup_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +09001368{
1369 struct authenticate_message *authblob;
1370 struct ksmbd_user *user;
1371 char *name;
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001372 unsigned int auth_msg_len, name_off, name_len, secbuf_len;
Namjae Jeone2f34482021-03-16 10:49:09 +09001373
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001374 secbuf_len = le16_to_cpu(req->SecurityBufferLength);
1375 if (secbuf_len < sizeof(struct authenticate_message)) {
1376 ksmbd_debug(SMB, "blob len %d too small\n", secbuf_len);
1377 return NULL;
1378 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001379 authblob = user_authblob(conn, req);
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001380 name_off = le32_to_cpu(authblob->UserName.BufferOffset);
1381 name_len = le16_to_cpu(authblob->UserName.Length);
1382 auth_msg_len = le16_to_cpu(req->SecurityBufferOffset) + secbuf_len;
1383
1384 if (auth_msg_len < (u64)name_off + name_len)
1385 return NULL;
1386
1387 name = smb_strndup_from_utf16((const char *)authblob + name_off,
1388 name_len,
Namjae Jeone2f34482021-03-16 10:49:09 +09001389 true,
1390 conn->local_nls);
1391 if (IS_ERR(name)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001392 pr_err("cannot allocate memory\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001393 return NULL;
1394 }
1395
1396 ksmbd_debug(SMB, "session setup request for user %s\n", name);
1397 user = ksmbd_login_user(name);
1398 kfree(name);
1399 return user;
1400}
1401
1402static int ntlm_authenticate(struct ksmbd_work *work)
1403{
Namjae Jeoncb451722021-11-03 08:08:44 +09001404 struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf);
1405 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001406 struct ksmbd_conn *conn = work->conn;
1407 struct ksmbd_session *sess = work->sess;
1408 struct channel *chann = NULL;
1409 struct ksmbd_user *user;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001410 u64 prev_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09001411 int sz, rc;
1412
1413 ksmbd_debug(SMB, "authenticate phase\n");
1414 if (conn->use_spnego) {
1415 unsigned char *spnego_blob;
1416 u16 spnego_blob_len;
1417
1418 rc = build_spnego_ntlmssp_auth_blob(&spnego_blob,
1419 &spnego_blob_len,
1420 0);
1421 if (rc)
1422 return -ENOMEM;
1423
1424 sz = le16_to_cpu(rsp->SecurityBufferOffset);
Namjae Jeon64b39f42021-03-30 14:25:35 +09001425 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001426 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1427 kfree(spnego_blob);
Namjae Jeoncb451722021-11-03 08:08:44 +09001428 inc_rfc1001_len(work->response_buf, spnego_blob_len - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001429 }
1430
1431 user = session_user(conn, req);
1432 if (!user) {
1433 ksmbd_debug(SMB, "Unknown user name or an error\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001434 return -EPERM;
Namjae Jeone2f34482021-03-16 10:49:09 +09001435 }
1436
1437 /* Check for previous session */
1438 prev_id = le64_to_cpu(req->PreviousSessionId);
1439 if (prev_id && prev_id != sess->id)
1440 destroy_previous_session(user, prev_id);
1441
1442 if (sess->state == SMB2_SESSION_VALID) {
1443 /*
1444 * Reuse session if anonymous try to connect
1445 * on reauthetication.
1446 */
1447 if (ksmbd_anonymous_user(user)) {
1448 ksmbd_free_user(user);
1449 return 0;
1450 }
Namjae Jeona58b45a2021-12-16 10:26:43 +09001451
1452 if (!ksmbd_compare_user(sess->user, user)) {
1453 ksmbd_free_user(user);
1454 return -EPERM;
1455 }
1456 ksmbd_free_user(user);
1457 } else {
1458 sess->user = user;
Namjae Jeone2f34482021-03-16 10:49:09 +09001459 }
1460
Namjae Jeone2f34482021-03-16 10:49:09 +09001461 if (user_guest(sess->user)) {
1462 if (conn->sign) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001463 ksmbd_debug(SMB, "Guest login not allowed when signing enabled\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001464 return -EPERM;
Namjae Jeone2f34482021-03-16 10:49:09 +09001465 }
1466
1467 rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE;
1468 } else {
1469 struct authenticate_message *authblob;
1470
1471 authblob = user_authblob(conn, req);
1472 sz = le16_to_cpu(req->SecurityBufferLength);
Namjae Jeonce53d362021-12-15 14:57:27 +09001473 rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, conn, sess);
Namjae Jeone2f34482021-03-16 10:49:09 +09001474 if (rc) {
1475 set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD);
1476 ksmbd_debug(SMB, "authentication failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001477 return -EPERM;
Namjae Jeone2f34482021-03-16 10:49:09 +09001478 }
1479
1480 /*
1481 * If session state is SMB2_SESSION_VALID, We can assume
1482 * that it is reauthentication. And the user/password
1483 * has been verified, so return it here.
1484 */
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001485 if (sess->state == SMB2_SESSION_VALID) {
1486 if (conn->binding)
1487 goto binding_session;
Namjae Jeone2f34482021-03-16 10:49:09 +09001488 return 0;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001489 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001490
1491 if ((conn->sign || server_conf.enforced_signing) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001492 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
Namjae Jeone2f34482021-03-16 10:49:09 +09001493 sess->sign = true;
1494
Marcos Del Sol Vives83912d62021-12-16 11:37:22 +01001495 if (smb3_encryption_negotiated(conn) &&
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001496 !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001497 rc = conn->ops->generate_encryptionkey(sess);
1498 if (rc) {
1499 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001500 "SMB3 encryption key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001501 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001502 }
1503 sess->enc = true;
1504 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1505 /*
1506 * signing is disable if encryption is enable
1507 * on this session
1508 */
1509 sess->sign = false;
1510 }
1511 }
1512
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001513binding_session:
Namjae Jeone2f34482021-03-16 10:49:09 +09001514 if (conn->dialect >= SMB30_PROT_ID) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001515 chann = lookup_chann_list(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001516 if (!chann) {
1517 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1518 if (!chann)
1519 return -ENOMEM;
1520
1521 chann->conn = conn;
1522 INIT_LIST_HEAD(&chann->chann_list);
1523 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1524 }
1525 }
1526
1527 if (conn->ops->generate_signingkey) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001528 rc = conn->ops->generate_signingkey(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001529 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001530 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001531 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001532 }
1533 }
1534
Namjae Jeon51a13872021-09-29 13:09:24 +09001535 if (!ksmbd_conn_lookup_dialect(conn)) {
1536 pr_err("fail to verify the dialect\n");
1537 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001538 }
1539 return 0;
1540}
1541
1542#ifdef CONFIG_SMB_SERVER_KERBEROS5
1543static int krb5_authenticate(struct ksmbd_work *work)
1544{
Namjae Jeoncb451722021-11-03 08:08:44 +09001545 struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf);
1546 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001547 struct ksmbd_conn *conn = work->conn;
1548 struct ksmbd_session *sess = work->sess;
1549 char *in_blob, *out_blob;
1550 struct channel *chann = NULL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001551 u64 prev_sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09001552 int in_len, out_len;
1553 int retval;
1554
1555 in_blob = (char *)&req->hdr.ProtocolId +
1556 le16_to_cpu(req->SecurityBufferOffset);
1557 in_len = le16_to_cpu(req->SecurityBufferLength);
1558 out_blob = (char *)&rsp->hdr.ProtocolId +
1559 le16_to_cpu(rsp->SecurityBufferOffset);
1560 out_len = work->response_sz -
Namjae Jeoncb451722021-11-03 08:08:44 +09001561 (le16_to_cpu(rsp->SecurityBufferOffset) + 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09001562
1563 /* Check previous session */
1564 prev_sess_id = le64_to_cpu(req->PreviousSessionId);
1565 if (prev_sess_id && prev_sess_id != sess->id)
1566 destroy_previous_session(sess->user, prev_sess_id);
1567
1568 if (sess->state == SMB2_SESSION_VALID)
1569 ksmbd_free_user(sess->user);
1570
1571 retval = ksmbd_krb5_authenticate(sess, in_blob, in_len,
Namjae Jeon070fb212021-05-26 17:57:12 +09001572 out_blob, &out_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001573 if (retval) {
1574 ksmbd_debug(SMB, "krb5 authentication failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001575 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001576 }
1577 rsp->SecurityBufferLength = cpu_to_le16(out_len);
Namjae Jeoncb451722021-11-03 08:08:44 +09001578 inc_rfc1001_len(work->response_buf, out_len - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001579
1580 if ((conn->sign || server_conf.enforced_signing) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001581 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
Namjae Jeone2f34482021-03-16 10:49:09 +09001582 sess->sign = true;
1583
Marcos Del Sol Vives83912d62021-12-16 11:37:22 +01001584 if (smb3_encryption_negotiated(conn)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001585 retval = conn->ops->generate_encryptionkey(sess);
1586 if (retval) {
1587 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001588 "SMB3 encryption key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001589 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001590 }
1591 sess->enc = true;
1592 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1593 sess->sign = false;
1594 }
1595
1596 if (conn->dialect >= SMB30_PROT_ID) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001597 chann = lookup_chann_list(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001598 if (!chann) {
1599 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1600 if (!chann)
1601 return -ENOMEM;
1602
1603 chann->conn = conn;
1604 INIT_LIST_HEAD(&chann->chann_list);
1605 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1606 }
1607 }
1608
1609 if (conn->ops->generate_signingkey) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001610 retval = conn->ops->generate_signingkey(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001611 if (retval) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001612 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001613 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001614 }
1615 }
1616
Namjae Jeon51a13872021-09-29 13:09:24 +09001617 if (!ksmbd_conn_lookup_dialect(conn)) {
1618 pr_err("fail to verify the dialect\n");
1619 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001620 }
1621 return 0;
1622}
1623#else
1624static int krb5_authenticate(struct ksmbd_work *work)
1625{
1626 return -EOPNOTSUPP;
1627}
1628#endif
1629
1630int smb2_sess_setup(struct ksmbd_work *work)
1631{
1632 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09001633 struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf);
1634 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001635 struct ksmbd_session *sess;
1636 struct negotiate_message *negblob;
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001637 unsigned int negblob_len, negblob_off;
Namjae Jeone2f34482021-03-16 10:49:09 +09001638 int rc = 0;
1639
1640 ksmbd_debug(SMB, "Received request for session setup\n");
1641
1642 rsp->StructureSize = cpu_to_le16(9);
1643 rsp->SessionFlags = 0;
1644 rsp->SecurityBufferOffset = cpu_to_le16(72);
1645 rsp->SecurityBufferLength = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09001646 inc_rfc1001_len(work->response_buf, 9);
Namjae Jeone2f34482021-03-16 10:49:09 +09001647
1648 if (!req->hdr.SessionId) {
1649 sess = ksmbd_smb2_session_create();
1650 if (!sess) {
1651 rc = -ENOMEM;
1652 goto out_err;
1653 }
1654 rsp->hdr.SessionId = cpu_to_le64(sess->id);
1655 ksmbd_session_register(conn, sess);
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001656 } else if (conn->dialect >= SMB30_PROT_ID &&
1657 (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
1658 req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) {
1659 u64 sess_id = le64_to_cpu(req->hdr.SessionId);
1660
1661 sess = ksmbd_session_lookup_slowpath(sess_id);
1662 if (!sess) {
1663 rc = -ENOENT;
1664 goto out_err;
1665 }
1666
1667 if (conn->dialect != sess->conn->dialect) {
1668 rc = -EINVAL;
1669 goto out_err;
1670 }
1671
1672 if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) {
1673 rc = -EINVAL;
1674 goto out_err;
1675 }
1676
1677 if (strncmp(conn->ClientGUID, sess->conn->ClientGUID,
1678 SMB2_CLIENT_GUID_SIZE)) {
1679 rc = -ENOENT;
1680 goto out_err;
1681 }
1682
1683 if (sess->state == SMB2_SESSION_IN_PROGRESS) {
1684 rc = -EACCES;
1685 goto out_err;
1686 }
1687
1688 if (sess->state == SMB2_SESSION_EXPIRED) {
1689 rc = -EFAULT;
1690 goto out_err;
1691 }
1692
1693 if (ksmbd_session_lookup(conn, sess_id)) {
1694 rc = -EACCES;
1695 goto out_err;
1696 }
1697
1698 conn->binding = true;
1699 } else if ((conn->dialect < SMB30_PROT_ID ||
1700 server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
1701 (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
Colin Ian King4951a842021-07-06 13:05:01 +01001702 sess = NULL;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001703 rc = -EACCES;
1704 goto out_err;
Namjae Jeone2f34482021-03-16 10:49:09 +09001705 } else {
1706 sess = ksmbd_session_lookup(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001707 le64_to_cpu(req->hdr.SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09001708 if (!sess) {
1709 rc = -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001710 goto out_err;
1711 }
1712 }
1713 work->sess = sess;
1714
1715 if (sess->state == SMB2_SESSION_EXPIRED)
1716 sess->state = SMB2_SESSION_IN_PROGRESS;
1717
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001718 negblob_off = le16_to_cpu(req->SecurityBufferOffset);
1719 negblob_len = le16_to_cpu(req->SecurityBufferLength);
Namjae Jeoncb451722021-11-03 08:08:44 +09001720 if (negblob_off < offsetof(struct smb2_sess_setup_req, Buffer) ||
Christophe JAILLETf8fbfd82021-11-07 16:22:57 +01001721 negblob_len < offsetof(struct negotiate_message, NegotiateFlags)) {
1722 rc = -EINVAL;
1723 goto out_err;
1724 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001725
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001726 negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId +
1727 negblob_off);
1728
1729 if (decode_negotiation_token(conn, negblob, negblob_len) == 0) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001730 if (conn->mechToken)
1731 negblob = (struct negotiate_message *)conn->mechToken;
1732 }
1733
1734 if (server_conf.auth_mechs & conn->auth_mechs) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001735 rc = generate_preauth_hash(work);
1736 if (rc)
1737 goto out_err;
1738
Namjae Jeone2f34482021-03-16 10:49:09 +09001739 if (conn->preferred_auth_mech &
1740 (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001741 rc = krb5_authenticate(work);
1742 if (rc) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001743 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001744 goto out_err;
1745 }
1746
1747 ksmbd_conn_set_good(work);
1748 sess->state = SMB2_SESSION_VALID;
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001749 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001750 sess->Preauth_HashValue = NULL;
1751 } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001752 if (negblob->MessageType == NtLmNegotiate) {
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001753 rc = ntlm_negotiate(work, negblob, negblob_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001754 if (rc)
1755 goto out_err;
1756 rsp->hdr.Status =
1757 STATUS_MORE_PROCESSING_REQUIRED;
1758 /*
1759 * Note: here total size -1 is done as an
1760 * adjustment for 0 size blob
1761 */
Namjae Jeoncb451722021-11-03 08:08:44 +09001762 inc_rfc1001_len(work->response_buf,
1763 le16_to_cpu(rsp->SecurityBufferLength) - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001764
1765 } else if (negblob->MessageType == NtLmAuthenticate) {
1766 rc = ntlm_authenticate(work);
1767 if (rc)
1768 goto out_err;
1769
1770 ksmbd_conn_set_good(work);
1771 sess->state = SMB2_SESSION_VALID;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001772 if (conn->binding) {
1773 struct preauth_session *preauth_sess;
1774
1775 preauth_sess =
1776 ksmbd_preauth_session_lookup(conn, sess->id);
1777 if (preauth_sess) {
1778 list_del(&preauth_sess->preauth_entry);
1779 kfree(preauth_sess);
1780 }
1781 }
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001782 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001783 sess->Preauth_HashValue = NULL;
1784 }
1785 } else {
1786 /* TODO: need one more negotiation */
Namjae Jeonbde16942021-06-28 15:23:19 +09001787 pr_err("Not support the preferred authentication\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001788 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001789 }
1790 } else {
Namjae Jeonbde16942021-06-28 15:23:19 +09001791 pr_err("Not support authentication\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001792 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001793 }
1794
1795out_err:
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001796 if (rc == -EINVAL)
1797 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1798 else if (rc == -ENOENT)
1799 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1800 else if (rc == -EACCES)
1801 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
1802 else if (rc == -EFAULT)
1803 rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED;
Namjae Jeon58090b12021-07-16 14:52:09 +09001804 else if (rc == -ENOMEM)
1805 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001806 else if (rc)
1807 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1808
Namjae Jeone2f34482021-03-16 10:49:09 +09001809 if (conn->use_spnego && conn->mechToken) {
1810 kfree(conn->mechToken);
1811 conn->mechToken = NULL;
1812 }
1813
Namjae Jeon621be842021-10-13 17:28:31 +09001814 if (rc < 0) {
1815 /*
1816 * SecurityBufferOffset should be set to zero
1817 * in session setup error response.
1818 */
1819 rsp->SecurityBufferOffset = 0;
1820
1821 if (sess) {
1822 bool try_delay = false;
1823
1824 /*
1825 * To avoid dictionary attacks (repeated session setups rapidly sent) to
1826 * connect to server, ksmbd make a delay of a 5 seconds on session setup
1827 * failure to make it harder to send enough random connection requests
1828 * to break into a server.
1829 */
1830 if (sess->user && sess->user->flags & KSMBD_USER_FLAG_DELAY_SESSION)
1831 try_delay = true;
1832
1833 ksmbd_session_destroy(sess);
1834 work->sess = NULL;
1835 if (try_delay)
1836 ssleep(5);
1837 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001838 }
1839
1840 return rc;
1841}
1842
1843/**
1844 * smb2_tree_connect() - handler for smb2 tree connect command
1845 * @work: smb work containing smb request buffer
1846 *
1847 * Return: 0 on success, otherwise error
1848 */
1849int smb2_tree_connect(struct ksmbd_work *work)
1850{
1851 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09001852 struct smb2_tree_connect_req *req = smb2_get_msg(work->request_buf);
1853 struct smb2_tree_connect_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001854 struct ksmbd_session *sess = work->sess;
1855 char *treename = NULL, *name = NULL;
1856 struct ksmbd_tree_conn_status status;
1857 struct ksmbd_share_config *share;
1858 int rc = -EINVAL;
1859
1860 treename = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09001861 le16_to_cpu(req->PathLength), true,
1862 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09001863 if (IS_ERR(treename)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001864 pr_err("treename is NULL\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001865 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1866 goto out_err1;
1867 }
1868
Stephen Rothwell36ba3862021-03-17 17:01:15 +09001869 name = ksmbd_extract_sharename(treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001870 if (IS_ERR(name)) {
1871 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1872 goto out_err1;
1873 }
1874
1875 ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001876 name, treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001877
1878 status = ksmbd_tree_conn_connect(sess, name);
1879 if (status.ret == KSMBD_TREE_CONN_STATUS_OK)
1880 rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id);
1881 else
1882 goto out_err1;
1883
1884 share = status.tree_conn->share_conf;
1885 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
1886 ksmbd_debug(SMB, "IPC share path request\n");
1887 rsp->ShareType = SMB2_SHARE_TYPE_PIPE;
1888 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1889 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE |
1890 FILE_DELETE_LE | FILE_READ_CONTROL_LE |
1891 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1892 FILE_SYNCHRONIZE_LE;
1893 } else {
1894 rsp->ShareType = SMB2_SHARE_TYPE_DISK;
1895 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1896 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE;
1897 if (test_tree_conn_flag(status.tree_conn,
1898 KSMBD_TREE_CONN_FLAG_WRITABLE)) {
1899 rsp->MaximalAccess |= FILE_WRITE_DATA_LE |
1900 FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE |
Wan Jiabing3aefd542021-06-07 12:54:32 +08001901 FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE |
1902 FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE |
1903 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1904 FILE_SYNCHRONIZE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09001905 }
1906 }
1907
1908 status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1909 if (conn->posix_ext_supported)
1910 status.tree_conn->posix_extensions = true;
1911
1912out_err1:
1913 rsp->StructureSize = cpu_to_le16(16);
1914 rsp->Capabilities = 0;
1915 rsp->Reserved = 0;
1916 /* default manual caching */
1917 rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING;
Namjae Jeoncb451722021-11-03 08:08:44 +09001918 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09001919
1920 if (!IS_ERR(treename))
1921 kfree(treename);
1922 if (!IS_ERR(name))
1923 kfree(name);
1924
1925 switch (status.ret) {
1926 case KSMBD_TREE_CONN_STATUS_OK:
1927 rsp->hdr.Status = STATUS_SUCCESS;
1928 rc = 0;
1929 break;
1930 case KSMBD_TREE_CONN_STATUS_NO_SHARE:
1931 rsp->hdr.Status = STATUS_BAD_NETWORK_PATH;
1932 break;
1933 case -ENOMEM:
1934 case KSMBD_TREE_CONN_STATUS_NOMEM:
1935 rsp->hdr.Status = STATUS_NO_MEMORY;
1936 break;
1937 case KSMBD_TREE_CONN_STATUS_ERROR:
1938 case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS:
1939 case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS:
1940 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1941 break;
1942 case -EINVAL:
1943 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1944 break;
1945 default:
1946 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1947 }
1948
1949 return rc;
1950}
1951
1952/**
1953 * smb2_create_open_flags() - convert smb open flags to unix open flags
1954 * @file_present: is file already present
1955 * @access: file access flags
1956 * @disposition: file disposition flags
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001957 * @may_flags: set with MAY_ flags
Namjae Jeone2f34482021-03-16 10:49:09 +09001958 *
1959 * Return: file open flags
1960 */
1961static int smb2_create_open_flags(bool file_present, __le32 access,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001962 __le32 disposition,
1963 int *may_flags)
Namjae Jeone2f34482021-03-16 10:49:09 +09001964{
1965 int oflags = O_NONBLOCK | O_LARGEFILE;
1966
1967 if (access & FILE_READ_DESIRED_ACCESS_LE &&
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001968 access & FILE_WRITE_DESIRE_ACCESS_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001969 oflags |= O_RDWR;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001970 *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE;
1971 } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001972 oflags |= O_WRONLY;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001973 *may_flags = MAY_OPEN | MAY_WRITE;
1974 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09001975 oflags |= O_RDONLY;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001976 *may_flags = MAY_OPEN | MAY_READ;
1977 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001978
1979 if (access == FILE_READ_ATTRIBUTES_LE)
1980 oflags |= O_PATH;
1981
1982 if (file_present) {
1983 switch (disposition & FILE_CREATE_MASK_LE) {
1984 case FILE_OPEN_LE:
1985 case FILE_CREATE_LE:
1986 break;
1987 case FILE_SUPERSEDE_LE:
1988 case FILE_OVERWRITE_LE:
1989 case FILE_OVERWRITE_IF_LE:
1990 oflags |= O_TRUNC;
1991 break;
1992 default:
1993 break;
1994 }
1995 } else {
1996 switch (disposition & FILE_CREATE_MASK_LE) {
1997 case FILE_SUPERSEDE_LE:
1998 case FILE_CREATE_LE:
1999 case FILE_OPEN_IF_LE:
2000 case FILE_OVERWRITE_IF_LE:
2001 oflags |= O_CREAT;
2002 break;
2003 case FILE_OPEN_LE:
2004 case FILE_OVERWRITE_LE:
2005 oflags &= ~O_CREAT;
2006 break;
2007 default:
2008 break;
2009 }
2010 }
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002011
Namjae Jeone2f34482021-03-16 10:49:09 +09002012 return oflags;
2013}
2014
2015/**
2016 * smb2_tree_disconnect() - handler for smb tree connect request
2017 * @work: smb work containing request buffer
2018 *
2019 * Return: 0
2020 */
2021int smb2_tree_disconnect(struct ksmbd_work *work)
2022{
Namjae Jeoncb451722021-11-03 08:08:44 +09002023 struct smb2_tree_disconnect_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002024 struct ksmbd_session *sess = work->sess;
2025 struct ksmbd_tree_connect *tcon = work->tcon;
2026
2027 rsp->StructureSize = cpu_to_le16(4);
Namjae Jeoncb451722021-11-03 08:08:44 +09002028 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09002029
2030 ksmbd_debug(SMB, "request\n");
2031
2032 if (!tcon) {
Namjae Jeoncb451722021-11-03 08:08:44 +09002033 struct smb2_tree_disconnect_req *req =
2034 smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002035
2036 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2037 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2038 smb2_set_err_rsp(work);
2039 return 0;
2040 }
2041
2042 ksmbd_close_tree_conn_fds(work);
2043 ksmbd_tree_conn_disconnect(sess, tcon);
2044 return 0;
2045}
2046
2047/**
2048 * smb2_session_logoff() - handler for session log off request
2049 * @work: smb work containing request buffer
2050 *
2051 * Return: 0
2052 */
2053int smb2_session_logoff(struct ksmbd_work *work)
2054{
2055 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09002056 struct smb2_logoff_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002057 struct ksmbd_session *sess = work->sess;
2058
2059 rsp->StructureSize = cpu_to_le16(4);
Namjae Jeoncb451722021-11-03 08:08:44 +09002060 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09002061
2062 ksmbd_debug(SMB, "request\n");
2063
Namjae Jeone2f34482021-03-16 10:49:09 +09002064 /* setting CifsExiting here may race with start_tcp_sess */
2065 ksmbd_conn_set_need_reconnect(work);
2066 ksmbd_close_session_fds(work);
2067 ksmbd_conn_wait_idle(conn);
2068
2069 if (ksmbd_tree_conn_session_logoff(sess)) {
Namjae Jeoncb451722021-11-03 08:08:44 +09002070 struct smb2_logoff_req *req = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002071
2072 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2073 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2074 smb2_set_err_rsp(work);
2075 return 0;
2076 }
2077
2078 ksmbd_destroy_file_table(&sess->file_table);
2079 sess->state = SMB2_SESSION_EXPIRED;
2080
2081 ksmbd_free_user(sess->user);
2082 sess->user = NULL;
2083
2084 /* let start_tcp_sess free connection info now */
2085 ksmbd_conn_set_need_negotiate(work);
2086 return 0;
2087}
2088
2089/**
2090 * create_smb2_pipe() - create IPC pipe
2091 * @work: smb work containing request buffer
2092 *
2093 * Return: 0 on success, otherwise error
2094 */
2095static noinline int create_smb2_pipe(struct ksmbd_work *work)
2096{
Namjae Jeoncb451722021-11-03 08:08:44 +09002097 struct smb2_create_rsp *rsp = smb2_get_msg(work->response_buf);
2098 struct smb2_create_req *req = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002099 int id;
2100 int err;
2101 char *name;
2102
2103 name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09002104 1, work->conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09002105 if (IS_ERR(name)) {
2106 rsp->hdr.Status = STATUS_NO_MEMORY;
2107 err = PTR_ERR(name);
2108 goto out;
2109 }
2110
2111 id = ksmbd_session_rpc_open(work->sess, name);
Marios Makassikis79caa962021-05-06 11:38:35 +09002112 if (id < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002113 pr_err("Unable to open RPC pipe: %d\n", id);
Marios Makassikis79caa962021-05-06 11:38:35 +09002114 err = id;
2115 goto out;
2116 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002117
Marios Makassikis79caa962021-05-06 11:38:35 +09002118 rsp->hdr.Status = STATUS_SUCCESS;
Namjae Jeone2f34482021-03-16 10:49:09 +09002119 rsp->StructureSize = cpu_to_le16(89);
2120 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002121 rsp->Flags = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002122 rsp->CreateAction = cpu_to_le32(FILE_OPENED);
2123
2124 rsp->CreationTime = cpu_to_le64(0);
2125 rsp->LastAccessTime = cpu_to_le64(0);
2126 rsp->ChangeTime = cpu_to_le64(0);
2127 rsp->AllocationSize = cpu_to_le64(0);
2128 rsp->EndofFile = cpu_to_le64(0);
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002129 rsp->FileAttributes = FILE_ATTRIBUTE_NORMAL_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09002130 rsp->Reserved2 = 0;
2131 rsp->VolatileFileId = cpu_to_le64(id);
2132 rsp->PersistentFileId = 0;
2133 rsp->CreateContextsOffset = 0;
2134 rsp->CreateContextsLength = 0;
2135
Namjae Jeoncb451722021-11-03 08:08:44 +09002136 inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/
Namjae Jeone2f34482021-03-16 10:49:09 +09002137 kfree(name);
2138 return 0;
2139
2140out:
Marios Makassikis79caa962021-05-06 11:38:35 +09002141 switch (err) {
2142 case -EINVAL:
2143 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2144 break;
2145 case -ENOSPC:
2146 case -ENOMEM:
2147 rsp->hdr.Status = STATUS_NO_MEMORY;
2148 break;
2149 }
2150
2151 if (!IS_ERR(name))
2152 kfree(name);
2153
Namjae Jeone2f34482021-03-16 10:49:09 +09002154 smb2_set_err_rsp(work);
2155 return err;
2156}
2157
Namjae Jeone2f34482021-03-16 10:49:09 +09002158/**
2159 * smb2_set_ea() - handler for setting extended attributes using set
2160 * info command
2161 * @eabuf: set info command buffer
Namjae Jeon9496e262021-09-29 15:41:48 +09002162 * @buf_len: set info command buffer length
Namjae Jeone2f34482021-03-16 10:49:09 +09002163 * @path: dentry path for get ea
2164 *
2165 * Return: 0 on success, otherwise error
2166 */
Namjae Jeon9496e262021-09-29 15:41:48 +09002167static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len,
2168 struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002169{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002170 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002171 char *attr_name = NULL, *value;
2172 int rc = 0;
Namjae Jeon9496e262021-09-29 15:41:48 +09002173 unsigned int next = 0;
2174
2175 if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength +
2176 le16_to_cpu(eabuf->EaValueLength))
2177 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002178
2179 attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL);
2180 if (!attr_name)
2181 return -ENOMEM;
2182
2183 do {
2184 if (!eabuf->EaNameLength)
2185 goto next;
2186
2187 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002188 "name : <%s>, name_len : %u, value_len : %u, next : %u\n",
2189 eabuf->name, eabuf->EaNameLength,
2190 le16_to_cpu(eabuf->EaValueLength),
2191 le32_to_cpu(eabuf->NextEntryOffset));
Namjae Jeone2f34482021-03-16 10:49:09 +09002192
2193 if (eabuf->EaNameLength >
Namjae Jeon070fb212021-05-26 17:57:12 +09002194 (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002195 rc = -EINVAL;
2196 break;
2197 }
2198
2199 memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2200 memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002201 eabuf->EaNameLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09002202 attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0';
2203 value = (char *)&eabuf->name + eabuf->EaNameLength + 1;
2204
2205 if (!eabuf->EaValueLength) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002206 rc = ksmbd_vfs_casexattr_len(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002207 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002208 attr_name,
2209 XATTR_USER_PREFIX_LEN +
2210 eabuf->EaNameLength);
2211
2212 /* delete the EA only when it exits */
2213 if (rc > 0) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002214 rc = ksmbd_vfs_remove_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002215 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002216 attr_name);
2217
2218 if (rc < 0) {
2219 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002220 "remove xattr failed(%d)\n",
2221 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002222 break;
2223 }
2224 }
2225
2226 /* if the EA doesn't exist, just do nothing. */
2227 rc = 0;
2228 } else {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002229 rc = ksmbd_vfs_setxattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002230 path->dentry, attr_name, value,
Namjae Jeon070fb212021-05-26 17:57:12 +09002231 le16_to_cpu(eabuf->EaValueLength), 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002232 if (rc < 0) {
2233 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002234 "ksmbd_vfs_setxattr is failed(%d)\n",
2235 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002236 break;
2237 }
2238 }
2239
2240next:
2241 next = le32_to_cpu(eabuf->NextEntryOffset);
Namjae Jeon9496e262021-09-29 15:41:48 +09002242 if (next == 0 || buf_len < next)
2243 break;
2244 buf_len -= next;
Namjae Jeone2f34482021-03-16 10:49:09 +09002245 eabuf = (struct smb2_ea_info *)((char *)eabuf + next);
Namjae Jeon9496e262021-09-29 15:41:48 +09002246 if (next < (u32)eabuf->EaNameLength + le16_to_cpu(eabuf->EaValueLength))
2247 break;
2248
Namjae Jeone2f34482021-03-16 10:49:09 +09002249 } while (next != 0);
2250
2251 kfree(attr_name);
2252 return rc;
2253}
2254
Namjae Jeone2f34482021-03-16 10:49:09 +09002255static noinline int smb2_set_stream_name_xattr(struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002256 struct ksmbd_file *fp,
2257 char *stream_name, int s_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09002258{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002259 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002260 size_t xattr_stream_size;
2261 char *xattr_stream_name;
2262 int rc;
2263
2264 rc = ksmbd_vfs_xattr_stream_name(stream_name,
2265 &xattr_stream_name,
2266 &xattr_stream_size,
2267 s_type);
2268 if (rc)
2269 return rc;
2270
2271 fp->stream.name = xattr_stream_name;
2272 fp->stream.size = xattr_stream_size;
2273
2274 /* Check if there is stream prefix in xattr space */
Hyunchul Lee465d7202021-07-03 12:10:36 +09002275 rc = ksmbd_vfs_casexattr_len(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002276 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002277 xattr_stream_name,
2278 xattr_stream_size);
2279 if (rc >= 0)
2280 return 0;
2281
2282 if (fp->cdoption == FILE_OPEN_LE) {
2283 ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc);
2284 return -EBADF;
2285 }
2286
Hyunchul Lee465d7202021-07-03 12:10:36 +09002287 rc = ksmbd_vfs_setxattr(user_ns, path->dentry,
2288 xattr_stream_name, NULL, 0, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002289 if (rc < 0)
Namjae Jeonbde16942021-06-28 15:23:19 +09002290 pr_err("Failed to store XATTR stream name :%d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002291 return 0;
2292}
2293
Hyunchul Leeef24c962021-06-30 18:25:52 +09002294static int smb2_remove_smb_xattrs(struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002295{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002296 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002297 char *name, *xattr_list = NULL;
2298 ssize_t xattr_list_len;
2299 int err = 0;
2300
Hyunchul Leeef24c962021-06-30 18:25:52 +09002301 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002302 if (xattr_list_len < 0) {
2303 goto out;
2304 } else if (!xattr_list_len) {
2305 ksmbd_debug(SMB, "empty xattr in the file\n");
2306 goto out;
2307 }
2308
2309 for (name = xattr_list; name - xattr_list < xattr_list_len;
2310 name += strlen(name) + 1) {
2311 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
2312
2313 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002314 strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX,
2315 DOS_ATTRIBUTE_PREFIX_LEN) &&
2316 strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09002317 continue;
2318
Hyunchul Lee465d7202021-07-03 12:10:36 +09002319 err = ksmbd_vfs_remove_xattr(user_ns, path->dentry, name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002320 if (err)
2321 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
2322 }
2323out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09002324 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002325 return err;
2326}
2327
2328static int smb2_create_truncate(struct path *path)
2329{
2330 int rc = vfs_truncate(path, 0);
2331
2332 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002333 pr_err("vfs_truncate failed, rc %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002334 return rc;
2335 }
2336
Hyunchul Leeef24c962021-06-30 18:25:52 +09002337 rc = smb2_remove_smb_xattrs(path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002338 if (rc == -EOPNOTSUPP)
2339 rc = 0;
2340 if (rc)
2341 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002342 "ksmbd_truncate_stream_name_xattr failed, rc %d\n",
2343 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002344 return rc;
2345}
2346
Namjae Jeon64b39f42021-03-30 14:25:35 +09002347static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002348 struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002349{
2350 struct xattr_dos_attrib da = {0};
2351 int rc;
2352
2353 if (!test_share_config_flag(tcon->share_conf,
2354 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
2355 return;
2356
2357 da.version = 4;
2358 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
2359 da.itime = da.create_time = fp->create_time;
2360 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
2361 XATTR_DOSINFO_ITIME;
2362
Hyunchul Leeaf349832021-06-30 18:25:53 +09002363 rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt),
2364 path->dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09002365 if (rc)
2366 ksmbd_debug(SMB, "failed to store file attribute into xattr\n");
2367}
2368
2369static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon,
Namjae Jeon070fb212021-05-26 17:57:12 +09002370 struct path *path, struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002371{
2372 struct xattr_dos_attrib da;
2373 int rc;
2374
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002375 fp->f_ci->m_fattr &= ~(FILE_ATTRIBUTE_HIDDEN_LE | FILE_ATTRIBUTE_SYSTEM_LE);
Namjae Jeone2f34482021-03-16 10:49:09 +09002376
2377 /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */
2378 if (!test_share_config_flag(tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002379 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
Namjae Jeone2f34482021-03-16 10:49:09 +09002380 return;
2381
Hyunchul Leeaf349832021-06-30 18:25:53 +09002382 rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_user_ns(path->mnt),
2383 path->dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09002384 if (rc > 0) {
2385 fp->f_ci->m_fattr = cpu_to_le32(da.attr);
2386 fp->create_time = da.create_time;
2387 fp->itime = da.itime;
2388 }
2389}
2390
Namjae Jeon64b39f42021-03-30 14:25:35 +09002391static int smb2_creat(struct ksmbd_work *work, struct path *path, char *name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002392 int open_flags, umode_t posix_mode, bool is_dir)
Namjae Jeone2f34482021-03-16 10:49:09 +09002393{
2394 struct ksmbd_tree_connect *tcon = work->tcon;
2395 struct ksmbd_share_config *share = tcon->share_conf;
2396 umode_t mode;
2397 int rc;
2398
2399 if (!(open_flags & O_CREAT))
2400 return -EBADF;
2401
2402 ksmbd_debug(SMB, "file does not exist, so creating\n");
2403 if (is_dir == true) {
2404 ksmbd_debug(SMB, "creating directory\n");
2405
2406 mode = share_config_directory_mode(share, posix_mode);
2407 rc = ksmbd_vfs_mkdir(work, name, mode);
2408 if (rc)
2409 return rc;
2410 } else {
2411 ksmbd_debug(SMB, "creating regular file\n");
2412
2413 mode = share_config_create_mode(share, posix_mode);
2414 rc = ksmbd_vfs_create(work, name, mode);
2415 if (rc)
2416 return rc;
2417 }
2418
Hyunchul Lee265fd192021-09-25 00:06:16 +09002419 rc = ksmbd_vfs_kern_path(work, name, 0, path, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002420 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002421 pr_err("cannot get linux path (%s), err = %d\n",
2422 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002423 return rc;
2424 }
2425 return 0;
2426}
2427
2428static int smb2_create_sd_buffer(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09002429 struct smb2_create_req *req,
Hyunchul Leeef24c962021-06-30 18:25:52 +09002430 struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002431{
2432 struct create_context *context;
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002433 struct create_sd_buf_req *sd_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09002434
2435 if (!req->CreateContextsOffset)
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002436 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09002437
2438 /* Parse SD BUFFER create contexts */
2439 context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER);
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002440 if (!context)
2441 return -ENOENT;
2442 else if (IS_ERR(context))
2443 return PTR_ERR(context);
Namjae Jeone2f34482021-03-16 10:49:09 +09002444
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002445 ksmbd_debug(SMB,
2446 "Set ACLs using SMB2_CREATE_SD_BUFFER context\n");
2447 sd_buf = (struct create_sd_buf_req *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002448 if (le16_to_cpu(context->DataOffset) +
2449 le32_to_cpu(context->DataLength) <
2450 sizeof(struct create_sd_buf_req))
2451 return -EINVAL;
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002452 return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd,
2453 le32_to_cpu(sd_buf->ccontext.DataLength), true);
Namjae Jeone2f34482021-03-16 10:49:09 +09002454}
2455
Christian Brauner43205ca2021-08-23 17:13:50 +02002456static void ksmbd_acls_fattr(struct smb_fattr *fattr,
2457 struct user_namespace *mnt_userns,
2458 struct inode *inode)
Namjae Jeon3d47e542021-04-20 14:25:35 +09002459{
Christian Brauner43205ca2021-08-23 17:13:50 +02002460 fattr->cf_uid = i_uid_into_mnt(mnt_userns, inode);
2461 fattr->cf_gid = i_gid_into_mnt(mnt_userns, inode);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002462 fattr->cf_mode = inode->i_mode;
Namjae Jeon777cad12021-08-13 08:15:33 +09002463 fattr->cf_acls = NULL;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002464 fattr->cf_dacls = NULL;
2465
Namjae Jeon777cad12021-08-13 08:15:33 +09002466 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
2467 fattr->cf_acls = get_acl(inode, ACL_TYPE_ACCESS);
2468 if (S_ISDIR(inode->i_mode))
2469 fattr->cf_dacls = get_acl(inode, ACL_TYPE_DEFAULT);
2470 }
Namjae Jeon3d47e542021-04-20 14:25:35 +09002471}
2472
Namjae Jeone2f34482021-03-16 10:49:09 +09002473/**
2474 * smb2_open() - handler for smb file open request
2475 * @work: smb work containing request buffer
2476 *
2477 * Return: 0 on success, otherwise error
2478 */
2479int smb2_open(struct ksmbd_work *work)
2480{
2481 struct ksmbd_conn *conn = work->conn;
2482 struct ksmbd_session *sess = work->sess;
2483 struct ksmbd_tree_connect *tcon = work->tcon;
2484 struct smb2_create_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09002485 struct smb2_create_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09002486 struct path path;
2487 struct ksmbd_share_config *share = tcon->share_conf;
2488 struct ksmbd_file *fp = NULL;
2489 struct file *filp = NULL;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002490 struct user_namespace *user_ns = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002491 struct kstat stat;
2492 struct create_context *context;
2493 struct lease_ctx_info *lc = NULL;
2494 struct create_ea_buf_req *ea_buf = NULL;
2495 struct oplock_info *opinfo;
2496 __le32 *next_ptr = NULL;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002497 int req_op_level = 0, open_flags = 0, may_flags = 0, file_info = 0;
Hyunchul Lee265fd192021-09-25 00:06:16 +09002498 int rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002499 int contxt_cnt = 0, query_disk_id = 0;
2500 int maximal_access_ctxt = 0, posix_ctxt = 0;
2501 int s_type = 0;
2502 int next_off = 0;
2503 char *name = NULL;
2504 char *stream_name = NULL;
2505 bool file_present = false, created = false, already_permitted = false;
Namjae Jeone2f34482021-03-16 10:49:09 +09002506 int share_ret, need_truncate = 0;
2507 u64 time;
2508 umode_t posix_mode = 0;
2509 __le32 daccess, maximal_access = 0;
2510
Namjae Jeone2f34482021-03-16 10:49:09 +09002511 WORK_BUFFERS(work, req, rsp);
2512
2513 if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002514 (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002515 ksmbd_debug(SMB, "invalid flag in chained command\n");
2516 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2517 smb2_set_err_rsp(work);
2518 return -EINVAL;
2519 }
2520
2521 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
2522 ksmbd_debug(SMB, "IPC pipe create request\n");
2523 return create_smb2_pipe(work);
2524 }
2525
2526 if (req->NameLength) {
2527 if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002528 *(char *)req->Buffer == '\\') {
Namjae Jeonbde16942021-06-28 15:23:19 +09002529 pr_err("not allow directory name included leading slash\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002530 rc = -EINVAL;
2531 goto err_out1;
2532 }
2533
Marios Makassikis80917f12021-12-01 21:41:19 +01002534 name = smb2_get_name(req->Buffer,
Namjae Jeone2f34482021-03-16 10:49:09 +09002535 le16_to_cpu(req->NameLength),
2536 work->conn->local_nls);
2537 if (IS_ERR(name)) {
2538 rc = PTR_ERR(name);
2539 if (rc != -ENOMEM)
2540 rc = -ENOENT;
Dan Carpenter8b99f352021-08-02 08:14:03 +09002541 name = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002542 goto err_out1;
2543 }
2544
2545 ksmbd_debug(SMB, "converted name = %s\n", name);
2546 if (strchr(name, ':')) {
2547 if (!test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002548 KSMBD_SHARE_FLAG_STREAMS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002549 rc = -EBADF;
2550 goto err_out1;
2551 }
2552 rc = parse_stream_name(name, &stream_name, &s_type);
2553 if (rc < 0)
2554 goto err_out1;
2555 }
2556
2557 rc = ksmbd_validate_filename(name);
2558 if (rc < 0)
2559 goto err_out1;
2560
2561 if (ksmbd_share_veto_filename(share, name)) {
2562 rc = -ENOENT;
2563 ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002564 name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002565 goto err_out1;
2566 }
2567 } else {
Hyunchul Lee265fd192021-09-25 00:06:16 +09002568 name = kstrdup("", GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09002569 if (!name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002570 rc = -ENOMEM;
2571 goto err_out1;
2572 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002573 }
2574
2575 req_op_level = req->RequestedOplockLevel;
Namjae Jeon73f9dad2021-04-16 14:12:06 +09002576 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002577 lc = parse_lease_state(req);
Namjae Jeone2f34482021-03-16 10:49:09 +09002578
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002579 if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002580 pr_err("Invalid impersonationlevel : 0x%x\n",
2581 le32_to_cpu(req->ImpersonationLevel));
Namjae Jeone2f34482021-03-16 10:49:09 +09002582 rc = -EIO;
2583 rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL;
2584 goto err_out1;
2585 }
2586
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002587 if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002588 pr_err("Invalid create options : 0x%x\n",
2589 le32_to_cpu(req->CreateOptions));
Namjae Jeone2f34482021-03-16 10:49:09 +09002590 rc = -EINVAL;
2591 goto err_out1;
2592 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09002593 if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002594 req->CreateOptions & FILE_RANDOM_ACCESS_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002595 req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE);
2596
Namjae Jeon070fb212021-05-26 17:57:12 +09002597 if (req->CreateOptions &
2598 (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION |
2599 FILE_RESERVE_OPFILTER_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002600 rc = -EOPNOTSUPP;
2601 goto err_out1;
2602 }
2603
2604 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2605 if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) {
2606 rc = -EINVAL;
2607 goto err_out1;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002608 } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002609 req->CreateOptions = ~(FILE_NO_COMPRESSION_LE);
Namjae Jeon64b39f42021-03-30 14:25:35 +09002610 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002611 }
2612 }
2613
2614 if (le32_to_cpu(req->CreateDisposition) >
Namjae Jeon070fb212021-05-26 17:57:12 +09002615 le32_to_cpu(FILE_OVERWRITE_IF_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002616 pr_err("Invalid create disposition : 0x%x\n",
2617 le32_to_cpu(req->CreateDisposition));
Namjae Jeone2f34482021-03-16 10:49:09 +09002618 rc = -EINVAL;
2619 goto err_out1;
2620 }
2621
2622 if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002623 pr_err("Invalid desired access : 0x%x\n",
2624 le32_to_cpu(req->DesiredAccess));
Namjae Jeone2f34482021-03-16 10:49:09 +09002625 rc = -EACCES;
2626 goto err_out1;
2627 }
2628
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002629 if (req->FileAttributes && !(req->FileAttributes & FILE_ATTRIBUTE_MASK_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002630 pr_err("Invalid file attribute : 0x%x\n",
2631 le32_to_cpu(req->FileAttributes));
Namjae Jeone2f34482021-03-16 10:49:09 +09002632 rc = -EINVAL;
2633 goto err_out1;
2634 }
2635
2636 if (req->CreateContextsOffset) {
2637 /* Parse non-durable handle create contexts */
2638 context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002639 if (IS_ERR(context)) {
2640 rc = PTR_ERR(context);
2641 goto err_out1;
2642 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002643 ea_buf = (struct create_ea_buf_req *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002644 if (le16_to_cpu(context->DataOffset) +
2645 le32_to_cpu(context->DataLength) <
2646 sizeof(struct create_ea_buf_req)) {
2647 rc = -EINVAL;
2648 goto err_out1;
2649 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002650 if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) {
2651 rsp->hdr.Status = STATUS_ACCESS_DENIED;
2652 rc = -EACCES;
2653 goto err_out1;
2654 }
2655 }
2656
2657 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002658 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002659 if (IS_ERR(context)) {
2660 rc = PTR_ERR(context);
2661 goto err_out1;
2662 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002663 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002664 "get query maximal access context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002665 maximal_access_ctxt = 1;
2666 }
2667
2668 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002669 SMB2_CREATE_TIMEWARP_REQUEST);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002670 if (IS_ERR(context)) {
2671 rc = PTR_ERR(context);
2672 goto err_out1;
2673 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002674 ksmbd_debug(SMB, "get timewarp context\n");
2675 rc = -EBADF;
2676 goto err_out1;
2677 }
2678
2679 if (tcon->posix_extensions) {
2680 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002681 SMB2_CREATE_TAG_POSIX);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002682 if (IS_ERR(context)) {
2683 rc = PTR_ERR(context);
2684 goto err_out1;
2685 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002686 struct create_posix *posix =
2687 (struct create_posix *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002688 if (le16_to_cpu(context->DataOffset) +
2689 le32_to_cpu(context->DataLength) <
2690 sizeof(struct create_posix)) {
2691 rc = -EINVAL;
2692 goto err_out1;
2693 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002694 ksmbd_debug(SMB, "get posix context\n");
2695
2696 posix_mode = le32_to_cpu(posix->Mode);
2697 posix_ctxt = 1;
2698 }
2699 }
2700 }
2701
2702 if (ksmbd_override_fsids(work)) {
2703 rc = -ENOMEM;
2704 goto err_out1;
2705 }
2706
Hyunchul Lee265fd192021-09-25 00:06:16 +09002707 rc = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, 1);
Namjae Jeon4ea47792021-09-21 14:19:33 +09002708 if (!rc) {
2709 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002710 /*
2711 * If file exists with under flags, return access
2712 * denied error.
2713 */
2714 if (req->CreateDisposition == FILE_OVERWRITE_IF_LE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09002715 req->CreateDisposition == FILE_OPEN_IF_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002716 rc = -EACCES;
2717 path_put(&path);
2718 goto err_out;
2719 }
2720
Namjae Jeon64b39f42021-03-30 14:25:35 +09002721 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002722 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002723 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002724 rc = -EACCES;
2725 path_put(&path);
2726 goto err_out;
2727 }
Namjae Jeon4ea47792021-09-21 14:19:33 +09002728 } else if (d_is_symlink(path.dentry)) {
2729 rc = -EACCES;
2730 path_put(&path);
2731 goto err_out;
Namjae Jeone2f34482021-03-16 10:49:09 +09002732 }
2733 }
2734
2735 if (rc) {
Hyunchul Lee265fd192021-09-25 00:06:16 +09002736 if (rc != -ENOENT)
Namjae Jeone2f34482021-03-16 10:49:09 +09002737 goto err_out;
Namjae Jeone2f34482021-03-16 10:49:09 +09002738 ksmbd_debug(SMB, "can not get linux path for %s, rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002739 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002740 rc = 0;
2741 } else {
2742 file_present = true;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002743 user_ns = mnt_user_ns(path.mnt);
2744 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09002745 }
2746 if (stream_name) {
2747 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2748 if (s_type == DATA_STREAM) {
2749 rc = -EIO;
2750 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2751 }
2752 } else {
2753 if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) {
2754 rc = -EIO;
2755 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2756 }
2757 }
2758
2759 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE &&
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002760 req->FileAttributes & FILE_ATTRIBUTE_NORMAL_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002761 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2762 rc = -EIO;
2763 }
2764
2765 if (rc < 0)
2766 goto err_out;
2767 }
2768
Namjae Jeon64b39f42021-03-30 14:25:35 +09002769 if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE &&
2770 S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002771 ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002772 name, req->CreateOptions);
Namjae Jeone2f34482021-03-16 10:49:09 +09002773 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2774 rc = -EIO;
2775 goto err_out;
2776 }
2777
2778 if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002779 !(req->CreateDisposition == FILE_CREATE_LE) &&
2780 !S_ISDIR(stat.mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002781 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2782 rc = -EIO;
2783 goto err_out;
2784 }
2785
2786 if (!stream_name && file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002787 req->CreateDisposition == FILE_CREATE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002788 rc = -EEXIST;
2789 goto err_out;
2790 }
2791
Namjae Jeone2f34482021-03-16 10:49:09 +09002792 daccess = smb_map_generic_desired_access(req->DesiredAccess);
2793
2794 if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002795 rc = smb_check_perm_dacl(conn, &path, &daccess,
Namjae Jeon070fb212021-05-26 17:57:12 +09002796 sess->user->uid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002797 if (rc)
2798 goto err_out;
2799 }
2800
2801 if (daccess & FILE_MAXIMAL_ACCESS_LE) {
2802 if (!file_present) {
2803 daccess = cpu_to_le32(GENERIC_ALL_FLAGS);
2804 } else {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002805 rc = ksmbd_vfs_query_maximal_access(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002806 path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002807 &daccess);
2808 if (rc)
2809 goto err_out;
2810 already_permitted = true;
2811 }
2812 maximal_access = daccess;
2813 }
2814
Namjae Jeon070fb212021-05-26 17:57:12 +09002815 open_flags = smb2_create_open_flags(file_present, daccess,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002816 req->CreateDisposition,
2817 &may_flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09002818
2819 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
2820 if (open_flags & O_CREAT) {
2821 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002822 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002823 rc = -EACCES;
2824 goto err_out;
2825 }
2826 }
2827
2828 /*create file if not present */
2829 if (!file_present) {
2830 rc = smb2_creat(work, &path, name, open_flags, posix_mode,
Namjae Jeon070fb212021-05-26 17:57:12 +09002831 req->CreateOptions & FILE_DIRECTORY_FILE_LE);
Marios Makassikisd337a442021-07-27 09:24:51 +09002832 if (rc) {
2833 if (rc == -ENOENT) {
2834 rc = -EIO;
2835 rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND;
2836 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002837 goto err_out;
Marios Makassikisd337a442021-07-27 09:24:51 +09002838 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002839
2840 created = true;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002841 user_ns = mnt_user_ns(path.mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002842 if (ea_buf) {
Namjae Jeon9496e262021-09-29 15:41:48 +09002843 if (le32_to_cpu(ea_buf->ccontext.DataLength) <
2844 sizeof(struct smb2_ea_info)) {
2845 rc = -EINVAL;
2846 goto err_out;
2847 }
2848
2849 rc = smb2_set_ea(&ea_buf->ea,
2850 le32_to_cpu(ea_buf->ccontext.DataLength),
2851 &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002852 if (rc == -EOPNOTSUPP)
2853 rc = 0;
2854 else if (rc)
2855 goto err_out;
2856 }
2857 } else if (!already_permitted) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002858 /* FILE_READ_ATTRIBUTE is allowed without inode_permission,
2859 * because execute(search) permission on a parent directory,
2860 * is already granted.
2861 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09002862 if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002863 rc = inode_permission(user_ns,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002864 d_inode(path.dentry),
2865 may_flags);
Namjae Jeonff1d5722021-04-13 13:18:10 +09002866 if (rc)
Namjae Jeone2f34482021-03-16 10:49:09 +09002867 goto err_out;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002868
2869 if ((daccess & FILE_DELETE_LE) ||
2870 (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002871 rc = ksmbd_vfs_may_delete(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002872 path.dentry);
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002873 if (rc)
2874 goto err_out;
2875 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002876 }
2877 }
2878
2879 rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent));
2880 if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) {
2881 rc = -EBUSY;
2882 goto err_out;
2883 }
2884
2885 rc = 0;
2886 filp = dentry_open(&path, open_flags, current_cred());
2887 if (IS_ERR(filp)) {
2888 rc = PTR_ERR(filp);
Namjae Jeonbde16942021-06-28 15:23:19 +09002889 pr_err("dentry open for dir failed, rc %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002890 goto err_out;
2891 }
2892
2893 if (file_present) {
2894 if (!(open_flags & O_TRUNC))
2895 file_info = FILE_OPENED;
2896 else
2897 file_info = FILE_OVERWRITTEN;
2898
Namjae Jeon070fb212021-05-26 17:57:12 +09002899 if ((req->CreateDisposition & FILE_CREATE_MASK_LE) ==
2900 FILE_SUPERSEDE_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002901 file_info = FILE_SUPERSEDED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002902 } else if (open_flags & O_CREAT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002903 file_info = FILE_CREATED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002904 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002905
2906 ksmbd_vfs_set_fadvise(filp, req->CreateOptions);
2907
2908 /* Obtain Volatile-ID */
2909 fp = ksmbd_open_fd(work, filp);
2910 if (IS_ERR(fp)) {
2911 fput(filp);
2912 rc = PTR_ERR(fp);
2913 fp = NULL;
2914 goto err_out;
2915 }
2916
2917 /* Get Persistent-ID */
2918 ksmbd_open_durable_fd(fp);
Namjae Jeon38673692021-07-08 12:32:27 +09002919 if (!has_file_id(fp->persistent_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002920 rc = -ENOMEM;
2921 goto err_out;
2922 }
2923
2924 fp->filename = name;
2925 fp->cdoption = req->CreateDisposition;
2926 fp->daccess = daccess;
2927 fp->saccess = req->ShareAccess;
2928 fp->coption = req->CreateOptions;
2929
2930 /* Set default windows and posix acls if creating new file */
2931 if (created) {
2932 int posix_acl_rc;
Namjae Jeonfba08fa2021-04-15 10:29:39 +09002933 struct inode *inode = d_inode(path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09002934
Hyunchul Lee465d7202021-07-03 12:10:36 +09002935 posix_acl_rc = ksmbd_vfs_inherit_posix_acl(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002936 inode,
2937 d_inode(path.dentry->d_parent));
Namjae Jeone2f34482021-03-16 10:49:09 +09002938 if (posix_acl_rc)
2939 ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc);
2940
2941 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002942 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002943 rc = smb_inherit_dacl(conn, &path, sess->user->uid,
Namjae Jeon070fb212021-05-26 17:57:12 +09002944 sess->user->gid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002945 }
2946
2947 if (rc) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002948 rc = smb2_create_sd_buffer(work, req, &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002949 if (rc) {
2950 if (posix_acl_rc)
Hyunchul Lee465d7202021-07-03 12:10:36 +09002951 ksmbd_vfs_set_init_posix_acl(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002952 inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09002953
2954 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002955 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002956 struct smb_fattr fattr;
2957 struct smb_ntsd *pntsd;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002958 int pntsd_size, ace_num = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002959
Christian Brauner43205ca2021-08-23 17:13:50 +02002960 ksmbd_acls_fattr(&fattr, user_ns, inode);
Marios Makassikise6b10592021-04-15 10:24:56 +09002961 if (fattr.cf_acls)
2962 ace_num = fattr.cf_acls->a_count;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002963 if (fattr.cf_dacls)
2964 ace_num += fattr.cf_dacls->a_count;
Namjae Jeone2f34482021-03-16 10:49:09 +09002965
2966 pntsd = kmalloc(sizeof(struct smb_ntsd) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002967 sizeof(struct smb_sid) * 3 +
Namjae Jeone2f34482021-03-16 10:49:09 +09002968 sizeof(struct smb_acl) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002969 sizeof(struct smb_ace) * ace_num * 2,
Namjae Jeone2f34482021-03-16 10:49:09 +09002970 GFP_KERNEL);
2971 if (!pntsd)
2972 goto err_out;
2973
Hyunchul Lee465d7202021-07-03 12:10:36 +09002974 rc = build_sec_desc(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002975 pntsd, NULL,
Namjae Jeon070fb212021-05-26 17:57:12 +09002976 OWNER_SECINFO |
Hyunchul Leeaf349832021-06-30 18:25:53 +09002977 GROUP_SECINFO |
2978 DACL_SECINFO,
Namjae Jeon070fb212021-05-26 17:57:12 +09002979 &pntsd_size, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09002980 posix_acl_release(fattr.cf_acls);
2981 posix_acl_release(fattr.cf_dacls);
Namjae Jeonf2e78af2021-12-01 10:12:39 +09002982 if (rc) {
2983 kfree(pntsd);
2984 goto err_out;
2985 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002986
2987 rc = ksmbd_vfs_set_sd_xattr(conn,
Hyunchul Lee465d7202021-07-03 12:10:36 +09002988 user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +09002989 path.dentry,
2990 pntsd,
2991 pntsd_size);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002992 kfree(pntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09002993 if (rc)
Namjae Jeonbde16942021-06-28 15:23:19 +09002994 pr_err("failed to store ntacl in xattr : %d\n",
2995 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002996 }
2997 }
2998 }
2999 rc = 0;
3000 }
3001
3002 if (stream_name) {
3003 rc = smb2_set_stream_name_xattr(&path,
3004 fp,
3005 stream_name,
3006 s_type);
3007 if (rc)
3008 goto err_out;
3009 file_info = FILE_CREATED;
3010 }
3011
3012 fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE |
3013 FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE));
Namjae Jeon64b39f42021-03-30 14:25:35 +09003014 if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC &&
3015 !fp->attrib_only && !stream_name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003016 smb_break_all_oplock(work, fp);
3017 need_truncate = 1;
3018 }
3019
3020 /* fp should be searchable through ksmbd_inode.m_fp_list
3021 * after daccess, saccess, attrib_only, and stream are
3022 * initialized.
3023 */
3024 write_lock(&fp->f_ci->m_lock);
3025 list_add(&fp->node, &fp->f_ci->m_fp_list);
3026 write_unlock(&fp->f_ci->m_lock);
3027
3028 rc = ksmbd_vfs_getattr(&path, &stat);
3029 if (rc) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09003030 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09003031 rc = 0;
3032 }
3033
3034 /* Check delete pending among previous fp before oplock break */
3035 if (ksmbd_inode_pending_delete(fp)) {
3036 rc = -EBUSY;
3037 goto err_out;
3038 }
3039
3040 share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp);
Namjae Jeon64b39f42021-03-30 14:25:35 +09003041 if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) ||
3042 (req_op_level == SMB2_OPLOCK_LEVEL_LEASE &&
3043 !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) {
Namjae Jeonab0b2632021-06-29 09:20:13 +09003044 if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003045 rc = share_ret;
3046 goto err_out;
3047 }
3048 } else {
3049 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) {
3050 req_op_level = smb2_map_lease_to_oplock(lc->req_state);
3051 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003052 "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n",
3053 name, req_op_level, lc->req_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09003054 rc = find_same_lease_key(sess, fp->f_ci, lc);
3055 if (rc)
3056 goto err_out;
3057 } else if (open_flags == O_RDONLY &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09003058 (req_op_level == SMB2_OPLOCK_LEVEL_BATCH ||
3059 req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
Namjae Jeone2f34482021-03-16 10:49:09 +09003060 req_op_level = SMB2_OPLOCK_LEVEL_II;
3061
3062 rc = smb_grant_oplock(work, req_op_level,
3063 fp->persistent_id, fp,
3064 le32_to_cpu(req->hdr.Id.SyncId.TreeId),
3065 lc, share_ret);
3066 if (rc < 0)
3067 goto err_out;
3068 }
3069
3070 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
3071 ksmbd_fd_set_delete_on_close(fp, file_info);
3072
3073 if (need_truncate) {
3074 rc = smb2_create_truncate(&path);
3075 if (rc)
3076 goto err_out;
3077 }
3078
3079 if (req->CreateContextsOffset) {
3080 struct create_alloc_size_req *az_req;
3081
Namjae Jeon070fb212021-05-26 17:57:12 +09003082 az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req,
3083 SMB2_CREATE_ALLOCATION_SIZE);
Namjae Jeonf19b3962021-07-13 09:59:34 +09003084 if (IS_ERR(az_req)) {
3085 rc = PTR_ERR(az_req);
3086 goto err_out;
3087 } else if (az_req) {
Hyunchul Lee8f771502021-09-24 22:22:22 +09003088 loff_t alloc_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09003089 int err;
3090
Hyunchul Lee8f771502021-09-24 22:22:22 +09003091 if (le16_to_cpu(az_req->ccontext.DataOffset) +
3092 le32_to_cpu(az_req->ccontext.DataLength) <
3093 sizeof(struct create_alloc_size_req)) {
3094 rc = -EINVAL;
3095 goto err_out;
3096 }
3097 alloc_size = le64_to_cpu(az_req->AllocationSize);
Namjae Jeone2f34482021-03-16 10:49:09 +09003098 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003099 "request smb2 create allocate size : %llu\n",
3100 alloc_size);
Namjae Jeone8c06192021-06-22 11:06:11 +09003101 smb_break_all_levII_oplock(work, fp, 1);
3102 err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
3103 alloc_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003104 if (err < 0)
3105 ksmbd_debug(SMB,
Namjae Jeone8c06192021-06-22 11:06:11 +09003106 "vfs_fallocate is failed : %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003107 err);
Namjae Jeone2f34482021-03-16 10:49:09 +09003108 }
3109
Namjae Jeon64b39f42021-03-30 14:25:35 +09003110 context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID);
Namjae Jeonf19b3962021-07-13 09:59:34 +09003111 if (IS_ERR(context)) {
3112 rc = PTR_ERR(context);
3113 goto err_out;
3114 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003115 ksmbd_debug(SMB, "get query on disk id context\n");
3116 query_disk_id = 1;
3117 }
3118 }
3119
3120 if (stat.result_mask & STATX_BTIME)
3121 fp->create_time = ksmbd_UnixTimeToNT(stat.btime);
3122 else
3123 fp->create_time = ksmbd_UnixTimeToNT(stat.ctime);
3124 if (req->FileAttributes || fp->f_ci->m_fattr == 0)
Namjae Jeon070fb212021-05-26 17:57:12 +09003125 fp->f_ci->m_fattr =
3126 cpu_to_le32(smb2_get_dos_mode(&stat, le32_to_cpu(req->FileAttributes)));
Namjae Jeone2f34482021-03-16 10:49:09 +09003127
3128 if (!created)
3129 smb2_update_xattrs(tcon, &path, fp);
3130 else
3131 smb2_new_xattrs(tcon, &path, fp);
3132
3133 memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE);
3134
Hyunchul Lee465d7202021-07-03 12:10:36 +09003135 generic_fillattr(user_ns, file_inode(fp->filp),
Hyunchul Leeaf349832021-06-30 18:25:53 +09003136 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09003137
3138 rsp->StructureSize = cpu_to_le16(89);
3139 rcu_read_lock();
3140 opinfo = rcu_dereference(fp->f_opinfo);
3141 rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0;
3142 rcu_read_unlock();
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003143 rsp->Flags = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09003144 rsp->CreateAction = cpu_to_le32(file_info);
3145 rsp->CreationTime = cpu_to_le64(fp->create_time);
3146 time = ksmbd_UnixTimeToNT(stat.atime);
3147 rsp->LastAccessTime = cpu_to_le64(time);
3148 time = ksmbd_UnixTimeToNT(stat.mtime);
3149 rsp->LastWriteTime = cpu_to_le64(time);
3150 time = ksmbd_UnixTimeToNT(stat.ctime);
3151 rsp->ChangeTime = cpu_to_le64(time);
3152 rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 :
3153 cpu_to_le64(stat.blocks << 9);
3154 rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
3155 rsp->FileAttributes = fp->f_ci->m_fattr;
3156
3157 rsp->Reserved2 = 0;
3158
3159 rsp->PersistentFileId = cpu_to_le64(fp->persistent_id);
3160 rsp->VolatileFileId = cpu_to_le64(fp->volatile_id);
3161
3162 rsp->CreateContextsOffset = 0;
3163 rsp->CreateContextsLength = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09003164 inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/
Namjae Jeone2f34482021-03-16 10:49:09 +09003165
3166 /* If lease is request send lease context response */
3167 if (opinfo && opinfo->is_lease) {
3168 struct create_context *lease_ccontext;
3169
3170 ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003171 name, opinfo->o_lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +09003172 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
3173
3174 lease_ccontext = (struct create_context *)rsp->Buffer;
3175 contxt_cnt++;
3176 create_lease_buf(rsp->Buffer, opinfo->o_lease);
3177 le32_add_cpu(&rsp->CreateContextsLength,
3178 conn->vals->create_lease_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003179 inc_rfc1001_len(work->response_buf,
3180 conn->vals->create_lease_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003181 next_ptr = &lease_ccontext->Next;
3182 next_off = conn->vals->create_lease_size;
3183 }
3184
Namjae Jeone2f34482021-03-16 10:49:09 +09003185 if (maximal_access_ctxt) {
3186 struct create_context *mxac_ccontext;
3187
3188 if (maximal_access == 0)
Hyunchul Lee465d7202021-07-03 12:10:36 +09003189 ksmbd_vfs_query_maximal_access(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09003190 path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09003191 &maximal_access);
3192 mxac_ccontext = (struct create_context *)(rsp->Buffer +
3193 le32_to_cpu(rsp->CreateContextsLength));
3194 contxt_cnt++;
3195 create_mxac_rsp_buf(rsp->Buffer +
3196 le32_to_cpu(rsp->CreateContextsLength),
3197 le32_to_cpu(maximal_access));
3198 le32_add_cpu(&rsp->CreateContextsLength,
3199 conn->vals->create_mxac_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003200 inc_rfc1001_len(work->response_buf,
3201 conn->vals->create_mxac_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003202 if (next_ptr)
3203 *next_ptr = cpu_to_le32(next_off);
3204 next_ptr = &mxac_ccontext->Next;
3205 next_off = conn->vals->create_mxac_size;
3206 }
3207
3208 if (query_disk_id) {
3209 struct create_context *disk_id_ccontext;
3210
3211 disk_id_ccontext = (struct create_context *)(rsp->Buffer +
3212 le32_to_cpu(rsp->CreateContextsLength));
3213 contxt_cnt++;
3214 create_disk_id_rsp_buf(rsp->Buffer +
3215 le32_to_cpu(rsp->CreateContextsLength),
3216 stat.ino, tcon->id);
3217 le32_add_cpu(&rsp->CreateContextsLength,
3218 conn->vals->create_disk_id_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003219 inc_rfc1001_len(work->response_buf,
3220 conn->vals->create_disk_id_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003221 if (next_ptr)
3222 *next_ptr = cpu_to_le32(next_off);
3223 next_ptr = &disk_id_ccontext->Next;
3224 next_off = conn->vals->create_disk_id_size;
3225 }
3226
3227 if (posix_ctxt) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003228 contxt_cnt++;
3229 create_posix_rsp_buf(rsp->Buffer +
3230 le32_to_cpu(rsp->CreateContextsLength),
3231 fp);
3232 le32_add_cpu(&rsp->CreateContextsLength,
3233 conn->vals->create_posix_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003234 inc_rfc1001_len(work->response_buf,
3235 conn->vals->create_posix_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003236 if (next_ptr)
3237 *next_ptr = cpu_to_le32(next_off);
3238 }
3239
3240 if (contxt_cnt > 0) {
3241 rsp->CreateContextsOffset =
Namjae Jeoncb451722021-11-03 08:08:44 +09003242 cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer));
Namjae Jeone2f34482021-03-16 10:49:09 +09003243 }
3244
3245err_out:
3246 if (file_present || created)
3247 path_put(&path);
3248 ksmbd_revert_fsids(work);
3249err_out1:
3250 if (rc) {
3251 if (rc == -EINVAL)
3252 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3253 else if (rc == -EOPNOTSUPP)
3254 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Hyunchul Lee265fd192021-09-25 00:06:16 +09003255 else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV)
Namjae Jeone2f34482021-03-16 10:49:09 +09003256 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3257 else if (rc == -ENOENT)
3258 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
3259 else if (rc == -EPERM)
3260 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
3261 else if (rc == -EBUSY)
3262 rsp->hdr.Status = STATUS_DELETE_PENDING;
3263 else if (rc == -EBADF)
3264 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
3265 else if (rc == -ENOEXEC)
3266 rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID;
3267 else if (rc == -ENXIO)
3268 rsp->hdr.Status = STATUS_NO_SUCH_DEVICE;
3269 else if (rc == -EEXIST)
3270 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
3271 else if (rc == -EMFILE)
3272 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
3273 if (!rsp->hdr.Status)
3274 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3275
3276 if (!fp || !fp->filename)
3277 kfree(name);
3278 if (fp)
3279 ksmbd_fd_put(work, fp);
3280 smb2_set_err_rsp(work);
3281 ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status);
3282 }
3283
3284 kfree(lc);
3285
3286 return 0;
3287}
3288
3289static int readdir_info_level_struct_sz(int info_level)
3290{
3291 switch (info_level) {
3292 case FILE_FULL_DIRECTORY_INFORMATION:
3293 return sizeof(struct file_full_directory_info);
3294 case FILE_BOTH_DIRECTORY_INFORMATION:
3295 return sizeof(struct file_both_directory_info);
3296 case FILE_DIRECTORY_INFORMATION:
3297 return sizeof(struct file_directory_info);
3298 case FILE_NAMES_INFORMATION:
3299 return sizeof(struct file_names_info);
3300 case FILEID_FULL_DIRECTORY_INFORMATION:
3301 return sizeof(struct file_id_full_dir_info);
3302 case FILEID_BOTH_DIRECTORY_INFORMATION:
3303 return sizeof(struct file_id_both_directory_info);
3304 case SMB_FIND_FILE_POSIX_INFO:
3305 return sizeof(struct smb2_posix_info);
3306 default:
3307 return -EOPNOTSUPP;
3308 }
3309}
3310
3311static int dentry_name(struct ksmbd_dir_info *d_info, int info_level)
3312{
3313 switch (info_level) {
3314 case FILE_FULL_DIRECTORY_INFORMATION:
3315 {
3316 struct file_full_directory_info *ffdinfo;
3317
3318 ffdinfo = (struct file_full_directory_info *)d_info->rptr;
3319 d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset);
3320 d_info->name = ffdinfo->FileName;
3321 d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength);
3322 return 0;
3323 }
3324 case FILE_BOTH_DIRECTORY_INFORMATION:
3325 {
3326 struct file_both_directory_info *fbdinfo;
3327
3328 fbdinfo = (struct file_both_directory_info *)d_info->rptr;
3329 d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset);
3330 d_info->name = fbdinfo->FileName;
3331 d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength);
3332 return 0;
3333 }
3334 case FILE_DIRECTORY_INFORMATION:
3335 {
3336 struct file_directory_info *fdinfo;
3337
3338 fdinfo = (struct file_directory_info *)d_info->rptr;
3339 d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset);
3340 d_info->name = fdinfo->FileName;
3341 d_info->name_len = le32_to_cpu(fdinfo->FileNameLength);
3342 return 0;
3343 }
3344 case FILE_NAMES_INFORMATION:
3345 {
3346 struct file_names_info *fninfo;
3347
3348 fninfo = (struct file_names_info *)d_info->rptr;
3349 d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset);
3350 d_info->name = fninfo->FileName;
3351 d_info->name_len = le32_to_cpu(fninfo->FileNameLength);
3352 return 0;
3353 }
3354 case FILEID_FULL_DIRECTORY_INFORMATION:
3355 {
3356 struct file_id_full_dir_info *dinfo;
3357
3358 dinfo = (struct file_id_full_dir_info *)d_info->rptr;
3359 d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset);
3360 d_info->name = dinfo->FileName;
3361 d_info->name_len = le32_to_cpu(dinfo->FileNameLength);
3362 return 0;
3363 }
3364 case FILEID_BOTH_DIRECTORY_INFORMATION:
3365 {
3366 struct file_id_both_directory_info *fibdinfo;
3367
3368 fibdinfo = (struct file_id_both_directory_info *)d_info->rptr;
3369 d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset);
3370 d_info->name = fibdinfo->FileName;
3371 d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength);
3372 return 0;
3373 }
3374 case SMB_FIND_FILE_POSIX_INFO:
3375 {
3376 struct smb2_posix_info *posix_info;
3377
3378 posix_info = (struct smb2_posix_info *)d_info->rptr;
3379 d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset);
3380 d_info->name = posix_info->name;
3381 d_info->name_len = le32_to_cpu(posix_info->name_len);
3382 return 0;
3383 }
3384 default:
3385 return -EINVAL;
3386 }
3387}
3388
3389/**
3390 * smb2_populate_readdir_entry() - encode directory entry in smb2 response
3391 * buffer
3392 * @conn: connection instance
3393 * @info_level: smb information level
3394 * @d_info: structure included variables for query dir
Hyunchul Leeaf349832021-06-30 18:25:53 +09003395 * @user_ns: user namespace
Namjae Jeone2f34482021-03-16 10:49:09 +09003396 * @ksmbd_kstat: ksmbd wrapper of dirent stat information
3397 *
3398 * if directory has many entries, find first can't read it fully.
3399 * find next might be called multiple times to read remaining dir entries
3400 *
3401 * Return: 0 on success, otherwise error
3402 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003403static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level,
Namjae Jeon070fb212021-05-26 17:57:12 +09003404 struct ksmbd_dir_info *d_info,
3405 struct ksmbd_kstat *ksmbd_kstat)
Namjae Jeone2f34482021-03-16 10:49:09 +09003406{
3407 int next_entry_offset = 0;
3408 char *conv_name;
3409 int conv_len;
3410 void *kstat;
Namjae Jeondac0ec62021-07-07 14:57:24 +09003411 int struct_sz, rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09003412
3413 conv_name = ksmbd_convert_dir_info_name(d_info,
3414 conn->local_nls,
3415 &conv_len);
3416 if (!conv_name)
3417 return -ENOMEM;
3418
3419 /* Somehow the name has only terminating NULL bytes */
3420 if (conv_len < 0) {
Namjae Jeondac0ec62021-07-07 14:57:24 +09003421 rc = -EINVAL;
3422 goto free_conv_name;
Namjae Jeone2f34482021-03-16 10:49:09 +09003423 }
3424
3425 struct_sz = readdir_info_level_struct_sz(info_level);
3426 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3427 KSMBD_DIR_INFO_ALIGNMENT);
3428
3429 if (next_entry_offset > d_info->out_buf_len) {
3430 d_info->out_buf_len = 0;
Namjae Jeondac0ec62021-07-07 14:57:24 +09003431 rc = -ENOSPC;
3432 goto free_conv_name;
Namjae Jeone2f34482021-03-16 10:49:09 +09003433 }
3434
3435 kstat = d_info->wptr;
3436 if (info_level != FILE_NAMES_INFORMATION)
3437 kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat);
3438
3439 switch (info_level) {
3440 case FILE_FULL_DIRECTORY_INFORMATION:
3441 {
3442 struct file_full_directory_info *ffdinfo;
3443
3444 ffdinfo = (struct file_full_directory_info *)kstat;
3445 ffdinfo->FileNameLength = cpu_to_le32(conv_len);
3446 ffdinfo->EaSize =
3447 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3448 if (ffdinfo->EaSize)
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003449 ffdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003450 if (d_info->hide_dot_file && d_info->name[0] == '.')
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003451 ffdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003452 memcpy(ffdinfo->FileName, conv_name, conv_len);
3453 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3454 break;
3455 }
3456 case FILE_BOTH_DIRECTORY_INFORMATION:
3457 {
3458 struct file_both_directory_info *fbdinfo;
3459
3460 fbdinfo = (struct file_both_directory_info *)kstat;
3461 fbdinfo->FileNameLength = cpu_to_le32(conv_len);
3462 fbdinfo->EaSize =
3463 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3464 if (fbdinfo->EaSize)
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003465 fbdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003466 fbdinfo->ShortNameLength = 0;
3467 fbdinfo->Reserved = 0;
3468 if (d_info->hide_dot_file && d_info->name[0] == '.')
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003469 fbdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003470 memcpy(fbdinfo->FileName, conv_name, conv_len);
3471 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3472 break;
3473 }
3474 case FILE_DIRECTORY_INFORMATION:
3475 {
3476 struct file_directory_info *fdinfo;
3477
3478 fdinfo = (struct file_directory_info *)kstat;
3479 fdinfo->FileNameLength = cpu_to_le32(conv_len);
3480 if (d_info->hide_dot_file && d_info->name[0] == '.')
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003481 fdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003482 memcpy(fdinfo->FileName, conv_name, conv_len);
3483 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3484 break;
3485 }
3486 case FILE_NAMES_INFORMATION:
3487 {
3488 struct file_names_info *fninfo;
3489
3490 fninfo = (struct file_names_info *)kstat;
3491 fninfo->FileNameLength = cpu_to_le32(conv_len);
3492 memcpy(fninfo->FileName, conv_name, conv_len);
3493 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3494 break;
3495 }
3496 case FILEID_FULL_DIRECTORY_INFORMATION:
3497 {
3498 struct file_id_full_dir_info *dinfo;
3499
3500 dinfo = (struct file_id_full_dir_info *)kstat;
3501 dinfo->FileNameLength = cpu_to_le32(conv_len);
3502 dinfo->EaSize =
3503 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3504 if (dinfo->EaSize)
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003505 dinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003506 dinfo->Reserved = 0;
3507 dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3508 if (d_info->hide_dot_file && d_info->name[0] == '.')
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003509 dinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003510 memcpy(dinfo->FileName, conv_name, conv_len);
3511 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3512 break;
3513 }
3514 case FILEID_BOTH_DIRECTORY_INFORMATION:
3515 {
3516 struct file_id_both_directory_info *fibdinfo;
3517
3518 fibdinfo = (struct file_id_both_directory_info *)kstat;
3519 fibdinfo->FileNameLength = cpu_to_le32(conv_len);
3520 fibdinfo->EaSize =
3521 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3522 if (fibdinfo->EaSize)
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003523 fibdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003524 fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3525 fibdinfo->ShortNameLength = 0;
3526 fibdinfo->Reserved = 0;
3527 fibdinfo->Reserved2 = cpu_to_le16(0);
3528 if (d_info->hide_dot_file && d_info->name[0] == '.')
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003529 fibdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003530 memcpy(fibdinfo->FileName, conv_name, conv_len);
3531 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3532 break;
3533 }
3534 case SMB_FIND_FILE_POSIX_INFO:
3535 {
3536 struct smb2_posix_info *posix_info;
3537 u64 time;
3538
3539 posix_info = (struct smb2_posix_info *)kstat;
3540 posix_info->Ignored = 0;
3541 posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
3542 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
3543 posix_info->ChangeTime = cpu_to_le64(time);
3544 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime);
3545 posix_info->LastAccessTime = cpu_to_le64(time);
3546 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime);
3547 posix_info->LastWriteTime = cpu_to_le64(time);
3548 posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size);
3549 posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9);
3550 posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev);
3551 posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink);
3552 posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode);
3553 posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino);
3554 posix_info->DosAttributes =
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003555 S_ISDIR(ksmbd_kstat->kstat->mode) ?
3556 FILE_ATTRIBUTE_DIRECTORY_LE : FILE_ATTRIBUTE_ARCHIVE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09003557 if (d_info->hide_dot_file && d_info->name[0] == '.')
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003558 posix_info->DosAttributes |= FILE_ATTRIBUTE_HIDDEN_LE;
Christian Brauner475d6f92021-08-23 17:13:48 +02003559 id_to_sid(from_kuid_munged(&init_user_ns, ksmbd_kstat->kstat->uid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003560 SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]);
Christian Brauner475d6f92021-08-23 17:13:48 +02003561 id_to_sid(from_kgid_munged(&init_user_ns, ksmbd_kstat->kstat->gid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003562 SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]);
Namjae Jeone2f34482021-03-16 10:49:09 +09003563 memcpy(posix_info->name, conv_name, conv_len);
3564 posix_info->name_len = cpu_to_le32(conv_len);
3565 posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset);
3566 break;
3567 }
3568
3569 } /* switch (info_level) */
3570
3571 d_info->last_entry_offset = d_info->data_count;
3572 d_info->data_count += next_entry_offset;
Marios Makassikise7735c82021-05-06 11:40:02 +09003573 d_info->out_buf_len -= next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003574 d_info->wptr += next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003575
3576 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003577 "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n",
3578 info_level, d_info->out_buf_len,
3579 next_entry_offset, d_info->data_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09003580
Namjae Jeondac0ec62021-07-07 14:57:24 +09003581free_conv_name:
3582 kfree(conv_name);
3583 return rc;
Namjae Jeone2f34482021-03-16 10:49:09 +09003584}
3585
3586struct smb2_query_dir_private {
3587 struct ksmbd_work *work;
3588 char *search_pattern;
3589 struct ksmbd_file *dir_fp;
3590
3591 struct ksmbd_dir_info *d_info;
3592 int info_level;
3593};
3594
3595static void lock_dir(struct ksmbd_file *dir_fp)
3596{
3597 struct dentry *dir = dir_fp->filp->f_path.dentry;
3598
3599 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
3600}
3601
3602static void unlock_dir(struct ksmbd_file *dir_fp)
3603{
3604 struct dentry *dir = dir_fp->filp->f_path.dentry;
3605
3606 inode_unlock(d_inode(dir));
3607}
3608
3609static int process_query_dir_entries(struct smb2_query_dir_private *priv)
3610{
Hyunchul Lee465d7202021-07-03 12:10:36 +09003611 struct user_namespace *user_ns = file_mnt_user_ns(priv->dir_fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003612 struct kstat kstat;
3613 struct ksmbd_kstat ksmbd_kstat;
3614 int rc;
3615 int i;
3616
3617 for (i = 0; i < priv->d_info->num_entry; i++) {
3618 struct dentry *dent;
3619
3620 if (dentry_name(priv->d_info, priv->info_level))
3621 return -EINVAL;
3622
3623 lock_dir(priv->dir_fp);
Christian Braunerda1e7ad2021-08-23 17:13:47 +02003624 dent = lookup_one(user_ns, priv->d_info->name,
3625 priv->dir_fp->filp->f_path.dentry,
3626 priv->d_info->name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09003627 unlock_dir(priv->dir_fp);
3628
3629 if (IS_ERR(dent)) {
3630 ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003631 priv->d_info->name,
3632 PTR_ERR(dent));
Namjae Jeone2f34482021-03-16 10:49:09 +09003633 continue;
3634 }
3635 if (unlikely(d_is_negative(dent))) {
3636 dput(dent);
3637 ksmbd_debug(SMB, "Negative dentry `%s'\n",
3638 priv->d_info->name);
3639 continue;
3640 }
3641
3642 ksmbd_kstat.kstat = &kstat;
3643 if (priv->info_level != FILE_NAMES_INFORMATION)
3644 ksmbd_vfs_fill_dentry_attrs(priv->work,
Hyunchul Lee465d7202021-07-03 12:10:36 +09003645 user_ns,
Namjae Jeone2f34482021-03-16 10:49:09 +09003646 dent,
3647 &ksmbd_kstat);
3648
3649 rc = smb2_populate_readdir_entry(priv->work->conn,
3650 priv->info_level,
3651 priv->d_info,
3652 &ksmbd_kstat);
3653 dput(dent);
3654 if (rc)
3655 return rc;
3656 }
3657 return 0;
3658}
3659
3660static int reserve_populate_dentry(struct ksmbd_dir_info *d_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09003661 int info_level)
Namjae Jeone2f34482021-03-16 10:49:09 +09003662{
3663 int struct_sz;
3664 int conv_len;
3665 int next_entry_offset;
3666
3667 struct_sz = readdir_info_level_struct_sz(info_level);
3668 if (struct_sz == -EOPNOTSUPP)
3669 return -EOPNOTSUPP;
3670
3671 conv_len = (d_info->name_len + 1) * 2;
3672 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3673 KSMBD_DIR_INFO_ALIGNMENT);
3674
3675 if (next_entry_offset > d_info->out_buf_len) {
3676 d_info->out_buf_len = 0;
3677 return -ENOSPC;
3678 }
3679
3680 switch (info_level) {
3681 case FILE_FULL_DIRECTORY_INFORMATION:
3682 {
3683 struct file_full_directory_info *ffdinfo;
3684
3685 ffdinfo = (struct file_full_directory_info *)d_info->wptr;
3686 memcpy(ffdinfo->FileName, d_info->name, d_info->name_len);
3687 ffdinfo->FileName[d_info->name_len] = 0x00;
3688 ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3689 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3690 break;
3691 }
3692 case FILE_BOTH_DIRECTORY_INFORMATION:
3693 {
3694 struct file_both_directory_info *fbdinfo;
3695
3696 fbdinfo = (struct file_both_directory_info *)d_info->wptr;
3697 memcpy(fbdinfo->FileName, d_info->name, d_info->name_len);
3698 fbdinfo->FileName[d_info->name_len] = 0x00;
3699 fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3700 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3701 break;
3702 }
3703 case FILE_DIRECTORY_INFORMATION:
3704 {
3705 struct file_directory_info *fdinfo;
3706
3707 fdinfo = (struct file_directory_info *)d_info->wptr;
3708 memcpy(fdinfo->FileName, d_info->name, d_info->name_len);
3709 fdinfo->FileName[d_info->name_len] = 0x00;
3710 fdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3711 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3712 break;
3713 }
3714 case FILE_NAMES_INFORMATION:
3715 {
3716 struct file_names_info *fninfo;
3717
3718 fninfo = (struct file_names_info *)d_info->wptr;
3719 memcpy(fninfo->FileName, d_info->name, d_info->name_len);
3720 fninfo->FileName[d_info->name_len] = 0x00;
3721 fninfo->FileNameLength = cpu_to_le32(d_info->name_len);
3722 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3723 break;
3724 }
3725 case FILEID_FULL_DIRECTORY_INFORMATION:
3726 {
3727 struct file_id_full_dir_info *dinfo;
3728
3729 dinfo = (struct file_id_full_dir_info *)d_info->wptr;
3730 memcpy(dinfo->FileName, d_info->name, d_info->name_len);
3731 dinfo->FileName[d_info->name_len] = 0x00;
3732 dinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3733 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3734 break;
3735 }
3736 case FILEID_BOTH_DIRECTORY_INFORMATION:
3737 {
3738 struct file_id_both_directory_info *fibdinfo;
3739
3740 fibdinfo = (struct file_id_both_directory_info *)d_info->wptr;
3741 memcpy(fibdinfo->FileName, d_info->name, d_info->name_len);
3742 fibdinfo->FileName[d_info->name_len] = 0x00;
3743 fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3744 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3745 break;
3746 }
3747 case SMB_FIND_FILE_POSIX_INFO:
3748 {
3749 struct smb2_posix_info *posix_info;
3750
3751 posix_info = (struct smb2_posix_info *)d_info->wptr;
3752 memcpy(posix_info->name, d_info->name, d_info->name_len);
3753 posix_info->name[d_info->name_len] = 0x00;
3754 posix_info->name_len = cpu_to_le32(d_info->name_len);
3755 posix_info->NextEntryOffset =
3756 cpu_to_le32(next_entry_offset);
3757 break;
3758 }
3759 } /* switch (info_level) */
3760
3761 d_info->num_entry++;
3762 d_info->out_buf_len -= next_entry_offset;
3763 d_info->wptr += next_entry_offset;
3764 return 0;
3765}
3766
Namjae Jeon64b39f42021-03-30 14:25:35 +09003767static int __query_dir(struct dir_context *ctx, const char *name, int namlen,
Namjae Jeon070fb212021-05-26 17:57:12 +09003768 loff_t offset, u64 ino, unsigned int d_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09003769{
3770 struct ksmbd_readdir_data *buf;
3771 struct smb2_query_dir_private *priv;
3772 struct ksmbd_dir_info *d_info;
3773 int rc;
3774
3775 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
3776 priv = buf->private;
3777 d_info = priv->d_info;
3778
3779 /* dot and dotdot entries are already reserved */
3780 if (!strcmp(".", name) || !strcmp("..", name))
3781 return 0;
3782 if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name))
3783 return 0;
Namjae Jeonb24c9332021-03-21 17:32:19 +09003784 if (!match_pattern(name, namlen, priv->search_pattern))
Namjae Jeone2f34482021-03-16 10:49:09 +09003785 return 0;
3786
3787 d_info->name = name;
3788 d_info->name_len = namlen;
3789 rc = reserve_populate_dentry(d_info, priv->info_level);
3790 if (rc)
3791 return rc;
3792 if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
3793 d_info->out_buf_len = 0;
3794 return 0;
3795 }
3796 return 0;
3797}
3798
3799static void restart_ctx(struct dir_context *ctx)
3800{
3801 ctx->pos = 0;
3802}
3803
3804static int verify_info_level(int info_level)
3805{
3806 switch (info_level) {
3807 case FILE_FULL_DIRECTORY_INFORMATION:
3808 case FILE_BOTH_DIRECTORY_INFORMATION:
3809 case FILE_DIRECTORY_INFORMATION:
3810 case FILE_NAMES_INFORMATION:
3811 case FILEID_FULL_DIRECTORY_INFORMATION:
3812 case FILEID_BOTH_DIRECTORY_INFORMATION:
3813 case SMB_FIND_FILE_POSIX_INFO:
3814 break;
3815 default:
3816 return -EOPNOTSUPP;
3817 }
3818
3819 return 0;
3820}
3821
Hyunchul Lee34061d62021-10-16 08:39:54 +09003822static int smb2_calc_max_out_buf_len(struct ksmbd_work *work,
3823 unsigned short hdr2_len,
3824 unsigned int out_buf_len)
3825{
3826 int free_len;
3827
3828 if (out_buf_len > work->conn->vals->max_trans_size)
3829 return -EINVAL;
3830
3831 free_len = (int)(work->response_sz -
3832 (get_rfc1002_len(work->response_buf) + 4)) -
3833 hdr2_len;
3834 if (free_len < 0)
3835 return -EINVAL;
3836
3837 return min_t(int, out_buf_len, free_len);
3838}
3839
Namjae Jeone2f34482021-03-16 10:49:09 +09003840int smb2_query_dir(struct ksmbd_work *work)
3841{
3842 struct ksmbd_conn *conn = work->conn;
3843 struct smb2_query_directory_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09003844 struct smb2_query_directory_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09003845 struct ksmbd_share_config *share = work->tcon->share_conf;
3846 struct ksmbd_file *dir_fp = NULL;
3847 struct ksmbd_dir_info d_info;
3848 int rc = 0;
3849 char *srch_ptr = NULL;
3850 unsigned char srch_flag;
3851 int buffer_sz;
3852 struct smb2_query_dir_private query_dir_private = {NULL, };
3853
Namjae Jeone2f34482021-03-16 10:49:09 +09003854 WORK_BUFFERS(work, req, rsp);
3855
3856 if (ksmbd_override_fsids(work)) {
3857 rsp->hdr.Status = STATUS_NO_MEMORY;
3858 smb2_set_err_rsp(work);
3859 return -ENOMEM;
3860 }
3861
3862 rc = verify_info_level(req->FileInformationClass);
3863 if (rc) {
3864 rc = -EFAULT;
3865 goto err_out2;
3866 }
3867
3868 dir_fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09003869 le64_to_cpu(req->VolatileFileId),
3870 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09003871 if (!dir_fp) {
3872 rc = -EBADF;
3873 goto err_out2;
3874 }
3875
3876 if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) ||
Hyunchul Leeaf349832021-06-30 18:25:53 +09003877 inode_permission(file_mnt_user_ns(dir_fp->filp),
3878 file_inode(dir_fp->filp),
Namjae Jeon070fb212021-05-26 17:57:12 +09003879 MAY_READ | MAY_EXEC)) {
Namjae Jeon493fa2f2021-06-29 09:22:16 +09003880 pr_err("no right to enumerate directory (%pd)\n",
3881 dir_fp->filp->f_path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003882 rc = -EACCES;
3883 goto err_out2;
3884 }
3885
3886 if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09003887 pr_err("can't do query dir for a file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09003888 rc = -EINVAL;
3889 goto err_out2;
3890 }
3891
3892 srch_flag = req->Flags;
3893 srch_ptr = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09003894 le16_to_cpu(req->FileNameLength), 1,
3895 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09003896 if (IS_ERR(srch_ptr)) {
3897 ksmbd_debug(SMB, "Search Pattern not found\n");
3898 rc = -EINVAL;
3899 goto err_out2;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003900 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003901 ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr);
Namjae Jeon64b39f42021-03-30 14:25:35 +09003902 }
Namjae Jeone2f34482021-03-16 10:49:09 +09003903
3904 ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename);
3905
3906 if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) {
3907 ksmbd_debug(SMB, "Restart directory scan\n");
3908 generic_file_llseek(dir_fp->filp, 0, SEEK_SET);
3909 restart_ctx(&dir_fp->readdir_data.ctx);
3910 }
3911
3912 memset(&d_info, 0, sizeof(struct ksmbd_dir_info));
3913 d_info.wptr = (char *)rsp->Buffer;
3914 d_info.rptr = (char *)rsp->Buffer;
Hyunchul Lee34061d62021-10-16 08:39:54 +09003915 d_info.out_buf_len =
3916 smb2_calc_max_out_buf_len(work, 8,
3917 le32_to_cpu(req->OutputBufferLength));
3918 if (d_info.out_buf_len < 0) {
3919 rc = -EINVAL;
3920 goto err_out;
3921 }
Namjae Jeone2f34482021-03-16 10:49:09 +09003922 d_info.flags = srch_flag;
3923
3924 /*
3925 * reserve dot and dotdot entries in head of buffer
3926 * in first response
3927 */
3928 rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass,
Namjae Jeon070fb212021-05-26 17:57:12 +09003929 dir_fp, &d_info, srch_ptr,
3930 smb2_populate_readdir_entry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003931 if (rc == -ENOSPC)
3932 rc = 0;
3933 else if (rc)
3934 goto err_out;
3935
3936 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES))
3937 d_info.hide_dot_file = true;
3938
3939 buffer_sz = d_info.out_buf_len;
3940 d_info.rptr = d_info.wptr;
3941 query_dir_private.work = work;
3942 query_dir_private.search_pattern = srch_ptr;
3943 query_dir_private.dir_fp = dir_fp;
3944 query_dir_private.d_info = &d_info;
3945 query_dir_private.info_level = req->FileInformationClass;
3946 dir_fp->readdir_data.private = &query_dir_private;
3947 set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir);
3948
Namjae Jeone8c06192021-06-22 11:06:11 +09003949 rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx);
Namjae Jeone2f34482021-03-16 10:49:09 +09003950 if (rc == 0)
3951 restart_ctx(&dir_fp->readdir_data.ctx);
3952 if (rc == -ENOSPC)
3953 rc = 0;
3954 if (rc)
3955 goto err_out;
3956
3957 d_info.wptr = d_info.rptr;
3958 d_info.out_buf_len = buffer_sz;
3959 rc = process_query_dir_entries(&query_dir_private);
3960 if (rc)
3961 goto err_out;
3962
3963 if (!d_info.data_count && d_info.out_buf_len >= 0) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09003964 if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003965 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003966 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003967 dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0;
3968 rsp->hdr.Status = STATUS_NO_MORE_FILES;
3969 }
3970 rsp->StructureSize = cpu_to_le16(9);
3971 rsp->OutputBufferOffset = cpu_to_le16(0);
3972 rsp->OutputBufferLength = cpu_to_le32(0);
3973 rsp->Buffer[0] = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09003974 inc_rfc1001_len(work->response_buf, 9);
Namjae Jeone2f34482021-03-16 10:49:09 +09003975 } else {
3976 ((struct file_directory_info *)
3977 ((char *)rsp->Buffer + d_info.last_entry_offset))
3978 ->NextEntryOffset = 0;
3979
3980 rsp->StructureSize = cpu_to_le16(9);
3981 rsp->OutputBufferOffset = cpu_to_le16(72);
3982 rsp->OutputBufferLength = cpu_to_le32(d_info.data_count);
Namjae Jeoncb451722021-11-03 08:08:44 +09003983 inc_rfc1001_len(work->response_buf, 8 + d_info.data_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09003984 }
3985
3986 kfree(srch_ptr);
3987 ksmbd_fd_put(work, dir_fp);
3988 ksmbd_revert_fsids(work);
3989 return 0;
3990
3991err_out:
Namjae Jeonbde16942021-06-28 15:23:19 +09003992 pr_err("error while processing smb2 query dir rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09003993 kfree(srch_ptr);
3994
3995err_out2:
3996 if (rc == -EINVAL)
3997 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3998 else if (rc == -EACCES)
3999 rsp->hdr.Status = STATUS_ACCESS_DENIED;
4000 else if (rc == -ENOENT)
4001 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
4002 else if (rc == -EBADF)
4003 rsp->hdr.Status = STATUS_FILE_CLOSED;
4004 else if (rc == -ENOMEM)
4005 rsp->hdr.Status = STATUS_NO_MEMORY;
4006 else if (rc == -EFAULT)
4007 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
4008 if (!rsp->hdr.Status)
4009 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
4010
4011 smb2_set_err_rsp(work);
4012 ksmbd_fd_put(work, dir_fp);
4013 ksmbd_revert_fsids(work);
4014 return 0;
4015}
4016
4017/**
4018 * buffer_check_err() - helper function to check buffer errors
4019 * @reqOutputBufferLength: max buffer length expected in command response
4020 * @rsp: query info response buffer contains output buffer length
4021 * @infoclass_size: query info class response buffer size
4022 *
4023 * Return: 0 on success, otherwise error
4024 */
4025static int buffer_check_err(int reqOutputBufferLength,
Namjae Jeoncb451722021-11-03 08:08:44 +09004026 struct smb2_query_info_rsp *rsp,
4027 void *rsp_org, int infoclass_size)
Namjae Jeone2f34482021-03-16 10:49:09 +09004028{
4029 if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) {
4030 if (reqOutputBufferLength < infoclass_size) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004031 pr_err("Invalid Buffer Size Requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004032 rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH;
Namjae Jeoncb451722021-11-03 08:08:44 +09004033 *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr));
Namjae Jeone2f34482021-03-16 10:49:09 +09004034 return -EINVAL;
4035 }
4036
4037 ksmbd_debug(SMB, "Buffer Overflow\n");
4038 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
Namjae Jeoncb451722021-11-03 08:08:44 +09004039 *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09004040 reqOutputBufferLength);
4041 rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09004042 }
4043 return 0;
4044}
4045
Namjae Jeoncb451722021-11-03 08:08:44 +09004046static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp,
4047 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004048{
4049 struct smb2_file_standard_info *sinfo;
4050
4051 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4052
4053 sinfo->AllocationSize = cpu_to_le64(4096);
4054 sinfo->EndOfFile = cpu_to_le64(0);
4055 sinfo->NumberOfLinks = cpu_to_le32(1);
4056 sinfo->DeletePending = 1;
4057 sinfo->Directory = 0;
4058 rsp->OutputBufferLength =
4059 cpu_to_le32(sizeof(struct smb2_file_standard_info));
Namjae Jeoncb451722021-11-03 08:08:44 +09004060 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_standard_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004061}
4062
Namjae Jeoncb451722021-11-03 08:08:44 +09004063static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num,
4064 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004065{
4066 struct smb2_file_internal_info *file_info;
4067
4068 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4069
4070 /* any unique number */
4071 file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63));
4072 rsp->OutputBufferLength =
4073 cpu_to_le32(sizeof(struct smb2_file_internal_info));
Namjae Jeoncb451722021-11-03 08:08:44 +09004074 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004075}
4076
Namjae Jeone2f34482021-03-16 10:49:09 +09004077static int smb2_get_info_file_pipe(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09004078 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004079 struct smb2_query_info_rsp *rsp,
4080 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004081{
Namjae Jeon64b39f42021-03-30 14:25:35 +09004082 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09004083 int rc;
4084
4085 /*
4086 * Windows can sometime send query file info request on
4087 * pipe without opening it, checking error condition here
4088 */
4089 id = le64_to_cpu(req->VolatileFileId);
4090 if (!ksmbd_session_rpc_method(sess, id))
4091 return -ENOENT;
4092
4093 ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004094 req->FileInfoClass, le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09004095
4096 switch (req->FileInfoClass) {
4097 case FILE_STANDARD_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004098 get_standard_info_pipe(rsp, rsp_org);
Namjae Jeone2f34482021-03-16 10:49:09 +09004099 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004100 rsp, rsp_org,
4101 FILE_STANDARD_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09004102 break;
4103 case FILE_INTERNAL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004104 get_internal_info_pipe(rsp, id, rsp_org);
Namjae Jeone2f34482021-03-16 10:49:09 +09004105 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004106 rsp, rsp_org,
4107 FILE_INTERNAL_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09004108 break;
4109 default:
4110 ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004111 req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09004112 rc = -EOPNOTSUPP;
4113 }
4114 return rc;
4115}
4116
4117/**
4118 * smb2_get_ea() - handler for smb2 get extended attribute command
4119 * @work: smb work containing query info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09004120 * @fp: ksmbd_file pointer
4121 * @req: get extended attribute request
4122 * @rsp: response buffer pointer
4123 * @rsp_org: base response buffer pointer in case of chained response
Namjae Jeone2f34482021-03-16 10:49:09 +09004124 *
4125 * Return: 0 on success, otherwise error
4126 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09004127static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004128 struct smb2_query_info_req *req,
4129 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004130{
4131 struct smb2_ea_info *eainfo, *prev_eainfo;
4132 char *name, *ptr, *xattr_list = NULL, *buf;
4133 int rc, name_len, value_len, xattr_list_len, idx;
4134 ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0;
4135 struct smb2_ea_info_req *ea_req = NULL;
4136 struct path *path;
Hyunchul Lee465d7202021-07-03 12:10:36 +09004137 struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09004138
4139 if (!(fp->daccess & FILE_READ_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004140 pr_err("Not permitted to read ext attr : 0x%x\n",
4141 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004142 return -EACCES;
4143 }
4144
4145 path = &fp->filp->f_path;
4146 /* single EA entry is requested with given user.* name */
Namjae Jeon64b39f42021-03-30 14:25:35 +09004147 if (req->InputBufferLength) {
Namjae Jeon6d562622021-09-18 18:45:12 +09004148 if (le32_to_cpu(req->InputBufferLength) <
4149 sizeof(struct smb2_ea_info_req))
4150 return -EINVAL;
4151
Namjae Jeone2f34482021-03-16 10:49:09 +09004152 ea_req = (struct smb2_ea_info_req *)req->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004153 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09004154 /* need to send all EAs, if no specific EA is requested*/
4155 if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY)
4156 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09004157 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n",
4158 le32_to_cpu(req->Flags));
Namjae Jeone2f34482021-03-16 10:49:09 +09004159 }
4160
Hyunchul Lee34061d62021-10-16 08:39:54 +09004161 buf_free_len =
4162 smb2_calc_max_out_buf_len(work, 8,
4163 le32_to_cpu(req->OutputBufferLength));
4164 if (buf_free_len < 0)
4165 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09004166
4167 rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4168 if (rc < 0) {
4169 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4170 goto out;
4171 } else if (!rc) { /* there is no EA in the file */
4172 ksmbd_debug(SMB, "no ea data in the file\n");
4173 goto done;
4174 }
4175 xattr_list_len = rc;
4176
4177 ptr = (char *)rsp->Buffer;
4178 eainfo = (struct smb2_ea_info *)ptr;
4179 prev_eainfo = eainfo;
4180 idx = 0;
4181
4182 while (idx < xattr_list_len) {
4183 name = xattr_list + idx;
4184 name_len = strlen(name);
4185
4186 ksmbd_debug(SMB, "%s, len %d\n", name, name_len);
4187 idx += name_len + 1;
4188
4189 /*
4190 * CIFS does not support EA other than user.* namespace,
4191 * still keep the framework generic, to list other attrs
4192 * in future.
4193 */
4194 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4195 continue;
4196
4197 if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004198 STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004199 continue;
4200
4201 if (req->InputBufferLength &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09004202 strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name,
4203 ea_req->EaNameLength))
Namjae Jeone2f34482021-03-16 10:49:09 +09004204 continue;
4205
4206 if (!strncmp(&name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004207 DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004208 continue;
4209
4210 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4211 name_len -= XATTR_USER_PREFIX_LEN;
4212
4213 ptr = (char *)(&eainfo->name + name_len + 1);
4214 buf_free_len -= (offsetof(struct smb2_ea_info, name) +
4215 name_len + 1);
4216 /* bailout if xattr can't fit in buf_free_len */
Hyunchul Lee465d7202021-07-03 12:10:36 +09004217 value_len = ksmbd_vfs_getxattr(user_ns, path->dentry,
4218 name, &buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004219 if (value_len <= 0) {
4220 rc = -ENOENT;
4221 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4222 goto out;
4223 }
4224
4225 buf_free_len -= value_len;
4226 if (buf_free_len < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09004227 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004228 break;
4229 }
4230
4231 memcpy(ptr, buf, value_len);
Namjae Jeon79f6b112021-04-02 12:47:14 +09004232 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004233
4234 ptr += value_len;
4235 eainfo->Flags = 0;
4236 eainfo->EaNameLength = name_len;
4237
Namjae Jeon64b39f42021-03-30 14:25:35 +09004238 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004239 memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN],
Namjae Jeon070fb212021-05-26 17:57:12 +09004240 name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09004241 else
4242 memcpy(eainfo->name, name, name_len);
4243
4244 eainfo->name[name_len] = '\0';
4245 eainfo->EaValueLength = cpu_to_le16(value_len);
4246 next_offset = offsetof(struct smb2_ea_info, name) +
4247 name_len + 1 + value_len;
4248
4249 /* align next xattr entry at 4 byte bundary */
4250 alignment_bytes = ((next_offset + 3) & ~3) - next_offset;
4251 if (alignment_bytes) {
4252 memset(ptr, '\0', alignment_bytes);
4253 ptr += alignment_bytes;
4254 next_offset += alignment_bytes;
4255 buf_free_len -= alignment_bytes;
4256 }
4257 eainfo->NextEntryOffset = cpu_to_le32(next_offset);
4258 prev_eainfo = eainfo;
4259 eainfo = (struct smb2_ea_info *)ptr;
4260 rsp_data_cnt += next_offset;
4261
4262 if (req->InputBufferLength) {
4263 ksmbd_debug(SMB, "single entry requested\n");
4264 break;
4265 }
4266 }
4267
4268 /* no more ea entries */
4269 prev_eainfo->NextEntryOffset = 0;
4270done:
4271 rc = 0;
4272 if (rsp_data_cnt == 0)
4273 rsp->hdr.Status = STATUS_NO_EAS_ON_FILE;
4274 rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt);
4275 inc_rfc1001_len(rsp_org, rsp_data_cnt);
4276out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004277 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004278 return rc;
4279}
4280
4281static void get_file_access_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004282 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004283{
4284 struct smb2_file_access_info *file_info;
4285
4286 file_info = (struct smb2_file_access_info *)rsp->Buffer;
4287 file_info->AccessFlags = fp->daccess;
4288 rsp->OutputBufferLength =
4289 cpu_to_le32(sizeof(struct smb2_file_access_info));
4290 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info));
4291}
4292
4293static int get_file_basic_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004294 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004295{
Namjae Jeon88d30052021-09-29 15:37:18 +09004296 struct smb2_file_basic_info *basic_info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004297 struct kstat stat;
4298 u64 time;
4299
4300 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004301 pr_err("no right to read the attributes : 0x%x\n",
4302 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004303 return -EACCES;
4304 }
4305
Namjae Jeon88d30052021-09-29 15:37:18 +09004306 basic_info = (struct smb2_file_basic_info *)rsp->Buffer;
Hyunchul Leeaf349832021-06-30 18:25:53 +09004307 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4308 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004309 basic_info->CreationTime = cpu_to_le64(fp->create_time);
4310 time = ksmbd_UnixTimeToNT(stat.atime);
4311 basic_info->LastAccessTime = cpu_to_le64(time);
4312 time = ksmbd_UnixTimeToNT(stat.mtime);
4313 basic_info->LastWriteTime = cpu_to_le64(time);
4314 time = ksmbd_UnixTimeToNT(stat.ctime);
4315 basic_info->ChangeTime = cpu_to_le64(time);
4316 basic_info->Attributes = fp->f_ci->m_fattr;
4317 basic_info->Pad1 = 0;
4318 rsp->OutputBufferLength =
Namjae Jeon88d30052021-09-29 15:37:18 +09004319 cpu_to_le32(sizeof(struct smb2_file_basic_info));
4320 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_basic_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004321 return 0;
4322}
4323
4324static unsigned long long get_allocation_size(struct inode *inode,
Namjae Jeon070fb212021-05-26 17:57:12 +09004325 struct kstat *stat)
Namjae Jeone2f34482021-03-16 10:49:09 +09004326{
4327 unsigned long long alloc_size = 0;
4328
4329 if (!S_ISDIR(stat->mode)) {
4330 if ((inode->i_blocks << 9) <= stat->size)
4331 alloc_size = stat->size;
4332 else
4333 alloc_size = inode->i_blocks << 9;
Namjae Jeone2f34482021-03-16 10:49:09 +09004334 }
4335
4336 return alloc_size;
4337}
4338
4339static void get_file_standard_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004340 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004341{
4342 struct smb2_file_standard_info *sinfo;
4343 unsigned int delete_pending;
4344 struct inode *inode;
4345 struct kstat stat;
4346
Namjae Jeonab0b2632021-06-29 09:20:13 +09004347 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004348 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004349
4350 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4351 delete_pending = ksmbd_inode_pending_delete(fp);
4352
4353 sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat));
4354 sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4355 sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending);
4356 sinfo->DeletePending = delete_pending;
4357 sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4358 rsp->OutputBufferLength =
4359 cpu_to_le32(sizeof(struct smb2_file_standard_info));
4360 inc_rfc1001_len(rsp_org,
4361 sizeof(struct smb2_file_standard_info));
4362}
4363
4364static void get_file_alignment_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004365 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004366{
4367 struct smb2_file_alignment_info *file_info;
4368
4369 file_info = (struct smb2_file_alignment_info *)rsp->Buffer;
4370 file_info->AlignmentRequirement = 0;
4371 rsp->OutputBufferLength =
4372 cpu_to_le32(sizeof(struct smb2_file_alignment_info));
4373 inc_rfc1001_len(rsp_org,
4374 sizeof(struct smb2_file_alignment_info));
4375}
4376
4377static int get_file_all_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004378 struct smb2_query_info_rsp *rsp,
4379 struct ksmbd_file *fp,
4380 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004381{
4382 struct ksmbd_conn *conn = work->conn;
4383 struct smb2_file_all_info *file_info;
4384 unsigned int delete_pending;
4385 struct inode *inode;
4386 struct kstat stat;
4387 int conv_len;
4388 char *filename;
4389 u64 time;
4390
4391 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4392 ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004393 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004394 return -EACCES;
4395 }
4396
Hyunchul Lee265fd192021-09-25 00:06:16 +09004397 filename = convert_to_nt_pathname(fp->filename);
Namjae Jeone2f34482021-03-16 10:49:09 +09004398 if (!filename)
4399 return -ENOMEM;
4400
Namjae Jeonab0b2632021-06-29 09:20:13 +09004401 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004402 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004403
4404 ksmbd_debug(SMB, "filename = %s\n", filename);
4405 delete_pending = ksmbd_inode_pending_delete(fp);
4406 file_info = (struct smb2_file_all_info *)rsp->Buffer;
4407
4408 file_info->CreationTime = cpu_to_le64(fp->create_time);
4409 time = ksmbd_UnixTimeToNT(stat.atime);
4410 file_info->LastAccessTime = cpu_to_le64(time);
4411 time = ksmbd_UnixTimeToNT(stat.mtime);
4412 file_info->LastWriteTime = cpu_to_le64(time);
4413 time = ksmbd_UnixTimeToNT(stat.ctime);
4414 file_info->ChangeTime = cpu_to_le64(time);
4415 file_info->Attributes = fp->f_ci->m_fattr;
4416 file_info->Pad1 = 0;
4417 file_info->AllocationSize =
4418 cpu_to_le64(get_allocation_size(inode, &stat));
4419 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4420 file_info->NumberOfLinks =
4421 cpu_to_le32(get_nlink(&stat) - delete_pending);
4422 file_info->DeletePending = delete_pending;
4423 file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4424 file_info->Pad2 = 0;
4425 file_info->IndexNumber = cpu_to_le64(stat.ino);
4426 file_info->EASize = 0;
4427 file_info->AccessFlags = fp->daccess;
4428 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4429 file_info->Mode = fp->coption;
4430 file_info->AlignmentRequirement = 0;
Namjae Jeon070fb212021-05-26 17:57:12 +09004431 conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename,
4432 PATH_MAX, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004433 conv_len *= 2;
4434 file_info->FileNameLength = cpu_to_le32(conv_len);
4435 rsp->OutputBufferLength =
4436 cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1);
4437 kfree(filename);
4438 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4439 return 0;
4440}
4441
4442static void get_file_alternate_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004443 struct smb2_query_info_rsp *rsp,
4444 struct ksmbd_file *fp,
4445 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004446{
4447 struct ksmbd_conn *conn = work->conn;
4448 struct smb2_file_alt_name_info *file_info;
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004449 struct dentry *dentry = fp->filp->f_path.dentry;
Namjae Jeone2f34482021-03-16 10:49:09 +09004450 int conv_len;
Namjae Jeone2f34482021-03-16 10:49:09 +09004451
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004452 spin_lock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004453 file_info = (struct smb2_file_alt_name_info *)rsp->Buffer;
4454 conv_len = ksmbd_extract_shortname(conn,
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004455 dentry->d_name.name,
Namjae Jeone2f34482021-03-16 10:49:09 +09004456 file_info->FileName);
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004457 spin_unlock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004458 file_info->FileNameLength = cpu_to_le32(conv_len);
4459 rsp->OutputBufferLength =
4460 cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len);
4461 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4462}
4463
4464static void get_file_stream_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004465 struct smb2_query_info_rsp *rsp,
4466 struct ksmbd_file *fp,
4467 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004468{
4469 struct ksmbd_conn *conn = work->conn;
4470 struct smb2_file_stream_info *file_info;
4471 char *stream_name, *xattr_list = NULL, *stream_buf;
4472 struct kstat stat;
4473 struct path *path = &fp->filp->f_path;
4474 ssize_t xattr_list_len;
4475 int nbytes = 0, streamlen, stream_name_len, next, idx = 0;
Hyunchul Lee34061d62021-10-16 08:39:54 +09004476 int buf_free_len;
4477 struct smb2_query_info_req *req = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09004478
Hyunchul Leeaf349832021-06-30 18:25:53 +09004479 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4480 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004481 file_info = (struct smb2_file_stream_info *)rsp->Buffer;
4482
Namjae Jeon1ec721532021-11-21 11:32:39 +09004483 buf_free_len =
4484 smb2_calc_max_out_buf_len(work, 8,
4485 le32_to_cpu(req->OutputBufferLength));
4486 if (buf_free_len < 0)
4487 goto out;
4488
Namjae Jeone2f34482021-03-16 10:49:09 +09004489 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4490 if (xattr_list_len < 0) {
4491 goto out;
4492 } else if (!xattr_list_len) {
4493 ksmbd_debug(SMB, "empty xattr in the file\n");
4494 goto out;
4495 }
4496
4497 while (idx < xattr_list_len) {
4498 stream_name = xattr_list + idx;
4499 streamlen = strlen(stream_name);
4500 idx += streamlen + 1;
4501
4502 ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen);
4503
4504 if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004505 STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004506 continue;
4507
4508 stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN +
4509 STREAM_PREFIX_LEN);
4510 streamlen = stream_name_len;
4511
4512 /* plus : size */
4513 streamlen += 1;
4514 stream_buf = kmalloc(streamlen + 1, GFP_KERNEL);
4515 if (!stream_buf)
4516 break;
4517
4518 streamlen = snprintf(stream_buf, streamlen + 1,
Namjae Jeon070fb212021-05-26 17:57:12 +09004519 ":%s", &stream_name[XATTR_NAME_STREAM_LEN]);
Namjae Jeone2f34482021-03-16 10:49:09 +09004520
Hyunchul Lee34061d62021-10-16 08:39:54 +09004521 next = sizeof(struct smb2_file_stream_info) + streamlen * 2;
Namjae Jeon178ca6f2021-11-24 10:23:02 +09004522 if (next > buf_free_len) {
4523 kfree(stream_buf);
Hyunchul Lee34061d62021-10-16 08:39:54 +09004524 break;
Namjae Jeon178ca6f2021-11-24 10:23:02 +09004525 }
Hyunchul Lee34061d62021-10-16 08:39:54 +09004526
Namjae Jeon070fb212021-05-26 17:57:12 +09004527 file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes];
Namjae Jeone2f34482021-03-16 10:49:09 +09004528 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004529 stream_buf, streamlen,
4530 conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004531 streamlen *= 2;
4532 kfree(stream_buf);
4533 file_info->StreamNameLength = cpu_to_le32(streamlen);
4534 file_info->StreamSize = cpu_to_le64(stream_name_len);
4535 file_info->StreamAllocationSize = cpu_to_le64(stream_name_len);
4536
Namjae Jeone2f34482021-03-16 10:49:09 +09004537 nbytes += next;
Hyunchul Lee34061d62021-10-16 08:39:54 +09004538 buf_free_len -= next;
Namjae Jeone2f34482021-03-16 10:49:09 +09004539 file_info->NextEntryOffset = cpu_to_le32(next);
4540 }
4541
Namjae Jeon1ec721532021-11-21 11:32:39 +09004542out:
Hyunchul Lee34061d62021-10-16 08:39:54 +09004543 if (!S_ISDIR(stat.mode) &&
4544 buf_free_len >= sizeof(struct smb2_file_stream_info) + 7 * 2) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004545 file_info = (struct smb2_file_stream_info *)
4546 &rsp->Buffer[nbytes];
4547 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004548 "::$DATA", 7, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004549 streamlen *= 2;
4550 file_info->StreamNameLength = cpu_to_le32(streamlen);
Namjae Jeon1ec721532021-11-21 11:32:39 +09004551 file_info->StreamSize = cpu_to_le64(stat.size);
4552 file_info->StreamAllocationSize = cpu_to_le64(stat.blocks << 9);
Namjae Jeone2f34482021-03-16 10:49:09 +09004553 nbytes += sizeof(struct smb2_file_stream_info) + streamlen;
4554 }
4555
4556 /* last entry offset should be 0 */
4557 file_info->NextEntryOffset = 0;
Namjae Jeon79f6b112021-04-02 12:47:14 +09004558 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004559
4560 rsp->OutputBufferLength = cpu_to_le32(nbytes);
4561 inc_rfc1001_len(rsp_org, nbytes);
4562}
4563
4564static void get_file_internal_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004565 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004566{
4567 struct smb2_file_internal_info *file_info;
4568 struct kstat stat;
4569
Hyunchul Leeaf349832021-06-30 18:25:53 +09004570 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4571 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004572 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4573 file_info->IndexNumber = cpu_to_le64(stat.ino);
4574 rsp->OutputBufferLength =
4575 cpu_to_le32(sizeof(struct smb2_file_internal_info));
4576 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
4577}
4578
4579static int get_file_network_open_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004580 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004581{
4582 struct smb2_file_ntwrk_info *file_info;
4583 struct inode *inode;
4584 struct kstat stat;
4585 u64 time;
4586
4587 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004588 pr_err("no right to read the attributes : 0x%x\n",
4589 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004590 return -EACCES;
4591 }
4592
4593 file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer;
4594
Namjae Jeonab0b2632021-06-29 09:20:13 +09004595 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004596 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004597
4598 file_info->CreationTime = cpu_to_le64(fp->create_time);
4599 time = ksmbd_UnixTimeToNT(stat.atime);
4600 file_info->LastAccessTime = cpu_to_le64(time);
4601 time = ksmbd_UnixTimeToNT(stat.mtime);
4602 file_info->LastWriteTime = cpu_to_le64(time);
4603 time = ksmbd_UnixTimeToNT(stat.ctime);
4604 file_info->ChangeTime = cpu_to_le64(time);
4605 file_info->Attributes = fp->f_ci->m_fattr;
4606 file_info->AllocationSize =
4607 cpu_to_le64(get_allocation_size(inode, &stat));
4608 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4609 file_info->Reserved = cpu_to_le32(0);
4610 rsp->OutputBufferLength =
4611 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info));
4612 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info));
4613 return 0;
4614}
4615
Namjae Jeon64b39f42021-03-30 14:25:35 +09004616static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004617{
4618 struct smb2_file_ea_info *file_info;
4619
4620 file_info = (struct smb2_file_ea_info *)rsp->Buffer;
4621 file_info->EASize = 0;
4622 rsp->OutputBufferLength =
4623 cpu_to_le32(sizeof(struct smb2_file_ea_info));
4624 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info));
4625}
4626
4627static void get_file_position_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004628 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004629{
4630 struct smb2_file_pos_info *file_info;
4631
4632 file_info = (struct smb2_file_pos_info *)rsp->Buffer;
4633 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4634 rsp->OutputBufferLength =
4635 cpu_to_le32(sizeof(struct smb2_file_pos_info));
4636 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info));
4637}
4638
4639static void get_file_mode_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004640 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004641{
4642 struct smb2_file_mode_info *file_info;
4643
4644 file_info = (struct smb2_file_mode_info *)rsp->Buffer;
4645 file_info->Mode = fp->coption & FILE_MODE_INFO_MASK;
4646 rsp->OutputBufferLength =
4647 cpu_to_le32(sizeof(struct smb2_file_mode_info));
4648 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info));
4649}
4650
4651static void get_file_compression_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004652 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004653{
4654 struct smb2_file_comp_info *file_info;
4655 struct kstat stat;
4656
Hyunchul Leeaf349832021-06-30 18:25:53 +09004657 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4658 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004659
4660 file_info = (struct smb2_file_comp_info *)rsp->Buffer;
4661 file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9);
4662 file_info->CompressionFormat = COMPRESSION_FORMAT_NONE;
4663 file_info->CompressionUnitShift = 0;
4664 file_info->ChunkShift = 0;
4665 file_info->ClusterShift = 0;
4666 memset(&file_info->Reserved[0], 0, 3);
4667
4668 rsp->OutputBufferLength =
4669 cpu_to_le32(sizeof(struct smb2_file_comp_info));
4670 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info));
4671}
4672
4673static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004674 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004675{
4676 struct smb2_file_attr_tag_info *file_info;
4677
4678 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004679 pr_err("no right to read the attributes : 0x%x\n",
4680 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004681 return -EACCES;
4682 }
4683
4684 file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer;
4685 file_info->FileAttributes = fp->f_ci->m_fattr;
4686 file_info->ReparseTag = 0;
4687 rsp->OutputBufferLength =
4688 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info));
Namjae Jeon070fb212021-05-26 17:57:12 +09004689 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004690 return 0;
4691}
4692
4693static int find_file_posix_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004694 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004695{
4696 struct smb311_posix_qinfo *file_info;
Namjae Jeonab0b2632021-06-29 09:20:13 +09004697 struct inode *inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09004698 u64 time;
4699
4700 file_info = (struct smb311_posix_qinfo *)rsp->Buffer;
4701 file_info->CreationTime = cpu_to_le64(fp->create_time);
4702 time = ksmbd_UnixTimeToNT(inode->i_atime);
4703 file_info->LastAccessTime = cpu_to_le64(time);
4704 time = ksmbd_UnixTimeToNT(inode->i_mtime);
4705 file_info->LastWriteTime = cpu_to_le64(time);
4706 time = ksmbd_UnixTimeToNT(inode->i_ctime);
4707 file_info->ChangeTime = cpu_to_le64(time);
4708 file_info->DosAttributes = fp->f_ci->m_fattr;
4709 file_info->Inode = cpu_to_le64(inode->i_ino);
4710 file_info->EndOfFile = cpu_to_le64(inode->i_size);
4711 file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9);
4712 file_info->HardLinks = cpu_to_le32(inode->i_nlink);
4713 file_info->Mode = cpu_to_le32(inode->i_mode);
4714 file_info->DeviceId = cpu_to_le32(inode->i_rdev);
4715 rsp->OutputBufferLength =
4716 cpu_to_le32(sizeof(struct smb311_posix_qinfo));
Namjae Jeon64b39f42021-03-30 14:25:35 +09004717 inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo));
Namjae Jeone2f34482021-03-16 10:49:09 +09004718 return 0;
4719}
4720
Namjae Jeone2f34482021-03-16 10:49:09 +09004721static int smb2_get_info_file(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004722 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004723 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09004724{
4725 struct ksmbd_file *fp;
4726 int fileinfoclass = 0;
4727 int rc = 0;
4728 int file_infoclass_size;
4729 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4730
4731 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004732 KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004733 /* smb2 info file called for pipe */
Namjae Jeoncb451722021-11-03 08:08:44 +09004734 return smb2_get_info_file_pipe(work->sess, req, rsp,
4735 work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004736 }
4737
4738 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon38673692021-07-08 12:32:27 +09004739 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
4740 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004741 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004742 id = work->compound_fid;
4743 pid = work->compound_pfid;
4744 }
4745 }
4746
Namjae Jeon38673692021-07-08 12:32:27 +09004747 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004748 id = le64_to_cpu(req->VolatileFileId);
4749 pid = le64_to_cpu(req->PersistentFileId);
4750 }
4751
4752 fp = ksmbd_lookup_fd_slow(work, id, pid);
4753 if (!fp)
4754 return -ENOENT;
4755
4756 fileinfoclass = req->FileInfoClass;
4757
4758 switch (fileinfoclass) {
4759 case FILE_ACCESS_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004760 get_file_access_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004761 file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE;
4762 break;
4763
4764 case FILE_BASIC_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004765 rc = get_file_basic_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004766 file_infoclass_size = FILE_BASIC_INFORMATION_SIZE;
4767 break;
4768
4769 case FILE_STANDARD_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004770 get_file_standard_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004771 file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE;
4772 break;
4773
4774 case FILE_ALIGNMENT_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004775 get_file_alignment_info(rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004776 file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE;
4777 break;
4778
4779 case FILE_ALL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004780 rc = get_file_all_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004781 file_infoclass_size = FILE_ALL_INFORMATION_SIZE;
4782 break;
4783
4784 case FILE_ALTERNATE_NAME_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004785 get_file_alternate_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004786 file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE;
4787 break;
4788
4789 case FILE_STREAM_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004790 get_file_stream_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004791 file_infoclass_size = FILE_STREAM_INFORMATION_SIZE;
4792 break;
4793
4794 case FILE_INTERNAL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004795 get_file_internal_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004796 file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE;
4797 break;
4798
4799 case FILE_NETWORK_OPEN_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004800 rc = get_file_network_open_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004801 file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE;
4802 break;
4803
4804 case FILE_EA_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004805 get_file_ea_info(rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004806 file_infoclass_size = FILE_EA_INFORMATION_SIZE;
4807 break;
4808
4809 case FILE_FULL_EA_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004810 rc = smb2_get_ea(work, fp, req, rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004811 file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE;
4812 break;
4813
4814 case FILE_POSITION_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004815 get_file_position_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004816 file_infoclass_size = FILE_POSITION_INFORMATION_SIZE;
4817 break;
4818
4819 case FILE_MODE_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004820 get_file_mode_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004821 file_infoclass_size = FILE_MODE_INFORMATION_SIZE;
4822 break;
4823
4824 case FILE_COMPRESSION_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004825 get_file_compression_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004826 file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE;
4827 break;
4828
4829 case FILE_ATTRIBUTE_TAG_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004830 rc = get_file_attribute_tag_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004831 file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE;
4832 break;
4833 case SMB_FIND_FILE_POSIX_INFO:
4834 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004835 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004836 rc = -EOPNOTSUPP;
4837 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09004838 rc = find_file_posix_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004839 file_infoclass_size = sizeof(struct smb311_posix_qinfo);
4840 }
4841 break;
4842 default:
4843 ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n",
4844 fileinfoclass);
4845 rc = -EOPNOTSUPP;
4846 }
4847 if (!rc)
4848 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004849 rsp, work->response_buf,
Namjae Jeone2f34482021-03-16 10:49:09 +09004850 file_infoclass_size);
4851 ksmbd_fd_put(work, fp);
4852 return rc;
4853}
4854
Namjae Jeone2f34482021-03-16 10:49:09 +09004855static int smb2_get_info_filesystem(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004856 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004857 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09004858{
4859 struct ksmbd_session *sess = work->sess;
4860 struct ksmbd_conn *conn = sess->conn;
4861 struct ksmbd_share_config *share = work->tcon->share_conf;
4862 int fsinfoclass = 0;
4863 struct kstatfs stfs;
4864 struct path path;
4865 int rc = 0, len;
4866 int fs_infoclass_size = 0;
4867
Hyunchul Lee265fd192021-09-25 00:06:16 +09004868 rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09004869 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004870 pr_err("cannot create vfs path\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004871 return -EIO;
4872 }
4873
4874 rc = vfs_statfs(&path, &stfs);
4875 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004876 pr_err("cannot do stat of path %s\n", share->path);
Namjae Jeone2f34482021-03-16 10:49:09 +09004877 path_put(&path);
4878 return -EIO;
4879 }
4880
4881 fsinfoclass = req->FileInfoClass;
4882
4883 switch (fsinfoclass) {
4884 case FS_DEVICE_INFORMATION:
4885 {
4886 struct filesystem_device_info *info;
4887
4888 info = (struct filesystem_device_info *)rsp->Buffer;
4889
4890 info->DeviceType = cpu_to_le32(stfs.f_type);
4891 info->DeviceCharacteristics = cpu_to_le32(0x00000020);
4892 rsp->OutputBufferLength = cpu_to_le32(8);
Namjae Jeoncb451722021-11-03 08:08:44 +09004893 inc_rfc1001_len(work->response_buf, 8);
Namjae Jeone2f34482021-03-16 10:49:09 +09004894 fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE;
4895 break;
4896 }
4897 case FS_ATTRIBUTE_INFORMATION:
4898 {
4899 struct filesystem_attribute_info *info;
4900 size_t sz;
4901
4902 info = (struct filesystem_attribute_info *)rsp->Buffer;
4903 info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS |
4904 FILE_PERSISTENT_ACLS |
4905 FILE_UNICODE_ON_DISK |
4906 FILE_CASE_PRESERVED_NAMES |
Namjae Jeoneb817362021-05-18 10:37:59 +09004907 FILE_CASE_SENSITIVE_SEARCH |
4908 FILE_SUPPORTS_BLOCK_REFCOUNTING);
Namjae Jeone2f34482021-03-16 10:49:09 +09004909
4910 info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps);
4911
4912 info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen);
4913 len = smbConvertToUTF16((__le16 *)info->FileSystemName,
4914 "NTFS", PATH_MAX, conn->local_nls, 0);
4915 len = len * 2;
4916 info->FileSystemNameLen = cpu_to_le32(len);
4917 sz = sizeof(struct filesystem_attribute_info) - 2 + len;
4918 rsp->OutputBufferLength = cpu_to_le32(sz);
Namjae Jeoncb451722021-11-03 08:08:44 +09004919 inc_rfc1001_len(work->response_buf, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09004920 fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE;
4921 break;
4922 }
4923 case FS_VOLUME_INFORMATION:
4924 {
4925 struct filesystem_vol_info *info;
4926 size_t sz;
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004927 unsigned int serial_crc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09004928
4929 info = (struct filesystem_vol_info *)(rsp->Buffer);
4930 info->VolumeCreationTime = 0;
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004931 serial_crc = crc32_le(serial_crc, share->name,
4932 strlen(share->name));
4933 serial_crc = crc32_le(serial_crc, share->path,
4934 strlen(share->path));
4935 serial_crc = crc32_le(serial_crc, ksmbd_netbios_name(),
4936 strlen(ksmbd_netbios_name()));
Namjae Jeone2f34482021-03-16 10:49:09 +09004937 /* Taking dummy value of serial number*/
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004938 info->SerialNumber = cpu_to_le32(serial_crc);
Namjae Jeone2f34482021-03-16 10:49:09 +09004939 len = smbConvertToUTF16((__le16 *)info->VolumeLabel,
4940 share->name, PATH_MAX,
4941 conn->local_nls, 0);
4942 len = len * 2;
4943 info->VolumeLabelSize = cpu_to_le32(len);
4944 info->Reserved = 0;
4945 sz = sizeof(struct filesystem_vol_info) - 2 + len;
4946 rsp->OutputBufferLength = cpu_to_le32(sz);
Namjae Jeoncb451722021-11-03 08:08:44 +09004947 inc_rfc1001_len(work->response_buf, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09004948 fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE;
4949 break;
4950 }
4951 case FS_SIZE_INFORMATION:
4952 {
4953 struct filesystem_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004954
4955 info = (struct filesystem_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004956 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4957 info->FreeAllocationUnits = 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(24);
Namjae Jeoncb451722021-11-03 08:08:44 +09004961 inc_rfc1001_len(work->response_buf, 24);
Namjae Jeone2f34482021-03-16 10:49:09 +09004962 fs_infoclass_size = FS_SIZE_INFORMATION_SIZE;
4963 break;
4964 }
4965 case FS_FULL_SIZE_INFORMATION:
4966 {
4967 struct smb2_fs_full_size_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004968
4969 info = (struct smb2_fs_full_size_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004970 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4971 info->CallerAvailableAllocationUnits =
4972 cpu_to_le64(stfs.f_bavail);
4973 info->ActualAvailableAllocationUnits =
4974 cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004975 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4976 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004977 rsp->OutputBufferLength = cpu_to_le32(32);
Namjae Jeoncb451722021-11-03 08:08:44 +09004978 inc_rfc1001_len(work->response_buf, 32);
Namjae Jeone2f34482021-03-16 10:49:09 +09004979 fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE;
4980 break;
4981 }
4982 case FS_OBJECT_ID_INFORMATION:
4983 {
4984 struct object_id_info *info;
4985
4986 info = (struct object_id_info *)(rsp->Buffer);
4987
4988 if (!user_guest(sess->user))
4989 memcpy(info->objid, user_passkey(sess->user), 16);
4990 else
4991 memset(info->objid, 0, 16);
4992
4993 info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC);
4994 info->extended_info.version = cpu_to_le32(1);
4995 info->extended_info.release = cpu_to_le32(1);
4996 info->extended_info.rel_date = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004997 memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0"));
Namjae Jeone2f34482021-03-16 10:49:09 +09004998 rsp->OutputBufferLength = cpu_to_le32(64);
Namjae Jeoncb451722021-11-03 08:08:44 +09004999 inc_rfc1001_len(work->response_buf, 64);
Namjae Jeone2f34482021-03-16 10:49:09 +09005000 fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE;
5001 break;
5002 }
5003 case FS_SECTOR_SIZE_INFORMATION:
5004 {
5005 struct smb3_fs_ss_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09005006
5007 info = (struct smb3_fs_ss_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09005008
Namjae Jeon131bac12021-06-22 16:20:47 +09005009 info->LogicalBytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005010 info->PhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09005011 cpu_to_le32(stfs.f_bsize);
5012 info->PhysicalBytesPerSectorForPerf = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005013 info->FSEffPhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09005014 cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005015 info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE |
5016 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE);
5017 info->ByteOffsetForSectorAlignment = 0;
5018 info->ByteOffsetForPartitionAlignment = 0;
5019 rsp->OutputBufferLength = cpu_to_le32(28);
Namjae Jeoncb451722021-11-03 08:08:44 +09005020 inc_rfc1001_len(work->response_buf, 28);
Namjae Jeone2f34482021-03-16 10:49:09 +09005021 fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE;
5022 break;
5023 }
5024 case FS_CONTROL_INFORMATION:
5025 {
5026 /*
5027 * TODO : The current implementation is based on
5028 * test result with win7(NTFS) server. It's need to
5029 * modify this to get valid Quota values
5030 * from Linux kernel
5031 */
5032 struct smb2_fs_control_info *info;
5033
5034 info = (struct smb2_fs_control_info *)(rsp->Buffer);
5035 info->FreeSpaceStartFiltering = 0;
5036 info->FreeSpaceThreshold = 0;
5037 info->FreeSpaceStopFiltering = 0;
5038 info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID);
5039 info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID);
5040 info->Padding = 0;
5041 rsp->OutputBufferLength = cpu_to_le32(48);
Namjae Jeoncb451722021-11-03 08:08:44 +09005042 inc_rfc1001_len(work->response_buf, 48);
Namjae Jeone2f34482021-03-16 10:49:09 +09005043 fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE;
5044 break;
5045 }
5046 case FS_POSIX_INFORMATION:
5047 {
5048 struct filesystem_posix_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09005049
5050 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005051 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005052 rc = -EOPNOTSUPP;
5053 } else {
5054 info = (struct filesystem_posix_info *)(rsp->Buffer);
Namjae Jeonee81cae2021-06-26 22:32:34 +09005055 info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005056 info->BlockSize = cpu_to_le32(stfs.f_bsize);
5057 info->TotalBlocks = cpu_to_le64(stfs.f_blocks);
5058 info->BlocksAvail = cpu_to_le64(stfs.f_bfree);
5059 info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail);
5060 info->TotalFileNodes = cpu_to_le64(stfs.f_files);
5061 info->FreeFileNodes = cpu_to_le64(stfs.f_ffree);
5062 rsp->OutputBufferLength = cpu_to_le32(56);
Namjae Jeoncb451722021-11-03 08:08:44 +09005063 inc_rfc1001_len(work->response_buf, 56);
Namjae Jeone2f34482021-03-16 10:49:09 +09005064 fs_infoclass_size = FS_POSIX_INFORMATION_SIZE;
5065 }
5066 break;
5067 }
5068 default:
5069 path_put(&path);
5070 return -EOPNOTSUPP;
5071 }
5072 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09005073 rsp, work->response_buf,
Namjae Jeone2f34482021-03-16 10:49:09 +09005074 fs_infoclass_size);
5075 path_put(&path);
5076 return rc;
5077}
5078
5079static int smb2_get_info_sec(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005080 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09005081 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09005082{
5083 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005084 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09005085 struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL;
5086 struct smb_fattr fattr = {{0}};
5087 struct inode *inode;
5088 __u32 secdesclen;
5089 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5090 int addition_info = le32_to_cpu(req->AdditionalInformation);
5091 int rc;
5092
Namjae Jeone294f782021-06-28 15:26:37 +09005093 if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
5094 PROTECTED_DACL_SECINFO |
5095 UNPROTECTED_DACL_SECINFO)) {
Namjae Jeon8e537d12021-11-21 07:48:45 +09005096 ksmbd_debug(SMB, "Unsupported addition info: 0x%x)\n",
Namjae Jeone294f782021-06-28 15:26:37 +09005097 addition_info);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09005098
5099 pntsd->revision = cpu_to_le16(1);
5100 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED);
5101 pntsd->osidoffset = 0;
5102 pntsd->gsidoffset = 0;
5103 pntsd->sacloffset = 0;
5104 pntsd->dacloffset = 0;
5105
5106 secdesclen = sizeof(struct smb_ntsd);
5107 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
Namjae Jeoncb451722021-11-03 08:08:44 +09005108 inc_rfc1001_len(work->response_buf, secdesclen);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09005109
5110 return 0;
5111 }
5112
Namjae Jeone2f34482021-03-16 10:49:09 +09005113 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon38673692021-07-08 12:32:27 +09005114 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
5115 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005116 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005117 id = work->compound_fid;
5118 pid = work->compound_pfid;
5119 }
5120 }
5121
Namjae Jeon38673692021-07-08 12:32:27 +09005122 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005123 id = le64_to_cpu(req->VolatileFileId);
5124 pid = le64_to_cpu(req->PersistentFileId);
5125 }
5126
5127 fp = ksmbd_lookup_fd_slow(work, id, pid);
5128 if (!fp)
5129 return -ENOENT;
5130
Hyunchul Lee465d7202021-07-03 12:10:36 +09005131 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeonab0b2632021-06-29 09:20:13 +09005132 inode = file_inode(fp->filp);
Christian Brauner43205ca2021-08-23 17:13:50 +02005133 ksmbd_acls_fattr(&fattr, user_ns, inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09005134
5135 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09005136 KSMBD_SHARE_FLAG_ACL_XATTR))
Hyunchul Lee465d7202021-07-03 12:10:36 +09005137 ksmbd_vfs_get_sd_xattr(work->conn, user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005138 fp->filp->f_path.dentry, &ppntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09005139
Hyunchul Lee465d7202021-07-03 12:10:36 +09005140 rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info,
5141 &secdesclen, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09005142 posix_acl_release(fattr.cf_acls);
5143 posix_acl_release(fattr.cf_dacls);
5144 kfree(ppntsd);
5145 ksmbd_fd_put(work, fp);
5146 if (rc)
5147 return rc;
5148
5149 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
Namjae Jeoncb451722021-11-03 08:08:44 +09005150 inc_rfc1001_len(work->response_buf, secdesclen);
Namjae Jeone2f34482021-03-16 10:49:09 +09005151 return 0;
5152}
5153
5154/**
5155 * smb2_query_info() - handler for smb2 query info command
5156 * @work: smb work containing query info request buffer
5157 *
5158 * Return: 0 on success, otherwise error
5159 */
5160int smb2_query_info(struct ksmbd_work *work)
5161{
5162 struct smb2_query_info_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09005163 struct smb2_query_info_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005164 int rc = 0;
5165
Namjae Jeone2f34482021-03-16 10:49:09 +09005166 WORK_BUFFERS(work, req, rsp);
5167
5168 ksmbd_debug(SMB, "GOT query info request\n");
5169
5170 switch (req->InfoType) {
5171 case SMB2_O_INFO_FILE:
5172 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005173 rc = smb2_get_info_file(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005174 break;
5175 case SMB2_O_INFO_FILESYSTEM:
5176 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005177 rc = smb2_get_info_filesystem(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005178 break;
5179 case SMB2_O_INFO_SECURITY:
5180 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005181 rc = smb2_get_info_sec(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005182 break;
5183 default:
5184 ksmbd_debug(SMB, "InfoType %d not supported yet\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005185 req->InfoType);
Namjae Jeone2f34482021-03-16 10:49:09 +09005186 rc = -EOPNOTSUPP;
5187 }
5188
5189 if (rc < 0) {
5190 if (rc == -EACCES)
5191 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5192 else if (rc == -ENOENT)
5193 rsp->hdr.Status = STATUS_FILE_CLOSED;
5194 else if (rc == -EIO)
5195 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5196 else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0)
5197 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5198 smb2_set_err_rsp(work);
5199
5200 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005201 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005202 return rc;
5203 }
5204 rsp->StructureSize = cpu_to_le16(9);
5205 rsp->OutputBufferOffset = cpu_to_le16(72);
Namjae Jeoncb451722021-11-03 08:08:44 +09005206 inc_rfc1001_len(work->response_buf, 8);
Namjae Jeone2f34482021-03-16 10:49:09 +09005207 return 0;
5208}
5209
5210/**
5211 * smb2_close_pipe() - handler for closing IPC pipe
5212 * @work: smb work containing close request buffer
5213 *
5214 * Return: 0
5215 */
5216static noinline int smb2_close_pipe(struct ksmbd_work *work)
5217{
Namjae Jeon64b39f42021-03-30 14:25:35 +09005218 u64 id;
Namjae Jeoncb451722021-11-03 08:08:44 +09005219 struct smb2_close_req *req = smb2_get_msg(work->request_buf);
5220 struct smb2_close_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005221
5222 id = le64_to_cpu(req->VolatileFileId);
5223 ksmbd_session_rpc_close(work->sess, id);
5224
5225 rsp->StructureSize = cpu_to_le16(60);
5226 rsp->Flags = 0;
5227 rsp->Reserved = 0;
5228 rsp->CreationTime = 0;
5229 rsp->LastAccessTime = 0;
5230 rsp->LastWriteTime = 0;
5231 rsp->ChangeTime = 0;
5232 rsp->AllocationSize = 0;
5233 rsp->EndOfFile = 0;
5234 rsp->Attributes = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09005235 inc_rfc1001_len(work->response_buf, 60);
Namjae Jeone2f34482021-03-16 10:49:09 +09005236 return 0;
5237}
5238
5239/**
5240 * smb2_close() - handler for smb2 close file command
5241 * @work: smb work containing close request buffer
5242 *
5243 * Return: 0
5244 */
5245int smb2_close(struct ksmbd_work *work)
5246{
Namjae Jeon38673692021-07-08 12:32:27 +09005247 u64 volatile_id = KSMBD_NO_FID;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005248 u64 sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09005249 struct smb2_close_req *req;
5250 struct smb2_close_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005251 struct ksmbd_conn *conn = work->conn;
5252 struct ksmbd_file *fp;
5253 struct inode *inode;
5254 u64 time;
5255 int err = 0;
5256
Namjae Jeone2f34482021-03-16 10:49:09 +09005257 WORK_BUFFERS(work, req, rsp);
5258
5259 if (test_share_config_flag(work->tcon->share_conf,
5260 KSMBD_SHARE_FLAG_PIPE)) {
5261 ksmbd_debug(SMB, "IPC pipe close request\n");
5262 return smb2_close_pipe(work);
5263 }
5264
5265 sess_id = le64_to_cpu(req->hdr.SessionId);
5266 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5267 sess_id = work->compound_sid;
5268
5269 work->compound_sid = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005270 if (check_session_id(conn, sess_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005271 work->compound_sid = sess_id;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005272 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005273 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
5274 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5275 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5276 err = -EBADF;
5277 goto out;
5278 }
5279
5280 if (work->next_smb2_rcv_hdr_off &&
Namjae Jeon38673692021-07-08 12:32:27 +09005281 !has_file_id(le64_to_cpu(req->VolatileFileId))) {
5282 if (!has_file_id(work->compound_fid)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005283 /* file already closed, return FILE_CLOSED */
5284 ksmbd_debug(SMB, "file already closed\n");
5285 rsp->hdr.Status = STATUS_FILE_CLOSED;
5286 err = -EBADF;
5287 goto out;
5288 } else {
Namjae Jeon38673692021-07-08 12:32:27 +09005289 ksmbd_debug(SMB,
5290 "Compound request set FID = %llu:%llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005291 work->compound_fid,
5292 work->compound_pfid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005293 volatile_id = work->compound_fid;
5294
5295 /* file closed, stored id is not valid anymore */
5296 work->compound_fid = KSMBD_NO_FID;
5297 work->compound_pfid = KSMBD_NO_FID;
5298 }
5299 } else {
5300 volatile_id = le64_to_cpu(req->VolatileFileId);
5301 }
Namjae Jeon38673692021-07-08 12:32:27 +09005302 ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id);
Namjae Jeone2f34482021-03-16 10:49:09 +09005303
5304 rsp->StructureSize = cpu_to_le16(60);
5305 rsp->Reserved = 0;
5306
5307 if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) {
5308 fp = ksmbd_lookup_fd_fast(work, volatile_id);
5309 if (!fp) {
5310 err = -ENOENT;
5311 goto out;
5312 }
5313
Namjae Jeonab0b2632021-06-29 09:20:13 +09005314 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005315 rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
5316 rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 :
5317 cpu_to_le64(inode->i_blocks << 9);
5318 rsp->EndOfFile = cpu_to_le64(inode->i_size);
5319 rsp->Attributes = fp->f_ci->m_fattr;
5320 rsp->CreationTime = cpu_to_le64(fp->create_time);
5321 time = ksmbd_UnixTimeToNT(inode->i_atime);
5322 rsp->LastAccessTime = cpu_to_le64(time);
5323 time = ksmbd_UnixTimeToNT(inode->i_mtime);
5324 rsp->LastWriteTime = cpu_to_le64(time);
5325 time = ksmbd_UnixTimeToNT(inode->i_ctime);
5326 rsp->ChangeTime = cpu_to_le64(time);
5327 ksmbd_fd_put(work, fp);
5328 } else {
5329 rsp->Flags = 0;
5330 rsp->AllocationSize = 0;
5331 rsp->EndOfFile = 0;
5332 rsp->Attributes = 0;
5333 rsp->CreationTime = 0;
5334 rsp->LastAccessTime = 0;
5335 rsp->LastWriteTime = 0;
5336 rsp->ChangeTime = 0;
5337 }
5338
5339 err = ksmbd_close_fd(work, volatile_id);
5340out:
5341 if (err) {
5342 if (rsp->hdr.Status == 0)
5343 rsp->hdr.Status = STATUS_FILE_CLOSED;
5344 smb2_set_err_rsp(work);
5345 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09005346 inc_rfc1001_len(work->response_buf, 60);
Namjae Jeone2f34482021-03-16 10:49:09 +09005347 }
5348
5349 return 0;
5350}
5351
5352/**
5353 * smb2_echo() - handler for smb2 echo(ping) command
5354 * @work: smb work containing echo request buffer
5355 *
5356 * Return: 0
5357 */
5358int smb2_echo(struct ksmbd_work *work)
5359{
Namjae Jeoncb451722021-11-03 08:08:44 +09005360 struct smb2_echo_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005361
5362 rsp->StructureSize = cpu_to_le16(4);
5363 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09005364 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09005365 return 0;
5366}
5367
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005368static int smb2_rename(struct ksmbd_work *work,
5369 struct ksmbd_file *fp,
5370 struct user_namespace *user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +09005371 struct smb2_file_rename_info *file_info,
5372 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005373{
5374 struct ksmbd_share_config *share = fp->tcon->share_conf;
5375 char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL;
5376 char *pathname = NULL;
5377 struct path path;
5378 bool file_present = true;
5379 int rc;
5380
5381 ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n");
5382 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5383 if (!pathname)
5384 return -ENOMEM;
5385
5386 abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
5387 if (IS_ERR(abs_oldname)) {
5388 rc = -EINVAL;
5389 goto out;
5390 }
5391 old_name = strrchr(abs_oldname, '/');
Namjae Jeon64b39f42021-03-30 14:25:35 +09005392 if (old_name && old_name[1] != '\0') {
Namjae Jeone2f34482021-03-16 10:49:09 +09005393 old_name++;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005394 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005395 ksmbd_debug(SMB, "can't get last component in path %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005396 abs_oldname);
Namjae Jeone2f34482021-03-16 10:49:09 +09005397 rc = -ENOENT;
5398 goto out;
5399 }
5400
Marios Makassikis80917f12021-12-01 21:41:19 +01005401 new_name = smb2_get_name(file_info->FileName,
Namjae Jeone2f34482021-03-16 10:49:09 +09005402 le32_to_cpu(file_info->FileNameLength),
5403 local_nls);
5404 if (IS_ERR(new_name)) {
5405 rc = PTR_ERR(new_name);
5406 goto out;
5407 }
5408
5409 if (strchr(new_name, ':')) {
5410 int s_type;
5411 char *xattr_stream_name, *stream_name = NULL;
5412 size_t xattr_stream_size;
5413 int len;
5414
5415 rc = parse_stream_name(new_name, &stream_name, &s_type);
5416 if (rc < 0)
5417 goto out;
5418
5419 len = strlen(new_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005420 if (len > 0 && new_name[len - 1] != '/') {
Namjae Jeonbde16942021-06-28 15:23:19 +09005421 pr_err("not allow base filename in rename\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005422 rc = -ESHARE;
5423 goto out;
5424 }
5425
5426 rc = ksmbd_vfs_xattr_stream_name(stream_name,
5427 &xattr_stream_name,
5428 &xattr_stream_size,
5429 s_type);
5430 if (rc)
5431 goto out;
5432
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005433 rc = ksmbd_vfs_setxattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005434 fp->filp->f_path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09005435 xattr_stream_name,
5436 NULL, 0, 0);
5437 if (rc < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005438 pr_err("failed to store stream name in xattr: %d\n",
5439 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005440 rc = -EINVAL;
5441 goto out;
5442 }
5443
5444 goto out;
5445 }
5446
5447 ksmbd_debug(SMB, "new name %s\n", new_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005448 rc = ksmbd_vfs_kern_path(work, new_name, LOOKUP_NO_SYMLINKS, &path, 1);
5449 if (rc) {
5450 if (rc != -ENOENT)
5451 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005452 file_present = false;
Hyunchul Lee265fd192021-09-25 00:06:16 +09005453 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005454 path_put(&path);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005455 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005456
5457 if (ksmbd_share_veto_filename(share, new_name)) {
5458 rc = -ENOENT;
5459 ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name);
5460 goto out;
5461 }
5462
5463 if (file_info->ReplaceIfExists) {
5464 if (file_present) {
5465 rc = ksmbd_vfs_remove_file(work, new_name);
5466 if (rc) {
5467 if (rc != -ENOTEMPTY)
5468 rc = -EINVAL;
5469 ksmbd_debug(SMB, "cannot delete %s, rc %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005470 new_name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005471 goto out;
5472 }
5473 }
5474 } else {
5475 if (file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005476 strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005477 rc = -EEXIST;
5478 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005479 "cannot rename already existing file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005480 goto out;
5481 }
5482 }
5483
5484 rc = ksmbd_vfs_fp_rename(work, fp, new_name);
5485out:
5486 kfree(pathname);
5487 if (!IS_ERR(new_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005488 kfree(new_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005489 return rc;
5490}
5491
Namjae Jeone2f34482021-03-16 10:49:09 +09005492static int smb2_create_link(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005493 struct ksmbd_share_config *share,
5494 struct smb2_file_link_info *file_info,
Namjae Jeon9496e262021-09-29 15:41:48 +09005495 unsigned int buf_len, struct file *filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005496 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005497{
5498 char *link_name = NULL, *target_name = NULL, *pathname = NULL;
5499 struct path path;
5500 bool file_present = true;
5501 int rc;
5502
Namjae Jeon9496e262021-09-29 15:41:48 +09005503 if (buf_len < (u64)sizeof(struct smb2_file_link_info) +
5504 le32_to_cpu(file_info->FileNameLength))
5505 return -EINVAL;
5506
Namjae Jeone2f34482021-03-16 10:49:09 +09005507 ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n");
5508 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5509 if (!pathname)
5510 return -ENOMEM;
5511
Marios Makassikis80917f12021-12-01 21:41:19 +01005512 link_name = smb2_get_name(file_info->FileName,
Namjae Jeone2f34482021-03-16 10:49:09 +09005513 le32_to_cpu(file_info->FileNameLength),
5514 local_nls);
5515 if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) {
5516 rc = -EINVAL;
5517 goto out;
5518 }
5519
5520 ksmbd_debug(SMB, "link name is %s\n", link_name);
5521 target_name = d_path(&filp->f_path, pathname, PATH_MAX);
5522 if (IS_ERR(target_name)) {
5523 rc = -EINVAL;
5524 goto out;
5525 }
5526
5527 ksmbd_debug(SMB, "target name is %s\n", target_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005528 rc = ksmbd_vfs_kern_path(work, link_name, LOOKUP_NO_SYMLINKS, &path, 0);
5529 if (rc) {
5530 if (rc != -ENOENT)
5531 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005532 file_present = false;
Hyunchul Lee265fd192021-09-25 00:06:16 +09005533 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005534 path_put(&path);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005535 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005536
5537 if (file_info->ReplaceIfExists) {
5538 if (file_present) {
5539 rc = ksmbd_vfs_remove_file(work, link_name);
5540 if (rc) {
5541 rc = -EINVAL;
5542 ksmbd_debug(SMB, "cannot delete %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005543 link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005544 goto out;
5545 }
5546 }
5547 } else {
5548 if (file_present) {
5549 rc = -EEXIST;
5550 ksmbd_debug(SMB, "link already exists\n");
5551 goto out;
5552 }
5553 }
5554
5555 rc = ksmbd_vfs_link(work, target_name, link_name);
5556 if (rc)
5557 rc = -EINVAL;
5558out:
5559 if (!IS_ERR(link_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005560 kfree(link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005561 kfree(pathname);
5562 return rc;
5563}
5564
Namjae Jeon9496e262021-09-29 15:41:48 +09005565static int set_file_basic_info(struct ksmbd_file *fp,
5566 struct smb2_file_basic_info *file_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005567 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005568{
Namjae Jeone2f34482021-03-16 10:49:09 +09005569 struct iattr attrs;
Namjae Jeone2f34482021-03-16 10:49:09 +09005570 struct file *filp;
5571 struct inode *inode;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005572 struct user_namespace *user_ns;
Namjae Jeon4ffd5262021-09-07 08:15:21 +09005573 int rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09005574
Marios Makassikis7adfd4f2021-04-27 15:30:22 +09005575 if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005576 return -EACCES;
5577
Namjae Jeone2f34482021-03-16 10:49:09 +09005578 attrs.ia_valid = 0;
5579 filp = fp->filp;
5580 inode = file_inode(filp);
Hyunchul Lee465d7202021-07-03 12:10:36 +09005581 user_ns = file_mnt_user_ns(filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005582
5583 if (file_info->CreationTime)
5584 fp->create_time = le64_to_cpu(file_info->CreationTime);
5585
5586 if (file_info->LastAccessTime) {
5587 attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime);
5588 attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
5589 }
5590
Namjae Jeon64e78752021-10-03 13:19:00 +09005591 attrs.ia_valid |= ATTR_CTIME;
5592 if (file_info->ChangeTime)
Christian Braunerdb7fb6f2021-08-26 10:07:05 +09005593 attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime);
Namjae Jeon64e78752021-10-03 13:19:00 +09005594 else
5595 attrs.ia_ctime = inode->i_ctime;
Namjae Jeone2f34482021-03-16 10:49:09 +09005596
5597 if (file_info->LastWriteTime) {
5598 attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime);
5599 attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
5600 }
5601
5602 if (file_info->Attributes) {
5603 if (!S_ISDIR(inode->i_mode) &&
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09005604 file_info->Attributes & FILE_ATTRIBUTE_DIRECTORY_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005605 pr_err("can't change a file to a directory\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005606 return -EINVAL;
5607 }
5608
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09005609 if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == FILE_ATTRIBUTE_NORMAL_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005610 fp->f_ci->m_fattr = file_info->Attributes |
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09005611 (fp->f_ci->m_fattr & FILE_ATTRIBUTE_DIRECTORY_LE);
Namjae Jeone2f34482021-03-16 10:49:09 +09005612 }
5613
5614 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
5615 (file_info->CreationTime || file_info->Attributes)) {
5616 struct xattr_dos_attrib da = {0};
5617
5618 da.version = 4;
5619 da.itime = fp->itime;
5620 da.create_time = fp->create_time;
5621 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
5622 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
5623 XATTR_DOSINFO_ITIME;
5624
Hyunchul Lee465d7202021-07-03 12:10:36 +09005625 rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005626 filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09005627 if (rc)
5628 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005629 "failed to restore file attribute in EA\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005630 rc = 0;
5631 }
5632
Namjae Jeone2f34482021-03-16 10:49:09 +09005633 if (attrs.ia_valid) {
5634 struct dentry *dentry = filp->f_path.dentry;
5635 struct inode *inode = d_inode(dentry);
5636
5637 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
5638 return -EACCES;
5639
Namjae Jeone2f34482021-03-16 10:49:09 +09005640 inode_lock(inode);
Namjae Jeon64e78752021-10-03 13:19:00 +09005641 inode->i_ctime = attrs.ia_ctime;
5642 attrs.ia_valid &= ~ATTR_CTIME;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005643 rc = notify_change(user_ns, dentry, &attrs, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09005644 inode_unlock(inode);
5645 }
Christian Braunereb5784f2021-08-23 17:13:55 +02005646 return rc;
Namjae Jeone2f34482021-03-16 10:49:09 +09005647}
5648
5649static int set_file_allocation_info(struct ksmbd_work *work,
Namjae Jeon9496e262021-09-29 15:41:48 +09005650 struct ksmbd_file *fp,
5651 struct smb2_file_alloc_info *file_alloc_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005652{
5653 /*
5654 * TODO : It's working fine only when store dos attributes
5655 * is not yes. need to implement a logic which works
5656 * properly with any smb.conf option
5657 */
5658
Namjae Jeone2f34482021-03-16 10:49:09 +09005659 loff_t alloc_blks;
5660 struct inode *inode;
5661 int rc;
5662
Marios Makassikisa2996692021-04-27 15:29:01 +09005663 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005664 return -EACCES;
5665
Namjae Jeone2f34482021-03-16 10:49:09 +09005666 alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9;
5667 inode = file_inode(fp->filp);
5668
5669 if (alloc_blks > inode->i_blocks) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005670 smb_break_all_levII_oplock(work, fp, 1);
5671 rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
5672 alloc_blks * 512);
Namjae Jeone2f34482021-03-16 10:49:09 +09005673 if (rc && rc != -EOPNOTSUPP) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005674 pr_err("vfs_fallocate is failed : %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005675 return rc;
5676 }
5677 } else if (alloc_blks < inode->i_blocks) {
5678 loff_t size;
5679
5680 /*
5681 * Allocation size could be smaller than original one
5682 * which means allocated blocks in file should be
5683 * deallocated. use truncate to cut out it, but inode
5684 * size is also updated with truncate offset.
5685 * inode size is retained by backup inode size.
5686 */
5687 size = i_size_read(inode);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005688 rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512);
Namjae Jeone2f34482021-03-16 10:49:09 +09005689 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005690 pr_err("truncate failed! filename : %s, err %d\n",
5691 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005692 return rc;
5693 }
5694 if (size < alloc_blks * 512)
5695 i_size_write(inode, size);
5696 }
5697 return 0;
5698}
5699
Namjae Jeon64b39f42021-03-30 14:25:35 +09005700static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005701 struct smb2_file_eof_info *file_eof_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005702{
Namjae Jeone2f34482021-03-16 10:49:09 +09005703 loff_t newsize;
5704 struct inode *inode;
5705 int rc;
5706
Marios Makassikisa2996692021-04-27 15:29:01 +09005707 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005708 return -EACCES;
5709
Namjae Jeone2f34482021-03-16 10:49:09 +09005710 newsize = le64_to_cpu(file_eof_info->EndOfFile);
5711 inode = file_inode(fp->filp);
5712
5713 /*
5714 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called
5715 * on FAT32 shared device, truncate execution time is too long
5716 * and network error could cause from windows client. because
5717 * truncate of some filesystem like FAT32 fill zero data in
5718 * truncated range.
5719 */
5720 if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) {
5721 ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005722 fp->filename, newsize);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005723 rc = ksmbd_vfs_truncate(work, fp, newsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005724 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005725 ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005726 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005727 if (rc != -EAGAIN)
5728 rc = -EBADF;
5729 return rc;
5730 }
5731 }
5732 return 0;
5733}
5734
Namjae Jeon64b39f42021-03-30 14:25:35 +09005735static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005736 struct smb2_file_rename_info *rename_info,
5737 unsigned int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005738{
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005739 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09005740 struct ksmbd_file *parent_fp;
Namjae Jeon12202c02021-06-29 09:23:56 +09005741 struct dentry *parent;
5742 struct dentry *dentry = fp->filp->f_path.dentry;
5743 int ret;
Namjae Jeone2f34482021-03-16 10:49:09 +09005744
5745 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005746 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005747 return -EACCES;
5748 }
5749
Namjae Jeon9496e262021-09-29 15:41:48 +09005750 if (buf_len < (u64)sizeof(struct smb2_file_rename_info) +
5751 le32_to_cpu(rename_info->FileNameLength))
5752 return -EINVAL;
5753
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005754 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005755 if (ksmbd_stream_fd(fp))
5756 goto next;
5757
Namjae Jeon12202c02021-06-29 09:23:56 +09005758 parent = dget_parent(dentry);
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005759 ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
Namjae Jeon12202c02021-06-29 09:23:56 +09005760 if (ret) {
5761 dput(parent);
5762 return ret;
5763 }
5764
5765 parent_fp = ksmbd_lookup_fd_inode(d_inode(parent));
5766 inode_unlock(d_inode(parent));
5767 dput(parent);
5768
Namjae Jeone2f34482021-03-16 10:49:09 +09005769 if (parent_fp) {
5770 if (parent_fp->daccess & FILE_DELETE_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005771 pr_err("parent dir is opened with delete access\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005772 return -ESHARE;
5773 }
5774 }
5775next:
Namjae Jeon9496e262021-09-29 15:41:48 +09005776 return smb2_rename(work, fp, user_ns, rename_info,
Namjae Jeone2f34482021-03-16 10:49:09 +09005777 work->sess->conn->local_nls);
5778}
5779
Namjae Jeon9496e262021-09-29 15:41:48 +09005780static int set_file_disposition_info(struct ksmbd_file *fp,
5781 struct smb2_file_disposition_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005782{
Namjae Jeone2f34482021-03-16 10:49:09 +09005783 struct inode *inode;
5784
5785 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005786 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005787 return -EACCES;
5788 }
5789
5790 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005791 if (file_info->DeletePending) {
5792 if (S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005793 ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY)
Namjae Jeone2f34482021-03-16 10:49:09 +09005794 return -EBUSY;
5795 ksmbd_set_inode_pending_delete(fp);
5796 } else {
5797 ksmbd_clear_inode_pending_delete(fp);
5798 }
5799 return 0;
5800}
5801
Namjae Jeon9496e262021-09-29 15:41:48 +09005802static int set_file_position_info(struct ksmbd_file *fp,
5803 struct smb2_file_pos_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005804{
Namjae Jeone2f34482021-03-16 10:49:09 +09005805 loff_t current_byte_offset;
Namjae Jeonee81cae2021-06-26 22:32:34 +09005806 unsigned long sector_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09005807 struct inode *inode;
5808
5809 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005810 current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset);
Namjae Jeonee81cae2021-06-26 22:32:34 +09005811 sector_size = inode->i_sb->s_blocksize;
Namjae Jeone2f34482021-03-16 10:49:09 +09005812
5813 if (current_byte_offset < 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09005814 (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE &&
5815 current_byte_offset & (sector_size - 1))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005816 pr_err("CurrentByteOffset is not valid : %llu\n",
5817 current_byte_offset);
Namjae Jeone2f34482021-03-16 10:49:09 +09005818 return -EINVAL;
5819 }
5820
5821 fp->filp->f_pos = current_byte_offset;
5822 return 0;
5823}
5824
Namjae Jeon9496e262021-09-29 15:41:48 +09005825static int set_file_mode_info(struct ksmbd_file *fp,
5826 struct smb2_file_mode_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005827{
Namjae Jeone2f34482021-03-16 10:49:09 +09005828 __le32 mode;
5829
Namjae Jeone2f34482021-03-16 10:49:09 +09005830 mode = file_info->Mode;
5831
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09005832 if ((mode & ~FILE_MODE_INFO_MASK)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005833 pr_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode));
Namjae Jeone2f34482021-03-16 10:49:09 +09005834 return -EINVAL;
5835 }
5836
5837 /*
5838 * TODO : need to implement consideration for
5839 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT
5840 */
5841 ksmbd_vfs_set_fadvise(fp->filp, mode);
5842 fp->coption = mode;
5843 return 0;
5844}
5845
5846/**
5847 * smb2_set_info_file() - handler for smb2 set info command
5848 * @work: smb work containing set info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09005849 * @fp: ksmbd_file pointer
5850 * @info_class: smb2 set info class
5851 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09005852 *
5853 * Return: 0 on success, otherwise error
5854 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH
5855 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09005856static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005857 struct smb2_set_info_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09005858 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005859{
Namjae Jeon9496e262021-09-29 15:41:48 +09005860 unsigned int buf_len = le32_to_cpu(req->BufferLength);
5861
5862 switch (req->FileInfoClass) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005863 case FILE_BASIC_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005864 {
5865 if (buf_len < sizeof(struct smb2_file_basic_info))
5866 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005867
Namjae Jeon9496e262021-09-29 15:41:48 +09005868 return set_file_basic_info(fp, (struct smb2_file_basic_info *)req->Buffer, share);
5869 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005870 case FILE_ALLOCATION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005871 {
5872 if (buf_len < sizeof(struct smb2_file_alloc_info))
5873 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005874
Namjae Jeon9496e262021-09-29 15:41:48 +09005875 return set_file_allocation_info(work, fp,
5876 (struct smb2_file_alloc_info *)req->Buffer);
5877 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005878 case FILE_END_OF_FILE_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005879 {
5880 if (buf_len < sizeof(struct smb2_file_eof_info))
5881 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005882
Namjae Jeon9496e262021-09-29 15:41:48 +09005883 return set_end_of_file_info(work, fp,
5884 (struct smb2_file_eof_info *)req->Buffer);
5885 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005886 case FILE_RENAME_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005887 {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005888 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005889 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005890 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005891 return -EACCES;
5892 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005893
Namjae Jeon9496e262021-09-29 15:41:48 +09005894 if (buf_len < sizeof(struct smb2_file_rename_info))
5895 return -EINVAL;
5896
5897 return set_rename_info(work, fp,
5898 (struct smb2_file_rename_info *)req->Buffer,
5899 buf_len);
5900 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005901 case FILE_LINK_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005902 {
5903 if (buf_len < sizeof(struct smb2_file_link_info))
5904 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005905
Namjae Jeon9496e262021-09-29 15:41:48 +09005906 return smb2_create_link(work, work->tcon->share_conf,
5907 (struct smb2_file_link_info *)req->Buffer,
5908 buf_len, fp->filp,
5909 work->sess->conn->local_nls);
5910 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005911 case FILE_DISPOSITION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005912 {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005913 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005914 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005915 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005916 return -EACCES;
5917 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005918
Namjae Jeon9496e262021-09-29 15:41:48 +09005919 if (buf_len < sizeof(struct smb2_file_disposition_info))
5920 return -EINVAL;
5921
5922 return set_file_disposition_info(fp,
5923 (struct smb2_file_disposition_info *)req->Buffer);
5924 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005925 case FILE_FULL_EA_INFORMATION:
5926 {
5927 if (!(fp->daccess & FILE_WRITE_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005928 pr_err("Not permitted to write ext attr: 0x%x\n",
5929 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005930 return -EACCES;
5931 }
5932
Namjae Jeon9496e262021-09-29 15:41:48 +09005933 if (buf_len < sizeof(struct smb2_ea_info))
5934 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005935
Namjae Jeon9496e262021-09-29 15:41:48 +09005936 return smb2_set_ea((struct smb2_ea_info *)req->Buffer,
5937 buf_len, &fp->filp->f_path);
5938 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005939 case FILE_POSITION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005940 {
5941 if (buf_len < sizeof(struct smb2_file_pos_info))
5942 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005943
Namjae Jeon9496e262021-09-29 15:41:48 +09005944 return set_file_position_info(fp, (struct smb2_file_pos_info *)req->Buffer);
5945 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005946 case FILE_MODE_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005947 {
5948 if (buf_len < sizeof(struct smb2_file_mode_info))
5949 return -EINVAL;
5950
5951 return set_file_mode_info(fp, (struct smb2_file_mode_info *)req->Buffer);
5952 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005953 }
5954
Namjae Jeon9496e262021-09-29 15:41:48 +09005955 pr_err("Unimplemented Fileinfoclass :%d\n", req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09005956 return -EOPNOTSUPP;
5957}
5958
Namjae Jeon64b39f42021-03-30 14:25:35 +09005959static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005960 char *buffer, int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005961{
5962 struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer;
5963
5964 fp->saccess |= FILE_SHARE_DELETE_LE;
5965
Hyunchul Leeef24c962021-06-30 18:25:52 +09005966 return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd,
Namjae Jeone2f34482021-03-16 10:49:09 +09005967 buf_len, false);
5968}
5969
5970/**
5971 * smb2_set_info() - handler for smb2 set info command handler
5972 * @work: smb work containing set info request buffer
5973 *
5974 * Return: 0 on success, otherwise error
5975 */
5976int smb2_set_info(struct ksmbd_work *work)
5977{
5978 struct smb2_set_info_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09005979 struct smb2_set_info_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005980 struct ksmbd_file *fp;
5981 int rc = 0;
5982 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5983
5984 ksmbd_debug(SMB, "Received set info request\n");
5985
Namjae Jeone2f34482021-03-16 10:49:09 +09005986 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09005987 req = ksmbd_req_buf_next(work);
5988 rsp = ksmbd_resp_buf_next(work);
Namjae Jeon38673692021-07-08 12:32:27 +09005989 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
5990 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005991 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005992 id = work->compound_fid;
5993 pid = work->compound_pfid;
5994 }
5995 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09005996 req = smb2_get_msg(work->request_buf);
5997 rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005998 }
5999
Namjae Jeon38673692021-07-08 12:32:27 +09006000 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006001 id = le64_to_cpu(req->VolatileFileId);
6002 pid = le64_to_cpu(req->PersistentFileId);
6003 }
6004
6005 fp = ksmbd_lookup_fd_slow(work, id, pid);
6006 if (!fp) {
6007 ksmbd_debug(SMB, "Invalid id for close: %u\n", id);
6008 rc = -ENOENT;
6009 goto err_out;
6010 }
6011
6012 switch (req->InfoType) {
6013 case SMB2_O_INFO_FILE:
6014 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
Namjae Jeon9496e262021-09-29 15:41:48 +09006015 rc = smb2_set_info_file(work, fp, req, work->tcon->share_conf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006016 break;
6017 case SMB2_O_INFO_SECURITY:
6018 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
Namjae Jeone70e3922021-08-21 23:26:01 +09006019 if (ksmbd_override_fsids(work)) {
6020 rc = -ENOMEM;
6021 goto err_out;
6022 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006023 rc = smb2_set_info_sec(fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09006024 le32_to_cpu(req->AdditionalInformation),
6025 req->Buffer,
6026 le32_to_cpu(req->BufferLength));
Namjae Jeone70e3922021-08-21 23:26:01 +09006027 ksmbd_revert_fsids(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09006028 break;
6029 default:
6030 rc = -EOPNOTSUPP;
6031 }
6032
6033 if (rc < 0)
6034 goto err_out;
6035
6036 rsp->StructureSize = cpu_to_le16(2);
Namjae Jeoncb451722021-11-03 08:08:44 +09006037 inc_rfc1001_len(work->response_buf, 2);
Namjae Jeone2f34482021-03-16 10:49:09 +09006038 ksmbd_fd_put(work, fp);
6039 return 0;
6040
6041err_out:
Hyunchul Lee265fd192021-09-25 00:06:16 +09006042 if (rc == -EACCES || rc == -EPERM || rc == -EXDEV)
Namjae Jeone2f34482021-03-16 10:49:09 +09006043 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6044 else if (rc == -EINVAL)
6045 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6046 else if (rc == -ESHARE)
6047 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6048 else if (rc == -ENOENT)
6049 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
6050 else if (rc == -EBUSY || rc == -ENOTEMPTY)
6051 rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY;
6052 else if (rc == -EAGAIN)
6053 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
Namjae Jeonff1d5722021-04-13 13:18:10 +09006054 else if (rc == -EBADF || rc == -ESTALE)
Namjae Jeone2f34482021-03-16 10:49:09 +09006055 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6056 else if (rc == -EEXIST)
6057 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
6058 else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP)
6059 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
6060 smb2_set_err_rsp(work);
6061 ksmbd_fd_put(work, fp);
Namjae Jeon070fb212021-05-26 17:57:12 +09006062 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09006063 return rc;
6064}
6065
6066/**
6067 * smb2_read_pipe() - handler for smb2 read from IPC pipe
6068 * @work: smb work containing read IPC pipe command buffer
6069 *
6070 * Return: 0 on success, otherwise error
6071 */
6072static noinline int smb2_read_pipe(struct ksmbd_work *work)
6073{
6074 int nbytes = 0, err;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006075 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09006076 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeoncb451722021-11-03 08:08:44 +09006077 struct smb2_read_req *req = smb2_get_msg(work->request_buf);
6078 struct smb2_read_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006079
6080 id = le64_to_cpu(req->VolatileFileId);
6081
Namjae Jeoncb451722021-11-03 08:08:44 +09006082 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006083 rpc_resp = ksmbd_rpc_read(work->sess, id);
6084 if (rpc_resp) {
6085 if (rpc_resp->flags != KSMBD_RPC_OK) {
6086 err = -EINVAL;
6087 goto out;
6088 }
6089
6090 work->aux_payload_buf =
Namjae Jeon79f6b112021-04-02 12:47:14 +09006091 kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006092 if (!work->aux_payload_buf) {
6093 err = -ENOMEM;
6094 goto out;
6095 }
6096
6097 memcpy(work->aux_payload_buf, rpc_resp->payload,
Namjae Jeon070fb212021-05-26 17:57:12 +09006098 rpc_resp->payload_sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09006099
6100 nbytes = rpc_resp->payload_sz;
Namjae Jeoncb451722021-11-03 08:08:44 +09006101 work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09006102 work->aux_payload_sz = nbytes;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006103 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006104 }
6105
6106 rsp->StructureSize = cpu_to_le16(17);
6107 rsp->DataOffset = 80;
6108 rsp->Reserved = 0;
6109 rsp->DataLength = cpu_to_le32(nbytes);
6110 rsp->DataRemaining = 0;
Ronnie Sahlberg699230f2021-09-09 12:26:12 +09006111 rsp->Flags = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006112 inc_rfc1001_len(work->response_buf, nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09006113 return 0;
6114
6115out:
6116 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
6117 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006118 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006119 return err;
6120}
6121
6122static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006123 struct smb2_read_req *req, void *data_buf,
6124 size_t length)
Namjae Jeone2f34482021-03-16 10:49:09 +09006125{
6126 struct smb2_buffer_desc_v1 *desc =
6127 (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6128 int err;
6129
Namjae Jeon64b39f42021-03-30 14:25:35 +09006130 if (work->conn->dialect == SMB30_PROT_ID &&
6131 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09006132 return -EINVAL;
6133
Namjae Jeon64b39f42021-03-30 14:25:35 +09006134 if (req->ReadChannelInfoOffset == 0 ||
6135 le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09006136 return -EINVAL;
6137
6138 work->need_invalidate_rkey =
6139 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6140 work->remote_key = le32_to_cpu(desc->token);
6141
Namjae Jeon64b39f42021-03-30 14:25:35 +09006142 err = ksmbd_conn_rdma_write(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006143 le32_to_cpu(desc->token),
6144 le64_to_cpu(desc->offset),
6145 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006146 if (err)
6147 return err;
6148
6149 return length;
6150}
6151
6152/**
6153 * smb2_read() - handler for smb2 read from file
6154 * @work: smb work containing read command buffer
6155 *
6156 * Return: 0 on success, otherwise error
6157 */
6158int smb2_read(struct ksmbd_work *work)
6159{
6160 struct ksmbd_conn *conn = work->conn;
6161 struct smb2_read_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006162 struct smb2_read_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09006163 struct ksmbd_file *fp;
6164 loff_t offset;
6165 size_t length, mincount;
6166 ssize_t nbytes = 0, remain_bytes = 0;
6167 int err = 0;
6168
Namjae Jeone2f34482021-03-16 10:49:09 +09006169 WORK_BUFFERS(work, req, rsp);
6170
6171 if (test_share_config_flag(work->tcon->share_conf,
6172 KSMBD_SHARE_FLAG_PIPE)) {
6173 ksmbd_debug(SMB, "IPC pipe read request\n");
6174 return smb2_read_pipe(work);
6175 }
6176
Namjae Jeon070fb212021-05-26 17:57:12 +09006177 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
6178 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006179 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006180 err = -ENOENT;
6181 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006182 }
6183
6184 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006185 pr_err("Not permitted to read : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09006186 err = -EACCES;
6187 goto out;
6188 }
6189
6190 offset = le64_to_cpu(req->Offset);
6191 length = le32_to_cpu(req->Length);
6192 mincount = le32_to_cpu(req->MinimumCount);
6193
6194 if (length > conn->vals->max_read_size) {
6195 ksmbd_debug(SMB, "limiting read size to max size(%u)\n",
6196 conn->vals->max_read_size);
6197 err = -EINVAL;
6198 goto out;
6199 }
6200
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006201 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6202 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006203
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006204 work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006205 if (!work->aux_payload_buf) {
Dan Carpenterc1ea1112021-03-22 17:50:11 +03006206 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09006207 goto out;
6208 }
6209
6210 nbytes = ksmbd_vfs_read(work, fp, length, &offset);
6211 if (nbytes < 0) {
6212 err = nbytes;
6213 goto out;
6214 }
6215
6216 if ((nbytes == 0 && length != 0) || nbytes < mincount) {
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006217 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006218 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006219 rsp->hdr.Status = STATUS_END_OF_FILE;
6220 smb2_set_err_rsp(work);
6221 ksmbd_fd_put(work, fp);
6222 return 0;
6223 }
6224
6225 ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006226 nbytes, offset, mincount);
Namjae Jeone2f34482021-03-16 10:49:09 +09006227
6228 if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006229 req->Channel == SMB2_CHANNEL_RDMA_V1) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006230 /* write data to the client using rdma channel */
6231 remain_bytes = smb2_read_rdma_channel(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09006232 work->aux_payload_buf,
6233 nbytes);
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006234 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006235 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006236
6237 nbytes = 0;
6238 if (remain_bytes < 0) {
6239 err = (int)remain_bytes;
6240 goto out;
6241 }
6242 }
6243
6244 rsp->StructureSize = cpu_to_le16(17);
6245 rsp->DataOffset = 80;
6246 rsp->Reserved = 0;
6247 rsp->DataLength = cpu_to_le32(nbytes);
6248 rsp->DataRemaining = cpu_to_le32(remain_bytes);
Ronnie Sahlberg699230f2021-09-09 12:26:12 +09006249 rsp->Flags = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006250 inc_rfc1001_len(work->response_buf, 16);
6251 work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09006252 work->aux_payload_sz = nbytes;
Namjae Jeoncb451722021-11-03 08:08:44 +09006253 inc_rfc1001_len(work->response_buf, nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09006254 ksmbd_fd_put(work, fp);
6255 return 0;
6256
6257out:
6258 if (err) {
6259 if (err == -EISDIR)
6260 rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST;
6261 else if (err == -EAGAIN)
6262 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6263 else if (err == -ENOENT)
6264 rsp->hdr.Status = STATUS_FILE_CLOSED;
6265 else if (err == -EACCES)
6266 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6267 else if (err == -ESHARE)
6268 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6269 else if (err == -EINVAL)
6270 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6271 else
6272 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6273
6274 smb2_set_err_rsp(work);
6275 }
6276 ksmbd_fd_put(work, fp);
6277 return err;
6278}
6279
6280/**
6281 * smb2_write_pipe() - handler for smb2 write on IPC pipe
6282 * @work: smb work containing write IPC pipe command buffer
6283 *
6284 * Return: 0 on success, otherwise error
6285 */
6286static noinline int smb2_write_pipe(struct ksmbd_work *work)
6287{
Namjae Jeoncb451722021-11-03 08:08:44 +09006288 struct smb2_write_req *req = smb2_get_msg(work->request_buf);
6289 struct smb2_write_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006290 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006291 u64 id = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09006292 int err = 0, ret = 0;
6293 char *data_buf;
6294 size_t length;
6295
6296 length = le32_to_cpu(req->Length);
6297 id = le64_to_cpu(req->VolatileFileId);
6298
6299 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeoncb451722021-11-03 08:08:44 +09006300 offsetof(struct smb2_write_req, Buffer)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006301 data_buf = (char *)&req->Buffer[0];
6302 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09006303 if ((u64)le16_to_cpu(req->DataOffset) + length >
6304 get_rfc1002_len(work->request_buf)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006305 pr_err("invalid write data offset %u, smb_len %u\n",
6306 le16_to_cpu(req->DataOffset),
Namjae Jeoncb451722021-11-03 08:08:44 +09006307 get_rfc1002_len(work->request_buf));
Namjae Jeone2f34482021-03-16 10:49:09 +09006308 err = -EINVAL;
6309 goto out;
6310 }
6311
6312 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6313 le16_to_cpu(req->DataOffset));
6314 }
6315
6316 rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length);
6317 if (rpc_resp) {
6318 if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
6319 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006320 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006321 smb2_set_err_rsp(work);
6322 return -EOPNOTSUPP;
6323 }
6324 if (rpc_resp->flags != KSMBD_RPC_OK) {
6325 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6326 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006327 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006328 return ret;
6329 }
Namjae Jeon79f6b112021-04-02 12:47:14 +09006330 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006331 }
6332
6333 rsp->StructureSize = cpu_to_le16(17);
6334 rsp->DataOffset = 0;
6335 rsp->Reserved = 0;
6336 rsp->DataLength = cpu_to_le32(length);
6337 rsp->DataRemaining = 0;
6338 rsp->Reserved2 = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006339 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006340 return 0;
6341out:
6342 if (err) {
6343 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6344 smb2_set_err_rsp(work);
6345 }
6346
6347 return err;
6348}
6349
6350static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006351 struct smb2_write_req *req,
6352 struct ksmbd_file *fp,
6353 loff_t offset, size_t length, bool sync)
Namjae Jeone2f34482021-03-16 10:49:09 +09006354{
6355 struct smb2_buffer_desc_v1 *desc;
6356 char *data_buf;
6357 int ret;
6358 ssize_t nbytes;
6359
6360 desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6361
6362 if (work->conn->dialect == SMB30_PROT_ID &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006363 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09006364 return -EINVAL;
6365
6366 if (req->Length != 0 || req->DataOffset != 0)
6367 return -EINVAL;
6368
Namjae Jeon64b39f42021-03-30 14:25:35 +09006369 if (req->WriteChannelInfoOffset == 0 ||
6370 le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09006371 return -EINVAL;
6372
6373 work->need_invalidate_rkey =
6374 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6375 work->remote_key = le32_to_cpu(desc->token);
6376
Namjae Jeon79f6b112021-04-02 12:47:14 +09006377 data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006378 if (!data_buf)
6379 return -ENOMEM;
6380
6381 ret = ksmbd_conn_rdma_read(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006382 le32_to_cpu(desc->token),
6383 le64_to_cpu(desc->offset),
6384 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006385 if (ret < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09006386 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006387 return ret;
6388 }
6389
Namjae Jeon64b39f42021-03-30 14:25:35 +09006390 ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006391 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006392 if (ret < 0)
6393 return ret;
6394
6395 return nbytes;
6396}
6397
6398/**
6399 * smb2_write() - handler for smb2 write from file
6400 * @work: smb work containing write command buffer
6401 *
6402 * Return: 0 on success, otherwise error
6403 */
6404int smb2_write(struct ksmbd_work *work)
6405{
6406 struct smb2_write_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006407 struct smb2_write_rsp *rsp;
Namjae Jeonbcd62a32021-05-10 09:08:19 +09006408 struct ksmbd_file *fp = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006409 loff_t offset;
6410 size_t length;
6411 ssize_t nbytes;
6412 char *data_buf;
6413 bool writethrough = false;
6414 int err = 0;
6415
Namjae Jeone2f34482021-03-16 10:49:09 +09006416 WORK_BUFFERS(work, req, rsp);
6417
Namjae Jeon64b39f42021-03-30 14:25:35 +09006418 if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006419 ksmbd_debug(SMB, "IPC pipe write request\n");
6420 return smb2_write_pipe(work);
6421 }
6422
6423 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
6424 ksmbd_debug(SMB, "User does not have write permission\n");
6425 err = -EACCES;
6426 goto out;
6427 }
6428
Namjae Jeon64b39f42021-03-30 14:25:35 +09006429 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
Namjae Jeon070fb212021-05-26 17:57:12 +09006430 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006431 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006432 err = -ENOENT;
6433 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006434 }
6435
6436 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006437 pr_err("Not permitted to write : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09006438 err = -EACCES;
6439 goto out;
6440 }
6441
6442 offset = le64_to_cpu(req->Offset);
6443 length = le32_to_cpu(req->Length);
6444
6445 if (length > work->conn->vals->max_write_size) {
6446 ksmbd_debug(SMB, "limiting write size to max size(%u)\n",
6447 work->conn->vals->max_write_size);
6448 err = -EINVAL;
6449 goto out;
6450 }
6451
6452 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6453 writethrough = true;
6454
6455 if (req->Channel != SMB2_CHANNEL_RDMA_V1 &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006456 req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006457 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeoncb451722021-11-03 08:08:44 +09006458 offsetof(struct smb2_write_req, Buffer)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006459 data_buf = (char *)&req->Buffer[0];
6460 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09006461 if ((u64)le16_to_cpu(req->DataOffset) + length >
6462 get_rfc1002_len(work->request_buf)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006463 pr_err("invalid write data offset %u, smb_len %u\n",
6464 le16_to_cpu(req->DataOffset),
Namjae Jeoncb451722021-11-03 08:08:44 +09006465 get_rfc1002_len(work->request_buf));
Namjae Jeone2f34482021-03-16 10:49:09 +09006466 err = -EINVAL;
6467 goto out;
6468 }
6469
6470 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6471 le16_to_cpu(req->DataOffset));
6472 }
6473
6474 ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags));
6475 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6476 writethrough = true;
6477
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006478 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6479 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006480 err = ksmbd_vfs_write(work, fp, data_buf, length, &offset,
6481 writethrough, &nbytes);
6482 if (err < 0)
6483 goto out;
6484 } else {
6485 /* read data from the client using rdma channel, and
6486 * write the data.
6487 */
6488 nbytes = smb2_write_rdma_channel(work, req, fp, offset,
Namjae Jeon070fb212021-05-26 17:57:12 +09006489 le32_to_cpu(req->RemainingBytes),
6490 writethrough);
Namjae Jeone2f34482021-03-16 10:49:09 +09006491 if (nbytes < 0) {
6492 err = (int)nbytes;
6493 goto out;
6494 }
6495 }
6496
6497 rsp->StructureSize = cpu_to_le16(17);
6498 rsp->DataOffset = 0;
6499 rsp->Reserved = 0;
6500 rsp->DataLength = cpu_to_le32(nbytes);
6501 rsp->DataRemaining = 0;
6502 rsp->Reserved2 = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006503 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006504 ksmbd_fd_put(work, fp);
6505 return 0;
6506
6507out:
6508 if (err == -EAGAIN)
6509 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6510 else if (err == -ENOSPC || err == -EFBIG)
6511 rsp->hdr.Status = STATUS_DISK_FULL;
6512 else if (err == -ENOENT)
6513 rsp->hdr.Status = STATUS_FILE_CLOSED;
6514 else if (err == -EACCES)
6515 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6516 else if (err == -ESHARE)
6517 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6518 else if (err == -EINVAL)
6519 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6520 else
6521 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6522
6523 smb2_set_err_rsp(work);
6524 ksmbd_fd_put(work, fp);
6525 return err;
6526}
6527
6528/**
6529 * smb2_flush() - handler for smb2 flush file - fsync
6530 * @work: smb work containing flush command buffer
6531 *
6532 * Return: 0 on success, otherwise error
6533 */
6534int smb2_flush(struct ksmbd_work *work)
6535{
6536 struct smb2_flush_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006537 struct smb2_flush_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09006538 int err;
6539
Namjae Jeone2f34482021-03-16 10:49:09 +09006540 WORK_BUFFERS(work, req, rsp);
6541
6542 ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006543 le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006544
6545 err = ksmbd_vfs_fsync(work,
6546 le64_to_cpu(req->VolatileFileId),
6547 le64_to_cpu(req->PersistentFileId));
6548 if (err)
6549 goto out;
6550
6551 rsp->StructureSize = cpu_to_le16(4);
6552 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006553 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09006554 return 0;
6555
6556out:
6557 if (err) {
6558 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6559 smb2_set_err_rsp(work);
6560 }
6561
6562 return err;
6563}
6564
6565/**
6566 * smb2_cancel() - handler for smb2 cancel command
6567 * @work: smb work containing cancel command buffer
6568 *
6569 * Return: 0 on success, otherwise error
6570 */
6571int smb2_cancel(struct ksmbd_work *work)
6572{
6573 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09006574 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006575 struct smb2_hdr *chdr;
6576 struct ksmbd_work *cancel_work = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006577 int canceled = 0;
6578 struct list_head *command_list;
6579
6580 ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006581 hdr->MessageId, hdr->Flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09006582
6583 if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) {
6584 command_list = &conn->async_requests;
6585
6586 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006587 list_for_each_entry(cancel_work, command_list,
6588 async_request_entry) {
Namjae Jeoncb451722021-11-03 08:08:44 +09006589 chdr = smb2_get_msg(cancel_work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006590
6591 if (cancel_work->async_id !=
Namjae Jeon64b39f42021-03-30 14:25:35 +09006592 le64_to_cpu(hdr->Id.AsyncId))
Namjae Jeone2f34482021-03-16 10:49:09 +09006593 continue;
6594
6595 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006596 "smb2 with AsyncId %llu cancelled command = 0x%x\n",
6597 le64_to_cpu(hdr->Id.AsyncId),
6598 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006599 canceled = 1;
6600 break;
6601 }
6602 spin_unlock(&conn->request_lock);
6603 } else {
6604 command_list = &conn->requests;
6605
6606 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006607 list_for_each_entry(cancel_work, command_list, request_entry) {
Namjae Jeoncb451722021-11-03 08:08:44 +09006608 chdr = smb2_get_msg(cancel_work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006609
6610 if (chdr->MessageId != hdr->MessageId ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006611 cancel_work == work)
Namjae Jeone2f34482021-03-16 10:49:09 +09006612 continue;
6613
6614 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006615 "smb2 with mid %llu cancelled command = 0x%x\n",
6616 le64_to_cpu(hdr->MessageId),
6617 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006618 canceled = 1;
6619 break;
6620 }
6621 spin_unlock(&conn->request_lock);
6622 }
6623
6624 if (canceled) {
6625 cancel_work->state = KSMBD_WORK_CANCELLED;
6626 if (cancel_work->cancel_fn)
6627 cancel_work->cancel_fn(cancel_work->cancel_argv);
6628 }
6629
6630 /* For SMB2_CANCEL command itself send no response*/
6631 work->send_no_response = 1;
6632 return 0;
6633}
6634
6635struct file_lock *smb_flock_init(struct file *f)
6636{
6637 struct file_lock *fl;
6638
6639 fl = locks_alloc_lock();
6640 if (!fl)
6641 goto out;
6642
6643 locks_init_lock(fl);
6644
6645 fl->fl_owner = f;
6646 fl->fl_pid = current->tgid;
6647 fl->fl_file = f;
6648 fl->fl_flags = FL_POSIX;
6649 fl->fl_ops = NULL;
6650 fl->fl_lmops = NULL;
6651
6652out:
6653 return fl;
6654}
6655
6656static int smb2_set_flock_flags(struct file_lock *flock, int flags)
6657{
6658 int cmd = -EINVAL;
6659
6660 /* Checking for wrong flag combination during lock request*/
6661 switch (flags) {
6662 case SMB2_LOCKFLAG_SHARED:
6663 ksmbd_debug(SMB, "received shared request\n");
6664 cmd = F_SETLKW;
6665 flock->fl_type = F_RDLCK;
6666 flock->fl_flags |= FL_SLEEP;
6667 break;
6668 case SMB2_LOCKFLAG_EXCLUSIVE:
6669 ksmbd_debug(SMB, "received exclusive request\n");
6670 cmd = F_SETLKW;
6671 flock->fl_type = F_WRLCK;
6672 flock->fl_flags |= FL_SLEEP;
6673 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006674 case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006675 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006676 "received shared & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006677 cmd = F_SETLK;
6678 flock->fl_type = F_RDLCK;
6679 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006680 case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006681 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006682 "received exclusive & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006683 cmd = F_SETLK;
6684 flock->fl_type = F_WRLCK;
6685 break;
6686 case SMB2_LOCKFLAG_UNLOCK:
6687 ksmbd_debug(SMB, "received unlock request\n");
6688 flock->fl_type = F_UNLCK;
6689 cmd = 0;
6690 break;
6691 }
6692
6693 return cmd;
6694}
6695
6696static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock,
Namjae Jeon070fb212021-05-26 17:57:12 +09006697 unsigned int cmd, int flags,
6698 struct list_head *lock_list)
Namjae Jeone2f34482021-03-16 10:49:09 +09006699{
6700 struct ksmbd_lock *lock;
6701
6702 lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL);
6703 if (!lock)
6704 return NULL;
6705
6706 lock->cmd = cmd;
6707 lock->fl = flock;
6708 lock->start = flock->fl_start;
6709 lock->end = flock->fl_end;
6710 lock->flags = flags;
6711 if (lock->start == lock->end)
6712 lock->zero_len = 1;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006713 INIT_LIST_HEAD(&lock->clist);
6714 INIT_LIST_HEAD(&lock->flist);
Namjae Jeone2f34482021-03-16 10:49:09 +09006715 INIT_LIST_HEAD(&lock->llist);
Namjae Jeone2f34482021-03-16 10:49:09 +09006716 list_add_tail(&lock->llist, lock_list);
6717
6718 return lock;
6719}
6720
6721static void smb2_remove_blocked_lock(void **argv)
6722{
6723 struct file_lock *flock = (struct file_lock *)argv[0];
6724
6725 ksmbd_vfs_posix_lock_unblock(flock);
6726 wake_up(&flock->fl_wait);
6727}
6728
6729static inline bool lock_defer_pending(struct file_lock *fl)
6730{
6731 /* check pending lock waiters */
6732 return waitqueue_active(&fl->fl_wait);
6733}
6734
6735/**
6736 * smb2_lock() - handler for smb2 file lock command
6737 * @work: smb work containing lock command buffer
6738 *
6739 * Return: 0 on success, otherwise error
6740 */
6741int smb2_lock(struct ksmbd_work *work)
6742{
Namjae Jeoncb451722021-11-03 08:08:44 +09006743 struct smb2_lock_req *req = smb2_get_msg(work->request_buf);
6744 struct smb2_lock_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006745 struct smb2_lock_element *lock_ele;
6746 struct ksmbd_file *fp = NULL;
6747 struct file_lock *flock = NULL;
6748 struct file *filp = NULL;
6749 int lock_count;
6750 int flags = 0;
6751 int cmd = 0;
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006752 int err = -EIO, i, rc = 0;
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006753 u64 lock_start, lock_length;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006754 struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2;
6755 struct ksmbd_conn *conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09006756 int nolock = 0;
6757 LIST_HEAD(lock_list);
6758 LIST_HEAD(rollback_list);
6759 int prior_lock = 0;
6760
6761 ksmbd_debug(SMB, "Received lock request\n");
6762 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006763 le64_to_cpu(req->VolatileFileId),
6764 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006765 if (!fp) {
6766 ksmbd_debug(SMB, "Invalid file id for lock : %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006767 le64_to_cpu(req->VolatileFileId));
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006768 err = -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09006769 goto out2;
6770 }
6771
6772 filp = fp->filp;
6773 lock_count = le16_to_cpu(req->LockCount);
6774 lock_ele = req->locks;
6775
6776 ksmbd_debug(SMB, "lock count is %d\n", lock_count);
Namjae Jeon070fb212021-05-26 17:57:12 +09006777 if (!lock_count) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006778 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006779 goto out2;
6780 }
6781
6782 for (i = 0; i < lock_count; i++) {
6783 flags = le32_to_cpu(lock_ele[i].Flags);
6784
6785 flock = smb_flock_init(filp);
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006786 if (!flock)
Namjae Jeone2f34482021-03-16 10:49:09 +09006787 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006788
6789 cmd = smb2_set_flock_flags(flock, flags);
6790
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006791 lock_start = le64_to_cpu(lock_ele[i].Offset);
6792 lock_length = le64_to_cpu(lock_ele[i].Length);
6793 if (lock_start > U64_MAX - lock_length) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006794 pr_err("Invalid lock range requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006795 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6796 goto out;
6797 }
6798
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006799 if (lock_start > OFFSET_MAX)
6800 flock->fl_start = OFFSET_MAX;
6801 else
6802 flock->fl_start = lock_start;
6803
Namjae Jeone2f34482021-03-16 10:49:09 +09006804 lock_length = le64_to_cpu(lock_ele[i].Length);
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006805 if (lock_length > OFFSET_MAX - flock->fl_start)
6806 lock_length = OFFSET_MAX - flock->fl_start;
Namjae Jeone2f34482021-03-16 10:49:09 +09006807
6808 flock->fl_end = flock->fl_start + lock_length;
6809
6810 if (flock->fl_end < flock->fl_start) {
6811 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006812 "the end offset(%llx) is smaller than the start offset(%llx)\n",
6813 flock->fl_end, flock->fl_start);
Namjae Jeone2f34482021-03-16 10:49:09 +09006814 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6815 goto out;
6816 }
6817
6818 /* Check conflict locks in one request */
6819 list_for_each_entry(cmp_lock, &lock_list, llist) {
6820 if (cmp_lock->fl->fl_start <= flock->fl_start &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006821 cmp_lock->fl->fl_end >= flock->fl_end) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006822 if (cmp_lock->fl->fl_type != F_UNLCK &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006823 flock->fl_type != F_UNLCK) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006824 pr_err("conflict two locks in one request\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006825 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006826 goto out;
6827 }
6828 }
6829 }
6830
6831 smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list);
6832 if (!smb_lock) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006833 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006834 goto out;
6835 }
6836 }
6837
6838 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6839 if (smb_lock->cmd < 0) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006840 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006841 goto out;
6842 }
6843
6844 if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006845 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006846 goto out;
6847 }
6848
Namjae Jeon64b39f42021-03-30 14:25:35 +09006849 if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) &&
6850 smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) ||
6851 (prior_lock == SMB2_LOCKFLAG_UNLOCK &&
6852 !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006853 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006854 goto out;
6855 }
6856
6857 prior_lock = smb_lock->flags;
6858
6859 if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006860 !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY))
Hyunchul Leed63528e2021-07-10 16:22:41 +09006861 goto no_check_cl;
Namjae Jeone2f34482021-03-16 10:49:09 +09006862
6863 nolock = 1;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006864 /* check locks in connection list */
6865 read_lock(&conn_list_lock);
6866 list_for_each_entry(conn, &conn_list, conns_list) {
6867 spin_lock(&conn->llist_lock);
6868 list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) {
6869 if (file_inode(cmp_lock->fl->fl_file) !=
6870 file_inode(smb_lock->fl->fl_file))
6871 continue;
Namjae Jeone2f34482021-03-16 10:49:09 +09006872
Hyunchul Leed63528e2021-07-10 16:22:41 +09006873 if (smb_lock->fl->fl_type == F_UNLCK) {
6874 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file &&
6875 cmp_lock->start == smb_lock->start &&
6876 cmp_lock->end == smb_lock->end &&
6877 !lock_defer_pending(cmp_lock->fl)) {
6878 nolock = 0;
6879 list_del(&cmp_lock->flist);
6880 list_del(&cmp_lock->clist);
6881 spin_unlock(&conn->llist_lock);
6882 read_unlock(&conn_list_lock);
6883
6884 locks_free_lock(cmp_lock->fl);
6885 kfree(cmp_lock);
6886 goto out_check_cl;
6887 }
6888 continue;
Namjae Jeone2f34482021-03-16 10:49:09 +09006889 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006890
Hyunchul Leed63528e2021-07-10 16:22:41 +09006891 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) {
6892 if (smb_lock->flags & SMB2_LOCKFLAG_SHARED)
6893 continue;
6894 } else {
6895 if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED)
6896 continue;
6897 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006898
Hyunchul Leed63528e2021-07-10 16:22:41 +09006899 /* check zero byte lock range */
6900 if (cmp_lock->zero_len && !smb_lock->zero_len &&
6901 cmp_lock->start > smb_lock->start &&
6902 cmp_lock->start < smb_lock->end) {
6903 spin_unlock(&conn->llist_lock);
6904 read_unlock(&conn_list_lock);
6905 pr_err("previous lock conflict with zero byte lock range\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006906 goto out;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006907 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006908
Hyunchul Leed63528e2021-07-10 16:22:41 +09006909 if (smb_lock->zero_len && !cmp_lock->zero_len &&
6910 smb_lock->start > cmp_lock->start &&
6911 smb_lock->start < cmp_lock->end) {
6912 spin_unlock(&conn->llist_lock);
6913 read_unlock(&conn_list_lock);
6914 pr_err("current lock conflict with zero byte lock range\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006915 goto out;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006916 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006917
Hyunchul Leed63528e2021-07-10 16:22:41 +09006918 if (((cmp_lock->start <= smb_lock->start &&
6919 cmp_lock->end > smb_lock->start) ||
6920 (cmp_lock->start < smb_lock->end &&
6921 cmp_lock->end >= smb_lock->end)) &&
6922 !cmp_lock->zero_len && !smb_lock->zero_len) {
6923 spin_unlock(&conn->llist_lock);
6924 read_unlock(&conn_list_lock);
6925 pr_err("Not allow lock operation on exclusive lock range\n");
Hyunchul Leed63528e2021-07-10 16:22:41 +09006926 goto out;
6927 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006928 }
Hyunchul Leed63528e2021-07-10 16:22:41 +09006929 spin_unlock(&conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006930 }
Hyunchul Leed63528e2021-07-10 16:22:41 +09006931 read_unlock(&conn_list_lock);
6932out_check_cl:
Namjae Jeone2f34482021-03-16 10:49:09 +09006933 if (smb_lock->fl->fl_type == F_UNLCK && nolock) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006934 pr_err("Try to unlock nolocked range\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006935 rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED;
6936 goto out;
6937 }
6938
Hyunchul Leed63528e2021-07-10 16:22:41 +09006939no_check_cl:
Namjae Jeone2f34482021-03-16 10:49:09 +09006940 if (smb_lock->zero_len) {
6941 err = 0;
6942 goto skip;
6943 }
6944
6945 flock = smb_lock->fl;
6946 list_del(&smb_lock->llist);
6947retry:
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006948 rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09006949skip:
6950 if (flags & SMB2_LOCKFLAG_UNLOCK) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006951 if (!rc) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006952 ksmbd_debug(SMB, "File unlocked\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006953 } else if (rc == -ENOENT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006954 rsp->hdr.Status = STATUS_NOT_LOCKED;
6955 goto out;
6956 }
6957 locks_free_lock(flock);
6958 kfree(smb_lock);
6959 } else {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006960 if (rc == FILE_LOCK_DEFERRED) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006961 void **argv;
6962
6963 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006964 "would have to wait for getting lock\n");
Hyunchul Leed63528e2021-07-10 16:22:41 +09006965 spin_lock(&work->conn->llist_lock);
6966 list_add_tail(&smb_lock->clist,
6967 &work->conn->lock_list);
6968 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006969 list_add(&smb_lock->llist, &rollback_list);
6970
6971 argv = kmalloc(sizeof(void *), GFP_KERNEL);
6972 if (!argv) {
6973 err = -ENOMEM;
6974 goto out;
6975 }
6976 argv[0] = flock;
6977
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006978 rc = setup_async_work(work,
6979 smb2_remove_blocked_lock,
6980 argv);
6981 if (rc) {
6982 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09006983 goto out;
6984 }
6985 spin_lock(&fp->f_lock);
6986 list_add(&work->fp_entry, &fp->blocked_works);
6987 spin_unlock(&fp->f_lock);
6988
6989 smb2_send_interim_resp(work, STATUS_PENDING);
6990
Hyunchul Lee45a64e82021-07-10 09:34:20 +09006991 ksmbd_vfs_posix_lock_wait(flock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006992
Hyunchul Leed4075ab2021-06-25 07:02:10 +09006993 if (work->state != KSMBD_WORK_ACTIVE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006994 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09006995 spin_lock(&work->conn->llist_lock);
6996 list_del(&smb_lock->clist);
6997 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006998 locks_free_lock(flock);
6999
Hyunchul Leed4075ab2021-06-25 07:02:10 +09007000 if (work->state == KSMBD_WORK_CANCELLED) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007001 spin_lock(&fp->f_lock);
7002 list_del(&work->fp_entry);
7003 spin_unlock(&fp->f_lock);
7004 rsp->hdr.Status =
7005 STATUS_CANCELLED;
7006 kfree(smb_lock);
7007 smb2_send_interim_resp(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007008 STATUS_CANCELLED);
Namjae Jeone2f34482021-03-16 10:49:09 +09007009 work->send_no_response = 1;
7010 goto out;
7011 }
7012 init_smb2_rsp_hdr(work);
7013 smb2_set_err_rsp(work);
7014 rsp->hdr.Status =
7015 STATUS_RANGE_NOT_LOCKED;
7016 kfree(smb_lock);
7017 goto out2;
7018 }
7019
7020 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007021 spin_lock(&work->conn->llist_lock);
7022 list_del(&smb_lock->clist);
7023 spin_unlock(&work->conn->llist_lock);
7024
Namjae Jeone2f34482021-03-16 10:49:09 +09007025 spin_lock(&fp->f_lock);
7026 list_del(&work->fp_entry);
7027 spin_unlock(&fp->f_lock);
7028 goto retry;
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09007029 } else if (!rc) {
Hyunchul Leed63528e2021-07-10 16:22:41 +09007030 spin_lock(&work->conn->llist_lock);
7031 list_add_tail(&smb_lock->clist,
7032 &work->conn->lock_list);
7033 list_add_tail(&smb_lock->flist,
7034 &fp->lock_list);
7035 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09007036 list_add(&smb_lock->llist, &rollback_list);
7037 ksmbd_debug(SMB, "successful in taking lock\n");
7038 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007039 goto out;
7040 }
7041 }
7042 }
7043
7044 if (atomic_read(&fp->f_ci->op_count) > 1)
7045 smb_break_all_oplock(work, fp);
7046
7047 rsp->StructureSize = cpu_to_le16(4);
7048 ksmbd_debug(SMB, "successful in taking lock\n");
7049 rsp->hdr.Status = STATUS_SUCCESS;
7050 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09007051 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09007052 ksmbd_fd_put(work, fp);
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007053 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09007054
7055out:
7056 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
7057 locks_free_lock(smb_lock->fl);
7058 list_del(&smb_lock->llist);
7059 kfree(smb_lock);
7060 }
7061
7062 list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) {
7063 struct file_lock *rlock = NULL;
7064
7065 rlock = smb_flock_init(filp);
7066 rlock->fl_type = F_UNLCK;
7067 rlock->fl_start = smb_lock->start;
7068 rlock->fl_end = smb_lock->end;
7069
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007070 rc = vfs_lock_file(filp, 0, rlock, NULL);
7071 if (rc)
7072 pr_err("rollback unlock fail : %d\n", rc);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007073
Namjae Jeone2f34482021-03-16 10:49:09 +09007074 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007075 spin_lock(&work->conn->llist_lock);
7076 if (!list_empty(&smb_lock->flist))
7077 list_del(&smb_lock->flist);
7078 list_del(&smb_lock->clist);
7079 spin_unlock(&work->conn->llist_lock);
7080
Namjae Jeone2f34482021-03-16 10:49:09 +09007081 locks_free_lock(smb_lock->fl);
7082 locks_free_lock(rlock);
7083 kfree(smb_lock);
7084 }
7085out2:
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09007086 ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err);
7087
7088 if (!rsp->hdr.Status) {
7089 if (err == -EINVAL)
7090 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7091 else if (err == -ENOMEM)
7092 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
7093 else if (err == -ENOENT)
7094 rsp->hdr.Status = STATUS_FILE_CLOSED;
7095 else
7096 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
7097 }
7098
Namjae Jeone2f34482021-03-16 10:49:09 +09007099 smb2_set_err_rsp(work);
7100 ksmbd_fd_put(work, fp);
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007101 return err;
Namjae Jeone2f34482021-03-16 10:49:09 +09007102}
7103
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007104static int fsctl_copychunk(struct ksmbd_work *work,
7105 struct copychunk_ioctl_req *ci_req,
7106 unsigned int cnt_code,
7107 unsigned int input_count,
7108 unsigned long long volatile_id,
7109 unsigned long long persistent_id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007110 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007111{
Namjae Jeone2f34482021-03-16 10:49:09 +09007112 struct copychunk_ioctl_rsp *ci_rsp;
7113 struct ksmbd_file *src_fp = NULL, *dst_fp = NULL;
7114 struct srv_copychunk *chunks;
7115 unsigned int i, chunk_count, chunk_count_written = 0;
7116 unsigned int chunk_size_written = 0;
7117 loff_t total_size_written = 0;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007118 int ret = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09007119
Namjae Jeone2f34482021-03-16 10:49:09 +09007120 ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0];
7121
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007122 rsp->VolatileFileId = cpu_to_le64(volatile_id);
7123 rsp->PersistentFileId = cpu_to_le64(persistent_id);
Namjae Jeon64b39f42021-03-30 14:25:35 +09007124 ci_rsp->ChunksWritten =
7125 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count());
7126 ci_rsp->ChunkBytesWritten =
7127 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size());
7128 ci_rsp->TotalBytesWritten =
7129 cpu_to_le32(ksmbd_server_side_copy_max_total_size());
Namjae Jeone2f34482021-03-16 10:49:09 +09007130
7131 chunks = (struct srv_copychunk *)&ci_req->Chunks[0];
7132 chunk_count = le32_to_cpu(ci_req->ChunkCount);
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007133 if (chunk_count == 0)
7134 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09007135 total_size_written = 0;
7136
7137 /* verify the SRV_COPYCHUNK_COPY packet */
7138 if (chunk_count > ksmbd_server_side_copy_max_chunk_count() ||
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007139 input_count < offsetof(struct copychunk_ioctl_req, Chunks) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09007140 chunk_count * sizeof(struct srv_copychunk)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007141 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7142 return -EINVAL;
7143 }
7144
7145 for (i = 0; i < chunk_count; i++) {
7146 if (le32_to_cpu(chunks[i].Length) == 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09007147 le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size())
Namjae Jeone2f34482021-03-16 10:49:09 +09007148 break;
7149 total_size_written += le32_to_cpu(chunks[i].Length);
7150 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09007151
7152 if (i < chunk_count ||
7153 total_size_written > ksmbd_server_side_copy_max_total_size()) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007154 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7155 return -EINVAL;
7156 }
7157
7158 src_fp = ksmbd_lookup_foreign_fd(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007159 le64_to_cpu(ci_req->ResumeKey[0]));
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007160 dst_fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
Namjae Jeone2f34482021-03-16 10:49:09 +09007161 ret = -EINVAL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007162 if (!src_fp ||
7163 src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007164 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7165 goto out;
7166 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09007167
Namjae Jeone2f34482021-03-16 10:49:09 +09007168 if (!dst_fp) {
7169 rsp->hdr.Status = STATUS_FILE_CLOSED;
7170 goto out;
7171 }
7172
7173 /*
7174 * FILE_READ_DATA should only be included in
7175 * the FSCTL_COPYCHUNK case
7176 */
Namjae Jeon070fb212021-05-26 17:57:12 +09007177 if (cnt_code == FSCTL_COPYCHUNK &&
7178 !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007179 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7180 goto out;
7181 }
7182
7183 ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007184 chunks, chunk_count,
7185 &chunk_count_written,
7186 &chunk_size_written,
7187 &total_size_written);
Namjae Jeone2f34482021-03-16 10:49:09 +09007188 if (ret < 0) {
7189 if (ret == -EACCES)
7190 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7191 if (ret == -EAGAIN)
7192 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
7193 else if (ret == -EBADF)
7194 rsp->hdr.Status = STATUS_INVALID_HANDLE;
7195 else if (ret == -EFBIG || ret == -ENOSPC)
7196 rsp->hdr.Status = STATUS_DISK_FULL;
7197 else if (ret == -EINVAL)
7198 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7199 else if (ret == -EISDIR)
7200 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
7201 else if (ret == -E2BIG)
7202 rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE;
7203 else
7204 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
7205 }
7206
7207 ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written);
7208 ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written);
7209 ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written);
7210out:
7211 ksmbd_fd_put(work, src_fp);
7212 ksmbd_fd_put(work, dst_fp);
7213 return ret;
7214}
7215
7216static __be32 idev_ipv4_address(struct in_device *idev)
7217{
7218 __be32 addr = 0;
7219
7220 struct in_ifaddr *ifa;
7221
7222 rcu_read_lock();
7223 in_dev_for_each_ifa_rcu(ifa, idev) {
7224 if (ifa->ifa_flags & IFA_F_SECONDARY)
7225 continue;
7226
7227 addr = ifa->ifa_address;
7228 break;
7229 }
7230 rcu_read_unlock();
7231 return addr;
7232}
7233
7234static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007235 struct smb2_ioctl_rsp *rsp,
7236 unsigned int out_buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09007237{
7238 struct network_interface_info_ioctl_rsp *nii_rsp = NULL;
7239 int nbytes = 0;
7240 struct net_device *netdev;
7241 struct sockaddr_storage_rsp *sockaddr_storage;
7242 unsigned int flags;
7243 unsigned long long speed;
7244
7245 rtnl_lock();
7246 for_each_netdev(&init_net, netdev) {
Namjae Jeon71cd9cb2021-12-16 10:31:44 +09007247 bool ipv4_set = false;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007248
Namjae Jeone2f34482021-03-16 10:49:09 +09007249 if (netdev->type == ARPHRD_LOOPBACK)
7250 continue;
7251
7252 flags = dev_get_flags(netdev);
7253 if (!(flags & IFF_RUNNING))
7254 continue;
Namjae Jeon71cd9cb2021-12-16 10:31:44 +09007255ipv6_retry:
7256 if (out_buf_len <
7257 nbytes + sizeof(struct network_interface_info_ioctl_rsp)) {
7258 rtnl_unlock();
7259 return -ENOSPC;
7260 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007261
7262 nii_rsp = (struct network_interface_info_ioctl_rsp *)
7263 &rsp->Buffer[nbytes];
7264 nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex);
7265
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09007266 nii_rsp->Capability = 0;
Namjae Jeona58b45a2021-12-16 10:26:43 +09007267 if (netdev->real_num_tx_queues > 1)
7268 nii_rsp->Capability |= cpu_to_le32(RSS_CAPABLE);
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09007269 if (ksmbd_rdma_capable_netdev(netdev))
7270 nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE);
Namjae Jeone2f34482021-03-16 10:49:09 +09007271
7272 nii_rsp->Next = cpu_to_le32(152);
7273 nii_rsp->Reserved = 0;
7274
7275 if (netdev->ethtool_ops->get_link_ksettings) {
7276 struct ethtool_link_ksettings cmd;
7277
7278 netdev->ethtool_ops->get_link_ksettings(netdev, &cmd);
7279 speed = cmd.base.speed;
7280 } else {
Per Forlind4758662021-08-30 13:23:04 +09007281 ksmbd_debug(SMB, "%s %s\n", netdev->name,
7282 "speed is unknown, defaulting to 1Gb/sec");
Namjae Jeone2f34482021-03-16 10:49:09 +09007283 speed = SPEED_1000;
7284 }
7285
7286 speed *= 1000000;
7287 nii_rsp->LinkSpeed = cpu_to_le64(speed);
7288
7289 sockaddr_storage = (struct sockaddr_storage_rsp *)
7290 nii_rsp->SockAddr_Storage;
7291 memset(sockaddr_storage, 0, 128);
7292
Namjae Jeon71cd9cb2021-12-16 10:31:44 +09007293 if (!ipv4_set) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007294 struct in_device *idev;
7295
7296 sockaddr_storage->Family = cpu_to_le16(INTERNETWORK);
7297 sockaddr_storage->addr4.Port = 0;
7298
7299 idev = __in_dev_get_rtnl(netdev);
7300 if (!idev)
7301 continue;
7302 sockaddr_storage->addr4.IPv4address =
7303 idev_ipv4_address(idev);
Namjae Jeon71cd9cb2021-12-16 10:31:44 +09007304 nbytes += sizeof(struct network_interface_info_ioctl_rsp);
7305 ipv4_set = true;
7306 goto ipv6_retry;
Namjae Jeone2f34482021-03-16 10:49:09 +09007307 } else {
7308 struct inet6_dev *idev6;
7309 struct inet6_ifaddr *ifa;
7310 __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address;
7311
7312 sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6);
7313 sockaddr_storage->addr6.Port = 0;
7314 sockaddr_storage->addr6.FlowInfo = 0;
7315
7316 idev6 = __in6_dev_get(netdev);
7317 if (!idev6)
7318 continue;
7319
7320 list_for_each_entry(ifa, &idev6->addr_list, if_list) {
7321 if (ifa->flags & (IFA_F_TENTATIVE |
7322 IFA_F_DEPRECATED))
7323 continue;
7324 memcpy(ipv6_addr, ifa->addr.s6_addr, 16);
7325 break;
7326 }
7327 sockaddr_storage->addr6.ScopeId = 0;
Namjae Jeon71cd9cb2021-12-16 10:31:44 +09007328 nbytes += sizeof(struct network_interface_info_ioctl_rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007329 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007330 }
7331 rtnl_unlock();
7332
7333 /* zero if this is last one */
7334 if (nii_rsp)
7335 nii_rsp->Next = 0;
7336
Namjae Jeone2f34482021-03-16 10:49:09 +09007337 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7338 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7339 return nbytes;
7340}
7341
Namjae Jeone2f34482021-03-16 10:49:09 +09007342static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09007343 struct validate_negotiate_info_req *neg_req,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007344 struct validate_negotiate_info_rsp *neg_rsp,
7345 unsigned int in_buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09007346{
7347 int ret = 0;
7348 int dialect;
7349
Marios Makassikis78f16882021-10-28 21:01:27 +02007350 if (in_buf_len < offsetof(struct validate_negotiate_info_req, Dialects) +
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007351 le16_to_cpu(neg_req->DialectCount) * sizeof(__le16))
7352 return -EINVAL;
7353
Namjae Jeone2f34482021-03-16 10:49:09 +09007354 dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects,
Namjae Jeon070fb212021-05-26 17:57:12 +09007355 neg_req->DialectCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09007356 if (dialect == BAD_PROT_ID || dialect != conn->dialect) {
7357 ret = -EINVAL;
7358 goto err_out;
7359 }
7360
7361 if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) {
7362 ret = -EINVAL;
7363 goto err_out;
7364 }
7365
7366 if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) {
7367 ret = -EINVAL;
7368 goto err_out;
7369 }
7370
7371 if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) {
7372 ret = -EINVAL;
7373 goto err_out;
7374 }
7375
7376 neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
7377 memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE);
7378 neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode);
7379 neg_rsp->Dialect = cpu_to_le16(conn->dialect);
7380err_out:
7381 return ret;
7382}
7383
Namjae Jeon64b39f42021-03-30 14:25:35 +09007384static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007385 struct file_allocated_range_buffer *qar_req,
7386 struct file_allocated_range_buffer *qar_rsp,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007387 unsigned int in_count, unsigned int *out_count)
Namjae Jeone2f34482021-03-16 10:49:09 +09007388{
7389 struct ksmbd_file *fp;
7390 loff_t start, length;
7391 int ret = 0;
7392
7393 *out_count = 0;
7394 if (in_count == 0)
7395 return -EINVAL;
7396
7397 fp = ksmbd_lookup_fd_fast(work, id);
7398 if (!fp)
7399 return -ENOENT;
7400
7401 start = le64_to_cpu(qar_req->file_offset);
7402 length = le64_to_cpu(qar_req->length);
7403
7404 ret = ksmbd_vfs_fqar_lseek(fp, start, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09007405 qar_rsp, in_count, out_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09007406 if (ret && ret != -E2BIG)
7407 *out_count = 0;
7408
7409 ksmbd_fd_put(work, fp);
7410 return ret;
7411}
7412
Namjae Jeon64b39f42021-03-30 14:25:35 +09007413static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007414 unsigned int out_buf_len,
7415 struct smb2_ioctl_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007416 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007417{
7418 struct ksmbd_rpc_command *rpc_resp;
7419 char *data_buf = (char *)&req->Buffer[0];
7420 int nbytes = 0;
7421
Namjae Jeon64b39f42021-03-30 14:25:35 +09007422 rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09007423 le32_to_cpu(req->InputCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09007424 if (rpc_resp) {
7425 if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) {
7426 /*
7427 * set STATUS_SOME_NOT_MAPPED response
7428 * for unknown domain sid.
7429 */
7430 rsp->hdr.Status = STATUS_SOME_NOT_MAPPED;
7431 } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
7432 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7433 goto out;
7434 } else if (rpc_resp->flags != KSMBD_RPC_OK) {
7435 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7436 goto out;
7437 }
7438
7439 nbytes = rpc_resp->payload_sz;
7440 if (rpc_resp->payload_sz > out_buf_len) {
7441 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7442 nbytes = out_buf_len;
7443 }
7444
7445 if (!rpc_resp->payload_sz) {
7446 rsp->hdr.Status =
7447 STATUS_UNEXPECTED_IO_ERROR;
7448 goto out;
7449 }
7450
7451 memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes);
7452 }
7453out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09007454 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007455 return nbytes;
7456}
7457
Namjae Jeon64b39f42021-03-30 14:25:35 +09007458static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007459 struct file_sparse *sparse)
Namjae Jeone2f34482021-03-16 10:49:09 +09007460{
7461 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007462 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09007463 int ret = 0;
7464 __le32 old_fattr;
7465
7466 fp = ksmbd_lookup_fd_fast(work, id);
7467 if (!fp)
7468 return -ENOENT;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007469 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007470
7471 old_fattr = fp->f_ci->m_fattr;
7472 if (sparse->SetSparse)
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09007473 fp->f_ci->m_fattr |= FILE_ATTRIBUTE_SPARSE_FILE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09007474 else
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09007475 fp->f_ci->m_fattr &= ~FILE_ATTRIBUTE_SPARSE_FILE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09007476
7477 if (fp->f_ci->m_fattr != old_fattr &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007478 test_share_config_flag(work->tcon->share_conf,
7479 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007480 struct xattr_dos_attrib da;
7481
Hyunchul Lee465d7202021-07-03 12:10:36 +09007482 ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007483 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007484 if (ret <= 0)
7485 goto out;
7486
7487 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
Hyunchul Lee465d7202021-07-03 12:10:36 +09007488 ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007489 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007490 if (ret)
7491 fp->f_ci->m_fattr = old_fattr;
7492 }
7493
7494out:
7495 ksmbd_fd_put(work, fp);
7496 return ret;
7497}
7498
7499static int fsctl_request_resume_key(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007500 struct smb2_ioctl_req *req,
7501 struct resume_key_ioctl_rsp *key_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007502{
7503 struct ksmbd_file *fp;
7504
7505 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007506 le64_to_cpu(req->VolatileFileId),
7507 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09007508 if (!fp)
7509 return -ENOENT;
7510
7511 memset(key_rsp, 0, sizeof(*key_rsp));
7512 key_rsp->ResumeKey[0] = req->VolatileFileId;
7513 key_rsp->ResumeKey[1] = req->PersistentFileId;
7514 ksmbd_fd_put(work, fp);
7515
7516 return 0;
7517}
7518
7519/**
7520 * smb2_ioctl() - handler for smb2 ioctl command
7521 * @work: smb work containing ioctl command buffer
7522 *
7523 * Return: 0 on success, otherwise error
7524 */
7525int smb2_ioctl(struct ksmbd_work *work)
7526{
7527 struct smb2_ioctl_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09007528 struct smb2_ioctl_rsp *rsp;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007529 unsigned int cnt_code, nbytes = 0, out_buf_len, in_buf_len;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007530 u64 id = KSMBD_NO_FID;
Namjae Jeone2f34482021-03-16 10:49:09 +09007531 struct ksmbd_conn *conn = work->conn;
7532 int ret = 0;
7533
Namjae Jeone2f34482021-03-16 10:49:09 +09007534 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09007535 req = ksmbd_req_buf_next(work);
7536 rsp = ksmbd_resp_buf_next(work);
Namjae Jeon38673692021-07-08 12:32:27 +09007537 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
7538 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007539 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09007540 id = work->compound_fid;
7541 }
7542 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09007543 req = smb2_get_msg(work->request_buf);
7544 rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09007545 }
7546
Namjae Jeon38673692021-07-08 12:32:27 +09007547 if (!has_file_id(id))
Namjae Jeone2f34482021-03-16 10:49:09 +09007548 id = le64_to_cpu(req->VolatileFileId);
7549
7550 if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) {
7551 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7552 goto out;
7553 }
7554
7555 cnt_code = le32_to_cpu(req->CntCode);
Hyunchul Lee34061d62021-10-16 08:39:54 +09007556 ret = smb2_calc_max_out_buf_len(work, 48,
7557 le32_to_cpu(req->MaxOutputResponse));
7558 if (ret < 0) {
7559 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7560 goto out;
7561 }
7562 out_buf_len = (unsigned int)ret;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007563 in_buf_len = le32_to_cpu(req->InputCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09007564
7565 switch (cnt_code) {
7566 case FSCTL_DFS_GET_REFERRALS:
7567 case FSCTL_DFS_GET_REFERRALS_EX:
7568 /* Not support DFS yet */
7569 rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED;
7570 goto out;
7571 case FSCTL_CREATE_OR_GET_OBJECT_ID:
7572 {
7573 struct file_object_buf_type1_ioctl_rsp *obj_buf;
7574
7575 nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp);
7576 obj_buf = (struct file_object_buf_type1_ioctl_rsp *)
7577 &rsp->Buffer[0];
7578
7579 /*
7580 * TODO: This is dummy implementation to pass smbtorture
7581 * Need to check correct response later
7582 */
7583 memset(obj_buf->ObjectId, 0x0, 16);
7584 memset(obj_buf->BirthVolumeId, 0x0, 16);
7585 memset(obj_buf->BirthObjectId, 0x0, 16);
7586 memset(obj_buf->DomainId, 0x0, 16);
7587
7588 break;
7589 }
7590 case FSCTL_PIPE_TRANSCEIVE:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007591 out_buf_len = min_t(u32, KSMBD_IPC_MAX_PAYLOAD, out_buf_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09007592 nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp);
7593 break;
7594 case FSCTL_VALIDATE_NEGOTIATE_INFO:
7595 if (conn->dialect < SMB30_PROT_ID) {
7596 ret = -EOPNOTSUPP;
7597 goto out;
7598 }
7599
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007600 if (in_buf_len < sizeof(struct validate_negotiate_info_req))
7601 return -EINVAL;
7602
7603 if (out_buf_len < sizeof(struct validate_negotiate_info_rsp))
7604 return -EINVAL;
7605
Namjae Jeone2f34482021-03-16 10:49:09 +09007606 ret = fsctl_validate_negotiate_info(conn,
7607 (struct validate_negotiate_info_req *)&req->Buffer[0],
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007608 (struct validate_negotiate_info_rsp *)&rsp->Buffer[0],
7609 in_buf_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09007610 if (ret < 0)
7611 goto out;
7612
7613 nbytes = sizeof(struct validate_negotiate_info_rsp);
7614 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7615 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7616 break;
7617 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007618 ret = fsctl_query_iface_info_ioctl(conn, rsp, out_buf_len);
7619 if (ret < 0)
Namjae Jeone2f34482021-03-16 10:49:09 +09007620 goto out;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007621 nbytes = ret;
Namjae Jeone2f34482021-03-16 10:49:09 +09007622 break;
7623 case FSCTL_REQUEST_RESUME_KEY:
7624 if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) {
7625 ret = -EINVAL;
7626 goto out;
7627 }
7628
7629 ret = fsctl_request_resume_key(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007630 (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007631 if (ret < 0)
7632 goto out;
7633 rsp->PersistentFileId = req->PersistentFileId;
7634 rsp->VolatileFileId = req->VolatileFileId;
7635 nbytes = sizeof(struct resume_key_ioctl_rsp);
7636 break;
7637 case FSCTL_COPYCHUNK:
7638 case FSCTL_COPYCHUNK_WRITE:
Namjae Jeon64b39f42021-03-30 14:25:35 +09007639 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007640 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007641 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007642 ret = -EACCES;
7643 goto out;
7644 }
7645
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007646 if (in_buf_len < sizeof(struct copychunk_ioctl_req)) {
7647 ret = -EINVAL;
7648 goto out;
7649 }
7650
Namjae Jeone2f34482021-03-16 10:49:09 +09007651 if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) {
7652 ret = -EINVAL;
7653 goto out;
7654 }
7655
7656 nbytes = sizeof(struct copychunk_ioctl_rsp);
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007657 rsp->VolatileFileId = req->VolatileFileId;
7658 rsp->PersistentFileId = req->PersistentFileId;
7659 fsctl_copychunk(work,
7660 (struct copychunk_ioctl_req *)&req->Buffer[0],
7661 le32_to_cpu(req->CntCode),
7662 le32_to_cpu(req->InputCount),
7663 le64_to_cpu(req->VolatileFileId),
7664 le64_to_cpu(req->PersistentFileId),
7665 rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007666 break;
7667 case FSCTL_SET_SPARSE:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007668 if (in_buf_len < sizeof(struct file_sparse)) {
7669 ret = -EINVAL;
7670 goto out;
7671 }
7672
Namjae Jeone2f34482021-03-16 10:49:09 +09007673 ret = fsctl_set_sparse(work, id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007674 (struct file_sparse *)&req->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007675 if (ret < 0)
7676 goto out;
7677 break;
7678 case FSCTL_SET_ZERO_DATA:
7679 {
7680 struct file_zero_data_information *zero_data;
7681 struct ksmbd_file *fp;
7682 loff_t off, len;
7683
Namjae Jeon64b39f42021-03-30 14:25:35 +09007684 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007685 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007686 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007687 ret = -EACCES;
7688 goto out;
7689 }
7690
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007691 if (in_buf_len < sizeof(struct file_zero_data_information)) {
7692 ret = -EINVAL;
7693 goto out;
7694 }
7695
Namjae Jeone2f34482021-03-16 10:49:09 +09007696 zero_data =
7697 (struct file_zero_data_information *)&req->Buffer[0];
7698
7699 fp = ksmbd_lookup_fd_fast(work, id);
7700 if (!fp) {
7701 ret = -ENOENT;
7702 goto out;
7703 }
7704
7705 off = le64_to_cpu(zero_data->FileOffset);
7706 len = le64_to_cpu(zero_data->BeyondFinalZero) - off;
7707
7708 ret = ksmbd_vfs_zero_data(work, fp, off, len);
7709 ksmbd_fd_put(work, fp);
7710 if (ret < 0)
7711 goto out;
7712 break;
7713 }
7714 case FSCTL_QUERY_ALLOCATED_RANGES:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007715 if (in_buf_len < sizeof(struct file_allocated_range_buffer)) {
7716 ret = -EINVAL;
7717 goto out;
7718 }
7719
Namjae Jeone2f34482021-03-16 10:49:09 +09007720 ret = fsctl_query_allocated_ranges(work, id,
7721 (struct file_allocated_range_buffer *)&req->Buffer[0],
7722 (struct file_allocated_range_buffer *)&rsp->Buffer[0],
7723 out_buf_len /
7724 sizeof(struct file_allocated_range_buffer), &nbytes);
7725 if (ret == -E2BIG) {
7726 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7727 } else if (ret < 0) {
7728 nbytes = 0;
7729 goto out;
7730 }
7731
7732 nbytes *= sizeof(struct file_allocated_range_buffer);
7733 break;
7734 case FSCTL_GET_REPARSE_POINT:
7735 {
7736 struct reparse_data_buffer *reparse_ptr;
7737 struct ksmbd_file *fp;
7738
7739 reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0];
7740 fp = ksmbd_lookup_fd_fast(work, id);
7741 if (!fp) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007742 pr_err("not found fp!!\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007743 ret = -ENOENT;
7744 goto out;
7745 }
7746
7747 reparse_ptr->ReparseTag =
Namjae Jeonab0b2632021-06-29 09:20:13 +09007748 smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode);
Namjae Jeone2f34482021-03-16 10:49:09 +09007749 reparse_ptr->ReparseDataLength = 0;
7750 ksmbd_fd_put(work, fp);
7751 nbytes = sizeof(struct reparse_data_buffer);
7752 break;
7753 }
Namjae Jeoneb817362021-05-18 10:37:59 +09007754 case FSCTL_DUPLICATE_EXTENTS_TO_FILE:
7755 {
7756 struct ksmbd_file *fp_in, *fp_out = NULL;
7757 struct duplicate_extents_to_file *dup_ext;
7758 loff_t src_off, dst_off, length, cloned;
7759
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007760 if (in_buf_len < sizeof(struct duplicate_extents_to_file)) {
7761 ret = -EINVAL;
7762 goto out;
7763 }
7764
Namjae Jeoneb817362021-05-18 10:37:59 +09007765 dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0];
7766
7767 fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle,
Namjae Jeon070fb212021-05-26 17:57:12 +09007768 dup_ext->PersistentFileHandle);
Namjae Jeoneb817362021-05-18 10:37:59 +09007769 if (!fp_in) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007770 pr_err("not found file handle in duplicate extent to file\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007771 ret = -ENOENT;
7772 goto out;
7773 }
7774
7775 fp_out = ksmbd_lookup_fd_fast(work, id);
7776 if (!fp_out) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007777 pr_err("not found fp\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007778 ret = -ENOENT;
7779 goto dup_ext_out;
7780 }
7781
7782 src_off = le64_to_cpu(dup_ext->SourceFileOffset);
7783 dst_off = le64_to_cpu(dup_ext->TargetFileOffset);
7784 length = le64_to_cpu(dup_ext->ByteCount);
7785 cloned = vfs_clone_file_range(fp_in->filp, src_off, fp_out->filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007786 dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007787 if (cloned == -EXDEV || cloned == -EOPNOTSUPP) {
7788 ret = -EOPNOTSUPP;
7789 goto dup_ext_out;
7790 } else if (cloned != length) {
Namjae Jeonf8524772021-06-18 10:28:00 +09007791 cloned = vfs_copy_file_range(fp_in->filp, src_off,
7792 fp_out->filp, dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007793 if (cloned != length) {
7794 if (cloned < 0)
7795 ret = cloned;
7796 else
7797 ret = -EINVAL;
7798 }
7799 }
7800
7801dup_ext_out:
7802 ksmbd_fd_put(work, fp_in);
7803 ksmbd_fd_put(work, fp_out);
7804 if (ret < 0)
7805 goto out;
7806 break;
7807 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007808 default:
7809 ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007810 cnt_code);
Namjae Jeone2f34482021-03-16 10:49:09 +09007811 ret = -EOPNOTSUPP;
7812 goto out;
7813 }
7814
7815 rsp->CntCode = cpu_to_le32(cnt_code);
7816 rsp->InputCount = cpu_to_le32(0);
7817 rsp->InputOffset = cpu_to_le32(112);
7818 rsp->OutputOffset = cpu_to_le32(112);
7819 rsp->OutputCount = cpu_to_le32(nbytes);
7820 rsp->StructureSize = cpu_to_le16(49);
7821 rsp->Reserved = cpu_to_le16(0);
7822 rsp->Flags = cpu_to_le32(0);
7823 rsp->Reserved2 = cpu_to_le32(0);
Namjae Jeoncb451722021-11-03 08:08:44 +09007824 inc_rfc1001_len(work->response_buf, 48 + nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09007825
7826 return 0;
7827
7828out:
7829 if (ret == -EACCES)
7830 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7831 else if (ret == -ENOENT)
7832 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7833 else if (ret == -EOPNOTSUPP)
7834 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007835 else if (ret == -ENOSPC)
7836 rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL;
Namjae Jeone2f34482021-03-16 10:49:09 +09007837 else if (ret < 0 || rsp->hdr.Status == 0)
7838 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7839 smb2_set_err_rsp(work);
7840 return 0;
7841}
7842
7843/**
7844 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command
7845 * @work: smb work containing oplock break command buffer
7846 *
7847 * Return: 0
7848 */
7849static void smb20_oplock_break_ack(struct ksmbd_work *work)
7850{
Namjae Jeoncb451722021-11-03 08:08:44 +09007851 struct smb2_oplock_break *req = smb2_get_msg(work->request_buf);
7852 struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09007853 struct ksmbd_file *fp;
7854 struct oplock_info *opinfo = NULL;
7855 __le32 err = 0;
7856 int ret = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007857 u64 volatile_id, persistent_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09007858 char req_oplevel = 0, rsp_oplevel = 0;
7859 unsigned int oplock_change_type;
7860
7861 volatile_id = le64_to_cpu(req->VolatileFid);
7862 persistent_id = le64_to_cpu(req->PersistentFid);
7863 req_oplevel = req->OplockLevel;
7864 ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n",
7865 volatile_id, persistent_id, req_oplevel);
7866
7867 fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
7868 if (!fp) {
7869 rsp->hdr.Status = STATUS_FILE_CLOSED;
7870 smb2_set_err_rsp(work);
7871 return;
7872 }
7873
7874 opinfo = opinfo_get(fp);
7875 if (!opinfo) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007876 pr_err("unexpected null oplock_info\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007877 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7878 smb2_set_err_rsp(work);
7879 ksmbd_fd_put(work, fp);
7880 return;
7881 }
7882
7883 if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) {
7884 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7885 goto err_out;
7886 }
7887
7888 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7889 ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state);
7890 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7891 goto err_out;
7892 }
7893
Namjae Jeon64b39f42021-03-30 14:25:35 +09007894 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7895 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7896 (req_oplevel != SMB2_OPLOCK_LEVEL_II &&
7897 req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007898 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7899 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007900 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7901 req_oplevel != SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007902 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7903 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007904 } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II ||
7905 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007906 err = STATUS_INVALID_DEVICE_STATE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007907 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7908 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7909 req_oplevel == SMB2_OPLOCK_LEVEL_II) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007910 oplock_change_type = OPLOCK_WRITE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007911 } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7912 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7913 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007914 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007915 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7916 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007917 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007918 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007919 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007920 }
7921 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007922 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007923 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007924
7925 switch (oplock_change_type) {
7926 case OPLOCK_WRITE_TO_READ:
7927 ret = opinfo_write_to_read(opinfo);
7928 rsp_oplevel = SMB2_OPLOCK_LEVEL_II;
7929 break;
7930 case OPLOCK_WRITE_TO_NONE:
7931 ret = opinfo_write_to_none(opinfo);
7932 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7933 break;
7934 case OPLOCK_READ_TO_NONE:
7935 ret = opinfo_read_to_none(opinfo);
7936 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7937 break;
7938 default:
Namjae Jeonbde16942021-06-28 15:23:19 +09007939 pr_err("unknown oplock change 0x%x -> 0x%x\n",
7940 opinfo->level, rsp_oplevel);
Namjae Jeone2f34482021-03-16 10:49:09 +09007941 }
7942
7943 if (ret < 0) {
7944 rsp->hdr.Status = err;
7945 goto err_out;
7946 }
7947
7948 opinfo_put(opinfo);
7949 ksmbd_fd_put(work, fp);
7950 opinfo->op_state = OPLOCK_STATE_NONE;
7951 wake_up_interruptible_all(&opinfo->oplock_q);
7952
7953 rsp->StructureSize = cpu_to_le16(24);
7954 rsp->OplockLevel = rsp_oplevel;
7955 rsp->Reserved = 0;
7956 rsp->Reserved2 = 0;
7957 rsp->VolatileFid = cpu_to_le64(volatile_id);
7958 rsp->PersistentFid = cpu_to_le64(persistent_id);
Namjae Jeoncb451722021-11-03 08:08:44 +09007959 inc_rfc1001_len(work->response_buf, 24);
Namjae Jeone2f34482021-03-16 10:49:09 +09007960 return;
7961
7962err_out:
7963 opinfo->op_state = OPLOCK_STATE_NONE;
7964 wake_up_interruptible_all(&opinfo->oplock_q);
7965
7966 opinfo_put(opinfo);
7967 ksmbd_fd_put(work, fp);
7968 smb2_set_err_rsp(work);
7969}
7970
7971static int check_lease_state(struct lease *lease, __le32 req_state)
7972{
7973 if ((lease->new_state ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09007974 (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) &&
7975 !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007976 lease->new_state = req_state;
7977 return 0;
7978 }
7979
7980 if (lease->new_state == req_state)
7981 return 0;
7982
7983 return 1;
7984}
7985
7986/**
7987 * smb21_lease_break_ack() - handler for smb2.1 lease break command
7988 * @work: smb work containing lease break command buffer
7989 *
7990 * Return: 0
7991 */
7992static void smb21_lease_break_ack(struct ksmbd_work *work)
7993{
7994 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09007995 struct smb2_lease_ack *req = smb2_get_msg(work->request_buf);
7996 struct smb2_lease_ack *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09007997 struct oplock_info *opinfo;
7998 __le32 err = 0;
7999 int ret = 0;
8000 unsigned int lease_change_type;
8001 __le32 lease_state;
8002 struct lease *lease;
8003
8004 ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008005 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09008006 opinfo = lookup_lease_in_table(conn, req->LeaseKey);
8007 if (!opinfo) {
8008 ksmbd_debug(OPLOCK, "file not opened\n");
8009 smb2_set_err_rsp(work);
8010 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
8011 return;
8012 }
8013 lease = opinfo->o_lease;
8014
8015 if (opinfo->op_state == OPLOCK_STATE_NONE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008016 pr_err("unexpected lease break state 0x%x\n",
8017 opinfo->op_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09008018 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
8019 goto err_out;
8020 }
8021
8022 if (check_lease_state(lease, req->LeaseState)) {
8023 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
8024 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +09008025 "req lease state: 0x%x, expected state: 0x%x\n",
8026 req->LeaseState, lease->new_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09008027 goto err_out;
8028 }
8029
8030 if (!atomic_read(&opinfo->breaking_cnt)) {
8031 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
8032 goto err_out;
8033 }
8034
8035 /* check for bad lease state */
Namjae Jeon070fb212021-05-26 17:57:12 +09008036 if (req->LeaseState &
8037 (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008038 err = STATUS_INVALID_OPLOCK_PROTOCOL;
8039 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8040 lease_change_type = OPLOCK_WRITE_TO_NONE;
8041 else
8042 lease_change_type = OPLOCK_READ_TO_NONE;
8043 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008044 le32_to_cpu(lease->state),
8045 le32_to_cpu(req->LeaseState));
Namjae Jeon64b39f42021-03-30 14:25:35 +09008046 } else if (lease->state == SMB2_LEASE_READ_CACHING_LE &&
8047 req->LeaseState != SMB2_LEASE_NONE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008048 err = STATUS_INVALID_OPLOCK_PROTOCOL;
8049 lease_change_type = OPLOCK_READ_TO_NONE;
8050 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008051 le32_to_cpu(lease->state),
8052 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09008053 } else {
8054 /* valid lease state changes */
8055 err = STATUS_INVALID_DEVICE_STATE;
8056 if (req->LeaseState == SMB2_LEASE_NONE_LE) {
8057 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8058 lease_change_type = OPLOCK_WRITE_TO_NONE;
8059 else
8060 lease_change_type = OPLOCK_READ_TO_NONE;
8061 } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) {
8062 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8063 lease_change_type = OPLOCK_WRITE_TO_READ;
8064 else
8065 lease_change_type = OPLOCK_READ_HANDLE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09008066 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09008067 lease_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09008068 }
Namjae Jeone2f34482021-03-16 10:49:09 +09008069 }
8070
8071 switch (lease_change_type) {
8072 case OPLOCK_WRITE_TO_READ:
8073 ret = opinfo_write_to_read(opinfo);
8074 break;
8075 case OPLOCK_READ_HANDLE_TO_READ:
8076 ret = opinfo_read_handle_to_read(opinfo);
8077 break;
8078 case OPLOCK_WRITE_TO_NONE:
8079 ret = opinfo_write_to_none(opinfo);
8080 break;
8081 case OPLOCK_READ_TO_NONE:
8082 ret = opinfo_read_to_none(opinfo);
8083 break;
8084 default:
8085 ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008086 le32_to_cpu(lease->state),
8087 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09008088 }
8089
8090 lease_state = lease->state;
8091 opinfo->op_state = OPLOCK_STATE_NONE;
8092 wake_up_interruptible_all(&opinfo->oplock_q);
8093 atomic_dec(&opinfo->breaking_cnt);
8094 wake_up_interruptible_all(&opinfo->oplock_brk);
8095 opinfo_put(opinfo);
8096
8097 if (ret < 0) {
8098 rsp->hdr.Status = err;
8099 goto err_out;
8100 }
8101
8102 rsp->StructureSize = cpu_to_le16(36);
8103 rsp->Reserved = 0;
8104 rsp->Flags = 0;
8105 memcpy(rsp->LeaseKey, req->LeaseKey, 16);
8106 rsp->LeaseState = lease_state;
8107 rsp->LeaseDuration = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09008108 inc_rfc1001_len(work->response_buf, 36);
Namjae Jeone2f34482021-03-16 10:49:09 +09008109 return;
8110
8111err_out:
8112 opinfo->op_state = OPLOCK_STATE_NONE;
8113 wake_up_interruptible_all(&opinfo->oplock_q);
8114 atomic_dec(&opinfo->breaking_cnt);
8115 wake_up_interruptible_all(&opinfo->oplock_brk);
8116
8117 opinfo_put(opinfo);
8118 smb2_set_err_rsp(work);
8119}
8120
8121/**
8122 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break
8123 * @work: smb work containing oplock/lease break command buffer
8124 *
8125 * Return: 0
8126 */
8127int smb2_oplock_break(struct ksmbd_work *work)
8128{
Namjae Jeoncb451722021-11-03 08:08:44 +09008129 struct smb2_oplock_break *req = smb2_get_msg(work->request_buf);
8130 struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008131
8132 switch (le16_to_cpu(req->StructureSize)) {
8133 case OP_BREAK_STRUCT_SIZE_20:
8134 smb20_oplock_break_ack(work);
8135 break;
8136 case OP_BREAK_STRUCT_SIZE_21:
8137 smb21_lease_break_ack(work);
8138 break;
8139 default:
8140 ksmbd_debug(OPLOCK, "invalid break cmd %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008141 le16_to_cpu(req->StructureSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09008142 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
8143 smb2_set_err_rsp(work);
8144 }
8145
8146 return 0;
8147}
8148
8149/**
8150 * smb2_notify() - handler for smb2 notify request
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09008151 * @work: smb work containing notify command buffer
Namjae Jeone2f34482021-03-16 10:49:09 +09008152 *
8153 * Return: 0
8154 */
8155int smb2_notify(struct ksmbd_work *work)
8156{
Ronnie Sahlberg699230f2021-09-09 12:26:12 +09008157 struct smb2_change_notify_req *req;
8158 struct smb2_change_notify_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09008159
8160 WORK_BUFFERS(work, req, rsp);
8161
8162 if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) {
8163 rsp->hdr.Status = STATUS_INTERNAL_ERROR;
8164 smb2_set_err_rsp(work);
8165 return 0;
8166 }
8167
8168 smb2_set_err_rsp(work);
8169 rsp->hdr.Status = STATUS_NOT_IMPLEMENTED;
8170 return 0;
8171}
8172
8173/**
8174 * smb2_is_sign_req() - handler for checking packet signing status
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09008175 * @work: smb work containing notify command buffer
8176 * @command: SMB2 command id
Namjae Jeone2f34482021-03-16 10:49:09 +09008177 *
8178 * Return: true if packed is signed, false otherwise
8179 */
8180bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command)
8181{
Namjae Jeoncb451722021-11-03 08:08:44 +09008182 struct smb2_hdr *rcv_hdr2 = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008183
8184 if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008185 command != SMB2_NEGOTIATE_HE &&
8186 command != SMB2_SESSION_SETUP_HE &&
8187 command != SMB2_OPLOCK_BREAK_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +09008188 return true;
8189
kernel test robot56160152021-05-12 09:24:37 +09008190 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +09008191}
8192
8193/**
8194 * smb2_check_sign_req() - handler for req packet sign processing
8195 * @work: smb work containing notify command buffer
8196 *
8197 * Return: 1 on success, 0 otherwise
8198 */
8199int smb2_check_sign_req(struct ksmbd_work *work)
8200{
Namjae Jeoncb451722021-11-03 08:08:44 +09008201 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008202 char signature_req[SMB2_SIGNATURE_SIZE];
8203 char signature[SMB2_HMACSHA256_SIZE];
8204 struct kvec iov[1];
8205 size_t len;
8206
Namjae Jeoncb451722021-11-03 08:08:44 +09008207 hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008208 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008209 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008210
8211 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
Namjae Jeoncb451722021-11-03 08:08:44 +09008212 len = get_rfc1002_len(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008213 else if (hdr->NextCommand)
8214 len = le32_to_cpu(hdr->NextCommand);
8215 else
Namjae Jeoncb451722021-11-03 08:08:44 +09008216 len = get_rfc1002_len(work->request_buf) -
Namjae Jeone2f34482021-03-16 10:49:09 +09008217 work->next_smb2_rcv_hdr_off;
8218
8219 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8220 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8221
8222 iov[0].iov_base = (char *)&hdr->ProtocolId;
8223 iov[0].iov_len = len;
8224
8225 if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1,
Namjae Jeon64b39f42021-03-30 14:25:35 +09008226 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09008227 return 0;
8228
8229 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008230 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008231 return 0;
8232 }
8233
8234 return 1;
8235}
8236
8237/**
8238 * smb2_set_sign_rsp() - handler for rsp packet sign processing
8239 * @work: smb work containing notify command buffer
8240 *
8241 */
8242void smb2_set_sign_rsp(struct ksmbd_work *work)
8243{
Namjae Jeoncb451722021-11-03 08:08:44 +09008244 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008245 struct smb2_hdr *req_hdr;
8246 char signature[SMB2_HMACSHA256_SIZE];
8247 struct kvec iov[2];
8248 size_t len;
8249 int n_vec = 1;
8250
Namjae Jeoncb451722021-11-03 08:08:44 +09008251 hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008252 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008253 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008254
Namjae Jeon8a893312021-06-25 13:43:37 +09008255 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008256
8257 if (!work->next_smb2_rsp_hdr_off) {
Namjae Jeoncb451722021-11-03 08:08:44 +09008258 len = get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008259 if (req_hdr->NextCommand)
8260 len = ALIGN(len, 8);
8261 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09008262 len = get_rfc1002_len(work->response_buf) -
8263 work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +09008264 len = ALIGN(len, 8);
8265 }
8266
8267 if (req_hdr->NextCommand)
8268 hdr->NextCommand = cpu_to_le32(len);
8269
8270 hdr->Flags |= SMB2_FLAGS_SIGNED;
8271 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8272
8273 iov[0].iov_base = (char *)&hdr->ProtocolId;
8274 iov[0].iov_len = len;
8275
Namjae Jeone5066492021-03-30 12:35:23 +09008276 if (work->aux_payload_sz) {
8277 iov[0].iov_len -= work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008278
Namjae Jeone5066492021-03-30 12:35:23 +09008279 iov[1].iov_base = work->aux_payload_buf;
8280 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008281 n_vec++;
8282 }
8283
8284 if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec,
Namjae Jeon64b39f42021-03-30 14:25:35 +09008285 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09008286 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8287}
8288
8289/**
8290 * smb3_check_sign_req() - handler for req packet sign processing
8291 * @work: smb work containing notify command buffer
8292 *
8293 * Return: 1 on success, 0 otherwise
8294 */
8295int smb3_check_sign_req(struct ksmbd_work *work)
8296{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008297 struct ksmbd_conn *conn = work->conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09008298 char *signing_key;
Namjae Jeoncb451722021-11-03 08:08:44 +09008299 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008300 struct channel *chann;
8301 char signature_req[SMB2_SIGNATURE_SIZE];
8302 char signature[SMB2_CMACAES_SIZE];
8303 struct kvec iov[1];
8304 size_t len;
8305
Namjae Jeoncb451722021-11-03 08:08:44 +09008306 hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008307 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008308 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008309
8310 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
Namjae Jeoncb451722021-11-03 08:08:44 +09008311 len = get_rfc1002_len(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008312 else if (hdr->NextCommand)
8313 len = le32_to_cpu(hdr->NextCommand);
8314 else
Namjae Jeoncb451722021-11-03 08:08:44 +09008315 len = get_rfc1002_len(work->request_buf) -
Namjae Jeone2f34482021-03-16 10:49:09 +09008316 work->next_smb2_rcv_hdr_off;
8317
8318 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
8319 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008320 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008321 chann = lookup_chann_list(work->sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008322 if (!chann)
8323 return 0;
8324 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008325 }
8326
8327 if (!signing_key) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008328 pr_err("SMB3 signing key is not generated\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008329 return 0;
8330 }
8331
8332 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8333 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8334 iov[0].iov_base = (char *)&hdr->ProtocolId;
8335 iov[0].iov_len = len;
8336
8337 if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature))
8338 return 0;
8339
8340 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008341 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008342 return 0;
8343 }
8344
8345 return 1;
8346}
8347
8348/**
8349 * smb3_set_sign_rsp() - handler for rsp packet sign processing
8350 * @work: smb work containing notify command buffer
8351 *
8352 */
8353void smb3_set_sign_rsp(struct ksmbd_work *work)
8354{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008355 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09008356 struct smb2_hdr *req_hdr, *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008357 struct channel *chann;
8358 char signature[SMB2_CMACAES_SIZE];
8359 struct kvec iov[2];
8360 int n_vec = 1;
8361 size_t len;
8362 char *signing_key;
8363
Namjae Jeoncb451722021-11-03 08:08:44 +09008364 hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008365 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008366 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008367
Namjae Jeon8a893312021-06-25 13:43:37 +09008368 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008369
8370 if (!work->next_smb2_rsp_hdr_off) {
Namjae Jeoncb451722021-11-03 08:08:44 +09008371 len = get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008372 if (req_hdr->NextCommand)
8373 len = ALIGN(len, 8);
8374 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09008375 len = get_rfc1002_len(work->response_buf) -
8376 work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +09008377 len = ALIGN(len, 8);
8378 }
8379
Namjae Jeon08bdbc62021-07-27 09:30:29 +09008380 if (conn->binding == false &&
8381 le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008382 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008383 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008384 chann = lookup_chann_list(work->sess, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008385 if (!chann)
8386 return;
8387 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008388 }
8389
8390 if (!signing_key)
8391 return;
8392
8393 if (req_hdr->NextCommand)
8394 hdr->NextCommand = cpu_to_le32(len);
8395
8396 hdr->Flags |= SMB2_FLAGS_SIGNED;
8397 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8398 iov[0].iov_base = (char *)&hdr->ProtocolId;
8399 iov[0].iov_len = len;
Namjae Jeone5066492021-03-30 12:35:23 +09008400 if (work->aux_payload_sz) {
8401 iov[0].iov_len -= work->aux_payload_sz;
8402 iov[1].iov_base = work->aux_payload_buf;
8403 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008404 n_vec++;
8405 }
8406
8407 if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature))
8408 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8409}
8410
8411/**
8412 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response
8413 * @work: smb work containing response buffer
8414 *
8415 */
8416void smb3_preauth_hash_rsp(struct ksmbd_work *work)
8417{
8418 struct ksmbd_conn *conn = work->conn;
8419 struct ksmbd_session *sess = work->sess;
8420 struct smb2_hdr *req, *rsp;
8421
8422 if (conn->dialect != SMB311_PROT_ID)
8423 return;
8424
8425 WORK_BUFFERS(work, req, rsp);
8426
Namjae Jeon442ff9e2021-09-29 15:44:32 +09008427 if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE &&
8428 conn->preauth_info)
Namjae Jeoncb451722021-11-03 08:08:44 +09008429 ksmbd_gen_preauth_integrity_hash(conn, work->response_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008430 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09008431
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008432 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008433 __u8 *hash_value;
8434
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008435 if (conn->binding) {
8436 struct preauth_session *preauth_sess;
8437
8438 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
8439 if (!preauth_sess)
8440 return;
8441 hash_value = preauth_sess->Preauth_HashValue;
8442 } else {
8443 hash_value = sess->Preauth_HashValue;
8444 if (!hash_value)
8445 return;
8446 }
Namjae Jeoncb451722021-11-03 08:08:44 +09008447 ksmbd_gen_preauth_integrity_hash(conn, work->response_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008448 hash_value);
Namjae Jeone2f34482021-03-16 10:49:09 +09008449 }
8450}
8451
Namjae Jeon2dd91292021-11-03 08:25:54 +09008452static void fill_transform_hdr(void *tr_buf, char *old_buf, __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09008453{
Namjae Jeon2dd91292021-11-03 08:25:54 +09008454 struct smb2_transform_hdr *tr_hdr = tr_buf + 4;
8455 struct smb2_hdr *hdr = smb2_get_msg(old_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008456 unsigned int orig_len = get_rfc1002_len(old_buf);
8457
Namjae Jeon2dd91292021-11-03 08:25:54 +09008458 memset(tr_buf, 0, sizeof(struct smb2_transform_hdr) + 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09008459 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
8460 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
Ronnie Sahlberg4355a8f2021-11-03 08:43:42 +09008461 tr_hdr->Flags = cpu_to_le16(TRANSFORM_FLAG_ENCRYPTED);
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008462 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
8463 cipher_type == SMB2_ENCRYPTION_AES256_GCM)
8464 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008465 else
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008466 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008467 memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8);
Namjae Jeon2dd91292021-11-03 08:25:54 +09008468 inc_rfc1001_len(tr_buf, sizeof(struct smb2_transform_hdr));
8469 inc_rfc1001_len(tr_buf, orig_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09008470}
8471
8472int smb3_encrypt_resp(struct ksmbd_work *work)
8473{
Namjae Jeone5066492021-03-30 12:35:23 +09008474 char *buf = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008475 struct kvec iov[3];
8476 int rc = -ENOMEM;
Namjae Jeone5066492021-03-30 12:35:23 +09008477 int buf_size = 0, rq_nvec = 2 + (work->aux_payload_sz ? 1 : 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09008478
8479 if (ARRAY_SIZE(iov) < rq_nvec)
8480 return -ENOMEM;
8481
Namjae Jeon2dd91292021-11-03 08:25:54 +09008482 work->tr_buf = kzalloc(sizeof(struct smb2_transform_hdr) + 4, GFP_KERNEL);
8483 if (!work->tr_buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09008484 return rc;
8485
8486 /* fill transform header */
Namjae Jeon2dd91292021-11-03 08:25:54 +09008487 fill_transform_hdr(work->tr_buf, buf, work->conn->cipher_type);
Namjae Jeone2f34482021-03-16 10:49:09 +09008488
Namjae Jeon2dd91292021-11-03 08:25:54 +09008489 iov[0].iov_base = work->tr_buf;
8490 iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008491 buf_size += iov[0].iov_len - 4;
8492
8493 iov[1].iov_base = buf + 4;
8494 iov[1].iov_len = get_rfc1002_len(buf);
Namjae Jeone5066492021-03-30 12:35:23 +09008495 if (work->aux_payload_sz) {
8496 iov[1].iov_len = work->resp_hdr_sz - 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008497
Namjae Jeone5066492021-03-30 12:35:23 +09008498 iov[2].iov_base = work->aux_payload_buf;
8499 iov[2].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008500 buf_size += iov[2].iov_len;
8501 }
8502 buf_size += iov[1].iov_len;
8503 work->resp_hdr_sz = iov[1].iov_len;
8504
8505 rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1);
8506 if (rc)
8507 return rc;
8508
8509 memmove(buf, iov[1].iov_base, iov[1].iov_len);
Namjae Jeon2dd91292021-11-03 08:25:54 +09008510 *(__be32 *)work->tr_buf = cpu_to_be32(buf_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09008511
8512 return rc;
8513}
8514
Namjae Jeonf4228b62021-08-12 10:16:40 +09008515bool smb3_is_transform_hdr(void *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09008516{
Namjae Jeon2dd91292021-11-03 08:25:54 +09008517 struct smb2_transform_hdr *trhdr = smb2_get_msg(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008518
8519 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
8520}
8521
8522int smb3_decrypt_req(struct ksmbd_work *work)
8523{
8524 struct ksmbd_conn *conn = work->conn;
8525 struct ksmbd_session *sess;
Namjae Jeone5066492021-03-30 12:35:23 +09008526 char *buf = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008527 unsigned int pdu_length = get_rfc1002_len(buf);
8528 struct kvec iov[2];
Namjae Jeon2dd91292021-11-03 08:25:54 +09008529 int buf_data_size = pdu_length - sizeof(struct smb2_transform_hdr);
8530 struct smb2_transform_hdr *tr_hdr = smb2_get_msg(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008531 int rc = 0;
8532
Namjae Jeonc7705ee2021-10-04 20:44:52 +09008533 if (buf_data_size < sizeof(struct smb2_hdr)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008534 pr_err("Transform message is too small (%u)\n",
8535 pdu_length);
Namjae Jeone2f34482021-03-16 10:49:09 +09008536 return -ECONNABORTED;
8537 }
8538
Namjae Jeonc7705ee2021-10-04 20:44:52 +09008539 if (buf_data_size < le32_to_cpu(tr_hdr->OriginalMessageSize)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008540 pr_err("Transform message is broken\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008541 return -ECONNABORTED;
8542 }
8543
Namjae Jeon4227f812021-09-29 19:52:51 +09008544 sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId));
8545 if (!sess) {
8546 pr_err("invalid session id(%llx) in transform header\n",
8547 le64_to_cpu(tr_hdr->SessionId));
8548 return -ECONNABORTED;
8549 }
8550
Namjae Jeone2f34482021-03-16 10:49:09 +09008551 iov[0].iov_base = buf;
Namjae Jeon2dd91292021-11-03 08:25:54 +09008552 iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4;
8553 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008554 iov[1].iov_len = buf_data_size;
8555 rc = ksmbd_crypt_message(conn, iov, 2, 0);
8556 if (rc)
8557 return rc;
8558
8559 memmove(buf + 4, iov[1].iov_base, buf_data_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09008560 *(__be32 *)buf = cpu_to_be32(buf_data_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09008561
8562 return rc;
8563}
8564
8565bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work)
8566{
8567 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09008568 struct smb2_hdr *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008569
8570 if (conn->dialect < SMB30_PROT_ID)
8571 return false;
8572
8573 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008574 rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008575
8576 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008577 rsp->Status == STATUS_SUCCESS)
Namjae Jeone2f34482021-03-16 10:49:09 +09008578 return true;
8579 return false;
8580}