blob: 761e12171dc4418fb2594bea381fc2a93df57f69 [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 Jeone5066492021-03-30 12:35:23 +090047 *req = work->request_buf;
48 *rsp = 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 Jeone5066492021-03-30 12:35:23 +090096 struct smb2_hdr *req_hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +090097 int tree_id;
98
99 work->tcon = NULL;
Namjae Jeon64b39f42021-03-30 14:25:35 +0900100 if (work->conn->ops->get_cmd_val(work) == SMB2_TREE_CONNECT_HE ||
101 work->conn->ops->get_cmd_val(work) == SMB2_CANCEL_HE ||
102 work->conn->ops->get_cmd_val(work) == SMB2_LOGOFF_HE) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900103 ksmbd_debug(SMB, "skip to check tree connect request\n");
104 return 0;
105 }
106
Namjae Jeon02b68b22021-04-01 17:45:33 +0900107 if (xa_empty(&work->sess->tree_conns)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900108 ksmbd_debug(SMB, "NO tree connected\n");
Namjae Jeonc6ce2b52021-08-12 10:18:18 +0900109 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +0900110 }
111
112 tree_id = le32_to_cpu(req_hdr->Id.SyncId.TreeId);
113 work->tcon = ksmbd_tree_conn_lookup(work->sess, tree_id);
114 if (!work->tcon) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900115 pr_err("Invalid tid %d\n", tree_id);
Namjae Jeonc6ce2b52021-08-12 10:18:18 +0900116 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +0900117 }
118
119 return 1;
120}
121
122/**
123 * smb2_set_err_rsp() - set error response code on smb response
124 * @work: smb work containing response buffer
125 */
126void smb2_set_err_rsp(struct ksmbd_work *work)
127{
128 struct smb2_err_rsp *err_rsp;
129
130 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +0900131 err_rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900132 else
Namjae Jeone5066492021-03-30 12:35:23 +0900133 err_rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900134
135 if (err_rsp->hdr.Status != STATUS_STOPPED_ON_SYMLINK) {
136 err_rsp->StructureSize = SMB2_ERROR_STRUCTURE_SIZE2_LE;
137 err_rsp->ErrorContextCount = 0;
138 err_rsp->Reserved = 0;
139 err_rsp->ByteCount = 0;
140 err_rsp->ErrorData[0] = 0;
Namjae Jeone5066492021-03-30 12:35:23 +0900141 inc_rfc1001_len(work->response_buf, SMB2_ERROR_STRUCTURE_SIZE2);
Namjae Jeone2f34482021-03-16 10:49:09 +0900142 }
143}
144
145/**
146 * is_smb2_neg_cmd() - is it smb2 negotiation command
147 * @work: smb work containing smb header
148 *
Namjae Jeonf4228b62021-08-12 10:16:40 +0900149 * Return: true if smb2 negotiation command, otherwise false
Namjae Jeone2f34482021-03-16 10:49:09 +0900150 */
Namjae Jeonf4228b62021-08-12 10:16:40 +0900151bool is_smb2_neg_cmd(struct ksmbd_work *work)
Namjae Jeone2f34482021-03-16 10:49:09 +0900152{
Namjae Jeone5066492021-03-30 12:35:23 +0900153 struct smb2_hdr *hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900154
155 /* is it SMB2 header ? */
156 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
Namjae Jeonf4228b62021-08-12 10:16:40 +0900157 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900158
159 /* make sure it is request not response message */
160 if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
Namjae Jeonf4228b62021-08-12 10:16:40 +0900161 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900162
163 if (hdr->Command != SMB2_NEGOTIATE)
Namjae Jeonf4228b62021-08-12 10:16:40 +0900164 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900165
Namjae Jeonf4228b62021-08-12 10:16:40 +0900166 return true;
Namjae Jeone2f34482021-03-16 10:49:09 +0900167}
168
169/**
170 * is_smb2_rsp() - is it smb2 response
171 * @work: smb work containing smb response buffer
172 *
Namjae Jeonf4228b62021-08-12 10:16:40 +0900173 * Return: true if smb2 response, otherwise false
Namjae Jeone2f34482021-03-16 10:49:09 +0900174 */
Namjae Jeonf4228b62021-08-12 10:16:40 +0900175bool is_smb2_rsp(struct ksmbd_work *work)
Namjae Jeone2f34482021-03-16 10:49:09 +0900176{
Namjae Jeone5066492021-03-30 12:35:23 +0900177 struct smb2_hdr *hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900178
179 /* is it SMB2 header ? */
180 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
Namjae Jeonf4228b62021-08-12 10:16:40 +0900181 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900182
183 /* make sure it is response not request message */
184 if (!(hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR))
Namjae Jeonf4228b62021-08-12 10:16:40 +0900185 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +0900186
Namjae Jeonf4228b62021-08-12 10:16:40 +0900187 return true;
Namjae Jeone2f34482021-03-16 10:49:09 +0900188}
189
190/**
191 * get_smb2_cmd_val() - get smb command code from smb header
192 * @work: smb work containing smb request buffer
193 *
194 * Return: smb2 request command value
195 */
Namjae Jeonfc2d1b52021-05-26 18:01:08 +0900196u16 get_smb2_cmd_val(struct ksmbd_work *work)
Namjae Jeone2f34482021-03-16 10:49:09 +0900197{
198 struct smb2_hdr *rcv_hdr;
199
200 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +0900201 rcv_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900202 else
Namjae Jeone5066492021-03-30 12:35:23 +0900203 rcv_hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900204 return le16_to_cpu(rcv_hdr->Command);
205}
206
207/**
208 * set_smb2_rsp_status() - set error response code on smb2 header
209 * @work: smb work containing response buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900210 * @err: error response code
Namjae Jeone2f34482021-03-16 10:49:09 +0900211 */
212void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err)
213{
214 struct smb2_hdr *rsp_hdr;
215
216 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +0900217 rsp_hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900218 else
Namjae Jeone5066492021-03-30 12:35:23 +0900219 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900220 rsp_hdr->Status = err;
221 smb2_set_err_rsp(work);
222}
223
224/**
225 * init_smb2_neg_rsp() - initialize smb2 response for negotiate command
226 * @work: smb work containing smb request buffer
227 *
228 * smb2 negotiate response is sent in reply of smb1 negotiate command for
229 * dialect auto-negotiation.
230 */
231int init_smb2_neg_rsp(struct ksmbd_work *work)
232{
233 struct smb2_hdr *rsp_hdr;
234 struct smb2_negotiate_rsp *rsp;
235 struct ksmbd_conn *conn = work->conn;
236
237 if (conn->need_neg == false)
238 return -EINVAL;
239 if (!(conn->dialect >= SMB20_PROT_ID &&
Namjae Jeon64b39f42021-03-30 14:25:35 +0900240 conn->dialect <= SMB311_PROT_ID))
Namjae Jeone2f34482021-03-16 10:49:09 +0900241 return -EINVAL;
242
Namjae Jeone5066492021-03-30 12:35:23 +0900243 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900244
245 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
246
247 rsp_hdr->smb2_buf_length =
Hyunchul Leed8fb2992021-06-25 11:53:26 +0900248 cpu_to_be32(smb2_hdr_size_no_buflen(conn->vals));
Namjae Jeone2f34482021-03-16 10:49:09 +0900249
250 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
251 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
252 rsp_hdr->CreditRequest = cpu_to_le16(2);
253 rsp_hdr->Command = SMB2_NEGOTIATE;
254 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
255 rsp_hdr->NextCommand = 0;
256 rsp_hdr->MessageId = 0;
257 rsp_hdr->Id.SyncId.ProcessId = 0;
258 rsp_hdr->Id.SyncId.TreeId = 0;
259 rsp_hdr->SessionId = 0;
260 memset(rsp_hdr->Signature, 0, 16);
261
Namjae Jeone5066492021-03-30 12:35:23 +0900262 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900263
264 WARN_ON(ksmbd_conn_good(work));
265
266 rsp->StructureSize = cpu_to_le16(65);
267 ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect);
268 rsp->DialectRevision = cpu_to_le16(conn->dialect);
269 /* Not setting conn guid rsp->ServerGUID, as it
270 * not used by client for identifying connection
271 */
272 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
273 /* Default Max Message Size till SMB2.0, 64K*/
274 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
275 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
276 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
277
278 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
279 rsp->ServerStartTime = 0;
280
281 rsp->SecurityBufferOffset = cpu_to_le16(128);
282 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
283 ksmbd_copy_gss_neg_header(((char *)(&rsp->hdr) +
284 sizeof(rsp->hdr.smb2_buf_length)) +
285 le16_to_cpu(rsp->SecurityBufferOffset));
286 inc_rfc1001_len(rsp, sizeof(struct smb2_negotiate_rsp) -
287 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
288 AUTH_GSS_LENGTH);
289 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
290 if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY)
291 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
292 conn->use_spnego = true;
293
294 ksmbd_conn_set_need_negotiate(work);
295 return 0;
296}
297
298static int smb2_consume_credit_charge(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +0900299 unsigned short credit_charge)
Namjae Jeone2f34482021-03-16 10:49:09 +0900300{
301 struct ksmbd_conn *conn = work->conn;
302 unsigned int rsp_credits = 1;
303
304 if (!conn->total_credits)
305 return 0;
306
307 if (credit_charge > 0)
308 rsp_credits = credit_charge;
309
310 conn->total_credits -= rsp_credits;
311 return rsp_credits;
312}
313
314/**
315 * smb2_set_rsp_credits() - set number of credits in response buffer
316 * @work: smb work containing smb response buffer
317 */
318int smb2_set_rsp_credits(struct ksmbd_work *work)
319{
Namjae Jeon8a893312021-06-25 13:43:37 +0900320 struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work);
321 struct smb2_hdr *hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900322 struct ksmbd_conn *conn = work->conn;
323 unsigned short credits_requested = le16_to_cpu(req_hdr->CreditRequest);
324 unsigned short credit_charge = 1, credits_granted = 0;
325 unsigned short aux_max, aux_credits, min_credits;
326 int rsp_credit_charge;
327
328 if (hdr->Command == SMB2_CANCEL)
329 goto out;
330
331 /* get default minimum credits by shifting maximum credits by 4 */
332 min_credits = conn->max_credits >> 4;
333
334 if (conn->total_credits >= conn->max_credits) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900335 pr_err("Total credits overflow: %d\n", conn->total_credits);
Namjae Jeone2f34482021-03-16 10:49:09 +0900336 conn->total_credits = min_credits;
337 }
338
Namjae Jeon070fb212021-05-26 17:57:12 +0900339 rsp_credit_charge =
340 smb2_consume_credit_charge(work, le16_to_cpu(req_hdr->CreditCharge));
Namjae Jeone2f34482021-03-16 10:49:09 +0900341 if (rsp_credit_charge < 0)
342 return -EINVAL;
343
344 hdr->CreditCharge = cpu_to_le16(rsp_credit_charge);
345
346 if (credits_requested > 0) {
347 aux_credits = credits_requested - 1;
348 aux_max = 32;
349 if (hdr->Command == SMB2_NEGOTIATE)
350 aux_max = 0;
351 aux_credits = (aux_credits < aux_max) ? aux_credits : aux_max;
352 credits_granted = aux_credits + credit_charge;
353
354 /* if credits granted per client is getting bigger than default
355 * minimum credits then we should wrap it up within the limits.
356 */
357 if ((conn->total_credits + credits_granted) > min_credits)
358 credits_granted = min_credits - conn->total_credits;
359 /*
360 * TODO: Need to adjuct CreditRequest value according to
361 * current cpu load
362 */
363 } else if (conn->total_credits == 0) {
364 credits_granted = 1;
365 }
366
367 conn->total_credits += credits_granted;
368 work->credits_granted += credits_granted;
369
370 if (!req_hdr->NextCommand) {
371 /* Update CreditRequest in last request */
372 hdr->CreditRequest = cpu_to_le16(work->credits_granted);
373 }
374out:
375 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900376 "credits: requested[%d] granted[%d] total_granted[%d]\n",
377 credits_requested, credits_granted,
378 conn->total_credits);
Namjae Jeone2f34482021-03-16 10:49:09 +0900379 return 0;
380}
381
382/**
383 * init_chained_smb2_rsp() - initialize smb2 chained response
384 * @work: smb work containing smb response buffer
385 */
386static void init_chained_smb2_rsp(struct ksmbd_work *work)
387{
Namjae Jeon8a893312021-06-25 13:43:37 +0900388 struct smb2_hdr *req = ksmbd_req_buf_next(work);
389 struct smb2_hdr *rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900390 struct smb2_hdr *rsp_hdr;
391 struct smb2_hdr *rcv_hdr;
392 int next_hdr_offset = 0;
393 int len, new_len;
394
395 /* Len of this response = updated RFC len - offset of previous cmd
396 * in the compound rsp
397 */
398
399 /* Storing the current local FID which may be needed by subsequent
400 * command in the compound request
401 */
402 if (req->Command == SMB2_CREATE && rsp->Status == STATUS_SUCCESS) {
403 work->compound_fid =
404 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
405 VolatileFileId);
406 work->compound_pfid =
407 le64_to_cpu(((struct smb2_create_rsp *)rsp)->
408 PersistentFileId);
409 work->compound_sid = le64_to_cpu(rsp->SessionId);
410 }
411
Namjae Jeone5066492021-03-30 12:35:23 +0900412 len = get_rfc1002_len(work->response_buf) - work->next_smb2_rsp_hdr_off;
Namjae Jeone2f34482021-03-16 10:49:09 +0900413 next_hdr_offset = le32_to_cpu(req->NextCommand);
414
415 new_len = ALIGN(len, 8);
Namjae Jeone5066492021-03-30 12:35:23 +0900416 inc_rfc1001_len(work->response_buf, ((sizeof(struct smb2_hdr) - 4)
Namjae Jeone2f34482021-03-16 10:49:09 +0900417 + new_len - len));
418 rsp->NextCommand = cpu_to_le32(new_len);
419
420 work->next_smb2_rcv_hdr_off += next_hdr_offset;
421 work->next_smb2_rsp_hdr_off += new_len;
422 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900423 "Compound req new_len = %d rcv off = %d rsp off = %d\n",
424 new_len, work->next_smb2_rcv_hdr_off,
425 work->next_smb2_rsp_hdr_off);
Namjae Jeone2f34482021-03-16 10:49:09 +0900426
Namjae Jeon8a893312021-06-25 13:43:37 +0900427 rsp_hdr = ksmbd_resp_buf_next(work);
428 rcv_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900429
430 if (!(rcv_hdr->Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
431 ksmbd_debug(SMB, "related flag should be set\n");
432 work->compound_fid = KSMBD_NO_FID;
433 work->compound_pfid = KSMBD_NO_FID;
434 }
435 memset((char *)rsp_hdr + 4, 0, sizeof(struct smb2_hdr) + 2);
Namjae Jeon18a015b2021-09-22 21:00:57 +0900436 rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER;
Namjae Jeone2f34482021-03-16 10:49:09 +0900437 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
438 rsp_hdr->Command = rcv_hdr->Command;
439
440 /*
441 * Message is response. We don't grant oplock yet.
442 */
443 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR |
444 SMB2_FLAGS_RELATED_OPERATIONS);
445 rsp_hdr->NextCommand = 0;
446 rsp_hdr->MessageId = rcv_hdr->MessageId;
447 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
448 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
449 rsp_hdr->SessionId = rcv_hdr->SessionId;
450 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
451}
452
453/**
454 * is_chained_smb2_message() - check for chained command
455 * @work: smb work containing smb request buffer
456 *
457 * Return: true if chained request, otherwise false
458 */
459bool is_chained_smb2_message(struct ksmbd_work *work)
460{
Namjae Jeone5066492021-03-30 12:35:23 +0900461 struct smb2_hdr *hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900462 unsigned int len;
463
464 if (hdr->ProtocolId != SMB2_PROTO_NUMBER)
465 return false;
466
Namjae Jeon8a893312021-06-25 13:43:37 +0900467 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +0900468 if (le32_to_cpu(hdr->NextCommand) > 0) {
469 ksmbd_debug(SMB, "got SMB2 chained command\n");
470 init_chained_smb2_rsp(work);
471 return true;
472 } else if (work->next_smb2_rcv_hdr_off) {
473 /*
474 * This is last request in chained command,
475 * align response to 8 byte
476 */
Namjae Jeone5066492021-03-30 12:35:23 +0900477 len = ALIGN(get_rfc1002_len(work->response_buf), 8);
478 len = len - get_rfc1002_len(work->response_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +0900479 if (len) {
480 ksmbd_debug(SMB, "padding len %u\n", len);
Namjae Jeone5066492021-03-30 12:35:23 +0900481 inc_rfc1001_len(work->response_buf, len);
482 if (work->aux_payload_sz)
Namjae Jeone2f34482021-03-16 10:49:09 +0900483 work->aux_payload_sz += len;
484 }
485 }
486 return false;
487}
488
489/**
490 * init_smb2_rsp_hdr() - initialize smb2 response
491 * @work: smb work containing smb request buffer
492 *
493 * Return: 0
494 */
495int init_smb2_rsp_hdr(struct ksmbd_work *work)
496{
Namjae Jeone5066492021-03-30 12:35:23 +0900497 struct smb2_hdr *rsp_hdr = work->response_buf;
498 struct smb2_hdr *rcv_hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900499 struct ksmbd_conn *conn = work->conn;
500
501 memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2);
Hyunchul Leed8fb2992021-06-25 11:53:26 +0900502 rsp_hdr->smb2_buf_length =
503 cpu_to_be32(smb2_hdr_size_no_buflen(conn->vals));
Namjae Jeone2f34482021-03-16 10:49:09 +0900504 rsp_hdr->ProtocolId = rcv_hdr->ProtocolId;
505 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
506 rsp_hdr->Command = rcv_hdr->Command;
507
508 /*
509 * Message is response. We don't grant oplock yet.
510 */
511 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
512 rsp_hdr->NextCommand = 0;
513 rsp_hdr->MessageId = rcv_hdr->MessageId;
514 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
515 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
516 rsp_hdr->SessionId = rcv_hdr->SessionId;
517 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
518
519 work->syncronous = true;
520 if (work->async_id) {
Namjae Jeond40012a2021-04-13 13:06:30 +0900521 ksmbd_release_id(&conn->async_ida, work->async_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900522 work->async_id = 0;
523 }
524
525 return 0;
526}
527
528/**
529 * smb2_allocate_rsp_buf() - allocate smb2 response buffer
530 * @work: smb work containing smb request buffer
531 *
532 * Return: 0 on success, otherwise -ENOMEM
533 */
534int smb2_allocate_rsp_buf(struct ksmbd_work *work)
535{
Namjae Jeone5066492021-03-30 12:35:23 +0900536 struct smb2_hdr *hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900537 size_t small_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
538 size_t large_sz = work->conn->vals->max_trans_size + MAX_SMB2_HDR_SIZE;
539 size_t sz = small_sz;
540 int cmd = le16_to_cpu(hdr->Command);
541
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900542 if (cmd == SMB2_IOCTL_HE || cmd == SMB2_QUERY_DIRECTORY_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900543 sz = large_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +0900544
545 if (cmd == SMB2_QUERY_INFO_HE) {
546 struct smb2_query_info_req *req;
547
Namjae Jeone5066492021-03-30 12:35:23 +0900548 req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900549 if (req->InfoType == SMB2_O_INFO_FILE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +0900550 (req->FileInfoClass == FILE_FULL_EA_INFORMATION ||
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900551 req->FileInfoClass == FILE_ALL_INFORMATION))
Namjae Jeone2f34482021-03-16 10:49:09 +0900552 sz = large_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +0900553 }
554
555 /* allocate large response buf for chained commands */
556 if (le32_to_cpu(hdr->NextCommand) > 0)
557 sz = large_sz;
558
Namjae Jeonc30f4eb2021-06-18 10:17:37 +0900559 work->response_buf = kvmalloc(sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeon63c454f2021-04-20 14:24:28 +0900560 if (!work->response_buf)
Namjae Jeone2f34482021-03-16 10:49:09 +0900561 return -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +0900562
563 work->response_sz = sz;
564 return 0;
565}
566
567/**
568 * smb2_check_user_session() - check for valid session for a user
569 * @work: smb work containing smb request buffer
570 *
571 * Return: 0 on success, otherwise error
572 */
573int smb2_check_user_session(struct ksmbd_work *work)
574{
Namjae Jeone5066492021-03-30 12:35:23 +0900575 struct smb2_hdr *req_hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900576 struct ksmbd_conn *conn = work->conn;
577 unsigned int cmd = conn->ops->get_cmd_val(work);
578 unsigned long long sess_id;
579
580 work->sess = NULL;
581 /*
582 * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not
583 * require a session id, so no need to validate user session's for
584 * these commands.
585 */
586 if (cmd == SMB2_ECHO_HE || cmd == SMB2_NEGOTIATE_HE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +0900587 cmd == SMB2_SESSION_SETUP_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900588 return 0;
589
590 if (!ksmbd_conn_good(work))
591 return -EINVAL;
592
593 sess_id = le64_to_cpu(req_hdr->SessionId);
594 /* Check for validity of user session */
Namjae Jeonf5a544e2021-06-18 10:04:19 +0900595 work->sess = ksmbd_session_lookup_all(conn, sess_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900596 if (work->sess)
597 return 1;
598 ksmbd_debug(SMB, "Invalid user session, Uid %llu\n", sess_id);
599 return -EINVAL;
600}
601
Namjae Jeon64b39f42021-03-30 14:25:35 +0900602static void destroy_previous_session(struct ksmbd_user *user, u64 id)
Namjae Jeone2f34482021-03-16 10:49:09 +0900603{
604 struct ksmbd_session *prev_sess = ksmbd_session_lookup_slowpath(id);
605 struct ksmbd_user *prev_user;
606
607 if (!prev_sess)
608 return;
609
610 prev_user = prev_sess->user;
611
Marios Makassikis1fca8032021-05-06 11:41:54 +0900612 if (!prev_user ||
613 strcmp(user->name, prev_user->name) ||
Namjae Jeone2f34482021-03-16 10:49:09 +0900614 user->passkey_sz != prev_user->passkey_sz ||
615 memcmp(user->passkey, prev_user->passkey, user->passkey_sz)) {
616 put_session(prev_sess);
617 return;
618 }
619
620 put_session(prev_sess);
621 ksmbd_session_destroy(prev_sess);
622}
623
624/**
625 * smb2_get_name() - get filename string from on the wire smb format
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900626 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +0900627 * @src: source buffer
628 * @maxlen: maxlen of source string
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900629 * @nls_table: nls_table pointer
Namjae Jeone2f34482021-03-16 10:49:09 +0900630 *
631 * Return: matching converted filename on success, otherwise error ptr
632 */
633static char *
Namjae Jeon64b39f42021-03-30 14:25:35 +0900634smb2_get_name(struct ksmbd_share_config *share, const char *src,
Namjae Jeon070fb212021-05-26 17:57:12 +0900635 const int maxlen, struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +0900636{
Hyunchul Lee265fd192021-09-25 00:06:16 +0900637 char *name;
Namjae Jeone2f34482021-03-16 10:49:09 +0900638
Namjae Jeon64b39f42021-03-30 14:25:35 +0900639 name = smb_strndup_from_utf16(src, maxlen, 1, local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +0900640 if (IS_ERR(name)) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900641 pr_err("failed to get name %ld\n", PTR_ERR(name));
Namjae Jeone2f34482021-03-16 10:49:09 +0900642 return name;
643 }
644
Hyunchul Lee265fd192021-09-25 00:06:16 +0900645 ksmbd_conv_path_to_unix(name);
646 ksmbd_strip_last_slash(name);
647 return name;
Namjae Jeone2f34482021-03-16 10:49:09 +0900648}
649
Namjae Jeone2f34482021-03-16 10:49:09 +0900650int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg)
651{
652 struct smb2_hdr *rsp_hdr;
653 struct ksmbd_conn *conn = work->conn;
654 int id;
655
Namjae Jeone5066492021-03-30 12:35:23 +0900656 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900657 rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND;
658
Namjae Jeond40012a2021-04-13 13:06:30 +0900659 id = ksmbd_acquire_async_msg_id(&conn->async_ida);
Namjae Jeone2f34482021-03-16 10:49:09 +0900660 if (id < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +0900661 pr_err("Failed to alloc async message id\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900662 return id;
663 }
664 work->syncronous = false;
665 work->async_id = id;
666 rsp_hdr->Id.AsyncId = cpu_to_le64(id);
667
668 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900669 "Send interim Response to inform async request id : %d\n",
670 work->async_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900671
672 work->cancel_fn = fn;
673 work->cancel_argv = arg;
674
Namjae Jeon6c4e6752021-06-07 09:08:45 +0900675 if (list_empty(&work->async_request_entry)) {
676 spin_lock(&conn->request_lock);
677 list_add_tail(&work->async_request_entry, &conn->async_requests);
678 spin_unlock(&conn->request_lock);
679 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900680
681 return 0;
682}
683
684void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
685{
686 struct smb2_hdr *rsp_hdr;
687
Namjae Jeone5066492021-03-30 12:35:23 +0900688 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900689 smb2_set_err_rsp(work);
690 rsp_hdr->Status = status;
691
692 work->multiRsp = 1;
693 ksmbd_conn_write(work);
694 rsp_hdr->Status = 0;
695 work->multiRsp = 0;
696}
697
698static __le32 smb2_get_reparse_tag_special_file(umode_t mode)
699{
700 if (S_ISDIR(mode) || S_ISREG(mode))
701 return 0;
702
703 if (S_ISLNK(mode))
704 return IO_REPARSE_TAG_LX_SYMLINK_LE;
705 else if (S_ISFIFO(mode))
706 return IO_REPARSE_TAG_LX_FIFO_LE;
707 else if (S_ISSOCK(mode))
708 return IO_REPARSE_TAG_AF_UNIX_LE;
709 else if (S_ISCHR(mode))
710 return IO_REPARSE_TAG_LX_CHR_LE;
711 else if (S_ISBLK(mode))
712 return IO_REPARSE_TAG_LX_BLK_LE;
713
714 return 0;
715}
716
717/**
718 * smb2_get_dos_mode() - get file mode in dos format from unix mode
719 * @stat: kstat containing file mode
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900720 * @attribute: attribute flags
Namjae Jeone2f34482021-03-16 10:49:09 +0900721 *
722 * Return: converted dos mode
723 */
724static int smb2_get_dos_mode(struct kstat *stat, int attribute)
725{
726 int attr = 0;
727
Namjae Jeon64b39f42021-03-30 14:25:35 +0900728 if (S_ISDIR(stat->mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900729 attr = ATTR_DIRECTORY |
730 (attribute & (ATTR_HIDDEN | ATTR_SYSTEM));
Namjae Jeon64b39f42021-03-30 14:25:35 +0900731 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +0900732 attr = (attribute & 0x00005137) | ATTR_ARCHIVE;
733 attr &= ~(ATTR_DIRECTORY);
734 if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps &
735 FILE_SUPPORTS_SPARSE_FILES))
736 attr |= ATTR_SPARSE;
737
738 if (smb2_get_reparse_tag_special_file(stat->mode))
739 attr |= ATTR_REPARSE;
740 }
741
742 return attr;
743}
744
745static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900746 __le16 hash_id)
Namjae Jeone2f34482021-03-16 10:49:09 +0900747{
748 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
749 pneg_ctxt->DataLength = cpu_to_le16(38);
750 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
751 pneg_ctxt->Reserved = cpu_to_le32(0);
752 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
753 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
754 pneg_ctxt->HashAlgorithms = hash_id;
755}
756
757static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900758 __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +0900759{
760 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
761 pneg_ctxt->DataLength = cpu_to_le16(4);
762 pneg_ctxt->Reserved = cpu_to_le32(0);
763 pneg_ctxt->CipherCount = cpu_to_le16(1);
764 pneg_ctxt->Ciphers[0] = cipher_type;
765}
766
767static void build_compression_ctxt(struct smb2_compression_ctx *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900768 __le16 comp_algo)
Namjae Jeone2f34482021-03-16 10:49:09 +0900769{
770 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
771 pneg_ctxt->DataLength =
772 cpu_to_le16(sizeof(struct smb2_compression_ctx)
773 - sizeof(struct smb2_neg_context));
774 pneg_ctxt->Reserved = cpu_to_le32(0);
775 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(1);
776 pneg_ctxt->Reserved1 = cpu_to_le32(0);
777 pneg_ctxt->CompressionAlgorithms[0] = comp_algo;
778}
779
Namjae Jeon378087c2021-07-21 10:05:53 +0900780static void build_sign_cap_ctxt(struct smb2_signing_capabilities *pneg_ctxt,
781 __le16 sign_algo)
782{
783 pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;
784 pneg_ctxt->DataLength =
785 cpu_to_le16((sizeof(struct smb2_signing_capabilities) + 2)
786 - sizeof(struct smb2_neg_context));
787 pneg_ctxt->Reserved = cpu_to_le32(0);
788 pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(1);
789 pneg_ctxt->SigningAlgorithms[0] = sign_algo;
790}
791
Namjae Jeon64b39f42021-03-30 14:25:35 +0900792static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900793{
794 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
795 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
796 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
797 pneg_ctxt->Name[0] = 0x93;
798 pneg_ctxt->Name[1] = 0xAD;
799 pneg_ctxt->Name[2] = 0x25;
800 pneg_ctxt->Name[3] = 0x50;
801 pneg_ctxt->Name[4] = 0x9C;
802 pneg_ctxt->Name[5] = 0xB4;
803 pneg_ctxt->Name[6] = 0x11;
804 pneg_ctxt->Name[7] = 0xE7;
805 pneg_ctxt->Name[8] = 0xB4;
806 pneg_ctxt->Name[9] = 0x23;
807 pneg_ctxt->Name[10] = 0x83;
808 pneg_ctxt->Name[11] = 0xDE;
809 pneg_ctxt->Name[12] = 0x96;
810 pneg_ctxt->Name[13] = 0x8B;
811 pneg_ctxt->Name[14] = 0xCD;
812 pneg_ctxt->Name[15] = 0x7C;
813}
814
Namjae Jeon64b39f42021-03-30 14:25:35 +0900815static void assemble_neg_contexts(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900816 struct smb2_negotiate_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +0900817{
818 /* +4 is to account for the RFC1001 len field */
819 char *pneg_ctxt = (char *)rsp +
820 le32_to_cpu(rsp->NegotiateContextOffset) + 4;
821 int neg_ctxt_cnt = 1;
822 int ctxt_size;
823
824 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900825 "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900826 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900827 conn->preauth_info->Preauth_HashId);
Namjae Jeone2f34482021-03-16 10:49:09 +0900828 rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt);
829 inc_rfc1001_len(rsp, AUTH_GSS_PADDING);
830 ctxt_size = sizeof(struct smb2_preauth_neg_context);
831 /* Round to 8 byte boundary */
832 pneg_ctxt += round_up(sizeof(struct smb2_preauth_neg_context), 8);
833
834 if (conn->cipher_type) {
835 ctxt_size = round_up(ctxt_size, 8);
836 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900837 "assemble SMB2_ENCRYPTION_CAPABILITIES context\n");
Namjae Jeon64b39f42021-03-30 14:25:35 +0900838 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900839 conn->cipher_type);
Namjae Jeone2f34482021-03-16 10:49:09 +0900840 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900841 ctxt_size += sizeof(struct smb2_encryption_neg_context) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900842 /* Round to 8 byte boundary */
843 pneg_ctxt +=
Namjae Jeonaf320a72021-07-21 10:03:19 +0900844 round_up(sizeof(struct smb2_encryption_neg_context) + 2,
Namjae Jeone2f34482021-03-16 10:49:09 +0900845 8);
846 }
847
848 if (conn->compress_algorithm) {
849 ctxt_size = round_up(ctxt_size, 8);
850 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900851 "assemble SMB2_COMPRESSION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900852 /* Temporarily set to SMB3_COMPRESS_NONE */
853 build_compression_ctxt((struct smb2_compression_ctx *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900854 conn->compress_algorithm);
Namjae Jeone2f34482021-03-16 10:49:09 +0900855 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900856 ctxt_size += sizeof(struct smb2_compression_ctx) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900857 /* Round to 8 byte boundary */
Namjae Jeonaf320a72021-07-21 10:03:19 +0900858 pneg_ctxt += round_up(sizeof(struct smb2_compression_ctx) + 2,
859 8);
Namjae Jeone2f34482021-03-16 10:49:09 +0900860 }
861
862 if (conn->posix_ext_supported) {
863 ctxt_size = round_up(ctxt_size, 8);
864 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900865 "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900866 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
867 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
868 ctxt_size += sizeof(struct smb2_posix_neg_context);
Namjae Jeon378087c2021-07-21 10:05:53 +0900869 /* Round to 8 byte boundary */
870 pneg_ctxt += round_up(sizeof(struct smb2_posix_neg_context), 8);
871 }
872
873 if (conn->signing_negotiated) {
874 ctxt_size = round_up(ctxt_size, 8);
875 ksmbd_debug(SMB,
876 "assemble SMB2_SIGNING_CAPABILITIES context\n");
877 build_sign_cap_ctxt((struct smb2_signing_capabilities *)pneg_ctxt,
878 conn->signing_algorithm);
879 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
880 ctxt_size += sizeof(struct smb2_signing_capabilities) + 2;
Namjae Jeone2f34482021-03-16 10:49:09 +0900881 }
882
883 inc_rfc1001_len(rsp, ctxt_size);
884}
885
Namjae Jeon64b39f42021-03-30 14:25:35 +0900886static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900887 struct smb2_preauth_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900888{
889 __le32 err = STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP;
890
Namjae Jeon070fb212021-05-26 17:57:12 +0900891 if (pneg_ctxt->HashAlgorithms == SMB2_PREAUTH_INTEGRITY_SHA512) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900892 conn->preauth_info->Preauth_HashId =
893 SMB2_PREAUTH_INTEGRITY_SHA512;
894 err = STATUS_SUCCESS;
895 }
896
897 return err;
898}
899
Namjae Jeonaf320a72021-07-21 10:03:19 +0900900static void decode_encrypt_ctxt(struct ksmbd_conn *conn,
901 struct smb2_encryption_neg_context *pneg_ctxt,
902 int len_of_ctxts)
Namjae Jeone2f34482021-03-16 10:49:09 +0900903{
Namjae Jeone2f34482021-03-16 10:49:09 +0900904 int cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900905 int i, cphs_size = cph_cnt * sizeof(__le16);
Namjae Jeone2f34482021-03-16 10:49:09 +0900906
907 conn->cipher_type = 0;
908
Namjae Jeonaf320a72021-07-21 10:03:19 +0900909 if (sizeof(struct smb2_encryption_neg_context) + cphs_size >
910 len_of_ctxts) {
911 pr_err("Invalid cipher count(%d)\n", cph_cnt);
912 return;
913 }
914
Namjae Jeone2f34482021-03-16 10:49:09 +0900915 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION))
Namjae Jeonaf320a72021-07-21 10:03:19 +0900916 return;
Namjae Jeone2f34482021-03-16 10:49:09 +0900917
918 for (i = 0; i < cph_cnt; i++) {
919 if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM ||
Namjae Jeon5a0ca772021-05-06 11:43:37 +0900920 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM ||
921 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM ||
922 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900923 ksmbd_debug(SMB, "Cipher ID = 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +0900924 pneg_ctxt->Ciphers[i]);
Namjae Jeone2f34482021-03-16 10:49:09 +0900925 conn->cipher_type = pneg_ctxt->Ciphers[i];
926 break;
927 }
928 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900929}
930
Namjae Jeonaf320a72021-07-21 10:03:19 +0900931static void decode_compress_ctxt(struct ksmbd_conn *conn,
932 struct smb2_compression_ctx *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900933{
Namjae Jeone2f34482021-03-16 10:49:09 +0900934 conn->compress_algorithm = SMB3_COMPRESS_NONE;
Namjae Jeone2f34482021-03-16 10:49:09 +0900935}
936
Namjae Jeon378087c2021-07-21 10:05:53 +0900937static void decode_sign_cap_ctxt(struct ksmbd_conn *conn,
938 struct smb2_signing_capabilities *pneg_ctxt,
939 int len_of_ctxts)
940{
941 int sign_algo_cnt = le16_to_cpu(pneg_ctxt->SigningAlgorithmCount);
942 int i, sign_alos_size = sign_algo_cnt * sizeof(__le16);
943
944 conn->signing_negotiated = false;
945
946 if (sizeof(struct smb2_signing_capabilities) + sign_alos_size >
947 len_of_ctxts) {
948 pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt);
949 return;
950 }
951
952 for (i = 0; i < sign_algo_cnt; i++) {
953 if (pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_HMAC_SHA256 ||
954 pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_AES_CMAC) {
955 ksmbd_debug(SMB, "Signing Algorithm ID = 0x%x\n",
956 pneg_ctxt->SigningAlgorithms[i]);
957 conn->signing_negotiated = true;
958 conn->signing_algorithm =
959 pneg_ctxt->SigningAlgorithms[i];
960 break;
961 }
962 }
963}
964
Namjae Jeone2f34482021-03-16 10:49:09 +0900965static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900966 struct smb2_negotiate_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +0900967{
Namjae Jeone2f34482021-03-16 10:49:09 +0900968 /* +4 is to account for the RFC1001 len field */
Namjae Jeonaf320a72021-07-21 10:03:19 +0900969 struct smb2_neg_context *pctx = (struct smb2_neg_context *)((char *)req + 4);
970 int i = 0, len_of_ctxts;
971 int offset = le32_to_cpu(req->NegotiateContextOffset);
Namjae Jeone2f34482021-03-16 10:49:09 +0900972 int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount);
Namjae Jeonaf320a72021-07-21 10:03:19 +0900973 int len_of_smb = be32_to_cpu(req->hdr.smb2_buf_length);
974 __le32 status = STATUS_INVALID_PARAMETER;
Namjae Jeone2f34482021-03-16 10:49:09 +0900975
Namjae Jeonaf320a72021-07-21 10:03:19 +0900976 ksmbd_debug(SMB, "decoding %d negotiate contexts\n", neg_ctxt_cnt);
977 if (len_of_smb <= offset) {
978 ksmbd_debug(SMB, "Invalid response: negotiate context offset\n");
979 return status;
980 }
981
982 len_of_ctxts = len_of_smb - offset;
983
Namjae Jeone2f34482021-03-16 10:49:09 +0900984 while (i++ < neg_ctxt_cnt) {
Namjae Jeonaf320a72021-07-21 10:03:19 +0900985 int clen;
986
987 /* check that offset is not beyond end of SMB */
988 if (len_of_ctxts == 0)
989 break;
990
991 if (len_of_ctxts < sizeof(struct smb2_neg_context))
992 break;
993
994 pctx = (struct smb2_neg_context *)((char *)pctx + offset);
995 clen = le16_to_cpu(pctx->DataLength);
996 if (clen + sizeof(struct smb2_neg_context) > len_of_ctxts)
997 break;
998
999 if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001000 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001001 "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001002 if (conn->preauth_info->Preauth_HashId)
1003 break;
1004
1005 status = decode_preauth_ctxt(conn,
Namjae Jeonaf320a72021-07-21 10:03:19 +09001006 (struct smb2_preauth_neg_context *)pctx);
1007 if (status != STATUS_SUCCESS)
1008 break;
1009 } else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001010 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001011 "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001012 if (conn->cipher_type)
1013 break;
1014
Namjae Jeonaf320a72021-07-21 10:03:19 +09001015 decode_encrypt_ctxt(conn,
1016 (struct smb2_encryption_neg_context *)pctx,
1017 len_of_ctxts);
1018 } else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001019 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001020 "deassemble SMB2_COMPRESSION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001021 if (conn->compress_algorithm)
1022 break;
1023
Namjae Jeonaf320a72021-07-21 10:03:19 +09001024 decode_compress_ctxt(conn,
1025 (struct smb2_compression_ctx *)pctx);
1026 } else if (pctx->ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001027 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001028 "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n");
Namjae Jeonaf320a72021-07-21 10:03:19 +09001029 } else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001030 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001031 "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001032 conn->posix_ext_supported = true;
Namjae Jeon378087c2021-07-21 10:05:53 +09001033 } else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES) {
1034 ksmbd_debug(SMB,
1035 "deassemble SMB2_SIGNING_CAPABILITIES context\n");
1036 decode_sign_cap_ctxt(conn,
1037 (struct smb2_signing_capabilities *)pctx,
1038 len_of_ctxts);
Namjae Jeone2f34482021-03-16 10:49:09 +09001039 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001040
Namjae Jeonaf320a72021-07-21 10:03:19 +09001041 /* offsets must be 8 byte aligned */
1042 clen = (clen + 7) & ~0x7;
1043 offset = clen + sizeof(struct smb2_neg_context);
1044 len_of_ctxts -= clen + sizeof(struct smb2_neg_context);
Namjae Jeone2f34482021-03-16 10:49:09 +09001045 }
1046 return status;
1047}
1048
1049/**
1050 * smb2_handle_negotiate() - handler for smb2 negotiate command
1051 * @work: smb work containing smb request buffer
1052 *
1053 * Return: 0
1054 */
1055int smb2_handle_negotiate(struct ksmbd_work *work)
1056{
1057 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09001058 struct smb2_negotiate_req *req = work->request_buf;
1059 struct smb2_negotiate_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001060 int rc = 0;
1061 __le32 status;
1062
1063 ksmbd_debug(SMB, "Received negotiate request\n");
1064 conn->need_neg = false;
1065 if (ksmbd_conn_good(work)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001066 pr_err("conn->tcp_status is already in CifsGood State\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001067 work->send_no_response = 1;
1068 return rc;
1069 }
1070
1071 if (req->DialectCount == 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001072 pr_err("malformed packet\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001073 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1074 rc = -EINVAL;
1075 goto err_out;
1076 }
1077
1078 conn->cli_cap = le32_to_cpu(req->Capabilities);
1079 switch (conn->dialect) {
1080 case SMB311_PROT_ID:
1081 conn->preauth_info =
1082 kzalloc(sizeof(struct preauth_integrity_info),
Namjae Jeon070fb212021-05-26 17:57:12 +09001083 GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09001084 if (!conn->preauth_info) {
1085 rc = -ENOMEM;
1086 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1087 goto err_out;
1088 }
1089
1090 status = deassemble_neg_contexts(conn, req);
1091 if (status != STATUS_SUCCESS) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001092 pr_err("deassemble_neg_contexts error(0x%x)\n",
1093 status);
Namjae Jeone2f34482021-03-16 10:49:09 +09001094 rsp->hdr.Status = status;
1095 rc = -EINVAL;
1096 goto err_out;
1097 }
1098
1099 rc = init_smb3_11_server(conn);
1100 if (rc < 0) {
1101 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1102 goto err_out;
1103 }
1104
1105 ksmbd_gen_preauth_integrity_hash(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001106 work->request_buf,
1107 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001108 rsp->NegotiateContextOffset =
1109 cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
1110 assemble_neg_contexts(conn, rsp);
1111 break;
1112 case SMB302_PROT_ID:
1113 init_smb3_02_server(conn);
1114 break;
1115 case SMB30_PROT_ID:
1116 init_smb3_0_server(conn);
1117 break;
1118 case SMB21_PROT_ID:
1119 init_smb2_1_server(conn);
1120 break;
1121 case SMB20_PROT_ID:
1122 rc = init_smb2_0_server(conn);
1123 if (rc) {
1124 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
1125 goto err_out;
1126 }
1127 break;
1128 case SMB2X_PROT_ID:
1129 case BAD_PROT_ID:
1130 default:
1131 ksmbd_debug(SMB, "Server dialect :0x%x not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001132 conn->dialect);
Namjae Jeone2f34482021-03-16 10:49:09 +09001133 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
1134 rc = -EINVAL;
1135 goto err_out;
1136 }
1137 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
1138
1139 /* For stats */
1140 conn->connection_type = conn->dialect;
1141
1142 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
1143 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
1144 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
1145
1146 if (conn->dialect > SMB20_PROT_ID) {
1147 memcpy(conn->ClientGUID, req->ClientGUID,
Namjae Jeon070fb212021-05-26 17:57:12 +09001148 SMB2_CLIENT_GUID_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09001149 conn->cli_sec_mode = le16_to_cpu(req->SecurityMode);
1150 }
1151
1152 rsp->StructureSize = cpu_to_le16(65);
1153 rsp->DialectRevision = cpu_to_le16(conn->dialect);
1154 /* Not setting conn guid rsp->ServerGUID, as it
1155 * not used by client for identifying server
1156 */
1157 memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE);
1158
1159 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
1160 rsp->ServerStartTime = 0;
1161 ksmbd_debug(SMB, "negotiate context offset %d, count %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001162 le32_to_cpu(rsp->NegotiateContextOffset),
1163 le16_to_cpu(rsp->NegotiateContextCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09001164
1165 rsp->SecurityBufferOffset = cpu_to_le16(128);
1166 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
1167 ksmbd_copy_gss_neg_header(((char *)(&rsp->hdr) +
Namjae Jeon070fb212021-05-26 17:57:12 +09001168 sizeof(rsp->hdr.smb2_buf_length)) +
1169 le16_to_cpu(rsp->SecurityBufferOffset));
Namjae Jeone2f34482021-03-16 10:49:09 +09001170 inc_rfc1001_len(rsp, sizeof(struct smb2_negotiate_rsp) -
Namjae Jeon070fb212021-05-26 17:57:12 +09001171 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
1172 AUTH_GSS_LENGTH);
Namjae Jeone2f34482021-03-16 10:49:09 +09001173 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
1174 conn->use_spnego = true;
1175
1176 if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001177 server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) &&
1178 req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09001179 conn->sign = true;
1180 else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) {
1181 server_conf.enforced_signing = true;
1182 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
1183 conn->sign = true;
1184 }
1185
1186 conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode);
1187 ksmbd_conn_set_need_negotiate(work);
1188
1189err_out:
1190 if (rc < 0)
1191 smb2_set_err_rsp(work);
1192
1193 return rc;
1194}
1195
1196static int alloc_preauth_hash(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09001197 struct ksmbd_conn *conn)
Namjae Jeone2f34482021-03-16 10:49:09 +09001198{
1199 if (sess->Preauth_HashValue)
1200 return 0;
1201
kernel test robot86f52972021-04-02 12:17:24 +09001202 sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue,
Namjae Jeon070fb212021-05-26 17:57:12 +09001203 PREAUTH_HASHVALUE_SIZE, GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09001204 if (!sess->Preauth_HashValue)
1205 return -ENOMEM;
1206
Namjae Jeone2f34482021-03-16 10:49:09 +09001207 return 0;
1208}
1209
1210static int generate_preauth_hash(struct ksmbd_work *work)
1211{
1212 struct ksmbd_conn *conn = work->conn;
1213 struct ksmbd_session *sess = work->sess;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001214 u8 *preauth_hash;
Namjae Jeone2f34482021-03-16 10:49:09 +09001215
1216 if (conn->dialect != SMB311_PROT_ID)
1217 return 0;
1218
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001219 if (conn->binding) {
1220 struct preauth_session *preauth_sess;
1221
1222 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
1223 if (!preauth_sess) {
1224 preauth_sess = ksmbd_preauth_session_alloc(conn, sess->id);
1225 if (!preauth_sess)
1226 return -ENOMEM;
1227 }
1228
1229 preauth_hash = preauth_sess->Preauth_HashValue;
1230 } else {
1231 if (!sess->Preauth_HashValue)
1232 if (alloc_preauth_hash(sess, conn))
1233 return -ENOMEM;
1234 preauth_hash = sess->Preauth_HashValue;
Namjae Jeone2f34482021-03-16 10:49:09 +09001235 }
1236
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001237 ksmbd_gen_preauth_integrity_hash(conn, work->request_buf, preauth_hash);
Namjae Jeone2f34482021-03-16 10:49:09 +09001238 return 0;
1239}
1240
1241static int decode_negotiation_token(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09001242 struct negotiate_message *negblob)
Namjae Jeone2f34482021-03-16 10:49:09 +09001243{
1244 struct ksmbd_conn *conn = work->conn;
1245 struct smb2_sess_setup_req *req;
1246 int sz;
1247
1248 if (!conn->use_spnego)
1249 return -EINVAL;
1250
Namjae Jeone5066492021-03-30 12:35:23 +09001251 req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001252 sz = le16_to_cpu(req->SecurityBufferLength);
1253
Hyunchul Leefad41612021-04-19 17:26:15 +09001254 if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) {
1255 if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001256 conn->auth_mechs |= KSMBD_AUTH_NTLMSSP;
1257 conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP;
1258 conn->use_spnego = false;
1259 }
1260 }
1261 return 0;
1262}
1263
1264static int ntlm_negotiate(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09001265 struct negotiate_message *negblob)
Namjae Jeone2f34482021-03-16 10:49:09 +09001266{
Namjae Jeone5066492021-03-30 12:35:23 +09001267 struct smb2_sess_setup_req *req = work->request_buf;
1268 struct smb2_sess_setup_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001269 struct challenge_message *chgblob;
1270 unsigned char *spnego_blob = NULL;
1271 u16 spnego_blob_len;
1272 char *neg_blob;
1273 int sz, rc;
1274
1275 ksmbd_debug(SMB, "negotiate phase\n");
1276 sz = le16_to_cpu(req->SecurityBufferLength);
1277 rc = ksmbd_decode_ntlmssp_neg_blob(negblob, sz, work->sess);
1278 if (rc)
1279 return rc;
1280
1281 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1282 chgblob =
1283 (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz);
1284 memset(chgblob, 0, sizeof(struct challenge_message));
1285
1286 if (!work->conn->use_spnego) {
1287 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess);
1288 if (sz < 0)
1289 return -ENOMEM;
1290
1291 rsp->SecurityBufferLength = cpu_to_le16(sz);
1292 return 0;
1293 }
1294
1295 sz = sizeof(struct challenge_message);
1296 sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6;
1297
1298 neg_blob = kzalloc(sz, GFP_KERNEL);
1299 if (!neg_blob)
1300 return -ENOMEM;
1301
1302 chgblob = (struct challenge_message *)neg_blob;
1303 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess);
1304 if (sz < 0) {
1305 rc = -ENOMEM;
1306 goto out;
1307 }
1308
Namjae Jeon070fb212021-05-26 17:57:12 +09001309 rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len,
1310 neg_blob, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09001311 if (rc) {
1312 rc = -ENOMEM;
1313 goto out;
1314 }
1315
1316 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1317 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
1318 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1319
1320out:
1321 kfree(spnego_blob);
1322 kfree(neg_blob);
1323 return rc;
1324}
1325
1326static struct authenticate_message *user_authblob(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001327 struct smb2_sess_setup_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +09001328{
1329 int sz;
1330
1331 if (conn->use_spnego && conn->mechToken)
1332 return (struct authenticate_message *)conn->mechToken;
1333
1334 sz = le16_to_cpu(req->SecurityBufferOffset);
1335 return (struct authenticate_message *)((char *)&req->hdr.ProtocolId
1336 + sz);
1337}
1338
1339static struct ksmbd_user *session_user(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001340 struct smb2_sess_setup_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +09001341{
1342 struct authenticate_message *authblob;
1343 struct ksmbd_user *user;
1344 char *name;
1345 int sz;
1346
1347 authblob = user_authblob(conn, req);
1348 sz = le32_to_cpu(authblob->UserName.BufferOffset);
1349 name = smb_strndup_from_utf16((const char *)authblob + sz,
1350 le16_to_cpu(authblob->UserName.Length),
1351 true,
1352 conn->local_nls);
1353 if (IS_ERR(name)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001354 pr_err("cannot allocate memory\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001355 return NULL;
1356 }
1357
1358 ksmbd_debug(SMB, "session setup request for user %s\n", name);
1359 user = ksmbd_login_user(name);
1360 kfree(name);
1361 return user;
1362}
1363
1364static int ntlm_authenticate(struct ksmbd_work *work)
1365{
Namjae Jeone5066492021-03-30 12:35:23 +09001366 struct smb2_sess_setup_req *req = work->request_buf;
1367 struct smb2_sess_setup_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001368 struct ksmbd_conn *conn = work->conn;
1369 struct ksmbd_session *sess = work->sess;
1370 struct channel *chann = NULL;
1371 struct ksmbd_user *user;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001372 u64 prev_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09001373 int sz, rc;
1374
1375 ksmbd_debug(SMB, "authenticate phase\n");
1376 if (conn->use_spnego) {
1377 unsigned char *spnego_blob;
1378 u16 spnego_blob_len;
1379
1380 rc = build_spnego_ntlmssp_auth_blob(&spnego_blob,
1381 &spnego_blob_len,
1382 0);
1383 if (rc)
1384 return -ENOMEM;
1385
1386 sz = le16_to_cpu(rsp->SecurityBufferOffset);
Namjae Jeon64b39f42021-03-30 14:25:35 +09001387 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001388 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1389 kfree(spnego_blob);
1390 inc_rfc1001_len(rsp, spnego_blob_len - 1);
1391 }
1392
1393 user = session_user(conn, req);
1394 if (!user) {
1395 ksmbd_debug(SMB, "Unknown user name or an error\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001396 return -EPERM;
Namjae Jeone2f34482021-03-16 10:49:09 +09001397 }
1398
1399 /* Check for previous session */
1400 prev_id = le64_to_cpu(req->PreviousSessionId);
1401 if (prev_id && prev_id != sess->id)
1402 destroy_previous_session(user, prev_id);
1403
1404 if (sess->state == SMB2_SESSION_VALID) {
1405 /*
1406 * Reuse session if anonymous try to connect
1407 * on reauthetication.
1408 */
1409 if (ksmbd_anonymous_user(user)) {
1410 ksmbd_free_user(user);
1411 return 0;
1412 }
1413 ksmbd_free_user(sess->user);
1414 }
1415
1416 sess->user = user;
1417 if (user_guest(sess->user)) {
1418 if (conn->sign) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001419 ksmbd_debug(SMB, "Guest login not allowed when signing enabled\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001420 return -EPERM;
Namjae Jeone2f34482021-03-16 10:49:09 +09001421 }
1422
1423 rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE;
1424 } else {
1425 struct authenticate_message *authblob;
1426
1427 authblob = user_authblob(conn, req);
1428 sz = le16_to_cpu(req->SecurityBufferLength);
1429 rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, sess);
1430 if (rc) {
1431 set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD);
1432 ksmbd_debug(SMB, "authentication failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001433 return -EPERM;
Namjae Jeone2f34482021-03-16 10:49:09 +09001434 }
1435
1436 /*
1437 * If session state is SMB2_SESSION_VALID, We can assume
1438 * that it is reauthentication. And the user/password
1439 * has been verified, so return it here.
1440 */
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001441 if (sess->state == SMB2_SESSION_VALID) {
1442 if (conn->binding)
1443 goto binding_session;
Namjae Jeone2f34482021-03-16 10:49:09 +09001444 return 0;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001445 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001446
1447 if ((conn->sign || server_conf.enforced_signing) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001448 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
Namjae Jeone2f34482021-03-16 10:49:09 +09001449 sess->sign = true;
1450
1451 if (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION &&
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001452 conn->ops->generate_encryptionkey &&
1453 !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001454 rc = conn->ops->generate_encryptionkey(sess);
1455 if (rc) {
1456 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001457 "SMB3 encryption key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001458 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001459 }
1460 sess->enc = true;
1461 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1462 /*
1463 * signing is disable if encryption is enable
1464 * on this session
1465 */
1466 sess->sign = false;
1467 }
1468 }
1469
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001470binding_session:
Namjae Jeone2f34482021-03-16 10:49:09 +09001471 if (conn->dialect >= SMB30_PROT_ID) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001472 chann = lookup_chann_list(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001473 if (!chann) {
1474 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1475 if (!chann)
1476 return -ENOMEM;
1477
1478 chann->conn = conn;
1479 INIT_LIST_HEAD(&chann->chann_list);
1480 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1481 }
1482 }
1483
1484 if (conn->ops->generate_signingkey) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001485 rc = conn->ops->generate_signingkey(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001486 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001487 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001488 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001489 }
1490 }
1491
1492 if (conn->dialect > SMB20_PROT_ID) {
1493 if (!ksmbd_conn_lookup_dialect(conn)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001494 pr_err("fail to verify the dialect\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001495 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001496 }
1497 }
1498 return 0;
1499}
1500
1501#ifdef CONFIG_SMB_SERVER_KERBEROS5
1502static int krb5_authenticate(struct ksmbd_work *work)
1503{
Namjae Jeone5066492021-03-30 12:35:23 +09001504 struct smb2_sess_setup_req *req = work->request_buf;
1505 struct smb2_sess_setup_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001506 struct ksmbd_conn *conn = work->conn;
1507 struct ksmbd_session *sess = work->sess;
1508 char *in_blob, *out_blob;
1509 struct channel *chann = NULL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001510 u64 prev_sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09001511 int in_len, out_len;
1512 int retval;
1513
1514 in_blob = (char *)&req->hdr.ProtocolId +
1515 le16_to_cpu(req->SecurityBufferOffset);
1516 in_len = le16_to_cpu(req->SecurityBufferLength);
1517 out_blob = (char *)&rsp->hdr.ProtocolId +
1518 le16_to_cpu(rsp->SecurityBufferOffset);
1519 out_len = work->response_sz -
1520 offsetof(struct smb2_hdr, smb2_buf_length) -
1521 le16_to_cpu(rsp->SecurityBufferOffset);
1522
1523 /* Check previous session */
1524 prev_sess_id = le64_to_cpu(req->PreviousSessionId);
1525 if (prev_sess_id && prev_sess_id != sess->id)
1526 destroy_previous_session(sess->user, prev_sess_id);
1527
1528 if (sess->state == SMB2_SESSION_VALID)
1529 ksmbd_free_user(sess->user);
1530
1531 retval = ksmbd_krb5_authenticate(sess, in_blob, in_len,
Namjae Jeon070fb212021-05-26 17:57:12 +09001532 out_blob, &out_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001533 if (retval) {
1534 ksmbd_debug(SMB, "krb5 authentication failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001535 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001536 }
1537 rsp->SecurityBufferLength = cpu_to_le16(out_len);
1538 inc_rfc1001_len(rsp, out_len - 1);
1539
1540 if ((conn->sign || server_conf.enforced_signing) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001541 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
Namjae Jeone2f34482021-03-16 10:49:09 +09001542 sess->sign = true;
1543
1544 if ((conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09001545 conn->ops->generate_encryptionkey) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001546 retval = conn->ops->generate_encryptionkey(sess);
1547 if (retval) {
1548 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001549 "SMB3 encryption key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001550 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001551 }
1552 sess->enc = true;
1553 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1554 sess->sign = false;
1555 }
1556
1557 if (conn->dialect >= SMB30_PROT_ID) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001558 chann = lookup_chann_list(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001559 if (!chann) {
1560 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1561 if (!chann)
1562 return -ENOMEM;
1563
1564 chann->conn = conn;
1565 INIT_LIST_HEAD(&chann->chann_list);
1566 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1567 }
1568 }
1569
1570 if (conn->ops->generate_signingkey) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001571 retval = conn->ops->generate_signingkey(sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09001572 if (retval) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001573 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001574 return -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001575 }
1576 }
1577
1578 if (conn->dialect > SMB20_PROT_ID) {
1579 if (!ksmbd_conn_lookup_dialect(conn)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001580 pr_err("fail to verify the dialect\n");
Namjae Jeon58090b12021-07-16 14:52:09 +09001581 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001582 }
1583 }
1584 return 0;
1585}
1586#else
1587static int krb5_authenticate(struct ksmbd_work *work)
1588{
1589 return -EOPNOTSUPP;
1590}
1591#endif
1592
1593int smb2_sess_setup(struct ksmbd_work *work)
1594{
1595 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09001596 struct smb2_sess_setup_req *req = work->request_buf;
1597 struct smb2_sess_setup_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001598 struct ksmbd_session *sess;
1599 struct negotiate_message *negblob;
1600 int rc = 0;
1601
1602 ksmbd_debug(SMB, "Received request for session setup\n");
1603
1604 rsp->StructureSize = cpu_to_le16(9);
1605 rsp->SessionFlags = 0;
1606 rsp->SecurityBufferOffset = cpu_to_le16(72);
1607 rsp->SecurityBufferLength = 0;
1608 inc_rfc1001_len(rsp, 9);
1609
1610 if (!req->hdr.SessionId) {
1611 sess = ksmbd_smb2_session_create();
1612 if (!sess) {
1613 rc = -ENOMEM;
1614 goto out_err;
1615 }
1616 rsp->hdr.SessionId = cpu_to_le64(sess->id);
1617 ksmbd_session_register(conn, sess);
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001618 } else if (conn->dialect >= SMB30_PROT_ID &&
1619 (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
1620 req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) {
1621 u64 sess_id = le64_to_cpu(req->hdr.SessionId);
1622
1623 sess = ksmbd_session_lookup_slowpath(sess_id);
1624 if (!sess) {
1625 rc = -ENOENT;
1626 goto out_err;
1627 }
1628
1629 if (conn->dialect != sess->conn->dialect) {
1630 rc = -EINVAL;
1631 goto out_err;
1632 }
1633
1634 if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) {
1635 rc = -EINVAL;
1636 goto out_err;
1637 }
1638
1639 if (strncmp(conn->ClientGUID, sess->conn->ClientGUID,
1640 SMB2_CLIENT_GUID_SIZE)) {
1641 rc = -ENOENT;
1642 goto out_err;
1643 }
1644
1645 if (sess->state == SMB2_SESSION_IN_PROGRESS) {
1646 rc = -EACCES;
1647 goto out_err;
1648 }
1649
1650 if (sess->state == SMB2_SESSION_EXPIRED) {
1651 rc = -EFAULT;
1652 goto out_err;
1653 }
1654
1655 if (ksmbd_session_lookup(conn, sess_id)) {
1656 rc = -EACCES;
1657 goto out_err;
1658 }
1659
1660 conn->binding = true;
1661 } else if ((conn->dialect < SMB30_PROT_ID ||
1662 server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) &&
1663 (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) {
Colin Ian King4951a842021-07-06 13:05:01 +01001664 sess = NULL;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001665 rc = -EACCES;
1666 goto out_err;
Namjae Jeone2f34482021-03-16 10:49:09 +09001667 } else {
1668 sess = ksmbd_session_lookup(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001669 le64_to_cpu(req->hdr.SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09001670 if (!sess) {
1671 rc = -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09001672 goto out_err;
1673 }
1674 }
1675 work->sess = sess;
1676
1677 if (sess->state == SMB2_SESSION_EXPIRED)
1678 sess->state = SMB2_SESSION_IN_PROGRESS;
1679
1680 negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId +
1681 le16_to_cpu(req->SecurityBufferOffset));
1682
1683 if (decode_negotiation_token(work, negblob) == 0) {
1684 if (conn->mechToken)
1685 negblob = (struct negotiate_message *)conn->mechToken;
1686 }
1687
1688 if (server_conf.auth_mechs & conn->auth_mechs) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001689 rc = generate_preauth_hash(work);
1690 if (rc)
1691 goto out_err;
1692
Namjae Jeone2f34482021-03-16 10:49:09 +09001693 if (conn->preferred_auth_mech &
1694 (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001695 rc = krb5_authenticate(work);
1696 if (rc) {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001697 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001698 goto out_err;
1699 }
1700
1701 ksmbd_conn_set_good(work);
1702 sess->state = SMB2_SESSION_VALID;
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001703 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001704 sess->Preauth_HashValue = NULL;
1705 } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001706 if (negblob->MessageType == NtLmNegotiate) {
1707 rc = ntlm_negotiate(work, negblob);
1708 if (rc)
1709 goto out_err;
1710 rsp->hdr.Status =
1711 STATUS_MORE_PROCESSING_REQUIRED;
1712 /*
1713 * Note: here total size -1 is done as an
1714 * adjustment for 0 size blob
1715 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09001716 inc_rfc1001_len(rsp, le16_to_cpu(rsp->SecurityBufferLength) - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001717
1718 } else if (negblob->MessageType == NtLmAuthenticate) {
1719 rc = ntlm_authenticate(work);
1720 if (rc)
1721 goto out_err;
1722
1723 ksmbd_conn_set_good(work);
1724 sess->state = SMB2_SESSION_VALID;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001725 if (conn->binding) {
1726 struct preauth_session *preauth_sess;
1727
1728 preauth_sess =
1729 ksmbd_preauth_session_lookup(conn, sess->id);
1730 if (preauth_sess) {
1731 list_del(&preauth_sess->preauth_entry);
1732 kfree(preauth_sess);
1733 }
1734 }
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001735 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001736 sess->Preauth_HashValue = NULL;
1737 }
1738 } else {
1739 /* TODO: need one more negotiation */
Namjae Jeonbde16942021-06-28 15:23:19 +09001740 pr_err("Not support the preferred authentication\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001741 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001742 }
1743 } else {
Namjae Jeonbde16942021-06-28 15:23:19 +09001744 pr_err("Not support authentication\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001745 rc = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09001746 }
1747
1748out_err:
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001749 if (rc == -EINVAL)
1750 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1751 else if (rc == -ENOENT)
1752 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1753 else if (rc == -EACCES)
1754 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
1755 else if (rc == -EFAULT)
1756 rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED;
Namjae Jeon58090b12021-07-16 14:52:09 +09001757 else if (rc == -ENOMEM)
1758 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
Namjae Jeonf5a544e2021-06-18 10:04:19 +09001759 else if (rc)
1760 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1761
Namjae Jeone2f34482021-03-16 10:49:09 +09001762 if (conn->use_spnego && conn->mechToken) {
1763 kfree(conn->mechToken);
1764 conn->mechToken = NULL;
1765 }
1766
1767 if (rc < 0 && sess) {
1768 ksmbd_session_destroy(sess);
1769 work->sess = NULL;
1770 }
1771
1772 return rc;
1773}
1774
1775/**
1776 * smb2_tree_connect() - handler for smb2 tree connect command
1777 * @work: smb work containing smb request buffer
1778 *
1779 * Return: 0 on success, otherwise error
1780 */
1781int smb2_tree_connect(struct ksmbd_work *work)
1782{
1783 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09001784 struct smb2_tree_connect_req *req = work->request_buf;
1785 struct smb2_tree_connect_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001786 struct ksmbd_session *sess = work->sess;
1787 char *treename = NULL, *name = NULL;
1788 struct ksmbd_tree_conn_status status;
1789 struct ksmbd_share_config *share;
1790 int rc = -EINVAL;
1791
1792 treename = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09001793 le16_to_cpu(req->PathLength), true,
1794 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09001795 if (IS_ERR(treename)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09001796 pr_err("treename is NULL\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001797 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1798 goto out_err1;
1799 }
1800
Stephen Rothwell36ba3862021-03-17 17:01:15 +09001801 name = ksmbd_extract_sharename(treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001802 if (IS_ERR(name)) {
1803 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1804 goto out_err1;
1805 }
1806
1807 ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001808 name, treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001809
1810 status = ksmbd_tree_conn_connect(sess, name);
1811 if (status.ret == KSMBD_TREE_CONN_STATUS_OK)
1812 rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id);
1813 else
1814 goto out_err1;
1815
1816 share = status.tree_conn->share_conf;
1817 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
1818 ksmbd_debug(SMB, "IPC share path request\n");
1819 rsp->ShareType = SMB2_SHARE_TYPE_PIPE;
1820 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1821 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE |
1822 FILE_DELETE_LE | FILE_READ_CONTROL_LE |
1823 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1824 FILE_SYNCHRONIZE_LE;
1825 } else {
1826 rsp->ShareType = SMB2_SHARE_TYPE_DISK;
1827 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1828 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE;
1829 if (test_tree_conn_flag(status.tree_conn,
1830 KSMBD_TREE_CONN_FLAG_WRITABLE)) {
1831 rsp->MaximalAccess |= FILE_WRITE_DATA_LE |
1832 FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE |
Wan Jiabing3aefd542021-06-07 12:54:32 +08001833 FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE |
1834 FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE |
1835 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1836 FILE_SYNCHRONIZE_LE;
Namjae Jeone2f34482021-03-16 10:49:09 +09001837 }
1838 }
1839
1840 status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1841 if (conn->posix_ext_supported)
1842 status.tree_conn->posix_extensions = true;
1843
1844out_err1:
1845 rsp->StructureSize = cpu_to_le16(16);
1846 rsp->Capabilities = 0;
1847 rsp->Reserved = 0;
1848 /* default manual caching */
1849 rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING;
1850 inc_rfc1001_len(rsp, 16);
1851
1852 if (!IS_ERR(treename))
1853 kfree(treename);
1854 if (!IS_ERR(name))
1855 kfree(name);
1856
1857 switch (status.ret) {
1858 case KSMBD_TREE_CONN_STATUS_OK:
1859 rsp->hdr.Status = STATUS_SUCCESS;
1860 rc = 0;
1861 break;
1862 case KSMBD_TREE_CONN_STATUS_NO_SHARE:
1863 rsp->hdr.Status = STATUS_BAD_NETWORK_PATH;
1864 break;
1865 case -ENOMEM:
1866 case KSMBD_TREE_CONN_STATUS_NOMEM:
1867 rsp->hdr.Status = STATUS_NO_MEMORY;
1868 break;
1869 case KSMBD_TREE_CONN_STATUS_ERROR:
1870 case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS:
1871 case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS:
1872 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1873 break;
1874 case -EINVAL:
1875 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1876 break;
1877 default:
1878 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1879 }
1880
1881 return rc;
1882}
1883
1884/**
1885 * smb2_create_open_flags() - convert smb open flags to unix open flags
1886 * @file_present: is file already present
1887 * @access: file access flags
1888 * @disposition: file disposition flags
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001889 * @may_flags: set with MAY_ flags
Namjae Jeone2f34482021-03-16 10:49:09 +09001890 *
1891 * Return: file open flags
1892 */
1893static int smb2_create_open_flags(bool file_present, __le32 access,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001894 __le32 disposition,
1895 int *may_flags)
Namjae Jeone2f34482021-03-16 10:49:09 +09001896{
1897 int oflags = O_NONBLOCK | O_LARGEFILE;
1898
1899 if (access & FILE_READ_DESIRED_ACCESS_LE &&
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001900 access & FILE_WRITE_DESIRE_ACCESS_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001901 oflags |= O_RDWR;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001902 *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE;
1903 } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001904 oflags |= O_WRONLY;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001905 *may_flags = MAY_OPEN | MAY_WRITE;
1906 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09001907 oflags |= O_RDONLY;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001908 *may_flags = MAY_OPEN | MAY_READ;
1909 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001910
1911 if (access == FILE_READ_ATTRIBUTES_LE)
1912 oflags |= O_PATH;
1913
1914 if (file_present) {
1915 switch (disposition & FILE_CREATE_MASK_LE) {
1916 case FILE_OPEN_LE:
1917 case FILE_CREATE_LE:
1918 break;
1919 case FILE_SUPERSEDE_LE:
1920 case FILE_OVERWRITE_LE:
1921 case FILE_OVERWRITE_IF_LE:
1922 oflags |= O_TRUNC;
1923 break;
1924 default:
1925 break;
1926 }
1927 } else {
1928 switch (disposition & FILE_CREATE_MASK_LE) {
1929 case FILE_SUPERSEDE_LE:
1930 case FILE_CREATE_LE:
1931 case FILE_OPEN_IF_LE:
1932 case FILE_OVERWRITE_IF_LE:
1933 oflags |= O_CREAT;
1934 break;
1935 case FILE_OPEN_LE:
1936 case FILE_OVERWRITE_LE:
1937 oflags &= ~O_CREAT;
1938 break;
1939 default:
1940 break;
1941 }
1942 }
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09001943
Namjae Jeone2f34482021-03-16 10:49:09 +09001944 return oflags;
1945}
1946
1947/**
1948 * smb2_tree_disconnect() - handler for smb tree connect request
1949 * @work: smb work containing request buffer
1950 *
1951 * Return: 0
1952 */
1953int smb2_tree_disconnect(struct ksmbd_work *work)
1954{
Namjae Jeone5066492021-03-30 12:35:23 +09001955 struct smb2_tree_disconnect_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001956 struct ksmbd_session *sess = work->sess;
1957 struct ksmbd_tree_connect *tcon = work->tcon;
1958
1959 rsp->StructureSize = cpu_to_le16(4);
1960 inc_rfc1001_len(rsp, 4);
1961
1962 ksmbd_debug(SMB, "request\n");
1963
1964 if (!tcon) {
Namjae Jeone5066492021-03-30 12:35:23 +09001965 struct smb2_tree_disconnect_req *req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001966
1967 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
1968 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
1969 smb2_set_err_rsp(work);
1970 return 0;
1971 }
1972
1973 ksmbd_close_tree_conn_fds(work);
1974 ksmbd_tree_conn_disconnect(sess, tcon);
1975 return 0;
1976}
1977
1978/**
1979 * smb2_session_logoff() - handler for session log off request
1980 * @work: smb work containing request buffer
1981 *
1982 * Return: 0
1983 */
1984int smb2_session_logoff(struct ksmbd_work *work)
1985{
1986 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09001987 struct smb2_logoff_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001988 struct ksmbd_session *sess = work->sess;
1989
1990 rsp->StructureSize = cpu_to_le16(4);
1991 inc_rfc1001_len(rsp, 4);
1992
1993 ksmbd_debug(SMB, "request\n");
1994
1995 /* Got a valid session, set connection state */
1996 WARN_ON(sess->conn != conn);
1997
1998 /* setting CifsExiting here may race with start_tcp_sess */
1999 ksmbd_conn_set_need_reconnect(work);
2000 ksmbd_close_session_fds(work);
2001 ksmbd_conn_wait_idle(conn);
2002
2003 if (ksmbd_tree_conn_session_logoff(sess)) {
Namjae Jeone5066492021-03-30 12:35:23 +09002004 struct smb2_logoff_req *req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09002005
2006 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
2007 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
2008 smb2_set_err_rsp(work);
2009 return 0;
2010 }
2011
2012 ksmbd_destroy_file_table(&sess->file_table);
2013 sess->state = SMB2_SESSION_EXPIRED;
2014
2015 ksmbd_free_user(sess->user);
2016 sess->user = NULL;
2017
2018 /* let start_tcp_sess free connection info now */
2019 ksmbd_conn_set_need_negotiate(work);
2020 return 0;
2021}
2022
2023/**
2024 * create_smb2_pipe() - create IPC pipe
2025 * @work: smb work containing request buffer
2026 *
2027 * Return: 0 on success, otherwise error
2028 */
2029static noinline int create_smb2_pipe(struct ksmbd_work *work)
2030{
Namjae Jeone5066492021-03-30 12:35:23 +09002031 struct smb2_create_rsp *rsp = work->response_buf;
2032 struct smb2_create_req *req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09002033 int id;
2034 int err;
2035 char *name;
2036
2037 name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09002038 1, work->conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09002039 if (IS_ERR(name)) {
2040 rsp->hdr.Status = STATUS_NO_MEMORY;
2041 err = PTR_ERR(name);
2042 goto out;
2043 }
2044
2045 id = ksmbd_session_rpc_open(work->sess, name);
Marios Makassikis79caa962021-05-06 11:38:35 +09002046 if (id < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002047 pr_err("Unable to open RPC pipe: %d\n", id);
Marios Makassikis79caa962021-05-06 11:38:35 +09002048 err = id;
2049 goto out;
2050 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002051
Marios Makassikis79caa962021-05-06 11:38:35 +09002052 rsp->hdr.Status = STATUS_SUCCESS;
Namjae Jeone2f34482021-03-16 10:49:09 +09002053 rsp->StructureSize = cpu_to_le16(89);
2054 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
2055 rsp->Reserved = 0;
2056 rsp->CreateAction = cpu_to_le32(FILE_OPENED);
2057
2058 rsp->CreationTime = cpu_to_le64(0);
2059 rsp->LastAccessTime = cpu_to_le64(0);
2060 rsp->ChangeTime = cpu_to_le64(0);
2061 rsp->AllocationSize = cpu_to_le64(0);
2062 rsp->EndofFile = cpu_to_le64(0);
2063 rsp->FileAttributes = ATTR_NORMAL_LE;
2064 rsp->Reserved2 = 0;
2065 rsp->VolatileFileId = cpu_to_le64(id);
2066 rsp->PersistentFileId = 0;
2067 rsp->CreateContextsOffset = 0;
2068 rsp->CreateContextsLength = 0;
2069
2070 inc_rfc1001_len(rsp, 88); /* StructureSize - 1*/
2071 kfree(name);
2072 return 0;
2073
2074out:
Marios Makassikis79caa962021-05-06 11:38:35 +09002075 switch (err) {
2076 case -EINVAL:
2077 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2078 break;
2079 case -ENOSPC:
2080 case -ENOMEM:
2081 rsp->hdr.Status = STATUS_NO_MEMORY;
2082 break;
2083 }
2084
2085 if (!IS_ERR(name))
2086 kfree(name);
2087
Namjae Jeone2f34482021-03-16 10:49:09 +09002088 smb2_set_err_rsp(work);
2089 return err;
2090}
2091
Namjae Jeone2f34482021-03-16 10:49:09 +09002092/**
2093 * smb2_set_ea() - handler for setting extended attributes using set
2094 * info command
2095 * @eabuf: set info command buffer
2096 * @path: dentry path for get ea
2097 *
2098 * Return: 0 on success, otherwise error
2099 */
2100static int smb2_set_ea(struct smb2_ea_info *eabuf, struct path *path)
2101{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002102 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002103 char *attr_name = NULL, *value;
2104 int rc = 0;
2105 int next = 0;
2106
2107 attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL);
2108 if (!attr_name)
2109 return -ENOMEM;
2110
2111 do {
2112 if (!eabuf->EaNameLength)
2113 goto next;
2114
2115 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002116 "name : <%s>, name_len : %u, value_len : %u, next : %u\n",
2117 eabuf->name, eabuf->EaNameLength,
2118 le16_to_cpu(eabuf->EaValueLength),
2119 le32_to_cpu(eabuf->NextEntryOffset));
Namjae Jeone2f34482021-03-16 10:49:09 +09002120
2121 if (eabuf->EaNameLength >
Namjae Jeon070fb212021-05-26 17:57:12 +09002122 (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002123 rc = -EINVAL;
2124 break;
2125 }
2126
2127 memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2128 memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002129 eabuf->EaNameLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09002130 attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0';
2131 value = (char *)&eabuf->name + eabuf->EaNameLength + 1;
2132
2133 if (!eabuf->EaValueLength) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002134 rc = ksmbd_vfs_casexattr_len(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002135 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002136 attr_name,
2137 XATTR_USER_PREFIX_LEN +
2138 eabuf->EaNameLength);
2139
2140 /* delete the EA only when it exits */
2141 if (rc > 0) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002142 rc = ksmbd_vfs_remove_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002143 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002144 attr_name);
2145
2146 if (rc < 0) {
2147 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002148 "remove xattr failed(%d)\n",
2149 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002150 break;
2151 }
2152 }
2153
2154 /* if the EA doesn't exist, just do nothing. */
2155 rc = 0;
2156 } else {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002157 rc = ksmbd_vfs_setxattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002158 path->dentry, attr_name, value,
Namjae Jeon070fb212021-05-26 17:57:12 +09002159 le16_to_cpu(eabuf->EaValueLength), 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002160 if (rc < 0) {
2161 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002162 "ksmbd_vfs_setxattr is failed(%d)\n",
2163 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002164 break;
2165 }
2166 }
2167
2168next:
2169 next = le32_to_cpu(eabuf->NextEntryOffset);
2170 eabuf = (struct smb2_ea_info *)((char *)eabuf + next);
2171 } while (next != 0);
2172
2173 kfree(attr_name);
2174 return rc;
2175}
2176
Namjae Jeone2f34482021-03-16 10:49:09 +09002177static noinline int smb2_set_stream_name_xattr(struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002178 struct ksmbd_file *fp,
2179 char *stream_name, int s_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09002180{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002181 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002182 size_t xattr_stream_size;
2183 char *xattr_stream_name;
2184 int rc;
2185
2186 rc = ksmbd_vfs_xattr_stream_name(stream_name,
2187 &xattr_stream_name,
2188 &xattr_stream_size,
2189 s_type);
2190 if (rc)
2191 return rc;
2192
2193 fp->stream.name = xattr_stream_name;
2194 fp->stream.size = xattr_stream_size;
2195
2196 /* Check if there is stream prefix in xattr space */
Hyunchul Lee465d7202021-07-03 12:10:36 +09002197 rc = ksmbd_vfs_casexattr_len(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002198 path->dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002199 xattr_stream_name,
2200 xattr_stream_size);
2201 if (rc >= 0)
2202 return 0;
2203
2204 if (fp->cdoption == FILE_OPEN_LE) {
2205 ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc);
2206 return -EBADF;
2207 }
2208
Hyunchul Lee465d7202021-07-03 12:10:36 +09002209 rc = ksmbd_vfs_setxattr(user_ns, path->dentry,
2210 xattr_stream_name, NULL, 0, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002211 if (rc < 0)
Namjae Jeonbde16942021-06-28 15:23:19 +09002212 pr_err("Failed to store XATTR stream name :%d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002213 return 0;
2214}
2215
Hyunchul Leeef24c962021-06-30 18:25:52 +09002216static int smb2_remove_smb_xattrs(struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002217{
Hyunchul Lee465d7202021-07-03 12:10:36 +09002218 struct user_namespace *user_ns = mnt_user_ns(path->mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002219 char *name, *xattr_list = NULL;
2220 ssize_t xattr_list_len;
2221 int err = 0;
2222
Hyunchul Leeef24c962021-06-30 18:25:52 +09002223 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002224 if (xattr_list_len < 0) {
2225 goto out;
2226 } else if (!xattr_list_len) {
2227 ksmbd_debug(SMB, "empty xattr in the file\n");
2228 goto out;
2229 }
2230
2231 for (name = xattr_list; name - xattr_list < xattr_list_len;
2232 name += strlen(name) + 1) {
2233 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
2234
2235 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002236 strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX,
2237 DOS_ATTRIBUTE_PREFIX_LEN) &&
2238 strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09002239 continue;
2240
Hyunchul Lee465d7202021-07-03 12:10:36 +09002241 err = ksmbd_vfs_remove_xattr(user_ns, path->dentry, name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002242 if (err)
2243 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
2244 }
2245out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09002246 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002247 return err;
2248}
2249
2250static int smb2_create_truncate(struct path *path)
2251{
2252 int rc = vfs_truncate(path, 0);
2253
2254 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002255 pr_err("vfs_truncate failed, rc %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002256 return rc;
2257 }
2258
Hyunchul Leeef24c962021-06-30 18:25:52 +09002259 rc = smb2_remove_smb_xattrs(path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002260 if (rc == -EOPNOTSUPP)
2261 rc = 0;
2262 if (rc)
2263 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002264 "ksmbd_truncate_stream_name_xattr failed, rc %d\n",
2265 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002266 return rc;
2267}
2268
Namjae Jeon64b39f42021-03-30 14:25:35 +09002269static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002270 struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002271{
2272 struct xattr_dos_attrib da = {0};
2273 int rc;
2274
2275 if (!test_share_config_flag(tcon->share_conf,
2276 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
2277 return;
2278
2279 da.version = 4;
2280 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
2281 da.itime = da.create_time = fp->create_time;
2282 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
2283 XATTR_DOSINFO_ITIME;
2284
Hyunchul Leeaf349832021-06-30 18:25:53 +09002285 rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_user_ns(path->mnt),
2286 path->dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09002287 if (rc)
2288 ksmbd_debug(SMB, "failed to store file attribute into xattr\n");
2289}
2290
2291static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon,
Namjae Jeon070fb212021-05-26 17:57:12 +09002292 struct path *path, struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002293{
2294 struct xattr_dos_attrib da;
2295 int rc;
2296
2297 fp->f_ci->m_fattr &= ~(ATTR_HIDDEN_LE | ATTR_SYSTEM_LE);
2298
2299 /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */
2300 if (!test_share_config_flag(tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002301 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
Namjae Jeone2f34482021-03-16 10:49:09 +09002302 return;
2303
Hyunchul Leeaf349832021-06-30 18:25:53 +09002304 rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_user_ns(path->mnt),
2305 path->dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09002306 if (rc > 0) {
2307 fp->f_ci->m_fattr = cpu_to_le32(da.attr);
2308 fp->create_time = da.create_time;
2309 fp->itime = da.itime;
2310 }
2311}
2312
Namjae Jeon64b39f42021-03-30 14:25:35 +09002313static int smb2_creat(struct ksmbd_work *work, struct path *path, char *name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002314 int open_flags, umode_t posix_mode, bool is_dir)
Namjae Jeone2f34482021-03-16 10:49:09 +09002315{
2316 struct ksmbd_tree_connect *tcon = work->tcon;
2317 struct ksmbd_share_config *share = tcon->share_conf;
2318 umode_t mode;
2319 int rc;
2320
2321 if (!(open_flags & O_CREAT))
2322 return -EBADF;
2323
2324 ksmbd_debug(SMB, "file does not exist, so creating\n");
2325 if (is_dir == true) {
2326 ksmbd_debug(SMB, "creating directory\n");
2327
2328 mode = share_config_directory_mode(share, posix_mode);
2329 rc = ksmbd_vfs_mkdir(work, name, mode);
2330 if (rc)
2331 return rc;
2332 } else {
2333 ksmbd_debug(SMB, "creating regular file\n");
2334
2335 mode = share_config_create_mode(share, posix_mode);
2336 rc = ksmbd_vfs_create(work, name, mode);
2337 if (rc)
2338 return rc;
2339 }
2340
Hyunchul Lee265fd192021-09-25 00:06:16 +09002341 rc = ksmbd_vfs_kern_path(work, name, 0, path, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002342 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002343 pr_err("cannot get linux path (%s), err = %d\n",
2344 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002345 return rc;
2346 }
2347 return 0;
2348}
2349
2350static int smb2_create_sd_buffer(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09002351 struct smb2_create_req *req,
Hyunchul Leeef24c962021-06-30 18:25:52 +09002352 struct path *path)
Namjae Jeone2f34482021-03-16 10:49:09 +09002353{
2354 struct create_context *context;
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002355 struct create_sd_buf_req *sd_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09002356
2357 if (!req->CreateContextsOffset)
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002358 return -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09002359
2360 /* Parse SD BUFFER create contexts */
2361 context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER);
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002362 if (!context)
2363 return -ENOENT;
2364 else if (IS_ERR(context))
2365 return PTR_ERR(context);
Namjae Jeone2f34482021-03-16 10:49:09 +09002366
Hyunchul Lee21dd1fd2021-07-09 17:06:34 +09002367 ksmbd_debug(SMB,
2368 "Set ACLs using SMB2_CREATE_SD_BUFFER context\n");
2369 sd_buf = (struct create_sd_buf_req *)context;
2370 return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd,
2371 le32_to_cpu(sd_buf->ccontext.DataLength), true);
Namjae Jeone2f34482021-03-16 10:49:09 +09002372}
2373
Christian Brauner43205ca2021-08-23 17:13:50 +02002374static void ksmbd_acls_fattr(struct smb_fattr *fattr,
2375 struct user_namespace *mnt_userns,
2376 struct inode *inode)
Namjae Jeon3d47e542021-04-20 14:25:35 +09002377{
Christian Brauner43205ca2021-08-23 17:13:50 +02002378 fattr->cf_uid = i_uid_into_mnt(mnt_userns, inode);
2379 fattr->cf_gid = i_gid_into_mnt(mnt_userns, inode);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002380 fattr->cf_mode = inode->i_mode;
Namjae Jeon777cad12021-08-13 08:15:33 +09002381 fattr->cf_acls = NULL;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002382 fattr->cf_dacls = NULL;
2383
Namjae Jeon777cad12021-08-13 08:15:33 +09002384 if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) {
2385 fattr->cf_acls = get_acl(inode, ACL_TYPE_ACCESS);
2386 if (S_ISDIR(inode->i_mode))
2387 fattr->cf_dacls = get_acl(inode, ACL_TYPE_DEFAULT);
2388 }
Namjae Jeon3d47e542021-04-20 14:25:35 +09002389}
2390
Namjae Jeone2f34482021-03-16 10:49:09 +09002391/**
2392 * smb2_open() - handler for smb file open request
2393 * @work: smb work containing request buffer
2394 *
2395 * Return: 0 on success, otherwise error
2396 */
2397int smb2_open(struct ksmbd_work *work)
2398{
2399 struct ksmbd_conn *conn = work->conn;
2400 struct ksmbd_session *sess = work->sess;
2401 struct ksmbd_tree_connect *tcon = work->tcon;
2402 struct smb2_create_req *req;
2403 struct smb2_create_rsp *rsp, *rsp_org;
2404 struct path path;
2405 struct ksmbd_share_config *share = tcon->share_conf;
2406 struct ksmbd_file *fp = NULL;
2407 struct file *filp = NULL;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002408 struct user_namespace *user_ns = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002409 struct kstat stat;
2410 struct create_context *context;
2411 struct lease_ctx_info *lc = NULL;
2412 struct create_ea_buf_req *ea_buf = NULL;
2413 struct oplock_info *opinfo;
2414 __le32 *next_ptr = NULL;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002415 int req_op_level = 0, open_flags = 0, may_flags = 0, file_info = 0;
Hyunchul Lee265fd192021-09-25 00:06:16 +09002416 int rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002417 int contxt_cnt = 0, query_disk_id = 0;
2418 int maximal_access_ctxt = 0, posix_ctxt = 0;
2419 int s_type = 0;
2420 int next_off = 0;
2421 char *name = NULL;
2422 char *stream_name = NULL;
2423 bool file_present = false, created = false, already_permitted = false;
Namjae Jeone2f34482021-03-16 10:49:09 +09002424 int share_ret, need_truncate = 0;
2425 u64 time;
2426 umode_t posix_mode = 0;
2427 __le32 daccess, maximal_access = 0;
2428
Namjae Jeone5066492021-03-30 12:35:23 +09002429 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09002430 WORK_BUFFERS(work, req, rsp);
2431
2432 if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002433 (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002434 ksmbd_debug(SMB, "invalid flag in chained command\n");
2435 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2436 smb2_set_err_rsp(work);
2437 return -EINVAL;
2438 }
2439
2440 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
2441 ksmbd_debug(SMB, "IPC pipe create request\n");
2442 return create_smb2_pipe(work);
2443 }
2444
2445 if (req->NameLength) {
2446 if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002447 *(char *)req->Buffer == '\\') {
Namjae Jeonbde16942021-06-28 15:23:19 +09002448 pr_err("not allow directory name included leading slash\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002449 rc = -EINVAL;
2450 goto err_out1;
2451 }
2452
2453 name = smb2_get_name(share,
2454 req->Buffer,
2455 le16_to_cpu(req->NameLength),
2456 work->conn->local_nls);
2457 if (IS_ERR(name)) {
2458 rc = PTR_ERR(name);
2459 if (rc != -ENOMEM)
2460 rc = -ENOENT;
Dan Carpenter8b99f352021-08-02 08:14:03 +09002461 name = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09002462 goto err_out1;
2463 }
2464
2465 ksmbd_debug(SMB, "converted name = %s\n", name);
2466 if (strchr(name, ':')) {
2467 if (!test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002468 KSMBD_SHARE_FLAG_STREAMS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002469 rc = -EBADF;
2470 goto err_out1;
2471 }
2472 rc = parse_stream_name(name, &stream_name, &s_type);
2473 if (rc < 0)
2474 goto err_out1;
2475 }
2476
2477 rc = ksmbd_validate_filename(name);
2478 if (rc < 0)
2479 goto err_out1;
2480
2481 if (ksmbd_share_veto_filename(share, name)) {
2482 rc = -ENOENT;
2483 ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002484 name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002485 goto err_out1;
2486 }
2487 } else {
Hyunchul Lee265fd192021-09-25 00:06:16 +09002488 name = kstrdup("", GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09002489 if (!name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002490 rc = -ENOMEM;
2491 goto err_out1;
2492 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002493 }
2494
2495 req_op_level = req->RequestedOplockLevel;
Namjae Jeon73f9dad2021-04-16 14:12:06 +09002496 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002497 lc = parse_lease_state(req);
Namjae Jeone2f34482021-03-16 10:49:09 +09002498
Namjae Jeon64b39f42021-03-30 14:25:35 +09002499 if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002500 pr_err("Invalid impersonationlevel : 0x%x\n",
2501 le32_to_cpu(req->ImpersonationLevel));
Namjae Jeone2f34482021-03-16 10:49:09 +09002502 rc = -EIO;
2503 rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL;
2504 goto err_out1;
2505 }
2506
2507 if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002508 pr_err("Invalid create options : 0x%x\n",
2509 le32_to_cpu(req->CreateOptions));
Namjae Jeone2f34482021-03-16 10:49:09 +09002510 rc = -EINVAL;
2511 goto err_out1;
2512 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09002513 if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002514 req->CreateOptions & FILE_RANDOM_ACCESS_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002515 req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE);
2516
Namjae Jeon070fb212021-05-26 17:57:12 +09002517 if (req->CreateOptions &
2518 (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION |
2519 FILE_RESERVE_OPFILTER_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002520 rc = -EOPNOTSUPP;
2521 goto err_out1;
2522 }
2523
2524 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2525 if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) {
2526 rc = -EINVAL;
2527 goto err_out1;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002528 } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002529 req->CreateOptions = ~(FILE_NO_COMPRESSION_LE);
Namjae Jeon64b39f42021-03-30 14:25:35 +09002530 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002531 }
2532 }
2533
2534 if (le32_to_cpu(req->CreateDisposition) >
Namjae Jeon070fb212021-05-26 17:57:12 +09002535 le32_to_cpu(FILE_OVERWRITE_IF_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002536 pr_err("Invalid create disposition : 0x%x\n",
2537 le32_to_cpu(req->CreateDisposition));
Namjae Jeone2f34482021-03-16 10:49:09 +09002538 rc = -EINVAL;
2539 goto err_out1;
2540 }
2541
2542 if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002543 pr_err("Invalid desired access : 0x%x\n",
2544 le32_to_cpu(req->DesiredAccess));
Namjae Jeone2f34482021-03-16 10:49:09 +09002545 rc = -EACCES;
2546 goto err_out1;
2547 }
2548
Namjae Jeon64b39f42021-03-30 14:25:35 +09002549 if (req->FileAttributes && !(req->FileAttributes & ATTR_MASK_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09002550 pr_err("Invalid file attribute : 0x%x\n",
2551 le32_to_cpu(req->FileAttributes));
Namjae Jeone2f34482021-03-16 10:49:09 +09002552 rc = -EINVAL;
2553 goto err_out1;
2554 }
2555
2556 if (req->CreateContextsOffset) {
2557 /* Parse non-durable handle create contexts */
2558 context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002559 if (IS_ERR(context)) {
2560 rc = PTR_ERR(context);
2561 goto err_out1;
2562 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002563 ea_buf = (struct create_ea_buf_req *)context;
2564 if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) {
2565 rsp->hdr.Status = STATUS_ACCESS_DENIED;
2566 rc = -EACCES;
2567 goto err_out1;
2568 }
2569 }
2570
2571 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002572 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002573 if (IS_ERR(context)) {
2574 rc = PTR_ERR(context);
2575 goto err_out1;
2576 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002577 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002578 "get query maximal access context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002579 maximal_access_ctxt = 1;
2580 }
2581
2582 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002583 SMB2_CREATE_TIMEWARP_REQUEST);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002584 if (IS_ERR(context)) {
2585 rc = PTR_ERR(context);
2586 goto err_out1;
2587 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002588 ksmbd_debug(SMB, "get timewarp context\n");
2589 rc = -EBADF;
2590 goto err_out1;
2591 }
2592
2593 if (tcon->posix_extensions) {
2594 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002595 SMB2_CREATE_TAG_POSIX);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002596 if (IS_ERR(context)) {
2597 rc = PTR_ERR(context);
2598 goto err_out1;
2599 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002600 struct create_posix *posix =
2601 (struct create_posix *)context;
2602 ksmbd_debug(SMB, "get posix context\n");
2603
2604 posix_mode = le32_to_cpu(posix->Mode);
2605 posix_ctxt = 1;
2606 }
2607 }
2608 }
2609
2610 if (ksmbd_override_fsids(work)) {
2611 rc = -ENOMEM;
2612 goto err_out1;
2613 }
2614
Hyunchul Lee265fd192021-09-25 00:06:16 +09002615 rc = ksmbd_vfs_kern_path(work, name, LOOKUP_NO_SYMLINKS, &path, 1);
Namjae Jeon4ea47792021-09-21 14:19:33 +09002616 if (!rc) {
2617 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002618 /*
2619 * If file exists with under flags, return access
2620 * denied error.
2621 */
2622 if (req->CreateDisposition == FILE_OVERWRITE_IF_LE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09002623 req->CreateDisposition == FILE_OPEN_IF_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002624 rc = -EACCES;
2625 path_put(&path);
2626 goto err_out;
2627 }
2628
Namjae Jeon64b39f42021-03-30 14:25:35 +09002629 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002630 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002631 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002632 rc = -EACCES;
2633 path_put(&path);
2634 goto err_out;
2635 }
Namjae Jeon4ea47792021-09-21 14:19:33 +09002636 } else if (d_is_symlink(path.dentry)) {
2637 rc = -EACCES;
2638 path_put(&path);
2639 goto err_out;
Namjae Jeone2f34482021-03-16 10:49:09 +09002640 }
2641 }
2642
2643 if (rc) {
Hyunchul Lee265fd192021-09-25 00:06:16 +09002644 if (rc != -ENOENT)
Namjae Jeone2f34482021-03-16 10:49:09 +09002645 goto err_out;
Namjae Jeone2f34482021-03-16 10:49:09 +09002646 ksmbd_debug(SMB, "can not get linux path for %s, rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002647 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002648 rc = 0;
2649 } else {
2650 file_present = true;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002651 user_ns = mnt_user_ns(path.mnt);
2652 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09002653 }
2654 if (stream_name) {
2655 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2656 if (s_type == DATA_STREAM) {
2657 rc = -EIO;
2658 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2659 }
2660 } else {
2661 if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) {
2662 rc = -EIO;
2663 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2664 }
2665 }
2666
2667 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002668 req->FileAttributes & ATTR_NORMAL_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002669 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2670 rc = -EIO;
2671 }
2672
2673 if (rc < 0)
2674 goto err_out;
2675 }
2676
Namjae Jeon64b39f42021-03-30 14:25:35 +09002677 if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE &&
2678 S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002679 ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002680 name, req->CreateOptions);
Namjae Jeone2f34482021-03-16 10:49:09 +09002681 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2682 rc = -EIO;
2683 goto err_out;
2684 }
2685
2686 if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002687 !(req->CreateDisposition == FILE_CREATE_LE) &&
2688 !S_ISDIR(stat.mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002689 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2690 rc = -EIO;
2691 goto err_out;
2692 }
2693
2694 if (!stream_name && file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002695 req->CreateDisposition == FILE_CREATE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002696 rc = -EEXIST;
2697 goto err_out;
2698 }
2699
Namjae Jeone2f34482021-03-16 10:49:09 +09002700 daccess = smb_map_generic_desired_access(req->DesiredAccess);
2701
2702 if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002703 rc = smb_check_perm_dacl(conn, &path, &daccess,
Namjae Jeon070fb212021-05-26 17:57:12 +09002704 sess->user->uid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002705 if (rc)
2706 goto err_out;
2707 }
2708
2709 if (daccess & FILE_MAXIMAL_ACCESS_LE) {
2710 if (!file_present) {
2711 daccess = cpu_to_le32(GENERIC_ALL_FLAGS);
2712 } else {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002713 rc = ksmbd_vfs_query_maximal_access(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002714 path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09002715 &daccess);
2716 if (rc)
2717 goto err_out;
2718 already_permitted = true;
2719 }
2720 maximal_access = daccess;
2721 }
2722
Namjae Jeon070fb212021-05-26 17:57:12 +09002723 open_flags = smb2_create_open_flags(file_present, daccess,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002724 req->CreateDisposition,
2725 &may_flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09002726
2727 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
2728 if (open_flags & O_CREAT) {
2729 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002730 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002731 rc = -EACCES;
2732 goto err_out;
2733 }
2734 }
2735
2736 /*create file if not present */
2737 if (!file_present) {
2738 rc = smb2_creat(work, &path, name, open_flags, posix_mode,
Namjae Jeon070fb212021-05-26 17:57:12 +09002739 req->CreateOptions & FILE_DIRECTORY_FILE_LE);
Marios Makassikisd337a442021-07-27 09:24:51 +09002740 if (rc) {
2741 if (rc == -ENOENT) {
2742 rc = -EIO;
2743 rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND;
2744 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002745 goto err_out;
Marios Makassikisd337a442021-07-27 09:24:51 +09002746 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002747
2748 created = true;
Hyunchul Lee465d7202021-07-03 12:10:36 +09002749 user_ns = mnt_user_ns(path.mnt);
Namjae Jeone2f34482021-03-16 10:49:09 +09002750 if (ea_buf) {
2751 rc = smb2_set_ea(&ea_buf->ea, &path);
2752 if (rc == -EOPNOTSUPP)
2753 rc = 0;
2754 else if (rc)
2755 goto err_out;
2756 }
2757 } else if (!already_permitted) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002758 /* FILE_READ_ATTRIBUTE is allowed without inode_permission,
2759 * because execute(search) permission on a parent directory,
2760 * is already granted.
2761 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09002762 if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002763 rc = inode_permission(user_ns,
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002764 d_inode(path.dentry),
2765 may_flags);
Namjae Jeonff1d5722021-04-13 13:18:10 +09002766 if (rc)
Namjae Jeone2f34482021-03-16 10:49:09 +09002767 goto err_out;
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002768
2769 if ((daccess & FILE_DELETE_LE) ||
2770 (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002771 rc = ksmbd_vfs_may_delete(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002772 path.dentry);
Hyunchul Lee6c5e36d2021-06-23 13:48:24 +09002773 if (rc)
2774 goto err_out;
2775 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002776 }
2777 }
2778
2779 rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent));
2780 if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) {
2781 rc = -EBUSY;
2782 goto err_out;
2783 }
2784
2785 rc = 0;
2786 filp = dentry_open(&path, open_flags, current_cred());
2787 if (IS_ERR(filp)) {
2788 rc = PTR_ERR(filp);
Namjae Jeonbde16942021-06-28 15:23:19 +09002789 pr_err("dentry open for dir failed, rc %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002790 goto err_out;
2791 }
2792
2793 if (file_present) {
2794 if (!(open_flags & O_TRUNC))
2795 file_info = FILE_OPENED;
2796 else
2797 file_info = FILE_OVERWRITTEN;
2798
Namjae Jeon070fb212021-05-26 17:57:12 +09002799 if ((req->CreateDisposition & FILE_CREATE_MASK_LE) ==
2800 FILE_SUPERSEDE_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002801 file_info = FILE_SUPERSEDED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002802 } else if (open_flags & O_CREAT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002803 file_info = FILE_CREATED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002804 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002805
2806 ksmbd_vfs_set_fadvise(filp, req->CreateOptions);
2807
2808 /* Obtain Volatile-ID */
2809 fp = ksmbd_open_fd(work, filp);
2810 if (IS_ERR(fp)) {
2811 fput(filp);
2812 rc = PTR_ERR(fp);
2813 fp = NULL;
2814 goto err_out;
2815 }
2816
2817 /* Get Persistent-ID */
2818 ksmbd_open_durable_fd(fp);
Namjae Jeon38673692021-07-08 12:32:27 +09002819 if (!has_file_id(fp->persistent_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002820 rc = -ENOMEM;
2821 goto err_out;
2822 }
2823
2824 fp->filename = name;
2825 fp->cdoption = req->CreateDisposition;
2826 fp->daccess = daccess;
2827 fp->saccess = req->ShareAccess;
2828 fp->coption = req->CreateOptions;
2829
2830 /* Set default windows and posix acls if creating new file */
2831 if (created) {
2832 int posix_acl_rc;
Namjae Jeonfba08fa2021-04-15 10:29:39 +09002833 struct inode *inode = d_inode(path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09002834
Hyunchul Lee465d7202021-07-03 12:10:36 +09002835 posix_acl_rc = ksmbd_vfs_inherit_posix_acl(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002836 inode,
2837 d_inode(path.dentry->d_parent));
Namjae Jeone2f34482021-03-16 10:49:09 +09002838 if (posix_acl_rc)
2839 ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc);
2840
2841 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002842 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002843 rc = smb_inherit_dacl(conn, &path, sess->user->uid,
Namjae Jeon070fb212021-05-26 17:57:12 +09002844 sess->user->gid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002845 }
2846
2847 if (rc) {
Hyunchul Leeef24c962021-06-30 18:25:52 +09002848 rc = smb2_create_sd_buffer(work, req, &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09002849 if (rc) {
2850 if (posix_acl_rc)
Hyunchul Lee465d7202021-07-03 12:10:36 +09002851 ksmbd_vfs_set_init_posix_acl(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002852 inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09002853
2854 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002855 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002856 struct smb_fattr fattr;
2857 struct smb_ntsd *pntsd;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002858 int pntsd_size, ace_num = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002859
Christian Brauner43205ca2021-08-23 17:13:50 +02002860 ksmbd_acls_fattr(&fattr, user_ns, inode);
Marios Makassikise6b10592021-04-15 10:24:56 +09002861 if (fattr.cf_acls)
2862 ace_num = fattr.cf_acls->a_count;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002863 if (fattr.cf_dacls)
2864 ace_num += fattr.cf_dacls->a_count;
Namjae Jeone2f34482021-03-16 10:49:09 +09002865
2866 pntsd = kmalloc(sizeof(struct smb_ntsd) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002867 sizeof(struct smb_sid) * 3 +
Namjae Jeone2f34482021-03-16 10:49:09 +09002868 sizeof(struct smb_acl) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002869 sizeof(struct smb_ace) * ace_num * 2,
Namjae Jeone2f34482021-03-16 10:49:09 +09002870 GFP_KERNEL);
2871 if (!pntsd)
2872 goto err_out;
2873
Hyunchul Lee465d7202021-07-03 12:10:36 +09002874 rc = build_sec_desc(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09002875 pntsd, NULL,
Namjae Jeon070fb212021-05-26 17:57:12 +09002876 OWNER_SECINFO |
Hyunchul Leeaf349832021-06-30 18:25:53 +09002877 GROUP_SECINFO |
2878 DACL_SECINFO,
Namjae Jeon070fb212021-05-26 17:57:12 +09002879 &pntsd_size, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09002880 posix_acl_release(fattr.cf_acls);
2881 posix_acl_release(fattr.cf_dacls);
2882
2883 rc = ksmbd_vfs_set_sd_xattr(conn,
Hyunchul Lee465d7202021-07-03 12:10:36 +09002884 user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +09002885 path.dentry,
2886 pntsd,
2887 pntsd_size);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002888 kfree(pntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09002889 if (rc)
Namjae Jeonbde16942021-06-28 15:23:19 +09002890 pr_err("failed to store ntacl in xattr : %d\n",
2891 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002892 }
2893 }
2894 }
2895 rc = 0;
2896 }
2897
2898 if (stream_name) {
2899 rc = smb2_set_stream_name_xattr(&path,
2900 fp,
2901 stream_name,
2902 s_type);
2903 if (rc)
2904 goto err_out;
2905 file_info = FILE_CREATED;
2906 }
2907
2908 fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE |
2909 FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE));
Namjae Jeon64b39f42021-03-30 14:25:35 +09002910 if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC &&
2911 !fp->attrib_only && !stream_name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002912 smb_break_all_oplock(work, fp);
2913 need_truncate = 1;
2914 }
2915
2916 /* fp should be searchable through ksmbd_inode.m_fp_list
2917 * after daccess, saccess, attrib_only, and stream are
2918 * initialized.
2919 */
2920 write_lock(&fp->f_ci->m_lock);
2921 list_add(&fp->node, &fp->f_ci->m_fp_list);
2922 write_unlock(&fp->f_ci->m_lock);
2923
2924 rc = ksmbd_vfs_getattr(&path, &stat);
2925 if (rc) {
Hyunchul Lee465d7202021-07-03 12:10:36 +09002926 generic_fillattr(user_ns, d_inode(path.dentry), &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09002927 rc = 0;
2928 }
2929
2930 /* Check delete pending among previous fp before oplock break */
2931 if (ksmbd_inode_pending_delete(fp)) {
2932 rc = -EBUSY;
2933 goto err_out;
2934 }
2935
2936 share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp);
Namjae Jeon64b39f42021-03-30 14:25:35 +09002937 if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) ||
2938 (req_op_level == SMB2_OPLOCK_LEVEL_LEASE &&
2939 !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) {
Namjae Jeonab0b2632021-06-29 09:20:13 +09002940 if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002941 rc = share_ret;
2942 goto err_out;
2943 }
2944 } else {
2945 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) {
2946 req_op_level = smb2_map_lease_to_oplock(lc->req_state);
2947 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002948 "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n",
2949 name, req_op_level, lc->req_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09002950 rc = find_same_lease_key(sess, fp->f_ci, lc);
2951 if (rc)
2952 goto err_out;
2953 } else if (open_flags == O_RDONLY &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002954 (req_op_level == SMB2_OPLOCK_LEVEL_BATCH ||
2955 req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
Namjae Jeone2f34482021-03-16 10:49:09 +09002956 req_op_level = SMB2_OPLOCK_LEVEL_II;
2957
2958 rc = smb_grant_oplock(work, req_op_level,
2959 fp->persistent_id, fp,
2960 le32_to_cpu(req->hdr.Id.SyncId.TreeId),
2961 lc, share_ret);
2962 if (rc < 0)
2963 goto err_out;
2964 }
2965
2966 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
2967 ksmbd_fd_set_delete_on_close(fp, file_info);
2968
2969 if (need_truncate) {
2970 rc = smb2_create_truncate(&path);
2971 if (rc)
2972 goto err_out;
2973 }
2974
2975 if (req->CreateContextsOffset) {
2976 struct create_alloc_size_req *az_req;
2977
Namjae Jeon070fb212021-05-26 17:57:12 +09002978 az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req,
2979 SMB2_CREATE_ALLOCATION_SIZE);
Namjae Jeonf19b3962021-07-13 09:59:34 +09002980 if (IS_ERR(az_req)) {
2981 rc = PTR_ERR(az_req);
2982 goto err_out;
2983 } else if (az_req) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002984 loff_t alloc_size = le64_to_cpu(az_req->AllocationSize);
2985 int err;
2986
2987 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002988 "request smb2 create allocate size : %llu\n",
2989 alloc_size);
Namjae Jeone8c06192021-06-22 11:06:11 +09002990 smb_break_all_levII_oplock(work, fp, 1);
2991 err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
2992 alloc_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09002993 if (err < 0)
2994 ksmbd_debug(SMB,
Namjae Jeone8c06192021-06-22 11:06:11 +09002995 "vfs_fallocate is failed : %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002996 err);
Namjae Jeone2f34482021-03-16 10:49:09 +09002997 }
2998
Namjae Jeon64b39f42021-03-30 14:25:35 +09002999 context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID);
Namjae Jeonf19b3962021-07-13 09:59:34 +09003000 if (IS_ERR(context)) {
3001 rc = PTR_ERR(context);
3002 goto err_out;
3003 } else if (context) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003004 ksmbd_debug(SMB, "get query on disk id context\n");
3005 query_disk_id = 1;
3006 }
3007 }
3008
3009 if (stat.result_mask & STATX_BTIME)
3010 fp->create_time = ksmbd_UnixTimeToNT(stat.btime);
3011 else
3012 fp->create_time = ksmbd_UnixTimeToNT(stat.ctime);
3013 if (req->FileAttributes || fp->f_ci->m_fattr == 0)
Namjae Jeon070fb212021-05-26 17:57:12 +09003014 fp->f_ci->m_fattr =
3015 cpu_to_le32(smb2_get_dos_mode(&stat, le32_to_cpu(req->FileAttributes)));
Namjae Jeone2f34482021-03-16 10:49:09 +09003016
3017 if (!created)
3018 smb2_update_xattrs(tcon, &path, fp);
3019 else
3020 smb2_new_xattrs(tcon, &path, fp);
3021
3022 memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE);
3023
Hyunchul Lee465d7202021-07-03 12:10:36 +09003024 generic_fillattr(user_ns, file_inode(fp->filp),
Hyunchul Leeaf349832021-06-30 18:25:53 +09003025 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09003026
3027 rsp->StructureSize = cpu_to_le16(89);
3028 rcu_read_lock();
3029 opinfo = rcu_dereference(fp->f_opinfo);
3030 rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0;
3031 rcu_read_unlock();
3032 rsp->Reserved = 0;
3033 rsp->CreateAction = cpu_to_le32(file_info);
3034 rsp->CreationTime = cpu_to_le64(fp->create_time);
3035 time = ksmbd_UnixTimeToNT(stat.atime);
3036 rsp->LastAccessTime = cpu_to_le64(time);
3037 time = ksmbd_UnixTimeToNT(stat.mtime);
3038 rsp->LastWriteTime = cpu_to_le64(time);
3039 time = ksmbd_UnixTimeToNT(stat.ctime);
3040 rsp->ChangeTime = cpu_to_le64(time);
3041 rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 :
3042 cpu_to_le64(stat.blocks << 9);
3043 rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
3044 rsp->FileAttributes = fp->f_ci->m_fattr;
3045
3046 rsp->Reserved2 = 0;
3047
3048 rsp->PersistentFileId = cpu_to_le64(fp->persistent_id);
3049 rsp->VolatileFileId = cpu_to_le64(fp->volatile_id);
3050
3051 rsp->CreateContextsOffset = 0;
3052 rsp->CreateContextsLength = 0;
3053 inc_rfc1001_len(rsp_org, 88); /* StructureSize - 1*/
3054
3055 /* If lease is request send lease context response */
3056 if (opinfo && opinfo->is_lease) {
3057 struct create_context *lease_ccontext;
3058
3059 ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003060 name, opinfo->o_lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +09003061 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
3062
3063 lease_ccontext = (struct create_context *)rsp->Buffer;
3064 contxt_cnt++;
3065 create_lease_buf(rsp->Buffer, opinfo->o_lease);
3066 le32_add_cpu(&rsp->CreateContextsLength,
3067 conn->vals->create_lease_size);
3068 inc_rfc1001_len(rsp_org, conn->vals->create_lease_size);
3069 next_ptr = &lease_ccontext->Next;
3070 next_off = conn->vals->create_lease_size;
3071 }
3072
Namjae Jeone2f34482021-03-16 10:49:09 +09003073 if (maximal_access_ctxt) {
3074 struct create_context *mxac_ccontext;
3075
3076 if (maximal_access == 0)
Hyunchul Lee465d7202021-07-03 12:10:36 +09003077 ksmbd_vfs_query_maximal_access(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09003078 path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09003079 &maximal_access);
3080 mxac_ccontext = (struct create_context *)(rsp->Buffer +
3081 le32_to_cpu(rsp->CreateContextsLength));
3082 contxt_cnt++;
3083 create_mxac_rsp_buf(rsp->Buffer +
3084 le32_to_cpu(rsp->CreateContextsLength),
3085 le32_to_cpu(maximal_access));
3086 le32_add_cpu(&rsp->CreateContextsLength,
3087 conn->vals->create_mxac_size);
3088 inc_rfc1001_len(rsp_org, conn->vals->create_mxac_size);
3089 if (next_ptr)
3090 *next_ptr = cpu_to_le32(next_off);
3091 next_ptr = &mxac_ccontext->Next;
3092 next_off = conn->vals->create_mxac_size;
3093 }
3094
3095 if (query_disk_id) {
3096 struct create_context *disk_id_ccontext;
3097
3098 disk_id_ccontext = (struct create_context *)(rsp->Buffer +
3099 le32_to_cpu(rsp->CreateContextsLength));
3100 contxt_cnt++;
3101 create_disk_id_rsp_buf(rsp->Buffer +
3102 le32_to_cpu(rsp->CreateContextsLength),
3103 stat.ino, tcon->id);
3104 le32_add_cpu(&rsp->CreateContextsLength,
3105 conn->vals->create_disk_id_size);
3106 inc_rfc1001_len(rsp_org, conn->vals->create_disk_id_size);
3107 if (next_ptr)
3108 *next_ptr = cpu_to_le32(next_off);
3109 next_ptr = &disk_id_ccontext->Next;
3110 next_off = conn->vals->create_disk_id_size;
3111 }
3112
3113 if (posix_ctxt) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003114 contxt_cnt++;
3115 create_posix_rsp_buf(rsp->Buffer +
3116 le32_to_cpu(rsp->CreateContextsLength),
3117 fp);
3118 le32_add_cpu(&rsp->CreateContextsLength,
3119 conn->vals->create_posix_size);
3120 inc_rfc1001_len(rsp_org, conn->vals->create_posix_size);
3121 if (next_ptr)
3122 *next_ptr = cpu_to_le32(next_off);
3123 }
3124
3125 if (contxt_cnt > 0) {
3126 rsp->CreateContextsOffset =
3127 cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer)
3128 - 4);
3129 }
3130
3131err_out:
3132 if (file_present || created)
3133 path_put(&path);
3134 ksmbd_revert_fsids(work);
3135err_out1:
3136 if (rc) {
3137 if (rc == -EINVAL)
3138 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3139 else if (rc == -EOPNOTSUPP)
3140 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Hyunchul Lee265fd192021-09-25 00:06:16 +09003141 else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV)
Namjae Jeone2f34482021-03-16 10:49:09 +09003142 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3143 else if (rc == -ENOENT)
3144 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
3145 else if (rc == -EPERM)
3146 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
3147 else if (rc == -EBUSY)
3148 rsp->hdr.Status = STATUS_DELETE_PENDING;
3149 else if (rc == -EBADF)
3150 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
3151 else if (rc == -ENOEXEC)
3152 rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID;
3153 else if (rc == -ENXIO)
3154 rsp->hdr.Status = STATUS_NO_SUCH_DEVICE;
3155 else if (rc == -EEXIST)
3156 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
3157 else if (rc == -EMFILE)
3158 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
3159 if (!rsp->hdr.Status)
3160 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3161
3162 if (!fp || !fp->filename)
3163 kfree(name);
3164 if (fp)
3165 ksmbd_fd_put(work, fp);
3166 smb2_set_err_rsp(work);
3167 ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status);
3168 }
3169
3170 kfree(lc);
3171
3172 return 0;
3173}
3174
3175static int readdir_info_level_struct_sz(int info_level)
3176{
3177 switch (info_level) {
3178 case FILE_FULL_DIRECTORY_INFORMATION:
3179 return sizeof(struct file_full_directory_info);
3180 case FILE_BOTH_DIRECTORY_INFORMATION:
3181 return sizeof(struct file_both_directory_info);
3182 case FILE_DIRECTORY_INFORMATION:
3183 return sizeof(struct file_directory_info);
3184 case FILE_NAMES_INFORMATION:
3185 return sizeof(struct file_names_info);
3186 case FILEID_FULL_DIRECTORY_INFORMATION:
3187 return sizeof(struct file_id_full_dir_info);
3188 case FILEID_BOTH_DIRECTORY_INFORMATION:
3189 return sizeof(struct file_id_both_directory_info);
3190 case SMB_FIND_FILE_POSIX_INFO:
3191 return sizeof(struct smb2_posix_info);
3192 default:
3193 return -EOPNOTSUPP;
3194 }
3195}
3196
3197static int dentry_name(struct ksmbd_dir_info *d_info, int info_level)
3198{
3199 switch (info_level) {
3200 case FILE_FULL_DIRECTORY_INFORMATION:
3201 {
3202 struct file_full_directory_info *ffdinfo;
3203
3204 ffdinfo = (struct file_full_directory_info *)d_info->rptr;
3205 d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset);
3206 d_info->name = ffdinfo->FileName;
3207 d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength);
3208 return 0;
3209 }
3210 case FILE_BOTH_DIRECTORY_INFORMATION:
3211 {
3212 struct file_both_directory_info *fbdinfo;
3213
3214 fbdinfo = (struct file_both_directory_info *)d_info->rptr;
3215 d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset);
3216 d_info->name = fbdinfo->FileName;
3217 d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength);
3218 return 0;
3219 }
3220 case FILE_DIRECTORY_INFORMATION:
3221 {
3222 struct file_directory_info *fdinfo;
3223
3224 fdinfo = (struct file_directory_info *)d_info->rptr;
3225 d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset);
3226 d_info->name = fdinfo->FileName;
3227 d_info->name_len = le32_to_cpu(fdinfo->FileNameLength);
3228 return 0;
3229 }
3230 case FILE_NAMES_INFORMATION:
3231 {
3232 struct file_names_info *fninfo;
3233
3234 fninfo = (struct file_names_info *)d_info->rptr;
3235 d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset);
3236 d_info->name = fninfo->FileName;
3237 d_info->name_len = le32_to_cpu(fninfo->FileNameLength);
3238 return 0;
3239 }
3240 case FILEID_FULL_DIRECTORY_INFORMATION:
3241 {
3242 struct file_id_full_dir_info *dinfo;
3243
3244 dinfo = (struct file_id_full_dir_info *)d_info->rptr;
3245 d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset);
3246 d_info->name = dinfo->FileName;
3247 d_info->name_len = le32_to_cpu(dinfo->FileNameLength);
3248 return 0;
3249 }
3250 case FILEID_BOTH_DIRECTORY_INFORMATION:
3251 {
3252 struct file_id_both_directory_info *fibdinfo;
3253
3254 fibdinfo = (struct file_id_both_directory_info *)d_info->rptr;
3255 d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset);
3256 d_info->name = fibdinfo->FileName;
3257 d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength);
3258 return 0;
3259 }
3260 case SMB_FIND_FILE_POSIX_INFO:
3261 {
3262 struct smb2_posix_info *posix_info;
3263
3264 posix_info = (struct smb2_posix_info *)d_info->rptr;
3265 d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset);
3266 d_info->name = posix_info->name;
3267 d_info->name_len = le32_to_cpu(posix_info->name_len);
3268 return 0;
3269 }
3270 default:
3271 return -EINVAL;
3272 }
3273}
3274
3275/**
3276 * smb2_populate_readdir_entry() - encode directory entry in smb2 response
3277 * buffer
3278 * @conn: connection instance
3279 * @info_level: smb information level
3280 * @d_info: structure included variables for query dir
Hyunchul Leeaf349832021-06-30 18:25:53 +09003281 * @user_ns: user namespace
Namjae Jeone2f34482021-03-16 10:49:09 +09003282 * @ksmbd_kstat: ksmbd wrapper of dirent stat information
3283 *
3284 * if directory has many entries, find first can't read it fully.
3285 * find next might be called multiple times to read remaining dir entries
3286 *
3287 * Return: 0 on success, otherwise error
3288 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003289static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level,
Namjae Jeon070fb212021-05-26 17:57:12 +09003290 struct ksmbd_dir_info *d_info,
3291 struct ksmbd_kstat *ksmbd_kstat)
Namjae Jeone2f34482021-03-16 10:49:09 +09003292{
3293 int next_entry_offset = 0;
3294 char *conv_name;
3295 int conv_len;
3296 void *kstat;
Namjae Jeondac0ec62021-07-07 14:57:24 +09003297 int struct_sz, rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09003298
3299 conv_name = ksmbd_convert_dir_info_name(d_info,
3300 conn->local_nls,
3301 &conv_len);
3302 if (!conv_name)
3303 return -ENOMEM;
3304
3305 /* Somehow the name has only terminating NULL bytes */
3306 if (conv_len < 0) {
Namjae Jeondac0ec62021-07-07 14:57:24 +09003307 rc = -EINVAL;
3308 goto free_conv_name;
Namjae Jeone2f34482021-03-16 10:49:09 +09003309 }
3310
3311 struct_sz = readdir_info_level_struct_sz(info_level);
3312 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3313 KSMBD_DIR_INFO_ALIGNMENT);
3314
3315 if (next_entry_offset > d_info->out_buf_len) {
3316 d_info->out_buf_len = 0;
Namjae Jeondac0ec62021-07-07 14:57:24 +09003317 rc = -ENOSPC;
3318 goto free_conv_name;
Namjae Jeone2f34482021-03-16 10:49:09 +09003319 }
3320
3321 kstat = d_info->wptr;
3322 if (info_level != FILE_NAMES_INFORMATION)
3323 kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat);
3324
3325 switch (info_level) {
3326 case FILE_FULL_DIRECTORY_INFORMATION:
3327 {
3328 struct file_full_directory_info *ffdinfo;
3329
3330 ffdinfo = (struct file_full_directory_info *)kstat;
3331 ffdinfo->FileNameLength = cpu_to_le32(conv_len);
3332 ffdinfo->EaSize =
3333 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3334 if (ffdinfo->EaSize)
3335 ffdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3336 if (d_info->hide_dot_file && d_info->name[0] == '.')
3337 ffdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3338 memcpy(ffdinfo->FileName, conv_name, conv_len);
3339 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3340 break;
3341 }
3342 case FILE_BOTH_DIRECTORY_INFORMATION:
3343 {
3344 struct file_both_directory_info *fbdinfo;
3345
3346 fbdinfo = (struct file_both_directory_info *)kstat;
3347 fbdinfo->FileNameLength = cpu_to_le32(conv_len);
3348 fbdinfo->EaSize =
3349 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3350 if (fbdinfo->EaSize)
3351 fbdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3352 fbdinfo->ShortNameLength = 0;
3353 fbdinfo->Reserved = 0;
3354 if (d_info->hide_dot_file && d_info->name[0] == '.')
3355 fbdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3356 memcpy(fbdinfo->FileName, conv_name, conv_len);
3357 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3358 break;
3359 }
3360 case FILE_DIRECTORY_INFORMATION:
3361 {
3362 struct file_directory_info *fdinfo;
3363
3364 fdinfo = (struct file_directory_info *)kstat;
3365 fdinfo->FileNameLength = cpu_to_le32(conv_len);
3366 if (d_info->hide_dot_file && d_info->name[0] == '.')
3367 fdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3368 memcpy(fdinfo->FileName, conv_name, conv_len);
3369 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3370 break;
3371 }
3372 case FILE_NAMES_INFORMATION:
3373 {
3374 struct file_names_info *fninfo;
3375
3376 fninfo = (struct file_names_info *)kstat;
3377 fninfo->FileNameLength = cpu_to_le32(conv_len);
3378 memcpy(fninfo->FileName, conv_name, conv_len);
3379 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3380 break;
3381 }
3382 case FILEID_FULL_DIRECTORY_INFORMATION:
3383 {
3384 struct file_id_full_dir_info *dinfo;
3385
3386 dinfo = (struct file_id_full_dir_info *)kstat;
3387 dinfo->FileNameLength = cpu_to_le32(conv_len);
3388 dinfo->EaSize =
3389 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3390 if (dinfo->EaSize)
3391 dinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3392 dinfo->Reserved = 0;
3393 dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3394 if (d_info->hide_dot_file && d_info->name[0] == '.')
3395 dinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3396 memcpy(dinfo->FileName, conv_name, conv_len);
3397 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3398 break;
3399 }
3400 case FILEID_BOTH_DIRECTORY_INFORMATION:
3401 {
3402 struct file_id_both_directory_info *fibdinfo;
3403
3404 fibdinfo = (struct file_id_both_directory_info *)kstat;
3405 fibdinfo->FileNameLength = cpu_to_le32(conv_len);
3406 fibdinfo->EaSize =
3407 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3408 if (fibdinfo->EaSize)
3409 fibdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3410 fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3411 fibdinfo->ShortNameLength = 0;
3412 fibdinfo->Reserved = 0;
3413 fibdinfo->Reserved2 = cpu_to_le16(0);
3414 if (d_info->hide_dot_file && d_info->name[0] == '.')
3415 fibdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3416 memcpy(fibdinfo->FileName, conv_name, conv_len);
3417 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3418 break;
3419 }
3420 case SMB_FIND_FILE_POSIX_INFO:
3421 {
3422 struct smb2_posix_info *posix_info;
3423 u64 time;
3424
3425 posix_info = (struct smb2_posix_info *)kstat;
3426 posix_info->Ignored = 0;
3427 posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
3428 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
3429 posix_info->ChangeTime = cpu_to_le64(time);
3430 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime);
3431 posix_info->LastAccessTime = cpu_to_le64(time);
3432 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime);
3433 posix_info->LastWriteTime = cpu_to_le64(time);
3434 posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size);
3435 posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9);
3436 posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev);
3437 posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink);
3438 posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode);
3439 posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino);
3440 posix_info->DosAttributes =
3441 S_ISDIR(ksmbd_kstat->kstat->mode) ? ATTR_DIRECTORY_LE : ATTR_ARCHIVE_LE;
3442 if (d_info->hide_dot_file && d_info->name[0] == '.')
3443 posix_info->DosAttributes |= ATTR_HIDDEN_LE;
Christian Brauner475d6f92021-08-23 17:13:48 +02003444 id_to_sid(from_kuid_munged(&init_user_ns, ksmbd_kstat->kstat->uid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003445 SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]);
Christian Brauner475d6f92021-08-23 17:13:48 +02003446 id_to_sid(from_kgid_munged(&init_user_ns, ksmbd_kstat->kstat->gid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003447 SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]);
Namjae Jeone2f34482021-03-16 10:49:09 +09003448 memcpy(posix_info->name, conv_name, conv_len);
3449 posix_info->name_len = cpu_to_le32(conv_len);
3450 posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset);
3451 break;
3452 }
3453
3454 } /* switch (info_level) */
3455
3456 d_info->last_entry_offset = d_info->data_count;
3457 d_info->data_count += next_entry_offset;
Marios Makassikise7735c82021-05-06 11:40:02 +09003458 d_info->out_buf_len -= next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003459 d_info->wptr += next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003460
3461 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003462 "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n",
3463 info_level, d_info->out_buf_len,
3464 next_entry_offset, d_info->data_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09003465
Namjae Jeondac0ec62021-07-07 14:57:24 +09003466free_conv_name:
3467 kfree(conv_name);
3468 return rc;
Namjae Jeone2f34482021-03-16 10:49:09 +09003469}
3470
3471struct smb2_query_dir_private {
3472 struct ksmbd_work *work;
3473 char *search_pattern;
3474 struct ksmbd_file *dir_fp;
3475
3476 struct ksmbd_dir_info *d_info;
3477 int info_level;
3478};
3479
3480static void lock_dir(struct ksmbd_file *dir_fp)
3481{
3482 struct dentry *dir = dir_fp->filp->f_path.dentry;
3483
3484 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
3485}
3486
3487static void unlock_dir(struct ksmbd_file *dir_fp)
3488{
3489 struct dentry *dir = dir_fp->filp->f_path.dentry;
3490
3491 inode_unlock(d_inode(dir));
3492}
3493
3494static int process_query_dir_entries(struct smb2_query_dir_private *priv)
3495{
Hyunchul Lee465d7202021-07-03 12:10:36 +09003496 struct user_namespace *user_ns = file_mnt_user_ns(priv->dir_fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003497 struct kstat kstat;
3498 struct ksmbd_kstat ksmbd_kstat;
3499 int rc;
3500 int i;
3501
3502 for (i = 0; i < priv->d_info->num_entry; i++) {
3503 struct dentry *dent;
3504
3505 if (dentry_name(priv->d_info, priv->info_level))
3506 return -EINVAL;
3507
3508 lock_dir(priv->dir_fp);
Christian Braunerda1e7ad2021-08-23 17:13:47 +02003509 dent = lookup_one(user_ns, priv->d_info->name,
3510 priv->dir_fp->filp->f_path.dentry,
3511 priv->d_info->name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09003512 unlock_dir(priv->dir_fp);
3513
3514 if (IS_ERR(dent)) {
3515 ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003516 priv->d_info->name,
3517 PTR_ERR(dent));
Namjae Jeone2f34482021-03-16 10:49:09 +09003518 continue;
3519 }
3520 if (unlikely(d_is_negative(dent))) {
3521 dput(dent);
3522 ksmbd_debug(SMB, "Negative dentry `%s'\n",
3523 priv->d_info->name);
3524 continue;
3525 }
3526
3527 ksmbd_kstat.kstat = &kstat;
3528 if (priv->info_level != FILE_NAMES_INFORMATION)
3529 ksmbd_vfs_fill_dentry_attrs(priv->work,
Hyunchul Lee465d7202021-07-03 12:10:36 +09003530 user_ns,
Namjae Jeone2f34482021-03-16 10:49:09 +09003531 dent,
3532 &ksmbd_kstat);
3533
3534 rc = smb2_populate_readdir_entry(priv->work->conn,
3535 priv->info_level,
3536 priv->d_info,
3537 &ksmbd_kstat);
3538 dput(dent);
3539 if (rc)
3540 return rc;
3541 }
3542 return 0;
3543}
3544
3545static int reserve_populate_dentry(struct ksmbd_dir_info *d_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09003546 int info_level)
Namjae Jeone2f34482021-03-16 10:49:09 +09003547{
3548 int struct_sz;
3549 int conv_len;
3550 int next_entry_offset;
3551
3552 struct_sz = readdir_info_level_struct_sz(info_level);
3553 if (struct_sz == -EOPNOTSUPP)
3554 return -EOPNOTSUPP;
3555
3556 conv_len = (d_info->name_len + 1) * 2;
3557 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3558 KSMBD_DIR_INFO_ALIGNMENT);
3559
3560 if (next_entry_offset > d_info->out_buf_len) {
3561 d_info->out_buf_len = 0;
3562 return -ENOSPC;
3563 }
3564
3565 switch (info_level) {
3566 case FILE_FULL_DIRECTORY_INFORMATION:
3567 {
3568 struct file_full_directory_info *ffdinfo;
3569
3570 ffdinfo = (struct file_full_directory_info *)d_info->wptr;
3571 memcpy(ffdinfo->FileName, d_info->name, d_info->name_len);
3572 ffdinfo->FileName[d_info->name_len] = 0x00;
3573 ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3574 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3575 break;
3576 }
3577 case FILE_BOTH_DIRECTORY_INFORMATION:
3578 {
3579 struct file_both_directory_info *fbdinfo;
3580
3581 fbdinfo = (struct file_both_directory_info *)d_info->wptr;
3582 memcpy(fbdinfo->FileName, d_info->name, d_info->name_len);
3583 fbdinfo->FileName[d_info->name_len] = 0x00;
3584 fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3585 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3586 break;
3587 }
3588 case FILE_DIRECTORY_INFORMATION:
3589 {
3590 struct file_directory_info *fdinfo;
3591
3592 fdinfo = (struct file_directory_info *)d_info->wptr;
3593 memcpy(fdinfo->FileName, d_info->name, d_info->name_len);
3594 fdinfo->FileName[d_info->name_len] = 0x00;
3595 fdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3596 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3597 break;
3598 }
3599 case FILE_NAMES_INFORMATION:
3600 {
3601 struct file_names_info *fninfo;
3602
3603 fninfo = (struct file_names_info *)d_info->wptr;
3604 memcpy(fninfo->FileName, d_info->name, d_info->name_len);
3605 fninfo->FileName[d_info->name_len] = 0x00;
3606 fninfo->FileNameLength = cpu_to_le32(d_info->name_len);
3607 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3608 break;
3609 }
3610 case FILEID_FULL_DIRECTORY_INFORMATION:
3611 {
3612 struct file_id_full_dir_info *dinfo;
3613
3614 dinfo = (struct file_id_full_dir_info *)d_info->wptr;
3615 memcpy(dinfo->FileName, d_info->name, d_info->name_len);
3616 dinfo->FileName[d_info->name_len] = 0x00;
3617 dinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3618 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3619 break;
3620 }
3621 case FILEID_BOTH_DIRECTORY_INFORMATION:
3622 {
3623 struct file_id_both_directory_info *fibdinfo;
3624
3625 fibdinfo = (struct file_id_both_directory_info *)d_info->wptr;
3626 memcpy(fibdinfo->FileName, d_info->name, d_info->name_len);
3627 fibdinfo->FileName[d_info->name_len] = 0x00;
3628 fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3629 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3630 break;
3631 }
3632 case SMB_FIND_FILE_POSIX_INFO:
3633 {
3634 struct smb2_posix_info *posix_info;
3635
3636 posix_info = (struct smb2_posix_info *)d_info->wptr;
3637 memcpy(posix_info->name, d_info->name, d_info->name_len);
3638 posix_info->name[d_info->name_len] = 0x00;
3639 posix_info->name_len = cpu_to_le32(d_info->name_len);
3640 posix_info->NextEntryOffset =
3641 cpu_to_le32(next_entry_offset);
3642 break;
3643 }
3644 } /* switch (info_level) */
3645
3646 d_info->num_entry++;
3647 d_info->out_buf_len -= next_entry_offset;
3648 d_info->wptr += next_entry_offset;
3649 return 0;
3650}
3651
Namjae Jeon64b39f42021-03-30 14:25:35 +09003652static int __query_dir(struct dir_context *ctx, const char *name, int namlen,
Namjae Jeon070fb212021-05-26 17:57:12 +09003653 loff_t offset, u64 ino, unsigned int d_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09003654{
3655 struct ksmbd_readdir_data *buf;
3656 struct smb2_query_dir_private *priv;
3657 struct ksmbd_dir_info *d_info;
3658 int rc;
3659
3660 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
3661 priv = buf->private;
3662 d_info = priv->d_info;
3663
3664 /* dot and dotdot entries are already reserved */
3665 if (!strcmp(".", name) || !strcmp("..", name))
3666 return 0;
3667 if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name))
3668 return 0;
Namjae Jeonb24c9332021-03-21 17:32:19 +09003669 if (!match_pattern(name, namlen, priv->search_pattern))
Namjae Jeone2f34482021-03-16 10:49:09 +09003670 return 0;
3671
3672 d_info->name = name;
3673 d_info->name_len = namlen;
3674 rc = reserve_populate_dentry(d_info, priv->info_level);
3675 if (rc)
3676 return rc;
3677 if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
3678 d_info->out_buf_len = 0;
3679 return 0;
3680 }
3681 return 0;
3682}
3683
3684static void restart_ctx(struct dir_context *ctx)
3685{
3686 ctx->pos = 0;
3687}
3688
3689static int verify_info_level(int info_level)
3690{
3691 switch (info_level) {
3692 case FILE_FULL_DIRECTORY_INFORMATION:
3693 case FILE_BOTH_DIRECTORY_INFORMATION:
3694 case FILE_DIRECTORY_INFORMATION:
3695 case FILE_NAMES_INFORMATION:
3696 case FILEID_FULL_DIRECTORY_INFORMATION:
3697 case FILEID_BOTH_DIRECTORY_INFORMATION:
3698 case SMB_FIND_FILE_POSIX_INFO:
3699 break;
3700 default:
3701 return -EOPNOTSUPP;
3702 }
3703
3704 return 0;
3705}
3706
3707int smb2_query_dir(struct ksmbd_work *work)
3708{
3709 struct ksmbd_conn *conn = work->conn;
3710 struct smb2_query_directory_req *req;
3711 struct smb2_query_directory_rsp *rsp, *rsp_org;
3712 struct ksmbd_share_config *share = work->tcon->share_conf;
3713 struct ksmbd_file *dir_fp = NULL;
3714 struct ksmbd_dir_info d_info;
3715 int rc = 0;
3716 char *srch_ptr = NULL;
3717 unsigned char srch_flag;
3718 int buffer_sz;
3719 struct smb2_query_dir_private query_dir_private = {NULL, };
3720
Namjae Jeone5066492021-03-30 12:35:23 +09003721 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09003722 WORK_BUFFERS(work, req, rsp);
3723
3724 if (ksmbd_override_fsids(work)) {
3725 rsp->hdr.Status = STATUS_NO_MEMORY;
3726 smb2_set_err_rsp(work);
3727 return -ENOMEM;
3728 }
3729
3730 rc = verify_info_level(req->FileInformationClass);
3731 if (rc) {
3732 rc = -EFAULT;
3733 goto err_out2;
3734 }
3735
3736 dir_fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09003737 le64_to_cpu(req->VolatileFileId),
3738 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09003739 if (!dir_fp) {
3740 rc = -EBADF;
3741 goto err_out2;
3742 }
3743
3744 if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) ||
Hyunchul Leeaf349832021-06-30 18:25:53 +09003745 inode_permission(file_mnt_user_ns(dir_fp->filp),
3746 file_inode(dir_fp->filp),
Namjae Jeon070fb212021-05-26 17:57:12 +09003747 MAY_READ | MAY_EXEC)) {
Namjae Jeon493fa2f2021-06-29 09:22:16 +09003748 pr_err("no right to enumerate directory (%pd)\n",
3749 dir_fp->filp->f_path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003750 rc = -EACCES;
3751 goto err_out2;
3752 }
3753
3754 if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09003755 pr_err("can't do query dir for a file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09003756 rc = -EINVAL;
3757 goto err_out2;
3758 }
3759
3760 srch_flag = req->Flags;
3761 srch_ptr = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09003762 le16_to_cpu(req->FileNameLength), 1,
3763 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09003764 if (IS_ERR(srch_ptr)) {
3765 ksmbd_debug(SMB, "Search Pattern not found\n");
3766 rc = -EINVAL;
3767 goto err_out2;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003768 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003769 ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr);
Namjae Jeon64b39f42021-03-30 14:25:35 +09003770 }
Namjae Jeone2f34482021-03-16 10:49:09 +09003771
3772 ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename);
3773
3774 if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) {
3775 ksmbd_debug(SMB, "Restart directory scan\n");
3776 generic_file_llseek(dir_fp->filp, 0, SEEK_SET);
3777 restart_ctx(&dir_fp->readdir_data.ctx);
3778 }
3779
3780 memset(&d_info, 0, sizeof(struct ksmbd_dir_info));
3781 d_info.wptr = (char *)rsp->Buffer;
3782 d_info.rptr = (char *)rsp->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003783 d_info.out_buf_len = (work->response_sz - (get_rfc1002_len(rsp_org) + 4));
Namjae Jeon070fb212021-05-26 17:57:12 +09003784 d_info.out_buf_len = min_t(int, d_info.out_buf_len, le32_to_cpu(req->OutputBufferLength)) -
3785 sizeof(struct smb2_query_directory_rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003786 d_info.flags = srch_flag;
3787
3788 /*
3789 * reserve dot and dotdot entries in head of buffer
3790 * in first response
3791 */
3792 rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass,
Namjae Jeon070fb212021-05-26 17:57:12 +09003793 dir_fp, &d_info, srch_ptr,
3794 smb2_populate_readdir_entry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003795 if (rc == -ENOSPC)
3796 rc = 0;
3797 else if (rc)
3798 goto err_out;
3799
3800 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES))
3801 d_info.hide_dot_file = true;
3802
3803 buffer_sz = d_info.out_buf_len;
3804 d_info.rptr = d_info.wptr;
3805 query_dir_private.work = work;
3806 query_dir_private.search_pattern = srch_ptr;
3807 query_dir_private.dir_fp = dir_fp;
3808 query_dir_private.d_info = &d_info;
3809 query_dir_private.info_level = req->FileInformationClass;
3810 dir_fp->readdir_data.private = &query_dir_private;
3811 set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir);
3812
Namjae Jeone8c06192021-06-22 11:06:11 +09003813 rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx);
Namjae Jeone2f34482021-03-16 10:49:09 +09003814 if (rc == 0)
3815 restart_ctx(&dir_fp->readdir_data.ctx);
3816 if (rc == -ENOSPC)
3817 rc = 0;
3818 if (rc)
3819 goto err_out;
3820
3821 d_info.wptr = d_info.rptr;
3822 d_info.out_buf_len = buffer_sz;
3823 rc = process_query_dir_entries(&query_dir_private);
3824 if (rc)
3825 goto err_out;
3826
3827 if (!d_info.data_count && d_info.out_buf_len >= 0) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09003828 if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003829 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003830 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003831 dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0;
3832 rsp->hdr.Status = STATUS_NO_MORE_FILES;
3833 }
3834 rsp->StructureSize = cpu_to_le16(9);
3835 rsp->OutputBufferOffset = cpu_to_le16(0);
3836 rsp->OutputBufferLength = cpu_to_le32(0);
3837 rsp->Buffer[0] = 0;
3838 inc_rfc1001_len(rsp_org, 9);
3839 } else {
3840 ((struct file_directory_info *)
3841 ((char *)rsp->Buffer + d_info.last_entry_offset))
3842 ->NextEntryOffset = 0;
3843
3844 rsp->StructureSize = cpu_to_le16(9);
3845 rsp->OutputBufferOffset = cpu_to_le16(72);
3846 rsp->OutputBufferLength = cpu_to_le32(d_info.data_count);
3847 inc_rfc1001_len(rsp_org, 8 + d_info.data_count);
3848 }
3849
3850 kfree(srch_ptr);
3851 ksmbd_fd_put(work, dir_fp);
3852 ksmbd_revert_fsids(work);
3853 return 0;
3854
3855err_out:
Namjae Jeonbde16942021-06-28 15:23:19 +09003856 pr_err("error while processing smb2 query dir rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09003857 kfree(srch_ptr);
3858
3859err_out2:
3860 if (rc == -EINVAL)
3861 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3862 else if (rc == -EACCES)
3863 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3864 else if (rc == -ENOENT)
3865 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
3866 else if (rc == -EBADF)
3867 rsp->hdr.Status = STATUS_FILE_CLOSED;
3868 else if (rc == -ENOMEM)
3869 rsp->hdr.Status = STATUS_NO_MEMORY;
3870 else if (rc == -EFAULT)
3871 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
3872 if (!rsp->hdr.Status)
3873 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3874
3875 smb2_set_err_rsp(work);
3876 ksmbd_fd_put(work, dir_fp);
3877 ksmbd_revert_fsids(work);
3878 return 0;
3879}
3880
3881/**
3882 * buffer_check_err() - helper function to check buffer errors
3883 * @reqOutputBufferLength: max buffer length expected in command response
3884 * @rsp: query info response buffer contains output buffer length
3885 * @infoclass_size: query info class response buffer size
3886 *
3887 * Return: 0 on success, otherwise error
3888 */
3889static int buffer_check_err(int reqOutputBufferLength,
Namjae Jeon070fb212021-05-26 17:57:12 +09003890 struct smb2_query_info_rsp *rsp, int infoclass_size)
Namjae Jeone2f34482021-03-16 10:49:09 +09003891{
3892 if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) {
3893 if (reqOutputBufferLength < infoclass_size) {
Namjae Jeonbde16942021-06-28 15:23:19 +09003894 pr_err("Invalid Buffer Size Requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09003895 rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003896 rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09003897 return -EINVAL;
3898 }
3899
3900 ksmbd_debug(SMB, "Buffer Overflow\n");
3901 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003902 rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4 +
3903 reqOutputBufferLength);
3904 rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09003905 }
3906 return 0;
3907}
3908
3909static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp)
3910{
3911 struct smb2_file_standard_info *sinfo;
3912
3913 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
3914
3915 sinfo->AllocationSize = cpu_to_le64(4096);
3916 sinfo->EndOfFile = cpu_to_le64(0);
3917 sinfo->NumberOfLinks = cpu_to_le32(1);
3918 sinfo->DeletePending = 1;
3919 sinfo->Directory = 0;
3920 rsp->OutputBufferLength =
3921 cpu_to_le32(sizeof(struct smb2_file_standard_info));
3922 inc_rfc1001_len(rsp, sizeof(struct smb2_file_standard_info));
3923}
3924
Namjae Jeon070fb212021-05-26 17:57:12 +09003925static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num)
Namjae Jeone2f34482021-03-16 10:49:09 +09003926{
3927 struct smb2_file_internal_info *file_info;
3928
3929 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
3930
3931 /* any unique number */
3932 file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63));
3933 rsp->OutputBufferLength =
3934 cpu_to_le32(sizeof(struct smb2_file_internal_info));
3935 inc_rfc1001_len(rsp, sizeof(struct smb2_file_internal_info));
3936}
3937
Namjae Jeone2f34482021-03-16 10:49:09 +09003938static int smb2_get_info_file_pipe(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09003939 struct smb2_query_info_req *req,
3940 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09003941{
Namjae Jeon64b39f42021-03-30 14:25:35 +09003942 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09003943 int rc;
3944
3945 /*
3946 * Windows can sometime send query file info request on
3947 * pipe without opening it, checking error condition here
3948 */
3949 id = le64_to_cpu(req->VolatileFileId);
3950 if (!ksmbd_session_rpc_method(sess, id))
3951 return -ENOENT;
3952
3953 ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003954 req->FileInfoClass, le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09003955
3956 switch (req->FileInfoClass) {
3957 case FILE_STANDARD_INFORMATION:
3958 get_standard_info_pipe(rsp);
3959 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09003960 rsp, FILE_STANDARD_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09003961 break;
3962 case FILE_INTERNAL_INFORMATION:
3963 get_internal_info_pipe(rsp, id);
3964 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09003965 rsp, FILE_INTERNAL_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09003966 break;
3967 default:
3968 ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003969 req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09003970 rc = -EOPNOTSUPP;
3971 }
3972 return rc;
3973}
3974
3975/**
3976 * smb2_get_ea() - handler for smb2 get extended attribute command
3977 * @work: smb work containing query info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09003978 * @fp: ksmbd_file pointer
3979 * @req: get extended attribute request
3980 * @rsp: response buffer pointer
3981 * @rsp_org: base response buffer pointer in case of chained response
Namjae Jeone2f34482021-03-16 10:49:09 +09003982 *
3983 * Return: 0 on success, otherwise error
3984 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003985static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09003986 struct smb2_query_info_req *req,
3987 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09003988{
3989 struct smb2_ea_info *eainfo, *prev_eainfo;
3990 char *name, *ptr, *xattr_list = NULL, *buf;
3991 int rc, name_len, value_len, xattr_list_len, idx;
3992 ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0;
3993 struct smb2_ea_info_req *ea_req = NULL;
3994 struct path *path;
Hyunchul Lee465d7202021-07-03 12:10:36 +09003995 struct user_namespace *user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003996
3997 if (!(fp->daccess & FILE_READ_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09003998 pr_err("Not permitted to read ext attr : 0x%x\n",
3999 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004000 return -EACCES;
4001 }
4002
4003 path = &fp->filp->f_path;
4004 /* single EA entry is requested with given user.* name */
Namjae Jeon64b39f42021-03-30 14:25:35 +09004005 if (req->InputBufferLength) {
Namjae Jeon6d562622021-09-18 18:45:12 +09004006 if (le32_to_cpu(req->InputBufferLength) <
4007 sizeof(struct smb2_ea_info_req))
4008 return -EINVAL;
4009
Namjae Jeone2f34482021-03-16 10:49:09 +09004010 ea_req = (struct smb2_ea_info_req *)req->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004011 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09004012 /* need to send all EAs, if no specific EA is requested*/
4013 if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY)
4014 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09004015 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n",
4016 le32_to_cpu(req->Flags));
Namjae Jeone2f34482021-03-16 10:49:09 +09004017 }
4018
4019 buf_free_len = work->response_sz -
4020 (get_rfc1002_len(rsp_org) + 4) -
4021 sizeof(struct smb2_query_info_rsp);
4022
4023 if (le32_to_cpu(req->OutputBufferLength) < buf_free_len)
4024 buf_free_len = le32_to_cpu(req->OutputBufferLength);
4025
4026 rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4027 if (rc < 0) {
4028 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4029 goto out;
4030 } else if (!rc) { /* there is no EA in the file */
4031 ksmbd_debug(SMB, "no ea data in the file\n");
4032 goto done;
4033 }
4034 xattr_list_len = rc;
4035
4036 ptr = (char *)rsp->Buffer;
4037 eainfo = (struct smb2_ea_info *)ptr;
4038 prev_eainfo = eainfo;
4039 idx = 0;
4040
4041 while (idx < xattr_list_len) {
4042 name = xattr_list + idx;
4043 name_len = strlen(name);
4044
4045 ksmbd_debug(SMB, "%s, len %d\n", name, name_len);
4046 idx += name_len + 1;
4047
4048 /*
4049 * CIFS does not support EA other than user.* namespace,
4050 * still keep the framework generic, to list other attrs
4051 * in future.
4052 */
4053 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4054 continue;
4055
4056 if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004057 STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004058 continue;
4059
4060 if (req->InputBufferLength &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09004061 strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name,
4062 ea_req->EaNameLength))
Namjae Jeone2f34482021-03-16 10:49:09 +09004063 continue;
4064
4065 if (!strncmp(&name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004066 DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004067 continue;
4068
4069 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4070 name_len -= XATTR_USER_PREFIX_LEN;
4071
4072 ptr = (char *)(&eainfo->name + name_len + 1);
4073 buf_free_len -= (offsetof(struct smb2_ea_info, name) +
4074 name_len + 1);
4075 /* bailout if xattr can't fit in buf_free_len */
Hyunchul Lee465d7202021-07-03 12:10:36 +09004076 value_len = ksmbd_vfs_getxattr(user_ns, path->dentry,
4077 name, &buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004078 if (value_len <= 0) {
4079 rc = -ENOENT;
4080 rsp->hdr.Status = STATUS_INVALID_HANDLE;
4081 goto out;
4082 }
4083
4084 buf_free_len -= value_len;
4085 if (buf_free_len < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09004086 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004087 break;
4088 }
4089
4090 memcpy(ptr, buf, value_len);
Namjae Jeon79f6b112021-04-02 12:47:14 +09004091 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09004092
4093 ptr += value_len;
4094 eainfo->Flags = 0;
4095 eainfo->EaNameLength = name_len;
4096
Namjae Jeon64b39f42021-03-30 14:25:35 +09004097 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004098 memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN],
Namjae Jeon070fb212021-05-26 17:57:12 +09004099 name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09004100 else
4101 memcpy(eainfo->name, name, name_len);
4102
4103 eainfo->name[name_len] = '\0';
4104 eainfo->EaValueLength = cpu_to_le16(value_len);
4105 next_offset = offsetof(struct smb2_ea_info, name) +
4106 name_len + 1 + value_len;
4107
4108 /* align next xattr entry at 4 byte bundary */
4109 alignment_bytes = ((next_offset + 3) & ~3) - next_offset;
4110 if (alignment_bytes) {
4111 memset(ptr, '\0', alignment_bytes);
4112 ptr += alignment_bytes;
4113 next_offset += alignment_bytes;
4114 buf_free_len -= alignment_bytes;
4115 }
4116 eainfo->NextEntryOffset = cpu_to_le32(next_offset);
4117 prev_eainfo = eainfo;
4118 eainfo = (struct smb2_ea_info *)ptr;
4119 rsp_data_cnt += next_offset;
4120
4121 if (req->InputBufferLength) {
4122 ksmbd_debug(SMB, "single entry requested\n");
4123 break;
4124 }
4125 }
4126
4127 /* no more ea entries */
4128 prev_eainfo->NextEntryOffset = 0;
4129done:
4130 rc = 0;
4131 if (rsp_data_cnt == 0)
4132 rsp->hdr.Status = STATUS_NO_EAS_ON_FILE;
4133 rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt);
4134 inc_rfc1001_len(rsp_org, rsp_data_cnt);
4135out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004136 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004137 return rc;
4138}
4139
4140static void get_file_access_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004141 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004142{
4143 struct smb2_file_access_info *file_info;
4144
4145 file_info = (struct smb2_file_access_info *)rsp->Buffer;
4146 file_info->AccessFlags = fp->daccess;
4147 rsp->OutputBufferLength =
4148 cpu_to_le32(sizeof(struct smb2_file_access_info));
4149 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info));
4150}
4151
4152static int get_file_basic_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004153 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004154{
4155 struct smb2_file_all_info *basic_info;
4156 struct kstat stat;
4157 u64 time;
4158
4159 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004160 pr_err("no right to read the attributes : 0x%x\n",
4161 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004162 return -EACCES;
4163 }
4164
4165 basic_info = (struct smb2_file_all_info *)rsp->Buffer;
Hyunchul Leeaf349832021-06-30 18:25:53 +09004166 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4167 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004168 basic_info->CreationTime = cpu_to_le64(fp->create_time);
4169 time = ksmbd_UnixTimeToNT(stat.atime);
4170 basic_info->LastAccessTime = cpu_to_le64(time);
4171 time = ksmbd_UnixTimeToNT(stat.mtime);
4172 basic_info->LastWriteTime = cpu_to_le64(time);
4173 time = ksmbd_UnixTimeToNT(stat.ctime);
4174 basic_info->ChangeTime = cpu_to_le64(time);
4175 basic_info->Attributes = fp->f_ci->m_fattr;
4176 basic_info->Pad1 = 0;
4177 rsp->OutputBufferLength =
Namjae Jeon64b39f42021-03-30 14:25:35 +09004178 cpu_to_le32(offsetof(struct smb2_file_all_info, AllocationSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09004179 inc_rfc1001_len(rsp_org, offsetof(struct smb2_file_all_info,
4180 AllocationSize));
4181 return 0;
4182}
4183
4184static unsigned long long get_allocation_size(struct inode *inode,
Namjae Jeon070fb212021-05-26 17:57:12 +09004185 struct kstat *stat)
Namjae Jeone2f34482021-03-16 10:49:09 +09004186{
4187 unsigned long long alloc_size = 0;
4188
4189 if (!S_ISDIR(stat->mode)) {
4190 if ((inode->i_blocks << 9) <= stat->size)
4191 alloc_size = stat->size;
4192 else
4193 alloc_size = inode->i_blocks << 9;
Namjae Jeone2f34482021-03-16 10:49:09 +09004194 }
4195
4196 return alloc_size;
4197}
4198
4199static void get_file_standard_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004200 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004201{
4202 struct smb2_file_standard_info *sinfo;
4203 unsigned int delete_pending;
4204 struct inode *inode;
4205 struct kstat stat;
4206
Namjae Jeonab0b2632021-06-29 09:20:13 +09004207 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004208 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004209
4210 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4211 delete_pending = ksmbd_inode_pending_delete(fp);
4212
4213 sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat));
4214 sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4215 sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending);
4216 sinfo->DeletePending = delete_pending;
4217 sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4218 rsp->OutputBufferLength =
4219 cpu_to_le32(sizeof(struct smb2_file_standard_info));
4220 inc_rfc1001_len(rsp_org,
4221 sizeof(struct smb2_file_standard_info));
4222}
4223
4224static void get_file_alignment_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004225 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004226{
4227 struct smb2_file_alignment_info *file_info;
4228
4229 file_info = (struct smb2_file_alignment_info *)rsp->Buffer;
4230 file_info->AlignmentRequirement = 0;
4231 rsp->OutputBufferLength =
4232 cpu_to_le32(sizeof(struct smb2_file_alignment_info));
4233 inc_rfc1001_len(rsp_org,
4234 sizeof(struct smb2_file_alignment_info));
4235}
4236
4237static int get_file_all_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004238 struct smb2_query_info_rsp *rsp,
4239 struct ksmbd_file *fp,
4240 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004241{
4242 struct ksmbd_conn *conn = work->conn;
4243 struct smb2_file_all_info *file_info;
4244 unsigned int delete_pending;
4245 struct inode *inode;
4246 struct kstat stat;
4247 int conv_len;
4248 char *filename;
4249 u64 time;
4250
4251 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4252 ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004253 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004254 return -EACCES;
4255 }
4256
Hyunchul Lee265fd192021-09-25 00:06:16 +09004257 filename = convert_to_nt_pathname(fp->filename);
Namjae Jeone2f34482021-03-16 10:49:09 +09004258 if (!filename)
4259 return -ENOMEM;
4260
Namjae Jeonab0b2632021-06-29 09:20:13 +09004261 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004262 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004263
4264 ksmbd_debug(SMB, "filename = %s\n", filename);
4265 delete_pending = ksmbd_inode_pending_delete(fp);
4266 file_info = (struct smb2_file_all_info *)rsp->Buffer;
4267
4268 file_info->CreationTime = cpu_to_le64(fp->create_time);
4269 time = ksmbd_UnixTimeToNT(stat.atime);
4270 file_info->LastAccessTime = cpu_to_le64(time);
4271 time = ksmbd_UnixTimeToNT(stat.mtime);
4272 file_info->LastWriteTime = cpu_to_le64(time);
4273 time = ksmbd_UnixTimeToNT(stat.ctime);
4274 file_info->ChangeTime = cpu_to_le64(time);
4275 file_info->Attributes = fp->f_ci->m_fattr;
4276 file_info->Pad1 = 0;
4277 file_info->AllocationSize =
4278 cpu_to_le64(get_allocation_size(inode, &stat));
4279 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4280 file_info->NumberOfLinks =
4281 cpu_to_le32(get_nlink(&stat) - delete_pending);
4282 file_info->DeletePending = delete_pending;
4283 file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4284 file_info->Pad2 = 0;
4285 file_info->IndexNumber = cpu_to_le64(stat.ino);
4286 file_info->EASize = 0;
4287 file_info->AccessFlags = fp->daccess;
4288 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4289 file_info->Mode = fp->coption;
4290 file_info->AlignmentRequirement = 0;
Namjae Jeon070fb212021-05-26 17:57:12 +09004291 conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename,
4292 PATH_MAX, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004293 conv_len *= 2;
4294 file_info->FileNameLength = cpu_to_le32(conv_len);
4295 rsp->OutputBufferLength =
4296 cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1);
4297 kfree(filename);
4298 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4299 return 0;
4300}
4301
4302static void get_file_alternate_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004303 struct smb2_query_info_rsp *rsp,
4304 struct ksmbd_file *fp,
4305 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004306{
4307 struct ksmbd_conn *conn = work->conn;
4308 struct smb2_file_alt_name_info *file_info;
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004309 struct dentry *dentry = fp->filp->f_path.dentry;
Namjae Jeone2f34482021-03-16 10:49:09 +09004310 int conv_len;
Namjae Jeone2f34482021-03-16 10:49:09 +09004311
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004312 spin_lock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004313 file_info = (struct smb2_file_alt_name_info *)rsp->Buffer;
4314 conv_len = ksmbd_extract_shortname(conn,
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004315 dentry->d_name.name,
Namjae Jeone2f34482021-03-16 10:49:09 +09004316 file_info->FileName);
Namjae Jeon493fa2f2021-06-29 09:22:16 +09004317 spin_unlock(&dentry->d_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09004318 file_info->FileNameLength = cpu_to_le32(conv_len);
4319 rsp->OutputBufferLength =
4320 cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len);
4321 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4322}
4323
4324static void get_file_stream_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004325 struct smb2_query_info_rsp *rsp,
4326 struct ksmbd_file *fp,
4327 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004328{
4329 struct ksmbd_conn *conn = work->conn;
4330 struct smb2_file_stream_info *file_info;
4331 char *stream_name, *xattr_list = NULL, *stream_buf;
4332 struct kstat stat;
4333 struct path *path = &fp->filp->f_path;
4334 ssize_t xattr_list_len;
4335 int nbytes = 0, streamlen, stream_name_len, next, idx = 0;
4336
Hyunchul Leeaf349832021-06-30 18:25:53 +09004337 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4338 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004339 file_info = (struct smb2_file_stream_info *)rsp->Buffer;
4340
4341 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4342 if (xattr_list_len < 0) {
4343 goto out;
4344 } else if (!xattr_list_len) {
4345 ksmbd_debug(SMB, "empty xattr in the file\n");
4346 goto out;
4347 }
4348
4349 while (idx < xattr_list_len) {
4350 stream_name = xattr_list + idx;
4351 streamlen = strlen(stream_name);
4352 idx += streamlen + 1;
4353
4354 ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen);
4355
4356 if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004357 STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004358 continue;
4359
4360 stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN +
4361 STREAM_PREFIX_LEN);
4362 streamlen = stream_name_len;
4363
4364 /* plus : size */
4365 streamlen += 1;
4366 stream_buf = kmalloc(streamlen + 1, GFP_KERNEL);
4367 if (!stream_buf)
4368 break;
4369
4370 streamlen = snprintf(stream_buf, streamlen + 1,
Namjae Jeon070fb212021-05-26 17:57:12 +09004371 ":%s", &stream_name[XATTR_NAME_STREAM_LEN]);
Namjae Jeone2f34482021-03-16 10:49:09 +09004372
Namjae Jeon070fb212021-05-26 17:57:12 +09004373 file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes];
Namjae Jeone2f34482021-03-16 10:49:09 +09004374 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004375 stream_buf, streamlen,
4376 conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004377 streamlen *= 2;
4378 kfree(stream_buf);
4379 file_info->StreamNameLength = cpu_to_le32(streamlen);
4380 file_info->StreamSize = cpu_to_le64(stream_name_len);
4381 file_info->StreamAllocationSize = cpu_to_le64(stream_name_len);
4382
4383 next = sizeof(struct smb2_file_stream_info) + streamlen;
4384 nbytes += next;
4385 file_info->NextEntryOffset = cpu_to_le32(next);
4386 }
4387
Namjae Jeon9f6323312021-09-18 21:02:39 +09004388 if (!S_ISDIR(stat.mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004389 file_info = (struct smb2_file_stream_info *)
4390 &rsp->Buffer[nbytes];
4391 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004392 "::$DATA", 7, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004393 streamlen *= 2;
4394 file_info->StreamNameLength = cpu_to_le32(streamlen);
Namjae Jeon9f6323312021-09-18 21:02:39 +09004395 file_info->StreamSize = 0;
4396 file_info->StreamAllocationSize = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09004397 nbytes += sizeof(struct smb2_file_stream_info) + streamlen;
4398 }
4399
4400 /* last entry offset should be 0 */
4401 file_info->NextEntryOffset = 0;
4402out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004403 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004404
4405 rsp->OutputBufferLength = cpu_to_le32(nbytes);
4406 inc_rfc1001_len(rsp_org, nbytes);
4407}
4408
4409static void get_file_internal_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004410 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004411{
4412 struct smb2_file_internal_info *file_info;
4413 struct kstat stat;
4414
Hyunchul Leeaf349832021-06-30 18:25:53 +09004415 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4416 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004417 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4418 file_info->IndexNumber = cpu_to_le64(stat.ino);
4419 rsp->OutputBufferLength =
4420 cpu_to_le32(sizeof(struct smb2_file_internal_info));
4421 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
4422}
4423
4424static int get_file_network_open_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004425 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004426{
4427 struct smb2_file_ntwrk_info *file_info;
4428 struct inode *inode;
4429 struct kstat stat;
4430 u64 time;
4431
4432 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004433 pr_err("no right to read the attributes : 0x%x\n",
4434 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004435 return -EACCES;
4436 }
4437
4438 file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer;
4439
Namjae Jeonab0b2632021-06-29 09:20:13 +09004440 inode = file_inode(fp->filp);
Hyunchul Leeaf349832021-06-30 18:25:53 +09004441 generic_fillattr(file_mnt_user_ns(fp->filp), inode, &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004442
4443 file_info->CreationTime = cpu_to_le64(fp->create_time);
4444 time = ksmbd_UnixTimeToNT(stat.atime);
4445 file_info->LastAccessTime = cpu_to_le64(time);
4446 time = ksmbd_UnixTimeToNT(stat.mtime);
4447 file_info->LastWriteTime = cpu_to_le64(time);
4448 time = ksmbd_UnixTimeToNT(stat.ctime);
4449 file_info->ChangeTime = cpu_to_le64(time);
4450 file_info->Attributes = fp->f_ci->m_fattr;
4451 file_info->AllocationSize =
4452 cpu_to_le64(get_allocation_size(inode, &stat));
4453 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4454 file_info->Reserved = cpu_to_le32(0);
4455 rsp->OutputBufferLength =
4456 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info));
4457 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info));
4458 return 0;
4459}
4460
Namjae Jeon64b39f42021-03-30 14:25:35 +09004461static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004462{
4463 struct smb2_file_ea_info *file_info;
4464
4465 file_info = (struct smb2_file_ea_info *)rsp->Buffer;
4466 file_info->EASize = 0;
4467 rsp->OutputBufferLength =
4468 cpu_to_le32(sizeof(struct smb2_file_ea_info));
4469 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info));
4470}
4471
4472static void get_file_position_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004473 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004474{
4475 struct smb2_file_pos_info *file_info;
4476
4477 file_info = (struct smb2_file_pos_info *)rsp->Buffer;
4478 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4479 rsp->OutputBufferLength =
4480 cpu_to_le32(sizeof(struct smb2_file_pos_info));
4481 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info));
4482}
4483
4484static void get_file_mode_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004485 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004486{
4487 struct smb2_file_mode_info *file_info;
4488
4489 file_info = (struct smb2_file_mode_info *)rsp->Buffer;
4490 file_info->Mode = fp->coption & FILE_MODE_INFO_MASK;
4491 rsp->OutputBufferLength =
4492 cpu_to_le32(sizeof(struct smb2_file_mode_info));
4493 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info));
4494}
4495
4496static void get_file_compression_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004497 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004498{
4499 struct smb2_file_comp_info *file_info;
4500 struct kstat stat;
4501
Hyunchul Leeaf349832021-06-30 18:25:53 +09004502 generic_fillattr(file_mnt_user_ns(fp->filp), file_inode(fp->filp),
4503 &stat);
Namjae Jeone2f34482021-03-16 10:49:09 +09004504
4505 file_info = (struct smb2_file_comp_info *)rsp->Buffer;
4506 file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9);
4507 file_info->CompressionFormat = COMPRESSION_FORMAT_NONE;
4508 file_info->CompressionUnitShift = 0;
4509 file_info->ChunkShift = 0;
4510 file_info->ClusterShift = 0;
4511 memset(&file_info->Reserved[0], 0, 3);
4512
4513 rsp->OutputBufferLength =
4514 cpu_to_le32(sizeof(struct smb2_file_comp_info));
4515 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info));
4516}
4517
4518static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004519 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004520{
4521 struct smb2_file_attr_tag_info *file_info;
4522
4523 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004524 pr_err("no right to read the attributes : 0x%x\n",
4525 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004526 return -EACCES;
4527 }
4528
4529 file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer;
4530 file_info->FileAttributes = fp->f_ci->m_fattr;
4531 file_info->ReparseTag = 0;
4532 rsp->OutputBufferLength =
4533 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info));
Namjae Jeon070fb212021-05-26 17:57:12 +09004534 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004535 return 0;
4536}
4537
4538static int find_file_posix_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004539 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004540{
4541 struct smb311_posix_qinfo *file_info;
Namjae Jeonab0b2632021-06-29 09:20:13 +09004542 struct inode *inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09004543 u64 time;
4544
4545 file_info = (struct smb311_posix_qinfo *)rsp->Buffer;
4546 file_info->CreationTime = cpu_to_le64(fp->create_time);
4547 time = ksmbd_UnixTimeToNT(inode->i_atime);
4548 file_info->LastAccessTime = cpu_to_le64(time);
4549 time = ksmbd_UnixTimeToNT(inode->i_mtime);
4550 file_info->LastWriteTime = cpu_to_le64(time);
4551 time = ksmbd_UnixTimeToNT(inode->i_ctime);
4552 file_info->ChangeTime = cpu_to_le64(time);
4553 file_info->DosAttributes = fp->f_ci->m_fattr;
4554 file_info->Inode = cpu_to_le64(inode->i_ino);
4555 file_info->EndOfFile = cpu_to_le64(inode->i_size);
4556 file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9);
4557 file_info->HardLinks = cpu_to_le32(inode->i_nlink);
4558 file_info->Mode = cpu_to_le32(inode->i_mode);
4559 file_info->DeviceId = cpu_to_le32(inode->i_rdev);
4560 rsp->OutputBufferLength =
4561 cpu_to_le32(sizeof(struct smb311_posix_qinfo));
Namjae Jeon64b39f42021-03-30 14:25:35 +09004562 inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo));
Namjae Jeone2f34482021-03-16 10:49:09 +09004563 return 0;
4564}
4565
Namjae Jeone2f34482021-03-16 10:49:09 +09004566static int smb2_get_info_file(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004567 struct smb2_query_info_req *req,
4568 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004569{
4570 struct ksmbd_file *fp;
4571 int fileinfoclass = 0;
4572 int rc = 0;
4573 int file_infoclass_size;
4574 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4575
4576 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004577 KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004578 /* smb2 info file called for pipe */
4579 return smb2_get_info_file_pipe(work->sess, req, rsp);
4580 }
4581
4582 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon38673692021-07-08 12:32:27 +09004583 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
4584 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004585 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004586 id = work->compound_fid;
4587 pid = work->compound_pfid;
4588 }
4589 }
4590
Namjae Jeon38673692021-07-08 12:32:27 +09004591 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004592 id = le64_to_cpu(req->VolatileFileId);
4593 pid = le64_to_cpu(req->PersistentFileId);
4594 }
4595
4596 fp = ksmbd_lookup_fd_slow(work, id, pid);
4597 if (!fp)
4598 return -ENOENT;
4599
4600 fileinfoclass = req->FileInfoClass;
4601
4602 switch (fileinfoclass) {
4603 case FILE_ACCESS_INFORMATION:
4604 get_file_access_info(rsp, fp, rsp_org);
4605 file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE;
4606 break;
4607
4608 case FILE_BASIC_INFORMATION:
4609 rc = get_file_basic_info(rsp, fp, rsp_org);
4610 file_infoclass_size = FILE_BASIC_INFORMATION_SIZE;
4611 break;
4612
4613 case FILE_STANDARD_INFORMATION:
4614 get_file_standard_info(rsp, fp, rsp_org);
4615 file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE;
4616 break;
4617
4618 case FILE_ALIGNMENT_INFORMATION:
4619 get_file_alignment_info(rsp, rsp_org);
4620 file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE;
4621 break;
4622
4623 case FILE_ALL_INFORMATION:
4624 rc = get_file_all_info(work, rsp, fp, rsp_org);
4625 file_infoclass_size = FILE_ALL_INFORMATION_SIZE;
4626 break;
4627
4628 case FILE_ALTERNATE_NAME_INFORMATION:
4629 get_file_alternate_info(work, rsp, fp, rsp_org);
4630 file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE;
4631 break;
4632
4633 case FILE_STREAM_INFORMATION:
4634 get_file_stream_info(work, rsp, fp, rsp_org);
4635 file_infoclass_size = FILE_STREAM_INFORMATION_SIZE;
4636 break;
4637
4638 case FILE_INTERNAL_INFORMATION:
4639 get_file_internal_info(rsp, fp, rsp_org);
4640 file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE;
4641 break;
4642
4643 case FILE_NETWORK_OPEN_INFORMATION:
4644 rc = get_file_network_open_info(rsp, fp, rsp_org);
4645 file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE;
4646 break;
4647
4648 case FILE_EA_INFORMATION:
4649 get_file_ea_info(rsp, rsp_org);
4650 file_infoclass_size = FILE_EA_INFORMATION_SIZE;
4651 break;
4652
4653 case FILE_FULL_EA_INFORMATION:
4654 rc = smb2_get_ea(work, fp, req, rsp, rsp_org);
4655 file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE;
4656 break;
4657
4658 case FILE_POSITION_INFORMATION:
4659 get_file_position_info(rsp, fp, rsp_org);
4660 file_infoclass_size = FILE_POSITION_INFORMATION_SIZE;
4661 break;
4662
4663 case FILE_MODE_INFORMATION:
4664 get_file_mode_info(rsp, fp, rsp_org);
4665 file_infoclass_size = FILE_MODE_INFORMATION_SIZE;
4666 break;
4667
4668 case FILE_COMPRESSION_INFORMATION:
4669 get_file_compression_info(rsp, fp, rsp_org);
4670 file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE;
4671 break;
4672
4673 case FILE_ATTRIBUTE_TAG_INFORMATION:
4674 rc = get_file_attribute_tag_info(rsp, fp, rsp_org);
4675 file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE;
4676 break;
4677 case SMB_FIND_FILE_POSIX_INFO:
4678 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004679 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004680 rc = -EOPNOTSUPP;
4681 } else {
4682 rc = find_file_posix_info(rsp, fp, rsp_org);
4683 file_infoclass_size = sizeof(struct smb311_posix_qinfo);
4684 }
4685 break;
4686 default:
4687 ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n",
4688 fileinfoclass);
4689 rc = -EOPNOTSUPP;
4690 }
4691 if (!rc)
4692 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4693 rsp,
4694 file_infoclass_size);
4695 ksmbd_fd_put(work, fp);
4696 return rc;
4697}
4698
Namjae Jeone2f34482021-03-16 10:49:09 +09004699static int smb2_get_info_filesystem(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004700 struct smb2_query_info_req *req,
4701 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004702{
4703 struct ksmbd_session *sess = work->sess;
4704 struct ksmbd_conn *conn = sess->conn;
4705 struct ksmbd_share_config *share = work->tcon->share_conf;
4706 int fsinfoclass = 0;
4707 struct kstatfs stfs;
4708 struct path path;
4709 int rc = 0, len;
4710 int fs_infoclass_size = 0;
4711
Hyunchul Lee265fd192021-09-25 00:06:16 +09004712 rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path);
Namjae Jeone2f34482021-03-16 10:49:09 +09004713 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004714 pr_err("cannot create vfs path\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004715 return -EIO;
4716 }
4717
4718 rc = vfs_statfs(&path, &stfs);
4719 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004720 pr_err("cannot do stat of path %s\n", share->path);
Namjae Jeone2f34482021-03-16 10:49:09 +09004721 path_put(&path);
4722 return -EIO;
4723 }
4724
4725 fsinfoclass = req->FileInfoClass;
4726
4727 switch (fsinfoclass) {
4728 case FS_DEVICE_INFORMATION:
4729 {
4730 struct filesystem_device_info *info;
4731
4732 info = (struct filesystem_device_info *)rsp->Buffer;
4733
4734 info->DeviceType = cpu_to_le32(stfs.f_type);
4735 info->DeviceCharacteristics = cpu_to_le32(0x00000020);
4736 rsp->OutputBufferLength = cpu_to_le32(8);
4737 inc_rfc1001_len(rsp_org, 8);
4738 fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE;
4739 break;
4740 }
4741 case FS_ATTRIBUTE_INFORMATION:
4742 {
4743 struct filesystem_attribute_info *info;
4744 size_t sz;
4745
4746 info = (struct filesystem_attribute_info *)rsp->Buffer;
4747 info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS |
4748 FILE_PERSISTENT_ACLS |
4749 FILE_UNICODE_ON_DISK |
4750 FILE_CASE_PRESERVED_NAMES |
Namjae Jeoneb817362021-05-18 10:37:59 +09004751 FILE_CASE_SENSITIVE_SEARCH |
4752 FILE_SUPPORTS_BLOCK_REFCOUNTING);
Namjae Jeone2f34482021-03-16 10:49:09 +09004753
4754 info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps);
4755
4756 info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen);
4757 len = smbConvertToUTF16((__le16 *)info->FileSystemName,
4758 "NTFS", PATH_MAX, conn->local_nls, 0);
4759 len = len * 2;
4760 info->FileSystemNameLen = cpu_to_le32(len);
4761 sz = sizeof(struct filesystem_attribute_info) - 2 + len;
4762 rsp->OutputBufferLength = cpu_to_le32(sz);
4763 inc_rfc1001_len(rsp_org, sz);
4764 fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE;
4765 break;
4766 }
4767 case FS_VOLUME_INFORMATION:
4768 {
4769 struct filesystem_vol_info *info;
4770 size_t sz;
4771
4772 info = (struct filesystem_vol_info *)(rsp->Buffer);
4773 info->VolumeCreationTime = 0;
4774 /* Taking dummy value of serial number*/
4775 info->SerialNumber = cpu_to_le32(0xbc3ac512);
4776 len = smbConvertToUTF16((__le16 *)info->VolumeLabel,
4777 share->name, PATH_MAX,
4778 conn->local_nls, 0);
4779 len = len * 2;
4780 info->VolumeLabelSize = cpu_to_le32(len);
4781 info->Reserved = 0;
4782 sz = sizeof(struct filesystem_vol_info) - 2 + len;
4783 rsp->OutputBufferLength = cpu_to_le32(sz);
4784 inc_rfc1001_len(rsp_org, sz);
4785 fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE;
4786 break;
4787 }
4788 case FS_SIZE_INFORMATION:
4789 {
4790 struct filesystem_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004791
4792 info = (struct filesystem_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004793 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4794 info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004795 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4796 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004797 rsp->OutputBufferLength = cpu_to_le32(24);
4798 inc_rfc1001_len(rsp_org, 24);
4799 fs_infoclass_size = FS_SIZE_INFORMATION_SIZE;
4800 break;
4801 }
4802 case FS_FULL_SIZE_INFORMATION:
4803 {
4804 struct smb2_fs_full_size_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004805
4806 info = (struct smb2_fs_full_size_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004807 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4808 info->CallerAvailableAllocationUnits =
4809 cpu_to_le64(stfs.f_bavail);
4810 info->ActualAvailableAllocationUnits =
4811 cpu_to_le64(stfs.f_bfree);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004812 info->SectorsPerAllocationUnit = cpu_to_le32(1);
4813 info->BytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004814 rsp->OutputBufferLength = cpu_to_le32(32);
4815 inc_rfc1001_len(rsp_org, 32);
4816 fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE;
4817 break;
4818 }
4819 case FS_OBJECT_ID_INFORMATION:
4820 {
4821 struct object_id_info *info;
4822
4823 info = (struct object_id_info *)(rsp->Buffer);
4824
4825 if (!user_guest(sess->user))
4826 memcpy(info->objid, user_passkey(sess->user), 16);
4827 else
4828 memset(info->objid, 0, 16);
4829
4830 info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC);
4831 info->extended_info.version = cpu_to_le32(1);
4832 info->extended_info.release = cpu_to_le32(1);
4833 info->extended_info.rel_date = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004834 memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0"));
Namjae Jeone2f34482021-03-16 10:49:09 +09004835 rsp->OutputBufferLength = cpu_to_le32(64);
4836 inc_rfc1001_len(rsp_org, 64);
4837 fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE;
4838 break;
4839 }
4840 case FS_SECTOR_SIZE_INFORMATION:
4841 {
4842 struct smb3_fs_ss_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004843
4844 info = (struct smb3_fs_ss_info *)(rsp->Buffer);
Namjae Jeone2f34482021-03-16 10:49:09 +09004845
Namjae Jeon131bac12021-06-22 16:20:47 +09004846 info->LogicalBytesPerSector = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004847 info->PhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09004848 cpu_to_le32(stfs.f_bsize);
4849 info->PhysicalBytesPerSectorForPerf = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004850 info->FSEffPhysicalBytesPerSectorForAtomicity =
Namjae Jeon131bac12021-06-22 16:20:47 +09004851 cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004852 info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE |
4853 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE);
4854 info->ByteOffsetForSectorAlignment = 0;
4855 info->ByteOffsetForPartitionAlignment = 0;
4856 rsp->OutputBufferLength = cpu_to_le32(28);
4857 inc_rfc1001_len(rsp_org, 28);
4858 fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE;
4859 break;
4860 }
4861 case FS_CONTROL_INFORMATION:
4862 {
4863 /*
4864 * TODO : The current implementation is based on
4865 * test result with win7(NTFS) server. It's need to
4866 * modify this to get valid Quota values
4867 * from Linux kernel
4868 */
4869 struct smb2_fs_control_info *info;
4870
4871 info = (struct smb2_fs_control_info *)(rsp->Buffer);
4872 info->FreeSpaceStartFiltering = 0;
4873 info->FreeSpaceThreshold = 0;
4874 info->FreeSpaceStopFiltering = 0;
4875 info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID);
4876 info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID);
4877 info->Padding = 0;
4878 rsp->OutputBufferLength = cpu_to_le32(48);
4879 inc_rfc1001_len(rsp_org, 48);
4880 fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE;
4881 break;
4882 }
4883 case FS_POSIX_INFORMATION:
4884 {
4885 struct filesystem_posix_info *info;
Namjae Jeone2f34482021-03-16 10:49:09 +09004886
4887 if (!work->tcon->posix_extensions) {
Namjae Jeonbde16942021-06-28 15:23:19 +09004888 pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09004889 rc = -EOPNOTSUPP;
4890 } else {
4891 info = (struct filesystem_posix_info *)(rsp->Buffer);
Namjae Jeonee81cae2021-06-26 22:32:34 +09004892 info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09004893 info->BlockSize = cpu_to_le32(stfs.f_bsize);
4894 info->TotalBlocks = cpu_to_le64(stfs.f_blocks);
4895 info->BlocksAvail = cpu_to_le64(stfs.f_bfree);
4896 info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail);
4897 info->TotalFileNodes = cpu_to_le64(stfs.f_files);
4898 info->FreeFileNodes = cpu_to_le64(stfs.f_ffree);
4899 rsp->OutputBufferLength = cpu_to_le32(56);
4900 inc_rfc1001_len(rsp_org, 56);
4901 fs_infoclass_size = FS_POSIX_INFORMATION_SIZE;
4902 }
4903 break;
4904 }
4905 default:
4906 path_put(&path);
4907 return -EOPNOTSUPP;
4908 }
4909 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4910 rsp,
4911 fs_infoclass_size);
4912 path_put(&path);
4913 return rc;
4914}
4915
4916static int smb2_get_info_sec(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004917 struct smb2_query_info_req *req,
4918 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004919{
4920 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09004921 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09004922 struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL;
4923 struct smb_fattr fattr = {{0}};
4924 struct inode *inode;
4925 __u32 secdesclen;
4926 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4927 int addition_info = le32_to_cpu(req->AdditionalInformation);
4928 int rc;
4929
Namjae Jeone294f782021-06-28 15:26:37 +09004930 if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO |
4931 PROTECTED_DACL_SECINFO |
4932 UNPROTECTED_DACL_SECINFO)) {
4933 pr_err("Unsupported addition info: 0x%x)\n",
4934 addition_info);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09004935
4936 pntsd->revision = cpu_to_le16(1);
4937 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED);
4938 pntsd->osidoffset = 0;
4939 pntsd->gsidoffset = 0;
4940 pntsd->sacloffset = 0;
4941 pntsd->dacloffset = 0;
4942
4943 secdesclen = sizeof(struct smb_ntsd);
4944 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
4945 inc_rfc1001_len(rsp_org, secdesclen);
4946
4947 return 0;
4948 }
4949
Namjae Jeone2f34482021-03-16 10:49:09 +09004950 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon38673692021-07-08 12:32:27 +09004951 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
4952 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004953 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004954 id = work->compound_fid;
4955 pid = work->compound_pfid;
4956 }
4957 }
4958
Namjae Jeon38673692021-07-08 12:32:27 +09004959 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004960 id = le64_to_cpu(req->VolatileFileId);
4961 pid = le64_to_cpu(req->PersistentFileId);
4962 }
4963
4964 fp = ksmbd_lookup_fd_slow(work, id, pid);
4965 if (!fp)
4966 return -ENOENT;
4967
Hyunchul Lee465d7202021-07-03 12:10:36 +09004968 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeonab0b2632021-06-29 09:20:13 +09004969 inode = file_inode(fp->filp);
Christian Brauner43205ca2021-08-23 17:13:50 +02004970 ksmbd_acls_fattr(&fattr, user_ns, inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09004971
4972 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004973 KSMBD_SHARE_FLAG_ACL_XATTR))
Hyunchul Lee465d7202021-07-03 12:10:36 +09004974 ksmbd_vfs_get_sd_xattr(work->conn, user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09004975 fp->filp->f_path.dentry, &ppntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09004976
Hyunchul Lee465d7202021-07-03 12:10:36 +09004977 rc = build_sec_desc(user_ns, pntsd, ppntsd, addition_info,
4978 &secdesclen, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09004979 posix_acl_release(fattr.cf_acls);
4980 posix_acl_release(fattr.cf_dacls);
4981 kfree(ppntsd);
4982 ksmbd_fd_put(work, fp);
4983 if (rc)
4984 return rc;
4985
4986 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
4987 inc_rfc1001_len(rsp_org, secdesclen);
4988 return 0;
4989}
4990
4991/**
4992 * smb2_query_info() - handler for smb2 query info command
4993 * @work: smb work containing query info request buffer
4994 *
4995 * Return: 0 on success, otherwise error
4996 */
4997int smb2_query_info(struct ksmbd_work *work)
4998{
4999 struct smb2_query_info_req *req;
5000 struct smb2_query_info_rsp *rsp, *rsp_org;
5001 int rc = 0;
5002
Namjae Jeone5066492021-03-30 12:35:23 +09005003 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005004 WORK_BUFFERS(work, req, rsp);
5005
5006 ksmbd_debug(SMB, "GOT query info request\n");
5007
5008 switch (req->InfoType) {
5009 case SMB2_O_INFO_FILE:
5010 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
5011 rc = smb2_get_info_file(work, req, rsp, (void *)rsp_org);
5012 break;
5013 case SMB2_O_INFO_FILESYSTEM:
5014 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n");
5015 rc = smb2_get_info_filesystem(work, req, rsp, (void *)rsp_org);
5016 break;
5017 case SMB2_O_INFO_SECURITY:
5018 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
5019 rc = smb2_get_info_sec(work, req, rsp, (void *)rsp_org);
5020 break;
5021 default:
5022 ksmbd_debug(SMB, "InfoType %d not supported yet\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005023 req->InfoType);
Namjae Jeone2f34482021-03-16 10:49:09 +09005024 rc = -EOPNOTSUPP;
5025 }
5026
5027 if (rc < 0) {
5028 if (rc == -EACCES)
5029 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5030 else if (rc == -ENOENT)
5031 rsp->hdr.Status = STATUS_FILE_CLOSED;
5032 else if (rc == -EIO)
5033 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5034 else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0)
5035 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5036 smb2_set_err_rsp(work);
5037
5038 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005039 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005040 return rc;
5041 }
5042 rsp->StructureSize = cpu_to_le16(9);
5043 rsp->OutputBufferOffset = cpu_to_le16(72);
5044 inc_rfc1001_len(rsp_org, 8);
5045 return 0;
5046}
5047
5048/**
5049 * smb2_close_pipe() - handler for closing IPC pipe
5050 * @work: smb work containing close request buffer
5051 *
5052 * Return: 0
5053 */
5054static noinline int smb2_close_pipe(struct ksmbd_work *work)
5055{
Namjae Jeon64b39f42021-03-30 14:25:35 +09005056 u64 id;
Namjae Jeone5066492021-03-30 12:35:23 +09005057 struct smb2_close_req *req = work->request_buf;
5058 struct smb2_close_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005059
5060 id = le64_to_cpu(req->VolatileFileId);
5061 ksmbd_session_rpc_close(work->sess, id);
5062
5063 rsp->StructureSize = cpu_to_le16(60);
5064 rsp->Flags = 0;
5065 rsp->Reserved = 0;
5066 rsp->CreationTime = 0;
5067 rsp->LastAccessTime = 0;
5068 rsp->LastWriteTime = 0;
5069 rsp->ChangeTime = 0;
5070 rsp->AllocationSize = 0;
5071 rsp->EndOfFile = 0;
5072 rsp->Attributes = 0;
5073 inc_rfc1001_len(rsp, 60);
5074 return 0;
5075}
5076
5077/**
5078 * smb2_close() - handler for smb2 close file command
5079 * @work: smb work containing close request buffer
5080 *
5081 * Return: 0
5082 */
5083int smb2_close(struct ksmbd_work *work)
5084{
Namjae Jeon38673692021-07-08 12:32:27 +09005085 u64 volatile_id = KSMBD_NO_FID;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005086 u64 sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09005087 struct smb2_close_req *req;
5088 struct smb2_close_rsp *rsp;
5089 struct smb2_close_rsp *rsp_org;
5090 struct ksmbd_conn *conn = work->conn;
5091 struct ksmbd_file *fp;
5092 struct inode *inode;
5093 u64 time;
5094 int err = 0;
5095
Namjae Jeone5066492021-03-30 12:35:23 +09005096 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005097 WORK_BUFFERS(work, req, rsp);
5098
5099 if (test_share_config_flag(work->tcon->share_conf,
5100 KSMBD_SHARE_FLAG_PIPE)) {
5101 ksmbd_debug(SMB, "IPC pipe close request\n");
5102 return smb2_close_pipe(work);
5103 }
5104
5105 sess_id = le64_to_cpu(req->hdr.SessionId);
5106 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5107 sess_id = work->compound_sid;
5108
5109 work->compound_sid = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005110 if (check_session_id(conn, sess_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005111 work->compound_sid = sess_id;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005112 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005113 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
5114 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5115 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5116 err = -EBADF;
5117 goto out;
5118 }
5119
5120 if (work->next_smb2_rcv_hdr_off &&
Namjae Jeon38673692021-07-08 12:32:27 +09005121 !has_file_id(le64_to_cpu(req->VolatileFileId))) {
5122 if (!has_file_id(work->compound_fid)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005123 /* file already closed, return FILE_CLOSED */
5124 ksmbd_debug(SMB, "file already closed\n");
5125 rsp->hdr.Status = STATUS_FILE_CLOSED;
5126 err = -EBADF;
5127 goto out;
5128 } else {
Namjae Jeon38673692021-07-08 12:32:27 +09005129 ksmbd_debug(SMB,
5130 "Compound request set FID = %llu:%llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005131 work->compound_fid,
5132 work->compound_pfid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005133 volatile_id = work->compound_fid;
5134
5135 /* file closed, stored id is not valid anymore */
5136 work->compound_fid = KSMBD_NO_FID;
5137 work->compound_pfid = KSMBD_NO_FID;
5138 }
5139 } else {
5140 volatile_id = le64_to_cpu(req->VolatileFileId);
5141 }
Namjae Jeon38673692021-07-08 12:32:27 +09005142 ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id);
Namjae Jeone2f34482021-03-16 10:49:09 +09005143
5144 rsp->StructureSize = cpu_to_le16(60);
5145 rsp->Reserved = 0;
5146
5147 if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) {
5148 fp = ksmbd_lookup_fd_fast(work, volatile_id);
5149 if (!fp) {
5150 err = -ENOENT;
5151 goto out;
5152 }
5153
Namjae Jeonab0b2632021-06-29 09:20:13 +09005154 inode = file_inode(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005155 rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
5156 rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 :
5157 cpu_to_le64(inode->i_blocks << 9);
5158 rsp->EndOfFile = cpu_to_le64(inode->i_size);
5159 rsp->Attributes = fp->f_ci->m_fattr;
5160 rsp->CreationTime = cpu_to_le64(fp->create_time);
5161 time = ksmbd_UnixTimeToNT(inode->i_atime);
5162 rsp->LastAccessTime = cpu_to_le64(time);
5163 time = ksmbd_UnixTimeToNT(inode->i_mtime);
5164 rsp->LastWriteTime = cpu_to_le64(time);
5165 time = ksmbd_UnixTimeToNT(inode->i_ctime);
5166 rsp->ChangeTime = cpu_to_le64(time);
5167 ksmbd_fd_put(work, fp);
5168 } else {
5169 rsp->Flags = 0;
5170 rsp->AllocationSize = 0;
5171 rsp->EndOfFile = 0;
5172 rsp->Attributes = 0;
5173 rsp->CreationTime = 0;
5174 rsp->LastAccessTime = 0;
5175 rsp->LastWriteTime = 0;
5176 rsp->ChangeTime = 0;
5177 }
5178
5179 err = ksmbd_close_fd(work, volatile_id);
5180out:
5181 if (err) {
5182 if (rsp->hdr.Status == 0)
5183 rsp->hdr.Status = STATUS_FILE_CLOSED;
5184 smb2_set_err_rsp(work);
5185 } else {
5186 inc_rfc1001_len(rsp_org, 60);
5187 }
5188
5189 return 0;
5190}
5191
5192/**
5193 * smb2_echo() - handler for smb2 echo(ping) command
5194 * @work: smb work containing echo request buffer
5195 *
5196 * Return: 0
5197 */
5198int smb2_echo(struct ksmbd_work *work)
5199{
Namjae Jeone5066492021-03-30 12:35:23 +09005200 struct smb2_echo_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005201
5202 rsp->StructureSize = cpu_to_le16(4);
5203 rsp->Reserved = 0;
5204 inc_rfc1001_len(rsp, 4);
5205 return 0;
5206}
5207
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005208static int smb2_rename(struct ksmbd_work *work,
5209 struct ksmbd_file *fp,
5210 struct user_namespace *user_ns,
Namjae Jeon070fb212021-05-26 17:57:12 +09005211 struct smb2_file_rename_info *file_info,
5212 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005213{
5214 struct ksmbd_share_config *share = fp->tcon->share_conf;
5215 char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL;
5216 char *pathname = NULL;
5217 struct path path;
5218 bool file_present = true;
5219 int rc;
5220
5221 ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n");
5222 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5223 if (!pathname)
5224 return -ENOMEM;
5225
5226 abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
5227 if (IS_ERR(abs_oldname)) {
5228 rc = -EINVAL;
5229 goto out;
5230 }
5231 old_name = strrchr(abs_oldname, '/');
Namjae Jeon64b39f42021-03-30 14:25:35 +09005232 if (old_name && old_name[1] != '\0') {
Namjae Jeone2f34482021-03-16 10:49:09 +09005233 old_name++;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005234 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005235 ksmbd_debug(SMB, "can't get last component in path %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005236 abs_oldname);
Namjae Jeone2f34482021-03-16 10:49:09 +09005237 rc = -ENOENT;
5238 goto out;
5239 }
5240
5241 new_name = smb2_get_name(share,
5242 file_info->FileName,
5243 le32_to_cpu(file_info->FileNameLength),
5244 local_nls);
5245 if (IS_ERR(new_name)) {
5246 rc = PTR_ERR(new_name);
5247 goto out;
5248 }
5249
5250 if (strchr(new_name, ':')) {
5251 int s_type;
5252 char *xattr_stream_name, *stream_name = NULL;
5253 size_t xattr_stream_size;
5254 int len;
5255
5256 rc = parse_stream_name(new_name, &stream_name, &s_type);
5257 if (rc < 0)
5258 goto out;
5259
5260 len = strlen(new_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005261 if (len > 0 && new_name[len - 1] != '/') {
Namjae Jeonbde16942021-06-28 15:23:19 +09005262 pr_err("not allow base filename in rename\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005263 rc = -ESHARE;
5264 goto out;
5265 }
5266
5267 rc = ksmbd_vfs_xattr_stream_name(stream_name,
5268 &xattr_stream_name,
5269 &xattr_stream_size,
5270 s_type);
5271 if (rc)
5272 goto out;
5273
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005274 rc = ksmbd_vfs_setxattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005275 fp->filp->f_path.dentry,
Namjae Jeone2f34482021-03-16 10:49:09 +09005276 xattr_stream_name,
5277 NULL, 0, 0);
5278 if (rc < 0) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005279 pr_err("failed to store stream name in xattr: %d\n",
5280 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005281 rc = -EINVAL;
5282 goto out;
5283 }
5284
5285 goto out;
5286 }
5287
5288 ksmbd_debug(SMB, "new name %s\n", new_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005289 rc = ksmbd_vfs_kern_path(work, new_name, LOOKUP_NO_SYMLINKS, &path, 1);
5290 if (rc) {
5291 if (rc != -ENOENT)
5292 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005293 file_present = false;
Hyunchul Lee265fd192021-09-25 00:06:16 +09005294 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005295 path_put(&path);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005296 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005297
5298 if (ksmbd_share_veto_filename(share, new_name)) {
5299 rc = -ENOENT;
5300 ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name);
5301 goto out;
5302 }
5303
5304 if (file_info->ReplaceIfExists) {
5305 if (file_present) {
5306 rc = ksmbd_vfs_remove_file(work, new_name);
5307 if (rc) {
5308 if (rc != -ENOTEMPTY)
5309 rc = -EINVAL;
5310 ksmbd_debug(SMB, "cannot delete %s, rc %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005311 new_name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005312 goto out;
5313 }
5314 }
5315 } else {
5316 if (file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005317 strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005318 rc = -EEXIST;
5319 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005320 "cannot rename already existing file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005321 goto out;
5322 }
5323 }
5324
5325 rc = ksmbd_vfs_fp_rename(work, fp, new_name);
5326out:
5327 kfree(pathname);
5328 if (!IS_ERR(new_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005329 kfree(new_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005330 return rc;
5331}
5332
Namjae Jeone2f34482021-03-16 10:49:09 +09005333static int smb2_create_link(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005334 struct ksmbd_share_config *share,
5335 struct smb2_file_link_info *file_info,
5336 struct file *filp,
5337 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005338{
5339 char *link_name = NULL, *target_name = NULL, *pathname = NULL;
5340 struct path path;
5341 bool file_present = true;
5342 int rc;
5343
5344 ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n");
5345 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5346 if (!pathname)
5347 return -ENOMEM;
5348
5349 link_name = smb2_get_name(share,
5350 file_info->FileName,
5351 le32_to_cpu(file_info->FileNameLength),
5352 local_nls);
5353 if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) {
5354 rc = -EINVAL;
5355 goto out;
5356 }
5357
5358 ksmbd_debug(SMB, "link name is %s\n", link_name);
5359 target_name = d_path(&filp->f_path, pathname, PATH_MAX);
5360 if (IS_ERR(target_name)) {
5361 rc = -EINVAL;
5362 goto out;
5363 }
5364
5365 ksmbd_debug(SMB, "target name is %s\n", target_name);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005366 rc = ksmbd_vfs_kern_path(work, link_name, LOOKUP_NO_SYMLINKS, &path, 0);
5367 if (rc) {
5368 if (rc != -ENOENT)
5369 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005370 file_present = false;
Hyunchul Lee265fd192021-09-25 00:06:16 +09005371 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005372 path_put(&path);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005373 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005374
5375 if (file_info->ReplaceIfExists) {
5376 if (file_present) {
5377 rc = ksmbd_vfs_remove_file(work, link_name);
5378 if (rc) {
5379 rc = -EINVAL;
5380 ksmbd_debug(SMB, "cannot delete %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005381 link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005382 goto out;
5383 }
5384 }
5385 } else {
5386 if (file_present) {
5387 rc = -EEXIST;
5388 ksmbd_debug(SMB, "link already exists\n");
5389 goto out;
5390 }
5391 }
5392
5393 rc = ksmbd_vfs_link(work, target_name, link_name);
5394 if (rc)
5395 rc = -EINVAL;
5396out:
5397 if (!IS_ERR(link_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005398 kfree(link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005399 kfree(pathname);
5400 return rc;
5401}
5402
Namjae Jeon64b39f42021-03-30 14:25:35 +09005403static int set_file_basic_info(struct ksmbd_file *fp, char *buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09005404 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005405{
5406 struct smb2_file_all_info *file_info;
5407 struct iattr attrs;
Christian Braunerdb7fb6f2021-08-26 10:07:05 +09005408 struct timespec64 ctime;
Namjae Jeone2f34482021-03-16 10:49:09 +09005409 struct file *filp;
5410 struct inode *inode;
Hyunchul Lee465d7202021-07-03 12:10:36 +09005411 struct user_namespace *user_ns;
Namjae Jeon4ffd5262021-09-07 08:15:21 +09005412 int rc = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09005413
Marios Makassikis7adfd4f2021-04-27 15:30:22 +09005414 if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005415 return -EACCES;
5416
5417 file_info = (struct smb2_file_all_info *)buf;
5418 attrs.ia_valid = 0;
5419 filp = fp->filp;
5420 inode = file_inode(filp);
Hyunchul Lee465d7202021-07-03 12:10:36 +09005421 user_ns = file_mnt_user_ns(filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005422
5423 if (file_info->CreationTime)
5424 fp->create_time = le64_to_cpu(file_info->CreationTime);
5425
5426 if (file_info->LastAccessTime) {
5427 attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime);
5428 attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
5429 }
5430
5431 if (file_info->ChangeTime) {
Christian Braunerdb7fb6f2021-08-26 10:07:05 +09005432 attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime);
5433 ctime = attrs.ia_ctime;
Namjae Jeone2f34482021-03-16 10:49:09 +09005434 attrs.ia_valid |= ATTR_CTIME;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005435 } else {
Christian Braunerdb7fb6f2021-08-26 10:07:05 +09005436 ctime = inode->i_ctime;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005437 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005438
5439 if (file_info->LastWriteTime) {
5440 attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime);
5441 attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
5442 }
5443
5444 if (file_info->Attributes) {
5445 if (!S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005446 file_info->Attributes & ATTR_DIRECTORY_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005447 pr_err("can't change a file to a directory\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005448 return -EINVAL;
5449 }
5450
5451 if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == ATTR_NORMAL_LE))
5452 fp->f_ci->m_fattr = file_info->Attributes |
5453 (fp->f_ci->m_fattr & ATTR_DIRECTORY_LE);
5454 }
5455
5456 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
5457 (file_info->CreationTime || file_info->Attributes)) {
5458 struct xattr_dos_attrib da = {0};
5459
5460 da.version = 4;
5461 da.itime = fp->itime;
5462 da.create_time = fp->create_time;
5463 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
5464 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
5465 XATTR_DOSINFO_ITIME;
5466
Hyunchul Lee465d7202021-07-03 12:10:36 +09005467 rc = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09005468 filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09005469 if (rc)
5470 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005471 "failed to restore file attribute in EA\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005472 rc = 0;
5473 }
5474
Namjae Jeone2f34482021-03-16 10:49:09 +09005475 if (attrs.ia_valid) {
5476 struct dentry *dentry = filp->f_path.dentry;
5477 struct inode *inode = d_inode(dentry);
5478
5479 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
5480 return -EACCES;
5481
Namjae Jeone2f34482021-03-16 10:49:09 +09005482 inode_lock(inode);
Hyunchul Lee465d7202021-07-03 12:10:36 +09005483 rc = notify_change(user_ns, dentry, &attrs, NULL);
Christian Braunerdb7fb6f2021-08-26 10:07:05 +09005484 if (!rc) {
5485 inode->i_ctime = ctime;
5486 mark_inode_dirty(inode);
5487 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005488 inode_unlock(inode);
5489 }
Christian Braunereb5784f2021-08-23 17:13:55 +02005490 return rc;
Namjae Jeone2f34482021-03-16 10:49:09 +09005491}
5492
5493static int set_file_allocation_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005494 struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005495{
5496 /*
5497 * TODO : It's working fine only when store dos attributes
5498 * is not yes. need to implement a logic which works
5499 * properly with any smb.conf option
5500 */
5501
5502 struct smb2_file_alloc_info *file_alloc_info;
5503 loff_t alloc_blks;
5504 struct inode *inode;
5505 int rc;
5506
Marios Makassikisa2996692021-04-27 15:29:01 +09005507 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005508 return -EACCES;
5509
5510 file_alloc_info = (struct smb2_file_alloc_info *)buf;
5511 alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9;
5512 inode = file_inode(fp->filp);
5513
5514 if (alloc_blks > inode->i_blocks) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005515 smb_break_all_levII_oplock(work, fp, 1);
5516 rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0,
5517 alloc_blks * 512);
Namjae Jeone2f34482021-03-16 10:49:09 +09005518 if (rc && rc != -EOPNOTSUPP) {
Namjae Jeone8c06192021-06-22 11:06:11 +09005519 pr_err("vfs_fallocate is failed : %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005520 return rc;
5521 }
5522 } else if (alloc_blks < inode->i_blocks) {
5523 loff_t size;
5524
5525 /*
5526 * Allocation size could be smaller than original one
5527 * which means allocated blocks in file should be
5528 * deallocated. use truncate to cut out it, but inode
5529 * size is also updated with truncate offset.
5530 * inode size is retained by backup inode size.
5531 */
5532 size = i_size_read(inode);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005533 rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512);
Namjae Jeone2f34482021-03-16 10:49:09 +09005534 if (rc) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005535 pr_err("truncate failed! filename : %s, err %d\n",
5536 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005537 return rc;
5538 }
5539 if (size < alloc_blks * 512)
5540 i_size_write(inode, size);
5541 }
5542 return 0;
5543}
5544
Namjae Jeon64b39f42021-03-30 14:25:35 +09005545static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005546 char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005547{
5548 struct smb2_file_eof_info *file_eof_info;
5549 loff_t newsize;
5550 struct inode *inode;
5551 int rc;
5552
Marios Makassikisa2996692021-04-27 15:29:01 +09005553 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005554 return -EACCES;
5555
5556 file_eof_info = (struct smb2_file_eof_info *)buf;
5557 newsize = le64_to_cpu(file_eof_info->EndOfFile);
5558 inode = file_inode(fp->filp);
5559
5560 /*
5561 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called
5562 * on FAT32 shared device, truncate execution time is too long
5563 * and network error could cause from windows client. because
5564 * truncate of some filesystem like FAT32 fill zero data in
5565 * truncated range.
5566 */
5567 if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) {
5568 ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005569 fp->filename, newsize);
Hyunchul Lee265fd192021-09-25 00:06:16 +09005570 rc = ksmbd_vfs_truncate(work, fp, newsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005571 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005572 ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005573 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005574 if (rc != -EAGAIN)
5575 rc = -EBADF;
5576 return rc;
5577 }
5578 }
5579 return 0;
5580}
5581
Namjae Jeon64b39f42021-03-30 14:25:35 +09005582static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005583 char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005584{
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005585 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09005586 struct ksmbd_file *parent_fp;
Namjae Jeon12202c02021-06-29 09:23:56 +09005587 struct dentry *parent;
5588 struct dentry *dentry = fp->filp->f_path.dentry;
5589 int ret;
Namjae Jeone2f34482021-03-16 10:49:09 +09005590
5591 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005592 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005593 return -EACCES;
5594 }
5595
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005596 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005597 if (ksmbd_stream_fd(fp))
5598 goto next;
5599
Namjae Jeon12202c02021-06-29 09:23:56 +09005600 parent = dget_parent(dentry);
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005601 ret = ksmbd_vfs_lock_parent(user_ns, parent, dentry);
Namjae Jeon12202c02021-06-29 09:23:56 +09005602 if (ret) {
5603 dput(parent);
5604 return ret;
5605 }
5606
5607 parent_fp = ksmbd_lookup_fd_inode(d_inode(parent));
5608 inode_unlock(d_inode(parent));
5609 dput(parent);
5610
Namjae Jeone2f34482021-03-16 10:49:09 +09005611 if (parent_fp) {
5612 if (parent_fp->daccess & FILE_DELETE_LE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005613 pr_err("parent dir is opened with delete access\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005614 return -ESHARE;
5615 }
5616 }
5617next:
Christian Braunerda1e7ad2021-08-23 17:13:47 +02005618 return smb2_rename(work, fp, user_ns,
Namjae Jeone2f34482021-03-16 10:49:09 +09005619 (struct smb2_file_rename_info *)buf,
5620 work->sess->conn->local_nls);
5621}
5622
Namjae Jeon64b39f42021-03-30 14:25:35 +09005623static int set_file_disposition_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005624{
5625 struct smb2_file_disposition_info *file_info;
5626 struct inode *inode;
5627
5628 if (!(fp->daccess & FILE_DELETE_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005629 pr_err("no right to delete : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005630 return -EACCES;
5631 }
5632
5633 inode = file_inode(fp->filp);
5634 file_info = (struct smb2_file_disposition_info *)buf;
5635 if (file_info->DeletePending) {
5636 if (S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005637 ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY)
Namjae Jeone2f34482021-03-16 10:49:09 +09005638 return -EBUSY;
5639 ksmbd_set_inode_pending_delete(fp);
5640 } else {
5641 ksmbd_clear_inode_pending_delete(fp);
5642 }
5643 return 0;
5644}
5645
Namjae Jeon64b39f42021-03-30 14:25:35 +09005646static int set_file_position_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005647{
5648 struct smb2_file_pos_info *file_info;
5649 loff_t current_byte_offset;
Namjae Jeonee81cae2021-06-26 22:32:34 +09005650 unsigned long sector_size;
Namjae Jeone2f34482021-03-16 10:49:09 +09005651 struct inode *inode;
5652
5653 inode = file_inode(fp->filp);
5654 file_info = (struct smb2_file_pos_info *)buf;
5655 current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset);
Namjae Jeonee81cae2021-06-26 22:32:34 +09005656 sector_size = inode->i_sb->s_blocksize;
Namjae Jeone2f34482021-03-16 10:49:09 +09005657
5658 if (current_byte_offset < 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09005659 (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE &&
5660 current_byte_offset & (sector_size - 1))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005661 pr_err("CurrentByteOffset is not valid : %llu\n",
5662 current_byte_offset);
Namjae Jeone2f34482021-03-16 10:49:09 +09005663 return -EINVAL;
5664 }
5665
5666 fp->filp->f_pos = current_byte_offset;
5667 return 0;
5668}
5669
Namjae Jeon64b39f42021-03-30 14:25:35 +09005670static int set_file_mode_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005671{
5672 struct smb2_file_mode_info *file_info;
5673 __le32 mode;
5674
5675 file_info = (struct smb2_file_mode_info *)buf;
5676 mode = file_info->Mode;
5677
Namjae Jeon64b39f42021-03-30 14:25:35 +09005678 if ((mode & ~FILE_MODE_INFO_MASK) ||
5679 (mode & FILE_SYNCHRONOUS_IO_ALERT_LE &&
5680 mode & FILE_SYNCHRONOUS_IO_NONALERT_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005681 pr_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode));
Namjae Jeone2f34482021-03-16 10:49:09 +09005682 return -EINVAL;
5683 }
5684
5685 /*
5686 * TODO : need to implement consideration for
5687 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT
5688 */
5689 ksmbd_vfs_set_fadvise(fp->filp, mode);
5690 fp->coption = mode;
5691 return 0;
5692}
5693
5694/**
5695 * smb2_set_info_file() - handler for smb2 set info command
5696 * @work: smb work containing set info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09005697 * @fp: ksmbd_file pointer
5698 * @info_class: smb2 set info class
5699 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09005700 *
5701 * Return: 0 on success, otherwise error
5702 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH
5703 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09005704static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005705 int info_class, char *buf,
5706 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005707{
5708 switch (info_class) {
5709 case FILE_BASIC_INFORMATION:
5710 return set_file_basic_info(fp, buf, share);
5711
5712 case FILE_ALLOCATION_INFORMATION:
5713 return set_file_allocation_info(work, fp, buf);
5714
5715 case FILE_END_OF_FILE_INFORMATION:
5716 return set_end_of_file_info(work, fp, buf);
5717
5718 case FILE_RENAME_INFORMATION:
Namjae Jeon64b39f42021-03-30 14:25:35 +09005719 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005720 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005721 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005722 return -EACCES;
5723 }
5724 return set_rename_info(work, fp, buf);
5725
5726 case FILE_LINK_INFORMATION:
5727 return smb2_create_link(work, work->tcon->share_conf,
Namjae Jeon070fb212021-05-26 17:57:12 +09005728 (struct smb2_file_link_info *)buf, fp->filp,
5729 work->sess->conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09005730
5731 case FILE_DISPOSITION_INFORMATION:
Namjae Jeon64b39f42021-03-30 14:25:35 +09005732 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005733 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005734 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005735 return -EACCES;
5736 }
5737 return set_file_disposition_info(fp, buf);
5738
5739 case FILE_FULL_EA_INFORMATION:
5740 {
5741 if (!(fp->daccess & FILE_WRITE_EA_LE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005742 pr_err("Not permitted to write ext attr: 0x%x\n",
5743 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005744 return -EACCES;
5745 }
5746
5747 return smb2_set_ea((struct smb2_ea_info *)buf,
5748 &fp->filp->f_path);
5749 }
5750
5751 case FILE_POSITION_INFORMATION:
5752 return set_file_position_info(fp, buf);
5753
5754 case FILE_MODE_INFORMATION:
5755 return set_file_mode_info(fp, buf);
5756 }
5757
Namjae Jeonbde16942021-06-28 15:23:19 +09005758 pr_err("Unimplemented Fileinfoclass :%d\n", info_class);
Namjae Jeone2f34482021-03-16 10:49:09 +09005759 return -EOPNOTSUPP;
5760}
5761
Namjae Jeon64b39f42021-03-30 14:25:35 +09005762static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005763 char *buffer, int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005764{
5765 struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer;
5766
5767 fp->saccess |= FILE_SHARE_DELETE_LE;
5768
Hyunchul Leeef24c962021-06-30 18:25:52 +09005769 return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd,
Namjae Jeone2f34482021-03-16 10:49:09 +09005770 buf_len, false);
5771}
5772
5773/**
5774 * smb2_set_info() - handler for smb2 set info command handler
5775 * @work: smb work containing set info request buffer
5776 *
5777 * Return: 0 on success, otherwise error
5778 */
5779int smb2_set_info(struct ksmbd_work *work)
5780{
5781 struct smb2_set_info_req *req;
5782 struct smb2_set_info_rsp *rsp, *rsp_org;
5783 struct ksmbd_file *fp;
5784 int rc = 0;
5785 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5786
5787 ksmbd_debug(SMB, "Received set info request\n");
5788
Namjae Jeone5066492021-03-30 12:35:23 +09005789 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005790 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09005791 req = ksmbd_req_buf_next(work);
5792 rsp = ksmbd_resp_buf_next(work);
Namjae Jeon38673692021-07-08 12:32:27 +09005793 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
5794 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005795 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005796 id = work->compound_fid;
5797 pid = work->compound_pfid;
5798 }
5799 } else {
Namjae Jeone5066492021-03-30 12:35:23 +09005800 req = work->request_buf;
5801 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005802 }
5803
Namjae Jeon38673692021-07-08 12:32:27 +09005804 if (!has_file_id(id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005805 id = le64_to_cpu(req->VolatileFileId);
5806 pid = le64_to_cpu(req->PersistentFileId);
5807 }
5808
5809 fp = ksmbd_lookup_fd_slow(work, id, pid);
5810 if (!fp) {
5811 ksmbd_debug(SMB, "Invalid id for close: %u\n", id);
5812 rc = -ENOENT;
5813 goto err_out;
5814 }
5815
5816 switch (req->InfoType) {
5817 case SMB2_O_INFO_FILE:
5818 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
5819 rc = smb2_set_info_file(work, fp, req->FileInfoClass,
5820 req->Buffer, work->tcon->share_conf);
5821 break;
5822 case SMB2_O_INFO_SECURITY:
5823 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
Namjae Jeone70e3922021-08-21 23:26:01 +09005824 if (ksmbd_override_fsids(work)) {
5825 rc = -ENOMEM;
5826 goto err_out;
5827 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005828 rc = smb2_set_info_sec(fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005829 le32_to_cpu(req->AdditionalInformation),
5830 req->Buffer,
5831 le32_to_cpu(req->BufferLength));
Namjae Jeone70e3922021-08-21 23:26:01 +09005832 ksmbd_revert_fsids(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09005833 break;
5834 default:
5835 rc = -EOPNOTSUPP;
5836 }
5837
5838 if (rc < 0)
5839 goto err_out;
5840
5841 rsp->StructureSize = cpu_to_le16(2);
5842 inc_rfc1001_len(rsp_org, 2);
5843 ksmbd_fd_put(work, fp);
5844 return 0;
5845
5846err_out:
Hyunchul Lee265fd192021-09-25 00:06:16 +09005847 if (rc == -EACCES || rc == -EPERM || rc == -EXDEV)
Namjae Jeone2f34482021-03-16 10:49:09 +09005848 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5849 else if (rc == -EINVAL)
5850 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5851 else if (rc == -ESHARE)
5852 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
5853 else if (rc == -ENOENT)
5854 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
5855 else if (rc == -EBUSY || rc == -ENOTEMPTY)
5856 rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY;
5857 else if (rc == -EAGAIN)
5858 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
Namjae Jeonff1d5722021-04-13 13:18:10 +09005859 else if (rc == -EBADF || rc == -ESTALE)
Namjae Jeone2f34482021-03-16 10:49:09 +09005860 rsp->hdr.Status = STATUS_INVALID_HANDLE;
5861 else if (rc == -EEXIST)
5862 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
5863 else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP)
5864 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5865 smb2_set_err_rsp(work);
5866 ksmbd_fd_put(work, fp);
Namjae Jeon070fb212021-05-26 17:57:12 +09005867 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005868 return rc;
5869}
5870
5871/**
5872 * smb2_read_pipe() - handler for smb2 read from IPC pipe
5873 * @work: smb work containing read IPC pipe command buffer
5874 *
5875 * Return: 0 on success, otherwise error
5876 */
5877static noinline int smb2_read_pipe(struct ksmbd_work *work)
5878{
5879 int nbytes = 0, err;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005880 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09005881 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeone5066492021-03-30 12:35:23 +09005882 struct smb2_read_req *req = work->request_buf;
5883 struct smb2_read_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005884
5885 id = le64_to_cpu(req->VolatileFileId);
5886
5887 inc_rfc1001_len(rsp, 16);
5888 rpc_resp = ksmbd_rpc_read(work->sess, id);
5889 if (rpc_resp) {
5890 if (rpc_resp->flags != KSMBD_RPC_OK) {
5891 err = -EINVAL;
5892 goto out;
5893 }
5894
5895 work->aux_payload_buf =
Namjae Jeon79f6b112021-04-02 12:47:14 +09005896 kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09005897 if (!work->aux_payload_buf) {
5898 err = -ENOMEM;
5899 goto out;
5900 }
5901
5902 memcpy(work->aux_payload_buf, rpc_resp->payload,
Namjae Jeon070fb212021-05-26 17:57:12 +09005903 rpc_resp->payload_sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09005904
5905 nbytes = rpc_resp->payload_sz;
5906 work->resp_hdr_sz = get_rfc1002_len(rsp) + 4;
5907 work->aux_payload_sz = nbytes;
Namjae Jeon79f6b112021-04-02 12:47:14 +09005908 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005909 }
5910
5911 rsp->StructureSize = cpu_to_le16(17);
5912 rsp->DataOffset = 80;
5913 rsp->Reserved = 0;
5914 rsp->DataLength = cpu_to_le32(nbytes);
5915 rsp->DataRemaining = 0;
5916 rsp->Reserved2 = 0;
5917 inc_rfc1001_len(rsp, nbytes);
5918 return 0;
5919
5920out:
5921 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5922 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09005923 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005924 return err;
5925}
5926
5927static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005928 struct smb2_read_req *req, void *data_buf,
5929 size_t length)
Namjae Jeone2f34482021-03-16 10:49:09 +09005930{
5931 struct smb2_buffer_desc_v1 *desc =
5932 (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
5933 int err;
5934
Namjae Jeon64b39f42021-03-30 14:25:35 +09005935 if (work->conn->dialect == SMB30_PROT_ID &&
5936 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09005937 return -EINVAL;
5938
Namjae Jeon64b39f42021-03-30 14:25:35 +09005939 if (req->ReadChannelInfoOffset == 0 ||
5940 le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09005941 return -EINVAL;
5942
5943 work->need_invalidate_rkey =
5944 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
5945 work->remote_key = le32_to_cpu(desc->token);
5946
Namjae Jeon64b39f42021-03-30 14:25:35 +09005947 err = ksmbd_conn_rdma_write(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09005948 le32_to_cpu(desc->token),
5949 le64_to_cpu(desc->offset),
5950 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09005951 if (err)
5952 return err;
5953
5954 return length;
5955}
5956
5957/**
5958 * smb2_read() - handler for smb2 read from file
5959 * @work: smb work containing read command buffer
5960 *
5961 * Return: 0 on success, otherwise error
5962 */
5963int smb2_read(struct ksmbd_work *work)
5964{
5965 struct ksmbd_conn *conn = work->conn;
5966 struct smb2_read_req *req;
5967 struct smb2_read_rsp *rsp, *rsp_org;
5968 struct ksmbd_file *fp;
5969 loff_t offset;
5970 size_t length, mincount;
5971 ssize_t nbytes = 0, remain_bytes = 0;
5972 int err = 0;
5973
Namjae Jeone5066492021-03-30 12:35:23 +09005974 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005975 WORK_BUFFERS(work, req, rsp);
5976
5977 if (test_share_config_flag(work->tcon->share_conf,
5978 KSMBD_SHARE_FLAG_PIPE)) {
5979 ksmbd_debug(SMB, "IPC pipe read request\n");
5980 return smb2_read_pipe(work);
5981 }
5982
Namjae Jeon070fb212021-05-26 17:57:12 +09005983 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
5984 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09005985 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09005986 err = -ENOENT;
5987 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005988 }
5989
5990 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09005991 pr_err("Not permitted to read : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09005992 err = -EACCES;
5993 goto out;
5994 }
5995
5996 offset = le64_to_cpu(req->Offset);
5997 length = le32_to_cpu(req->Length);
5998 mincount = le32_to_cpu(req->MinimumCount);
5999
6000 if (length > conn->vals->max_read_size) {
6001 ksmbd_debug(SMB, "limiting read size to max size(%u)\n",
6002 conn->vals->max_read_size);
6003 err = -EINVAL;
6004 goto out;
6005 }
6006
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006007 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6008 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006009
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006010 work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006011 if (!work->aux_payload_buf) {
Dan Carpenterc1ea1112021-03-22 17:50:11 +03006012 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09006013 goto out;
6014 }
6015
6016 nbytes = ksmbd_vfs_read(work, fp, length, &offset);
6017 if (nbytes < 0) {
6018 err = nbytes;
6019 goto out;
6020 }
6021
6022 if ((nbytes == 0 && length != 0) || nbytes < mincount) {
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006023 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006024 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006025 rsp->hdr.Status = STATUS_END_OF_FILE;
6026 smb2_set_err_rsp(work);
6027 ksmbd_fd_put(work, fp);
6028 return 0;
6029 }
6030
6031 ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006032 nbytes, offset, mincount);
Namjae Jeone2f34482021-03-16 10:49:09 +09006033
6034 if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006035 req->Channel == SMB2_CHANNEL_RDMA_V1) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006036 /* write data to the client using rdma channel */
6037 remain_bytes = smb2_read_rdma_channel(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09006038 work->aux_payload_buf,
6039 nbytes);
Namjae Jeonc30f4eb2021-06-18 10:17:37 +09006040 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09006041 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006042
6043 nbytes = 0;
6044 if (remain_bytes < 0) {
6045 err = (int)remain_bytes;
6046 goto out;
6047 }
6048 }
6049
6050 rsp->StructureSize = cpu_to_le16(17);
6051 rsp->DataOffset = 80;
6052 rsp->Reserved = 0;
6053 rsp->DataLength = cpu_to_le32(nbytes);
6054 rsp->DataRemaining = cpu_to_le32(remain_bytes);
6055 rsp->Reserved2 = 0;
6056 inc_rfc1001_len(rsp_org, 16);
6057 work->resp_hdr_sz = get_rfc1002_len(rsp_org) + 4;
6058 work->aux_payload_sz = nbytes;
6059 inc_rfc1001_len(rsp_org, nbytes);
6060 ksmbd_fd_put(work, fp);
6061 return 0;
6062
6063out:
6064 if (err) {
6065 if (err == -EISDIR)
6066 rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST;
6067 else if (err == -EAGAIN)
6068 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6069 else if (err == -ENOENT)
6070 rsp->hdr.Status = STATUS_FILE_CLOSED;
6071 else if (err == -EACCES)
6072 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6073 else if (err == -ESHARE)
6074 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6075 else if (err == -EINVAL)
6076 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6077 else
6078 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6079
6080 smb2_set_err_rsp(work);
6081 }
6082 ksmbd_fd_put(work, fp);
6083 return err;
6084}
6085
6086/**
6087 * smb2_write_pipe() - handler for smb2 write on IPC pipe
6088 * @work: smb work containing write IPC pipe command buffer
6089 *
6090 * Return: 0 on success, otherwise error
6091 */
6092static noinline int smb2_write_pipe(struct ksmbd_work *work)
6093{
Namjae Jeone5066492021-03-30 12:35:23 +09006094 struct smb2_write_req *req = work->request_buf;
6095 struct smb2_write_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006096 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006097 u64 id = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09006098 int err = 0, ret = 0;
6099 char *data_buf;
6100 size_t length;
6101
6102 length = le32_to_cpu(req->Length);
6103 id = le64_to_cpu(req->VolatileFileId);
6104
6105 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09006106 (offsetof(struct smb2_write_req, Buffer) - 4)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006107 data_buf = (char *)&req->Buffer[0];
6108 } else {
6109 if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006110 (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006111 pr_err("invalid write data offset %u, smb_len %u\n",
6112 le16_to_cpu(req->DataOffset),
6113 get_rfc1002_len(req));
Namjae Jeone2f34482021-03-16 10:49:09 +09006114 err = -EINVAL;
6115 goto out;
6116 }
6117
6118 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6119 le16_to_cpu(req->DataOffset));
6120 }
6121
6122 rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length);
6123 if (rpc_resp) {
6124 if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
6125 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006126 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006127 smb2_set_err_rsp(work);
6128 return -EOPNOTSUPP;
6129 }
6130 if (rpc_resp->flags != KSMBD_RPC_OK) {
6131 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6132 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006133 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006134 return ret;
6135 }
Namjae Jeon79f6b112021-04-02 12:47:14 +09006136 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006137 }
6138
6139 rsp->StructureSize = cpu_to_le16(17);
6140 rsp->DataOffset = 0;
6141 rsp->Reserved = 0;
6142 rsp->DataLength = cpu_to_le32(length);
6143 rsp->DataRemaining = 0;
6144 rsp->Reserved2 = 0;
6145 inc_rfc1001_len(rsp, 16);
6146 return 0;
6147out:
6148 if (err) {
6149 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6150 smb2_set_err_rsp(work);
6151 }
6152
6153 return err;
6154}
6155
6156static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006157 struct smb2_write_req *req,
6158 struct ksmbd_file *fp,
6159 loff_t offset, size_t length, bool sync)
Namjae Jeone2f34482021-03-16 10:49:09 +09006160{
6161 struct smb2_buffer_desc_v1 *desc;
6162 char *data_buf;
6163 int ret;
6164 ssize_t nbytes;
6165
6166 desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6167
6168 if (work->conn->dialect == SMB30_PROT_ID &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006169 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09006170 return -EINVAL;
6171
6172 if (req->Length != 0 || req->DataOffset != 0)
6173 return -EINVAL;
6174
Namjae Jeon64b39f42021-03-30 14:25:35 +09006175 if (req->WriteChannelInfoOffset == 0 ||
6176 le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09006177 return -EINVAL;
6178
6179 work->need_invalidate_rkey =
6180 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6181 work->remote_key = le32_to_cpu(desc->token);
6182
Namjae Jeon79f6b112021-04-02 12:47:14 +09006183 data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006184 if (!data_buf)
6185 return -ENOMEM;
6186
6187 ret = ksmbd_conn_rdma_read(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006188 le32_to_cpu(desc->token),
6189 le64_to_cpu(desc->offset),
6190 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006191 if (ret < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09006192 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006193 return ret;
6194 }
6195
Namjae Jeon64b39f42021-03-30 14:25:35 +09006196 ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006197 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006198 if (ret < 0)
6199 return ret;
6200
6201 return nbytes;
6202}
6203
6204/**
6205 * smb2_write() - handler for smb2 write from file
6206 * @work: smb work containing write command buffer
6207 *
6208 * Return: 0 on success, otherwise error
6209 */
6210int smb2_write(struct ksmbd_work *work)
6211{
6212 struct smb2_write_req *req;
6213 struct smb2_write_rsp *rsp, *rsp_org;
Namjae Jeonbcd62a32021-05-10 09:08:19 +09006214 struct ksmbd_file *fp = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006215 loff_t offset;
6216 size_t length;
6217 ssize_t nbytes;
6218 char *data_buf;
6219 bool writethrough = false;
6220 int err = 0;
6221
Namjae Jeone5066492021-03-30 12:35:23 +09006222 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006223 WORK_BUFFERS(work, req, rsp);
6224
Namjae Jeon64b39f42021-03-30 14:25:35 +09006225 if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006226 ksmbd_debug(SMB, "IPC pipe write request\n");
6227 return smb2_write_pipe(work);
6228 }
6229
6230 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
6231 ksmbd_debug(SMB, "User does not have write permission\n");
6232 err = -EACCES;
6233 goto out;
6234 }
6235
Namjae Jeon64b39f42021-03-30 14:25:35 +09006236 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
Namjae Jeon070fb212021-05-26 17:57:12 +09006237 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006238 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006239 err = -ENOENT;
6240 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006241 }
6242
6243 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006244 pr_err("Not permitted to write : 0x%x\n", fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09006245 err = -EACCES;
6246 goto out;
6247 }
6248
6249 offset = le64_to_cpu(req->Offset);
6250 length = le32_to_cpu(req->Length);
6251
6252 if (length > work->conn->vals->max_write_size) {
6253 ksmbd_debug(SMB, "limiting write size to max size(%u)\n",
6254 work->conn->vals->max_write_size);
6255 err = -EINVAL;
6256 goto out;
6257 }
6258
6259 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6260 writethrough = true;
6261
6262 if (req->Channel != SMB2_CHANNEL_RDMA_V1 &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006263 req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006264 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeon070fb212021-05-26 17:57:12 +09006265 (offsetof(struct smb2_write_req, Buffer) - 4)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006266 data_buf = (char *)&req->Buffer[0];
6267 } else {
Namjae Jeon64b39f42021-03-30 14:25:35 +09006268 if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) ||
6269 (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006270 pr_err("invalid write data offset %u, smb_len %u\n",
6271 le16_to_cpu(req->DataOffset),
6272 get_rfc1002_len(req));
Namjae Jeone2f34482021-03-16 10:49:09 +09006273 err = -EINVAL;
6274 goto out;
6275 }
6276
6277 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6278 le16_to_cpu(req->DataOffset));
6279 }
6280
6281 ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags));
6282 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6283 writethrough = true;
6284
Namjae Jeon493fa2f2021-06-29 09:22:16 +09006285 ksmbd_debug(SMB, "filename %pd, offset %lld, len %zu\n",
6286 fp->filp->f_path.dentry, offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006287 err = ksmbd_vfs_write(work, fp, data_buf, length, &offset,
6288 writethrough, &nbytes);
6289 if (err < 0)
6290 goto out;
6291 } else {
6292 /* read data from the client using rdma channel, and
6293 * write the data.
6294 */
6295 nbytes = smb2_write_rdma_channel(work, req, fp, offset,
Namjae Jeon070fb212021-05-26 17:57:12 +09006296 le32_to_cpu(req->RemainingBytes),
6297 writethrough);
Namjae Jeone2f34482021-03-16 10:49:09 +09006298 if (nbytes < 0) {
6299 err = (int)nbytes;
6300 goto out;
6301 }
6302 }
6303
6304 rsp->StructureSize = cpu_to_le16(17);
6305 rsp->DataOffset = 0;
6306 rsp->Reserved = 0;
6307 rsp->DataLength = cpu_to_le32(nbytes);
6308 rsp->DataRemaining = 0;
6309 rsp->Reserved2 = 0;
6310 inc_rfc1001_len(rsp_org, 16);
6311 ksmbd_fd_put(work, fp);
6312 return 0;
6313
6314out:
6315 if (err == -EAGAIN)
6316 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6317 else if (err == -ENOSPC || err == -EFBIG)
6318 rsp->hdr.Status = STATUS_DISK_FULL;
6319 else if (err == -ENOENT)
6320 rsp->hdr.Status = STATUS_FILE_CLOSED;
6321 else if (err == -EACCES)
6322 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6323 else if (err == -ESHARE)
6324 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6325 else if (err == -EINVAL)
6326 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6327 else
6328 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6329
6330 smb2_set_err_rsp(work);
6331 ksmbd_fd_put(work, fp);
6332 return err;
6333}
6334
6335/**
6336 * smb2_flush() - handler for smb2 flush file - fsync
6337 * @work: smb work containing flush command buffer
6338 *
6339 * Return: 0 on success, otherwise error
6340 */
6341int smb2_flush(struct ksmbd_work *work)
6342{
6343 struct smb2_flush_req *req;
6344 struct smb2_flush_rsp *rsp, *rsp_org;
6345 int err;
6346
Namjae Jeone5066492021-03-30 12:35:23 +09006347 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006348 WORK_BUFFERS(work, req, rsp);
6349
6350 ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006351 le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006352
6353 err = ksmbd_vfs_fsync(work,
6354 le64_to_cpu(req->VolatileFileId),
6355 le64_to_cpu(req->PersistentFileId));
6356 if (err)
6357 goto out;
6358
6359 rsp->StructureSize = cpu_to_le16(4);
6360 rsp->Reserved = 0;
6361 inc_rfc1001_len(rsp_org, 4);
6362 return 0;
6363
6364out:
6365 if (err) {
6366 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6367 smb2_set_err_rsp(work);
6368 }
6369
6370 return err;
6371}
6372
6373/**
6374 * smb2_cancel() - handler for smb2 cancel command
6375 * @work: smb work containing cancel command buffer
6376 *
6377 * Return: 0 on success, otherwise error
6378 */
6379int smb2_cancel(struct ksmbd_work *work)
6380{
6381 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09006382 struct smb2_hdr *hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006383 struct smb2_hdr *chdr;
6384 struct ksmbd_work *cancel_work = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006385 int canceled = 0;
6386 struct list_head *command_list;
6387
6388 ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006389 hdr->MessageId, hdr->Flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09006390
6391 if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) {
6392 command_list = &conn->async_requests;
6393
6394 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006395 list_for_each_entry(cancel_work, command_list,
6396 async_request_entry) {
Namjae Jeone5066492021-03-30 12:35:23 +09006397 chdr = cancel_work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006398
6399 if (cancel_work->async_id !=
Namjae Jeon64b39f42021-03-30 14:25:35 +09006400 le64_to_cpu(hdr->Id.AsyncId))
Namjae Jeone2f34482021-03-16 10:49:09 +09006401 continue;
6402
6403 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006404 "smb2 with AsyncId %llu cancelled command = 0x%x\n",
6405 le64_to_cpu(hdr->Id.AsyncId),
6406 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006407 canceled = 1;
6408 break;
6409 }
6410 spin_unlock(&conn->request_lock);
6411 } else {
6412 command_list = &conn->requests;
6413
6414 spin_lock(&conn->request_lock);
Namjae Jeon6f3d5ee2021-06-18 10:28:52 +09006415 list_for_each_entry(cancel_work, command_list, request_entry) {
Namjae Jeone5066492021-03-30 12:35:23 +09006416 chdr = cancel_work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006417
6418 if (chdr->MessageId != hdr->MessageId ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006419 cancel_work == work)
Namjae Jeone2f34482021-03-16 10:49:09 +09006420 continue;
6421
6422 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006423 "smb2 with mid %llu cancelled command = 0x%x\n",
6424 le64_to_cpu(hdr->MessageId),
6425 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006426 canceled = 1;
6427 break;
6428 }
6429 spin_unlock(&conn->request_lock);
6430 }
6431
6432 if (canceled) {
6433 cancel_work->state = KSMBD_WORK_CANCELLED;
6434 if (cancel_work->cancel_fn)
6435 cancel_work->cancel_fn(cancel_work->cancel_argv);
6436 }
6437
6438 /* For SMB2_CANCEL command itself send no response*/
6439 work->send_no_response = 1;
6440 return 0;
6441}
6442
6443struct file_lock *smb_flock_init(struct file *f)
6444{
6445 struct file_lock *fl;
6446
6447 fl = locks_alloc_lock();
6448 if (!fl)
6449 goto out;
6450
6451 locks_init_lock(fl);
6452
6453 fl->fl_owner = f;
6454 fl->fl_pid = current->tgid;
6455 fl->fl_file = f;
6456 fl->fl_flags = FL_POSIX;
6457 fl->fl_ops = NULL;
6458 fl->fl_lmops = NULL;
6459
6460out:
6461 return fl;
6462}
6463
6464static int smb2_set_flock_flags(struct file_lock *flock, int flags)
6465{
6466 int cmd = -EINVAL;
6467
6468 /* Checking for wrong flag combination during lock request*/
6469 switch (flags) {
6470 case SMB2_LOCKFLAG_SHARED:
6471 ksmbd_debug(SMB, "received shared request\n");
6472 cmd = F_SETLKW;
6473 flock->fl_type = F_RDLCK;
6474 flock->fl_flags |= FL_SLEEP;
6475 break;
6476 case SMB2_LOCKFLAG_EXCLUSIVE:
6477 ksmbd_debug(SMB, "received exclusive request\n");
6478 cmd = F_SETLKW;
6479 flock->fl_type = F_WRLCK;
6480 flock->fl_flags |= FL_SLEEP;
6481 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006482 case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006483 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006484 "received shared & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006485 cmd = F_SETLK;
6486 flock->fl_type = F_RDLCK;
6487 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006488 case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006489 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006490 "received exclusive & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006491 cmd = F_SETLK;
6492 flock->fl_type = F_WRLCK;
6493 break;
6494 case SMB2_LOCKFLAG_UNLOCK:
6495 ksmbd_debug(SMB, "received unlock request\n");
6496 flock->fl_type = F_UNLCK;
6497 cmd = 0;
6498 break;
6499 }
6500
6501 return cmd;
6502}
6503
6504static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock,
Namjae Jeon070fb212021-05-26 17:57:12 +09006505 unsigned int cmd, int flags,
6506 struct list_head *lock_list)
Namjae Jeone2f34482021-03-16 10:49:09 +09006507{
6508 struct ksmbd_lock *lock;
6509
6510 lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL);
6511 if (!lock)
6512 return NULL;
6513
6514 lock->cmd = cmd;
6515 lock->fl = flock;
6516 lock->start = flock->fl_start;
6517 lock->end = flock->fl_end;
6518 lock->flags = flags;
6519 if (lock->start == lock->end)
6520 lock->zero_len = 1;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006521 INIT_LIST_HEAD(&lock->clist);
6522 INIT_LIST_HEAD(&lock->flist);
Namjae Jeone2f34482021-03-16 10:49:09 +09006523 INIT_LIST_HEAD(&lock->llist);
Namjae Jeone2f34482021-03-16 10:49:09 +09006524 list_add_tail(&lock->llist, lock_list);
6525
6526 return lock;
6527}
6528
6529static void smb2_remove_blocked_lock(void **argv)
6530{
6531 struct file_lock *flock = (struct file_lock *)argv[0];
6532
6533 ksmbd_vfs_posix_lock_unblock(flock);
6534 wake_up(&flock->fl_wait);
6535}
6536
6537static inline bool lock_defer_pending(struct file_lock *fl)
6538{
6539 /* check pending lock waiters */
6540 return waitqueue_active(&fl->fl_wait);
6541}
6542
6543/**
6544 * smb2_lock() - handler for smb2 file lock command
6545 * @work: smb work containing lock command buffer
6546 *
6547 * Return: 0 on success, otherwise error
6548 */
6549int smb2_lock(struct ksmbd_work *work)
6550{
Namjae Jeone5066492021-03-30 12:35:23 +09006551 struct smb2_lock_req *req = work->request_buf;
6552 struct smb2_lock_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006553 struct smb2_lock_element *lock_ele;
6554 struct ksmbd_file *fp = NULL;
6555 struct file_lock *flock = NULL;
6556 struct file *filp = NULL;
6557 int lock_count;
6558 int flags = 0;
6559 int cmd = 0;
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006560 int err = -EIO, i, rc = 0;
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006561 u64 lock_start, lock_length;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006562 struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2;
6563 struct ksmbd_conn *conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09006564 int nolock = 0;
6565 LIST_HEAD(lock_list);
6566 LIST_HEAD(rollback_list);
6567 int prior_lock = 0;
6568
6569 ksmbd_debug(SMB, "Received lock request\n");
6570 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006571 le64_to_cpu(req->VolatileFileId),
6572 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006573 if (!fp) {
6574 ksmbd_debug(SMB, "Invalid file id for lock : %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006575 le64_to_cpu(req->VolatileFileId));
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006576 err = -ENOENT;
Namjae Jeone2f34482021-03-16 10:49:09 +09006577 goto out2;
6578 }
6579
6580 filp = fp->filp;
6581 lock_count = le16_to_cpu(req->LockCount);
6582 lock_ele = req->locks;
6583
6584 ksmbd_debug(SMB, "lock count is %d\n", lock_count);
Namjae Jeon070fb212021-05-26 17:57:12 +09006585 if (!lock_count) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006586 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006587 goto out2;
6588 }
6589
6590 for (i = 0; i < lock_count; i++) {
6591 flags = le32_to_cpu(lock_ele[i].Flags);
6592
6593 flock = smb_flock_init(filp);
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006594 if (!flock)
Namjae Jeone2f34482021-03-16 10:49:09 +09006595 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006596
6597 cmd = smb2_set_flock_flags(flock, flags);
6598
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006599 lock_start = le64_to_cpu(lock_ele[i].Offset);
6600 lock_length = le64_to_cpu(lock_ele[i].Length);
6601 if (lock_start > U64_MAX - lock_length) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006602 pr_err("Invalid lock range requested\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006603 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6604 goto out;
6605 }
6606
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006607 if (lock_start > OFFSET_MAX)
6608 flock->fl_start = OFFSET_MAX;
6609 else
6610 flock->fl_start = lock_start;
6611
Namjae Jeone2f34482021-03-16 10:49:09 +09006612 lock_length = le64_to_cpu(lock_ele[i].Length);
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006613 if (lock_length > OFFSET_MAX - flock->fl_start)
6614 lock_length = OFFSET_MAX - flock->fl_start;
Namjae Jeone2f34482021-03-16 10:49:09 +09006615
6616 flock->fl_end = flock->fl_start + lock_length;
6617
6618 if (flock->fl_end < flock->fl_start) {
6619 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006620 "the end offset(%llx) is smaller than the start offset(%llx)\n",
6621 flock->fl_end, flock->fl_start);
Namjae Jeone2f34482021-03-16 10:49:09 +09006622 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6623 goto out;
6624 }
6625
6626 /* Check conflict locks in one request */
6627 list_for_each_entry(cmp_lock, &lock_list, llist) {
6628 if (cmp_lock->fl->fl_start <= flock->fl_start &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006629 cmp_lock->fl->fl_end >= flock->fl_end) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006630 if (cmp_lock->fl->fl_type != F_UNLCK &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006631 flock->fl_type != F_UNLCK) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006632 pr_err("conflict two locks in one request\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006633 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006634 goto out;
6635 }
6636 }
6637 }
6638
6639 smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list);
6640 if (!smb_lock) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006641 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006642 goto out;
6643 }
6644 }
6645
6646 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6647 if (smb_lock->cmd < 0) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006648 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006649 goto out;
6650 }
6651
6652 if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006653 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006654 goto out;
6655 }
6656
Namjae Jeon64b39f42021-03-30 14:25:35 +09006657 if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) &&
6658 smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) ||
6659 (prior_lock == SMB2_LOCKFLAG_UNLOCK &&
6660 !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006661 err = -EINVAL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006662 goto out;
6663 }
6664
6665 prior_lock = smb_lock->flags;
6666
6667 if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006668 !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY))
Hyunchul Leed63528e2021-07-10 16:22:41 +09006669 goto no_check_cl;
Namjae Jeone2f34482021-03-16 10:49:09 +09006670
6671 nolock = 1;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006672 /* check locks in connection list */
6673 read_lock(&conn_list_lock);
6674 list_for_each_entry(conn, &conn_list, conns_list) {
6675 spin_lock(&conn->llist_lock);
6676 list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) {
6677 if (file_inode(cmp_lock->fl->fl_file) !=
6678 file_inode(smb_lock->fl->fl_file))
6679 continue;
Namjae Jeone2f34482021-03-16 10:49:09 +09006680
Hyunchul Leed63528e2021-07-10 16:22:41 +09006681 if (smb_lock->fl->fl_type == F_UNLCK) {
6682 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file &&
6683 cmp_lock->start == smb_lock->start &&
6684 cmp_lock->end == smb_lock->end &&
6685 !lock_defer_pending(cmp_lock->fl)) {
6686 nolock = 0;
6687 list_del(&cmp_lock->flist);
6688 list_del(&cmp_lock->clist);
6689 spin_unlock(&conn->llist_lock);
6690 read_unlock(&conn_list_lock);
6691
6692 locks_free_lock(cmp_lock->fl);
6693 kfree(cmp_lock);
6694 goto out_check_cl;
6695 }
6696 continue;
Namjae Jeone2f34482021-03-16 10:49:09 +09006697 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006698
Hyunchul Leed63528e2021-07-10 16:22:41 +09006699 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) {
6700 if (smb_lock->flags & SMB2_LOCKFLAG_SHARED)
6701 continue;
6702 } else {
6703 if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED)
6704 continue;
6705 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006706
Hyunchul Leed63528e2021-07-10 16:22:41 +09006707 /* check zero byte lock range */
6708 if (cmp_lock->zero_len && !smb_lock->zero_len &&
6709 cmp_lock->start > smb_lock->start &&
6710 cmp_lock->start < smb_lock->end) {
6711 spin_unlock(&conn->llist_lock);
6712 read_unlock(&conn_list_lock);
6713 pr_err("previous lock conflict with zero byte lock range\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006714 goto out;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006715 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006716
Hyunchul Leed63528e2021-07-10 16:22:41 +09006717 if (smb_lock->zero_len && !cmp_lock->zero_len &&
6718 smb_lock->start > cmp_lock->start &&
6719 smb_lock->start < cmp_lock->end) {
6720 spin_unlock(&conn->llist_lock);
6721 read_unlock(&conn_list_lock);
6722 pr_err("current lock conflict with zero byte lock range\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006723 goto out;
Hyunchul Leed63528e2021-07-10 16:22:41 +09006724 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006725
Hyunchul Leed63528e2021-07-10 16:22:41 +09006726 if (((cmp_lock->start <= smb_lock->start &&
6727 cmp_lock->end > smb_lock->start) ||
6728 (cmp_lock->start < smb_lock->end &&
6729 cmp_lock->end >= smb_lock->end)) &&
6730 !cmp_lock->zero_len && !smb_lock->zero_len) {
6731 spin_unlock(&conn->llist_lock);
6732 read_unlock(&conn_list_lock);
6733 pr_err("Not allow lock operation on exclusive lock range\n");
Hyunchul Leed63528e2021-07-10 16:22:41 +09006734 goto out;
6735 }
Namjae Jeone2f34482021-03-16 10:49:09 +09006736 }
Hyunchul Leed63528e2021-07-10 16:22:41 +09006737 spin_unlock(&conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006738 }
Hyunchul Leed63528e2021-07-10 16:22:41 +09006739 read_unlock(&conn_list_lock);
6740out_check_cl:
Namjae Jeone2f34482021-03-16 10:49:09 +09006741 if (smb_lock->fl->fl_type == F_UNLCK && nolock) {
Namjae Jeonbde16942021-06-28 15:23:19 +09006742 pr_err("Try to unlock nolocked range\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006743 rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED;
6744 goto out;
6745 }
6746
Hyunchul Leed63528e2021-07-10 16:22:41 +09006747no_check_cl:
Namjae Jeone2f34482021-03-16 10:49:09 +09006748 if (smb_lock->zero_len) {
6749 err = 0;
6750 goto skip;
6751 }
6752
6753 flock = smb_lock->fl;
6754 list_del(&smb_lock->llist);
6755retry:
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006756 rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL);
Namjae Jeone2f34482021-03-16 10:49:09 +09006757skip:
6758 if (flags & SMB2_LOCKFLAG_UNLOCK) {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006759 if (!rc) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006760 ksmbd_debug(SMB, "File unlocked\n");
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006761 } else if (rc == -ENOENT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006762 rsp->hdr.Status = STATUS_NOT_LOCKED;
6763 goto out;
6764 }
6765 locks_free_lock(flock);
6766 kfree(smb_lock);
6767 } else {
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006768 if (rc == FILE_LOCK_DEFERRED) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006769 void **argv;
6770
6771 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006772 "would have to wait for getting lock\n");
Hyunchul Leed63528e2021-07-10 16:22:41 +09006773 spin_lock(&work->conn->llist_lock);
6774 list_add_tail(&smb_lock->clist,
6775 &work->conn->lock_list);
6776 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006777 list_add(&smb_lock->llist, &rollback_list);
6778
6779 argv = kmalloc(sizeof(void *), GFP_KERNEL);
6780 if (!argv) {
6781 err = -ENOMEM;
6782 goto out;
6783 }
6784 argv[0] = flock;
6785
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006786 rc = setup_async_work(work,
6787 smb2_remove_blocked_lock,
6788 argv);
6789 if (rc) {
6790 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09006791 goto out;
6792 }
6793 spin_lock(&fp->f_lock);
6794 list_add(&work->fp_entry, &fp->blocked_works);
6795 spin_unlock(&fp->f_lock);
6796
6797 smb2_send_interim_resp(work, STATUS_PENDING);
6798
Hyunchul Lee45a64e82021-07-10 09:34:20 +09006799 ksmbd_vfs_posix_lock_wait(flock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006800
Hyunchul Leed4075ab2021-06-25 07:02:10 +09006801 if (work->state != KSMBD_WORK_ACTIVE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006802 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09006803 spin_lock(&work->conn->llist_lock);
6804 list_del(&smb_lock->clist);
6805 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006806 locks_free_lock(flock);
6807
Hyunchul Leed4075ab2021-06-25 07:02:10 +09006808 if (work->state == KSMBD_WORK_CANCELLED) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006809 spin_lock(&fp->f_lock);
6810 list_del(&work->fp_entry);
6811 spin_unlock(&fp->f_lock);
6812 rsp->hdr.Status =
6813 STATUS_CANCELLED;
6814 kfree(smb_lock);
6815 smb2_send_interim_resp(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006816 STATUS_CANCELLED);
Namjae Jeone2f34482021-03-16 10:49:09 +09006817 work->send_no_response = 1;
6818 goto out;
6819 }
6820 init_smb2_rsp_hdr(work);
6821 smb2_set_err_rsp(work);
6822 rsp->hdr.Status =
6823 STATUS_RANGE_NOT_LOCKED;
6824 kfree(smb_lock);
6825 goto out2;
6826 }
6827
6828 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09006829 spin_lock(&work->conn->llist_lock);
6830 list_del(&smb_lock->clist);
6831 spin_unlock(&work->conn->llist_lock);
6832
Namjae Jeone2f34482021-03-16 10:49:09 +09006833 spin_lock(&fp->f_lock);
6834 list_del(&work->fp_entry);
6835 spin_unlock(&fp->f_lock);
6836 goto retry;
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006837 } else if (!rc) {
Hyunchul Leed63528e2021-07-10 16:22:41 +09006838 spin_lock(&work->conn->llist_lock);
6839 list_add_tail(&smb_lock->clist,
6840 &work->conn->lock_list);
6841 list_add_tail(&smb_lock->flist,
6842 &fp->lock_list);
6843 spin_unlock(&work->conn->llist_lock);
Namjae Jeone2f34482021-03-16 10:49:09 +09006844 list_add(&smb_lock->llist, &rollback_list);
6845 ksmbd_debug(SMB, "successful in taking lock\n");
6846 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09006847 goto out;
6848 }
6849 }
6850 }
6851
6852 if (atomic_read(&fp->f_ci->op_count) > 1)
6853 smb_break_all_oplock(work, fp);
6854
6855 rsp->StructureSize = cpu_to_le16(4);
6856 ksmbd_debug(SMB, "successful in taking lock\n");
6857 rsp->hdr.Status = STATUS_SUCCESS;
6858 rsp->Reserved = 0;
6859 inc_rfc1001_len(rsp, 4);
6860 ksmbd_fd_put(work, fp);
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09006861 return 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09006862
6863out:
6864 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6865 locks_free_lock(smb_lock->fl);
6866 list_del(&smb_lock->llist);
6867 kfree(smb_lock);
6868 }
6869
6870 list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) {
6871 struct file_lock *rlock = NULL;
6872
6873 rlock = smb_flock_init(filp);
6874 rlock->fl_type = F_UNLCK;
6875 rlock->fl_start = smb_lock->start;
6876 rlock->fl_end = smb_lock->end;
6877
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09006878 rc = vfs_lock_file(filp, 0, rlock, NULL);
6879 if (rc)
6880 pr_err("rollback unlock fail : %d\n", rc);
Hyunchul Leed63528e2021-07-10 16:22:41 +09006881
Namjae Jeone2f34482021-03-16 10:49:09 +09006882 list_del(&smb_lock->llist);
Hyunchul Leed63528e2021-07-10 16:22:41 +09006883 spin_lock(&work->conn->llist_lock);
6884 if (!list_empty(&smb_lock->flist))
6885 list_del(&smb_lock->flist);
6886 list_del(&smb_lock->clist);
6887 spin_unlock(&work->conn->llist_lock);
6888
Namjae Jeone2f34482021-03-16 10:49:09 +09006889 locks_free_lock(smb_lock->fl);
6890 locks_free_lock(rlock);
6891 kfree(smb_lock);
6892 }
6893out2:
Namjae Jeon6c99dfc2021-07-27 09:40:05 +09006894 ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err);
6895
6896 if (!rsp->hdr.Status) {
6897 if (err == -EINVAL)
6898 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6899 else if (err == -ENOMEM)
6900 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
6901 else if (err == -ENOENT)
6902 rsp->hdr.Status = STATUS_FILE_CLOSED;
6903 else
6904 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6905 }
6906
Namjae Jeone2f34482021-03-16 10:49:09 +09006907 smb2_set_err_rsp(work);
6908 ksmbd_fd_put(work, fp);
Namjae Jeon96ad4ec2021-07-13 17:17:28 +09006909 return err;
Namjae Jeone2f34482021-03-16 10:49:09 +09006910}
6911
Namjae Jeon64b39f42021-03-30 14:25:35 +09006912static int fsctl_copychunk(struct ksmbd_work *work, struct smb2_ioctl_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09006913 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09006914{
6915 struct copychunk_ioctl_req *ci_req;
6916 struct copychunk_ioctl_rsp *ci_rsp;
6917 struct ksmbd_file *src_fp = NULL, *dst_fp = NULL;
6918 struct srv_copychunk *chunks;
6919 unsigned int i, chunk_count, chunk_count_written = 0;
6920 unsigned int chunk_size_written = 0;
6921 loff_t total_size_written = 0;
6922 int ret, cnt_code;
6923
6924 cnt_code = le32_to_cpu(req->CntCode);
6925 ci_req = (struct copychunk_ioctl_req *)&req->Buffer[0];
6926 ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0];
6927
6928 rsp->VolatileFileId = req->VolatileFileId;
6929 rsp->PersistentFileId = req->PersistentFileId;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006930 ci_rsp->ChunksWritten =
6931 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count());
6932 ci_rsp->ChunkBytesWritten =
6933 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size());
6934 ci_rsp->TotalBytesWritten =
6935 cpu_to_le32(ksmbd_server_side_copy_max_total_size());
Namjae Jeone2f34482021-03-16 10:49:09 +09006936
6937 chunks = (struct srv_copychunk *)&ci_req->Chunks[0];
6938 chunk_count = le32_to_cpu(ci_req->ChunkCount);
6939 total_size_written = 0;
6940
6941 /* verify the SRV_COPYCHUNK_COPY packet */
6942 if (chunk_count > ksmbd_server_side_copy_max_chunk_count() ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006943 le32_to_cpu(req->InputCount) <
6944 offsetof(struct copychunk_ioctl_req, Chunks) +
6945 chunk_count * sizeof(struct srv_copychunk)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006946 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6947 return -EINVAL;
6948 }
6949
6950 for (i = 0; i < chunk_count; i++) {
6951 if (le32_to_cpu(chunks[i].Length) == 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006952 le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size())
Namjae Jeone2f34482021-03-16 10:49:09 +09006953 break;
6954 total_size_written += le32_to_cpu(chunks[i].Length);
6955 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09006956
6957 if (i < chunk_count ||
6958 total_size_written > ksmbd_server_side_copy_max_total_size()) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006959 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6960 return -EINVAL;
6961 }
6962
6963 src_fp = ksmbd_lookup_foreign_fd(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006964 le64_to_cpu(ci_req->ResumeKey[0]));
Namjae Jeone2f34482021-03-16 10:49:09 +09006965 dst_fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006966 le64_to_cpu(req->VolatileFileId),
6967 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006968 ret = -EINVAL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006969 if (!src_fp ||
6970 src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006971 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
6972 goto out;
6973 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09006974
Namjae Jeone2f34482021-03-16 10:49:09 +09006975 if (!dst_fp) {
6976 rsp->hdr.Status = STATUS_FILE_CLOSED;
6977 goto out;
6978 }
6979
6980 /*
6981 * FILE_READ_DATA should only be included in
6982 * the FSCTL_COPYCHUNK case
6983 */
Namjae Jeon070fb212021-05-26 17:57:12 +09006984 if (cnt_code == FSCTL_COPYCHUNK &&
6985 !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006986 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6987 goto out;
6988 }
6989
6990 ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09006991 chunks, chunk_count,
6992 &chunk_count_written,
6993 &chunk_size_written,
6994 &total_size_written);
Namjae Jeone2f34482021-03-16 10:49:09 +09006995 if (ret < 0) {
6996 if (ret == -EACCES)
6997 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6998 if (ret == -EAGAIN)
6999 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
7000 else if (ret == -EBADF)
7001 rsp->hdr.Status = STATUS_INVALID_HANDLE;
7002 else if (ret == -EFBIG || ret == -ENOSPC)
7003 rsp->hdr.Status = STATUS_DISK_FULL;
7004 else if (ret == -EINVAL)
7005 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7006 else if (ret == -EISDIR)
7007 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
7008 else if (ret == -E2BIG)
7009 rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE;
7010 else
7011 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
7012 }
7013
7014 ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written);
7015 ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written);
7016 ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written);
7017out:
7018 ksmbd_fd_put(work, src_fp);
7019 ksmbd_fd_put(work, dst_fp);
7020 return ret;
7021}
7022
7023static __be32 idev_ipv4_address(struct in_device *idev)
7024{
7025 __be32 addr = 0;
7026
7027 struct in_ifaddr *ifa;
7028
7029 rcu_read_lock();
7030 in_dev_for_each_ifa_rcu(ifa, idev) {
7031 if (ifa->ifa_flags & IFA_F_SECONDARY)
7032 continue;
7033
7034 addr = ifa->ifa_address;
7035 break;
7036 }
7037 rcu_read_unlock();
7038 return addr;
7039}
7040
7041static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09007042 struct smb2_ioctl_req *req,
7043 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007044{
7045 struct network_interface_info_ioctl_rsp *nii_rsp = NULL;
7046 int nbytes = 0;
7047 struct net_device *netdev;
7048 struct sockaddr_storage_rsp *sockaddr_storage;
7049 unsigned int flags;
7050 unsigned long long speed;
Namjae Jeonf1abdb72021-07-27 13:25:13 +09007051 struct sockaddr_in6 *csin6 = (struct sockaddr_in6 *)&conn->peer_addr;
Namjae Jeone2f34482021-03-16 10:49:09 +09007052
7053 rtnl_lock();
7054 for_each_netdev(&init_net, netdev) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007055 if (netdev->type == ARPHRD_LOOPBACK)
7056 continue;
7057
7058 flags = dev_get_flags(netdev);
7059 if (!(flags & IFF_RUNNING))
7060 continue;
7061
7062 nii_rsp = (struct network_interface_info_ioctl_rsp *)
7063 &rsp->Buffer[nbytes];
7064 nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex);
7065
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09007066 nii_rsp->Capability = 0;
Hyunchul Lee03d8d4f2021-07-13 16:09:34 +09007067 if (ksmbd_rdma_capable_netdev(netdev))
7068 nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE);
Namjae Jeone2f34482021-03-16 10:49:09 +09007069
7070 nii_rsp->Next = cpu_to_le32(152);
7071 nii_rsp->Reserved = 0;
7072
7073 if (netdev->ethtool_ops->get_link_ksettings) {
7074 struct ethtool_link_ksettings cmd;
7075
7076 netdev->ethtool_ops->get_link_ksettings(netdev, &cmd);
7077 speed = cmd.base.speed;
7078 } else {
Per Forlind4758662021-08-30 13:23:04 +09007079 ksmbd_debug(SMB, "%s %s\n", netdev->name,
7080 "speed is unknown, defaulting to 1Gb/sec");
Namjae Jeone2f34482021-03-16 10:49:09 +09007081 speed = SPEED_1000;
7082 }
7083
7084 speed *= 1000000;
7085 nii_rsp->LinkSpeed = cpu_to_le64(speed);
7086
7087 sockaddr_storage = (struct sockaddr_storage_rsp *)
7088 nii_rsp->SockAddr_Storage;
7089 memset(sockaddr_storage, 0, 128);
7090
Namjae Jeonf1abdb72021-07-27 13:25:13 +09007091 if (conn->peer_addr.ss_family == PF_INET ||
7092 ipv6_addr_v4mapped(&csin6->sin6_addr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007093 struct in_device *idev;
7094
7095 sockaddr_storage->Family = cpu_to_le16(INTERNETWORK);
7096 sockaddr_storage->addr4.Port = 0;
7097
7098 idev = __in_dev_get_rtnl(netdev);
7099 if (!idev)
7100 continue;
7101 sockaddr_storage->addr4.IPv4address =
7102 idev_ipv4_address(idev);
7103 } else {
7104 struct inet6_dev *idev6;
7105 struct inet6_ifaddr *ifa;
7106 __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address;
7107
7108 sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6);
7109 sockaddr_storage->addr6.Port = 0;
7110 sockaddr_storage->addr6.FlowInfo = 0;
7111
7112 idev6 = __in6_dev_get(netdev);
7113 if (!idev6)
7114 continue;
7115
7116 list_for_each_entry(ifa, &idev6->addr_list, if_list) {
7117 if (ifa->flags & (IFA_F_TENTATIVE |
7118 IFA_F_DEPRECATED))
7119 continue;
7120 memcpy(ipv6_addr, ifa->addr.s6_addr, 16);
7121 break;
7122 }
7123 sockaddr_storage->addr6.ScopeId = 0;
7124 }
7125
7126 nbytes += sizeof(struct network_interface_info_ioctl_rsp);
7127 }
7128 rtnl_unlock();
7129
7130 /* zero if this is last one */
7131 if (nii_rsp)
7132 nii_rsp->Next = 0;
7133
7134 if (!nbytes) {
7135 rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL;
7136 return -EINVAL;
7137 }
7138
7139 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7140 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7141 return nbytes;
7142}
7143
Namjae Jeone2f34482021-03-16 10:49:09 +09007144static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09007145 struct validate_negotiate_info_req *neg_req,
7146 struct validate_negotiate_info_rsp *neg_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007147{
7148 int ret = 0;
7149 int dialect;
7150
7151 dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects,
Namjae Jeon070fb212021-05-26 17:57:12 +09007152 neg_req->DialectCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09007153 if (dialect == BAD_PROT_ID || dialect != conn->dialect) {
7154 ret = -EINVAL;
7155 goto err_out;
7156 }
7157
7158 if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) {
7159 ret = -EINVAL;
7160 goto err_out;
7161 }
7162
7163 if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) {
7164 ret = -EINVAL;
7165 goto err_out;
7166 }
7167
7168 if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) {
7169 ret = -EINVAL;
7170 goto err_out;
7171 }
7172
7173 neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
7174 memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE);
7175 neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode);
7176 neg_rsp->Dialect = cpu_to_le16(conn->dialect);
7177err_out:
7178 return ret;
7179}
7180
Namjae Jeon64b39f42021-03-30 14:25:35 +09007181static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007182 struct file_allocated_range_buffer *qar_req,
7183 struct file_allocated_range_buffer *qar_rsp,
7184 int in_count, int *out_count)
Namjae Jeone2f34482021-03-16 10:49:09 +09007185{
7186 struct ksmbd_file *fp;
7187 loff_t start, length;
7188 int ret = 0;
7189
7190 *out_count = 0;
7191 if (in_count == 0)
7192 return -EINVAL;
7193
7194 fp = ksmbd_lookup_fd_fast(work, id);
7195 if (!fp)
7196 return -ENOENT;
7197
7198 start = le64_to_cpu(qar_req->file_offset);
7199 length = le64_to_cpu(qar_req->length);
7200
7201 ret = ksmbd_vfs_fqar_lseek(fp, start, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09007202 qar_rsp, in_count, out_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09007203 if (ret && ret != -E2BIG)
7204 *out_count = 0;
7205
7206 ksmbd_fd_put(work, fp);
7207 return ret;
7208}
7209
Namjae Jeon64b39f42021-03-30 14:25:35 +09007210static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007211 int out_buf_len, struct smb2_ioctl_req *req,
7212 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007213{
7214 struct ksmbd_rpc_command *rpc_resp;
7215 char *data_buf = (char *)&req->Buffer[0];
7216 int nbytes = 0;
7217
Namjae Jeon64b39f42021-03-30 14:25:35 +09007218 rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09007219 le32_to_cpu(req->InputCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09007220 if (rpc_resp) {
7221 if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) {
7222 /*
7223 * set STATUS_SOME_NOT_MAPPED response
7224 * for unknown domain sid.
7225 */
7226 rsp->hdr.Status = STATUS_SOME_NOT_MAPPED;
7227 } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
7228 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7229 goto out;
7230 } else if (rpc_resp->flags != KSMBD_RPC_OK) {
7231 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7232 goto out;
7233 }
7234
7235 nbytes = rpc_resp->payload_sz;
7236 if (rpc_resp->payload_sz > out_buf_len) {
7237 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7238 nbytes = out_buf_len;
7239 }
7240
7241 if (!rpc_resp->payload_sz) {
7242 rsp->hdr.Status =
7243 STATUS_UNEXPECTED_IO_ERROR;
7244 goto out;
7245 }
7246
7247 memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes);
7248 }
7249out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09007250 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007251 return nbytes;
7252}
7253
Namjae Jeon64b39f42021-03-30 14:25:35 +09007254static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007255 struct file_sparse *sparse)
Namjae Jeone2f34482021-03-16 10:49:09 +09007256{
7257 struct ksmbd_file *fp;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007258 struct user_namespace *user_ns;
Namjae Jeone2f34482021-03-16 10:49:09 +09007259 int ret = 0;
7260 __le32 old_fattr;
7261
7262 fp = ksmbd_lookup_fd_fast(work, id);
7263 if (!fp)
7264 return -ENOENT;
Hyunchul Lee465d7202021-07-03 12:10:36 +09007265 user_ns = file_mnt_user_ns(fp->filp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007266
7267 old_fattr = fp->f_ci->m_fattr;
7268 if (sparse->SetSparse)
7269 fp->f_ci->m_fattr |= ATTR_SPARSE_FILE_LE;
7270 else
7271 fp->f_ci->m_fattr &= ~ATTR_SPARSE_FILE_LE;
7272
7273 if (fp->f_ci->m_fattr != old_fattr &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007274 test_share_config_flag(work->tcon->share_conf,
7275 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007276 struct xattr_dos_attrib da;
7277
Hyunchul Lee465d7202021-07-03 12:10:36 +09007278 ret = ksmbd_vfs_get_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007279 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007280 if (ret <= 0)
7281 goto out;
7282
7283 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
Hyunchul Lee465d7202021-07-03 12:10:36 +09007284 ret = ksmbd_vfs_set_dos_attrib_xattr(user_ns,
Hyunchul Leeaf349832021-06-30 18:25:53 +09007285 fp->filp->f_path.dentry, &da);
Namjae Jeone2f34482021-03-16 10:49:09 +09007286 if (ret)
7287 fp->f_ci->m_fattr = old_fattr;
7288 }
7289
7290out:
7291 ksmbd_fd_put(work, fp);
7292 return ret;
7293}
7294
7295static int fsctl_request_resume_key(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007296 struct smb2_ioctl_req *req,
7297 struct resume_key_ioctl_rsp *key_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007298{
7299 struct ksmbd_file *fp;
7300
7301 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007302 le64_to_cpu(req->VolatileFileId),
7303 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09007304 if (!fp)
7305 return -ENOENT;
7306
7307 memset(key_rsp, 0, sizeof(*key_rsp));
7308 key_rsp->ResumeKey[0] = req->VolatileFileId;
7309 key_rsp->ResumeKey[1] = req->PersistentFileId;
7310 ksmbd_fd_put(work, fp);
7311
7312 return 0;
7313}
7314
7315/**
7316 * smb2_ioctl() - handler for smb2 ioctl command
7317 * @work: smb work containing ioctl command buffer
7318 *
7319 * Return: 0 on success, otherwise error
7320 */
7321int smb2_ioctl(struct ksmbd_work *work)
7322{
7323 struct smb2_ioctl_req *req;
7324 struct smb2_ioctl_rsp *rsp, *rsp_org;
7325 int cnt_code, nbytes = 0;
7326 int out_buf_len;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007327 u64 id = KSMBD_NO_FID;
Namjae Jeone2f34482021-03-16 10:49:09 +09007328 struct ksmbd_conn *conn = work->conn;
7329 int ret = 0;
7330
Namjae Jeone5066492021-03-30 12:35:23 +09007331 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007332 if (work->next_smb2_rcv_hdr_off) {
Namjae Jeon8a893312021-06-25 13:43:37 +09007333 req = ksmbd_req_buf_next(work);
7334 rsp = ksmbd_resp_buf_next(work);
Namjae Jeon38673692021-07-08 12:32:27 +09007335 if (!has_file_id(le64_to_cpu(req->VolatileFileId))) {
7336 ksmbd_debug(SMB, "Compound request set FID = %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007337 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09007338 id = work->compound_fid;
7339 }
7340 } else {
Namjae Jeone5066492021-03-30 12:35:23 +09007341 req = work->request_buf;
7342 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007343 }
7344
Namjae Jeon38673692021-07-08 12:32:27 +09007345 if (!has_file_id(id))
Namjae Jeone2f34482021-03-16 10:49:09 +09007346 id = le64_to_cpu(req->VolatileFileId);
7347
7348 if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) {
7349 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7350 goto out;
7351 }
7352
7353 cnt_code = le32_to_cpu(req->CntCode);
7354 out_buf_len = le32_to_cpu(req->MaxOutputResponse);
7355 out_buf_len = min(KSMBD_IPC_MAX_PAYLOAD, out_buf_len);
7356
7357 switch (cnt_code) {
7358 case FSCTL_DFS_GET_REFERRALS:
7359 case FSCTL_DFS_GET_REFERRALS_EX:
7360 /* Not support DFS yet */
7361 rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED;
7362 goto out;
7363 case FSCTL_CREATE_OR_GET_OBJECT_ID:
7364 {
7365 struct file_object_buf_type1_ioctl_rsp *obj_buf;
7366
7367 nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp);
7368 obj_buf = (struct file_object_buf_type1_ioctl_rsp *)
7369 &rsp->Buffer[0];
7370
7371 /*
7372 * TODO: This is dummy implementation to pass smbtorture
7373 * Need to check correct response later
7374 */
7375 memset(obj_buf->ObjectId, 0x0, 16);
7376 memset(obj_buf->BirthVolumeId, 0x0, 16);
7377 memset(obj_buf->BirthObjectId, 0x0, 16);
7378 memset(obj_buf->DomainId, 0x0, 16);
7379
7380 break;
7381 }
7382 case FSCTL_PIPE_TRANSCEIVE:
7383 nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp);
7384 break;
7385 case FSCTL_VALIDATE_NEGOTIATE_INFO:
7386 if (conn->dialect < SMB30_PROT_ID) {
7387 ret = -EOPNOTSUPP;
7388 goto out;
7389 }
7390
7391 ret = fsctl_validate_negotiate_info(conn,
7392 (struct validate_negotiate_info_req *)&req->Buffer[0],
7393 (struct validate_negotiate_info_rsp *)&rsp->Buffer[0]);
7394 if (ret < 0)
7395 goto out;
7396
7397 nbytes = sizeof(struct validate_negotiate_info_rsp);
7398 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7399 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7400 break;
7401 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
7402 nbytes = fsctl_query_iface_info_ioctl(conn, req, rsp);
7403 if (nbytes < 0)
7404 goto out;
7405 break;
7406 case FSCTL_REQUEST_RESUME_KEY:
7407 if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) {
7408 ret = -EINVAL;
7409 goto out;
7410 }
7411
7412 ret = fsctl_request_resume_key(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007413 (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007414 if (ret < 0)
7415 goto out;
7416 rsp->PersistentFileId = req->PersistentFileId;
7417 rsp->VolatileFileId = req->VolatileFileId;
7418 nbytes = sizeof(struct resume_key_ioctl_rsp);
7419 break;
7420 case FSCTL_COPYCHUNK:
7421 case FSCTL_COPYCHUNK_WRITE:
Namjae Jeon64b39f42021-03-30 14:25:35 +09007422 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007423 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007424 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007425 ret = -EACCES;
7426 goto out;
7427 }
7428
7429 if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) {
7430 ret = -EINVAL;
7431 goto out;
7432 }
7433
7434 nbytes = sizeof(struct copychunk_ioctl_rsp);
7435 fsctl_copychunk(work, req, rsp);
7436 break;
7437 case FSCTL_SET_SPARSE:
7438 ret = fsctl_set_sparse(work, id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007439 (struct file_sparse *)&req->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007440 if (ret < 0)
7441 goto out;
7442 break;
7443 case FSCTL_SET_ZERO_DATA:
7444 {
7445 struct file_zero_data_information *zero_data;
7446 struct ksmbd_file *fp;
7447 loff_t off, len;
7448
Namjae Jeon64b39f42021-03-30 14:25:35 +09007449 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007450 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007451 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007452 ret = -EACCES;
7453 goto out;
7454 }
7455
7456 zero_data =
7457 (struct file_zero_data_information *)&req->Buffer[0];
7458
7459 fp = ksmbd_lookup_fd_fast(work, id);
7460 if (!fp) {
7461 ret = -ENOENT;
7462 goto out;
7463 }
7464
7465 off = le64_to_cpu(zero_data->FileOffset);
7466 len = le64_to_cpu(zero_data->BeyondFinalZero) - off;
7467
7468 ret = ksmbd_vfs_zero_data(work, fp, off, len);
7469 ksmbd_fd_put(work, fp);
7470 if (ret < 0)
7471 goto out;
7472 break;
7473 }
7474 case FSCTL_QUERY_ALLOCATED_RANGES:
7475 ret = fsctl_query_allocated_ranges(work, id,
7476 (struct file_allocated_range_buffer *)&req->Buffer[0],
7477 (struct file_allocated_range_buffer *)&rsp->Buffer[0],
7478 out_buf_len /
7479 sizeof(struct file_allocated_range_buffer), &nbytes);
7480 if (ret == -E2BIG) {
7481 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7482 } else if (ret < 0) {
7483 nbytes = 0;
7484 goto out;
7485 }
7486
7487 nbytes *= sizeof(struct file_allocated_range_buffer);
7488 break;
7489 case FSCTL_GET_REPARSE_POINT:
7490 {
7491 struct reparse_data_buffer *reparse_ptr;
7492 struct ksmbd_file *fp;
7493
7494 reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0];
7495 fp = ksmbd_lookup_fd_fast(work, id);
7496 if (!fp) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007497 pr_err("not found fp!!\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007498 ret = -ENOENT;
7499 goto out;
7500 }
7501
7502 reparse_ptr->ReparseTag =
Namjae Jeonab0b2632021-06-29 09:20:13 +09007503 smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode);
Namjae Jeone2f34482021-03-16 10:49:09 +09007504 reparse_ptr->ReparseDataLength = 0;
7505 ksmbd_fd_put(work, fp);
7506 nbytes = sizeof(struct reparse_data_buffer);
7507 break;
7508 }
Namjae Jeoneb817362021-05-18 10:37:59 +09007509 case FSCTL_DUPLICATE_EXTENTS_TO_FILE:
7510 {
7511 struct ksmbd_file *fp_in, *fp_out = NULL;
7512 struct duplicate_extents_to_file *dup_ext;
7513 loff_t src_off, dst_off, length, cloned;
7514
7515 dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0];
7516
7517 fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle,
Namjae Jeon070fb212021-05-26 17:57:12 +09007518 dup_ext->PersistentFileHandle);
Namjae Jeoneb817362021-05-18 10:37:59 +09007519 if (!fp_in) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007520 pr_err("not found file handle in duplicate extent to file\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007521 ret = -ENOENT;
7522 goto out;
7523 }
7524
7525 fp_out = ksmbd_lookup_fd_fast(work, id);
7526 if (!fp_out) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007527 pr_err("not found fp\n");
Namjae Jeoneb817362021-05-18 10:37:59 +09007528 ret = -ENOENT;
7529 goto dup_ext_out;
7530 }
7531
7532 src_off = le64_to_cpu(dup_ext->SourceFileOffset);
7533 dst_off = le64_to_cpu(dup_ext->TargetFileOffset);
7534 length = le64_to_cpu(dup_ext->ByteCount);
7535 cloned = vfs_clone_file_range(fp_in->filp, src_off, fp_out->filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007536 dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007537 if (cloned == -EXDEV || cloned == -EOPNOTSUPP) {
7538 ret = -EOPNOTSUPP;
7539 goto dup_ext_out;
7540 } else if (cloned != length) {
Namjae Jeonf8524772021-06-18 10:28:00 +09007541 cloned = vfs_copy_file_range(fp_in->filp, src_off,
7542 fp_out->filp, dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007543 if (cloned != length) {
7544 if (cloned < 0)
7545 ret = cloned;
7546 else
7547 ret = -EINVAL;
7548 }
7549 }
7550
7551dup_ext_out:
7552 ksmbd_fd_put(work, fp_in);
7553 ksmbd_fd_put(work, fp_out);
7554 if (ret < 0)
7555 goto out;
7556 break;
7557 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007558 default:
7559 ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007560 cnt_code);
Namjae Jeone2f34482021-03-16 10:49:09 +09007561 ret = -EOPNOTSUPP;
7562 goto out;
7563 }
7564
7565 rsp->CntCode = cpu_to_le32(cnt_code);
7566 rsp->InputCount = cpu_to_le32(0);
7567 rsp->InputOffset = cpu_to_le32(112);
7568 rsp->OutputOffset = cpu_to_le32(112);
7569 rsp->OutputCount = cpu_to_le32(nbytes);
7570 rsp->StructureSize = cpu_to_le16(49);
7571 rsp->Reserved = cpu_to_le16(0);
7572 rsp->Flags = cpu_to_le32(0);
7573 rsp->Reserved2 = cpu_to_le32(0);
7574 inc_rfc1001_len(rsp_org, 48 + nbytes);
7575
7576 return 0;
7577
7578out:
7579 if (ret == -EACCES)
7580 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7581 else if (ret == -ENOENT)
7582 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7583 else if (ret == -EOPNOTSUPP)
7584 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7585 else if (ret < 0 || rsp->hdr.Status == 0)
7586 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7587 smb2_set_err_rsp(work);
7588 return 0;
7589}
7590
7591/**
7592 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command
7593 * @work: smb work containing oplock break command buffer
7594 *
7595 * Return: 0
7596 */
7597static void smb20_oplock_break_ack(struct ksmbd_work *work)
7598{
Namjae Jeone5066492021-03-30 12:35:23 +09007599 struct smb2_oplock_break *req = work->request_buf;
7600 struct smb2_oplock_break *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007601 struct ksmbd_file *fp;
7602 struct oplock_info *opinfo = NULL;
7603 __le32 err = 0;
7604 int ret = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007605 u64 volatile_id, persistent_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09007606 char req_oplevel = 0, rsp_oplevel = 0;
7607 unsigned int oplock_change_type;
7608
7609 volatile_id = le64_to_cpu(req->VolatileFid);
7610 persistent_id = le64_to_cpu(req->PersistentFid);
7611 req_oplevel = req->OplockLevel;
7612 ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n",
7613 volatile_id, persistent_id, req_oplevel);
7614
7615 fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
7616 if (!fp) {
7617 rsp->hdr.Status = STATUS_FILE_CLOSED;
7618 smb2_set_err_rsp(work);
7619 return;
7620 }
7621
7622 opinfo = opinfo_get(fp);
7623 if (!opinfo) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007624 pr_err("unexpected null oplock_info\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007625 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7626 smb2_set_err_rsp(work);
7627 ksmbd_fd_put(work, fp);
7628 return;
7629 }
7630
7631 if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) {
7632 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7633 goto err_out;
7634 }
7635
7636 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7637 ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state);
7638 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7639 goto err_out;
7640 }
7641
Namjae Jeon64b39f42021-03-30 14:25:35 +09007642 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7643 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7644 (req_oplevel != SMB2_OPLOCK_LEVEL_II &&
7645 req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007646 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7647 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007648 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7649 req_oplevel != SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007650 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7651 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007652 } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II ||
7653 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007654 err = STATUS_INVALID_DEVICE_STATE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007655 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7656 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7657 req_oplevel == SMB2_OPLOCK_LEVEL_II) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007658 oplock_change_type = OPLOCK_WRITE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007659 } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7660 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7661 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007662 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007663 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7664 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007665 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007666 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007667 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007668 }
7669 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007670 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007671 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007672
7673 switch (oplock_change_type) {
7674 case OPLOCK_WRITE_TO_READ:
7675 ret = opinfo_write_to_read(opinfo);
7676 rsp_oplevel = SMB2_OPLOCK_LEVEL_II;
7677 break;
7678 case OPLOCK_WRITE_TO_NONE:
7679 ret = opinfo_write_to_none(opinfo);
7680 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7681 break;
7682 case OPLOCK_READ_TO_NONE:
7683 ret = opinfo_read_to_none(opinfo);
7684 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7685 break;
7686 default:
Namjae Jeonbde16942021-06-28 15:23:19 +09007687 pr_err("unknown oplock change 0x%x -> 0x%x\n",
7688 opinfo->level, rsp_oplevel);
Namjae Jeone2f34482021-03-16 10:49:09 +09007689 }
7690
7691 if (ret < 0) {
7692 rsp->hdr.Status = err;
7693 goto err_out;
7694 }
7695
7696 opinfo_put(opinfo);
7697 ksmbd_fd_put(work, fp);
7698 opinfo->op_state = OPLOCK_STATE_NONE;
7699 wake_up_interruptible_all(&opinfo->oplock_q);
7700
7701 rsp->StructureSize = cpu_to_le16(24);
7702 rsp->OplockLevel = rsp_oplevel;
7703 rsp->Reserved = 0;
7704 rsp->Reserved2 = 0;
7705 rsp->VolatileFid = cpu_to_le64(volatile_id);
7706 rsp->PersistentFid = cpu_to_le64(persistent_id);
7707 inc_rfc1001_len(rsp, 24);
7708 return;
7709
7710err_out:
7711 opinfo->op_state = OPLOCK_STATE_NONE;
7712 wake_up_interruptible_all(&opinfo->oplock_q);
7713
7714 opinfo_put(opinfo);
7715 ksmbd_fd_put(work, fp);
7716 smb2_set_err_rsp(work);
7717}
7718
7719static int check_lease_state(struct lease *lease, __le32 req_state)
7720{
7721 if ((lease->new_state ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09007722 (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) &&
7723 !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007724 lease->new_state = req_state;
7725 return 0;
7726 }
7727
7728 if (lease->new_state == req_state)
7729 return 0;
7730
7731 return 1;
7732}
7733
7734/**
7735 * smb21_lease_break_ack() - handler for smb2.1 lease break command
7736 * @work: smb work containing lease break command buffer
7737 *
7738 * Return: 0
7739 */
7740static void smb21_lease_break_ack(struct ksmbd_work *work)
7741{
7742 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09007743 struct smb2_lease_ack *req = work->request_buf;
7744 struct smb2_lease_ack *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007745 struct oplock_info *opinfo;
7746 __le32 err = 0;
7747 int ret = 0;
7748 unsigned int lease_change_type;
7749 __le32 lease_state;
7750 struct lease *lease;
7751
7752 ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007753 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007754 opinfo = lookup_lease_in_table(conn, req->LeaseKey);
7755 if (!opinfo) {
7756 ksmbd_debug(OPLOCK, "file not opened\n");
7757 smb2_set_err_rsp(work);
7758 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7759 return;
7760 }
7761 lease = opinfo->o_lease;
7762
7763 if (opinfo->op_state == OPLOCK_STATE_NONE) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007764 pr_err("unexpected lease break state 0x%x\n",
7765 opinfo->op_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09007766 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7767 goto err_out;
7768 }
7769
7770 if (check_lease_state(lease, req->LeaseState)) {
7771 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
7772 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +09007773 "req lease state: 0x%x, expected state: 0x%x\n",
7774 req->LeaseState, lease->new_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09007775 goto err_out;
7776 }
7777
7778 if (!atomic_read(&opinfo->breaking_cnt)) {
7779 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7780 goto err_out;
7781 }
7782
7783 /* check for bad lease state */
Namjae Jeon070fb212021-05-26 17:57:12 +09007784 if (req->LeaseState &
7785 (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007786 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7787 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7788 lease_change_type = OPLOCK_WRITE_TO_NONE;
7789 else
7790 lease_change_type = OPLOCK_READ_TO_NONE;
7791 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007792 le32_to_cpu(lease->state),
7793 le32_to_cpu(req->LeaseState));
Namjae Jeon64b39f42021-03-30 14:25:35 +09007794 } else if (lease->state == SMB2_LEASE_READ_CACHING_LE &&
7795 req->LeaseState != SMB2_LEASE_NONE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007796 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7797 lease_change_type = OPLOCK_READ_TO_NONE;
7798 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007799 le32_to_cpu(lease->state),
7800 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007801 } else {
7802 /* valid lease state changes */
7803 err = STATUS_INVALID_DEVICE_STATE;
7804 if (req->LeaseState == SMB2_LEASE_NONE_LE) {
7805 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7806 lease_change_type = OPLOCK_WRITE_TO_NONE;
7807 else
7808 lease_change_type = OPLOCK_READ_TO_NONE;
7809 } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) {
7810 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7811 lease_change_type = OPLOCK_WRITE_TO_READ;
7812 else
7813 lease_change_type = OPLOCK_READ_HANDLE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007814 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007815 lease_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007816 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007817 }
7818
7819 switch (lease_change_type) {
7820 case OPLOCK_WRITE_TO_READ:
7821 ret = opinfo_write_to_read(opinfo);
7822 break;
7823 case OPLOCK_READ_HANDLE_TO_READ:
7824 ret = opinfo_read_handle_to_read(opinfo);
7825 break;
7826 case OPLOCK_WRITE_TO_NONE:
7827 ret = opinfo_write_to_none(opinfo);
7828 break;
7829 case OPLOCK_READ_TO_NONE:
7830 ret = opinfo_read_to_none(opinfo);
7831 break;
7832 default:
7833 ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007834 le32_to_cpu(lease->state),
7835 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007836 }
7837
7838 lease_state = lease->state;
7839 opinfo->op_state = OPLOCK_STATE_NONE;
7840 wake_up_interruptible_all(&opinfo->oplock_q);
7841 atomic_dec(&opinfo->breaking_cnt);
7842 wake_up_interruptible_all(&opinfo->oplock_brk);
7843 opinfo_put(opinfo);
7844
7845 if (ret < 0) {
7846 rsp->hdr.Status = err;
7847 goto err_out;
7848 }
7849
7850 rsp->StructureSize = cpu_to_le16(36);
7851 rsp->Reserved = 0;
7852 rsp->Flags = 0;
7853 memcpy(rsp->LeaseKey, req->LeaseKey, 16);
7854 rsp->LeaseState = lease_state;
7855 rsp->LeaseDuration = 0;
7856 inc_rfc1001_len(rsp, 36);
7857 return;
7858
7859err_out:
7860 opinfo->op_state = OPLOCK_STATE_NONE;
7861 wake_up_interruptible_all(&opinfo->oplock_q);
7862 atomic_dec(&opinfo->breaking_cnt);
7863 wake_up_interruptible_all(&opinfo->oplock_brk);
7864
7865 opinfo_put(opinfo);
7866 smb2_set_err_rsp(work);
7867}
7868
7869/**
7870 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break
7871 * @work: smb work containing oplock/lease break command buffer
7872 *
7873 * Return: 0
7874 */
7875int smb2_oplock_break(struct ksmbd_work *work)
7876{
Namjae Jeone5066492021-03-30 12:35:23 +09007877 struct smb2_oplock_break *req = work->request_buf;
7878 struct smb2_oplock_break *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007879
7880 switch (le16_to_cpu(req->StructureSize)) {
7881 case OP_BREAK_STRUCT_SIZE_20:
7882 smb20_oplock_break_ack(work);
7883 break;
7884 case OP_BREAK_STRUCT_SIZE_21:
7885 smb21_lease_break_ack(work);
7886 break;
7887 default:
7888 ksmbd_debug(OPLOCK, "invalid break cmd %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007889 le16_to_cpu(req->StructureSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09007890 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7891 smb2_set_err_rsp(work);
7892 }
7893
7894 return 0;
7895}
7896
7897/**
7898 * smb2_notify() - handler for smb2 notify request
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09007899 * @work: smb work containing notify command buffer
Namjae Jeone2f34482021-03-16 10:49:09 +09007900 *
7901 * Return: 0
7902 */
7903int smb2_notify(struct ksmbd_work *work)
7904{
7905 struct smb2_notify_req *req;
7906 struct smb2_notify_rsp *rsp;
7907
7908 WORK_BUFFERS(work, req, rsp);
7909
7910 if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) {
7911 rsp->hdr.Status = STATUS_INTERNAL_ERROR;
7912 smb2_set_err_rsp(work);
7913 return 0;
7914 }
7915
7916 smb2_set_err_rsp(work);
7917 rsp->hdr.Status = STATUS_NOT_IMPLEMENTED;
7918 return 0;
7919}
7920
7921/**
7922 * smb2_is_sign_req() - handler for checking packet signing status
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09007923 * @work: smb work containing notify command buffer
7924 * @command: SMB2 command id
Namjae Jeone2f34482021-03-16 10:49:09 +09007925 *
7926 * Return: true if packed is signed, false otherwise
7927 */
7928bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command)
7929{
Namjae Jeone5066492021-03-30 12:35:23 +09007930 struct smb2_hdr *rcv_hdr2 = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007931
7932 if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007933 command != SMB2_NEGOTIATE_HE &&
7934 command != SMB2_SESSION_SETUP_HE &&
7935 command != SMB2_OPLOCK_BREAK_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +09007936 return true;
7937
kernel test robot56160152021-05-12 09:24:37 +09007938 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +09007939}
7940
7941/**
7942 * smb2_check_sign_req() - handler for req packet sign processing
7943 * @work: smb work containing notify command buffer
7944 *
7945 * Return: 1 on success, 0 otherwise
7946 */
7947int smb2_check_sign_req(struct ksmbd_work *work)
7948{
7949 struct smb2_hdr *hdr, *hdr_org;
7950 char signature_req[SMB2_SIGNATURE_SIZE];
7951 char signature[SMB2_HMACSHA256_SIZE];
7952 struct kvec iov[1];
7953 size_t len;
7954
Namjae Jeone5066492021-03-30 12:35:23 +09007955 hdr_org = hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007956 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09007957 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09007958
7959 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
7960 len = be32_to_cpu(hdr_org->smb2_buf_length);
7961 else if (hdr->NextCommand)
7962 len = le32_to_cpu(hdr->NextCommand);
7963 else
7964 len = be32_to_cpu(hdr_org->smb2_buf_length) -
7965 work->next_smb2_rcv_hdr_off;
7966
7967 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
7968 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7969
7970 iov[0].iov_base = (char *)&hdr->ProtocolId;
7971 iov[0].iov_len = len;
7972
7973 if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1,
Namjae Jeon64b39f42021-03-30 14:25:35 +09007974 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09007975 return 0;
7976
7977 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09007978 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007979 return 0;
7980 }
7981
7982 return 1;
7983}
7984
7985/**
7986 * smb2_set_sign_rsp() - handler for rsp packet sign processing
7987 * @work: smb work containing notify command buffer
7988 *
7989 */
7990void smb2_set_sign_rsp(struct ksmbd_work *work)
7991{
7992 struct smb2_hdr *hdr, *hdr_org;
7993 struct smb2_hdr *req_hdr;
7994 char signature[SMB2_HMACSHA256_SIZE];
7995 struct kvec iov[2];
7996 size_t len;
7997 int n_vec = 1;
7998
Namjae Jeone5066492021-03-30 12:35:23 +09007999 hdr_org = hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008000 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008001 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008002
Namjae Jeon8a893312021-06-25 13:43:37 +09008003 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008004
8005 if (!work->next_smb2_rsp_hdr_off) {
8006 len = get_rfc1002_len(hdr_org);
8007 if (req_hdr->NextCommand)
8008 len = ALIGN(len, 8);
8009 } else {
8010 len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off;
8011 len = ALIGN(len, 8);
8012 }
8013
8014 if (req_hdr->NextCommand)
8015 hdr->NextCommand = cpu_to_le32(len);
8016
8017 hdr->Flags |= SMB2_FLAGS_SIGNED;
8018 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8019
8020 iov[0].iov_base = (char *)&hdr->ProtocolId;
8021 iov[0].iov_len = len;
8022
Namjae Jeone5066492021-03-30 12:35:23 +09008023 if (work->aux_payload_sz) {
8024 iov[0].iov_len -= work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008025
Namjae Jeone5066492021-03-30 12:35:23 +09008026 iov[1].iov_base = work->aux_payload_buf;
8027 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008028 n_vec++;
8029 }
8030
8031 if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec,
Namjae Jeon64b39f42021-03-30 14:25:35 +09008032 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09008033 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8034}
8035
8036/**
8037 * smb3_check_sign_req() - handler for req packet sign processing
8038 * @work: smb work containing notify command buffer
8039 *
8040 * Return: 1 on success, 0 otherwise
8041 */
8042int smb3_check_sign_req(struct ksmbd_work *work)
8043{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008044 struct ksmbd_conn *conn = work->conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09008045 char *signing_key;
8046 struct smb2_hdr *hdr, *hdr_org;
8047 struct channel *chann;
8048 char signature_req[SMB2_SIGNATURE_SIZE];
8049 char signature[SMB2_CMACAES_SIZE];
8050 struct kvec iov[1];
8051 size_t len;
8052
Namjae Jeone5066492021-03-30 12:35:23 +09008053 hdr_org = hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008054 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008055 hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008056
8057 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
8058 len = be32_to_cpu(hdr_org->smb2_buf_length);
8059 else if (hdr->NextCommand)
8060 len = le32_to_cpu(hdr->NextCommand);
8061 else
8062 len = be32_to_cpu(hdr_org->smb2_buf_length) -
8063 work->next_smb2_rcv_hdr_off;
8064
8065 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
8066 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008067 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008068 chann = lookup_chann_list(work->sess, conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008069 if (!chann)
8070 return 0;
8071 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008072 }
8073
8074 if (!signing_key) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008075 pr_err("SMB3 signing key is not generated\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008076 return 0;
8077 }
8078
8079 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
8080 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8081 iov[0].iov_base = (char *)&hdr->ProtocolId;
8082 iov[0].iov_len = len;
8083
8084 if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature))
8085 return 0;
8086
8087 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008088 pr_err("bad smb2 signature\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008089 return 0;
8090 }
8091
8092 return 1;
8093}
8094
8095/**
8096 * smb3_set_sign_rsp() - handler for rsp packet sign processing
8097 * @work: smb work containing notify command buffer
8098 *
8099 */
8100void smb3_set_sign_rsp(struct ksmbd_work *work)
8101{
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008102 struct ksmbd_conn *conn = work->conn;
Namjae Jeone2f34482021-03-16 10:49:09 +09008103 struct smb2_hdr *req_hdr;
8104 struct smb2_hdr *hdr, *hdr_org;
8105 struct channel *chann;
8106 char signature[SMB2_CMACAES_SIZE];
8107 struct kvec iov[2];
8108 int n_vec = 1;
8109 size_t len;
8110 char *signing_key;
8111
Namjae Jeone5066492021-03-30 12:35:23 +09008112 hdr_org = hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008113 if (work->next_smb2_rsp_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008114 hdr = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008115
Namjae Jeon8a893312021-06-25 13:43:37 +09008116 req_hdr = ksmbd_req_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008117
8118 if (!work->next_smb2_rsp_hdr_off) {
8119 len = get_rfc1002_len(hdr_org);
8120 if (req_hdr->NextCommand)
8121 len = ALIGN(len, 8);
8122 } else {
8123 len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off;
8124 len = ALIGN(len, 8);
8125 }
8126
Namjae Jeon08bdbc62021-07-27 09:30:29 +09008127 if (conn->binding == false &&
8128 le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008129 signing_key = work->sess->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008130 } else {
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008131 chann = lookup_chann_list(work->sess, work->conn);
Namjae Jeone2f34482021-03-16 10:49:09 +09008132 if (!chann)
8133 return;
8134 signing_key = chann->smb3signingkey;
Namjae Jeone2f34482021-03-16 10:49:09 +09008135 }
8136
8137 if (!signing_key)
8138 return;
8139
8140 if (req_hdr->NextCommand)
8141 hdr->NextCommand = cpu_to_le32(len);
8142
8143 hdr->Flags |= SMB2_FLAGS_SIGNED;
8144 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
8145 iov[0].iov_base = (char *)&hdr->ProtocolId;
8146 iov[0].iov_len = len;
Namjae Jeone5066492021-03-30 12:35:23 +09008147 if (work->aux_payload_sz) {
8148 iov[0].iov_len -= work->aux_payload_sz;
8149 iov[1].iov_base = work->aux_payload_buf;
8150 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008151 n_vec++;
8152 }
8153
8154 if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature))
8155 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8156}
8157
8158/**
8159 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response
8160 * @work: smb work containing response buffer
8161 *
8162 */
8163void smb3_preauth_hash_rsp(struct ksmbd_work *work)
8164{
8165 struct ksmbd_conn *conn = work->conn;
8166 struct ksmbd_session *sess = work->sess;
8167 struct smb2_hdr *req, *rsp;
8168
8169 if (conn->dialect != SMB311_PROT_ID)
8170 return;
8171
8172 WORK_BUFFERS(work, req, rsp);
8173
8174 if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE)
8175 ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09008176 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09008177
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008178 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008179 __u8 *hash_value;
8180
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008181 if (conn->binding) {
8182 struct preauth_session *preauth_sess;
8183
8184 preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id);
8185 if (!preauth_sess)
8186 return;
8187 hash_value = preauth_sess->Preauth_HashValue;
8188 } else {
8189 hash_value = sess->Preauth_HashValue;
8190 if (!hash_value)
8191 return;
8192 }
Namjae Jeone2f34482021-03-16 10:49:09 +09008193 ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09008194 hash_value);
Namjae Jeone2f34482021-03-16 10:49:09 +09008195 }
8196}
8197
Namjae Jeon64b39f42021-03-30 14:25:35 +09008198static void fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, char *old_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008199 __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09008200{
8201 struct smb2_hdr *hdr = (struct smb2_hdr *)old_buf;
8202 unsigned int orig_len = get_rfc1002_len(old_buf);
8203
8204 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
8205 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
8206 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
8207 tr_hdr->Flags = cpu_to_le16(0x01);
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008208 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
8209 cipher_type == SMB2_ENCRYPTION_AES256_GCM)
8210 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008211 else
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008212 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008213 memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8);
8214 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
8215 inc_rfc1001_len(tr_hdr, orig_len);
8216}
8217
8218int smb3_encrypt_resp(struct ksmbd_work *work)
8219{
Namjae Jeone5066492021-03-30 12:35:23 +09008220 char *buf = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008221 struct smb2_transform_hdr *tr_hdr;
8222 struct kvec iov[3];
8223 int rc = -ENOMEM;
Namjae Jeone5066492021-03-30 12:35:23 +09008224 int buf_size = 0, rq_nvec = 2 + (work->aux_payload_sz ? 1 : 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09008225
8226 if (ARRAY_SIZE(iov) < rq_nvec)
8227 return -ENOMEM;
8228
Namjae Jeon20ea7fd2021-03-30 12:40:47 +09008229 tr_hdr = kzalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09008230 if (!tr_hdr)
8231 return rc;
8232
8233 /* fill transform header */
8234 fill_transform_hdr(tr_hdr, buf, work->conn->cipher_type);
8235
8236 iov[0].iov_base = tr_hdr;
8237 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8238 buf_size += iov[0].iov_len - 4;
8239
8240 iov[1].iov_base = buf + 4;
8241 iov[1].iov_len = get_rfc1002_len(buf);
Namjae Jeone5066492021-03-30 12:35:23 +09008242 if (work->aux_payload_sz) {
8243 iov[1].iov_len = work->resp_hdr_sz - 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008244
Namjae Jeone5066492021-03-30 12:35:23 +09008245 iov[2].iov_base = work->aux_payload_buf;
8246 iov[2].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008247 buf_size += iov[2].iov_len;
8248 }
8249 buf_size += iov[1].iov_len;
8250 work->resp_hdr_sz = iov[1].iov_len;
8251
8252 rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1);
8253 if (rc)
8254 return rc;
8255
8256 memmove(buf, iov[1].iov_base, iov[1].iov_len);
8257 tr_hdr->smb2_buf_length = cpu_to_be32(buf_size);
8258 work->tr_buf = tr_hdr;
8259
8260 return rc;
8261}
8262
Namjae Jeonf4228b62021-08-12 10:16:40 +09008263bool smb3_is_transform_hdr(void *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09008264{
8265 struct smb2_transform_hdr *trhdr = buf;
8266
8267 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
8268}
8269
8270int smb3_decrypt_req(struct ksmbd_work *work)
8271{
8272 struct ksmbd_conn *conn = work->conn;
8273 struct ksmbd_session *sess;
Namjae Jeone5066492021-03-30 12:35:23 +09008274 char *buf = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008275 struct smb2_hdr *hdr;
8276 unsigned int pdu_length = get_rfc1002_len(buf);
8277 struct kvec iov[2];
8278 unsigned int buf_data_size = pdu_length + 4 -
8279 sizeof(struct smb2_transform_hdr);
8280 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
8281 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
8282 int rc = 0;
8283
Namjae Jeonf5a544e2021-06-18 10:04:19 +09008284 sess = ksmbd_session_lookup_all(conn, le64_to_cpu(tr_hdr->SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09008285 if (!sess) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008286 pr_err("invalid session id(%llx) in transform header\n",
8287 le64_to_cpu(tr_hdr->SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09008288 return -ECONNABORTED;
8289 }
8290
Namjae Jeon070fb212021-05-26 17:57:12 +09008291 if (pdu_length + 4 <
8292 sizeof(struct smb2_transform_hdr) + sizeof(struct smb2_hdr)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008293 pr_err("Transform message is too small (%u)\n",
8294 pdu_length);
Namjae Jeone2f34482021-03-16 10:49:09 +09008295 return -ECONNABORTED;
8296 }
8297
8298 if (pdu_length + 4 < orig_len + sizeof(struct smb2_transform_hdr)) {
Namjae Jeonbde16942021-06-28 15:23:19 +09008299 pr_err("Transform message is broken\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09008300 return -ECONNABORTED;
8301 }
8302
8303 iov[0].iov_base = buf;
8304 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8305 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
8306 iov[1].iov_len = buf_data_size;
8307 rc = ksmbd_crypt_message(conn, iov, 2, 0);
8308 if (rc)
8309 return rc;
8310
8311 memmove(buf + 4, iov[1].iov_base, buf_data_size);
8312 hdr = (struct smb2_hdr *)buf;
8313 hdr->smb2_buf_length = cpu_to_be32(buf_data_size);
8314
8315 return rc;
8316}
8317
8318bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work)
8319{
8320 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09008321 struct smb2_hdr *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008322
8323 if (conn->dialect < SMB30_PROT_ID)
8324 return false;
8325
8326 if (work->next_smb2_rcv_hdr_off)
Namjae Jeon8a893312021-06-25 13:43:37 +09008327 rsp = ksmbd_resp_buf_next(work);
Namjae Jeone2f34482021-03-16 10:49:09 +09008328
8329 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008330 rsp->Status == STATUS_SUCCESS)
Namjae Jeone2f34482021-03-16 10:49:09 +09008331 return true;
8332 return false;
8333}