blob: 67e8e28e3fc3529648dc99b615fdae012afa3349 [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;
Namjae Jeon914d7e52021-12-29 23:10:03 +0900302 unsigned short credits_requested, aux_max;
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900303 unsigned short credit_charge, credits_granted = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +0900304
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900305 if (work->send_no_response)
306 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +0900307
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900308 hdr->CreditCharge = req_hdr->CreditCharge;
Namjae Jeone2f34482021-03-16 10:49:09 +0900309
Namjae Jeon004443b2021-12-29 23:08:46 +0900310 if (conn->total_credits > conn->vals->max_credits) {
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900311 hdr->CreditRequest = 0;
Namjae Jeonbde16942021-06-28 15:23:19 +0900312 pr_err("Total credits overflow: %d\n", conn->total_credits);
Namjae Jeone2f34482021-03-16 10:49:09 +0900313 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +0900314 }
315
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900316 credit_charge = max_t(unsigned short,
317 le16_to_cpu(req_hdr->CreditCharge), 1);
Namjae Jeon914d7e52021-12-29 23:10:03 +0900318 if (credit_charge > conn->total_credits) {
319 ksmbd_debug(SMB, "Insufficient credits granted, given: %u, granted: %u\n",
320 credit_charge, conn->total_credits);
321 return -EINVAL;
322 }
323
324 conn->total_credits -= credit_charge;
Namjae Jeonb589f5d2021-12-31 09:26:25 +0900325 conn->outstanding_credits -= credit_charge;
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900326 credits_requested = max_t(unsigned short,
327 le16_to_cpu(req_hdr->CreditRequest), 1);
328
329 /* according to smb2.credits smbtorture, Windows server
330 * 2016 or later grant up to 8192 credits at once.
331 *
332 * TODO: Need to adjuct CreditRequest value according to
333 * current cpu load
334 */
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900335 if (hdr->Command == SMB2_NEGOTIATE)
Namjae Jeon914d7e52021-12-29 23:10:03 +0900336 aux_max = 1;
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900337 else
Namjae Jeon004443b2021-12-29 23:08:46 +0900338 aux_max = conn->vals->max_credits - credit_charge;
Namjae Jeon914d7e52021-12-29 23:10:03 +0900339 credits_granted = min_t(unsigned short, credits_requested, aux_max);
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900340
Namjae Jeon004443b2021-12-29 23:08:46 +0900341 if (conn->vals->max_credits - conn->total_credits < credits_granted)
342 credits_granted = conn->vals->max_credits -
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900343 conn->total_credits;
344
Namjae Jeone2f34482021-03-16 10:49:09 +0900345 conn->total_credits += credits_granted;
346 work->credits_granted += credits_granted;
347
348 if (!req_hdr->NextCommand) {
349 /* Update CreditRequest in last request */
350 hdr->CreditRequest = cpu_to_le16(work->credits_granted);
351 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900352 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900353 "credits: requested[%d] granted[%d] total_granted[%d]\n",
354 credits_requested, credits_granted,
355 conn->total_credits);
Namjae Jeone2f34482021-03-16 10:49:09 +0900356 return 0;
357}
358
359/**
360 * init_chained_smb2_rsp() - initialize smb2 chained response
361 * @work: smb work containing smb response buffer
362 */
363static void init_chained_smb2_rsp(struct ksmbd_work *work)
364{
Namjae Jeon8a893312021-06-25 13:43:37 +0900365 struct smb2_hdr *req = ksmbd_req_buf_next(work);
366 struct smb2_hdr *rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900367 struct smb2_hdr *rsp_hdr;
368 struct smb2_hdr *rcv_hdr;
369 int next_hdr_offset = 0;
370 int len, new_len;
371
372 /* Len of this response = updated RFC len - offset of previous cmd
373 * in the compound rsp
374 */
375
376 /* Storing the current local FID which may be needed by subsequent
377 * command in the compound request
378 */
379 if (req->Command == SMB2_CREATE && rsp->Status == STATUS_SUCCESS) {
380 work->compound_fid =
381 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
382 VolatileFileId);
383 work->compound_pfid =
384 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
385 PersistentFileId);
386 work->compound_sid = le64_to_cpu(rsp->SessionId);
387 }
388
Namjae Jeone5066492021-03-30 12:35:23 +0900389 len = get_rfc1002_len(work->response_buf) - work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +0900390 next_hdr_offset = le32_to_cpu(req->NextCommand);
391
392 new_len = ALIGN(len, 8);
Namjae Jeoncb451722021-11-03 08:08:44 +0900393 inc_rfc1001_len(work->response_buf,
394 sizeof(struct smb2_hdr) + new_len - len);
Namjae Jeone2f34482021-03-16 10:49:09 +0900395 rsp->NextCommand = cpu_to_le32(new_len);
396
397 work->next_smb2_rcv_hdr_off += next_hdr_offset;
398 work->next_smb2_rsp_hdr_off += new_len;
399 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900400 "Compound req new_len = %d rcv off = %d rsp off = %d\n",
401 new_len, work->next_smb2_rcv_hdr_off,
402 work->next_smb2_rsp_hdr_off);
Namjae Jeone2f34482021-03-16 10:49:09 +0900403
Namjae Jeon8a893312021-06-25 13:43:37 +0900404 rsp_hdr = ksmbd_resp_buf_next(work);
405 rcv_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900406
407 if (!(rcv_hdr->Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
408 ksmbd_debug(SMB, "related flag should be set\n");
409 work->compound_fid = KSMBD_NO_FID;
410 work->compound_pfid = KSMBD_NO_FID;
411 }
Namjae Jeoncb451722021-11-03 08:08:44 +0900412 memset((char *)rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
Namjae Jeon18a015b2021-09-22 21:00:57 +0900413 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
Namjae Jeone2f34482021-03-16 10:49:09 +0900414 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
415 rsp_hdr->Command = rcv_hdr->Command;
416
417 /*
418 * Message is response. We don't grant oplock yet.
419 */
420 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR |
421 SMB2_FLAGS_RELATED_OPERATIONS);
422 rsp_hdr->NextCommand = 0;
423 rsp_hdr->MessageId = rcv_hdr->MessageId;
424 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
425 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
426 rsp_hdr->SessionId = rcv_hdr->SessionId;
427 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
428}
429
430/**
431 * is_chained_smb2_message() - check for chained command
432 * @work: smb work containing smb request buffer
433 *
434 * Return: true if chained request, otherwise false
435 */
436bool is_chained_smb2_message(struct ksmbd_work *work)
437{
Namjae Jeoncb451722021-11-03 08:08:44 +0900438 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
Namjae Jeond72a9c12021-09-24 09:24:08 +0900439 unsigned int len, next_cmd;
Namjae Jeone2f34482021-03-16 10:49:09 +0900440
441 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
442 return false;
443
Namjae Jeon8a893312021-06-25 13:43:37 +0900444 hdr = ksmbd_req_buf_next(work);
Namjae Jeond72a9c12021-09-24 09:24:08 +0900445 next_cmd = le32_to_cpu(hdr->NextCommand);
446 if (next_cmd > 0) {
447 if ((u64)work->next_smb2_rcv_hdr_off + next_cmd +
448 __SMB2_HEADER_STRUCTURE_SIZE >
449 get_rfc1002_len(work->request_buf)) {
450 pr_err("next command(%u) offset exceeds smb msg size\n",
451 next_cmd);
452 return false;
453 }
454
Namjae Jeondbad6302021-10-11 19:15:25 +0900455 if ((u64)get_rfc1002_len(work->response_buf) + MAX_CIFS_SMALL_BUFFER_SIZE >
456 work->response_sz) {
457 pr_err("next response offset exceeds response buffer size\n");
458 return false;
459 }
460
Namjae Jeone2f34482021-03-16 10:49:09 +0900461 ksmbd_debug(SMB, "got SMB2 chained command\n");
462 init_chained_smb2_rsp(work);
463 return true;
464 } else if (work->next_smb2_rcv_hdr_off) {
465 /*
466 * This is last request in chained command,
467 * align response to 8 byte
468 */
Namjae Jeone5066492021-03-30 12:35:23 +0900469 len = ALIGN(get_rfc1002_len(work->response_buf), 8);
470 len = len - get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900471 if (len) {
472 ksmbd_debug(SMB, "padding len %u\n", len);
Namjae Jeone5066492021-03-30 12:35:23 +0900473 inc_rfc1001_len(work->response_buf, len);
474 if (work->aux_payload_sz)
Namjae Jeone2f34482021-03-16 10:49:09 +0900475 work->aux_payload_sz += len;
476 }
477 }
478 return false;
479}
480
481/**
482 * init_smb2_rsp_hdr() - initialize smb2 response
483 * @work: smb work containing smb request buffer
484 *
485 * Return: 0
486 */
487int init_smb2_rsp_hdr(struct ksmbd_work *work)
488{
Namjae Jeoncb451722021-11-03 08:08:44 +0900489 struct smb2_hdr *rsp_hdr = smb2_get_msg(work->response_buf);
490 struct smb2_hdr *rcv_hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900491 struct ksmbd_conn *conn = work->conn;
492
493 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
Namjae Jeoncb451722021-11-03 08:08:44 +0900494 *(__be32 *)work->response_buf =
495 cpu_to_be32(conn->vals->header_size);
Namjae Jeone2f34482021-03-16 10:49:09 +0900496 rsp_hdr->ProtocolId = rcv_hdr->ProtocolId;
497 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
498 rsp_hdr->Command = rcv_hdr->Command;
499
500 /*
501 * Message is response. We don't grant oplock yet.
502 */
503 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
504 rsp_hdr->NextCommand = 0;
505 rsp_hdr->MessageId = rcv_hdr->MessageId;
506 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
507 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
508 rsp_hdr->SessionId = rcv_hdr->SessionId;
509 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
510
511 work->syncronous = true;
512 if (work->async_id) {
Namjae Jeond40012a2021-04-13 13:06:30 +0900513 ksmbd_release_id(&conn->async_ida, work->async_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900514 work->async_id = 0;
515 }
516
517 return 0;
518}
519
520/**
521 * smb2_allocate_rsp_buf() - allocate smb2 response buffer
522 * @work: smb work containing smb request buffer
523 *
524 * Return: 0 on success, otherwise -ENOMEM
525 */
526int smb2_allocate_rsp_buf(struct ksmbd_work *work)
527{
Namjae Jeoncb451722021-11-03 08:08:44 +0900528 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900529 size_t small_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
Namjae Jeon4bc59472021-10-15 17:14:02 +0900530 size_t large_sz = small_sz + work->conn->vals->max_trans_size;
Namjae Jeone2f34482021-03-16 10:49:09 +0900531 size_t sz = small_sz;
532 int cmd = le16_to_cpu(hdr->Command);
533
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900534 if (cmd == SMB2_IOCTL_HE || cmd == SMB2_QUERY_DIRECTORY_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900535 sz = large_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +0900536
537 if (cmd == SMB2_QUERY_INFO_HE) {
538 struct smb2_query_info_req *req;
539
Namjae Jeoncb451722021-11-03 08:08:44 +0900540 req = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900541 if (req->InfoType == SMB2_O_INFO_FILE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +0900542 (req->FileInfoClass == FILE_FULL_EA_INFORMATION ||
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900543 req->FileInfoClass == FILE_ALL_INFORMATION))
Namjae Jeone2f34482021-03-16 10:49:09 +0900544 sz = large_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +0900545 }
546
547 /* allocate large response buf for chained commands */
548 if (le32_to_cpu(hdr->NextCommand) > 0)
549 sz = large_sz;
550
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900551 work->response_buf = kvmalloc(sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeon63c454f2021-04-20 14:24:28 +0900552 if (!work->response_buf)
Namjae Jeone2f34482021-03-16 10:49:09 +0900553 return -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +0900554
555 work->response_sz = sz;
556 return 0;
557}
558
559/**
560 * smb2_check_user_session() - check for valid session for a user
561 * @work: smb work containing smb request buffer
562 *
563 * Return: 0 on success, otherwise error
564 */
565int smb2_check_user_session(struct ksmbd_work *work)
566{
Namjae Jeoncb451722021-11-03 08:08:44 +0900567 struct smb2_hdr *req_hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900568 struct ksmbd_conn *conn = work->conn;
569 unsigned int cmd = conn->ops->get_cmd_val(work);
570 unsigned long long sess_id;
571
572 work->sess = NULL;
573 /*
574 * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not
575 * require a session id, so no need to validate user session's for
576 * these commands.
577 */
578 if (cmd == SMB2_ECHO_HE || cmd == SMB2_NEGOTIATE_HE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +0900579 cmd == SMB2_SESSION_SETUP_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900580 return 0;
581
582 if (!ksmbd_conn_good(work))
583 return -EINVAL;
584
585 sess_id = le64_to_cpu(req_hdr->SessionId);
586 /* Check for validity of user session */
Namjae Jeonf5a544e2021-06-18 10:04:19 +0900587 work->sess = ksmbd_session_lookup_all(conn, sess_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900588 if (work->sess)
589 return 1;
590 ksmbd_debug(SMB, "Invalid user session, Uid %llu\n", sess_id);
591 return -EINVAL;
592}
593
Namjae Jeon64b39f42021-03-30 14:25:35 +0900594static void destroy_previous_session(struct ksmbd_user *user, u64 id)
Namjae Jeone2f34482021-03-16 10:49:09 +0900595{
596 struct ksmbd_session *prev_sess = ksmbd_session_lookup_slowpath(id);
597 struct ksmbd_user *prev_user;
598
599 if (!prev_sess)
600 return;
601
602 prev_user = prev_sess->user;
603
Marios Makassikis1fca8032021-05-06 11:41:54 +0900604 if (!prev_user ||
605 strcmp(user->name, prev_user->name) ||
Namjae Jeone2f34482021-03-16 10:49:09 +0900606 user->passkey_sz != prev_user->passkey_sz ||
607 memcmp(user->passkey, prev_user->passkey, user->passkey_sz)) {
608 put_session(prev_sess);
609 return;
610 }
611
612 put_session(prev_sess);
613 ksmbd_session_destroy(prev_sess);
614}
615
616/**
617 * smb2_get_name() - get filename string from on the wire smb format
618 * @src: source buffer
619 * @maxlen: maxlen of source string
Yang Lid4eeb822021-12-21 20:48:57 +0900620 * @local_nls: nls_table pointer
Namjae Jeone2f34482021-03-16 10:49:09 +0900621 *
622 * Return: matching converted filename on success, otherwise error ptr
623 */
624static char *
Marios Makassikis80917f12021-12-01 21:41:19 +0100625smb2_get_name(const char *src, const int maxlen, struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +0900626{
Hyunchul Lee265fd192021-09-25 00:06:16 +0900627 char *name;
Namjae Jeone2f34482021-03-16 10:49:09 +0900628
Namjae Jeon64b39f42021-03-30 14:25:35 +0900629 name = smb_strndup_from_utf16(src, maxlen, 1, local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +0900630 if (IS_ERR(name)) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900631 pr_err("failed to get name %ld\n", PTR_ERR(name));
Namjae Jeone2f34482021-03-16 10:49:09 +0900632 return name;
633 }
634
Hyunchul Lee265fd192021-09-25 00:06:16 +0900635 ksmbd_conv_path_to_unix(name);
636 ksmbd_strip_last_slash(name);
637 return name;
Namjae Jeone2f34482021-03-16 10:49:09 +0900638}
639
Namjae Jeone2f34482021-03-16 10:49:09 +0900640int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg)
641{
642 struct smb2_hdr *rsp_hdr;
643 struct ksmbd_conn *conn = work->conn;
644 int id;
645
Namjae Jeoncb451722021-11-03 08:08:44 +0900646 rsp_hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900647 rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND;
648
Namjae Jeond40012a2021-04-13 13:06:30 +0900649 id = ksmbd_acquire_async_msg_id(&conn->async_ida);
Namjae Jeone2f34482021-03-16 10:49:09 +0900650 if (id < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900651 pr_err("Failed to alloc async message id\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900652 return id;
653 }
654 work->syncronous = false;
655 work->async_id = id;
656 rsp_hdr->Id.AsyncId = cpu_to_le64(id);
657
658 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900659 "Send interim Response to inform async request id : %d\n",
660 work->async_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900661
662 work->cancel_fn = fn;
663 work->cancel_argv = arg;
664
Namjae Jeon6c4e6752021-06-07 09:08:45 +0900665 if (list_empty(&work->async_request_entry)) {
666 spin_lock(&conn->request_lock);
667 list_add_tail(&work->async_request_entry, &conn->async_requests);
668 spin_unlock(&conn->request_lock);
669 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900670
671 return 0;
672}
673
674void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
675{
676 struct smb2_hdr *rsp_hdr;
677
Namjae Jeoncb451722021-11-03 08:08:44 +0900678 rsp_hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900679 smb2_set_err_rsp(work);
680 rsp_hdr->Status = status;
681
682 work->multiRsp = 1;
683 ksmbd_conn_write(work);
684 rsp_hdr->Status = 0;
685 work->multiRsp = 0;
686}
687
688static __le32 smb2_get_reparse_tag_special_file(umode_t mode)
689{
690 if (S_ISDIR(mode) || S_ISREG(mode))
691 return 0;
692
693 if (S_ISLNK(mode))
694 return IO_REPARSE_TAG_LX_SYMLINK_LE;
695 else if (S_ISFIFO(mode))
696 return IO_REPARSE_TAG_LX_FIFO_LE;
697 else if (S_ISSOCK(mode))
698 return IO_REPARSE_TAG_AF_UNIX_LE;
699 else if (S_ISCHR(mode))
700 return IO_REPARSE_TAG_LX_CHR_LE;
701 else if (S_ISBLK(mode))
702 return IO_REPARSE_TAG_LX_BLK_LE;
703
704 return 0;
705}
706
707/**
708 * smb2_get_dos_mode() - get file mode in dos format from unix mode
709 * @stat: kstat containing file mode
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900710 * @attribute: attribute flags
Namjae Jeone2f34482021-03-16 10:49:09 +0900711 *
712 * Return: converted dos mode
713 */
714static int smb2_get_dos_mode(struct kstat *stat, int attribute)
715{
716 int attr = 0;
717
Namjae Jeon64b39f42021-03-30 14:25:35 +0900718 if (S_ISDIR(stat->mode)) {
Ronnie Sahlberg26a27872021-11-03 08:45:52 +0900719 attr = FILE_ATTRIBUTE_DIRECTORY |
720 (attribute & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM));
Namjae Jeon64b39f42021-03-30 14:25:35 +0900721 } else {
Ronnie Sahlberg26a27872021-11-03 08:45:52 +0900722 attr = (attribute & 0x00005137) | FILE_ATTRIBUTE_ARCHIVE;
723 attr &= ~(FILE_ATTRIBUTE_DIRECTORY);
Namjae Jeone2f34482021-03-16 10:49:09 +0900724 if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps &
725 FILE_SUPPORTS_SPARSE_FILES))
Ronnie Sahlberg26a27872021-11-03 08:45:52 +0900726 attr |= FILE_ATTRIBUTE_SPARSE_FILE;
Namjae Jeone2f34482021-03-16 10:49:09 +0900727
728 if (smb2_get_reparse_tag_special_file(stat->mode))
Ronnie Sahlberg26a27872021-11-03 08:45:52 +0900729 attr |= FILE_ATTRIBUTE_REPARSE_POINT;
Namjae Jeone2f34482021-03-16 10:49:09 +0900730 }
731
732 return attr;
733}
734
735static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900736 __le16 hash_id)
Namjae Jeone2f34482021-03-16 10:49:09 +0900737{
738 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
739 pneg_ctxt->DataLength = cpu_to_le16(38);
740 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
741 pneg_ctxt->Reserved = cpu_to_le32(0);
742 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
743 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
744 pneg_ctxt->HashAlgorithms = hash_id;
745}
746
747static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900748 __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +0900749{
750 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
751 pneg_ctxt->DataLength = cpu_to_le16(4);
752 pneg_ctxt->Reserved = cpu_to_le32(0);
753 pneg_ctxt->CipherCount = cpu_to_le16(1);
754 pneg_ctxt->Ciphers[0] = cipher_type;
755}
756
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900757static void build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900758 __le16 comp_algo)
Namjae Jeone2f34482021-03-16 10:49:09 +0900759{
760 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
761 pneg_ctxt->DataLength =
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900762 cpu_to_le16(sizeof(struct smb2_compression_capabilities_context)
Namjae Jeone2f34482021-03-16 10:49:09 +0900763 - sizeof(struct smb2_neg_context));
764 pneg_ctxt->Reserved = cpu_to_le32(0);
765 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(1);
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900766 pneg_ctxt->Flags = cpu_to_le32(0);
Namjae Jeone2f34482021-03-16 10:49:09 +0900767 pneg_ctxt->CompressionAlgorithms[0] = comp_algo;
768}
769
Namjae Jeon378087c2021-07-21 10:05:53 +0900770static void build_sign_cap_ctxt(struct smb2_signing_capabilities *pneg_ctxt,
771 __le16 sign_algo)
772{
773 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
774 pneg_ctxt->DataLength =
775 cpu_to_le16((sizeof(struct smb2_signing_capabilities) + 2)
776 - sizeof(struct smb2_neg_context));
777 pneg_ctxt->Reserved = cpu_to_le32(0);
778 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(1);
779 pneg_ctxt->SigningAlgorithms[0] = sign_algo;
780}
781
Namjae Jeon64b39f42021-03-30 14:25:35 +0900782static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900783{
784 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
785 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
786 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
787 pneg_ctxt->Name[0] = 0x93;
788 pneg_ctxt->Name[1] = 0xAD;
789 pneg_ctxt->Name[2] = 0x25;
790 pneg_ctxt->Name[3] = 0x50;
791 pneg_ctxt->Name[4] = 0x9C;
792 pneg_ctxt->Name[5] = 0xB4;
793 pneg_ctxt->Name[6] = 0x11;
794 pneg_ctxt->Name[7] = 0xE7;
795 pneg_ctxt->Name[8] = 0xB4;
796 pneg_ctxt->Name[9] = 0x23;
797 pneg_ctxt->Name[10] = 0x83;
798 pneg_ctxt->Name[11] = 0xDE;
799 pneg_ctxt->Name[12] = 0x96;
800 pneg_ctxt->Name[13] = 0x8B;
801 pneg_ctxt->Name[14] = 0xCD;
802 pneg_ctxt->Name[15] = 0x7C;
803}
804
Namjae Jeon64b39f42021-03-30 14:25:35 +0900805static void assemble_neg_contexts(struct ksmbd_conn *conn,
Namjae Jeoncb451722021-11-03 08:08:44 +0900806 struct smb2_negotiate_rsp *rsp,
807 void *smb2_buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +0900808{
Namjae Jeone2f34482021-03-16 10:49:09 +0900809 char *pneg_ctxt = (char *)rsp +
Namjae Jeoncb451722021-11-03 08:08:44 +0900810 le32_to_cpu(rsp->NegotiateContextOffset);
Namjae Jeone2f34482021-03-16 10:49:09 +0900811 int neg_ctxt_cnt = 1;
812 int ctxt_size;
813
814 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900815 "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900816 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900817 conn->preauth_info->Preauth_HashId);
Namjae Jeone2f34482021-03-16 10:49:09 +0900818 rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt);
Namjae Jeoncb451722021-11-03 08:08:44 +0900819 inc_rfc1001_len(smb2_buf_len, AUTH_GSS_PADDING);
Namjae Jeone2f34482021-03-16 10:49:09 +0900820 ctxt_size = sizeof(struct smb2_preauth_neg_context);
821 /* Round to 8 byte boundary */
822 pneg_ctxt += round_up(sizeof(struct smb2_preauth_neg_context), 8);
823
824 if (conn->cipher_type) {
825 ctxt_size = round_up(ctxt_size, 8);
826 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900827 "assemble SMB2_ENCRYPTION_CAPABILITIES context\n");
Namjae Jeon64b39f42021-03-30 14:25:35 +0900828 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900829 conn->cipher_type);
Namjae Jeone2f34482021-03-16 10:49:09 +0900830 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900831 ctxt_size += sizeof(struct smb2_encryption_neg_context) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900832 /* Round to 8 byte boundary */
833 pneg_ctxt +=
Namjae Jeonaf320a72021-07-21 10:03:19 +0900834 round_up(sizeof(struct smb2_encryption_neg_context) + 2,
Namjae Jeone2f34482021-03-16 10:49:09 +0900835 8);
836 }
837
838 if (conn->compress_algorithm) {
839 ctxt_size = round_up(ctxt_size, 8);
840 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900841 "assemble SMB2_COMPRESSION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900842 /* Temporarily set to SMB3_COMPRESS_NONE */
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900843 build_compression_ctxt((struct smb2_compression_capabilities_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900844 conn->compress_algorithm);
Namjae Jeone2f34482021-03-16 10:49:09 +0900845 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900846 ctxt_size += sizeof(struct smb2_compression_capabilities_context) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900847 /* Round to 8 byte boundary */
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900848 pneg_ctxt += round_up(sizeof(struct smb2_compression_capabilities_context) + 2,
Namjae Jeonaf320a72021-07-21 10:03:19 +0900849 8);
Namjae Jeone2f34482021-03-16 10:49:09 +0900850 }
851
852 if (conn->posix_ext_supported) {
853 ctxt_size = round_up(ctxt_size, 8);
854 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900855 "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900856 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
857 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
858 ctxt_size += sizeof(struct smb2_posix_neg_context);
Namjae Jeon378087c2021-07-21 10:05:53 +0900859 /* Round to 8 byte boundary */
860 pneg_ctxt += round_up(sizeof(struct smb2_posix_neg_context), 8);
861 }
862
863 if (conn->signing_negotiated) {
864 ctxt_size = round_up(ctxt_size, 8);
865 ksmbd_debug(SMB,
866 "assemble SMB2_SIGNING_CAPABILITIES context\n");
867 build_sign_cap_ctxt((struct smb2_signing_capabilities *)pneg_ctxt,
868 conn->signing_algorithm);
869 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
870 ctxt_size += sizeof(struct smb2_signing_capabilities) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900871 }
872
Namjae Jeoncb451722021-11-03 08:08:44 +0900873 inc_rfc1001_len(smb2_buf_len, ctxt_size);
Namjae Jeone2f34482021-03-16 10:49:09 +0900874}
875
Namjae Jeon64b39f42021-03-30 14:25:35 +0900876static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900877 struct smb2_preauth_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900878{
879 __le32 err = STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP;
880
Namjae Jeon070fb212021-05-26 17:57:12 +0900881 if (pneg_ctxt->HashAlgorithms == SMB2_PREAUTH_INTEGRITY_SHA512) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900882 conn->preauth_info->Preauth_HashId =
883 SMB2_PREAUTH_INTEGRITY_SHA512;
884 err = STATUS_SUCCESS;
885 }
886
887 return err;
888}
889
Namjae Jeonaf320a72021-07-21 10:03:19 +0900890static void decode_encrypt_ctxt(struct ksmbd_conn *conn,
891 struct smb2_encryption_neg_context *pneg_ctxt,
892 int len_of_ctxts)
Namjae Jeone2f34482021-03-16 10:49:09 +0900893{
Namjae Jeone2f34482021-03-16 10:49:09 +0900894 int cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900895 int i, cphs_size = cph_cnt * sizeof(__le16);
Namjae Jeone2f34482021-03-16 10:49:09 +0900896
897 conn->cipher_type = 0;
898
Namjae Jeonaf320a72021-07-21 10:03:19 +0900899 if (sizeof(struct smb2_encryption_neg_context) + cphs_size >
900 len_of_ctxts) {
901 pr_err("Invalid cipher count(%d)\n", cph_cnt);
902 return;
903 }
904
Namjae Jeone2f34482021-03-16 10:49:09 +0900905 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION))
Namjae Jeonaf320a72021-07-21 10:03:19 +0900906 return;
Namjae Jeone2f34482021-03-16 10:49:09 +0900907
908 for (i = 0; i < cph_cnt; i++) {
909 if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM ||
Namjae Jeon5a0ca772021-05-06 11:43:37 +0900910 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM ||
911 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM ||
912 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900913 ksmbd_debug(SMB, "Cipher ID = 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +0900914 pneg_ctxt->Ciphers[i]);
Namjae Jeone2f34482021-03-16 10:49:09 +0900915 conn->cipher_type = pneg_ctxt->Ciphers[i];
916 break;
917 }
918 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900919}
920
Marcos Del Sol Vives83912d62021-12-16 11:37:22 +0100921/**
922 * smb3_encryption_negotiated() - checks if server and client agreed on enabling encryption
923 * @conn: smb connection
924 *
925 * Return: true if connection should be encrypted, else false
926 */
927static bool smb3_encryption_negotiated(struct ksmbd_conn *conn)
928{
929 if (!conn->ops->generate_encryptionkey)
930 return false;
931
932 /*
933 * SMB 3.0 and 3.0.2 dialects use the SMB2_GLOBAL_CAP_ENCRYPTION flag.
934 * SMB 3.1.1 uses the cipher_type field.
935 */
936 return (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) ||
937 conn->cipher_type;
938}
939
Namjae Jeonaf320a72021-07-21 10:03:19 +0900940static void decode_compress_ctxt(struct ksmbd_conn *conn,
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900941 struct smb2_compression_capabilities_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900942{
Namjae Jeone2f34482021-03-16 10:49:09 +0900943 conn->compress_algorithm = SMB3_COMPRESS_NONE;
Namjae Jeone2f34482021-03-16 10:49:09 +0900944}
945
Namjae Jeon378087c2021-07-21 10:05:53 +0900946static void decode_sign_cap_ctxt(struct ksmbd_conn *conn,
947 struct smb2_signing_capabilities *pneg_ctxt,
948 int len_of_ctxts)
949{
950 int sign_algo_cnt = le16_to_cpu(pneg_ctxt->SigningAlgorithmCount);
951 int i, sign_alos_size = sign_algo_cnt * sizeof(__le16);
952
953 conn->signing_negotiated = false;
954
955 if (sizeof(struct smb2_signing_capabilities) + sign_alos_size >
956 len_of_ctxts) {
957 pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt);
958 return;
959 }
960
961 for (i = 0; i < sign_algo_cnt; i++) {
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +0900962 if (pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_HMAC_SHA256_LE ||
963 pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_AES_CMAC_LE) {
Namjae Jeon378087c2021-07-21 10:05:53 +0900964 ksmbd_debug(SMB, "Signing Algorithm ID = 0x%x\n",
965 pneg_ctxt->SigningAlgorithms[i]);
966 conn->signing_negotiated = true;
967 conn->signing_algorithm =
968 pneg_ctxt->SigningAlgorithms[i];
969 break;
970 }
971 }
972}
973
Namjae Jeone2f34482021-03-16 10:49:09 +0900974static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn,
Namjae Jeoncb451722021-11-03 08:08:44 +0900975 struct smb2_negotiate_req *req,
976 int len_of_smb)
Namjae Jeone2f34482021-03-16 10:49:09 +0900977{
Namjae Jeone2f34482021-03-16 10:49:09 +0900978 /* +4 is to account for the RFC1001 len field */
Namjae Jeoncb451722021-11-03 08:08:44 +0900979 struct smb2_neg_context *pctx = (struct smb2_neg_context *)req;
Namjae Jeonaf320a72021-07-21 10:03:19 +0900980 int i = 0, len_of_ctxts;
981 int offset = le32_to_cpu(req->NegotiateContextOffset);
Namjae Jeone2f34482021-03-16 10:49:09 +0900982 int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900983 __le32 status = STATUS_INVALID_PARAMETER;
Namjae Jeone2f34482021-03-16 10:49:09 +0900984
Namjae Jeonaf320a72021-07-21 10:03:19 +0900985 ksmbd_debug(SMB, "decoding %d negotiate contexts\n", neg_ctxt_cnt);
986 if (len_of_smb <= offset) {
987 ksmbd_debug(SMB, "Invalid response: negotiate context offset\n");
988 return status;
989 }
990
991 len_of_ctxts = len_of_smb - offset;
992
Namjae Jeone2f34482021-03-16 10:49:09 +0900993 while (i++ < neg_ctxt_cnt) {
Namjae Jeonaf320a72021-07-21 10:03:19 +0900994 int clen;
995
996 /* check that offset is not beyond end of SMB */
997 if (len_of_ctxts == 0)
998 break;
999
1000 if (len_of_ctxts < sizeof(struct smb2_neg_context))
1001 break;
1002
1003 pctx = (struct smb2_neg_context *)((char *)pctx + offset);
1004 clen = le16_to_cpu(pctx->DataLength);
1005 if (clen + sizeof(struct smb2_neg_context) > len_of_ctxts)
1006 break;
1007
1008 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001009 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001010 "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001011 if (conn->preauth_info->Preauth_HashId)
1012 break;
1013
1014 status = decode_preauth_ctxt(conn,
Namjae Jeonaf320a72021-07-21 10:03:19 +09001015 (struct smb2_preauth_neg_context *)pctx);
1016 if (status != STATUS_SUCCESS)
1017 break;
1018 } else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001019 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001020 "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001021 if (conn->cipher_type)
1022 break;
1023
Namjae Jeonaf320a72021-07-21 10:03:19 +09001024 decode_encrypt_ctxt(conn,
1025 (struct smb2_encryption_neg_context *)pctx,
1026 len_of_ctxts);
1027 } else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001028 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001029 "deassemble SMB2_COMPRESSION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001030 if (conn->compress_algorithm)
1031 break;
1032
Namjae Jeonaf320a72021-07-21 10:03:19 +09001033 decode_compress_ctxt(conn,
Ronnie Sahlbergd6c9ad23b2021-11-03 08:44:38 +09001034 (struct smb2_compression_capabilities_context *)pctx);
Namjae Jeonaf320a72021-07-21 10:03:19 +09001035 } else if (pctx->ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001036 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001037 "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n");
Namjae Jeonaf320a72021-07-21 10:03:19 +09001038 } else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001039 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001040 "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001041 conn->posix_ext_supported = true;
Namjae Jeon378087c2021-07-21 10:05:53 +09001042 } else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES) {
1043 ksmbd_debug(SMB,
1044 "deassemble SMB2_SIGNING_CAPABILITIES context\n");
1045 decode_sign_cap_ctxt(conn,
1046 (struct smb2_signing_capabilities *)pctx,
1047 len_of_ctxts);
Namjae Jeone2f34482021-03-16 10:49:09 +09001048 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001049
Namjae Jeonaf320a72021-07-21 10:03:19 +09001050 /* offsets must be 8 byte aligned */
1051 clen = (clen + 7) & ~0x7;
1052 offset = clen + sizeof(struct smb2_neg_context);
1053 len_of_ctxts -= clen + sizeof(struct smb2_neg_context);
Namjae Jeone2f34482021-03-16 10:49:09 +09001054 }
1055 return status;
1056}
1057
1058/**
1059 * smb2_handle_negotiate() - handler for smb2 negotiate command
1060 * @work: smb work containing smb request buffer
1061 *
1062 * Return: 0
1063 */
1064int smb2_handle_negotiate(struct ksmbd_work *work)
1065{
1066 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09001067 struct smb2_negotiate_req *req = smb2_get_msg(work->request_buf);
1068 struct smb2_negotiate_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001069 int rc = 0;
Namjae Jeon442ff9e2021-09-29 15:44:32 +09001070 unsigned int smb2_buf_len, smb2_neg_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09001071 __le32 status;
1072
1073 ksmbd_debug(SMB, "Received negotiate request\n");
1074 conn->need_neg = false;
1075 if (ksmbd_conn_good(work)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001076 pr_err("conn->tcp_status is already in CifsGood State\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001077 work->send_no_response = 1;
1078 return rc;
1079 }
1080
1081 if (req->DialectCount == 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001082 pr_err("malformed packet\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001083 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1084 rc = -EINVAL;
1085 goto err_out;
1086 }
1087
Namjae Jeon442ff9e2021-09-29 15:44:32 +09001088 smb2_buf_len = get_rfc1002_len(work->request_buf);
Namjae Jeoncb451722021-11-03 08:08:44 +09001089 smb2_neg_size = offsetof(struct smb2_negotiate_req, Dialects);
Namjae Jeon442ff9e2021-09-29 15:44:32 +09001090 if (smb2_neg_size > smb2_buf_len) {
1091 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1092 rc = -EINVAL;
1093 goto err_out;
1094 }
1095
1096 if (conn->dialect == SMB311_PROT_ID) {
1097 unsigned int nego_ctxt_off = le32_to_cpu(req->NegotiateContextOffset);
1098
1099 if (smb2_buf_len < nego_ctxt_off) {
1100 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1101 rc = -EINVAL;
1102 goto err_out;
1103 }
1104
1105 if (smb2_neg_size > nego_ctxt_off) {
1106 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1107 rc = -EINVAL;
1108 goto err_out;
1109 }
1110
1111 if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) >
1112 nego_ctxt_off) {
1113 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1114 rc = -EINVAL;
1115 goto err_out;
1116 }
1117 } else {
1118 if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) >
1119 smb2_buf_len) {
1120 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1121 rc = -EINVAL;
1122 goto err_out;
1123 }
1124 }
1125
Namjae Jeone2f34482021-03-16 10:49:09 +09001126 conn->cli_cap = le32_to_cpu(req->Capabilities);
1127 switch (conn->dialect) {
1128 case SMB311_PROT_ID:
1129 conn->preauth_info =
1130 kzalloc(sizeof(struct preauth_integrity_info),
Namjae Jeon070fb212021-05-26 17:57:12 +09001131 GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09001132 if (!conn->preauth_info) {
1133 rc = -ENOMEM;
1134 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1135 goto err_out;
1136 }
1137
Namjae Jeoncb451722021-11-03 08:08:44 +09001138 status = deassemble_neg_contexts(conn, req,
1139 get_rfc1002_len(work->request_buf));
Namjae Jeone2f34482021-03-16 10:49:09 +09001140 if (status != STATUS_SUCCESS) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001141 pr_err("deassemble_neg_contexts error(0x%x)\n",
1142 status);
Namjae Jeone2f34482021-03-16 10:49:09 +09001143 rsp->hdr.Status = status;
1144 rc = -EINVAL;
1145 goto err_out;
1146 }
1147
1148 rc = init_smb3_11_server(conn);
1149 if (rc < 0) {
1150 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1151 goto err_out;
1152 }
1153
1154 ksmbd_gen_preauth_integrity_hash(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001155 work->request_buf,
1156 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001157 rsp->NegotiateContextOffset =
1158 cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
Namjae Jeoncb451722021-11-03 08:08:44 +09001159 assemble_neg_contexts(conn, rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001160 break;
1161 case SMB302_PROT_ID:
1162 init_smb3_02_server(conn);
1163 break;
1164 case SMB30_PROT_ID:
1165 init_smb3_0_server(conn);
1166 break;
1167 case SMB21_PROT_ID:
1168 init_smb2_1_server(conn);
1169 break;
Namjae Jeone2f34482021-03-16 10:49:09 +09001170 case SMB2X_PROT_ID:
1171 case BAD_PROT_ID:
1172 default:
1173 ksmbd_debug(SMB, "Server dialect :0x%x not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001174 conn->dialect);
Namjae Jeone2f34482021-03-16 10:49:09 +09001175 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
1176 rc = -EINVAL;
1177 goto err_out;
1178 }
1179 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
1180
1181 /* For stats */
1182 conn->connection_type = conn->dialect;
1183
1184 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
1185 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
1186 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
1187
Namjae Jeon51a13872021-09-29 13:09:24 +09001188 memcpy(conn->ClientGUID, req->ClientGUID,
1189 SMB2_CLIENT_GUID_SIZE);
1190 conn->cli_sec_mode = le16_to_cpu(req->SecurityMode);
Namjae Jeone2f34482021-03-16 10:49:09 +09001191
1192 rsp->StructureSize = cpu_to_le16(65);
1193 rsp->DialectRevision = cpu_to_le16(conn->dialect);
1194 /* Not setting conn guid rsp->ServerGUID, as it
1195 * not used by client for identifying server
1196 */
1197 memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE);
1198
1199 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
1200 rsp->ServerStartTime = 0;
1201 ksmbd_debug(SMB, "negotiate context offset %d, count %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001202 le32_to_cpu(rsp->NegotiateContextOffset),
1203 le16_to_cpu(rsp->NegotiateContextCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09001204
1205 rsp->SecurityBufferOffset = cpu_to_le16(128);
1206 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
Namjae Jeoncb451722021-11-03 08:08:44 +09001207 ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) +
1208 le16_to_cpu(rsp->SecurityBufferOffset));
1209 inc_rfc1001_len(work->response_buf, sizeof(struct smb2_negotiate_rsp) -
Namjae Jeon070fb212021-05-26 17:57:12 +09001210 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
1211 AUTH_GSS_LENGTH);
Namjae Jeone2f34482021-03-16 10:49:09 +09001212 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
1213 conn->use_spnego = true;
1214
1215 if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001216 server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) &&
1217 req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09001218 conn->sign = true;
1219 else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) {
1220 server_conf.enforced_signing = true;
1221 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
1222 conn->sign = true;
1223 }
1224
1225 conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode);
1226 ksmbd_conn_set_need_negotiate(work);
1227
1228err_out:
1229 if (rc < 0)
1230 smb2_set_err_rsp(work);
1231
1232 return rc;
1233}
1234
1235static int alloc_preauth_hash(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09001236 struct ksmbd_conn *conn)
Namjae Jeone2f34482021-03-16 10:49:09 +09001237{
1238 if (sess->Preauth_HashValue)
1239 return 0;
1240
kernel test robot86f52972021-04-02 12:17:24 +09001241 sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue,
Namjae Jeon070fb212021-05-26 17:57:12 +09001242 PREAUTH_HASHVALUE_SIZE, GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09001243 if (!sess->Preauth_HashValue)
1244 return -ENOMEM;
1245
Namjae Jeone2f34482021-03-16 10:49:09 +09001246 return 0;
1247}
1248
1249static int generate_preauth_hash(struct ksmbd_work *work)
1250{
1251 struct ksmbd_conn *conn = work->conn;
1252 struct ksmbd_session *sess = work->sess;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001253 u8 *preauth_hash;
Namjae Jeone2f34482021-03-16 10:49:09 +09001254
1255 if (conn->dialect != SMB311_PROT_ID)
1256 return 0;
1257
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001258 if (conn->binding) {
1259 struct preauth_session *preauth_sess;
1260
1261 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
1262 if (!preauth_sess) {
1263 preauth_sess = ksmbd_preauth_session_alloc(conn, sess->id);
1264 if (!preauth_sess)
1265 return -ENOMEM;
1266 }
1267
1268 preauth_hash = preauth_sess->Preauth_HashValue;
1269 } else {
1270 if (!sess->Preauth_HashValue)
1271 if (alloc_preauth_hash(sess, conn))
1272 return -ENOMEM;
1273 preauth_hash = sess->Preauth_HashValue;
Namjae Jeone2f34482021-03-16 10:49:09 +09001274 }
1275
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001276 ksmbd_gen_preauth_integrity_hash(conn, work->request_buf, preauth_hash);
Namjae Jeone2f34482021-03-16 10:49:09 +09001277 return 0;
1278}
1279
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001280static int decode_negotiation_token(struct ksmbd_conn *conn,
1281 struct negotiate_message *negblob,
1282 size_t sz)
Namjae Jeone2f34482021-03-16 10:49:09 +09001283{
Namjae Jeone2f34482021-03-16 10:49:09 +09001284 if (!conn->use_spnego)
1285 return -EINVAL;
1286
Hyunchul Leefad41612021-04-19 17:26:15 +09001287 if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) {
1288 if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001289 conn->auth_mechs |= KSMBD_AUTH_NTLMSSP;
1290 conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP;
1291 conn->use_spnego = false;
1292 }
1293 }
1294 return 0;
1295}
1296
1297static int ntlm_negotiate(struct ksmbd_work *work,
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001298 struct negotiate_message *negblob,
1299 size_t negblob_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09001300{
Namjae Jeoncb451722021-11-03 08:08:44 +09001301 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001302 struct challenge_message *chgblob;
1303 unsigned char *spnego_blob = NULL;
1304 u16 spnego_blob_len;
1305 char *neg_blob;
1306 int sz, rc;
1307
1308 ksmbd_debug(SMB, "negotiate phase\n");
Namjae Jeonce53d362021-12-15 14:57:27 +09001309 rc = ksmbd_decode_ntlmssp_neg_blob(negblob, negblob_len, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001310 if (rc)
1311 return rc;
1312
1313 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1314 chgblob =
1315 (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz);
1316 memset(chgblob, 0, sizeof(struct challenge_message));
1317
1318 if (!work->conn->use_spnego) {
Namjae Jeonce53d362021-12-15 14:57:27 +09001319 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001320 if (sz < 0)
1321 return -ENOMEM;
1322
1323 rsp->SecurityBufferLength = cpu_to_le16(sz);
1324 return 0;
1325 }
1326
1327 sz = sizeof(struct challenge_message);
1328 sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6;
1329
1330 neg_blob = kzalloc(sz, GFP_KERNEL);
1331 if (!neg_blob)
1332 return -ENOMEM;
1333
1334 chgblob = (struct challenge_message *)neg_blob;
Namjae Jeonce53d362021-12-15 14:57:27 +09001335 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001336 if (sz < 0) {
1337 rc = -ENOMEM;
1338 goto out;
1339 }
1340
Namjae Jeon070fb212021-05-26 17:57:12 +09001341 rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len,
1342 neg_blob, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09001343 if (rc) {
1344 rc = -ENOMEM;
1345 goto out;
1346 }
1347
1348 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1349 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
1350 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1351
1352out:
1353 kfree(spnego_blob);
1354 kfree(neg_blob);
1355 return rc;
1356}
1357
1358static struct authenticate_message *user_authblob(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001359 struct smb2_sess_setup_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +09001360{
1361 int sz;
1362
1363 if (conn->use_spnego && conn->mechToken)
1364 return (struct authenticate_message *)conn->mechToken;
1365
1366 sz = le16_to_cpu(req->SecurityBufferOffset);
1367 return (struct authenticate_message *)((char *)&req->hdr.ProtocolId
1368 + sz);
1369}
1370
1371static struct ksmbd_user *session_user(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001372 struct smb2_sess_setup_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +09001373{
1374 struct authenticate_message *authblob;
1375 struct ksmbd_user *user;
1376 char *name;
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001377 unsigned int auth_msg_len, name_off, name_len, secbuf_len;
Namjae Jeone2f34482021-03-16 10:49:09 +09001378
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001379 secbuf_len = le16_to_cpu(req->SecurityBufferLength);
1380 if (secbuf_len < sizeof(struct authenticate_message)) {
1381 ksmbd_debug(SMB, "blob len %d too small\n", secbuf_len);
1382 return NULL;
1383 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001384 authblob = user_authblob(conn, req);
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001385 name_off = le32_to_cpu(authblob->UserName.BufferOffset);
1386 name_len = le16_to_cpu(authblob->UserName.Length);
1387 auth_msg_len = le16_to_cpu(req->SecurityBufferOffset) + secbuf_len;
1388
1389 if (auth_msg_len < (u64)name_off + name_len)
1390 return NULL;
1391
1392 name = smb_strndup_from_utf16((const char *)authblob + name_off,
1393 name_len,
Namjae Jeone2f34482021-03-16 10:49:09 +09001394 true,
1395 conn->local_nls);
1396 if (IS_ERR(name)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001397 pr_err("cannot allocate memory\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001398 return NULL;
1399 }
1400
1401 ksmbd_debug(SMB, "session setup request for user %s\n", name);
1402 user = ksmbd_login_user(name);
1403 kfree(name);
1404 return user;
1405}
1406
1407static int ntlm_authenticate(struct ksmbd_work *work)
1408{
Namjae Jeoncb451722021-11-03 08:08:44 +09001409 struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf);
1410 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001411 struct ksmbd_conn *conn = work->conn;
1412 struct ksmbd_session *sess = work->sess;
1413 struct channel *chann = NULL;
1414 struct ksmbd_user *user;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001415 u64 prev_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09001416 int sz, rc;
1417
1418 ksmbd_debug(SMB, "authenticate phase\n");
1419 if (conn->use_spnego) {
1420 unsigned char *spnego_blob;
1421 u16 spnego_blob_len;
1422
1423 rc = build_spnego_ntlmssp_auth_blob(&spnego_blob,
1424 &spnego_blob_len,
1425 0);
1426 if (rc)
1427 return -ENOMEM;
1428
1429 sz = le16_to_cpu(rsp->SecurityBufferOffset);
Namjae Jeon64b39f42021-03-30 14:25:35 +09001430 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001431 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1432 kfree(spnego_blob);
Namjae Jeoncb451722021-11-03 08:08:44 +09001433 inc_rfc1001_len(work->response_buf, spnego_blob_len - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001434 }
1435
1436 user = session_user(conn, req);
1437 if (!user) {
1438 ksmbd_debug(SMB, "Unknown user name or an error\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001439 return -EPERM;
Namjae Jeone2f34482021-03-16 10:49:09 +09001440 }
1441
1442 /* Check for previous session */
1443 prev_id = le64_to_cpu(req->PreviousSessionId);
1444 if (prev_id && prev_id != sess->id)
1445 destroy_previous_session(user, prev_id);
1446
1447 if (sess->state == SMB2_SESSION_VALID) {
1448 /*
1449 * Reuse session if anonymous try to connect
1450 * on reauthetication.
1451 */
1452 if (ksmbd_anonymous_user(user)) {
1453 ksmbd_free_user(user);
1454 return 0;
1455 }
Namjae Jeona58b45a2021-12-16 10:26:43 +09001456
1457 if (!ksmbd_compare_user(sess->user, user)) {
1458 ksmbd_free_user(user);
1459 return -EPERM;
1460 }
1461 ksmbd_free_user(user);
1462 } else {
1463 sess->user = user;
Namjae Jeone2f34482021-03-16 10:49:09 +09001464 }
1465
Namjae Jeone2f34482021-03-16 10:49:09 +09001466 if (user_guest(sess->user)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001467 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 }
Namjae Jeonac090d92022-01-17 22:16:01 +09001479 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001480
Namjae Jeonac090d92022-01-17 22:16:01 +09001481 /*
1482 * If session state is SMB2_SESSION_VALID, We can assume
1483 * that it is reauthentication. And the user/password
1484 * has been verified, so return it here.
1485 */
1486 if (sess->state == SMB2_SESSION_VALID) {
1487 if (conn->binding)
1488 goto binding_session;
1489 return 0;
1490 }
1491
1492 if ((rsp->SessionFlags != SMB2_SESSION_FLAG_IS_GUEST_LE &&
1493 (conn->sign || server_conf.enforced_signing)) ||
1494 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
1495 sess->sign = true;
1496
1497 if (smb3_encryption_negotiated(conn) &&
1498 !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
1499 rc = conn->ops->generate_encryptionkey(sess);
1500 if (rc) {
1501 ksmbd_debug(SMB,
1502 "SMB3 encryption key generation failed\n");
1503 return -EINVAL;
1504 }
1505 sess->enc = true;
1506 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09001507 /*
Namjae Jeonac090d92022-01-17 22:16:01 +09001508 * signing is disable if encryption is enable
1509 * on this session
Namjae Jeone2f34482021-03-16 10:49:09 +09001510 */
Namjae Jeonac090d92022-01-17 22:16:01 +09001511 sess->sign = false;
Namjae Jeone2f34482021-03-16 10:49:09 +09001512 }
1513
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001514binding_session:
Namjae Jeone2f34482021-03-16 10:49:09 +09001515 if (conn->dialect >= SMB30_PROT_ID) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001516 chann = lookup_chann_list(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001517 if (!chann) {
1518 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1519 if (!chann)
1520 return -ENOMEM;
1521
1522 chann->conn = conn;
1523 INIT_LIST_HEAD(&chann->chann_list);
1524 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1525 }
1526 }
1527
1528 if (conn->ops->generate_signingkey) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001529 rc = conn->ops->generate_signingkey(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001530 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001531 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001532 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001533 }
1534 }
1535
Namjae Jeon51a13872021-09-29 13:09:24 +09001536 if (!ksmbd_conn_lookup_dialect(conn)) {
1537 pr_err("fail to verify the dialect\n");
1538 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001539 }
1540 return 0;
1541}
1542
1543#ifdef CONFIG_SMB_SERVER_KERBEROS5
1544static int krb5_authenticate(struct ksmbd_work *work)
1545{
Namjae Jeoncb451722021-11-03 08:08:44 +09001546 struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf);
1547 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001548 struct ksmbd_conn *conn = work->conn;
1549 struct ksmbd_session *sess = work->sess;
1550 char *in_blob, *out_blob;
1551 struct channel *chann = NULL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001552 u64 prev_sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09001553 int in_len, out_len;
1554 int retval;
1555
1556 in_blob = (char *)&req->hdr.ProtocolId +
1557 le16_to_cpu(req->SecurityBufferOffset);
1558 in_len = le16_to_cpu(req->SecurityBufferLength);
1559 out_blob = (char *)&rsp->hdr.ProtocolId +
1560 le16_to_cpu(rsp->SecurityBufferOffset);
1561 out_len = work->response_sz -
Namjae Jeoncb451722021-11-03 08:08:44 +09001562 (le16_to_cpu(rsp->SecurityBufferOffset) + 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09001563
1564 /* Check previous session */
1565 prev_sess_id = le64_to_cpu(req->PreviousSessionId);
1566 if (prev_sess_id && prev_sess_id != sess->id)
1567 destroy_previous_session(sess->user, prev_sess_id);
1568
1569 if (sess->state == SMB2_SESSION_VALID)
1570 ksmbd_free_user(sess->user);
1571
1572 retval = ksmbd_krb5_authenticate(sess, in_blob, in_len,
Namjae Jeon070fb212021-05-26 17:57:12 +09001573 out_blob, &out_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001574 if (retval) {
1575 ksmbd_debug(SMB, "krb5 authentication failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001576 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001577 }
1578 rsp->SecurityBufferLength = cpu_to_le16(out_len);
Namjae Jeoncb451722021-11-03 08:08:44 +09001579 inc_rfc1001_len(work->response_buf, out_len - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001580
1581 if ((conn->sign || server_conf.enforced_signing) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001582 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
Namjae Jeone2f34482021-03-16 10:49:09 +09001583 sess->sign = true;
1584
Marcos Del Sol Vives83912d62021-12-16 11:37:22 +01001585 if (smb3_encryption_negotiated(conn)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001586 retval = conn->ops->generate_encryptionkey(sess);
1587 if (retval) {
1588 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001589 "SMB3 encryption key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001590 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001591 }
1592 sess->enc = true;
1593 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1594 sess->sign = false;
1595 }
1596
1597 if (conn->dialect >= SMB30_PROT_ID) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001598 chann = lookup_chann_list(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001599 if (!chann) {
1600 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1601 if (!chann)
1602 return -ENOMEM;
1603
1604 chann->conn = conn;
1605 INIT_LIST_HEAD(&chann->chann_list);
1606 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1607 }
1608 }
1609
1610 if (conn->ops->generate_signingkey) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001611 retval = conn->ops->generate_signingkey(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001612 if (retval) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001613 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001614 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001615 }
1616 }
1617
Namjae Jeon51a13872021-09-29 13:09:24 +09001618 if (!ksmbd_conn_lookup_dialect(conn)) {
1619 pr_err("fail to verify the dialect\n");
1620 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001621 }
1622 return 0;
1623}
1624#else
1625static int krb5_authenticate(struct ksmbd_work *work)
1626{
1627 return -EOPNOTSUPP;
1628}
1629#endif
1630
1631int smb2_sess_setup(struct ksmbd_work *work)
1632{
1633 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09001634 struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf);
1635 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001636 struct ksmbd_session *sess;
1637 struct negotiate_message *negblob;
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001638 unsigned int negblob_len, negblob_off;
Namjae Jeone2f34482021-03-16 10:49:09 +09001639 int rc = 0;
1640
1641 ksmbd_debug(SMB, "Received request for session setup\n");
1642
1643 rsp->StructureSize = cpu_to_le16(9);
1644 rsp->SessionFlags = 0;
1645 rsp->SecurityBufferOffset = cpu_to_le16(72);
1646 rsp->SecurityBufferLength = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09001647 inc_rfc1001_len(work->response_buf, 9);
Namjae Jeone2f34482021-03-16 10:49:09 +09001648
1649 if (!req->hdr.SessionId) {
1650 sess = ksmbd_smb2_session_create();
1651 if (!sess) {
1652 rc = -ENOMEM;
1653 goto out_err;
1654 }
1655 rsp->hdr.SessionId = cpu_to_le64(sess->id);
1656 ksmbd_session_register(conn, sess);
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001657 } else if (conn->dialect >= SMB30_PROT_ID &&
1658 (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
1659 req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) {
1660 u64 sess_id = le64_to_cpu(req->hdr.SessionId);
1661
1662 sess = ksmbd_session_lookup_slowpath(sess_id);
1663 if (!sess) {
1664 rc = -ENOENT;
1665 goto out_err;
1666 }
1667
1668 if (conn->dialect != sess->conn->dialect) {
1669 rc = -EINVAL;
1670 goto out_err;
1671 }
1672
1673 if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) {
1674 rc = -EINVAL;
1675 goto out_err;
1676 }
1677
1678 if (strncmp(conn->ClientGUID, sess->conn->ClientGUID,
1679 SMB2_CLIENT_GUID_SIZE)) {
1680 rc = -ENOENT;
1681 goto out_err;
1682 }
1683
1684 if (sess->state == SMB2_SESSION_IN_PROGRESS) {
1685 rc = -EACCES;
1686 goto out_err;
1687 }
1688
1689 if (sess->state == SMB2_SESSION_EXPIRED) {
1690 rc = -EFAULT;
1691 goto out_err;
1692 }
1693
1694 if (ksmbd_session_lookup(conn, sess_id)) {
1695 rc = -EACCES;
1696 goto out_err;
1697 }
1698
1699 conn->binding = true;
1700 } else if ((conn->dialect < SMB30_PROT_ID ||
1701 server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
1702 (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
Colin Ian King4951a842021-07-06 13:05:01 +01001703 sess = NULL;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001704 rc = -EACCES;
1705 goto out_err;
Namjae Jeone2f34482021-03-16 10:49:09 +09001706 } else {
1707 sess = ksmbd_session_lookup(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001708 le64_to_cpu(req->hdr.SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09001709 if (!sess) {
1710 rc = -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001711 goto out_err;
1712 }
1713 }
1714 work->sess = sess;
1715
1716 if (sess->state == SMB2_SESSION_EXPIRED)
1717 sess->state = SMB2_SESSION_IN_PROGRESS;
1718
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001719 negblob_off = le16_to_cpu(req->SecurityBufferOffset);
1720 negblob_len = le16_to_cpu(req->SecurityBufferLength);
Namjae Jeoncb451722021-11-03 08:08:44 +09001721 if (negblob_off < offsetof(struct smb2_sess_setup_req, Buffer) ||
Christophe JAILLETf8fbfd82021-11-07 16:22:57 +01001722 negblob_len < offsetof(struct negotiate_message, NegotiateFlags)) {
1723 rc = -EINVAL;
1724 goto out_err;
1725 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001726
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001727 negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId +
1728 negblob_off);
1729
1730 if (decode_negotiation_token(conn, negblob, negblob_len) == 0) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001731 if (conn->mechToken)
1732 negblob = (struct negotiate_message *)conn->mechToken;
1733 }
1734
1735 if (server_conf.auth_mechs & conn->auth_mechs) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001736 rc = generate_preauth_hash(work);
1737 if (rc)
1738 goto out_err;
1739
Namjae Jeone2f34482021-03-16 10:49:09 +09001740 if (conn->preferred_auth_mech &
1741 (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001742 rc = krb5_authenticate(work);
1743 if (rc) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001744 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001745 goto out_err;
1746 }
1747
1748 ksmbd_conn_set_good(work);
1749 sess->state = SMB2_SESSION_VALID;
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001750 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001751 sess->Preauth_HashValue = NULL;
1752 } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001753 if (negblob->MessageType == NtLmNegotiate) {
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001754 rc = ntlm_negotiate(work, negblob, negblob_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001755 if (rc)
1756 goto out_err;
1757 rsp->hdr.Status =
1758 STATUS_MORE_PROCESSING_REQUIRED;
1759 /*
1760 * Note: here total size -1 is done as an
1761 * adjustment for 0 size blob
1762 */
Namjae Jeoncb451722021-11-03 08:08:44 +09001763 inc_rfc1001_len(work->response_buf,
1764 le16_to_cpu(rsp->SecurityBufferLength) - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001765
1766 } else if (negblob->MessageType == NtLmAuthenticate) {
1767 rc = ntlm_authenticate(work);
1768 if (rc)
1769 goto out_err;
1770
1771 ksmbd_conn_set_good(work);
1772 sess->state = SMB2_SESSION_VALID;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001773 if (conn->binding) {
1774 struct preauth_session *preauth_sess;
1775
1776 preauth_sess =
1777 ksmbd_preauth_session_lookup(conn, sess->id);
1778 if (preauth_sess) {
1779 list_del(&preauth_sess->preauth_entry);
1780 kfree(preauth_sess);
1781 }
1782 }
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001783 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001784 sess->Preauth_HashValue = NULL;
1785 }
1786 } else {
1787 /* TODO: need one more negotiation */
Namjae Jeonbde16942021-06-28 15:23:19 +09001788 pr_err("Not support the preferred authentication\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001789 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001790 }
1791 } else {
Namjae Jeonbde16942021-06-28 15:23:19 +09001792 pr_err("Not support authentication\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001793 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001794 }
1795
1796out_err:
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001797 if (rc == -EINVAL)
1798 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1799 else if (rc == -ENOENT)
1800 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1801 else if (rc == -EACCES)
1802 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
1803 else if (rc == -EFAULT)
1804 rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED;
Namjae Jeon58090b12021-07-16 14:52:09 +09001805 else if (rc == -ENOMEM)
1806 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001807 else if (rc)
1808 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1809
Namjae Jeone2f34482021-03-16 10:49:09 +09001810 if (conn->use_spnego && conn->mechToken) {
1811 kfree(conn->mechToken);
1812 conn->mechToken = NULL;
1813 }
1814
Namjae Jeon621be842021-10-13 17:28:31 +09001815 if (rc < 0) {
1816 /*
1817 * SecurityBufferOffset should be set to zero
1818 * in session setup error response.
1819 */
1820 rsp->SecurityBufferOffset = 0;
1821
1822 if (sess) {
1823 bool try_delay = false;
1824
1825 /*
1826 * To avoid dictionary attacks (repeated session setups rapidly sent) to
1827 * connect to server, ksmbd make a delay of a 5 seconds on session setup
1828 * failure to make it harder to send enough random connection requests
1829 * to break into a server.
1830 */
1831 if (sess->user && sess->user->flags & KSMBD_USER_FLAG_DELAY_SESSION)
1832 try_delay = true;
1833
1834 ksmbd_session_destroy(sess);
1835 work->sess = NULL;
1836 if (try_delay)
1837 ssleep(5);
1838 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001839 }
1840
1841 return rc;
1842}
1843
1844/**
1845 * smb2_tree_connect() - handler for smb2 tree connect command
1846 * @work: smb work containing smb request buffer
1847 *
1848 * Return: 0 on success, otherwise error
1849 */
1850int smb2_tree_connect(struct ksmbd_work *work)
1851{
1852 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09001853 struct smb2_tree_connect_req *req = smb2_get_msg(work->request_buf);
1854 struct smb2_tree_connect_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001855 struct ksmbd_session *sess = work->sess;
1856 char *treename = NULL, *name = NULL;
1857 struct ksmbd_tree_conn_status status;
1858 struct ksmbd_share_config *share;
1859 int rc = -EINVAL;
1860
1861 treename = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09001862 le16_to_cpu(req->PathLength), true,
1863 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09001864 if (IS_ERR(treename)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001865 pr_err("treename is NULL\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001866 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1867 goto out_err1;
1868 }
1869
Stephen Rothwell36ba3862021-03-17 17:01:15 +09001870 name = ksmbd_extract_sharename(treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001871 if (IS_ERR(name)) {
1872 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1873 goto out_err1;
1874 }
1875
1876 ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001877 name, treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001878
1879 status = ksmbd_tree_conn_connect(sess, name);
1880 if (status.ret == KSMBD_TREE_CONN_STATUS_OK)
1881 rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id);
1882 else
1883 goto out_err1;
1884
1885 share = status.tree_conn->share_conf;
1886 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
1887 ksmbd_debug(SMB, "IPC share path request\n");
1888 rsp->ShareType = SMB2_SHARE_TYPE_PIPE;
1889 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1890 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE |
1891 FILE_DELETE_LE | FILE_READ_CONTROL_LE |
1892 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1893 FILE_SYNCHRONIZE_LE;
1894 } else {
1895 rsp->ShareType = SMB2_SHARE_TYPE_DISK;
1896 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1897 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE;
1898 if (test_tree_conn_flag(status.tree_conn,
1899 KSMBD_TREE_CONN_FLAG_WRITABLE)) {
1900 rsp->MaximalAccess |= FILE_WRITE_DATA_LE |
1901 FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE |
Wan Jiabing3aefd542021-06-07 12:54:32 +08001902 FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE |
1903 FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE |
1904 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1905 FILE_SYNCHRONIZE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09001906 }
1907 }
1908
1909 status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1910 if (conn->posix_ext_supported)
1911 status.tree_conn->posix_extensions = true;
1912
1913out_err1:
1914 rsp->StructureSize = cpu_to_le16(16);
1915 rsp->Capabilities = 0;
1916 rsp->Reserved = 0;
1917 /* default manual caching */
1918 rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING;
Namjae Jeoncb451722021-11-03 08:08:44 +09001919 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09001920
1921 if (!IS_ERR(treename))
1922 kfree(treename);
1923 if (!IS_ERR(name))
1924 kfree(name);
1925
1926 switch (status.ret) {
1927 case KSMBD_TREE_CONN_STATUS_OK:
1928 rsp->hdr.Status = STATUS_SUCCESS;
1929 rc = 0;
1930 break;
1931 case KSMBD_TREE_CONN_STATUS_NO_SHARE:
1932 rsp->hdr.Status = STATUS_BAD_NETWORK_PATH;
1933 break;
1934 case -ENOMEM:
1935 case KSMBD_TREE_CONN_STATUS_NOMEM:
1936 rsp->hdr.Status = STATUS_NO_MEMORY;
1937 break;
1938 case KSMBD_TREE_CONN_STATUS_ERROR:
1939 case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS:
1940 case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS:
1941 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1942 break;
1943 case -EINVAL:
1944 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1945 break;
1946 default:
1947 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1948 }
1949
1950 return rc;
1951}
1952
1953/**
1954 * smb2_create_open_flags() - convert smb open flags to unix open flags
1955 * @file_present: is file already present
1956 * @access: file access flags
1957 * @disposition: file disposition flags
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001958 * @may_flags: set with MAY_ flags
Namjae Jeone2f34482021-03-16 10:49:09 +09001959 *
1960 * Return: file open flags
1961 */
1962static int smb2_create_open_flags(bool file_present, __le32 access,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001963 __le32 disposition,
1964 int *may_flags)
Namjae Jeone2f34482021-03-16 10:49:09 +09001965{
1966 int oflags = O_NONBLOCK | O_LARGEFILE;
1967
1968 if (access & FILE_READ_DESIRED_ACCESS_LE &&
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001969 access & FILE_WRITE_DESIRE_ACCESS_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001970 oflags |= O_RDWR;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001971 *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE;
1972 } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001973 oflags |= O_WRONLY;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001974 *may_flags = MAY_OPEN | MAY_WRITE;
1975 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09001976 oflags |= O_RDONLY;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001977 *may_flags = MAY_OPEN | MAY_READ;
1978 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001979
1980 if (access == FILE_READ_ATTRIBUTES_LE)
1981 oflags |= O_PATH;
1982
1983 if (file_present) {
1984 switch (disposition & FILE_CREATE_MASK_LE) {
1985 case FILE_OPEN_LE:
1986 case FILE_CREATE_LE:
1987 break;
1988 case FILE_SUPERSEDE_LE:
1989 case FILE_OVERWRITE_LE:
1990 case FILE_OVERWRITE_IF_LE:
1991 oflags |= O_TRUNC;
1992 break;
1993 default:
1994 break;
1995 }
1996 } else {
1997 switch (disposition & FILE_CREATE_MASK_LE) {
1998 case FILE_SUPERSEDE_LE:
1999 case FILE_CREATE_LE:
2000 case FILE_OPEN_IF_LE:
2001 case FILE_OVERWRITE_IF_LE:
2002 oflags |= O_CREAT;
2003 break;
2004 case FILE_OPEN_LE:
2005 case FILE_OVERWRITE_LE:
2006 oflags &= ~O_CREAT;
2007 break;
2008 default:
2009 break;
2010 }
2011 }
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002012
Namjae Jeone2f34482021-03-16 10:49:09 +09002013 return oflags;
2014}
2015
2016/**
2017 * smb2_tree_disconnect() - handler for smb tree connect request
2018 * @work: smb work containing request buffer
2019 *
2020 * Return: 0
2021 */
2022int smb2_tree_disconnect(struct ksmbd_work *work)
2023{
Namjae Jeoncb451722021-11-03 08:08:44 +09002024 struct smb2_tree_disconnect_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002025 struct ksmbd_session *sess = work->sess;
2026 struct ksmbd_tree_connect *tcon = work->tcon;
2027
2028 rsp->StructureSize = cpu_to_le16(4);
Namjae Jeoncb451722021-11-03 08:08:44 +09002029 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09002030
2031 ksmbd_debug(SMB, "request\n");
2032
2033 if (!tcon) {
Namjae Jeoncb451722021-11-03 08:08:44 +09002034 struct smb2_tree_disconnect_req *req =
2035 smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002036
2037 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2038 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2039 smb2_set_err_rsp(work);
2040 return 0;
2041 }
2042
2043 ksmbd_close_tree_conn_fds(work);
2044 ksmbd_tree_conn_disconnect(sess, tcon);
2045 return 0;
2046}
2047
2048/**
2049 * smb2_session_logoff() - handler for session log off request
2050 * @work: smb work containing request buffer
2051 *
2052 * Return: 0
2053 */
2054int smb2_session_logoff(struct ksmbd_work *work)
2055{
2056 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09002057 struct smb2_logoff_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002058 struct ksmbd_session *sess = work->sess;
2059
2060 rsp->StructureSize = cpu_to_le16(4);
Namjae Jeoncb451722021-11-03 08:08:44 +09002061 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09002062
2063 ksmbd_debug(SMB, "request\n");
2064
Namjae Jeone2f34482021-03-16 10:49:09 +09002065 /* setting CifsExiting here may race with start_tcp_sess */
2066 ksmbd_conn_set_need_reconnect(work);
2067 ksmbd_close_session_fds(work);
2068 ksmbd_conn_wait_idle(conn);
2069
2070 if (ksmbd_tree_conn_session_logoff(sess)) {
Namjae Jeoncb451722021-11-03 08:08:44 +09002071 struct smb2_logoff_req *req = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002072
2073 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2074 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2075 smb2_set_err_rsp(work);
2076 return 0;
2077 }
2078
2079 ksmbd_destroy_file_table(&sess->file_table);
2080 sess->state = SMB2_SESSION_EXPIRED;
2081
2082 ksmbd_free_user(sess->user);
2083 sess->user = NULL;
2084
2085 /* let start_tcp_sess free connection info now */
2086 ksmbd_conn_set_need_negotiate(work);
2087 return 0;
2088}
2089
2090/**
2091 * create_smb2_pipe() - create IPC pipe
2092 * @work: smb work containing request buffer
2093 *
2094 * Return: 0 on success, otherwise error
2095 */
2096static noinline int create_smb2_pipe(struct ksmbd_work *work)
2097{
Namjae Jeoncb451722021-11-03 08:08:44 +09002098 struct smb2_create_rsp *rsp = smb2_get_msg(work->response_buf);
2099 struct smb2_create_req *req = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002100 int id;
2101 int err;
2102 char *name;
2103
2104 name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09002105 1, work->conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09002106 if (IS_ERR(name)) {
2107 rsp->hdr.Status = STATUS_NO_MEMORY;
2108 err = PTR_ERR(name);
2109 goto out;
2110 }
2111
2112 id = ksmbd_session_rpc_open(work->sess, name);
Marios Makassikis79caa962021-05-06 11:38:35 +09002113 if (id < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002114 pr_err("Unable to open RPC pipe: %d\n", id);
Marios Makassikis79caa962021-05-06 11:38:35 +09002115 err = id;
2116 goto out;
2117 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002118
Marios Makassikis79caa962021-05-06 11:38:35 +09002119 rsp->hdr.Status = STATUS_SUCCESS;
Namjae Jeone2f34482021-03-16 10:49:09 +09002120 rsp->StructureSize = cpu_to_le16(89);
2121 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002122 rsp->Flags = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002123 rsp->CreateAction = cpu_to_le32(FILE_OPENED);
2124
2125 rsp->CreationTime = cpu_to_le64(0);
2126 rsp->LastAccessTime = cpu_to_le64(0);
2127 rsp->ChangeTime = cpu_to_le64(0);
2128 rsp->AllocationSize = cpu_to_le64(0);
2129 rsp->EndofFile = cpu_to_le64(0);
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002130 rsp->FileAttributes = FILE_ATTRIBUTE_NORMAL_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09002131 rsp->Reserved2 = 0;
2132 rsp->VolatileFileId = cpu_to_le64(id);
2133 rsp->PersistentFileId = 0;
2134 rsp->CreateContextsOffset = 0;
2135 rsp->CreateContextsLength = 0;
2136
Namjae Jeoncb451722021-11-03 08:08:44 +09002137 inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/
Namjae Jeone2f34482021-03-16 10:49:09 +09002138 kfree(name);
2139 return 0;
2140
2141out:
Marios Makassikis79caa962021-05-06 11:38:35 +09002142 switch (err) {
2143 case -EINVAL:
2144 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2145 break;
2146 case -ENOSPC:
2147 case -ENOMEM:
2148 rsp->hdr.Status = STATUS_NO_MEMORY;
2149 break;
2150 }
2151
2152 if (!IS_ERR(name))
2153 kfree(name);
2154
Namjae Jeone2f34482021-03-16 10:49:09 +09002155 smb2_set_err_rsp(work);
2156 return err;
2157}
2158
Namjae Jeone2f34482021-03-16 10:49:09 +09002159/**
2160 * smb2_set_ea() - handler for setting extended attributes using set
2161 * info command
2162 * @eabuf: set info command buffer
Namjae Jeon9496e262021-09-29 15:41:48 +09002163 * @buf_len: set info command buffer length
Namjae Jeone2f34482021-03-16 10:49:09 +09002164 * @path: dentry path for get ea
2165 *
2166 * Return: 0 on success, otherwise error
2167 */
Namjae Jeon9496e262021-09-29 15:41:48 +09002168static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len,
2169 struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002170{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002171 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002172 char *attr_name = NULL, *value;
2173 int rc = 0;
Namjae Jeon9496e262021-09-29 15:41:48 +09002174 unsigned int next = 0;
2175
2176 if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength +
2177 le16_to_cpu(eabuf->EaValueLength))
2178 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002179
2180 attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL);
2181 if (!attr_name)
2182 return -ENOMEM;
2183
2184 do {
2185 if (!eabuf->EaNameLength)
2186 goto next;
2187
2188 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002189 "name : <%s>, name_len : %u, value_len : %u, next : %u\n",
2190 eabuf->name, eabuf->EaNameLength,
2191 le16_to_cpu(eabuf->EaValueLength),
2192 le32_to_cpu(eabuf->NextEntryOffset));
Namjae Jeone2f34482021-03-16 10:49:09 +09002193
2194 if (eabuf->EaNameLength >
Namjae Jeon070fb212021-05-26 17:57:12 +09002195 (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002196 rc = -EINVAL;
2197 break;
2198 }
2199
2200 memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2201 memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002202 eabuf->EaNameLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09002203 attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0';
2204 value = (char *)&eabuf->name + eabuf->EaNameLength + 1;
2205
2206 if (!eabuf->EaValueLength) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002207 rc = ksmbd_vfs_casexattr_len(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002208 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002209 attr_name,
2210 XATTR_USER_PREFIX_LEN +
2211 eabuf->EaNameLength);
2212
2213 /* delete the EA only when it exits */
2214 if (rc > 0) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002215 rc = ksmbd_vfs_remove_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002216 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002217 attr_name);
2218
2219 if (rc < 0) {
2220 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002221 "remove xattr failed(%d)\n",
2222 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002223 break;
2224 }
2225 }
2226
2227 /* if the EA doesn't exist, just do nothing. */
2228 rc = 0;
2229 } else {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002230 rc = ksmbd_vfs_setxattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002231 path->dentry, attr_name, value,
Namjae Jeon070fb212021-05-26 17:57:12 +09002232 le16_to_cpu(eabuf->EaValueLength), 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002233 if (rc < 0) {
2234 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002235 "ksmbd_vfs_setxattr is failed(%d)\n",
2236 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002237 break;
2238 }
2239 }
2240
2241next:
2242 next = le32_to_cpu(eabuf->NextEntryOffset);
Namjae Jeon9496e262021-09-29 15:41:48 +09002243 if (next == 0 || buf_len < next)
2244 break;
2245 buf_len -= next;
Namjae Jeone2f34482021-03-16 10:49:09 +09002246 eabuf = (struct smb2_ea_info *)((char *)eabuf + next);
Namjae Jeon9496e262021-09-29 15:41:48 +09002247 if (next < (u32)eabuf->EaNameLength + le16_to_cpu(eabuf->EaValueLength))
2248 break;
2249
Namjae Jeone2f34482021-03-16 10:49:09 +09002250 } while (next != 0);
2251
2252 kfree(attr_name);
2253 return rc;
2254}
2255
Namjae Jeone2f34482021-03-16 10:49:09 +09002256static noinline int smb2_set_stream_name_xattr(struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002257 struct ksmbd_file *fp,
2258 char *stream_name, int s_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09002259{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002260 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002261 size_t xattr_stream_size;
2262 char *xattr_stream_name;
2263 int rc;
2264
2265 rc = ksmbd_vfs_xattr_stream_name(stream_name,
2266 &xattr_stream_name,
2267 &xattr_stream_size,
2268 s_type);
2269 if (rc)
2270 return rc;
2271
2272 fp->stream.name = xattr_stream_name;
2273 fp->stream.size = xattr_stream_size;
2274
2275 /* Check if there is stream prefix in xattr space */
Hyunchul Lee465d7202021-07-03 12:10:36 +09002276 rc = ksmbd_vfs_casexattr_len(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002277 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002278 xattr_stream_name,
2279 xattr_stream_size);
2280 if (rc >= 0)
2281 return 0;
2282
2283 if (fp->cdoption == FILE_OPEN_LE) {
2284 ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc);
2285 return -EBADF;
2286 }
2287
Hyunchul Lee465d7202021-07-03 12:10:36 +09002288 rc = ksmbd_vfs_setxattr(user_ns, path->dentry,
2289 xattr_stream_name, NULL, 0, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002290 if (rc < 0)
Namjae Jeonbde16942021-06-28 15:23:19 +09002291 pr_err("Failed to store XATTR stream name :%d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002292 return 0;
2293}
2294
Hyunchul Leeef24c962021-06-30 18:25:52 +09002295static int smb2_remove_smb_xattrs(struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002296{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002297 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002298 char *name, *xattr_list = NULL;
2299 ssize_t xattr_list_len;
2300 int err = 0;
2301
Hyunchul Leeef24c962021-06-30 18:25:52 +09002302 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002303 if (xattr_list_len < 0) {
2304 goto out;
2305 } else if (!xattr_list_len) {
2306 ksmbd_debug(SMB, "empty xattr in the file\n");
2307 goto out;
2308 }
2309
2310 for (name = xattr_list; name - xattr_list < xattr_list_len;
2311 name += strlen(name) + 1) {
2312 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
2313
2314 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002315 strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX,
2316 DOS_ATTRIBUTE_PREFIX_LEN) &&
2317 strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09002318 continue;
2319
Hyunchul Lee465d7202021-07-03 12:10:36 +09002320 err = ksmbd_vfs_remove_xattr(user_ns, path->dentry, name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002321 if (err)
2322 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
2323 }
2324out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09002325 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002326 return err;
2327}
2328
2329static int smb2_create_truncate(struct path *path)
2330{
2331 int rc = vfs_truncate(path, 0);
2332
2333 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002334 pr_err("vfs_truncate failed, rc %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002335 return rc;
2336 }
2337
Hyunchul Leeef24c962021-06-30 18:25:52 +09002338 rc = smb2_remove_smb_xattrs(path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002339 if (rc == -EOPNOTSUPP)
2340 rc = 0;
2341 if (rc)
2342 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002343 "ksmbd_truncate_stream_name_xattr failed, rc %d\n",
2344 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002345 return rc;
2346}
2347
Namjae Jeon64b39f42021-03-30 14:25:35 +09002348static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002349 struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002350{
2351 struct xattr_dos_attrib da = {0};
2352 int rc;
2353
2354 if (!test_share_config_flag(tcon->share_conf,
2355 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
2356 return;
2357
2358 da.version = 4;
2359 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
2360 da.itime = da.create_time = fp->create_time;
2361 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
2362 XATTR_DOSINFO_ITIME;
2363
Hyunchul Leeaf349832021-06-30 18:25:53 +09002364 rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt),
2365 path->dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09002366 if (rc)
2367 ksmbd_debug(SMB, "failed to store file attribute into xattr\n");
2368}
2369
2370static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon,
Namjae Jeon070fb212021-05-26 17:57:12 +09002371 struct path *path, struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002372{
2373 struct xattr_dos_attrib da;
2374 int rc;
2375
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002376 fp->f_ci->m_fattr &= ~(FILE_ATTRIBUTE_HIDDEN_LE | FILE_ATTRIBUTE_SYSTEM_LE);
Namjae Jeone2f34482021-03-16 10:49:09 +09002377
2378 /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */
2379 if (!test_share_config_flag(tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002380 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
Namjae Jeone2f34482021-03-16 10:49:09 +09002381 return;
2382
Hyunchul Leeaf349832021-06-30 18:25:53 +09002383 rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_user_ns(path->mnt),
2384 path->dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09002385 if (rc > 0) {
2386 fp->f_ci->m_fattr = cpu_to_le32(da.attr);
2387 fp->create_time = da.create_time;
2388 fp->itime = da.itime;
2389 }
2390}
2391
Namjae Jeon64b39f42021-03-30 14:25:35 +09002392static int smb2_creat(struct ksmbd_work *work, struct path *path, char *name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002393 int open_flags, umode_t posix_mode, bool is_dir)
Namjae Jeone2f34482021-03-16 10:49:09 +09002394{
2395 struct ksmbd_tree_connect *tcon = work->tcon;
2396 struct ksmbd_share_config *share = tcon->share_conf;
2397 umode_t mode;
2398 int rc;
2399
2400 if (!(open_flags & O_CREAT))
2401 return -EBADF;
2402
2403 ksmbd_debug(SMB, "file does not exist, so creating\n");
2404 if (is_dir == true) {
2405 ksmbd_debug(SMB, "creating directory\n");
2406
2407 mode = share_config_directory_mode(share, posix_mode);
2408 rc = ksmbd_vfs_mkdir(work, name, mode);
2409 if (rc)
2410 return rc;
2411 } else {
2412 ksmbd_debug(SMB, "creating regular file\n");
2413
2414 mode = share_config_create_mode(share, posix_mode);
2415 rc = ksmbd_vfs_create(work, name, mode);
2416 if (rc)
2417 return rc;
2418 }
2419
Hyunchul Lee265fd192021-09-25 00:06:16 +09002420 rc = ksmbd_vfs_kern_path(work, name, 0, path, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002421 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002422 pr_err("cannot get linux path (%s), err = %d\n",
2423 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002424 return rc;
2425 }
2426 return 0;
2427}
2428
2429static int smb2_create_sd_buffer(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09002430 struct smb2_create_req *req,
Hyunchul Leeef24c962021-06-30 18:25:52 +09002431 struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002432{
2433 struct create_context *context;
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002434 struct create_sd_buf_req *sd_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09002435
2436 if (!req->CreateContextsOffset)
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002437 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09002438
2439 /* Parse SD BUFFER create contexts */
2440 context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER);
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002441 if (!context)
2442 return -ENOENT;
2443 else if (IS_ERR(context))
2444 return PTR_ERR(context);
Namjae Jeone2f34482021-03-16 10:49:09 +09002445
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002446 ksmbd_debug(SMB,
2447 "Set ACLs using SMB2_CREATE_SD_BUFFER context\n");
2448 sd_buf = (struct create_sd_buf_req *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002449 if (le16_to_cpu(context->DataOffset) +
2450 le32_to_cpu(context->DataLength) <
2451 sizeof(struct create_sd_buf_req))
2452 return -EINVAL;
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002453 return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd,
2454 le32_to_cpu(sd_buf->ccontext.DataLength), true);
Namjae Jeone2f34482021-03-16 10:49:09 +09002455}
2456
Christian Brauner43205ca2021-08-23 17:13:50 +02002457static void ksmbd_acls_fattr(struct smb_fattr *fattr,
2458 struct user_namespace *mnt_userns,
2459 struct inode *inode)
Namjae Jeon3d47e542021-04-20 14:25:35 +09002460{
Christian Brauner43205ca2021-08-23 17:13:50 +02002461 fattr->cf_uid = i_uid_into_mnt(mnt_userns, inode);
2462 fattr->cf_gid = i_gid_into_mnt(mnt_userns, inode);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002463 fattr->cf_mode = inode->i_mode;
Namjae Jeon777cad12021-08-13 08:15:33 +09002464 fattr->cf_acls = NULL;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002465 fattr->cf_dacls = NULL;
2466
Namjae Jeon777cad12021-08-13 08:15:33 +09002467 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
2468 fattr->cf_acls = get_acl(inode, ACL_TYPE_ACCESS);
2469 if (S_ISDIR(inode->i_mode))
2470 fattr->cf_dacls = get_acl(inode, ACL_TYPE_DEFAULT);
2471 }
Namjae Jeon3d47e542021-04-20 14:25:35 +09002472}
2473
Namjae Jeone2f34482021-03-16 10:49:09 +09002474/**
2475 * smb2_open() - handler for smb file open request
2476 * @work: smb work containing request buffer
2477 *
2478 * Return: 0 on success, otherwise error
2479 */
2480int smb2_open(struct ksmbd_work *work)
2481{
2482 struct ksmbd_conn *conn = work->conn;
2483 struct ksmbd_session *sess = work->sess;
2484 struct ksmbd_tree_connect *tcon = work->tcon;
2485 struct smb2_create_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09002486 struct smb2_create_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09002487 struct path path;
2488 struct ksmbd_share_config *share = tcon->share_conf;
2489 struct ksmbd_file *fp = NULL;
2490 struct file *filp = NULL;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002491 struct user_namespace *user_ns = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002492 struct kstat stat;
2493 struct create_context *context;
2494 struct lease_ctx_info *lc = NULL;
2495 struct create_ea_buf_req *ea_buf = NULL;
2496 struct oplock_info *opinfo;
2497 __le32 *next_ptr = NULL;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002498 int req_op_level = 0, open_flags = 0, may_flags = 0, file_info = 0;
Hyunchul Lee265fd192021-09-25 00:06:16 +09002499 int rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002500 int contxt_cnt = 0, query_disk_id = 0;
2501 int maximal_access_ctxt = 0, posix_ctxt = 0;
2502 int s_type = 0;
2503 int next_off = 0;
2504 char *name = NULL;
2505 char *stream_name = NULL;
2506 bool file_present = false, created = false, already_permitted = false;
Namjae Jeone2f34482021-03-16 10:49:09 +09002507 int share_ret, need_truncate = 0;
2508 u64 time;
2509 umode_t posix_mode = 0;
2510 __le32 daccess, maximal_access = 0;
2511
Namjae Jeone2f34482021-03-16 10:49:09 +09002512 WORK_BUFFERS(work, req, rsp);
2513
2514 if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002515 (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002516 ksmbd_debug(SMB, "invalid flag in chained command\n");
2517 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2518 smb2_set_err_rsp(work);
2519 return -EINVAL;
2520 }
2521
2522 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
2523 ksmbd_debug(SMB, "IPC pipe create request\n");
2524 return create_smb2_pipe(work);
2525 }
2526
2527 if (req->NameLength) {
2528 if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002529 *(char *)req->Buffer == '\\') {
Namjae Jeonbde16942021-06-28 15:23:19 +09002530 pr_err("not allow directory name included leading slash\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002531 rc = -EINVAL;
2532 goto err_out1;
2533 }
2534
Marios Makassikis80917f12021-12-01 21:41:19 +01002535 name = smb2_get_name(req->Buffer,
Namjae Jeone2f34482021-03-16 10:49:09 +09002536 le16_to_cpu(req->NameLength),
2537 work->conn->local_nls);
2538 if (IS_ERR(name)) {
2539 rc = PTR_ERR(name);
2540 if (rc != -ENOMEM)
2541 rc = -ENOENT;
Dan Carpenter8b99f352021-08-02 08:14:03 +09002542 name = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002543 goto err_out1;
2544 }
2545
2546 ksmbd_debug(SMB, "converted name = %s\n", name);
2547 if (strchr(name, ':')) {
2548 if (!test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002549 KSMBD_SHARE_FLAG_STREAMS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002550 rc = -EBADF;
2551 goto err_out1;
2552 }
2553 rc = parse_stream_name(name, &stream_name, &s_type);
2554 if (rc < 0)
2555 goto err_out1;
2556 }
2557
2558 rc = ksmbd_validate_filename(name);
2559 if (rc < 0)
2560 goto err_out1;
2561
2562 if (ksmbd_share_veto_filename(share, name)) {
2563 rc = -ENOENT;
2564 ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002565 name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002566 goto err_out1;
2567 }
2568 } else {
Hyunchul Lee265fd192021-09-25 00:06:16 +09002569 name = kstrdup("", GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09002570 if (!name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002571 rc = -ENOMEM;
2572 goto err_out1;
2573 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002574 }
2575
2576 req_op_level = req->RequestedOplockLevel;
Namjae Jeon73f9dad2021-04-16 14:12:06 +09002577 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002578 lc = parse_lease_state(req);
Namjae Jeone2f34482021-03-16 10:49:09 +09002579
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002580 if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002581 pr_err("Invalid impersonationlevel : 0x%x\n",
2582 le32_to_cpu(req->ImpersonationLevel));
Namjae Jeone2f34482021-03-16 10:49:09 +09002583 rc = -EIO;
2584 rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL;
2585 goto err_out1;
2586 }
2587
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002588 if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002589 pr_err("Invalid create options : 0x%x\n",
2590 le32_to_cpu(req->CreateOptions));
Namjae Jeone2f34482021-03-16 10:49:09 +09002591 rc = -EINVAL;
2592 goto err_out1;
2593 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09002594 if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002595 req->CreateOptions & FILE_RANDOM_ACCESS_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002596 req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE);
2597
Namjae Jeon070fb212021-05-26 17:57:12 +09002598 if (req->CreateOptions &
2599 (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION |
2600 FILE_RESERVE_OPFILTER_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002601 rc = -EOPNOTSUPP;
2602 goto err_out1;
2603 }
2604
2605 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2606 if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) {
2607 rc = -EINVAL;
2608 goto err_out1;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002609 } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002610 req->CreateOptions = ~(FILE_NO_COMPRESSION_LE);
Namjae Jeon64b39f42021-03-30 14:25:35 +09002611 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002612 }
2613 }
2614
2615 if (le32_to_cpu(req->CreateDisposition) >
Namjae Jeon070fb212021-05-26 17:57:12 +09002616 le32_to_cpu(FILE_OVERWRITE_IF_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002617 pr_err("Invalid create disposition : 0x%x\n",
2618 le32_to_cpu(req->CreateDisposition));
Namjae Jeone2f34482021-03-16 10:49:09 +09002619 rc = -EINVAL;
2620 goto err_out1;
2621 }
2622
2623 if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002624 pr_err("Invalid desired access : 0x%x\n",
2625 le32_to_cpu(req->DesiredAccess));
Namjae Jeone2f34482021-03-16 10:49:09 +09002626 rc = -EACCES;
2627 goto err_out1;
2628 }
2629
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002630 if (req->FileAttributes && !(req->FileAttributes & FILE_ATTRIBUTE_MASK_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002631 pr_err("Invalid file attribute : 0x%x\n",
2632 le32_to_cpu(req->FileAttributes));
Namjae Jeone2f34482021-03-16 10:49:09 +09002633 rc = -EINVAL;
2634 goto err_out1;
2635 }
2636
2637 if (req->CreateContextsOffset) {
2638 /* Parse non-durable handle create contexts */
2639 context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002640 if (IS_ERR(context)) {
2641 rc = PTR_ERR(context);
2642 goto err_out1;
2643 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002644 ea_buf = (struct create_ea_buf_req *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002645 if (le16_to_cpu(context->DataOffset) +
2646 le32_to_cpu(context->DataLength) <
2647 sizeof(struct create_ea_buf_req)) {
2648 rc = -EINVAL;
2649 goto err_out1;
2650 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002651 if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) {
2652 rsp->hdr.Status = STATUS_ACCESS_DENIED;
2653 rc = -EACCES;
2654 goto err_out1;
2655 }
2656 }
2657
2658 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002659 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002660 if (IS_ERR(context)) {
2661 rc = PTR_ERR(context);
2662 goto err_out1;
2663 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002664 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002665 "get query maximal access context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002666 maximal_access_ctxt = 1;
2667 }
2668
2669 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002670 SMB2_CREATE_TIMEWARP_REQUEST);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002671 if (IS_ERR(context)) {
2672 rc = PTR_ERR(context);
2673 goto err_out1;
2674 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002675 ksmbd_debug(SMB, "get timewarp context\n");
2676 rc = -EBADF;
2677 goto err_out1;
2678 }
2679
2680 if (tcon->posix_extensions) {
2681 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002682 SMB2_CREATE_TAG_POSIX);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002683 if (IS_ERR(context)) {
2684 rc = PTR_ERR(context);
2685 goto err_out1;
2686 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002687 struct create_posix *posix =
2688 (struct create_posix *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002689 if (le16_to_cpu(context->DataOffset) +
2690 le32_to_cpu(context->DataLength) <
Namjae Jeon9ca85812022-01-22 10:47:22 +09002691 sizeof(struct create_posix) - 4) {
Hyunchul Lee8f771502021-09-24 22:22:22 +09002692 rc = -EINVAL;
2693 goto err_out1;
2694 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002695 ksmbd_debug(SMB, "get posix context\n");
2696
2697 posix_mode = le32_to_cpu(posix->Mode);
2698 posix_ctxt = 1;
2699 }
2700 }
2701 }
2702
2703 if (ksmbd_override_fsids(work)) {
2704 rc = -ENOMEM;
2705 goto err_out1;
2706 }
2707
Hyunchul Lee265fd192021-09-25 00:06:16 +09002708 rc = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, 1);
Namjae Jeon4ea47792021-09-21 14:19:33 +09002709 if (!rc) {
2710 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002711 /*
2712 * If file exists with under flags, return access
2713 * denied error.
2714 */
2715 if (req->CreateDisposition == FILE_OVERWRITE_IF_LE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09002716 req->CreateDisposition == FILE_OPEN_IF_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002717 rc = -EACCES;
2718 path_put(&path);
2719 goto err_out;
2720 }
2721
Namjae Jeon64b39f42021-03-30 14:25:35 +09002722 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002723 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002724 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002725 rc = -EACCES;
2726 path_put(&path);
2727 goto err_out;
2728 }
Namjae Jeon4ea47792021-09-21 14:19:33 +09002729 } else if (d_is_symlink(path.dentry)) {
2730 rc = -EACCES;
2731 path_put(&path);
2732 goto err_out;
Namjae Jeone2f34482021-03-16 10:49:09 +09002733 }
2734 }
2735
2736 if (rc) {
Hyunchul Lee265fd192021-09-25 00:06:16 +09002737 if (rc != -ENOENT)
Namjae Jeone2f34482021-03-16 10:49:09 +09002738 goto err_out;
Namjae Jeone2f34482021-03-16 10:49:09 +09002739 ksmbd_debug(SMB, "can not get linux path for %s, rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002740 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002741 rc = 0;
2742 } else {
2743 file_present = true;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002744 user_ns = mnt_user_ns(path.mnt);
2745 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09002746 }
2747 if (stream_name) {
2748 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2749 if (s_type == DATA_STREAM) {
2750 rc = -EIO;
2751 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2752 }
2753 } else {
2754 if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) {
2755 rc = -EIO;
2756 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2757 }
2758 }
2759
2760 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE &&
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09002761 req->FileAttributes & FILE_ATTRIBUTE_NORMAL_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002762 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2763 rc = -EIO;
2764 }
2765
2766 if (rc < 0)
2767 goto err_out;
2768 }
2769
Namjae Jeon64b39f42021-03-30 14:25:35 +09002770 if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE &&
2771 S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002772 ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002773 name, req->CreateOptions);
Namjae Jeone2f34482021-03-16 10:49:09 +09002774 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2775 rc = -EIO;
2776 goto err_out;
2777 }
2778
2779 if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002780 !(req->CreateDisposition == FILE_CREATE_LE) &&
2781 !S_ISDIR(stat.mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002782 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2783 rc = -EIO;
2784 goto err_out;
2785 }
2786
2787 if (!stream_name && file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002788 req->CreateDisposition == FILE_CREATE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002789 rc = -EEXIST;
2790 goto err_out;
2791 }
2792
Namjae Jeone2f34482021-03-16 10:49:09 +09002793 daccess = smb_map_generic_desired_access(req->DesiredAccess);
2794
2795 if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002796 rc = smb_check_perm_dacl(conn, &path, &daccess,
Namjae Jeon070fb212021-05-26 17:57:12 +09002797 sess->user->uid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002798 if (rc)
2799 goto err_out;
2800 }
2801
2802 if (daccess & FILE_MAXIMAL_ACCESS_LE) {
2803 if (!file_present) {
2804 daccess = cpu_to_le32(GENERIC_ALL_FLAGS);
2805 } else {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002806 rc = ksmbd_vfs_query_maximal_access(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002807 path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002808 &daccess);
2809 if (rc)
2810 goto err_out;
2811 already_permitted = true;
2812 }
2813 maximal_access = daccess;
2814 }
2815
Namjae Jeon070fb212021-05-26 17:57:12 +09002816 open_flags = smb2_create_open_flags(file_present, daccess,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002817 req->CreateDisposition,
2818 &may_flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09002819
2820 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
2821 if (open_flags & O_CREAT) {
2822 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002823 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002824 rc = -EACCES;
2825 goto err_out;
2826 }
2827 }
2828
2829 /*create file if not present */
2830 if (!file_present) {
2831 rc = smb2_creat(work, &path, name, open_flags, posix_mode,
Namjae Jeon070fb212021-05-26 17:57:12 +09002832 req->CreateOptions & FILE_DIRECTORY_FILE_LE);
Marios Makassikisd337a442021-07-27 09:24:51 +09002833 if (rc) {
2834 if (rc == -ENOENT) {
2835 rc = -EIO;
2836 rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND;
2837 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002838 goto err_out;
Marios Makassikisd337a442021-07-27 09:24:51 +09002839 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002840
2841 created = true;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002842 user_ns = mnt_user_ns(path.mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002843 if (ea_buf) {
Namjae Jeon9496e262021-09-29 15:41:48 +09002844 if (le32_to_cpu(ea_buf->ccontext.DataLength) <
2845 sizeof(struct smb2_ea_info)) {
2846 rc = -EINVAL;
2847 goto err_out;
2848 }
2849
2850 rc = smb2_set_ea(&ea_buf->ea,
2851 le32_to_cpu(ea_buf->ccontext.DataLength),
2852 &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002853 if (rc == -EOPNOTSUPP)
2854 rc = 0;
2855 else if (rc)
2856 goto err_out;
2857 }
2858 } else if (!already_permitted) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002859 /* FILE_READ_ATTRIBUTE is allowed without inode_permission,
2860 * because execute(search) permission on a parent directory,
2861 * is already granted.
2862 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09002863 if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002864 rc = inode_permission(user_ns,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002865 d_inode(path.dentry),
2866 may_flags);
Namjae Jeonff1d5722021-04-13 13:18:10 +09002867 if (rc)
Namjae Jeone2f34482021-03-16 10:49:09 +09002868 goto err_out;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002869
2870 if ((daccess & FILE_DELETE_LE) ||
2871 (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002872 rc = ksmbd_vfs_may_delete(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002873 path.dentry);
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002874 if (rc)
2875 goto err_out;
2876 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002877 }
2878 }
2879
2880 rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent));
2881 if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) {
2882 rc = -EBUSY;
2883 goto err_out;
2884 }
2885
2886 rc = 0;
2887 filp = dentry_open(&path, open_flags, current_cred());
2888 if (IS_ERR(filp)) {
2889 rc = PTR_ERR(filp);
Namjae Jeonbde16942021-06-28 15:23:19 +09002890 pr_err("dentry open for dir failed, rc %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002891 goto err_out;
2892 }
2893
2894 if (file_present) {
2895 if (!(open_flags & O_TRUNC))
2896 file_info = FILE_OPENED;
2897 else
2898 file_info = FILE_OVERWRITTEN;
2899
Namjae Jeon070fb212021-05-26 17:57:12 +09002900 if ((req->CreateDisposition & FILE_CREATE_MASK_LE) ==
2901 FILE_SUPERSEDE_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002902 file_info = FILE_SUPERSEDED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002903 } else if (open_flags & O_CREAT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002904 file_info = FILE_CREATED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002905 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002906
2907 ksmbd_vfs_set_fadvise(filp, req->CreateOptions);
2908
2909 /* Obtain Volatile-ID */
2910 fp = ksmbd_open_fd(work, filp);
2911 if (IS_ERR(fp)) {
2912 fput(filp);
2913 rc = PTR_ERR(fp);
2914 fp = NULL;
2915 goto err_out;
2916 }
2917
2918 /* Get Persistent-ID */
2919 ksmbd_open_durable_fd(fp);
Namjae Jeon38673692021-07-08 12:32:27 +09002920 if (!has_file_id(fp->persistent_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002921 rc = -ENOMEM;
2922 goto err_out;
2923 }
2924
2925 fp->filename = name;
2926 fp->cdoption = req->CreateDisposition;
2927 fp->daccess = daccess;
2928 fp->saccess = req->ShareAccess;
2929 fp->coption = req->CreateOptions;
2930
2931 /* Set default windows and posix acls if creating new file */
2932 if (created) {
2933 int posix_acl_rc;
Namjae Jeonfba08fa2021-04-15 10:29:39 +09002934 struct inode *inode = d_inode(path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09002935
Hyunchul Lee465d7202021-07-03 12:10:36 +09002936 posix_acl_rc = ksmbd_vfs_inherit_posix_acl(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002937 inode,
2938 d_inode(path.dentry->d_parent));
Namjae Jeone2f34482021-03-16 10:49:09 +09002939 if (posix_acl_rc)
2940 ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc);
2941
2942 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002943 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002944 rc = smb_inherit_dacl(conn, &path, sess->user->uid,
Namjae Jeon070fb212021-05-26 17:57:12 +09002945 sess->user->gid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002946 }
2947
2948 if (rc) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002949 rc = smb2_create_sd_buffer(work, req, &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002950 if (rc) {
2951 if (posix_acl_rc)
Hyunchul Lee465d7202021-07-03 12:10:36 +09002952 ksmbd_vfs_set_init_posix_acl(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002953 inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09002954
2955 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002956 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002957 struct smb_fattr fattr;
2958 struct smb_ntsd *pntsd;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002959 int pntsd_size, ace_num = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002960
Christian Brauner43205ca2021-08-23 17:13:50 +02002961 ksmbd_acls_fattr(&fattr, user_ns, inode);
Marios Makassikise6b10592021-04-15 10:24:56 +09002962 if (fattr.cf_acls)
2963 ace_num = fattr.cf_acls->a_count;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002964 if (fattr.cf_dacls)
2965 ace_num += fattr.cf_dacls->a_count;
Namjae Jeone2f34482021-03-16 10:49:09 +09002966
2967 pntsd = kmalloc(sizeof(struct smb_ntsd) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002968 sizeof(struct smb_sid) * 3 +
Namjae Jeone2f34482021-03-16 10:49:09 +09002969 sizeof(struct smb_acl) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002970 sizeof(struct smb_ace) * ace_num * 2,
Namjae Jeone2f34482021-03-16 10:49:09 +09002971 GFP_KERNEL);
2972 if (!pntsd)
2973 goto err_out;
2974
Hyunchul Lee465d7202021-07-03 12:10:36 +09002975 rc = build_sec_desc(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002976 pntsd, NULL,
Namjae Jeon070fb212021-05-26 17:57:12 +09002977 OWNER_SECINFO |
Hyunchul Leeaf349832021-06-30 18:25:53 +09002978 GROUP_SECINFO |
2979 DACL_SECINFO,
Namjae Jeon070fb212021-05-26 17:57:12 +09002980 &pntsd_size, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09002981 posix_acl_release(fattr.cf_acls);
2982 posix_acl_release(fattr.cf_dacls);
Namjae Jeonf2e78af2021-12-01 10:12:39 +09002983 if (rc) {
2984 kfree(pntsd);
2985 goto err_out;
2986 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002987
2988 rc = ksmbd_vfs_set_sd_xattr(conn,
Hyunchul Lee465d7202021-07-03 12:10:36 +09002989 user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +09002990 path.dentry,
2991 pntsd,
2992 pntsd_size);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002993 kfree(pntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09002994 if (rc)
Namjae Jeonbde16942021-06-28 15:23:19 +09002995 pr_err("failed to store ntacl in xattr : %d\n",
2996 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002997 }
2998 }
2999 }
3000 rc = 0;
3001 }
3002
3003 if (stream_name) {
3004 rc = smb2_set_stream_name_xattr(&path,
3005 fp,
3006 stream_name,
3007 s_type);
3008 if (rc)
3009 goto err_out;
3010 file_info = FILE_CREATED;
3011 }
3012
3013 fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE |
3014 FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE));
Namjae Jeon64b39f42021-03-30 14:25:35 +09003015 if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC &&
3016 !fp->attrib_only && !stream_name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003017 smb_break_all_oplock(work, fp);
3018 need_truncate = 1;
3019 }
3020
3021 /* fp should be searchable through ksmbd_inode.m_fp_list
3022 * after daccess, saccess, attrib_only, and stream are
3023 * initialized.
3024 */
3025 write_lock(&fp->f_ci->m_lock);
3026 list_add(&fp->node, &fp->f_ci->m_fp_list);
3027 write_unlock(&fp->f_ci->m_lock);
3028
3029 rc = ksmbd_vfs_getattr(&path, &stat);
3030 if (rc) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09003031 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09003032 rc = 0;
3033 }
3034
3035 /* Check delete pending among previous fp before oplock break */
3036 if (ksmbd_inode_pending_delete(fp)) {
3037 rc = -EBUSY;
3038 goto err_out;
3039 }
3040
3041 share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp);
Namjae Jeon64b39f42021-03-30 14:25:35 +09003042 if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) ||
3043 (req_op_level == SMB2_OPLOCK_LEVEL_LEASE &&
3044 !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) {
Namjae Jeonab0b2632021-06-29 09:20:13 +09003045 if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003046 rc = share_ret;
3047 goto err_out;
3048 }
3049 } else {
3050 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) {
3051 req_op_level = smb2_map_lease_to_oplock(lc->req_state);
3052 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003053 "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n",
3054 name, req_op_level, lc->req_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09003055 rc = find_same_lease_key(sess, fp->f_ci, lc);
3056 if (rc)
3057 goto err_out;
3058 } else if (open_flags == O_RDONLY &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09003059 (req_op_level == SMB2_OPLOCK_LEVEL_BATCH ||
3060 req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
Namjae Jeone2f34482021-03-16 10:49:09 +09003061 req_op_level = SMB2_OPLOCK_LEVEL_II;
3062
3063 rc = smb_grant_oplock(work, req_op_level,
3064 fp->persistent_id, fp,
3065 le32_to_cpu(req->hdr.Id.SyncId.TreeId),
3066 lc, share_ret);
3067 if (rc < 0)
3068 goto err_out;
3069 }
3070
3071 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
3072 ksmbd_fd_set_delete_on_close(fp, file_info);
3073
3074 if (need_truncate) {
3075 rc = smb2_create_truncate(&path);
3076 if (rc)
3077 goto err_out;
3078 }
3079
3080 if (req->CreateContextsOffset) {
3081 struct create_alloc_size_req *az_req;
3082
Namjae Jeon070fb212021-05-26 17:57:12 +09003083 az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req,
3084 SMB2_CREATE_ALLOCATION_SIZE);
Namjae Jeonf19b3962021-07-13 09:59:34 +09003085 if (IS_ERR(az_req)) {
3086 rc = PTR_ERR(az_req);
3087 goto err_out;
3088 } else if (az_req) {
Hyunchul Lee8f771502021-09-24 22:22:22 +09003089 loff_t alloc_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09003090 int err;
3091
Hyunchul Lee8f771502021-09-24 22:22:22 +09003092 if (le16_to_cpu(az_req->ccontext.DataOffset) +
3093 le32_to_cpu(az_req->ccontext.DataLength) <
3094 sizeof(struct create_alloc_size_req)) {
3095 rc = -EINVAL;
3096 goto err_out;
3097 }
3098 alloc_size = le64_to_cpu(az_req->AllocationSize);
Namjae Jeone2f34482021-03-16 10:49:09 +09003099 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003100 "request smb2 create allocate size : %llu\n",
3101 alloc_size);
Namjae Jeone8c06192021-06-22 11:06:11 +09003102 smb_break_all_levII_oplock(work, fp, 1);
3103 err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
3104 alloc_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003105 if (err < 0)
3106 ksmbd_debug(SMB,
Namjae Jeone8c06192021-06-22 11:06:11 +09003107 "vfs_fallocate is failed : %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003108 err);
Namjae Jeone2f34482021-03-16 10:49:09 +09003109 }
3110
Namjae Jeon64b39f42021-03-30 14:25:35 +09003111 context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID);
Namjae Jeonf19b3962021-07-13 09:59:34 +09003112 if (IS_ERR(context)) {
3113 rc = PTR_ERR(context);
3114 goto err_out;
3115 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003116 ksmbd_debug(SMB, "get query on disk id context\n");
3117 query_disk_id = 1;
3118 }
3119 }
3120
3121 if (stat.result_mask & STATX_BTIME)
3122 fp->create_time = ksmbd_UnixTimeToNT(stat.btime);
3123 else
3124 fp->create_time = ksmbd_UnixTimeToNT(stat.ctime);
3125 if (req->FileAttributes || fp->f_ci->m_fattr == 0)
Namjae Jeon070fb212021-05-26 17:57:12 +09003126 fp->f_ci->m_fattr =
3127 cpu_to_le32(smb2_get_dos_mode(&stat, le32_to_cpu(req->FileAttributes)));
Namjae Jeone2f34482021-03-16 10:49:09 +09003128
3129 if (!created)
3130 smb2_update_xattrs(tcon, &path, fp);
3131 else
3132 smb2_new_xattrs(tcon, &path, fp);
3133
3134 memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE);
3135
Hyunchul Lee465d7202021-07-03 12:10:36 +09003136 generic_fillattr(user_ns, file_inode(fp->filp),
Hyunchul Leeaf349832021-06-30 18:25:53 +09003137 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09003138
3139 rsp->StructureSize = cpu_to_le16(89);
3140 rcu_read_lock();
3141 opinfo = rcu_dereference(fp->f_opinfo);
3142 rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0;
3143 rcu_read_unlock();
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09003144 rsp->Flags = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09003145 rsp->CreateAction = cpu_to_le32(file_info);
3146 rsp->CreationTime = cpu_to_le64(fp->create_time);
3147 time = ksmbd_UnixTimeToNT(stat.atime);
3148 rsp->LastAccessTime = cpu_to_le64(time);
3149 time = ksmbd_UnixTimeToNT(stat.mtime);
3150 rsp->LastWriteTime = cpu_to_le64(time);
3151 time = ksmbd_UnixTimeToNT(stat.ctime);
3152 rsp->ChangeTime = cpu_to_le64(time);
3153 rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 :
3154 cpu_to_le64(stat.blocks << 9);
3155 rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
3156 rsp->FileAttributes = fp->f_ci->m_fattr;
3157
3158 rsp->Reserved2 = 0;
3159
3160 rsp->PersistentFileId = cpu_to_le64(fp->persistent_id);
3161 rsp->VolatileFileId = cpu_to_le64(fp->volatile_id);
3162
3163 rsp->CreateContextsOffset = 0;
3164 rsp->CreateContextsLength = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09003165 inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/
Namjae Jeone2f34482021-03-16 10:49:09 +09003166
3167 /* If lease is request send lease context response */
3168 if (opinfo && opinfo->is_lease) {
3169 struct create_context *lease_ccontext;
3170
3171 ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003172 name, opinfo->o_lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +09003173 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
3174
3175 lease_ccontext = (struct create_context *)rsp->Buffer;
3176 contxt_cnt++;
3177 create_lease_buf(rsp->Buffer, opinfo->o_lease);
3178 le32_add_cpu(&rsp->CreateContextsLength,
3179 conn->vals->create_lease_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003180 inc_rfc1001_len(work->response_buf,
3181 conn->vals->create_lease_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003182 next_ptr = &lease_ccontext->Next;
3183 next_off = conn->vals->create_lease_size;
3184 }
3185
Namjae Jeone2f34482021-03-16 10:49:09 +09003186 if (maximal_access_ctxt) {
3187 struct create_context *mxac_ccontext;
3188
3189 if (maximal_access == 0)
Hyunchul Lee465d7202021-07-03 12:10:36 +09003190 ksmbd_vfs_query_maximal_access(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09003191 path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09003192 &maximal_access);
3193 mxac_ccontext = (struct create_context *)(rsp->Buffer +
3194 le32_to_cpu(rsp->CreateContextsLength));
3195 contxt_cnt++;
3196 create_mxac_rsp_buf(rsp->Buffer +
3197 le32_to_cpu(rsp->CreateContextsLength),
3198 le32_to_cpu(maximal_access));
3199 le32_add_cpu(&rsp->CreateContextsLength,
3200 conn->vals->create_mxac_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003201 inc_rfc1001_len(work->response_buf,
3202 conn->vals->create_mxac_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003203 if (next_ptr)
3204 *next_ptr = cpu_to_le32(next_off);
3205 next_ptr = &mxac_ccontext->Next;
3206 next_off = conn->vals->create_mxac_size;
3207 }
3208
3209 if (query_disk_id) {
3210 struct create_context *disk_id_ccontext;
3211
3212 disk_id_ccontext = (struct create_context *)(rsp->Buffer +
3213 le32_to_cpu(rsp->CreateContextsLength));
3214 contxt_cnt++;
3215 create_disk_id_rsp_buf(rsp->Buffer +
3216 le32_to_cpu(rsp->CreateContextsLength),
3217 stat.ino, tcon->id);
3218 le32_add_cpu(&rsp->CreateContextsLength,
3219 conn->vals->create_disk_id_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003220 inc_rfc1001_len(work->response_buf,
3221 conn->vals->create_disk_id_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003222 if (next_ptr)
3223 *next_ptr = cpu_to_le32(next_off);
3224 next_ptr = &disk_id_ccontext->Next;
3225 next_off = conn->vals->create_disk_id_size;
3226 }
3227
3228 if (posix_ctxt) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003229 contxt_cnt++;
3230 create_posix_rsp_buf(rsp->Buffer +
3231 le32_to_cpu(rsp->CreateContextsLength),
3232 fp);
3233 le32_add_cpu(&rsp->CreateContextsLength,
3234 conn->vals->create_posix_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003235 inc_rfc1001_len(work->response_buf,
3236 conn->vals->create_posix_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003237 if (next_ptr)
3238 *next_ptr = cpu_to_le32(next_off);
3239 }
3240
3241 if (contxt_cnt > 0) {
3242 rsp->CreateContextsOffset =
Namjae Jeoncb451722021-11-03 08:08:44 +09003243 cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer));
Namjae Jeone2f34482021-03-16 10:49:09 +09003244 }
3245
3246err_out:
3247 if (file_present || created)
3248 path_put(&path);
3249 ksmbd_revert_fsids(work);
3250err_out1:
3251 if (rc) {
3252 if (rc == -EINVAL)
3253 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3254 else if (rc == -EOPNOTSUPP)
3255 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Hyunchul Lee265fd192021-09-25 00:06:16 +09003256 else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV)
Namjae Jeone2f34482021-03-16 10:49:09 +09003257 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3258 else if (rc == -ENOENT)
3259 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
3260 else if (rc == -EPERM)
3261 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
3262 else if (rc == -EBUSY)
3263 rsp->hdr.Status = STATUS_DELETE_PENDING;
3264 else if (rc == -EBADF)
3265 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
3266 else if (rc == -ENOEXEC)
3267 rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID;
3268 else if (rc == -ENXIO)
3269 rsp->hdr.Status = STATUS_NO_SUCH_DEVICE;
3270 else if (rc == -EEXIST)
3271 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
3272 else if (rc == -EMFILE)
3273 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
3274 if (!rsp->hdr.Status)
3275 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3276
3277 if (!fp || !fp->filename)
3278 kfree(name);
3279 if (fp)
3280 ksmbd_fd_put(work, fp);
3281 smb2_set_err_rsp(work);
3282 ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status);
3283 }
3284
3285 kfree(lc);
3286
3287 return 0;
3288}
3289
3290static int readdir_info_level_struct_sz(int info_level)
3291{
3292 switch (info_level) {
3293 case FILE_FULL_DIRECTORY_INFORMATION:
3294 return sizeof(struct file_full_directory_info);
3295 case FILE_BOTH_DIRECTORY_INFORMATION:
3296 return sizeof(struct file_both_directory_info);
3297 case FILE_DIRECTORY_INFORMATION:
3298 return sizeof(struct file_directory_info);
3299 case FILE_NAMES_INFORMATION:
3300 return sizeof(struct file_names_info);
3301 case FILEID_FULL_DIRECTORY_INFORMATION:
3302 return sizeof(struct file_id_full_dir_info);
3303 case FILEID_BOTH_DIRECTORY_INFORMATION:
3304 return sizeof(struct file_id_both_directory_info);
3305 case SMB_FIND_FILE_POSIX_INFO:
3306 return sizeof(struct smb2_posix_info);
3307 default:
3308 return -EOPNOTSUPP;
3309 }
3310}
3311
3312static int dentry_name(struct ksmbd_dir_info *d_info, int info_level)
3313{
3314 switch (info_level) {
3315 case FILE_FULL_DIRECTORY_INFORMATION:
3316 {
3317 struct file_full_directory_info *ffdinfo;
3318
3319 ffdinfo = (struct file_full_directory_info *)d_info->rptr;
3320 d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset);
3321 d_info->name = ffdinfo->FileName;
3322 d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength);
3323 return 0;
3324 }
3325 case FILE_BOTH_DIRECTORY_INFORMATION:
3326 {
3327 struct file_both_directory_info *fbdinfo;
3328
3329 fbdinfo = (struct file_both_directory_info *)d_info->rptr;
3330 d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset);
3331 d_info->name = fbdinfo->FileName;
3332 d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength);
3333 return 0;
3334 }
3335 case FILE_DIRECTORY_INFORMATION:
3336 {
3337 struct file_directory_info *fdinfo;
3338
3339 fdinfo = (struct file_directory_info *)d_info->rptr;
3340 d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset);
3341 d_info->name = fdinfo->FileName;
3342 d_info->name_len = le32_to_cpu(fdinfo->FileNameLength);
3343 return 0;
3344 }
3345 case FILE_NAMES_INFORMATION:
3346 {
3347 struct file_names_info *fninfo;
3348
3349 fninfo = (struct file_names_info *)d_info->rptr;
3350 d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset);
3351 d_info->name = fninfo->FileName;
3352 d_info->name_len = le32_to_cpu(fninfo->FileNameLength);
3353 return 0;
3354 }
3355 case FILEID_FULL_DIRECTORY_INFORMATION:
3356 {
3357 struct file_id_full_dir_info *dinfo;
3358
3359 dinfo = (struct file_id_full_dir_info *)d_info->rptr;
3360 d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset);
3361 d_info->name = dinfo->FileName;
3362 d_info->name_len = le32_to_cpu(dinfo->FileNameLength);
3363 return 0;
3364 }
3365 case FILEID_BOTH_DIRECTORY_INFORMATION:
3366 {
3367 struct file_id_both_directory_info *fibdinfo;
3368
3369 fibdinfo = (struct file_id_both_directory_info *)d_info->rptr;
3370 d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset);
3371 d_info->name = fibdinfo->FileName;
3372 d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength);
3373 return 0;
3374 }
3375 case SMB_FIND_FILE_POSIX_INFO:
3376 {
3377 struct smb2_posix_info *posix_info;
3378
3379 posix_info = (struct smb2_posix_info *)d_info->rptr;
3380 d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset);
3381 d_info->name = posix_info->name;
3382 d_info->name_len = le32_to_cpu(posix_info->name_len);
3383 return 0;
3384 }
3385 default:
3386 return -EINVAL;
3387 }
3388}
3389
3390/**
3391 * smb2_populate_readdir_entry() - encode directory entry in smb2 response
3392 * buffer
3393 * @conn: connection instance
3394 * @info_level: smb information level
3395 * @d_info: structure included variables for query dir
3396 * @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
Namjae Jeon04e26092022-01-30 18:31:01 +09003425 struct_sz = readdir_info_level_struct_sz(info_level) - 1 + conv_len;
3426 next_entry_offset = ALIGN(struct_sz, KSMBD_DIR_INFO_ALIGNMENT);
3427 d_info->last_entry_off_align = next_entry_offset - struct_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09003428
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;
Namjae Jeon04e26092022-01-30 18:31:01 +09003979 d_info.data_count -= d_info.last_entry_off_align;
Namjae Jeone2f34482021-03-16 10:49:09 +09003980
3981 rsp->StructureSize = cpu_to_le16(9);
3982 rsp->OutputBufferOffset = cpu_to_le16(72);
3983 rsp->OutputBufferLength = cpu_to_le32(d_info.data_count);
Namjae Jeoncb451722021-11-03 08:08:44 +09003984 inc_rfc1001_len(work->response_buf, 8 + d_info.data_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09003985 }
3986
3987 kfree(srch_ptr);
3988 ksmbd_fd_put(work, dir_fp);
3989 ksmbd_revert_fsids(work);
3990 return 0;
3991
3992err_out:
Namjae Jeonbde16942021-06-28 15:23:19 +09003993 pr_err("error while processing smb2 query dir rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09003994 kfree(srch_ptr);
3995
3996err_out2:
3997 if (rc == -EINVAL)
3998 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3999 else if (rc == -EACCES)
4000 rsp->hdr.Status = STATUS_ACCESS_DENIED;
4001 else if (rc == -ENOENT)
4002 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
4003 else if (rc == -EBADF)
4004 rsp->hdr.Status = STATUS_FILE_CLOSED;
4005 else if (rc == -ENOMEM)
4006 rsp->hdr.Status = STATUS_NO_MEMORY;
4007 else if (rc == -EFAULT)
4008 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
4009 if (!rsp->hdr.Status)
4010 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
4011
4012 smb2_set_err_rsp(work);
4013 ksmbd_fd_put(work, dir_fp);
4014 ksmbd_revert_fsids(work);
4015 return 0;
4016}
4017
4018/**
4019 * buffer_check_err() - helper function to check buffer errors
4020 * @reqOutputBufferLength: max buffer length expected in command response
4021 * @rsp: query info response buffer contains output buffer length
Yang Lie230d012021-12-21 17:07:11 +08004022 * @rsp_org: base response buffer pointer in case of chained response
Namjae Jeone2f34482021-03-16 10:49:09 +09004023 * @infoclass_size: query info class response buffer size
4024 *
4025 * Return: 0 on success, otherwise error
4026 */
4027static int buffer_check_err(int reqOutputBufferLength,
Namjae Jeoncb451722021-11-03 08:08:44 +09004028 struct smb2_query_info_rsp *rsp,
4029 void *rsp_org, int infoclass_size)
Namjae Jeone2f34482021-03-16 10:49:09 +09004030{
4031 if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) {
4032 if (reqOutputBufferLength < infoclass_size) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004033 pr_err("Invalid Buffer Size Requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004034 rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH;
Namjae Jeoncb451722021-11-03 08:08:44 +09004035 *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr));
Namjae Jeone2f34482021-03-16 10:49:09 +09004036 return -EINVAL;
4037 }
4038
4039 ksmbd_debug(SMB, "Buffer Overflow\n");
4040 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
Namjae Jeoncb451722021-11-03 08:08:44 +09004041 *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09004042 reqOutputBufferLength);
4043 rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09004044 }
4045 return 0;
4046}
4047
Namjae Jeoncb451722021-11-03 08:08:44 +09004048static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp,
4049 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004050{
4051 struct smb2_file_standard_info *sinfo;
4052
4053 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4054
4055 sinfo->AllocationSize = cpu_to_le64(4096);
4056 sinfo->EndOfFile = cpu_to_le64(0);
4057 sinfo->NumberOfLinks = cpu_to_le32(1);
4058 sinfo->DeletePending = 1;
4059 sinfo->Directory = 0;
4060 rsp->OutputBufferLength =
4061 cpu_to_le32(sizeof(struct smb2_file_standard_info));
Namjae Jeoncb451722021-11-03 08:08:44 +09004062 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_standard_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004063}
4064
Namjae Jeoncb451722021-11-03 08:08:44 +09004065static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num,
4066 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004067{
4068 struct smb2_file_internal_info *file_info;
4069
4070 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4071
4072 /* any unique number */
4073 file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63));
4074 rsp->OutputBufferLength =
4075 cpu_to_le32(sizeof(struct smb2_file_internal_info));
Namjae Jeoncb451722021-11-03 08:08:44 +09004076 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004077}
4078
Namjae Jeone2f34482021-03-16 10:49:09 +09004079static int smb2_get_info_file_pipe(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09004080 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004081 struct smb2_query_info_rsp *rsp,
4082 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004083{
Namjae Jeon64b39f42021-03-30 14:25:35 +09004084 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09004085 int rc;
4086
4087 /*
4088 * Windows can sometime send query file info request on
4089 * pipe without opening it, checking error condition here
4090 */
4091 id = le64_to_cpu(req->VolatileFileId);
4092 if (!ksmbd_session_rpc_method(sess, id))
4093 return -ENOENT;
4094
4095 ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004096 req->FileInfoClass, le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09004097
4098 switch (req->FileInfoClass) {
4099 case FILE_STANDARD_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004100 get_standard_info_pipe(rsp, rsp_org);
Namjae Jeone2f34482021-03-16 10:49:09 +09004101 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004102 rsp, rsp_org,
4103 FILE_STANDARD_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09004104 break;
4105 case FILE_INTERNAL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004106 get_internal_info_pipe(rsp, id, rsp_org);
Namjae Jeone2f34482021-03-16 10:49:09 +09004107 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004108 rsp, rsp_org,
4109 FILE_INTERNAL_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09004110 break;
4111 default:
4112 ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004113 req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09004114 rc = -EOPNOTSUPP;
4115 }
4116 return rc;
4117}
4118
4119/**
4120 * smb2_get_ea() - handler for smb2 get extended attribute command
4121 * @work: smb work containing query info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09004122 * @fp: ksmbd_file pointer
4123 * @req: get extended attribute request
4124 * @rsp: response buffer pointer
4125 * @rsp_org: base response buffer pointer in case of chained response
Namjae Jeone2f34482021-03-16 10:49:09 +09004126 *
4127 * Return: 0 on success, otherwise error
4128 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09004129static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004130 struct smb2_query_info_req *req,
4131 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004132{
4133 struct smb2_ea_info *eainfo, *prev_eainfo;
4134 char *name, *ptr, *xattr_list = NULL, *buf;
4135 int rc, name_len, value_len, xattr_list_len, idx;
4136 ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0;
4137 struct smb2_ea_info_req *ea_req = NULL;
4138 struct path *path;
Hyunchul Lee465d7202021-07-03 12:10:36 +09004139 struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09004140
4141 if (!(fp->daccess & FILE_READ_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004142 pr_err("Not permitted to read ext attr : 0x%x\n",
4143 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004144 return -EACCES;
4145 }
4146
4147 path = &fp->filp->f_path;
4148 /* single EA entry is requested with given user.* name */
Namjae Jeon64b39f42021-03-30 14:25:35 +09004149 if (req->InputBufferLength) {
Namjae Jeon6d562622021-09-18 18:45:12 +09004150 if (le32_to_cpu(req->InputBufferLength) <
4151 sizeof(struct smb2_ea_info_req))
4152 return -EINVAL;
4153
Namjae Jeone2f34482021-03-16 10:49:09 +09004154 ea_req = (struct smb2_ea_info_req *)req->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004155 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09004156 /* need to send all EAs, if no specific EA is requested*/
4157 if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY)
4158 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09004159 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n",
4160 le32_to_cpu(req->Flags));
Namjae Jeone2f34482021-03-16 10:49:09 +09004161 }
4162
Hyunchul Lee34061d62021-10-16 08:39:54 +09004163 buf_free_len =
4164 smb2_calc_max_out_buf_len(work, 8,
4165 le32_to_cpu(req->OutputBufferLength));
4166 if (buf_free_len < 0)
4167 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09004168
4169 rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4170 if (rc < 0) {
4171 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4172 goto out;
4173 } else if (!rc) { /* there is no EA in the file */
4174 ksmbd_debug(SMB, "no ea data in the file\n");
4175 goto done;
4176 }
4177 xattr_list_len = rc;
4178
4179 ptr = (char *)rsp->Buffer;
4180 eainfo = (struct smb2_ea_info *)ptr;
4181 prev_eainfo = eainfo;
4182 idx = 0;
4183
4184 while (idx < xattr_list_len) {
4185 name = xattr_list + idx;
4186 name_len = strlen(name);
4187
4188 ksmbd_debug(SMB, "%s, len %d\n", name, name_len);
4189 idx += name_len + 1;
4190
4191 /*
4192 * CIFS does not support EA other than user.* namespace,
4193 * still keep the framework generic, to list other attrs
4194 * in future.
4195 */
4196 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4197 continue;
4198
4199 if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004200 STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004201 continue;
4202
4203 if (req->InputBufferLength &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09004204 strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name,
4205 ea_req->EaNameLength))
Namjae Jeone2f34482021-03-16 10:49:09 +09004206 continue;
4207
4208 if (!strncmp(&name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004209 DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004210 continue;
4211
4212 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4213 name_len -= XATTR_USER_PREFIX_LEN;
4214
4215 ptr = (char *)(&eainfo->name + name_len + 1);
4216 buf_free_len -= (offsetof(struct smb2_ea_info, name) +
4217 name_len + 1);
4218 /* bailout if xattr can't fit in buf_free_len */
Hyunchul Lee465d7202021-07-03 12:10:36 +09004219 value_len = ksmbd_vfs_getxattr(user_ns, path->dentry,
4220 name, &buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004221 if (value_len <= 0) {
4222 rc = -ENOENT;
4223 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4224 goto out;
4225 }
4226
4227 buf_free_len -= value_len;
4228 if (buf_free_len < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09004229 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004230 break;
4231 }
4232
4233 memcpy(ptr, buf, value_len);
Namjae Jeon79f6b112021-04-02 12:47:14 +09004234 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004235
4236 ptr += value_len;
4237 eainfo->Flags = 0;
4238 eainfo->EaNameLength = name_len;
4239
Namjae Jeon64b39f42021-03-30 14:25:35 +09004240 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004241 memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN],
Namjae Jeon070fb212021-05-26 17:57:12 +09004242 name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09004243 else
4244 memcpy(eainfo->name, name, name_len);
4245
4246 eainfo->name[name_len] = '\0';
4247 eainfo->EaValueLength = cpu_to_le16(value_len);
4248 next_offset = offsetof(struct smb2_ea_info, name) +
4249 name_len + 1 + value_len;
4250
4251 /* align next xattr entry at 4 byte bundary */
4252 alignment_bytes = ((next_offset + 3) & ~3) - next_offset;
4253 if (alignment_bytes) {
4254 memset(ptr, '\0', alignment_bytes);
4255 ptr += alignment_bytes;
4256 next_offset += alignment_bytes;
4257 buf_free_len -= alignment_bytes;
4258 }
4259 eainfo->NextEntryOffset = cpu_to_le32(next_offset);
4260 prev_eainfo = eainfo;
4261 eainfo = (struct smb2_ea_info *)ptr;
4262 rsp_data_cnt += next_offset;
4263
4264 if (req->InputBufferLength) {
4265 ksmbd_debug(SMB, "single entry requested\n");
4266 break;
4267 }
4268 }
4269
4270 /* no more ea entries */
4271 prev_eainfo->NextEntryOffset = 0;
4272done:
4273 rc = 0;
4274 if (rsp_data_cnt == 0)
4275 rsp->hdr.Status = STATUS_NO_EAS_ON_FILE;
4276 rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt);
4277 inc_rfc1001_len(rsp_org, rsp_data_cnt);
4278out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004279 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004280 return rc;
4281}
4282
4283static void get_file_access_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004284 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004285{
4286 struct smb2_file_access_info *file_info;
4287
4288 file_info = (struct smb2_file_access_info *)rsp->Buffer;
4289 file_info->AccessFlags = fp->daccess;
4290 rsp->OutputBufferLength =
4291 cpu_to_le32(sizeof(struct smb2_file_access_info));
4292 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info));
4293}
4294
4295static int get_file_basic_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004296 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004297{
Namjae Jeon88d30052021-09-29 15:37:18 +09004298 struct smb2_file_basic_info *basic_info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004299 struct kstat stat;
4300 u64 time;
4301
4302 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004303 pr_err("no right to read the attributes : 0x%x\n",
4304 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004305 return -EACCES;
4306 }
4307
Namjae Jeon88d30052021-09-29 15:37:18 +09004308 basic_info = (struct smb2_file_basic_info *)rsp->Buffer;
Hyunchul Leeaf349832021-06-30 18:25:53 +09004309 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4310 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004311 basic_info->CreationTime = cpu_to_le64(fp->create_time);
4312 time = ksmbd_UnixTimeToNT(stat.atime);
4313 basic_info->LastAccessTime = cpu_to_le64(time);
4314 time = ksmbd_UnixTimeToNT(stat.mtime);
4315 basic_info->LastWriteTime = cpu_to_le64(time);
4316 time = ksmbd_UnixTimeToNT(stat.ctime);
4317 basic_info->ChangeTime = cpu_to_le64(time);
4318 basic_info->Attributes = fp->f_ci->m_fattr;
4319 basic_info->Pad1 = 0;
4320 rsp->OutputBufferLength =
Namjae Jeon88d30052021-09-29 15:37:18 +09004321 cpu_to_le32(sizeof(struct smb2_file_basic_info));
4322 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_basic_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004323 return 0;
4324}
4325
4326static unsigned long long get_allocation_size(struct inode *inode,
Namjae Jeon070fb212021-05-26 17:57:12 +09004327 struct kstat *stat)
Namjae Jeone2f34482021-03-16 10:49:09 +09004328{
4329 unsigned long long alloc_size = 0;
4330
4331 if (!S_ISDIR(stat->mode)) {
4332 if ((inode->i_blocks << 9) <= stat->size)
4333 alloc_size = stat->size;
4334 else
4335 alloc_size = inode->i_blocks << 9;
Namjae Jeone2f34482021-03-16 10:49:09 +09004336 }
4337
4338 return alloc_size;
4339}
4340
4341static void get_file_standard_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004342 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004343{
4344 struct smb2_file_standard_info *sinfo;
4345 unsigned int delete_pending;
4346 struct inode *inode;
4347 struct kstat stat;
4348
Namjae Jeonab0b2632021-06-29 09:20:13 +09004349 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004350 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004351
4352 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4353 delete_pending = ksmbd_inode_pending_delete(fp);
4354
4355 sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat));
4356 sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4357 sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending);
4358 sinfo->DeletePending = delete_pending;
4359 sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4360 rsp->OutputBufferLength =
4361 cpu_to_le32(sizeof(struct smb2_file_standard_info));
4362 inc_rfc1001_len(rsp_org,
4363 sizeof(struct smb2_file_standard_info));
4364}
4365
4366static void get_file_alignment_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004367 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004368{
4369 struct smb2_file_alignment_info *file_info;
4370
4371 file_info = (struct smb2_file_alignment_info *)rsp->Buffer;
4372 file_info->AlignmentRequirement = 0;
4373 rsp->OutputBufferLength =
4374 cpu_to_le32(sizeof(struct smb2_file_alignment_info));
4375 inc_rfc1001_len(rsp_org,
4376 sizeof(struct smb2_file_alignment_info));
4377}
4378
4379static int get_file_all_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004380 struct smb2_query_info_rsp *rsp,
4381 struct ksmbd_file *fp,
4382 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004383{
4384 struct ksmbd_conn *conn = work->conn;
4385 struct smb2_file_all_info *file_info;
4386 unsigned int delete_pending;
4387 struct inode *inode;
4388 struct kstat stat;
4389 int conv_len;
4390 char *filename;
4391 u64 time;
4392
4393 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4394 ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004395 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004396 return -EACCES;
4397 }
4398
Hyunchul Lee265fd192021-09-25 00:06:16 +09004399 filename = convert_to_nt_pathname(fp->filename);
Namjae Jeone2f34482021-03-16 10:49:09 +09004400 if (!filename)
4401 return -ENOMEM;
4402
Namjae Jeonab0b2632021-06-29 09:20:13 +09004403 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004404 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004405
4406 ksmbd_debug(SMB, "filename = %s\n", filename);
4407 delete_pending = ksmbd_inode_pending_delete(fp);
4408 file_info = (struct smb2_file_all_info *)rsp->Buffer;
4409
4410 file_info->CreationTime = cpu_to_le64(fp->create_time);
4411 time = ksmbd_UnixTimeToNT(stat.atime);
4412 file_info->LastAccessTime = cpu_to_le64(time);
4413 time = ksmbd_UnixTimeToNT(stat.mtime);
4414 file_info->LastWriteTime = cpu_to_le64(time);
4415 time = ksmbd_UnixTimeToNT(stat.ctime);
4416 file_info->ChangeTime = cpu_to_le64(time);
4417 file_info->Attributes = fp->f_ci->m_fattr;
4418 file_info->Pad1 = 0;
4419 file_info->AllocationSize =
4420 cpu_to_le64(get_allocation_size(inode, &stat));
4421 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4422 file_info->NumberOfLinks =
4423 cpu_to_le32(get_nlink(&stat) - delete_pending);
4424 file_info->DeletePending = delete_pending;
4425 file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4426 file_info->Pad2 = 0;
4427 file_info->IndexNumber = cpu_to_le64(stat.ino);
4428 file_info->EASize = 0;
4429 file_info->AccessFlags = fp->daccess;
4430 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4431 file_info->Mode = fp->coption;
4432 file_info->AlignmentRequirement = 0;
Namjae Jeon070fb212021-05-26 17:57:12 +09004433 conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename,
4434 PATH_MAX, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004435 conv_len *= 2;
4436 file_info->FileNameLength = cpu_to_le32(conv_len);
4437 rsp->OutputBufferLength =
4438 cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1);
4439 kfree(filename);
4440 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4441 return 0;
4442}
4443
4444static void get_file_alternate_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004445 struct smb2_query_info_rsp *rsp,
4446 struct ksmbd_file *fp,
4447 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004448{
4449 struct ksmbd_conn *conn = work->conn;
4450 struct smb2_file_alt_name_info *file_info;
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004451 struct dentry *dentry = fp->filp->f_path.dentry;
Namjae Jeone2f34482021-03-16 10:49:09 +09004452 int conv_len;
Namjae Jeone2f34482021-03-16 10:49:09 +09004453
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004454 spin_lock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004455 file_info = (struct smb2_file_alt_name_info *)rsp->Buffer;
4456 conv_len = ksmbd_extract_shortname(conn,
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004457 dentry->d_name.name,
Namjae Jeone2f34482021-03-16 10:49:09 +09004458 file_info->FileName);
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004459 spin_unlock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004460 file_info->FileNameLength = cpu_to_le32(conv_len);
4461 rsp->OutputBufferLength =
4462 cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len);
4463 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4464}
4465
4466static void get_file_stream_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004467 struct smb2_query_info_rsp *rsp,
4468 struct ksmbd_file *fp,
4469 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004470{
4471 struct ksmbd_conn *conn = work->conn;
4472 struct smb2_file_stream_info *file_info;
4473 char *stream_name, *xattr_list = NULL, *stream_buf;
4474 struct kstat stat;
4475 struct path *path = &fp->filp->f_path;
4476 ssize_t xattr_list_len;
4477 int nbytes = 0, streamlen, stream_name_len, next, idx = 0;
Hyunchul Lee34061d62021-10-16 08:39:54 +09004478 int buf_free_len;
4479 struct smb2_query_info_req *req = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09004480
Hyunchul Leeaf349832021-06-30 18:25:53 +09004481 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4482 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004483 file_info = (struct smb2_file_stream_info *)rsp->Buffer;
4484
Namjae Jeon1ec721532021-11-21 11:32:39 +09004485 buf_free_len =
4486 smb2_calc_max_out_buf_len(work, 8,
4487 le32_to_cpu(req->OutputBufferLength));
4488 if (buf_free_len < 0)
4489 goto out;
4490
Namjae Jeone2f34482021-03-16 10:49:09 +09004491 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4492 if (xattr_list_len < 0) {
4493 goto out;
4494 } else if (!xattr_list_len) {
4495 ksmbd_debug(SMB, "empty xattr in the file\n");
4496 goto out;
4497 }
4498
4499 while (idx < xattr_list_len) {
4500 stream_name = xattr_list + idx;
4501 streamlen = strlen(stream_name);
4502 idx += streamlen + 1;
4503
4504 ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen);
4505
4506 if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004507 STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004508 continue;
4509
4510 stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN +
4511 STREAM_PREFIX_LEN);
4512 streamlen = stream_name_len;
4513
4514 /* plus : size */
4515 streamlen += 1;
4516 stream_buf = kmalloc(streamlen + 1, GFP_KERNEL);
4517 if (!stream_buf)
4518 break;
4519
4520 streamlen = snprintf(stream_buf, streamlen + 1,
Namjae Jeon070fb212021-05-26 17:57:12 +09004521 ":%s", &stream_name[XATTR_NAME_STREAM_LEN]);
Namjae Jeone2f34482021-03-16 10:49:09 +09004522
Hyunchul Lee34061d62021-10-16 08:39:54 +09004523 next = sizeof(struct smb2_file_stream_info) + streamlen * 2;
Namjae Jeon178ca6f2021-11-24 10:23:02 +09004524 if (next > buf_free_len) {
4525 kfree(stream_buf);
Hyunchul Lee34061d62021-10-16 08:39:54 +09004526 break;
Namjae Jeon178ca6f2021-11-24 10:23:02 +09004527 }
Hyunchul Lee34061d62021-10-16 08:39:54 +09004528
Namjae Jeon070fb212021-05-26 17:57:12 +09004529 file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes];
Namjae Jeone2f34482021-03-16 10:49:09 +09004530 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004531 stream_buf, streamlen,
4532 conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004533 streamlen *= 2;
4534 kfree(stream_buf);
4535 file_info->StreamNameLength = cpu_to_le32(streamlen);
4536 file_info->StreamSize = cpu_to_le64(stream_name_len);
4537 file_info->StreamAllocationSize = cpu_to_le64(stream_name_len);
4538
Namjae Jeone2f34482021-03-16 10:49:09 +09004539 nbytes += next;
Hyunchul Lee34061d62021-10-16 08:39:54 +09004540 buf_free_len -= next;
Namjae Jeone2f34482021-03-16 10:49:09 +09004541 file_info->NextEntryOffset = cpu_to_le32(next);
4542 }
4543
Namjae Jeon1ec721532021-11-21 11:32:39 +09004544out:
Hyunchul Lee34061d62021-10-16 08:39:54 +09004545 if (!S_ISDIR(stat.mode) &&
4546 buf_free_len >= sizeof(struct smb2_file_stream_info) + 7 * 2) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004547 file_info = (struct smb2_file_stream_info *)
4548 &rsp->Buffer[nbytes];
4549 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004550 "::$DATA", 7, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004551 streamlen *= 2;
4552 file_info->StreamNameLength = cpu_to_le32(streamlen);
Namjae Jeon1ec721532021-11-21 11:32:39 +09004553 file_info->StreamSize = cpu_to_le64(stat.size);
4554 file_info->StreamAllocationSize = cpu_to_le64(stat.blocks << 9);
Namjae Jeone2f34482021-03-16 10:49:09 +09004555 nbytes += sizeof(struct smb2_file_stream_info) + streamlen;
4556 }
4557
4558 /* last entry offset should be 0 */
4559 file_info->NextEntryOffset = 0;
Namjae Jeon79f6b112021-04-02 12:47:14 +09004560 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004561
4562 rsp->OutputBufferLength = cpu_to_le32(nbytes);
4563 inc_rfc1001_len(rsp_org, nbytes);
4564}
4565
4566static void get_file_internal_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004567 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004568{
4569 struct smb2_file_internal_info *file_info;
4570 struct kstat stat;
4571
Hyunchul Leeaf349832021-06-30 18:25:53 +09004572 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4573 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004574 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4575 file_info->IndexNumber = cpu_to_le64(stat.ino);
4576 rsp->OutputBufferLength =
4577 cpu_to_le32(sizeof(struct smb2_file_internal_info));
4578 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
4579}
4580
4581static int get_file_network_open_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004582 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004583{
4584 struct smb2_file_ntwrk_info *file_info;
4585 struct inode *inode;
4586 struct kstat stat;
4587 u64 time;
4588
4589 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004590 pr_err("no right to read the attributes : 0x%x\n",
4591 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004592 return -EACCES;
4593 }
4594
4595 file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer;
4596
Namjae Jeonab0b2632021-06-29 09:20:13 +09004597 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004598 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004599
4600 file_info->CreationTime = cpu_to_le64(fp->create_time);
4601 time = ksmbd_UnixTimeToNT(stat.atime);
4602 file_info->LastAccessTime = cpu_to_le64(time);
4603 time = ksmbd_UnixTimeToNT(stat.mtime);
4604 file_info->LastWriteTime = cpu_to_le64(time);
4605 time = ksmbd_UnixTimeToNT(stat.ctime);
4606 file_info->ChangeTime = cpu_to_le64(time);
4607 file_info->Attributes = fp->f_ci->m_fattr;
4608 file_info->AllocationSize =
4609 cpu_to_le64(get_allocation_size(inode, &stat));
4610 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4611 file_info->Reserved = cpu_to_le32(0);
4612 rsp->OutputBufferLength =
4613 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info));
4614 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info));
4615 return 0;
4616}
4617
Namjae Jeon64b39f42021-03-30 14:25:35 +09004618static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004619{
4620 struct smb2_file_ea_info *file_info;
4621
4622 file_info = (struct smb2_file_ea_info *)rsp->Buffer;
4623 file_info->EASize = 0;
4624 rsp->OutputBufferLength =
4625 cpu_to_le32(sizeof(struct smb2_file_ea_info));
4626 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info));
4627}
4628
4629static void get_file_position_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004630 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004631{
4632 struct smb2_file_pos_info *file_info;
4633
4634 file_info = (struct smb2_file_pos_info *)rsp->Buffer;
4635 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4636 rsp->OutputBufferLength =
4637 cpu_to_le32(sizeof(struct smb2_file_pos_info));
4638 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info));
4639}
4640
4641static void get_file_mode_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004642 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004643{
4644 struct smb2_file_mode_info *file_info;
4645
4646 file_info = (struct smb2_file_mode_info *)rsp->Buffer;
4647 file_info->Mode = fp->coption & FILE_MODE_INFO_MASK;
4648 rsp->OutputBufferLength =
4649 cpu_to_le32(sizeof(struct smb2_file_mode_info));
4650 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info));
4651}
4652
4653static void get_file_compression_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004654 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004655{
4656 struct smb2_file_comp_info *file_info;
4657 struct kstat stat;
4658
Hyunchul Leeaf349832021-06-30 18:25:53 +09004659 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4660 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004661
4662 file_info = (struct smb2_file_comp_info *)rsp->Buffer;
4663 file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9);
4664 file_info->CompressionFormat = COMPRESSION_FORMAT_NONE;
4665 file_info->CompressionUnitShift = 0;
4666 file_info->ChunkShift = 0;
4667 file_info->ClusterShift = 0;
4668 memset(&file_info->Reserved[0], 0, 3);
4669
4670 rsp->OutputBufferLength =
4671 cpu_to_le32(sizeof(struct smb2_file_comp_info));
4672 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info));
4673}
4674
4675static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004676 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004677{
4678 struct smb2_file_attr_tag_info *file_info;
4679
4680 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004681 pr_err("no right to read the attributes : 0x%x\n",
4682 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004683 return -EACCES;
4684 }
4685
4686 file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer;
4687 file_info->FileAttributes = fp->f_ci->m_fattr;
4688 file_info->ReparseTag = 0;
4689 rsp->OutputBufferLength =
4690 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info));
Namjae Jeon070fb212021-05-26 17:57:12 +09004691 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004692 return 0;
4693}
4694
4695static int find_file_posix_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004696 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004697{
4698 struct smb311_posix_qinfo *file_info;
Namjae Jeonab0b2632021-06-29 09:20:13 +09004699 struct inode *inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09004700 u64 time;
4701
4702 file_info = (struct smb311_posix_qinfo *)rsp->Buffer;
4703 file_info->CreationTime = cpu_to_le64(fp->create_time);
4704 time = ksmbd_UnixTimeToNT(inode->i_atime);
4705 file_info->LastAccessTime = cpu_to_le64(time);
4706 time = ksmbd_UnixTimeToNT(inode->i_mtime);
4707 file_info->LastWriteTime = cpu_to_le64(time);
4708 time = ksmbd_UnixTimeToNT(inode->i_ctime);
4709 file_info->ChangeTime = cpu_to_le64(time);
4710 file_info->DosAttributes = fp->f_ci->m_fattr;
4711 file_info->Inode = cpu_to_le64(inode->i_ino);
4712 file_info->EndOfFile = cpu_to_le64(inode->i_size);
4713 file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9);
4714 file_info->HardLinks = cpu_to_le32(inode->i_nlink);
4715 file_info->Mode = cpu_to_le32(inode->i_mode);
4716 file_info->DeviceId = cpu_to_le32(inode->i_rdev);
4717 rsp->OutputBufferLength =
4718 cpu_to_le32(sizeof(struct smb311_posix_qinfo));
Namjae Jeon64b39f42021-03-30 14:25:35 +09004719 inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo));
Namjae Jeone2f34482021-03-16 10:49:09 +09004720 return 0;
4721}
4722
Namjae Jeone2f34482021-03-16 10:49:09 +09004723static int smb2_get_info_file(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004724 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004725 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09004726{
4727 struct ksmbd_file *fp;
4728 int fileinfoclass = 0;
4729 int rc = 0;
4730 int file_infoclass_size;
4731 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4732
4733 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004734 KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004735 /* smb2 info file called for pipe */
Namjae Jeoncb451722021-11-03 08:08:44 +09004736 return smb2_get_info_file_pipe(work->sess, req, rsp,
4737 work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004738 }
4739
4740 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon38673692021-07-08 12:32:27 +09004741 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
4742 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004743 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004744 id = work->compound_fid;
4745 pid = work->compound_pfid;
4746 }
4747 }
4748
Namjae Jeon38673692021-07-08 12:32:27 +09004749 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004750 id = le64_to_cpu(req->VolatileFileId);
4751 pid = le64_to_cpu(req->PersistentFileId);
4752 }
4753
4754 fp = ksmbd_lookup_fd_slow(work, id, pid);
4755 if (!fp)
4756 return -ENOENT;
4757
4758 fileinfoclass = req->FileInfoClass;
4759
4760 switch (fileinfoclass) {
4761 case FILE_ACCESS_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004762 get_file_access_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004763 file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE;
4764 break;
4765
4766 case FILE_BASIC_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004767 rc = get_file_basic_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004768 file_infoclass_size = FILE_BASIC_INFORMATION_SIZE;
4769 break;
4770
4771 case FILE_STANDARD_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004772 get_file_standard_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004773 file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE;
4774 break;
4775
4776 case FILE_ALIGNMENT_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004777 get_file_alignment_info(rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004778 file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE;
4779 break;
4780
4781 case FILE_ALL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004782 rc = get_file_all_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004783 file_infoclass_size = FILE_ALL_INFORMATION_SIZE;
4784 break;
4785
4786 case FILE_ALTERNATE_NAME_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004787 get_file_alternate_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004788 file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE;
4789 break;
4790
4791 case FILE_STREAM_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004792 get_file_stream_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004793 file_infoclass_size = FILE_STREAM_INFORMATION_SIZE;
4794 break;
4795
4796 case FILE_INTERNAL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004797 get_file_internal_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004798 file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE;
4799 break;
4800
4801 case FILE_NETWORK_OPEN_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004802 rc = get_file_network_open_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004803 file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE;
4804 break;
4805
4806 case FILE_EA_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004807 get_file_ea_info(rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004808 file_infoclass_size = FILE_EA_INFORMATION_SIZE;
4809 break;
4810
4811 case FILE_FULL_EA_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004812 rc = smb2_get_ea(work, fp, req, rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004813 file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE;
4814 break;
4815
4816 case FILE_POSITION_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004817 get_file_position_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004818 file_infoclass_size = FILE_POSITION_INFORMATION_SIZE;
4819 break;
4820
4821 case FILE_MODE_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004822 get_file_mode_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004823 file_infoclass_size = FILE_MODE_INFORMATION_SIZE;
4824 break;
4825
4826 case FILE_COMPRESSION_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004827 get_file_compression_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004828 file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE;
4829 break;
4830
4831 case FILE_ATTRIBUTE_TAG_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004832 rc = get_file_attribute_tag_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004833 file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE;
4834 break;
4835 case SMB_FIND_FILE_POSIX_INFO:
4836 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004837 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004838 rc = -EOPNOTSUPP;
4839 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09004840 rc = find_file_posix_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004841 file_infoclass_size = sizeof(struct smb311_posix_qinfo);
4842 }
4843 break;
4844 default:
4845 ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n",
4846 fileinfoclass);
4847 rc = -EOPNOTSUPP;
4848 }
4849 if (!rc)
4850 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004851 rsp, work->response_buf,
Namjae Jeone2f34482021-03-16 10:49:09 +09004852 file_infoclass_size);
4853 ksmbd_fd_put(work, fp);
4854 return rc;
4855}
4856
Namjae Jeone2f34482021-03-16 10:49:09 +09004857static int smb2_get_info_filesystem(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004858 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004859 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09004860{
4861 struct ksmbd_session *sess = work->sess;
4862 struct ksmbd_conn *conn = sess->conn;
4863 struct ksmbd_share_config *share = work->tcon->share_conf;
4864 int fsinfoclass = 0;
4865 struct kstatfs stfs;
4866 struct path path;
4867 int rc = 0, len;
4868 int fs_infoclass_size = 0;
4869
Hyunchul Lee265fd192021-09-25 00:06:16 +09004870 rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09004871 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004872 pr_err("cannot create vfs path\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004873 return -EIO;
4874 }
4875
4876 rc = vfs_statfs(&path, &stfs);
4877 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004878 pr_err("cannot do stat of path %s\n", share->path);
Namjae Jeone2f34482021-03-16 10:49:09 +09004879 path_put(&path);
4880 return -EIO;
4881 }
4882
4883 fsinfoclass = req->FileInfoClass;
4884
4885 switch (fsinfoclass) {
4886 case FS_DEVICE_INFORMATION:
4887 {
4888 struct filesystem_device_info *info;
4889
4890 info = (struct filesystem_device_info *)rsp->Buffer;
4891
4892 info->DeviceType = cpu_to_le32(stfs.f_type);
4893 info->DeviceCharacteristics = cpu_to_le32(0x00000020);
4894 rsp->OutputBufferLength = cpu_to_le32(8);
Namjae Jeoncb451722021-11-03 08:08:44 +09004895 inc_rfc1001_len(work->response_buf, 8);
Namjae Jeone2f34482021-03-16 10:49:09 +09004896 fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE;
4897 break;
4898 }
4899 case FS_ATTRIBUTE_INFORMATION:
4900 {
4901 struct filesystem_attribute_info *info;
4902 size_t sz;
4903
4904 info = (struct filesystem_attribute_info *)rsp->Buffer;
4905 info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS |
4906 FILE_PERSISTENT_ACLS |
4907 FILE_UNICODE_ON_DISK |
4908 FILE_CASE_PRESERVED_NAMES |
Namjae Jeoneb817362021-05-18 10:37:59 +09004909 FILE_CASE_SENSITIVE_SEARCH |
4910 FILE_SUPPORTS_BLOCK_REFCOUNTING);
Namjae Jeone2f34482021-03-16 10:49:09 +09004911
4912 info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps);
4913
4914 info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen);
4915 len = smbConvertToUTF16((__le16 *)info->FileSystemName,
4916 "NTFS", PATH_MAX, conn->local_nls, 0);
4917 len = len * 2;
4918 info->FileSystemNameLen = cpu_to_le32(len);
4919 sz = sizeof(struct filesystem_attribute_info) - 2 + len;
4920 rsp->OutputBufferLength = cpu_to_le32(sz);
Namjae Jeoncb451722021-11-03 08:08:44 +09004921 inc_rfc1001_len(work->response_buf, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09004922 fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE;
4923 break;
4924 }
4925 case FS_VOLUME_INFORMATION:
4926 {
4927 struct filesystem_vol_info *info;
4928 size_t sz;
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004929 unsigned int serial_crc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09004930
4931 info = (struct filesystem_vol_info *)(rsp->Buffer);
4932 info->VolumeCreationTime = 0;
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004933 serial_crc = crc32_le(serial_crc, share->name,
4934 strlen(share->name));
4935 serial_crc = crc32_le(serial_crc, share->path,
4936 strlen(share->path));
4937 serial_crc = crc32_le(serial_crc, ksmbd_netbios_name(),
4938 strlen(ksmbd_netbios_name()));
Namjae Jeone2f34482021-03-16 10:49:09 +09004939 /* Taking dummy value of serial number*/
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004940 info->SerialNumber = cpu_to_le32(serial_crc);
Namjae Jeone2f34482021-03-16 10:49:09 +09004941 len = smbConvertToUTF16((__le16 *)info->VolumeLabel,
4942 share->name, PATH_MAX,
4943 conn->local_nls, 0);
4944 len = len * 2;
4945 info->VolumeLabelSize = cpu_to_le32(len);
4946 info->Reserved = 0;
4947 sz = sizeof(struct filesystem_vol_info) - 2 + len;
4948 rsp->OutputBufferLength = cpu_to_le32(sz);
Namjae Jeoncb451722021-11-03 08:08:44 +09004949 inc_rfc1001_len(work->response_buf, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09004950 fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE;
4951 break;
4952 }
4953 case FS_SIZE_INFORMATION:
4954 {
4955 struct filesystem_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004956
4957 info = (struct filesystem_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004958 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4959 info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004960 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4961 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004962 rsp->OutputBufferLength = cpu_to_le32(24);
Namjae Jeoncb451722021-11-03 08:08:44 +09004963 inc_rfc1001_len(work->response_buf, 24);
Namjae Jeone2f34482021-03-16 10:49:09 +09004964 fs_infoclass_size = FS_SIZE_INFORMATION_SIZE;
4965 break;
4966 }
4967 case FS_FULL_SIZE_INFORMATION:
4968 {
4969 struct smb2_fs_full_size_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004970
4971 info = (struct smb2_fs_full_size_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004972 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4973 info->CallerAvailableAllocationUnits =
4974 cpu_to_le64(stfs.f_bavail);
4975 info->ActualAvailableAllocationUnits =
4976 cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004977 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4978 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004979 rsp->OutputBufferLength = cpu_to_le32(32);
Namjae Jeoncb451722021-11-03 08:08:44 +09004980 inc_rfc1001_len(work->response_buf, 32);
Namjae Jeone2f34482021-03-16 10:49:09 +09004981 fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE;
4982 break;
4983 }
4984 case FS_OBJECT_ID_INFORMATION:
4985 {
4986 struct object_id_info *info;
4987
4988 info = (struct object_id_info *)(rsp->Buffer);
4989
4990 if (!user_guest(sess->user))
4991 memcpy(info->objid, user_passkey(sess->user), 16);
4992 else
4993 memset(info->objid, 0, 16);
4994
4995 info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC);
4996 info->extended_info.version = cpu_to_le32(1);
4997 info->extended_info.release = cpu_to_le32(1);
4998 info->extended_info.rel_date = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004999 memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0"));
Namjae Jeone2f34482021-03-16 10:49:09 +09005000 rsp->OutputBufferLength = cpu_to_le32(64);
Namjae Jeoncb451722021-11-03 08:08:44 +09005001 inc_rfc1001_len(work->response_buf, 64);
Namjae Jeone2f34482021-03-16 10:49:09 +09005002 fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE;
5003 break;
5004 }
5005 case FS_SECTOR_SIZE_INFORMATION:
5006 {
5007 struct smb3_fs_ss_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09005008
5009 info = (struct smb3_fs_ss_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09005010
Namjae Jeon131bac12021-06-22 16:20:47 +09005011 info->LogicalBytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005012 info->PhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09005013 cpu_to_le32(stfs.f_bsize);
5014 info->PhysicalBytesPerSectorForPerf = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005015 info->FSEffPhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09005016 cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005017 info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE |
5018 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE);
5019 info->ByteOffsetForSectorAlignment = 0;
5020 info->ByteOffsetForPartitionAlignment = 0;
5021 rsp->OutputBufferLength = cpu_to_le32(28);
Namjae Jeoncb451722021-11-03 08:08:44 +09005022 inc_rfc1001_len(work->response_buf, 28);
Namjae Jeone2f34482021-03-16 10:49:09 +09005023 fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE;
5024 break;
5025 }
5026 case FS_CONTROL_INFORMATION:
5027 {
5028 /*
5029 * TODO : The current implementation is based on
5030 * test result with win7(NTFS) server. It's need to
5031 * modify this to get valid Quota values
5032 * from Linux kernel
5033 */
5034 struct smb2_fs_control_info *info;
5035
5036 info = (struct smb2_fs_control_info *)(rsp->Buffer);
5037 info->FreeSpaceStartFiltering = 0;
5038 info->FreeSpaceThreshold = 0;
5039 info->FreeSpaceStopFiltering = 0;
5040 info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID);
5041 info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID);
5042 info->Padding = 0;
5043 rsp->OutputBufferLength = cpu_to_le32(48);
Namjae Jeoncb451722021-11-03 08:08:44 +09005044 inc_rfc1001_len(work->response_buf, 48);
Namjae Jeone2f34482021-03-16 10:49:09 +09005045 fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE;
5046 break;
5047 }
5048 case FS_POSIX_INFORMATION:
5049 {
5050 struct filesystem_posix_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09005051
5052 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005053 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005054 rc = -EOPNOTSUPP;
5055 } else {
5056 info = (struct filesystem_posix_info *)(rsp->Buffer);
Namjae Jeonee81cae2021-06-26 22:32:34 +09005057 info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005058 info->BlockSize = cpu_to_le32(stfs.f_bsize);
5059 info->TotalBlocks = cpu_to_le64(stfs.f_blocks);
5060 info->BlocksAvail = cpu_to_le64(stfs.f_bfree);
5061 info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail);
5062 info->TotalFileNodes = cpu_to_le64(stfs.f_files);
5063 info->FreeFileNodes = cpu_to_le64(stfs.f_ffree);
5064 rsp->OutputBufferLength = cpu_to_le32(56);
Namjae Jeoncb451722021-11-03 08:08:44 +09005065 inc_rfc1001_len(work->response_buf, 56);
Namjae Jeone2f34482021-03-16 10:49:09 +09005066 fs_infoclass_size = FS_POSIX_INFORMATION_SIZE;
5067 }
5068 break;
5069 }
5070 default:
5071 path_put(&path);
5072 return -EOPNOTSUPP;
5073 }
5074 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09005075 rsp, work->response_buf,
Namjae Jeone2f34482021-03-16 10:49:09 +09005076 fs_infoclass_size);
5077 path_put(&path);
5078 return rc;
5079}
5080
5081static int smb2_get_info_sec(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005082 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09005083 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09005084{
5085 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005086 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09005087 struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL;
5088 struct smb_fattr fattr = {{0}};
5089 struct inode *inode;
5090 __u32 secdesclen;
5091 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5092 int addition_info = le32_to_cpu(req->AdditionalInformation);
5093 int rc;
5094
Namjae Jeone294f782021-06-28 15:26:37 +09005095 if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
5096 PROTECTED_DACL_SECINFO |
5097 UNPROTECTED_DACL_SECINFO)) {
Namjae Jeon8e537d12021-11-21 07:48:45 +09005098 ksmbd_debug(SMB, "Unsupported addition info: 0x%x)\n",
Namjae Jeone294f782021-06-28 15:26:37 +09005099 addition_info);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09005100
5101 pntsd->revision = cpu_to_le16(1);
5102 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED);
5103 pntsd->osidoffset = 0;
5104 pntsd->gsidoffset = 0;
5105 pntsd->sacloffset = 0;
5106 pntsd->dacloffset = 0;
5107
5108 secdesclen = sizeof(struct smb_ntsd);
5109 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
Namjae Jeoncb451722021-11-03 08:08:44 +09005110 inc_rfc1001_len(work->response_buf, secdesclen);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09005111
5112 return 0;
5113 }
5114
Namjae Jeone2f34482021-03-16 10:49:09 +09005115 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon38673692021-07-08 12:32:27 +09005116 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
5117 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005118 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005119 id = work->compound_fid;
5120 pid = work->compound_pfid;
5121 }
5122 }
5123
Namjae Jeon38673692021-07-08 12:32:27 +09005124 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005125 id = le64_to_cpu(req->VolatileFileId);
5126 pid = le64_to_cpu(req->PersistentFileId);
5127 }
5128
5129 fp = ksmbd_lookup_fd_slow(work, id, pid);
5130 if (!fp)
5131 return -ENOENT;
5132
Hyunchul Lee465d7202021-07-03 12:10:36 +09005133 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeonab0b2632021-06-29 09:20:13 +09005134 inode = file_inode(fp->filp);
Christian Brauner43205ca2021-08-23 17:13:50 +02005135 ksmbd_acls_fattr(&fattr, user_ns, inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09005136
5137 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09005138 KSMBD_SHARE_FLAG_ACL_XATTR))
Hyunchul Lee465d7202021-07-03 12:10:36 +09005139 ksmbd_vfs_get_sd_xattr(work->conn, user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005140 fp->filp->f_path.dentry, &ppntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09005141
Hyunchul Lee465d7202021-07-03 12:10:36 +09005142 rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info,
5143 &secdesclen, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09005144 posix_acl_release(fattr.cf_acls);
5145 posix_acl_release(fattr.cf_dacls);
5146 kfree(ppntsd);
5147 ksmbd_fd_put(work, fp);
5148 if (rc)
5149 return rc;
5150
5151 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
Namjae Jeoncb451722021-11-03 08:08:44 +09005152 inc_rfc1001_len(work->response_buf, secdesclen);
Namjae Jeone2f34482021-03-16 10:49:09 +09005153 return 0;
5154}
5155
5156/**
5157 * smb2_query_info() - handler for smb2 query info command
5158 * @work: smb work containing query info request buffer
5159 *
5160 * Return: 0 on success, otherwise error
5161 */
5162int smb2_query_info(struct ksmbd_work *work)
5163{
5164 struct smb2_query_info_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09005165 struct smb2_query_info_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005166 int rc = 0;
5167
Namjae Jeone2f34482021-03-16 10:49:09 +09005168 WORK_BUFFERS(work, req, rsp);
5169
5170 ksmbd_debug(SMB, "GOT query info request\n");
5171
5172 switch (req->InfoType) {
5173 case SMB2_O_INFO_FILE:
5174 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005175 rc = smb2_get_info_file(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005176 break;
5177 case SMB2_O_INFO_FILESYSTEM:
5178 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005179 rc = smb2_get_info_filesystem(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005180 break;
5181 case SMB2_O_INFO_SECURITY:
5182 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005183 rc = smb2_get_info_sec(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005184 break;
5185 default:
5186 ksmbd_debug(SMB, "InfoType %d not supported yet\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005187 req->InfoType);
Namjae Jeone2f34482021-03-16 10:49:09 +09005188 rc = -EOPNOTSUPP;
5189 }
5190
5191 if (rc < 0) {
5192 if (rc == -EACCES)
5193 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5194 else if (rc == -ENOENT)
5195 rsp->hdr.Status = STATUS_FILE_CLOSED;
5196 else if (rc == -EIO)
5197 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5198 else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0)
5199 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5200 smb2_set_err_rsp(work);
5201
5202 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005203 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005204 return rc;
5205 }
5206 rsp->StructureSize = cpu_to_le16(9);
5207 rsp->OutputBufferOffset = cpu_to_le16(72);
Namjae Jeoncb451722021-11-03 08:08:44 +09005208 inc_rfc1001_len(work->response_buf, 8);
Namjae Jeone2f34482021-03-16 10:49:09 +09005209 return 0;
5210}
5211
5212/**
5213 * smb2_close_pipe() - handler for closing IPC pipe
5214 * @work: smb work containing close request buffer
5215 *
5216 * Return: 0
5217 */
5218static noinline int smb2_close_pipe(struct ksmbd_work *work)
5219{
Namjae Jeon64b39f42021-03-30 14:25:35 +09005220 u64 id;
Namjae Jeoncb451722021-11-03 08:08:44 +09005221 struct smb2_close_req *req = smb2_get_msg(work->request_buf);
5222 struct smb2_close_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005223
5224 id = le64_to_cpu(req->VolatileFileId);
5225 ksmbd_session_rpc_close(work->sess, id);
5226
5227 rsp->StructureSize = cpu_to_le16(60);
5228 rsp->Flags = 0;
5229 rsp->Reserved = 0;
5230 rsp->CreationTime = 0;
5231 rsp->LastAccessTime = 0;
5232 rsp->LastWriteTime = 0;
5233 rsp->ChangeTime = 0;
5234 rsp->AllocationSize = 0;
5235 rsp->EndOfFile = 0;
5236 rsp->Attributes = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09005237 inc_rfc1001_len(work->response_buf, 60);
Namjae Jeone2f34482021-03-16 10:49:09 +09005238 return 0;
5239}
5240
5241/**
5242 * smb2_close() - handler for smb2 close file command
5243 * @work: smb work containing close request buffer
5244 *
5245 * Return: 0
5246 */
5247int smb2_close(struct ksmbd_work *work)
5248{
Namjae Jeon38673692021-07-08 12:32:27 +09005249 u64 volatile_id = KSMBD_NO_FID;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005250 u64 sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09005251 struct smb2_close_req *req;
5252 struct smb2_close_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005253 struct ksmbd_conn *conn = work->conn;
5254 struct ksmbd_file *fp;
5255 struct inode *inode;
5256 u64 time;
5257 int err = 0;
5258
Namjae Jeone2f34482021-03-16 10:49:09 +09005259 WORK_BUFFERS(work, req, rsp);
5260
5261 if (test_share_config_flag(work->tcon->share_conf,
5262 KSMBD_SHARE_FLAG_PIPE)) {
5263 ksmbd_debug(SMB, "IPC pipe close request\n");
5264 return smb2_close_pipe(work);
5265 }
5266
5267 sess_id = le64_to_cpu(req->hdr.SessionId);
5268 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5269 sess_id = work->compound_sid;
5270
5271 work->compound_sid = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005272 if (check_session_id(conn, sess_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005273 work->compound_sid = sess_id;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005274 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005275 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
5276 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5277 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5278 err = -EBADF;
5279 goto out;
5280 }
5281
5282 if (work->next_smb2_rcv_hdr_off &&
Namjae Jeon38673692021-07-08 12:32:27 +09005283 !has_file_id(le64_to_cpu(req->VolatileFileId))) {
5284 if (!has_file_id(work->compound_fid)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005285 /* file already closed, return FILE_CLOSED */
5286 ksmbd_debug(SMB, "file already closed\n");
5287 rsp->hdr.Status = STATUS_FILE_CLOSED;
5288 err = -EBADF;
5289 goto out;
5290 } else {
Namjae Jeon38673692021-07-08 12:32:27 +09005291 ksmbd_debug(SMB,
5292 "Compound request set FID = %llu:%llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005293 work->compound_fid,
5294 work->compound_pfid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005295 volatile_id = work->compound_fid;
5296
5297 /* file closed, stored id is not valid anymore */
5298 work->compound_fid = KSMBD_NO_FID;
5299 work->compound_pfid = KSMBD_NO_FID;
5300 }
5301 } else {
5302 volatile_id = le64_to_cpu(req->VolatileFileId);
5303 }
Namjae Jeon38673692021-07-08 12:32:27 +09005304 ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id);
Namjae Jeone2f34482021-03-16 10:49:09 +09005305
5306 rsp->StructureSize = cpu_to_le16(60);
5307 rsp->Reserved = 0;
5308
5309 if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) {
5310 fp = ksmbd_lookup_fd_fast(work, volatile_id);
5311 if (!fp) {
5312 err = -ENOENT;
5313 goto out;
5314 }
5315
Namjae Jeonab0b2632021-06-29 09:20:13 +09005316 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005317 rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
5318 rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 :
5319 cpu_to_le64(inode->i_blocks << 9);
5320 rsp->EndOfFile = cpu_to_le64(inode->i_size);
5321 rsp->Attributes = fp->f_ci->m_fattr;
5322 rsp->CreationTime = cpu_to_le64(fp->create_time);
5323 time = ksmbd_UnixTimeToNT(inode->i_atime);
5324 rsp->LastAccessTime = cpu_to_le64(time);
5325 time = ksmbd_UnixTimeToNT(inode->i_mtime);
5326 rsp->LastWriteTime = cpu_to_le64(time);
5327 time = ksmbd_UnixTimeToNT(inode->i_ctime);
5328 rsp->ChangeTime = cpu_to_le64(time);
5329 ksmbd_fd_put(work, fp);
5330 } else {
5331 rsp->Flags = 0;
5332 rsp->AllocationSize = 0;
5333 rsp->EndOfFile = 0;
5334 rsp->Attributes = 0;
5335 rsp->CreationTime = 0;
5336 rsp->LastAccessTime = 0;
5337 rsp->LastWriteTime = 0;
5338 rsp->ChangeTime = 0;
5339 }
5340
5341 err = ksmbd_close_fd(work, volatile_id);
5342out:
5343 if (err) {
5344 if (rsp->hdr.Status == 0)
5345 rsp->hdr.Status = STATUS_FILE_CLOSED;
5346 smb2_set_err_rsp(work);
5347 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09005348 inc_rfc1001_len(work->response_buf, 60);
Namjae Jeone2f34482021-03-16 10:49:09 +09005349 }
5350
5351 return 0;
5352}
5353
5354/**
5355 * smb2_echo() - handler for smb2 echo(ping) command
5356 * @work: smb work containing echo request buffer
5357 *
5358 * Return: 0
5359 */
5360int smb2_echo(struct ksmbd_work *work)
5361{
Namjae Jeoncb451722021-11-03 08:08:44 +09005362 struct smb2_echo_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005363
5364 rsp->StructureSize = cpu_to_le16(4);
5365 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09005366 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09005367 return 0;
5368}
5369
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005370static int smb2_rename(struct ksmbd_work *work,
5371 struct ksmbd_file *fp,
5372 struct user_namespace *user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +09005373 struct smb2_file_rename_info *file_info,
5374 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005375{
5376 struct ksmbd_share_config *share = fp->tcon->share_conf;
5377 char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL;
5378 char *pathname = NULL;
5379 struct path path;
5380 bool file_present = true;
5381 int rc;
5382
5383 ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n");
5384 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5385 if (!pathname)
5386 return -ENOMEM;
5387
5388 abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
5389 if (IS_ERR(abs_oldname)) {
5390 rc = -EINVAL;
5391 goto out;
5392 }
5393 old_name = strrchr(abs_oldname, '/');
Namjae Jeon64b39f42021-03-30 14:25:35 +09005394 if (old_name && old_name[1] != '\0') {
Namjae Jeone2f34482021-03-16 10:49:09 +09005395 old_name++;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005396 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005397 ksmbd_debug(SMB, "can't get last component in path %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005398 abs_oldname);
Namjae Jeone2f34482021-03-16 10:49:09 +09005399 rc = -ENOENT;
5400 goto out;
5401 }
5402
Marios Makassikis80917f12021-12-01 21:41:19 +01005403 new_name = smb2_get_name(file_info->FileName,
Namjae Jeone2f34482021-03-16 10:49:09 +09005404 le32_to_cpu(file_info->FileNameLength),
5405 local_nls);
5406 if (IS_ERR(new_name)) {
5407 rc = PTR_ERR(new_name);
5408 goto out;
5409 }
5410
5411 if (strchr(new_name, ':')) {
5412 int s_type;
5413 char *xattr_stream_name, *stream_name = NULL;
5414 size_t xattr_stream_size;
5415 int len;
5416
5417 rc = parse_stream_name(new_name, &stream_name, &s_type);
5418 if (rc < 0)
5419 goto out;
5420
5421 len = strlen(new_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005422 if (len > 0 && new_name[len - 1] != '/') {
Namjae Jeonbde16942021-06-28 15:23:19 +09005423 pr_err("not allow base filename in rename\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005424 rc = -ESHARE;
5425 goto out;
5426 }
5427
5428 rc = ksmbd_vfs_xattr_stream_name(stream_name,
5429 &xattr_stream_name,
5430 &xattr_stream_size,
5431 s_type);
5432 if (rc)
5433 goto out;
5434
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005435 rc = ksmbd_vfs_setxattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005436 fp->filp->f_path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09005437 xattr_stream_name,
5438 NULL, 0, 0);
5439 if (rc < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005440 pr_err("failed to store stream name in xattr: %d\n",
5441 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005442 rc = -EINVAL;
5443 goto out;
5444 }
5445
5446 goto out;
5447 }
5448
5449 ksmbd_debug(SMB, "new name %s\n", new_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005450 rc = ksmbd_vfs_kern_path(work, new_name, LOOKUP_NO_SYMLINKS, &path, 1);
5451 if (rc) {
5452 if (rc != -ENOENT)
5453 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005454 file_present = false;
Hyunchul Lee265fd192021-09-25 00:06:16 +09005455 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005456 path_put(&path);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005457 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005458
5459 if (ksmbd_share_veto_filename(share, new_name)) {
5460 rc = -ENOENT;
5461 ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name);
5462 goto out;
5463 }
5464
5465 if (file_info->ReplaceIfExists) {
5466 if (file_present) {
5467 rc = ksmbd_vfs_remove_file(work, new_name);
5468 if (rc) {
5469 if (rc != -ENOTEMPTY)
5470 rc = -EINVAL;
5471 ksmbd_debug(SMB, "cannot delete %s, rc %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005472 new_name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005473 goto out;
5474 }
5475 }
5476 } else {
5477 if (file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005478 strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005479 rc = -EEXIST;
5480 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005481 "cannot rename already existing file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005482 goto out;
5483 }
5484 }
5485
5486 rc = ksmbd_vfs_fp_rename(work, fp, new_name);
5487out:
5488 kfree(pathname);
5489 if (!IS_ERR(new_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005490 kfree(new_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005491 return rc;
5492}
5493
Namjae Jeone2f34482021-03-16 10:49:09 +09005494static int smb2_create_link(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005495 struct ksmbd_share_config *share,
5496 struct smb2_file_link_info *file_info,
Namjae Jeon9496e262021-09-29 15:41:48 +09005497 unsigned int buf_len, struct file *filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005498 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005499{
5500 char *link_name = NULL, *target_name = NULL, *pathname = NULL;
5501 struct path path;
5502 bool file_present = true;
5503 int rc;
5504
Namjae Jeon9496e262021-09-29 15:41:48 +09005505 if (buf_len < (u64)sizeof(struct smb2_file_link_info) +
5506 le32_to_cpu(file_info->FileNameLength))
5507 return -EINVAL;
5508
Namjae Jeone2f34482021-03-16 10:49:09 +09005509 ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n");
5510 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5511 if (!pathname)
5512 return -ENOMEM;
5513
Marios Makassikis80917f12021-12-01 21:41:19 +01005514 link_name = smb2_get_name(file_info->FileName,
Namjae Jeone2f34482021-03-16 10:49:09 +09005515 le32_to_cpu(file_info->FileNameLength),
5516 local_nls);
5517 if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) {
5518 rc = -EINVAL;
5519 goto out;
5520 }
5521
5522 ksmbd_debug(SMB, "link name is %s\n", link_name);
5523 target_name = d_path(&filp->f_path, pathname, PATH_MAX);
5524 if (IS_ERR(target_name)) {
5525 rc = -EINVAL;
5526 goto out;
5527 }
5528
5529 ksmbd_debug(SMB, "target name is %s\n", target_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005530 rc = ksmbd_vfs_kern_path(work, link_name, LOOKUP_NO_SYMLINKS, &path, 0);
5531 if (rc) {
5532 if (rc != -ENOENT)
5533 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005534 file_present = false;
Hyunchul Lee265fd192021-09-25 00:06:16 +09005535 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005536 path_put(&path);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005537 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005538
5539 if (file_info->ReplaceIfExists) {
5540 if (file_present) {
5541 rc = ksmbd_vfs_remove_file(work, link_name);
5542 if (rc) {
5543 rc = -EINVAL;
5544 ksmbd_debug(SMB, "cannot delete %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005545 link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005546 goto out;
5547 }
5548 }
5549 } else {
5550 if (file_present) {
5551 rc = -EEXIST;
5552 ksmbd_debug(SMB, "link already exists\n");
5553 goto out;
5554 }
5555 }
5556
5557 rc = ksmbd_vfs_link(work, target_name, link_name);
5558 if (rc)
5559 rc = -EINVAL;
5560out:
5561 if (!IS_ERR(link_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005562 kfree(link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005563 kfree(pathname);
5564 return rc;
5565}
5566
Namjae Jeon9496e262021-09-29 15:41:48 +09005567static int set_file_basic_info(struct ksmbd_file *fp,
5568 struct smb2_file_basic_info *file_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005569 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005570{
Namjae Jeone2f34482021-03-16 10:49:09 +09005571 struct iattr attrs;
Namjae Jeone2f34482021-03-16 10:49:09 +09005572 struct file *filp;
5573 struct inode *inode;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005574 struct user_namespace *user_ns;
Namjae Jeon4ffd5262021-09-07 08:15:21 +09005575 int rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09005576
Marios Makassikis7adfd4f2021-04-27 15:30:22 +09005577 if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005578 return -EACCES;
5579
Namjae Jeone2f34482021-03-16 10:49:09 +09005580 attrs.ia_valid = 0;
5581 filp = fp->filp;
5582 inode = file_inode(filp);
Hyunchul Lee465d7202021-07-03 12:10:36 +09005583 user_ns = file_mnt_user_ns(filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005584
5585 if (file_info->CreationTime)
5586 fp->create_time = le64_to_cpu(file_info->CreationTime);
5587
5588 if (file_info->LastAccessTime) {
5589 attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime);
5590 attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
5591 }
5592
Namjae Jeon64e78752021-10-03 13:19:00 +09005593 attrs.ia_valid |= ATTR_CTIME;
5594 if (file_info->ChangeTime)
Christian Braunerdb7fb6f2021-08-26 10:07:05 +09005595 attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime);
Namjae Jeon64e78752021-10-03 13:19:00 +09005596 else
5597 attrs.ia_ctime = inode->i_ctime;
Namjae Jeone2f34482021-03-16 10:49:09 +09005598
5599 if (file_info->LastWriteTime) {
5600 attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime);
5601 attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
5602 }
5603
5604 if (file_info->Attributes) {
5605 if (!S_ISDIR(inode->i_mode) &&
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09005606 file_info->Attributes & FILE_ATTRIBUTE_DIRECTORY_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005607 pr_err("can't change a file to a directory\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005608 return -EINVAL;
5609 }
5610
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09005611 if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == FILE_ATTRIBUTE_NORMAL_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005612 fp->f_ci->m_fattr = file_info->Attributes |
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09005613 (fp->f_ci->m_fattr & FILE_ATTRIBUTE_DIRECTORY_LE);
Namjae Jeone2f34482021-03-16 10:49:09 +09005614 }
5615
5616 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
5617 (file_info->CreationTime || file_info->Attributes)) {
5618 struct xattr_dos_attrib da = {0};
5619
5620 da.version = 4;
5621 da.itime = fp->itime;
5622 da.create_time = fp->create_time;
5623 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
5624 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
5625 XATTR_DOSINFO_ITIME;
5626
Hyunchul Lee465d7202021-07-03 12:10:36 +09005627 rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005628 filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09005629 if (rc)
5630 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005631 "failed to restore file attribute in EA\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005632 rc = 0;
5633 }
5634
Namjae Jeone2f34482021-03-16 10:49:09 +09005635 if (attrs.ia_valid) {
5636 struct dentry *dentry = filp->f_path.dentry;
5637 struct inode *inode = d_inode(dentry);
5638
5639 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
5640 return -EACCES;
5641
Namjae Jeone2f34482021-03-16 10:49:09 +09005642 inode_lock(inode);
Namjae Jeon64e78752021-10-03 13:19:00 +09005643 inode->i_ctime = attrs.ia_ctime;
5644 attrs.ia_valid &= ~ATTR_CTIME;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005645 rc = notify_change(user_ns, dentry, &attrs, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09005646 inode_unlock(inode);
5647 }
Christian Braunereb5784f2021-08-23 17:13:55 +02005648 return rc;
Namjae Jeone2f34482021-03-16 10:49:09 +09005649}
5650
5651static int set_file_allocation_info(struct ksmbd_work *work,
Namjae Jeon9496e262021-09-29 15:41:48 +09005652 struct ksmbd_file *fp,
5653 struct smb2_file_alloc_info *file_alloc_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005654{
5655 /*
5656 * TODO : It's working fine only when store dos attributes
5657 * is not yes. need to implement a logic which works
5658 * properly with any smb.conf option
5659 */
5660
Namjae Jeone2f34482021-03-16 10:49:09 +09005661 loff_t alloc_blks;
5662 struct inode *inode;
5663 int rc;
5664
Marios Makassikisa2996692021-04-27 15:29:01 +09005665 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005666 return -EACCES;
5667
Namjae Jeone2f34482021-03-16 10:49:09 +09005668 alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9;
5669 inode = file_inode(fp->filp);
5670
5671 if (alloc_blks > inode->i_blocks) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005672 smb_break_all_levII_oplock(work, fp, 1);
5673 rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
5674 alloc_blks * 512);
Namjae Jeone2f34482021-03-16 10:49:09 +09005675 if (rc && rc != -EOPNOTSUPP) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005676 pr_err("vfs_fallocate is failed : %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005677 return rc;
5678 }
5679 } else if (alloc_blks < inode->i_blocks) {
5680 loff_t size;
5681
5682 /*
5683 * Allocation size could be smaller than original one
5684 * which means allocated blocks in file should be
5685 * deallocated. use truncate to cut out it, but inode
5686 * size is also updated with truncate offset.
5687 * inode size is retained by backup inode size.
5688 */
5689 size = i_size_read(inode);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005690 rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512);
Namjae Jeone2f34482021-03-16 10:49:09 +09005691 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005692 pr_err("truncate failed! filename : %s, err %d\n",
5693 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005694 return rc;
5695 }
5696 if (size < alloc_blks * 512)
5697 i_size_write(inode, size);
5698 }
5699 return 0;
5700}
5701
Namjae Jeon64b39f42021-03-30 14:25:35 +09005702static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005703 struct smb2_file_eof_info *file_eof_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005704{
Namjae Jeone2f34482021-03-16 10:49:09 +09005705 loff_t newsize;
5706 struct inode *inode;
5707 int rc;
5708
Marios Makassikisa2996692021-04-27 15:29:01 +09005709 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005710 return -EACCES;
5711
Namjae Jeone2f34482021-03-16 10:49:09 +09005712 newsize = le64_to_cpu(file_eof_info->EndOfFile);
5713 inode = file_inode(fp->filp);
5714
5715 /*
5716 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called
5717 * on FAT32 shared device, truncate execution time is too long
5718 * and network error could cause from windows client. because
5719 * truncate of some filesystem like FAT32 fill zero data in
5720 * truncated range.
5721 */
5722 if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) {
5723 ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005724 fp->filename, newsize);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005725 rc = ksmbd_vfs_truncate(work, fp, newsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005726 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005727 ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005728 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005729 if (rc != -EAGAIN)
5730 rc = -EBADF;
5731 return rc;
5732 }
5733 }
5734 return 0;
5735}
5736
Namjae Jeon64b39f42021-03-30 14:25:35 +09005737static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005738 struct smb2_file_rename_info *rename_info,
5739 unsigned int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005740{
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005741 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09005742 struct ksmbd_file *parent_fp;
Namjae Jeon12202c02021-06-29 09:23:56 +09005743 struct dentry *parent;
5744 struct dentry *dentry = fp->filp->f_path.dentry;
5745 int ret;
Namjae Jeone2f34482021-03-16 10:49:09 +09005746
5747 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005748 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005749 return -EACCES;
5750 }
5751
Namjae Jeon9496e262021-09-29 15:41:48 +09005752 if (buf_len < (u64)sizeof(struct smb2_file_rename_info) +
5753 le32_to_cpu(rename_info->FileNameLength))
5754 return -EINVAL;
5755
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005756 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005757 if (ksmbd_stream_fd(fp))
5758 goto next;
5759
Namjae Jeon12202c02021-06-29 09:23:56 +09005760 parent = dget_parent(dentry);
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005761 ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
Namjae Jeon12202c02021-06-29 09:23:56 +09005762 if (ret) {
5763 dput(parent);
5764 return ret;
5765 }
5766
5767 parent_fp = ksmbd_lookup_fd_inode(d_inode(parent));
5768 inode_unlock(d_inode(parent));
5769 dput(parent);
5770
Namjae Jeone2f34482021-03-16 10:49:09 +09005771 if (parent_fp) {
5772 if (parent_fp->daccess & FILE_DELETE_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005773 pr_err("parent dir is opened with delete access\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005774 return -ESHARE;
5775 }
5776 }
5777next:
Namjae Jeon9496e262021-09-29 15:41:48 +09005778 return smb2_rename(work, fp, user_ns, rename_info,
Namjae Jeone2f34482021-03-16 10:49:09 +09005779 work->sess->conn->local_nls);
5780}
5781
Namjae Jeon9496e262021-09-29 15:41:48 +09005782static int set_file_disposition_info(struct ksmbd_file *fp,
5783 struct smb2_file_disposition_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005784{
Namjae Jeone2f34482021-03-16 10:49:09 +09005785 struct inode *inode;
5786
5787 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005788 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005789 return -EACCES;
5790 }
5791
5792 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005793 if (file_info->DeletePending) {
5794 if (S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005795 ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY)
Namjae Jeone2f34482021-03-16 10:49:09 +09005796 return -EBUSY;
5797 ksmbd_set_inode_pending_delete(fp);
5798 } else {
5799 ksmbd_clear_inode_pending_delete(fp);
5800 }
5801 return 0;
5802}
5803
Namjae Jeon9496e262021-09-29 15:41:48 +09005804static int set_file_position_info(struct ksmbd_file *fp,
5805 struct smb2_file_pos_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005806{
Namjae Jeone2f34482021-03-16 10:49:09 +09005807 loff_t current_byte_offset;
Namjae Jeonee81cae2021-06-26 22:32:34 +09005808 unsigned long sector_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09005809 struct inode *inode;
5810
5811 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005812 current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset);
Namjae Jeonee81cae2021-06-26 22:32:34 +09005813 sector_size = inode->i_sb->s_blocksize;
Namjae Jeone2f34482021-03-16 10:49:09 +09005814
5815 if (current_byte_offset < 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09005816 (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE &&
5817 current_byte_offset & (sector_size - 1))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005818 pr_err("CurrentByteOffset is not valid : %llu\n",
5819 current_byte_offset);
Namjae Jeone2f34482021-03-16 10:49:09 +09005820 return -EINVAL;
5821 }
5822
5823 fp->filp->f_pos = current_byte_offset;
5824 return 0;
5825}
5826
Namjae Jeon9496e262021-09-29 15:41:48 +09005827static int set_file_mode_info(struct ksmbd_file *fp,
5828 struct smb2_file_mode_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005829{
Namjae Jeone2f34482021-03-16 10:49:09 +09005830 __le32 mode;
5831
Namjae Jeone2f34482021-03-16 10:49:09 +09005832 mode = file_info->Mode;
5833
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09005834 if ((mode & ~FILE_MODE_INFO_MASK)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005835 pr_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode));
Namjae Jeone2f34482021-03-16 10:49:09 +09005836 return -EINVAL;
5837 }
5838
5839 /*
5840 * TODO : need to implement consideration for
5841 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT
5842 */
5843 ksmbd_vfs_set_fadvise(fp->filp, mode);
5844 fp->coption = mode;
5845 return 0;
5846}
5847
5848/**
5849 * smb2_set_info_file() - handler for smb2 set info command
5850 * @work: smb work containing set info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09005851 * @fp: ksmbd_file pointer
Yang Li4bfd9ee2021-12-21 17:07:12 +08005852 * @req: request buffer pointer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09005853 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09005854 *
5855 * Return: 0 on success, otherwise error
5856 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH
5857 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09005858static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005859 struct smb2_set_info_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09005860 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005861{
Namjae Jeon9496e262021-09-29 15:41:48 +09005862 unsigned int buf_len = le32_to_cpu(req->BufferLength);
5863
5864 switch (req->FileInfoClass) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005865 case FILE_BASIC_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005866 {
5867 if (buf_len < sizeof(struct smb2_file_basic_info))
5868 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005869
Namjae Jeon9496e262021-09-29 15:41:48 +09005870 return set_file_basic_info(fp, (struct smb2_file_basic_info *)req->Buffer, share);
5871 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005872 case FILE_ALLOCATION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005873 {
5874 if (buf_len < sizeof(struct smb2_file_alloc_info))
5875 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005876
Namjae Jeon9496e262021-09-29 15:41:48 +09005877 return set_file_allocation_info(work, fp,
5878 (struct smb2_file_alloc_info *)req->Buffer);
5879 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005880 case FILE_END_OF_FILE_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005881 {
5882 if (buf_len < sizeof(struct smb2_file_eof_info))
5883 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005884
Namjae Jeon9496e262021-09-29 15:41:48 +09005885 return set_end_of_file_info(work, fp,
5886 (struct smb2_file_eof_info *)req->Buffer);
5887 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005888 case FILE_RENAME_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005889 {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005890 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005891 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005892 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005893 return -EACCES;
5894 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005895
Namjae Jeon9496e262021-09-29 15:41:48 +09005896 if (buf_len < sizeof(struct smb2_file_rename_info))
5897 return -EINVAL;
5898
5899 return set_rename_info(work, fp,
5900 (struct smb2_file_rename_info *)req->Buffer,
5901 buf_len);
5902 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005903 case FILE_LINK_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005904 {
5905 if (buf_len < sizeof(struct smb2_file_link_info))
5906 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005907
Namjae Jeon9496e262021-09-29 15:41:48 +09005908 return smb2_create_link(work, work->tcon->share_conf,
5909 (struct smb2_file_link_info *)req->Buffer,
5910 buf_len, fp->filp,
5911 work->sess->conn->local_nls);
5912 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005913 case FILE_DISPOSITION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005914 {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005915 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005916 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005917 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005918 return -EACCES;
5919 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005920
Namjae Jeon9496e262021-09-29 15:41:48 +09005921 if (buf_len < sizeof(struct smb2_file_disposition_info))
5922 return -EINVAL;
5923
5924 return set_file_disposition_info(fp,
5925 (struct smb2_file_disposition_info *)req->Buffer);
5926 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005927 case FILE_FULL_EA_INFORMATION:
5928 {
5929 if (!(fp->daccess & FILE_WRITE_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005930 pr_err("Not permitted to write ext attr: 0x%x\n",
5931 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005932 return -EACCES;
5933 }
5934
Namjae Jeon9496e262021-09-29 15:41:48 +09005935 if (buf_len < sizeof(struct smb2_ea_info))
5936 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005937
Namjae Jeon9496e262021-09-29 15:41:48 +09005938 return smb2_set_ea((struct smb2_ea_info *)req->Buffer,
5939 buf_len, &fp->filp->f_path);
5940 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005941 case FILE_POSITION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005942 {
5943 if (buf_len < sizeof(struct smb2_file_pos_info))
5944 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005945
Namjae Jeon9496e262021-09-29 15:41:48 +09005946 return set_file_position_info(fp, (struct smb2_file_pos_info *)req->Buffer);
5947 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005948 case FILE_MODE_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005949 {
5950 if (buf_len < sizeof(struct smb2_file_mode_info))
5951 return -EINVAL;
5952
5953 return set_file_mode_info(fp, (struct smb2_file_mode_info *)req->Buffer);
5954 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005955 }
5956
Namjae Jeon9496e262021-09-29 15:41:48 +09005957 pr_err("Unimplemented Fileinfoclass :%d\n", req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09005958 return -EOPNOTSUPP;
5959}
5960
Namjae Jeon64b39f42021-03-30 14:25:35 +09005961static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005962 char *buffer, int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005963{
5964 struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer;
5965
5966 fp->saccess |= FILE_SHARE_DELETE_LE;
5967
Hyunchul Leeef24c962021-06-30 18:25:52 +09005968 return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd,
Namjae Jeone2f34482021-03-16 10:49:09 +09005969 buf_len, false);
5970}
5971
5972/**
5973 * smb2_set_info() - handler for smb2 set info command handler
5974 * @work: smb work containing set info request buffer
5975 *
5976 * Return: 0 on success, otherwise error
5977 */
5978int smb2_set_info(struct ksmbd_work *work)
5979{
5980 struct smb2_set_info_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09005981 struct smb2_set_info_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005982 struct ksmbd_file *fp;
5983 int rc = 0;
5984 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5985
5986 ksmbd_debug(SMB, "Received set info request\n");
5987
Namjae Jeone2f34482021-03-16 10:49:09 +09005988 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09005989 req = ksmbd_req_buf_next(work);
5990 rsp = ksmbd_resp_buf_next(work);
Namjae Jeon38673692021-07-08 12:32:27 +09005991 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
5992 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005993 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005994 id = work->compound_fid;
5995 pid = work->compound_pfid;
5996 }
5997 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09005998 req = smb2_get_msg(work->request_buf);
5999 rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006000 }
6001
Namjae Jeon38673692021-07-08 12:32:27 +09006002 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006003 id = le64_to_cpu(req->VolatileFileId);
6004 pid = le64_to_cpu(req->PersistentFileId);
6005 }
6006
6007 fp = ksmbd_lookup_fd_slow(work, id, pid);
6008 if (!fp) {
6009 ksmbd_debug(SMB, "Invalid id for close: %u\n", id);
6010 rc = -ENOENT;
6011 goto err_out;
6012 }
6013
6014 switch (req->InfoType) {
6015 case SMB2_O_INFO_FILE:
6016 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
Namjae Jeon9496e262021-09-29 15:41:48 +09006017 rc = smb2_set_info_file(work, fp, req, work->tcon->share_conf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006018 break;
6019 case SMB2_O_INFO_SECURITY:
6020 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
Namjae Jeone70e3922021-08-21 23:26:01 +09006021 if (ksmbd_override_fsids(work)) {
6022 rc = -ENOMEM;
6023 goto err_out;
6024 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006025 rc = smb2_set_info_sec(fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09006026 le32_to_cpu(req->AdditionalInformation),
6027 req->Buffer,
6028 le32_to_cpu(req->BufferLength));
Namjae Jeone70e3922021-08-21 23:26:01 +09006029 ksmbd_revert_fsids(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09006030 break;
6031 default:
6032 rc = -EOPNOTSUPP;
6033 }
6034
6035 if (rc < 0)
6036 goto err_out;
6037
6038 rsp->StructureSize = cpu_to_le16(2);
Namjae Jeoncb451722021-11-03 08:08:44 +09006039 inc_rfc1001_len(work->response_buf, 2);
Namjae Jeone2f34482021-03-16 10:49:09 +09006040 ksmbd_fd_put(work, fp);
6041 return 0;
6042
6043err_out:
Hyunchul Lee265fd192021-09-25 00:06:16 +09006044 if (rc == -EACCES || rc == -EPERM || rc == -EXDEV)
Namjae Jeone2f34482021-03-16 10:49:09 +09006045 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6046 else if (rc == -EINVAL)
6047 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6048 else if (rc == -ESHARE)
6049 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6050 else if (rc == -ENOENT)
6051 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
6052 else if (rc == -EBUSY || rc == -ENOTEMPTY)
6053 rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY;
6054 else if (rc == -EAGAIN)
6055 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
Namjae Jeonff1d5722021-04-13 13:18:10 +09006056 else if (rc == -EBADF || rc == -ESTALE)
Namjae Jeone2f34482021-03-16 10:49:09 +09006057 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6058 else if (rc == -EEXIST)
6059 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
6060 else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP)
6061 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
6062 smb2_set_err_rsp(work);
6063 ksmbd_fd_put(work, fp);
Namjae Jeon070fb212021-05-26 17:57:12 +09006064 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09006065 return rc;
6066}
6067
6068/**
6069 * smb2_read_pipe() - handler for smb2 read from IPC pipe
6070 * @work: smb work containing read IPC pipe command buffer
6071 *
6072 * Return: 0 on success, otherwise error
6073 */
6074static noinline int smb2_read_pipe(struct ksmbd_work *work)
6075{
6076 int nbytes = 0, err;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006077 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09006078 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeoncb451722021-11-03 08:08:44 +09006079 struct smb2_read_req *req = smb2_get_msg(work->request_buf);
6080 struct smb2_read_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006081
6082 id = le64_to_cpu(req->VolatileFileId);
6083
Namjae Jeoncb451722021-11-03 08:08:44 +09006084 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006085 rpc_resp = ksmbd_rpc_read(work->sess, id);
6086 if (rpc_resp) {
6087 if (rpc_resp->flags != KSMBD_RPC_OK) {
6088 err = -EINVAL;
6089 goto out;
6090 }
6091
6092 work->aux_payload_buf =
Namjae Jeon79f6b112021-04-02 12:47:14 +09006093 kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006094 if (!work->aux_payload_buf) {
6095 err = -ENOMEM;
6096 goto out;
6097 }
6098
6099 memcpy(work->aux_payload_buf, rpc_resp->payload,
Namjae Jeon070fb212021-05-26 17:57:12 +09006100 rpc_resp->payload_sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09006101
6102 nbytes = rpc_resp->payload_sz;
Namjae Jeoncb451722021-11-03 08:08:44 +09006103 work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09006104 work->aux_payload_sz = nbytes;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006105 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006106 }
6107
6108 rsp->StructureSize = cpu_to_le16(17);
6109 rsp->DataOffset = 80;
6110 rsp->Reserved = 0;
6111 rsp->DataLength = cpu_to_le32(nbytes);
6112 rsp->DataRemaining = 0;
Ronnie Sahlberg699230f2021-09-09 12:26:12 +09006113 rsp->Flags = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006114 inc_rfc1001_len(work->response_buf, nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09006115 return 0;
6116
6117out:
6118 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
6119 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006120 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006121 return err;
6122}
6123
Hyunchul Lee2fd5dcb2022-01-13 09:51:39 +09006124static int smb2_set_remote_key_for_rdma(struct ksmbd_work *work,
6125 struct smb2_buffer_desc_v1 *desc,
6126 __le32 Channel,
6127 __le16 ChannelInfoOffset,
6128 __le16 ChannelInfoLength)
6129{
Hyunchul Lee6d896d32022-01-20 21:10:11 +09006130 unsigned int i, ch_count;
6131
Hyunchul Lee2fd5dcb2022-01-13 09:51:39 +09006132 if (work->conn->dialect == SMB30_PROT_ID &&
6133 Channel != SMB2_CHANNEL_RDMA_V1)
6134 return -EINVAL;
6135
Hyunchul Lee6d896d32022-01-20 21:10:11 +09006136 ch_count = le16_to_cpu(ChannelInfoLength) / sizeof(*desc);
6137 if (ksmbd_debug_types & KSMBD_DEBUG_RDMA) {
6138 for (i = 0; i < ch_count; i++) {
6139 pr_info("RDMA r/w request %#x: token %#x, length %#x\n",
6140 i,
6141 le32_to_cpu(desc[i].token),
6142 le32_to_cpu(desc[i].length));
6143 }
6144 }
6145 if (ch_count != 1) {
6146 ksmbd_debug(RDMA, "RDMA multiple buffer descriptors %d are not supported yet\n",
6147 ch_count);
Hyunchul Lee2fd5dcb2022-01-13 09:51:39 +09006148 return -EINVAL;
Hyunchul Lee6d896d32022-01-20 21:10:11 +09006149 }
Hyunchul Lee2fd5dcb2022-01-13 09:51:39 +09006150
6151 work->need_invalidate_rkey =
6152 (Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6153 work->remote_key = le32_to_cpu(desc->token);
6154 return 0;
6155}
6156
Namjae Jeone2f34482021-03-16 10:49:09 +09006157static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006158 struct smb2_read_req *req, void *data_buf,
6159 size_t length)
Namjae Jeone2f34482021-03-16 10:49:09 +09006160{
6161 struct smb2_buffer_desc_v1 *desc =
6162 (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6163 int err;
6164
Namjae Jeon64b39f42021-03-30 14:25:35 +09006165 err = ksmbd_conn_rdma_write(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006166 le32_to_cpu(desc->token),
6167 le64_to_cpu(desc->offset),
6168 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006169 if (err)
6170 return err;
6171
6172 return length;
6173}
6174
6175/**
6176 * smb2_read() - handler for smb2 read from file
6177 * @work: smb work containing read command buffer
6178 *
6179 * Return: 0 on success, otherwise error
6180 */
6181int smb2_read(struct ksmbd_work *work)
6182{
6183 struct ksmbd_conn *conn = work->conn;
6184 struct smb2_read_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006185 struct smb2_read_rsp *rsp;
Hyunchul Lee2fd5dcb2022-01-13 09:51:39 +09006186 struct ksmbd_file *fp = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006187 loff_t offset;
6188 size_t length, mincount;
6189 ssize_t nbytes = 0, remain_bytes = 0;
6190 int err = 0;
6191
Namjae Jeone2f34482021-03-16 10:49:09 +09006192 WORK_BUFFERS(work, req, rsp);
6193
6194 if (test_share_config_flag(work->tcon->share_conf,
6195 KSMBD_SHARE_FLAG_PIPE)) {
6196 ksmbd_debug(SMB, "IPC pipe read request\n");
6197 return smb2_read_pipe(work);
6198 }
6199
Hyunchul Lee2fd5dcb2022-01-13 09:51:39 +09006200 if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
6201 req->Channel == SMB2_CHANNEL_RDMA_V1) {
Hyunchul Lee6d896d32022-01-20 21:10:11 +09006202 unsigned int ch_offset = le16_to_cpu(req->ReadChannelInfoOffset);
6203
6204 if (ch_offset < offsetof(struct smb2_read_req, Buffer)) {
6205 err = -EINVAL;
6206 goto out;
6207 }
Hyunchul Lee2fd5dcb2022-01-13 09:51:39 +09006208 err = smb2_set_remote_key_for_rdma(work,
6209 (struct smb2_buffer_desc_v1 *)
Hyunchul Lee6d896d32022-01-20 21:10:11 +09006210 ((char *)req + ch_offset),
Hyunchul Lee2fd5dcb2022-01-13 09:51:39 +09006211 req->Channel,
6212 req->ReadChannelInfoOffset,
6213 req->ReadChannelInfoLength);
6214 if (err)
6215 goto out;
6216 }
6217
Namjae Jeon070fb212021-05-26 17:57:12 +09006218 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
6219 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006220 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006221 err = -ENOENT;
6222 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006223 }
6224
6225 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006226 pr_err("Not permitted to read : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09006227 err = -EACCES;
6228 goto out;
6229 }
6230
6231 offset = le64_to_cpu(req->Offset);
6232 length = le32_to_cpu(req->Length);
6233 mincount = le32_to_cpu(req->MinimumCount);
6234
6235 if (length > conn->vals->max_read_size) {
6236 ksmbd_debug(SMB, "limiting read size to max size(%u)\n",
6237 conn->vals->max_read_size);
6238 err = -EINVAL;
6239 goto out;
6240 }
6241
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006242 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6243 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006244
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006245 work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006246 if (!work->aux_payload_buf) {
Dan Carpenterc1ea1112021-03-22 17:50:11 +03006247 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09006248 goto out;
6249 }
6250
6251 nbytes = ksmbd_vfs_read(work, fp, length, &offset);
6252 if (nbytes < 0) {
6253 err = nbytes;
6254 goto out;
6255 }
6256
6257 if ((nbytes == 0 && length != 0) || nbytes < mincount) {
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006258 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006259 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006260 rsp->hdr.Status = STATUS_END_OF_FILE;
6261 smb2_set_err_rsp(work);
6262 ksmbd_fd_put(work, fp);
6263 return 0;
6264 }
6265
6266 ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006267 nbytes, offset, mincount);
Namjae Jeone2f34482021-03-16 10:49:09 +09006268
6269 if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006270 req->Channel == SMB2_CHANNEL_RDMA_V1) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006271 /* write data to the client using rdma channel */
6272 remain_bytes = smb2_read_rdma_channel(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09006273 work->aux_payload_buf,
6274 nbytes);
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006275 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006276 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006277
6278 nbytes = 0;
6279 if (remain_bytes < 0) {
6280 err = (int)remain_bytes;
6281 goto out;
6282 }
6283 }
6284
6285 rsp->StructureSize = cpu_to_le16(17);
6286 rsp->DataOffset = 80;
6287 rsp->Reserved = 0;
6288 rsp->DataLength = cpu_to_le32(nbytes);
6289 rsp->DataRemaining = cpu_to_le32(remain_bytes);
Ronnie Sahlberg699230f2021-09-09 12:26:12 +09006290 rsp->Flags = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006291 inc_rfc1001_len(work->response_buf, 16);
6292 work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09006293 work->aux_payload_sz = nbytes;
Namjae Jeoncb451722021-11-03 08:08:44 +09006294 inc_rfc1001_len(work->response_buf, nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09006295 ksmbd_fd_put(work, fp);
6296 return 0;
6297
6298out:
6299 if (err) {
6300 if (err == -EISDIR)
6301 rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST;
6302 else if (err == -EAGAIN)
6303 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6304 else if (err == -ENOENT)
6305 rsp->hdr.Status = STATUS_FILE_CLOSED;
6306 else if (err == -EACCES)
6307 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6308 else if (err == -ESHARE)
6309 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6310 else if (err == -EINVAL)
6311 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6312 else
6313 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6314
6315 smb2_set_err_rsp(work);
6316 }
6317 ksmbd_fd_put(work, fp);
6318 return err;
6319}
6320
6321/**
6322 * smb2_write_pipe() - handler for smb2 write on IPC pipe
6323 * @work: smb work containing write IPC pipe command buffer
6324 *
6325 * Return: 0 on success, otherwise error
6326 */
6327static noinline int smb2_write_pipe(struct ksmbd_work *work)
6328{
Namjae Jeoncb451722021-11-03 08:08:44 +09006329 struct smb2_write_req *req = smb2_get_msg(work->request_buf);
6330 struct smb2_write_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006331 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006332 u64 id = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09006333 int err = 0, ret = 0;
6334 char *data_buf;
6335 size_t length;
6336
6337 length = le32_to_cpu(req->Length);
6338 id = le64_to_cpu(req->VolatileFileId);
6339
6340 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeoncb451722021-11-03 08:08:44 +09006341 offsetof(struct smb2_write_req, Buffer)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006342 data_buf = (char *)&req->Buffer[0];
6343 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09006344 if ((u64)le16_to_cpu(req->DataOffset) + length >
6345 get_rfc1002_len(work->request_buf)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006346 pr_err("invalid write data offset %u, smb_len %u\n",
6347 le16_to_cpu(req->DataOffset),
Namjae Jeoncb451722021-11-03 08:08:44 +09006348 get_rfc1002_len(work->request_buf));
Namjae Jeone2f34482021-03-16 10:49:09 +09006349 err = -EINVAL;
6350 goto out;
6351 }
6352
6353 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6354 le16_to_cpu(req->DataOffset));
6355 }
6356
6357 rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length);
6358 if (rpc_resp) {
6359 if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
6360 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006361 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006362 smb2_set_err_rsp(work);
6363 return -EOPNOTSUPP;
6364 }
6365 if (rpc_resp->flags != KSMBD_RPC_OK) {
6366 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6367 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006368 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006369 return ret;
6370 }
Namjae Jeon79f6b112021-04-02 12:47:14 +09006371 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006372 }
6373
6374 rsp->StructureSize = cpu_to_le16(17);
6375 rsp->DataOffset = 0;
6376 rsp->Reserved = 0;
6377 rsp->DataLength = cpu_to_le32(length);
6378 rsp->DataRemaining = 0;
6379 rsp->Reserved2 = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006380 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006381 return 0;
6382out:
6383 if (err) {
6384 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6385 smb2_set_err_rsp(work);
6386 }
6387
6388 return err;
6389}
6390
6391static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006392 struct smb2_write_req *req,
6393 struct ksmbd_file *fp,
6394 loff_t offset, size_t length, bool sync)
Namjae Jeone2f34482021-03-16 10:49:09 +09006395{
6396 struct smb2_buffer_desc_v1 *desc;
6397 char *data_buf;
6398 int ret;
6399 ssize_t nbytes;
6400
6401 desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6402
Namjae Jeon79f6b112021-04-02 12:47:14 +09006403 data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006404 if (!data_buf)
6405 return -ENOMEM;
6406
6407 ret = ksmbd_conn_rdma_read(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006408 le32_to_cpu(desc->token),
6409 le64_to_cpu(desc->offset),
6410 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006411 if (ret < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09006412 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006413 return ret;
6414 }
6415
Namjae Jeon64b39f42021-03-30 14:25:35 +09006416 ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006417 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006418 if (ret < 0)
6419 return ret;
6420
6421 return nbytes;
6422}
6423
6424/**
6425 * smb2_write() - handler for smb2 write from file
6426 * @work: smb work containing write command buffer
6427 *
6428 * Return: 0 on success, otherwise error
6429 */
6430int smb2_write(struct ksmbd_work *work)
6431{
6432 struct smb2_write_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006433 struct smb2_write_rsp *rsp;
Namjae Jeonbcd62a32021-05-10 09:08:19 +09006434 struct ksmbd_file *fp = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006435 loff_t offset;
6436 size_t length;
6437 ssize_t nbytes;
6438 char *data_buf;
6439 bool writethrough = false;
6440 int err = 0;
6441
Namjae Jeone2f34482021-03-16 10:49:09 +09006442 WORK_BUFFERS(work, req, rsp);
6443
Namjae Jeon64b39f42021-03-30 14:25:35 +09006444 if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006445 ksmbd_debug(SMB, "IPC pipe write request\n");
6446 return smb2_write_pipe(work);
6447 }
6448
Hyunchul Lee2fd5dcb2022-01-13 09:51:39 +09006449 if (req->Channel == SMB2_CHANNEL_RDMA_V1 ||
6450 req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
Hyunchul Lee6d896d32022-01-20 21:10:11 +09006451 unsigned int ch_offset = le16_to_cpu(req->WriteChannelInfoOffset);
6452
6453 if (req->Length != 0 || req->DataOffset != 0 ||
6454 ch_offset < offsetof(struct smb2_write_req, Buffer)) {
6455 err = -EINVAL;
6456 goto out;
6457 }
Hyunchul Lee2fd5dcb2022-01-13 09:51:39 +09006458 err = smb2_set_remote_key_for_rdma(work,
6459 (struct smb2_buffer_desc_v1 *)
Hyunchul Lee6d896d32022-01-20 21:10:11 +09006460 ((char *)req + ch_offset),
Hyunchul Lee2fd5dcb2022-01-13 09:51:39 +09006461 req->Channel,
6462 req->WriteChannelInfoOffset,
6463 req->WriteChannelInfoLength);
6464 if (err)
6465 goto out;
6466 }
6467
Namjae Jeone2f34482021-03-16 10:49:09 +09006468 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
6469 ksmbd_debug(SMB, "User does not have write permission\n");
6470 err = -EACCES;
6471 goto out;
6472 }
6473
Namjae Jeon64b39f42021-03-30 14:25:35 +09006474 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
Namjae Jeon070fb212021-05-26 17:57:12 +09006475 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006476 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006477 err = -ENOENT;
6478 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006479 }
6480
6481 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006482 pr_err("Not permitted to write : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09006483 err = -EACCES;
6484 goto out;
6485 }
6486
6487 offset = le64_to_cpu(req->Offset);
6488 length = le32_to_cpu(req->Length);
6489
6490 if (length > work->conn->vals->max_write_size) {
6491 ksmbd_debug(SMB, "limiting write size to max size(%u)\n",
6492 work->conn->vals->max_write_size);
6493 err = -EINVAL;
6494 goto out;
6495 }
6496
6497 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6498 writethrough = true;
6499
6500 if (req->Channel != SMB2_CHANNEL_RDMA_V1 &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006501 req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006502 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeoncb451722021-11-03 08:08:44 +09006503 offsetof(struct smb2_write_req, Buffer)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006504 data_buf = (char *)&req->Buffer[0];
6505 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09006506 if ((u64)le16_to_cpu(req->DataOffset) + length >
6507 get_rfc1002_len(work->request_buf)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006508 pr_err("invalid write data offset %u, smb_len %u\n",
6509 le16_to_cpu(req->DataOffset),
Namjae Jeoncb451722021-11-03 08:08:44 +09006510 get_rfc1002_len(work->request_buf));
Namjae Jeone2f34482021-03-16 10:49:09 +09006511 err = -EINVAL;
6512 goto out;
6513 }
6514
6515 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6516 le16_to_cpu(req->DataOffset));
6517 }
6518
6519 ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags));
6520 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6521 writethrough = true;
6522
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006523 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6524 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006525 err = ksmbd_vfs_write(work, fp, data_buf, length, &offset,
6526 writethrough, &nbytes);
6527 if (err < 0)
6528 goto out;
6529 } else {
6530 /* read data from the client using rdma channel, and
6531 * write the data.
6532 */
6533 nbytes = smb2_write_rdma_channel(work, req, fp, offset,
Namjae Jeon070fb212021-05-26 17:57:12 +09006534 le32_to_cpu(req->RemainingBytes),
6535 writethrough);
Namjae Jeone2f34482021-03-16 10:49:09 +09006536 if (nbytes < 0) {
6537 err = (int)nbytes;
6538 goto out;
6539 }
6540 }
6541
6542 rsp->StructureSize = cpu_to_le16(17);
6543 rsp->DataOffset = 0;
6544 rsp->Reserved = 0;
6545 rsp->DataLength = cpu_to_le32(nbytes);
6546 rsp->DataRemaining = 0;
6547 rsp->Reserved2 = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006548 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006549 ksmbd_fd_put(work, fp);
6550 return 0;
6551
6552out:
6553 if (err == -EAGAIN)
6554 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6555 else if (err == -ENOSPC || err == -EFBIG)
6556 rsp->hdr.Status = STATUS_DISK_FULL;
6557 else if (err == -ENOENT)
6558 rsp->hdr.Status = STATUS_FILE_CLOSED;
6559 else if (err == -EACCES)
6560 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6561 else if (err == -ESHARE)
6562 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6563 else if (err == -EINVAL)
6564 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6565 else
6566 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6567
6568 smb2_set_err_rsp(work);
6569 ksmbd_fd_put(work, fp);
6570 return err;
6571}
6572
6573/**
6574 * smb2_flush() - handler for smb2 flush file - fsync
6575 * @work: smb work containing flush command buffer
6576 *
6577 * Return: 0 on success, otherwise error
6578 */
6579int smb2_flush(struct ksmbd_work *work)
6580{
6581 struct smb2_flush_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006582 struct smb2_flush_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09006583 int err;
6584
Namjae Jeone2f34482021-03-16 10:49:09 +09006585 WORK_BUFFERS(work, req, rsp);
6586
6587 ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006588 le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006589
6590 err = ksmbd_vfs_fsync(work,
6591 le64_to_cpu(req->VolatileFileId),
6592 le64_to_cpu(req->PersistentFileId));
6593 if (err)
6594 goto out;
6595
6596 rsp->StructureSize = cpu_to_le16(4);
6597 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006598 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09006599 return 0;
6600
6601out:
6602 if (err) {
6603 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6604 smb2_set_err_rsp(work);
6605 }
6606
6607 return err;
6608}
6609
6610/**
6611 * smb2_cancel() - handler for smb2 cancel command
6612 * @work: smb work containing cancel command buffer
6613 *
6614 * Return: 0 on success, otherwise error
6615 */
6616int smb2_cancel(struct ksmbd_work *work)
6617{
6618 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09006619 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006620 struct smb2_hdr *chdr;
6621 struct ksmbd_work *cancel_work = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006622 int canceled = 0;
6623 struct list_head *command_list;
6624
6625 ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006626 hdr->MessageId, hdr->Flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09006627
6628 if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) {
6629 command_list = &conn->async_requests;
6630
6631 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006632 list_for_each_entry(cancel_work, command_list,
6633 async_request_entry) {
Namjae Jeoncb451722021-11-03 08:08:44 +09006634 chdr = smb2_get_msg(cancel_work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006635
6636 if (cancel_work->async_id !=
Namjae Jeon64b39f42021-03-30 14:25:35 +09006637 le64_to_cpu(hdr->Id.AsyncId))
Namjae Jeone2f34482021-03-16 10:49:09 +09006638 continue;
6639
6640 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006641 "smb2 with AsyncId %llu cancelled command = 0x%x\n",
6642 le64_to_cpu(hdr->Id.AsyncId),
6643 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006644 canceled = 1;
6645 break;
6646 }
6647 spin_unlock(&conn->request_lock);
6648 } else {
6649 command_list = &conn->requests;
6650
6651 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006652 list_for_each_entry(cancel_work, command_list, request_entry) {
Namjae Jeoncb451722021-11-03 08:08:44 +09006653 chdr = smb2_get_msg(cancel_work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006654
6655 if (chdr->MessageId != hdr->MessageId ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006656 cancel_work == work)
Namjae Jeone2f34482021-03-16 10:49:09 +09006657 continue;
6658
6659 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006660 "smb2 with mid %llu cancelled command = 0x%x\n",
6661 le64_to_cpu(hdr->MessageId),
6662 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006663 canceled = 1;
6664 break;
6665 }
6666 spin_unlock(&conn->request_lock);
6667 }
6668
6669 if (canceled) {
6670 cancel_work->state = KSMBD_WORK_CANCELLED;
6671 if (cancel_work->cancel_fn)
6672 cancel_work->cancel_fn(cancel_work->cancel_argv);
6673 }
6674
6675 /* For SMB2_CANCEL command itself send no response*/
6676 work->send_no_response = 1;
6677 return 0;
6678}
6679
6680struct file_lock *smb_flock_init(struct file *f)
6681{
6682 struct file_lock *fl;
6683
6684 fl = locks_alloc_lock();
6685 if (!fl)
6686 goto out;
6687
6688 locks_init_lock(fl);
6689
6690 fl->fl_owner = f;
6691 fl->fl_pid = current->tgid;
6692 fl->fl_file = f;
6693 fl->fl_flags = FL_POSIX;
6694 fl->fl_ops = NULL;
6695 fl->fl_lmops = NULL;
6696
6697out:
6698 return fl;
6699}
6700
6701static int smb2_set_flock_flags(struct file_lock *flock, int flags)
6702{
6703 int cmd = -EINVAL;
6704
6705 /* Checking for wrong flag combination during lock request*/
6706 switch (flags) {
6707 case SMB2_LOCKFLAG_SHARED:
6708 ksmbd_debug(SMB, "received shared request\n");
6709 cmd = F_SETLKW;
6710 flock->fl_type = F_RDLCK;
6711 flock->fl_flags |= FL_SLEEP;
6712 break;
6713 case SMB2_LOCKFLAG_EXCLUSIVE:
6714 ksmbd_debug(SMB, "received exclusive request\n");
6715 cmd = F_SETLKW;
6716 flock->fl_type = F_WRLCK;
6717 flock->fl_flags |= FL_SLEEP;
6718 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006719 case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006720 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006721 "received shared & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006722 cmd = F_SETLK;
6723 flock->fl_type = F_RDLCK;
6724 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006725 case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006726 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006727 "received exclusive & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006728 cmd = F_SETLK;
6729 flock->fl_type = F_WRLCK;
6730 break;
6731 case SMB2_LOCKFLAG_UNLOCK:
6732 ksmbd_debug(SMB, "received unlock request\n");
6733 flock->fl_type = F_UNLCK;
6734 cmd = 0;
6735 break;
6736 }
6737
6738 return cmd;
6739}
6740
6741static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock,
Namjae Jeon070fb212021-05-26 17:57:12 +09006742 unsigned int cmd, int flags,
6743 struct list_head *lock_list)
Namjae Jeone2f34482021-03-16 10:49:09 +09006744{
6745 struct ksmbd_lock *lock;
6746
6747 lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL);
6748 if (!lock)
6749 return NULL;
6750
6751 lock->cmd = cmd;
6752 lock->fl = flock;
6753 lock->start = flock->fl_start;
6754 lock->end = flock->fl_end;
6755 lock->flags = flags;
6756 if (lock->start == lock->end)
6757 lock->zero_len = 1;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006758 INIT_LIST_HEAD(&lock->clist);
6759 INIT_LIST_HEAD(&lock->flist);
Namjae Jeone2f34482021-03-16 10:49:09 +09006760 INIT_LIST_HEAD(&lock->llist);
Namjae Jeone2f34482021-03-16 10:49:09 +09006761 list_add_tail(&lock->llist, lock_list);
6762
6763 return lock;
6764}
6765
6766static void smb2_remove_blocked_lock(void **argv)
6767{
6768 struct file_lock *flock = (struct file_lock *)argv[0];
6769
6770 ksmbd_vfs_posix_lock_unblock(flock);
6771 wake_up(&flock->fl_wait);
6772}
6773
6774static inline bool lock_defer_pending(struct file_lock *fl)
6775{
6776 /* check pending lock waiters */
6777 return waitqueue_active(&fl->fl_wait);
6778}
6779
6780/**
6781 * smb2_lock() - handler for smb2 file lock command
6782 * @work: smb work containing lock command buffer
6783 *
6784 * Return: 0 on success, otherwise error
6785 */
6786int smb2_lock(struct ksmbd_work *work)
6787{
Namjae Jeoncb451722021-11-03 08:08:44 +09006788 struct smb2_lock_req *req = smb2_get_msg(work->request_buf);
6789 struct smb2_lock_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006790 struct smb2_lock_element *lock_ele;
6791 struct ksmbd_file *fp = NULL;
6792 struct file_lock *flock = NULL;
6793 struct file *filp = NULL;
6794 int lock_count;
6795 int flags = 0;
6796 int cmd = 0;
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006797 int err = -EIO, i, rc = 0;
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006798 u64 lock_start, lock_length;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006799 struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2;
6800 struct ksmbd_conn *conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09006801 int nolock = 0;
6802 LIST_HEAD(lock_list);
6803 LIST_HEAD(rollback_list);
6804 int prior_lock = 0;
6805
6806 ksmbd_debug(SMB, "Received lock request\n");
6807 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006808 le64_to_cpu(req->VolatileFileId),
6809 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006810 if (!fp) {
6811 ksmbd_debug(SMB, "Invalid file id for lock : %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006812 le64_to_cpu(req->VolatileFileId));
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006813 err = -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09006814 goto out2;
6815 }
6816
6817 filp = fp->filp;
6818 lock_count = le16_to_cpu(req->LockCount);
6819 lock_ele = req->locks;
6820
6821 ksmbd_debug(SMB, "lock count is %d\n", lock_count);
Namjae Jeon070fb212021-05-26 17:57:12 +09006822 if (!lock_count) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006823 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006824 goto out2;
6825 }
6826
6827 for (i = 0; i < lock_count; i++) {
6828 flags = le32_to_cpu(lock_ele[i].Flags);
6829
6830 flock = smb_flock_init(filp);
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006831 if (!flock)
Namjae Jeone2f34482021-03-16 10:49:09 +09006832 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006833
6834 cmd = smb2_set_flock_flags(flock, flags);
6835
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006836 lock_start = le64_to_cpu(lock_ele[i].Offset);
6837 lock_length = le64_to_cpu(lock_ele[i].Length);
6838 if (lock_start > U64_MAX - lock_length) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006839 pr_err("Invalid lock range requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006840 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6841 goto out;
6842 }
6843
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006844 if (lock_start > OFFSET_MAX)
6845 flock->fl_start = OFFSET_MAX;
6846 else
6847 flock->fl_start = lock_start;
6848
Namjae Jeone2f34482021-03-16 10:49:09 +09006849 lock_length = le64_to_cpu(lock_ele[i].Length);
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006850 if (lock_length > OFFSET_MAX - flock->fl_start)
6851 lock_length = OFFSET_MAX - flock->fl_start;
Namjae Jeone2f34482021-03-16 10:49:09 +09006852
6853 flock->fl_end = flock->fl_start + lock_length;
6854
6855 if (flock->fl_end < flock->fl_start) {
6856 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006857 "the end offset(%llx) is smaller than the start offset(%llx)\n",
6858 flock->fl_end, flock->fl_start);
Namjae Jeone2f34482021-03-16 10:49:09 +09006859 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6860 goto out;
6861 }
6862
6863 /* Check conflict locks in one request */
6864 list_for_each_entry(cmp_lock, &lock_list, llist) {
6865 if (cmp_lock->fl->fl_start <= flock->fl_start &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006866 cmp_lock->fl->fl_end >= flock->fl_end) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006867 if (cmp_lock->fl->fl_type != F_UNLCK &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006868 flock->fl_type != F_UNLCK) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006869 pr_err("conflict two locks in one request\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006870 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006871 goto out;
6872 }
6873 }
6874 }
6875
6876 smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list);
6877 if (!smb_lock) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006878 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006879 goto out;
6880 }
6881 }
6882
6883 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6884 if (smb_lock->cmd < 0) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006885 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006886 goto out;
6887 }
6888
6889 if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006890 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006891 goto out;
6892 }
6893
Namjae Jeon64b39f42021-03-30 14:25:35 +09006894 if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) &&
6895 smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) ||
6896 (prior_lock == SMB2_LOCKFLAG_UNLOCK &&
6897 !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006898 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006899 goto out;
6900 }
6901
6902 prior_lock = smb_lock->flags;
6903
6904 if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006905 !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY))
Hyunchul Leed63528e2021-07-10 16:22:41 +09006906 goto no_check_cl;
Namjae Jeone2f34482021-03-16 10:49:09 +09006907
6908 nolock = 1;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006909 /* check locks in connection list */
6910 read_lock(&conn_list_lock);
6911 list_for_each_entry(conn, &conn_list, conns_list) {
6912 spin_lock(&conn->llist_lock);
6913 list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) {
6914 if (file_inode(cmp_lock->fl->fl_file) !=
6915 file_inode(smb_lock->fl->fl_file))
6916 continue;
Namjae Jeone2f34482021-03-16 10:49:09 +09006917
Hyunchul Leed63528e2021-07-10 16:22:41 +09006918 if (smb_lock->fl->fl_type == F_UNLCK) {
6919 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file &&
6920 cmp_lock->start == smb_lock->start &&
6921 cmp_lock->end == smb_lock->end &&
6922 !lock_defer_pending(cmp_lock->fl)) {
6923 nolock = 0;
6924 list_del(&cmp_lock->flist);
6925 list_del(&cmp_lock->clist);
6926 spin_unlock(&conn->llist_lock);
6927 read_unlock(&conn_list_lock);
6928
6929 locks_free_lock(cmp_lock->fl);
6930 kfree(cmp_lock);
6931 goto out_check_cl;
6932 }
6933 continue;
Namjae Jeone2f34482021-03-16 10:49:09 +09006934 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006935
Hyunchul Leed63528e2021-07-10 16:22:41 +09006936 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) {
6937 if (smb_lock->flags & SMB2_LOCKFLAG_SHARED)
6938 continue;
6939 } else {
6940 if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED)
6941 continue;
6942 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006943
Hyunchul Leed63528e2021-07-10 16:22:41 +09006944 /* check zero byte lock range */
6945 if (cmp_lock->zero_len && !smb_lock->zero_len &&
6946 cmp_lock->start > smb_lock->start &&
6947 cmp_lock->start < smb_lock->end) {
6948 spin_unlock(&conn->llist_lock);
6949 read_unlock(&conn_list_lock);
6950 pr_err("previous lock conflict with zero byte lock range\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006951 goto out;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006952 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006953
Hyunchul Leed63528e2021-07-10 16:22:41 +09006954 if (smb_lock->zero_len && !cmp_lock->zero_len &&
6955 smb_lock->start > cmp_lock->start &&
6956 smb_lock->start < cmp_lock->end) {
6957 spin_unlock(&conn->llist_lock);
6958 read_unlock(&conn_list_lock);
6959 pr_err("current lock conflict with zero byte lock range\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006960 goto out;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006961 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006962
Hyunchul Leed63528e2021-07-10 16:22:41 +09006963 if (((cmp_lock->start <= smb_lock->start &&
6964 cmp_lock->end > smb_lock->start) ||
6965 (cmp_lock->start < smb_lock->end &&
6966 cmp_lock->end >= smb_lock->end)) &&
6967 !cmp_lock->zero_len && !smb_lock->zero_len) {
6968 spin_unlock(&conn->llist_lock);
6969 read_unlock(&conn_list_lock);
6970 pr_err("Not allow lock operation on exclusive lock range\n");
Hyunchul Leed63528e2021-07-10 16:22:41 +09006971 goto out;
6972 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006973 }
Hyunchul Leed63528e2021-07-10 16:22:41 +09006974 spin_unlock(&conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006975 }
Hyunchul Leed63528e2021-07-10 16:22:41 +09006976 read_unlock(&conn_list_lock);
6977out_check_cl:
Namjae Jeone2f34482021-03-16 10:49:09 +09006978 if (smb_lock->fl->fl_type == F_UNLCK && nolock) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006979 pr_err("Try to unlock nolocked range\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006980 rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED;
6981 goto out;
6982 }
6983
Hyunchul Leed63528e2021-07-10 16:22:41 +09006984no_check_cl:
Namjae Jeone2f34482021-03-16 10:49:09 +09006985 if (smb_lock->zero_len) {
6986 err = 0;
6987 goto skip;
6988 }
6989
6990 flock = smb_lock->fl;
6991 list_del(&smb_lock->llist);
6992retry:
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006993 rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09006994skip:
6995 if (flags & SMB2_LOCKFLAG_UNLOCK) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006996 if (!rc) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006997 ksmbd_debug(SMB, "File unlocked\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006998 } else if (rc == -ENOENT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006999 rsp->hdr.Status = STATUS_NOT_LOCKED;
7000 goto out;
7001 }
7002 locks_free_lock(flock);
7003 kfree(smb_lock);
7004 } else {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09007005 if (rc == FILE_LOCK_DEFERRED) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007006 void **argv;
7007
7008 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007009 "would have to wait for getting lock\n");
Hyunchul Leed63528e2021-07-10 16:22:41 +09007010 spin_lock(&work->conn->llist_lock);
7011 list_add_tail(&smb_lock->clist,
7012 &work->conn->lock_list);
7013 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09007014 list_add(&smb_lock->llist, &rollback_list);
7015
7016 argv = kmalloc(sizeof(void *), GFP_KERNEL);
7017 if (!argv) {
7018 err = -ENOMEM;
7019 goto out;
7020 }
7021 argv[0] = flock;
7022
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09007023 rc = setup_async_work(work,
7024 smb2_remove_blocked_lock,
7025 argv);
7026 if (rc) {
7027 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09007028 goto out;
7029 }
7030 spin_lock(&fp->f_lock);
7031 list_add(&work->fp_entry, &fp->blocked_works);
7032 spin_unlock(&fp->f_lock);
7033
7034 smb2_send_interim_resp(work, STATUS_PENDING);
7035
Hyunchul Lee45a64e82021-07-10 09:34:20 +09007036 ksmbd_vfs_posix_lock_wait(flock);
Namjae Jeone2f34482021-03-16 10:49:09 +09007037
Hyunchul Leed4075ab2021-06-25 07:02:10 +09007038 if (work->state != KSMBD_WORK_ACTIVE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007039 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007040 spin_lock(&work->conn->llist_lock);
7041 list_del(&smb_lock->clist);
7042 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09007043 locks_free_lock(flock);
7044
Hyunchul Leed4075ab2021-06-25 07:02:10 +09007045 if (work->state == KSMBD_WORK_CANCELLED) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007046 spin_lock(&fp->f_lock);
7047 list_del(&work->fp_entry);
7048 spin_unlock(&fp->f_lock);
7049 rsp->hdr.Status =
7050 STATUS_CANCELLED;
7051 kfree(smb_lock);
7052 smb2_send_interim_resp(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007053 STATUS_CANCELLED);
Namjae Jeone2f34482021-03-16 10:49:09 +09007054 work->send_no_response = 1;
7055 goto out;
7056 }
7057 init_smb2_rsp_hdr(work);
7058 smb2_set_err_rsp(work);
7059 rsp->hdr.Status =
7060 STATUS_RANGE_NOT_LOCKED;
7061 kfree(smb_lock);
7062 goto out2;
7063 }
7064
7065 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007066 spin_lock(&work->conn->llist_lock);
7067 list_del(&smb_lock->clist);
7068 spin_unlock(&work->conn->llist_lock);
7069
Namjae Jeone2f34482021-03-16 10:49:09 +09007070 spin_lock(&fp->f_lock);
7071 list_del(&work->fp_entry);
7072 spin_unlock(&fp->f_lock);
7073 goto retry;
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09007074 } else if (!rc) {
Hyunchul Leed63528e2021-07-10 16:22:41 +09007075 spin_lock(&work->conn->llist_lock);
7076 list_add_tail(&smb_lock->clist,
7077 &work->conn->lock_list);
7078 list_add_tail(&smb_lock->flist,
7079 &fp->lock_list);
7080 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09007081 list_add(&smb_lock->llist, &rollback_list);
7082 ksmbd_debug(SMB, "successful in taking lock\n");
7083 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007084 goto out;
7085 }
7086 }
7087 }
7088
7089 if (atomic_read(&fp->f_ci->op_count) > 1)
7090 smb_break_all_oplock(work, fp);
7091
7092 rsp->StructureSize = cpu_to_le16(4);
7093 ksmbd_debug(SMB, "successful in taking lock\n");
7094 rsp->hdr.Status = STATUS_SUCCESS;
7095 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09007096 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09007097 ksmbd_fd_put(work, fp);
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007098 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09007099
7100out:
7101 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
7102 locks_free_lock(smb_lock->fl);
7103 list_del(&smb_lock->llist);
7104 kfree(smb_lock);
7105 }
7106
7107 list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) {
7108 struct file_lock *rlock = NULL;
7109
7110 rlock = smb_flock_init(filp);
7111 rlock->fl_type = F_UNLCK;
7112 rlock->fl_start = smb_lock->start;
7113 rlock->fl_end = smb_lock->end;
7114
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007115 rc = vfs_lock_file(filp, 0, rlock, NULL);
7116 if (rc)
7117 pr_err("rollback unlock fail : %d\n", rc);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007118
Namjae Jeone2f34482021-03-16 10:49:09 +09007119 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007120 spin_lock(&work->conn->llist_lock);
7121 if (!list_empty(&smb_lock->flist))
7122 list_del(&smb_lock->flist);
7123 list_del(&smb_lock->clist);
7124 spin_unlock(&work->conn->llist_lock);
7125
Namjae Jeone2f34482021-03-16 10:49:09 +09007126 locks_free_lock(smb_lock->fl);
7127 locks_free_lock(rlock);
7128 kfree(smb_lock);
7129 }
7130out2:
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09007131 ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err);
7132
7133 if (!rsp->hdr.Status) {
7134 if (err == -EINVAL)
7135 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7136 else if (err == -ENOMEM)
7137 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
7138 else if (err == -ENOENT)
7139 rsp->hdr.Status = STATUS_FILE_CLOSED;
7140 else
7141 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
7142 }
7143
Namjae Jeone2f34482021-03-16 10:49:09 +09007144 smb2_set_err_rsp(work);
7145 ksmbd_fd_put(work, fp);
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007146 return err;
Namjae Jeone2f34482021-03-16 10:49:09 +09007147}
7148
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007149static int fsctl_copychunk(struct ksmbd_work *work,
7150 struct copychunk_ioctl_req *ci_req,
7151 unsigned int cnt_code,
7152 unsigned int input_count,
7153 unsigned long long volatile_id,
7154 unsigned long long persistent_id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007155 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007156{
Namjae Jeone2f34482021-03-16 10:49:09 +09007157 struct copychunk_ioctl_rsp *ci_rsp;
7158 struct ksmbd_file *src_fp = NULL, *dst_fp = NULL;
7159 struct srv_copychunk *chunks;
7160 unsigned int i, chunk_count, chunk_count_written = 0;
7161 unsigned int chunk_size_written = 0;
7162 loff_t total_size_written = 0;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007163 int ret = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09007164
Namjae Jeone2f34482021-03-16 10:49:09 +09007165 ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0];
7166
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007167 rsp->VolatileFileId = cpu_to_le64(volatile_id);
7168 rsp->PersistentFileId = cpu_to_le64(persistent_id);
Namjae Jeon64b39f42021-03-30 14:25:35 +09007169 ci_rsp->ChunksWritten =
7170 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count());
7171 ci_rsp->ChunkBytesWritten =
7172 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size());
7173 ci_rsp->TotalBytesWritten =
7174 cpu_to_le32(ksmbd_server_side_copy_max_total_size());
Namjae Jeone2f34482021-03-16 10:49:09 +09007175
7176 chunks = (struct srv_copychunk *)&ci_req->Chunks[0];
7177 chunk_count = le32_to_cpu(ci_req->ChunkCount);
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007178 if (chunk_count == 0)
7179 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09007180 total_size_written = 0;
7181
7182 /* verify the SRV_COPYCHUNK_COPY packet */
7183 if (chunk_count > ksmbd_server_side_copy_max_chunk_count() ||
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007184 input_count < offsetof(struct copychunk_ioctl_req, Chunks) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09007185 chunk_count * sizeof(struct srv_copychunk)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007186 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7187 return -EINVAL;
7188 }
7189
7190 for (i = 0; i < chunk_count; i++) {
7191 if (le32_to_cpu(chunks[i].Length) == 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09007192 le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size())
Namjae Jeone2f34482021-03-16 10:49:09 +09007193 break;
7194 total_size_written += le32_to_cpu(chunks[i].Length);
7195 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09007196
7197 if (i < chunk_count ||
7198 total_size_written > ksmbd_server_side_copy_max_total_size()) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007199 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7200 return -EINVAL;
7201 }
7202
7203 src_fp = ksmbd_lookup_foreign_fd(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007204 le64_to_cpu(ci_req->ResumeKey[0]));
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007205 dst_fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
Namjae Jeone2f34482021-03-16 10:49:09 +09007206 ret = -EINVAL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007207 if (!src_fp ||
7208 src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007209 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7210 goto out;
7211 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09007212
Namjae Jeone2f34482021-03-16 10:49:09 +09007213 if (!dst_fp) {
7214 rsp->hdr.Status = STATUS_FILE_CLOSED;
7215 goto out;
7216 }
7217
7218 /*
7219 * FILE_READ_DATA should only be included in
7220 * the FSCTL_COPYCHUNK case
7221 */
Namjae Jeon070fb212021-05-26 17:57:12 +09007222 if (cnt_code == FSCTL_COPYCHUNK &&
7223 !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007224 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7225 goto out;
7226 }
7227
7228 ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007229 chunks, chunk_count,
7230 &chunk_count_written,
7231 &chunk_size_written,
7232 &total_size_written);
Namjae Jeone2f34482021-03-16 10:49:09 +09007233 if (ret < 0) {
7234 if (ret == -EACCES)
7235 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7236 if (ret == -EAGAIN)
7237 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
7238 else if (ret == -EBADF)
7239 rsp->hdr.Status = STATUS_INVALID_HANDLE;
7240 else if (ret == -EFBIG || ret == -ENOSPC)
7241 rsp->hdr.Status = STATUS_DISK_FULL;
7242 else if (ret == -EINVAL)
7243 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7244 else if (ret == -EISDIR)
7245 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
7246 else if (ret == -E2BIG)
7247 rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE;
7248 else
7249 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
7250 }
7251
7252 ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written);
7253 ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written);
7254 ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written);
7255out:
7256 ksmbd_fd_put(work, src_fp);
7257 ksmbd_fd_put(work, dst_fp);
7258 return ret;
7259}
7260
7261static __be32 idev_ipv4_address(struct in_device *idev)
7262{
7263 __be32 addr = 0;
7264
7265 struct in_ifaddr *ifa;
7266
7267 rcu_read_lock();
7268 in_dev_for_each_ifa_rcu(ifa, idev) {
7269 if (ifa->ifa_flags & IFA_F_SECONDARY)
7270 continue;
7271
7272 addr = ifa->ifa_address;
7273 break;
7274 }
7275 rcu_read_unlock();
7276 return addr;
7277}
7278
7279static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007280 struct smb2_ioctl_rsp *rsp,
7281 unsigned int out_buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09007282{
7283 struct network_interface_info_ioctl_rsp *nii_rsp = NULL;
7284 int nbytes = 0;
7285 struct net_device *netdev;
7286 struct sockaddr_storage_rsp *sockaddr_storage;
7287 unsigned int flags;
7288 unsigned long long speed;
7289
7290 rtnl_lock();
7291 for_each_netdev(&init_net, netdev) {
Namjae Jeon71cd9cb2021-12-16 10:31:44 +09007292 bool ipv4_set = false;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007293
Namjae Jeone2f34482021-03-16 10:49:09 +09007294 if (netdev->type == ARPHRD_LOOPBACK)
7295 continue;
7296
7297 flags = dev_get_flags(netdev);
7298 if (!(flags & IFF_RUNNING))
7299 continue;
Namjae Jeon71cd9cb2021-12-16 10:31:44 +09007300ipv6_retry:
7301 if (out_buf_len <
7302 nbytes + sizeof(struct network_interface_info_ioctl_rsp)) {
7303 rtnl_unlock();
7304 return -ENOSPC;
7305 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007306
7307 nii_rsp = (struct network_interface_info_ioctl_rsp *)
7308 &rsp->Buffer[nbytes];
7309 nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex);
7310
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09007311 nii_rsp->Capability = 0;
Namjae Jeona58b45a2021-12-16 10:26:43 +09007312 if (netdev->real_num_tx_queues > 1)
7313 nii_rsp->Capability |= cpu_to_le32(RSS_CAPABLE);
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09007314 if (ksmbd_rdma_capable_netdev(netdev))
7315 nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE);
Namjae Jeone2f34482021-03-16 10:49:09 +09007316
7317 nii_rsp->Next = cpu_to_le32(152);
7318 nii_rsp->Reserved = 0;
7319
7320 if (netdev->ethtool_ops->get_link_ksettings) {
7321 struct ethtool_link_ksettings cmd;
7322
7323 netdev->ethtool_ops->get_link_ksettings(netdev, &cmd);
7324 speed = cmd.base.speed;
7325 } else {
Per Forlind4758662021-08-30 13:23:04 +09007326 ksmbd_debug(SMB, "%s %s\n", netdev->name,
7327 "speed is unknown, defaulting to 1Gb/sec");
Namjae Jeone2f34482021-03-16 10:49:09 +09007328 speed = SPEED_1000;
7329 }
7330
7331 speed *= 1000000;
7332 nii_rsp->LinkSpeed = cpu_to_le64(speed);
7333
7334 sockaddr_storage = (struct sockaddr_storage_rsp *)
7335 nii_rsp->SockAddr_Storage;
7336 memset(sockaddr_storage, 0, 128);
7337
Namjae Jeon71cd9cb2021-12-16 10:31:44 +09007338 if (!ipv4_set) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007339 struct in_device *idev;
7340
7341 sockaddr_storage->Family = cpu_to_le16(INTERNETWORK);
7342 sockaddr_storage->addr4.Port = 0;
7343
7344 idev = __in_dev_get_rtnl(netdev);
7345 if (!idev)
7346 continue;
7347 sockaddr_storage->addr4.IPv4address =
7348 idev_ipv4_address(idev);
Namjae Jeon71cd9cb2021-12-16 10:31:44 +09007349 nbytes += sizeof(struct network_interface_info_ioctl_rsp);
7350 ipv4_set = true;
7351 goto ipv6_retry;
Namjae Jeone2f34482021-03-16 10:49:09 +09007352 } else {
7353 struct inet6_dev *idev6;
7354 struct inet6_ifaddr *ifa;
7355 __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address;
7356
7357 sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6);
7358 sockaddr_storage->addr6.Port = 0;
7359 sockaddr_storage->addr6.FlowInfo = 0;
7360
7361 idev6 = __in6_dev_get(netdev);
7362 if (!idev6)
7363 continue;
7364
7365 list_for_each_entry(ifa, &idev6->addr_list, if_list) {
7366 if (ifa->flags & (IFA_F_TENTATIVE |
7367 IFA_F_DEPRECATED))
7368 continue;
7369 memcpy(ipv6_addr, ifa->addr.s6_addr, 16);
7370 break;
7371 }
7372 sockaddr_storage->addr6.ScopeId = 0;
Namjae Jeon71cd9cb2021-12-16 10:31:44 +09007373 nbytes += sizeof(struct network_interface_info_ioctl_rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007374 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007375 }
7376 rtnl_unlock();
7377
7378 /* zero if this is last one */
7379 if (nii_rsp)
7380 nii_rsp->Next = 0;
7381
Namjae Jeone2f34482021-03-16 10:49:09 +09007382 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7383 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7384 return nbytes;
7385}
7386
Namjae Jeone2f34482021-03-16 10:49:09 +09007387static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09007388 struct validate_negotiate_info_req *neg_req,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007389 struct validate_negotiate_info_rsp *neg_rsp,
7390 unsigned int in_buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09007391{
7392 int ret = 0;
7393 int dialect;
7394
Marios Makassikis78f16882021-10-28 21:01:27 +02007395 if (in_buf_len < offsetof(struct validate_negotiate_info_req, Dialects) +
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007396 le16_to_cpu(neg_req->DialectCount) * sizeof(__le16))
7397 return -EINVAL;
7398
Namjae Jeone2f34482021-03-16 10:49:09 +09007399 dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects,
Namjae Jeon070fb212021-05-26 17:57:12 +09007400 neg_req->DialectCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09007401 if (dialect == BAD_PROT_ID || dialect != conn->dialect) {
7402 ret = -EINVAL;
7403 goto err_out;
7404 }
7405
7406 if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) {
7407 ret = -EINVAL;
7408 goto err_out;
7409 }
7410
7411 if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) {
7412 ret = -EINVAL;
7413 goto err_out;
7414 }
7415
7416 if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) {
7417 ret = -EINVAL;
7418 goto err_out;
7419 }
7420
7421 neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
7422 memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE);
7423 neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode);
7424 neg_rsp->Dialect = cpu_to_le16(conn->dialect);
7425err_out:
7426 return ret;
7427}
7428
Namjae Jeon64b39f42021-03-30 14:25:35 +09007429static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007430 struct file_allocated_range_buffer *qar_req,
7431 struct file_allocated_range_buffer *qar_rsp,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007432 unsigned int in_count, unsigned int *out_count)
Namjae Jeone2f34482021-03-16 10:49:09 +09007433{
7434 struct ksmbd_file *fp;
7435 loff_t start, length;
7436 int ret = 0;
7437
7438 *out_count = 0;
7439 if (in_count == 0)
7440 return -EINVAL;
7441
7442 fp = ksmbd_lookup_fd_fast(work, id);
7443 if (!fp)
7444 return -ENOENT;
7445
7446 start = le64_to_cpu(qar_req->file_offset);
7447 length = le64_to_cpu(qar_req->length);
7448
7449 ret = ksmbd_vfs_fqar_lseek(fp, start, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09007450 qar_rsp, in_count, out_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09007451 if (ret && ret != -E2BIG)
7452 *out_count = 0;
7453
7454 ksmbd_fd_put(work, fp);
7455 return ret;
7456}
7457
Namjae Jeon64b39f42021-03-30 14:25:35 +09007458static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007459 unsigned int out_buf_len,
7460 struct smb2_ioctl_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007461 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007462{
7463 struct ksmbd_rpc_command *rpc_resp;
7464 char *data_buf = (char *)&req->Buffer[0];
7465 int nbytes = 0;
7466
Namjae Jeon64b39f42021-03-30 14:25:35 +09007467 rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09007468 le32_to_cpu(req->InputCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09007469 if (rpc_resp) {
7470 if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) {
7471 /*
7472 * set STATUS_SOME_NOT_MAPPED response
7473 * for unknown domain sid.
7474 */
7475 rsp->hdr.Status = STATUS_SOME_NOT_MAPPED;
7476 } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
7477 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7478 goto out;
7479 } else if (rpc_resp->flags != KSMBD_RPC_OK) {
7480 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7481 goto out;
7482 }
7483
7484 nbytes = rpc_resp->payload_sz;
7485 if (rpc_resp->payload_sz > out_buf_len) {
7486 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7487 nbytes = out_buf_len;
7488 }
7489
7490 if (!rpc_resp->payload_sz) {
7491 rsp->hdr.Status =
7492 STATUS_UNEXPECTED_IO_ERROR;
7493 goto out;
7494 }
7495
7496 memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes);
7497 }
7498out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09007499 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007500 return nbytes;
7501}
7502
Namjae Jeon64b39f42021-03-30 14:25:35 +09007503static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007504 struct file_sparse *sparse)
Namjae Jeone2f34482021-03-16 10:49:09 +09007505{
7506 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007507 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09007508 int ret = 0;
7509 __le32 old_fattr;
7510
7511 fp = ksmbd_lookup_fd_fast(work, id);
7512 if (!fp)
7513 return -ENOENT;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007514 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007515
7516 old_fattr = fp->f_ci->m_fattr;
7517 if (sparse->SetSparse)
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09007518 fp->f_ci->m_fattr |= FILE_ATTRIBUTE_SPARSE_FILE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09007519 else
Ronnie Sahlberg26a27872021-11-03 08:45:52 +09007520 fp->f_ci->m_fattr &= ~FILE_ATTRIBUTE_SPARSE_FILE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09007521
7522 if (fp->f_ci->m_fattr != old_fattr &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007523 test_share_config_flag(work->tcon->share_conf,
7524 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007525 struct xattr_dos_attrib da;
7526
Hyunchul Lee465d7202021-07-03 12:10:36 +09007527 ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007528 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007529 if (ret <= 0)
7530 goto out;
7531
7532 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
Hyunchul Lee465d7202021-07-03 12:10:36 +09007533 ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007534 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007535 if (ret)
7536 fp->f_ci->m_fattr = old_fattr;
7537 }
7538
7539out:
7540 ksmbd_fd_put(work, fp);
7541 return ret;
7542}
7543
7544static int fsctl_request_resume_key(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007545 struct smb2_ioctl_req *req,
7546 struct resume_key_ioctl_rsp *key_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007547{
7548 struct ksmbd_file *fp;
7549
7550 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007551 le64_to_cpu(req->VolatileFileId),
7552 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09007553 if (!fp)
7554 return -ENOENT;
7555
7556 memset(key_rsp, 0, sizeof(*key_rsp));
7557 key_rsp->ResumeKey[0] = req->VolatileFileId;
7558 key_rsp->ResumeKey[1] = req->PersistentFileId;
7559 ksmbd_fd_put(work, fp);
7560
7561 return 0;
7562}
7563
7564/**
7565 * smb2_ioctl() - handler for smb2 ioctl command
7566 * @work: smb work containing ioctl command buffer
7567 *
7568 * Return: 0 on success, otherwise error
7569 */
7570int smb2_ioctl(struct ksmbd_work *work)
7571{
7572 struct smb2_ioctl_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09007573 struct smb2_ioctl_rsp *rsp;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007574 unsigned int cnt_code, nbytes = 0, out_buf_len, in_buf_len;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007575 u64 id = KSMBD_NO_FID;
Namjae Jeone2f34482021-03-16 10:49:09 +09007576 struct ksmbd_conn *conn = work->conn;
7577 int ret = 0;
7578
Namjae Jeone2f34482021-03-16 10:49:09 +09007579 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09007580 req = ksmbd_req_buf_next(work);
7581 rsp = ksmbd_resp_buf_next(work);
Namjae Jeon38673692021-07-08 12:32:27 +09007582 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
7583 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007584 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09007585 id = work->compound_fid;
7586 }
7587 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09007588 req = smb2_get_msg(work->request_buf);
7589 rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09007590 }
7591
Namjae Jeon38673692021-07-08 12:32:27 +09007592 if (!has_file_id(id))
Namjae Jeone2f34482021-03-16 10:49:09 +09007593 id = le64_to_cpu(req->VolatileFileId);
7594
7595 if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) {
7596 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7597 goto out;
7598 }
7599
7600 cnt_code = le32_to_cpu(req->CntCode);
Hyunchul Lee34061d62021-10-16 08:39:54 +09007601 ret = smb2_calc_max_out_buf_len(work, 48,
7602 le32_to_cpu(req->MaxOutputResponse));
7603 if (ret < 0) {
7604 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7605 goto out;
7606 }
7607 out_buf_len = (unsigned int)ret;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007608 in_buf_len = le32_to_cpu(req->InputCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09007609
7610 switch (cnt_code) {
7611 case FSCTL_DFS_GET_REFERRALS:
7612 case FSCTL_DFS_GET_REFERRALS_EX:
7613 /* Not support DFS yet */
7614 rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED;
7615 goto out;
7616 case FSCTL_CREATE_OR_GET_OBJECT_ID:
7617 {
7618 struct file_object_buf_type1_ioctl_rsp *obj_buf;
7619
7620 nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp);
7621 obj_buf = (struct file_object_buf_type1_ioctl_rsp *)
7622 &rsp->Buffer[0];
7623
7624 /*
7625 * TODO: This is dummy implementation to pass smbtorture
7626 * Need to check correct response later
7627 */
7628 memset(obj_buf->ObjectId, 0x0, 16);
7629 memset(obj_buf->BirthVolumeId, 0x0, 16);
7630 memset(obj_buf->BirthObjectId, 0x0, 16);
7631 memset(obj_buf->DomainId, 0x0, 16);
7632
7633 break;
7634 }
7635 case FSCTL_PIPE_TRANSCEIVE:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007636 out_buf_len = min_t(u32, KSMBD_IPC_MAX_PAYLOAD, out_buf_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09007637 nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp);
7638 break;
7639 case FSCTL_VALIDATE_NEGOTIATE_INFO:
7640 if (conn->dialect < SMB30_PROT_ID) {
7641 ret = -EOPNOTSUPP;
7642 goto out;
7643 }
7644
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007645 if (in_buf_len < sizeof(struct validate_negotiate_info_req))
7646 return -EINVAL;
7647
7648 if (out_buf_len < sizeof(struct validate_negotiate_info_rsp))
7649 return -EINVAL;
7650
Namjae Jeone2f34482021-03-16 10:49:09 +09007651 ret = fsctl_validate_negotiate_info(conn,
7652 (struct validate_negotiate_info_req *)&req->Buffer[0],
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007653 (struct validate_negotiate_info_rsp *)&rsp->Buffer[0],
7654 in_buf_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09007655 if (ret < 0)
7656 goto out;
7657
7658 nbytes = sizeof(struct validate_negotiate_info_rsp);
7659 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7660 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7661 break;
7662 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007663 ret = fsctl_query_iface_info_ioctl(conn, rsp, out_buf_len);
7664 if (ret < 0)
Namjae Jeone2f34482021-03-16 10:49:09 +09007665 goto out;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007666 nbytes = ret;
Namjae Jeone2f34482021-03-16 10:49:09 +09007667 break;
7668 case FSCTL_REQUEST_RESUME_KEY:
7669 if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) {
7670 ret = -EINVAL;
7671 goto out;
7672 }
7673
7674 ret = fsctl_request_resume_key(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007675 (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007676 if (ret < 0)
7677 goto out;
7678 rsp->PersistentFileId = req->PersistentFileId;
7679 rsp->VolatileFileId = req->VolatileFileId;
7680 nbytes = sizeof(struct resume_key_ioctl_rsp);
7681 break;
7682 case FSCTL_COPYCHUNK:
7683 case FSCTL_COPYCHUNK_WRITE:
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 copychunk_ioctl_req)) {
7692 ret = -EINVAL;
7693 goto out;
7694 }
7695
Namjae Jeone2f34482021-03-16 10:49:09 +09007696 if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) {
7697 ret = -EINVAL;
7698 goto out;
7699 }
7700
7701 nbytes = sizeof(struct copychunk_ioctl_rsp);
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007702 rsp->VolatileFileId = req->VolatileFileId;
7703 rsp->PersistentFileId = req->PersistentFileId;
7704 fsctl_copychunk(work,
7705 (struct copychunk_ioctl_req *)&req->Buffer[0],
7706 le32_to_cpu(req->CntCode),
7707 le32_to_cpu(req->InputCount),
7708 le64_to_cpu(req->VolatileFileId),
7709 le64_to_cpu(req->PersistentFileId),
7710 rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007711 break;
7712 case FSCTL_SET_SPARSE:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007713 if (in_buf_len < sizeof(struct file_sparse)) {
7714 ret = -EINVAL;
7715 goto out;
7716 }
7717
Namjae Jeone2f34482021-03-16 10:49:09 +09007718 ret = fsctl_set_sparse(work, id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007719 (struct file_sparse *)&req->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007720 if (ret < 0)
7721 goto out;
7722 break;
7723 case FSCTL_SET_ZERO_DATA:
7724 {
7725 struct file_zero_data_information *zero_data;
7726 struct ksmbd_file *fp;
7727 loff_t off, len;
7728
Namjae Jeon64b39f42021-03-30 14:25:35 +09007729 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007730 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007731 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007732 ret = -EACCES;
7733 goto out;
7734 }
7735
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007736 if (in_buf_len < sizeof(struct file_zero_data_information)) {
7737 ret = -EINVAL;
7738 goto out;
7739 }
7740
Namjae Jeone2f34482021-03-16 10:49:09 +09007741 zero_data =
7742 (struct file_zero_data_information *)&req->Buffer[0];
7743
7744 fp = ksmbd_lookup_fd_fast(work, id);
7745 if (!fp) {
7746 ret = -ENOENT;
7747 goto out;
7748 }
7749
7750 off = le64_to_cpu(zero_data->FileOffset);
7751 len = le64_to_cpu(zero_data->BeyondFinalZero) - off;
7752
7753 ret = ksmbd_vfs_zero_data(work, fp, off, len);
7754 ksmbd_fd_put(work, fp);
7755 if (ret < 0)
7756 goto out;
7757 break;
7758 }
7759 case FSCTL_QUERY_ALLOCATED_RANGES:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007760 if (in_buf_len < sizeof(struct file_allocated_range_buffer)) {
7761 ret = -EINVAL;
7762 goto out;
7763 }
7764
Namjae Jeone2f34482021-03-16 10:49:09 +09007765 ret = fsctl_query_allocated_ranges(work, id,
7766 (struct file_allocated_range_buffer *)&req->Buffer[0],
7767 (struct file_allocated_range_buffer *)&rsp->Buffer[0],
7768 out_buf_len /
7769 sizeof(struct file_allocated_range_buffer), &nbytes);
7770 if (ret == -E2BIG) {
7771 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7772 } else if (ret < 0) {
7773 nbytes = 0;
7774 goto out;
7775 }
7776
7777 nbytes *= sizeof(struct file_allocated_range_buffer);
7778 break;
7779 case FSCTL_GET_REPARSE_POINT:
7780 {
7781 struct reparse_data_buffer *reparse_ptr;
7782 struct ksmbd_file *fp;
7783
7784 reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0];
7785 fp = ksmbd_lookup_fd_fast(work, id);
7786 if (!fp) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007787 pr_err("not found fp!!\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007788 ret = -ENOENT;
7789 goto out;
7790 }
7791
7792 reparse_ptr->ReparseTag =
Namjae Jeonab0b2632021-06-29 09:20:13 +09007793 smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode);
Namjae Jeone2f34482021-03-16 10:49:09 +09007794 reparse_ptr->ReparseDataLength = 0;
7795 ksmbd_fd_put(work, fp);
7796 nbytes = sizeof(struct reparse_data_buffer);
7797 break;
7798 }
Namjae Jeoneb817362021-05-18 10:37:59 +09007799 case FSCTL_DUPLICATE_EXTENTS_TO_FILE:
7800 {
7801 struct ksmbd_file *fp_in, *fp_out = NULL;
7802 struct duplicate_extents_to_file *dup_ext;
7803 loff_t src_off, dst_off, length, cloned;
7804
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007805 if (in_buf_len < sizeof(struct duplicate_extents_to_file)) {
7806 ret = -EINVAL;
7807 goto out;
7808 }
7809
Namjae Jeoneb817362021-05-18 10:37:59 +09007810 dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0];
7811
7812 fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle,
Namjae Jeon070fb212021-05-26 17:57:12 +09007813 dup_ext->PersistentFileHandle);
Namjae Jeoneb817362021-05-18 10:37:59 +09007814 if (!fp_in) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007815 pr_err("not found file handle in duplicate extent to file\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007816 ret = -ENOENT;
7817 goto out;
7818 }
7819
7820 fp_out = ksmbd_lookup_fd_fast(work, id);
7821 if (!fp_out) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007822 pr_err("not found fp\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007823 ret = -ENOENT;
7824 goto dup_ext_out;
7825 }
7826
7827 src_off = le64_to_cpu(dup_ext->SourceFileOffset);
7828 dst_off = le64_to_cpu(dup_ext->TargetFileOffset);
7829 length = le64_to_cpu(dup_ext->ByteCount);
7830 cloned = vfs_clone_file_range(fp_in->filp, src_off, fp_out->filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007831 dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007832 if (cloned == -EXDEV || cloned == -EOPNOTSUPP) {
7833 ret = -EOPNOTSUPP;
7834 goto dup_ext_out;
7835 } else if (cloned != length) {
Namjae Jeonf8524772021-06-18 10:28:00 +09007836 cloned = vfs_copy_file_range(fp_in->filp, src_off,
7837 fp_out->filp, dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007838 if (cloned != length) {
7839 if (cloned < 0)
7840 ret = cloned;
7841 else
7842 ret = -EINVAL;
7843 }
7844 }
7845
7846dup_ext_out:
7847 ksmbd_fd_put(work, fp_in);
7848 ksmbd_fd_put(work, fp_out);
7849 if (ret < 0)
7850 goto out;
7851 break;
7852 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007853 default:
7854 ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007855 cnt_code);
Namjae Jeone2f34482021-03-16 10:49:09 +09007856 ret = -EOPNOTSUPP;
7857 goto out;
7858 }
7859
7860 rsp->CntCode = cpu_to_le32(cnt_code);
7861 rsp->InputCount = cpu_to_le32(0);
7862 rsp->InputOffset = cpu_to_le32(112);
7863 rsp->OutputOffset = cpu_to_le32(112);
7864 rsp->OutputCount = cpu_to_le32(nbytes);
7865 rsp->StructureSize = cpu_to_le16(49);
7866 rsp->Reserved = cpu_to_le16(0);
7867 rsp->Flags = cpu_to_le32(0);
7868 rsp->Reserved2 = cpu_to_le32(0);
Namjae Jeoncb451722021-11-03 08:08:44 +09007869 inc_rfc1001_len(work->response_buf, 48 + nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09007870
7871 return 0;
7872
7873out:
7874 if (ret == -EACCES)
7875 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7876 else if (ret == -ENOENT)
7877 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7878 else if (ret == -EOPNOTSUPP)
7879 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007880 else if (ret == -ENOSPC)
7881 rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL;
Namjae Jeone2f34482021-03-16 10:49:09 +09007882 else if (ret < 0 || rsp->hdr.Status == 0)
7883 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7884 smb2_set_err_rsp(work);
7885 return 0;
7886}
7887
7888/**
7889 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command
7890 * @work: smb work containing oplock break command buffer
7891 *
7892 * Return: 0
7893 */
7894static void smb20_oplock_break_ack(struct ksmbd_work *work)
7895{
Namjae Jeoncb451722021-11-03 08:08:44 +09007896 struct smb2_oplock_break *req = smb2_get_msg(work->request_buf);
7897 struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09007898 struct ksmbd_file *fp;
7899 struct oplock_info *opinfo = NULL;
7900 __le32 err = 0;
7901 int ret = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007902 u64 volatile_id, persistent_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09007903 char req_oplevel = 0, rsp_oplevel = 0;
7904 unsigned int oplock_change_type;
7905
7906 volatile_id = le64_to_cpu(req->VolatileFid);
7907 persistent_id = le64_to_cpu(req->PersistentFid);
7908 req_oplevel = req->OplockLevel;
7909 ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n",
7910 volatile_id, persistent_id, req_oplevel);
7911
7912 fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
7913 if (!fp) {
7914 rsp->hdr.Status = STATUS_FILE_CLOSED;
7915 smb2_set_err_rsp(work);
7916 return;
7917 }
7918
7919 opinfo = opinfo_get(fp);
7920 if (!opinfo) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007921 pr_err("unexpected null oplock_info\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007922 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7923 smb2_set_err_rsp(work);
7924 ksmbd_fd_put(work, fp);
7925 return;
7926 }
7927
7928 if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) {
7929 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7930 goto err_out;
7931 }
7932
7933 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7934 ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state);
7935 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7936 goto err_out;
7937 }
7938
Namjae Jeon64b39f42021-03-30 14:25:35 +09007939 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7940 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7941 (req_oplevel != SMB2_OPLOCK_LEVEL_II &&
7942 req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007943 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7944 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007945 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7946 req_oplevel != SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007947 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7948 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007949 } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II ||
7950 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007951 err = STATUS_INVALID_DEVICE_STATE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007952 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7953 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7954 req_oplevel == SMB2_OPLOCK_LEVEL_II) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007955 oplock_change_type = OPLOCK_WRITE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007956 } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7957 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7958 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007959 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007960 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7961 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007962 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007963 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007964 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007965 }
7966 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007967 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007968 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007969
7970 switch (oplock_change_type) {
7971 case OPLOCK_WRITE_TO_READ:
7972 ret = opinfo_write_to_read(opinfo);
7973 rsp_oplevel = SMB2_OPLOCK_LEVEL_II;
7974 break;
7975 case OPLOCK_WRITE_TO_NONE:
7976 ret = opinfo_write_to_none(opinfo);
7977 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7978 break;
7979 case OPLOCK_READ_TO_NONE:
7980 ret = opinfo_read_to_none(opinfo);
7981 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7982 break;
7983 default:
Namjae Jeonbde16942021-06-28 15:23:19 +09007984 pr_err("unknown oplock change 0x%x -> 0x%x\n",
7985 opinfo->level, rsp_oplevel);
Namjae Jeone2f34482021-03-16 10:49:09 +09007986 }
7987
7988 if (ret < 0) {
7989 rsp->hdr.Status = err;
7990 goto err_out;
7991 }
7992
7993 opinfo_put(opinfo);
7994 ksmbd_fd_put(work, fp);
7995 opinfo->op_state = OPLOCK_STATE_NONE;
7996 wake_up_interruptible_all(&opinfo->oplock_q);
7997
7998 rsp->StructureSize = cpu_to_le16(24);
7999 rsp->OplockLevel = rsp_oplevel;
8000 rsp->Reserved = 0;
8001 rsp->Reserved2 = 0;
8002 rsp->VolatileFid = cpu_to_le64(volatile_id);
8003 rsp->PersistentFid = cpu_to_le64(persistent_id);
Namjae Jeoncb451722021-11-03 08:08:44 +09008004 inc_rfc1001_len(work->response_buf, 24);
Namjae Jeone2f34482021-03-16 10:49:09 +09008005 return;
8006
8007err_out:
8008 opinfo->op_state = OPLOCK_STATE_NONE;
8009 wake_up_interruptible_all(&opinfo->oplock_q);
8010
8011 opinfo_put(opinfo);
8012 ksmbd_fd_put(work, fp);
8013 smb2_set_err_rsp(work);
8014}
8015
8016static int check_lease_state(struct lease *lease, __le32 req_state)
8017{
8018 if ((lease->new_state ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09008019 (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) &&
8020 !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008021 lease->new_state = req_state;
8022 return 0;
8023 }
8024
8025 if (lease->new_state == req_state)
8026 return 0;
8027
8028 return 1;
8029}
8030
8031/**
8032 * smb21_lease_break_ack() - handler for smb2.1 lease break command
8033 * @work: smb work containing lease break command buffer
8034 *
8035 * Return: 0
8036 */
8037static void smb21_lease_break_ack(struct ksmbd_work *work)
8038{
8039 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09008040 struct smb2_lease_ack *req = smb2_get_msg(work->request_buf);
8041 struct smb2_lease_ack *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008042 struct oplock_info *opinfo;
8043 __le32 err = 0;
8044 int ret = 0;
8045 unsigned int lease_change_type;
8046 __le32 lease_state;
8047 struct lease *lease;
8048
8049 ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008050 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09008051 opinfo = lookup_lease_in_table(conn, req->LeaseKey);
8052 if (!opinfo) {
8053 ksmbd_debug(OPLOCK, "file not opened\n");
8054 smb2_set_err_rsp(work);
8055 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
8056 return;
8057 }
8058 lease = opinfo->o_lease;
8059
8060 if (opinfo->op_state == OPLOCK_STATE_NONE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008061 pr_err("unexpected lease break state 0x%x\n",
8062 opinfo->op_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09008063 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
8064 goto err_out;
8065 }
8066
8067 if (check_lease_state(lease, req->LeaseState)) {
8068 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
8069 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +09008070 "req lease state: 0x%x, expected state: 0x%x\n",
8071 req->LeaseState, lease->new_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09008072 goto err_out;
8073 }
8074
8075 if (!atomic_read(&opinfo->breaking_cnt)) {
8076 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
8077 goto err_out;
8078 }
8079
8080 /* check for bad lease state */
Namjae Jeon070fb212021-05-26 17:57:12 +09008081 if (req->LeaseState &
8082 (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008083 err = STATUS_INVALID_OPLOCK_PROTOCOL;
8084 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8085 lease_change_type = OPLOCK_WRITE_TO_NONE;
8086 else
8087 lease_change_type = OPLOCK_READ_TO_NONE;
8088 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008089 le32_to_cpu(lease->state),
8090 le32_to_cpu(req->LeaseState));
Namjae Jeon64b39f42021-03-30 14:25:35 +09008091 } else if (lease->state == SMB2_LEASE_READ_CACHING_LE &&
8092 req->LeaseState != SMB2_LEASE_NONE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008093 err = STATUS_INVALID_OPLOCK_PROTOCOL;
8094 lease_change_type = OPLOCK_READ_TO_NONE;
8095 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008096 le32_to_cpu(lease->state),
8097 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09008098 } else {
8099 /* valid lease state changes */
8100 err = STATUS_INVALID_DEVICE_STATE;
8101 if (req->LeaseState == SMB2_LEASE_NONE_LE) {
8102 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8103 lease_change_type = OPLOCK_WRITE_TO_NONE;
8104 else
8105 lease_change_type = OPLOCK_READ_TO_NONE;
8106 } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) {
8107 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8108 lease_change_type = OPLOCK_WRITE_TO_READ;
8109 else
8110 lease_change_type = OPLOCK_READ_HANDLE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09008111 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09008112 lease_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09008113 }
Namjae Jeone2f34482021-03-16 10:49:09 +09008114 }
8115
8116 switch (lease_change_type) {
8117 case OPLOCK_WRITE_TO_READ:
8118 ret = opinfo_write_to_read(opinfo);
8119 break;
8120 case OPLOCK_READ_HANDLE_TO_READ:
8121 ret = opinfo_read_handle_to_read(opinfo);
8122 break;
8123 case OPLOCK_WRITE_TO_NONE:
8124 ret = opinfo_write_to_none(opinfo);
8125 break;
8126 case OPLOCK_READ_TO_NONE:
8127 ret = opinfo_read_to_none(opinfo);
8128 break;
8129 default:
8130 ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008131 le32_to_cpu(lease->state),
8132 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09008133 }
8134
8135 lease_state = lease->state;
8136 opinfo->op_state = OPLOCK_STATE_NONE;
8137 wake_up_interruptible_all(&opinfo->oplock_q);
8138 atomic_dec(&opinfo->breaking_cnt);
8139 wake_up_interruptible_all(&opinfo->oplock_brk);
8140 opinfo_put(opinfo);
8141
8142 if (ret < 0) {
8143 rsp->hdr.Status = err;
8144 goto err_out;
8145 }
8146
8147 rsp->StructureSize = cpu_to_le16(36);
8148 rsp->Reserved = 0;
8149 rsp->Flags = 0;
8150 memcpy(rsp->LeaseKey, req->LeaseKey, 16);
8151 rsp->LeaseState = lease_state;
8152 rsp->LeaseDuration = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09008153 inc_rfc1001_len(work->response_buf, 36);
Namjae Jeone2f34482021-03-16 10:49:09 +09008154 return;
8155
8156err_out:
8157 opinfo->op_state = OPLOCK_STATE_NONE;
8158 wake_up_interruptible_all(&opinfo->oplock_q);
8159 atomic_dec(&opinfo->breaking_cnt);
8160 wake_up_interruptible_all(&opinfo->oplock_brk);
8161
8162 opinfo_put(opinfo);
8163 smb2_set_err_rsp(work);
8164}
8165
8166/**
8167 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break
8168 * @work: smb work containing oplock/lease break command buffer
8169 *
8170 * Return: 0
8171 */
8172int smb2_oplock_break(struct ksmbd_work *work)
8173{
Namjae Jeoncb451722021-11-03 08:08:44 +09008174 struct smb2_oplock_break *req = smb2_get_msg(work->request_buf);
8175 struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008176
8177 switch (le16_to_cpu(req->StructureSize)) {
8178 case OP_BREAK_STRUCT_SIZE_20:
8179 smb20_oplock_break_ack(work);
8180 break;
8181 case OP_BREAK_STRUCT_SIZE_21:
8182 smb21_lease_break_ack(work);
8183 break;
8184 default:
8185 ksmbd_debug(OPLOCK, "invalid break cmd %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008186 le16_to_cpu(req->StructureSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09008187 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
8188 smb2_set_err_rsp(work);
8189 }
8190
8191 return 0;
8192}
8193
8194/**
8195 * smb2_notify() - handler for smb2 notify request
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09008196 * @work: smb work containing notify command buffer
Namjae Jeone2f34482021-03-16 10:49:09 +09008197 *
8198 * Return: 0
8199 */
8200int smb2_notify(struct ksmbd_work *work)
8201{
Ronnie Sahlberg699230f2021-09-09 12:26:12 +09008202 struct smb2_change_notify_req *req;
8203 struct smb2_change_notify_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09008204
8205 WORK_BUFFERS(work, req, rsp);
8206
8207 if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) {
8208 rsp->hdr.Status = STATUS_INTERNAL_ERROR;
8209 smb2_set_err_rsp(work);
8210 return 0;
8211 }
8212
8213 smb2_set_err_rsp(work);
8214 rsp->hdr.Status = STATUS_NOT_IMPLEMENTED;
8215 return 0;
8216}
8217
8218/**
8219 * smb2_is_sign_req() - handler for checking packet signing status
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09008220 * @work: smb work containing notify command buffer
8221 * @command: SMB2 command id
Namjae Jeone2f34482021-03-16 10:49:09 +09008222 *
8223 * Return: true if packed is signed, false otherwise
8224 */
8225bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command)
8226{
Namjae Jeoncb451722021-11-03 08:08:44 +09008227 struct smb2_hdr *rcv_hdr2 = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008228
8229 if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008230 command != SMB2_NEGOTIATE_HE &&
8231 command != SMB2_SESSION_SETUP_HE &&
8232 command != SMB2_OPLOCK_BREAK_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +09008233 return true;
8234
kernel test robot56160152021-05-12 09:24:37 +09008235 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +09008236}
8237
8238/**
8239 * smb2_check_sign_req() - handler for req packet sign processing
8240 * @work: smb work containing notify command buffer
8241 *
8242 * Return: 1 on success, 0 otherwise
8243 */
8244int smb2_check_sign_req(struct ksmbd_work *work)
8245{
Namjae Jeoncb451722021-11-03 08:08:44 +09008246 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008247 char signature_req[SMB2_SIGNATURE_SIZE];
8248 char signature[SMB2_HMACSHA256_SIZE];
8249 struct kvec iov[1];
8250 size_t len;
8251
Namjae Jeoncb451722021-11-03 08:08:44 +09008252 hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008253 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008254 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008255
8256 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
Namjae Jeoncb451722021-11-03 08:08:44 +09008257 len = get_rfc1002_len(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008258 else if (hdr->NextCommand)
8259 len = le32_to_cpu(hdr->NextCommand);
8260 else
Namjae Jeoncb451722021-11-03 08:08:44 +09008261 len = get_rfc1002_len(work->request_buf) -
Namjae Jeone2f34482021-03-16 10:49:09 +09008262 work->next_smb2_rcv_hdr_off;
8263
8264 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8265 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8266
8267 iov[0].iov_base = (char *)&hdr->ProtocolId;
8268 iov[0].iov_len = len;
8269
8270 if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1,
Namjae Jeon64b39f42021-03-30 14:25:35 +09008271 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09008272 return 0;
8273
8274 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008275 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008276 return 0;
8277 }
8278
8279 return 1;
8280}
8281
8282/**
8283 * smb2_set_sign_rsp() - handler for rsp packet sign processing
8284 * @work: smb work containing notify command buffer
8285 *
8286 */
8287void smb2_set_sign_rsp(struct ksmbd_work *work)
8288{
Namjae Jeoncb451722021-11-03 08:08:44 +09008289 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008290 struct smb2_hdr *req_hdr;
8291 char signature[SMB2_HMACSHA256_SIZE];
8292 struct kvec iov[2];
8293 size_t len;
8294 int n_vec = 1;
8295
Namjae Jeoncb451722021-11-03 08:08:44 +09008296 hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008297 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008298 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008299
Namjae Jeon8a893312021-06-25 13:43:37 +09008300 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008301
8302 if (!work->next_smb2_rsp_hdr_off) {
Namjae Jeoncb451722021-11-03 08:08:44 +09008303 len = get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008304 if (req_hdr->NextCommand)
8305 len = ALIGN(len, 8);
8306 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09008307 len = get_rfc1002_len(work->response_buf) -
8308 work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +09008309 len = ALIGN(len, 8);
8310 }
8311
8312 if (req_hdr->NextCommand)
8313 hdr->NextCommand = cpu_to_le32(len);
8314
8315 hdr->Flags |= SMB2_FLAGS_SIGNED;
8316 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8317
8318 iov[0].iov_base = (char *)&hdr->ProtocolId;
8319 iov[0].iov_len = len;
8320
Namjae Jeone5066492021-03-30 12:35:23 +09008321 if (work->aux_payload_sz) {
8322 iov[0].iov_len -= work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008323
Namjae Jeone5066492021-03-30 12:35:23 +09008324 iov[1].iov_base = work->aux_payload_buf;
8325 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008326 n_vec++;
8327 }
8328
8329 if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec,
Namjae Jeon64b39f42021-03-30 14:25:35 +09008330 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09008331 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8332}
8333
8334/**
8335 * smb3_check_sign_req() - handler for req packet sign processing
8336 * @work: smb work containing notify command buffer
8337 *
8338 * Return: 1 on success, 0 otherwise
8339 */
8340int smb3_check_sign_req(struct ksmbd_work *work)
8341{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008342 struct ksmbd_conn *conn = work->conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09008343 char *signing_key;
Namjae Jeoncb451722021-11-03 08:08:44 +09008344 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008345 struct channel *chann;
8346 char signature_req[SMB2_SIGNATURE_SIZE];
8347 char signature[SMB2_CMACAES_SIZE];
8348 struct kvec iov[1];
8349 size_t len;
8350
Namjae Jeoncb451722021-11-03 08:08:44 +09008351 hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008352 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008353 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008354
8355 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
Namjae Jeoncb451722021-11-03 08:08:44 +09008356 len = get_rfc1002_len(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008357 else if (hdr->NextCommand)
8358 len = le32_to_cpu(hdr->NextCommand);
8359 else
Namjae Jeoncb451722021-11-03 08:08:44 +09008360 len = get_rfc1002_len(work->request_buf) -
Namjae Jeone2f34482021-03-16 10:49:09 +09008361 work->next_smb2_rcv_hdr_off;
8362
8363 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
8364 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008365 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008366 chann = lookup_chann_list(work->sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008367 if (!chann)
8368 return 0;
8369 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008370 }
8371
8372 if (!signing_key) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008373 pr_err("SMB3 signing key is not generated\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008374 return 0;
8375 }
8376
8377 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8378 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8379 iov[0].iov_base = (char *)&hdr->ProtocolId;
8380 iov[0].iov_len = len;
8381
8382 if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature))
8383 return 0;
8384
8385 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008386 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008387 return 0;
8388 }
8389
8390 return 1;
8391}
8392
8393/**
8394 * smb3_set_sign_rsp() - handler for rsp packet sign processing
8395 * @work: smb work containing notify command buffer
8396 *
8397 */
8398void smb3_set_sign_rsp(struct ksmbd_work *work)
8399{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008400 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09008401 struct smb2_hdr *req_hdr, *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008402 struct channel *chann;
8403 char signature[SMB2_CMACAES_SIZE];
8404 struct kvec iov[2];
8405 int n_vec = 1;
8406 size_t len;
8407 char *signing_key;
8408
Namjae Jeoncb451722021-11-03 08:08:44 +09008409 hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008410 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008411 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008412
Namjae Jeon8a893312021-06-25 13:43:37 +09008413 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008414
8415 if (!work->next_smb2_rsp_hdr_off) {
Namjae Jeoncb451722021-11-03 08:08:44 +09008416 len = get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008417 if (req_hdr->NextCommand)
8418 len = ALIGN(len, 8);
8419 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09008420 len = get_rfc1002_len(work->response_buf) -
8421 work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +09008422 len = ALIGN(len, 8);
8423 }
8424
Namjae Jeon08bdbc62021-07-27 09:30:29 +09008425 if (conn->binding == false &&
8426 le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008427 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008428 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008429 chann = lookup_chann_list(work->sess, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008430 if (!chann)
8431 return;
8432 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008433 }
8434
8435 if (!signing_key)
8436 return;
8437
8438 if (req_hdr->NextCommand)
8439 hdr->NextCommand = cpu_to_le32(len);
8440
8441 hdr->Flags |= SMB2_FLAGS_SIGNED;
8442 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8443 iov[0].iov_base = (char *)&hdr->ProtocolId;
8444 iov[0].iov_len = len;
Namjae Jeone5066492021-03-30 12:35:23 +09008445 if (work->aux_payload_sz) {
8446 iov[0].iov_len -= work->aux_payload_sz;
8447 iov[1].iov_base = work->aux_payload_buf;
8448 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008449 n_vec++;
8450 }
8451
8452 if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature))
8453 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8454}
8455
8456/**
8457 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response
8458 * @work: smb work containing response buffer
8459 *
8460 */
8461void smb3_preauth_hash_rsp(struct ksmbd_work *work)
8462{
8463 struct ksmbd_conn *conn = work->conn;
8464 struct ksmbd_session *sess = work->sess;
8465 struct smb2_hdr *req, *rsp;
8466
8467 if (conn->dialect != SMB311_PROT_ID)
8468 return;
8469
8470 WORK_BUFFERS(work, req, rsp);
8471
Namjae Jeon442ff9e2021-09-29 15:44:32 +09008472 if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE &&
8473 conn->preauth_info)
Namjae Jeoncb451722021-11-03 08:08:44 +09008474 ksmbd_gen_preauth_integrity_hash(conn, work->response_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008475 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09008476
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008477 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008478 __u8 *hash_value;
8479
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008480 if (conn->binding) {
8481 struct preauth_session *preauth_sess;
8482
8483 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
8484 if (!preauth_sess)
8485 return;
8486 hash_value = preauth_sess->Preauth_HashValue;
8487 } else {
8488 hash_value = sess->Preauth_HashValue;
8489 if (!hash_value)
8490 return;
8491 }
Namjae Jeoncb451722021-11-03 08:08:44 +09008492 ksmbd_gen_preauth_integrity_hash(conn, work->response_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008493 hash_value);
Namjae Jeone2f34482021-03-16 10:49:09 +09008494 }
8495}
8496
Namjae Jeon2dd91292021-11-03 08:25:54 +09008497static void fill_transform_hdr(void *tr_buf, char *old_buf, __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09008498{
Namjae Jeon2dd91292021-11-03 08:25:54 +09008499 struct smb2_transform_hdr *tr_hdr = tr_buf + 4;
8500 struct smb2_hdr *hdr = smb2_get_msg(old_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008501 unsigned int orig_len = get_rfc1002_len(old_buf);
8502
Namjae Jeon2dd91292021-11-03 08:25:54 +09008503 memset(tr_buf, 0, sizeof(struct smb2_transform_hdr) + 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09008504 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
8505 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
Ronnie Sahlberg4355a8f2021-11-03 08:43:42 +09008506 tr_hdr->Flags = cpu_to_le16(TRANSFORM_FLAG_ENCRYPTED);
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008507 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
8508 cipher_type == SMB2_ENCRYPTION_AES256_GCM)
8509 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008510 else
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008511 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008512 memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8);
Namjae Jeon2dd91292021-11-03 08:25:54 +09008513 inc_rfc1001_len(tr_buf, sizeof(struct smb2_transform_hdr));
8514 inc_rfc1001_len(tr_buf, orig_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09008515}
8516
8517int smb3_encrypt_resp(struct ksmbd_work *work)
8518{
Namjae Jeone5066492021-03-30 12:35:23 +09008519 char *buf = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008520 struct kvec iov[3];
8521 int rc = -ENOMEM;
Namjae Jeone5066492021-03-30 12:35:23 +09008522 int buf_size = 0, rq_nvec = 2 + (work->aux_payload_sz ? 1 : 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09008523
8524 if (ARRAY_SIZE(iov) < rq_nvec)
8525 return -ENOMEM;
8526
Namjae Jeon2dd91292021-11-03 08:25:54 +09008527 work->tr_buf = kzalloc(sizeof(struct smb2_transform_hdr) + 4, GFP_KERNEL);
8528 if (!work->tr_buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09008529 return rc;
8530
8531 /* fill transform header */
Namjae Jeon2dd91292021-11-03 08:25:54 +09008532 fill_transform_hdr(work->tr_buf, buf, work->conn->cipher_type);
Namjae Jeone2f34482021-03-16 10:49:09 +09008533
Namjae Jeon2dd91292021-11-03 08:25:54 +09008534 iov[0].iov_base = work->tr_buf;
8535 iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008536 buf_size += iov[0].iov_len - 4;
8537
8538 iov[1].iov_base = buf + 4;
8539 iov[1].iov_len = get_rfc1002_len(buf);
Namjae Jeone5066492021-03-30 12:35:23 +09008540 if (work->aux_payload_sz) {
8541 iov[1].iov_len = work->resp_hdr_sz - 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008542
Namjae Jeone5066492021-03-30 12:35:23 +09008543 iov[2].iov_base = work->aux_payload_buf;
8544 iov[2].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008545 buf_size += iov[2].iov_len;
8546 }
8547 buf_size += iov[1].iov_len;
8548 work->resp_hdr_sz = iov[1].iov_len;
8549
8550 rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1);
8551 if (rc)
8552 return rc;
8553
8554 memmove(buf, iov[1].iov_base, iov[1].iov_len);
Namjae Jeon2dd91292021-11-03 08:25:54 +09008555 *(__be32 *)work->tr_buf = cpu_to_be32(buf_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09008556
8557 return rc;
8558}
8559
Namjae Jeonf4228b62021-08-12 10:16:40 +09008560bool smb3_is_transform_hdr(void *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09008561{
Namjae Jeon2dd91292021-11-03 08:25:54 +09008562 struct smb2_transform_hdr *trhdr = smb2_get_msg(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008563
8564 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
8565}
8566
8567int smb3_decrypt_req(struct ksmbd_work *work)
8568{
8569 struct ksmbd_conn *conn = work->conn;
8570 struct ksmbd_session *sess;
Namjae Jeone5066492021-03-30 12:35:23 +09008571 char *buf = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008572 unsigned int pdu_length = get_rfc1002_len(buf);
8573 struct kvec iov[2];
Namjae Jeon2dd91292021-11-03 08:25:54 +09008574 int buf_data_size = pdu_length - sizeof(struct smb2_transform_hdr);
8575 struct smb2_transform_hdr *tr_hdr = smb2_get_msg(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008576 int rc = 0;
8577
Namjae Jeonc7705ee2021-10-04 20:44:52 +09008578 if (buf_data_size < sizeof(struct smb2_hdr)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008579 pr_err("Transform message is too small (%u)\n",
8580 pdu_length);
Namjae Jeone2f34482021-03-16 10:49:09 +09008581 return -ECONNABORTED;
8582 }
8583
Namjae Jeonc7705ee2021-10-04 20:44:52 +09008584 if (buf_data_size < le32_to_cpu(tr_hdr->OriginalMessageSize)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008585 pr_err("Transform message is broken\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008586 return -ECONNABORTED;
8587 }
8588
Namjae Jeon4227f812021-09-29 19:52:51 +09008589 sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId));
8590 if (!sess) {
8591 pr_err("invalid session id(%llx) in transform header\n",
8592 le64_to_cpu(tr_hdr->SessionId));
8593 return -ECONNABORTED;
8594 }
8595
Namjae Jeone2f34482021-03-16 10:49:09 +09008596 iov[0].iov_base = buf;
Namjae Jeon2dd91292021-11-03 08:25:54 +09008597 iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4;
8598 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008599 iov[1].iov_len = buf_data_size;
8600 rc = ksmbd_crypt_message(conn, iov, 2, 0);
8601 if (rc)
8602 return rc;
8603
8604 memmove(buf + 4, iov[1].iov_base, buf_data_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09008605 *(__be32 *)buf = cpu_to_be32(buf_data_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09008606
8607 return rc;
8608}
8609
8610bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work)
8611{
8612 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09008613 struct smb2_hdr *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008614
8615 if (conn->dialect < SMB30_PROT_ID)
8616 return false;
8617
8618 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008619 rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008620
8621 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008622 rsp->Status == STATUS_SUCCESS)
Namjae Jeone2f34482021-03-16 10:49:09 +09008623 return true;
8624 return false;
8625}