blob: 63bf185530c2ee10c142baf00788e44af54f12cc [file] [log] [blame]
Namjae Jeone2f34482021-03-16 10:49:09 +09001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2016 Namjae Jeon <linkinjeon@kernel.org>
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd.
5 */
6
7#include <linux/inetdevice.h>
8#include <net/addrconf.h>
9#include <linux/syscalls.h>
10#include <linux/namei.h>
11#include <linux/statfs.h>
12#include <linux/ethtool.h>
Namjae Jeone8c06192021-06-22 11:06:11 +090013#include <linux/falloc.h>
Namjae Jeone2f34482021-03-16 10:49:09 +090014
15#include "glob.h"
16#include "smb2pdu.h"
17#include "smbfsctl.h"
18#include "oplock.h"
19#include "smbacl.h"
20
21#include "auth.h"
22#include "asn1.h"
Namjae Jeone2f34482021-03-16 10:49:09 +090023#include "connection.h"
24#include "transport_ipc.h"
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +090025#include "transport_rdma.h"
Namjae Jeone2f34482021-03-16 10:49:09 +090026#include "vfs.h"
27#include "vfs_cache.h"
28#include "misc.h"
29
Namjae Jeone2f34482021-03-16 10:49:09 +090030#include "server.h"
31#include "smb_common.h"
32#include "smbstatus.h"
33#include "ksmbd_work.h"
34#include "mgmt/user_config.h"
35#include "mgmt/share_config.h"
36#include "mgmt/tree_connect.h"
37#include "mgmt/user_session.h"
38#include "mgmt/ksmbd_ida.h"
39#include "ndr.h"
40
41static void __wbuf(struct ksmbd_work *work, void **req, void **rsp)
42{
43 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +090044 *req = ksmbd_req_buf_next(work);
45 *rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +090046 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +090047 *req = smb2_get_msg(work->request_buf);
48 *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +090049 }
50}
51
52#define WORK_BUFFERS(w, rq, rs) __wbuf((w), (void **)&(rq), (void **)&(rs))
53
54/**
55 * check_session_id() - check for valid session id in smb header
56 * @conn: connection instance
57 * @id: session id from smb header
58 *
59 * Return: 1 if valid session id, otherwise 0
60 */
Namjae Jeonf4228b62021-08-12 10:16:40 +090061static inline bool check_session_id(struct ksmbd_conn *conn, u64 id)
Namjae Jeone2f34482021-03-16 10:49:09 +090062{
63 struct ksmbd_session *sess;
64
65 if (id == 0 || id == -1)
Namjae Jeonf4228b62021-08-12 10:16:40 +090066 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +090067
Namjae Jeonf5a544e2021-06-18 10:04:19 +090068 sess = ksmbd_session_lookup_all(conn, id);
Namjae Jeone2f34482021-03-16 10:49:09 +090069 if (sess)
Namjae Jeonf4228b62021-08-12 10:16:40 +090070 return true;
Namjae Jeonbde16942021-06-28 15:23:19 +090071 pr_err("Invalid user session id: %llu\n", id);
Namjae Jeonf4228b62021-08-12 10:16:40 +090072 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +090073}
74
Namjae Jeonf5a544e2021-06-18 10:04:19 +090075struct channel *lookup_chann_list(struct ksmbd_session *sess, struct ksmbd_conn *conn)
Namjae Jeone2f34482021-03-16 10:49:09 +090076{
77 struct channel *chann;
Namjae Jeone2f34482021-03-16 10:49:09 +090078
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +090079 list_for_each_entry(chann, &sess->ksmbd_chann_list, chann_list) {
Namjae Jeon560ac052021-06-22 16:16:45 +090080 if (chann->conn == conn)
Namjae Jeone2f34482021-03-16 10:49:09 +090081 return chann;
82 }
83
84 return NULL;
85}
86
87/**
Namjae Jeon5ec3df8e2021-08-12 10:17:39 +090088 * smb2_get_ksmbd_tcon() - get tree connection information using a tree id.
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +090089 * @work: smb work
Namjae Jeone2f34482021-03-16 10:49:09 +090090 *
Namjae Jeon5ec3df8e2021-08-12 10:17:39 +090091 * Return: 0 if there is a tree connection matched or these are
92 * skipable commands, otherwise error
Namjae Jeone2f34482021-03-16 10:49:09 +090093 */
94int smb2_get_ksmbd_tcon(struct ksmbd_work *work)
95{
Namjae Jeoncb451722021-11-03 08:08:44 +090096 struct smb2_hdr *req_hdr = smb2_get_msg(work->request_buf);
Ralph Boehme341b1602021-10-05 07:03:40 +020097 unsigned int cmd = le16_to_cpu(req_hdr->Command);
Namjae Jeone2f34482021-03-16 10:49:09 +090098 int tree_id;
99
100 work->tcon = NULL;
Ralph Boehme341b1602021-10-05 07:03:40 +0200101 if (cmd == SMB2_TREE_CONNECT_HE ||
102 cmd == SMB2_CANCEL_HE ||
103 cmd == SMB2_LOGOFF_HE) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900104 ksmbd_debug(SMB, "skip to check tree connect request\n");
105 return 0;
106 }
107
Namjae Jeon02b68b22021-04-01 17:45:33 +0900108 if (xa_empty(&work->sess->tree_conns)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900109 ksmbd_debug(SMB, "NO tree connected\n");
Namjae Jeonc6ce2b52021-08-12 10:18:18 +0900110 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +0900111 }
112
113 tree_id = le32_to_cpu(req_hdr->Id.SyncId.TreeId);
114 work->tcon = ksmbd_tree_conn_lookup(work->sess, tree_id);
115 if (!work->tcon) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900116 pr_err("Invalid tid %d\n", tree_id);
Namjae Jeonc6ce2b52021-08-12 10:18:18 +0900117 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +0900118 }
119
120 return 1;
121}
122
123/**
124 * smb2_set_err_rsp() - set error response code on smb response
125 * @work: smb work containing response buffer
126 */
127void smb2_set_err_rsp(struct ksmbd_work *work)
128{
129 struct smb2_err_rsp *err_rsp;
130
131 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +0900132 err_rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900133 else
Namjae Jeoncb451722021-11-03 08:08:44 +0900134 err_rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900135
136 if (err_rsp->hdr.Status != STATUS_STOPPED_ON_SYMLINK) {
137 err_rsp->StructureSize = SMB2_ERROR_STRUCTURE_SIZE2_LE;
138 err_rsp->ErrorContextCount = 0;
139 err_rsp->Reserved = 0;
140 err_rsp->ByteCount = 0;
141 err_rsp->ErrorData[0] = 0;
Namjae Jeone5066492021-03-30 12:35:23 +0900142 inc_rfc1001_len(work->response_buf, SMB2_ERROR_STRUCTURE_SIZE2);
Namjae Jeone2f34482021-03-16 10:49:09 +0900143 }
144}
145
146/**
147 * is_smb2_neg_cmd() - is it smb2 negotiation command
148 * @work: smb work containing smb header
149 *
Namjae Jeonf4228b62021-08-12 10:16:40 +0900150 * Return: true if smb2 negotiation command, otherwise false
Namjae Jeone2f34482021-03-16 10:49:09 +0900151 */
Namjae Jeonf4228b62021-08-12 10:16:40 +0900152bool is_smb2_neg_cmd(struct ksmbd_work *work)
Namjae Jeone2f34482021-03-16 10:49:09 +0900153{
Namjae Jeoncb451722021-11-03 08:08:44 +0900154 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900155
156 /* is it SMB2 header ? */
157 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
Namjae Jeonf4228b62021-08-12 10:16:40 +0900158 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900159
160 /* make sure it is request not response message */
161 if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
Namjae Jeonf4228b62021-08-12 10:16:40 +0900162 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900163
164 if (hdr->Command != SMB2_NEGOTIATE)
Namjae Jeonf4228b62021-08-12 10:16:40 +0900165 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900166
Namjae Jeonf4228b62021-08-12 10:16:40 +0900167 return true;
Namjae Jeone2f34482021-03-16 10:49:09 +0900168}
169
170/**
171 * is_smb2_rsp() - is it smb2 response
172 * @work: smb work containing smb response buffer
173 *
Namjae Jeonf4228b62021-08-12 10:16:40 +0900174 * Return: true if smb2 response, otherwise false
Namjae Jeone2f34482021-03-16 10:49:09 +0900175 */
Namjae Jeonf4228b62021-08-12 10:16:40 +0900176bool is_smb2_rsp(struct ksmbd_work *work)
Namjae Jeone2f34482021-03-16 10:49:09 +0900177{
Namjae Jeoncb451722021-11-03 08:08:44 +0900178 struct smb2_hdr *hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900179
180 /* is it SMB2 header ? */
181 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
Namjae Jeonf4228b62021-08-12 10:16:40 +0900182 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900183
184 /* make sure it is response not request message */
185 if (!(hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR))
Namjae Jeonf4228b62021-08-12 10:16:40 +0900186 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900187
Namjae Jeonf4228b62021-08-12 10:16:40 +0900188 return true;
Namjae Jeone2f34482021-03-16 10:49:09 +0900189}
190
191/**
192 * get_smb2_cmd_val() - get smb command code from smb header
193 * @work: smb work containing smb request buffer
194 *
195 * Return: smb2 request command value
196 */
Namjae Jeonfc2d1b52021-05-26 18:01:08 +0900197u16 get_smb2_cmd_val(struct ksmbd_work *work)
Namjae Jeone2f34482021-03-16 10:49:09 +0900198{
199 struct smb2_hdr *rcv_hdr;
200
201 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +0900202 rcv_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900203 else
Namjae Jeoncb451722021-11-03 08:08:44 +0900204 rcv_hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900205 return le16_to_cpu(rcv_hdr->Command);
206}
207
208/**
209 * set_smb2_rsp_status() - set error response code on smb2 header
210 * @work: smb work containing response buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900211 * @err: error response code
Namjae Jeone2f34482021-03-16 10:49:09 +0900212 */
213void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err)
214{
215 struct smb2_hdr *rsp_hdr;
216
217 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +0900218 rsp_hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900219 else
Namjae Jeoncb451722021-11-03 08:08:44 +0900220 rsp_hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900221 rsp_hdr->Status = err;
222 smb2_set_err_rsp(work);
223}
224
225/**
226 * init_smb2_neg_rsp() - initialize smb2 response for negotiate command
227 * @work: smb work containing smb request buffer
228 *
229 * smb2 negotiate response is sent in reply of smb1 negotiate command for
230 * dialect auto-negotiation.
231 */
232int init_smb2_neg_rsp(struct ksmbd_work *work)
233{
234 struct smb2_hdr *rsp_hdr;
235 struct smb2_negotiate_rsp *rsp;
236 struct ksmbd_conn *conn = work->conn;
237
238 if (conn->need_neg == false)
239 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +0900240
Namjae Jeoncb451722021-11-03 08:08:44 +0900241 *(__be32 *)work->response_buf =
242 cpu_to_be32(conn->vals->header_size);
Namjae Jeone2f34482021-03-16 10:49:09 +0900243
Namjae Jeoncb451722021-11-03 08:08:44 +0900244 rsp_hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900245 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
Namjae Jeone2f34482021-03-16 10:49:09 +0900246 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
247 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
248 rsp_hdr->CreditRequest = cpu_to_le16(2);
249 rsp_hdr->Command = SMB2_NEGOTIATE;
250 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
251 rsp_hdr->NextCommand = 0;
252 rsp_hdr->MessageId = 0;
253 rsp_hdr->Id.SyncId.ProcessId = 0;
254 rsp_hdr->Id.SyncId.TreeId = 0;
255 rsp_hdr->SessionId = 0;
256 memset(rsp_hdr->Signature, 0, 16);
257
Namjae Jeoncb451722021-11-03 08:08:44 +0900258 rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900259
260 WARN_ON(ksmbd_conn_good(work));
261
262 rsp->StructureSize = cpu_to_le16(65);
263 ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect);
264 rsp->DialectRevision = cpu_to_le16(conn->dialect);
265 /* Not setting conn guid rsp->ServerGUID, as it
266 * not used by client for identifying connection
267 */
268 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
269 /* Default Max Message Size till SMB2.0, 64K*/
270 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
271 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
272 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
273
274 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
275 rsp->ServerStartTime = 0;
276
277 rsp->SecurityBufferOffset = cpu_to_le16(128);
278 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
Namjae Jeoncb451722021-11-03 08:08:44 +0900279 ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) +
Namjae Jeone2f34482021-03-16 10:49:09 +0900280 le16_to_cpu(rsp->SecurityBufferOffset));
Namjae Jeoncb451722021-11-03 08:08:44 +0900281 inc_rfc1001_len(work->response_buf,
282 sizeof(struct smb2_negotiate_rsp) -
283 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
284 AUTH_GSS_LENGTH);
Namjae Jeone2f34482021-03-16 10:49:09 +0900285 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
286 if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY)
287 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
288 conn->use_spnego = true;
289
290 ksmbd_conn_set_need_negotiate(work);
291 return 0;
292}
293
Namjae Jeone2f34482021-03-16 10:49:09 +0900294/**
295 * smb2_set_rsp_credits() - set number of credits in response buffer
296 * @work: smb work containing smb response buffer
297 */
298int smb2_set_rsp_credits(struct ksmbd_work *work)
299{
Namjae Jeon8a893312021-06-25 13:43:37 +0900300 struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work);
301 struct smb2_hdr *hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900302 struct ksmbd_conn *conn = work->conn;
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900303 unsigned short credits_requested;
304 unsigned short credit_charge, credits_granted = 0;
305 unsigned short aux_max, aux_credits;
Namjae Jeone2f34482021-03-16 10:49:09 +0900306
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900307 if (work->send_no_response)
308 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +0900309
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900310 hdr->CreditCharge = req_hdr->CreditCharge;
Namjae Jeone2f34482021-03-16 10:49:09 +0900311
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900312 if (conn->total_credits > conn->max_credits) {
313 hdr->CreditRequest = 0;
Namjae Jeonbde16942021-06-28 15:23:19 +0900314 pr_err("Total credits overflow: %d\n", conn->total_credits);
Namjae Jeone2f34482021-03-16 10:49:09 +0900315 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +0900316 }
317
Hyunchul Leebf8acc92021-10-07 16:26:58 +0900318 credit_charge = max_t(unsigned short,
319 le16_to_cpu(req_hdr->CreditCharge), 1);
320 credits_requested = max_t(unsigned short,
321 le16_to_cpu(req_hdr->CreditRequest), 1);
322
323 /* according to smb2.credits smbtorture, Windows server
324 * 2016 or later grant up to 8192 credits at once.
325 *
326 * TODO: Need to adjuct CreditRequest value according to
327 * current cpu load
328 */
329 aux_credits = credits_requested - 1;
330 if (hdr->Command == SMB2_NEGOTIATE)
331 aux_max = 0;
332 else
333 aux_max = conn->max_credits - credit_charge;
334 aux_credits = min_t(unsigned short, aux_credits, aux_max);
335 credits_granted = credit_charge + aux_credits;
336
337 if (conn->max_credits - conn->total_credits < credits_granted)
338 credits_granted = conn->max_credits -
339 conn->total_credits;
340
Namjae Jeone2f34482021-03-16 10:49:09 +0900341 conn->total_credits += credits_granted;
342 work->credits_granted += credits_granted;
343
344 if (!req_hdr->NextCommand) {
345 /* Update CreditRequest in last request */
346 hdr->CreditRequest = cpu_to_le16(work->credits_granted);
347 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900348 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900349 "credits: requested[%d] granted[%d] total_granted[%d]\n",
350 credits_requested, credits_granted,
351 conn->total_credits);
Namjae Jeone2f34482021-03-16 10:49:09 +0900352 return 0;
353}
354
355/**
356 * init_chained_smb2_rsp() - initialize smb2 chained response
357 * @work: smb work containing smb response buffer
358 */
359static void init_chained_smb2_rsp(struct ksmbd_work *work)
360{
Namjae Jeon8a893312021-06-25 13:43:37 +0900361 struct smb2_hdr *req = ksmbd_req_buf_next(work);
362 struct smb2_hdr *rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900363 struct smb2_hdr *rsp_hdr;
364 struct smb2_hdr *rcv_hdr;
365 int next_hdr_offset = 0;
366 int len, new_len;
367
368 /* Len of this response = updated RFC len - offset of previous cmd
369 * in the compound rsp
370 */
371
372 /* Storing the current local FID which may be needed by subsequent
373 * command in the compound request
374 */
375 if (req->Command == SMB2_CREATE && rsp->Status == STATUS_SUCCESS) {
376 work->compound_fid =
377 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
378 VolatileFileId);
379 work->compound_pfid =
380 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
381 PersistentFileId);
382 work->compound_sid = le64_to_cpu(rsp->SessionId);
383 }
384
Namjae Jeone5066492021-03-30 12:35:23 +0900385 len = get_rfc1002_len(work->response_buf) - work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +0900386 next_hdr_offset = le32_to_cpu(req->NextCommand);
387
388 new_len = ALIGN(len, 8);
Namjae Jeoncb451722021-11-03 08:08:44 +0900389 inc_rfc1001_len(work->response_buf,
390 sizeof(struct smb2_hdr) + new_len - len);
Namjae Jeone2f34482021-03-16 10:49:09 +0900391 rsp->NextCommand = cpu_to_le32(new_len);
392
393 work->next_smb2_rcv_hdr_off += next_hdr_offset;
394 work->next_smb2_rsp_hdr_off += new_len;
395 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900396 "Compound req new_len = %d rcv off = %d rsp off = %d\n",
397 new_len, work->next_smb2_rcv_hdr_off,
398 work->next_smb2_rsp_hdr_off);
Namjae Jeone2f34482021-03-16 10:49:09 +0900399
Namjae Jeon8a893312021-06-25 13:43:37 +0900400 rsp_hdr = ksmbd_resp_buf_next(work);
401 rcv_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900402
403 if (!(rcv_hdr->Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
404 ksmbd_debug(SMB, "related flag should be set\n");
405 work->compound_fid = KSMBD_NO_FID;
406 work->compound_pfid = KSMBD_NO_FID;
407 }
Namjae Jeoncb451722021-11-03 08:08:44 +0900408 memset((char *)rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
Namjae Jeon18a015b2021-09-22 21:00:57 +0900409 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
Namjae Jeone2f34482021-03-16 10:49:09 +0900410 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
411 rsp_hdr->Command = rcv_hdr->Command;
412
413 /*
414 * Message is response. We don't grant oplock yet.
415 */
416 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR |
417 SMB2_FLAGS_RELATED_OPERATIONS);
418 rsp_hdr->NextCommand = 0;
419 rsp_hdr->MessageId = rcv_hdr->MessageId;
420 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
421 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
422 rsp_hdr->SessionId = rcv_hdr->SessionId;
423 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
424}
425
426/**
427 * is_chained_smb2_message() - check for chained command
428 * @work: smb work containing smb request buffer
429 *
430 * Return: true if chained request, otherwise false
431 */
432bool is_chained_smb2_message(struct ksmbd_work *work)
433{
Namjae Jeoncb451722021-11-03 08:08:44 +0900434 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
Namjae Jeond72a9c12021-09-24 09:24:08 +0900435 unsigned int len, next_cmd;
Namjae Jeone2f34482021-03-16 10:49:09 +0900436
437 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
438 return false;
439
Namjae Jeon8a893312021-06-25 13:43:37 +0900440 hdr = ksmbd_req_buf_next(work);
Namjae Jeond72a9c12021-09-24 09:24:08 +0900441 next_cmd = le32_to_cpu(hdr->NextCommand);
442 if (next_cmd > 0) {
443 if ((u64)work->next_smb2_rcv_hdr_off + next_cmd +
444 __SMB2_HEADER_STRUCTURE_SIZE >
445 get_rfc1002_len(work->request_buf)) {
446 pr_err("next command(%u) offset exceeds smb msg size\n",
447 next_cmd);
448 return false;
449 }
450
Namjae Jeondbad6302021-10-11 19:15:25 +0900451 if ((u64)get_rfc1002_len(work->response_buf) + MAX_CIFS_SMALL_BUFFER_SIZE >
452 work->response_sz) {
453 pr_err("next response offset exceeds response buffer size\n");
454 return false;
455 }
456
Namjae Jeone2f34482021-03-16 10:49:09 +0900457 ksmbd_debug(SMB, "got SMB2 chained command\n");
458 init_chained_smb2_rsp(work);
459 return true;
460 } else if (work->next_smb2_rcv_hdr_off) {
461 /*
462 * This is last request in chained command,
463 * align response to 8 byte
464 */
Namjae Jeone5066492021-03-30 12:35:23 +0900465 len = ALIGN(get_rfc1002_len(work->response_buf), 8);
466 len = len - get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900467 if (len) {
468 ksmbd_debug(SMB, "padding len %u\n", len);
Namjae Jeone5066492021-03-30 12:35:23 +0900469 inc_rfc1001_len(work->response_buf, len);
470 if (work->aux_payload_sz)
Namjae Jeone2f34482021-03-16 10:49:09 +0900471 work->aux_payload_sz += len;
472 }
473 }
474 return false;
475}
476
477/**
478 * init_smb2_rsp_hdr() - initialize smb2 response
479 * @work: smb work containing smb request buffer
480 *
481 * Return: 0
482 */
483int init_smb2_rsp_hdr(struct ksmbd_work *work)
484{
Namjae Jeoncb451722021-11-03 08:08:44 +0900485 struct smb2_hdr *rsp_hdr = smb2_get_msg(work->response_buf);
486 struct smb2_hdr *rcv_hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900487 struct ksmbd_conn *conn = work->conn;
488
489 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
Namjae Jeoncb451722021-11-03 08:08:44 +0900490 *(__be32 *)work->response_buf =
491 cpu_to_be32(conn->vals->header_size);
Namjae Jeone2f34482021-03-16 10:49:09 +0900492 rsp_hdr->ProtocolId = rcv_hdr->ProtocolId;
493 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
494 rsp_hdr->Command = rcv_hdr->Command;
495
496 /*
497 * Message is response. We don't grant oplock yet.
498 */
499 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
500 rsp_hdr->NextCommand = 0;
501 rsp_hdr->MessageId = rcv_hdr->MessageId;
502 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
503 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
504 rsp_hdr->SessionId = rcv_hdr->SessionId;
505 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
506
507 work->syncronous = true;
508 if (work->async_id) {
Namjae Jeond40012a2021-04-13 13:06:30 +0900509 ksmbd_release_id(&conn->async_ida, work->async_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900510 work->async_id = 0;
511 }
512
513 return 0;
514}
515
516/**
517 * smb2_allocate_rsp_buf() - allocate smb2 response buffer
518 * @work: smb work containing smb request buffer
519 *
520 * Return: 0 on success, otherwise -ENOMEM
521 */
522int smb2_allocate_rsp_buf(struct ksmbd_work *work)
523{
Namjae Jeoncb451722021-11-03 08:08:44 +0900524 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900525 size_t small_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
Namjae Jeon4bc59472021-10-15 17:14:02 +0900526 size_t large_sz = small_sz + work->conn->vals->max_trans_size;
Namjae Jeone2f34482021-03-16 10:49:09 +0900527 size_t sz = small_sz;
528 int cmd = le16_to_cpu(hdr->Command);
529
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900530 if (cmd == SMB2_IOCTL_HE || cmd == SMB2_QUERY_DIRECTORY_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900531 sz = large_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +0900532
533 if (cmd == SMB2_QUERY_INFO_HE) {
534 struct smb2_query_info_req *req;
535
Namjae Jeoncb451722021-11-03 08:08:44 +0900536 req = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900537 if (req->InfoType == SMB2_O_INFO_FILE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +0900538 (req->FileInfoClass == FILE_FULL_EA_INFORMATION ||
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900539 req->FileInfoClass == FILE_ALL_INFORMATION))
Namjae Jeone2f34482021-03-16 10:49:09 +0900540 sz = large_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +0900541 }
542
543 /* allocate large response buf for chained commands */
544 if (le32_to_cpu(hdr->NextCommand) > 0)
545 sz = large_sz;
546
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900547 work->response_buf = kvmalloc(sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeon63c454f2021-04-20 14:24:28 +0900548 if (!work->response_buf)
Namjae Jeone2f34482021-03-16 10:49:09 +0900549 return -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +0900550
551 work->response_sz = sz;
552 return 0;
553}
554
555/**
556 * smb2_check_user_session() - check for valid session for a user
557 * @work: smb work containing smb request buffer
558 *
559 * Return: 0 on success, otherwise error
560 */
561int smb2_check_user_session(struct ksmbd_work *work)
562{
Namjae Jeoncb451722021-11-03 08:08:44 +0900563 struct smb2_hdr *req_hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900564 struct ksmbd_conn *conn = work->conn;
565 unsigned int cmd = conn->ops->get_cmd_val(work);
566 unsigned long long sess_id;
567
568 work->sess = NULL;
569 /*
570 * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not
571 * require a session id, so no need to validate user session's for
572 * these commands.
573 */
574 if (cmd == SMB2_ECHO_HE || cmd == SMB2_NEGOTIATE_HE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +0900575 cmd == SMB2_SESSION_SETUP_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900576 return 0;
577
578 if (!ksmbd_conn_good(work))
579 return -EINVAL;
580
581 sess_id = le64_to_cpu(req_hdr->SessionId);
582 /* Check for validity of user session */
Namjae Jeonf5a544e2021-06-18 10:04:19 +0900583 work->sess = ksmbd_session_lookup_all(conn, sess_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900584 if (work->sess)
585 return 1;
586 ksmbd_debug(SMB, "Invalid user session, Uid %llu\n", sess_id);
587 return -EINVAL;
588}
589
Namjae Jeon64b39f42021-03-30 14:25:35 +0900590static void destroy_previous_session(struct ksmbd_user *user, u64 id)
Namjae Jeone2f34482021-03-16 10:49:09 +0900591{
592 struct ksmbd_session *prev_sess = ksmbd_session_lookup_slowpath(id);
593 struct ksmbd_user *prev_user;
594
595 if (!prev_sess)
596 return;
597
598 prev_user = prev_sess->user;
599
Marios Makassikis1fca8032021-05-06 11:41:54 +0900600 if (!prev_user ||
601 strcmp(user->name, prev_user->name) ||
Namjae Jeone2f34482021-03-16 10:49:09 +0900602 user->passkey_sz != prev_user->passkey_sz ||
603 memcmp(user->passkey, prev_user->passkey, user->passkey_sz)) {
604 put_session(prev_sess);
605 return;
606 }
607
608 put_session(prev_sess);
609 ksmbd_session_destroy(prev_sess);
610}
611
612/**
613 * smb2_get_name() - get filename string from on the wire smb format
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900614 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +0900615 * @src: source buffer
616 * @maxlen: maxlen of source string
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900617 * @nls_table: nls_table pointer
Namjae Jeone2f34482021-03-16 10:49:09 +0900618 *
619 * Return: matching converted filename on success, otherwise error ptr
620 */
621static char *
Namjae Jeon64b39f42021-03-30 14:25:35 +0900622smb2_get_name(struct ksmbd_share_config *share, const char *src,
Namjae Jeon070fb212021-05-26 17:57:12 +0900623 const int maxlen, struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +0900624{
Hyunchul Lee265fd192021-09-25 00:06:16 +0900625 char *name;
Namjae Jeone2f34482021-03-16 10:49:09 +0900626
Namjae Jeon64b39f42021-03-30 14:25:35 +0900627 name = smb_strndup_from_utf16(src, maxlen, 1, local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +0900628 if (IS_ERR(name)) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900629 pr_err("failed to get name %ld\n", PTR_ERR(name));
Namjae Jeone2f34482021-03-16 10:49:09 +0900630 return name;
631 }
632
Hyunchul Lee265fd192021-09-25 00:06:16 +0900633 ksmbd_conv_path_to_unix(name);
634 ksmbd_strip_last_slash(name);
635 return name;
Namjae Jeone2f34482021-03-16 10:49:09 +0900636}
637
Namjae Jeone2f34482021-03-16 10:49:09 +0900638int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg)
639{
640 struct smb2_hdr *rsp_hdr;
641 struct ksmbd_conn *conn = work->conn;
642 int id;
643
Namjae Jeoncb451722021-11-03 08:08:44 +0900644 rsp_hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900645 rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND;
646
Namjae Jeond40012a2021-04-13 13:06:30 +0900647 id = ksmbd_acquire_async_msg_id(&conn->async_ida);
Namjae Jeone2f34482021-03-16 10:49:09 +0900648 if (id < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900649 pr_err("Failed to alloc async message id\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900650 return id;
651 }
652 work->syncronous = false;
653 work->async_id = id;
654 rsp_hdr->Id.AsyncId = cpu_to_le64(id);
655
656 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900657 "Send interim Response to inform async request id : %d\n",
658 work->async_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900659
660 work->cancel_fn = fn;
661 work->cancel_argv = arg;
662
Namjae Jeon6c4e6752021-06-07 09:08:45 +0900663 if (list_empty(&work->async_request_entry)) {
664 spin_lock(&conn->request_lock);
665 list_add_tail(&work->async_request_entry, &conn->async_requests);
666 spin_unlock(&conn->request_lock);
667 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900668
669 return 0;
670}
671
672void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
673{
674 struct smb2_hdr *rsp_hdr;
675
Namjae Jeoncb451722021-11-03 08:08:44 +0900676 rsp_hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900677 smb2_set_err_rsp(work);
678 rsp_hdr->Status = status;
679
680 work->multiRsp = 1;
681 ksmbd_conn_write(work);
682 rsp_hdr->Status = 0;
683 work->multiRsp = 0;
684}
685
686static __le32 smb2_get_reparse_tag_special_file(umode_t mode)
687{
688 if (S_ISDIR(mode) || S_ISREG(mode))
689 return 0;
690
691 if (S_ISLNK(mode))
692 return IO_REPARSE_TAG_LX_SYMLINK_LE;
693 else if (S_ISFIFO(mode))
694 return IO_REPARSE_TAG_LX_FIFO_LE;
695 else if (S_ISSOCK(mode))
696 return IO_REPARSE_TAG_AF_UNIX_LE;
697 else if (S_ISCHR(mode))
698 return IO_REPARSE_TAG_LX_CHR_LE;
699 else if (S_ISBLK(mode))
700 return IO_REPARSE_TAG_LX_BLK_LE;
701
702 return 0;
703}
704
705/**
706 * smb2_get_dos_mode() - get file mode in dos format from unix mode
707 * @stat: kstat containing file mode
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900708 * @attribute: attribute flags
Namjae Jeone2f34482021-03-16 10:49:09 +0900709 *
710 * Return: converted dos mode
711 */
712static int smb2_get_dos_mode(struct kstat *stat, int attribute)
713{
714 int attr = 0;
715
Namjae Jeon64b39f42021-03-30 14:25:35 +0900716 if (S_ISDIR(stat->mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900717 attr = ATTR_DIRECTORY |
718 (attribute & (ATTR_HIDDEN | ATTR_SYSTEM));
Namjae Jeon64b39f42021-03-30 14:25:35 +0900719 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +0900720 attr = (attribute & 0x00005137) | ATTR_ARCHIVE;
721 attr &= ~(ATTR_DIRECTORY);
722 if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps &
723 FILE_SUPPORTS_SPARSE_FILES))
724 attr |= ATTR_SPARSE;
725
726 if (smb2_get_reparse_tag_special_file(stat->mode))
727 attr |= ATTR_REPARSE;
728 }
729
730 return attr;
731}
732
733static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900734 __le16 hash_id)
Namjae Jeone2f34482021-03-16 10:49:09 +0900735{
736 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
737 pneg_ctxt->DataLength = cpu_to_le16(38);
738 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
739 pneg_ctxt->Reserved = cpu_to_le32(0);
740 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
741 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
742 pneg_ctxt->HashAlgorithms = hash_id;
743}
744
745static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900746 __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +0900747{
748 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
749 pneg_ctxt->DataLength = cpu_to_le16(4);
750 pneg_ctxt->Reserved = cpu_to_le32(0);
751 pneg_ctxt->CipherCount = cpu_to_le16(1);
752 pneg_ctxt->Ciphers[0] = cipher_type;
753}
754
755static void build_compression_ctxt(struct smb2_compression_ctx *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900756 __le16 comp_algo)
Namjae Jeone2f34482021-03-16 10:49:09 +0900757{
758 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
759 pneg_ctxt->DataLength =
760 cpu_to_le16(sizeof(struct smb2_compression_ctx)
761 - sizeof(struct smb2_neg_context));
762 pneg_ctxt->Reserved = cpu_to_le32(0);
763 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(1);
764 pneg_ctxt->Reserved1 = cpu_to_le32(0);
765 pneg_ctxt->CompressionAlgorithms[0] = comp_algo;
766}
767
Namjae Jeon378087c2021-07-21 10:05:53 +0900768static void build_sign_cap_ctxt(struct smb2_signing_capabilities *pneg_ctxt,
769 __le16 sign_algo)
770{
771 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
772 pneg_ctxt->DataLength =
773 cpu_to_le16((sizeof(struct smb2_signing_capabilities) + 2)
774 - sizeof(struct smb2_neg_context));
775 pneg_ctxt->Reserved = cpu_to_le32(0);
776 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(1);
777 pneg_ctxt->SigningAlgorithms[0] = sign_algo;
778}
779
Namjae Jeon64b39f42021-03-30 14:25:35 +0900780static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900781{
782 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
783 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
784 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
785 pneg_ctxt->Name[0] = 0x93;
786 pneg_ctxt->Name[1] = 0xAD;
787 pneg_ctxt->Name[2] = 0x25;
788 pneg_ctxt->Name[3] = 0x50;
789 pneg_ctxt->Name[4] = 0x9C;
790 pneg_ctxt->Name[5] = 0xB4;
791 pneg_ctxt->Name[6] = 0x11;
792 pneg_ctxt->Name[7] = 0xE7;
793 pneg_ctxt->Name[8] = 0xB4;
794 pneg_ctxt->Name[9] = 0x23;
795 pneg_ctxt->Name[10] = 0x83;
796 pneg_ctxt->Name[11] = 0xDE;
797 pneg_ctxt->Name[12] = 0x96;
798 pneg_ctxt->Name[13] = 0x8B;
799 pneg_ctxt->Name[14] = 0xCD;
800 pneg_ctxt->Name[15] = 0x7C;
801}
802
Namjae Jeon64b39f42021-03-30 14:25:35 +0900803static void assemble_neg_contexts(struct ksmbd_conn *conn,
Namjae Jeoncb451722021-11-03 08:08:44 +0900804 struct smb2_negotiate_rsp *rsp,
805 void *smb2_buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +0900806{
Namjae Jeone2f34482021-03-16 10:49:09 +0900807 char *pneg_ctxt = (char *)rsp +
Namjae Jeoncb451722021-11-03 08:08:44 +0900808 le32_to_cpu(rsp->NegotiateContextOffset);
Namjae Jeone2f34482021-03-16 10:49:09 +0900809 int neg_ctxt_cnt = 1;
810 int ctxt_size;
811
812 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900813 "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900814 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900815 conn->preauth_info->Preauth_HashId);
Namjae Jeone2f34482021-03-16 10:49:09 +0900816 rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt);
Namjae Jeoncb451722021-11-03 08:08:44 +0900817 inc_rfc1001_len(smb2_buf_len, AUTH_GSS_PADDING);
Namjae Jeone2f34482021-03-16 10:49:09 +0900818 ctxt_size = sizeof(struct smb2_preauth_neg_context);
819 /* Round to 8 byte boundary */
820 pneg_ctxt += round_up(sizeof(struct smb2_preauth_neg_context), 8);
821
822 if (conn->cipher_type) {
823 ctxt_size = round_up(ctxt_size, 8);
824 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900825 "assemble SMB2_ENCRYPTION_CAPABILITIES context\n");
Namjae Jeon64b39f42021-03-30 14:25:35 +0900826 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900827 conn->cipher_type);
Namjae Jeone2f34482021-03-16 10:49:09 +0900828 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900829 ctxt_size += sizeof(struct smb2_encryption_neg_context) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900830 /* Round to 8 byte boundary */
831 pneg_ctxt +=
Namjae Jeonaf320a72021-07-21 10:03:19 +0900832 round_up(sizeof(struct smb2_encryption_neg_context) + 2,
Namjae Jeone2f34482021-03-16 10:49:09 +0900833 8);
834 }
835
836 if (conn->compress_algorithm) {
837 ctxt_size = round_up(ctxt_size, 8);
838 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900839 "assemble SMB2_COMPRESSION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900840 /* Temporarily set to SMB3_COMPRESS_NONE */
841 build_compression_ctxt((struct smb2_compression_ctx *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900842 conn->compress_algorithm);
Namjae Jeone2f34482021-03-16 10:49:09 +0900843 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900844 ctxt_size += sizeof(struct smb2_compression_ctx) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900845 /* Round to 8 byte boundary */
Namjae Jeonaf320a72021-07-21 10:03:19 +0900846 pneg_ctxt += round_up(sizeof(struct smb2_compression_ctx) + 2,
847 8);
Namjae Jeone2f34482021-03-16 10:49:09 +0900848 }
849
850 if (conn->posix_ext_supported) {
851 ctxt_size = round_up(ctxt_size, 8);
852 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900853 "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900854 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
855 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
856 ctxt_size += sizeof(struct smb2_posix_neg_context);
Namjae Jeon378087c2021-07-21 10:05:53 +0900857 /* Round to 8 byte boundary */
858 pneg_ctxt += round_up(sizeof(struct smb2_posix_neg_context), 8);
859 }
860
861 if (conn->signing_negotiated) {
862 ctxt_size = round_up(ctxt_size, 8);
863 ksmbd_debug(SMB,
864 "assemble SMB2_SIGNING_CAPABILITIES context\n");
865 build_sign_cap_ctxt((struct smb2_signing_capabilities *)pneg_ctxt,
866 conn->signing_algorithm);
867 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
868 ctxt_size += sizeof(struct smb2_signing_capabilities) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900869 }
870
Namjae Jeoncb451722021-11-03 08:08:44 +0900871 inc_rfc1001_len(smb2_buf_len, ctxt_size);
Namjae Jeone2f34482021-03-16 10:49:09 +0900872}
873
Namjae Jeon64b39f42021-03-30 14:25:35 +0900874static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900875 struct smb2_preauth_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900876{
877 __le32 err = STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP;
878
Namjae Jeon070fb212021-05-26 17:57:12 +0900879 if (pneg_ctxt->HashAlgorithms == SMB2_PREAUTH_INTEGRITY_SHA512) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900880 conn->preauth_info->Preauth_HashId =
881 SMB2_PREAUTH_INTEGRITY_SHA512;
882 err = STATUS_SUCCESS;
883 }
884
885 return err;
886}
887
Namjae Jeonaf320a72021-07-21 10:03:19 +0900888static void decode_encrypt_ctxt(struct ksmbd_conn *conn,
889 struct smb2_encryption_neg_context *pneg_ctxt,
890 int len_of_ctxts)
Namjae Jeone2f34482021-03-16 10:49:09 +0900891{
Namjae Jeone2f34482021-03-16 10:49:09 +0900892 int cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900893 int i, cphs_size = cph_cnt * sizeof(__le16);
Namjae Jeone2f34482021-03-16 10:49:09 +0900894
895 conn->cipher_type = 0;
896
Namjae Jeonaf320a72021-07-21 10:03:19 +0900897 if (sizeof(struct smb2_encryption_neg_context) + cphs_size >
898 len_of_ctxts) {
899 pr_err("Invalid cipher count(%d)\n", cph_cnt);
900 return;
901 }
902
Namjae Jeone2f34482021-03-16 10:49:09 +0900903 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION))
Namjae Jeonaf320a72021-07-21 10:03:19 +0900904 return;
Namjae Jeone2f34482021-03-16 10:49:09 +0900905
906 for (i = 0; i < cph_cnt; i++) {
907 if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM ||
Namjae Jeon5a0ca772021-05-06 11:43:37 +0900908 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM ||
909 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM ||
910 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900911 ksmbd_debug(SMB, "Cipher ID = 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +0900912 pneg_ctxt->Ciphers[i]);
Namjae Jeone2f34482021-03-16 10:49:09 +0900913 conn->cipher_type = pneg_ctxt->Ciphers[i];
914 break;
915 }
916 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900917}
918
Namjae Jeonaf320a72021-07-21 10:03:19 +0900919static void decode_compress_ctxt(struct ksmbd_conn *conn,
920 struct smb2_compression_ctx *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900921{
Namjae Jeone2f34482021-03-16 10:49:09 +0900922 conn->compress_algorithm = SMB3_COMPRESS_NONE;
Namjae Jeone2f34482021-03-16 10:49:09 +0900923}
924
Namjae Jeon378087c2021-07-21 10:05:53 +0900925static void decode_sign_cap_ctxt(struct ksmbd_conn *conn,
926 struct smb2_signing_capabilities *pneg_ctxt,
927 int len_of_ctxts)
928{
929 int sign_algo_cnt = le16_to_cpu(pneg_ctxt->SigningAlgorithmCount);
930 int i, sign_alos_size = sign_algo_cnt * sizeof(__le16);
931
932 conn->signing_negotiated = false;
933
934 if (sizeof(struct smb2_signing_capabilities) + sign_alos_size >
935 len_of_ctxts) {
936 pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt);
937 return;
938 }
939
940 for (i = 0; i < sign_algo_cnt; i++) {
941 if (pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_HMAC_SHA256 ||
942 pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_AES_CMAC) {
943 ksmbd_debug(SMB, "Signing Algorithm ID = 0x%x\n",
944 pneg_ctxt->SigningAlgorithms[i]);
945 conn->signing_negotiated = true;
946 conn->signing_algorithm =
947 pneg_ctxt->SigningAlgorithms[i];
948 break;
949 }
950 }
951}
952
Namjae Jeone2f34482021-03-16 10:49:09 +0900953static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn,
Namjae Jeoncb451722021-11-03 08:08:44 +0900954 struct smb2_negotiate_req *req,
955 int len_of_smb)
Namjae Jeone2f34482021-03-16 10:49:09 +0900956{
Namjae Jeone2f34482021-03-16 10:49:09 +0900957 /* +4 is to account for the RFC1001 len field */
Namjae Jeoncb451722021-11-03 08:08:44 +0900958 struct smb2_neg_context *pctx = (struct smb2_neg_context *)req;
Namjae Jeonaf320a72021-07-21 10:03:19 +0900959 int i = 0, len_of_ctxts;
960 int offset = le32_to_cpu(req->NegotiateContextOffset);
Namjae Jeone2f34482021-03-16 10:49:09 +0900961 int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900962 __le32 status = STATUS_INVALID_PARAMETER;
Namjae Jeone2f34482021-03-16 10:49:09 +0900963
Namjae Jeonaf320a72021-07-21 10:03:19 +0900964 ksmbd_debug(SMB, "decoding %d negotiate contexts\n", neg_ctxt_cnt);
965 if (len_of_smb <= offset) {
966 ksmbd_debug(SMB, "Invalid response: negotiate context offset\n");
967 return status;
968 }
969
970 len_of_ctxts = len_of_smb - offset;
971
Namjae Jeone2f34482021-03-16 10:49:09 +0900972 while (i++ < neg_ctxt_cnt) {
Namjae Jeonaf320a72021-07-21 10:03:19 +0900973 int clen;
974
975 /* check that offset is not beyond end of SMB */
976 if (len_of_ctxts == 0)
977 break;
978
979 if (len_of_ctxts < sizeof(struct smb2_neg_context))
980 break;
981
982 pctx = (struct smb2_neg_context *)((char *)pctx + offset);
983 clen = le16_to_cpu(pctx->DataLength);
984 if (clen + sizeof(struct smb2_neg_context) > len_of_ctxts)
985 break;
986
987 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900988 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900989 "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900990 if (conn->preauth_info->Preauth_HashId)
991 break;
992
993 status = decode_preauth_ctxt(conn,
Namjae Jeonaf320a72021-07-21 10:03:19 +0900994 (struct smb2_preauth_neg_context *)pctx);
995 if (status != STATUS_SUCCESS)
996 break;
997 } else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900998 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900999 "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001000 if (conn->cipher_type)
1001 break;
1002
Namjae Jeonaf320a72021-07-21 10:03:19 +09001003 decode_encrypt_ctxt(conn,
1004 (struct smb2_encryption_neg_context *)pctx,
1005 len_of_ctxts);
1006 } else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001007 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001008 "deassemble SMB2_COMPRESSION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001009 if (conn->compress_algorithm)
1010 break;
1011
Namjae Jeonaf320a72021-07-21 10:03:19 +09001012 decode_compress_ctxt(conn,
1013 (struct smb2_compression_ctx *)pctx);
1014 } else if (pctx->ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001015 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001016 "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n");
Namjae Jeonaf320a72021-07-21 10:03:19 +09001017 } else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001018 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001019 "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001020 conn->posix_ext_supported = true;
Namjae Jeon378087c2021-07-21 10:05:53 +09001021 } else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES) {
1022 ksmbd_debug(SMB,
1023 "deassemble SMB2_SIGNING_CAPABILITIES context\n");
1024 decode_sign_cap_ctxt(conn,
1025 (struct smb2_signing_capabilities *)pctx,
1026 len_of_ctxts);
Namjae Jeone2f34482021-03-16 10:49:09 +09001027 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001028
Namjae Jeonaf320a72021-07-21 10:03:19 +09001029 /* offsets must be 8 byte aligned */
1030 clen = (clen + 7) & ~0x7;
1031 offset = clen + sizeof(struct smb2_neg_context);
1032 len_of_ctxts -= clen + sizeof(struct smb2_neg_context);
Namjae Jeone2f34482021-03-16 10:49:09 +09001033 }
1034 return status;
1035}
1036
1037/**
1038 * smb2_handle_negotiate() - handler for smb2 negotiate command
1039 * @work: smb work containing smb request buffer
1040 *
1041 * Return: 0
1042 */
1043int smb2_handle_negotiate(struct ksmbd_work *work)
1044{
1045 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09001046 struct smb2_negotiate_req *req = smb2_get_msg(work->request_buf);
1047 struct smb2_negotiate_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001048 int rc = 0;
Namjae Jeon442ff9e2021-09-29 15:44:32 +09001049 unsigned int smb2_buf_len, smb2_neg_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09001050 __le32 status;
1051
1052 ksmbd_debug(SMB, "Received negotiate request\n");
1053 conn->need_neg = false;
1054 if (ksmbd_conn_good(work)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001055 pr_err("conn->tcp_status is already in CifsGood State\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001056 work->send_no_response = 1;
1057 return rc;
1058 }
1059
1060 if (req->DialectCount == 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001061 pr_err("malformed packet\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001062 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1063 rc = -EINVAL;
1064 goto err_out;
1065 }
1066
Namjae Jeon442ff9e2021-09-29 15:44:32 +09001067 smb2_buf_len = get_rfc1002_len(work->request_buf);
Namjae Jeoncb451722021-11-03 08:08:44 +09001068 smb2_neg_size = offsetof(struct smb2_negotiate_req, Dialects);
Namjae Jeon442ff9e2021-09-29 15:44:32 +09001069 if (smb2_neg_size > smb2_buf_len) {
1070 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1071 rc = -EINVAL;
1072 goto err_out;
1073 }
1074
1075 if (conn->dialect == SMB311_PROT_ID) {
1076 unsigned int nego_ctxt_off = le32_to_cpu(req->NegotiateContextOffset);
1077
1078 if (smb2_buf_len < nego_ctxt_off) {
1079 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1080 rc = -EINVAL;
1081 goto err_out;
1082 }
1083
1084 if (smb2_neg_size > nego_ctxt_off) {
1085 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1086 rc = -EINVAL;
1087 goto err_out;
1088 }
1089
1090 if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) >
1091 nego_ctxt_off) {
1092 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1093 rc = -EINVAL;
1094 goto err_out;
1095 }
1096 } else {
1097 if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) >
1098 smb2_buf_len) {
1099 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1100 rc = -EINVAL;
1101 goto err_out;
1102 }
1103 }
1104
Namjae Jeone2f34482021-03-16 10:49:09 +09001105 conn->cli_cap = le32_to_cpu(req->Capabilities);
1106 switch (conn->dialect) {
1107 case SMB311_PROT_ID:
1108 conn->preauth_info =
1109 kzalloc(sizeof(struct preauth_integrity_info),
Namjae Jeon070fb212021-05-26 17:57:12 +09001110 GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09001111 if (!conn->preauth_info) {
1112 rc = -ENOMEM;
1113 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1114 goto err_out;
1115 }
1116
Namjae Jeoncb451722021-11-03 08:08:44 +09001117 status = deassemble_neg_contexts(conn, req,
1118 get_rfc1002_len(work->request_buf));
Namjae Jeone2f34482021-03-16 10:49:09 +09001119 if (status != STATUS_SUCCESS) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001120 pr_err("deassemble_neg_contexts error(0x%x)\n",
1121 status);
Namjae Jeone2f34482021-03-16 10:49:09 +09001122 rsp->hdr.Status = status;
1123 rc = -EINVAL;
1124 goto err_out;
1125 }
1126
1127 rc = init_smb3_11_server(conn);
1128 if (rc < 0) {
1129 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1130 goto err_out;
1131 }
1132
1133 ksmbd_gen_preauth_integrity_hash(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001134 work->request_buf,
1135 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001136 rsp->NegotiateContextOffset =
1137 cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
Namjae Jeoncb451722021-11-03 08:08:44 +09001138 assemble_neg_contexts(conn, rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001139 break;
1140 case SMB302_PROT_ID:
1141 init_smb3_02_server(conn);
1142 break;
1143 case SMB30_PROT_ID:
1144 init_smb3_0_server(conn);
1145 break;
1146 case SMB21_PROT_ID:
1147 init_smb2_1_server(conn);
1148 break;
Namjae Jeone2f34482021-03-16 10:49:09 +09001149 case SMB2X_PROT_ID:
1150 case BAD_PROT_ID:
1151 default:
1152 ksmbd_debug(SMB, "Server dialect :0x%x not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001153 conn->dialect);
Namjae Jeone2f34482021-03-16 10:49:09 +09001154 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
1155 rc = -EINVAL;
1156 goto err_out;
1157 }
1158 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
1159
1160 /* For stats */
1161 conn->connection_type = conn->dialect;
1162
1163 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
1164 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
1165 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
1166
Namjae Jeon51a13872021-09-29 13:09:24 +09001167 memcpy(conn->ClientGUID, req->ClientGUID,
1168 SMB2_CLIENT_GUID_SIZE);
1169 conn->cli_sec_mode = le16_to_cpu(req->SecurityMode);
Namjae Jeone2f34482021-03-16 10:49:09 +09001170
1171 rsp->StructureSize = cpu_to_le16(65);
1172 rsp->DialectRevision = cpu_to_le16(conn->dialect);
1173 /* Not setting conn guid rsp->ServerGUID, as it
1174 * not used by client for identifying server
1175 */
1176 memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE);
1177
1178 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
1179 rsp->ServerStartTime = 0;
1180 ksmbd_debug(SMB, "negotiate context offset %d, count %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001181 le32_to_cpu(rsp->NegotiateContextOffset),
1182 le16_to_cpu(rsp->NegotiateContextCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09001183
1184 rsp->SecurityBufferOffset = cpu_to_le16(128);
1185 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
Namjae Jeoncb451722021-11-03 08:08:44 +09001186 ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) +
1187 le16_to_cpu(rsp->SecurityBufferOffset));
1188 inc_rfc1001_len(work->response_buf, sizeof(struct smb2_negotiate_rsp) -
Namjae Jeon070fb212021-05-26 17:57:12 +09001189 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
1190 AUTH_GSS_LENGTH);
Namjae Jeone2f34482021-03-16 10:49:09 +09001191 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
1192 conn->use_spnego = true;
1193
1194 if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001195 server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) &&
1196 req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09001197 conn->sign = true;
1198 else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) {
1199 server_conf.enforced_signing = true;
1200 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
1201 conn->sign = true;
1202 }
1203
1204 conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode);
1205 ksmbd_conn_set_need_negotiate(work);
1206
1207err_out:
1208 if (rc < 0)
1209 smb2_set_err_rsp(work);
1210
1211 return rc;
1212}
1213
1214static int alloc_preauth_hash(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09001215 struct ksmbd_conn *conn)
Namjae Jeone2f34482021-03-16 10:49:09 +09001216{
1217 if (sess->Preauth_HashValue)
1218 return 0;
1219
kernel test robot86f52972021-04-02 12:17:24 +09001220 sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue,
Namjae Jeon070fb212021-05-26 17:57:12 +09001221 PREAUTH_HASHVALUE_SIZE, GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09001222 if (!sess->Preauth_HashValue)
1223 return -ENOMEM;
1224
Namjae Jeone2f34482021-03-16 10:49:09 +09001225 return 0;
1226}
1227
1228static int generate_preauth_hash(struct ksmbd_work *work)
1229{
1230 struct ksmbd_conn *conn = work->conn;
1231 struct ksmbd_session *sess = work->sess;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001232 u8 *preauth_hash;
Namjae Jeone2f34482021-03-16 10:49:09 +09001233
1234 if (conn->dialect != SMB311_PROT_ID)
1235 return 0;
1236
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001237 if (conn->binding) {
1238 struct preauth_session *preauth_sess;
1239
1240 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
1241 if (!preauth_sess) {
1242 preauth_sess = ksmbd_preauth_session_alloc(conn, sess->id);
1243 if (!preauth_sess)
1244 return -ENOMEM;
1245 }
1246
1247 preauth_hash = preauth_sess->Preauth_HashValue;
1248 } else {
1249 if (!sess->Preauth_HashValue)
1250 if (alloc_preauth_hash(sess, conn))
1251 return -ENOMEM;
1252 preauth_hash = sess->Preauth_HashValue;
Namjae Jeone2f34482021-03-16 10:49:09 +09001253 }
1254
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001255 ksmbd_gen_preauth_integrity_hash(conn, work->request_buf, preauth_hash);
Namjae Jeone2f34482021-03-16 10:49:09 +09001256 return 0;
1257}
1258
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001259static int decode_negotiation_token(struct ksmbd_conn *conn,
1260 struct negotiate_message *negblob,
1261 size_t sz)
Namjae Jeone2f34482021-03-16 10:49:09 +09001262{
Namjae Jeone2f34482021-03-16 10:49:09 +09001263 if (!conn->use_spnego)
1264 return -EINVAL;
1265
Hyunchul Leefad41612021-04-19 17:26:15 +09001266 if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) {
1267 if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001268 conn->auth_mechs |= KSMBD_AUTH_NTLMSSP;
1269 conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP;
1270 conn->use_spnego = false;
1271 }
1272 }
1273 return 0;
1274}
1275
1276static int ntlm_negotiate(struct ksmbd_work *work,
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001277 struct negotiate_message *negblob,
1278 size_t negblob_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09001279{
Namjae Jeoncb451722021-11-03 08:08:44 +09001280 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001281 struct challenge_message *chgblob;
1282 unsigned char *spnego_blob = NULL;
1283 u16 spnego_blob_len;
1284 char *neg_blob;
1285 int sz, rc;
1286
1287 ksmbd_debug(SMB, "negotiate phase\n");
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001288 rc = ksmbd_decode_ntlmssp_neg_blob(negblob, negblob_len, work->sess);
Namjae Jeone2f34482021-03-16 10:49:09 +09001289 if (rc)
1290 return rc;
1291
1292 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1293 chgblob =
1294 (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz);
1295 memset(chgblob, 0, sizeof(struct challenge_message));
1296
1297 if (!work->conn->use_spnego) {
1298 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess);
1299 if (sz < 0)
1300 return -ENOMEM;
1301
1302 rsp->SecurityBufferLength = cpu_to_le16(sz);
1303 return 0;
1304 }
1305
1306 sz = sizeof(struct challenge_message);
1307 sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6;
1308
1309 neg_blob = kzalloc(sz, GFP_KERNEL);
1310 if (!neg_blob)
1311 return -ENOMEM;
1312
1313 chgblob = (struct challenge_message *)neg_blob;
1314 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess);
1315 if (sz < 0) {
1316 rc = -ENOMEM;
1317 goto out;
1318 }
1319
Namjae Jeon070fb212021-05-26 17:57:12 +09001320 rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len,
1321 neg_blob, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09001322 if (rc) {
1323 rc = -ENOMEM;
1324 goto out;
1325 }
1326
1327 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1328 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
1329 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1330
1331out:
1332 kfree(spnego_blob);
1333 kfree(neg_blob);
1334 return rc;
1335}
1336
1337static struct authenticate_message *user_authblob(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001338 struct smb2_sess_setup_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +09001339{
1340 int sz;
1341
1342 if (conn->use_spnego && conn->mechToken)
1343 return (struct authenticate_message *)conn->mechToken;
1344
1345 sz = le16_to_cpu(req->SecurityBufferOffset);
1346 return (struct authenticate_message *)((char *)&req->hdr.ProtocolId
1347 + sz);
1348}
1349
1350static struct ksmbd_user *session_user(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001351 struct smb2_sess_setup_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +09001352{
1353 struct authenticate_message *authblob;
1354 struct ksmbd_user *user;
1355 char *name;
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001356 unsigned int auth_msg_len, name_off, name_len, secbuf_len;
Namjae Jeone2f34482021-03-16 10:49:09 +09001357
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001358 secbuf_len = le16_to_cpu(req->SecurityBufferLength);
1359 if (secbuf_len < sizeof(struct authenticate_message)) {
1360 ksmbd_debug(SMB, "blob len %d too small\n", secbuf_len);
1361 return NULL;
1362 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001363 authblob = user_authblob(conn, req);
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001364 name_off = le32_to_cpu(authblob->UserName.BufferOffset);
1365 name_len = le16_to_cpu(authblob->UserName.Length);
1366 auth_msg_len = le16_to_cpu(req->SecurityBufferOffset) + secbuf_len;
1367
1368 if (auth_msg_len < (u64)name_off + name_len)
1369 return NULL;
1370
1371 name = smb_strndup_from_utf16((const char *)authblob + name_off,
1372 name_len,
Namjae Jeone2f34482021-03-16 10:49:09 +09001373 true,
1374 conn->local_nls);
1375 if (IS_ERR(name)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001376 pr_err("cannot allocate memory\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001377 return NULL;
1378 }
1379
1380 ksmbd_debug(SMB, "session setup request for user %s\n", name);
1381 user = ksmbd_login_user(name);
1382 kfree(name);
1383 return user;
1384}
1385
1386static int ntlm_authenticate(struct ksmbd_work *work)
1387{
Namjae Jeoncb451722021-11-03 08:08:44 +09001388 struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf);
1389 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001390 struct ksmbd_conn *conn = work->conn;
1391 struct ksmbd_session *sess = work->sess;
1392 struct channel *chann = NULL;
1393 struct ksmbd_user *user;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001394 u64 prev_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09001395 int sz, rc;
1396
1397 ksmbd_debug(SMB, "authenticate phase\n");
1398 if (conn->use_spnego) {
1399 unsigned char *spnego_blob;
1400 u16 spnego_blob_len;
1401
1402 rc = build_spnego_ntlmssp_auth_blob(&spnego_blob,
1403 &spnego_blob_len,
1404 0);
1405 if (rc)
1406 return -ENOMEM;
1407
1408 sz = le16_to_cpu(rsp->SecurityBufferOffset);
Namjae Jeon64b39f42021-03-30 14:25:35 +09001409 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001410 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1411 kfree(spnego_blob);
Namjae Jeoncb451722021-11-03 08:08:44 +09001412 inc_rfc1001_len(work->response_buf, spnego_blob_len - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001413 }
1414
1415 user = session_user(conn, req);
1416 if (!user) {
1417 ksmbd_debug(SMB, "Unknown user name or an error\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001418 return -EPERM;
Namjae Jeone2f34482021-03-16 10:49:09 +09001419 }
1420
1421 /* Check for previous session */
1422 prev_id = le64_to_cpu(req->PreviousSessionId);
1423 if (prev_id && prev_id != sess->id)
1424 destroy_previous_session(user, prev_id);
1425
1426 if (sess->state == SMB2_SESSION_VALID) {
1427 /*
1428 * Reuse session if anonymous try to connect
1429 * on reauthetication.
1430 */
1431 if (ksmbd_anonymous_user(user)) {
1432 ksmbd_free_user(user);
1433 return 0;
1434 }
1435 ksmbd_free_user(sess->user);
1436 }
1437
1438 sess->user = user;
1439 if (user_guest(sess->user)) {
1440 if (conn->sign) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001441 ksmbd_debug(SMB, "Guest login not allowed when signing enabled\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001442 return -EPERM;
Namjae Jeone2f34482021-03-16 10:49:09 +09001443 }
1444
1445 rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE;
1446 } else {
1447 struct authenticate_message *authblob;
1448
1449 authblob = user_authblob(conn, req);
1450 sz = le16_to_cpu(req->SecurityBufferLength);
1451 rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, sess);
1452 if (rc) {
1453 set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD);
1454 ksmbd_debug(SMB, "authentication failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001455 return -EPERM;
Namjae Jeone2f34482021-03-16 10:49:09 +09001456 }
1457
1458 /*
1459 * If session state is SMB2_SESSION_VALID, We can assume
1460 * that it is reauthentication. And the user/password
1461 * has been verified, so return it here.
1462 */
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001463 if (sess->state == SMB2_SESSION_VALID) {
1464 if (conn->binding)
1465 goto binding_session;
Namjae Jeone2f34482021-03-16 10:49:09 +09001466 return 0;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001467 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001468
1469 if ((conn->sign || server_conf.enforced_signing) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001470 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
Namjae Jeone2f34482021-03-16 10:49:09 +09001471 sess->sign = true;
1472
1473 if (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION &&
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001474 conn->ops->generate_encryptionkey &&
1475 !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001476 rc = conn->ops->generate_encryptionkey(sess);
1477 if (rc) {
1478 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001479 "SMB3 encryption key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001480 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001481 }
1482 sess->enc = true;
1483 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1484 /*
1485 * signing is disable if encryption is enable
1486 * on this session
1487 */
1488 sess->sign = false;
1489 }
1490 }
1491
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001492binding_session:
Namjae Jeone2f34482021-03-16 10:49:09 +09001493 if (conn->dialect >= SMB30_PROT_ID) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001494 chann = lookup_chann_list(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001495 if (!chann) {
1496 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1497 if (!chann)
1498 return -ENOMEM;
1499
1500 chann->conn = conn;
1501 INIT_LIST_HEAD(&chann->chann_list);
1502 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1503 }
1504 }
1505
1506 if (conn->ops->generate_signingkey) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001507 rc = conn->ops->generate_signingkey(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001508 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001509 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001510 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001511 }
1512 }
1513
Namjae Jeon51a13872021-09-29 13:09:24 +09001514 if (!ksmbd_conn_lookup_dialect(conn)) {
1515 pr_err("fail to verify the dialect\n");
1516 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001517 }
1518 return 0;
1519}
1520
1521#ifdef CONFIG_SMB_SERVER_KERBEROS5
1522static int krb5_authenticate(struct ksmbd_work *work)
1523{
Namjae Jeoncb451722021-11-03 08:08:44 +09001524 struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf);
1525 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001526 struct ksmbd_conn *conn = work->conn;
1527 struct ksmbd_session *sess = work->sess;
1528 char *in_blob, *out_blob;
1529 struct channel *chann = NULL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001530 u64 prev_sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09001531 int in_len, out_len;
1532 int retval;
1533
1534 in_blob = (char *)&req->hdr.ProtocolId +
1535 le16_to_cpu(req->SecurityBufferOffset);
1536 in_len = le16_to_cpu(req->SecurityBufferLength);
1537 out_blob = (char *)&rsp->hdr.ProtocolId +
1538 le16_to_cpu(rsp->SecurityBufferOffset);
1539 out_len = work->response_sz -
Namjae Jeoncb451722021-11-03 08:08:44 +09001540 (le16_to_cpu(rsp->SecurityBufferOffset) + 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09001541
1542 /* Check previous session */
1543 prev_sess_id = le64_to_cpu(req->PreviousSessionId);
1544 if (prev_sess_id && prev_sess_id != sess->id)
1545 destroy_previous_session(sess->user, prev_sess_id);
1546
1547 if (sess->state == SMB2_SESSION_VALID)
1548 ksmbd_free_user(sess->user);
1549
1550 retval = ksmbd_krb5_authenticate(sess, in_blob, in_len,
Namjae Jeon070fb212021-05-26 17:57:12 +09001551 out_blob, &out_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001552 if (retval) {
1553 ksmbd_debug(SMB, "krb5 authentication failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001554 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001555 }
1556 rsp->SecurityBufferLength = cpu_to_le16(out_len);
Namjae Jeoncb451722021-11-03 08:08:44 +09001557 inc_rfc1001_len(work->response_buf, out_len - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001558
1559 if ((conn->sign || server_conf.enforced_signing) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001560 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
Namjae Jeone2f34482021-03-16 10:49:09 +09001561 sess->sign = true;
1562
1563 if ((conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09001564 conn->ops->generate_encryptionkey) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001565 retval = conn->ops->generate_encryptionkey(sess);
1566 if (retval) {
1567 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001568 "SMB3 encryption key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001569 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001570 }
1571 sess->enc = true;
1572 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1573 sess->sign = false;
1574 }
1575
1576 if (conn->dialect >= SMB30_PROT_ID) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001577 chann = lookup_chann_list(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001578 if (!chann) {
1579 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1580 if (!chann)
1581 return -ENOMEM;
1582
1583 chann->conn = conn;
1584 INIT_LIST_HEAD(&chann->chann_list);
1585 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1586 }
1587 }
1588
1589 if (conn->ops->generate_signingkey) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001590 retval = conn->ops->generate_signingkey(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001591 if (retval) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001592 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001593 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001594 }
1595 }
1596
Namjae Jeon51a13872021-09-29 13:09:24 +09001597 if (!ksmbd_conn_lookup_dialect(conn)) {
1598 pr_err("fail to verify the dialect\n");
1599 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001600 }
1601 return 0;
1602}
1603#else
1604static int krb5_authenticate(struct ksmbd_work *work)
1605{
1606 return -EOPNOTSUPP;
1607}
1608#endif
1609
1610int smb2_sess_setup(struct ksmbd_work *work)
1611{
1612 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09001613 struct smb2_sess_setup_req *req = smb2_get_msg(work->request_buf);
1614 struct smb2_sess_setup_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001615 struct ksmbd_session *sess;
1616 struct negotiate_message *negblob;
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001617 unsigned int negblob_len, negblob_off;
Namjae Jeone2f34482021-03-16 10:49:09 +09001618 int rc = 0;
1619
1620 ksmbd_debug(SMB, "Received request for session setup\n");
1621
1622 rsp->StructureSize = cpu_to_le16(9);
1623 rsp->SessionFlags = 0;
1624 rsp->SecurityBufferOffset = cpu_to_le16(72);
1625 rsp->SecurityBufferLength = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09001626 inc_rfc1001_len(work->response_buf, 9);
Namjae Jeone2f34482021-03-16 10:49:09 +09001627
1628 if (!req->hdr.SessionId) {
1629 sess = ksmbd_smb2_session_create();
1630 if (!sess) {
1631 rc = -ENOMEM;
1632 goto out_err;
1633 }
1634 rsp->hdr.SessionId = cpu_to_le64(sess->id);
1635 ksmbd_session_register(conn, sess);
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001636 } else if (conn->dialect >= SMB30_PROT_ID &&
1637 (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
1638 req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) {
1639 u64 sess_id = le64_to_cpu(req->hdr.SessionId);
1640
1641 sess = ksmbd_session_lookup_slowpath(sess_id);
1642 if (!sess) {
1643 rc = -ENOENT;
1644 goto out_err;
1645 }
1646
1647 if (conn->dialect != sess->conn->dialect) {
1648 rc = -EINVAL;
1649 goto out_err;
1650 }
1651
1652 if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) {
1653 rc = -EINVAL;
1654 goto out_err;
1655 }
1656
1657 if (strncmp(conn->ClientGUID, sess->conn->ClientGUID,
1658 SMB2_CLIENT_GUID_SIZE)) {
1659 rc = -ENOENT;
1660 goto out_err;
1661 }
1662
1663 if (sess->state == SMB2_SESSION_IN_PROGRESS) {
1664 rc = -EACCES;
1665 goto out_err;
1666 }
1667
1668 if (sess->state == SMB2_SESSION_EXPIRED) {
1669 rc = -EFAULT;
1670 goto out_err;
1671 }
1672
1673 if (ksmbd_session_lookup(conn, sess_id)) {
1674 rc = -EACCES;
1675 goto out_err;
1676 }
1677
1678 conn->binding = true;
1679 } else if ((conn->dialect < SMB30_PROT_ID ||
1680 server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
1681 (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
Colin Ian King4951a842021-07-06 13:05:01 +01001682 sess = NULL;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001683 rc = -EACCES;
1684 goto out_err;
Namjae Jeone2f34482021-03-16 10:49:09 +09001685 } else {
1686 sess = ksmbd_session_lookup(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001687 le64_to_cpu(req->hdr.SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09001688 if (!sess) {
1689 rc = -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001690 goto out_err;
1691 }
1692 }
1693 work->sess = sess;
1694
1695 if (sess->state == SMB2_SESSION_EXPIRED)
1696 sess->state = SMB2_SESSION_IN_PROGRESS;
1697
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001698 negblob_off = le16_to_cpu(req->SecurityBufferOffset);
1699 negblob_len = le16_to_cpu(req->SecurityBufferLength);
Namjae Jeoncb451722021-11-03 08:08:44 +09001700 if (negblob_off < offsetof(struct smb2_sess_setup_req, Buffer) ||
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001701 negblob_len < offsetof(struct negotiate_message, NegotiateFlags))
1702 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001703
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001704 negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId +
1705 negblob_off);
1706
1707 if (decode_negotiation_token(conn, negblob, negblob_len) == 0) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001708 if (conn->mechToken)
1709 negblob = (struct negotiate_message *)conn->mechToken;
1710 }
1711
1712 if (server_conf.auth_mechs & conn->auth_mechs) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001713 rc = generate_preauth_hash(work);
1714 if (rc)
1715 goto out_err;
1716
Namjae Jeone2f34482021-03-16 10:49:09 +09001717 if (conn->preferred_auth_mech &
1718 (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001719 rc = krb5_authenticate(work);
1720 if (rc) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001721 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001722 goto out_err;
1723 }
1724
1725 ksmbd_conn_set_good(work);
1726 sess->state = SMB2_SESSION_VALID;
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001727 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001728 sess->Preauth_HashValue = NULL;
1729 } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001730 if (negblob->MessageType == NtLmNegotiate) {
Marios Makassikis0d994cd2021-10-19 17:39:38 +02001731 rc = ntlm_negotiate(work, negblob, negblob_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001732 if (rc)
1733 goto out_err;
1734 rsp->hdr.Status =
1735 STATUS_MORE_PROCESSING_REQUIRED;
1736 /*
1737 * Note: here total size -1 is done as an
1738 * adjustment for 0 size blob
1739 */
Namjae Jeoncb451722021-11-03 08:08:44 +09001740 inc_rfc1001_len(work->response_buf,
1741 le16_to_cpu(rsp->SecurityBufferLength) - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001742
1743 } else if (negblob->MessageType == NtLmAuthenticate) {
1744 rc = ntlm_authenticate(work);
1745 if (rc)
1746 goto out_err;
1747
1748 ksmbd_conn_set_good(work);
1749 sess->state = SMB2_SESSION_VALID;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001750 if (conn->binding) {
1751 struct preauth_session *preauth_sess;
1752
1753 preauth_sess =
1754 ksmbd_preauth_session_lookup(conn, sess->id);
1755 if (preauth_sess) {
1756 list_del(&preauth_sess->preauth_entry);
1757 kfree(preauth_sess);
1758 }
1759 }
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001760 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001761 sess->Preauth_HashValue = NULL;
1762 }
1763 } else {
1764 /* TODO: need one more negotiation */
Namjae Jeonbde16942021-06-28 15:23:19 +09001765 pr_err("Not support the preferred authentication\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001766 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001767 }
1768 } else {
Namjae Jeonbde16942021-06-28 15:23:19 +09001769 pr_err("Not support authentication\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001770 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001771 }
1772
1773out_err:
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001774 if (rc == -EINVAL)
1775 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1776 else if (rc == -ENOENT)
1777 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1778 else if (rc == -EACCES)
1779 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
1780 else if (rc == -EFAULT)
1781 rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED;
Namjae Jeon58090b12021-07-16 14:52:09 +09001782 else if (rc == -ENOMEM)
1783 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001784 else if (rc)
1785 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1786
Namjae Jeone2f34482021-03-16 10:49:09 +09001787 if (conn->use_spnego && conn->mechToken) {
1788 kfree(conn->mechToken);
1789 conn->mechToken = NULL;
1790 }
1791
Namjae Jeon621be842021-10-13 17:28:31 +09001792 if (rc < 0) {
1793 /*
1794 * SecurityBufferOffset should be set to zero
1795 * in session setup error response.
1796 */
1797 rsp->SecurityBufferOffset = 0;
1798
1799 if (sess) {
1800 bool try_delay = false;
1801
1802 /*
1803 * To avoid dictionary attacks (repeated session setups rapidly sent) to
1804 * connect to server, ksmbd make a delay of a 5 seconds on session setup
1805 * failure to make it harder to send enough random connection requests
1806 * to break into a server.
1807 */
1808 if (sess->user && sess->user->flags & KSMBD_USER_FLAG_DELAY_SESSION)
1809 try_delay = true;
1810
1811 ksmbd_session_destroy(sess);
1812 work->sess = NULL;
1813 if (try_delay)
1814 ssleep(5);
1815 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001816 }
1817
1818 return rc;
1819}
1820
1821/**
1822 * smb2_tree_connect() - handler for smb2 tree connect command
1823 * @work: smb work containing smb request buffer
1824 *
1825 * Return: 0 on success, otherwise error
1826 */
1827int smb2_tree_connect(struct ksmbd_work *work)
1828{
1829 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09001830 struct smb2_tree_connect_req *req = smb2_get_msg(work->request_buf);
1831 struct smb2_tree_connect_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09001832 struct ksmbd_session *sess = work->sess;
1833 char *treename = NULL, *name = NULL;
1834 struct ksmbd_tree_conn_status status;
1835 struct ksmbd_share_config *share;
1836 int rc = -EINVAL;
1837
1838 treename = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09001839 le16_to_cpu(req->PathLength), true,
1840 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09001841 if (IS_ERR(treename)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001842 pr_err("treename is NULL\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001843 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1844 goto out_err1;
1845 }
1846
Stephen Rothwell36ba3862021-03-17 17:01:15 +09001847 name = ksmbd_extract_sharename(treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001848 if (IS_ERR(name)) {
1849 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1850 goto out_err1;
1851 }
1852
1853 ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001854 name, treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001855
1856 status = ksmbd_tree_conn_connect(sess, name);
1857 if (status.ret == KSMBD_TREE_CONN_STATUS_OK)
1858 rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id);
1859 else
1860 goto out_err1;
1861
1862 share = status.tree_conn->share_conf;
1863 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
1864 ksmbd_debug(SMB, "IPC share path request\n");
1865 rsp->ShareType = SMB2_SHARE_TYPE_PIPE;
1866 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1867 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE |
1868 FILE_DELETE_LE | FILE_READ_CONTROL_LE |
1869 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1870 FILE_SYNCHRONIZE_LE;
1871 } else {
1872 rsp->ShareType = SMB2_SHARE_TYPE_DISK;
1873 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1874 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE;
1875 if (test_tree_conn_flag(status.tree_conn,
1876 KSMBD_TREE_CONN_FLAG_WRITABLE)) {
1877 rsp->MaximalAccess |= FILE_WRITE_DATA_LE |
1878 FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE |
Wan Jiabing3aefd542021-06-07 12:54:32 +08001879 FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE |
1880 FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE |
1881 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1882 FILE_SYNCHRONIZE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09001883 }
1884 }
1885
1886 status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1887 if (conn->posix_ext_supported)
1888 status.tree_conn->posix_extensions = true;
1889
1890out_err1:
1891 rsp->StructureSize = cpu_to_le16(16);
1892 rsp->Capabilities = 0;
1893 rsp->Reserved = 0;
1894 /* default manual caching */
1895 rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING;
Namjae Jeoncb451722021-11-03 08:08:44 +09001896 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09001897
1898 if (!IS_ERR(treename))
1899 kfree(treename);
1900 if (!IS_ERR(name))
1901 kfree(name);
1902
1903 switch (status.ret) {
1904 case KSMBD_TREE_CONN_STATUS_OK:
1905 rsp->hdr.Status = STATUS_SUCCESS;
1906 rc = 0;
1907 break;
1908 case KSMBD_TREE_CONN_STATUS_NO_SHARE:
1909 rsp->hdr.Status = STATUS_BAD_NETWORK_PATH;
1910 break;
1911 case -ENOMEM:
1912 case KSMBD_TREE_CONN_STATUS_NOMEM:
1913 rsp->hdr.Status = STATUS_NO_MEMORY;
1914 break;
1915 case KSMBD_TREE_CONN_STATUS_ERROR:
1916 case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS:
1917 case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS:
1918 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1919 break;
1920 case -EINVAL:
1921 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1922 break;
1923 default:
1924 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1925 }
1926
1927 return rc;
1928}
1929
1930/**
1931 * smb2_create_open_flags() - convert smb open flags to unix open flags
1932 * @file_present: is file already present
1933 * @access: file access flags
1934 * @disposition: file disposition flags
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001935 * @may_flags: set with MAY_ flags
Namjae Jeone2f34482021-03-16 10:49:09 +09001936 *
1937 * Return: file open flags
1938 */
1939static int smb2_create_open_flags(bool file_present, __le32 access,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001940 __le32 disposition,
1941 int *may_flags)
Namjae Jeone2f34482021-03-16 10:49:09 +09001942{
1943 int oflags = O_NONBLOCK | O_LARGEFILE;
1944
1945 if (access & FILE_READ_DESIRED_ACCESS_LE &&
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001946 access & FILE_WRITE_DESIRE_ACCESS_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001947 oflags |= O_RDWR;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001948 *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE;
1949 } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001950 oflags |= O_WRONLY;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001951 *may_flags = MAY_OPEN | MAY_WRITE;
1952 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09001953 oflags |= O_RDONLY;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001954 *may_flags = MAY_OPEN | MAY_READ;
1955 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001956
1957 if (access == FILE_READ_ATTRIBUTES_LE)
1958 oflags |= O_PATH;
1959
1960 if (file_present) {
1961 switch (disposition & FILE_CREATE_MASK_LE) {
1962 case FILE_OPEN_LE:
1963 case FILE_CREATE_LE:
1964 break;
1965 case FILE_SUPERSEDE_LE:
1966 case FILE_OVERWRITE_LE:
1967 case FILE_OVERWRITE_IF_LE:
1968 oflags |= O_TRUNC;
1969 break;
1970 default:
1971 break;
1972 }
1973 } else {
1974 switch (disposition & FILE_CREATE_MASK_LE) {
1975 case FILE_SUPERSEDE_LE:
1976 case FILE_CREATE_LE:
1977 case FILE_OPEN_IF_LE:
1978 case FILE_OVERWRITE_IF_LE:
1979 oflags |= O_CREAT;
1980 break;
1981 case FILE_OPEN_LE:
1982 case FILE_OVERWRITE_LE:
1983 oflags &= ~O_CREAT;
1984 break;
1985 default:
1986 break;
1987 }
1988 }
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001989
Namjae Jeone2f34482021-03-16 10:49:09 +09001990 return oflags;
1991}
1992
1993/**
1994 * smb2_tree_disconnect() - handler for smb tree connect request
1995 * @work: smb work containing request buffer
1996 *
1997 * Return: 0
1998 */
1999int smb2_tree_disconnect(struct ksmbd_work *work)
2000{
Namjae Jeoncb451722021-11-03 08:08:44 +09002001 struct smb2_tree_disconnect_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002002 struct ksmbd_session *sess = work->sess;
2003 struct ksmbd_tree_connect *tcon = work->tcon;
2004
2005 rsp->StructureSize = cpu_to_le16(4);
Namjae Jeoncb451722021-11-03 08:08:44 +09002006 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09002007
2008 ksmbd_debug(SMB, "request\n");
2009
2010 if (!tcon) {
Namjae Jeoncb451722021-11-03 08:08:44 +09002011 struct smb2_tree_disconnect_req *req =
2012 smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002013
2014 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2015 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2016 smb2_set_err_rsp(work);
2017 return 0;
2018 }
2019
2020 ksmbd_close_tree_conn_fds(work);
2021 ksmbd_tree_conn_disconnect(sess, tcon);
2022 return 0;
2023}
2024
2025/**
2026 * smb2_session_logoff() - handler for session log off request
2027 * @work: smb work containing request buffer
2028 *
2029 * Return: 0
2030 */
2031int smb2_session_logoff(struct ksmbd_work *work)
2032{
2033 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09002034 struct smb2_logoff_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002035 struct ksmbd_session *sess = work->sess;
2036
2037 rsp->StructureSize = cpu_to_le16(4);
Namjae Jeoncb451722021-11-03 08:08:44 +09002038 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09002039
2040 ksmbd_debug(SMB, "request\n");
2041
2042 /* Got a valid session, set connection state */
2043 WARN_ON(sess->conn != conn);
2044
2045 /* setting CifsExiting here may race with start_tcp_sess */
2046 ksmbd_conn_set_need_reconnect(work);
2047 ksmbd_close_session_fds(work);
2048 ksmbd_conn_wait_idle(conn);
2049
2050 if (ksmbd_tree_conn_session_logoff(sess)) {
Namjae Jeoncb451722021-11-03 08:08:44 +09002051 struct smb2_logoff_req *req = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002052
2053 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2054 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2055 smb2_set_err_rsp(work);
2056 return 0;
2057 }
2058
2059 ksmbd_destroy_file_table(&sess->file_table);
2060 sess->state = SMB2_SESSION_EXPIRED;
2061
2062 ksmbd_free_user(sess->user);
2063 sess->user = NULL;
2064
2065 /* let start_tcp_sess free connection info now */
2066 ksmbd_conn_set_need_negotiate(work);
2067 return 0;
2068}
2069
2070/**
2071 * create_smb2_pipe() - create IPC pipe
2072 * @work: smb work containing request buffer
2073 *
2074 * Return: 0 on success, otherwise error
2075 */
2076static noinline int create_smb2_pipe(struct ksmbd_work *work)
2077{
Namjae Jeoncb451722021-11-03 08:08:44 +09002078 struct smb2_create_rsp *rsp = smb2_get_msg(work->response_buf);
2079 struct smb2_create_req *req = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09002080 int id;
2081 int err;
2082 char *name;
2083
2084 name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09002085 1, work->conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09002086 if (IS_ERR(name)) {
2087 rsp->hdr.Status = STATUS_NO_MEMORY;
2088 err = PTR_ERR(name);
2089 goto out;
2090 }
2091
2092 id = ksmbd_session_rpc_open(work->sess, name);
Marios Makassikis79caa962021-05-06 11:38:35 +09002093 if (id < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002094 pr_err("Unable to open RPC pipe: %d\n", id);
Marios Makassikis79caa962021-05-06 11:38:35 +09002095 err = id;
2096 goto out;
2097 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002098
Marios Makassikis79caa962021-05-06 11:38:35 +09002099 rsp->hdr.Status = STATUS_SUCCESS;
Namjae Jeone2f34482021-03-16 10:49:09 +09002100 rsp->StructureSize = cpu_to_le16(89);
2101 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2102 rsp->Reserved = 0;
2103 rsp->CreateAction = cpu_to_le32(FILE_OPENED);
2104
2105 rsp->CreationTime = cpu_to_le64(0);
2106 rsp->LastAccessTime = cpu_to_le64(0);
2107 rsp->ChangeTime = cpu_to_le64(0);
2108 rsp->AllocationSize = cpu_to_le64(0);
2109 rsp->EndofFile = cpu_to_le64(0);
2110 rsp->FileAttributes = ATTR_NORMAL_LE;
2111 rsp->Reserved2 = 0;
2112 rsp->VolatileFileId = cpu_to_le64(id);
2113 rsp->PersistentFileId = 0;
2114 rsp->CreateContextsOffset = 0;
2115 rsp->CreateContextsLength = 0;
2116
Namjae Jeoncb451722021-11-03 08:08:44 +09002117 inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/
Namjae Jeone2f34482021-03-16 10:49:09 +09002118 kfree(name);
2119 return 0;
2120
2121out:
Marios Makassikis79caa962021-05-06 11:38:35 +09002122 switch (err) {
2123 case -EINVAL:
2124 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2125 break;
2126 case -ENOSPC:
2127 case -ENOMEM:
2128 rsp->hdr.Status = STATUS_NO_MEMORY;
2129 break;
2130 }
2131
2132 if (!IS_ERR(name))
2133 kfree(name);
2134
Namjae Jeone2f34482021-03-16 10:49:09 +09002135 smb2_set_err_rsp(work);
2136 return err;
2137}
2138
Namjae Jeone2f34482021-03-16 10:49:09 +09002139/**
2140 * smb2_set_ea() - handler for setting extended attributes using set
2141 * info command
2142 * @eabuf: set info command buffer
Namjae Jeon9496e262021-09-29 15:41:48 +09002143 * @buf_len: set info command buffer length
Namjae Jeone2f34482021-03-16 10:49:09 +09002144 * @path: dentry path for get ea
2145 *
2146 * Return: 0 on success, otherwise error
2147 */
Namjae Jeon9496e262021-09-29 15:41:48 +09002148static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len,
2149 struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002150{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002151 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002152 char *attr_name = NULL, *value;
2153 int rc = 0;
Namjae Jeon9496e262021-09-29 15:41:48 +09002154 unsigned int next = 0;
2155
2156 if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength +
2157 le16_to_cpu(eabuf->EaValueLength))
2158 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002159
2160 attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL);
2161 if (!attr_name)
2162 return -ENOMEM;
2163
2164 do {
2165 if (!eabuf->EaNameLength)
2166 goto next;
2167
2168 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002169 "name : <%s>, name_len : %u, value_len : %u, next : %u\n",
2170 eabuf->name, eabuf->EaNameLength,
2171 le16_to_cpu(eabuf->EaValueLength),
2172 le32_to_cpu(eabuf->NextEntryOffset));
Namjae Jeone2f34482021-03-16 10:49:09 +09002173
2174 if (eabuf->EaNameLength >
Namjae Jeon070fb212021-05-26 17:57:12 +09002175 (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002176 rc = -EINVAL;
2177 break;
2178 }
2179
2180 memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2181 memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002182 eabuf->EaNameLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09002183 attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0';
2184 value = (char *)&eabuf->name + eabuf->EaNameLength + 1;
2185
2186 if (!eabuf->EaValueLength) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002187 rc = ksmbd_vfs_casexattr_len(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002188 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002189 attr_name,
2190 XATTR_USER_PREFIX_LEN +
2191 eabuf->EaNameLength);
2192
2193 /* delete the EA only when it exits */
2194 if (rc > 0) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002195 rc = ksmbd_vfs_remove_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002196 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002197 attr_name);
2198
2199 if (rc < 0) {
2200 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002201 "remove xattr failed(%d)\n",
2202 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002203 break;
2204 }
2205 }
2206
2207 /* if the EA doesn't exist, just do nothing. */
2208 rc = 0;
2209 } else {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002210 rc = ksmbd_vfs_setxattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002211 path->dentry, attr_name, value,
Namjae Jeon070fb212021-05-26 17:57:12 +09002212 le16_to_cpu(eabuf->EaValueLength), 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002213 if (rc < 0) {
2214 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002215 "ksmbd_vfs_setxattr is failed(%d)\n",
2216 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002217 break;
2218 }
2219 }
2220
2221next:
2222 next = le32_to_cpu(eabuf->NextEntryOffset);
Namjae Jeon9496e262021-09-29 15:41:48 +09002223 if (next == 0 || buf_len < next)
2224 break;
2225 buf_len -= next;
Namjae Jeone2f34482021-03-16 10:49:09 +09002226 eabuf = (struct smb2_ea_info *)((char *)eabuf + next);
Namjae Jeon9496e262021-09-29 15:41:48 +09002227 if (next < (u32)eabuf->EaNameLength + le16_to_cpu(eabuf->EaValueLength))
2228 break;
2229
Namjae Jeone2f34482021-03-16 10:49:09 +09002230 } while (next != 0);
2231
2232 kfree(attr_name);
2233 return rc;
2234}
2235
Namjae Jeone2f34482021-03-16 10:49:09 +09002236static noinline int smb2_set_stream_name_xattr(struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002237 struct ksmbd_file *fp,
2238 char *stream_name, int s_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09002239{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002240 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002241 size_t xattr_stream_size;
2242 char *xattr_stream_name;
2243 int rc;
2244
2245 rc = ksmbd_vfs_xattr_stream_name(stream_name,
2246 &xattr_stream_name,
2247 &xattr_stream_size,
2248 s_type);
2249 if (rc)
2250 return rc;
2251
2252 fp->stream.name = xattr_stream_name;
2253 fp->stream.size = xattr_stream_size;
2254
2255 /* Check if there is stream prefix in xattr space */
Hyunchul Lee465d7202021-07-03 12:10:36 +09002256 rc = ksmbd_vfs_casexattr_len(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002257 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002258 xattr_stream_name,
2259 xattr_stream_size);
2260 if (rc >= 0)
2261 return 0;
2262
2263 if (fp->cdoption == FILE_OPEN_LE) {
2264 ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc);
2265 return -EBADF;
2266 }
2267
Hyunchul Lee465d7202021-07-03 12:10:36 +09002268 rc = ksmbd_vfs_setxattr(user_ns, path->dentry,
2269 xattr_stream_name, NULL, 0, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002270 if (rc < 0)
Namjae Jeonbde16942021-06-28 15:23:19 +09002271 pr_err("Failed to store XATTR stream name :%d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002272 return 0;
2273}
2274
Hyunchul Leeef24c962021-06-30 18:25:52 +09002275static int smb2_remove_smb_xattrs(struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002276{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002277 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002278 char *name, *xattr_list = NULL;
2279 ssize_t xattr_list_len;
2280 int err = 0;
2281
Hyunchul Leeef24c962021-06-30 18:25:52 +09002282 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002283 if (xattr_list_len < 0) {
2284 goto out;
2285 } else if (!xattr_list_len) {
2286 ksmbd_debug(SMB, "empty xattr in the file\n");
2287 goto out;
2288 }
2289
2290 for (name = xattr_list; name - xattr_list < xattr_list_len;
2291 name += strlen(name) + 1) {
2292 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
2293
2294 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002295 strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX,
2296 DOS_ATTRIBUTE_PREFIX_LEN) &&
2297 strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09002298 continue;
2299
Hyunchul Lee465d7202021-07-03 12:10:36 +09002300 err = ksmbd_vfs_remove_xattr(user_ns, path->dentry, name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002301 if (err)
2302 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
2303 }
2304out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09002305 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002306 return err;
2307}
2308
2309static int smb2_create_truncate(struct path *path)
2310{
2311 int rc = vfs_truncate(path, 0);
2312
2313 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002314 pr_err("vfs_truncate failed, rc %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002315 return rc;
2316 }
2317
Hyunchul Leeef24c962021-06-30 18:25:52 +09002318 rc = smb2_remove_smb_xattrs(path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002319 if (rc == -EOPNOTSUPP)
2320 rc = 0;
2321 if (rc)
2322 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002323 "ksmbd_truncate_stream_name_xattr failed, rc %d\n",
2324 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002325 return rc;
2326}
2327
Namjae Jeon64b39f42021-03-30 14:25:35 +09002328static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002329 struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002330{
2331 struct xattr_dos_attrib da = {0};
2332 int rc;
2333
2334 if (!test_share_config_flag(tcon->share_conf,
2335 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
2336 return;
2337
2338 da.version = 4;
2339 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
2340 da.itime = da.create_time = fp->create_time;
2341 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
2342 XATTR_DOSINFO_ITIME;
2343
Hyunchul Leeaf349832021-06-30 18:25:53 +09002344 rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt),
2345 path->dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09002346 if (rc)
2347 ksmbd_debug(SMB, "failed to store file attribute into xattr\n");
2348}
2349
2350static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon,
Namjae Jeon070fb212021-05-26 17:57:12 +09002351 struct path *path, struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002352{
2353 struct xattr_dos_attrib da;
2354 int rc;
2355
2356 fp->f_ci->m_fattr &= ~(ATTR_HIDDEN_LE | ATTR_SYSTEM_LE);
2357
2358 /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */
2359 if (!test_share_config_flag(tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002360 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
Namjae Jeone2f34482021-03-16 10:49:09 +09002361 return;
2362
Hyunchul Leeaf349832021-06-30 18:25:53 +09002363 rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_user_ns(path->mnt),
2364 path->dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09002365 if (rc > 0) {
2366 fp->f_ci->m_fattr = cpu_to_le32(da.attr);
2367 fp->create_time = da.create_time;
2368 fp->itime = da.itime;
2369 }
2370}
2371
Namjae Jeon64b39f42021-03-30 14:25:35 +09002372static int smb2_creat(struct ksmbd_work *work, struct path *path, char *name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002373 int open_flags, umode_t posix_mode, bool is_dir)
Namjae Jeone2f34482021-03-16 10:49:09 +09002374{
2375 struct ksmbd_tree_connect *tcon = work->tcon;
2376 struct ksmbd_share_config *share = tcon->share_conf;
2377 umode_t mode;
2378 int rc;
2379
2380 if (!(open_flags & O_CREAT))
2381 return -EBADF;
2382
2383 ksmbd_debug(SMB, "file does not exist, so creating\n");
2384 if (is_dir == true) {
2385 ksmbd_debug(SMB, "creating directory\n");
2386
2387 mode = share_config_directory_mode(share, posix_mode);
2388 rc = ksmbd_vfs_mkdir(work, name, mode);
2389 if (rc)
2390 return rc;
2391 } else {
2392 ksmbd_debug(SMB, "creating regular file\n");
2393
2394 mode = share_config_create_mode(share, posix_mode);
2395 rc = ksmbd_vfs_create(work, name, mode);
2396 if (rc)
2397 return rc;
2398 }
2399
Hyunchul Lee265fd192021-09-25 00:06:16 +09002400 rc = ksmbd_vfs_kern_path(work, name, 0, path, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002401 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002402 pr_err("cannot get linux path (%s), err = %d\n",
2403 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002404 return rc;
2405 }
2406 return 0;
2407}
2408
2409static int smb2_create_sd_buffer(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09002410 struct smb2_create_req *req,
Hyunchul Leeef24c962021-06-30 18:25:52 +09002411 struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002412{
2413 struct create_context *context;
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002414 struct create_sd_buf_req *sd_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09002415
2416 if (!req->CreateContextsOffset)
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002417 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09002418
2419 /* Parse SD BUFFER create contexts */
2420 context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER);
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002421 if (!context)
2422 return -ENOENT;
2423 else if (IS_ERR(context))
2424 return PTR_ERR(context);
Namjae Jeone2f34482021-03-16 10:49:09 +09002425
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002426 ksmbd_debug(SMB,
2427 "Set ACLs using SMB2_CREATE_SD_BUFFER context\n");
2428 sd_buf = (struct create_sd_buf_req *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002429 if (le16_to_cpu(context->DataOffset) +
2430 le32_to_cpu(context->DataLength) <
2431 sizeof(struct create_sd_buf_req))
2432 return -EINVAL;
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002433 return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd,
2434 le32_to_cpu(sd_buf->ccontext.DataLength), true);
Namjae Jeone2f34482021-03-16 10:49:09 +09002435}
2436
Christian Brauner43205ca2021-08-23 17:13:50 +02002437static void ksmbd_acls_fattr(struct smb_fattr *fattr,
2438 struct user_namespace *mnt_userns,
2439 struct inode *inode)
Namjae Jeon3d47e542021-04-20 14:25:35 +09002440{
Christian Brauner43205ca2021-08-23 17:13:50 +02002441 fattr->cf_uid = i_uid_into_mnt(mnt_userns, inode);
2442 fattr->cf_gid = i_gid_into_mnt(mnt_userns, inode);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002443 fattr->cf_mode = inode->i_mode;
Namjae Jeon777cad12021-08-13 08:15:33 +09002444 fattr->cf_acls = NULL;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002445 fattr->cf_dacls = NULL;
2446
Namjae Jeon777cad12021-08-13 08:15:33 +09002447 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
2448 fattr->cf_acls = get_acl(inode, ACL_TYPE_ACCESS);
2449 if (S_ISDIR(inode->i_mode))
2450 fattr->cf_dacls = get_acl(inode, ACL_TYPE_DEFAULT);
2451 }
Namjae Jeon3d47e542021-04-20 14:25:35 +09002452}
2453
Namjae Jeone2f34482021-03-16 10:49:09 +09002454/**
2455 * smb2_open() - handler for smb file open request
2456 * @work: smb work containing request buffer
2457 *
2458 * Return: 0 on success, otherwise error
2459 */
2460int smb2_open(struct ksmbd_work *work)
2461{
2462 struct ksmbd_conn *conn = work->conn;
2463 struct ksmbd_session *sess = work->sess;
2464 struct ksmbd_tree_connect *tcon = work->tcon;
2465 struct smb2_create_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09002466 struct smb2_create_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09002467 struct path path;
2468 struct ksmbd_share_config *share = tcon->share_conf;
2469 struct ksmbd_file *fp = NULL;
2470 struct file *filp = NULL;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002471 struct user_namespace *user_ns = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002472 struct kstat stat;
2473 struct create_context *context;
2474 struct lease_ctx_info *lc = NULL;
2475 struct create_ea_buf_req *ea_buf = NULL;
2476 struct oplock_info *opinfo;
2477 __le32 *next_ptr = NULL;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002478 int req_op_level = 0, open_flags = 0, may_flags = 0, file_info = 0;
Hyunchul Lee265fd192021-09-25 00:06:16 +09002479 int rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002480 int contxt_cnt = 0, query_disk_id = 0;
2481 int maximal_access_ctxt = 0, posix_ctxt = 0;
2482 int s_type = 0;
2483 int next_off = 0;
2484 char *name = NULL;
2485 char *stream_name = NULL;
2486 bool file_present = false, created = false, already_permitted = false;
Namjae Jeone2f34482021-03-16 10:49:09 +09002487 int share_ret, need_truncate = 0;
2488 u64 time;
2489 umode_t posix_mode = 0;
2490 __le32 daccess, maximal_access = 0;
2491
Namjae Jeone2f34482021-03-16 10:49:09 +09002492 WORK_BUFFERS(work, req, rsp);
2493
2494 if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002495 (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002496 ksmbd_debug(SMB, "invalid flag in chained command\n");
2497 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2498 smb2_set_err_rsp(work);
2499 return -EINVAL;
2500 }
2501
2502 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
2503 ksmbd_debug(SMB, "IPC pipe create request\n");
2504 return create_smb2_pipe(work);
2505 }
2506
2507 if (req->NameLength) {
2508 if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002509 *(char *)req->Buffer == '\\') {
Namjae Jeonbde16942021-06-28 15:23:19 +09002510 pr_err("not allow directory name included leading slash\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002511 rc = -EINVAL;
2512 goto err_out1;
2513 }
2514
2515 name = smb2_get_name(share,
2516 req->Buffer,
2517 le16_to_cpu(req->NameLength),
2518 work->conn->local_nls);
2519 if (IS_ERR(name)) {
2520 rc = PTR_ERR(name);
2521 if (rc != -ENOMEM)
2522 rc = -ENOENT;
Dan Carpenter8b99f352021-08-02 08:14:03 +09002523 name = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002524 goto err_out1;
2525 }
2526
2527 ksmbd_debug(SMB, "converted name = %s\n", name);
2528 if (strchr(name, ':')) {
2529 if (!test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002530 KSMBD_SHARE_FLAG_STREAMS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002531 rc = -EBADF;
2532 goto err_out1;
2533 }
2534 rc = parse_stream_name(name, &stream_name, &s_type);
2535 if (rc < 0)
2536 goto err_out1;
2537 }
2538
2539 rc = ksmbd_validate_filename(name);
2540 if (rc < 0)
2541 goto err_out1;
2542
2543 if (ksmbd_share_veto_filename(share, name)) {
2544 rc = -ENOENT;
2545 ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002546 name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002547 goto err_out1;
2548 }
2549 } else {
Hyunchul Lee265fd192021-09-25 00:06:16 +09002550 name = kstrdup("", GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09002551 if (!name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002552 rc = -ENOMEM;
2553 goto err_out1;
2554 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002555 }
2556
2557 req_op_level = req->RequestedOplockLevel;
Namjae Jeon73f9dad2021-04-16 14:12:06 +09002558 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002559 lc = parse_lease_state(req);
Namjae Jeone2f34482021-03-16 10:49:09 +09002560
Namjae Jeon64b39f42021-03-30 14:25:35 +09002561 if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002562 pr_err("Invalid impersonationlevel : 0x%x\n",
2563 le32_to_cpu(req->ImpersonationLevel));
Namjae Jeone2f34482021-03-16 10:49:09 +09002564 rc = -EIO;
2565 rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL;
2566 goto err_out1;
2567 }
2568
2569 if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002570 pr_err("Invalid create options : 0x%x\n",
2571 le32_to_cpu(req->CreateOptions));
Namjae Jeone2f34482021-03-16 10:49:09 +09002572 rc = -EINVAL;
2573 goto err_out1;
2574 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09002575 if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002576 req->CreateOptions & FILE_RANDOM_ACCESS_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002577 req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE);
2578
Namjae Jeon070fb212021-05-26 17:57:12 +09002579 if (req->CreateOptions &
2580 (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION |
2581 FILE_RESERVE_OPFILTER_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002582 rc = -EOPNOTSUPP;
2583 goto err_out1;
2584 }
2585
2586 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2587 if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) {
2588 rc = -EINVAL;
2589 goto err_out1;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002590 } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002591 req->CreateOptions = ~(FILE_NO_COMPRESSION_LE);
Namjae Jeon64b39f42021-03-30 14:25:35 +09002592 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002593 }
2594 }
2595
2596 if (le32_to_cpu(req->CreateDisposition) >
Namjae Jeon070fb212021-05-26 17:57:12 +09002597 le32_to_cpu(FILE_OVERWRITE_IF_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002598 pr_err("Invalid create disposition : 0x%x\n",
2599 le32_to_cpu(req->CreateDisposition));
Namjae Jeone2f34482021-03-16 10:49:09 +09002600 rc = -EINVAL;
2601 goto err_out1;
2602 }
2603
2604 if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002605 pr_err("Invalid desired access : 0x%x\n",
2606 le32_to_cpu(req->DesiredAccess));
Namjae Jeone2f34482021-03-16 10:49:09 +09002607 rc = -EACCES;
2608 goto err_out1;
2609 }
2610
Namjae Jeon64b39f42021-03-30 14:25:35 +09002611 if (req->FileAttributes && !(req->FileAttributes & ATTR_MASK_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002612 pr_err("Invalid file attribute : 0x%x\n",
2613 le32_to_cpu(req->FileAttributes));
Namjae Jeone2f34482021-03-16 10:49:09 +09002614 rc = -EINVAL;
2615 goto err_out1;
2616 }
2617
2618 if (req->CreateContextsOffset) {
2619 /* Parse non-durable handle create contexts */
2620 context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002621 if (IS_ERR(context)) {
2622 rc = PTR_ERR(context);
2623 goto err_out1;
2624 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002625 ea_buf = (struct create_ea_buf_req *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002626 if (le16_to_cpu(context->DataOffset) +
2627 le32_to_cpu(context->DataLength) <
2628 sizeof(struct create_ea_buf_req)) {
2629 rc = -EINVAL;
2630 goto err_out1;
2631 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002632 if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) {
2633 rsp->hdr.Status = STATUS_ACCESS_DENIED;
2634 rc = -EACCES;
2635 goto err_out1;
2636 }
2637 }
2638
2639 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002640 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002641 if (IS_ERR(context)) {
2642 rc = PTR_ERR(context);
2643 goto err_out1;
2644 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002645 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002646 "get query maximal access context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002647 maximal_access_ctxt = 1;
2648 }
2649
2650 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002651 SMB2_CREATE_TIMEWARP_REQUEST);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002652 if (IS_ERR(context)) {
2653 rc = PTR_ERR(context);
2654 goto err_out1;
2655 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002656 ksmbd_debug(SMB, "get timewarp context\n");
2657 rc = -EBADF;
2658 goto err_out1;
2659 }
2660
2661 if (tcon->posix_extensions) {
2662 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002663 SMB2_CREATE_TAG_POSIX);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002664 if (IS_ERR(context)) {
2665 rc = PTR_ERR(context);
2666 goto err_out1;
2667 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002668 struct create_posix *posix =
2669 (struct create_posix *)context;
Hyunchul Lee8f771502021-09-24 22:22:22 +09002670 if (le16_to_cpu(context->DataOffset) +
2671 le32_to_cpu(context->DataLength) <
2672 sizeof(struct create_posix)) {
2673 rc = -EINVAL;
2674 goto err_out1;
2675 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002676 ksmbd_debug(SMB, "get posix context\n");
2677
2678 posix_mode = le32_to_cpu(posix->Mode);
2679 posix_ctxt = 1;
2680 }
2681 }
2682 }
2683
2684 if (ksmbd_override_fsids(work)) {
2685 rc = -ENOMEM;
2686 goto err_out1;
2687 }
2688
Hyunchul Lee265fd192021-09-25 00:06:16 +09002689 rc = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, 1);
Namjae Jeon4ea47792021-09-21 14:19:33 +09002690 if (!rc) {
2691 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002692 /*
2693 * If file exists with under flags, return access
2694 * denied error.
2695 */
2696 if (req->CreateDisposition == FILE_OVERWRITE_IF_LE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09002697 req->CreateDisposition == FILE_OPEN_IF_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002698 rc = -EACCES;
2699 path_put(&path);
2700 goto err_out;
2701 }
2702
Namjae Jeon64b39f42021-03-30 14:25:35 +09002703 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002704 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002705 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002706 rc = -EACCES;
2707 path_put(&path);
2708 goto err_out;
2709 }
Namjae Jeon4ea47792021-09-21 14:19:33 +09002710 } else if (d_is_symlink(path.dentry)) {
2711 rc = -EACCES;
2712 path_put(&path);
2713 goto err_out;
Namjae Jeone2f34482021-03-16 10:49:09 +09002714 }
2715 }
2716
2717 if (rc) {
Hyunchul Lee265fd192021-09-25 00:06:16 +09002718 if (rc != -ENOENT)
Namjae Jeone2f34482021-03-16 10:49:09 +09002719 goto err_out;
Namjae Jeone2f34482021-03-16 10:49:09 +09002720 ksmbd_debug(SMB, "can not get linux path for %s, rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002721 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002722 rc = 0;
2723 } else {
2724 file_present = true;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002725 user_ns = mnt_user_ns(path.mnt);
2726 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09002727 }
2728 if (stream_name) {
2729 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2730 if (s_type == DATA_STREAM) {
2731 rc = -EIO;
2732 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2733 }
2734 } else {
2735 if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) {
2736 rc = -EIO;
2737 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2738 }
2739 }
2740
2741 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002742 req->FileAttributes & ATTR_NORMAL_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002743 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2744 rc = -EIO;
2745 }
2746
2747 if (rc < 0)
2748 goto err_out;
2749 }
2750
Namjae Jeon64b39f42021-03-30 14:25:35 +09002751 if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE &&
2752 S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002753 ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002754 name, req->CreateOptions);
Namjae Jeone2f34482021-03-16 10:49:09 +09002755 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2756 rc = -EIO;
2757 goto err_out;
2758 }
2759
2760 if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002761 !(req->CreateDisposition == FILE_CREATE_LE) &&
2762 !S_ISDIR(stat.mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002763 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2764 rc = -EIO;
2765 goto err_out;
2766 }
2767
2768 if (!stream_name && file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002769 req->CreateDisposition == FILE_CREATE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002770 rc = -EEXIST;
2771 goto err_out;
2772 }
2773
Namjae Jeone2f34482021-03-16 10:49:09 +09002774 daccess = smb_map_generic_desired_access(req->DesiredAccess);
2775
2776 if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002777 rc = smb_check_perm_dacl(conn, &path, &daccess,
Namjae Jeon070fb212021-05-26 17:57:12 +09002778 sess->user->uid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002779 if (rc)
2780 goto err_out;
2781 }
2782
2783 if (daccess & FILE_MAXIMAL_ACCESS_LE) {
2784 if (!file_present) {
2785 daccess = cpu_to_le32(GENERIC_ALL_FLAGS);
2786 } else {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002787 rc = ksmbd_vfs_query_maximal_access(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002788 path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002789 &daccess);
2790 if (rc)
2791 goto err_out;
2792 already_permitted = true;
2793 }
2794 maximal_access = daccess;
2795 }
2796
Namjae Jeon070fb212021-05-26 17:57:12 +09002797 open_flags = smb2_create_open_flags(file_present, daccess,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002798 req->CreateDisposition,
2799 &may_flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09002800
2801 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
2802 if (open_flags & O_CREAT) {
2803 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002804 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002805 rc = -EACCES;
2806 goto err_out;
2807 }
2808 }
2809
2810 /*create file if not present */
2811 if (!file_present) {
2812 rc = smb2_creat(work, &path, name, open_flags, posix_mode,
Namjae Jeon070fb212021-05-26 17:57:12 +09002813 req->CreateOptions & FILE_DIRECTORY_FILE_LE);
Marios Makassikisd337a442021-07-27 09:24:51 +09002814 if (rc) {
2815 if (rc == -ENOENT) {
2816 rc = -EIO;
2817 rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND;
2818 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002819 goto err_out;
Marios Makassikisd337a442021-07-27 09:24:51 +09002820 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002821
2822 created = true;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002823 user_ns = mnt_user_ns(path.mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002824 if (ea_buf) {
Namjae Jeon9496e262021-09-29 15:41:48 +09002825 if (le32_to_cpu(ea_buf->ccontext.DataLength) <
2826 sizeof(struct smb2_ea_info)) {
2827 rc = -EINVAL;
2828 goto err_out;
2829 }
2830
2831 rc = smb2_set_ea(&ea_buf->ea,
2832 le32_to_cpu(ea_buf->ccontext.DataLength),
2833 &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002834 if (rc == -EOPNOTSUPP)
2835 rc = 0;
2836 else if (rc)
2837 goto err_out;
2838 }
2839 } else if (!already_permitted) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002840 /* FILE_READ_ATTRIBUTE is allowed without inode_permission,
2841 * because execute(search) permission on a parent directory,
2842 * is already granted.
2843 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09002844 if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002845 rc = inode_permission(user_ns,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002846 d_inode(path.dentry),
2847 may_flags);
Namjae Jeonff1d5722021-04-13 13:18:10 +09002848 if (rc)
Namjae Jeone2f34482021-03-16 10:49:09 +09002849 goto err_out;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002850
2851 if ((daccess & FILE_DELETE_LE) ||
2852 (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002853 rc = ksmbd_vfs_may_delete(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002854 path.dentry);
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002855 if (rc)
2856 goto err_out;
2857 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002858 }
2859 }
2860
2861 rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent));
2862 if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) {
2863 rc = -EBUSY;
2864 goto err_out;
2865 }
2866
2867 rc = 0;
2868 filp = dentry_open(&path, open_flags, current_cred());
2869 if (IS_ERR(filp)) {
2870 rc = PTR_ERR(filp);
Namjae Jeonbde16942021-06-28 15:23:19 +09002871 pr_err("dentry open for dir failed, rc %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002872 goto err_out;
2873 }
2874
2875 if (file_present) {
2876 if (!(open_flags & O_TRUNC))
2877 file_info = FILE_OPENED;
2878 else
2879 file_info = FILE_OVERWRITTEN;
2880
Namjae Jeon070fb212021-05-26 17:57:12 +09002881 if ((req->CreateDisposition & FILE_CREATE_MASK_LE) ==
2882 FILE_SUPERSEDE_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002883 file_info = FILE_SUPERSEDED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002884 } else if (open_flags & O_CREAT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002885 file_info = FILE_CREATED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002886 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002887
2888 ksmbd_vfs_set_fadvise(filp, req->CreateOptions);
2889
2890 /* Obtain Volatile-ID */
2891 fp = ksmbd_open_fd(work, filp);
2892 if (IS_ERR(fp)) {
2893 fput(filp);
2894 rc = PTR_ERR(fp);
2895 fp = NULL;
2896 goto err_out;
2897 }
2898
2899 /* Get Persistent-ID */
2900 ksmbd_open_durable_fd(fp);
Namjae Jeon38673692021-07-08 12:32:27 +09002901 if (!has_file_id(fp->persistent_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002902 rc = -ENOMEM;
2903 goto err_out;
2904 }
2905
2906 fp->filename = name;
2907 fp->cdoption = req->CreateDisposition;
2908 fp->daccess = daccess;
2909 fp->saccess = req->ShareAccess;
2910 fp->coption = req->CreateOptions;
2911
2912 /* Set default windows and posix acls if creating new file */
2913 if (created) {
2914 int posix_acl_rc;
Namjae Jeonfba08fa2021-04-15 10:29:39 +09002915 struct inode *inode = d_inode(path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09002916
Hyunchul Lee465d7202021-07-03 12:10:36 +09002917 posix_acl_rc = ksmbd_vfs_inherit_posix_acl(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002918 inode,
2919 d_inode(path.dentry->d_parent));
Namjae Jeone2f34482021-03-16 10:49:09 +09002920 if (posix_acl_rc)
2921 ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc);
2922
2923 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002924 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002925 rc = smb_inherit_dacl(conn, &path, sess->user->uid,
Namjae Jeon070fb212021-05-26 17:57:12 +09002926 sess->user->gid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002927 }
2928
2929 if (rc) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002930 rc = smb2_create_sd_buffer(work, req, &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002931 if (rc) {
2932 if (posix_acl_rc)
Hyunchul Lee465d7202021-07-03 12:10:36 +09002933 ksmbd_vfs_set_init_posix_acl(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002934 inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09002935
2936 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002937 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002938 struct smb_fattr fattr;
2939 struct smb_ntsd *pntsd;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002940 int pntsd_size, ace_num = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002941
Christian Brauner43205ca2021-08-23 17:13:50 +02002942 ksmbd_acls_fattr(&fattr, user_ns, inode);
Marios Makassikise6b10592021-04-15 10:24:56 +09002943 if (fattr.cf_acls)
2944 ace_num = fattr.cf_acls->a_count;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002945 if (fattr.cf_dacls)
2946 ace_num += fattr.cf_dacls->a_count;
Namjae Jeone2f34482021-03-16 10:49:09 +09002947
2948 pntsd = kmalloc(sizeof(struct smb_ntsd) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002949 sizeof(struct smb_sid) * 3 +
Namjae Jeone2f34482021-03-16 10:49:09 +09002950 sizeof(struct smb_acl) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002951 sizeof(struct smb_ace) * ace_num * 2,
Namjae Jeone2f34482021-03-16 10:49:09 +09002952 GFP_KERNEL);
2953 if (!pntsd)
2954 goto err_out;
2955
Hyunchul Lee465d7202021-07-03 12:10:36 +09002956 rc = build_sec_desc(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002957 pntsd, NULL,
Namjae Jeon070fb212021-05-26 17:57:12 +09002958 OWNER_SECINFO |
Hyunchul Leeaf349832021-06-30 18:25:53 +09002959 GROUP_SECINFO |
2960 DACL_SECINFO,
Namjae Jeon070fb212021-05-26 17:57:12 +09002961 &pntsd_size, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09002962 posix_acl_release(fattr.cf_acls);
2963 posix_acl_release(fattr.cf_dacls);
2964
2965 rc = ksmbd_vfs_set_sd_xattr(conn,
Hyunchul Lee465d7202021-07-03 12:10:36 +09002966 user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +09002967 path.dentry,
2968 pntsd,
2969 pntsd_size);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002970 kfree(pntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09002971 if (rc)
Namjae Jeonbde16942021-06-28 15:23:19 +09002972 pr_err("failed to store ntacl in xattr : %d\n",
2973 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002974 }
2975 }
2976 }
2977 rc = 0;
2978 }
2979
2980 if (stream_name) {
2981 rc = smb2_set_stream_name_xattr(&path,
2982 fp,
2983 stream_name,
2984 s_type);
2985 if (rc)
2986 goto err_out;
2987 file_info = FILE_CREATED;
2988 }
2989
2990 fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE |
2991 FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE));
Namjae Jeon64b39f42021-03-30 14:25:35 +09002992 if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC &&
2993 !fp->attrib_only && !stream_name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002994 smb_break_all_oplock(work, fp);
2995 need_truncate = 1;
2996 }
2997
2998 /* fp should be searchable through ksmbd_inode.m_fp_list
2999 * after daccess, saccess, attrib_only, and stream are
3000 * initialized.
3001 */
3002 write_lock(&fp->f_ci->m_lock);
3003 list_add(&fp->node, &fp->f_ci->m_fp_list);
3004 write_unlock(&fp->f_ci->m_lock);
3005
3006 rc = ksmbd_vfs_getattr(&path, &stat);
3007 if (rc) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09003008 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09003009 rc = 0;
3010 }
3011
3012 /* Check delete pending among previous fp before oplock break */
3013 if (ksmbd_inode_pending_delete(fp)) {
3014 rc = -EBUSY;
3015 goto err_out;
3016 }
3017
3018 share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp);
Namjae Jeon64b39f42021-03-30 14:25:35 +09003019 if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) ||
3020 (req_op_level == SMB2_OPLOCK_LEVEL_LEASE &&
3021 !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) {
Namjae Jeonab0b2632021-06-29 09:20:13 +09003022 if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003023 rc = share_ret;
3024 goto err_out;
3025 }
3026 } else {
3027 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) {
3028 req_op_level = smb2_map_lease_to_oplock(lc->req_state);
3029 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003030 "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n",
3031 name, req_op_level, lc->req_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09003032 rc = find_same_lease_key(sess, fp->f_ci, lc);
3033 if (rc)
3034 goto err_out;
3035 } else if (open_flags == O_RDONLY &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09003036 (req_op_level == SMB2_OPLOCK_LEVEL_BATCH ||
3037 req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
Namjae Jeone2f34482021-03-16 10:49:09 +09003038 req_op_level = SMB2_OPLOCK_LEVEL_II;
3039
3040 rc = smb_grant_oplock(work, req_op_level,
3041 fp->persistent_id, fp,
3042 le32_to_cpu(req->hdr.Id.SyncId.TreeId),
3043 lc, share_ret);
3044 if (rc < 0)
3045 goto err_out;
3046 }
3047
3048 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
3049 ksmbd_fd_set_delete_on_close(fp, file_info);
3050
3051 if (need_truncate) {
3052 rc = smb2_create_truncate(&path);
3053 if (rc)
3054 goto err_out;
3055 }
3056
3057 if (req->CreateContextsOffset) {
3058 struct create_alloc_size_req *az_req;
3059
Namjae Jeon070fb212021-05-26 17:57:12 +09003060 az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req,
3061 SMB2_CREATE_ALLOCATION_SIZE);
Namjae Jeonf19b3962021-07-13 09:59:34 +09003062 if (IS_ERR(az_req)) {
3063 rc = PTR_ERR(az_req);
3064 goto err_out;
3065 } else if (az_req) {
Hyunchul Lee8f771502021-09-24 22:22:22 +09003066 loff_t alloc_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09003067 int err;
3068
Hyunchul Lee8f771502021-09-24 22:22:22 +09003069 if (le16_to_cpu(az_req->ccontext.DataOffset) +
3070 le32_to_cpu(az_req->ccontext.DataLength) <
3071 sizeof(struct create_alloc_size_req)) {
3072 rc = -EINVAL;
3073 goto err_out;
3074 }
3075 alloc_size = le64_to_cpu(az_req->AllocationSize);
Namjae Jeone2f34482021-03-16 10:49:09 +09003076 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003077 "request smb2 create allocate size : %llu\n",
3078 alloc_size);
Namjae Jeone8c06192021-06-22 11:06:11 +09003079 smb_break_all_levII_oplock(work, fp, 1);
3080 err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
3081 alloc_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003082 if (err < 0)
3083 ksmbd_debug(SMB,
Namjae Jeone8c06192021-06-22 11:06:11 +09003084 "vfs_fallocate is failed : %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003085 err);
Namjae Jeone2f34482021-03-16 10:49:09 +09003086 }
3087
Namjae Jeon64b39f42021-03-30 14:25:35 +09003088 context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID);
Namjae Jeonf19b3962021-07-13 09:59:34 +09003089 if (IS_ERR(context)) {
3090 rc = PTR_ERR(context);
3091 goto err_out;
3092 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003093 ksmbd_debug(SMB, "get query on disk id context\n");
3094 query_disk_id = 1;
3095 }
3096 }
3097
3098 if (stat.result_mask & STATX_BTIME)
3099 fp->create_time = ksmbd_UnixTimeToNT(stat.btime);
3100 else
3101 fp->create_time = ksmbd_UnixTimeToNT(stat.ctime);
3102 if (req->FileAttributes || fp->f_ci->m_fattr == 0)
Namjae Jeon070fb212021-05-26 17:57:12 +09003103 fp->f_ci->m_fattr =
3104 cpu_to_le32(smb2_get_dos_mode(&stat, le32_to_cpu(req->FileAttributes)));
Namjae Jeone2f34482021-03-16 10:49:09 +09003105
3106 if (!created)
3107 smb2_update_xattrs(tcon, &path, fp);
3108 else
3109 smb2_new_xattrs(tcon, &path, fp);
3110
3111 memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE);
3112
Hyunchul Lee465d7202021-07-03 12:10:36 +09003113 generic_fillattr(user_ns, file_inode(fp->filp),
Hyunchul Leeaf349832021-06-30 18:25:53 +09003114 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09003115
3116 rsp->StructureSize = cpu_to_le16(89);
3117 rcu_read_lock();
3118 opinfo = rcu_dereference(fp->f_opinfo);
3119 rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0;
3120 rcu_read_unlock();
3121 rsp->Reserved = 0;
3122 rsp->CreateAction = cpu_to_le32(file_info);
3123 rsp->CreationTime = cpu_to_le64(fp->create_time);
3124 time = ksmbd_UnixTimeToNT(stat.atime);
3125 rsp->LastAccessTime = cpu_to_le64(time);
3126 time = ksmbd_UnixTimeToNT(stat.mtime);
3127 rsp->LastWriteTime = cpu_to_le64(time);
3128 time = ksmbd_UnixTimeToNT(stat.ctime);
3129 rsp->ChangeTime = cpu_to_le64(time);
3130 rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 :
3131 cpu_to_le64(stat.blocks << 9);
3132 rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
3133 rsp->FileAttributes = fp->f_ci->m_fattr;
3134
3135 rsp->Reserved2 = 0;
3136
3137 rsp->PersistentFileId = cpu_to_le64(fp->persistent_id);
3138 rsp->VolatileFileId = cpu_to_le64(fp->volatile_id);
3139
3140 rsp->CreateContextsOffset = 0;
3141 rsp->CreateContextsLength = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09003142 inc_rfc1001_len(work->response_buf, 88); /* StructureSize - 1*/
Namjae Jeone2f34482021-03-16 10:49:09 +09003143
3144 /* If lease is request send lease context response */
3145 if (opinfo && opinfo->is_lease) {
3146 struct create_context *lease_ccontext;
3147
3148 ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003149 name, opinfo->o_lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +09003150 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
3151
3152 lease_ccontext = (struct create_context *)rsp->Buffer;
3153 contxt_cnt++;
3154 create_lease_buf(rsp->Buffer, opinfo->o_lease);
3155 le32_add_cpu(&rsp->CreateContextsLength,
3156 conn->vals->create_lease_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003157 inc_rfc1001_len(work->response_buf,
3158 conn->vals->create_lease_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003159 next_ptr = &lease_ccontext->Next;
3160 next_off = conn->vals->create_lease_size;
3161 }
3162
Namjae Jeone2f34482021-03-16 10:49:09 +09003163 if (maximal_access_ctxt) {
3164 struct create_context *mxac_ccontext;
3165
3166 if (maximal_access == 0)
Hyunchul Lee465d7202021-07-03 12:10:36 +09003167 ksmbd_vfs_query_maximal_access(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09003168 path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09003169 &maximal_access);
3170 mxac_ccontext = (struct create_context *)(rsp->Buffer +
3171 le32_to_cpu(rsp->CreateContextsLength));
3172 contxt_cnt++;
3173 create_mxac_rsp_buf(rsp->Buffer +
3174 le32_to_cpu(rsp->CreateContextsLength),
3175 le32_to_cpu(maximal_access));
3176 le32_add_cpu(&rsp->CreateContextsLength,
3177 conn->vals->create_mxac_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003178 inc_rfc1001_len(work->response_buf,
3179 conn->vals->create_mxac_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003180 if (next_ptr)
3181 *next_ptr = cpu_to_le32(next_off);
3182 next_ptr = &mxac_ccontext->Next;
3183 next_off = conn->vals->create_mxac_size;
3184 }
3185
3186 if (query_disk_id) {
3187 struct create_context *disk_id_ccontext;
3188
3189 disk_id_ccontext = (struct create_context *)(rsp->Buffer +
3190 le32_to_cpu(rsp->CreateContextsLength));
3191 contxt_cnt++;
3192 create_disk_id_rsp_buf(rsp->Buffer +
3193 le32_to_cpu(rsp->CreateContextsLength),
3194 stat.ino, tcon->id);
3195 le32_add_cpu(&rsp->CreateContextsLength,
3196 conn->vals->create_disk_id_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003197 inc_rfc1001_len(work->response_buf,
3198 conn->vals->create_disk_id_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003199 if (next_ptr)
3200 *next_ptr = cpu_to_le32(next_off);
3201 next_ptr = &disk_id_ccontext->Next;
3202 next_off = conn->vals->create_disk_id_size;
3203 }
3204
3205 if (posix_ctxt) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003206 contxt_cnt++;
3207 create_posix_rsp_buf(rsp->Buffer +
3208 le32_to_cpu(rsp->CreateContextsLength),
3209 fp);
3210 le32_add_cpu(&rsp->CreateContextsLength,
3211 conn->vals->create_posix_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09003212 inc_rfc1001_len(work->response_buf,
3213 conn->vals->create_posix_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09003214 if (next_ptr)
3215 *next_ptr = cpu_to_le32(next_off);
3216 }
3217
3218 if (contxt_cnt > 0) {
3219 rsp->CreateContextsOffset =
Namjae Jeoncb451722021-11-03 08:08:44 +09003220 cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer));
Namjae Jeone2f34482021-03-16 10:49:09 +09003221 }
3222
3223err_out:
3224 if (file_present || created)
3225 path_put(&path);
3226 ksmbd_revert_fsids(work);
3227err_out1:
3228 if (rc) {
3229 if (rc == -EINVAL)
3230 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3231 else if (rc == -EOPNOTSUPP)
3232 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Hyunchul Lee265fd192021-09-25 00:06:16 +09003233 else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV)
Namjae Jeone2f34482021-03-16 10:49:09 +09003234 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3235 else if (rc == -ENOENT)
3236 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
3237 else if (rc == -EPERM)
3238 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
3239 else if (rc == -EBUSY)
3240 rsp->hdr.Status = STATUS_DELETE_PENDING;
3241 else if (rc == -EBADF)
3242 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
3243 else if (rc == -ENOEXEC)
3244 rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID;
3245 else if (rc == -ENXIO)
3246 rsp->hdr.Status = STATUS_NO_SUCH_DEVICE;
3247 else if (rc == -EEXIST)
3248 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
3249 else if (rc == -EMFILE)
3250 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
3251 if (!rsp->hdr.Status)
3252 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3253
3254 if (!fp || !fp->filename)
3255 kfree(name);
3256 if (fp)
3257 ksmbd_fd_put(work, fp);
3258 smb2_set_err_rsp(work);
3259 ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status);
3260 }
3261
3262 kfree(lc);
3263
3264 return 0;
3265}
3266
3267static int readdir_info_level_struct_sz(int info_level)
3268{
3269 switch (info_level) {
3270 case FILE_FULL_DIRECTORY_INFORMATION:
3271 return sizeof(struct file_full_directory_info);
3272 case FILE_BOTH_DIRECTORY_INFORMATION:
3273 return sizeof(struct file_both_directory_info);
3274 case FILE_DIRECTORY_INFORMATION:
3275 return sizeof(struct file_directory_info);
3276 case FILE_NAMES_INFORMATION:
3277 return sizeof(struct file_names_info);
3278 case FILEID_FULL_DIRECTORY_INFORMATION:
3279 return sizeof(struct file_id_full_dir_info);
3280 case FILEID_BOTH_DIRECTORY_INFORMATION:
3281 return sizeof(struct file_id_both_directory_info);
3282 case SMB_FIND_FILE_POSIX_INFO:
3283 return sizeof(struct smb2_posix_info);
3284 default:
3285 return -EOPNOTSUPP;
3286 }
3287}
3288
3289static int dentry_name(struct ksmbd_dir_info *d_info, int info_level)
3290{
3291 switch (info_level) {
3292 case FILE_FULL_DIRECTORY_INFORMATION:
3293 {
3294 struct file_full_directory_info *ffdinfo;
3295
3296 ffdinfo = (struct file_full_directory_info *)d_info->rptr;
3297 d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset);
3298 d_info->name = ffdinfo->FileName;
3299 d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength);
3300 return 0;
3301 }
3302 case FILE_BOTH_DIRECTORY_INFORMATION:
3303 {
3304 struct file_both_directory_info *fbdinfo;
3305
3306 fbdinfo = (struct file_both_directory_info *)d_info->rptr;
3307 d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset);
3308 d_info->name = fbdinfo->FileName;
3309 d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength);
3310 return 0;
3311 }
3312 case FILE_DIRECTORY_INFORMATION:
3313 {
3314 struct file_directory_info *fdinfo;
3315
3316 fdinfo = (struct file_directory_info *)d_info->rptr;
3317 d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset);
3318 d_info->name = fdinfo->FileName;
3319 d_info->name_len = le32_to_cpu(fdinfo->FileNameLength);
3320 return 0;
3321 }
3322 case FILE_NAMES_INFORMATION:
3323 {
3324 struct file_names_info *fninfo;
3325
3326 fninfo = (struct file_names_info *)d_info->rptr;
3327 d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset);
3328 d_info->name = fninfo->FileName;
3329 d_info->name_len = le32_to_cpu(fninfo->FileNameLength);
3330 return 0;
3331 }
3332 case FILEID_FULL_DIRECTORY_INFORMATION:
3333 {
3334 struct file_id_full_dir_info *dinfo;
3335
3336 dinfo = (struct file_id_full_dir_info *)d_info->rptr;
3337 d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset);
3338 d_info->name = dinfo->FileName;
3339 d_info->name_len = le32_to_cpu(dinfo->FileNameLength);
3340 return 0;
3341 }
3342 case FILEID_BOTH_DIRECTORY_INFORMATION:
3343 {
3344 struct file_id_both_directory_info *fibdinfo;
3345
3346 fibdinfo = (struct file_id_both_directory_info *)d_info->rptr;
3347 d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset);
3348 d_info->name = fibdinfo->FileName;
3349 d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength);
3350 return 0;
3351 }
3352 case SMB_FIND_FILE_POSIX_INFO:
3353 {
3354 struct smb2_posix_info *posix_info;
3355
3356 posix_info = (struct smb2_posix_info *)d_info->rptr;
3357 d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset);
3358 d_info->name = posix_info->name;
3359 d_info->name_len = le32_to_cpu(posix_info->name_len);
3360 return 0;
3361 }
3362 default:
3363 return -EINVAL;
3364 }
3365}
3366
3367/**
3368 * smb2_populate_readdir_entry() - encode directory entry in smb2 response
3369 * buffer
3370 * @conn: connection instance
3371 * @info_level: smb information level
3372 * @d_info: structure included variables for query dir
Hyunchul Leeaf349832021-06-30 18:25:53 +09003373 * @user_ns: user namespace
Namjae Jeone2f34482021-03-16 10:49:09 +09003374 * @ksmbd_kstat: ksmbd wrapper of dirent stat information
3375 *
3376 * if directory has many entries, find first can't read it fully.
3377 * find next might be called multiple times to read remaining dir entries
3378 *
3379 * Return: 0 on success, otherwise error
3380 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003381static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level,
Namjae Jeon070fb212021-05-26 17:57:12 +09003382 struct ksmbd_dir_info *d_info,
3383 struct ksmbd_kstat *ksmbd_kstat)
Namjae Jeone2f34482021-03-16 10:49:09 +09003384{
3385 int next_entry_offset = 0;
3386 char *conv_name;
3387 int conv_len;
3388 void *kstat;
Namjae Jeondac0ec62021-07-07 14:57:24 +09003389 int struct_sz, rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09003390
3391 conv_name = ksmbd_convert_dir_info_name(d_info,
3392 conn->local_nls,
3393 &conv_len);
3394 if (!conv_name)
3395 return -ENOMEM;
3396
3397 /* Somehow the name has only terminating NULL bytes */
3398 if (conv_len < 0) {
Namjae Jeondac0ec62021-07-07 14:57:24 +09003399 rc = -EINVAL;
3400 goto free_conv_name;
Namjae Jeone2f34482021-03-16 10:49:09 +09003401 }
3402
3403 struct_sz = readdir_info_level_struct_sz(info_level);
3404 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3405 KSMBD_DIR_INFO_ALIGNMENT);
3406
3407 if (next_entry_offset > d_info->out_buf_len) {
3408 d_info->out_buf_len = 0;
Namjae Jeondac0ec62021-07-07 14:57:24 +09003409 rc = -ENOSPC;
3410 goto free_conv_name;
Namjae Jeone2f34482021-03-16 10:49:09 +09003411 }
3412
3413 kstat = d_info->wptr;
3414 if (info_level != FILE_NAMES_INFORMATION)
3415 kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat);
3416
3417 switch (info_level) {
3418 case FILE_FULL_DIRECTORY_INFORMATION:
3419 {
3420 struct file_full_directory_info *ffdinfo;
3421
3422 ffdinfo = (struct file_full_directory_info *)kstat;
3423 ffdinfo->FileNameLength = cpu_to_le32(conv_len);
3424 ffdinfo->EaSize =
3425 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3426 if (ffdinfo->EaSize)
3427 ffdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3428 if (d_info->hide_dot_file && d_info->name[0] == '.')
3429 ffdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3430 memcpy(ffdinfo->FileName, conv_name, conv_len);
3431 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3432 break;
3433 }
3434 case FILE_BOTH_DIRECTORY_INFORMATION:
3435 {
3436 struct file_both_directory_info *fbdinfo;
3437
3438 fbdinfo = (struct file_both_directory_info *)kstat;
3439 fbdinfo->FileNameLength = cpu_to_le32(conv_len);
3440 fbdinfo->EaSize =
3441 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3442 if (fbdinfo->EaSize)
3443 fbdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3444 fbdinfo->ShortNameLength = 0;
3445 fbdinfo->Reserved = 0;
3446 if (d_info->hide_dot_file && d_info->name[0] == '.')
3447 fbdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3448 memcpy(fbdinfo->FileName, conv_name, conv_len);
3449 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3450 break;
3451 }
3452 case FILE_DIRECTORY_INFORMATION:
3453 {
3454 struct file_directory_info *fdinfo;
3455
3456 fdinfo = (struct file_directory_info *)kstat;
3457 fdinfo->FileNameLength = cpu_to_le32(conv_len);
3458 if (d_info->hide_dot_file && d_info->name[0] == '.')
3459 fdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3460 memcpy(fdinfo->FileName, conv_name, conv_len);
3461 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3462 break;
3463 }
3464 case FILE_NAMES_INFORMATION:
3465 {
3466 struct file_names_info *fninfo;
3467
3468 fninfo = (struct file_names_info *)kstat;
3469 fninfo->FileNameLength = cpu_to_le32(conv_len);
3470 memcpy(fninfo->FileName, conv_name, conv_len);
3471 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3472 break;
3473 }
3474 case FILEID_FULL_DIRECTORY_INFORMATION:
3475 {
3476 struct file_id_full_dir_info *dinfo;
3477
3478 dinfo = (struct file_id_full_dir_info *)kstat;
3479 dinfo->FileNameLength = cpu_to_le32(conv_len);
3480 dinfo->EaSize =
3481 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3482 if (dinfo->EaSize)
3483 dinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3484 dinfo->Reserved = 0;
3485 dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3486 if (d_info->hide_dot_file && d_info->name[0] == '.')
3487 dinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3488 memcpy(dinfo->FileName, conv_name, conv_len);
3489 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3490 break;
3491 }
3492 case FILEID_BOTH_DIRECTORY_INFORMATION:
3493 {
3494 struct file_id_both_directory_info *fibdinfo;
3495
3496 fibdinfo = (struct file_id_both_directory_info *)kstat;
3497 fibdinfo->FileNameLength = cpu_to_le32(conv_len);
3498 fibdinfo->EaSize =
3499 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3500 if (fibdinfo->EaSize)
3501 fibdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3502 fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3503 fibdinfo->ShortNameLength = 0;
3504 fibdinfo->Reserved = 0;
3505 fibdinfo->Reserved2 = cpu_to_le16(0);
3506 if (d_info->hide_dot_file && d_info->name[0] == '.')
3507 fibdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3508 memcpy(fibdinfo->FileName, conv_name, conv_len);
3509 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3510 break;
3511 }
3512 case SMB_FIND_FILE_POSIX_INFO:
3513 {
3514 struct smb2_posix_info *posix_info;
3515 u64 time;
3516
3517 posix_info = (struct smb2_posix_info *)kstat;
3518 posix_info->Ignored = 0;
3519 posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
3520 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
3521 posix_info->ChangeTime = cpu_to_le64(time);
3522 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime);
3523 posix_info->LastAccessTime = cpu_to_le64(time);
3524 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime);
3525 posix_info->LastWriteTime = cpu_to_le64(time);
3526 posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size);
3527 posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9);
3528 posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev);
3529 posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink);
3530 posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode);
3531 posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino);
3532 posix_info->DosAttributes =
3533 S_ISDIR(ksmbd_kstat->kstat->mode) ? ATTR_DIRECTORY_LE : ATTR_ARCHIVE_LE;
3534 if (d_info->hide_dot_file && d_info->name[0] == '.')
3535 posix_info->DosAttributes |= ATTR_HIDDEN_LE;
Christian Brauner475d6f92021-08-23 17:13:48 +02003536 id_to_sid(from_kuid_munged(&init_user_ns, ksmbd_kstat->kstat->uid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003537 SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]);
Christian Brauner475d6f92021-08-23 17:13:48 +02003538 id_to_sid(from_kgid_munged(&init_user_ns, ksmbd_kstat->kstat->gid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003539 SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]);
Namjae Jeone2f34482021-03-16 10:49:09 +09003540 memcpy(posix_info->name, conv_name, conv_len);
3541 posix_info->name_len = cpu_to_le32(conv_len);
3542 posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset);
3543 break;
3544 }
3545
3546 } /* switch (info_level) */
3547
3548 d_info->last_entry_offset = d_info->data_count;
3549 d_info->data_count += next_entry_offset;
Marios Makassikise7735c82021-05-06 11:40:02 +09003550 d_info->out_buf_len -= next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003551 d_info->wptr += next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003552
3553 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003554 "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n",
3555 info_level, d_info->out_buf_len,
3556 next_entry_offset, d_info->data_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09003557
Namjae Jeondac0ec62021-07-07 14:57:24 +09003558free_conv_name:
3559 kfree(conv_name);
3560 return rc;
Namjae Jeone2f34482021-03-16 10:49:09 +09003561}
3562
3563struct smb2_query_dir_private {
3564 struct ksmbd_work *work;
3565 char *search_pattern;
3566 struct ksmbd_file *dir_fp;
3567
3568 struct ksmbd_dir_info *d_info;
3569 int info_level;
3570};
3571
3572static void lock_dir(struct ksmbd_file *dir_fp)
3573{
3574 struct dentry *dir = dir_fp->filp->f_path.dentry;
3575
3576 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
3577}
3578
3579static void unlock_dir(struct ksmbd_file *dir_fp)
3580{
3581 struct dentry *dir = dir_fp->filp->f_path.dentry;
3582
3583 inode_unlock(d_inode(dir));
3584}
3585
3586static int process_query_dir_entries(struct smb2_query_dir_private *priv)
3587{
Hyunchul Lee465d7202021-07-03 12:10:36 +09003588 struct user_namespace *user_ns = file_mnt_user_ns(priv->dir_fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003589 struct kstat kstat;
3590 struct ksmbd_kstat ksmbd_kstat;
3591 int rc;
3592 int i;
3593
3594 for (i = 0; i < priv->d_info->num_entry; i++) {
3595 struct dentry *dent;
3596
3597 if (dentry_name(priv->d_info, priv->info_level))
3598 return -EINVAL;
3599
3600 lock_dir(priv->dir_fp);
Christian Braunerda1e7ad2021-08-23 17:13:47 +02003601 dent = lookup_one(user_ns, priv->d_info->name,
3602 priv->dir_fp->filp->f_path.dentry,
3603 priv->d_info->name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09003604 unlock_dir(priv->dir_fp);
3605
3606 if (IS_ERR(dent)) {
3607 ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003608 priv->d_info->name,
3609 PTR_ERR(dent));
Namjae Jeone2f34482021-03-16 10:49:09 +09003610 continue;
3611 }
3612 if (unlikely(d_is_negative(dent))) {
3613 dput(dent);
3614 ksmbd_debug(SMB, "Negative dentry `%s'\n",
3615 priv->d_info->name);
3616 continue;
3617 }
3618
3619 ksmbd_kstat.kstat = &kstat;
3620 if (priv->info_level != FILE_NAMES_INFORMATION)
3621 ksmbd_vfs_fill_dentry_attrs(priv->work,
Hyunchul Lee465d7202021-07-03 12:10:36 +09003622 user_ns,
Namjae Jeone2f34482021-03-16 10:49:09 +09003623 dent,
3624 &ksmbd_kstat);
3625
3626 rc = smb2_populate_readdir_entry(priv->work->conn,
3627 priv->info_level,
3628 priv->d_info,
3629 &ksmbd_kstat);
3630 dput(dent);
3631 if (rc)
3632 return rc;
3633 }
3634 return 0;
3635}
3636
3637static int reserve_populate_dentry(struct ksmbd_dir_info *d_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09003638 int info_level)
Namjae Jeone2f34482021-03-16 10:49:09 +09003639{
3640 int struct_sz;
3641 int conv_len;
3642 int next_entry_offset;
3643
3644 struct_sz = readdir_info_level_struct_sz(info_level);
3645 if (struct_sz == -EOPNOTSUPP)
3646 return -EOPNOTSUPP;
3647
3648 conv_len = (d_info->name_len + 1) * 2;
3649 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3650 KSMBD_DIR_INFO_ALIGNMENT);
3651
3652 if (next_entry_offset > d_info->out_buf_len) {
3653 d_info->out_buf_len = 0;
3654 return -ENOSPC;
3655 }
3656
3657 switch (info_level) {
3658 case FILE_FULL_DIRECTORY_INFORMATION:
3659 {
3660 struct file_full_directory_info *ffdinfo;
3661
3662 ffdinfo = (struct file_full_directory_info *)d_info->wptr;
3663 memcpy(ffdinfo->FileName, d_info->name, d_info->name_len);
3664 ffdinfo->FileName[d_info->name_len] = 0x00;
3665 ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3666 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3667 break;
3668 }
3669 case FILE_BOTH_DIRECTORY_INFORMATION:
3670 {
3671 struct file_both_directory_info *fbdinfo;
3672
3673 fbdinfo = (struct file_both_directory_info *)d_info->wptr;
3674 memcpy(fbdinfo->FileName, d_info->name, d_info->name_len);
3675 fbdinfo->FileName[d_info->name_len] = 0x00;
3676 fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3677 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3678 break;
3679 }
3680 case FILE_DIRECTORY_INFORMATION:
3681 {
3682 struct file_directory_info *fdinfo;
3683
3684 fdinfo = (struct file_directory_info *)d_info->wptr;
3685 memcpy(fdinfo->FileName, d_info->name, d_info->name_len);
3686 fdinfo->FileName[d_info->name_len] = 0x00;
3687 fdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3688 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3689 break;
3690 }
3691 case FILE_NAMES_INFORMATION:
3692 {
3693 struct file_names_info *fninfo;
3694
3695 fninfo = (struct file_names_info *)d_info->wptr;
3696 memcpy(fninfo->FileName, d_info->name, d_info->name_len);
3697 fninfo->FileName[d_info->name_len] = 0x00;
3698 fninfo->FileNameLength = cpu_to_le32(d_info->name_len);
3699 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3700 break;
3701 }
3702 case FILEID_FULL_DIRECTORY_INFORMATION:
3703 {
3704 struct file_id_full_dir_info *dinfo;
3705
3706 dinfo = (struct file_id_full_dir_info *)d_info->wptr;
3707 memcpy(dinfo->FileName, d_info->name, d_info->name_len);
3708 dinfo->FileName[d_info->name_len] = 0x00;
3709 dinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3710 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3711 break;
3712 }
3713 case FILEID_BOTH_DIRECTORY_INFORMATION:
3714 {
3715 struct file_id_both_directory_info *fibdinfo;
3716
3717 fibdinfo = (struct file_id_both_directory_info *)d_info->wptr;
3718 memcpy(fibdinfo->FileName, d_info->name, d_info->name_len);
3719 fibdinfo->FileName[d_info->name_len] = 0x00;
3720 fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3721 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3722 break;
3723 }
3724 case SMB_FIND_FILE_POSIX_INFO:
3725 {
3726 struct smb2_posix_info *posix_info;
3727
3728 posix_info = (struct smb2_posix_info *)d_info->wptr;
3729 memcpy(posix_info->name, d_info->name, d_info->name_len);
3730 posix_info->name[d_info->name_len] = 0x00;
3731 posix_info->name_len = cpu_to_le32(d_info->name_len);
3732 posix_info->NextEntryOffset =
3733 cpu_to_le32(next_entry_offset);
3734 break;
3735 }
3736 } /* switch (info_level) */
3737
3738 d_info->num_entry++;
3739 d_info->out_buf_len -= next_entry_offset;
3740 d_info->wptr += next_entry_offset;
3741 return 0;
3742}
3743
Namjae Jeon64b39f42021-03-30 14:25:35 +09003744static int __query_dir(struct dir_context *ctx, const char *name, int namlen,
Namjae Jeon070fb212021-05-26 17:57:12 +09003745 loff_t offset, u64 ino, unsigned int d_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09003746{
3747 struct ksmbd_readdir_data *buf;
3748 struct smb2_query_dir_private *priv;
3749 struct ksmbd_dir_info *d_info;
3750 int rc;
3751
3752 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
3753 priv = buf->private;
3754 d_info = priv->d_info;
3755
3756 /* dot and dotdot entries are already reserved */
3757 if (!strcmp(".", name) || !strcmp("..", name))
3758 return 0;
3759 if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name))
3760 return 0;
Namjae Jeonb24c9332021-03-21 17:32:19 +09003761 if (!match_pattern(name, namlen, priv->search_pattern))
Namjae Jeone2f34482021-03-16 10:49:09 +09003762 return 0;
3763
3764 d_info->name = name;
3765 d_info->name_len = namlen;
3766 rc = reserve_populate_dentry(d_info, priv->info_level);
3767 if (rc)
3768 return rc;
3769 if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
3770 d_info->out_buf_len = 0;
3771 return 0;
3772 }
3773 return 0;
3774}
3775
3776static void restart_ctx(struct dir_context *ctx)
3777{
3778 ctx->pos = 0;
3779}
3780
3781static int verify_info_level(int info_level)
3782{
3783 switch (info_level) {
3784 case FILE_FULL_DIRECTORY_INFORMATION:
3785 case FILE_BOTH_DIRECTORY_INFORMATION:
3786 case FILE_DIRECTORY_INFORMATION:
3787 case FILE_NAMES_INFORMATION:
3788 case FILEID_FULL_DIRECTORY_INFORMATION:
3789 case FILEID_BOTH_DIRECTORY_INFORMATION:
3790 case SMB_FIND_FILE_POSIX_INFO:
3791 break;
3792 default:
3793 return -EOPNOTSUPP;
3794 }
3795
3796 return 0;
3797}
3798
Hyunchul Lee34061d62021-10-16 08:39:54 +09003799static int smb2_calc_max_out_buf_len(struct ksmbd_work *work,
3800 unsigned short hdr2_len,
3801 unsigned int out_buf_len)
3802{
3803 int free_len;
3804
3805 if (out_buf_len > work->conn->vals->max_trans_size)
3806 return -EINVAL;
3807
3808 free_len = (int)(work->response_sz -
3809 (get_rfc1002_len(work->response_buf) + 4)) -
3810 hdr2_len;
3811 if (free_len < 0)
3812 return -EINVAL;
3813
3814 return min_t(int, out_buf_len, free_len);
3815}
3816
Namjae Jeone2f34482021-03-16 10:49:09 +09003817int smb2_query_dir(struct ksmbd_work *work)
3818{
3819 struct ksmbd_conn *conn = work->conn;
3820 struct smb2_query_directory_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09003821 struct smb2_query_directory_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09003822 struct ksmbd_share_config *share = work->tcon->share_conf;
3823 struct ksmbd_file *dir_fp = NULL;
3824 struct ksmbd_dir_info d_info;
3825 int rc = 0;
3826 char *srch_ptr = NULL;
3827 unsigned char srch_flag;
3828 int buffer_sz;
3829 struct smb2_query_dir_private query_dir_private = {NULL, };
3830
Namjae Jeone2f34482021-03-16 10:49:09 +09003831 WORK_BUFFERS(work, req, rsp);
3832
3833 if (ksmbd_override_fsids(work)) {
3834 rsp->hdr.Status = STATUS_NO_MEMORY;
3835 smb2_set_err_rsp(work);
3836 return -ENOMEM;
3837 }
3838
3839 rc = verify_info_level(req->FileInformationClass);
3840 if (rc) {
3841 rc = -EFAULT;
3842 goto err_out2;
3843 }
3844
3845 dir_fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09003846 le64_to_cpu(req->VolatileFileId),
3847 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09003848 if (!dir_fp) {
3849 rc = -EBADF;
3850 goto err_out2;
3851 }
3852
3853 if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) ||
Hyunchul Leeaf349832021-06-30 18:25:53 +09003854 inode_permission(file_mnt_user_ns(dir_fp->filp),
3855 file_inode(dir_fp->filp),
Namjae Jeon070fb212021-05-26 17:57:12 +09003856 MAY_READ | MAY_EXEC)) {
Namjae Jeon493fa2f2021-06-29 09:22:16 +09003857 pr_err("no right to enumerate directory (%pd)\n",
3858 dir_fp->filp->f_path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003859 rc = -EACCES;
3860 goto err_out2;
3861 }
3862
3863 if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09003864 pr_err("can't do query dir for a file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09003865 rc = -EINVAL;
3866 goto err_out2;
3867 }
3868
3869 srch_flag = req->Flags;
3870 srch_ptr = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09003871 le16_to_cpu(req->FileNameLength), 1,
3872 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09003873 if (IS_ERR(srch_ptr)) {
3874 ksmbd_debug(SMB, "Search Pattern not found\n");
3875 rc = -EINVAL;
3876 goto err_out2;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003877 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003878 ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr);
Namjae Jeon64b39f42021-03-30 14:25:35 +09003879 }
Namjae Jeone2f34482021-03-16 10:49:09 +09003880
3881 ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename);
3882
3883 if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) {
3884 ksmbd_debug(SMB, "Restart directory scan\n");
3885 generic_file_llseek(dir_fp->filp, 0, SEEK_SET);
3886 restart_ctx(&dir_fp->readdir_data.ctx);
3887 }
3888
3889 memset(&d_info, 0, sizeof(struct ksmbd_dir_info));
3890 d_info.wptr = (char *)rsp->Buffer;
3891 d_info.rptr = (char *)rsp->Buffer;
Hyunchul Lee34061d62021-10-16 08:39:54 +09003892 d_info.out_buf_len =
3893 smb2_calc_max_out_buf_len(work, 8,
3894 le32_to_cpu(req->OutputBufferLength));
3895 if (d_info.out_buf_len < 0) {
3896 rc = -EINVAL;
3897 goto err_out;
3898 }
Namjae Jeone2f34482021-03-16 10:49:09 +09003899 d_info.flags = srch_flag;
3900
3901 /*
3902 * reserve dot and dotdot entries in head of buffer
3903 * in first response
3904 */
3905 rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass,
Namjae Jeon070fb212021-05-26 17:57:12 +09003906 dir_fp, &d_info, srch_ptr,
3907 smb2_populate_readdir_entry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003908 if (rc == -ENOSPC)
3909 rc = 0;
3910 else if (rc)
3911 goto err_out;
3912
3913 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES))
3914 d_info.hide_dot_file = true;
3915
3916 buffer_sz = d_info.out_buf_len;
3917 d_info.rptr = d_info.wptr;
3918 query_dir_private.work = work;
3919 query_dir_private.search_pattern = srch_ptr;
3920 query_dir_private.dir_fp = dir_fp;
3921 query_dir_private.d_info = &d_info;
3922 query_dir_private.info_level = req->FileInformationClass;
3923 dir_fp->readdir_data.private = &query_dir_private;
3924 set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir);
3925
Namjae Jeone8c06192021-06-22 11:06:11 +09003926 rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx);
Namjae Jeone2f34482021-03-16 10:49:09 +09003927 if (rc == 0)
3928 restart_ctx(&dir_fp->readdir_data.ctx);
3929 if (rc == -ENOSPC)
3930 rc = 0;
3931 if (rc)
3932 goto err_out;
3933
3934 d_info.wptr = d_info.rptr;
3935 d_info.out_buf_len = buffer_sz;
3936 rc = process_query_dir_entries(&query_dir_private);
3937 if (rc)
3938 goto err_out;
3939
3940 if (!d_info.data_count && d_info.out_buf_len >= 0) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09003941 if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003942 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003943 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003944 dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0;
3945 rsp->hdr.Status = STATUS_NO_MORE_FILES;
3946 }
3947 rsp->StructureSize = cpu_to_le16(9);
3948 rsp->OutputBufferOffset = cpu_to_le16(0);
3949 rsp->OutputBufferLength = cpu_to_le32(0);
3950 rsp->Buffer[0] = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09003951 inc_rfc1001_len(work->response_buf, 9);
Namjae Jeone2f34482021-03-16 10:49:09 +09003952 } else {
3953 ((struct file_directory_info *)
3954 ((char *)rsp->Buffer + d_info.last_entry_offset))
3955 ->NextEntryOffset = 0;
3956
3957 rsp->StructureSize = cpu_to_le16(9);
3958 rsp->OutputBufferOffset = cpu_to_le16(72);
3959 rsp->OutputBufferLength = cpu_to_le32(d_info.data_count);
Namjae Jeoncb451722021-11-03 08:08:44 +09003960 inc_rfc1001_len(work->response_buf, 8 + d_info.data_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09003961 }
3962
3963 kfree(srch_ptr);
3964 ksmbd_fd_put(work, dir_fp);
3965 ksmbd_revert_fsids(work);
3966 return 0;
3967
3968err_out:
Namjae Jeonbde16942021-06-28 15:23:19 +09003969 pr_err("error while processing smb2 query dir rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09003970 kfree(srch_ptr);
3971
3972err_out2:
3973 if (rc == -EINVAL)
3974 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3975 else if (rc == -EACCES)
3976 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3977 else if (rc == -ENOENT)
3978 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
3979 else if (rc == -EBADF)
3980 rsp->hdr.Status = STATUS_FILE_CLOSED;
3981 else if (rc == -ENOMEM)
3982 rsp->hdr.Status = STATUS_NO_MEMORY;
3983 else if (rc == -EFAULT)
3984 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
3985 if (!rsp->hdr.Status)
3986 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3987
3988 smb2_set_err_rsp(work);
3989 ksmbd_fd_put(work, dir_fp);
3990 ksmbd_revert_fsids(work);
3991 return 0;
3992}
3993
3994/**
3995 * buffer_check_err() - helper function to check buffer errors
3996 * @reqOutputBufferLength: max buffer length expected in command response
3997 * @rsp: query info response buffer contains output buffer length
3998 * @infoclass_size: query info class response buffer size
3999 *
4000 * Return: 0 on success, otherwise error
4001 */
4002static int buffer_check_err(int reqOutputBufferLength,
Namjae Jeoncb451722021-11-03 08:08:44 +09004003 struct smb2_query_info_rsp *rsp,
4004 void *rsp_org, int infoclass_size)
Namjae Jeone2f34482021-03-16 10:49:09 +09004005{
4006 if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) {
4007 if (reqOutputBufferLength < infoclass_size) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004008 pr_err("Invalid Buffer Size Requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004009 rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH;
Namjae Jeoncb451722021-11-03 08:08:44 +09004010 *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr));
Namjae Jeone2f34482021-03-16 10:49:09 +09004011 return -EINVAL;
4012 }
4013
4014 ksmbd_debug(SMB, "Buffer Overflow\n");
4015 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
Namjae Jeoncb451722021-11-03 08:08:44 +09004016 *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09004017 reqOutputBufferLength);
4018 rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09004019 }
4020 return 0;
4021}
4022
Namjae Jeoncb451722021-11-03 08:08:44 +09004023static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp,
4024 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004025{
4026 struct smb2_file_standard_info *sinfo;
4027
4028 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4029
4030 sinfo->AllocationSize = cpu_to_le64(4096);
4031 sinfo->EndOfFile = cpu_to_le64(0);
4032 sinfo->NumberOfLinks = cpu_to_le32(1);
4033 sinfo->DeletePending = 1;
4034 sinfo->Directory = 0;
4035 rsp->OutputBufferLength =
4036 cpu_to_le32(sizeof(struct smb2_file_standard_info));
Namjae Jeoncb451722021-11-03 08:08:44 +09004037 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_standard_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004038}
4039
Namjae Jeoncb451722021-11-03 08:08:44 +09004040static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num,
4041 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004042{
4043 struct smb2_file_internal_info *file_info;
4044
4045 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4046
4047 /* any unique number */
4048 file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63));
4049 rsp->OutputBufferLength =
4050 cpu_to_le32(sizeof(struct smb2_file_internal_info));
Namjae Jeoncb451722021-11-03 08:08:44 +09004051 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004052}
4053
Namjae Jeone2f34482021-03-16 10:49:09 +09004054static int smb2_get_info_file_pipe(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09004055 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004056 struct smb2_query_info_rsp *rsp,
4057 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004058{
Namjae Jeon64b39f42021-03-30 14:25:35 +09004059 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09004060 int rc;
4061
4062 /*
4063 * Windows can sometime send query file info request on
4064 * pipe without opening it, checking error condition here
4065 */
4066 id = le64_to_cpu(req->VolatileFileId);
4067 if (!ksmbd_session_rpc_method(sess, id))
4068 return -ENOENT;
4069
4070 ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004071 req->FileInfoClass, le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09004072
4073 switch (req->FileInfoClass) {
4074 case FILE_STANDARD_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004075 get_standard_info_pipe(rsp, rsp_org);
Namjae Jeone2f34482021-03-16 10:49:09 +09004076 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004077 rsp, rsp_org,
4078 FILE_STANDARD_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09004079 break;
4080 case FILE_INTERNAL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004081 get_internal_info_pipe(rsp, id, rsp_org);
Namjae Jeone2f34482021-03-16 10:49:09 +09004082 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004083 rsp, rsp_org,
4084 FILE_INTERNAL_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09004085 break;
4086 default:
4087 ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004088 req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09004089 rc = -EOPNOTSUPP;
4090 }
4091 return rc;
4092}
4093
4094/**
4095 * smb2_get_ea() - handler for smb2 get extended attribute command
4096 * @work: smb work containing query info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09004097 * @fp: ksmbd_file pointer
4098 * @req: get extended attribute request
4099 * @rsp: response buffer pointer
4100 * @rsp_org: base response buffer pointer in case of chained response
Namjae Jeone2f34482021-03-16 10:49:09 +09004101 *
4102 * Return: 0 on success, otherwise error
4103 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09004104static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004105 struct smb2_query_info_req *req,
4106 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004107{
4108 struct smb2_ea_info *eainfo, *prev_eainfo;
4109 char *name, *ptr, *xattr_list = NULL, *buf;
4110 int rc, name_len, value_len, xattr_list_len, idx;
4111 ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0;
4112 struct smb2_ea_info_req *ea_req = NULL;
4113 struct path *path;
Hyunchul Lee465d7202021-07-03 12:10:36 +09004114 struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09004115
4116 if (!(fp->daccess & FILE_READ_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004117 pr_err("Not permitted to read ext attr : 0x%x\n",
4118 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004119 return -EACCES;
4120 }
4121
4122 path = &fp->filp->f_path;
4123 /* single EA entry is requested with given user.* name */
Namjae Jeon64b39f42021-03-30 14:25:35 +09004124 if (req->InputBufferLength) {
Namjae Jeon6d562622021-09-18 18:45:12 +09004125 if (le32_to_cpu(req->InputBufferLength) <
4126 sizeof(struct smb2_ea_info_req))
4127 return -EINVAL;
4128
Namjae Jeone2f34482021-03-16 10:49:09 +09004129 ea_req = (struct smb2_ea_info_req *)req->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004130 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09004131 /* need to send all EAs, if no specific EA is requested*/
4132 if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY)
4133 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09004134 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n",
4135 le32_to_cpu(req->Flags));
Namjae Jeone2f34482021-03-16 10:49:09 +09004136 }
4137
Hyunchul Lee34061d62021-10-16 08:39:54 +09004138 buf_free_len =
4139 smb2_calc_max_out_buf_len(work, 8,
4140 le32_to_cpu(req->OutputBufferLength));
4141 if (buf_free_len < 0)
4142 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09004143
4144 rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4145 if (rc < 0) {
4146 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4147 goto out;
4148 } else if (!rc) { /* there is no EA in the file */
4149 ksmbd_debug(SMB, "no ea data in the file\n");
4150 goto done;
4151 }
4152 xattr_list_len = rc;
4153
4154 ptr = (char *)rsp->Buffer;
4155 eainfo = (struct smb2_ea_info *)ptr;
4156 prev_eainfo = eainfo;
4157 idx = 0;
4158
4159 while (idx < xattr_list_len) {
4160 name = xattr_list + idx;
4161 name_len = strlen(name);
4162
4163 ksmbd_debug(SMB, "%s, len %d\n", name, name_len);
4164 idx += name_len + 1;
4165
4166 /*
4167 * CIFS does not support EA other than user.* namespace,
4168 * still keep the framework generic, to list other attrs
4169 * in future.
4170 */
4171 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4172 continue;
4173
4174 if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004175 STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004176 continue;
4177
4178 if (req->InputBufferLength &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09004179 strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name,
4180 ea_req->EaNameLength))
Namjae Jeone2f34482021-03-16 10:49:09 +09004181 continue;
4182
4183 if (!strncmp(&name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004184 DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004185 continue;
4186
4187 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4188 name_len -= XATTR_USER_PREFIX_LEN;
4189
4190 ptr = (char *)(&eainfo->name + name_len + 1);
4191 buf_free_len -= (offsetof(struct smb2_ea_info, name) +
4192 name_len + 1);
4193 /* bailout if xattr can't fit in buf_free_len */
Hyunchul Lee465d7202021-07-03 12:10:36 +09004194 value_len = ksmbd_vfs_getxattr(user_ns, path->dentry,
4195 name, &buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004196 if (value_len <= 0) {
4197 rc = -ENOENT;
4198 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4199 goto out;
4200 }
4201
4202 buf_free_len -= value_len;
4203 if (buf_free_len < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09004204 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004205 break;
4206 }
4207
4208 memcpy(ptr, buf, value_len);
Namjae Jeon79f6b112021-04-02 12:47:14 +09004209 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004210
4211 ptr += value_len;
4212 eainfo->Flags = 0;
4213 eainfo->EaNameLength = name_len;
4214
Namjae Jeon64b39f42021-03-30 14:25:35 +09004215 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004216 memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN],
Namjae Jeon070fb212021-05-26 17:57:12 +09004217 name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09004218 else
4219 memcpy(eainfo->name, name, name_len);
4220
4221 eainfo->name[name_len] = '\0';
4222 eainfo->EaValueLength = cpu_to_le16(value_len);
4223 next_offset = offsetof(struct smb2_ea_info, name) +
4224 name_len + 1 + value_len;
4225
4226 /* align next xattr entry at 4 byte bundary */
4227 alignment_bytes = ((next_offset + 3) & ~3) - next_offset;
4228 if (alignment_bytes) {
4229 memset(ptr, '\0', alignment_bytes);
4230 ptr += alignment_bytes;
4231 next_offset += alignment_bytes;
4232 buf_free_len -= alignment_bytes;
4233 }
4234 eainfo->NextEntryOffset = cpu_to_le32(next_offset);
4235 prev_eainfo = eainfo;
4236 eainfo = (struct smb2_ea_info *)ptr;
4237 rsp_data_cnt += next_offset;
4238
4239 if (req->InputBufferLength) {
4240 ksmbd_debug(SMB, "single entry requested\n");
4241 break;
4242 }
4243 }
4244
4245 /* no more ea entries */
4246 prev_eainfo->NextEntryOffset = 0;
4247done:
4248 rc = 0;
4249 if (rsp_data_cnt == 0)
4250 rsp->hdr.Status = STATUS_NO_EAS_ON_FILE;
4251 rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt);
4252 inc_rfc1001_len(rsp_org, rsp_data_cnt);
4253out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004254 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004255 return rc;
4256}
4257
4258static void get_file_access_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004259 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004260{
4261 struct smb2_file_access_info *file_info;
4262
4263 file_info = (struct smb2_file_access_info *)rsp->Buffer;
4264 file_info->AccessFlags = fp->daccess;
4265 rsp->OutputBufferLength =
4266 cpu_to_le32(sizeof(struct smb2_file_access_info));
4267 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info));
4268}
4269
4270static int get_file_basic_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004271 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004272{
Namjae Jeon88d30052021-09-29 15:37:18 +09004273 struct smb2_file_basic_info *basic_info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004274 struct kstat stat;
4275 u64 time;
4276
4277 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004278 pr_err("no right to read the attributes : 0x%x\n",
4279 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004280 return -EACCES;
4281 }
4282
Namjae Jeon88d30052021-09-29 15:37:18 +09004283 basic_info = (struct smb2_file_basic_info *)rsp->Buffer;
Hyunchul Leeaf349832021-06-30 18:25:53 +09004284 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4285 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004286 basic_info->CreationTime = cpu_to_le64(fp->create_time);
4287 time = ksmbd_UnixTimeToNT(stat.atime);
4288 basic_info->LastAccessTime = cpu_to_le64(time);
4289 time = ksmbd_UnixTimeToNT(stat.mtime);
4290 basic_info->LastWriteTime = cpu_to_le64(time);
4291 time = ksmbd_UnixTimeToNT(stat.ctime);
4292 basic_info->ChangeTime = cpu_to_le64(time);
4293 basic_info->Attributes = fp->f_ci->m_fattr;
4294 basic_info->Pad1 = 0;
4295 rsp->OutputBufferLength =
Namjae Jeon88d30052021-09-29 15:37:18 +09004296 cpu_to_le32(sizeof(struct smb2_file_basic_info));
4297 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_basic_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004298 return 0;
4299}
4300
4301static unsigned long long get_allocation_size(struct inode *inode,
Namjae Jeon070fb212021-05-26 17:57:12 +09004302 struct kstat *stat)
Namjae Jeone2f34482021-03-16 10:49:09 +09004303{
4304 unsigned long long alloc_size = 0;
4305
4306 if (!S_ISDIR(stat->mode)) {
4307 if ((inode->i_blocks << 9) <= stat->size)
4308 alloc_size = stat->size;
4309 else
4310 alloc_size = inode->i_blocks << 9;
Namjae Jeone2f34482021-03-16 10:49:09 +09004311 }
4312
4313 return alloc_size;
4314}
4315
4316static void get_file_standard_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004317 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004318{
4319 struct smb2_file_standard_info *sinfo;
4320 unsigned int delete_pending;
4321 struct inode *inode;
4322 struct kstat stat;
4323
Namjae Jeonab0b2632021-06-29 09:20:13 +09004324 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004325 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004326
4327 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4328 delete_pending = ksmbd_inode_pending_delete(fp);
4329
4330 sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat));
4331 sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4332 sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending);
4333 sinfo->DeletePending = delete_pending;
4334 sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4335 rsp->OutputBufferLength =
4336 cpu_to_le32(sizeof(struct smb2_file_standard_info));
4337 inc_rfc1001_len(rsp_org,
4338 sizeof(struct smb2_file_standard_info));
4339}
4340
4341static void get_file_alignment_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004342 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004343{
4344 struct smb2_file_alignment_info *file_info;
4345
4346 file_info = (struct smb2_file_alignment_info *)rsp->Buffer;
4347 file_info->AlignmentRequirement = 0;
4348 rsp->OutputBufferLength =
4349 cpu_to_le32(sizeof(struct smb2_file_alignment_info));
4350 inc_rfc1001_len(rsp_org,
4351 sizeof(struct smb2_file_alignment_info));
4352}
4353
4354static int get_file_all_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004355 struct smb2_query_info_rsp *rsp,
4356 struct ksmbd_file *fp,
4357 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004358{
4359 struct ksmbd_conn *conn = work->conn;
4360 struct smb2_file_all_info *file_info;
4361 unsigned int delete_pending;
4362 struct inode *inode;
4363 struct kstat stat;
4364 int conv_len;
4365 char *filename;
4366 u64 time;
4367
4368 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4369 ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004370 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004371 return -EACCES;
4372 }
4373
Hyunchul Lee265fd192021-09-25 00:06:16 +09004374 filename = convert_to_nt_pathname(fp->filename);
Namjae Jeone2f34482021-03-16 10:49:09 +09004375 if (!filename)
4376 return -ENOMEM;
4377
Namjae Jeonab0b2632021-06-29 09:20:13 +09004378 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004379 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004380
4381 ksmbd_debug(SMB, "filename = %s\n", filename);
4382 delete_pending = ksmbd_inode_pending_delete(fp);
4383 file_info = (struct smb2_file_all_info *)rsp->Buffer;
4384
4385 file_info->CreationTime = cpu_to_le64(fp->create_time);
4386 time = ksmbd_UnixTimeToNT(stat.atime);
4387 file_info->LastAccessTime = cpu_to_le64(time);
4388 time = ksmbd_UnixTimeToNT(stat.mtime);
4389 file_info->LastWriteTime = cpu_to_le64(time);
4390 time = ksmbd_UnixTimeToNT(stat.ctime);
4391 file_info->ChangeTime = cpu_to_le64(time);
4392 file_info->Attributes = fp->f_ci->m_fattr;
4393 file_info->Pad1 = 0;
4394 file_info->AllocationSize =
4395 cpu_to_le64(get_allocation_size(inode, &stat));
4396 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4397 file_info->NumberOfLinks =
4398 cpu_to_le32(get_nlink(&stat) - delete_pending);
4399 file_info->DeletePending = delete_pending;
4400 file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4401 file_info->Pad2 = 0;
4402 file_info->IndexNumber = cpu_to_le64(stat.ino);
4403 file_info->EASize = 0;
4404 file_info->AccessFlags = fp->daccess;
4405 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4406 file_info->Mode = fp->coption;
4407 file_info->AlignmentRequirement = 0;
Namjae Jeon070fb212021-05-26 17:57:12 +09004408 conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename,
4409 PATH_MAX, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004410 conv_len *= 2;
4411 file_info->FileNameLength = cpu_to_le32(conv_len);
4412 rsp->OutputBufferLength =
4413 cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1);
4414 kfree(filename);
4415 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4416 return 0;
4417}
4418
4419static void get_file_alternate_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004420 struct smb2_query_info_rsp *rsp,
4421 struct ksmbd_file *fp,
4422 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004423{
4424 struct ksmbd_conn *conn = work->conn;
4425 struct smb2_file_alt_name_info *file_info;
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004426 struct dentry *dentry = fp->filp->f_path.dentry;
Namjae Jeone2f34482021-03-16 10:49:09 +09004427 int conv_len;
Namjae Jeone2f34482021-03-16 10:49:09 +09004428
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004429 spin_lock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004430 file_info = (struct smb2_file_alt_name_info *)rsp->Buffer;
4431 conv_len = ksmbd_extract_shortname(conn,
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004432 dentry->d_name.name,
Namjae Jeone2f34482021-03-16 10:49:09 +09004433 file_info->FileName);
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004434 spin_unlock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004435 file_info->FileNameLength = cpu_to_le32(conv_len);
4436 rsp->OutputBufferLength =
4437 cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len);
4438 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4439}
4440
4441static void get_file_stream_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004442 struct smb2_query_info_rsp *rsp,
4443 struct ksmbd_file *fp,
4444 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004445{
4446 struct ksmbd_conn *conn = work->conn;
4447 struct smb2_file_stream_info *file_info;
4448 char *stream_name, *xattr_list = NULL, *stream_buf;
4449 struct kstat stat;
4450 struct path *path = &fp->filp->f_path;
4451 ssize_t xattr_list_len;
4452 int nbytes = 0, streamlen, stream_name_len, next, idx = 0;
Hyunchul Lee34061d62021-10-16 08:39:54 +09004453 int buf_free_len;
4454 struct smb2_query_info_req *req = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09004455
Hyunchul Leeaf349832021-06-30 18:25:53 +09004456 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4457 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004458 file_info = (struct smb2_file_stream_info *)rsp->Buffer;
4459
4460 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4461 if (xattr_list_len < 0) {
4462 goto out;
4463 } else if (!xattr_list_len) {
4464 ksmbd_debug(SMB, "empty xattr in the file\n");
4465 goto out;
4466 }
4467
Hyunchul Lee34061d62021-10-16 08:39:54 +09004468 buf_free_len =
4469 smb2_calc_max_out_buf_len(work, 8,
4470 le32_to_cpu(req->OutputBufferLength));
4471 if (buf_free_len < 0)
4472 goto out;
4473
Namjae Jeone2f34482021-03-16 10:49:09 +09004474 while (idx < xattr_list_len) {
4475 stream_name = xattr_list + idx;
4476 streamlen = strlen(stream_name);
4477 idx += streamlen + 1;
4478
4479 ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen);
4480
4481 if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004482 STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004483 continue;
4484
4485 stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN +
4486 STREAM_PREFIX_LEN);
4487 streamlen = stream_name_len;
4488
4489 /* plus : size */
4490 streamlen += 1;
4491 stream_buf = kmalloc(streamlen + 1, GFP_KERNEL);
4492 if (!stream_buf)
4493 break;
4494
4495 streamlen = snprintf(stream_buf, streamlen + 1,
Namjae Jeon070fb212021-05-26 17:57:12 +09004496 ":%s", &stream_name[XATTR_NAME_STREAM_LEN]);
Namjae Jeone2f34482021-03-16 10:49:09 +09004497
Hyunchul Lee34061d62021-10-16 08:39:54 +09004498 next = sizeof(struct smb2_file_stream_info) + streamlen * 2;
4499 if (next > buf_free_len)
4500 break;
4501
Namjae Jeon070fb212021-05-26 17:57:12 +09004502 file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes];
Namjae Jeone2f34482021-03-16 10:49:09 +09004503 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004504 stream_buf, streamlen,
4505 conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004506 streamlen *= 2;
4507 kfree(stream_buf);
4508 file_info->StreamNameLength = cpu_to_le32(streamlen);
4509 file_info->StreamSize = cpu_to_le64(stream_name_len);
4510 file_info->StreamAllocationSize = cpu_to_le64(stream_name_len);
4511
Namjae Jeone2f34482021-03-16 10:49:09 +09004512 nbytes += next;
Hyunchul Lee34061d62021-10-16 08:39:54 +09004513 buf_free_len -= next;
Namjae Jeone2f34482021-03-16 10:49:09 +09004514 file_info->NextEntryOffset = cpu_to_le32(next);
4515 }
4516
Hyunchul Lee34061d62021-10-16 08:39:54 +09004517 if (!S_ISDIR(stat.mode) &&
4518 buf_free_len >= sizeof(struct smb2_file_stream_info) + 7 * 2) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004519 file_info = (struct smb2_file_stream_info *)
4520 &rsp->Buffer[nbytes];
4521 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004522 "::$DATA", 7, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004523 streamlen *= 2;
4524 file_info->StreamNameLength = cpu_to_le32(streamlen);
Namjae Jeon9f6323312021-09-18 21:02:39 +09004525 file_info->StreamSize = 0;
4526 file_info->StreamAllocationSize = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09004527 nbytes += sizeof(struct smb2_file_stream_info) + streamlen;
4528 }
4529
4530 /* last entry offset should be 0 */
4531 file_info->NextEntryOffset = 0;
4532out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004533 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004534
4535 rsp->OutputBufferLength = cpu_to_le32(nbytes);
4536 inc_rfc1001_len(rsp_org, nbytes);
4537}
4538
4539static void get_file_internal_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004540 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004541{
4542 struct smb2_file_internal_info *file_info;
4543 struct kstat stat;
4544
Hyunchul Leeaf349832021-06-30 18:25:53 +09004545 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4546 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004547 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4548 file_info->IndexNumber = cpu_to_le64(stat.ino);
4549 rsp->OutputBufferLength =
4550 cpu_to_le32(sizeof(struct smb2_file_internal_info));
4551 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
4552}
4553
4554static int get_file_network_open_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004555 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004556{
4557 struct smb2_file_ntwrk_info *file_info;
4558 struct inode *inode;
4559 struct kstat stat;
4560 u64 time;
4561
4562 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004563 pr_err("no right to read the attributes : 0x%x\n",
4564 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004565 return -EACCES;
4566 }
4567
4568 file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer;
4569
Namjae Jeonab0b2632021-06-29 09:20:13 +09004570 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004571 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004572
4573 file_info->CreationTime = cpu_to_le64(fp->create_time);
4574 time = ksmbd_UnixTimeToNT(stat.atime);
4575 file_info->LastAccessTime = cpu_to_le64(time);
4576 time = ksmbd_UnixTimeToNT(stat.mtime);
4577 file_info->LastWriteTime = cpu_to_le64(time);
4578 time = ksmbd_UnixTimeToNT(stat.ctime);
4579 file_info->ChangeTime = cpu_to_le64(time);
4580 file_info->Attributes = fp->f_ci->m_fattr;
4581 file_info->AllocationSize =
4582 cpu_to_le64(get_allocation_size(inode, &stat));
4583 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4584 file_info->Reserved = cpu_to_le32(0);
4585 rsp->OutputBufferLength =
4586 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info));
4587 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info));
4588 return 0;
4589}
4590
Namjae Jeon64b39f42021-03-30 14:25:35 +09004591static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004592{
4593 struct smb2_file_ea_info *file_info;
4594
4595 file_info = (struct smb2_file_ea_info *)rsp->Buffer;
4596 file_info->EASize = 0;
4597 rsp->OutputBufferLength =
4598 cpu_to_le32(sizeof(struct smb2_file_ea_info));
4599 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info));
4600}
4601
4602static void get_file_position_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004603 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004604{
4605 struct smb2_file_pos_info *file_info;
4606
4607 file_info = (struct smb2_file_pos_info *)rsp->Buffer;
4608 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4609 rsp->OutputBufferLength =
4610 cpu_to_le32(sizeof(struct smb2_file_pos_info));
4611 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info));
4612}
4613
4614static void get_file_mode_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004615 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004616{
4617 struct smb2_file_mode_info *file_info;
4618
4619 file_info = (struct smb2_file_mode_info *)rsp->Buffer;
4620 file_info->Mode = fp->coption & FILE_MODE_INFO_MASK;
4621 rsp->OutputBufferLength =
4622 cpu_to_le32(sizeof(struct smb2_file_mode_info));
4623 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info));
4624}
4625
4626static void get_file_compression_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004627 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004628{
4629 struct smb2_file_comp_info *file_info;
4630 struct kstat stat;
4631
Hyunchul Leeaf349832021-06-30 18:25:53 +09004632 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4633 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004634
4635 file_info = (struct smb2_file_comp_info *)rsp->Buffer;
4636 file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9);
4637 file_info->CompressionFormat = COMPRESSION_FORMAT_NONE;
4638 file_info->CompressionUnitShift = 0;
4639 file_info->ChunkShift = 0;
4640 file_info->ClusterShift = 0;
4641 memset(&file_info->Reserved[0], 0, 3);
4642
4643 rsp->OutputBufferLength =
4644 cpu_to_le32(sizeof(struct smb2_file_comp_info));
4645 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info));
4646}
4647
4648static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004649 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004650{
4651 struct smb2_file_attr_tag_info *file_info;
4652
4653 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004654 pr_err("no right to read the attributes : 0x%x\n",
4655 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004656 return -EACCES;
4657 }
4658
4659 file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer;
4660 file_info->FileAttributes = fp->f_ci->m_fattr;
4661 file_info->ReparseTag = 0;
4662 rsp->OutputBufferLength =
4663 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info));
Namjae Jeon070fb212021-05-26 17:57:12 +09004664 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004665 return 0;
4666}
4667
4668static int find_file_posix_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004669 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004670{
4671 struct smb311_posix_qinfo *file_info;
Namjae Jeonab0b2632021-06-29 09:20:13 +09004672 struct inode *inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09004673 u64 time;
4674
4675 file_info = (struct smb311_posix_qinfo *)rsp->Buffer;
4676 file_info->CreationTime = cpu_to_le64(fp->create_time);
4677 time = ksmbd_UnixTimeToNT(inode->i_atime);
4678 file_info->LastAccessTime = cpu_to_le64(time);
4679 time = ksmbd_UnixTimeToNT(inode->i_mtime);
4680 file_info->LastWriteTime = cpu_to_le64(time);
4681 time = ksmbd_UnixTimeToNT(inode->i_ctime);
4682 file_info->ChangeTime = cpu_to_le64(time);
4683 file_info->DosAttributes = fp->f_ci->m_fattr;
4684 file_info->Inode = cpu_to_le64(inode->i_ino);
4685 file_info->EndOfFile = cpu_to_le64(inode->i_size);
4686 file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9);
4687 file_info->HardLinks = cpu_to_le32(inode->i_nlink);
4688 file_info->Mode = cpu_to_le32(inode->i_mode);
4689 file_info->DeviceId = cpu_to_le32(inode->i_rdev);
4690 rsp->OutputBufferLength =
4691 cpu_to_le32(sizeof(struct smb311_posix_qinfo));
Namjae Jeon64b39f42021-03-30 14:25:35 +09004692 inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo));
Namjae Jeone2f34482021-03-16 10:49:09 +09004693 return 0;
4694}
4695
Namjae Jeone2f34482021-03-16 10:49:09 +09004696static int smb2_get_info_file(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004697 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004698 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09004699{
4700 struct ksmbd_file *fp;
4701 int fileinfoclass = 0;
4702 int rc = 0;
4703 int file_infoclass_size;
4704 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4705
4706 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004707 KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004708 /* smb2 info file called for pipe */
Namjae Jeoncb451722021-11-03 08:08:44 +09004709 return smb2_get_info_file_pipe(work->sess, req, rsp,
4710 work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004711 }
4712
4713 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon38673692021-07-08 12:32:27 +09004714 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
4715 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004716 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004717 id = work->compound_fid;
4718 pid = work->compound_pfid;
4719 }
4720 }
4721
Namjae Jeon38673692021-07-08 12:32:27 +09004722 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004723 id = le64_to_cpu(req->VolatileFileId);
4724 pid = le64_to_cpu(req->PersistentFileId);
4725 }
4726
4727 fp = ksmbd_lookup_fd_slow(work, id, pid);
4728 if (!fp)
4729 return -ENOENT;
4730
4731 fileinfoclass = req->FileInfoClass;
4732
4733 switch (fileinfoclass) {
4734 case FILE_ACCESS_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004735 get_file_access_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004736 file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE;
4737 break;
4738
4739 case FILE_BASIC_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004740 rc = get_file_basic_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004741 file_infoclass_size = FILE_BASIC_INFORMATION_SIZE;
4742 break;
4743
4744 case FILE_STANDARD_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004745 get_file_standard_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004746 file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE;
4747 break;
4748
4749 case FILE_ALIGNMENT_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004750 get_file_alignment_info(rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004751 file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE;
4752 break;
4753
4754 case FILE_ALL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004755 rc = get_file_all_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004756 file_infoclass_size = FILE_ALL_INFORMATION_SIZE;
4757 break;
4758
4759 case FILE_ALTERNATE_NAME_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004760 get_file_alternate_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004761 file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE;
4762 break;
4763
4764 case FILE_STREAM_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004765 get_file_stream_info(work, rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004766 file_infoclass_size = FILE_STREAM_INFORMATION_SIZE;
4767 break;
4768
4769 case FILE_INTERNAL_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004770 get_file_internal_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004771 file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE;
4772 break;
4773
4774 case FILE_NETWORK_OPEN_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004775 rc = get_file_network_open_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004776 file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE;
4777 break;
4778
4779 case FILE_EA_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004780 get_file_ea_info(rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004781 file_infoclass_size = FILE_EA_INFORMATION_SIZE;
4782 break;
4783
4784 case FILE_FULL_EA_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004785 rc = smb2_get_ea(work, fp, req, rsp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004786 file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE;
4787 break;
4788
4789 case FILE_POSITION_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004790 get_file_position_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004791 file_infoclass_size = FILE_POSITION_INFORMATION_SIZE;
4792 break;
4793
4794 case FILE_MODE_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004795 get_file_mode_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004796 file_infoclass_size = FILE_MODE_INFORMATION_SIZE;
4797 break;
4798
4799 case FILE_COMPRESSION_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004800 get_file_compression_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004801 file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE;
4802 break;
4803
4804 case FILE_ATTRIBUTE_TAG_INFORMATION:
Namjae Jeoncb451722021-11-03 08:08:44 +09004805 rc = get_file_attribute_tag_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004806 file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE;
4807 break;
4808 case SMB_FIND_FILE_POSIX_INFO:
4809 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004810 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004811 rc = -EOPNOTSUPP;
4812 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09004813 rc = find_file_posix_info(rsp, fp, work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004814 file_infoclass_size = sizeof(struct smb311_posix_qinfo);
4815 }
4816 break;
4817 default:
4818 ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n",
4819 fileinfoclass);
4820 rc = -EOPNOTSUPP;
4821 }
4822 if (!rc)
4823 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09004824 rsp, work->response_buf,
Namjae Jeone2f34482021-03-16 10:49:09 +09004825 file_infoclass_size);
4826 ksmbd_fd_put(work, fp);
4827 return rc;
4828}
4829
Namjae Jeone2f34482021-03-16 10:49:09 +09004830static int smb2_get_info_filesystem(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004831 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09004832 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09004833{
4834 struct ksmbd_session *sess = work->sess;
4835 struct ksmbd_conn *conn = sess->conn;
4836 struct ksmbd_share_config *share = work->tcon->share_conf;
4837 int fsinfoclass = 0;
4838 struct kstatfs stfs;
4839 struct path path;
4840 int rc = 0, len;
4841 int fs_infoclass_size = 0;
4842
Hyunchul Lee265fd192021-09-25 00:06:16 +09004843 rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09004844 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004845 pr_err("cannot create vfs path\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004846 return -EIO;
4847 }
4848
4849 rc = vfs_statfs(&path, &stfs);
4850 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004851 pr_err("cannot do stat of path %s\n", share->path);
Namjae Jeone2f34482021-03-16 10:49:09 +09004852 path_put(&path);
4853 return -EIO;
4854 }
4855
4856 fsinfoclass = req->FileInfoClass;
4857
4858 switch (fsinfoclass) {
4859 case FS_DEVICE_INFORMATION:
4860 {
4861 struct filesystem_device_info *info;
4862
4863 info = (struct filesystem_device_info *)rsp->Buffer;
4864
4865 info->DeviceType = cpu_to_le32(stfs.f_type);
4866 info->DeviceCharacteristics = cpu_to_le32(0x00000020);
4867 rsp->OutputBufferLength = cpu_to_le32(8);
Namjae Jeoncb451722021-11-03 08:08:44 +09004868 inc_rfc1001_len(work->response_buf, 8);
Namjae Jeone2f34482021-03-16 10:49:09 +09004869 fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE;
4870 break;
4871 }
4872 case FS_ATTRIBUTE_INFORMATION:
4873 {
4874 struct filesystem_attribute_info *info;
4875 size_t sz;
4876
4877 info = (struct filesystem_attribute_info *)rsp->Buffer;
4878 info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS |
4879 FILE_PERSISTENT_ACLS |
4880 FILE_UNICODE_ON_DISK |
4881 FILE_CASE_PRESERVED_NAMES |
Namjae Jeoneb817362021-05-18 10:37:59 +09004882 FILE_CASE_SENSITIVE_SEARCH |
4883 FILE_SUPPORTS_BLOCK_REFCOUNTING);
Namjae Jeone2f34482021-03-16 10:49:09 +09004884
4885 info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps);
4886
4887 info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen);
4888 len = smbConvertToUTF16((__le16 *)info->FileSystemName,
4889 "NTFS", PATH_MAX, conn->local_nls, 0);
4890 len = len * 2;
4891 info->FileSystemNameLen = cpu_to_le32(len);
4892 sz = sizeof(struct filesystem_attribute_info) - 2 + len;
4893 rsp->OutputBufferLength = cpu_to_le32(sz);
Namjae Jeoncb451722021-11-03 08:08:44 +09004894 inc_rfc1001_len(work->response_buf, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09004895 fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE;
4896 break;
4897 }
4898 case FS_VOLUME_INFORMATION:
4899 {
4900 struct filesystem_vol_info *info;
4901 size_t sz;
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004902 unsigned int serial_crc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09004903
4904 info = (struct filesystem_vol_info *)(rsp->Buffer);
4905 info->VolumeCreationTime = 0;
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004906 serial_crc = crc32_le(serial_crc, share->name,
4907 strlen(share->name));
4908 serial_crc = crc32_le(serial_crc, share->path,
4909 strlen(share->path));
4910 serial_crc = crc32_le(serial_crc, ksmbd_netbios_name(),
4911 strlen(ksmbd_netbios_name()));
Namjae Jeone2f34482021-03-16 10:49:09 +09004912 /* Taking dummy value of serial number*/
Namjae Jeon5d2f0b12021-10-31 09:53:50 +09004913 info->SerialNumber = cpu_to_le32(serial_crc);
Namjae Jeone2f34482021-03-16 10:49:09 +09004914 len = smbConvertToUTF16((__le16 *)info->VolumeLabel,
4915 share->name, PATH_MAX,
4916 conn->local_nls, 0);
4917 len = len * 2;
4918 info->VolumeLabelSize = cpu_to_le32(len);
4919 info->Reserved = 0;
4920 sz = sizeof(struct filesystem_vol_info) - 2 + len;
4921 rsp->OutputBufferLength = cpu_to_le32(sz);
Namjae Jeoncb451722021-11-03 08:08:44 +09004922 inc_rfc1001_len(work->response_buf, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09004923 fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE;
4924 break;
4925 }
4926 case FS_SIZE_INFORMATION:
4927 {
4928 struct filesystem_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004929
4930 info = (struct filesystem_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004931 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4932 info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004933 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4934 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004935 rsp->OutputBufferLength = cpu_to_le32(24);
Namjae Jeoncb451722021-11-03 08:08:44 +09004936 inc_rfc1001_len(work->response_buf, 24);
Namjae Jeone2f34482021-03-16 10:49:09 +09004937 fs_infoclass_size = FS_SIZE_INFORMATION_SIZE;
4938 break;
4939 }
4940 case FS_FULL_SIZE_INFORMATION:
4941 {
4942 struct smb2_fs_full_size_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004943
4944 info = (struct smb2_fs_full_size_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004945 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4946 info->CallerAvailableAllocationUnits =
4947 cpu_to_le64(stfs.f_bavail);
4948 info->ActualAvailableAllocationUnits =
4949 cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004950 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4951 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004952 rsp->OutputBufferLength = cpu_to_le32(32);
Namjae Jeoncb451722021-11-03 08:08:44 +09004953 inc_rfc1001_len(work->response_buf, 32);
Namjae Jeone2f34482021-03-16 10:49:09 +09004954 fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE;
4955 break;
4956 }
4957 case FS_OBJECT_ID_INFORMATION:
4958 {
4959 struct object_id_info *info;
4960
4961 info = (struct object_id_info *)(rsp->Buffer);
4962
4963 if (!user_guest(sess->user))
4964 memcpy(info->objid, user_passkey(sess->user), 16);
4965 else
4966 memset(info->objid, 0, 16);
4967
4968 info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC);
4969 info->extended_info.version = cpu_to_le32(1);
4970 info->extended_info.release = cpu_to_le32(1);
4971 info->extended_info.rel_date = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004972 memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0"));
Namjae Jeone2f34482021-03-16 10:49:09 +09004973 rsp->OutputBufferLength = cpu_to_le32(64);
Namjae Jeoncb451722021-11-03 08:08:44 +09004974 inc_rfc1001_len(work->response_buf, 64);
Namjae Jeone2f34482021-03-16 10:49:09 +09004975 fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE;
4976 break;
4977 }
4978 case FS_SECTOR_SIZE_INFORMATION:
4979 {
4980 struct smb3_fs_ss_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004981
4982 info = (struct smb3_fs_ss_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004983
Namjae Jeon131bac12021-06-22 16:20:47 +09004984 info->LogicalBytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004985 info->PhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09004986 cpu_to_le32(stfs.f_bsize);
4987 info->PhysicalBytesPerSectorForPerf = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004988 info->FSEffPhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09004989 cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004990 info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE |
4991 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE);
4992 info->ByteOffsetForSectorAlignment = 0;
4993 info->ByteOffsetForPartitionAlignment = 0;
4994 rsp->OutputBufferLength = cpu_to_le32(28);
Namjae Jeoncb451722021-11-03 08:08:44 +09004995 inc_rfc1001_len(work->response_buf, 28);
Namjae Jeone2f34482021-03-16 10:49:09 +09004996 fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE;
4997 break;
4998 }
4999 case FS_CONTROL_INFORMATION:
5000 {
5001 /*
5002 * TODO : The current implementation is based on
5003 * test result with win7(NTFS) server. It's need to
5004 * modify this to get valid Quota values
5005 * from Linux kernel
5006 */
5007 struct smb2_fs_control_info *info;
5008
5009 info = (struct smb2_fs_control_info *)(rsp->Buffer);
5010 info->FreeSpaceStartFiltering = 0;
5011 info->FreeSpaceThreshold = 0;
5012 info->FreeSpaceStopFiltering = 0;
5013 info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID);
5014 info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID);
5015 info->Padding = 0;
5016 rsp->OutputBufferLength = cpu_to_le32(48);
Namjae Jeoncb451722021-11-03 08:08:44 +09005017 inc_rfc1001_len(work->response_buf, 48);
Namjae Jeone2f34482021-03-16 10:49:09 +09005018 fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE;
5019 break;
5020 }
5021 case FS_POSIX_INFORMATION:
5022 {
5023 struct filesystem_posix_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09005024
5025 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005026 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005027 rc = -EOPNOTSUPP;
5028 } else {
5029 info = (struct filesystem_posix_info *)(rsp->Buffer);
Namjae Jeonee81cae2021-06-26 22:32:34 +09005030 info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005031 info->BlockSize = cpu_to_le32(stfs.f_bsize);
5032 info->TotalBlocks = cpu_to_le64(stfs.f_blocks);
5033 info->BlocksAvail = cpu_to_le64(stfs.f_bfree);
5034 info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail);
5035 info->TotalFileNodes = cpu_to_le64(stfs.f_files);
5036 info->FreeFileNodes = cpu_to_le64(stfs.f_ffree);
5037 rsp->OutputBufferLength = cpu_to_le32(56);
Namjae Jeoncb451722021-11-03 08:08:44 +09005038 inc_rfc1001_len(work->response_buf, 56);
Namjae Jeone2f34482021-03-16 10:49:09 +09005039 fs_infoclass_size = FS_POSIX_INFORMATION_SIZE;
5040 }
5041 break;
5042 }
5043 default:
5044 path_put(&path);
5045 return -EOPNOTSUPP;
5046 }
5047 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeoncb451722021-11-03 08:08:44 +09005048 rsp, work->response_buf,
Namjae Jeone2f34482021-03-16 10:49:09 +09005049 fs_infoclass_size);
5050 path_put(&path);
5051 return rc;
5052}
5053
5054static int smb2_get_info_sec(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005055 struct smb2_query_info_req *req,
Namjae Jeoncb451722021-11-03 08:08:44 +09005056 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09005057{
5058 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005059 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09005060 struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL;
5061 struct smb_fattr fattr = {{0}};
5062 struct inode *inode;
5063 __u32 secdesclen;
5064 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5065 int addition_info = le32_to_cpu(req->AdditionalInformation);
5066 int rc;
5067
Namjae Jeone294f782021-06-28 15:26:37 +09005068 if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
5069 PROTECTED_DACL_SECINFO |
5070 UNPROTECTED_DACL_SECINFO)) {
5071 pr_err("Unsupported addition info: 0x%x)\n",
5072 addition_info);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09005073
5074 pntsd->revision = cpu_to_le16(1);
5075 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED);
5076 pntsd->osidoffset = 0;
5077 pntsd->gsidoffset = 0;
5078 pntsd->sacloffset = 0;
5079 pntsd->dacloffset = 0;
5080
5081 secdesclen = sizeof(struct smb_ntsd);
5082 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
Namjae Jeoncb451722021-11-03 08:08:44 +09005083 inc_rfc1001_len(work->response_buf, secdesclen);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09005084
5085 return 0;
5086 }
5087
Namjae Jeone2f34482021-03-16 10:49:09 +09005088 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon38673692021-07-08 12:32:27 +09005089 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
5090 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005091 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005092 id = work->compound_fid;
5093 pid = work->compound_pfid;
5094 }
5095 }
5096
Namjae Jeon38673692021-07-08 12:32:27 +09005097 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005098 id = le64_to_cpu(req->VolatileFileId);
5099 pid = le64_to_cpu(req->PersistentFileId);
5100 }
5101
5102 fp = ksmbd_lookup_fd_slow(work, id, pid);
5103 if (!fp)
5104 return -ENOENT;
5105
Hyunchul Lee465d7202021-07-03 12:10:36 +09005106 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeonab0b2632021-06-29 09:20:13 +09005107 inode = file_inode(fp->filp);
Christian Brauner43205ca2021-08-23 17:13:50 +02005108 ksmbd_acls_fattr(&fattr, user_ns, inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09005109
5110 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09005111 KSMBD_SHARE_FLAG_ACL_XATTR))
Hyunchul Lee465d7202021-07-03 12:10:36 +09005112 ksmbd_vfs_get_sd_xattr(work->conn, user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005113 fp->filp->f_path.dentry, &ppntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09005114
Hyunchul Lee465d7202021-07-03 12:10:36 +09005115 rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info,
5116 &secdesclen, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09005117 posix_acl_release(fattr.cf_acls);
5118 posix_acl_release(fattr.cf_dacls);
5119 kfree(ppntsd);
5120 ksmbd_fd_put(work, fp);
5121 if (rc)
5122 return rc;
5123
5124 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
Namjae Jeoncb451722021-11-03 08:08:44 +09005125 inc_rfc1001_len(work->response_buf, secdesclen);
Namjae Jeone2f34482021-03-16 10:49:09 +09005126 return 0;
5127}
5128
5129/**
5130 * smb2_query_info() - handler for smb2 query info command
5131 * @work: smb work containing query info request buffer
5132 *
5133 * Return: 0 on success, otherwise error
5134 */
5135int smb2_query_info(struct ksmbd_work *work)
5136{
5137 struct smb2_query_info_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09005138 struct smb2_query_info_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005139 int rc = 0;
5140
Namjae Jeone2f34482021-03-16 10:49:09 +09005141 WORK_BUFFERS(work, req, rsp);
5142
5143 ksmbd_debug(SMB, "GOT query info request\n");
5144
5145 switch (req->InfoType) {
5146 case SMB2_O_INFO_FILE:
5147 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005148 rc = smb2_get_info_file(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005149 break;
5150 case SMB2_O_INFO_FILESYSTEM:
5151 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005152 rc = smb2_get_info_filesystem(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005153 break;
5154 case SMB2_O_INFO_SECURITY:
5155 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
Namjae Jeoncb451722021-11-03 08:08:44 +09005156 rc = smb2_get_info_sec(work, req, rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005157 break;
5158 default:
5159 ksmbd_debug(SMB, "InfoType %d not supported yet\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005160 req->InfoType);
Namjae Jeone2f34482021-03-16 10:49:09 +09005161 rc = -EOPNOTSUPP;
5162 }
5163
5164 if (rc < 0) {
5165 if (rc == -EACCES)
5166 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5167 else if (rc == -ENOENT)
5168 rsp->hdr.Status = STATUS_FILE_CLOSED;
5169 else if (rc == -EIO)
5170 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5171 else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0)
5172 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5173 smb2_set_err_rsp(work);
5174
5175 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005176 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005177 return rc;
5178 }
5179 rsp->StructureSize = cpu_to_le16(9);
5180 rsp->OutputBufferOffset = cpu_to_le16(72);
Namjae Jeoncb451722021-11-03 08:08:44 +09005181 inc_rfc1001_len(work->response_buf, 8);
Namjae Jeone2f34482021-03-16 10:49:09 +09005182 return 0;
5183}
5184
5185/**
5186 * smb2_close_pipe() - handler for closing IPC pipe
5187 * @work: smb work containing close request buffer
5188 *
5189 * Return: 0
5190 */
5191static noinline int smb2_close_pipe(struct ksmbd_work *work)
5192{
Namjae Jeon64b39f42021-03-30 14:25:35 +09005193 u64 id;
Namjae Jeoncb451722021-11-03 08:08:44 +09005194 struct smb2_close_req *req = smb2_get_msg(work->request_buf);
5195 struct smb2_close_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005196
5197 id = le64_to_cpu(req->VolatileFileId);
5198 ksmbd_session_rpc_close(work->sess, id);
5199
5200 rsp->StructureSize = cpu_to_le16(60);
5201 rsp->Flags = 0;
5202 rsp->Reserved = 0;
5203 rsp->CreationTime = 0;
5204 rsp->LastAccessTime = 0;
5205 rsp->LastWriteTime = 0;
5206 rsp->ChangeTime = 0;
5207 rsp->AllocationSize = 0;
5208 rsp->EndOfFile = 0;
5209 rsp->Attributes = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09005210 inc_rfc1001_len(work->response_buf, 60);
Namjae Jeone2f34482021-03-16 10:49:09 +09005211 return 0;
5212}
5213
5214/**
5215 * smb2_close() - handler for smb2 close file command
5216 * @work: smb work containing close request buffer
5217 *
5218 * Return: 0
5219 */
5220int smb2_close(struct ksmbd_work *work)
5221{
Namjae Jeon38673692021-07-08 12:32:27 +09005222 u64 volatile_id = KSMBD_NO_FID;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005223 u64 sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09005224 struct smb2_close_req *req;
5225 struct smb2_close_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005226 struct ksmbd_conn *conn = work->conn;
5227 struct ksmbd_file *fp;
5228 struct inode *inode;
5229 u64 time;
5230 int err = 0;
5231
Namjae Jeone2f34482021-03-16 10:49:09 +09005232 WORK_BUFFERS(work, req, rsp);
5233
5234 if (test_share_config_flag(work->tcon->share_conf,
5235 KSMBD_SHARE_FLAG_PIPE)) {
5236 ksmbd_debug(SMB, "IPC pipe close request\n");
5237 return smb2_close_pipe(work);
5238 }
5239
5240 sess_id = le64_to_cpu(req->hdr.SessionId);
5241 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5242 sess_id = work->compound_sid;
5243
5244 work->compound_sid = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005245 if (check_session_id(conn, sess_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005246 work->compound_sid = sess_id;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005247 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005248 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
5249 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5250 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5251 err = -EBADF;
5252 goto out;
5253 }
5254
5255 if (work->next_smb2_rcv_hdr_off &&
Namjae Jeon38673692021-07-08 12:32:27 +09005256 !has_file_id(le64_to_cpu(req->VolatileFileId))) {
5257 if (!has_file_id(work->compound_fid)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005258 /* file already closed, return FILE_CLOSED */
5259 ksmbd_debug(SMB, "file already closed\n");
5260 rsp->hdr.Status = STATUS_FILE_CLOSED;
5261 err = -EBADF;
5262 goto out;
5263 } else {
Namjae Jeon38673692021-07-08 12:32:27 +09005264 ksmbd_debug(SMB,
5265 "Compound request set FID = %llu:%llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005266 work->compound_fid,
5267 work->compound_pfid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005268 volatile_id = work->compound_fid;
5269
5270 /* file closed, stored id is not valid anymore */
5271 work->compound_fid = KSMBD_NO_FID;
5272 work->compound_pfid = KSMBD_NO_FID;
5273 }
5274 } else {
5275 volatile_id = le64_to_cpu(req->VolatileFileId);
5276 }
Namjae Jeon38673692021-07-08 12:32:27 +09005277 ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id);
Namjae Jeone2f34482021-03-16 10:49:09 +09005278
5279 rsp->StructureSize = cpu_to_le16(60);
5280 rsp->Reserved = 0;
5281
5282 if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) {
5283 fp = ksmbd_lookup_fd_fast(work, volatile_id);
5284 if (!fp) {
5285 err = -ENOENT;
5286 goto out;
5287 }
5288
Namjae Jeonab0b2632021-06-29 09:20:13 +09005289 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005290 rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
5291 rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 :
5292 cpu_to_le64(inode->i_blocks << 9);
5293 rsp->EndOfFile = cpu_to_le64(inode->i_size);
5294 rsp->Attributes = fp->f_ci->m_fattr;
5295 rsp->CreationTime = cpu_to_le64(fp->create_time);
5296 time = ksmbd_UnixTimeToNT(inode->i_atime);
5297 rsp->LastAccessTime = cpu_to_le64(time);
5298 time = ksmbd_UnixTimeToNT(inode->i_mtime);
5299 rsp->LastWriteTime = cpu_to_le64(time);
5300 time = ksmbd_UnixTimeToNT(inode->i_ctime);
5301 rsp->ChangeTime = cpu_to_le64(time);
5302 ksmbd_fd_put(work, fp);
5303 } else {
5304 rsp->Flags = 0;
5305 rsp->AllocationSize = 0;
5306 rsp->EndOfFile = 0;
5307 rsp->Attributes = 0;
5308 rsp->CreationTime = 0;
5309 rsp->LastAccessTime = 0;
5310 rsp->LastWriteTime = 0;
5311 rsp->ChangeTime = 0;
5312 }
5313
5314 err = ksmbd_close_fd(work, volatile_id);
5315out:
5316 if (err) {
5317 if (rsp->hdr.Status == 0)
5318 rsp->hdr.Status = STATUS_FILE_CLOSED;
5319 smb2_set_err_rsp(work);
5320 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09005321 inc_rfc1001_len(work->response_buf, 60);
Namjae Jeone2f34482021-03-16 10:49:09 +09005322 }
5323
5324 return 0;
5325}
5326
5327/**
5328 * smb2_echo() - handler for smb2 echo(ping) command
5329 * @work: smb work containing echo request buffer
5330 *
5331 * Return: 0
5332 */
5333int smb2_echo(struct ksmbd_work *work)
5334{
Namjae Jeoncb451722021-11-03 08:08:44 +09005335 struct smb2_echo_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005336
5337 rsp->StructureSize = cpu_to_le16(4);
5338 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09005339 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09005340 return 0;
5341}
5342
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005343static int smb2_rename(struct ksmbd_work *work,
5344 struct ksmbd_file *fp,
5345 struct user_namespace *user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +09005346 struct smb2_file_rename_info *file_info,
5347 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005348{
5349 struct ksmbd_share_config *share = fp->tcon->share_conf;
5350 char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL;
5351 char *pathname = NULL;
5352 struct path path;
5353 bool file_present = true;
5354 int rc;
5355
5356 ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n");
5357 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5358 if (!pathname)
5359 return -ENOMEM;
5360
5361 abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
5362 if (IS_ERR(abs_oldname)) {
5363 rc = -EINVAL;
5364 goto out;
5365 }
5366 old_name = strrchr(abs_oldname, '/');
Namjae Jeon64b39f42021-03-30 14:25:35 +09005367 if (old_name && old_name[1] != '\0') {
Namjae Jeone2f34482021-03-16 10:49:09 +09005368 old_name++;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005369 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005370 ksmbd_debug(SMB, "can't get last component in path %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005371 abs_oldname);
Namjae Jeone2f34482021-03-16 10:49:09 +09005372 rc = -ENOENT;
5373 goto out;
5374 }
5375
5376 new_name = smb2_get_name(share,
5377 file_info->FileName,
5378 le32_to_cpu(file_info->FileNameLength),
5379 local_nls);
5380 if (IS_ERR(new_name)) {
5381 rc = PTR_ERR(new_name);
5382 goto out;
5383 }
5384
5385 if (strchr(new_name, ':')) {
5386 int s_type;
5387 char *xattr_stream_name, *stream_name = NULL;
5388 size_t xattr_stream_size;
5389 int len;
5390
5391 rc = parse_stream_name(new_name, &stream_name, &s_type);
5392 if (rc < 0)
5393 goto out;
5394
5395 len = strlen(new_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005396 if (len > 0 && new_name[len - 1] != '/') {
Namjae Jeonbde16942021-06-28 15:23:19 +09005397 pr_err("not allow base filename in rename\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005398 rc = -ESHARE;
5399 goto out;
5400 }
5401
5402 rc = ksmbd_vfs_xattr_stream_name(stream_name,
5403 &xattr_stream_name,
5404 &xattr_stream_size,
5405 s_type);
5406 if (rc)
5407 goto out;
5408
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005409 rc = ksmbd_vfs_setxattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005410 fp->filp->f_path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09005411 xattr_stream_name,
5412 NULL, 0, 0);
5413 if (rc < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005414 pr_err("failed to store stream name in xattr: %d\n",
5415 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005416 rc = -EINVAL;
5417 goto out;
5418 }
5419
5420 goto out;
5421 }
5422
5423 ksmbd_debug(SMB, "new name %s\n", new_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005424 rc = ksmbd_vfs_kern_path(work, new_name, LOOKUP_NO_SYMLINKS, &path, 1);
5425 if (rc) {
5426 if (rc != -ENOENT)
5427 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005428 file_present = false;
Hyunchul Lee265fd192021-09-25 00:06:16 +09005429 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005430 path_put(&path);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005431 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005432
5433 if (ksmbd_share_veto_filename(share, new_name)) {
5434 rc = -ENOENT;
5435 ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name);
5436 goto out;
5437 }
5438
5439 if (file_info->ReplaceIfExists) {
5440 if (file_present) {
5441 rc = ksmbd_vfs_remove_file(work, new_name);
5442 if (rc) {
5443 if (rc != -ENOTEMPTY)
5444 rc = -EINVAL;
5445 ksmbd_debug(SMB, "cannot delete %s, rc %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005446 new_name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005447 goto out;
5448 }
5449 }
5450 } else {
5451 if (file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005452 strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005453 rc = -EEXIST;
5454 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005455 "cannot rename already existing file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005456 goto out;
5457 }
5458 }
5459
5460 rc = ksmbd_vfs_fp_rename(work, fp, new_name);
5461out:
5462 kfree(pathname);
5463 if (!IS_ERR(new_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005464 kfree(new_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005465 return rc;
5466}
5467
Namjae Jeone2f34482021-03-16 10:49:09 +09005468static int smb2_create_link(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005469 struct ksmbd_share_config *share,
5470 struct smb2_file_link_info *file_info,
Namjae Jeon9496e262021-09-29 15:41:48 +09005471 unsigned int buf_len, struct file *filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005472 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005473{
5474 char *link_name = NULL, *target_name = NULL, *pathname = NULL;
5475 struct path path;
5476 bool file_present = true;
5477 int rc;
5478
Namjae Jeon9496e262021-09-29 15:41:48 +09005479 if (buf_len < (u64)sizeof(struct smb2_file_link_info) +
5480 le32_to_cpu(file_info->FileNameLength))
5481 return -EINVAL;
5482
Namjae Jeone2f34482021-03-16 10:49:09 +09005483 ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n");
5484 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5485 if (!pathname)
5486 return -ENOMEM;
5487
5488 link_name = smb2_get_name(share,
5489 file_info->FileName,
5490 le32_to_cpu(file_info->FileNameLength),
5491 local_nls);
5492 if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) {
5493 rc = -EINVAL;
5494 goto out;
5495 }
5496
5497 ksmbd_debug(SMB, "link name is %s\n", link_name);
5498 target_name = d_path(&filp->f_path, pathname, PATH_MAX);
5499 if (IS_ERR(target_name)) {
5500 rc = -EINVAL;
5501 goto out;
5502 }
5503
5504 ksmbd_debug(SMB, "target name is %s\n", target_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005505 rc = ksmbd_vfs_kern_path(work, link_name, LOOKUP_NO_SYMLINKS, &path, 0);
5506 if (rc) {
5507 if (rc != -ENOENT)
5508 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005509 file_present = false;
Hyunchul Lee265fd192021-09-25 00:06:16 +09005510 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005511 path_put(&path);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005512 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005513
5514 if (file_info->ReplaceIfExists) {
5515 if (file_present) {
5516 rc = ksmbd_vfs_remove_file(work, link_name);
5517 if (rc) {
5518 rc = -EINVAL;
5519 ksmbd_debug(SMB, "cannot delete %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005520 link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005521 goto out;
5522 }
5523 }
5524 } else {
5525 if (file_present) {
5526 rc = -EEXIST;
5527 ksmbd_debug(SMB, "link already exists\n");
5528 goto out;
5529 }
5530 }
5531
5532 rc = ksmbd_vfs_link(work, target_name, link_name);
5533 if (rc)
5534 rc = -EINVAL;
5535out:
5536 if (!IS_ERR(link_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005537 kfree(link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005538 kfree(pathname);
5539 return rc;
5540}
5541
Namjae Jeon9496e262021-09-29 15:41:48 +09005542static int set_file_basic_info(struct ksmbd_file *fp,
5543 struct smb2_file_basic_info *file_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005544 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005545{
Namjae Jeone2f34482021-03-16 10:49:09 +09005546 struct iattr attrs;
Namjae Jeone2f34482021-03-16 10:49:09 +09005547 struct file *filp;
5548 struct inode *inode;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005549 struct user_namespace *user_ns;
Namjae Jeon4ffd5262021-09-07 08:15:21 +09005550 int rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09005551
Marios Makassikis7adfd4f2021-04-27 15:30:22 +09005552 if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005553 return -EACCES;
5554
Namjae Jeone2f34482021-03-16 10:49:09 +09005555 attrs.ia_valid = 0;
5556 filp = fp->filp;
5557 inode = file_inode(filp);
Hyunchul Lee465d7202021-07-03 12:10:36 +09005558 user_ns = file_mnt_user_ns(filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005559
5560 if (file_info->CreationTime)
5561 fp->create_time = le64_to_cpu(file_info->CreationTime);
5562
5563 if (file_info->LastAccessTime) {
5564 attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime);
5565 attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
5566 }
5567
Namjae Jeon64e78752021-10-03 13:19:00 +09005568 attrs.ia_valid |= ATTR_CTIME;
5569 if (file_info->ChangeTime)
Christian Braunerdb7fb6f2021-08-26 10:07:05 +09005570 attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime);
Namjae Jeon64e78752021-10-03 13:19:00 +09005571 else
5572 attrs.ia_ctime = inode->i_ctime;
Namjae Jeone2f34482021-03-16 10:49:09 +09005573
5574 if (file_info->LastWriteTime) {
5575 attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime);
5576 attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
5577 }
5578
5579 if (file_info->Attributes) {
5580 if (!S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005581 file_info->Attributes & ATTR_DIRECTORY_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005582 pr_err("can't change a file to a directory\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005583 return -EINVAL;
5584 }
5585
5586 if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == ATTR_NORMAL_LE))
5587 fp->f_ci->m_fattr = file_info->Attributes |
5588 (fp->f_ci->m_fattr & ATTR_DIRECTORY_LE);
5589 }
5590
5591 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
5592 (file_info->CreationTime || file_info->Attributes)) {
5593 struct xattr_dos_attrib da = {0};
5594
5595 da.version = 4;
5596 da.itime = fp->itime;
5597 da.create_time = fp->create_time;
5598 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
5599 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
5600 XATTR_DOSINFO_ITIME;
5601
Hyunchul Lee465d7202021-07-03 12:10:36 +09005602 rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005603 filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09005604 if (rc)
5605 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005606 "failed to restore file attribute in EA\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005607 rc = 0;
5608 }
5609
Namjae Jeone2f34482021-03-16 10:49:09 +09005610 if (attrs.ia_valid) {
5611 struct dentry *dentry = filp->f_path.dentry;
5612 struct inode *inode = d_inode(dentry);
5613
5614 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
5615 return -EACCES;
5616
Namjae Jeone2f34482021-03-16 10:49:09 +09005617 inode_lock(inode);
Namjae Jeon64e78752021-10-03 13:19:00 +09005618 inode->i_ctime = attrs.ia_ctime;
5619 attrs.ia_valid &= ~ATTR_CTIME;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005620 rc = notify_change(user_ns, dentry, &attrs, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09005621 inode_unlock(inode);
5622 }
Christian Braunereb5784f2021-08-23 17:13:55 +02005623 return rc;
Namjae Jeone2f34482021-03-16 10:49:09 +09005624}
5625
5626static int set_file_allocation_info(struct ksmbd_work *work,
Namjae Jeon9496e262021-09-29 15:41:48 +09005627 struct ksmbd_file *fp,
5628 struct smb2_file_alloc_info *file_alloc_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005629{
5630 /*
5631 * TODO : It's working fine only when store dos attributes
5632 * is not yes. need to implement a logic which works
5633 * properly with any smb.conf option
5634 */
5635
Namjae Jeone2f34482021-03-16 10:49:09 +09005636 loff_t alloc_blks;
5637 struct inode *inode;
5638 int rc;
5639
Marios Makassikisa2996692021-04-27 15:29:01 +09005640 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005641 return -EACCES;
5642
Namjae Jeone2f34482021-03-16 10:49:09 +09005643 alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9;
5644 inode = file_inode(fp->filp);
5645
5646 if (alloc_blks > inode->i_blocks) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005647 smb_break_all_levII_oplock(work, fp, 1);
5648 rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
5649 alloc_blks * 512);
Namjae Jeone2f34482021-03-16 10:49:09 +09005650 if (rc && rc != -EOPNOTSUPP) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005651 pr_err("vfs_fallocate is failed : %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005652 return rc;
5653 }
5654 } else if (alloc_blks < inode->i_blocks) {
5655 loff_t size;
5656
5657 /*
5658 * Allocation size could be smaller than original one
5659 * which means allocated blocks in file should be
5660 * deallocated. use truncate to cut out it, but inode
5661 * size is also updated with truncate offset.
5662 * inode size is retained by backup inode size.
5663 */
5664 size = i_size_read(inode);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005665 rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512);
Namjae Jeone2f34482021-03-16 10:49:09 +09005666 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005667 pr_err("truncate failed! filename : %s, err %d\n",
5668 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005669 return rc;
5670 }
5671 if (size < alloc_blks * 512)
5672 i_size_write(inode, size);
5673 }
5674 return 0;
5675}
5676
Namjae Jeon64b39f42021-03-30 14:25:35 +09005677static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005678 struct smb2_file_eof_info *file_eof_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005679{
Namjae Jeone2f34482021-03-16 10:49:09 +09005680 loff_t newsize;
5681 struct inode *inode;
5682 int rc;
5683
Marios Makassikisa2996692021-04-27 15:29:01 +09005684 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005685 return -EACCES;
5686
Namjae Jeone2f34482021-03-16 10:49:09 +09005687 newsize = le64_to_cpu(file_eof_info->EndOfFile);
5688 inode = file_inode(fp->filp);
5689
5690 /*
5691 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called
5692 * on FAT32 shared device, truncate execution time is too long
5693 * and network error could cause from windows client. because
5694 * truncate of some filesystem like FAT32 fill zero data in
5695 * truncated range.
5696 */
5697 if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) {
5698 ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005699 fp->filename, newsize);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005700 rc = ksmbd_vfs_truncate(work, fp, newsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005701 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005702 ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005703 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005704 if (rc != -EAGAIN)
5705 rc = -EBADF;
5706 return rc;
5707 }
5708 }
5709 return 0;
5710}
5711
Namjae Jeon64b39f42021-03-30 14:25:35 +09005712static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005713 struct smb2_file_rename_info *rename_info,
5714 unsigned int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005715{
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005716 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09005717 struct ksmbd_file *parent_fp;
Namjae Jeon12202c02021-06-29 09:23:56 +09005718 struct dentry *parent;
5719 struct dentry *dentry = fp->filp->f_path.dentry;
5720 int ret;
Namjae Jeone2f34482021-03-16 10:49:09 +09005721
5722 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005723 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005724 return -EACCES;
5725 }
5726
Namjae Jeon9496e262021-09-29 15:41:48 +09005727 if (buf_len < (u64)sizeof(struct smb2_file_rename_info) +
5728 le32_to_cpu(rename_info->FileNameLength))
5729 return -EINVAL;
5730
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005731 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005732 if (ksmbd_stream_fd(fp))
5733 goto next;
5734
Namjae Jeon12202c02021-06-29 09:23:56 +09005735 parent = dget_parent(dentry);
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005736 ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
Namjae Jeon12202c02021-06-29 09:23:56 +09005737 if (ret) {
5738 dput(parent);
5739 return ret;
5740 }
5741
5742 parent_fp = ksmbd_lookup_fd_inode(d_inode(parent));
5743 inode_unlock(d_inode(parent));
5744 dput(parent);
5745
Namjae Jeone2f34482021-03-16 10:49:09 +09005746 if (parent_fp) {
5747 if (parent_fp->daccess & FILE_DELETE_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005748 pr_err("parent dir is opened with delete access\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005749 return -ESHARE;
5750 }
5751 }
5752next:
Namjae Jeon9496e262021-09-29 15:41:48 +09005753 return smb2_rename(work, fp, user_ns, rename_info,
Namjae Jeone2f34482021-03-16 10:49:09 +09005754 work->sess->conn->local_nls);
5755}
5756
Namjae Jeon9496e262021-09-29 15:41:48 +09005757static int set_file_disposition_info(struct ksmbd_file *fp,
5758 struct smb2_file_disposition_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005759{
Namjae Jeone2f34482021-03-16 10:49:09 +09005760 struct inode *inode;
5761
5762 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005763 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005764 return -EACCES;
5765 }
5766
5767 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005768 if (file_info->DeletePending) {
5769 if (S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005770 ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY)
Namjae Jeone2f34482021-03-16 10:49:09 +09005771 return -EBUSY;
5772 ksmbd_set_inode_pending_delete(fp);
5773 } else {
5774 ksmbd_clear_inode_pending_delete(fp);
5775 }
5776 return 0;
5777}
5778
Namjae Jeon9496e262021-09-29 15:41:48 +09005779static int set_file_position_info(struct ksmbd_file *fp,
5780 struct smb2_file_pos_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005781{
Namjae Jeone2f34482021-03-16 10:49:09 +09005782 loff_t current_byte_offset;
Namjae Jeonee81cae2021-06-26 22:32:34 +09005783 unsigned long sector_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09005784 struct inode *inode;
5785
5786 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005787 current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset);
Namjae Jeonee81cae2021-06-26 22:32:34 +09005788 sector_size = inode->i_sb->s_blocksize;
Namjae Jeone2f34482021-03-16 10:49:09 +09005789
5790 if (current_byte_offset < 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09005791 (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE &&
5792 current_byte_offset & (sector_size - 1))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005793 pr_err("CurrentByteOffset is not valid : %llu\n",
5794 current_byte_offset);
Namjae Jeone2f34482021-03-16 10:49:09 +09005795 return -EINVAL;
5796 }
5797
5798 fp->filp->f_pos = current_byte_offset;
5799 return 0;
5800}
5801
Namjae Jeon9496e262021-09-29 15:41:48 +09005802static int set_file_mode_info(struct ksmbd_file *fp,
5803 struct smb2_file_mode_info *file_info)
Namjae Jeone2f34482021-03-16 10:49:09 +09005804{
Namjae Jeone2f34482021-03-16 10:49:09 +09005805 __le32 mode;
5806
Namjae Jeone2f34482021-03-16 10:49:09 +09005807 mode = file_info->Mode;
5808
Namjae Jeon64b39f42021-03-30 14:25:35 +09005809 if ((mode & ~FILE_MODE_INFO_MASK) ||
5810 (mode & FILE_SYNCHRONOUS_IO_ALERT_LE &&
5811 mode & FILE_SYNCHRONOUS_IO_NONALERT_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005812 pr_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode));
Namjae Jeone2f34482021-03-16 10:49:09 +09005813 return -EINVAL;
5814 }
5815
5816 /*
5817 * TODO : need to implement consideration for
5818 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT
5819 */
5820 ksmbd_vfs_set_fadvise(fp->filp, mode);
5821 fp->coption = mode;
5822 return 0;
5823}
5824
5825/**
5826 * smb2_set_info_file() - handler for smb2 set info command
5827 * @work: smb work containing set info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09005828 * @fp: ksmbd_file pointer
5829 * @info_class: smb2 set info class
5830 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09005831 *
5832 * Return: 0 on success, otherwise error
5833 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH
5834 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09005835static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon9496e262021-09-29 15:41:48 +09005836 struct smb2_set_info_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09005837 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005838{
Namjae Jeon9496e262021-09-29 15:41:48 +09005839 unsigned int buf_len = le32_to_cpu(req->BufferLength);
5840
5841 switch (req->FileInfoClass) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005842 case FILE_BASIC_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005843 {
5844 if (buf_len < sizeof(struct smb2_file_basic_info))
5845 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005846
Namjae Jeon9496e262021-09-29 15:41:48 +09005847 return set_file_basic_info(fp, (struct smb2_file_basic_info *)req->Buffer, share);
5848 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005849 case FILE_ALLOCATION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005850 {
5851 if (buf_len < sizeof(struct smb2_file_alloc_info))
5852 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005853
Namjae Jeon9496e262021-09-29 15:41:48 +09005854 return set_file_allocation_info(work, fp,
5855 (struct smb2_file_alloc_info *)req->Buffer);
5856 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005857 case FILE_END_OF_FILE_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005858 {
5859 if (buf_len < sizeof(struct smb2_file_eof_info))
5860 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005861
Namjae Jeon9496e262021-09-29 15:41:48 +09005862 return set_end_of_file_info(work, fp,
5863 (struct smb2_file_eof_info *)req->Buffer);
5864 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005865 case FILE_RENAME_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005866 {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005867 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005868 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005869 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005870 return -EACCES;
5871 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005872
Namjae Jeon9496e262021-09-29 15:41:48 +09005873 if (buf_len < sizeof(struct smb2_file_rename_info))
5874 return -EINVAL;
5875
5876 return set_rename_info(work, fp,
5877 (struct smb2_file_rename_info *)req->Buffer,
5878 buf_len);
5879 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005880 case FILE_LINK_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005881 {
5882 if (buf_len < sizeof(struct smb2_file_link_info))
5883 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005884
Namjae Jeon9496e262021-09-29 15:41:48 +09005885 return smb2_create_link(work, work->tcon->share_conf,
5886 (struct smb2_file_link_info *)req->Buffer,
5887 buf_len, fp->filp,
5888 work->sess->conn->local_nls);
5889 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005890 case FILE_DISPOSITION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005891 {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005892 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005893 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005894 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005895 return -EACCES;
5896 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005897
Namjae Jeon9496e262021-09-29 15:41:48 +09005898 if (buf_len < sizeof(struct smb2_file_disposition_info))
5899 return -EINVAL;
5900
5901 return set_file_disposition_info(fp,
5902 (struct smb2_file_disposition_info *)req->Buffer);
5903 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005904 case FILE_FULL_EA_INFORMATION:
5905 {
5906 if (!(fp->daccess & FILE_WRITE_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005907 pr_err("Not permitted to write ext attr: 0x%x\n",
5908 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005909 return -EACCES;
5910 }
5911
Namjae Jeon9496e262021-09-29 15:41:48 +09005912 if (buf_len < sizeof(struct smb2_ea_info))
5913 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005914
Namjae Jeon9496e262021-09-29 15:41:48 +09005915 return smb2_set_ea((struct smb2_ea_info *)req->Buffer,
5916 buf_len, &fp->filp->f_path);
5917 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005918 case FILE_POSITION_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005919 {
5920 if (buf_len < sizeof(struct smb2_file_pos_info))
5921 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005922
Namjae Jeon9496e262021-09-29 15:41:48 +09005923 return set_file_position_info(fp, (struct smb2_file_pos_info *)req->Buffer);
5924 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005925 case FILE_MODE_INFORMATION:
Namjae Jeon9496e262021-09-29 15:41:48 +09005926 {
5927 if (buf_len < sizeof(struct smb2_file_mode_info))
5928 return -EINVAL;
5929
5930 return set_file_mode_info(fp, (struct smb2_file_mode_info *)req->Buffer);
5931 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005932 }
5933
Namjae Jeon9496e262021-09-29 15:41:48 +09005934 pr_err("Unimplemented Fileinfoclass :%d\n", req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09005935 return -EOPNOTSUPP;
5936}
5937
Namjae Jeon64b39f42021-03-30 14:25:35 +09005938static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005939 char *buffer, int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005940{
5941 struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer;
5942
5943 fp->saccess |= FILE_SHARE_DELETE_LE;
5944
Hyunchul Leeef24c962021-06-30 18:25:52 +09005945 return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd,
Namjae Jeone2f34482021-03-16 10:49:09 +09005946 buf_len, false);
5947}
5948
5949/**
5950 * smb2_set_info() - handler for smb2 set info command handler
5951 * @work: smb work containing set info request buffer
5952 *
5953 * Return: 0 on success, otherwise error
5954 */
5955int smb2_set_info(struct ksmbd_work *work)
5956{
5957 struct smb2_set_info_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09005958 struct smb2_set_info_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09005959 struct ksmbd_file *fp;
5960 int rc = 0;
5961 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5962
5963 ksmbd_debug(SMB, "Received set info request\n");
5964
Namjae Jeone2f34482021-03-16 10:49:09 +09005965 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09005966 req = ksmbd_req_buf_next(work);
5967 rsp = ksmbd_resp_buf_next(work);
Namjae Jeon38673692021-07-08 12:32:27 +09005968 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
5969 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005970 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005971 id = work->compound_fid;
5972 pid = work->compound_pfid;
5973 }
5974 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09005975 req = smb2_get_msg(work->request_buf);
5976 rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005977 }
5978
Namjae Jeon38673692021-07-08 12:32:27 +09005979 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005980 id = le64_to_cpu(req->VolatileFileId);
5981 pid = le64_to_cpu(req->PersistentFileId);
5982 }
5983
5984 fp = ksmbd_lookup_fd_slow(work, id, pid);
5985 if (!fp) {
5986 ksmbd_debug(SMB, "Invalid id for close: %u\n", id);
5987 rc = -ENOENT;
5988 goto err_out;
5989 }
5990
5991 switch (req->InfoType) {
5992 case SMB2_O_INFO_FILE:
5993 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
Namjae Jeon9496e262021-09-29 15:41:48 +09005994 rc = smb2_set_info_file(work, fp, req, work->tcon->share_conf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005995 break;
5996 case SMB2_O_INFO_SECURITY:
5997 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
Namjae Jeone70e3922021-08-21 23:26:01 +09005998 if (ksmbd_override_fsids(work)) {
5999 rc = -ENOMEM;
6000 goto err_out;
6001 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006002 rc = smb2_set_info_sec(fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09006003 le32_to_cpu(req->AdditionalInformation),
6004 req->Buffer,
6005 le32_to_cpu(req->BufferLength));
Namjae Jeone70e3922021-08-21 23:26:01 +09006006 ksmbd_revert_fsids(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09006007 break;
6008 default:
6009 rc = -EOPNOTSUPP;
6010 }
6011
6012 if (rc < 0)
6013 goto err_out;
6014
6015 rsp->StructureSize = cpu_to_le16(2);
Namjae Jeoncb451722021-11-03 08:08:44 +09006016 inc_rfc1001_len(work->response_buf, 2);
Namjae Jeone2f34482021-03-16 10:49:09 +09006017 ksmbd_fd_put(work, fp);
6018 return 0;
6019
6020err_out:
Hyunchul Lee265fd192021-09-25 00:06:16 +09006021 if (rc == -EACCES || rc == -EPERM || rc == -EXDEV)
Namjae Jeone2f34482021-03-16 10:49:09 +09006022 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6023 else if (rc == -EINVAL)
6024 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6025 else if (rc == -ESHARE)
6026 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6027 else if (rc == -ENOENT)
6028 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
6029 else if (rc == -EBUSY || rc == -ENOTEMPTY)
6030 rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY;
6031 else if (rc == -EAGAIN)
6032 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
Namjae Jeonff1d5722021-04-13 13:18:10 +09006033 else if (rc == -EBADF || rc == -ESTALE)
Namjae Jeone2f34482021-03-16 10:49:09 +09006034 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6035 else if (rc == -EEXIST)
6036 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
6037 else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP)
6038 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
6039 smb2_set_err_rsp(work);
6040 ksmbd_fd_put(work, fp);
Namjae Jeon070fb212021-05-26 17:57:12 +09006041 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09006042 return rc;
6043}
6044
6045/**
6046 * smb2_read_pipe() - handler for smb2 read from IPC pipe
6047 * @work: smb work containing read IPC pipe command buffer
6048 *
6049 * Return: 0 on success, otherwise error
6050 */
6051static noinline int smb2_read_pipe(struct ksmbd_work *work)
6052{
6053 int nbytes = 0, err;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006054 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09006055 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeoncb451722021-11-03 08:08:44 +09006056 struct smb2_read_req *req = smb2_get_msg(work->request_buf);
6057 struct smb2_read_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006058
6059 id = le64_to_cpu(req->VolatileFileId);
6060
Namjae Jeoncb451722021-11-03 08:08:44 +09006061 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006062 rpc_resp = ksmbd_rpc_read(work->sess, id);
6063 if (rpc_resp) {
6064 if (rpc_resp->flags != KSMBD_RPC_OK) {
6065 err = -EINVAL;
6066 goto out;
6067 }
6068
6069 work->aux_payload_buf =
Namjae Jeon79f6b112021-04-02 12:47:14 +09006070 kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006071 if (!work->aux_payload_buf) {
6072 err = -ENOMEM;
6073 goto out;
6074 }
6075
6076 memcpy(work->aux_payload_buf, rpc_resp->payload,
Namjae Jeon070fb212021-05-26 17:57:12 +09006077 rpc_resp->payload_sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09006078
6079 nbytes = rpc_resp->payload_sz;
Namjae Jeoncb451722021-11-03 08:08:44 +09006080 work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09006081 work->aux_payload_sz = nbytes;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006082 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006083 }
6084
6085 rsp->StructureSize = cpu_to_le16(17);
6086 rsp->DataOffset = 80;
6087 rsp->Reserved = 0;
6088 rsp->DataLength = cpu_to_le32(nbytes);
6089 rsp->DataRemaining = 0;
6090 rsp->Reserved2 = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006091 inc_rfc1001_len(work->response_buf, nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09006092 return 0;
6093
6094out:
6095 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
6096 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006097 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006098 return err;
6099}
6100
6101static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006102 struct smb2_read_req *req, void *data_buf,
6103 size_t length)
Namjae Jeone2f34482021-03-16 10:49:09 +09006104{
6105 struct smb2_buffer_desc_v1 *desc =
6106 (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6107 int err;
6108
Namjae Jeon64b39f42021-03-30 14:25:35 +09006109 if (work->conn->dialect == SMB30_PROT_ID &&
6110 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09006111 return -EINVAL;
6112
Namjae Jeon64b39f42021-03-30 14:25:35 +09006113 if (req->ReadChannelInfoOffset == 0 ||
6114 le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09006115 return -EINVAL;
6116
6117 work->need_invalidate_rkey =
6118 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6119 work->remote_key = le32_to_cpu(desc->token);
6120
Namjae Jeon64b39f42021-03-30 14:25:35 +09006121 err = ksmbd_conn_rdma_write(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006122 le32_to_cpu(desc->token),
6123 le64_to_cpu(desc->offset),
6124 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006125 if (err)
6126 return err;
6127
6128 return length;
6129}
6130
6131/**
6132 * smb2_read() - handler for smb2 read from file
6133 * @work: smb work containing read command buffer
6134 *
6135 * Return: 0 on success, otherwise error
6136 */
6137int smb2_read(struct ksmbd_work *work)
6138{
6139 struct ksmbd_conn *conn = work->conn;
6140 struct smb2_read_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006141 struct smb2_read_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09006142 struct ksmbd_file *fp;
6143 loff_t offset;
6144 size_t length, mincount;
6145 ssize_t nbytes = 0, remain_bytes = 0;
6146 int err = 0;
6147
Namjae Jeone2f34482021-03-16 10:49:09 +09006148 WORK_BUFFERS(work, req, rsp);
6149
6150 if (test_share_config_flag(work->tcon->share_conf,
6151 KSMBD_SHARE_FLAG_PIPE)) {
6152 ksmbd_debug(SMB, "IPC pipe read request\n");
6153 return smb2_read_pipe(work);
6154 }
6155
Namjae Jeon070fb212021-05-26 17:57:12 +09006156 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
6157 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006158 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006159 err = -ENOENT;
6160 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006161 }
6162
6163 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006164 pr_err("Not permitted to read : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09006165 err = -EACCES;
6166 goto out;
6167 }
6168
6169 offset = le64_to_cpu(req->Offset);
6170 length = le32_to_cpu(req->Length);
6171 mincount = le32_to_cpu(req->MinimumCount);
6172
6173 if (length > conn->vals->max_read_size) {
6174 ksmbd_debug(SMB, "limiting read size to max size(%u)\n",
6175 conn->vals->max_read_size);
6176 err = -EINVAL;
6177 goto out;
6178 }
6179
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006180 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6181 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006182
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006183 work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006184 if (!work->aux_payload_buf) {
Dan Carpenterc1ea1112021-03-22 17:50:11 +03006185 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09006186 goto out;
6187 }
6188
6189 nbytes = ksmbd_vfs_read(work, fp, length, &offset);
6190 if (nbytes < 0) {
6191 err = nbytes;
6192 goto out;
6193 }
6194
6195 if ((nbytes == 0 && length != 0) || nbytes < mincount) {
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006196 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006197 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006198 rsp->hdr.Status = STATUS_END_OF_FILE;
6199 smb2_set_err_rsp(work);
6200 ksmbd_fd_put(work, fp);
6201 return 0;
6202 }
6203
6204 ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006205 nbytes, offset, mincount);
Namjae Jeone2f34482021-03-16 10:49:09 +09006206
6207 if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006208 req->Channel == SMB2_CHANNEL_RDMA_V1) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006209 /* write data to the client using rdma channel */
6210 remain_bytes = smb2_read_rdma_channel(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09006211 work->aux_payload_buf,
6212 nbytes);
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006213 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006214 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006215
6216 nbytes = 0;
6217 if (remain_bytes < 0) {
6218 err = (int)remain_bytes;
6219 goto out;
6220 }
6221 }
6222
6223 rsp->StructureSize = cpu_to_le16(17);
6224 rsp->DataOffset = 80;
6225 rsp->Reserved = 0;
6226 rsp->DataLength = cpu_to_le32(nbytes);
6227 rsp->DataRemaining = cpu_to_le32(remain_bytes);
6228 rsp->Reserved2 = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006229 inc_rfc1001_len(work->response_buf, 16);
6230 work->resp_hdr_sz = get_rfc1002_len(work->response_buf) + 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09006231 work->aux_payload_sz = nbytes;
Namjae Jeoncb451722021-11-03 08:08:44 +09006232 inc_rfc1001_len(work->response_buf, nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09006233 ksmbd_fd_put(work, fp);
6234 return 0;
6235
6236out:
6237 if (err) {
6238 if (err == -EISDIR)
6239 rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST;
6240 else if (err == -EAGAIN)
6241 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6242 else if (err == -ENOENT)
6243 rsp->hdr.Status = STATUS_FILE_CLOSED;
6244 else if (err == -EACCES)
6245 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6246 else if (err == -ESHARE)
6247 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6248 else if (err == -EINVAL)
6249 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6250 else
6251 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6252
6253 smb2_set_err_rsp(work);
6254 }
6255 ksmbd_fd_put(work, fp);
6256 return err;
6257}
6258
6259/**
6260 * smb2_write_pipe() - handler for smb2 write on IPC pipe
6261 * @work: smb work containing write IPC pipe command buffer
6262 *
6263 * Return: 0 on success, otherwise error
6264 */
6265static noinline int smb2_write_pipe(struct ksmbd_work *work)
6266{
Namjae Jeoncb451722021-11-03 08:08:44 +09006267 struct smb2_write_req *req = smb2_get_msg(work->request_buf);
6268 struct smb2_write_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006269 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006270 u64 id = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09006271 int err = 0, ret = 0;
6272 char *data_buf;
6273 size_t length;
6274
6275 length = le32_to_cpu(req->Length);
6276 id = le64_to_cpu(req->VolatileFileId);
6277
6278 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeoncb451722021-11-03 08:08:44 +09006279 offsetof(struct smb2_write_req, Buffer)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006280 data_buf = (char *)&req->Buffer[0];
6281 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09006282 if ((u64)le16_to_cpu(req->DataOffset) + length >
6283 get_rfc1002_len(work->request_buf)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006284 pr_err("invalid write data offset %u, smb_len %u\n",
6285 le16_to_cpu(req->DataOffset),
Namjae Jeoncb451722021-11-03 08:08:44 +09006286 get_rfc1002_len(work->request_buf));
Namjae Jeone2f34482021-03-16 10:49:09 +09006287 err = -EINVAL;
6288 goto out;
6289 }
6290
6291 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6292 le16_to_cpu(req->DataOffset));
6293 }
6294
6295 rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length);
6296 if (rpc_resp) {
6297 if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
6298 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006299 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006300 smb2_set_err_rsp(work);
6301 return -EOPNOTSUPP;
6302 }
6303 if (rpc_resp->flags != KSMBD_RPC_OK) {
6304 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6305 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006306 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006307 return ret;
6308 }
Namjae Jeon79f6b112021-04-02 12:47:14 +09006309 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006310 }
6311
6312 rsp->StructureSize = cpu_to_le16(17);
6313 rsp->DataOffset = 0;
6314 rsp->Reserved = 0;
6315 rsp->DataLength = cpu_to_le32(length);
6316 rsp->DataRemaining = 0;
6317 rsp->Reserved2 = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006318 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006319 return 0;
6320out:
6321 if (err) {
6322 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6323 smb2_set_err_rsp(work);
6324 }
6325
6326 return err;
6327}
6328
6329static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006330 struct smb2_write_req *req,
6331 struct ksmbd_file *fp,
6332 loff_t offset, size_t length, bool sync)
Namjae Jeone2f34482021-03-16 10:49:09 +09006333{
6334 struct smb2_buffer_desc_v1 *desc;
6335 char *data_buf;
6336 int ret;
6337 ssize_t nbytes;
6338
6339 desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6340
6341 if (work->conn->dialect == SMB30_PROT_ID &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006342 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09006343 return -EINVAL;
6344
6345 if (req->Length != 0 || req->DataOffset != 0)
6346 return -EINVAL;
6347
Namjae Jeon64b39f42021-03-30 14:25:35 +09006348 if (req->WriteChannelInfoOffset == 0 ||
6349 le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09006350 return -EINVAL;
6351
6352 work->need_invalidate_rkey =
6353 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6354 work->remote_key = le32_to_cpu(desc->token);
6355
Namjae Jeon79f6b112021-04-02 12:47:14 +09006356 data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006357 if (!data_buf)
6358 return -ENOMEM;
6359
6360 ret = ksmbd_conn_rdma_read(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006361 le32_to_cpu(desc->token),
6362 le64_to_cpu(desc->offset),
6363 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006364 if (ret < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09006365 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006366 return ret;
6367 }
6368
Namjae Jeon64b39f42021-03-30 14:25:35 +09006369 ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006370 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006371 if (ret < 0)
6372 return ret;
6373
6374 return nbytes;
6375}
6376
6377/**
6378 * smb2_write() - handler for smb2 write from file
6379 * @work: smb work containing write command buffer
6380 *
6381 * Return: 0 on success, otherwise error
6382 */
6383int smb2_write(struct ksmbd_work *work)
6384{
6385 struct smb2_write_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006386 struct smb2_write_rsp *rsp;
Namjae Jeonbcd62a32021-05-10 09:08:19 +09006387 struct ksmbd_file *fp = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006388 loff_t offset;
6389 size_t length;
6390 ssize_t nbytes;
6391 char *data_buf;
6392 bool writethrough = false;
6393 int err = 0;
6394
Namjae Jeone2f34482021-03-16 10:49:09 +09006395 WORK_BUFFERS(work, req, rsp);
6396
Namjae Jeon64b39f42021-03-30 14:25:35 +09006397 if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006398 ksmbd_debug(SMB, "IPC pipe write request\n");
6399 return smb2_write_pipe(work);
6400 }
6401
6402 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
6403 ksmbd_debug(SMB, "User does not have write permission\n");
6404 err = -EACCES;
6405 goto out;
6406 }
6407
Namjae Jeon64b39f42021-03-30 14:25:35 +09006408 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
Namjae Jeon070fb212021-05-26 17:57:12 +09006409 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006410 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006411 err = -ENOENT;
6412 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006413 }
6414
6415 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006416 pr_err("Not permitted to write : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09006417 err = -EACCES;
6418 goto out;
6419 }
6420
6421 offset = le64_to_cpu(req->Offset);
6422 length = le32_to_cpu(req->Length);
6423
6424 if (length > work->conn->vals->max_write_size) {
6425 ksmbd_debug(SMB, "limiting write size to max size(%u)\n",
6426 work->conn->vals->max_write_size);
6427 err = -EINVAL;
6428 goto out;
6429 }
6430
6431 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6432 writethrough = true;
6433
6434 if (req->Channel != SMB2_CHANNEL_RDMA_V1 &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006435 req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006436 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeoncb451722021-11-03 08:08:44 +09006437 offsetof(struct smb2_write_req, Buffer)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006438 data_buf = (char *)&req->Buffer[0];
6439 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09006440 if ((u64)le16_to_cpu(req->DataOffset) + length >
6441 get_rfc1002_len(work->request_buf)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006442 pr_err("invalid write data offset %u, smb_len %u\n",
6443 le16_to_cpu(req->DataOffset),
Namjae Jeoncb451722021-11-03 08:08:44 +09006444 get_rfc1002_len(work->request_buf));
Namjae Jeone2f34482021-03-16 10:49:09 +09006445 err = -EINVAL;
6446 goto out;
6447 }
6448
6449 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6450 le16_to_cpu(req->DataOffset));
6451 }
6452
6453 ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags));
6454 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6455 writethrough = true;
6456
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006457 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6458 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006459 err = ksmbd_vfs_write(work, fp, data_buf, length, &offset,
6460 writethrough, &nbytes);
6461 if (err < 0)
6462 goto out;
6463 } else {
6464 /* read data from the client using rdma channel, and
6465 * write the data.
6466 */
6467 nbytes = smb2_write_rdma_channel(work, req, fp, offset,
Namjae Jeon070fb212021-05-26 17:57:12 +09006468 le32_to_cpu(req->RemainingBytes),
6469 writethrough);
Namjae Jeone2f34482021-03-16 10:49:09 +09006470 if (nbytes < 0) {
6471 err = (int)nbytes;
6472 goto out;
6473 }
6474 }
6475
6476 rsp->StructureSize = cpu_to_le16(17);
6477 rsp->DataOffset = 0;
6478 rsp->Reserved = 0;
6479 rsp->DataLength = cpu_to_le32(nbytes);
6480 rsp->DataRemaining = 0;
6481 rsp->Reserved2 = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006482 inc_rfc1001_len(work->response_buf, 16);
Namjae Jeone2f34482021-03-16 10:49:09 +09006483 ksmbd_fd_put(work, fp);
6484 return 0;
6485
6486out:
6487 if (err == -EAGAIN)
6488 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6489 else if (err == -ENOSPC || err == -EFBIG)
6490 rsp->hdr.Status = STATUS_DISK_FULL;
6491 else if (err == -ENOENT)
6492 rsp->hdr.Status = STATUS_FILE_CLOSED;
6493 else if (err == -EACCES)
6494 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6495 else if (err == -ESHARE)
6496 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6497 else if (err == -EINVAL)
6498 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6499 else
6500 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6501
6502 smb2_set_err_rsp(work);
6503 ksmbd_fd_put(work, fp);
6504 return err;
6505}
6506
6507/**
6508 * smb2_flush() - handler for smb2 flush file - fsync
6509 * @work: smb work containing flush command buffer
6510 *
6511 * Return: 0 on success, otherwise error
6512 */
6513int smb2_flush(struct ksmbd_work *work)
6514{
6515 struct smb2_flush_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09006516 struct smb2_flush_rsp *rsp;
Namjae Jeone2f34482021-03-16 10:49:09 +09006517 int err;
6518
Namjae Jeone2f34482021-03-16 10:49:09 +09006519 WORK_BUFFERS(work, req, rsp);
6520
6521 ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006522 le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006523
6524 err = ksmbd_vfs_fsync(work,
6525 le64_to_cpu(req->VolatileFileId),
6526 le64_to_cpu(req->PersistentFileId));
6527 if (err)
6528 goto out;
6529
6530 rsp->StructureSize = cpu_to_le16(4);
6531 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09006532 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09006533 return 0;
6534
6535out:
6536 if (err) {
6537 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6538 smb2_set_err_rsp(work);
6539 }
6540
6541 return err;
6542}
6543
6544/**
6545 * smb2_cancel() - handler for smb2 cancel command
6546 * @work: smb work containing cancel command buffer
6547 *
6548 * Return: 0 on success, otherwise error
6549 */
6550int smb2_cancel(struct ksmbd_work *work)
6551{
6552 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09006553 struct smb2_hdr *hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006554 struct smb2_hdr *chdr;
6555 struct ksmbd_work *cancel_work = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006556 int canceled = 0;
6557 struct list_head *command_list;
6558
6559 ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006560 hdr->MessageId, hdr->Flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09006561
6562 if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) {
6563 command_list = &conn->async_requests;
6564
6565 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006566 list_for_each_entry(cancel_work, command_list,
6567 async_request_entry) {
Namjae Jeoncb451722021-11-03 08:08:44 +09006568 chdr = smb2_get_msg(cancel_work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006569
6570 if (cancel_work->async_id !=
Namjae Jeon64b39f42021-03-30 14:25:35 +09006571 le64_to_cpu(hdr->Id.AsyncId))
Namjae Jeone2f34482021-03-16 10:49:09 +09006572 continue;
6573
6574 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006575 "smb2 with AsyncId %llu cancelled command = 0x%x\n",
6576 le64_to_cpu(hdr->Id.AsyncId),
6577 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006578 canceled = 1;
6579 break;
6580 }
6581 spin_unlock(&conn->request_lock);
6582 } else {
6583 command_list = &conn->requests;
6584
6585 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006586 list_for_each_entry(cancel_work, command_list, request_entry) {
Namjae Jeoncb451722021-11-03 08:08:44 +09006587 chdr = smb2_get_msg(cancel_work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006588
6589 if (chdr->MessageId != hdr->MessageId ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006590 cancel_work == work)
Namjae Jeone2f34482021-03-16 10:49:09 +09006591 continue;
6592
6593 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006594 "smb2 with mid %llu cancelled command = 0x%x\n",
6595 le64_to_cpu(hdr->MessageId),
6596 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006597 canceled = 1;
6598 break;
6599 }
6600 spin_unlock(&conn->request_lock);
6601 }
6602
6603 if (canceled) {
6604 cancel_work->state = KSMBD_WORK_CANCELLED;
6605 if (cancel_work->cancel_fn)
6606 cancel_work->cancel_fn(cancel_work->cancel_argv);
6607 }
6608
6609 /* For SMB2_CANCEL command itself send no response*/
6610 work->send_no_response = 1;
6611 return 0;
6612}
6613
6614struct file_lock *smb_flock_init(struct file *f)
6615{
6616 struct file_lock *fl;
6617
6618 fl = locks_alloc_lock();
6619 if (!fl)
6620 goto out;
6621
6622 locks_init_lock(fl);
6623
6624 fl->fl_owner = f;
6625 fl->fl_pid = current->tgid;
6626 fl->fl_file = f;
6627 fl->fl_flags = FL_POSIX;
6628 fl->fl_ops = NULL;
6629 fl->fl_lmops = NULL;
6630
6631out:
6632 return fl;
6633}
6634
6635static int smb2_set_flock_flags(struct file_lock *flock, int flags)
6636{
6637 int cmd = -EINVAL;
6638
6639 /* Checking for wrong flag combination during lock request*/
6640 switch (flags) {
6641 case SMB2_LOCKFLAG_SHARED:
6642 ksmbd_debug(SMB, "received shared request\n");
6643 cmd = F_SETLKW;
6644 flock->fl_type = F_RDLCK;
6645 flock->fl_flags |= FL_SLEEP;
6646 break;
6647 case SMB2_LOCKFLAG_EXCLUSIVE:
6648 ksmbd_debug(SMB, "received exclusive request\n");
6649 cmd = F_SETLKW;
6650 flock->fl_type = F_WRLCK;
6651 flock->fl_flags |= FL_SLEEP;
6652 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006653 case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006654 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006655 "received shared & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006656 cmd = F_SETLK;
6657 flock->fl_type = F_RDLCK;
6658 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006659 case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006660 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006661 "received exclusive & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006662 cmd = F_SETLK;
6663 flock->fl_type = F_WRLCK;
6664 break;
6665 case SMB2_LOCKFLAG_UNLOCK:
6666 ksmbd_debug(SMB, "received unlock request\n");
6667 flock->fl_type = F_UNLCK;
6668 cmd = 0;
6669 break;
6670 }
6671
6672 return cmd;
6673}
6674
6675static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock,
Namjae Jeon070fb212021-05-26 17:57:12 +09006676 unsigned int cmd, int flags,
6677 struct list_head *lock_list)
Namjae Jeone2f34482021-03-16 10:49:09 +09006678{
6679 struct ksmbd_lock *lock;
6680
6681 lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL);
6682 if (!lock)
6683 return NULL;
6684
6685 lock->cmd = cmd;
6686 lock->fl = flock;
6687 lock->start = flock->fl_start;
6688 lock->end = flock->fl_end;
6689 lock->flags = flags;
6690 if (lock->start == lock->end)
6691 lock->zero_len = 1;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006692 INIT_LIST_HEAD(&lock->clist);
6693 INIT_LIST_HEAD(&lock->flist);
Namjae Jeone2f34482021-03-16 10:49:09 +09006694 INIT_LIST_HEAD(&lock->llist);
Namjae Jeone2f34482021-03-16 10:49:09 +09006695 list_add_tail(&lock->llist, lock_list);
6696
6697 return lock;
6698}
6699
6700static void smb2_remove_blocked_lock(void **argv)
6701{
6702 struct file_lock *flock = (struct file_lock *)argv[0];
6703
6704 ksmbd_vfs_posix_lock_unblock(flock);
6705 wake_up(&flock->fl_wait);
6706}
6707
6708static inline bool lock_defer_pending(struct file_lock *fl)
6709{
6710 /* check pending lock waiters */
6711 return waitqueue_active(&fl->fl_wait);
6712}
6713
6714/**
6715 * smb2_lock() - handler for smb2 file lock command
6716 * @work: smb work containing lock command buffer
6717 *
6718 * Return: 0 on success, otherwise error
6719 */
6720int smb2_lock(struct ksmbd_work *work)
6721{
Namjae Jeoncb451722021-11-03 08:08:44 +09006722 struct smb2_lock_req *req = smb2_get_msg(work->request_buf);
6723 struct smb2_lock_rsp *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006724 struct smb2_lock_element *lock_ele;
6725 struct ksmbd_file *fp = NULL;
6726 struct file_lock *flock = NULL;
6727 struct file *filp = NULL;
6728 int lock_count;
6729 int flags = 0;
6730 int cmd = 0;
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006731 int err = -EIO, i, rc = 0;
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006732 u64 lock_start, lock_length;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006733 struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2;
6734 struct ksmbd_conn *conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09006735 int nolock = 0;
6736 LIST_HEAD(lock_list);
6737 LIST_HEAD(rollback_list);
6738 int prior_lock = 0;
6739
6740 ksmbd_debug(SMB, "Received lock request\n");
6741 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006742 le64_to_cpu(req->VolatileFileId),
6743 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006744 if (!fp) {
6745 ksmbd_debug(SMB, "Invalid file id for lock : %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006746 le64_to_cpu(req->VolatileFileId));
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006747 err = -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09006748 goto out2;
6749 }
6750
6751 filp = fp->filp;
6752 lock_count = le16_to_cpu(req->LockCount);
6753 lock_ele = req->locks;
6754
6755 ksmbd_debug(SMB, "lock count is %d\n", lock_count);
Namjae Jeon070fb212021-05-26 17:57:12 +09006756 if (!lock_count) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006757 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006758 goto out2;
6759 }
6760
6761 for (i = 0; i < lock_count; i++) {
6762 flags = le32_to_cpu(lock_ele[i].Flags);
6763
6764 flock = smb_flock_init(filp);
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006765 if (!flock)
Namjae Jeone2f34482021-03-16 10:49:09 +09006766 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006767
6768 cmd = smb2_set_flock_flags(flock, flags);
6769
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006770 lock_start = le64_to_cpu(lock_ele[i].Offset);
6771 lock_length = le64_to_cpu(lock_ele[i].Length);
6772 if (lock_start > U64_MAX - lock_length) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006773 pr_err("Invalid lock range requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006774 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6775 goto out;
6776 }
6777
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006778 if (lock_start > OFFSET_MAX)
6779 flock->fl_start = OFFSET_MAX;
6780 else
6781 flock->fl_start = lock_start;
6782
Namjae Jeone2f34482021-03-16 10:49:09 +09006783 lock_length = le64_to_cpu(lock_ele[i].Length);
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006784 if (lock_length > OFFSET_MAX - flock->fl_start)
6785 lock_length = OFFSET_MAX - flock->fl_start;
Namjae Jeone2f34482021-03-16 10:49:09 +09006786
6787 flock->fl_end = flock->fl_start + lock_length;
6788
6789 if (flock->fl_end < flock->fl_start) {
6790 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006791 "the end offset(%llx) is smaller than the start offset(%llx)\n",
6792 flock->fl_end, flock->fl_start);
Namjae Jeone2f34482021-03-16 10:49:09 +09006793 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6794 goto out;
6795 }
6796
6797 /* Check conflict locks in one request */
6798 list_for_each_entry(cmp_lock, &lock_list, llist) {
6799 if (cmp_lock->fl->fl_start <= flock->fl_start &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006800 cmp_lock->fl->fl_end >= flock->fl_end) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006801 if (cmp_lock->fl->fl_type != F_UNLCK &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006802 flock->fl_type != F_UNLCK) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006803 pr_err("conflict two locks in one request\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006804 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006805 goto out;
6806 }
6807 }
6808 }
6809
6810 smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list);
6811 if (!smb_lock) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006812 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006813 goto out;
6814 }
6815 }
6816
6817 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6818 if (smb_lock->cmd < 0) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006819 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006820 goto out;
6821 }
6822
6823 if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006824 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006825 goto out;
6826 }
6827
Namjae Jeon64b39f42021-03-30 14:25:35 +09006828 if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) &&
6829 smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) ||
6830 (prior_lock == SMB2_LOCKFLAG_UNLOCK &&
6831 !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006832 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006833 goto out;
6834 }
6835
6836 prior_lock = smb_lock->flags;
6837
6838 if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006839 !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY))
Hyunchul Leed63528e2021-07-10 16:22:41 +09006840 goto no_check_cl;
Namjae Jeone2f34482021-03-16 10:49:09 +09006841
6842 nolock = 1;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006843 /* check locks in connection list */
6844 read_lock(&conn_list_lock);
6845 list_for_each_entry(conn, &conn_list, conns_list) {
6846 spin_lock(&conn->llist_lock);
6847 list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) {
6848 if (file_inode(cmp_lock->fl->fl_file) !=
6849 file_inode(smb_lock->fl->fl_file))
6850 continue;
Namjae Jeone2f34482021-03-16 10:49:09 +09006851
Hyunchul Leed63528e2021-07-10 16:22:41 +09006852 if (smb_lock->fl->fl_type == F_UNLCK) {
6853 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file &&
6854 cmp_lock->start == smb_lock->start &&
6855 cmp_lock->end == smb_lock->end &&
6856 !lock_defer_pending(cmp_lock->fl)) {
6857 nolock = 0;
6858 list_del(&cmp_lock->flist);
6859 list_del(&cmp_lock->clist);
6860 spin_unlock(&conn->llist_lock);
6861 read_unlock(&conn_list_lock);
6862
6863 locks_free_lock(cmp_lock->fl);
6864 kfree(cmp_lock);
6865 goto out_check_cl;
6866 }
6867 continue;
Namjae Jeone2f34482021-03-16 10:49:09 +09006868 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006869
Hyunchul Leed63528e2021-07-10 16:22:41 +09006870 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) {
6871 if (smb_lock->flags & SMB2_LOCKFLAG_SHARED)
6872 continue;
6873 } else {
6874 if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED)
6875 continue;
6876 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006877
Hyunchul Leed63528e2021-07-10 16:22:41 +09006878 /* check zero byte lock range */
6879 if (cmp_lock->zero_len && !smb_lock->zero_len &&
6880 cmp_lock->start > smb_lock->start &&
6881 cmp_lock->start < smb_lock->end) {
6882 spin_unlock(&conn->llist_lock);
6883 read_unlock(&conn_list_lock);
6884 pr_err("previous lock conflict with zero byte lock range\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006885 goto out;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006886 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006887
Hyunchul Leed63528e2021-07-10 16:22:41 +09006888 if (smb_lock->zero_len && !cmp_lock->zero_len &&
6889 smb_lock->start > cmp_lock->start &&
6890 smb_lock->start < cmp_lock->end) {
6891 spin_unlock(&conn->llist_lock);
6892 read_unlock(&conn_list_lock);
6893 pr_err("current lock conflict with zero byte lock range\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006894 goto out;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006895 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006896
Hyunchul Leed63528e2021-07-10 16:22:41 +09006897 if (((cmp_lock->start <= smb_lock->start &&
6898 cmp_lock->end > smb_lock->start) ||
6899 (cmp_lock->start < smb_lock->end &&
6900 cmp_lock->end >= smb_lock->end)) &&
6901 !cmp_lock->zero_len && !smb_lock->zero_len) {
6902 spin_unlock(&conn->llist_lock);
6903 read_unlock(&conn_list_lock);
6904 pr_err("Not allow lock operation on exclusive lock range\n");
Hyunchul Leed63528e2021-07-10 16:22:41 +09006905 goto out;
6906 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006907 }
Hyunchul Leed63528e2021-07-10 16:22:41 +09006908 spin_unlock(&conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006909 }
Hyunchul Leed63528e2021-07-10 16:22:41 +09006910 read_unlock(&conn_list_lock);
6911out_check_cl:
Namjae Jeone2f34482021-03-16 10:49:09 +09006912 if (smb_lock->fl->fl_type == F_UNLCK && nolock) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006913 pr_err("Try to unlock nolocked range\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006914 rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED;
6915 goto out;
6916 }
6917
Hyunchul Leed63528e2021-07-10 16:22:41 +09006918no_check_cl:
Namjae Jeone2f34482021-03-16 10:49:09 +09006919 if (smb_lock->zero_len) {
6920 err = 0;
6921 goto skip;
6922 }
6923
6924 flock = smb_lock->fl;
6925 list_del(&smb_lock->llist);
6926retry:
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006927 rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09006928skip:
6929 if (flags & SMB2_LOCKFLAG_UNLOCK) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006930 if (!rc) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006931 ksmbd_debug(SMB, "File unlocked\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006932 } else if (rc == -ENOENT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006933 rsp->hdr.Status = STATUS_NOT_LOCKED;
6934 goto out;
6935 }
6936 locks_free_lock(flock);
6937 kfree(smb_lock);
6938 } else {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006939 if (rc == FILE_LOCK_DEFERRED) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006940 void **argv;
6941
6942 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006943 "would have to wait for getting lock\n");
Hyunchul Leed63528e2021-07-10 16:22:41 +09006944 spin_lock(&work->conn->llist_lock);
6945 list_add_tail(&smb_lock->clist,
6946 &work->conn->lock_list);
6947 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006948 list_add(&smb_lock->llist, &rollback_list);
6949
6950 argv = kmalloc(sizeof(void *), GFP_KERNEL);
6951 if (!argv) {
6952 err = -ENOMEM;
6953 goto out;
6954 }
6955 argv[0] = flock;
6956
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006957 rc = setup_async_work(work,
6958 smb2_remove_blocked_lock,
6959 argv);
6960 if (rc) {
6961 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09006962 goto out;
6963 }
6964 spin_lock(&fp->f_lock);
6965 list_add(&work->fp_entry, &fp->blocked_works);
6966 spin_unlock(&fp->f_lock);
6967
6968 smb2_send_interim_resp(work, STATUS_PENDING);
6969
Hyunchul Lee45a64e82021-07-10 09:34:20 +09006970 ksmbd_vfs_posix_lock_wait(flock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006971
Hyunchul Leed4075ab2021-06-25 07:02:10 +09006972 if (work->state != KSMBD_WORK_ACTIVE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006973 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09006974 spin_lock(&work->conn->llist_lock);
6975 list_del(&smb_lock->clist);
6976 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006977 locks_free_lock(flock);
6978
Hyunchul Leed4075ab2021-06-25 07:02:10 +09006979 if (work->state == KSMBD_WORK_CANCELLED) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006980 spin_lock(&fp->f_lock);
6981 list_del(&work->fp_entry);
6982 spin_unlock(&fp->f_lock);
6983 rsp->hdr.Status =
6984 STATUS_CANCELLED;
6985 kfree(smb_lock);
6986 smb2_send_interim_resp(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006987 STATUS_CANCELLED);
Namjae Jeone2f34482021-03-16 10:49:09 +09006988 work->send_no_response = 1;
6989 goto out;
6990 }
6991 init_smb2_rsp_hdr(work);
6992 smb2_set_err_rsp(work);
6993 rsp->hdr.Status =
6994 STATUS_RANGE_NOT_LOCKED;
6995 kfree(smb_lock);
6996 goto out2;
6997 }
6998
6999 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007000 spin_lock(&work->conn->llist_lock);
7001 list_del(&smb_lock->clist);
7002 spin_unlock(&work->conn->llist_lock);
7003
Namjae Jeone2f34482021-03-16 10:49:09 +09007004 spin_lock(&fp->f_lock);
7005 list_del(&work->fp_entry);
7006 spin_unlock(&fp->f_lock);
7007 goto retry;
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09007008 } else if (!rc) {
Hyunchul Leed63528e2021-07-10 16:22:41 +09007009 spin_lock(&work->conn->llist_lock);
7010 list_add_tail(&smb_lock->clist,
7011 &work->conn->lock_list);
7012 list_add_tail(&smb_lock->flist,
7013 &fp->lock_list);
7014 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09007015 list_add(&smb_lock->llist, &rollback_list);
7016 ksmbd_debug(SMB, "successful in taking lock\n");
7017 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007018 goto out;
7019 }
7020 }
7021 }
7022
7023 if (atomic_read(&fp->f_ci->op_count) > 1)
7024 smb_break_all_oplock(work, fp);
7025
7026 rsp->StructureSize = cpu_to_le16(4);
7027 ksmbd_debug(SMB, "successful in taking lock\n");
7028 rsp->hdr.Status = STATUS_SUCCESS;
7029 rsp->Reserved = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09007030 inc_rfc1001_len(work->response_buf, 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09007031 ksmbd_fd_put(work, fp);
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007032 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09007033
7034out:
7035 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
7036 locks_free_lock(smb_lock->fl);
7037 list_del(&smb_lock->llist);
7038 kfree(smb_lock);
7039 }
7040
7041 list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) {
7042 struct file_lock *rlock = NULL;
7043
7044 rlock = smb_flock_init(filp);
7045 rlock->fl_type = F_UNLCK;
7046 rlock->fl_start = smb_lock->start;
7047 rlock->fl_end = smb_lock->end;
7048
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007049 rc = vfs_lock_file(filp, 0, rlock, NULL);
7050 if (rc)
7051 pr_err("rollback unlock fail : %d\n", rc);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007052
Namjae Jeone2f34482021-03-16 10:49:09 +09007053 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09007054 spin_lock(&work->conn->llist_lock);
7055 if (!list_empty(&smb_lock->flist))
7056 list_del(&smb_lock->flist);
7057 list_del(&smb_lock->clist);
7058 spin_unlock(&work->conn->llist_lock);
7059
Namjae Jeone2f34482021-03-16 10:49:09 +09007060 locks_free_lock(smb_lock->fl);
7061 locks_free_lock(rlock);
7062 kfree(smb_lock);
7063 }
7064out2:
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09007065 ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err);
7066
7067 if (!rsp->hdr.Status) {
7068 if (err == -EINVAL)
7069 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7070 else if (err == -ENOMEM)
7071 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
7072 else if (err == -ENOENT)
7073 rsp->hdr.Status = STATUS_FILE_CLOSED;
7074 else
7075 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
7076 }
7077
Namjae Jeone2f34482021-03-16 10:49:09 +09007078 smb2_set_err_rsp(work);
7079 ksmbd_fd_put(work, fp);
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09007080 return err;
Namjae Jeone2f34482021-03-16 10:49:09 +09007081}
7082
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007083static int fsctl_copychunk(struct ksmbd_work *work,
7084 struct copychunk_ioctl_req *ci_req,
7085 unsigned int cnt_code,
7086 unsigned int input_count,
7087 unsigned long long volatile_id,
7088 unsigned long long persistent_id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007089 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007090{
Namjae Jeone2f34482021-03-16 10:49:09 +09007091 struct copychunk_ioctl_rsp *ci_rsp;
7092 struct ksmbd_file *src_fp = NULL, *dst_fp = NULL;
7093 struct srv_copychunk *chunks;
7094 unsigned int i, chunk_count, chunk_count_written = 0;
7095 unsigned int chunk_size_written = 0;
7096 loff_t total_size_written = 0;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007097 int ret = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09007098
Namjae Jeone2f34482021-03-16 10:49:09 +09007099 ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0];
7100
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007101 rsp->VolatileFileId = cpu_to_le64(volatile_id);
7102 rsp->PersistentFileId = cpu_to_le64(persistent_id);
Namjae Jeon64b39f42021-03-30 14:25:35 +09007103 ci_rsp->ChunksWritten =
7104 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count());
7105 ci_rsp->ChunkBytesWritten =
7106 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size());
7107 ci_rsp->TotalBytesWritten =
7108 cpu_to_le32(ksmbd_server_side_copy_max_total_size());
Namjae Jeone2f34482021-03-16 10:49:09 +09007109
7110 chunks = (struct srv_copychunk *)&ci_req->Chunks[0];
7111 chunk_count = le32_to_cpu(ci_req->ChunkCount);
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007112 if (chunk_count == 0)
7113 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09007114 total_size_written = 0;
7115
7116 /* verify the SRV_COPYCHUNK_COPY packet */
7117 if (chunk_count > ksmbd_server_side_copy_max_chunk_count() ||
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007118 input_count < offsetof(struct copychunk_ioctl_req, Chunks) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09007119 chunk_count * sizeof(struct srv_copychunk)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007120 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7121 return -EINVAL;
7122 }
7123
7124 for (i = 0; i < chunk_count; i++) {
7125 if (le32_to_cpu(chunks[i].Length) == 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09007126 le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size())
Namjae Jeone2f34482021-03-16 10:49:09 +09007127 break;
7128 total_size_written += le32_to_cpu(chunks[i].Length);
7129 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09007130
7131 if (i < chunk_count ||
7132 total_size_written > ksmbd_server_side_copy_max_total_size()) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007133 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7134 return -EINVAL;
7135 }
7136
7137 src_fp = ksmbd_lookup_foreign_fd(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007138 le64_to_cpu(ci_req->ResumeKey[0]));
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007139 dst_fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
Namjae Jeone2f34482021-03-16 10:49:09 +09007140 ret = -EINVAL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007141 if (!src_fp ||
7142 src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007143 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7144 goto out;
7145 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09007146
Namjae Jeone2f34482021-03-16 10:49:09 +09007147 if (!dst_fp) {
7148 rsp->hdr.Status = STATUS_FILE_CLOSED;
7149 goto out;
7150 }
7151
7152 /*
7153 * FILE_READ_DATA should only be included in
7154 * the FSCTL_COPYCHUNK case
7155 */
Namjae Jeon070fb212021-05-26 17:57:12 +09007156 if (cnt_code == FSCTL_COPYCHUNK &&
7157 !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007158 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7159 goto out;
7160 }
7161
7162 ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007163 chunks, chunk_count,
7164 &chunk_count_written,
7165 &chunk_size_written,
7166 &total_size_written);
Namjae Jeone2f34482021-03-16 10:49:09 +09007167 if (ret < 0) {
7168 if (ret == -EACCES)
7169 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7170 if (ret == -EAGAIN)
7171 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
7172 else if (ret == -EBADF)
7173 rsp->hdr.Status = STATUS_INVALID_HANDLE;
7174 else if (ret == -EFBIG || ret == -ENOSPC)
7175 rsp->hdr.Status = STATUS_DISK_FULL;
7176 else if (ret == -EINVAL)
7177 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7178 else if (ret == -EISDIR)
7179 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
7180 else if (ret == -E2BIG)
7181 rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE;
7182 else
7183 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
7184 }
7185
7186 ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written);
7187 ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written);
7188 ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written);
7189out:
7190 ksmbd_fd_put(work, src_fp);
7191 ksmbd_fd_put(work, dst_fp);
7192 return ret;
7193}
7194
7195static __be32 idev_ipv4_address(struct in_device *idev)
7196{
7197 __be32 addr = 0;
7198
7199 struct in_ifaddr *ifa;
7200
7201 rcu_read_lock();
7202 in_dev_for_each_ifa_rcu(ifa, idev) {
7203 if (ifa->ifa_flags & IFA_F_SECONDARY)
7204 continue;
7205
7206 addr = ifa->ifa_address;
7207 break;
7208 }
7209 rcu_read_unlock();
7210 return addr;
7211}
7212
7213static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007214 struct smb2_ioctl_rsp *rsp,
7215 unsigned int out_buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09007216{
7217 struct network_interface_info_ioctl_rsp *nii_rsp = NULL;
7218 int nbytes = 0;
7219 struct net_device *netdev;
7220 struct sockaddr_storage_rsp *sockaddr_storage;
7221 unsigned int flags;
7222 unsigned long long speed;
Namjae Jeonf1abdb72021-07-27 13:25:13 +09007223 struct sockaddr_in6 *csin6 = (struct sockaddr_in6 *)&conn->peer_addr;
Namjae Jeone2f34482021-03-16 10:49:09 +09007224
7225 rtnl_lock();
7226 for_each_netdev(&init_net, netdev) {
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007227 if (out_buf_len <
7228 nbytes + sizeof(struct network_interface_info_ioctl_rsp)) {
7229 rtnl_unlock();
7230 return -ENOSPC;
7231 }
7232
Namjae Jeone2f34482021-03-16 10:49:09 +09007233 if (netdev->type == ARPHRD_LOOPBACK)
7234 continue;
7235
7236 flags = dev_get_flags(netdev);
7237 if (!(flags & IFF_RUNNING))
7238 continue;
7239
7240 nii_rsp = (struct network_interface_info_ioctl_rsp *)
7241 &rsp->Buffer[nbytes];
7242 nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex);
7243
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09007244 nii_rsp->Capability = 0;
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09007245 if (ksmbd_rdma_capable_netdev(netdev))
7246 nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE);
Namjae Jeone2f34482021-03-16 10:49:09 +09007247
7248 nii_rsp->Next = cpu_to_le32(152);
7249 nii_rsp->Reserved = 0;
7250
7251 if (netdev->ethtool_ops->get_link_ksettings) {
7252 struct ethtool_link_ksettings cmd;
7253
7254 netdev->ethtool_ops->get_link_ksettings(netdev, &cmd);
7255 speed = cmd.base.speed;
7256 } else {
Per Forlind4758662021-08-30 13:23:04 +09007257 ksmbd_debug(SMB, "%s %s\n", netdev->name,
7258 "speed is unknown, defaulting to 1Gb/sec");
Namjae Jeone2f34482021-03-16 10:49:09 +09007259 speed = SPEED_1000;
7260 }
7261
7262 speed *= 1000000;
7263 nii_rsp->LinkSpeed = cpu_to_le64(speed);
7264
7265 sockaddr_storage = (struct sockaddr_storage_rsp *)
7266 nii_rsp->SockAddr_Storage;
7267 memset(sockaddr_storage, 0, 128);
7268
Namjae Jeonf1abdb72021-07-27 13:25:13 +09007269 if (conn->peer_addr.ss_family == PF_INET ||
7270 ipv6_addr_v4mapped(&csin6->sin6_addr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007271 struct in_device *idev;
7272
7273 sockaddr_storage->Family = cpu_to_le16(INTERNETWORK);
7274 sockaddr_storage->addr4.Port = 0;
7275
7276 idev = __in_dev_get_rtnl(netdev);
7277 if (!idev)
7278 continue;
7279 sockaddr_storage->addr4.IPv4address =
7280 idev_ipv4_address(idev);
7281 } else {
7282 struct inet6_dev *idev6;
7283 struct inet6_ifaddr *ifa;
7284 __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address;
7285
7286 sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6);
7287 sockaddr_storage->addr6.Port = 0;
7288 sockaddr_storage->addr6.FlowInfo = 0;
7289
7290 idev6 = __in6_dev_get(netdev);
7291 if (!idev6)
7292 continue;
7293
7294 list_for_each_entry(ifa, &idev6->addr_list, if_list) {
7295 if (ifa->flags & (IFA_F_TENTATIVE |
7296 IFA_F_DEPRECATED))
7297 continue;
7298 memcpy(ipv6_addr, ifa->addr.s6_addr, 16);
7299 break;
7300 }
7301 sockaddr_storage->addr6.ScopeId = 0;
7302 }
7303
7304 nbytes += sizeof(struct network_interface_info_ioctl_rsp);
7305 }
7306 rtnl_unlock();
7307
7308 /* zero if this is last one */
7309 if (nii_rsp)
7310 nii_rsp->Next = 0;
7311
Namjae Jeone2f34482021-03-16 10:49:09 +09007312 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7313 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7314 return nbytes;
7315}
7316
Namjae Jeone2f34482021-03-16 10:49:09 +09007317static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09007318 struct validate_negotiate_info_req *neg_req,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007319 struct validate_negotiate_info_rsp *neg_rsp,
7320 unsigned int in_buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09007321{
7322 int ret = 0;
7323 int dialect;
7324
Marios Makassikis78f16882021-10-28 21:01:27 +02007325 if (in_buf_len < offsetof(struct validate_negotiate_info_req, Dialects) +
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007326 le16_to_cpu(neg_req->DialectCount) * sizeof(__le16))
7327 return -EINVAL;
7328
Namjae Jeone2f34482021-03-16 10:49:09 +09007329 dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects,
Namjae Jeon070fb212021-05-26 17:57:12 +09007330 neg_req->DialectCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09007331 if (dialect == BAD_PROT_ID || dialect != conn->dialect) {
7332 ret = -EINVAL;
7333 goto err_out;
7334 }
7335
7336 if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) {
7337 ret = -EINVAL;
7338 goto err_out;
7339 }
7340
7341 if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) {
7342 ret = -EINVAL;
7343 goto err_out;
7344 }
7345
7346 if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) {
7347 ret = -EINVAL;
7348 goto err_out;
7349 }
7350
7351 neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
7352 memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE);
7353 neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode);
7354 neg_rsp->Dialect = cpu_to_le16(conn->dialect);
7355err_out:
7356 return ret;
7357}
7358
Namjae Jeon64b39f42021-03-30 14:25:35 +09007359static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007360 struct file_allocated_range_buffer *qar_req,
7361 struct file_allocated_range_buffer *qar_rsp,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007362 unsigned int in_count, unsigned int *out_count)
Namjae Jeone2f34482021-03-16 10:49:09 +09007363{
7364 struct ksmbd_file *fp;
7365 loff_t start, length;
7366 int ret = 0;
7367
7368 *out_count = 0;
7369 if (in_count == 0)
7370 return -EINVAL;
7371
7372 fp = ksmbd_lookup_fd_fast(work, id);
7373 if (!fp)
7374 return -ENOENT;
7375
7376 start = le64_to_cpu(qar_req->file_offset);
7377 length = le64_to_cpu(qar_req->length);
7378
7379 ret = ksmbd_vfs_fqar_lseek(fp, start, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09007380 qar_rsp, in_count, out_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09007381 if (ret && ret != -E2BIG)
7382 *out_count = 0;
7383
7384 ksmbd_fd_put(work, fp);
7385 return ret;
7386}
7387
Namjae Jeon64b39f42021-03-30 14:25:35 +09007388static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id,
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007389 unsigned int out_buf_len,
7390 struct smb2_ioctl_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007391 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007392{
7393 struct ksmbd_rpc_command *rpc_resp;
7394 char *data_buf = (char *)&req->Buffer[0];
7395 int nbytes = 0;
7396
Namjae Jeon64b39f42021-03-30 14:25:35 +09007397 rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09007398 le32_to_cpu(req->InputCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09007399 if (rpc_resp) {
7400 if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) {
7401 /*
7402 * set STATUS_SOME_NOT_MAPPED response
7403 * for unknown domain sid.
7404 */
7405 rsp->hdr.Status = STATUS_SOME_NOT_MAPPED;
7406 } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
7407 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7408 goto out;
7409 } else if (rpc_resp->flags != KSMBD_RPC_OK) {
7410 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7411 goto out;
7412 }
7413
7414 nbytes = rpc_resp->payload_sz;
7415 if (rpc_resp->payload_sz > out_buf_len) {
7416 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7417 nbytes = out_buf_len;
7418 }
7419
7420 if (!rpc_resp->payload_sz) {
7421 rsp->hdr.Status =
7422 STATUS_UNEXPECTED_IO_ERROR;
7423 goto out;
7424 }
7425
7426 memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes);
7427 }
7428out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09007429 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007430 return nbytes;
7431}
7432
Namjae Jeon64b39f42021-03-30 14:25:35 +09007433static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007434 struct file_sparse *sparse)
Namjae Jeone2f34482021-03-16 10:49:09 +09007435{
7436 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007437 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09007438 int ret = 0;
7439 __le32 old_fattr;
7440
7441 fp = ksmbd_lookup_fd_fast(work, id);
7442 if (!fp)
7443 return -ENOENT;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007444 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007445
7446 old_fattr = fp->f_ci->m_fattr;
7447 if (sparse->SetSparse)
7448 fp->f_ci->m_fattr |= ATTR_SPARSE_FILE_LE;
7449 else
7450 fp->f_ci->m_fattr &= ~ATTR_SPARSE_FILE_LE;
7451
7452 if (fp->f_ci->m_fattr != old_fattr &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007453 test_share_config_flag(work->tcon->share_conf,
7454 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007455 struct xattr_dos_attrib da;
7456
Hyunchul Lee465d7202021-07-03 12:10:36 +09007457 ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007458 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007459 if (ret <= 0)
7460 goto out;
7461
7462 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
Hyunchul Lee465d7202021-07-03 12:10:36 +09007463 ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007464 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007465 if (ret)
7466 fp->f_ci->m_fattr = old_fattr;
7467 }
7468
7469out:
7470 ksmbd_fd_put(work, fp);
7471 return ret;
7472}
7473
7474static int fsctl_request_resume_key(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007475 struct smb2_ioctl_req *req,
7476 struct resume_key_ioctl_rsp *key_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007477{
7478 struct ksmbd_file *fp;
7479
7480 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007481 le64_to_cpu(req->VolatileFileId),
7482 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09007483 if (!fp)
7484 return -ENOENT;
7485
7486 memset(key_rsp, 0, sizeof(*key_rsp));
7487 key_rsp->ResumeKey[0] = req->VolatileFileId;
7488 key_rsp->ResumeKey[1] = req->PersistentFileId;
7489 ksmbd_fd_put(work, fp);
7490
7491 return 0;
7492}
7493
7494/**
7495 * smb2_ioctl() - handler for smb2 ioctl command
7496 * @work: smb work containing ioctl command buffer
7497 *
7498 * Return: 0 on success, otherwise error
7499 */
7500int smb2_ioctl(struct ksmbd_work *work)
7501{
7502 struct smb2_ioctl_req *req;
Namjae Jeoncb451722021-11-03 08:08:44 +09007503 struct smb2_ioctl_rsp *rsp;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007504 unsigned int cnt_code, nbytes = 0, out_buf_len, in_buf_len;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007505 u64 id = KSMBD_NO_FID;
Namjae Jeone2f34482021-03-16 10:49:09 +09007506 struct ksmbd_conn *conn = work->conn;
7507 int ret = 0;
7508
Namjae Jeone2f34482021-03-16 10:49:09 +09007509 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09007510 req = ksmbd_req_buf_next(work);
7511 rsp = ksmbd_resp_buf_next(work);
Namjae Jeon38673692021-07-08 12:32:27 +09007512 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
7513 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007514 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09007515 id = work->compound_fid;
7516 }
7517 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09007518 req = smb2_get_msg(work->request_buf);
7519 rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09007520 }
7521
Namjae Jeon38673692021-07-08 12:32:27 +09007522 if (!has_file_id(id))
Namjae Jeone2f34482021-03-16 10:49:09 +09007523 id = le64_to_cpu(req->VolatileFileId);
7524
7525 if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) {
7526 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7527 goto out;
7528 }
7529
7530 cnt_code = le32_to_cpu(req->CntCode);
Hyunchul Lee34061d62021-10-16 08:39:54 +09007531 ret = smb2_calc_max_out_buf_len(work, 48,
7532 le32_to_cpu(req->MaxOutputResponse));
7533 if (ret < 0) {
7534 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7535 goto out;
7536 }
7537 out_buf_len = (unsigned int)ret;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007538 in_buf_len = le32_to_cpu(req->InputCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09007539
7540 switch (cnt_code) {
7541 case FSCTL_DFS_GET_REFERRALS:
7542 case FSCTL_DFS_GET_REFERRALS_EX:
7543 /* Not support DFS yet */
7544 rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED;
7545 goto out;
7546 case FSCTL_CREATE_OR_GET_OBJECT_ID:
7547 {
7548 struct file_object_buf_type1_ioctl_rsp *obj_buf;
7549
7550 nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp);
7551 obj_buf = (struct file_object_buf_type1_ioctl_rsp *)
7552 &rsp->Buffer[0];
7553
7554 /*
7555 * TODO: This is dummy implementation to pass smbtorture
7556 * Need to check correct response later
7557 */
7558 memset(obj_buf->ObjectId, 0x0, 16);
7559 memset(obj_buf->BirthVolumeId, 0x0, 16);
7560 memset(obj_buf->BirthObjectId, 0x0, 16);
7561 memset(obj_buf->DomainId, 0x0, 16);
7562
7563 break;
7564 }
7565 case FSCTL_PIPE_TRANSCEIVE:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007566 out_buf_len = min_t(u32, KSMBD_IPC_MAX_PAYLOAD, out_buf_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09007567 nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp);
7568 break;
7569 case FSCTL_VALIDATE_NEGOTIATE_INFO:
7570 if (conn->dialect < SMB30_PROT_ID) {
7571 ret = -EOPNOTSUPP;
7572 goto out;
7573 }
7574
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007575 if (in_buf_len < sizeof(struct validate_negotiate_info_req))
7576 return -EINVAL;
7577
7578 if (out_buf_len < sizeof(struct validate_negotiate_info_rsp))
7579 return -EINVAL;
7580
Namjae Jeone2f34482021-03-16 10:49:09 +09007581 ret = fsctl_validate_negotiate_info(conn,
7582 (struct validate_negotiate_info_req *)&req->Buffer[0],
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007583 (struct validate_negotiate_info_rsp *)&rsp->Buffer[0],
7584 in_buf_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09007585 if (ret < 0)
7586 goto out;
7587
7588 nbytes = sizeof(struct validate_negotiate_info_rsp);
7589 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7590 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7591 break;
7592 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007593 ret = fsctl_query_iface_info_ioctl(conn, rsp, out_buf_len);
7594 if (ret < 0)
Namjae Jeone2f34482021-03-16 10:49:09 +09007595 goto out;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007596 nbytes = ret;
Namjae Jeone2f34482021-03-16 10:49:09 +09007597 break;
7598 case FSCTL_REQUEST_RESUME_KEY:
7599 if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) {
7600 ret = -EINVAL;
7601 goto out;
7602 }
7603
7604 ret = fsctl_request_resume_key(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007605 (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007606 if (ret < 0)
7607 goto out;
7608 rsp->PersistentFileId = req->PersistentFileId;
7609 rsp->VolatileFileId = req->VolatileFileId;
7610 nbytes = sizeof(struct resume_key_ioctl_rsp);
7611 break;
7612 case FSCTL_COPYCHUNK:
7613 case FSCTL_COPYCHUNK_WRITE:
Namjae Jeon64b39f42021-03-30 14:25:35 +09007614 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007615 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007616 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007617 ret = -EACCES;
7618 goto out;
7619 }
7620
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007621 if (in_buf_len < sizeof(struct copychunk_ioctl_req)) {
7622 ret = -EINVAL;
7623 goto out;
7624 }
7625
Namjae Jeone2f34482021-03-16 10:49:09 +09007626 if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) {
7627 ret = -EINVAL;
7628 goto out;
7629 }
7630
7631 nbytes = sizeof(struct copychunk_ioctl_rsp);
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007632 rsp->VolatileFileId = req->VolatileFileId;
7633 rsp->PersistentFileId = req->PersistentFileId;
7634 fsctl_copychunk(work,
7635 (struct copychunk_ioctl_req *)&req->Buffer[0],
7636 le32_to_cpu(req->CntCode),
7637 le32_to_cpu(req->InputCount),
7638 le64_to_cpu(req->VolatileFileId),
7639 le64_to_cpu(req->PersistentFileId),
7640 rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007641 break;
7642 case FSCTL_SET_SPARSE:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007643 if (in_buf_len < sizeof(struct file_sparse)) {
7644 ret = -EINVAL;
7645 goto out;
7646 }
7647
Namjae Jeone2f34482021-03-16 10:49:09 +09007648 ret = fsctl_set_sparse(work, id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007649 (struct file_sparse *)&req->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007650 if (ret < 0)
7651 goto out;
7652 break;
7653 case FSCTL_SET_ZERO_DATA:
7654 {
7655 struct file_zero_data_information *zero_data;
7656 struct ksmbd_file *fp;
7657 loff_t off, len;
7658
Namjae Jeon64b39f42021-03-30 14:25:35 +09007659 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007660 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007661 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007662 ret = -EACCES;
7663 goto out;
7664 }
7665
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007666 if (in_buf_len < sizeof(struct file_zero_data_information)) {
7667 ret = -EINVAL;
7668 goto out;
7669 }
7670
Namjae Jeone2f34482021-03-16 10:49:09 +09007671 zero_data =
7672 (struct file_zero_data_information *)&req->Buffer[0];
7673
7674 fp = ksmbd_lookup_fd_fast(work, id);
7675 if (!fp) {
7676 ret = -ENOENT;
7677 goto out;
7678 }
7679
7680 off = le64_to_cpu(zero_data->FileOffset);
7681 len = le64_to_cpu(zero_data->BeyondFinalZero) - off;
7682
7683 ret = ksmbd_vfs_zero_data(work, fp, off, len);
7684 ksmbd_fd_put(work, fp);
7685 if (ret < 0)
7686 goto out;
7687 break;
7688 }
7689 case FSCTL_QUERY_ALLOCATED_RANGES:
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007690 if (in_buf_len < sizeof(struct file_allocated_range_buffer)) {
7691 ret = -EINVAL;
7692 goto out;
7693 }
7694
Namjae Jeone2f34482021-03-16 10:49:09 +09007695 ret = fsctl_query_allocated_ranges(work, id,
7696 (struct file_allocated_range_buffer *)&req->Buffer[0],
7697 (struct file_allocated_range_buffer *)&rsp->Buffer[0],
7698 out_buf_len /
7699 sizeof(struct file_allocated_range_buffer), &nbytes);
7700 if (ret == -E2BIG) {
7701 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7702 } else if (ret < 0) {
7703 nbytes = 0;
7704 goto out;
7705 }
7706
7707 nbytes *= sizeof(struct file_allocated_range_buffer);
7708 break;
7709 case FSCTL_GET_REPARSE_POINT:
7710 {
7711 struct reparse_data_buffer *reparse_ptr;
7712 struct ksmbd_file *fp;
7713
7714 reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0];
7715 fp = ksmbd_lookup_fd_fast(work, id);
7716 if (!fp) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007717 pr_err("not found fp!!\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007718 ret = -ENOENT;
7719 goto out;
7720 }
7721
7722 reparse_ptr->ReparseTag =
Namjae Jeonab0b2632021-06-29 09:20:13 +09007723 smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode);
Namjae Jeone2f34482021-03-16 10:49:09 +09007724 reparse_ptr->ReparseDataLength = 0;
7725 ksmbd_fd_put(work, fp);
7726 nbytes = sizeof(struct reparse_data_buffer);
7727 break;
7728 }
Namjae Jeoneb817362021-05-18 10:37:59 +09007729 case FSCTL_DUPLICATE_EXTENTS_TO_FILE:
7730 {
7731 struct ksmbd_file *fp_in, *fp_out = NULL;
7732 struct duplicate_extents_to_file *dup_ext;
7733 loff_t src_off, dst_off, length, cloned;
7734
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007735 if (in_buf_len < sizeof(struct duplicate_extents_to_file)) {
7736 ret = -EINVAL;
7737 goto out;
7738 }
7739
Namjae Jeoneb817362021-05-18 10:37:59 +09007740 dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0];
7741
7742 fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle,
Namjae Jeon070fb212021-05-26 17:57:12 +09007743 dup_ext->PersistentFileHandle);
Namjae Jeoneb817362021-05-18 10:37:59 +09007744 if (!fp_in) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007745 pr_err("not found file handle in duplicate extent to file\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007746 ret = -ENOENT;
7747 goto out;
7748 }
7749
7750 fp_out = ksmbd_lookup_fd_fast(work, id);
7751 if (!fp_out) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007752 pr_err("not found fp\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007753 ret = -ENOENT;
7754 goto dup_ext_out;
7755 }
7756
7757 src_off = le64_to_cpu(dup_ext->SourceFileOffset);
7758 dst_off = le64_to_cpu(dup_ext->TargetFileOffset);
7759 length = le64_to_cpu(dup_ext->ByteCount);
7760 cloned = vfs_clone_file_range(fp_in->filp, src_off, fp_out->filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007761 dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007762 if (cloned == -EXDEV || cloned == -EOPNOTSUPP) {
7763 ret = -EOPNOTSUPP;
7764 goto dup_ext_out;
7765 } else if (cloned != length) {
Namjae Jeonf8524772021-06-18 10:28:00 +09007766 cloned = vfs_copy_file_range(fp_in->filp, src_off,
7767 fp_out->filp, dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007768 if (cloned != length) {
7769 if (cloned < 0)
7770 ret = cloned;
7771 else
7772 ret = -EINVAL;
7773 }
7774 }
7775
7776dup_ext_out:
7777 ksmbd_fd_put(work, fp_in);
7778 ksmbd_fd_put(work, fp_out);
7779 if (ret < 0)
7780 goto out;
7781 break;
7782 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007783 default:
7784 ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007785 cnt_code);
Namjae Jeone2f34482021-03-16 10:49:09 +09007786 ret = -EOPNOTSUPP;
7787 goto out;
7788 }
7789
7790 rsp->CntCode = cpu_to_le32(cnt_code);
7791 rsp->InputCount = cpu_to_le32(0);
7792 rsp->InputOffset = cpu_to_le32(112);
7793 rsp->OutputOffset = cpu_to_le32(112);
7794 rsp->OutputCount = cpu_to_le32(nbytes);
7795 rsp->StructureSize = cpu_to_le16(49);
7796 rsp->Reserved = cpu_to_le16(0);
7797 rsp->Flags = cpu_to_le32(0);
7798 rsp->Reserved2 = cpu_to_le32(0);
Namjae Jeoncb451722021-11-03 08:08:44 +09007799 inc_rfc1001_len(work->response_buf, 48 + nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09007800
7801 return 0;
7802
7803out:
7804 if (ret == -EACCES)
7805 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7806 else if (ret == -ENOENT)
7807 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7808 else if (ret == -EOPNOTSUPP)
7809 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeonf7db8fd2021-10-08 07:31:03 +09007810 else if (ret == -ENOSPC)
7811 rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL;
Namjae Jeone2f34482021-03-16 10:49:09 +09007812 else if (ret < 0 || rsp->hdr.Status == 0)
7813 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7814 smb2_set_err_rsp(work);
7815 return 0;
7816}
7817
7818/**
7819 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command
7820 * @work: smb work containing oplock break command buffer
7821 *
7822 * Return: 0
7823 */
7824static void smb20_oplock_break_ack(struct ksmbd_work *work)
7825{
Namjae Jeoncb451722021-11-03 08:08:44 +09007826 struct smb2_oplock_break *req = smb2_get_msg(work->request_buf);
7827 struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09007828 struct ksmbd_file *fp;
7829 struct oplock_info *opinfo = NULL;
7830 __le32 err = 0;
7831 int ret = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007832 u64 volatile_id, persistent_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09007833 char req_oplevel = 0, rsp_oplevel = 0;
7834 unsigned int oplock_change_type;
7835
7836 volatile_id = le64_to_cpu(req->VolatileFid);
7837 persistent_id = le64_to_cpu(req->PersistentFid);
7838 req_oplevel = req->OplockLevel;
7839 ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n",
7840 volatile_id, persistent_id, req_oplevel);
7841
7842 fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
7843 if (!fp) {
7844 rsp->hdr.Status = STATUS_FILE_CLOSED;
7845 smb2_set_err_rsp(work);
7846 return;
7847 }
7848
7849 opinfo = opinfo_get(fp);
7850 if (!opinfo) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007851 pr_err("unexpected null oplock_info\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007852 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7853 smb2_set_err_rsp(work);
7854 ksmbd_fd_put(work, fp);
7855 return;
7856 }
7857
7858 if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) {
7859 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7860 goto err_out;
7861 }
7862
7863 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7864 ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state);
7865 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7866 goto err_out;
7867 }
7868
Namjae Jeon64b39f42021-03-30 14:25:35 +09007869 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7870 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7871 (req_oplevel != SMB2_OPLOCK_LEVEL_II &&
7872 req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007873 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7874 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007875 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7876 req_oplevel != SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007877 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7878 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007879 } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II ||
7880 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007881 err = STATUS_INVALID_DEVICE_STATE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007882 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7883 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7884 req_oplevel == SMB2_OPLOCK_LEVEL_II) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007885 oplock_change_type = OPLOCK_WRITE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007886 } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7887 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7888 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007889 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007890 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7891 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007892 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007893 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007894 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007895 }
7896 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007897 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007898 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007899
7900 switch (oplock_change_type) {
7901 case OPLOCK_WRITE_TO_READ:
7902 ret = opinfo_write_to_read(opinfo);
7903 rsp_oplevel = SMB2_OPLOCK_LEVEL_II;
7904 break;
7905 case OPLOCK_WRITE_TO_NONE:
7906 ret = opinfo_write_to_none(opinfo);
7907 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7908 break;
7909 case OPLOCK_READ_TO_NONE:
7910 ret = opinfo_read_to_none(opinfo);
7911 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7912 break;
7913 default:
Namjae Jeonbde16942021-06-28 15:23:19 +09007914 pr_err("unknown oplock change 0x%x -> 0x%x\n",
7915 opinfo->level, rsp_oplevel);
Namjae Jeone2f34482021-03-16 10:49:09 +09007916 }
7917
7918 if (ret < 0) {
7919 rsp->hdr.Status = err;
7920 goto err_out;
7921 }
7922
7923 opinfo_put(opinfo);
7924 ksmbd_fd_put(work, fp);
7925 opinfo->op_state = OPLOCK_STATE_NONE;
7926 wake_up_interruptible_all(&opinfo->oplock_q);
7927
7928 rsp->StructureSize = cpu_to_le16(24);
7929 rsp->OplockLevel = rsp_oplevel;
7930 rsp->Reserved = 0;
7931 rsp->Reserved2 = 0;
7932 rsp->VolatileFid = cpu_to_le64(volatile_id);
7933 rsp->PersistentFid = cpu_to_le64(persistent_id);
Namjae Jeoncb451722021-11-03 08:08:44 +09007934 inc_rfc1001_len(work->response_buf, 24);
Namjae Jeone2f34482021-03-16 10:49:09 +09007935 return;
7936
7937err_out:
7938 opinfo->op_state = OPLOCK_STATE_NONE;
7939 wake_up_interruptible_all(&opinfo->oplock_q);
7940
7941 opinfo_put(opinfo);
7942 ksmbd_fd_put(work, fp);
7943 smb2_set_err_rsp(work);
7944}
7945
7946static int check_lease_state(struct lease *lease, __le32 req_state)
7947{
7948 if ((lease->new_state ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09007949 (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) &&
7950 !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007951 lease->new_state = req_state;
7952 return 0;
7953 }
7954
7955 if (lease->new_state == req_state)
7956 return 0;
7957
7958 return 1;
7959}
7960
7961/**
7962 * smb21_lease_break_ack() - handler for smb2.1 lease break command
7963 * @work: smb work containing lease break command buffer
7964 *
7965 * Return: 0
7966 */
7967static void smb21_lease_break_ack(struct ksmbd_work *work)
7968{
7969 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09007970 struct smb2_lease_ack *req = smb2_get_msg(work->request_buf);
7971 struct smb2_lease_ack *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09007972 struct oplock_info *opinfo;
7973 __le32 err = 0;
7974 int ret = 0;
7975 unsigned int lease_change_type;
7976 __le32 lease_state;
7977 struct lease *lease;
7978
7979 ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007980 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007981 opinfo = lookup_lease_in_table(conn, req->LeaseKey);
7982 if (!opinfo) {
7983 ksmbd_debug(OPLOCK, "file not opened\n");
7984 smb2_set_err_rsp(work);
7985 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7986 return;
7987 }
7988 lease = opinfo->o_lease;
7989
7990 if (opinfo->op_state == OPLOCK_STATE_NONE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007991 pr_err("unexpected lease break state 0x%x\n",
7992 opinfo->op_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09007993 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7994 goto err_out;
7995 }
7996
7997 if (check_lease_state(lease, req->LeaseState)) {
7998 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
7999 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +09008000 "req lease state: 0x%x, expected state: 0x%x\n",
8001 req->LeaseState, lease->new_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09008002 goto err_out;
8003 }
8004
8005 if (!atomic_read(&opinfo->breaking_cnt)) {
8006 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
8007 goto err_out;
8008 }
8009
8010 /* check for bad lease state */
Namjae Jeon070fb212021-05-26 17:57:12 +09008011 if (req->LeaseState &
8012 (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008013 err = STATUS_INVALID_OPLOCK_PROTOCOL;
8014 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8015 lease_change_type = OPLOCK_WRITE_TO_NONE;
8016 else
8017 lease_change_type = OPLOCK_READ_TO_NONE;
8018 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008019 le32_to_cpu(lease->state),
8020 le32_to_cpu(req->LeaseState));
Namjae Jeon64b39f42021-03-30 14:25:35 +09008021 } else if (lease->state == SMB2_LEASE_READ_CACHING_LE &&
8022 req->LeaseState != SMB2_LEASE_NONE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008023 err = STATUS_INVALID_OPLOCK_PROTOCOL;
8024 lease_change_type = OPLOCK_READ_TO_NONE;
8025 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008026 le32_to_cpu(lease->state),
8027 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09008028 } else {
8029 /* valid lease state changes */
8030 err = STATUS_INVALID_DEVICE_STATE;
8031 if (req->LeaseState == SMB2_LEASE_NONE_LE) {
8032 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8033 lease_change_type = OPLOCK_WRITE_TO_NONE;
8034 else
8035 lease_change_type = OPLOCK_READ_TO_NONE;
8036 } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) {
8037 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
8038 lease_change_type = OPLOCK_WRITE_TO_READ;
8039 else
8040 lease_change_type = OPLOCK_READ_HANDLE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09008041 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09008042 lease_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09008043 }
Namjae Jeone2f34482021-03-16 10:49:09 +09008044 }
8045
8046 switch (lease_change_type) {
8047 case OPLOCK_WRITE_TO_READ:
8048 ret = opinfo_write_to_read(opinfo);
8049 break;
8050 case OPLOCK_READ_HANDLE_TO_READ:
8051 ret = opinfo_read_handle_to_read(opinfo);
8052 break;
8053 case OPLOCK_WRITE_TO_NONE:
8054 ret = opinfo_write_to_none(opinfo);
8055 break;
8056 case OPLOCK_READ_TO_NONE:
8057 ret = opinfo_read_to_none(opinfo);
8058 break;
8059 default:
8060 ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008061 le32_to_cpu(lease->state),
8062 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09008063 }
8064
8065 lease_state = lease->state;
8066 opinfo->op_state = OPLOCK_STATE_NONE;
8067 wake_up_interruptible_all(&opinfo->oplock_q);
8068 atomic_dec(&opinfo->breaking_cnt);
8069 wake_up_interruptible_all(&opinfo->oplock_brk);
8070 opinfo_put(opinfo);
8071
8072 if (ret < 0) {
8073 rsp->hdr.Status = err;
8074 goto err_out;
8075 }
8076
8077 rsp->StructureSize = cpu_to_le16(36);
8078 rsp->Reserved = 0;
8079 rsp->Flags = 0;
8080 memcpy(rsp->LeaseKey, req->LeaseKey, 16);
8081 rsp->LeaseState = lease_state;
8082 rsp->LeaseDuration = 0;
Namjae Jeoncb451722021-11-03 08:08:44 +09008083 inc_rfc1001_len(work->response_buf, 36);
Namjae Jeone2f34482021-03-16 10:49:09 +09008084 return;
8085
8086err_out:
8087 opinfo->op_state = OPLOCK_STATE_NONE;
8088 wake_up_interruptible_all(&opinfo->oplock_q);
8089 atomic_dec(&opinfo->breaking_cnt);
8090 wake_up_interruptible_all(&opinfo->oplock_brk);
8091
8092 opinfo_put(opinfo);
8093 smb2_set_err_rsp(work);
8094}
8095
8096/**
8097 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break
8098 * @work: smb work containing oplock/lease break command buffer
8099 *
8100 * Return: 0
8101 */
8102int smb2_oplock_break(struct ksmbd_work *work)
8103{
Namjae Jeoncb451722021-11-03 08:08:44 +09008104 struct smb2_oplock_break *req = smb2_get_msg(work->request_buf);
8105 struct smb2_oplock_break *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008106
8107 switch (le16_to_cpu(req->StructureSize)) {
8108 case OP_BREAK_STRUCT_SIZE_20:
8109 smb20_oplock_break_ack(work);
8110 break;
8111 case OP_BREAK_STRUCT_SIZE_21:
8112 smb21_lease_break_ack(work);
8113 break;
8114 default:
8115 ksmbd_debug(OPLOCK, "invalid break cmd %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008116 le16_to_cpu(req->StructureSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09008117 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
8118 smb2_set_err_rsp(work);
8119 }
8120
8121 return 0;
8122}
8123
8124/**
8125 * smb2_notify() - handler for smb2 notify request
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09008126 * @work: smb work containing notify command buffer
Namjae Jeone2f34482021-03-16 10:49:09 +09008127 *
8128 * Return: 0
8129 */
8130int smb2_notify(struct ksmbd_work *work)
8131{
8132 struct smb2_notify_req *req;
8133 struct smb2_notify_rsp *rsp;
8134
8135 WORK_BUFFERS(work, req, rsp);
8136
8137 if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) {
8138 rsp->hdr.Status = STATUS_INTERNAL_ERROR;
8139 smb2_set_err_rsp(work);
8140 return 0;
8141 }
8142
8143 smb2_set_err_rsp(work);
8144 rsp->hdr.Status = STATUS_NOT_IMPLEMENTED;
8145 return 0;
8146}
8147
8148/**
8149 * smb2_is_sign_req() - handler for checking packet signing status
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09008150 * @work: smb work containing notify command buffer
8151 * @command: SMB2 command id
Namjae Jeone2f34482021-03-16 10:49:09 +09008152 *
8153 * Return: true if packed is signed, false otherwise
8154 */
8155bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command)
8156{
Namjae Jeoncb451722021-11-03 08:08:44 +09008157 struct smb2_hdr *rcv_hdr2 = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008158
8159 if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008160 command != SMB2_NEGOTIATE_HE &&
8161 command != SMB2_SESSION_SETUP_HE &&
8162 command != SMB2_OPLOCK_BREAK_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +09008163 return true;
8164
kernel test robot56160152021-05-12 09:24:37 +09008165 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +09008166}
8167
8168/**
8169 * smb2_check_sign_req() - handler for req packet sign processing
8170 * @work: smb work containing notify command buffer
8171 *
8172 * Return: 1 on success, 0 otherwise
8173 */
8174int smb2_check_sign_req(struct ksmbd_work *work)
8175{
Namjae Jeoncb451722021-11-03 08:08:44 +09008176 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008177 char signature_req[SMB2_SIGNATURE_SIZE];
8178 char signature[SMB2_HMACSHA256_SIZE];
8179 struct kvec iov[1];
8180 size_t len;
8181
Namjae Jeoncb451722021-11-03 08:08:44 +09008182 hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008183 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008184 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008185
8186 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
Namjae Jeoncb451722021-11-03 08:08:44 +09008187 len = get_rfc1002_len(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008188 else if (hdr->NextCommand)
8189 len = le32_to_cpu(hdr->NextCommand);
8190 else
Namjae Jeoncb451722021-11-03 08:08:44 +09008191 len = get_rfc1002_len(work->request_buf) -
Namjae Jeone2f34482021-03-16 10:49:09 +09008192 work->next_smb2_rcv_hdr_off;
8193
8194 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8195 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8196
8197 iov[0].iov_base = (char *)&hdr->ProtocolId;
8198 iov[0].iov_len = len;
8199
8200 if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1,
Namjae Jeon64b39f42021-03-30 14:25:35 +09008201 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09008202 return 0;
8203
8204 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008205 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008206 return 0;
8207 }
8208
8209 return 1;
8210}
8211
8212/**
8213 * smb2_set_sign_rsp() - handler for rsp packet sign processing
8214 * @work: smb work containing notify command buffer
8215 *
8216 */
8217void smb2_set_sign_rsp(struct ksmbd_work *work)
8218{
Namjae Jeoncb451722021-11-03 08:08:44 +09008219 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008220 struct smb2_hdr *req_hdr;
8221 char signature[SMB2_HMACSHA256_SIZE];
8222 struct kvec iov[2];
8223 size_t len;
8224 int n_vec = 1;
8225
Namjae Jeoncb451722021-11-03 08:08:44 +09008226 hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008227 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008228 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008229
Namjae Jeon8a893312021-06-25 13:43:37 +09008230 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008231
8232 if (!work->next_smb2_rsp_hdr_off) {
Namjae Jeoncb451722021-11-03 08:08:44 +09008233 len = get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008234 if (req_hdr->NextCommand)
8235 len = ALIGN(len, 8);
8236 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09008237 len = get_rfc1002_len(work->response_buf) -
8238 work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +09008239 len = ALIGN(len, 8);
8240 }
8241
8242 if (req_hdr->NextCommand)
8243 hdr->NextCommand = cpu_to_le32(len);
8244
8245 hdr->Flags |= SMB2_FLAGS_SIGNED;
8246 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8247
8248 iov[0].iov_base = (char *)&hdr->ProtocolId;
8249 iov[0].iov_len = len;
8250
Namjae Jeone5066492021-03-30 12:35:23 +09008251 if (work->aux_payload_sz) {
8252 iov[0].iov_len -= work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008253
Namjae Jeone5066492021-03-30 12:35:23 +09008254 iov[1].iov_base = work->aux_payload_buf;
8255 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008256 n_vec++;
8257 }
8258
8259 if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec,
Namjae Jeon64b39f42021-03-30 14:25:35 +09008260 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09008261 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8262}
8263
8264/**
8265 * smb3_check_sign_req() - handler for req packet sign processing
8266 * @work: smb work containing notify command buffer
8267 *
8268 * Return: 1 on success, 0 otherwise
8269 */
8270int smb3_check_sign_req(struct ksmbd_work *work)
8271{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008272 struct ksmbd_conn *conn = work->conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09008273 char *signing_key;
Namjae Jeoncb451722021-11-03 08:08:44 +09008274 struct smb2_hdr *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008275 struct channel *chann;
8276 char signature_req[SMB2_SIGNATURE_SIZE];
8277 char signature[SMB2_CMACAES_SIZE];
8278 struct kvec iov[1];
8279 size_t len;
8280
Namjae Jeoncb451722021-11-03 08:08:44 +09008281 hdr = smb2_get_msg(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008282 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008283 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008284
8285 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
Namjae Jeoncb451722021-11-03 08:08:44 +09008286 len = get_rfc1002_len(work->request_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008287 else if (hdr->NextCommand)
8288 len = le32_to_cpu(hdr->NextCommand);
8289 else
Namjae Jeoncb451722021-11-03 08:08:44 +09008290 len = get_rfc1002_len(work->request_buf) -
Namjae Jeone2f34482021-03-16 10:49:09 +09008291 work->next_smb2_rcv_hdr_off;
8292
8293 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
8294 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008295 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008296 chann = lookup_chann_list(work->sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008297 if (!chann)
8298 return 0;
8299 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008300 }
8301
8302 if (!signing_key) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008303 pr_err("SMB3 signing key is not generated\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008304 return 0;
8305 }
8306
8307 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8308 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8309 iov[0].iov_base = (char *)&hdr->ProtocolId;
8310 iov[0].iov_len = len;
8311
8312 if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature))
8313 return 0;
8314
8315 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008316 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008317 return 0;
8318 }
8319
8320 return 1;
8321}
8322
8323/**
8324 * smb3_set_sign_rsp() - handler for rsp packet sign processing
8325 * @work: smb work containing notify command buffer
8326 *
8327 */
8328void smb3_set_sign_rsp(struct ksmbd_work *work)
8329{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008330 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09008331 struct smb2_hdr *req_hdr, *hdr;
Namjae Jeone2f34482021-03-16 10:49:09 +09008332 struct channel *chann;
8333 char signature[SMB2_CMACAES_SIZE];
8334 struct kvec iov[2];
8335 int n_vec = 1;
8336 size_t len;
8337 char *signing_key;
8338
Namjae Jeoncb451722021-11-03 08:08:44 +09008339 hdr = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008340 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008341 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008342
Namjae Jeon8a893312021-06-25 13:43:37 +09008343 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008344
8345 if (!work->next_smb2_rsp_hdr_off) {
Namjae Jeoncb451722021-11-03 08:08:44 +09008346 len = get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008347 if (req_hdr->NextCommand)
8348 len = ALIGN(len, 8);
8349 } else {
Namjae Jeoncb451722021-11-03 08:08:44 +09008350 len = get_rfc1002_len(work->response_buf) -
8351 work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +09008352 len = ALIGN(len, 8);
8353 }
8354
Namjae Jeon08bdbc62021-07-27 09:30:29 +09008355 if (conn->binding == false &&
8356 le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008357 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008358 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008359 chann = lookup_chann_list(work->sess, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008360 if (!chann)
8361 return;
8362 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008363 }
8364
8365 if (!signing_key)
8366 return;
8367
8368 if (req_hdr->NextCommand)
8369 hdr->NextCommand = cpu_to_le32(len);
8370
8371 hdr->Flags |= SMB2_FLAGS_SIGNED;
8372 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8373 iov[0].iov_base = (char *)&hdr->ProtocolId;
8374 iov[0].iov_len = len;
Namjae Jeone5066492021-03-30 12:35:23 +09008375 if (work->aux_payload_sz) {
8376 iov[0].iov_len -= work->aux_payload_sz;
8377 iov[1].iov_base = work->aux_payload_buf;
8378 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008379 n_vec++;
8380 }
8381
8382 if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature))
8383 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8384}
8385
8386/**
8387 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response
8388 * @work: smb work containing response buffer
8389 *
8390 */
8391void smb3_preauth_hash_rsp(struct ksmbd_work *work)
8392{
8393 struct ksmbd_conn *conn = work->conn;
8394 struct ksmbd_session *sess = work->sess;
8395 struct smb2_hdr *req, *rsp;
8396
8397 if (conn->dialect != SMB311_PROT_ID)
8398 return;
8399
8400 WORK_BUFFERS(work, req, rsp);
8401
Namjae Jeon442ff9e2021-09-29 15:44:32 +09008402 if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE &&
8403 conn->preauth_info)
Namjae Jeoncb451722021-11-03 08:08:44 +09008404 ksmbd_gen_preauth_integrity_hash(conn, work->response_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008405 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09008406
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008407 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008408 __u8 *hash_value;
8409
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008410 if (conn->binding) {
8411 struct preauth_session *preauth_sess;
8412
8413 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
8414 if (!preauth_sess)
8415 return;
8416 hash_value = preauth_sess->Preauth_HashValue;
8417 } else {
8418 hash_value = sess->Preauth_HashValue;
8419 if (!hash_value)
8420 return;
8421 }
Namjae Jeoncb451722021-11-03 08:08:44 +09008422 ksmbd_gen_preauth_integrity_hash(conn, work->response_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008423 hash_value);
Namjae Jeone2f34482021-03-16 10:49:09 +09008424 }
8425}
8426
Namjae Jeon64b39f42021-03-30 14:25:35 +09008427static void fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, char *old_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008428 __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09008429{
8430 struct smb2_hdr *hdr = (struct smb2_hdr *)old_buf;
8431 unsigned int orig_len = get_rfc1002_len(old_buf);
8432
8433 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
8434 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
8435 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
8436 tr_hdr->Flags = cpu_to_le16(0x01);
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008437 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
8438 cipher_type == SMB2_ENCRYPTION_AES256_GCM)
8439 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008440 else
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008441 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008442 memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8);
8443 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
8444 inc_rfc1001_len(tr_hdr, orig_len);
8445}
8446
8447int smb3_encrypt_resp(struct ksmbd_work *work)
8448{
Namjae Jeone5066492021-03-30 12:35:23 +09008449 char *buf = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008450 struct smb2_transform_hdr *tr_hdr;
8451 struct kvec iov[3];
8452 int rc = -ENOMEM;
Namjae Jeone5066492021-03-30 12:35:23 +09008453 int buf_size = 0, rq_nvec = 2 + (work->aux_payload_sz ? 1 : 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09008454
8455 if (ARRAY_SIZE(iov) < rq_nvec)
8456 return -ENOMEM;
8457
Namjae Jeon20ea7fd2021-03-30 12:40:47 +09008458 tr_hdr = kzalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09008459 if (!tr_hdr)
8460 return rc;
8461
8462 /* fill transform header */
8463 fill_transform_hdr(tr_hdr, buf, work->conn->cipher_type);
8464
8465 iov[0].iov_base = tr_hdr;
8466 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8467 buf_size += iov[0].iov_len - 4;
8468
8469 iov[1].iov_base = buf + 4;
8470 iov[1].iov_len = get_rfc1002_len(buf);
Namjae Jeone5066492021-03-30 12:35:23 +09008471 if (work->aux_payload_sz) {
8472 iov[1].iov_len = work->resp_hdr_sz - 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008473
Namjae Jeone5066492021-03-30 12:35:23 +09008474 iov[2].iov_base = work->aux_payload_buf;
8475 iov[2].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008476 buf_size += iov[2].iov_len;
8477 }
8478 buf_size += iov[1].iov_len;
8479 work->resp_hdr_sz = iov[1].iov_len;
8480
8481 rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1);
8482 if (rc)
8483 return rc;
8484
8485 memmove(buf, iov[1].iov_base, iov[1].iov_len);
8486 tr_hdr->smb2_buf_length = cpu_to_be32(buf_size);
8487 work->tr_buf = tr_hdr;
8488
8489 return rc;
8490}
8491
Namjae Jeonf4228b62021-08-12 10:16:40 +09008492bool smb3_is_transform_hdr(void *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09008493{
8494 struct smb2_transform_hdr *trhdr = buf;
8495
8496 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
8497}
8498
8499int smb3_decrypt_req(struct ksmbd_work *work)
8500{
8501 struct ksmbd_conn *conn = work->conn;
8502 struct ksmbd_session *sess;
Namjae Jeone5066492021-03-30 12:35:23 +09008503 char *buf = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008504 unsigned int pdu_length = get_rfc1002_len(buf);
8505 struct kvec iov[2];
Namjae Jeonc7705ee2021-10-04 20:44:52 +09008506 int buf_data_size = pdu_length + 4 -
Namjae Jeone2f34482021-03-16 10:49:09 +09008507 sizeof(struct smb2_transform_hdr);
8508 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008509 int rc = 0;
8510
Namjae Jeonc7705ee2021-10-04 20:44:52 +09008511 if (buf_data_size < sizeof(struct smb2_hdr)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008512 pr_err("Transform message is too small (%u)\n",
8513 pdu_length);
Namjae Jeone2f34482021-03-16 10:49:09 +09008514 return -ECONNABORTED;
8515 }
8516
Namjae Jeonc7705ee2021-10-04 20:44:52 +09008517 if (buf_data_size < le32_to_cpu(tr_hdr->OriginalMessageSize)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008518 pr_err("Transform message is broken\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008519 return -ECONNABORTED;
8520 }
8521
Namjae Jeon4227f812021-09-29 19:52:51 +09008522 sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId));
8523 if (!sess) {
8524 pr_err("invalid session id(%llx) in transform header\n",
8525 le64_to_cpu(tr_hdr->SessionId));
8526 return -ECONNABORTED;
8527 }
8528
Namjae Jeone2f34482021-03-16 10:49:09 +09008529 iov[0].iov_base = buf;
8530 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8531 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
8532 iov[1].iov_len = buf_data_size;
8533 rc = ksmbd_crypt_message(conn, iov, 2, 0);
8534 if (rc)
8535 return rc;
8536
8537 memmove(buf + 4, iov[1].iov_base, buf_data_size);
Namjae Jeoncb451722021-11-03 08:08:44 +09008538 *(__be32 *)buf = cpu_to_be32(buf_data_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09008539
8540 return rc;
8541}
8542
8543bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work)
8544{
8545 struct ksmbd_conn *conn = work->conn;
Namjae Jeoncb451722021-11-03 08:08:44 +09008546 struct smb2_hdr *rsp = smb2_get_msg(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09008547
8548 if (conn->dialect < SMB30_PROT_ID)
8549 return false;
8550
8551 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008552 rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008553
8554 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008555 rsp->Status == STATUS_SUCCESS)
Namjae Jeone2f34482021-03-16 10:49:09 +09008556 return true;
8557 return false;
8558}