blob: 5b92e00881bb74df0069df016fdb229f4f58c195 [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>
13
14#include "glob.h"
15#include "smb2pdu.h"
16#include "smbfsctl.h"
17#include "oplock.h"
18#include "smbacl.h"
19
20#include "auth.h"
21#include "asn1.h"
22#include "buffer_pool.h"
23#include "connection.h"
24#include "transport_ipc.h"
25#include "vfs.h"
26#include "vfs_cache.h"
27#include "misc.h"
28
Namjae Jeone2f34482021-03-16 10:49:09 +090029#include "server.h"
30#include "smb_common.h"
31#include "smbstatus.h"
32#include "ksmbd_work.h"
33#include "mgmt/user_config.h"
34#include "mgmt/share_config.h"
35#include "mgmt/tree_connect.h"
36#include "mgmt/user_session.h"
37#include "mgmt/ksmbd_ida.h"
38#include "ndr.h"
39
40static void __wbuf(struct ksmbd_work *work, void **req, void **rsp)
41{
42 if (work->next_smb2_rcv_hdr_off) {
43 *req = REQUEST_BUF_NEXT(work);
44 *rsp = RESPONSE_BUF_NEXT(work);
45 } else {
Namjae Jeone5066492021-03-30 12:35:23 +090046 *req = work->request_buf;
47 *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +090048 }
49}
50
51#define WORK_BUFFERS(w, rq, rs) __wbuf((w), (void **)&(rq), (void **)&(rs))
52
53/**
54 * check_session_id() - check for valid session id in smb header
55 * @conn: connection instance
56 * @id: session id from smb header
57 *
58 * Return: 1 if valid session id, otherwise 0
59 */
Namjae Jeon64b39f42021-03-30 14:25:35 +090060static inline int check_session_id(struct ksmbd_conn *conn, u64 id)
Namjae Jeone2f34482021-03-16 10:49:09 +090061{
62 struct ksmbd_session *sess;
63
64 if (id == 0 || id == -1)
65 return 0;
66
67 sess = ksmbd_session_lookup(conn, id);
68 if (sess)
69 return 1;
70 ksmbd_err("Invalid user session id: %llu\n", id);
71 return 0;
72}
73
74struct channel *lookup_chann_list(struct ksmbd_session *sess)
75{
76 struct channel *chann;
77 struct list_head *t;
78
79 list_for_each(t, &sess->ksmbd_chann_list) {
80 chann = list_entry(t, struct channel, chann_list);
81 if (chann && chann->conn == sess->conn)
82 return chann;
83 }
84
85 return NULL;
86}
87
88/**
89 * smb2_get_ksmbd_tcon() - get tree connection information for a tree id
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +090090 * @work: smb work
Namjae Jeone2f34482021-03-16 10:49:09 +090091 *
92 * Return: matching tree connection on success, otherwise error
93 */
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");
109 return -1;
110 }
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) {
115 ksmbd_err("Invalid tid %d\n", tree_id);
116 return -1;
117 }
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)
131 err_rsp = RESPONSE_BUF_NEXT(work);
132 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 *
149 * Return: 1 if smb2 negotiation command, otherwise 0
150 */
151int is_smb2_neg_cmd(struct ksmbd_work *work)
152{
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)
157 return 0;
158
159 /* make sure it is request not response message */
160 if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)
161 return 0;
162
163 if (hdr->Command != SMB2_NEGOTIATE)
164 return 0;
165
166 return 1;
167}
168
169/**
170 * is_smb2_rsp() - is it smb2 response
171 * @work: smb work containing smb response buffer
172 *
173 * Return: 1 if smb2 response, otherwise 0
174 */
175int is_smb2_rsp(struct ksmbd_work *work)
176{
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)
181 return 0;
182
183 /* make sure it is response not request message */
184 if (!(hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR))
185 return 0;
186
187 return 1;
188}
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)
201 rcv_hdr = REQUEST_BUF_NEXT(work);
202 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)
217 rsp_hdr = RESPONSE_BUF_NEXT(work);
218 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 =
248 cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
249
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{
320 struct smb2_hdr *req_hdr = REQUEST_BUF_NEXT(work);
321 struct smb2_hdr *hdr = RESPONSE_BUF_NEXT(work);
322 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) {
335 ksmbd_err("Total credits overflow: %d\n", conn->total_credits);
336 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{
388 struct smb2_hdr *req = REQUEST_BUF_NEXT(work);
389 struct smb2_hdr *rsp = RESPONSE_BUF_NEXT(work);
390 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
427 rsp_hdr = RESPONSE_BUF_NEXT(work);
428 rcv_hdr = REQUEST_BUF_NEXT(work);
429
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);
436 rsp_hdr->ProtocolId = rcv_hdr->ProtocolId;
437 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
467 hdr = REQUEST_BUF_NEXT(work);
468 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);
502 rsp_hdr->smb2_buf_length = cpu_to_be32(HEADER_SIZE_NO_BUF_LEN(conn));
503 rsp_hdr->ProtocolId = rcv_hdr->ProtocolId;
504 rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE;
505 rsp_hdr->Command = rcv_hdr->Command;
506
507 /*
508 * Message is response. We don't grant oplock yet.
509 */
510 rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR);
511 rsp_hdr->NextCommand = 0;
512 rsp_hdr->MessageId = rcv_hdr->MessageId;
513 rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId;
514 rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId;
515 rsp_hdr->SessionId = rcv_hdr->SessionId;
516 memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16);
517
518 work->syncronous = true;
519 if (work->async_id) {
Namjae Jeond40012a2021-04-13 13:06:30 +0900520 ksmbd_release_id(&conn->async_ida, work->async_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900521 work->async_id = 0;
522 }
523
524 return 0;
525}
526
527/**
528 * smb2_allocate_rsp_buf() - allocate smb2 response buffer
529 * @work: smb work containing smb request buffer
530 *
531 * Return: 0 on success, otherwise -ENOMEM
532 */
533int smb2_allocate_rsp_buf(struct ksmbd_work *work)
534{
Namjae Jeone5066492021-03-30 12:35:23 +0900535 struct smb2_hdr *hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900536 size_t small_sz = MAX_CIFS_SMALL_BUFFER_SIZE;
537 size_t large_sz = work->conn->vals->max_trans_size + MAX_SMB2_HDR_SIZE;
538 size_t sz = small_sz;
539 int cmd = le16_to_cpu(hdr->Command);
540
541 if (cmd == SMB2_IOCTL_HE || cmd == SMB2_QUERY_DIRECTORY_HE) {
542 sz = large_sz;
543 work->set_trans_buf = true;
544 }
545
546 if (cmd == SMB2_QUERY_INFO_HE) {
547 struct smb2_query_info_req *req;
548
Namjae Jeone5066492021-03-30 12:35:23 +0900549 req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900550 if (req->InfoType == SMB2_O_INFO_FILE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +0900551 (req->FileInfoClass == FILE_FULL_EA_INFORMATION ||
552 req->FileInfoClass == FILE_ALL_INFORMATION)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900553 sz = large_sz;
554 work->set_trans_buf = true;
555 }
556 }
557
558 /* allocate large response buf for chained commands */
559 if (le32_to_cpu(hdr->NextCommand) > 0)
560 sz = large_sz;
561
562 if (server_conf.flags & KSMBD_GLOBAL_FLAG_CACHE_TBUF &&
Hyunchul Leed7e58522021-05-29 09:59:59 +0900563 work->set_trans_buf)
Namjae Jeone2f34482021-03-16 10:49:09 +0900564 work->response_buf = ksmbd_find_buffer(sz);
565 else
Namjae Jeon79f6b112021-04-02 12:47:14 +0900566 work->response_buf = kvmalloc(sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +0900567
Namjae Jeon63c454f2021-04-20 14:24:28 +0900568 if (!work->response_buf)
Namjae Jeone2f34482021-03-16 10:49:09 +0900569 return -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +0900570
571 work->response_sz = sz;
572 return 0;
573}
574
575/**
576 * smb2_check_user_session() - check for valid session for a user
577 * @work: smb work containing smb request buffer
578 *
579 * Return: 0 on success, otherwise error
580 */
581int smb2_check_user_session(struct ksmbd_work *work)
582{
Namjae Jeone5066492021-03-30 12:35:23 +0900583 struct smb2_hdr *req_hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900584 struct ksmbd_conn *conn = work->conn;
585 unsigned int cmd = conn->ops->get_cmd_val(work);
586 unsigned long long sess_id;
587
588 work->sess = NULL;
589 /*
590 * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not
591 * require a session id, so no need to validate user session's for
592 * these commands.
593 */
594 if (cmd == SMB2_ECHO_HE || cmd == SMB2_NEGOTIATE_HE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +0900595 cmd == SMB2_SESSION_SETUP_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +0900596 return 0;
597
598 if (!ksmbd_conn_good(work))
599 return -EINVAL;
600
601 sess_id = le64_to_cpu(req_hdr->SessionId);
602 /* Check for validity of user session */
603 work->sess = ksmbd_session_lookup(conn, sess_id);
604 if (work->sess)
605 return 1;
606 ksmbd_debug(SMB, "Invalid user session, Uid %llu\n", sess_id);
607 return -EINVAL;
608}
609
Namjae Jeon64b39f42021-03-30 14:25:35 +0900610static void destroy_previous_session(struct ksmbd_user *user, u64 id)
Namjae Jeone2f34482021-03-16 10:49:09 +0900611{
612 struct ksmbd_session *prev_sess = ksmbd_session_lookup_slowpath(id);
613 struct ksmbd_user *prev_user;
614
615 if (!prev_sess)
616 return;
617
618 prev_user = prev_sess->user;
619
Marios Makassikis1fca8032021-05-06 11:41:54 +0900620 if (!prev_user ||
621 strcmp(user->name, prev_user->name) ||
Namjae Jeone2f34482021-03-16 10:49:09 +0900622 user->passkey_sz != prev_user->passkey_sz ||
623 memcmp(user->passkey, prev_user->passkey, user->passkey_sz)) {
624 put_session(prev_sess);
625 return;
626 }
627
628 put_session(prev_sess);
629 ksmbd_session_destroy(prev_sess);
630}
631
632/**
633 * smb2_get_name() - get filename string from on the wire smb format
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900634 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +0900635 * @src: source buffer
636 * @maxlen: maxlen of source string
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900637 * @nls_table: nls_table pointer
Namjae Jeone2f34482021-03-16 10:49:09 +0900638 *
639 * Return: matching converted filename on success, otherwise error ptr
640 */
641static char *
Namjae Jeon64b39f42021-03-30 14:25:35 +0900642smb2_get_name(struct ksmbd_share_config *share, const char *src,
Namjae Jeon070fb212021-05-26 17:57:12 +0900643 const int maxlen, struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +0900644{
645 char *name, *unixname;
646
Namjae Jeon64b39f42021-03-30 14:25:35 +0900647 name = smb_strndup_from_utf16(src, maxlen, 1, local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +0900648 if (IS_ERR(name)) {
649 ksmbd_err("failed to get name %ld\n", PTR_ERR(name));
650 return name;
651 }
652
653 /* change it to absolute unix name */
654 ksmbd_conv_path_to_unix(name);
655 ksmbd_strip_last_slash(name);
656
657 unixname = convert_to_unix_name(share, name);
658 kfree(name);
659 if (!unixname) {
660 ksmbd_err("can not convert absolute name\n");
661 return ERR_PTR(-ENOMEM);
662 }
663
664 ksmbd_debug(SMB, "absolute name = %s\n", unixname);
665 return unixname;
666}
667
Namjae Jeone2f34482021-03-16 10:49:09 +0900668int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg)
669{
670 struct smb2_hdr *rsp_hdr;
671 struct ksmbd_conn *conn = work->conn;
672 int id;
673
Namjae Jeone5066492021-03-30 12:35:23 +0900674 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900675 rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND;
676
Namjae Jeond40012a2021-04-13 13:06:30 +0900677 id = ksmbd_acquire_async_msg_id(&conn->async_ida);
Namjae Jeone2f34482021-03-16 10:49:09 +0900678 if (id < 0) {
679 ksmbd_err("Failed to alloc async message id\n");
680 return id;
681 }
682 work->syncronous = false;
683 work->async_id = id;
684 rsp_hdr->Id.AsyncId = cpu_to_le64(id);
685
686 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900687 "Send interim Response to inform async request id : %d\n",
688 work->async_id);
Namjae Jeone2f34482021-03-16 10:49:09 +0900689
690 work->cancel_fn = fn;
691 work->cancel_argv = arg;
692
Namjae Jeon6c4e6752021-06-07 09:08:45 +0900693 if (list_empty(&work->async_request_entry)) {
694 spin_lock(&conn->request_lock);
695 list_add_tail(&work->async_request_entry, &conn->async_requests);
696 spin_unlock(&conn->request_lock);
697 }
Namjae Jeone2f34482021-03-16 10:49:09 +0900698
699 return 0;
700}
701
702void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status)
703{
704 struct smb2_hdr *rsp_hdr;
705
Namjae Jeone5066492021-03-30 12:35:23 +0900706 rsp_hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +0900707 smb2_set_err_rsp(work);
708 rsp_hdr->Status = status;
709
710 work->multiRsp = 1;
711 ksmbd_conn_write(work);
712 rsp_hdr->Status = 0;
713 work->multiRsp = 0;
714}
715
716static __le32 smb2_get_reparse_tag_special_file(umode_t mode)
717{
718 if (S_ISDIR(mode) || S_ISREG(mode))
719 return 0;
720
721 if (S_ISLNK(mode))
722 return IO_REPARSE_TAG_LX_SYMLINK_LE;
723 else if (S_ISFIFO(mode))
724 return IO_REPARSE_TAG_LX_FIFO_LE;
725 else if (S_ISSOCK(mode))
726 return IO_REPARSE_TAG_AF_UNIX_LE;
727 else if (S_ISCHR(mode))
728 return IO_REPARSE_TAG_LX_CHR_LE;
729 else if (S_ISBLK(mode))
730 return IO_REPARSE_TAG_LX_BLK_LE;
731
732 return 0;
733}
734
735/**
736 * smb2_get_dos_mode() - get file mode in dos format from unix mode
737 * @stat: kstat containing file mode
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +0900738 * @attribute: attribute flags
Namjae Jeone2f34482021-03-16 10:49:09 +0900739 *
740 * Return: converted dos mode
741 */
742static int smb2_get_dos_mode(struct kstat *stat, int attribute)
743{
744 int attr = 0;
745
Namjae Jeon64b39f42021-03-30 14:25:35 +0900746 if (S_ISDIR(stat->mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900747 attr = ATTR_DIRECTORY |
748 (attribute & (ATTR_HIDDEN | ATTR_SYSTEM));
Namjae Jeon64b39f42021-03-30 14:25:35 +0900749 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +0900750 attr = (attribute & 0x00005137) | ATTR_ARCHIVE;
751 attr &= ~(ATTR_DIRECTORY);
752 if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps &
753 FILE_SUPPORTS_SPARSE_FILES))
754 attr |= ATTR_SPARSE;
755
756 if (smb2_get_reparse_tag_special_file(stat->mode))
757 attr |= ATTR_REPARSE;
758 }
759
760 return attr;
761}
762
763static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900764 __le16 hash_id)
Namjae Jeone2f34482021-03-16 10:49:09 +0900765{
766 pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;
767 pneg_ctxt->DataLength = cpu_to_le16(38);
768 pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1);
769 pneg_ctxt->Reserved = cpu_to_le32(0);
770 pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);
771 get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);
772 pneg_ctxt->HashAlgorithms = hash_id;
773}
774
775static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900776 __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +0900777{
778 pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;
779 pneg_ctxt->DataLength = cpu_to_le16(4);
780 pneg_ctxt->Reserved = cpu_to_le32(0);
781 pneg_ctxt->CipherCount = cpu_to_le16(1);
782 pneg_ctxt->Ciphers[0] = cipher_type;
783}
784
785static void build_compression_ctxt(struct smb2_compression_ctx *pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900786 __le16 comp_algo)
Namjae Jeone2f34482021-03-16 10:49:09 +0900787{
788 pneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;
789 pneg_ctxt->DataLength =
790 cpu_to_le16(sizeof(struct smb2_compression_ctx)
791 - sizeof(struct smb2_neg_context));
792 pneg_ctxt->Reserved = cpu_to_le32(0);
793 pneg_ctxt->CompressionAlgorithmCount = cpu_to_le16(1);
794 pneg_ctxt->Reserved1 = cpu_to_le32(0);
795 pneg_ctxt->CompressionAlgorithms[0] = comp_algo;
796}
797
Namjae Jeon64b39f42021-03-30 14:25:35 +0900798static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900799{
800 pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;
801 pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN);
802 /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */
803 pneg_ctxt->Name[0] = 0x93;
804 pneg_ctxt->Name[1] = 0xAD;
805 pneg_ctxt->Name[2] = 0x25;
806 pneg_ctxt->Name[3] = 0x50;
807 pneg_ctxt->Name[4] = 0x9C;
808 pneg_ctxt->Name[5] = 0xB4;
809 pneg_ctxt->Name[6] = 0x11;
810 pneg_ctxt->Name[7] = 0xE7;
811 pneg_ctxt->Name[8] = 0xB4;
812 pneg_ctxt->Name[9] = 0x23;
813 pneg_ctxt->Name[10] = 0x83;
814 pneg_ctxt->Name[11] = 0xDE;
815 pneg_ctxt->Name[12] = 0x96;
816 pneg_ctxt->Name[13] = 0x8B;
817 pneg_ctxt->Name[14] = 0xCD;
818 pneg_ctxt->Name[15] = 0x7C;
819}
820
Namjae Jeon64b39f42021-03-30 14:25:35 +0900821static void assemble_neg_contexts(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900822 struct smb2_negotiate_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +0900823{
824 /* +4 is to account for the RFC1001 len field */
825 char *pneg_ctxt = (char *)rsp +
826 le32_to_cpu(rsp->NegotiateContextOffset) + 4;
827 int neg_ctxt_cnt = 1;
828 int ctxt_size;
829
830 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900831 "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900832 build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900833 conn->preauth_info->Preauth_HashId);
Namjae Jeone2f34482021-03-16 10:49:09 +0900834 rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt);
835 inc_rfc1001_len(rsp, AUTH_GSS_PADDING);
836 ctxt_size = sizeof(struct smb2_preauth_neg_context);
837 /* Round to 8 byte boundary */
838 pneg_ctxt += round_up(sizeof(struct smb2_preauth_neg_context), 8);
839
840 if (conn->cipher_type) {
841 ctxt_size = round_up(ctxt_size, 8);
842 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900843 "assemble SMB2_ENCRYPTION_CAPABILITIES context\n");
Namjae Jeon64b39f42021-03-30 14:25:35 +0900844 build_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900845 conn->cipher_type);
Namjae Jeone2f34482021-03-16 10:49:09 +0900846 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
847 ctxt_size += sizeof(struct smb2_encryption_neg_context);
848 /* Round to 8 byte boundary */
849 pneg_ctxt +=
850 round_up(sizeof(struct smb2_encryption_neg_context),
851 8);
852 }
853
854 if (conn->compress_algorithm) {
855 ctxt_size = round_up(ctxt_size, 8);
856 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900857 "assemble SMB2_COMPRESSION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900858 /* Temporarily set to SMB3_COMPRESS_NONE */
859 build_compression_ctxt((struct smb2_compression_ctx *)pneg_ctxt,
Namjae Jeon070fb212021-05-26 17:57:12 +0900860 conn->compress_algorithm);
Namjae Jeone2f34482021-03-16 10:49:09 +0900861 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
862 ctxt_size += sizeof(struct smb2_compression_ctx);
863 /* Round to 8 byte boundary */
864 pneg_ctxt += round_up(sizeof(struct smb2_compression_ctx), 8);
865 }
866
867 if (conn->posix_ext_supported) {
868 ctxt_size = round_up(ctxt_size, 8);
869 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900870 "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900871 build_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);
872 rsp->NegotiateContextCount = cpu_to_le16(++neg_ctxt_cnt);
873 ctxt_size += sizeof(struct smb2_posix_neg_context);
874 }
875
876 inc_rfc1001_len(rsp, ctxt_size);
877}
878
Namjae Jeon64b39f42021-03-30 14:25:35 +0900879static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900880 struct smb2_preauth_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900881{
882 __le32 err = STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP;
883
Namjae Jeon070fb212021-05-26 17:57:12 +0900884 if (pneg_ctxt->HashAlgorithms == SMB2_PREAUTH_INTEGRITY_SHA512) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900885 conn->preauth_info->Preauth_HashId =
886 SMB2_PREAUTH_INTEGRITY_SHA512;
887 err = STATUS_SUCCESS;
888 }
889
890 return err;
891}
892
893static int decode_encrypt_ctxt(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900894 struct smb2_encryption_neg_context *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900895{
896 int i;
897 int cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount);
898
899 conn->cipher_type = 0;
900
901 if (!(server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION))
902 goto out;
903
904 for (i = 0; i < cph_cnt; i++) {
905 if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM ||
Namjae Jeon5a0ca772021-05-06 11:43:37 +0900906 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM ||
907 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM ||
908 pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) {
Namjae Jeone2f34482021-03-16 10:49:09 +0900909 ksmbd_debug(SMB, "Cipher ID = 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +0900910 pneg_ctxt->Ciphers[i]);
Namjae Jeone2f34482021-03-16 10:49:09 +0900911 conn->cipher_type = pneg_ctxt->Ciphers[i];
912 break;
913 }
914 }
915
916out:
917 /*
918 * Return encrypt context size in request.
919 * So need to plus extra number of ciphers size.
920 */
921 return sizeof(struct smb2_encryption_neg_context) +
922 ((cph_cnt - 1) * 2);
923}
924
925static int decode_compress_ctxt(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900926 struct smb2_compression_ctx *pneg_ctxt)
Namjae Jeone2f34482021-03-16 10:49:09 +0900927{
928 int algo_cnt = le16_to_cpu(pneg_ctxt->CompressionAlgorithmCount);
929
930 conn->compress_algorithm = SMB3_COMPRESS_NONE;
931
932 /*
933 * Return compression context size in request.
934 * So need to plus extra number of CompressionAlgorithms size.
935 */
936 return sizeof(struct smb2_encryption_neg_context) +
937 ((algo_cnt - 1) * 2);
938}
939
940static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900941 struct smb2_negotiate_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +0900942{
943 int i = 0;
944 __le32 status = 0;
945 /* +4 is to account for the RFC1001 len field */
946 char *pneg_ctxt = (char *)req +
947 le32_to_cpu(req->NegotiateContextOffset) + 4;
948 __le16 *ContextType = (__le16 *)pneg_ctxt;
949 int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount);
950 int ctxt_size;
951
952 ksmbd_debug(SMB, "negotiate context count = %d\n", neg_ctxt_cnt);
953 status = STATUS_INVALID_PARAMETER;
954 while (i++ < neg_ctxt_cnt) {
955 if (*ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) {
956 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900957 "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900958 if (conn->preauth_info->Preauth_HashId)
959 break;
960
961 status = decode_preauth_ctxt(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +0900962 (struct smb2_preauth_neg_context *)pneg_ctxt);
Namjae Jeon64b39f42021-03-30 14:25:35 +0900963 pneg_ctxt += DIV_ROUND_UP(sizeof(struct smb2_preauth_neg_context), 8) * 8;
Namjae Jeone2f34482021-03-16 10:49:09 +0900964 } else if (*ContextType == SMB2_ENCRYPTION_CAPABILITIES) {
965 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900966 "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900967 if (conn->cipher_type)
968 break;
969
970 ctxt_size = decode_encrypt_ctxt(conn,
Namjae Jeon64b39f42021-03-30 14:25:35 +0900971 (struct smb2_encryption_neg_context *)pneg_ctxt);
Namjae Jeone2f34482021-03-16 10:49:09 +0900972 pneg_ctxt += DIV_ROUND_UP(ctxt_size, 8) * 8;
973 } else if (*ContextType == SMB2_COMPRESSION_CAPABILITIES) {
974 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900975 "deassemble SMB2_COMPRESSION_CAPABILITIES context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900976 if (conn->compress_algorithm)
977 break;
978
979 ctxt_size = decode_compress_ctxt(conn,
Namjae Jeon10268f72021-05-26 16:44:21 +0900980 (struct smb2_compression_ctx *)pneg_ctxt);
Namjae Jeone2f34482021-03-16 10:49:09 +0900981 pneg_ctxt += DIV_ROUND_UP(ctxt_size, 8) * 8;
982 } else if (*ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) {
983 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900984 "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900985 ctxt_size = sizeof(struct smb2_netname_neg_context);
Namjae Jeon64b39f42021-03-30 14:25:35 +0900986 ctxt_size += DIV_ROUND_UP(le16_to_cpu(((struct smb2_netname_neg_context *)
Namjae Jeon070fb212021-05-26 17:57:12 +0900987 pneg_ctxt)->DataLength), 8) * 8;
Namjae Jeone2f34482021-03-16 10:49:09 +0900988 pneg_ctxt += ctxt_size;
989 } else if (*ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) {
990 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +0900991 "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +0900992 conn->posix_ext_supported = true;
Namjae Jeon64b39f42021-03-30 14:25:35 +0900993 pneg_ctxt += DIV_ROUND_UP(sizeof(struct smb2_posix_neg_context), 8) * 8;
Namjae Jeone2f34482021-03-16 10:49:09 +0900994 }
995 ContextType = (__le16 *)pneg_ctxt;
996
997 if (status != STATUS_SUCCESS)
998 break;
999 }
1000 return status;
1001}
1002
1003/**
1004 * smb2_handle_negotiate() - handler for smb2 negotiate command
1005 * @work: smb work containing smb request buffer
1006 *
1007 * Return: 0
1008 */
1009int smb2_handle_negotiate(struct ksmbd_work *work)
1010{
1011 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09001012 struct smb2_negotiate_req *req = work->request_buf;
1013 struct smb2_negotiate_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001014 int rc = 0;
1015 __le32 status;
1016
1017 ksmbd_debug(SMB, "Received negotiate request\n");
1018 conn->need_neg = false;
1019 if (ksmbd_conn_good(work)) {
1020 ksmbd_err("conn->tcp_status is already in CifsGood State\n");
1021 work->send_no_response = 1;
1022 return rc;
1023 }
1024
1025 if (req->DialectCount == 0) {
1026 ksmbd_err("malformed packet\n");
1027 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1028 rc = -EINVAL;
1029 goto err_out;
1030 }
1031
1032 conn->cli_cap = le32_to_cpu(req->Capabilities);
1033 switch (conn->dialect) {
1034 case SMB311_PROT_ID:
1035 conn->preauth_info =
1036 kzalloc(sizeof(struct preauth_integrity_info),
Namjae Jeon070fb212021-05-26 17:57:12 +09001037 GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09001038 if (!conn->preauth_info) {
1039 rc = -ENOMEM;
1040 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1041 goto err_out;
1042 }
1043
1044 status = deassemble_neg_contexts(conn, req);
1045 if (status != STATUS_SUCCESS) {
1046 ksmbd_err("deassemble_neg_contexts error(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001047 status);
Namjae Jeone2f34482021-03-16 10:49:09 +09001048 rsp->hdr.Status = status;
1049 rc = -EINVAL;
1050 goto err_out;
1051 }
1052
1053 rc = init_smb3_11_server(conn);
1054 if (rc < 0) {
1055 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1056 goto err_out;
1057 }
1058
1059 ksmbd_gen_preauth_integrity_hash(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001060 work->request_buf,
1061 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001062 rsp->NegotiateContextOffset =
1063 cpu_to_le32(OFFSET_OF_NEG_CONTEXT);
1064 assemble_neg_contexts(conn, rsp);
1065 break;
1066 case SMB302_PROT_ID:
1067 init_smb3_02_server(conn);
1068 break;
1069 case SMB30_PROT_ID:
1070 init_smb3_0_server(conn);
1071 break;
1072 case SMB21_PROT_ID:
1073 init_smb2_1_server(conn);
1074 break;
1075 case SMB20_PROT_ID:
1076 rc = init_smb2_0_server(conn);
1077 if (rc) {
1078 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
1079 goto err_out;
1080 }
1081 break;
1082 case SMB2X_PROT_ID:
1083 case BAD_PROT_ID:
1084 default:
1085 ksmbd_debug(SMB, "Server dialect :0x%x not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001086 conn->dialect);
Namjae Jeone2f34482021-03-16 10:49:09 +09001087 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
1088 rc = -EINVAL;
1089 goto err_out;
1090 }
1091 rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
1092
1093 /* For stats */
1094 conn->connection_type = conn->dialect;
1095
1096 rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size);
1097 rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size);
1098 rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size);
1099
1100 if (conn->dialect > SMB20_PROT_ID) {
1101 memcpy(conn->ClientGUID, req->ClientGUID,
Namjae Jeon070fb212021-05-26 17:57:12 +09001102 SMB2_CLIENT_GUID_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09001103 conn->cli_sec_mode = le16_to_cpu(req->SecurityMode);
1104 }
1105
1106 rsp->StructureSize = cpu_to_le16(65);
1107 rsp->DialectRevision = cpu_to_le16(conn->dialect);
1108 /* Not setting conn guid rsp->ServerGUID, as it
1109 * not used by client for identifying server
1110 */
1111 memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE);
1112
1113 rsp->SystemTime = cpu_to_le64(ksmbd_systime());
1114 rsp->ServerStartTime = 0;
1115 ksmbd_debug(SMB, "negotiate context offset %d, count %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001116 le32_to_cpu(rsp->NegotiateContextOffset),
1117 le16_to_cpu(rsp->NegotiateContextCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09001118
1119 rsp->SecurityBufferOffset = cpu_to_le16(128);
1120 rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH);
1121 ksmbd_copy_gss_neg_header(((char *)(&rsp->hdr) +
Namjae Jeon070fb212021-05-26 17:57:12 +09001122 sizeof(rsp->hdr.smb2_buf_length)) +
1123 le16_to_cpu(rsp->SecurityBufferOffset));
Namjae Jeone2f34482021-03-16 10:49:09 +09001124 inc_rfc1001_len(rsp, sizeof(struct smb2_negotiate_rsp) -
Namjae Jeon070fb212021-05-26 17:57:12 +09001125 sizeof(struct smb2_hdr) - sizeof(rsp->Buffer) +
1126 AUTH_GSS_LENGTH);
Namjae Jeone2f34482021-03-16 10:49:09 +09001127 rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE;
1128 conn->use_spnego = true;
1129
1130 if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001131 server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) &&
1132 req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09001133 conn->sign = true;
1134 else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) {
1135 server_conf.enforced_signing = true;
1136 rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE;
1137 conn->sign = true;
1138 }
1139
1140 conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode);
1141 ksmbd_conn_set_need_negotiate(work);
1142
1143err_out:
1144 if (rc < 0)
1145 smb2_set_err_rsp(work);
1146
1147 return rc;
1148}
1149
1150static int alloc_preauth_hash(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09001151 struct ksmbd_conn *conn)
Namjae Jeone2f34482021-03-16 10:49:09 +09001152{
1153 if (sess->Preauth_HashValue)
1154 return 0;
1155
kernel test robot86f52972021-04-02 12:17:24 +09001156 sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue,
Namjae Jeon070fb212021-05-26 17:57:12 +09001157 PREAUTH_HASHVALUE_SIZE, GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09001158 if (!sess->Preauth_HashValue)
1159 return -ENOMEM;
1160
Namjae Jeone2f34482021-03-16 10:49:09 +09001161 return 0;
1162}
1163
1164static int generate_preauth_hash(struct ksmbd_work *work)
1165{
1166 struct ksmbd_conn *conn = work->conn;
1167 struct ksmbd_session *sess = work->sess;
1168
1169 if (conn->dialect != SMB311_PROT_ID)
1170 return 0;
1171
1172 if (!sess->Preauth_HashValue) {
1173 if (alloc_preauth_hash(sess, conn))
1174 return -ENOMEM;
1175 }
1176
1177 ksmbd_gen_preauth_integrity_hash(conn,
Namjae Jeone5066492021-03-30 12:35:23 +09001178 work->request_buf,
Namjae Jeone2f34482021-03-16 10:49:09 +09001179 sess->Preauth_HashValue);
1180 return 0;
1181}
1182
1183static int decode_negotiation_token(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09001184 struct negotiate_message *negblob)
Namjae Jeone2f34482021-03-16 10:49:09 +09001185{
1186 struct ksmbd_conn *conn = work->conn;
1187 struct smb2_sess_setup_req *req;
1188 int sz;
1189
1190 if (!conn->use_spnego)
1191 return -EINVAL;
1192
Namjae Jeone5066492021-03-30 12:35:23 +09001193 req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001194 sz = le16_to_cpu(req->SecurityBufferLength);
1195
Hyunchul Leefad41612021-04-19 17:26:15 +09001196 if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) {
1197 if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001198 conn->auth_mechs |= KSMBD_AUTH_NTLMSSP;
1199 conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP;
1200 conn->use_spnego = false;
1201 }
1202 }
1203 return 0;
1204}
1205
1206static int ntlm_negotiate(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09001207 struct negotiate_message *negblob)
Namjae Jeone2f34482021-03-16 10:49:09 +09001208{
Namjae Jeone5066492021-03-30 12:35:23 +09001209 struct smb2_sess_setup_req *req = work->request_buf;
1210 struct smb2_sess_setup_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001211 struct challenge_message *chgblob;
1212 unsigned char *spnego_blob = NULL;
1213 u16 spnego_blob_len;
1214 char *neg_blob;
1215 int sz, rc;
1216
1217 ksmbd_debug(SMB, "negotiate phase\n");
1218 sz = le16_to_cpu(req->SecurityBufferLength);
1219 rc = ksmbd_decode_ntlmssp_neg_blob(negblob, sz, work->sess);
1220 if (rc)
1221 return rc;
1222
1223 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1224 chgblob =
1225 (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz);
1226 memset(chgblob, 0, sizeof(struct challenge_message));
1227
1228 if (!work->conn->use_spnego) {
1229 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess);
1230 if (sz < 0)
1231 return -ENOMEM;
1232
1233 rsp->SecurityBufferLength = cpu_to_le16(sz);
1234 return 0;
1235 }
1236
1237 sz = sizeof(struct challenge_message);
1238 sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6;
1239
1240 neg_blob = kzalloc(sz, GFP_KERNEL);
1241 if (!neg_blob)
1242 return -ENOMEM;
1243
1244 chgblob = (struct challenge_message *)neg_blob;
1245 sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->sess);
1246 if (sz < 0) {
1247 rc = -ENOMEM;
1248 goto out;
1249 }
1250
Namjae Jeon070fb212021-05-26 17:57:12 +09001251 rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len,
1252 neg_blob, sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09001253 if (rc) {
1254 rc = -ENOMEM;
1255 goto out;
1256 }
1257
1258 sz = le16_to_cpu(rsp->SecurityBufferOffset);
1259 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
1260 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1261
1262out:
1263 kfree(spnego_blob);
1264 kfree(neg_blob);
1265 return rc;
1266}
1267
1268static struct authenticate_message *user_authblob(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001269 struct smb2_sess_setup_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +09001270{
1271 int sz;
1272
1273 if (conn->use_spnego && conn->mechToken)
1274 return (struct authenticate_message *)conn->mechToken;
1275
1276 sz = le16_to_cpu(req->SecurityBufferOffset);
1277 return (struct authenticate_message *)((char *)&req->hdr.ProtocolId
1278 + sz);
1279}
1280
1281static struct ksmbd_user *session_user(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001282 struct smb2_sess_setup_req *req)
Namjae Jeone2f34482021-03-16 10:49:09 +09001283{
1284 struct authenticate_message *authblob;
1285 struct ksmbd_user *user;
1286 char *name;
1287 int sz;
1288
1289 authblob = user_authblob(conn, req);
1290 sz = le32_to_cpu(authblob->UserName.BufferOffset);
1291 name = smb_strndup_from_utf16((const char *)authblob + sz,
1292 le16_to_cpu(authblob->UserName.Length),
1293 true,
1294 conn->local_nls);
1295 if (IS_ERR(name)) {
1296 ksmbd_err("cannot allocate memory\n");
1297 return NULL;
1298 }
1299
1300 ksmbd_debug(SMB, "session setup request for user %s\n", name);
1301 user = ksmbd_login_user(name);
1302 kfree(name);
1303 return user;
1304}
1305
1306static int ntlm_authenticate(struct ksmbd_work *work)
1307{
Namjae Jeone5066492021-03-30 12:35:23 +09001308 struct smb2_sess_setup_req *req = work->request_buf;
1309 struct smb2_sess_setup_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001310 struct ksmbd_conn *conn = work->conn;
1311 struct ksmbd_session *sess = work->sess;
1312 struct channel *chann = NULL;
1313 struct ksmbd_user *user;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001314 u64 prev_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09001315 int sz, rc;
1316
1317 ksmbd_debug(SMB, "authenticate phase\n");
1318 if (conn->use_spnego) {
1319 unsigned char *spnego_blob;
1320 u16 spnego_blob_len;
1321
1322 rc = build_spnego_ntlmssp_auth_blob(&spnego_blob,
1323 &spnego_blob_len,
1324 0);
1325 if (rc)
1326 return -ENOMEM;
1327
1328 sz = le16_to_cpu(rsp->SecurityBufferOffset);
Namjae Jeon64b39f42021-03-30 14:25:35 +09001329 memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001330 rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len);
1331 kfree(spnego_blob);
1332 inc_rfc1001_len(rsp, spnego_blob_len - 1);
1333 }
1334
1335 user = session_user(conn, req);
1336 if (!user) {
1337 ksmbd_debug(SMB, "Unknown user name or an error\n");
1338 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1339 return -EINVAL;
1340 }
1341
1342 /* Check for previous session */
1343 prev_id = le64_to_cpu(req->PreviousSessionId);
1344 if (prev_id && prev_id != sess->id)
1345 destroy_previous_session(user, prev_id);
1346
1347 if (sess->state == SMB2_SESSION_VALID) {
1348 /*
1349 * Reuse session if anonymous try to connect
1350 * on reauthetication.
1351 */
1352 if (ksmbd_anonymous_user(user)) {
1353 ksmbd_free_user(user);
1354 return 0;
1355 }
1356 ksmbd_free_user(sess->user);
1357 }
1358
1359 sess->user = user;
1360 if (user_guest(sess->user)) {
1361 if (conn->sign) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001362 ksmbd_debug(SMB, "Guest login not allowed when signing enabled\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001363 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1364 return -EACCES;
1365 }
1366
1367 rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE;
1368 } else {
1369 struct authenticate_message *authblob;
1370
1371 authblob = user_authblob(conn, req);
1372 sz = le16_to_cpu(req->SecurityBufferLength);
1373 rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, sess);
1374 if (rc) {
1375 set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD);
1376 ksmbd_debug(SMB, "authentication failed\n");
1377 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1378 return -EINVAL;
1379 }
1380
1381 /*
1382 * If session state is SMB2_SESSION_VALID, We can assume
1383 * that it is reauthentication. And the user/password
1384 * has been verified, so return it here.
1385 */
1386 if (sess->state == SMB2_SESSION_VALID)
1387 return 0;
1388
1389 if ((conn->sign || server_conf.enforced_signing) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001390 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
Namjae Jeone2f34482021-03-16 10:49:09 +09001391 sess->sign = true;
1392
1393 if (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09001394 conn->ops->generate_encryptionkey) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001395 rc = conn->ops->generate_encryptionkey(sess);
1396 if (rc) {
1397 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001398 "SMB3 encryption key generation failed\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001399 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1400 return rc;
1401 }
1402 sess->enc = true;
1403 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1404 /*
1405 * signing is disable if encryption is enable
1406 * on this session
1407 */
1408 sess->sign = false;
1409 }
1410 }
1411
1412 if (conn->dialect >= SMB30_PROT_ID) {
1413 chann = lookup_chann_list(sess);
1414 if (!chann) {
1415 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1416 if (!chann)
1417 return -ENOMEM;
1418
1419 chann->conn = conn;
1420 INIT_LIST_HEAD(&chann->chann_list);
1421 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1422 }
1423 }
1424
1425 if (conn->ops->generate_signingkey) {
1426 rc = conn->ops->generate_signingkey(sess);
1427 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001428 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001429 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1430 return rc;
1431 }
1432 }
1433
1434 if (conn->dialect > SMB20_PROT_ID) {
1435 if (!ksmbd_conn_lookup_dialect(conn)) {
1436 ksmbd_err("fail to verify the dialect\n");
1437 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1438 return -EPERM;
1439 }
1440 }
1441 return 0;
1442}
1443
1444#ifdef CONFIG_SMB_SERVER_KERBEROS5
1445static int krb5_authenticate(struct ksmbd_work *work)
1446{
Namjae Jeone5066492021-03-30 12:35:23 +09001447 struct smb2_sess_setup_req *req = work->request_buf;
1448 struct smb2_sess_setup_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001449 struct ksmbd_conn *conn = work->conn;
1450 struct ksmbd_session *sess = work->sess;
1451 char *in_blob, *out_blob;
1452 struct channel *chann = NULL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09001453 u64 prev_sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09001454 int in_len, out_len;
1455 int retval;
1456
1457 in_blob = (char *)&req->hdr.ProtocolId +
1458 le16_to_cpu(req->SecurityBufferOffset);
1459 in_len = le16_to_cpu(req->SecurityBufferLength);
1460 out_blob = (char *)&rsp->hdr.ProtocolId +
1461 le16_to_cpu(rsp->SecurityBufferOffset);
1462 out_len = work->response_sz -
1463 offsetof(struct smb2_hdr, smb2_buf_length) -
1464 le16_to_cpu(rsp->SecurityBufferOffset);
1465
1466 /* Check previous session */
1467 prev_sess_id = le64_to_cpu(req->PreviousSessionId);
1468 if (prev_sess_id && prev_sess_id != sess->id)
1469 destroy_previous_session(sess->user, prev_sess_id);
1470
1471 if (sess->state == SMB2_SESSION_VALID)
1472 ksmbd_free_user(sess->user);
1473
1474 retval = ksmbd_krb5_authenticate(sess, in_blob, in_len,
Namjae Jeon070fb212021-05-26 17:57:12 +09001475 out_blob, &out_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09001476 if (retval) {
1477 ksmbd_debug(SMB, "krb5 authentication failed\n");
1478 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1479 return retval;
1480 }
1481 rsp->SecurityBufferLength = cpu_to_le16(out_len);
1482 inc_rfc1001_len(rsp, out_len - 1);
1483
1484 if ((conn->sign || server_conf.enforced_signing) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09001485 (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED))
Namjae Jeone2f34482021-03-16 10:49:09 +09001486 sess->sign = true;
1487
1488 if ((conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09001489 conn->ops->generate_encryptionkey) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001490 retval = conn->ops->generate_encryptionkey(sess);
1491 if (retval) {
1492 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001493 "SMB3 encryption key generation failed\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001494 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1495 return retval;
1496 }
1497 sess->enc = true;
1498 rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE;
1499 sess->sign = false;
1500 }
1501
1502 if (conn->dialect >= SMB30_PROT_ID) {
1503 chann = lookup_chann_list(sess);
1504 if (!chann) {
1505 chann = kmalloc(sizeof(struct channel), GFP_KERNEL);
1506 if (!chann)
1507 return -ENOMEM;
1508
1509 chann->conn = conn;
1510 INIT_LIST_HEAD(&chann->chann_list);
1511 list_add(&chann->chann_list, &sess->ksmbd_chann_list);
1512 }
1513 }
1514
1515 if (conn->ops->generate_signingkey) {
1516 retval = conn->ops->generate_signingkey(sess);
1517 if (retval) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09001518 ksmbd_debug(SMB, "SMB3 signing key generation failed\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09001519 rsp->hdr.Status = STATUS_LOGON_FAILURE;
1520 return retval;
1521 }
1522 }
1523
1524 if (conn->dialect > SMB20_PROT_ID) {
1525 if (!ksmbd_conn_lookup_dialect(conn)) {
1526 ksmbd_err("fail to verify the dialect\n");
1527 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1528 return -EPERM;
1529 }
1530 }
1531 return 0;
1532}
1533#else
1534static int krb5_authenticate(struct ksmbd_work *work)
1535{
1536 return -EOPNOTSUPP;
1537}
1538#endif
1539
1540int smb2_sess_setup(struct ksmbd_work *work)
1541{
1542 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09001543 struct smb2_sess_setup_req *req = work->request_buf;
1544 struct smb2_sess_setup_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001545 struct ksmbd_session *sess;
1546 struct negotiate_message *negblob;
1547 int rc = 0;
1548
1549 ksmbd_debug(SMB, "Received request for session setup\n");
1550
1551 rsp->StructureSize = cpu_to_le16(9);
1552 rsp->SessionFlags = 0;
1553 rsp->SecurityBufferOffset = cpu_to_le16(72);
1554 rsp->SecurityBufferLength = 0;
1555 inc_rfc1001_len(rsp, 9);
1556
1557 if (!req->hdr.SessionId) {
1558 sess = ksmbd_smb2_session_create();
1559 if (!sess) {
1560 rc = -ENOMEM;
1561 goto out_err;
1562 }
1563 rsp->hdr.SessionId = cpu_to_le64(sess->id);
1564 ksmbd_session_register(conn, sess);
1565 } else {
1566 sess = ksmbd_session_lookup(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09001567 le64_to_cpu(req->hdr.SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09001568 if (!sess) {
1569 rc = -ENOENT;
1570 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
1571 goto out_err;
1572 }
1573 }
1574 work->sess = sess;
1575
1576 if (sess->state == SMB2_SESSION_EXPIRED)
1577 sess->state = SMB2_SESSION_IN_PROGRESS;
1578
1579 negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId +
1580 le16_to_cpu(req->SecurityBufferOffset));
1581
1582 if (decode_negotiation_token(work, negblob) == 0) {
1583 if (conn->mechToken)
1584 negblob = (struct negotiate_message *)conn->mechToken;
1585 }
1586
1587 if (server_conf.auth_mechs & conn->auth_mechs) {
1588 if (conn->preferred_auth_mech &
1589 (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) {
1590 rc = generate_preauth_hash(work);
1591 if (rc)
1592 goto out_err;
1593
1594 rc = krb5_authenticate(work);
1595 if (rc) {
1596 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1597 goto out_err;
1598 }
1599
1600 ksmbd_conn_set_good(work);
1601 sess->state = SMB2_SESSION_VALID;
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001602 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001603 sess->Preauth_HashValue = NULL;
1604 } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) {
1605 rc = generate_preauth_hash(work);
1606 if (rc)
1607 goto out_err;
1608
1609 if (negblob->MessageType == NtLmNegotiate) {
1610 rc = ntlm_negotiate(work, negblob);
1611 if (rc)
1612 goto out_err;
1613 rsp->hdr.Status =
1614 STATUS_MORE_PROCESSING_REQUIRED;
1615 /*
1616 * Note: here total size -1 is done as an
1617 * adjustment for 0 size blob
1618 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09001619 inc_rfc1001_len(rsp, le16_to_cpu(rsp->SecurityBufferLength) - 1);
Namjae Jeone2f34482021-03-16 10:49:09 +09001620
1621 } else if (negblob->MessageType == NtLmAuthenticate) {
1622 rc = ntlm_authenticate(work);
1623 if (rc)
1624 goto out_err;
1625
1626 ksmbd_conn_set_good(work);
1627 sess->state = SMB2_SESSION_VALID;
Muhammad Usama Anjum822bc8e2021-04-02 09:25:35 +09001628 kfree(sess->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09001629 sess->Preauth_HashValue = NULL;
1630 }
1631 } else {
1632 /* TODO: need one more negotiation */
1633 ksmbd_err("Not support the preferred authentication\n");
1634 rc = -EINVAL;
1635 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1636 }
1637 } else {
1638 ksmbd_err("Not support authentication\n");
1639 rc = -EINVAL;
1640 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1641 }
1642
1643out_err:
1644 if (conn->use_spnego && conn->mechToken) {
1645 kfree(conn->mechToken);
1646 conn->mechToken = NULL;
1647 }
1648
1649 if (rc < 0 && sess) {
1650 ksmbd_session_destroy(sess);
1651 work->sess = NULL;
1652 }
1653
1654 return rc;
1655}
1656
1657/**
1658 * smb2_tree_connect() - handler for smb2 tree connect command
1659 * @work: smb work containing smb request buffer
1660 *
1661 * Return: 0 on success, otherwise error
1662 */
1663int smb2_tree_connect(struct ksmbd_work *work)
1664{
1665 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09001666 struct smb2_tree_connect_req *req = work->request_buf;
1667 struct smb2_tree_connect_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001668 struct ksmbd_session *sess = work->sess;
1669 char *treename = NULL, *name = NULL;
1670 struct ksmbd_tree_conn_status status;
1671 struct ksmbd_share_config *share;
1672 int rc = -EINVAL;
1673
1674 treename = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09001675 le16_to_cpu(req->PathLength), true,
1676 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09001677 if (IS_ERR(treename)) {
1678 ksmbd_err("treename is NULL\n");
1679 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1680 goto out_err1;
1681 }
1682
Stephen Rothwell36ba3862021-03-17 17:01:15 +09001683 name = ksmbd_extract_sharename(treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001684 if (IS_ERR(name)) {
1685 status.ret = KSMBD_TREE_CONN_STATUS_ERROR;
1686 goto out_err1;
1687 }
1688
1689 ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09001690 name, treename);
Namjae Jeone2f34482021-03-16 10:49:09 +09001691
1692 status = ksmbd_tree_conn_connect(sess, name);
1693 if (status.ret == KSMBD_TREE_CONN_STATUS_OK)
1694 rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id);
1695 else
1696 goto out_err1;
1697
1698 share = status.tree_conn->share_conf;
1699 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
1700 ksmbd_debug(SMB, "IPC share path request\n");
1701 rsp->ShareType = SMB2_SHARE_TYPE_PIPE;
1702 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1703 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE |
1704 FILE_DELETE_LE | FILE_READ_CONTROL_LE |
1705 FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE |
1706 FILE_SYNCHRONIZE_LE;
1707 } else {
1708 rsp->ShareType = SMB2_SHARE_TYPE_DISK;
1709 rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE |
1710 FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE;
1711 if (test_tree_conn_flag(status.tree_conn,
1712 KSMBD_TREE_CONN_FLAG_WRITABLE)) {
1713 rsp->MaximalAccess |= FILE_WRITE_DATA_LE |
1714 FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE |
1715 FILE_DELETE_CHILD_LE | FILE_DELETE_LE |
1716 FILE_WRITE_ATTRIBUTES_LE | FILE_DELETE_LE |
1717 FILE_READ_CONTROL_LE | FILE_WRITE_DAC_LE |
1718 FILE_WRITE_OWNER_LE | FILE_SYNCHRONIZE_LE;
1719 }
1720 }
1721
1722 status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess);
1723 if (conn->posix_ext_supported)
1724 status.tree_conn->posix_extensions = true;
1725
1726out_err1:
1727 rsp->StructureSize = cpu_to_le16(16);
1728 rsp->Capabilities = 0;
1729 rsp->Reserved = 0;
1730 /* default manual caching */
1731 rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING;
1732 inc_rfc1001_len(rsp, 16);
1733
1734 if (!IS_ERR(treename))
1735 kfree(treename);
1736 if (!IS_ERR(name))
1737 kfree(name);
1738
1739 switch (status.ret) {
1740 case KSMBD_TREE_CONN_STATUS_OK:
1741 rsp->hdr.Status = STATUS_SUCCESS;
1742 rc = 0;
1743 break;
1744 case KSMBD_TREE_CONN_STATUS_NO_SHARE:
1745 rsp->hdr.Status = STATUS_BAD_NETWORK_PATH;
1746 break;
1747 case -ENOMEM:
1748 case KSMBD_TREE_CONN_STATUS_NOMEM:
1749 rsp->hdr.Status = STATUS_NO_MEMORY;
1750 break;
1751 case KSMBD_TREE_CONN_STATUS_ERROR:
1752 case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS:
1753 case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS:
1754 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1755 break;
1756 case -EINVAL:
1757 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1758 break;
1759 default:
1760 rsp->hdr.Status = STATUS_ACCESS_DENIED;
1761 }
1762
1763 return rc;
1764}
1765
1766/**
1767 * smb2_create_open_flags() - convert smb open flags to unix open flags
1768 * @file_present: is file already present
1769 * @access: file access flags
1770 * @disposition: file disposition flags
Namjae Jeone2f34482021-03-16 10:49:09 +09001771 *
1772 * Return: file open flags
1773 */
1774static int smb2_create_open_flags(bool file_present, __le32 access,
Namjae Jeon070fb212021-05-26 17:57:12 +09001775 __le32 disposition)
Namjae Jeone2f34482021-03-16 10:49:09 +09001776{
1777 int oflags = O_NONBLOCK | O_LARGEFILE;
1778
1779 if (access & FILE_READ_DESIRED_ACCESS_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09001780 access & FILE_WRITE_DESIRE_ACCESS_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09001781 oflags |= O_RDWR;
1782 else if (access & FILE_WRITE_DESIRE_ACCESS_LE)
1783 oflags |= O_WRONLY;
1784 else
1785 oflags |= O_RDONLY;
1786
1787 if (access == FILE_READ_ATTRIBUTES_LE)
1788 oflags |= O_PATH;
1789
1790 if (file_present) {
1791 switch (disposition & FILE_CREATE_MASK_LE) {
1792 case FILE_OPEN_LE:
1793 case FILE_CREATE_LE:
1794 break;
1795 case FILE_SUPERSEDE_LE:
1796 case FILE_OVERWRITE_LE:
1797 case FILE_OVERWRITE_IF_LE:
1798 oflags |= O_TRUNC;
1799 break;
1800 default:
1801 break;
1802 }
1803 } else {
1804 switch (disposition & FILE_CREATE_MASK_LE) {
1805 case FILE_SUPERSEDE_LE:
1806 case FILE_CREATE_LE:
1807 case FILE_OPEN_IF_LE:
1808 case FILE_OVERWRITE_IF_LE:
1809 oflags |= O_CREAT;
1810 break;
1811 case FILE_OPEN_LE:
1812 case FILE_OVERWRITE_LE:
1813 oflags &= ~O_CREAT;
1814 break;
1815 default:
1816 break;
1817 }
1818 }
1819 return oflags;
1820}
1821
1822/**
1823 * smb2_tree_disconnect() - handler for smb tree connect request
1824 * @work: smb work containing request buffer
1825 *
1826 * Return: 0
1827 */
1828int smb2_tree_disconnect(struct ksmbd_work *work)
1829{
Namjae Jeone5066492021-03-30 12:35:23 +09001830 struct smb2_tree_disconnect_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001831 struct ksmbd_session *sess = work->sess;
1832 struct ksmbd_tree_connect *tcon = work->tcon;
1833
1834 rsp->StructureSize = cpu_to_le16(4);
1835 inc_rfc1001_len(rsp, 4);
1836
1837 ksmbd_debug(SMB, "request\n");
1838
1839 if (!tcon) {
Namjae Jeone5066492021-03-30 12:35:23 +09001840 struct smb2_tree_disconnect_req *req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001841
1842 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
1843 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
1844 smb2_set_err_rsp(work);
1845 return 0;
1846 }
1847
1848 ksmbd_close_tree_conn_fds(work);
1849 ksmbd_tree_conn_disconnect(sess, tcon);
1850 return 0;
1851}
1852
1853/**
1854 * smb2_session_logoff() - handler for session log off request
1855 * @work: smb work containing request buffer
1856 *
1857 * Return: 0
1858 */
1859int smb2_session_logoff(struct ksmbd_work *work)
1860{
1861 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09001862 struct smb2_logoff_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001863 struct ksmbd_session *sess = work->sess;
1864
1865 rsp->StructureSize = cpu_to_le16(4);
1866 inc_rfc1001_len(rsp, 4);
1867
1868 ksmbd_debug(SMB, "request\n");
1869
1870 /* Got a valid session, set connection state */
1871 WARN_ON(sess->conn != conn);
1872
1873 /* setting CifsExiting here may race with start_tcp_sess */
1874 ksmbd_conn_set_need_reconnect(work);
1875 ksmbd_close_session_fds(work);
1876 ksmbd_conn_wait_idle(conn);
1877
1878 if (ksmbd_tree_conn_session_logoff(sess)) {
Namjae Jeone5066492021-03-30 12:35:23 +09001879 struct smb2_logoff_req *req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001880
1881 ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId);
1882 rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED;
1883 smb2_set_err_rsp(work);
1884 return 0;
1885 }
1886
1887 ksmbd_destroy_file_table(&sess->file_table);
1888 sess->state = SMB2_SESSION_EXPIRED;
1889
1890 ksmbd_free_user(sess->user);
1891 sess->user = NULL;
1892
1893 /* let start_tcp_sess free connection info now */
1894 ksmbd_conn_set_need_negotiate(work);
1895 return 0;
1896}
1897
1898/**
1899 * create_smb2_pipe() - create IPC pipe
1900 * @work: smb work containing request buffer
1901 *
1902 * Return: 0 on success, otherwise error
1903 */
1904static noinline int create_smb2_pipe(struct ksmbd_work *work)
1905{
Namjae Jeone5066492021-03-30 12:35:23 +09001906 struct smb2_create_rsp *rsp = work->response_buf;
1907 struct smb2_create_req *req = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09001908 int id;
1909 int err;
1910 char *name;
1911
1912 name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09001913 1, work->conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09001914 if (IS_ERR(name)) {
1915 rsp->hdr.Status = STATUS_NO_MEMORY;
1916 err = PTR_ERR(name);
1917 goto out;
1918 }
1919
1920 id = ksmbd_session_rpc_open(work->sess, name);
Marios Makassikis79caa962021-05-06 11:38:35 +09001921 if (id < 0) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001922 ksmbd_err("Unable to open RPC pipe: %d\n", id);
Marios Makassikis79caa962021-05-06 11:38:35 +09001923 err = id;
1924 goto out;
1925 }
Namjae Jeone2f34482021-03-16 10:49:09 +09001926
Marios Makassikis79caa962021-05-06 11:38:35 +09001927 rsp->hdr.Status = STATUS_SUCCESS;
Namjae Jeone2f34482021-03-16 10:49:09 +09001928 rsp->StructureSize = cpu_to_le16(89);
1929 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE;
1930 rsp->Reserved = 0;
1931 rsp->CreateAction = cpu_to_le32(FILE_OPENED);
1932
1933 rsp->CreationTime = cpu_to_le64(0);
1934 rsp->LastAccessTime = cpu_to_le64(0);
1935 rsp->ChangeTime = cpu_to_le64(0);
1936 rsp->AllocationSize = cpu_to_le64(0);
1937 rsp->EndofFile = cpu_to_le64(0);
1938 rsp->FileAttributes = ATTR_NORMAL_LE;
1939 rsp->Reserved2 = 0;
1940 rsp->VolatileFileId = cpu_to_le64(id);
1941 rsp->PersistentFileId = 0;
1942 rsp->CreateContextsOffset = 0;
1943 rsp->CreateContextsLength = 0;
1944
1945 inc_rfc1001_len(rsp, 88); /* StructureSize - 1*/
1946 kfree(name);
1947 return 0;
1948
1949out:
Marios Makassikis79caa962021-05-06 11:38:35 +09001950 switch (err) {
1951 case -EINVAL:
1952 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
1953 break;
1954 case -ENOSPC:
1955 case -ENOMEM:
1956 rsp->hdr.Status = STATUS_NO_MEMORY;
1957 break;
1958 }
1959
1960 if (!IS_ERR(name))
1961 kfree(name);
1962
Namjae Jeone2f34482021-03-16 10:49:09 +09001963 smb2_set_err_rsp(work);
1964 return err;
1965}
1966
Namjae Jeone2f34482021-03-16 10:49:09 +09001967/**
1968 * smb2_set_ea() - handler for setting extended attributes using set
1969 * info command
1970 * @eabuf: set info command buffer
1971 * @path: dentry path for get ea
1972 *
1973 * Return: 0 on success, otherwise error
1974 */
1975static int smb2_set_ea(struct smb2_ea_info *eabuf, struct path *path)
1976{
1977 char *attr_name = NULL, *value;
1978 int rc = 0;
1979 int next = 0;
1980
1981 attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL);
1982 if (!attr_name)
1983 return -ENOMEM;
1984
1985 do {
1986 if (!eabuf->EaNameLength)
1987 goto next;
1988
1989 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09001990 "name : <%s>, name_len : %u, value_len : %u, next : %u\n",
1991 eabuf->name, eabuf->EaNameLength,
1992 le16_to_cpu(eabuf->EaValueLength),
1993 le32_to_cpu(eabuf->NextEntryOffset));
Namjae Jeone2f34482021-03-16 10:49:09 +09001994
1995 if (eabuf->EaNameLength >
Namjae Jeon070fb212021-05-26 17:57:12 +09001996 (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09001997 rc = -EINVAL;
1998 break;
1999 }
2000
2001 memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2002 memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002003 eabuf->EaNameLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09002004 attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0';
2005 value = (char *)&eabuf->name + eabuf->EaNameLength + 1;
2006
2007 if (!eabuf->EaValueLength) {
2008 rc = ksmbd_vfs_casexattr_len(path->dentry,
2009 attr_name,
2010 XATTR_USER_PREFIX_LEN +
2011 eabuf->EaNameLength);
2012
2013 /* delete the EA only when it exits */
2014 if (rc > 0) {
2015 rc = ksmbd_vfs_remove_xattr(path->dentry,
2016 attr_name);
2017
2018 if (rc < 0) {
2019 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002020 "remove xattr failed(%d)\n",
2021 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002022 break;
2023 }
2024 }
2025
2026 /* if the EA doesn't exist, just do nothing. */
2027 rc = 0;
2028 } else {
2029 rc = ksmbd_vfs_setxattr(path->dentry, attr_name, value,
Namjae Jeon070fb212021-05-26 17:57:12 +09002030 le16_to_cpu(eabuf->EaValueLength), 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09002031 if (rc < 0) {
2032 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002033 "ksmbd_vfs_setxattr is failed(%d)\n",
2034 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002035 break;
2036 }
2037 }
2038
2039next:
2040 next = le32_to_cpu(eabuf->NextEntryOffset);
2041 eabuf = (struct smb2_ea_info *)((char *)eabuf + next);
2042 } while (next != 0);
2043
2044 kfree(attr_name);
2045 return rc;
2046}
2047
2048static inline int check_context_err(void *ctx, char *str)
2049{
2050 int err;
2051
2052 err = PTR_ERR(ctx);
2053 ksmbd_debug(SMB, "find context %s err %d\n", str, err);
2054
2055 if (err == -EINVAL) {
2056 ksmbd_err("bad name length\n");
2057 return err;
2058 }
2059
2060 return 0;
2061}
2062
2063static noinline int smb2_set_stream_name_xattr(struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002064 struct ksmbd_file *fp,
2065 char *stream_name, int s_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09002066{
2067 size_t xattr_stream_size;
2068 char *xattr_stream_name;
2069 int rc;
2070
2071 rc = ksmbd_vfs_xattr_stream_name(stream_name,
2072 &xattr_stream_name,
2073 &xattr_stream_size,
2074 s_type);
2075 if (rc)
2076 return rc;
2077
2078 fp->stream.name = xattr_stream_name;
2079 fp->stream.size = xattr_stream_size;
2080
2081 /* Check if there is stream prefix in xattr space */
2082 rc = ksmbd_vfs_casexattr_len(path->dentry,
2083 xattr_stream_name,
2084 xattr_stream_size);
2085 if (rc >= 0)
2086 return 0;
2087
2088 if (fp->cdoption == FILE_OPEN_LE) {
2089 ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc);
2090 return -EBADF;
2091 }
2092
2093 rc = ksmbd_vfs_setxattr(path->dentry, xattr_stream_name, NULL, 0, 0);
2094 if (rc < 0)
2095 ksmbd_err("Failed to store XATTR stream name :%d\n", rc);
2096 return 0;
2097}
2098
2099static int smb2_remove_smb_xattrs(struct dentry *dentry)
2100{
2101 char *name, *xattr_list = NULL;
2102 ssize_t xattr_list_len;
2103 int err = 0;
2104
2105 xattr_list_len = ksmbd_vfs_listxattr(dentry, &xattr_list);
2106 if (xattr_list_len < 0) {
2107 goto out;
2108 } else if (!xattr_list_len) {
2109 ksmbd_debug(SMB, "empty xattr in the file\n");
2110 goto out;
2111 }
2112
2113 for (name = xattr_list; name - xattr_list < xattr_list_len;
2114 name += strlen(name) + 1) {
2115 ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name));
2116
2117 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002118 strncmp(&name[XATTR_USER_PREFIX_LEN], DOS_ATTRIBUTE_PREFIX,
2119 DOS_ATTRIBUTE_PREFIX_LEN) &&
2120 strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09002121 continue;
2122
2123 err = ksmbd_vfs_remove_xattr(dentry, name);
2124 if (err)
2125 ksmbd_debug(SMB, "remove xattr failed : %s\n", name);
2126 }
2127out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09002128 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09002129 return err;
2130}
2131
2132static int smb2_create_truncate(struct path *path)
2133{
2134 int rc = vfs_truncate(path, 0);
2135
2136 if (rc) {
2137 ksmbd_err("vfs_truncate failed, rc %d\n", rc);
2138 return rc;
2139 }
2140
2141 rc = smb2_remove_smb_xattrs(path->dentry);
2142 if (rc == -EOPNOTSUPP)
2143 rc = 0;
2144 if (rc)
2145 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002146 "ksmbd_truncate_stream_name_xattr failed, rc %d\n",
2147 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002148 return rc;
2149}
2150
Namjae Jeon64b39f42021-03-30 14:25:35 +09002151static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, struct path *path,
Namjae Jeon070fb212021-05-26 17:57:12 +09002152 struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002153{
2154 struct xattr_dos_attrib da = {0};
2155 int rc;
2156
2157 if (!test_share_config_flag(tcon->share_conf,
2158 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
2159 return;
2160
2161 da.version = 4;
2162 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
2163 da.itime = da.create_time = fp->create_time;
2164 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
2165 XATTR_DOSINFO_ITIME;
2166
2167 rc = ksmbd_vfs_set_dos_attrib_xattr(path->dentry, &da);
2168 if (rc)
2169 ksmbd_debug(SMB, "failed to store file attribute into xattr\n");
2170}
2171
2172static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon,
Namjae Jeon070fb212021-05-26 17:57:12 +09002173 struct path *path, struct ksmbd_file *fp)
Namjae Jeone2f34482021-03-16 10:49:09 +09002174{
2175 struct xattr_dos_attrib da;
2176 int rc;
2177
2178 fp->f_ci->m_fattr &= ~(ATTR_HIDDEN_LE | ATTR_SYSTEM_LE);
2179
2180 /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */
2181 if (!test_share_config_flag(tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002182 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS))
Namjae Jeone2f34482021-03-16 10:49:09 +09002183 return;
2184
2185 rc = ksmbd_vfs_get_dos_attrib_xattr(path->dentry, &da);
2186 if (rc > 0) {
2187 fp->f_ci->m_fattr = cpu_to_le32(da.attr);
2188 fp->create_time = da.create_time;
2189 fp->itime = da.itime;
2190 }
2191}
2192
Namjae Jeon64b39f42021-03-30 14:25:35 +09002193static int smb2_creat(struct ksmbd_work *work, struct path *path, char *name,
Namjae Jeon070fb212021-05-26 17:57:12 +09002194 int open_flags, umode_t posix_mode, bool is_dir)
Namjae Jeone2f34482021-03-16 10:49:09 +09002195{
2196 struct ksmbd_tree_connect *tcon = work->tcon;
2197 struct ksmbd_share_config *share = tcon->share_conf;
2198 umode_t mode;
2199 int rc;
2200
2201 if (!(open_flags & O_CREAT))
2202 return -EBADF;
2203
2204 ksmbd_debug(SMB, "file does not exist, so creating\n");
2205 if (is_dir == true) {
2206 ksmbd_debug(SMB, "creating directory\n");
2207
2208 mode = share_config_directory_mode(share, posix_mode);
2209 rc = ksmbd_vfs_mkdir(work, name, mode);
2210 if (rc)
2211 return rc;
2212 } else {
2213 ksmbd_debug(SMB, "creating regular file\n");
2214
2215 mode = share_config_create_mode(share, posix_mode);
2216 rc = ksmbd_vfs_create(work, name, mode);
2217 if (rc)
2218 return rc;
2219 }
2220
2221 rc = ksmbd_vfs_kern_path(name, 0, path, 0);
2222 if (rc) {
2223 ksmbd_err("cannot get linux path (%s), err = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002224 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002225 return rc;
2226 }
2227 return 0;
2228}
2229
2230static int smb2_create_sd_buffer(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09002231 struct smb2_create_req *req,
2232 struct dentry *dentry)
Namjae Jeone2f34482021-03-16 10:49:09 +09002233{
2234 struct create_context *context;
2235 int rc = -ENOENT;
2236
2237 if (!req->CreateContextsOffset)
2238 return rc;
2239
2240 /* Parse SD BUFFER create contexts */
2241 context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER);
2242 if (context && !IS_ERR(context)) {
2243 struct create_sd_buf_req *sd_buf;
2244
2245 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002246 "Set ACLs using SMB2_CREATE_SD_BUFFER context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002247 sd_buf = (struct create_sd_buf_req *)context;
2248 rc = set_info_sec(work->conn, work->tcon, dentry, &sd_buf->ntsd,
Namjae Jeon070fb212021-05-26 17:57:12 +09002249 le32_to_cpu(sd_buf->ccontext.DataLength), true);
Namjae Jeone2f34482021-03-16 10:49:09 +09002250 }
2251
2252 return rc;
2253}
2254
Namjae Jeon3d47e542021-04-20 14:25:35 +09002255static void ksmbd_acls_fattr(struct smb_fattr *fattr, struct inode *inode)
2256{
2257 fattr->cf_uid = inode->i_uid;
2258 fattr->cf_gid = inode->i_gid;
2259 fattr->cf_mode = inode->i_mode;
2260 fattr->cf_dacls = NULL;
2261
2262 fattr->cf_acls = ksmbd_vfs_get_acl(inode, ACL_TYPE_ACCESS);
2263 if (S_ISDIR(inode->i_mode))
2264 fattr->cf_dacls = ksmbd_vfs_get_acl(inode, ACL_TYPE_DEFAULT);
2265}
2266
Namjae Jeone2f34482021-03-16 10:49:09 +09002267/**
2268 * smb2_open() - handler for smb file open request
2269 * @work: smb work containing request buffer
2270 *
2271 * Return: 0 on success, otherwise error
2272 */
2273int smb2_open(struct ksmbd_work *work)
2274{
2275 struct ksmbd_conn *conn = work->conn;
2276 struct ksmbd_session *sess = work->sess;
2277 struct ksmbd_tree_connect *tcon = work->tcon;
2278 struct smb2_create_req *req;
2279 struct smb2_create_rsp *rsp, *rsp_org;
2280 struct path path;
2281 struct ksmbd_share_config *share = tcon->share_conf;
2282 struct ksmbd_file *fp = NULL;
2283 struct file *filp = NULL;
2284 struct kstat stat;
2285 struct create_context *context;
2286 struct lease_ctx_info *lc = NULL;
2287 struct create_ea_buf_req *ea_buf = NULL;
2288 struct oplock_info *opinfo;
2289 __le32 *next_ptr = NULL;
2290 int req_op_level = 0, open_flags = 0, file_info = 0;
2291 int rc = 0, len = 0;
2292 int contxt_cnt = 0, query_disk_id = 0;
2293 int maximal_access_ctxt = 0, posix_ctxt = 0;
2294 int s_type = 0;
2295 int next_off = 0;
2296 char *name = NULL;
2297 char *stream_name = NULL;
2298 bool file_present = false, created = false, already_permitted = false;
Namjae Jeone2f34482021-03-16 10:49:09 +09002299 int share_ret, need_truncate = 0;
2300 u64 time;
2301 umode_t posix_mode = 0;
2302 __le32 daccess, maximal_access = 0;
2303
Namjae Jeone5066492021-03-30 12:35:23 +09002304 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09002305 WORK_BUFFERS(work, req, rsp);
2306
2307 if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002308 (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002309 ksmbd_debug(SMB, "invalid flag in chained command\n");
2310 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
2311 smb2_set_err_rsp(work);
2312 return -EINVAL;
2313 }
2314
2315 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) {
2316 ksmbd_debug(SMB, "IPC pipe create request\n");
2317 return create_smb2_pipe(work);
2318 }
2319
2320 if (req->NameLength) {
2321 if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002322 *(char *)req->Buffer == '\\') {
Colin Ian King1e853b92021-03-17 09:36:58 +00002323 ksmbd_err("not allow directory name included leading slash\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002324 rc = -EINVAL;
2325 goto err_out1;
2326 }
2327
2328 name = smb2_get_name(share,
2329 req->Buffer,
2330 le16_to_cpu(req->NameLength),
2331 work->conn->local_nls);
2332 if (IS_ERR(name)) {
2333 rc = PTR_ERR(name);
2334 if (rc != -ENOMEM)
2335 rc = -ENOENT;
2336 goto err_out1;
2337 }
2338
2339 ksmbd_debug(SMB, "converted name = %s\n", name);
2340 if (strchr(name, ':')) {
2341 if (!test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002342 KSMBD_SHARE_FLAG_STREAMS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002343 rc = -EBADF;
2344 goto err_out1;
2345 }
2346 rc = parse_stream_name(name, &stream_name, &s_type);
2347 if (rc < 0)
2348 goto err_out1;
2349 }
2350
2351 rc = ksmbd_validate_filename(name);
2352 if (rc < 0)
2353 goto err_out1;
2354
2355 if (ksmbd_share_veto_filename(share, name)) {
2356 rc = -ENOENT;
2357 ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002358 name);
Namjae Jeone2f34482021-03-16 10:49:09 +09002359 goto err_out1;
2360 }
2361 } else {
2362 len = strlen(share->path);
2363 ksmbd_debug(SMB, "share path len %d\n", len);
2364 name = kmalloc(len + 1, GFP_KERNEL);
2365 if (!name) {
2366 rsp->hdr.Status = STATUS_NO_MEMORY;
2367 rc = -ENOMEM;
2368 goto err_out1;
2369 }
2370
2371 memcpy(name, share->path, len);
2372 *(name + len) = '\0';
2373 }
2374
2375 req_op_level = req->RequestedOplockLevel;
Namjae Jeon73f9dad2021-04-16 14:12:06 +09002376 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002377 lc = parse_lease_state(req);
Namjae Jeone2f34482021-03-16 10:49:09 +09002378
Namjae Jeon64b39f42021-03-30 14:25:35 +09002379 if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002380 ksmbd_err("Invalid impersonationlevel : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002381 le32_to_cpu(req->ImpersonationLevel));
Namjae Jeone2f34482021-03-16 10:49:09 +09002382 rc = -EIO;
2383 rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL;
2384 goto err_out1;
2385 }
2386
2387 if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK)) {
2388 ksmbd_err("Invalid create options : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002389 le32_to_cpu(req->CreateOptions));
Namjae Jeone2f34482021-03-16 10:49:09 +09002390 rc = -EINVAL;
2391 goto err_out1;
2392 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09002393 if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002394 req->CreateOptions & FILE_RANDOM_ACCESS_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002395 req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE);
2396
Namjae Jeon070fb212021-05-26 17:57:12 +09002397 if (req->CreateOptions &
2398 (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION |
2399 FILE_RESERVE_OPFILTER_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002400 rc = -EOPNOTSUPP;
2401 goto err_out1;
2402 }
2403
2404 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2405 if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) {
2406 rc = -EINVAL;
2407 goto err_out1;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002408 } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002409 req->CreateOptions = ~(FILE_NO_COMPRESSION_LE);
Namjae Jeon64b39f42021-03-30 14:25:35 +09002410 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002411 }
2412 }
2413
2414 if (le32_to_cpu(req->CreateDisposition) >
Namjae Jeon070fb212021-05-26 17:57:12 +09002415 le32_to_cpu(FILE_OVERWRITE_IF_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002416 ksmbd_err("Invalid create disposition : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002417 le32_to_cpu(req->CreateDisposition));
Namjae Jeone2f34482021-03-16 10:49:09 +09002418 rc = -EINVAL;
2419 goto err_out1;
2420 }
2421
2422 if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) {
Colin Ian King1e853b92021-03-17 09:36:58 +00002423 ksmbd_err("Invalid desired access : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002424 le32_to_cpu(req->DesiredAccess));
Namjae Jeone2f34482021-03-16 10:49:09 +09002425 rc = -EACCES;
2426 goto err_out1;
2427 }
2428
Namjae Jeon64b39f42021-03-30 14:25:35 +09002429 if (req->FileAttributes && !(req->FileAttributes & ATTR_MASK_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002430 ksmbd_err("Invalid file attribute : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002431 le32_to_cpu(req->FileAttributes));
Namjae Jeone2f34482021-03-16 10:49:09 +09002432 rc = -EINVAL;
2433 goto err_out1;
2434 }
2435
2436 if (req->CreateContextsOffset) {
2437 /* Parse non-durable handle create contexts */
2438 context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER);
2439 if (IS_ERR(context)) {
2440 rc = check_context_err(context, SMB2_CREATE_EA_BUFFER);
2441 if (rc < 0)
2442 goto err_out1;
2443 } else {
2444 ea_buf = (struct create_ea_buf_req *)context;
2445 if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) {
2446 rsp->hdr.Status = STATUS_ACCESS_DENIED;
2447 rc = -EACCES;
2448 goto err_out1;
2449 }
2450 }
2451
2452 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002453 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
Namjae Jeone2f34482021-03-16 10:49:09 +09002454 if (IS_ERR(context)) {
2455 rc = check_context_err(context,
Namjae Jeon070fb212021-05-26 17:57:12 +09002456 SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST);
Namjae Jeone2f34482021-03-16 10:49:09 +09002457 if (rc < 0)
2458 goto err_out1;
2459 } else {
2460 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002461 "get query maximal access context\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002462 maximal_access_ctxt = 1;
2463 }
2464
2465 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002466 SMB2_CREATE_TIMEWARP_REQUEST);
Namjae Jeone2f34482021-03-16 10:49:09 +09002467 if (IS_ERR(context)) {
2468 rc = check_context_err(context,
Namjae Jeon070fb212021-05-26 17:57:12 +09002469 SMB2_CREATE_TIMEWARP_REQUEST);
Namjae Jeone2f34482021-03-16 10:49:09 +09002470 if (rc < 0)
2471 goto err_out1;
2472 } else {
2473 ksmbd_debug(SMB, "get timewarp context\n");
2474 rc = -EBADF;
2475 goto err_out1;
2476 }
2477
2478 if (tcon->posix_extensions) {
2479 context = smb2_find_context_vals(req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002480 SMB2_CREATE_TAG_POSIX);
Namjae Jeone2f34482021-03-16 10:49:09 +09002481 if (IS_ERR(context)) {
2482 rc = check_context_err(context,
Namjae Jeon070fb212021-05-26 17:57:12 +09002483 SMB2_CREATE_TAG_POSIX);
Namjae Jeone2f34482021-03-16 10:49:09 +09002484 if (rc < 0)
2485 goto err_out1;
2486 } else {
2487 struct create_posix *posix =
2488 (struct create_posix *)context;
2489 ksmbd_debug(SMB, "get posix context\n");
2490
2491 posix_mode = le32_to_cpu(posix->Mode);
2492 posix_ctxt = 1;
2493 }
2494 }
2495 }
2496
2497 if (ksmbd_override_fsids(work)) {
2498 rc = -ENOMEM;
2499 goto err_out1;
2500 }
2501
2502 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) {
2503 /*
2504 * On delete request, instead of following up, need to
2505 * look the current entity
2506 */
2507 rc = ksmbd_vfs_kern_path(name, 0, &path, 1);
2508 if (!rc) {
2509 /*
2510 * If file exists with under flags, return access
2511 * denied error.
2512 */
2513 if (req->CreateDisposition == FILE_OVERWRITE_IF_LE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09002514 req->CreateDisposition == FILE_OPEN_IF_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002515 rc = -EACCES;
2516 path_put(&path);
2517 goto err_out;
2518 }
2519
Namjae Jeon64b39f42021-03-30 14:25:35 +09002520 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002521 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002522 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002523 rc = -EACCES;
2524 path_put(&path);
2525 goto err_out;
2526 }
2527 }
2528 } else {
2529 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002530 KSMBD_SHARE_FLAG_FOLLOW_SYMLINKS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002531 /*
2532 * Use LOOKUP_FOLLOW to follow the path of
2533 * symlink in path buildup
2534 */
2535 rc = ksmbd_vfs_kern_path(name, LOOKUP_FOLLOW, &path, 1);
2536 if (rc) { /* Case for broken link ?*/
2537 rc = ksmbd_vfs_kern_path(name, 0, &path, 1);
2538 }
2539 } else {
2540 rc = ksmbd_vfs_kern_path(name, 0, &path, 1);
2541 if (!rc && d_is_symlink(path.dentry)) {
2542 rc = -EACCES;
2543 path_put(&path);
2544 goto err_out;
2545 }
2546 }
2547 }
2548
2549 if (rc) {
2550 if (rc == -EACCES) {
2551 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002552 "User does not have right permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002553 goto err_out;
2554 }
2555 ksmbd_debug(SMB, "can not get linux path for %s, rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002556 name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002557 rc = 0;
2558 } else {
2559 file_present = true;
2560 generic_fillattr(&init_user_ns, d_inode(path.dentry), &stat);
2561 }
2562 if (stream_name) {
2563 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) {
2564 if (s_type == DATA_STREAM) {
2565 rc = -EIO;
2566 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2567 }
2568 } else {
2569 if (S_ISDIR(stat.mode) && s_type == DATA_STREAM) {
2570 rc = -EIO;
2571 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2572 }
2573 }
2574
2575 if (req->CreateOptions & FILE_DIRECTORY_FILE_LE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002576 req->FileAttributes & ATTR_NORMAL_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002577 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2578 rc = -EIO;
2579 }
2580
2581 if (rc < 0)
2582 goto err_out;
2583 }
2584
Namjae Jeon64b39f42021-03-30 14:25:35 +09002585 if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE &&
2586 S_ISDIR(stat.mode) && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002587 ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002588 name, req->CreateOptions);
Namjae Jeone2f34482021-03-16 10:49:09 +09002589 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
2590 rc = -EIO;
2591 goto err_out;
2592 }
2593
2594 if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002595 !(req->CreateDisposition == FILE_CREATE_LE) &&
2596 !S_ISDIR(stat.mode)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002597 rsp->hdr.Status = STATUS_NOT_A_DIRECTORY;
2598 rc = -EIO;
2599 goto err_out;
2600 }
2601
2602 if (!stream_name && file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002603 req->CreateDisposition == FILE_CREATE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002604 rc = -EEXIST;
2605 goto err_out;
2606 }
2607
Namjae Jeone2f34482021-03-16 10:49:09 +09002608 daccess = smb_map_generic_desired_access(req->DesiredAccess);
2609
2610 if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) {
2611 rc = smb_check_perm_dacl(conn, path.dentry, &daccess,
Namjae Jeon070fb212021-05-26 17:57:12 +09002612 sess->user->uid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002613 if (rc)
2614 goto err_out;
2615 }
2616
2617 if (daccess & FILE_MAXIMAL_ACCESS_LE) {
2618 if (!file_present) {
2619 daccess = cpu_to_le32(GENERIC_ALL_FLAGS);
2620 } else {
2621 rc = ksmbd_vfs_query_maximal_access(path.dentry,
2622 &daccess);
2623 if (rc)
2624 goto err_out;
2625 already_permitted = true;
2626 }
2627 maximal_access = daccess;
2628 }
2629
Namjae Jeon070fb212021-05-26 17:57:12 +09002630 open_flags = smb2_create_open_flags(file_present, daccess,
2631 req->CreateDisposition);
Namjae Jeone2f34482021-03-16 10:49:09 +09002632
2633 if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
2634 if (open_flags & O_CREAT) {
2635 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002636 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09002637 rc = -EACCES;
2638 goto err_out;
2639 }
2640 }
2641
2642 /*create file if not present */
2643 if (!file_present) {
2644 rc = smb2_creat(work, &path, name, open_flags, posix_mode,
Namjae Jeon070fb212021-05-26 17:57:12 +09002645 req->CreateOptions & FILE_DIRECTORY_FILE_LE);
Namjae Jeone2f34482021-03-16 10:49:09 +09002646 if (rc)
2647 goto err_out;
2648
2649 created = true;
2650 if (ea_buf) {
2651 rc = smb2_set_ea(&ea_buf->ea, &path);
2652 if (rc == -EOPNOTSUPP)
2653 rc = 0;
2654 else if (rc)
2655 goto err_out;
2656 }
2657 } else if (!already_permitted) {
2658 bool may_delete;
2659
2660 may_delete = daccess & FILE_DELETE_LE ||
2661 req->CreateOptions & FILE_DELETE_ON_CLOSE_LE;
2662
2663 /* FILE_READ_ATTRIBUTE is allowed without inode_permission,
2664 * because execute(search) permission on a parent directory,
2665 * is already granted.
2666 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09002667 if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) {
Namjae Jeonff1d5722021-04-13 13:18:10 +09002668 rc = ksmbd_vfs_inode_permission(path.dentry,
Namjae Jeon070fb212021-05-26 17:57:12 +09002669 open_flags & O_ACCMODE,
2670 may_delete);
Namjae Jeonff1d5722021-04-13 13:18:10 +09002671 if (rc)
Namjae Jeone2f34482021-03-16 10:49:09 +09002672 goto err_out;
Namjae Jeone2f34482021-03-16 10:49:09 +09002673 }
2674 }
2675
2676 rc = ksmbd_query_inode_status(d_inode(path.dentry->d_parent));
2677 if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) {
2678 rc = -EBUSY;
2679 goto err_out;
2680 }
2681
2682 rc = 0;
2683 filp = dentry_open(&path, open_flags, current_cred());
2684 if (IS_ERR(filp)) {
2685 rc = PTR_ERR(filp);
2686 ksmbd_err("dentry open for dir failed, rc %d\n", rc);
2687 goto err_out;
2688 }
2689
2690 if (file_present) {
2691 if (!(open_flags & O_TRUNC))
2692 file_info = FILE_OPENED;
2693 else
2694 file_info = FILE_OVERWRITTEN;
2695
Namjae Jeon070fb212021-05-26 17:57:12 +09002696 if ((req->CreateDisposition & FILE_CREATE_MASK_LE) ==
2697 FILE_SUPERSEDE_LE)
Namjae Jeone2f34482021-03-16 10:49:09 +09002698 file_info = FILE_SUPERSEDED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002699 } else if (open_flags & O_CREAT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002700 file_info = FILE_CREATED;
Namjae Jeon64b39f42021-03-30 14:25:35 +09002701 }
Namjae Jeone2f34482021-03-16 10:49:09 +09002702
2703 ksmbd_vfs_set_fadvise(filp, req->CreateOptions);
2704
2705 /* Obtain Volatile-ID */
2706 fp = ksmbd_open_fd(work, filp);
2707 if (IS_ERR(fp)) {
2708 fput(filp);
2709 rc = PTR_ERR(fp);
2710 fp = NULL;
2711 goto err_out;
2712 }
2713
2714 /* Get Persistent-ID */
2715 ksmbd_open_durable_fd(fp);
2716 if (!HAS_FILE_ID(fp->persistent_id)) {
2717 rc = -ENOMEM;
2718 goto err_out;
2719 }
2720
2721 fp->filename = name;
2722 fp->cdoption = req->CreateDisposition;
2723 fp->daccess = daccess;
2724 fp->saccess = req->ShareAccess;
2725 fp->coption = req->CreateOptions;
2726
2727 /* Set default windows and posix acls if creating new file */
2728 if (created) {
2729 int posix_acl_rc;
Namjae Jeonfba08fa2021-04-15 10:29:39 +09002730 struct inode *inode = d_inode(path.dentry);
Namjae Jeone2f34482021-03-16 10:49:09 +09002731
Namjae Jeonfba08fa2021-04-15 10:29:39 +09002732 posix_acl_rc = ksmbd_vfs_inherit_posix_acl(inode, d_inode(path.dentry->d_parent));
Namjae Jeone2f34482021-03-16 10:49:09 +09002733 if (posix_acl_rc)
2734 ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc);
2735
2736 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002737 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002738 rc = smb_inherit_dacl(conn, path.dentry, sess->user->uid,
Namjae Jeon070fb212021-05-26 17:57:12 +09002739 sess->user->gid);
Namjae Jeone2f34482021-03-16 10:49:09 +09002740 }
2741
2742 if (rc) {
2743 rc = smb2_create_sd_buffer(work, req, path.dentry);
2744 if (rc) {
2745 if (posix_acl_rc)
2746 ksmbd_vfs_set_init_posix_acl(inode);
2747
2748 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09002749 KSMBD_SHARE_FLAG_ACL_XATTR)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002750 struct smb_fattr fattr;
2751 struct smb_ntsd *pntsd;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002752 int pntsd_size, ace_num = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09002753
Namjae Jeon3d47e542021-04-20 14:25:35 +09002754 ksmbd_acls_fattr(&fattr, inode);
Marios Makassikise6b10592021-04-15 10:24:56 +09002755 if (fattr.cf_acls)
2756 ace_num = fattr.cf_acls->a_count;
Namjae Jeon3d47e542021-04-20 14:25:35 +09002757 if (fattr.cf_dacls)
2758 ace_num += fattr.cf_dacls->a_count;
Namjae Jeone2f34482021-03-16 10:49:09 +09002759
2760 pntsd = kmalloc(sizeof(struct smb_ntsd) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002761 sizeof(struct smb_sid) * 3 +
Namjae Jeone2f34482021-03-16 10:49:09 +09002762 sizeof(struct smb_acl) +
Namjae Jeon64b39f42021-03-30 14:25:35 +09002763 sizeof(struct smb_ace) * ace_num * 2,
Namjae Jeone2f34482021-03-16 10:49:09 +09002764 GFP_KERNEL);
2765 if (!pntsd)
2766 goto err_out;
2767
2768 rc = build_sec_desc(pntsd, NULL,
Namjae Jeon070fb212021-05-26 17:57:12 +09002769 OWNER_SECINFO |
2770 GROUP_SECINFO |
2771 DACL_SECINFO,
2772 &pntsd_size, &fattr);
Namjae Jeone2f34482021-03-16 10:49:09 +09002773 posix_acl_release(fattr.cf_acls);
2774 posix_acl_release(fattr.cf_dacls);
2775
2776 rc = ksmbd_vfs_set_sd_xattr(conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09002777 path.dentry,
2778 pntsd,
2779 pntsd_size);
Namjae Jeon3d47e542021-04-20 14:25:35 +09002780 kfree(pntsd);
Namjae Jeone2f34482021-03-16 10:49:09 +09002781 if (rc)
2782 ksmbd_err("failed to store ntacl in xattr : %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002783 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09002784 }
2785 }
2786 }
2787 rc = 0;
2788 }
2789
2790 if (stream_name) {
2791 rc = smb2_set_stream_name_xattr(&path,
2792 fp,
2793 stream_name,
2794 s_type);
2795 if (rc)
2796 goto err_out;
2797 file_info = FILE_CREATED;
2798 }
2799
2800 fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE |
2801 FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE));
Namjae Jeon64b39f42021-03-30 14:25:35 +09002802 if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC &&
2803 !fp->attrib_only && !stream_name) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002804 smb_break_all_oplock(work, fp);
2805 need_truncate = 1;
2806 }
2807
2808 /* fp should be searchable through ksmbd_inode.m_fp_list
2809 * after daccess, saccess, attrib_only, and stream are
2810 * initialized.
2811 */
2812 write_lock(&fp->f_ci->m_lock);
2813 list_add(&fp->node, &fp->f_ci->m_fp_list);
2814 write_unlock(&fp->f_ci->m_lock);
2815
2816 rc = ksmbd_vfs_getattr(&path, &stat);
2817 if (rc) {
2818 generic_fillattr(&init_user_ns, d_inode(path.dentry), &stat);
2819 rc = 0;
2820 }
2821
2822 /* Check delete pending among previous fp before oplock break */
2823 if (ksmbd_inode_pending_delete(fp)) {
2824 rc = -EBUSY;
2825 goto err_out;
2826 }
2827
2828 share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp);
Namjae Jeon64b39f42021-03-30 14:25:35 +09002829 if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) ||
2830 (req_op_level == SMB2_OPLOCK_LEVEL_LEASE &&
2831 !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09002832 if (share_ret < 0 && !S_ISDIR(FP_INODE(fp)->i_mode)) {
2833 rc = share_ret;
2834 goto err_out;
2835 }
2836 } else {
2837 if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) {
2838 req_op_level = smb2_map_lease_to_oplock(lc->req_state);
2839 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002840 "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n",
2841 name, req_op_level, lc->req_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09002842 rc = find_same_lease_key(sess, fp->f_ci, lc);
2843 if (rc)
2844 goto err_out;
2845 } else if (open_flags == O_RDONLY &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09002846 (req_op_level == SMB2_OPLOCK_LEVEL_BATCH ||
2847 req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE))
Namjae Jeone2f34482021-03-16 10:49:09 +09002848 req_op_level = SMB2_OPLOCK_LEVEL_II;
2849
2850 rc = smb_grant_oplock(work, req_op_level,
2851 fp->persistent_id, fp,
2852 le32_to_cpu(req->hdr.Id.SyncId.TreeId),
2853 lc, share_ret);
2854 if (rc < 0)
2855 goto err_out;
2856 }
2857
2858 if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)
2859 ksmbd_fd_set_delete_on_close(fp, file_info);
2860
2861 if (need_truncate) {
2862 rc = smb2_create_truncate(&path);
2863 if (rc)
2864 goto err_out;
2865 }
2866
2867 if (req->CreateContextsOffset) {
2868 struct create_alloc_size_req *az_req;
2869
Namjae Jeon070fb212021-05-26 17:57:12 +09002870 az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req,
2871 SMB2_CREATE_ALLOCATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09002872 if (IS_ERR(az_req)) {
2873 rc = check_context_err(az_req,
Namjae Jeon070fb212021-05-26 17:57:12 +09002874 SMB2_CREATE_ALLOCATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09002875 if (rc < 0)
2876 goto err_out;
2877 } else {
2878 loff_t alloc_size = le64_to_cpu(az_req->AllocationSize);
2879 int err;
2880
2881 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002882 "request smb2 create allocate size : %llu\n",
2883 alloc_size);
Namjae Jeone2f34482021-03-16 10:49:09 +09002884 err = ksmbd_vfs_alloc_size(work, fp, alloc_size);
2885 if (err < 0)
2886 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09002887 "ksmbd_vfs_alloc_size is failed : %d\n",
2888 err);
Namjae Jeone2f34482021-03-16 10:49:09 +09002889 }
2890
Namjae Jeon64b39f42021-03-30 14:25:35 +09002891 context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID);
Namjae Jeone2f34482021-03-16 10:49:09 +09002892 if (IS_ERR(context)) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09002893 rc = check_context_err(context, SMB2_CREATE_QUERY_ON_DISK_ID);
Namjae Jeone2f34482021-03-16 10:49:09 +09002894 if (rc < 0)
2895 goto err_out;
2896 } else {
2897 ksmbd_debug(SMB, "get query on disk id context\n");
2898 query_disk_id = 1;
2899 }
2900 }
2901
2902 if (stat.result_mask & STATX_BTIME)
2903 fp->create_time = ksmbd_UnixTimeToNT(stat.btime);
2904 else
2905 fp->create_time = ksmbd_UnixTimeToNT(stat.ctime);
2906 if (req->FileAttributes || fp->f_ci->m_fattr == 0)
Namjae Jeon070fb212021-05-26 17:57:12 +09002907 fp->f_ci->m_fattr =
2908 cpu_to_le32(smb2_get_dos_mode(&stat, le32_to_cpu(req->FileAttributes)));
Namjae Jeone2f34482021-03-16 10:49:09 +09002909
2910 if (!created)
2911 smb2_update_xattrs(tcon, &path, fp);
2912 else
2913 smb2_new_xattrs(tcon, &path, fp);
2914
2915 memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE);
2916
Namjae Jeone2f34482021-03-16 10:49:09 +09002917 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
2918
2919 rsp->StructureSize = cpu_to_le16(89);
2920 rcu_read_lock();
2921 opinfo = rcu_dereference(fp->f_opinfo);
2922 rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0;
2923 rcu_read_unlock();
2924 rsp->Reserved = 0;
2925 rsp->CreateAction = cpu_to_le32(file_info);
2926 rsp->CreationTime = cpu_to_le64(fp->create_time);
2927 time = ksmbd_UnixTimeToNT(stat.atime);
2928 rsp->LastAccessTime = cpu_to_le64(time);
2929 time = ksmbd_UnixTimeToNT(stat.mtime);
2930 rsp->LastWriteTime = cpu_to_le64(time);
2931 time = ksmbd_UnixTimeToNT(stat.ctime);
2932 rsp->ChangeTime = cpu_to_le64(time);
2933 rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 :
2934 cpu_to_le64(stat.blocks << 9);
2935 rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
2936 rsp->FileAttributes = fp->f_ci->m_fattr;
2937
2938 rsp->Reserved2 = 0;
2939
2940 rsp->PersistentFileId = cpu_to_le64(fp->persistent_id);
2941 rsp->VolatileFileId = cpu_to_le64(fp->volatile_id);
2942
2943 rsp->CreateContextsOffset = 0;
2944 rsp->CreateContextsLength = 0;
2945 inc_rfc1001_len(rsp_org, 88); /* StructureSize - 1*/
2946
2947 /* If lease is request send lease context response */
2948 if (opinfo && opinfo->is_lease) {
2949 struct create_context *lease_ccontext;
2950
2951 ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09002952 name, opinfo->o_lease->state);
Namjae Jeone2f34482021-03-16 10:49:09 +09002953 rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE;
2954
2955 lease_ccontext = (struct create_context *)rsp->Buffer;
2956 contxt_cnt++;
2957 create_lease_buf(rsp->Buffer, opinfo->o_lease);
2958 le32_add_cpu(&rsp->CreateContextsLength,
2959 conn->vals->create_lease_size);
2960 inc_rfc1001_len(rsp_org, conn->vals->create_lease_size);
2961 next_ptr = &lease_ccontext->Next;
2962 next_off = conn->vals->create_lease_size;
2963 }
2964
Namjae Jeone2f34482021-03-16 10:49:09 +09002965 if (maximal_access_ctxt) {
2966 struct create_context *mxac_ccontext;
2967
2968 if (maximal_access == 0)
2969 ksmbd_vfs_query_maximal_access(path.dentry,
2970 &maximal_access);
2971 mxac_ccontext = (struct create_context *)(rsp->Buffer +
2972 le32_to_cpu(rsp->CreateContextsLength));
2973 contxt_cnt++;
2974 create_mxac_rsp_buf(rsp->Buffer +
2975 le32_to_cpu(rsp->CreateContextsLength),
2976 le32_to_cpu(maximal_access));
2977 le32_add_cpu(&rsp->CreateContextsLength,
2978 conn->vals->create_mxac_size);
2979 inc_rfc1001_len(rsp_org, conn->vals->create_mxac_size);
2980 if (next_ptr)
2981 *next_ptr = cpu_to_le32(next_off);
2982 next_ptr = &mxac_ccontext->Next;
2983 next_off = conn->vals->create_mxac_size;
2984 }
2985
2986 if (query_disk_id) {
2987 struct create_context *disk_id_ccontext;
2988
2989 disk_id_ccontext = (struct create_context *)(rsp->Buffer +
2990 le32_to_cpu(rsp->CreateContextsLength));
2991 contxt_cnt++;
2992 create_disk_id_rsp_buf(rsp->Buffer +
2993 le32_to_cpu(rsp->CreateContextsLength),
2994 stat.ino, tcon->id);
2995 le32_add_cpu(&rsp->CreateContextsLength,
2996 conn->vals->create_disk_id_size);
2997 inc_rfc1001_len(rsp_org, conn->vals->create_disk_id_size);
2998 if (next_ptr)
2999 *next_ptr = cpu_to_le32(next_off);
3000 next_ptr = &disk_id_ccontext->Next;
3001 next_off = conn->vals->create_disk_id_size;
3002 }
3003
3004 if (posix_ctxt) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003005 contxt_cnt++;
3006 create_posix_rsp_buf(rsp->Buffer +
3007 le32_to_cpu(rsp->CreateContextsLength),
3008 fp);
3009 le32_add_cpu(&rsp->CreateContextsLength,
3010 conn->vals->create_posix_size);
3011 inc_rfc1001_len(rsp_org, conn->vals->create_posix_size);
3012 if (next_ptr)
3013 *next_ptr = cpu_to_le32(next_off);
3014 }
3015
3016 if (contxt_cnt > 0) {
3017 rsp->CreateContextsOffset =
3018 cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer)
3019 - 4);
3020 }
3021
3022err_out:
3023 if (file_present || created)
3024 path_put(&path);
3025 ksmbd_revert_fsids(work);
3026err_out1:
3027 if (rc) {
3028 if (rc == -EINVAL)
3029 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3030 else if (rc == -EOPNOTSUPP)
3031 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeonff1d5722021-04-13 13:18:10 +09003032 else if (rc == -EACCES || rc == -ESTALE)
Namjae Jeone2f34482021-03-16 10:49:09 +09003033 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3034 else if (rc == -ENOENT)
3035 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
3036 else if (rc == -EPERM)
3037 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
3038 else if (rc == -EBUSY)
3039 rsp->hdr.Status = STATUS_DELETE_PENDING;
3040 else if (rc == -EBADF)
3041 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
3042 else if (rc == -ENOEXEC)
3043 rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID;
3044 else if (rc == -ENXIO)
3045 rsp->hdr.Status = STATUS_NO_SUCH_DEVICE;
3046 else if (rc == -EEXIST)
3047 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
3048 else if (rc == -EMFILE)
3049 rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES;
3050 if (!rsp->hdr.Status)
3051 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3052
3053 if (!fp || !fp->filename)
3054 kfree(name);
3055 if (fp)
3056 ksmbd_fd_put(work, fp);
3057 smb2_set_err_rsp(work);
3058 ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status);
3059 }
3060
3061 kfree(lc);
3062
3063 return 0;
3064}
3065
3066static int readdir_info_level_struct_sz(int info_level)
3067{
3068 switch (info_level) {
3069 case FILE_FULL_DIRECTORY_INFORMATION:
3070 return sizeof(struct file_full_directory_info);
3071 case FILE_BOTH_DIRECTORY_INFORMATION:
3072 return sizeof(struct file_both_directory_info);
3073 case FILE_DIRECTORY_INFORMATION:
3074 return sizeof(struct file_directory_info);
3075 case FILE_NAMES_INFORMATION:
3076 return sizeof(struct file_names_info);
3077 case FILEID_FULL_DIRECTORY_INFORMATION:
3078 return sizeof(struct file_id_full_dir_info);
3079 case FILEID_BOTH_DIRECTORY_INFORMATION:
3080 return sizeof(struct file_id_both_directory_info);
3081 case SMB_FIND_FILE_POSIX_INFO:
3082 return sizeof(struct smb2_posix_info);
3083 default:
3084 return -EOPNOTSUPP;
3085 }
3086}
3087
3088static int dentry_name(struct ksmbd_dir_info *d_info, int info_level)
3089{
3090 switch (info_level) {
3091 case FILE_FULL_DIRECTORY_INFORMATION:
3092 {
3093 struct file_full_directory_info *ffdinfo;
3094
3095 ffdinfo = (struct file_full_directory_info *)d_info->rptr;
3096 d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset);
3097 d_info->name = ffdinfo->FileName;
3098 d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength);
3099 return 0;
3100 }
3101 case FILE_BOTH_DIRECTORY_INFORMATION:
3102 {
3103 struct file_both_directory_info *fbdinfo;
3104
3105 fbdinfo = (struct file_both_directory_info *)d_info->rptr;
3106 d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset);
3107 d_info->name = fbdinfo->FileName;
3108 d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength);
3109 return 0;
3110 }
3111 case FILE_DIRECTORY_INFORMATION:
3112 {
3113 struct file_directory_info *fdinfo;
3114
3115 fdinfo = (struct file_directory_info *)d_info->rptr;
3116 d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset);
3117 d_info->name = fdinfo->FileName;
3118 d_info->name_len = le32_to_cpu(fdinfo->FileNameLength);
3119 return 0;
3120 }
3121 case FILE_NAMES_INFORMATION:
3122 {
3123 struct file_names_info *fninfo;
3124
3125 fninfo = (struct file_names_info *)d_info->rptr;
3126 d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset);
3127 d_info->name = fninfo->FileName;
3128 d_info->name_len = le32_to_cpu(fninfo->FileNameLength);
3129 return 0;
3130 }
3131 case FILEID_FULL_DIRECTORY_INFORMATION:
3132 {
3133 struct file_id_full_dir_info *dinfo;
3134
3135 dinfo = (struct file_id_full_dir_info *)d_info->rptr;
3136 d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset);
3137 d_info->name = dinfo->FileName;
3138 d_info->name_len = le32_to_cpu(dinfo->FileNameLength);
3139 return 0;
3140 }
3141 case FILEID_BOTH_DIRECTORY_INFORMATION:
3142 {
3143 struct file_id_both_directory_info *fibdinfo;
3144
3145 fibdinfo = (struct file_id_both_directory_info *)d_info->rptr;
3146 d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset);
3147 d_info->name = fibdinfo->FileName;
3148 d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength);
3149 return 0;
3150 }
3151 case SMB_FIND_FILE_POSIX_INFO:
3152 {
3153 struct smb2_posix_info *posix_info;
3154
3155 posix_info = (struct smb2_posix_info *)d_info->rptr;
3156 d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset);
3157 d_info->name = posix_info->name;
3158 d_info->name_len = le32_to_cpu(posix_info->name_len);
3159 return 0;
3160 }
3161 default:
3162 return -EINVAL;
3163 }
3164}
3165
3166/**
3167 * smb2_populate_readdir_entry() - encode directory entry in smb2 response
3168 * buffer
3169 * @conn: connection instance
3170 * @info_level: smb information level
3171 * @d_info: structure included variables for query dir
3172 * @ksmbd_kstat: ksmbd wrapper of dirent stat information
3173 *
3174 * if directory has many entries, find first can't read it fully.
3175 * find next might be called multiple times to read remaining dir entries
3176 *
3177 * Return: 0 on success, otherwise error
3178 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003179static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level,
Namjae Jeon070fb212021-05-26 17:57:12 +09003180 struct ksmbd_dir_info *d_info,
3181 struct ksmbd_kstat *ksmbd_kstat)
Namjae Jeone2f34482021-03-16 10:49:09 +09003182{
3183 int next_entry_offset = 0;
3184 char *conv_name;
3185 int conv_len;
3186 void *kstat;
3187 int struct_sz;
3188
3189 conv_name = ksmbd_convert_dir_info_name(d_info,
3190 conn->local_nls,
3191 &conv_len);
3192 if (!conv_name)
3193 return -ENOMEM;
3194
3195 /* Somehow the name has only terminating NULL bytes */
3196 if (conv_len < 0) {
3197 kfree(conv_name);
3198 return -EINVAL;
3199 }
3200
3201 struct_sz = readdir_info_level_struct_sz(info_level);
3202 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3203 KSMBD_DIR_INFO_ALIGNMENT);
3204
3205 if (next_entry_offset > d_info->out_buf_len) {
3206 d_info->out_buf_len = 0;
3207 return -ENOSPC;
3208 }
3209
3210 kstat = d_info->wptr;
3211 if (info_level != FILE_NAMES_INFORMATION)
3212 kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat);
3213
3214 switch (info_level) {
3215 case FILE_FULL_DIRECTORY_INFORMATION:
3216 {
3217 struct file_full_directory_info *ffdinfo;
3218
3219 ffdinfo = (struct file_full_directory_info *)kstat;
3220 ffdinfo->FileNameLength = cpu_to_le32(conv_len);
3221 ffdinfo->EaSize =
3222 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3223 if (ffdinfo->EaSize)
3224 ffdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3225 if (d_info->hide_dot_file && d_info->name[0] == '.')
3226 ffdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3227 memcpy(ffdinfo->FileName, conv_name, conv_len);
3228 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3229 break;
3230 }
3231 case FILE_BOTH_DIRECTORY_INFORMATION:
3232 {
3233 struct file_both_directory_info *fbdinfo;
3234
3235 fbdinfo = (struct file_both_directory_info *)kstat;
3236 fbdinfo->FileNameLength = cpu_to_le32(conv_len);
3237 fbdinfo->EaSize =
3238 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3239 if (fbdinfo->EaSize)
3240 fbdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3241 fbdinfo->ShortNameLength = 0;
3242 fbdinfo->Reserved = 0;
3243 if (d_info->hide_dot_file && d_info->name[0] == '.')
3244 fbdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3245 memcpy(fbdinfo->FileName, conv_name, conv_len);
3246 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3247 break;
3248 }
3249 case FILE_DIRECTORY_INFORMATION:
3250 {
3251 struct file_directory_info *fdinfo;
3252
3253 fdinfo = (struct file_directory_info *)kstat;
3254 fdinfo->FileNameLength = cpu_to_le32(conv_len);
3255 if (d_info->hide_dot_file && d_info->name[0] == '.')
3256 fdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3257 memcpy(fdinfo->FileName, conv_name, conv_len);
3258 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3259 break;
3260 }
3261 case FILE_NAMES_INFORMATION:
3262 {
3263 struct file_names_info *fninfo;
3264
3265 fninfo = (struct file_names_info *)kstat;
3266 fninfo->FileNameLength = cpu_to_le32(conv_len);
3267 memcpy(fninfo->FileName, conv_name, conv_len);
3268 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3269 break;
3270 }
3271 case FILEID_FULL_DIRECTORY_INFORMATION:
3272 {
3273 struct file_id_full_dir_info *dinfo;
3274
3275 dinfo = (struct file_id_full_dir_info *)kstat;
3276 dinfo->FileNameLength = cpu_to_le32(conv_len);
3277 dinfo->EaSize =
3278 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3279 if (dinfo->EaSize)
3280 dinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3281 dinfo->Reserved = 0;
3282 dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3283 if (d_info->hide_dot_file && d_info->name[0] == '.')
3284 dinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3285 memcpy(dinfo->FileName, conv_name, conv_len);
3286 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3287 break;
3288 }
3289 case FILEID_BOTH_DIRECTORY_INFORMATION:
3290 {
3291 struct file_id_both_directory_info *fibdinfo;
3292
3293 fibdinfo = (struct file_id_both_directory_info *)kstat;
3294 fibdinfo->FileNameLength = cpu_to_le32(conv_len);
3295 fibdinfo->EaSize =
3296 smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode);
3297 if (fibdinfo->EaSize)
3298 fibdinfo->ExtFileAttributes = ATTR_REPARSE_POINT_LE;
3299 fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino);
3300 fibdinfo->ShortNameLength = 0;
3301 fibdinfo->Reserved = 0;
3302 fibdinfo->Reserved2 = cpu_to_le16(0);
3303 if (d_info->hide_dot_file && d_info->name[0] == '.')
3304 fibdinfo->ExtFileAttributes |= ATTR_HIDDEN_LE;
3305 memcpy(fibdinfo->FileName, conv_name, conv_len);
3306 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3307 break;
3308 }
3309 case SMB_FIND_FILE_POSIX_INFO:
3310 {
3311 struct smb2_posix_info *posix_info;
3312 u64 time;
3313
3314 posix_info = (struct smb2_posix_info *)kstat;
3315 posix_info->Ignored = 0;
3316 posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time);
3317 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime);
3318 posix_info->ChangeTime = cpu_to_le64(time);
3319 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime);
3320 posix_info->LastAccessTime = cpu_to_le64(time);
3321 time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime);
3322 posix_info->LastWriteTime = cpu_to_le64(time);
3323 posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size);
3324 posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9);
3325 posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev);
3326 posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink);
3327 posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode);
3328 posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino);
3329 posix_info->DosAttributes =
3330 S_ISDIR(ksmbd_kstat->kstat->mode) ? ATTR_DIRECTORY_LE : ATTR_ARCHIVE_LE;
3331 if (d_info->hide_dot_file && d_info->name[0] == '.')
3332 posix_info->DosAttributes |= ATTR_HIDDEN_LE;
3333 id_to_sid(from_kuid(&init_user_ns, ksmbd_kstat->kstat->uid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003334 SIDNFS_USER, (struct smb_sid *)&posix_info->SidBuffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09003335 id_to_sid(from_kgid(&init_user_ns, ksmbd_kstat->kstat->gid),
Namjae Jeon070fb212021-05-26 17:57:12 +09003336 SIDNFS_GROUP, (struct smb_sid *)&posix_info->SidBuffer[20]);
Namjae Jeone2f34482021-03-16 10:49:09 +09003337 memcpy(posix_info->name, conv_name, conv_len);
3338 posix_info->name_len = cpu_to_le32(conv_len);
3339 posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset);
3340 break;
3341 }
3342
3343 } /* switch (info_level) */
3344
3345 d_info->last_entry_offset = d_info->data_count;
3346 d_info->data_count += next_entry_offset;
Marios Makassikise7735c82021-05-06 11:40:02 +09003347 d_info->out_buf_len -= next_entry_offset;
Namjae Jeone2f34482021-03-16 10:49:09 +09003348 d_info->wptr += next_entry_offset;
3349 kfree(conv_name);
3350
3351 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003352 "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n",
3353 info_level, d_info->out_buf_len,
3354 next_entry_offset, d_info->data_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09003355
3356 return 0;
3357}
3358
3359struct smb2_query_dir_private {
3360 struct ksmbd_work *work;
3361 char *search_pattern;
3362 struct ksmbd_file *dir_fp;
3363
3364 struct ksmbd_dir_info *d_info;
3365 int info_level;
3366};
3367
3368static void lock_dir(struct ksmbd_file *dir_fp)
3369{
3370 struct dentry *dir = dir_fp->filp->f_path.dentry;
3371
3372 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
3373}
3374
3375static void unlock_dir(struct ksmbd_file *dir_fp)
3376{
3377 struct dentry *dir = dir_fp->filp->f_path.dentry;
3378
3379 inode_unlock(d_inode(dir));
3380}
3381
3382static int process_query_dir_entries(struct smb2_query_dir_private *priv)
3383{
3384 struct kstat kstat;
3385 struct ksmbd_kstat ksmbd_kstat;
3386 int rc;
3387 int i;
3388
3389 for (i = 0; i < priv->d_info->num_entry; i++) {
3390 struct dentry *dent;
3391
3392 if (dentry_name(priv->d_info, priv->info_level))
3393 return -EINVAL;
3394
3395 lock_dir(priv->dir_fp);
3396 dent = lookup_one_len(priv->d_info->name,
3397 priv->dir_fp->filp->f_path.dentry,
3398 priv->d_info->name_len);
3399 unlock_dir(priv->dir_fp);
3400
3401 if (IS_ERR(dent)) {
3402 ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003403 priv->d_info->name,
3404 PTR_ERR(dent));
Namjae Jeone2f34482021-03-16 10:49:09 +09003405 continue;
3406 }
3407 if (unlikely(d_is_negative(dent))) {
3408 dput(dent);
3409 ksmbd_debug(SMB, "Negative dentry `%s'\n",
3410 priv->d_info->name);
3411 continue;
3412 }
3413
3414 ksmbd_kstat.kstat = &kstat;
3415 if (priv->info_level != FILE_NAMES_INFORMATION)
3416 ksmbd_vfs_fill_dentry_attrs(priv->work,
3417 dent,
3418 &ksmbd_kstat);
3419
3420 rc = smb2_populate_readdir_entry(priv->work->conn,
3421 priv->info_level,
3422 priv->d_info,
3423 &ksmbd_kstat);
3424 dput(dent);
3425 if (rc)
3426 return rc;
3427 }
3428 return 0;
3429}
3430
3431static int reserve_populate_dentry(struct ksmbd_dir_info *d_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09003432 int info_level)
Namjae Jeone2f34482021-03-16 10:49:09 +09003433{
3434 int struct_sz;
3435 int conv_len;
3436 int next_entry_offset;
3437
3438 struct_sz = readdir_info_level_struct_sz(info_level);
3439 if (struct_sz == -EOPNOTSUPP)
3440 return -EOPNOTSUPP;
3441
3442 conv_len = (d_info->name_len + 1) * 2;
3443 next_entry_offset = ALIGN(struct_sz - 1 + conv_len,
3444 KSMBD_DIR_INFO_ALIGNMENT);
3445
3446 if (next_entry_offset > d_info->out_buf_len) {
3447 d_info->out_buf_len = 0;
3448 return -ENOSPC;
3449 }
3450
3451 switch (info_level) {
3452 case FILE_FULL_DIRECTORY_INFORMATION:
3453 {
3454 struct file_full_directory_info *ffdinfo;
3455
3456 ffdinfo = (struct file_full_directory_info *)d_info->wptr;
3457 memcpy(ffdinfo->FileName, d_info->name, d_info->name_len);
3458 ffdinfo->FileName[d_info->name_len] = 0x00;
3459 ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3460 ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3461 break;
3462 }
3463 case FILE_BOTH_DIRECTORY_INFORMATION:
3464 {
3465 struct file_both_directory_info *fbdinfo;
3466
3467 fbdinfo = (struct file_both_directory_info *)d_info->wptr;
3468 memcpy(fbdinfo->FileName, d_info->name, d_info->name_len);
3469 fbdinfo->FileName[d_info->name_len] = 0x00;
3470 fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3471 fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3472 break;
3473 }
3474 case FILE_DIRECTORY_INFORMATION:
3475 {
3476 struct file_directory_info *fdinfo;
3477
3478 fdinfo = (struct file_directory_info *)d_info->wptr;
3479 memcpy(fdinfo->FileName, d_info->name, d_info->name_len);
3480 fdinfo->FileName[d_info->name_len] = 0x00;
3481 fdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3482 fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3483 break;
3484 }
3485 case FILE_NAMES_INFORMATION:
3486 {
3487 struct file_names_info *fninfo;
3488
3489 fninfo = (struct file_names_info *)d_info->wptr;
3490 memcpy(fninfo->FileName, d_info->name, d_info->name_len);
3491 fninfo->FileName[d_info->name_len] = 0x00;
3492 fninfo->FileNameLength = cpu_to_le32(d_info->name_len);
3493 fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3494 break;
3495 }
3496 case FILEID_FULL_DIRECTORY_INFORMATION:
3497 {
3498 struct file_id_full_dir_info *dinfo;
3499
3500 dinfo = (struct file_id_full_dir_info *)d_info->wptr;
3501 memcpy(dinfo->FileName, d_info->name, d_info->name_len);
3502 dinfo->FileName[d_info->name_len] = 0x00;
3503 dinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3504 dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3505 break;
3506 }
3507 case FILEID_BOTH_DIRECTORY_INFORMATION:
3508 {
3509 struct file_id_both_directory_info *fibdinfo;
3510
3511 fibdinfo = (struct file_id_both_directory_info *)d_info->wptr;
3512 memcpy(fibdinfo->FileName, d_info->name, d_info->name_len);
3513 fibdinfo->FileName[d_info->name_len] = 0x00;
3514 fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len);
3515 fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset);
3516 break;
3517 }
3518 case SMB_FIND_FILE_POSIX_INFO:
3519 {
3520 struct smb2_posix_info *posix_info;
3521
3522 posix_info = (struct smb2_posix_info *)d_info->wptr;
3523 memcpy(posix_info->name, d_info->name, d_info->name_len);
3524 posix_info->name[d_info->name_len] = 0x00;
3525 posix_info->name_len = cpu_to_le32(d_info->name_len);
3526 posix_info->NextEntryOffset =
3527 cpu_to_le32(next_entry_offset);
3528 break;
3529 }
3530 } /* switch (info_level) */
3531
3532 d_info->num_entry++;
3533 d_info->out_buf_len -= next_entry_offset;
3534 d_info->wptr += next_entry_offset;
3535 return 0;
3536}
3537
Namjae Jeon64b39f42021-03-30 14:25:35 +09003538static int __query_dir(struct dir_context *ctx, const char *name, int namlen,
Namjae Jeon070fb212021-05-26 17:57:12 +09003539 loff_t offset, u64 ino, unsigned int d_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09003540{
3541 struct ksmbd_readdir_data *buf;
3542 struct smb2_query_dir_private *priv;
3543 struct ksmbd_dir_info *d_info;
3544 int rc;
3545
3546 buf = container_of(ctx, struct ksmbd_readdir_data, ctx);
3547 priv = buf->private;
3548 d_info = priv->d_info;
3549
3550 /* dot and dotdot entries are already reserved */
3551 if (!strcmp(".", name) || !strcmp("..", name))
3552 return 0;
3553 if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name))
3554 return 0;
Namjae Jeonb24c9332021-03-21 17:32:19 +09003555 if (!match_pattern(name, namlen, priv->search_pattern))
Namjae Jeone2f34482021-03-16 10:49:09 +09003556 return 0;
3557
3558 d_info->name = name;
3559 d_info->name_len = namlen;
3560 rc = reserve_populate_dentry(d_info, priv->info_level);
3561 if (rc)
3562 return rc;
3563 if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) {
3564 d_info->out_buf_len = 0;
3565 return 0;
3566 }
3567 return 0;
3568}
3569
3570static void restart_ctx(struct dir_context *ctx)
3571{
3572 ctx->pos = 0;
3573}
3574
3575static int verify_info_level(int info_level)
3576{
3577 switch (info_level) {
3578 case FILE_FULL_DIRECTORY_INFORMATION:
3579 case FILE_BOTH_DIRECTORY_INFORMATION:
3580 case FILE_DIRECTORY_INFORMATION:
3581 case FILE_NAMES_INFORMATION:
3582 case FILEID_FULL_DIRECTORY_INFORMATION:
3583 case FILEID_BOTH_DIRECTORY_INFORMATION:
3584 case SMB_FIND_FILE_POSIX_INFO:
3585 break;
3586 default:
3587 return -EOPNOTSUPP;
3588 }
3589
3590 return 0;
3591}
3592
3593int smb2_query_dir(struct ksmbd_work *work)
3594{
3595 struct ksmbd_conn *conn = work->conn;
3596 struct smb2_query_directory_req *req;
3597 struct smb2_query_directory_rsp *rsp, *rsp_org;
3598 struct ksmbd_share_config *share = work->tcon->share_conf;
3599 struct ksmbd_file *dir_fp = NULL;
3600 struct ksmbd_dir_info d_info;
3601 int rc = 0;
3602 char *srch_ptr = NULL;
3603 unsigned char srch_flag;
3604 int buffer_sz;
3605 struct smb2_query_dir_private query_dir_private = {NULL, };
3606
Namjae Jeone5066492021-03-30 12:35:23 +09003607 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09003608 WORK_BUFFERS(work, req, rsp);
3609
3610 if (ksmbd_override_fsids(work)) {
3611 rsp->hdr.Status = STATUS_NO_MEMORY;
3612 smb2_set_err_rsp(work);
3613 return -ENOMEM;
3614 }
3615
3616 rc = verify_info_level(req->FileInformationClass);
3617 if (rc) {
3618 rc = -EFAULT;
3619 goto err_out2;
3620 }
3621
3622 dir_fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09003623 le64_to_cpu(req->VolatileFileId),
3624 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09003625 if (!dir_fp) {
3626 rc = -EBADF;
3627 goto err_out2;
3628 }
3629
3630 if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) ||
Namjae Jeon070fb212021-05-26 17:57:12 +09003631 inode_permission(&init_user_ns, file_inode(dir_fp->filp),
3632 MAY_READ | MAY_EXEC)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003633 ksmbd_err("no right to enumerate directory (%s)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003634 FP_FILENAME(dir_fp));
Namjae Jeone2f34482021-03-16 10:49:09 +09003635 rc = -EACCES;
3636 goto err_out2;
3637 }
3638
3639 if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) {
3640 ksmbd_err("can't do query dir for a file\n");
3641 rc = -EINVAL;
3642 goto err_out2;
3643 }
3644
3645 srch_flag = req->Flags;
3646 srch_ptr = smb_strndup_from_utf16(req->Buffer,
Namjae Jeon070fb212021-05-26 17:57:12 +09003647 le16_to_cpu(req->FileNameLength), 1,
3648 conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09003649 if (IS_ERR(srch_ptr)) {
3650 ksmbd_debug(SMB, "Search Pattern not found\n");
3651 rc = -EINVAL;
3652 goto err_out2;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003653 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003654 ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr);
Namjae Jeon64b39f42021-03-30 14:25:35 +09003655 }
Namjae Jeone2f34482021-03-16 10:49:09 +09003656
3657 ksmbd_debug(SMB, "Directory name is %s\n", dir_fp->filename);
3658
3659 if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) {
3660 ksmbd_debug(SMB, "Restart directory scan\n");
3661 generic_file_llseek(dir_fp->filp, 0, SEEK_SET);
3662 restart_ctx(&dir_fp->readdir_data.ctx);
3663 }
3664
3665 memset(&d_info, 0, sizeof(struct ksmbd_dir_info));
3666 d_info.wptr = (char *)rsp->Buffer;
3667 d_info.rptr = (char *)rsp->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003668 d_info.out_buf_len = (work->response_sz - (get_rfc1002_len(rsp_org) + 4));
Namjae Jeon070fb212021-05-26 17:57:12 +09003669 d_info.out_buf_len = min_t(int, d_info.out_buf_len, le32_to_cpu(req->OutputBufferLength)) -
3670 sizeof(struct smb2_query_directory_rsp);
Namjae Jeone2f34482021-03-16 10:49:09 +09003671 d_info.flags = srch_flag;
3672
3673 /*
3674 * reserve dot and dotdot entries in head of buffer
3675 * in first response
3676 */
3677 rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass,
Namjae Jeon070fb212021-05-26 17:57:12 +09003678 dir_fp, &d_info, srch_ptr,
3679 smb2_populate_readdir_entry);
Namjae Jeone2f34482021-03-16 10:49:09 +09003680 if (rc == -ENOSPC)
3681 rc = 0;
3682 else if (rc)
3683 goto err_out;
3684
3685 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES))
3686 d_info.hide_dot_file = true;
3687
3688 buffer_sz = d_info.out_buf_len;
3689 d_info.rptr = d_info.wptr;
3690 query_dir_private.work = work;
3691 query_dir_private.search_pattern = srch_ptr;
3692 query_dir_private.dir_fp = dir_fp;
3693 query_dir_private.d_info = &d_info;
3694 query_dir_private.info_level = req->FileInformationClass;
3695 dir_fp->readdir_data.private = &query_dir_private;
3696 set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir);
3697
3698 rc = ksmbd_vfs_readdir(dir_fp->filp, &dir_fp->readdir_data);
3699 if (rc == 0)
3700 restart_ctx(&dir_fp->readdir_data.ctx);
3701 if (rc == -ENOSPC)
3702 rc = 0;
3703 if (rc)
3704 goto err_out;
3705
3706 d_info.wptr = d_info.rptr;
3707 d_info.out_buf_len = buffer_sz;
3708 rc = process_query_dir_entries(&query_dir_private);
3709 if (rc)
3710 goto err_out;
3711
3712 if (!d_info.data_count && d_info.out_buf_len >= 0) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09003713 if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003714 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003715 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003716 dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0;
3717 rsp->hdr.Status = STATUS_NO_MORE_FILES;
3718 }
3719 rsp->StructureSize = cpu_to_le16(9);
3720 rsp->OutputBufferOffset = cpu_to_le16(0);
3721 rsp->OutputBufferLength = cpu_to_le32(0);
3722 rsp->Buffer[0] = 0;
3723 inc_rfc1001_len(rsp_org, 9);
3724 } else {
3725 ((struct file_directory_info *)
3726 ((char *)rsp->Buffer + d_info.last_entry_offset))
3727 ->NextEntryOffset = 0;
3728
3729 rsp->StructureSize = cpu_to_le16(9);
3730 rsp->OutputBufferOffset = cpu_to_le16(72);
3731 rsp->OutputBufferLength = cpu_to_le32(d_info.data_count);
3732 inc_rfc1001_len(rsp_org, 8 + d_info.data_count);
3733 }
3734
3735 kfree(srch_ptr);
3736 ksmbd_fd_put(work, dir_fp);
3737 ksmbd_revert_fsids(work);
3738 return 0;
3739
3740err_out:
3741 ksmbd_err("error while processing smb2 query dir rc = %d\n", rc);
3742 kfree(srch_ptr);
3743
3744err_out2:
3745 if (rc == -EINVAL)
3746 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
3747 else if (rc == -EACCES)
3748 rsp->hdr.Status = STATUS_ACCESS_DENIED;
3749 else if (rc == -ENOENT)
3750 rsp->hdr.Status = STATUS_NO_SUCH_FILE;
3751 else if (rc == -EBADF)
3752 rsp->hdr.Status = STATUS_FILE_CLOSED;
3753 else if (rc == -ENOMEM)
3754 rsp->hdr.Status = STATUS_NO_MEMORY;
3755 else if (rc == -EFAULT)
3756 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
3757 if (!rsp->hdr.Status)
3758 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
3759
3760 smb2_set_err_rsp(work);
3761 ksmbd_fd_put(work, dir_fp);
3762 ksmbd_revert_fsids(work);
3763 return 0;
3764}
3765
3766/**
3767 * buffer_check_err() - helper function to check buffer errors
3768 * @reqOutputBufferLength: max buffer length expected in command response
3769 * @rsp: query info response buffer contains output buffer length
3770 * @infoclass_size: query info class response buffer size
3771 *
3772 * Return: 0 on success, otherwise error
3773 */
3774static int buffer_check_err(int reqOutputBufferLength,
Namjae Jeon070fb212021-05-26 17:57:12 +09003775 struct smb2_query_info_rsp *rsp, int infoclass_size)
Namjae Jeone2f34482021-03-16 10:49:09 +09003776{
3777 if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) {
3778 if (reqOutputBufferLength < infoclass_size) {
3779 ksmbd_err("Invalid Buffer Size Requested\n");
3780 rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003781 rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4);
Namjae Jeone2f34482021-03-16 10:49:09 +09003782 return -EINVAL;
3783 }
3784
3785 ksmbd_debug(SMB, "Buffer Overflow\n");
3786 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003787 rsp->hdr.smb2_buf_length = cpu_to_be32(sizeof(struct smb2_hdr) - 4 +
3788 reqOutputBufferLength);
3789 rsp->OutputBufferLength = cpu_to_le32(reqOutputBufferLength);
Namjae Jeone2f34482021-03-16 10:49:09 +09003790 }
3791 return 0;
3792}
3793
3794static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp)
3795{
3796 struct smb2_file_standard_info *sinfo;
3797
3798 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
3799
3800 sinfo->AllocationSize = cpu_to_le64(4096);
3801 sinfo->EndOfFile = cpu_to_le64(0);
3802 sinfo->NumberOfLinks = cpu_to_le32(1);
3803 sinfo->DeletePending = 1;
3804 sinfo->Directory = 0;
3805 rsp->OutputBufferLength =
3806 cpu_to_le32(sizeof(struct smb2_file_standard_info));
3807 inc_rfc1001_len(rsp, sizeof(struct smb2_file_standard_info));
3808}
3809
Namjae Jeon070fb212021-05-26 17:57:12 +09003810static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num)
Namjae Jeone2f34482021-03-16 10:49:09 +09003811{
3812 struct smb2_file_internal_info *file_info;
3813
3814 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
3815
3816 /* any unique number */
3817 file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63));
3818 rsp->OutputBufferLength =
3819 cpu_to_le32(sizeof(struct smb2_file_internal_info));
3820 inc_rfc1001_len(rsp, sizeof(struct smb2_file_internal_info));
3821}
3822
Namjae Jeone2f34482021-03-16 10:49:09 +09003823static int smb2_get_info_file_pipe(struct ksmbd_session *sess,
Namjae Jeon070fb212021-05-26 17:57:12 +09003824 struct smb2_query_info_req *req,
3825 struct smb2_query_info_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09003826{
Namjae Jeon64b39f42021-03-30 14:25:35 +09003827 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09003828 int rc;
3829
3830 /*
3831 * Windows can sometime send query file info request on
3832 * pipe without opening it, checking error condition here
3833 */
3834 id = le64_to_cpu(req->VolatileFileId);
3835 if (!ksmbd_session_rpc_method(sess, id))
3836 return -ENOENT;
3837
3838 ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003839 req->FileInfoClass, le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09003840
3841 switch (req->FileInfoClass) {
3842 case FILE_STANDARD_INFORMATION:
3843 get_standard_info_pipe(rsp);
3844 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09003845 rsp, FILE_STANDARD_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09003846 break;
3847 case FILE_INTERNAL_INFORMATION:
3848 get_internal_info_pipe(rsp, id);
3849 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
Namjae Jeon070fb212021-05-26 17:57:12 +09003850 rsp, FILE_INTERNAL_INFORMATION_SIZE);
Namjae Jeone2f34482021-03-16 10:49:09 +09003851 break;
3852 default:
3853 ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09003854 req->FileInfoClass);
Namjae Jeone2f34482021-03-16 10:49:09 +09003855 rc = -EOPNOTSUPP;
3856 }
3857 return rc;
3858}
3859
3860/**
3861 * smb2_get_ea() - handler for smb2 get extended attribute command
3862 * @work: smb work containing query info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09003863 * @fp: ksmbd_file pointer
3864 * @req: get extended attribute request
3865 * @rsp: response buffer pointer
3866 * @rsp_org: base response buffer pointer in case of chained response
Namjae Jeone2f34482021-03-16 10:49:09 +09003867 *
3868 * Return: 0 on success, otherwise error
3869 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003870static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09003871 struct smb2_query_info_req *req,
3872 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09003873{
3874 struct smb2_ea_info *eainfo, *prev_eainfo;
3875 char *name, *ptr, *xattr_list = NULL, *buf;
3876 int rc, name_len, value_len, xattr_list_len, idx;
3877 ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0;
3878 struct smb2_ea_info_req *ea_req = NULL;
3879 struct path *path;
3880
3881 if (!(fp->daccess & FILE_READ_EA_LE)) {
3882 ksmbd_err("Not permitted to read ext attr : 0x%x\n",
3883 fp->daccess);
3884 return -EACCES;
3885 }
3886
3887 path = &fp->filp->f_path;
3888 /* single EA entry is requested with given user.* name */
Namjae Jeon64b39f42021-03-30 14:25:35 +09003889 if (req->InputBufferLength) {
Namjae Jeone2f34482021-03-16 10:49:09 +09003890 ea_req = (struct smb2_ea_info_req *)req->Buffer;
Namjae Jeon64b39f42021-03-30 14:25:35 +09003891 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09003892 /* need to send all EAs, if no specific EA is requested*/
3893 if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY)
3894 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09003895 "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n",
3896 le32_to_cpu(req->Flags));
Namjae Jeone2f34482021-03-16 10:49:09 +09003897 }
3898
3899 buf_free_len = work->response_sz -
3900 (get_rfc1002_len(rsp_org) + 4) -
3901 sizeof(struct smb2_query_info_rsp);
3902
3903 if (le32_to_cpu(req->OutputBufferLength) < buf_free_len)
3904 buf_free_len = le32_to_cpu(req->OutputBufferLength);
3905
3906 rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
3907 if (rc < 0) {
3908 rsp->hdr.Status = STATUS_INVALID_HANDLE;
3909 goto out;
3910 } else if (!rc) { /* there is no EA in the file */
3911 ksmbd_debug(SMB, "no ea data in the file\n");
3912 goto done;
3913 }
3914 xattr_list_len = rc;
3915
3916 ptr = (char *)rsp->Buffer;
3917 eainfo = (struct smb2_ea_info *)ptr;
3918 prev_eainfo = eainfo;
3919 idx = 0;
3920
3921 while (idx < xattr_list_len) {
3922 name = xattr_list + idx;
3923 name_len = strlen(name);
3924
3925 ksmbd_debug(SMB, "%s, len %d\n", name, name_len);
3926 idx += name_len + 1;
3927
3928 /*
3929 * CIFS does not support EA other than user.* namespace,
3930 * still keep the framework generic, to list other attrs
3931 * in future.
3932 */
3933 if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
3934 continue;
3935
3936 if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX,
Namjae Jeon64b39f42021-03-30 14:25:35 +09003937 STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09003938 continue;
3939
3940 if (req->InputBufferLength &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09003941 strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name,
3942 ea_req->EaNameLength))
Namjae Jeone2f34482021-03-16 10:49:09 +09003943 continue;
3944
3945 if (!strncmp(&name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09003946 DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09003947 continue;
3948
3949 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
3950 name_len -= XATTR_USER_PREFIX_LEN;
3951
3952 ptr = (char *)(&eainfo->name + name_len + 1);
3953 buf_free_len -= (offsetof(struct smb2_ea_info, name) +
3954 name_len + 1);
3955 /* bailout if xattr can't fit in buf_free_len */
3956 value_len = ksmbd_vfs_getxattr(path->dentry, name, &buf);
3957 if (value_len <= 0) {
3958 rc = -ENOENT;
3959 rsp->hdr.Status = STATUS_INVALID_HANDLE;
3960 goto out;
3961 }
3962
3963 buf_free_len -= value_len;
3964 if (buf_free_len < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09003965 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09003966 break;
3967 }
3968
3969 memcpy(ptr, buf, value_len);
Namjae Jeon79f6b112021-04-02 12:47:14 +09003970 kfree(buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09003971
3972 ptr += value_len;
3973 eainfo->Flags = 0;
3974 eainfo->EaNameLength = name_len;
3975
Namjae Jeon64b39f42021-03-30 14:25:35 +09003976 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09003977 memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN],
Namjae Jeon070fb212021-05-26 17:57:12 +09003978 name_len);
Namjae Jeone2f34482021-03-16 10:49:09 +09003979 else
3980 memcpy(eainfo->name, name, name_len);
3981
3982 eainfo->name[name_len] = '\0';
3983 eainfo->EaValueLength = cpu_to_le16(value_len);
3984 next_offset = offsetof(struct smb2_ea_info, name) +
3985 name_len + 1 + value_len;
3986
3987 /* align next xattr entry at 4 byte bundary */
3988 alignment_bytes = ((next_offset + 3) & ~3) - next_offset;
3989 if (alignment_bytes) {
3990 memset(ptr, '\0', alignment_bytes);
3991 ptr += alignment_bytes;
3992 next_offset += alignment_bytes;
3993 buf_free_len -= alignment_bytes;
3994 }
3995 eainfo->NextEntryOffset = cpu_to_le32(next_offset);
3996 prev_eainfo = eainfo;
3997 eainfo = (struct smb2_ea_info *)ptr;
3998 rsp_data_cnt += next_offset;
3999
4000 if (req->InputBufferLength) {
4001 ksmbd_debug(SMB, "single entry requested\n");
4002 break;
4003 }
4004 }
4005
4006 /* no more ea entries */
4007 prev_eainfo->NextEntryOffset = 0;
4008done:
4009 rc = 0;
4010 if (rsp_data_cnt == 0)
4011 rsp->hdr.Status = STATUS_NO_EAS_ON_FILE;
4012 rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt);
4013 inc_rfc1001_len(rsp_org, rsp_data_cnt);
4014out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004015 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004016 return rc;
4017}
4018
4019static void get_file_access_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004020 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004021{
4022 struct smb2_file_access_info *file_info;
4023
4024 file_info = (struct smb2_file_access_info *)rsp->Buffer;
4025 file_info->AccessFlags = fp->daccess;
4026 rsp->OutputBufferLength =
4027 cpu_to_le32(sizeof(struct smb2_file_access_info));
4028 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_access_info));
4029}
4030
4031static int get_file_basic_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004032 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004033{
4034 struct smb2_file_all_info *basic_info;
4035 struct kstat stat;
4036 u64 time;
4037
4038 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4039 ksmbd_err("no right to read the attributes : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004040 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004041 return -EACCES;
4042 }
4043
4044 basic_info = (struct smb2_file_all_info *)rsp->Buffer;
4045 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
4046 basic_info->CreationTime = cpu_to_le64(fp->create_time);
4047 time = ksmbd_UnixTimeToNT(stat.atime);
4048 basic_info->LastAccessTime = cpu_to_le64(time);
4049 time = ksmbd_UnixTimeToNT(stat.mtime);
4050 basic_info->LastWriteTime = cpu_to_le64(time);
4051 time = ksmbd_UnixTimeToNT(stat.ctime);
4052 basic_info->ChangeTime = cpu_to_le64(time);
4053 basic_info->Attributes = fp->f_ci->m_fattr;
4054 basic_info->Pad1 = 0;
4055 rsp->OutputBufferLength =
Namjae Jeon64b39f42021-03-30 14:25:35 +09004056 cpu_to_le32(offsetof(struct smb2_file_all_info, AllocationSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09004057 inc_rfc1001_len(rsp_org, offsetof(struct smb2_file_all_info,
4058 AllocationSize));
4059 return 0;
4060}
4061
4062static unsigned long long get_allocation_size(struct inode *inode,
Namjae Jeon070fb212021-05-26 17:57:12 +09004063 struct kstat *stat)
Namjae Jeone2f34482021-03-16 10:49:09 +09004064{
4065 unsigned long long alloc_size = 0;
4066
4067 if (!S_ISDIR(stat->mode)) {
4068 if ((inode->i_blocks << 9) <= stat->size)
4069 alloc_size = stat->size;
4070 else
4071 alloc_size = inode->i_blocks << 9;
Namjae Jeone2f34482021-03-16 10:49:09 +09004072 }
4073
4074 return alloc_size;
4075}
4076
4077static void get_file_standard_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004078 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004079{
4080 struct smb2_file_standard_info *sinfo;
4081 unsigned int delete_pending;
4082 struct inode *inode;
4083 struct kstat stat;
4084
4085 inode = FP_INODE(fp);
4086 generic_fillattr(&init_user_ns, inode, &stat);
4087
4088 sinfo = (struct smb2_file_standard_info *)rsp->Buffer;
4089 delete_pending = ksmbd_inode_pending_delete(fp);
4090
4091 sinfo->AllocationSize = cpu_to_le64(get_allocation_size(inode, &stat));
4092 sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4093 sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending);
4094 sinfo->DeletePending = delete_pending;
4095 sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4096 rsp->OutputBufferLength =
4097 cpu_to_le32(sizeof(struct smb2_file_standard_info));
4098 inc_rfc1001_len(rsp_org,
4099 sizeof(struct smb2_file_standard_info));
4100}
4101
4102static void get_file_alignment_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004103 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004104{
4105 struct smb2_file_alignment_info *file_info;
4106
4107 file_info = (struct smb2_file_alignment_info *)rsp->Buffer;
4108 file_info->AlignmentRequirement = 0;
4109 rsp->OutputBufferLength =
4110 cpu_to_le32(sizeof(struct smb2_file_alignment_info));
4111 inc_rfc1001_len(rsp_org,
4112 sizeof(struct smb2_file_alignment_info));
4113}
4114
4115static int get_file_all_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004116 struct smb2_query_info_rsp *rsp,
4117 struct ksmbd_file *fp,
4118 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004119{
4120 struct ksmbd_conn *conn = work->conn;
4121 struct smb2_file_all_info *file_info;
4122 unsigned int delete_pending;
4123 struct inode *inode;
4124 struct kstat stat;
4125 int conv_len;
4126 char *filename;
4127 u64 time;
4128
4129 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4130 ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004131 fp->daccess);
Namjae Jeone2f34482021-03-16 10:49:09 +09004132 return -EACCES;
4133 }
4134
4135 filename = convert_to_nt_pathname(fp->filename,
4136 work->tcon->share_conf->path);
4137 if (!filename)
4138 return -ENOMEM;
4139
4140 inode = FP_INODE(fp);
4141 generic_fillattr(&init_user_ns, inode, &stat);
4142
4143 ksmbd_debug(SMB, "filename = %s\n", filename);
4144 delete_pending = ksmbd_inode_pending_delete(fp);
4145 file_info = (struct smb2_file_all_info *)rsp->Buffer;
4146
4147 file_info->CreationTime = cpu_to_le64(fp->create_time);
4148 time = ksmbd_UnixTimeToNT(stat.atime);
4149 file_info->LastAccessTime = cpu_to_le64(time);
4150 time = ksmbd_UnixTimeToNT(stat.mtime);
4151 file_info->LastWriteTime = cpu_to_le64(time);
4152 time = ksmbd_UnixTimeToNT(stat.ctime);
4153 file_info->ChangeTime = cpu_to_le64(time);
4154 file_info->Attributes = fp->f_ci->m_fattr;
4155 file_info->Pad1 = 0;
4156 file_info->AllocationSize =
4157 cpu_to_le64(get_allocation_size(inode, &stat));
4158 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4159 file_info->NumberOfLinks =
4160 cpu_to_le32(get_nlink(&stat) - delete_pending);
4161 file_info->DeletePending = delete_pending;
4162 file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0;
4163 file_info->Pad2 = 0;
4164 file_info->IndexNumber = cpu_to_le64(stat.ino);
4165 file_info->EASize = 0;
4166 file_info->AccessFlags = fp->daccess;
4167 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4168 file_info->Mode = fp->coption;
4169 file_info->AlignmentRequirement = 0;
Namjae Jeon070fb212021-05-26 17:57:12 +09004170 conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename,
4171 PATH_MAX, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004172 conv_len *= 2;
4173 file_info->FileNameLength = cpu_to_le32(conv_len);
4174 rsp->OutputBufferLength =
4175 cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1);
4176 kfree(filename);
4177 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4178 return 0;
4179}
4180
4181static void get_file_alternate_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004182 struct smb2_query_info_rsp *rsp,
4183 struct ksmbd_file *fp,
4184 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004185{
4186 struct ksmbd_conn *conn = work->conn;
4187 struct smb2_file_alt_name_info *file_info;
4188 int conv_len;
4189 char *filename;
4190
4191 filename = (char *)FP_FILENAME(fp);
4192 file_info = (struct smb2_file_alt_name_info *)rsp->Buffer;
4193 conv_len = ksmbd_extract_shortname(conn,
4194 filename,
4195 file_info->FileName);
4196 file_info->FileNameLength = cpu_to_le32(conv_len);
4197 rsp->OutputBufferLength =
4198 cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len);
4199 inc_rfc1001_len(rsp_org, le32_to_cpu(rsp->OutputBufferLength));
4200}
4201
4202static void get_file_stream_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004203 struct smb2_query_info_rsp *rsp,
4204 struct ksmbd_file *fp,
4205 void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004206{
4207 struct ksmbd_conn *conn = work->conn;
4208 struct smb2_file_stream_info *file_info;
4209 char *stream_name, *xattr_list = NULL, *stream_buf;
4210 struct kstat stat;
4211 struct path *path = &fp->filp->f_path;
4212 ssize_t xattr_list_len;
4213 int nbytes = 0, streamlen, stream_name_len, next, idx = 0;
4214
4215 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
4216 file_info = (struct smb2_file_stream_info *)rsp->Buffer;
4217
4218 xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list);
4219 if (xattr_list_len < 0) {
4220 goto out;
4221 } else if (!xattr_list_len) {
4222 ksmbd_debug(SMB, "empty xattr in the file\n");
4223 goto out;
4224 }
4225
4226 while (idx < xattr_list_len) {
4227 stream_name = xattr_list + idx;
4228 streamlen = strlen(stream_name);
4229 idx += streamlen + 1;
4230
4231 ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen);
4232
4233 if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN],
Namjae Jeon64b39f42021-03-30 14:25:35 +09004234 STREAM_PREFIX, STREAM_PREFIX_LEN))
Namjae Jeone2f34482021-03-16 10:49:09 +09004235 continue;
4236
4237 stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN +
4238 STREAM_PREFIX_LEN);
4239 streamlen = stream_name_len;
4240
4241 /* plus : size */
4242 streamlen += 1;
4243 stream_buf = kmalloc(streamlen + 1, GFP_KERNEL);
4244 if (!stream_buf)
4245 break;
4246
4247 streamlen = snprintf(stream_buf, streamlen + 1,
Namjae Jeon070fb212021-05-26 17:57:12 +09004248 ":%s", &stream_name[XATTR_NAME_STREAM_LEN]);
Namjae Jeone2f34482021-03-16 10:49:09 +09004249
Namjae Jeon070fb212021-05-26 17:57:12 +09004250 file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes];
Namjae Jeone2f34482021-03-16 10:49:09 +09004251 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004252 stream_buf, streamlen,
4253 conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004254 streamlen *= 2;
4255 kfree(stream_buf);
4256 file_info->StreamNameLength = cpu_to_le32(streamlen);
4257 file_info->StreamSize = cpu_to_le64(stream_name_len);
4258 file_info->StreamAllocationSize = cpu_to_le64(stream_name_len);
4259
4260 next = sizeof(struct smb2_file_stream_info) + streamlen;
4261 nbytes += next;
4262 file_info->NextEntryOffset = cpu_to_le32(next);
4263 }
4264
4265 if (nbytes) {
4266 file_info = (struct smb2_file_stream_info *)
4267 &rsp->Buffer[nbytes];
4268 streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName,
Namjae Jeon070fb212021-05-26 17:57:12 +09004269 "::$DATA", 7, conn->local_nls, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004270 streamlen *= 2;
4271 file_info->StreamNameLength = cpu_to_le32(streamlen);
4272 file_info->StreamSize = S_ISDIR(stat.mode) ? 0 :
4273 cpu_to_le64(stat.size);
4274 file_info->StreamAllocationSize = S_ISDIR(stat.mode) ? 0 :
4275 cpu_to_le64(stat.size);
4276 nbytes += sizeof(struct smb2_file_stream_info) + streamlen;
4277 }
4278
4279 /* last entry offset should be 0 */
4280 file_info->NextEntryOffset = 0;
4281out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09004282 kvfree(xattr_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09004283
4284 rsp->OutputBufferLength = cpu_to_le32(nbytes);
4285 inc_rfc1001_len(rsp_org, nbytes);
4286}
4287
4288static void get_file_internal_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004289 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004290{
4291 struct smb2_file_internal_info *file_info;
4292 struct kstat stat;
4293
4294 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
4295 file_info = (struct smb2_file_internal_info *)rsp->Buffer;
4296 file_info->IndexNumber = cpu_to_le64(stat.ino);
4297 rsp->OutputBufferLength =
4298 cpu_to_le32(sizeof(struct smb2_file_internal_info));
4299 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_internal_info));
4300}
4301
4302static int get_file_network_open_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004303 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004304{
4305 struct smb2_file_ntwrk_info *file_info;
4306 struct inode *inode;
4307 struct kstat stat;
4308 u64 time;
4309
4310 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4311 ksmbd_err("no right to read the attributes : 0x%x\n",
4312 fp->daccess);
4313 return -EACCES;
4314 }
4315
4316 file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer;
4317
4318 inode = FP_INODE(fp);
4319 generic_fillattr(&init_user_ns, inode, &stat);
4320
4321 file_info->CreationTime = cpu_to_le64(fp->create_time);
4322 time = ksmbd_UnixTimeToNT(stat.atime);
4323 file_info->LastAccessTime = cpu_to_le64(time);
4324 time = ksmbd_UnixTimeToNT(stat.mtime);
4325 file_info->LastWriteTime = cpu_to_le64(time);
4326 time = ksmbd_UnixTimeToNT(stat.ctime);
4327 file_info->ChangeTime = cpu_to_le64(time);
4328 file_info->Attributes = fp->f_ci->m_fattr;
4329 file_info->AllocationSize =
4330 cpu_to_le64(get_allocation_size(inode, &stat));
4331 file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size);
4332 file_info->Reserved = cpu_to_le32(0);
4333 rsp->OutputBufferLength =
4334 cpu_to_le32(sizeof(struct smb2_file_ntwrk_info));
4335 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ntwrk_info));
4336 return 0;
4337}
4338
Namjae Jeon64b39f42021-03-30 14:25:35 +09004339static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004340{
4341 struct smb2_file_ea_info *file_info;
4342
4343 file_info = (struct smb2_file_ea_info *)rsp->Buffer;
4344 file_info->EASize = 0;
4345 rsp->OutputBufferLength =
4346 cpu_to_le32(sizeof(struct smb2_file_ea_info));
4347 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_ea_info));
4348}
4349
4350static void get_file_position_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004351 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004352{
4353 struct smb2_file_pos_info *file_info;
4354
4355 file_info = (struct smb2_file_pos_info *)rsp->Buffer;
4356 file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos);
4357 rsp->OutputBufferLength =
4358 cpu_to_le32(sizeof(struct smb2_file_pos_info));
4359 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_pos_info));
4360}
4361
4362static void get_file_mode_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004363 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004364{
4365 struct smb2_file_mode_info *file_info;
4366
4367 file_info = (struct smb2_file_mode_info *)rsp->Buffer;
4368 file_info->Mode = fp->coption & FILE_MODE_INFO_MASK;
4369 rsp->OutputBufferLength =
4370 cpu_to_le32(sizeof(struct smb2_file_mode_info));
4371 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_mode_info));
4372}
4373
4374static void get_file_compression_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004375 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004376{
4377 struct smb2_file_comp_info *file_info;
4378 struct kstat stat;
4379
4380 generic_fillattr(&init_user_ns, FP_INODE(fp), &stat);
4381
4382 file_info = (struct smb2_file_comp_info *)rsp->Buffer;
4383 file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9);
4384 file_info->CompressionFormat = COMPRESSION_FORMAT_NONE;
4385 file_info->CompressionUnitShift = 0;
4386 file_info->ChunkShift = 0;
4387 file_info->ClusterShift = 0;
4388 memset(&file_info->Reserved[0], 0, 3);
4389
4390 rsp->OutputBufferLength =
4391 cpu_to_le32(sizeof(struct smb2_file_comp_info));
4392 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_comp_info));
4393}
4394
4395static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004396 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004397{
4398 struct smb2_file_attr_tag_info *file_info;
4399
4400 if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) {
4401 ksmbd_err("no right to read the attributes : 0x%x\n",
4402 fp->daccess);
4403 return -EACCES;
4404 }
4405
4406 file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer;
4407 file_info->FileAttributes = fp->f_ci->m_fattr;
4408 file_info->ReparseTag = 0;
4409 rsp->OutputBufferLength =
4410 cpu_to_le32(sizeof(struct smb2_file_attr_tag_info));
Namjae Jeon070fb212021-05-26 17:57:12 +09004411 inc_rfc1001_len(rsp_org, sizeof(struct smb2_file_attr_tag_info));
Namjae Jeone2f34482021-03-16 10:49:09 +09004412 return 0;
4413}
4414
4415static int find_file_posix_info(struct smb2_query_info_rsp *rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09004416 struct ksmbd_file *fp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004417{
4418 struct smb311_posix_qinfo *file_info;
4419 struct inode *inode = FP_INODE(fp);
4420 u64 time;
4421
4422 file_info = (struct smb311_posix_qinfo *)rsp->Buffer;
4423 file_info->CreationTime = cpu_to_le64(fp->create_time);
4424 time = ksmbd_UnixTimeToNT(inode->i_atime);
4425 file_info->LastAccessTime = cpu_to_le64(time);
4426 time = ksmbd_UnixTimeToNT(inode->i_mtime);
4427 file_info->LastWriteTime = cpu_to_le64(time);
4428 time = ksmbd_UnixTimeToNT(inode->i_ctime);
4429 file_info->ChangeTime = cpu_to_le64(time);
4430 file_info->DosAttributes = fp->f_ci->m_fattr;
4431 file_info->Inode = cpu_to_le64(inode->i_ino);
4432 file_info->EndOfFile = cpu_to_le64(inode->i_size);
4433 file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9);
4434 file_info->HardLinks = cpu_to_le32(inode->i_nlink);
4435 file_info->Mode = cpu_to_le32(inode->i_mode);
4436 file_info->DeviceId = cpu_to_le32(inode->i_rdev);
4437 rsp->OutputBufferLength =
4438 cpu_to_le32(sizeof(struct smb311_posix_qinfo));
Namjae Jeon64b39f42021-03-30 14:25:35 +09004439 inc_rfc1001_len(rsp_org, sizeof(struct smb311_posix_qinfo));
Namjae Jeone2f34482021-03-16 10:49:09 +09004440 return 0;
4441}
4442
Namjae Jeone2f34482021-03-16 10:49:09 +09004443static int smb2_get_info_file(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004444 struct smb2_query_info_req *req,
4445 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004446{
4447 struct ksmbd_file *fp;
4448 int fileinfoclass = 0;
4449 int rc = 0;
4450 int file_infoclass_size;
4451 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4452
4453 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004454 KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09004455 /* smb2 info file called for pipe */
4456 return smb2_get_info_file_pipe(work->sess, req, rsp);
4457 }
4458
4459 if (work->next_smb2_rcv_hdr_off) {
4460 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
4461 ksmbd_debug(SMB, "Compound request set FID = %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004462 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004463 id = work->compound_fid;
4464 pid = work->compound_pfid;
4465 }
4466 }
4467
4468 if (!HAS_FILE_ID(id)) {
4469 id = le64_to_cpu(req->VolatileFileId);
4470 pid = le64_to_cpu(req->PersistentFileId);
4471 }
4472
4473 fp = ksmbd_lookup_fd_slow(work, id, pid);
4474 if (!fp)
4475 return -ENOENT;
4476
4477 fileinfoclass = req->FileInfoClass;
4478
4479 switch (fileinfoclass) {
4480 case FILE_ACCESS_INFORMATION:
4481 get_file_access_info(rsp, fp, rsp_org);
4482 file_infoclass_size = FILE_ACCESS_INFORMATION_SIZE;
4483 break;
4484
4485 case FILE_BASIC_INFORMATION:
4486 rc = get_file_basic_info(rsp, fp, rsp_org);
4487 file_infoclass_size = FILE_BASIC_INFORMATION_SIZE;
4488 break;
4489
4490 case FILE_STANDARD_INFORMATION:
4491 get_file_standard_info(rsp, fp, rsp_org);
4492 file_infoclass_size = FILE_STANDARD_INFORMATION_SIZE;
4493 break;
4494
4495 case FILE_ALIGNMENT_INFORMATION:
4496 get_file_alignment_info(rsp, rsp_org);
4497 file_infoclass_size = FILE_ALIGNMENT_INFORMATION_SIZE;
4498 break;
4499
4500 case FILE_ALL_INFORMATION:
4501 rc = get_file_all_info(work, rsp, fp, rsp_org);
4502 file_infoclass_size = FILE_ALL_INFORMATION_SIZE;
4503 break;
4504
4505 case FILE_ALTERNATE_NAME_INFORMATION:
4506 get_file_alternate_info(work, rsp, fp, rsp_org);
4507 file_infoclass_size = FILE_ALTERNATE_NAME_INFORMATION_SIZE;
4508 break;
4509
4510 case FILE_STREAM_INFORMATION:
4511 get_file_stream_info(work, rsp, fp, rsp_org);
4512 file_infoclass_size = FILE_STREAM_INFORMATION_SIZE;
4513 break;
4514
4515 case FILE_INTERNAL_INFORMATION:
4516 get_file_internal_info(rsp, fp, rsp_org);
4517 file_infoclass_size = FILE_INTERNAL_INFORMATION_SIZE;
4518 break;
4519
4520 case FILE_NETWORK_OPEN_INFORMATION:
4521 rc = get_file_network_open_info(rsp, fp, rsp_org);
4522 file_infoclass_size = FILE_NETWORK_OPEN_INFORMATION_SIZE;
4523 break;
4524
4525 case FILE_EA_INFORMATION:
4526 get_file_ea_info(rsp, rsp_org);
4527 file_infoclass_size = FILE_EA_INFORMATION_SIZE;
4528 break;
4529
4530 case FILE_FULL_EA_INFORMATION:
4531 rc = smb2_get_ea(work, fp, req, rsp, rsp_org);
4532 file_infoclass_size = FILE_FULL_EA_INFORMATION_SIZE;
4533 break;
4534
4535 case FILE_POSITION_INFORMATION:
4536 get_file_position_info(rsp, fp, rsp_org);
4537 file_infoclass_size = FILE_POSITION_INFORMATION_SIZE;
4538 break;
4539
4540 case FILE_MODE_INFORMATION:
4541 get_file_mode_info(rsp, fp, rsp_org);
4542 file_infoclass_size = FILE_MODE_INFORMATION_SIZE;
4543 break;
4544
4545 case FILE_COMPRESSION_INFORMATION:
4546 get_file_compression_info(rsp, fp, rsp_org);
4547 file_infoclass_size = FILE_COMPRESSION_INFORMATION_SIZE;
4548 break;
4549
4550 case FILE_ATTRIBUTE_TAG_INFORMATION:
4551 rc = get_file_attribute_tag_info(rsp, fp, rsp_org);
4552 file_infoclass_size = FILE_ATTRIBUTE_TAG_INFORMATION_SIZE;
4553 break;
4554 case SMB_FIND_FILE_POSIX_INFO:
4555 if (!work->tcon->posix_extensions) {
4556 ksmbd_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
4557 rc = -EOPNOTSUPP;
4558 } else {
4559 rc = find_file_posix_info(rsp, fp, rsp_org);
4560 file_infoclass_size = sizeof(struct smb311_posix_qinfo);
4561 }
4562 break;
4563 default:
4564 ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n",
4565 fileinfoclass);
4566 rc = -EOPNOTSUPP;
4567 }
4568 if (!rc)
4569 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4570 rsp,
4571 file_infoclass_size);
4572 ksmbd_fd_put(work, fp);
4573 return rc;
4574}
4575
Namjae Jeone2f34482021-03-16 10:49:09 +09004576static int smb2_get_info_filesystem(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004577 struct smb2_query_info_req *req,
4578 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004579{
4580 struct ksmbd_session *sess = work->sess;
4581 struct ksmbd_conn *conn = sess->conn;
4582 struct ksmbd_share_config *share = work->tcon->share_conf;
4583 int fsinfoclass = 0;
4584 struct kstatfs stfs;
4585 struct path path;
4586 int rc = 0, len;
4587 int fs_infoclass_size = 0;
Hyunchul Leea6a5fa72021-05-26 18:59:06 +09004588 int lookup_flags = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09004589
Hyunchul Leea6a5fa72021-05-26 18:59:06 +09004590 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_FOLLOW_SYMLINKS))
4591 lookup_flags = LOOKUP_FOLLOW;
4592
4593 rc = ksmbd_vfs_kern_path(share->path, lookup_flags, &path, 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09004594 if (rc) {
4595 ksmbd_err("cannot create vfs path\n");
4596 return -EIO;
4597 }
4598
4599 rc = vfs_statfs(&path, &stfs);
4600 if (rc) {
4601 ksmbd_err("cannot do stat of path %s\n", share->path);
4602 path_put(&path);
4603 return -EIO;
4604 }
4605
4606 fsinfoclass = req->FileInfoClass;
4607
4608 switch (fsinfoclass) {
4609 case FS_DEVICE_INFORMATION:
4610 {
4611 struct filesystem_device_info *info;
4612
4613 info = (struct filesystem_device_info *)rsp->Buffer;
4614
4615 info->DeviceType = cpu_to_le32(stfs.f_type);
4616 info->DeviceCharacteristics = cpu_to_le32(0x00000020);
4617 rsp->OutputBufferLength = cpu_to_le32(8);
4618 inc_rfc1001_len(rsp_org, 8);
4619 fs_infoclass_size = FS_DEVICE_INFORMATION_SIZE;
4620 break;
4621 }
4622 case FS_ATTRIBUTE_INFORMATION:
4623 {
4624 struct filesystem_attribute_info *info;
4625 size_t sz;
4626
4627 info = (struct filesystem_attribute_info *)rsp->Buffer;
4628 info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS |
4629 FILE_PERSISTENT_ACLS |
4630 FILE_UNICODE_ON_DISK |
4631 FILE_CASE_PRESERVED_NAMES |
Namjae Jeoneb817362021-05-18 10:37:59 +09004632 FILE_CASE_SENSITIVE_SEARCH |
4633 FILE_SUPPORTS_BLOCK_REFCOUNTING);
Namjae Jeone2f34482021-03-16 10:49:09 +09004634
4635 info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps);
4636
4637 info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen);
4638 len = smbConvertToUTF16((__le16 *)info->FileSystemName,
4639 "NTFS", PATH_MAX, conn->local_nls, 0);
4640 len = len * 2;
4641 info->FileSystemNameLen = cpu_to_le32(len);
4642 sz = sizeof(struct filesystem_attribute_info) - 2 + len;
4643 rsp->OutputBufferLength = cpu_to_le32(sz);
4644 inc_rfc1001_len(rsp_org, sz);
4645 fs_infoclass_size = FS_ATTRIBUTE_INFORMATION_SIZE;
4646 break;
4647 }
4648 case FS_VOLUME_INFORMATION:
4649 {
4650 struct filesystem_vol_info *info;
4651 size_t sz;
4652
4653 info = (struct filesystem_vol_info *)(rsp->Buffer);
4654 info->VolumeCreationTime = 0;
4655 /* Taking dummy value of serial number*/
4656 info->SerialNumber = cpu_to_le32(0xbc3ac512);
4657 len = smbConvertToUTF16((__le16 *)info->VolumeLabel,
4658 share->name, PATH_MAX,
4659 conn->local_nls, 0);
4660 len = len * 2;
4661 info->VolumeLabelSize = cpu_to_le32(len);
4662 info->Reserved = 0;
4663 sz = sizeof(struct filesystem_vol_info) - 2 + len;
4664 rsp->OutputBufferLength = cpu_to_le32(sz);
4665 inc_rfc1001_len(rsp_org, sz);
4666 fs_infoclass_size = FS_VOLUME_INFORMATION_SIZE;
4667 break;
4668 }
4669 case FS_SIZE_INFORMATION:
4670 {
4671 struct filesystem_info *info;
4672 unsigned short logical_sector_size;
4673
4674 info = (struct filesystem_info *)(rsp->Buffer);
4675 logical_sector_size =
4676 ksmbd_vfs_logical_sector_size(d_inode(path.dentry));
4677
4678 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4679 info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree);
4680 info->SectorsPerAllocationUnit = cpu_to_le32(stfs.f_bsize >> 9);
4681 info->BytesPerSector = cpu_to_le32(logical_sector_size);
4682 rsp->OutputBufferLength = cpu_to_le32(24);
4683 inc_rfc1001_len(rsp_org, 24);
4684 fs_infoclass_size = FS_SIZE_INFORMATION_SIZE;
4685 break;
4686 }
4687 case FS_FULL_SIZE_INFORMATION:
4688 {
4689 struct smb2_fs_full_size_info *info;
4690 unsigned short logical_sector_size;
4691
4692 info = (struct smb2_fs_full_size_info *)(rsp->Buffer);
4693 logical_sector_size =
4694 ksmbd_vfs_logical_sector_size(d_inode(path.dentry));
4695
4696 info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks);
4697 info->CallerAvailableAllocationUnits =
4698 cpu_to_le64(stfs.f_bavail);
4699 info->ActualAvailableAllocationUnits =
4700 cpu_to_le64(stfs.f_bfree);
4701 info->SectorsPerAllocationUnit = cpu_to_le32(stfs.f_bsize >> 9);
4702 info->BytesPerSector = cpu_to_le32(logical_sector_size);
4703 rsp->OutputBufferLength = cpu_to_le32(32);
4704 inc_rfc1001_len(rsp_org, 32);
4705 fs_infoclass_size = FS_FULL_SIZE_INFORMATION_SIZE;
4706 break;
4707 }
4708 case FS_OBJECT_ID_INFORMATION:
4709 {
4710 struct object_id_info *info;
4711
4712 info = (struct object_id_info *)(rsp->Buffer);
4713
4714 if (!user_guest(sess->user))
4715 memcpy(info->objid, user_passkey(sess->user), 16);
4716 else
4717 memset(info->objid, 0, 16);
4718
4719 info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC);
4720 info->extended_info.version = cpu_to_le32(1);
4721 info->extended_info.release = cpu_to_le32(1);
4722 info->extended_info.rel_date = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004723 memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0"));
Namjae Jeone2f34482021-03-16 10:49:09 +09004724 rsp->OutputBufferLength = cpu_to_le32(64);
4725 inc_rfc1001_len(rsp_org, 64);
4726 fs_infoclass_size = FS_OBJECT_ID_INFORMATION_SIZE;
4727 break;
4728 }
4729 case FS_SECTOR_SIZE_INFORMATION:
4730 {
4731 struct smb3_fs_ss_info *info;
4732 struct ksmbd_fs_sector_size fs_ss;
4733
4734 info = (struct smb3_fs_ss_info *)(rsp->Buffer);
4735 ksmbd_vfs_smb2_sector_size(d_inode(path.dentry), &fs_ss);
4736
4737 info->LogicalBytesPerSector =
4738 cpu_to_le32(fs_ss.logical_sector_size);
4739 info->PhysicalBytesPerSectorForAtomicity =
4740 cpu_to_le32(fs_ss.physical_sector_size);
4741 info->PhysicalBytesPerSectorForPerf =
4742 cpu_to_le32(fs_ss.optimal_io_size);
4743 info->FSEffPhysicalBytesPerSectorForAtomicity =
4744 cpu_to_le32(fs_ss.optimal_io_size);
4745 info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE |
4746 SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE);
4747 info->ByteOffsetForSectorAlignment = 0;
4748 info->ByteOffsetForPartitionAlignment = 0;
4749 rsp->OutputBufferLength = cpu_to_le32(28);
4750 inc_rfc1001_len(rsp_org, 28);
4751 fs_infoclass_size = FS_SECTOR_SIZE_INFORMATION_SIZE;
4752 break;
4753 }
4754 case FS_CONTROL_INFORMATION:
4755 {
4756 /*
4757 * TODO : The current implementation is based on
4758 * test result with win7(NTFS) server. It's need to
4759 * modify this to get valid Quota values
4760 * from Linux kernel
4761 */
4762 struct smb2_fs_control_info *info;
4763
4764 info = (struct smb2_fs_control_info *)(rsp->Buffer);
4765 info->FreeSpaceStartFiltering = 0;
4766 info->FreeSpaceThreshold = 0;
4767 info->FreeSpaceStopFiltering = 0;
4768 info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID);
4769 info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID);
4770 info->Padding = 0;
4771 rsp->OutputBufferLength = cpu_to_le32(48);
4772 inc_rfc1001_len(rsp_org, 48);
4773 fs_infoclass_size = FS_CONTROL_INFORMATION_SIZE;
4774 break;
4775 }
4776 case FS_POSIX_INFORMATION:
4777 {
4778 struct filesystem_posix_info *info;
4779 unsigned short logical_sector_size;
4780
4781 if (!work->tcon->posix_extensions) {
4782 ksmbd_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n");
4783 rc = -EOPNOTSUPP;
4784 } else {
4785 info = (struct filesystem_posix_info *)(rsp->Buffer);
4786 logical_sector_size =
4787 ksmbd_vfs_logical_sector_size(d_inode(path.dentry));
4788 info->OptimalTransferSize = cpu_to_le32(logical_sector_size);
4789 info->BlockSize = cpu_to_le32(stfs.f_bsize);
4790 info->TotalBlocks = cpu_to_le64(stfs.f_blocks);
4791 info->BlocksAvail = cpu_to_le64(stfs.f_bfree);
4792 info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail);
4793 info->TotalFileNodes = cpu_to_le64(stfs.f_files);
4794 info->FreeFileNodes = cpu_to_le64(stfs.f_ffree);
4795 rsp->OutputBufferLength = cpu_to_le32(56);
4796 inc_rfc1001_len(rsp_org, 56);
4797 fs_infoclass_size = FS_POSIX_INFORMATION_SIZE;
4798 }
4799 break;
4800 }
4801 default:
4802 path_put(&path);
4803 return -EOPNOTSUPP;
4804 }
4805 rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength),
4806 rsp,
4807 fs_infoclass_size);
4808 path_put(&path);
4809 return rc;
4810}
4811
4812static int smb2_get_info_sec(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09004813 struct smb2_query_info_req *req,
4814 struct smb2_query_info_rsp *rsp, void *rsp_org)
Namjae Jeone2f34482021-03-16 10:49:09 +09004815{
4816 struct ksmbd_file *fp;
4817 struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL;
4818 struct smb_fattr fattr = {{0}};
4819 struct inode *inode;
4820 __u32 secdesclen;
4821 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
4822 int addition_info = le32_to_cpu(req->AdditionalInformation);
4823 int rc;
4824
Sebastian Gottschallced2b262021-04-27 15:33:54 +09004825 if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO)) {
4826 ksmbd_debug(SMB, "Unsupported addition info: 0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004827 addition_info);
Sebastian Gottschallced2b262021-04-27 15:33:54 +09004828
4829 pntsd->revision = cpu_to_le16(1);
4830 pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED);
4831 pntsd->osidoffset = 0;
4832 pntsd->gsidoffset = 0;
4833 pntsd->sacloffset = 0;
4834 pntsd->dacloffset = 0;
4835
4836 secdesclen = sizeof(struct smb_ntsd);
4837 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
4838 inc_rfc1001_len(rsp_org, secdesclen);
4839
4840 return 0;
4841 }
4842
Namjae Jeone2f34482021-03-16 10:49:09 +09004843 if (work->next_smb2_rcv_hdr_off) {
4844 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
4845 ksmbd_debug(SMB, "Compound request set FID = %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004846 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09004847 id = work->compound_fid;
4848 pid = work->compound_pfid;
4849 }
4850 }
4851
4852 if (!HAS_FILE_ID(id)) {
4853 id = le64_to_cpu(req->VolatileFileId);
4854 pid = le64_to_cpu(req->PersistentFileId);
4855 }
4856
4857 fp = ksmbd_lookup_fd_slow(work, id, pid);
4858 if (!fp)
4859 return -ENOENT;
4860
4861 inode = FP_INODE(fp);
Namjae Jeon3d47e542021-04-20 14:25:35 +09004862 ksmbd_acls_fattr(&fattr, inode);
Namjae Jeone2f34482021-03-16 10:49:09 +09004863
4864 if (test_share_config_flag(work->tcon->share_conf,
Namjae Jeon64b39f42021-03-30 14:25:35 +09004865 KSMBD_SHARE_FLAG_ACL_XATTR))
Namjae Jeone2f34482021-03-16 10:49:09 +09004866 ksmbd_vfs_get_sd_xattr(work->conn, fp->filp->f_path.dentry, &ppntsd);
4867
4868 rc = build_sec_desc(pntsd, ppntsd, addition_info, &secdesclen, &fattr);
4869 posix_acl_release(fattr.cf_acls);
4870 posix_acl_release(fattr.cf_dacls);
4871 kfree(ppntsd);
4872 ksmbd_fd_put(work, fp);
4873 if (rc)
4874 return rc;
4875
4876 rsp->OutputBufferLength = cpu_to_le32(secdesclen);
4877 inc_rfc1001_len(rsp_org, secdesclen);
4878 return 0;
4879}
4880
4881/**
4882 * smb2_query_info() - handler for smb2 query info command
4883 * @work: smb work containing query info request buffer
4884 *
4885 * Return: 0 on success, otherwise error
4886 */
4887int smb2_query_info(struct ksmbd_work *work)
4888{
4889 struct smb2_query_info_req *req;
4890 struct smb2_query_info_rsp *rsp, *rsp_org;
4891 int rc = 0;
4892
Namjae Jeone5066492021-03-30 12:35:23 +09004893 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09004894 WORK_BUFFERS(work, req, rsp);
4895
4896 ksmbd_debug(SMB, "GOT query info request\n");
4897
4898 switch (req->InfoType) {
4899 case SMB2_O_INFO_FILE:
4900 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
4901 rc = smb2_get_info_file(work, req, rsp, (void *)rsp_org);
4902 break;
4903 case SMB2_O_INFO_FILESYSTEM:
4904 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n");
4905 rc = smb2_get_info_filesystem(work, req, rsp, (void *)rsp_org);
4906 break;
4907 case SMB2_O_INFO_SECURITY:
4908 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
4909 rc = smb2_get_info_sec(work, req, rsp, (void *)rsp_org);
4910 break;
4911 default:
4912 ksmbd_debug(SMB, "InfoType %d not supported yet\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004913 req->InfoType);
Namjae Jeone2f34482021-03-16 10:49:09 +09004914 rc = -EOPNOTSUPP;
4915 }
4916
4917 if (rc < 0) {
4918 if (rc == -EACCES)
4919 rsp->hdr.Status = STATUS_ACCESS_DENIED;
4920 else if (rc == -ENOENT)
4921 rsp->hdr.Status = STATUS_FILE_CLOSED;
4922 else if (rc == -EIO)
4923 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
4924 else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0)
4925 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
4926 smb2_set_err_rsp(work);
4927
4928 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09004929 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09004930 return rc;
4931 }
4932 rsp->StructureSize = cpu_to_le16(9);
4933 rsp->OutputBufferOffset = cpu_to_le16(72);
4934 inc_rfc1001_len(rsp_org, 8);
4935 return 0;
4936}
4937
4938/**
4939 * smb2_close_pipe() - handler for closing IPC pipe
4940 * @work: smb work containing close request buffer
4941 *
4942 * Return: 0
4943 */
4944static noinline int smb2_close_pipe(struct ksmbd_work *work)
4945{
Namjae Jeon64b39f42021-03-30 14:25:35 +09004946 u64 id;
Namjae Jeone5066492021-03-30 12:35:23 +09004947 struct smb2_close_req *req = work->request_buf;
4948 struct smb2_close_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09004949
4950 id = le64_to_cpu(req->VolatileFileId);
4951 ksmbd_session_rpc_close(work->sess, id);
4952
4953 rsp->StructureSize = cpu_to_le16(60);
4954 rsp->Flags = 0;
4955 rsp->Reserved = 0;
4956 rsp->CreationTime = 0;
4957 rsp->LastAccessTime = 0;
4958 rsp->LastWriteTime = 0;
4959 rsp->ChangeTime = 0;
4960 rsp->AllocationSize = 0;
4961 rsp->EndOfFile = 0;
4962 rsp->Attributes = 0;
4963 inc_rfc1001_len(rsp, 60);
4964 return 0;
4965}
4966
4967/**
4968 * smb2_close() - handler for smb2 close file command
4969 * @work: smb work containing close request buffer
4970 *
4971 * Return: 0
4972 */
4973int smb2_close(struct ksmbd_work *work)
4974{
4975 unsigned int volatile_id = KSMBD_NO_FID;
Namjae Jeon64b39f42021-03-30 14:25:35 +09004976 u64 sess_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09004977 struct smb2_close_req *req;
4978 struct smb2_close_rsp *rsp;
4979 struct smb2_close_rsp *rsp_org;
4980 struct ksmbd_conn *conn = work->conn;
4981 struct ksmbd_file *fp;
4982 struct inode *inode;
4983 u64 time;
4984 int err = 0;
4985
Namjae Jeone5066492021-03-30 12:35:23 +09004986 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09004987 WORK_BUFFERS(work, req, rsp);
4988
4989 if (test_share_config_flag(work->tcon->share_conf,
4990 KSMBD_SHARE_FLAG_PIPE)) {
4991 ksmbd_debug(SMB, "IPC pipe close request\n");
4992 return smb2_close_pipe(work);
4993 }
4994
4995 sess_id = le64_to_cpu(req->hdr.SessionId);
4996 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
4997 sess_id = work->compound_sid;
4998
4999 work->compound_sid = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005000 if (check_session_id(conn, sess_id)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005001 work->compound_sid = sess_id;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005002 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005003 rsp->hdr.Status = STATUS_USER_SESSION_DELETED;
5004 if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)
5005 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5006 err = -EBADF;
5007 goto out;
5008 }
5009
5010 if (work->next_smb2_rcv_hdr_off &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005011 !HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005012 if (!HAS_FILE_ID(work->compound_fid)) {
5013 /* file already closed, return FILE_CLOSED */
5014 ksmbd_debug(SMB, "file already closed\n");
5015 rsp->hdr.Status = STATUS_FILE_CLOSED;
5016 err = -EBADF;
5017 goto out;
5018 } else {
5019 ksmbd_debug(SMB, "Compound request set FID = %u:%u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005020 work->compound_fid,
5021 work->compound_pfid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005022 volatile_id = work->compound_fid;
5023
5024 /* file closed, stored id is not valid anymore */
5025 work->compound_fid = KSMBD_NO_FID;
5026 work->compound_pfid = KSMBD_NO_FID;
5027 }
5028 } else {
5029 volatile_id = le64_to_cpu(req->VolatileFileId);
5030 }
5031 ksmbd_debug(SMB, "volatile_id = %u\n", volatile_id);
5032
5033 rsp->StructureSize = cpu_to_le16(60);
5034 rsp->Reserved = 0;
5035
5036 if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) {
5037 fp = ksmbd_lookup_fd_fast(work, volatile_id);
5038 if (!fp) {
5039 err = -ENOENT;
5040 goto out;
5041 }
5042
5043 inode = FP_INODE(fp);
5044 rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB;
5045 rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 :
5046 cpu_to_le64(inode->i_blocks << 9);
5047 rsp->EndOfFile = cpu_to_le64(inode->i_size);
5048 rsp->Attributes = fp->f_ci->m_fattr;
5049 rsp->CreationTime = cpu_to_le64(fp->create_time);
5050 time = ksmbd_UnixTimeToNT(inode->i_atime);
5051 rsp->LastAccessTime = cpu_to_le64(time);
5052 time = ksmbd_UnixTimeToNT(inode->i_mtime);
5053 rsp->LastWriteTime = cpu_to_le64(time);
5054 time = ksmbd_UnixTimeToNT(inode->i_ctime);
5055 rsp->ChangeTime = cpu_to_le64(time);
5056 ksmbd_fd_put(work, fp);
5057 } else {
5058 rsp->Flags = 0;
5059 rsp->AllocationSize = 0;
5060 rsp->EndOfFile = 0;
5061 rsp->Attributes = 0;
5062 rsp->CreationTime = 0;
5063 rsp->LastAccessTime = 0;
5064 rsp->LastWriteTime = 0;
5065 rsp->ChangeTime = 0;
5066 }
5067
5068 err = ksmbd_close_fd(work, volatile_id);
5069out:
5070 if (err) {
5071 if (rsp->hdr.Status == 0)
5072 rsp->hdr.Status = STATUS_FILE_CLOSED;
5073 smb2_set_err_rsp(work);
5074 } else {
5075 inc_rfc1001_len(rsp_org, 60);
5076 }
5077
5078 return 0;
5079}
5080
5081/**
5082 * smb2_echo() - handler for smb2 echo(ping) command
5083 * @work: smb work containing echo request buffer
5084 *
5085 * Return: 0
5086 */
5087int smb2_echo(struct ksmbd_work *work)
5088{
Namjae Jeone5066492021-03-30 12:35:23 +09005089 struct smb2_echo_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005090
5091 rsp->StructureSize = cpu_to_le16(4);
5092 rsp->Reserved = 0;
5093 inc_rfc1001_len(rsp, 4);
5094 return 0;
5095}
5096
Namjae Jeone2f34482021-03-16 10:49:09 +09005097static int smb2_rename(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005098 struct smb2_file_rename_info *file_info,
5099 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005100{
5101 struct ksmbd_share_config *share = fp->tcon->share_conf;
5102 char *new_name = NULL, *abs_oldname = NULL, *old_name = NULL;
5103 char *pathname = NULL;
5104 struct path path;
5105 bool file_present = true;
5106 int rc;
5107
5108 ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n");
5109 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5110 if (!pathname)
5111 return -ENOMEM;
5112
5113 abs_oldname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
5114 if (IS_ERR(abs_oldname)) {
5115 rc = -EINVAL;
5116 goto out;
5117 }
5118 old_name = strrchr(abs_oldname, '/');
Namjae Jeon64b39f42021-03-30 14:25:35 +09005119 if (old_name && old_name[1] != '\0') {
Namjae Jeone2f34482021-03-16 10:49:09 +09005120 old_name++;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005121 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005122 ksmbd_debug(SMB, "can't get last component in path %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005123 abs_oldname);
Namjae Jeone2f34482021-03-16 10:49:09 +09005124 rc = -ENOENT;
5125 goto out;
5126 }
5127
5128 new_name = smb2_get_name(share,
5129 file_info->FileName,
5130 le32_to_cpu(file_info->FileNameLength),
5131 local_nls);
5132 if (IS_ERR(new_name)) {
5133 rc = PTR_ERR(new_name);
5134 goto out;
5135 }
5136
5137 if (strchr(new_name, ':')) {
5138 int s_type;
5139 char *xattr_stream_name, *stream_name = NULL;
5140 size_t xattr_stream_size;
5141 int len;
5142
5143 rc = parse_stream_name(new_name, &stream_name, &s_type);
5144 if (rc < 0)
5145 goto out;
5146
5147 len = strlen(new_name);
5148 if (new_name[len - 1] != '/') {
5149 ksmbd_err("not allow base filename in rename\n");
5150 rc = -ESHARE;
5151 goto out;
5152 }
5153
5154 rc = ksmbd_vfs_xattr_stream_name(stream_name,
5155 &xattr_stream_name,
5156 &xattr_stream_size,
5157 s_type);
5158 if (rc)
5159 goto out;
5160
5161 rc = ksmbd_vfs_setxattr(fp->filp->f_path.dentry,
5162 xattr_stream_name,
5163 NULL, 0, 0);
5164 if (rc < 0) {
5165 ksmbd_err("failed to store stream name in xattr: %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005166 rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005167 rc = -EINVAL;
5168 goto out;
5169 }
5170
5171 goto out;
5172 }
5173
5174 ksmbd_debug(SMB, "new name %s\n", new_name);
5175 rc = ksmbd_vfs_kern_path(new_name, 0, &path, 1);
5176 if (rc)
5177 file_present = false;
5178 else
5179 path_put(&path);
5180
5181 if (ksmbd_share_veto_filename(share, new_name)) {
5182 rc = -ENOENT;
5183 ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name);
5184 goto out;
5185 }
5186
5187 if (file_info->ReplaceIfExists) {
5188 if (file_present) {
5189 rc = ksmbd_vfs_remove_file(work, new_name);
5190 if (rc) {
5191 if (rc != -ENOTEMPTY)
5192 rc = -EINVAL;
5193 ksmbd_debug(SMB, "cannot delete %s, rc %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005194 new_name, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005195 goto out;
5196 }
5197 }
5198 } else {
5199 if (file_present &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005200 strncmp(old_name, path.dentry->d_name.name, strlen(old_name))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005201 rc = -EEXIST;
5202 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005203 "cannot rename already existing file\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005204 goto out;
5205 }
5206 }
5207
5208 rc = ksmbd_vfs_fp_rename(work, fp, new_name);
5209out:
5210 kfree(pathname);
5211 if (!IS_ERR(new_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005212 kfree(new_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005213 return rc;
5214}
5215
Namjae Jeone2f34482021-03-16 10:49:09 +09005216static int smb2_create_link(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005217 struct ksmbd_share_config *share,
5218 struct smb2_file_link_info *file_info,
5219 struct file *filp,
5220 struct nls_table *local_nls)
Namjae Jeone2f34482021-03-16 10:49:09 +09005221{
5222 char *link_name = NULL, *target_name = NULL, *pathname = NULL;
5223 struct path path;
5224 bool file_present = true;
5225 int rc;
5226
5227 ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n");
5228 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
5229 if (!pathname)
5230 return -ENOMEM;
5231
5232 link_name = smb2_get_name(share,
5233 file_info->FileName,
5234 le32_to_cpu(file_info->FileNameLength),
5235 local_nls);
5236 if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) {
5237 rc = -EINVAL;
5238 goto out;
5239 }
5240
5241 ksmbd_debug(SMB, "link name is %s\n", link_name);
5242 target_name = d_path(&filp->f_path, pathname, PATH_MAX);
5243 if (IS_ERR(target_name)) {
5244 rc = -EINVAL;
5245 goto out;
5246 }
5247
5248 ksmbd_debug(SMB, "target name is %s\n", target_name);
5249 rc = ksmbd_vfs_kern_path(link_name, 0, &path, 0);
5250 if (rc)
5251 file_present = false;
5252 else
5253 path_put(&path);
5254
5255 if (file_info->ReplaceIfExists) {
5256 if (file_present) {
5257 rc = ksmbd_vfs_remove_file(work, link_name);
5258 if (rc) {
5259 rc = -EINVAL;
5260 ksmbd_debug(SMB, "cannot delete %s\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005261 link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005262 goto out;
5263 }
5264 }
5265 } else {
5266 if (file_present) {
5267 rc = -EEXIST;
5268 ksmbd_debug(SMB, "link already exists\n");
5269 goto out;
5270 }
5271 }
5272
5273 rc = ksmbd_vfs_link(work, target_name, link_name);
5274 if (rc)
5275 rc = -EINVAL;
5276out:
5277 if (!IS_ERR(link_name))
Marios Makassikis915f5702021-04-13 13:25:57 +09005278 kfree(link_name);
Namjae Jeone2f34482021-03-16 10:49:09 +09005279 kfree(pathname);
5280 return rc;
5281}
5282
Namjae Jeon64b39f42021-03-30 14:25:35 +09005283static int set_file_basic_info(struct ksmbd_file *fp, char *buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09005284 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005285{
5286 struct smb2_file_all_info *file_info;
5287 struct iattr attrs;
5288 struct iattr temp_attrs;
5289 struct file *filp;
5290 struct inode *inode;
5291 int rc;
5292
Marios Makassikis7adfd4f2021-04-27 15:30:22 +09005293 if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005294 return -EACCES;
5295
5296 file_info = (struct smb2_file_all_info *)buf;
5297 attrs.ia_valid = 0;
5298 filp = fp->filp;
5299 inode = file_inode(filp);
5300
5301 if (file_info->CreationTime)
5302 fp->create_time = le64_to_cpu(file_info->CreationTime);
5303
5304 if (file_info->LastAccessTime) {
5305 attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime);
5306 attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
5307 }
5308
5309 if (file_info->ChangeTime) {
5310 temp_attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime);
5311 attrs.ia_ctime = temp_attrs.ia_ctime;
5312 attrs.ia_valid |= ATTR_CTIME;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005313 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09005314 temp_attrs.ia_ctime = inode->i_ctime;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005315 }
Namjae Jeone2f34482021-03-16 10:49:09 +09005316
5317 if (file_info->LastWriteTime) {
5318 attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime);
5319 attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
5320 }
5321
5322 if (file_info->Attributes) {
5323 if (!S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005324 file_info->Attributes & ATTR_DIRECTORY_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005325 ksmbd_err("can't change a file to a directory\n");
5326 return -EINVAL;
5327 }
5328
5329 if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == ATTR_NORMAL_LE))
5330 fp->f_ci->m_fattr = file_info->Attributes |
5331 (fp->f_ci->m_fattr & ATTR_DIRECTORY_LE);
5332 }
5333
5334 if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) &&
5335 (file_info->CreationTime || file_info->Attributes)) {
5336 struct xattr_dos_attrib da = {0};
5337
5338 da.version = 4;
5339 da.itime = fp->itime;
5340 da.create_time = fp->create_time;
5341 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
5342 da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME |
5343 XATTR_DOSINFO_ITIME;
5344
5345 rc = ksmbd_vfs_set_dos_attrib_xattr(filp->f_path.dentry, &da);
5346 if (rc)
5347 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005348 "failed to restore file attribute in EA\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005349 rc = 0;
5350 }
5351
5352 /*
5353 * HACK : set ctime here to avoid ctime changed
5354 * when file_info->ChangeTime is zero.
5355 */
5356 attrs.ia_ctime = temp_attrs.ia_ctime;
5357 attrs.ia_valid |= ATTR_CTIME;
5358
5359 if (attrs.ia_valid) {
5360 struct dentry *dentry = filp->f_path.dentry;
5361 struct inode *inode = d_inode(dentry);
5362
5363 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
5364 return -EACCES;
5365
5366 rc = setattr_prepare(&init_user_ns, dentry, &attrs);
5367 if (rc)
5368 return -EINVAL;
5369
5370 inode_lock(inode);
5371 setattr_copy(&init_user_ns, inode, &attrs);
5372 attrs.ia_valid &= ~ATTR_CTIME;
5373 rc = notify_change(&init_user_ns, dentry, &attrs, NULL);
5374 inode_unlock(inode);
5375 }
5376 return 0;
5377}
5378
5379static int set_file_allocation_info(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005380 struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005381{
5382 /*
5383 * TODO : It's working fine only when store dos attributes
5384 * is not yes. need to implement a logic which works
5385 * properly with any smb.conf option
5386 */
5387
5388 struct smb2_file_alloc_info *file_alloc_info;
5389 loff_t alloc_blks;
5390 struct inode *inode;
5391 int rc;
5392
Marios Makassikisa2996692021-04-27 15:29:01 +09005393 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005394 return -EACCES;
5395
5396 file_alloc_info = (struct smb2_file_alloc_info *)buf;
5397 alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9;
5398 inode = file_inode(fp->filp);
5399
5400 if (alloc_blks > inode->i_blocks) {
5401 rc = ksmbd_vfs_alloc_size(work, fp, alloc_blks * 512);
5402 if (rc && rc != -EOPNOTSUPP) {
5403 ksmbd_err("ksmbd_vfs_alloc_size is failed : %d\n", rc);
5404 return rc;
5405 }
5406 } else if (alloc_blks < inode->i_blocks) {
5407 loff_t size;
5408
5409 /*
5410 * Allocation size could be smaller than original one
5411 * which means allocated blocks in file should be
5412 * deallocated. use truncate to cut out it, but inode
5413 * size is also updated with truncate offset.
5414 * inode size is retained by backup inode size.
5415 */
5416 size = i_size_read(inode);
5417 rc = ksmbd_vfs_truncate(work, NULL, fp, alloc_blks * 512);
5418 if (rc) {
5419 ksmbd_err("truncate failed! filename : %s, err %d\n",
5420 fp->filename, rc);
5421 return rc;
5422 }
5423 if (size < alloc_blks * 512)
5424 i_size_write(inode, size);
5425 }
5426 return 0;
5427}
5428
Namjae Jeon64b39f42021-03-30 14:25:35 +09005429static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005430 char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005431{
5432 struct smb2_file_eof_info *file_eof_info;
5433 loff_t newsize;
5434 struct inode *inode;
5435 int rc;
5436
Marios Makassikisa2996692021-04-27 15:29:01 +09005437 if (!(fp->daccess & FILE_WRITE_DATA_LE))
Namjae Jeone2f34482021-03-16 10:49:09 +09005438 return -EACCES;
5439
5440 file_eof_info = (struct smb2_file_eof_info *)buf;
5441 newsize = le64_to_cpu(file_eof_info->EndOfFile);
5442 inode = file_inode(fp->filp);
5443
5444 /*
5445 * If FILE_END_OF_FILE_INFORMATION of set_info_file is called
5446 * on FAT32 shared device, truncate execution time is too long
5447 * and network error could cause from windows client. because
5448 * truncate of some filesystem like FAT32 fill zero data in
5449 * truncated range.
5450 */
5451 if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) {
5452 ksmbd_debug(SMB, "filename : %s truncated to newsize %lld\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005453 fp->filename, newsize);
Namjae Jeone2f34482021-03-16 10:49:09 +09005454 rc = ksmbd_vfs_truncate(work, NULL, fp, newsize);
5455 if (rc) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09005456 ksmbd_debug(SMB, "truncate failed! filename : %s err %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005457 fp->filename, rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005458 if (rc != -EAGAIN)
5459 rc = -EBADF;
5460 return rc;
5461 }
5462 }
5463 return 0;
5464}
5465
Namjae Jeon64b39f42021-03-30 14:25:35 +09005466static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005467 char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005468{
5469 struct ksmbd_file *parent_fp;
5470
5471 if (!(fp->daccess & FILE_DELETE_LE)) {
5472 ksmbd_err("no right to delete : 0x%x\n", fp->daccess);
5473 return -EACCES;
5474 }
5475
5476 if (ksmbd_stream_fd(fp))
5477 goto next;
5478
5479 parent_fp = ksmbd_lookup_fd_inode(PARENT_INODE(fp));
5480 if (parent_fp) {
5481 if (parent_fp->daccess & FILE_DELETE_LE) {
5482 ksmbd_err("parent dir is opened with delete access\n");
5483 return -ESHARE;
5484 }
5485 }
5486next:
5487 return smb2_rename(work, fp,
5488 (struct smb2_file_rename_info *)buf,
5489 work->sess->conn->local_nls);
5490}
5491
Namjae Jeon64b39f42021-03-30 14:25:35 +09005492static int set_file_disposition_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005493{
5494 struct smb2_file_disposition_info *file_info;
5495 struct inode *inode;
5496
5497 if (!(fp->daccess & FILE_DELETE_LE)) {
5498 ksmbd_err("no right to delete : 0x%x\n", fp->daccess);
5499 return -EACCES;
5500 }
5501
5502 inode = file_inode(fp->filp);
5503 file_info = (struct smb2_file_disposition_info *)buf;
5504 if (file_info->DeletePending) {
5505 if (S_ISDIR(inode->i_mode) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09005506 ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY)
Namjae Jeone2f34482021-03-16 10:49:09 +09005507 return -EBUSY;
5508 ksmbd_set_inode_pending_delete(fp);
5509 } else {
5510 ksmbd_clear_inode_pending_delete(fp);
5511 }
5512 return 0;
5513}
5514
Namjae Jeon64b39f42021-03-30 14:25:35 +09005515static int set_file_position_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005516{
5517 struct smb2_file_pos_info *file_info;
5518 loff_t current_byte_offset;
5519 unsigned short sector_size;
5520 struct inode *inode;
5521
5522 inode = file_inode(fp->filp);
5523 file_info = (struct smb2_file_pos_info *)buf;
5524 current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset);
5525 sector_size = ksmbd_vfs_logical_sector_size(inode);
5526
5527 if (current_byte_offset < 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09005528 (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE &&
5529 current_byte_offset & (sector_size - 1))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005530 ksmbd_err("CurrentByteOffset is not valid : %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005531 current_byte_offset);
Namjae Jeone2f34482021-03-16 10:49:09 +09005532 return -EINVAL;
5533 }
5534
5535 fp->filp->f_pos = current_byte_offset;
5536 return 0;
5537}
5538
Namjae Jeon64b39f42021-03-30 14:25:35 +09005539static int set_file_mode_info(struct ksmbd_file *fp, char *buf)
Namjae Jeone2f34482021-03-16 10:49:09 +09005540{
5541 struct smb2_file_mode_info *file_info;
5542 __le32 mode;
5543
5544 file_info = (struct smb2_file_mode_info *)buf;
5545 mode = file_info->Mode;
5546
Namjae Jeon64b39f42021-03-30 14:25:35 +09005547 if ((mode & ~FILE_MODE_INFO_MASK) ||
5548 (mode & FILE_SYNCHRONOUS_IO_ALERT_LE &&
5549 mode & FILE_SYNCHRONOUS_IO_NONALERT_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005550 ksmbd_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode));
5551 return -EINVAL;
5552 }
5553
5554 /*
5555 * TODO : need to implement consideration for
5556 * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT
5557 */
5558 ksmbd_vfs_set_fadvise(fp->filp, mode);
5559 fp->coption = mode;
5560 return 0;
5561}
5562
5563/**
5564 * smb2_set_info_file() - handler for smb2 set info command
5565 * @work: smb work containing set info command buffer
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09005566 * @fp: ksmbd_file pointer
5567 * @info_class: smb2 set info class
5568 * @share: ksmbd_share_config pointer
Namjae Jeone2f34482021-03-16 10:49:09 +09005569 *
5570 * Return: 0 on success, otherwise error
5571 * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH
5572 */
Namjae Jeon64b39f42021-03-30 14:25:35 +09005573static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005574 int info_class, char *buf,
5575 struct ksmbd_share_config *share)
Namjae Jeone2f34482021-03-16 10:49:09 +09005576{
5577 switch (info_class) {
5578 case FILE_BASIC_INFORMATION:
5579 return set_file_basic_info(fp, buf, share);
5580
5581 case FILE_ALLOCATION_INFORMATION:
5582 return set_file_allocation_info(work, fp, buf);
5583
5584 case FILE_END_OF_FILE_INFORMATION:
5585 return set_end_of_file_info(work, fp, buf);
5586
5587 case FILE_RENAME_INFORMATION:
Namjae Jeon64b39f42021-03-30 14:25:35 +09005588 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005589 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005590 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005591 return -EACCES;
5592 }
5593 return set_rename_info(work, fp, buf);
5594
5595 case FILE_LINK_INFORMATION:
5596 return smb2_create_link(work, work->tcon->share_conf,
Namjae Jeon070fb212021-05-26 17:57:12 +09005597 (struct smb2_file_link_info *)buf, fp->filp,
5598 work->sess->conn->local_nls);
Namjae Jeone2f34482021-03-16 10:49:09 +09005599
5600 case FILE_DISPOSITION_INFORMATION:
Namjae Jeon64b39f42021-03-30 14:25:35 +09005601 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005602 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09005603 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09005604 return -EACCES;
5605 }
5606 return set_file_disposition_info(fp, buf);
5607
5608 case FILE_FULL_EA_INFORMATION:
5609 {
5610 if (!(fp->daccess & FILE_WRITE_EA_LE)) {
5611 ksmbd_err("Not permitted to write ext attr: 0x%x\n",
5612 fp->daccess);
5613 return -EACCES;
5614 }
5615
5616 return smb2_set_ea((struct smb2_ea_info *)buf,
5617 &fp->filp->f_path);
5618 }
5619
5620 case FILE_POSITION_INFORMATION:
5621 return set_file_position_info(fp, buf);
5622
5623 case FILE_MODE_INFORMATION:
5624 return set_file_mode_info(fp, buf);
5625 }
5626
5627 ksmbd_err("Unimplemented Fileinfoclass :%d\n", info_class);
5628 return -EOPNOTSUPP;
5629}
5630
Namjae Jeon64b39f42021-03-30 14:25:35 +09005631static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info,
Namjae Jeon070fb212021-05-26 17:57:12 +09005632 char *buffer, int buf_len)
Namjae Jeone2f34482021-03-16 10:49:09 +09005633{
5634 struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer;
5635
5636 fp->saccess |= FILE_SHARE_DELETE_LE;
5637
5638 return set_info_sec(fp->conn, fp->tcon, fp->filp->f_path.dentry, pntsd,
5639 buf_len, false);
5640}
5641
5642/**
5643 * smb2_set_info() - handler for smb2 set info command handler
5644 * @work: smb work containing set info request buffer
5645 *
5646 * Return: 0 on success, otherwise error
5647 */
5648int smb2_set_info(struct ksmbd_work *work)
5649{
5650 struct smb2_set_info_req *req;
5651 struct smb2_set_info_rsp *rsp, *rsp_org;
5652 struct ksmbd_file *fp;
5653 int rc = 0;
5654 unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID;
5655
5656 ksmbd_debug(SMB, "Received set info request\n");
5657
Namjae Jeone5066492021-03-30 12:35:23 +09005658 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005659 if (work->next_smb2_rcv_hdr_off) {
5660 req = REQUEST_BUF_NEXT(work);
5661 rsp = RESPONSE_BUF_NEXT(work);
5662 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
5663 ksmbd_debug(SMB, "Compound request set FID = %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005664 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09005665 id = work->compound_fid;
5666 pid = work->compound_pfid;
5667 }
5668 } else {
Namjae Jeone5066492021-03-30 12:35:23 +09005669 req = work->request_buf;
5670 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005671 }
5672
5673 if (!HAS_FILE_ID(id)) {
5674 id = le64_to_cpu(req->VolatileFileId);
5675 pid = le64_to_cpu(req->PersistentFileId);
5676 }
5677
5678 fp = ksmbd_lookup_fd_slow(work, id, pid);
5679 if (!fp) {
5680 ksmbd_debug(SMB, "Invalid id for close: %u\n", id);
5681 rc = -ENOENT;
5682 goto err_out;
5683 }
5684
5685 switch (req->InfoType) {
5686 case SMB2_O_INFO_FILE:
5687 ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n");
5688 rc = smb2_set_info_file(work, fp, req->FileInfoClass,
5689 req->Buffer, work->tcon->share_conf);
5690 break;
5691 case SMB2_O_INFO_SECURITY:
5692 ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n");
5693 rc = smb2_set_info_sec(fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09005694 le32_to_cpu(req->AdditionalInformation),
5695 req->Buffer,
5696 le32_to_cpu(req->BufferLength));
Namjae Jeone2f34482021-03-16 10:49:09 +09005697 break;
5698 default:
5699 rc = -EOPNOTSUPP;
5700 }
5701
5702 if (rc < 0)
5703 goto err_out;
5704
5705 rsp->StructureSize = cpu_to_le16(2);
5706 inc_rfc1001_len(rsp_org, 2);
5707 ksmbd_fd_put(work, fp);
5708 return 0;
5709
5710err_out:
5711 if (rc == -EACCES || rc == -EPERM)
5712 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5713 else if (rc == -EINVAL)
5714 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5715 else if (rc == -ESHARE)
5716 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
5717 else if (rc == -ENOENT)
5718 rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID;
5719 else if (rc == -EBUSY || rc == -ENOTEMPTY)
5720 rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY;
5721 else if (rc == -EAGAIN)
5722 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
Namjae Jeonff1d5722021-04-13 13:18:10 +09005723 else if (rc == -EBADF || rc == -ESTALE)
Namjae Jeone2f34482021-03-16 10:49:09 +09005724 rsp->hdr.Status = STATUS_INVALID_HANDLE;
5725 else if (rc == -EEXIST)
5726 rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION;
5727 else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP)
5728 rsp->hdr.Status = STATUS_INVALID_INFO_CLASS;
5729 smb2_set_err_rsp(work);
5730 ksmbd_fd_put(work, fp);
Namjae Jeon070fb212021-05-26 17:57:12 +09005731 ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc);
Namjae Jeone2f34482021-03-16 10:49:09 +09005732 return rc;
5733}
5734
5735/**
5736 * smb2_read_pipe() - handler for smb2 read from IPC pipe
5737 * @work: smb work containing read IPC pipe command buffer
5738 *
5739 * Return: 0 on success, otherwise error
5740 */
5741static noinline int smb2_read_pipe(struct ksmbd_work *work)
5742{
5743 int nbytes = 0, err;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005744 u64 id;
Namjae Jeone2f34482021-03-16 10:49:09 +09005745 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeone5066492021-03-30 12:35:23 +09005746 struct smb2_read_req *req = work->request_buf;
5747 struct smb2_read_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005748
5749 id = le64_to_cpu(req->VolatileFileId);
5750
5751 inc_rfc1001_len(rsp, 16);
5752 rpc_resp = ksmbd_rpc_read(work->sess, id);
5753 if (rpc_resp) {
5754 if (rpc_resp->flags != KSMBD_RPC_OK) {
5755 err = -EINVAL;
5756 goto out;
5757 }
5758
5759 work->aux_payload_buf =
Namjae Jeon79f6b112021-04-02 12:47:14 +09005760 kvmalloc(rpc_resp->payload_sz, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09005761 if (!work->aux_payload_buf) {
5762 err = -ENOMEM;
5763 goto out;
5764 }
5765
5766 memcpy(work->aux_payload_buf, rpc_resp->payload,
Namjae Jeon070fb212021-05-26 17:57:12 +09005767 rpc_resp->payload_sz);
Namjae Jeone2f34482021-03-16 10:49:09 +09005768
5769 nbytes = rpc_resp->payload_sz;
5770 work->resp_hdr_sz = get_rfc1002_len(rsp) + 4;
5771 work->aux_payload_sz = nbytes;
Namjae Jeon79f6b112021-04-02 12:47:14 +09005772 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005773 }
5774
5775 rsp->StructureSize = cpu_to_le16(17);
5776 rsp->DataOffset = 80;
5777 rsp->Reserved = 0;
5778 rsp->DataLength = cpu_to_le32(nbytes);
5779 rsp->DataRemaining = 0;
5780 rsp->Reserved2 = 0;
5781 inc_rfc1001_len(rsp, nbytes);
5782 return 0;
5783
5784out:
5785 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
5786 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09005787 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09005788 return err;
5789}
5790
5791static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09005792 struct smb2_read_req *req, void *data_buf,
5793 size_t length)
Namjae Jeone2f34482021-03-16 10:49:09 +09005794{
5795 struct smb2_buffer_desc_v1 *desc =
5796 (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
5797 int err;
5798
Namjae Jeon64b39f42021-03-30 14:25:35 +09005799 if (work->conn->dialect == SMB30_PROT_ID &&
5800 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09005801 return -EINVAL;
5802
Namjae Jeon64b39f42021-03-30 14:25:35 +09005803 if (req->ReadChannelInfoOffset == 0 ||
5804 le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09005805 return -EINVAL;
5806
5807 work->need_invalidate_rkey =
5808 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
5809 work->remote_key = le32_to_cpu(desc->token);
5810
Namjae Jeon64b39f42021-03-30 14:25:35 +09005811 err = ksmbd_conn_rdma_write(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09005812 le32_to_cpu(desc->token),
5813 le64_to_cpu(desc->offset),
5814 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09005815 if (err)
5816 return err;
5817
5818 return length;
5819}
5820
5821/**
5822 * smb2_read() - handler for smb2 read from file
5823 * @work: smb work containing read command buffer
5824 *
5825 * Return: 0 on success, otherwise error
5826 */
5827int smb2_read(struct ksmbd_work *work)
5828{
5829 struct ksmbd_conn *conn = work->conn;
5830 struct smb2_read_req *req;
5831 struct smb2_read_rsp *rsp, *rsp_org;
5832 struct ksmbd_file *fp;
5833 loff_t offset;
5834 size_t length, mincount;
5835 ssize_t nbytes = 0, remain_bytes = 0;
5836 int err = 0;
5837
Namjae Jeone5066492021-03-30 12:35:23 +09005838 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005839 WORK_BUFFERS(work, req, rsp);
5840
5841 if (test_share_config_flag(work->tcon->share_conf,
5842 KSMBD_SHARE_FLAG_PIPE)) {
5843 ksmbd_debug(SMB, "IPC pipe read request\n");
5844 return smb2_read_pipe(work);
5845 }
5846
Namjae Jeon070fb212021-05-26 17:57:12 +09005847 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
5848 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09005849 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09005850 err = -ENOENT;
5851 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09005852 }
5853
5854 if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
5855 ksmbd_err("Not permitted to read : 0x%x\n", fp->daccess);
5856 err = -EACCES;
5857 goto out;
5858 }
5859
5860 offset = le64_to_cpu(req->Offset);
5861 length = le32_to_cpu(req->Length);
5862 mincount = le32_to_cpu(req->MinimumCount);
5863
5864 if (length > conn->vals->max_read_size) {
5865 ksmbd_debug(SMB, "limiting read size to max size(%u)\n",
5866 conn->vals->max_read_size);
5867 err = -EINVAL;
5868 goto out;
5869 }
5870
5871 ksmbd_debug(SMB, "filename %s, offset %lld, len %zu\n", FP_FILENAME(fp),
Namjae Jeon070fb212021-05-26 17:57:12 +09005872 offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09005873
5874 if (server_conf.flags & KSMBD_GLOBAL_FLAG_CACHE_RBUF) {
5875 work->aux_payload_buf =
5876 ksmbd_find_buffer(conn->vals->max_read_size);
5877 work->set_read_buf = true;
5878 } else {
Namjae Jeon79f6b112021-04-02 12:47:14 +09005879 work->aux_payload_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09005880 }
5881 if (!work->aux_payload_buf) {
Dan Carpenterc1ea1112021-03-22 17:50:11 +03005882 err = -ENOMEM;
Namjae Jeone2f34482021-03-16 10:49:09 +09005883 goto out;
5884 }
5885
5886 nbytes = ksmbd_vfs_read(work, fp, length, &offset);
5887 if (nbytes < 0) {
5888 err = nbytes;
5889 goto out;
5890 }
5891
5892 if ((nbytes == 0 && length != 0) || nbytes < mincount) {
5893 if (server_conf.flags & KSMBD_GLOBAL_FLAG_CACHE_RBUF)
Namjae Jeone5066492021-03-30 12:35:23 +09005894 ksmbd_release_buffer(work->aux_payload_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005895 else
Namjae Jeon79f6b112021-04-02 12:47:14 +09005896 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09005897 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005898 rsp->hdr.Status = STATUS_END_OF_FILE;
5899 smb2_set_err_rsp(work);
5900 ksmbd_fd_put(work, fp);
5901 return 0;
5902 }
5903
5904 ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005905 nbytes, offset, mincount);
Namjae Jeone2f34482021-03-16 10:49:09 +09005906
5907 if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09005908 req->Channel == SMB2_CHANNEL_RDMA_V1) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005909 /* write data to the client using rdma channel */
5910 remain_bytes = smb2_read_rdma_channel(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09005911 work->aux_payload_buf,
5912 nbytes);
Namjae Jeone2f34482021-03-16 10:49:09 +09005913 if (server_conf.flags & KSMBD_GLOBAL_FLAG_CACHE_RBUF)
Namjae Jeone5066492021-03-30 12:35:23 +09005914 ksmbd_release_buffer(work->aux_payload_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09005915 else
Namjae Jeon79f6b112021-04-02 12:47:14 +09005916 kvfree(work->aux_payload_buf);
Namjae Jeone5066492021-03-30 12:35:23 +09005917 work->aux_payload_buf = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09005918
5919 nbytes = 0;
5920 if (remain_bytes < 0) {
5921 err = (int)remain_bytes;
5922 goto out;
5923 }
5924 }
5925
5926 rsp->StructureSize = cpu_to_le16(17);
5927 rsp->DataOffset = 80;
5928 rsp->Reserved = 0;
5929 rsp->DataLength = cpu_to_le32(nbytes);
5930 rsp->DataRemaining = cpu_to_le32(remain_bytes);
5931 rsp->Reserved2 = 0;
5932 inc_rfc1001_len(rsp_org, 16);
5933 work->resp_hdr_sz = get_rfc1002_len(rsp_org) + 4;
5934 work->aux_payload_sz = nbytes;
5935 inc_rfc1001_len(rsp_org, nbytes);
5936 ksmbd_fd_put(work, fp);
5937 return 0;
5938
5939out:
5940 if (err) {
5941 if (err == -EISDIR)
5942 rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST;
5943 else if (err == -EAGAIN)
5944 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
5945 else if (err == -ENOENT)
5946 rsp->hdr.Status = STATUS_FILE_CLOSED;
5947 else if (err == -EACCES)
5948 rsp->hdr.Status = STATUS_ACCESS_DENIED;
5949 else if (err == -ESHARE)
5950 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
5951 else if (err == -EINVAL)
5952 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
5953 else
5954 rsp->hdr.Status = STATUS_INVALID_HANDLE;
5955
5956 smb2_set_err_rsp(work);
5957 }
5958 ksmbd_fd_put(work, fp);
5959 return err;
5960}
5961
5962/**
5963 * smb2_write_pipe() - handler for smb2 write on IPC pipe
5964 * @work: smb work containing write IPC pipe command buffer
5965 *
5966 * Return: 0 on success, otherwise error
5967 */
5968static noinline int smb2_write_pipe(struct ksmbd_work *work)
5969{
Namjae Jeone5066492021-03-30 12:35:23 +09005970 struct smb2_write_req *req = work->request_buf;
5971 struct smb2_write_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09005972 struct ksmbd_rpc_command *rpc_resp;
Namjae Jeon64b39f42021-03-30 14:25:35 +09005973 u64 id = 0;
Namjae Jeone2f34482021-03-16 10:49:09 +09005974 int err = 0, ret = 0;
5975 char *data_buf;
5976 size_t length;
5977
5978 length = le32_to_cpu(req->Length);
5979 id = le64_to_cpu(req->VolatileFileId);
5980
5981 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09005982 (offsetof(struct smb2_write_req, Buffer) - 4)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005983 data_buf = (char *)&req->Buffer[0];
5984 } else {
5985 if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09005986 (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09005987 ksmbd_err("invalid write data offset %u, smb_len %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09005988 le16_to_cpu(req->DataOffset),
5989 get_rfc1002_len(req));
Namjae Jeone2f34482021-03-16 10:49:09 +09005990 err = -EINVAL;
5991 goto out;
5992 }
5993
5994 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
5995 le16_to_cpu(req->DataOffset));
5996 }
5997
5998 rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length);
5999 if (rpc_resp) {
6000 if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
6001 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
Namjae Jeon79f6b112021-04-02 12:47:14 +09006002 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006003 smb2_set_err_rsp(work);
6004 return -EOPNOTSUPP;
6005 }
6006 if (rpc_resp->flags != KSMBD_RPC_OK) {
6007 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6008 smb2_set_err_rsp(work);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006009 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006010 return ret;
6011 }
Namjae Jeon79f6b112021-04-02 12:47:14 +09006012 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09006013 }
6014
6015 rsp->StructureSize = cpu_to_le16(17);
6016 rsp->DataOffset = 0;
6017 rsp->Reserved = 0;
6018 rsp->DataLength = cpu_to_le32(length);
6019 rsp->DataRemaining = 0;
6020 rsp->Reserved2 = 0;
6021 inc_rfc1001_len(rsp, 16);
6022 return 0;
6023out:
6024 if (err) {
6025 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6026 smb2_set_err_rsp(work);
6027 }
6028
6029 return err;
6030}
6031
6032static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006033 struct smb2_write_req *req,
6034 struct ksmbd_file *fp,
6035 loff_t offset, size_t length, bool sync)
Namjae Jeone2f34482021-03-16 10:49:09 +09006036{
6037 struct smb2_buffer_desc_v1 *desc;
6038 char *data_buf;
6039 int ret;
6040 ssize_t nbytes;
6041
6042 desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
6043
6044 if (work->conn->dialect == SMB30_PROT_ID &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006045 req->Channel != SMB2_CHANNEL_RDMA_V1)
Namjae Jeone2f34482021-03-16 10:49:09 +09006046 return -EINVAL;
6047
6048 if (req->Length != 0 || req->DataOffset != 0)
6049 return -EINVAL;
6050
Namjae Jeon64b39f42021-03-30 14:25:35 +09006051 if (req->WriteChannelInfoOffset == 0 ||
6052 le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc))
Namjae Jeone2f34482021-03-16 10:49:09 +09006053 return -EINVAL;
6054
6055 work->need_invalidate_rkey =
6056 (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
6057 work->remote_key = le32_to_cpu(desc->token);
6058
Namjae Jeon79f6b112021-04-02 12:47:14 +09006059 data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
Namjae Jeone2f34482021-03-16 10:49:09 +09006060 if (!data_buf)
6061 return -ENOMEM;
6062
6063 ret = ksmbd_conn_rdma_read(work->conn, data_buf, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09006064 le32_to_cpu(desc->token),
6065 le64_to_cpu(desc->offset),
6066 le32_to_cpu(desc->length));
Namjae Jeone2f34482021-03-16 10:49:09 +09006067 if (ret < 0) {
Namjae Jeon79f6b112021-04-02 12:47:14 +09006068 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006069 return ret;
6070 }
6071
Namjae Jeon64b39f42021-03-30 14:25:35 +09006072 ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes);
Namjae Jeon79f6b112021-04-02 12:47:14 +09006073 kvfree(data_buf);
Namjae Jeone2f34482021-03-16 10:49:09 +09006074 if (ret < 0)
6075 return ret;
6076
6077 return nbytes;
6078}
6079
6080/**
6081 * smb2_write() - handler for smb2 write from file
6082 * @work: smb work containing write command buffer
6083 *
6084 * Return: 0 on success, otherwise error
6085 */
6086int smb2_write(struct ksmbd_work *work)
6087{
6088 struct smb2_write_req *req;
6089 struct smb2_write_rsp *rsp, *rsp_org;
Namjae Jeonbcd62a32021-05-10 09:08:19 +09006090 struct ksmbd_file *fp = NULL;
Namjae Jeone2f34482021-03-16 10:49:09 +09006091 loff_t offset;
6092 size_t length;
6093 ssize_t nbytes;
6094 char *data_buf;
6095 bool writethrough = false;
6096 int err = 0;
6097
Namjae Jeone5066492021-03-30 12:35:23 +09006098 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006099 WORK_BUFFERS(work, req, rsp);
6100
Namjae Jeon64b39f42021-03-30 14:25:35 +09006101 if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006102 ksmbd_debug(SMB, "IPC pipe write request\n");
6103 return smb2_write_pipe(work);
6104 }
6105
6106 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
6107 ksmbd_debug(SMB, "User does not have write permission\n");
6108 err = -EACCES;
6109 goto out;
6110 }
6111
Namjae Jeon64b39f42021-03-30 14:25:35 +09006112 fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
Namjae Jeon070fb212021-05-26 17:57:12 +09006113 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006114 if (!fp) {
Marios Makassikisa4382db2021-05-06 11:34:52 +09006115 err = -ENOENT;
6116 goto out;
Namjae Jeone2f34482021-03-16 10:49:09 +09006117 }
6118
6119 if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) {
6120 ksmbd_err("Not permitted to write : 0x%x\n", fp->daccess);
6121 err = -EACCES;
6122 goto out;
6123 }
6124
6125 offset = le64_to_cpu(req->Offset);
6126 length = le32_to_cpu(req->Length);
6127
6128 if (length > work->conn->vals->max_write_size) {
6129 ksmbd_debug(SMB, "limiting write size to max size(%u)\n",
6130 work->conn->vals->max_write_size);
6131 err = -EINVAL;
6132 goto out;
6133 }
6134
6135 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6136 writethrough = true;
6137
6138 if (req->Channel != SMB2_CHANNEL_RDMA_V1 &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006139 req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006140 if (le16_to_cpu(req->DataOffset) ==
Namjae Jeon070fb212021-05-26 17:57:12 +09006141 (offsetof(struct smb2_write_req, Buffer) - 4)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006142 data_buf = (char *)&req->Buffer[0];
6143 } else {
Namjae Jeon64b39f42021-03-30 14:25:35 +09006144 if ((le16_to_cpu(req->DataOffset) > get_rfc1002_len(req)) ||
6145 (le16_to_cpu(req->DataOffset) + length > get_rfc1002_len(req))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006146 ksmbd_err("invalid write data offset %u, smb_len %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006147 le16_to_cpu(req->DataOffset),
6148 get_rfc1002_len(req));
Namjae Jeone2f34482021-03-16 10:49:09 +09006149 err = -EINVAL;
6150 goto out;
6151 }
6152
6153 data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
6154 le16_to_cpu(req->DataOffset));
6155 }
6156
6157 ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags));
6158 if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
6159 writethrough = true;
6160
6161 ksmbd_debug(SMB, "filename %s, offset %lld, len %zu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006162 FP_FILENAME(fp), offset, length);
Namjae Jeone2f34482021-03-16 10:49:09 +09006163 err = ksmbd_vfs_write(work, fp, data_buf, length, &offset,
6164 writethrough, &nbytes);
6165 if (err < 0)
6166 goto out;
6167 } else {
6168 /* read data from the client using rdma channel, and
6169 * write the data.
6170 */
6171 nbytes = smb2_write_rdma_channel(work, req, fp, offset,
Namjae Jeon070fb212021-05-26 17:57:12 +09006172 le32_to_cpu(req->RemainingBytes),
6173 writethrough);
Namjae Jeone2f34482021-03-16 10:49:09 +09006174 if (nbytes < 0) {
6175 err = (int)nbytes;
6176 goto out;
6177 }
6178 }
6179
6180 rsp->StructureSize = cpu_to_le16(17);
6181 rsp->DataOffset = 0;
6182 rsp->Reserved = 0;
6183 rsp->DataLength = cpu_to_le32(nbytes);
6184 rsp->DataRemaining = 0;
6185 rsp->Reserved2 = 0;
6186 inc_rfc1001_len(rsp_org, 16);
6187 ksmbd_fd_put(work, fp);
6188 return 0;
6189
6190out:
6191 if (err == -EAGAIN)
6192 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6193 else if (err == -ENOSPC || err == -EFBIG)
6194 rsp->hdr.Status = STATUS_DISK_FULL;
6195 else if (err == -ENOENT)
6196 rsp->hdr.Status = STATUS_FILE_CLOSED;
6197 else if (err == -EACCES)
6198 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6199 else if (err == -ESHARE)
6200 rsp->hdr.Status = STATUS_SHARING_VIOLATION;
6201 else if (err == -EINVAL)
6202 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6203 else
6204 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6205
6206 smb2_set_err_rsp(work);
6207 ksmbd_fd_put(work, fp);
6208 return err;
6209}
6210
6211/**
6212 * smb2_flush() - handler for smb2 flush file - fsync
6213 * @work: smb work containing flush command buffer
6214 *
6215 * Return: 0 on success, otherwise error
6216 */
6217int smb2_flush(struct ksmbd_work *work)
6218{
6219 struct smb2_flush_req *req;
6220 struct smb2_flush_rsp *rsp, *rsp_org;
6221 int err;
6222
Namjae Jeone5066492021-03-30 12:35:23 +09006223 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006224 WORK_BUFFERS(work, req, rsp);
6225
6226 ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006227 le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006228
6229 err = ksmbd_vfs_fsync(work,
6230 le64_to_cpu(req->VolatileFileId),
6231 le64_to_cpu(req->PersistentFileId));
6232 if (err)
6233 goto out;
6234
6235 rsp->StructureSize = cpu_to_le16(4);
6236 rsp->Reserved = 0;
6237 inc_rfc1001_len(rsp_org, 4);
6238 return 0;
6239
6240out:
6241 if (err) {
6242 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6243 smb2_set_err_rsp(work);
6244 }
6245
6246 return err;
6247}
6248
6249/**
6250 * smb2_cancel() - handler for smb2 cancel command
6251 * @work: smb work containing cancel command buffer
6252 *
6253 * Return: 0 on success, otherwise error
6254 */
6255int smb2_cancel(struct ksmbd_work *work)
6256{
6257 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09006258 struct smb2_hdr *hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006259 struct smb2_hdr *chdr;
6260 struct ksmbd_work *cancel_work = NULL;
6261 struct list_head *tmp;
6262 int canceled = 0;
6263 struct list_head *command_list;
6264
6265 ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006266 hdr->MessageId, hdr->Flags);
Namjae Jeone2f34482021-03-16 10:49:09 +09006267
6268 if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) {
6269 command_list = &conn->async_requests;
6270
6271 spin_lock(&conn->request_lock);
6272 list_for_each(tmp, command_list) {
6273 cancel_work = list_entry(tmp, struct ksmbd_work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006274 async_request_entry);
Namjae Jeone5066492021-03-30 12:35:23 +09006275 chdr = cancel_work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006276
6277 if (cancel_work->async_id !=
Namjae Jeon64b39f42021-03-30 14:25:35 +09006278 le64_to_cpu(hdr->Id.AsyncId))
Namjae Jeone2f34482021-03-16 10:49:09 +09006279 continue;
6280
6281 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006282 "smb2 with AsyncId %llu cancelled command = 0x%x\n",
6283 le64_to_cpu(hdr->Id.AsyncId),
6284 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006285 canceled = 1;
6286 break;
6287 }
6288 spin_unlock(&conn->request_lock);
6289 } else {
6290 command_list = &conn->requests;
6291
6292 spin_lock(&conn->request_lock);
6293 list_for_each(tmp, command_list) {
6294 cancel_work = list_entry(tmp, struct ksmbd_work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006295 request_entry);
Namjae Jeone5066492021-03-30 12:35:23 +09006296 chdr = cancel_work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006297
6298 if (chdr->MessageId != hdr->MessageId ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006299 cancel_work == work)
Namjae Jeone2f34482021-03-16 10:49:09 +09006300 continue;
6301
6302 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006303 "smb2 with mid %llu cancelled command = 0x%x\n",
6304 le64_to_cpu(hdr->MessageId),
6305 le16_to_cpu(chdr->Command));
Namjae Jeone2f34482021-03-16 10:49:09 +09006306 canceled = 1;
6307 break;
6308 }
6309 spin_unlock(&conn->request_lock);
6310 }
6311
6312 if (canceled) {
6313 cancel_work->state = KSMBD_WORK_CANCELLED;
6314 if (cancel_work->cancel_fn)
6315 cancel_work->cancel_fn(cancel_work->cancel_argv);
6316 }
6317
6318 /* For SMB2_CANCEL command itself send no response*/
6319 work->send_no_response = 1;
6320 return 0;
6321}
6322
6323struct file_lock *smb_flock_init(struct file *f)
6324{
6325 struct file_lock *fl;
6326
6327 fl = locks_alloc_lock();
6328 if (!fl)
6329 goto out;
6330
6331 locks_init_lock(fl);
6332
6333 fl->fl_owner = f;
6334 fl->fl_pid = current->tgid;
6335 fl->fl_file = f;
6336 fl->fl_flags = FL_POSIX;
6337 fl->fl_ops = NULL;
6338 fl->fl_lmops = NULL;
6339
6340out:
6341 return fl;
6342}
6343
6344static int smb2_set_flock_flags(struct file_lock *flock, int flags)
6345{
6346 int cmd = -EINVAL;
6347
6348 /* Checking for wrong flag combination during lock request*/
6349 switch (flags) {
6350 case SMB2_LOCKFLAG_SHARED:
6351 ksmbd_debug(SMB, "received shared request\n");
6352 cmd = F_SETLKW;
6353 flock->fl_type = F_RDLCK;
6354 flock->fl_flags |= FL_SLEEP;
6355 break;
6356 case SMB2_LOCKFLAG_EXCLUSIVE:
6357 ksmbd_debug(SMB, "received exclusive request\n");
6358 cmd = F_SETLKW;
6359 flock->fl_type = F_WRLCK;
6360 flock->fl_flags |= FL_SLEEP;
6361 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006362 case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006363 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006364 "received shared & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006365 cmd = F_SETLK;
6366 flock->fl_type = F_RDLCK;
6367 break;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006368 case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY:
Namjae Jeone2f34482021-03-16 10:49:09 +09006369 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006370 "received exclusive & fail immediately request\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006371 cmd = F_SETLK;
6372 flock->fl_type = F_WRLCK;
6373 break;
6374 case SMB2_LOCKFLAG_UNLOCK:
6375 ksmbd_debug(SMB, "received unlock request\n");
6376 flock->fl_type = F_UNLCK;
6377 cmd = 0;
6378 break;
6379 }
6380
6381 return cmd;
6382}
6383
6384static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock,
Namjae Jeon070fb212021-05-26 17:57:12 +09006385 unsigned int cmd, int flags,
6386 struct list_head *lock_list)
Namjae Jeone2f34482021-03-16 10:49:09 +09006387{
6388 struct ksmbd_lock *lock;
6389
6390 lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL);
6391 if (!lock)
6392 return NULL;
6393
6394 lock->cmd = cmd;
6395 lock->fl = flock;
6396 lock->start = flock->fl_start;
6397 lock->end = flock->fl_end;
6398 lock->flags = flags;
6399 if (lock->start == lock->end)
6400 lock->zero_len = 1;
6401 INIT_LIST_HEAD(&lock->llist);
6402 INIT_LIST_HEAD(&lock->glist);
6403 list_add_tail(&lock->llist, lock_list);
6404
6405 return lock;
6406}
6407
6408static void smb2_remove_blocked_lock(void **argv)
6409{
6410 struct file_lock *flock = (struct file_lock *)argv[0];
6411
6412 ksmbd_vfs_posix_lock_unblock(flock);
6413 wake_up(&flock->fl_wait);
6414}
6415
6416static inline bool lock_defer_pending(struct file_lock *fl)
6417{
6418 /* check pending lock waiters */
6419 return waitqueue_active(&fl->fl_wait);
6420}
6421
6422/**
6423 * smb2_lock() - handler for smb2 file lock command
6424 * @work: smb work containing lock command buffer
6425 *
6426 * Return: 0 on success, otherwise error
6427 */
6428int smb2_lock(struct ksmbd_work *work)
6429{
Namjae Jeone5066492021-03-30 12:35:23 +09006430 struct smb2_lock_req *req = work->request_buf;
6431 struct smb2_lock_rsp *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09006432 struct smb2_lock_element *lock_ele;
6433 struct ksmbd_file *fp = NULL;
6434 struct file_lock *flock = NULL;
6435 struct file *filp = NULL;
6436 int lock_count;
6437 int flags = 0;
6438 int cmd = 0;
6439 int err = 0, i;
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006440 u64 lock_start, lock_length;
Namjae Jeone2f34482021-03-16 10:49:09 +09006441 struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp;
6442 int nolock = 0;
6443 LIST_HEAD(lock_list);
6444 LIST_HEAD(rollback_list);
6445 int prior_lock = 0;
6446
6447 ksmbd_debug(SMB, "Received lock request\n");
6448 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006449 le64_to_cpu(req->VolatileFileId),
6450 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006451 if (!fp) {
6452 ksmbd_debug(SMB, "Invalid file id for lock : %llu\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09006453 le64_to_cpu(req->VolatileFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006454 rsp->hdr.Status = STATUS_FILE_CLOSED;
6455 goto out2;
6456 }
6457
6458 filp = fp->filp;
6459 lock_count = le16_to_cpu(req->LockCount);
6460 lock_ele = req->locks;
6461
6462 ksmbd_debug(SMB, "lock count is %d\n", lock_count);
Namjae Jeon070fb212021-05-26 17:57:12 +09006463 if (!lock_count) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006464 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6465 goto out2;
6466 }
6467
6468 for (i = 0; i < lock_count; i++) {
6469 flags = le32_to_cpu(lock_ele[i].Flags);
6470
6471 flock = smb_flock_init(filp);
6472 if (!flock) {
6473 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6474 goto out;
6475 }
6476
6477 cmd = smb2_set_flock_flags(flock, flags);
6478
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006479 lock_start = le64_to_cpu(lock_ele[i].Offset);
6480 lock_length = le64_to_cpu(lock_ele[i].Length);
6481 if (lock_start > U64_MAX - lock_length) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006482 ksmbd_err("Invalid lock range requested\n");
6483 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6484 goto out;
6485 }
6486
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006487 if (lock_start > OFFSET_MAX)
6488 flock->fl_start = OFFSET_MAX;
6489 else
6490 flock->fl_start = lock_start;
6491
Namjae Jeone2f34482021-03-16 10:49:09 +09006492 lock_length = le64_to_cpu(lock_ele[i].Length);
Namjae Jeon50bf80a2021-05-14 12:20:07 +09006493 if (lock_length > OFFSET_MAX - flock->fl_start)
6494 lock_length = OFFSET_MAX - flock->fl_start;
Namjae Jeone2f34482021-03-16 10:49:09 +09006495
6496 flock->fl_end = flock->fl_start + lock_length;
6497
6498 if (flock->fl_end < flock->fl_start) {
6499 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006500 "the end offset(%llx) is smaller than the start offset(%llx)\n",
6501 flock->fl_end, flock->fl_start);
Namjae Jeone2f34482021-03-16 10:49:09 +09006502 rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE;
6503 goto out;
6504 }
6505
6506 /* Check conflict locks in one request */
6507 list_for_each_entry(cmp_lock, &lock_list, llist) {
6508 if (cmp_lock->fl->fl_start <= flock->fl_start &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006509 cmp_lock->fl->fl_end >= flock->fl_end) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006510 if (cmp_lock->fl->fl_type != F_UNLCK &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006511 flock->fl_type != F_UNLCK) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006512 ksmbd_err("conflict two locks in one request\n");
6513 rsp->hdr.Status =
6514 STATUS_INVALID_PARAMETER;
6515 goto out;
6516 }
6517 }
6518 }
6519
6520 smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list);
6521 if (!smb_lock) {
6522 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6523 goto out;
6524 }
6525 }
6526
6527 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6528 if (smb_lock->cmd < 0) {
6529 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6530 goto out;
6531 }
6532
6533 if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) {
6534 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6535 goto out;
6536 }
6537
Namjae Jeon64b39f42021-03-30 14:25:35 +09006538 if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) &&
6539 smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) ||
6540 (prior_lock == SMB2_LOCKFLAG_UNLOCK &&
6541 !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006542 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6543 goto out;
6544 }
6545
6546 prior_lock = smb_lock->flags;
6547
6548 if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006549 !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY))
Namjae Jeone2f34482021-03-16 10:49:09 +09006550 goto no_check_gl;
6551
6552 nolock = 1;
6553 /* check locks in global list */
6554 list_for_each_entry(cmp_lock, &global_lock_list, glist) {
6555 if (file_inode(cmp_lock->fl->fl_file) !=
Namjae Jeon64b39f42021-03-30 14:25:35 +09006556 file_inode(smb_lock->fl->fl_file))
Namjae Jeone2f34482021-03-16 10:49:09 +09006557 continue;
6558
6559 if (smb_lock->fl->fl_type == F_UNLCK) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09006560 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file &&
6561 cmp_lock->start == smb_lock->start &&
6562 cmp_lock->end == smb_lock->end &&
6563 !lock_defer_pending(cmp_lock->fl)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006564 nolock = 0;
6565 locks_free_lock(cmp_lock->fl);
6566 list_del(&cmp_lock->glist);
6567 kfree(cmp_lock);
6568 break;
6569 }
6570 continue;
6571 }
6572
6573 if (cmp_lock->fl->fl_file == smb_lock->fl->fl_file) {
6574 if (smb_lock->flags & SMB2_LOCKFLAG_SHARED)
6575 continue;
6576 } else {
6577 if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED)
6578 continue;
6579 }
6580
6581 /* check zero byte lock range */
6582 if (cmp_lock->zero_len && !smb_lock->zero_len &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006583 cmp_lock->start > smb_lock->start &&
6584 cmp_lock->start < smb_lock->end) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006585 ksmbd_err("previous lock conflict with zero byte lock range\n");
6586 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6587 goto out;
6588 }
6589
6590 if (smb_lock->zero_len && !cmp_lock->zero_len &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006591 smb_lock->start > cmp_lock->start &&
6592 smb_lock->start < cmp_lock->end) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006593 ksmbd_err("current lock conflict with zero byte lock range\n");
6594 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6595 goto out;
6596 }
6597
6598 if (((cmp_lock->start <= smb_lock->start &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09006599 cmp_lock->end > smb_lock->start) ||
6600 (cmp_lock->start < smb_lock->end && cmp_lock->end >= smb_lock->end)) &&
6601 !cmp_lock->zero_len && !smb_lock->zero_len) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006602 ksmbd_err("Not allow lock operation on exclusive lock range\n");
6603 rsp->hdr.Status =
6604 STATUS_LOCK_NOT_GRANTED;
6605 goto out;
6606 }
6607 }
6608
6609 if (smb_lock->fl->fl_type == F_UNLCK && nolock) {
6610 ksmbd_err("Try to unlock nolocked range\n");
6611 rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED;
6612 goto out;
6613 }
6614
6615no_check_gl:
6616 if (smb_lock->zero_len) {
6617 err = 0;
6618 goto skip;
6619 }
6620
6621 flock = smb_lock->fl;
6622 list_del(&smb_lock->llist);
6623retry:
6624 err = ksmbd_vfs_lock(filp, smb_lock->cmd, flock);
6625skip:
6626 if (flags & SMB2_LOCKFLAG_UNLOCK) {
Namjae Jeon64b39f42021-03-30 14:25:35 +09006627 if (!err) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006628 ksmbd_debug(SMB, "File unlocked\n");
Namjae Jeon64b39f42021-03-30 14:25:35 +09006629 } else if (err == -ENOENT) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006630 rsp->hdr.Status = STATUS_NOT_LOCKED;
6631 goto out;
6632 }
6633 locks_free_lock(flock);
6634 kfree(smb_lock);
6635 } else {
6636 if (err == FILE_LOCK_DEFERRED) {
6637 void **argv;
6638
6639 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09006640 "would have to wait for getting lock\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09006641 list_add_tail(&smb_lock->glist,
Namjae Jeon070fb212021-05-26 17:57:12 +09006642 &global_lock_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09006643 list_add(&smb_lock->llist, &rollback_list);
6644
6645 argv = kmalloc(sizeof(void *), GFP_KERNEL);
6646 if (!argv) {
6647 err = -ENOMEM;
6648 goto out;
6649 }
6650 argv[0] = flock;
6651
6652 err = setup_async_work(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006653 smb2_remove_blocked_lock,
6654 argv);
Namjae Jeone2f34482021-03-16 10:49:09 +09006655 if (err) {
6656 rsp->hdr.Status =
6657 STATUS_INSUFFICIENT_RESOURCES;
6658 goto out;
6659 }
6660 spin_lock(&fp->f_lock);
6661 list_add(&work->fp_entry, &fp->blocked_works);
6662 spin_unlock(&fp->f_lock);
6663
6664 smb2_send_interim_resp(work, STATUS_PENDING);
6665
6666 err = ksmbd_vfs_posix_lock_wait(flock);
6667
6668 if (!WORK_ACTIVE(work)) {
6669 list_del(&smb_lock->llist);
6670 list_del(&smb_lock->glist);
6671 locks_free_lock(flock);
6672
6673 if (WORK_CANCELLED(work)) {
6674 spin_lock(&fp->f_lock);
6675 list_del(&work->fp_entry);
6676 spin_unlock(&fp->f_lock);
6677 rsp->hdr.Status =
6678 STATUS_CANCELLED;
6679 kfree(smb_lock);
6680 smb2_send_interim_resp(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006681 STATUS_CANCELLED);
Namjae Jeone2f34482021-03-16 10:49:09 +09006682 work->send_no_response = 1;
6683 goto out;
6684 }
6685 init_smb2_rsp_hdr(work);
6686 smb2_set_err_rsp(work);
6687 rsp->hdr.Status =
6688 STATUS_RANGE_NOT_LOCKED;
6689 kfree(smb_lock);
6690 goto out2;
6691 }
6692
6693 list_del(&smb_lock->llist);
6694 list_del(&smb_lock->glist);
6695 spin_lock(&fp->f_lock);
6696 list_del(&work->fp_entry);
6697 spin_unlock(&fp->f_lock);
6698 goto retry;
6699 } else if (!err) {
6700 list_add_tail(&smb_lock->glist,
Namjae Jeon070fb212021-05-26 17:57:12 +09006701 &global_lock_list);
Namjae Jeone2f34482021-03-16 10:49:09 +09006702 list_add(&smb_lock->llist, &rollback_list);
6703 ksmbd_debug(SMB, "successful in taking lock\n");
6704 } else {
6705 rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED;
6706 goto out;
6707 }
6708 }
6709 }
6710
6711 if (atomic_read(&fp->f_ci->op_count) > 1)
6712 smb_break_all_oplock(work, fp);
6713
6714 rsp->StructureSize = cpu_to_le16(4);
6715 ksmbd_debug(SMB, "successful in taking lock\n");
6716 rsp->hdr.Status = STATUS_SUCCESS;
6717 rsp->Reserved = 0;
6718 inc_rfc1001_len(rsp, 4);
6719 ksmbd_fd_put(work, fp);
6720 return err;
6721
6722out:
6723 list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) {
6724 locks_free_lock(smb_lock->fl);
6725 list_del(&smb_lock->llist);
6726 kfree(smb_lock);
6727 }
6728
6729 list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) {
6730 struct file_lock *rlock = NULL;
6731
6732 rlock = smb_flock_init(filp);
6733 rlock->fl_type = F_UNLCK;
6734 rlock->fl_start = smb_lock->start;
6735 rlock->fl_end = smb_lock->end;
6736
6737 err = ksmbd_vfs_lock(filp, 0, rlock);
6738 if (err)
6739 ksmbd_err("rollback unlock fail : %d\n", err);
6740 list_del(&smb_lock->llist);
6741 list_del(&smb_lock->glist);
6742 locks_free_lock(smb_lock->fl);
6743 locks_free_lock(rlock);
6744 kfree(smb_lock);
6745 }
6746out2:
6747 ksmbd_debug(SMB, "failed in taking lock(flags : %x)\n", flags);
6748 smb2_set_err_rsp(work);
6749 ksmbd_fd_put(work, fp);
6750 return 0;
6751}
6752
Namjae Jeon64b39f42021-03-30 14:25:35 +09006753static int fsctl_copychunk(struct ksmbd_work *work, struct smb2_ioctl_req *req,
Namjae Jeon070fb212021-05-26 17:57:12 +09006754 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09006755{
6756 struct copychunk_ioctl_req *ci_req;
6757 struct copychunk_ioctl_rsp *ci_rsp;
6758 struct ksmbd_file *src_fp = NULL, *dst_fp = NULL;
6759 struct srv_copychunk *chunks;
6760 unsigned int i, chunk_count, chunk_count_written = 0;
6761 unsigned int chunk_size_written = 0;
6762 loff_t total_size_written = 0;
6763 int ret, cnt_code;
6764
6765 cnt_code = le32_to_cpu(req->CntCode);
6766 ci_req = (struct copychunk_ioctl_req *)&req->Buffer[0];
6767 ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0];
6768
6769 rsp->VolatileFileId = req->VolatileFileId;
6770 rsp->PersistentFileId = req->PersistentFileId;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006771 ci_rsp->ChunksWritten =
6772 cpu_to_le32(ksmbd_server_side_copy_max_chunk_count());
6773 ci_rsp->ChunkBytesWritten =
6774 cpu_to_le32(ksmbd_server_side_copy_max_chunk_size());
6775 ci_rsp->TotalBytesWritten =
6776 cpu_to_le32(ksmbd_server_side_copy_max_total_size());
Namjae Jeone2f34482021-03-16 10:49:09 +09006777
6778 chunks = (struct srv_copychunk *)&ci_req->Chunks[0];
6779 chunk_count = le32_to_cpu(ci_req->ChunkCount);
6780 total_size_written = 0;
6781
6782 /* verify the SRV_COPYCHUNK_COPY packet */
6783 if (chunk_count > ksmbd_server_side_copy_max_chunk_count() ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006784 le32_to_cpu(req->InputCount) <
6785 offsetof(struct copychunk_ioctl_req, Chunks) +
6786 chunk_count * sizeof(struct srv_copychunk)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006787 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6788 return -EINVAL;
6789 }
6790
6791 for (i = 0; i < chunk_count; i++) {
6792 if (le32_to_cpu(chunks[i].Length) == 0 ||
Namjae Jeon64b39f42021-03-30 14:25:35 +09006793 le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size())
Namjae Jeone2f34482021-03-16 10:49:09 +09006794 break;
6795 total_size_written += le32_to_cpu(chunks[i].Length);
6796 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09006797
6798 if (i < chunk_count ||
6799 total_size_written > ksmbd_server_side_copy_max_total_size()) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006800 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6801 return -EINVAL;
6802 }
6803
6804 src_fp = ksmbd_lookup_foreign_fd(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006805 le64_to_cpu(ci_req->ResumeKey[0]));
Namjae Jeone2f34482021-03-16 10:49:09 +09006806 dst_fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09006807 le64_to_cpu(req->VolatileFileId),
6808 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09006809 ret = -EINVAL;
Namjae Jeon64b39f42021-03-30 14:25:35 +09006810 if (!src_fp ||
6811 src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006812 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
6813 goto out;
6814 }
Namjae Jeon64b39f42021-03-30 14:25:35 +09006815
Namjae Jeone2f34482021-03-16 10:49:09 +09006816 if (!dst_fp) {
6817 rsp->hdr.Status = STATUS_FILE_CLOSED;
6818 goto out;
6819 }
6820
6821 /*
6822 * FILE_READ_DATA should only be included in
6823 * the FSCTL_COPYCHUNK case
6824 */
Namjae Jeon070fb212021-05-26 17:57:12 +09006825 if (cnt_code == FSCTL_COPYCHUNK &&
6826 !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09006827 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6828 goto out;
6829 }
6830
6831 ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp,
Namjae Jeon070fb212021-05-26 17:57:12 +09006832 chunks, chunk_count,
6833 &chunk_count_written,
6834 &chunk_size_written,
6835 &total_size_written);
Namjae Jeone2f34482021-03-16 10:49:09 +09006836 if (ret < 0) {
6837 if (ret == -EACCES)
6838 rsp->hdr.Status = STATUS_ACCESS_DENIED;
6839 if (ret == -EAGAIN)
6840 rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT;
6841 else if (ret == -EBADF)
6842 rsp->hdr.Status = STATUS_INVALID_HANDLE;
6843 else if (ret == -EFBIG || ret == -ENOSPC)
6844 rsp->hdr.Status = STATUS_DISK_FULL;
6845 else if (ret == -EINVAL)
6846 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
6847 else if (ret == -EISDIR)
6848 rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY;
6849 else if (ret == -E2BIG)
6850 rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE;
6851 else
6852 rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR;
6853 }
6854
6855 ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written);
6856 ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written);
6857 ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written);
6858out:
6859 ksmbd_fd_put(work, src_fp);
6860 ksmbd_fd_put(work, dst_fp);
6861 return ret;
6862}
6863
6864static __be32 idev_ipv4_address(struct in_device *idev)
6865{
6866 __be32 addr = 0;
6867
6868 struct in_ifaddr *ifa;
6869
6870 rcu_read_lock();
6871 in_dev_for_each_ifa_rcu(ifa, idev) {
6872 if (ifa->ifa_flags & IFA_F_SECONDARY)
6873 continue;
6874
6875 addr = ifa->ifa_address;
6876 break;
6877 }
6878 rcu_read_unlock();
6879 return addr;
6880}
6881
6882static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09006883 struct smb2_ioctl_req *req,
6884 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09006885{
6886 struct network_interface_info_ioctl_rsp *nii_rsp = NULL;
6887 int nbytes = 0;
6888 struct net_device *netdev;
6889 struct sockaddr_storage_rsp *sockaddr_storage;
6890 unsigned int flags;
6891 unsigned long long speed;
6892
6893 rtnl_lock();
6894 for_each_netdev(&init_net, netdev) {
6895 if (unlikely(!netdev)) {
6896 rtnl_unlock();
6897 return -EINVAL;
6898 }
6899
6900 if (netdev->type == ARPHRD_LOOPBACK)
6901 continue;
6902
6903 flags = dev_get_flags(netdev);
6904 if (!(flags & IFF_RUNNING))
6905 continue;
6906
6907 nii_rsp = (struct network_interface_info_ioctl_rsp *)
6908 &rsp->Buffer[nbytes];
6909 nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex);
6910
6911 /* TODO: specify the RDMA capabilities */
6912 if (netdev->num_tx_queues > 1)
6913 nii_rsp->Capability = cpu_to_le32(RSS_CAPABLE);
6914 else
6915 nii_rsp->Capability = 0;
6916
6917 nii_rsp->Next = cpu_to_le32(152);
6918 nii_rsp->Reserved = 0;
6919
6920 if (netdev->ethtool_ops->get_link_ksettings) {
6921 struct ethtool_link_ksettings cmd;
6922
6923 netdev->ethtool_ops->get_link_ksettings(netdev, &cmd);
6924 speed = cmd.base.speed;
6925 } else {
Namjae Jeon64b39f42021-03-30 14:25:35 +09006926 ksmbd_err("%s %s\n", netdev->name,
Namjae Jeon070fb212021-05-26 17:57:12 +09006927 "speed is unknown, defaulting to 1Gb/sec");
Namjae Jeone2f34482021-03-16 10:49:09 +09006928 speed = SPEED_1000;
6929 }
6930
6931 speed *= 1000000;
6932 nii_rsp->LinkSpeed = cpu_to_le64(speed);
6933
6934 sockaddr_storage = (struct sockaddr_storage_rsp *)
6935 nii_rsp->SockAddr_Storage;
6936 memset(sockaddr_storage, 0, 128);
6937
6938 if (conn->peer_addr.ss_family == PF_INET) {
6939 struct in_device *idev;
6940
6941 sockaddr_storage->Family = cpu_to_le16(INTERNETWORK);
6942 sockaddr_storage->addr4.Port = 0;
6943
6944 idev = __in_dev_get_rtnl(netdev);
6945 if (!idev)
6946 continue;
6947 sockaddr_storage->addr4.IPv4address =
6948 idev_ipv4_address(idev);
6949 } else {
6950 struct inet6_dev *idev6;
6951 struct inet6_ifaddr *ifa;
6952 __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address;
6953
6954 sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6);
6955 sockaddr_storage->addr6.Port = 0;
6956 sockaddr_storage->addr6.FlowInfo = 0;
6957
6958 idev6 = __in6_dev_get(netdev);
6959 if (!idev6)
6960 continue;
6961
6962 list_for_each_entry(ifa, &idev6->addr_list, if_list) {
6963 if (ifa->flags & (IFA_F_TENTATIVE |
6964 IFA_F_DEPRECATED))
6965 continue;
6966 memcpy(ipv6_addr, ifa->addr.s6_addr, 16);
6967 break;
6968 }
6969 sockaddr_storage->addr6.ScopeId = 0;
6970 }
6971
6972 nbytes += sizeof(struct network_interface_info_ioctl_rsp);
6973 }
6974 rtnl_unlock();
6975
6976 /* zero if this is last one */
6977 if (nii_rsp)
6978 nii_rsp->Next = 0;
6979
6980 if (!nbytes) {
6981 rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL;
6982 return -EINVAL;
6983 }
6984
6985 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
6986 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
6987 return nbytes;
6988}
6989
Namjae Jeone2f34482021-03-16 10:49:09 +09006990static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn,
Namjae Jeon070fb212021-05-26 17:57:12 +09006991 struct validate_negotiate_info_req *neg_req,
6992 struct validate_negotiate_info_rsp *neg_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09006993{
6994 int ret = 0;
6995 int dialect;
6996
6997 dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects,
Namjae Jeon070fb212021-05-26 17:57:12 +09006998 neg_req->DialectCount);
Namjae Jeone2f34482021-03-16 10:49:09 +09006999 if (dialect == BAD_PROT_ID || dialect != conn->dialect) {
7000 ret = -EINVAL;
7001 goto err_out;
7002 }
7003
7004 if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) {
7005 ret = -EINVAL;
7006 goto err_out;
7007 }
7008
7009 if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) {
7010 ret = -EINVAL;
7011 goto err_out;
7012 }
7013
7014 if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) {
7015 ret = -EINVAL;
7016 goto err_out;
7017 }
7018
7019 neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities);
7020 memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE);
7021 neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode);
7022 neg_rsp->Dialect = cpu_to_le16(conn->dialect);
7023err_out:
7024 return ret;
7025}
7026
Namjae Jeon64b39f42021-03-30 14:25:35 +09007027static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007028 struct file_allocated_range_buffer *qar_req,
7029 struct file_allocated_range_buffer *qar_rsp,
7030 int in_count, int *out_count)
Namjae Jeone2f34482021-03-16 10:49:09 +09007031{
7032 struct ksmbd_file *fp;
7033 loff_t start, length;
7034 int ret = 0;
7035
7036 *out_count = 0;
7037 if (in_count == 0)
7038 return -EINVAL;
7039
7040 fp = ksmbd_lookup_fd_fast(work, id);
7041 if (!fp)
7042 return -ENOENT;
7043
7044 start = le64_to_cpu(qar_req->file_offset);
7045 length = le64_to_cpu(qar_req->length);
7046
7047 ret = ksmbd_vfs_fqar_lseek(fp, start, length,
Namjae Jeon070fb212021-05-26 17:57:12 +09007048 qar_rsp, in_count, out_count);
Namjae Jeone2f34482021-03-16 10:49:09 +09007049 if (ret && ret != -E2BIG)
7050 *out_count = 0;
7051
7052 ksmbd_fd_put(work, fp);
7053 return ret;
7054}
7055
Namjae Jeon64b39f42021-03-30 14:25:35 +09007056static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007057 int out_buf_len, struct smb2_ioctl_req *req,
7058 struct smb2_ioctl_rsp *rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007059{
7060 struct ksmbd_rpc_command *rpc_resp;
7061 char *data_buf = (char *)&req->Buffer[0];
7062 int nbytes = 0;
7063
Namjae Jeon64b39f42021-03-30 14:25:35 +09007064 rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09007065 le32_to_cpu(req->InputCount));
Namjae Jeone2f34482021-03-16 10:49:09 +09007066 if (rpc_resp) {
7067 if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) {
7068 /*
7069 * set STATUS_SOME_NOT_MAPPED response
7070 * for unknown domain sid.
7071 */
7072 rsp->hdr.Status = STATUS_SOME_NOT_MAPPED;
7073 } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
7074 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7075 goto out;
7076 } else if (rpc_resp->flags != KSMBD_RPC_OK) {
7077 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7078 goto out;
7079 }
7080
7081 nbytes = rpc_resp->payload_sz;
7082 if (rpc_resp->payload_sz > out_buf_len) {
7083 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7084 nbytes = out_buf_len;
7085 }
7086
7087 if (!rpc_resp->payload_sz) {
7088 rsp->hdr.Status =
7089 STATUS_UNEXPECTED_IO_ERROR;
7090 goto out;
7091 }
7092
7093 memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes);
7094 }
7095out:
Namjae Jeon79f6b112021-04-02 12:47:14 +09007096 kvfree(rpc_resp);
Namjae Jeone2f34482021-03-16 10:49:09 +09007097 return nbytes;
7098}
7099
Namjae Jeon64b39f42021-03-30 14:25:35 +09007100static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007101 struct file_sparse *sparse)
Namjae Jeone2f34482021-03-16 10:49:09 +09007102{
7103 struct ksmbd_file *fp;
7104 int ret = 0;
7105 __le32 old_fattr;
7106
7107 fp = ksmbd_lookup_fd_fast(work, id);
7108 if (!fp)
7109 return -ENOENT;
7110
7111 old_fattr = fp->f_ci->m_fattr;
7112 if (sparse->SetSparse)
7113 fp->f_ci->m_fattr |= ATTR_SPARSE_FILE_LE;
7114 else
7115 fp->f_ci->m_fattr &= ~ATTR_SPARSE_FILE_LE;
7116
7117 if (fp->f_ci->m_fattr != old_fattr &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007118 test_share_config_flag(work->tcon->share_conf,
7119 KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007120 struct xattr_dos_attrib da;
7121
7122 ret = ksmbd_vfs_get_dos_attrib_xattr(fp->filp->f_path.dentry, &da);
7123 if (ret <= 0)
7124 goto out;
7125
7126 da.attr = le32_to_cpu(fp->f_ci->m_fattr);
7127 ret = ksmbd_vfs_set_dos_attrib_xattr(fp->filp->f_path.dentry, &da);
7128 if (ret)
7129 fp->f_ci->m_fattr = old_fattr;
7130 }
7131
7132out:
7133 ksmbd_fd_put(work, fp);
7134 return ret;
7135}
7136
7137static int fsctl_request_resume_key(struct ksmbd_work *work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007138 struct smb2_ioctl_req *req,
7139 struct resume_key_ioctl_rsp *key_rsp)
Namjae Jeone2f34482021-03-16 10:49:09 +09007140{
7141 struct ksmbd_file *fp;
7142
7143 fp = ksmbd_lookup_fd_slow(work,
Namjae Jeon070fb212021-05-26 17:57:12 +09007144 le64_to_cpu(req->VolatileFileId),
7145 le64_to_cpu(req->PersistentFileId));
Namjae Jeone2f34482021-03-16 10:49:09 +09007146 if (!fp)
7147 return -ENOENT;
7148
7149 memset(key_rsp, 0, sizeof(*key_rsp));
7150 key_rsp->ResumeKey[0] = req->VolatileFileId;
7151 key_rsp->ResumeKey[1] = req->PersistentFileId;
7152 ksmbd_fd_put(work, fp);
7153
7154 return 0;
7155}
7156
7157/**
7158 * smb2_ioctl() - handler for smb2 ioctl command
7159 * @work: smb work containing ioctl command buffer
7160 *
7161 * Return: 0 on success, otherwise error
7162 */
7163int smb2_ioctl(struct ksmbd_work *work)
7164{
7165 struct smb2_ioctl_req *req;
7166 struct smb2_ioctl_rsp *rsp, *rsp_org;
7167 int cnt_code, nbytes = 0;
7168 int out_buf_len;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007169 u64 id = KSMBD_NO_FID;
Namjae Jeone2f34482021-03-16 10:49:09 +09007170 struct ksmbd_conn *conn = work->conn;
7171 int ret = 0;
7172
Namjae Jeone5066492021-03-30 12:35:23 +09007173 rsp_org = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007174 if (work->next_smb2_rcv_hdr_off) {
7175 req = REQUEST_BUF_NEXT(work);
7176 rsp = RESPONSE_BUF_NEXT(work);
7177 if (!HAS_FILE_ID(le64_to_cpu(req->VolatileFileId))) {
7178 ksmbd_debug(SMB, "Compound request set FID = %u\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007179 work->compound_fid);
Namjae Jeone2f34482021-03-16 10:49:09 +09007180 id = work->compound_fid;
7181 }
7182 } else {
Namjae Jeone5066492021-03-30 12:35:23 +09007183 req = work->request_buf;
7184 rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007185 }
7186
7187 if (!HAS_FILE_ID(id))
7188 id = le64_to_cpu(req->VolatileFileId);
7189
7190 if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) {
7191 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7192 goto out;
7193 }
7194
7195 cnt_code = le32_to_cpu(req->CntCode);
7196 out_buf_len = le32_to_cpu(req->MaxOutputResponse);
7197 out_buf_len = min(KSMBD_IPC_MAX_PAYLOAD, out_buf_len);
7198
7199 switch (cnt_code) {
7200 case FSCTL_DFS_GET_REFERRALS:
7201 case FSCTL_DFS_GET_REFERRALS_EX:
7202 /* Not support DFS yet */
7203 rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED;
7204 goto out;
7205 case FSCTL_CREATE_OR_GET_OBJECT_ID:
7206 {
7207 struct file_object_buf_type1_ioctl_rsp *obj_buf;
7208
7209 nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp);
7210 obj_buf = (struct file_object_buf_type1_ioctl_rsp *)
7211 &rsp->Buffer[0];
7212
7213 /*
7214 * TODO: This is dummy implementation to pass smbtorture
7215 * Need to check correct response later
7216 */
7217 memset(obj_buf->ObjectId, 0x0, 16);
7218 memset(obj_buf->BirthVolumeId, 0x0, 16);
7219 memset(obj_buf->BirthObjectId, 0x0, 16);
7220 memset(obj_buf->DomainId, 0x0, 16);
7221
7222 break;
7223 }
7224 case FSCTL_PIPE_TRANSCEIVE:
7225 nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp);
7226 break;
7227 case FSCTL_VALIDATE_NEGOTIATE_INFO:
7228 if (conn->dialect < SMB30_PROT_ID) {
7229 ret = -EOPNOTSUPP;
7230 goto out;
7231 }
7232
7233 ret = fsctl_validate_negotiate_info(conn,
7234 (struct validate_negotiate_info_req *)&req->Buffer[0],
7235 (struct validate_negotiate_info_rsp *)&rsp->Buffer[0]);
7236 if (ret < 0)
7237 goto out;
7238
7239 nbytes = sizeof(struct validate_negotiate_info_rsp);
7240 rsp->PersistentFileId = cpu_to_le64(SMB2_NO_FID);
7241 rsp->VolatileFileId = cpu_to_le64(SMB2_NO_FID);
7242 break;
7243 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
7244 nbytes = fsctl_query_iface_info_ioctl(conn, req, rsp);
7245 if (nbytes < 0)
7246 goto out;
7247 break;
7248 case FSCTL_REQUEST_RESUME_KEY:
7249 if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) {
7250 ret = -EINVAL;
7251 goto out;
7252 }
7253
7254 ret = fsctl_request_resume_key(work, req,
Namjae Jeon070fb212021-05-26 17:57:12 +09007255 (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007256 if (ret < 0)
7257 goto out;
7258 rsp->PersistentFileId = req->PersistentFileId;
7259 rsp->VolatileFileId = req->VolatileFileId;
7260 nbytes = sizeof(struct resume_key_ioctl_rsp);
7261 break;
7262 case FSCTL_COPYCHUNK:
7263 case FSCTL_COPYCHUNK_WRITE:
Namjae Jeon64b39f42021-03-30 14:25:35 +09007264 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007265 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007266 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007267 ret = -EACCES;
7268 goto out;
7269 }
7270
7271 if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) {
7272 ret = -EINVAL;
7273 goto out;
7274 }
7275
7276 nbytes = sizeof(struct copychunk_ioctl_rsp);
7277 fsctl_copychunk(work, req, rsp);
7278 break;
7279 case FSCTL_SET_SPARSE:
7280 ret = fsctl_set_sparse(work, id,
Namjae Jeon070fb212021-05-26 17:57:12 +09007281 (struct file_sparse *)&req->Buffer[0]);
Namjae Jeone2f34482021-03-16 10:49:09 +09007282 if (ret < 0)
7283 goto out;
7284 break;
7285 case FSCTL_SET_ZERO_DATA:
7286 {
7287 struct file_zero_data_information *zero_data;
7288 struct ksmbd_file *fp;
7289 loff_t off, len;
7290
Namjae Jeon64b39f42021-03-30 14:25:35 +09007291 if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007292 ksmbd_debug(SMB,
Namjae Jeon070fb212021-05-26 17:57:12 +09007293 "User does not have write permission\n");
Namjae Jeone2f34482021-03-16 10:49:09 +09007294 ret = -EACCES;
7295 goto out;
7296 }
7297
7298 zero_data =
7299 (struct file_zero_data_information *)&req->Buffer[0];
7300
7301 fp = ksmbd_lookup_fd_fast(work, id);
7302 if (!fp) {
7303 ret = -ENOENT;
7304 goto out;
7305 }
7306
7307 off = le64_to_cpu(zero_data->FileOffset);
7308 len = le64_to_cpu(zero_data->BeyondFinalZero) - off;
7309
7310 ret = ksmbd_vfs_zero_data(work, fp, off, len);
7311 ksmbd_fd_put(work, fp);
7312 if (ret < 0)
7313 goto out;
7314 break;
7315 }
7316 case FSCTL_QUERY_ALLOCATED_RANGES:
7317 ret = fsctl_query_allocated_ranges(work, id,
7318 (struct file_allocated_range_buffer *)&req->Buffer[0],
7319 (struct file_allocated_range_buffer *)&rsp->Buffer[0],
7320 out_buf_len /
7321 sizeof(struct file_allocated_range_buffer), &nbytes);
7322 if (ret == -E2BIG) {
7323 rsp->hdr.Status = STATUS_BUFFER_OVERFLOW;
7324 } else if (ret < 0) {
7325 nbytes = 0;
7326 goto out;
7327 }
7328
7329 nbytes *= sizeof(struct file_allocated_range_buffer);
7330 break;
7331 case FSCTL_GET_REPARSE_POINT:
7332 {
7333 struct reparse_data_buffer *reparse_ptr;
7334 struct ksmbd_file *fp;
7335
7336 reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0];
7337 fp = ksmbd_lookup_fd_fast(work, id);
7338 if (!fp) {
7339 ksmbd_err("not found fp!!\n");
7340 ret = -ENOENT;
7341 goto out;
7342 }
7343
7344 reparse_ptr->ReparseTag =
7345 smb2_get_reparse_tag_special_file(FP_INODE(fp)->i_mode);
7346 reparse_ptr->ReparseDataLength = 0;
7347 ksmbd_fd_put(work, fp);
7348 nbytes = sizeof(struct reparse_data_buffer);
7349 break;
7350 }
Namjae Jeoneb817362021-05-18 10:37:59 +09007351 case FSCTL_DUPLICATE_EXTENTS_TO_FILE:
7352 {
7353 struct ksmbd_file *fp_in, *fp_out = NULL;
7354 struct duplicate_extents_to_file *dup_ext;
7355 loff_t src_off, dst_off, length, cloned;
7356
7357 dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0];
7358
7359 fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle,
Namjae Jeon070fb212021-05-26 17:57:12 +09007360 dup_ext->PersistentFileHandle);
Namjae Jeoneb817362021-05-18 10:37:59 +09007361 if (!fp_in) {
7362 ksmbd_err("not found file handle in duplicate extent to file\n");
7363 ret = -ENOENT;
7364 goto out;
7365 }
7366
7367 fp_out = ksmbd_lookup_fd_fast(work, id);
7368 if (!fp_out) {
7369 ksmbd_err("not found fp\n");
7370 ret = -ENOENT;
7371 goto dup_ext_out;
7372 }
7373
7374 src_off = le64_to_cpu(dup_ext->SourceFileOffset);
7375 dst_off = le64_to_cpu(dup_ext->TargetFileOffset);
7376 length = le64_to_cpu(dup_ext->ByteCount);
7377 cloned = vfs_clone_file_range(fp_in->filp, src_off, fp_out->filp,
Namjae Jeon070fb212021-05-26 17:57:12 +09007378 dst_off, length, 0);
Namjae Jeoneb817362021-05-18 10:37:59 +09007379 if (cloned == -EXDEV || cloned == -EOPNOTSUPP) {
7380 ret = -EOPNOTSUPP;
7381 goto dup_ext_out;
7382 } else if (cloned != length) {
7383 cloned = ksmbd_vfs_copy_file_range(fp_in->filp, src_off,
Namjae Jeon070fb212021-05-26 17:57:12 +09007384 fp_out->filp, dst_off, length);
Namjae Jeoneb817362021-05-18 10:37:59 +09007385 if (cloned != length) {
7386 if (cloned < 0)
7387 ret = cloned;
7388 else
7389 ret = -EINVAL;
7390 }
7391 }
7392
7393dup_ext_out:
7394 ksmbd_fd_put(work, fp_in);
7395 ksmbd_fd_put(work, fp_out);
7396 if (ret < 0)
7397 goto out;
7398 break;
7399 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007400 default:
7401 ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007402 cnt_code);
Namjae Jeone2f34482021-03-16 10:49:09 +09007403 ret = -EOPNOTSUPP;
7404 goto out;
7405 }
7406
7407 rsp->CntCode = cpu_to_le32(cnt_code);
7408 rsp->InputCount = cpu_to_le32(0);
7409 rsp->InputOffset = cpu_to_le32(112);
7410 rsp->OutputOffset = cpu_to_le32(112);
7411 rsp->OutputCount = cpu_to_le32(nbytes);
7412 rsp->StructureSize = cpu_to_le16(49);
7413 rsp->Reserved = cpu_to_le16(0);
7414 rsp->Flags = cpu_to_le32(0);
7415 rsp->Reserved2 = cpu_to_le32(0);
7416 inc_rfc1001_len(rsp_org, 48 + nbytes);
7417
7418 return 0;
7419
7420out:
7421 if (ret == -EACCES)
7422 rsp->hdr.Status = STATUS_ACCESS_DENIED;
7423 else if (ret == -ENOENT)
7424 rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND;
7425 else if (ret == -EOPNOTSUPP)
7426 rsp->hdr.Status = STATUS_NOT_SUPPORTED;
7427 else if (ret < 0 || rsp->hdr.Status == 0)
7428 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7429 smb2_set_err_rsp(work);
7430 return 0;
7431}
7432
7433/**
7434 * smb20_oplock_break_ack() - handler for smb2.0 oplock break command
7435 * @work: smb work containing oplock break command buffer
7436 *
7437 * Return: 0
7438 */
7439static void smb20_oplock_break_ack(struct ksmbd_work *work)
7440{
Namjae Jeone5066492021-03-30 12:35:23 +09007441 struct smb2_oplock_break *req = work->request_buf;
7442 struct smb2_oplock_break *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007443 struct ksmbd_file *fp;
7444 struct oplock_info *opinfo = NULL;
7445 __le32 err = 0;
7446 int ret = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007447 u64 volatile_id, persistent_id;
Namjae Jeone2f34482021-03-16 10:49:09 +09007448 char req_oplevel = 0, rsp_oplevel = 0;
7449 unsigned int oplock_change_type;
7450
7451 volatile_id = le64_to_cpu(req->VolatileFid);
7452 persistent_id = le64_to_cpu(req->PersistentFid);
7453 req_oplevel = req->OplockLevel;
7454 ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n",
7455 volatile_id, persistent_id, req_oplevel);
7456
7457 fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id);
7458 if (!fp) {
7459 rsp->hdr.Status = STATUS_FILE_CLOSED;
7460 smb2_set_err_rsp(work);
7461 return;
7462 }
7463
7464 opinfo = opinfo_get(fp);
7465 if (!opinfo) {
7466 ksmbd_err("unexpected null oplock_info\n");
7467 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7468 smb2_set_err_rsp(work);
7469 ksmbd_fd_put(work, fp);
7470 return;
7471 }
7472
7473 if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) {
7474 rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL;
7475 goto err_out;
7476 }
7477
7478 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7479 ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state);
7480 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7481 goto err_out;
7482 }
7483
Namjae Jeon64b39f42021-03-30 14:25:35 +09007484 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7485 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7486 (req_oplevel != SMB2_OPLOCK_LEVEL_II &&
7487 req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007488 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7489 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007490 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7491 req_oplevel != SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007492 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7493 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007494 } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II ||
7495 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007496 err = STATUS_INVALID_DEVICE_STATE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007497 if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7498 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7499 req_oplevel == SMB2_OPLOCK_LEVEL_II) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007500 oplock_change_type = OPLOCK_WRITE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007501 } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE ||
7502 opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) &&
7503 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007504 oplock_change_type = OPLOCK_WRITE_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007505 } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II &&
7506 req_oplevel == SMB2_OPLOCK_LEVEL_NONE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007507 oplock_change_type = OPLOCK_READ_TO_NONE;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007508 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007509 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007510 }
7511 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007512 oplock_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007513 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007514
7515 switch (oplock_change_type) {
7516 case OPLOCK_WRITE_TO_READ:
7517 ret = opinfo_write_to_read(opinfo);
7518 rsp_oplevel = SMB2_OPLOCK_LEVEL_II;
7519 break;
7520 case OPLOCK_WRITE_TO_NONE:
7521 ret = opinfo_write_to_none(opinfo);
7522 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7523 break;
7524 case OPLOCK_READ_TO_NONE:
7525 ret = opinfo_read_to_none(opinfo);
7526 rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE;
7527 break;
7528 default:
7529 ksmbd_err("unknown oplock change 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007530 opinfo->level, rsp_oplevel);
Namjae Jeone2f34482021-03-16 10:49:09 +09007531 }
7532
7533 if (ret < 0) {
7534 rsp->hdr.Status = err;
7535 goto err_out;
7536 }
7537
7538 opinfo_put(opinfo);
7539 ksmbd_fd_put(work, fp);
7540 opinfo->op_state = OPLOCK_STATE_NONE;
7541 wake_up_interruptible_all(&opinfo->oplock_q);
7542
7543 rsp->StructureSize = cpu_to_le16(24);
7544 rsp->OplockLevel = rsp_oplevel;
7545 rsp->Reserved = 0;
7546 rsp->Reserved2 = 0;
7547 rsp->VolatileFid = cpu_to_le64(volatile_id);
7548 rsp->PersistentFid = cpu_to_le64(persistent_id);
7549 inc_rfc1001_len(rsp, 24);
7550 return;
7551
7552err_out:
7553 opinfo->op_state = OPLOCK_STATE_NONE;
7554 wake_up_interruptible_all(&opinfo->oplock_q);
7555
7556 opinfo_put(opinfo);
7557 ksmbd_fd_put(work, fp);
7558 smb2_set_err_rsp(work);
7559}
7560
7561static int check_lease_state(struct lease *lease, __le32 req_state)
7562{
7563 if ((lease->new_state ==
Namjae Jeon64b39f42021-03-30 14:25:35 +09007564 (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) &&
7565 !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007566 lease->new_state = req_state;
7567 return 0;
7568 }
7569
7570 if (lease->new_state == req_state)
7571 return 0;
7572
7573 return 1;
7574}
7575
7576/**
7577 * smb21_lease_break_ack() - handler for smb2.1 lease break command
7578 * @work: smb work containing lease break command buffer
7579 *
7580 * Return: 0
7581 */
7582static void smb21_lease_break_ack(struct ksmbd_work *work)
7583{
7584 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09007585 struct smb2_lease_ack *req = work->request_buf;
7586 struct smb2_lease_ack *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007587 struct oplock_info *opinfo;
7588 __le32 err = 0;
7589 int ret = 0;
7590 unsigned int lease_change_type;
7591 __le32 lease_state;
7592 struct lease *lease;
7593
7594 ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007595 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007596 opinfo = lookup_lease_in_table(conn, req->LeaseKey);
7597 if (!opinfo) {
7598 ksmbd_debug(OPLOCK, "file not opened\n");
7599 smb2_set_err_rsp(work);
7600 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7601 return;
7602 }
7603 lease = opinfo->o_lease;
7604
7605 if (opinfo->op_state == OPLOCK_STATE_NONE) {
7606 ksmbd_err("unexpected lease break state 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007607 opinfo->op_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09007608 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7609 goto err_out;
7610 }
7611
7612 if (check_lease_state(lease, req->LeaseState)) {
7613 rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED;
7614 ksmbd_debug(OPLOCK,
Namjae Jeon070fb212021-05-26 17:57:12 +09007615 "req lease state: 0x%x, expected state: 0x%x\n",
7616 req->LeaseState, lease->new_state);
Namjae Jeone2f34482021-03-16 10:49:09 +09007617 goto err_out;
7618 }
7619
7620 if (!atomic_read(&opinfo->breaking_cnt)) {
7621 rsp->hdr.Status = STATUS_UNSUCCESSFUL;
7622 goto err_out;
7623 }
7624
7625 /* check for bad lease state */
Namjae Jeon070fb212021-05-26 17:57:12 +09007626 if (req->LeaseState &
7627 (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007628 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7629 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7630 lease_change_type = OPLOCK_WRITE_TO_NONE;
7631 else
7632 lease_change_type = OPLOCK_READ_TO_NONE;
7633 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007634 le32_to_cpu(lease->state),
7635 le32_to_cpu(req->LeaseState));
Namjae Jeon64b39f42021-03-30 14:25:35 +09007636 } else if (lease->state == SMB2_LEASE_READ_CACHING_LE &&
7637 req->LeaseState != SMB2_LEASE_NONE_LE) {
Namjae Jeone2f34482021-03-16 10:49:09 +09007638 err = STATUS_INVALID_OPLOCK_PROTOCOL;
7639 lease_change_type = OPLOCK_READ_TO_NONE;
7640 ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007641 le32_to_cpu(lease->state),
7642 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007643 } else {
7644 /* valid lease state changes */
7645 err = STATUS_INVALID_DEVICE_STATE;
7646 if (req->LeaseState == SMB2_LEASE_NONE_LE) {
7647 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7648 lease_change_type = OPLOCK_WRITE_TO_NONE;
7649 else
7650 lease_change_type = OPLOCK_READ_TO_NONE;
7651 } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) {
7652 if (lease->state & SMB2_LEASE_WRITE_CACHING_LE)
7653 lease_change_type = OPLOCK_WRITE_TO_READ;
7654 else
7655 lease_change_type = OPLOCK_READ_HANDLE_TO_READ;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007656 } else {
Namjae Jeone2f34482021-03-16 10:49:09 +09007657 lease_change_type = 0;
Namjae Jeon64b39f42021-03-30 14:25:35 +09007658 }
Namjae Jeone2f34482021-03-16 10:49:09 +09007659 }
7660
7661 switch (lease_change_type) {
7662 case OPLOCK_WRITE_TO_READ:
7663 ret = opinfo_write_to_read(opinfo);
7664 break;
7665 case OPLOCK_READ_HANDLE_TO_READ:
7666 ret = opinfo_read_handle_to_read(opinfo);
7667 break;
7668 case OPLOCK_WRITE_TO_NONE:
7669 ret = opinfo_write_to_none(opinfo);
7670 break;
7671 case OPLOCK_READ_TO_NONE:
7672 ret = opinfo_read_to_none(opinfo);
7673 break;
7674 default:
7675 ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007676 le32_to_cpu(lease->state),
7677 le32_to_cpu(req->LeaseState));
Namjae Jeone2f34482021-03-16 10:49:09 +09007678 }
7679
7680 lease_state = lease->state;
7681 opinfo->op_state = OPLOCK_STATE_NONE;
7682 wake_up_interruptible_all(&opinfo->oplock_q);
7683 atomic_dec(&opinfo->breaking_cnt);
7684 wake_up_interruptible_all(&opinfo->oplock_brk);
7685 opinfo_put(opinfo);
7686
7687 if (ret < 0) {
7688 rsp->hdr.Status = err;
7689 goto err_out;
7690 }
7691
7692 rsp->StructureSize = cpu_to_le16(36);
7693 rsp->Reserved = 0;
7694 rsp->Flags = 0;
7695 memcpy(rsp->LeaseKey, req->LeaseKey, 16);
7696 rsp->LeaseState = lease_state;
7697 rsp->LeaseDuration = 0;
7698 inc_rfc1001_len(rsp, 36);
7699 return;
7700
7701err_out:
7702 opinfo->op_state = OPLOCK_STATE_NONE;
7703 wake_up_interruptible_all(&opinfo->oplock_q);
7704 atomic_dec(&opinfo->breaking_cnt);
7705 wake_up_interruptible_all(&opinfo->oplock_brk);
7706
7707 opinfo_put(opinfo);
7708 smb2_set_err_rsp(work);
7709}
7710
7711/**
7712 * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break
7713 * @work: smb work containing oplock/lease break command buffer
7714 *
7715 * Return: 0
7716 */
7717int smb2_oplock_break(struct ksmbd_work *work)
7718{
Namjae Jeone5066492021-03-30 12:35:23 +09007719 struct smb2_oplock_break *req = work->request_buf;
7720 struct smb2_oplock_break *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007721
7722 switch (le16_to_cpu(req->StructureSize)) {
7723 case OP_BREAK_STRUCT_SIZE_20:
7724 smb20_oplock_break_ack(work);
7725 break;
7726 case OP_BREAK_STRUCT_SIZE_21:
7727 smb21_lease_break_ack(work);
7728 break;
7729 default:
7730 ksmbd_debug(OPLOCK, "invalid break cmd %d\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09007731 le16_to_cpu(req->StructureSize));
Namjae Jeone2f34482021-03-16 10:49:09 +09007732 rsp->hdr.Status = STATUS_INVALID_PARAMETER;
7733 smb2_set_err_rsp(work);
7734 }
7735
7736 return 0;
7737}
7738
7739/**
7740 * smb2_notify() - handler for smb2 notify request
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09007741 * @work: smb work containing notify command buffer
Namjae Jeone2f34482021-03-16 10:49:09 +09007742 *
7743 * Return: 0
7744 */
7745int smb2_notify(struct ksmbd_work *work)
7746{
7747 struct smb2_notify_req *req;
7748 struct smb2_notify_rsp *rsp;
7749
7750 WORK_BUFFERS(work, req, rsp);
7751
7752 if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) {
7753 rsp->hdr.Status = STATUS_INTERNAL_ERROR;
7754 smb2_set_err_rsp(work);
7755 return 0;
7756 }
7757
7758 smb2_set_err_rsp(work);
7759 rsp->hdr.Status = STATUS_NOT_IMPLEMENTED;
7760 return 0;
7761}
7762
7763/**
7764 * smb2_is_sign_req() - handler for checking packet signing status
Hyunchul Lee95fa1ce2021-03-21 17:05:56 +09007765 * @work: smb work containing notify command buffer
7766 * @command: SMB2 command id
Namjae Jeone2f34482021-03-16 10:49:09 +09007767 *
7768 * Return: true if packed is signed, false otherwise
7769 */
7770bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command)
7771{
Namjae Jeone5066492021-03-30 12:35:23 +09007772 struct smb2_hdr *rcv_hdr2 = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007773
7774 if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09007775 command != SMB2_NEGOTIATE_HE &&
7776 command != SMB2_SESSION_SETUP_HE &&
7777 command != SMB2_OPLOCK_BREAK_HE)
Namjae Jeone2f34482021-03-16 10:49:09 +09007778 return true;
7779
kernel test robot56160152021-05-12 09:24:37 +09007780 return false;
Namjae Jeone2f34482021-03-16 10:49:09 +09007781}
7782
7783/**
7784 * smb2_check_sign_req() - handler for req packet sign processing
7785 * @work: smb work containing notify command buffer
7786 *
7787 * Return: 1 on success, 0 otherwise
7788 */
7789int smb2_check_sign_req(struct ksmbd_work *work)
7790{
7791 struct smb2_hdr *hdr, *hdr_org;
7792 char signature_req[SMB2_SIGNATURE_SIZE];
7793 char signature[SMB2_HMACSHA256_SIZE];
7794 struct kvec iov[1];
7795 size_t len;
7796
Namjae Jeone5066492021-03-30 12:35:23 +09007797 hdr_org = hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007798 if (work->next_smb2_rcv_hdr_off)
7799 hdr = REQUEST_BUF_NEXT(work);
7800
7801 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
7802 len = be32_to_cpu(hdr_org->smb2_buf_length);
7803 else if (hdr->NextCommand)
7804 len = le32_to_cpu(hdr->NextCommand);
7805 else
7806 len = be32_to_cpu(hdr_org->smb2_buf_length) -
7807 work->next_smb2_rcv_hdr_off;
7808
7809 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
7810 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7811
7812 iov[0].iov_base = (char *)&hdr->ProtocolId;
7813 iov[0].iov_len = len;
7814
7815 if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1,
Namjae Jeon64b39f42021-03-30 14:25:35 +09007816 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09007817 return 0;
7818
7819 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
7820 ksmbd_err("bad smb2 signature\n");
7821 return 0;
7822 }
7823
7824 return 1;
7825}
7826
7827/**
7828 * smb2_set_sign_rsp() - handler for rsp packet sign processing
7829 * @work: smb work containing notify command buffer
7830 *
7831 */
7832void smb2_set_sign_rsp(struct ksmbd_work *work)
7833{
7834 struct smb2_hdr *hdr, *hdr_org;
7835 struct smb2_hdr *req_hdr;
7836 char signature[SMB2_HMACSHA256_SIZE];
7837 struct kvec iov[2];
7838 size_t len;
7839 int n_vec = 1;
7840
Namjae Jeone5066492021-03-30 12:35:23 +09007841 hdr_org = hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007842 if (work->next_smb2_rsp_hdr_off)
7843 hdr = RESPONSE_BUF_NEXT(work);
7844
7845 req_hdr = REQUEST_BUF_NEXT(work);
7846
7847 if (!work->next_smb2_rsp_hdr_off) {
7848 len = get_rfc1002_len(hdr_org);
7849 if (req_hdr->NextCommand)
7850 len = ALIGN(len, 8);
7851 } else {
7852 len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off;
7853 len = ALIGN(len, 8);
7854 }
7855
7856 if (req_hdr->NextCommand)
7857 hdr->NextCommand = cpu_to_le32(len);
7858
7859 hdr->Flags |= SMB2_FLAGS_SIGNED;
7860 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7861
7862 iov[0].iov_base = (char *)&hdr->ProtocolId;
7863 iov[0].iov_len = len;
7864
Namjae Jeone5066492021-03-30 12:35:23 +09007865 if (work->aux_payload_sz) {
7866 iov[0].iov_len -= work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09007867
Namjae Jeone5066492021-03-30 12:35:23 +09007868 iov[1].iov_base = work->aux_payload_buf;
7869 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09007870 n_vec++;
7871 }
7872
7873 if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec,
Namjae Jeon64b39f42021-03-30 14:25:35 +09007874 signature))
Namjae Jeone2f34482021-03-16 10:49:09 +09007875 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
7876}
7877
7878/**
7879 * smb3_check_sign_req() - handler for req packet sign processing
7880 * @work: smb work containing notify command buffer
7881 *
7882 * Return: 1 on success, 0 otherwise
7883 */
7884int smb3_check_sign_req(struct ksmbd_work *work)
7885{
7886 struct ksmbd_conn *conn;
7887 char *signing_key;
7888 struct smb2_hdr *hdr, *hdr_org;
7889 struct channel *chann;
7890 char signature_req[SMB2_SIGNATURE_SIZE];
7891 char signature[SMB2_CMACAES_SIZE];
7892 struct kvec iov[1];
7893 size_t len;
7894
Namjae Jeone5066492021-03-30 12:35:23 +09007895 hdr_org = hdr = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007896 if (work->next_smb2_rcv_hdr_off)
7897 hdr = REQUEST_BUF_NEXT(work);
7898
7899 if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off)
7900 len = be32_to_cpu(hdr_org->smb2_buf_length);
7901 else if (hdr->NextCommand)
7902 len = le32_to_cpu(hdr->NextCommand);
7903 else
7904 len = be32_to_cpu(hdr_org->smb2_buf_length) -
7905 work->next_smb2_rcv_hdr_off;
7906
7907 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
7908 signing_key = work->sess->smb3signingkey;
7909 conn = work->sess->conn;
7910 } else {
7911 chann = lookup_chann_list(work->sess);
7912 if (!chann)
7913 return 0;
7914 signing_key = chann->smb3signingkey;
7915 conn = chann->conn;
7916 }
7917
7918 if (!signing_key) {
7919 ksmbd_err("SMB3 signing key is not generated\n");
7920 return 0;
7921 }
7922
7923 memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE);
7924 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7925 iov[0].iov_base = (char *)&hdr->ProtocolId;
7926 iov[0].iov_len = len;
7927
7928 if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature))
7929 return 0;
7930
7931 if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) {
7932 ksmbd_err("bad smb2 signature\n");
7933 return 0;
7934 }
7935
7936 return 1;
7937}
7938
7939/**
7940 * smb3_set_sign_rsp() - handler for rsp packet sign processing
7941 * @work: smb work containing notify command buffer
7942 *
7943 */
7944void smb3_set_sign_rsp(struct ksmbd_work *work)
7945{
7946 struct ksmbd_conn *conn;
7947 struct smb2_hdr *req_hdr;
7948 struct smb2_hdr *hdr, *hdr_org;
7949 struct channel *chann;
7950 char signature[SMB2_CMACAES_SIZE];
7951 struct kvec iov[2];
7952 int n_vec = 1;
7953 size_t len;
7954 char *signing_key;
7955
Namjae Jeone5066492021-03-30 12:35:23 +09007956 hdr_org = hdr = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09007957 if (work->next_smb2_rsp_hdr_off)
7958 hdr = RESPONSE_BUF_NEXT(work);
7959
7960 req_hdr = REQUEST_BUF_NEXT(work);
7961
7962 if (!work->next_smb2_rsp_hdr_off) {
7963 len = get_rfc1002_len(hdr_org);
7964 if (req_hdr->NextCommand)
7965 len = ALIGN(len, 8);
7966 } else {
7967 len = get_rfc1002_len(hdr_org) - work->next_smb2_rsp_hdr_off;
7968 len = ALIGN(len, 8);
7969 }
7970
7971 if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) {
7972 signing_key = work->sess->smb3signingkey;
7973 conn = work->sess->conn;
7974 } else {
7975 chann = lookup_chann_list(work->sess);
7976 if (!chann)
7977 return;
7978 signing_key = chann->smb3signingkey;
7979 conn = chann->conn;
7980 }
7981
7982 if (!signing_key)
7983 return;
7984
7985 if (req_hdr->NextCommand)
7986 hdr->NextCommand = cpu_to_le32(len);
7987
7988 hdr->Flags |= SMB2_FLAGS_SIGNED;
7989 memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE);
7990 iov[0].iov_base = (char *)&hdr->ProtocolId;
7991 iov[0].iov_len = len;
Namjae Jeone5066492021-03-30 12:35:23 +09007992 if (work->aux_payload_sz) {
7993 iov[0].iov_len -= work->aux_payload_sz;
7994 iov[1].iov_base = work->aux_payload_buf;
7995 iov[1].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09007996 n_vec++;
7997 }
7998
7999 if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, signature))
8000 memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE);
8001}
8002
8003/**
8004 * smb3_preauth_hash_rsp() - handler for computing preauth hash on response
8005 * @work: smb work containing response buffer
8006 *
8007 */
8008void smb3_preauth_hash_rsp(struct ksmbd_work *work)
8009{
8010 struct ksmbd_conn *conn = work->conn;
8011 struct ksmbd_session *sess = work->sess;
8012 struct smb2_hdr *req, *rsp;
8013
8014 if (conn->dialect != SMB311_PROT_ID)
8015 return;
8016
8017 WORK_BUFFERS(work, req, rsp);
8018
8019 if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE)
8020 ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09008021 conn->preauth_info->Preauth_HashValue);
Namjae Jeone2f34482021-03-16 10:49:09 +09008022
8023 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008024 sess && sess->state == SMB2_SESSION_IN_PROGRESS) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008025 __u8 *hash_value;
8026
8027 hash_value = sess->Preauth_HashValue;
8028 ksmbd_gen_preauth_integrity_hash(conn, (char *)rsp,
Namjae Jeon070fb212021-05-26 17:57:12 +09008029 hash_value);
Namjae Jeone2f34482021-03-16 10:49:09 +09008030 }
8031}
8032
Namjae Jeon64b39f42021-03-30 14:25:35 +09008033static void fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, char *old_buf,
Namjae Jeon070fb212021-05-26 17:57:12 +09008034 __le16 cipher_type)
Namjae Jeone2f34482021-03-16 10:49:09 +09008035{
8036 struct smb2_hdr *hdr = (struct smb2_hdr *)old_buf;
8037 unsigned int orig_len = get_rfc1002_len(old_buf);
8038
8039 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
8040 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
8041 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
8042 tr_hdr->Flags = cpu_to_le16(0x01);
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008043 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM ||
8044 cipher_type == SMB2_ENCRYPTION_AES256_GCM)
8045 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008046 else
Namjae Jeon5a0ca772021-05-06 11:43:37 +09008047 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Namjae Jeone2f34482021-03-16 10:49:09 +09008048 memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8);
8049 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
8050 inc_rfc1001_len(tr_hdr, orig_len);
8051}
8052
8053int smb3_encrypt_resp(struct ksmbd_work *work)
8054{
Namjae Jeone5066492021-03-30 12:35:23 +09008055 char *buf = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008056 struct smb2_transform_hdr *tr_hdr;
8057 struct kvec iov[3];
8058 int rc = -ENOMEM;
Namjae Jeone5066492021-03-30 12:35:23 +09008059 int buf_size = 0, rq_nvec = 2 + (work->aux_payload_sz ? 1 : 0);
Namjae Jeone2f34482021-03-16 10:49:09 +09008060
8061 if (ARRAY_SIZE(iov) < rq_nvec)
8062 return -ENOMEM;
8063
Namjae Jeon20ea7fd2021-03-30 12:40:47 +09008064 tr_hdr = kzalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
Namjae Jeone2f34482021-03-16 10:49:09 +09008065 if (!tr_hdr)
8066 return rc;
8067
8068 /* fill transform header */
8069 fill_transform_hdr(tr_hdr, buf, work->conn->cipher_type);
8070
8071 iov[0].iov_base = tr_hdr;
8072 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8073 buf_size += iov[0].iov_len - 4;
8074
8075 iov[1].iov_base = buf + 4;
8076 iov[1].iov_len = get_rfc1002_len(buf);
Namjae Jeone5066492021-03-30 12:35:23 +09008077 if (work->aux_payload_sz) {
8078 iov[1].iov_len = work->resp_hdr_sz - 4;
Namjae Jeone2f34482021-03-16 10:49:09 +09008079
Namjae Jeone5066492021-03-30 12:35:23 +09008080 iov[2].iov_base = work->aux_payload_buf;
8081 iov[2].iov_len = work->aux_payload_sz;
Namjae Jeone2f34482021-03-16 10:49:09 +09008082 buf_size += iov[2].iov_len;
8083 }
8084 buf_size += iov[1].iov_len;
8085 work->resp_hdr_sz = iov[1].iov_len;
8086
8087 rc = ksmbd_crypt_message(work->conn, iov, rq_nvec, 1);
8088 if (rc)
8089 return rc;
8090
8091 memmove(buf, iov[1].iov_base, iov[1].iov_len);
8092 tr_hdr->smb2_buf_length = cpu_to_be32(buf_size);
8093 work->tr_buf = tr_hdr;
8094
8095 return rc;
8096}
8097
8098int smb3_is_transform_hdr(void *buf)
8099{
8100 struct smb2_transform_hdr *trhdr = buf;
8101
8102 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
8103}
8104
8105int smb3_decrypt_req(struct ksmbd_work *work)
8106{
8107 struct ksmbd_conn *conn = work->conn;
8108 struct ksmbd_session *sess;
Namjae Jeone5066492021-03-30 12:35:23 +09008109 char *buf = work->request_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008110 struct smb2_hdr *hdr;
8111 unsigned int pdu_length = get_rfc1002_len(buf);
8112 struct kvec iov[2];
8113 unsigned int buf_data_size = pdu_length + 4 -
8114 sizeof(struct smb2_transform_hdr);
8115 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
8116 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
8117 int rc = 0;
8118
8119 sess = ksmbd_session_lookup(conn, le64_to_cpu(tr_hdr->SessionId));
8120 if (!sess) {
8121 ksmbd_err("invalid session id(%llx) in transform header\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008122 le64_to_cpu(tr_hdr->SessionId));
Namjae Jeone2f34482021-03-16 10:49:09 +09008123 return -ECONNABORTED;
8124 }
8125
Namjae Jeon070fb212021-05-26 17:57:12 +09008126 if (pdu_length + 4 <
8127 sizeof(struct smb2_transform_hdr) + sizeof(struct smb2_hdr)) {
Namjae Jeone2f34482021-03-16 10:49:09 +09008128 ksmbd_err("Transform message is too small (%u)\n",
Namjae Jeon070fb212021-05-26 17:57:12 +09008129 pdu_length);
Namjae Jeone2f34482021-03-16 10:49:09 +09008130 return -ECONNABORTED;
8131 }
8132
8133 if (pdu_length + 4 < orig_len + sizeof(struct smb2_transform_hdr)) {
8134 ksmbd_err("Transform message is broken\n");
8135 return -ECONNABORTED;
8136 }
8137
8138 iov[0].iov_base = buf;
8139 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
8140 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
8141 iov[1].iov_len = buf_data_size;
8142 rc = ksmbd_crypt_message(conn, iov, 2, 0);
8143 if (rc)
8144 return rc;
8145
8146 memmove(buf + 4, iov[1].iov_base, buf_data_size);
8147 hdr = (struct smb2_hdr *)buf;
8148 hdr->smb2_buf_length = cpu_to_be32(buf_data_size);
8149
8150 return rc;
8151}
8152
8153bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work)
8154{
8155 struct ksmbd_conn *conn = work->conn;
Namjae Jeone5066492021-03-30 12:35:23 +09008156 struct smb2_hdr *rsp = work->response_buf;
Namjae Jeone2f34482021-03-16 10:49:09 +09008157
8158 if (conn->dialect < SMB30_PROT_ID)
8159 return false;
8160
8161 if (work->next_smb2_rcv_hdr_off)
8162 rsp = RESPONSE_BUF_NEXT(work);
8163
8164 if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE &&
Namjae Jeon64b39f42021-03-30 14:25:35 +09008165 rsp->Status == STATUS_SUCCESS)
Namjae Jeone2f34482021-03-16 10:49:09 +09008166 return true;
8167 return false;
8168}