]>
Commit | Line | Data |
---|---|---|
e2f34481 NJ |
1 | // 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> | |
e8c06191 | 13 | #include <linux/falloc.h> |
02655a70 | 14 | #include <linux/mount.h> |
5970e15d | 15 | #include <linux/filelock.h> |
e2f34481 NJ |
16 | |
17 | #include "glob.h" | |
e2f34481 NJ |
18 | #include "smbfsctl.h" |
19 | #include "oplock.h" | |
20 | #include "smbacl.h" | |
21 | ||
22 | #include "auth.h" | |
23 | #include "asn1.h" | |
e2f34481 NJ |
24 | #include "connection.h" |
25 | #include "transport_ipc.h" | |
03d8d4f1 | 26 | #include "transport_rdma.h" |
e2f34481 NJ |
27 | #include "vfs.h" |
28 | #include "vfs_cache.h" | |
29 | #include "misc.h" | |
30 | ||
e2f34481 NJ |
31 | #include "server.h" |
32 | #include "smb_common.h" | |
33 | #include "smbstatus.h" | |
34 | #include "ksmbd_work.h" | |
35 | #include "mgmt/user_config.h" | |
36 | #include "mgmt/share_config.h" | |
37 | #include "mgmt/tree_connect.h" | |
38 | #include "mgmt/user_session.h" | |
39 | #include "mgmt/ksmbd_ida.h" | |
40 | #include "ndr.h" | |
41 | ||
42 | static void __wbuf(struct ksmbd_work *work, void **req, void **rsp) | |
43 | { | |
44 | if (work->next_smb2_rcv_hdr_off) { | |
8a893315 NJ |
45 | *req = ksmbd_req_buf_next(work); |
46 | *rsp = ksmbd_resp_buf_next(work); | |
e2f34481 | 47 | } else { |
cb451720 NJ |
48 | *req = smb2_get_msg(work->request_buf); |
49 | *rsp = smb2_get_msg(work->response_buf); | |
e2f34481 NJ |
50 | } |
51 | } | |
52 | ||
53 | #define WORK_BUFFERS(w, rq, rs) __wbuf((w), (void **)&(rq), (void **)&(rs)) | |
54 | ||
55 | /** | |
56 | * check_session_id() - check for valid session id in smb header | |
57 | * @conn: connection instance | |
58 | * @id: session id from smb header | |
59 | * | |
60 | * Return: 1 if valid session id, otherwise 0 | |
61 | */ | |
f4228b67 | 62 | static inline bool check_session_id(struct ksmbd_conn *conn, u64 id) |
e2f34481 NJ |
63 | { |
64 | struct ksmbd_session *sess; | |
65 | ||
66 | if (id == 0 || id == -1) | |
f4228b67 | 67 | return false; |
e2f34481 | 68 | |
f5a544e3 | 69 | sess = ksmbd_session_lookup_all(conn, id); |
e2f34481 | 70 | if (sess) |
f4228b67 | 71 | return true; |
bde1694a | 72 | pr_err("Invalid user session id: %llu\n", id); |
f4228b67 | 73 | return false; |
e2f34481 NJ |
74 | } |
75 | ||
f5a544e3 | 76 | struct channel *lookup_chann_list(struct ksmbd_session *sess, struct ksmbd_conn *conn) |
e2f34481 | 77 | { |
1d9c4172 | 78 | return xa_load(&sess->ksmbd_chann_list, (long)conn); |
e2f34481 NJ |
79 | } |
80 | ||
81 | /** | |
5ec3df8e | 82 | * smb2_get_ksmbd_tcon() - get tree connection information using a tree id. |
95fa1ce9 | 83 | * @work: smb work |
e2f34481 | 84 | * |
5ec3df8e NJ |
85 | * Return: 0 if there is a tree connection matched or these are |
86 | * skipable commands, otherwise error | |
e2f34481 NJ |
87 | */ |
88 | int smb2_get_ksmbd_tcon(struct ksmbd_work *work) | |
89 | { | |
3df0411e | 90 | struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work); |
341b1601 | 91 | unsigned int cmd = le16_to_cpu(req_hdr->Command); |
3df0411e | 92 | unsigned int tree_id; |
e2f34481 | 93 | |
341b1601 RB |
94 | if (cmd == SMB2_TREE_CONNECT_HE || |
95 | cmd == SMB2_CANCEL_HE || | |
96 | cmd == SMB2_LOGOFF_HE) { | |
e2f34481 NJ |
97 | ksmbd_debug(SMB, "skip to check tree connect request\n"); |
98 | return 0; | |
99 | } | |
100 | ||
02b68b20 | 101 | if (xa_empty(&work->sess->tree_conns)) { |
e2f34481 | 102 | ksmbd_debug(SMB, "NO tree connected\n"); |
c6ce2b57 | 103 | return -ENOENT; |
e2f34481 NJ |
104 | } |
105 | ||
106 | tree_id = le32_to_cpu(req_hdr->Id.SyncId.TreeId); | |
5005bcb4 NJ |
107 | |
108 | /* | |
109 | * If request is not the first in Compound request, | |
110 | * Just validate tree id in header with work->tcon->id. | |
111 | */ | |
112 | if (work->next_smb2_rcv_hdr_off) { | |
113 | if (!work->tcon) { | |
114 | pr_err("The first operation in the compound does not have tcon\n"); | |
115 | return -EINVAL; | |
116 | } | |
3df0411e | 117 | if (tree_id != UINT_MAX && work->tcon->id != tree_id) { |
5005bcb4 NJ |
118 | pr_err("tree id(%u) is different with id(%u) in first operation\n", |
119 | tree_id, work->tcon->id); | |
120 | return -EINVAL; | |
121 | } | |
122 | return 1; | |
123 | } | |
124 | ||
e2f34481 NJ |
125 | work->tcon = ksmbd_tree_conn_lookup(work->sess, tree_id); |
126 | if (!work->tcon) { | |
bde1694a | 127 | pr_err("Invalid tid %d\n", tree_id); |
5005bcb4 | 128 | return -ENOENT; |
e2f34481 NJ |
129 | } |
130 | ||
131 | return 1; | |
132 | } | |
133 | ||
134 | /** | |
135 | * smb2_set_err_rsp() - set error response code on smb response | |
136 | * @work: smb work containing response buffer | |
137 | */ | |
138 | void smb2_set_err_rsp(struct ksmbd_work *work) | |
139 | { | |
140 | struct smb2_err_rsp *err_rsp; | |
141 | ||
142 | if (work->next_smb2_rcv_hdr_off) | |
8a893315 | 143 | err_rsp = ksmbd_resp_buf_next(work); |
e2f34481 | 144 | else |
cb451720 | 145 | err_rsp = smb2_get_msg(work->response_buf); |
e2f34481 NJ |
146 | |
147 | if (err_rsp->hdr.Status != STATUS_STOPPED_ON_SYMLINK) { | |
e2b76ab8 NJ |
148 | int err; |
149 | ||
e2f34481 NJ |
150 | err_rsp->StructureSize = SMB2_ERROR_STRUCTURE_SIZE2_LE; |
151 | err_rsp->ErrorContextCount = 0; | |
152 | err_rsp->Reserved = 0; | |
153 | err_rsp->ByteCount = 0; | |
154 | err_rsp->ErrorData[0] = 0; | |
e2b76ab8 | 155 | err = ksmbd_iov_pin_rsp(work, (void *)err_rsp, |
041bba44 NJ |
156 | __SMB2_HEADER_STRUCTURE_SIZE + |
157 | SMB2_ERROR_STRUCTURE_SIZE2); | |
e2b76ab8 NJ |
158 | if (err) |
159 | work->send_no_response = 1; | |
e2f34481 NJ |
160 | } |
161 | } | |
162 | ||
163 | /** | |
164 | * is_smb2_neg_cmd() - is it smb2 negotiation command | |
165 | * @work: smb work containing smb header | |
166 | * | |
f4228b67 | 167 | * Return: true if smb2 negotiation command, otherwise false |
e2f34481 | 168 | */ |
f4228b67 | 169 | bool is_smb2_neg_cmd(struct ksmbd_work *work) |
e2f34481 | 170 | { |
cb451720 | 171 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
e2f34481 NJ |
172 | |
173 | /* is it SMB2 header ? */ | |
174 | if (hdr->ProtocolId != SMB2_PROTO_NUMBER) | |
f4228b67 | 175 | return false; |
e2f34481 NJ |
176 | |
177 | /* make sure it is request not response message */ | |
178 | if (hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR) | |
f4228b67 | 179 | return false; |
e2f34481 NJ |
180 | |
181 | if (hdr->Command != SMB2_NEGOTIATE) | |
f4228b67 | 182 | return false; |
e2f34481 | 183 | |
f4228b67 | 184 | return true; |
e2f34481 NJ |
185 | } |
186 | ||
187 | /** | |
188 | * is_smb2_rsp() - is it smb2 response | |
189 | * @work: smb work containing smb response buffer | |
190 | * | |
f4228b67 | 191 | * Return: true if smb2 response, otherwise false |
e2f34481 | 192 | */ |
f4228b67 | 193 | bool is_smb2_rsp(struct ksmbd_work *work) |
e2f34481 | 194 | { |
cb451720 | 195 | struct smb2_hdr *hdr = smb2_get_msg(work->response_buf); |
e2f34481 NJ |
196 | |
197 | /* is it SMB2 header ? */ | |
198 | if (hdr->ProtocolId != SMB2_PROTO_NUMBER) | |
f4228b67 | 199 | return false; |
e2f34481 NJ |
200 | |
201 | /* make sure it is response not request message */ | |
202 | if (!(hdr->Flags & SMB2_FLAGS_SERVER_TO_REDIR)) | |
f4228b67 | 203 | return false; |
e2f34481 | 204 | |
f4228b67 | 205 | return true; |
e2f34481 NJ |
206 | } |
207 | ||
208 | /** | |
209 | * get_smb2_cmd_val() - get smb command code from smb header | |
210 | * @work: smb work containing smb request buffer | |
211 | * | |
212 | * Return: smb2 request command value | |
213 | */ | |
fc2d1b58 | 214 | u16 get_smb2_cmd_val(struct ksmbd_work *work) |
e2f34481 NJ |
215 | { |
216 | struct smb2_hdr *rcv_hdr; | |
217 | ||
218 | if (work->next_smb2_rcv_hdr_off) | |
8a893315 | 219 | rcv_hdr = ksmbd_req_buf_next(work); |
e2f34481 | 220 | else |
cb451720 | 221 | rcv_hdr = smb2_get_msg(work->request_buf); |
e2f34481 NJ |
222 | return le16_to_cpu(rcv_hdr->Command); |
223 | } | |
224 | ||
225 | /** | |
226 | * set_smb2_rsp_status() - set error response code on smb2 header | |
227 | * @work: smb work containing response buffer | |
95fa1ce9 | 228 | * @err: error response code |
e2f34481 NJ |
229 | */ |
230 | void set_smb2_rsp_status(struct ksmbd_work *work, __le32 err) | |
231 | { | |
232 | struct smb2_hdr *rsp_hdr; | |
233 | ||
be0f89d4 | 234 | rsp_hdr = smb2_get_msg(work->response_buf); |
e2f34481 | 235 | rsp_hdr->Status = err; |
be0f89d4 NJ |
236 | |
237 | work->iov_idx = 0; | |
238 | work->iov_cnt = 0; | |
239 | work->next_smb2_rcv_hdr_off = 0; | |
e2f34481 NJ |
240 | smb2_set_err_rsp(work); |
241 | } | |
242 | ||
243 | /** | |
244 | * init_smb2_neg_rsp() - initialize smb2 response for negotiate command | |
245 | * @work: smb work containing smb request buffer | |
246 | * | |
247 | * smb2 negotiate response is sent in reply of smb1 negotiate command for | |
248 | * dialect auto-negotiation. | |
249 | */ | |
250 | int init_smb2_neg_rsp(struct ksmbd_work *work) | |
251 | { | |
252 | struct smb2_hdr *rsp_hdr; | |
253 | struct smb2_negotiate_rsp *rsp; | |
254 | struct ksmbd_conn *conn = work->conn; | |
e2b76ab8 | 255 | int err; |
e2f34481 | 256 | |
cb451720 | 257 | rsp_hdr = smb2_get_msg(work->response_buf); |
e2f34481 | 258 | memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
e2f34481 NJ |
259 | rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; |
260 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; | |
261 | rsp_hdr->CreditRequest = cpu_to_le16(2); | |
262 | rsp_hdr->Command = SMB2_NEGOTIATE; | |
263 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); | |
264 | rsp_hdr->NextCommand = 0; | |
265 | rsp_hdr->MessageId = 0; | |
266 | rsp_hdr->Id.SyncId.ProcessId = 0; | |
267 | rsp_hdr->Id.SyncId.TreeId = 0; | |
268 | rsp_hdr->SessionId = 0; | |
269 | memset(rsp_hdr->Signature, 0, 16); | |
270 | ||
cb451720 | 271 | rsp = smb2_get_msg(work->response_buf); |
e2f34481 | 272 | |
f5c779b7 | 273 | WARN_ON(ksmbd_conn_good(conn)); |
e2f34481 NJ |
274 | |
275 | rsp->StructureSize = cpu_to_le16(65); | |
276 | ksmbd_debug(SMB, "conn->dialect 0x%x\n", conn->dialect); | |
277 | rsp->DialectRevision = cpu_to_le16(conn->dialect); | |
278 | /* Not setting conn guid rsp->ServerGUID, as it | |
279 | * not used by client for identifying connection | |
280 | */ | |
281 | rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); | |
282 | /* Default Max Message Size till SMB2.0, 64K*/ | |
283 | rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size); | |
284 | rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size); | |
285 | rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size); | |
286 | ||
287 | rsp->SystemTime = cpu_to_le64(ksmbd_systime()); | |
288 | rsp->ServerStartTime = 0; | |
289 | ||
290 | rsp->SecurityBufferOffset = cpu_to_le16(128); | |
291 | rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH); | |
cb451720 | 292 | ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) + |
e2f34481 | 293 | le16_to_cpu(rsp->SecurityBufferOffset)); |
e2f34481 NJ |
294 | rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE; |
295 | if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) | |
296 | rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE; | |
e2b76ab8 NJ |
297 | err = ksmbd_iov_pin_rsp(work, rsp, |
298 | sizeof(struct smb2_negotiate_rsp) + AUTH_GSS_LENGTH); | |
299 | if (err) | |
300 | return err; | |
e2f34481 NJ |
301 | conn->use_spnego = true; |
302 | ||
f5c779b7 | 303 | ksmbd_conn_set_need_negotiate(conn); |
e2f34481 NJ |
304 | return 0; |
305 | } | |
306 | ||
e2f34481 NJ |
307 | /** |
308 | * smb2_set_rsp_credits() - set number of credits in response buffer | |
309 | * @work: smb work containing smb response buffer | |
310 | */ | |
311 | int smb2_set_rsp_credits(struct ksmbd_work *work) | |
312 | { | |
8a893315 NJ |
313 | struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work); |
314 | struct smb2_hdr *hdr = ksmbd_resp_buf_next(work); | |
e2f34481 | 315 | struct ksmbd_conn *conn = work->conn; |
914d7e57 | 316 | unsigned short credits_requested, aux_max; |
bf8acc9e | 317 | unsigned short credit_charge, credits_granted = 0; |
e2f34481 | 318 | |
bf8acc9e HL |
319 | if (work->send_no_response) |
320 | return 0; | |
e2f34481 | 321 | |
bf8acc9e | 322 | hdr->CreditCharge = req_hdr->CreditCharge; |
e2f34481 | 323 | |
004443b3 | 324 | if (conn->total_credits > conn->vals->max_credits) { |
bf8acc9e | 325 | hdr->CreditRequest = 0; |
bde1694a | 326 | pr_err("Total credits overflow: %d\n", conn->total_credits); |
bf8acc9e | 327 | return -EINVAL; |
e2f34481 NJ |
328 | } |
329 | ||
bf8acc9e HL |
330 | credit_charge = max_t(unsigned short, |
331 | le16_to_cpu(req_hdr->CreditCharge), 1); | |
914d7e57 NJ |
332 | if (credit_charge > conn->total_credits) { |
333 | ksmbd_debug(SMB, "Insufficient credits granted, given: %u, granted: %u\n", | |
334 | credit_charge, conn->total_credits); | |
335 | return -EINVAL; | |
336 | } | |
337 | ||
338 | conn->total_credits -= credit_charge; | |
b589f5db | 339 | conn->outstanding_credits -= credit_charge; |
bf8acc9e HL |
340 | credits_requested = max_t(unsigned short, |
341 | le16_to_cpu(req_hdr->CreditRequest), 1); | |
e2f34481 | 342 | |
bf8acc9e HL |
343 | /* according to smb2.credits smbtorture, Windows server |
344 | * 2016 or later grant up to 8192 credits at once. | |
345 | * | |
346 | * TODO: Need to adjuct CreditRequest value according to | |
347 | * current cpu load | |
348 | */ | |
bf8acc9e | 349 | if (hdr->Command == SMB2_NEGOTIATE) |
914d7e57 | 350 | aux_max = 1; |
bf8acc9e | 351 | else |
84c5aa47 | 352 | aux_max = conn->vals->max_credits - conn->total_credits; |
914d7e57 | 353 | credits_granted = min_t(unsigned short, credits_requested, aux_max); |
bf8acc9e | 354 | |
e2f34481 NJ |
355 | conn->total_credits += credits_granted; |
356 | work->credits_granted += credits_granted; | |
357 | ||
358 | if (!req_hdr->NextCommand) { | |
359 | /* Update CreditRequest in last request */ | |
360 | hdr->CreditRequest = cpu_to_le16(work->credits_granted); | |
361 | } | |
e2f34481 | 362 | ksmbd_debug(SMB, |
070fb21e NJ |
363 | "credits: requested[%d] granted[%d] total_granted[%d]\n", |
364 | credits_requested, credits_granted, | |
365 | conn->total_credits); | |
e2f34481 NJ |
366 | return 0; |
367 | } | |
368 | ||
369 | /** | |
370 | * init_chained_smb2_rsp() - initialize smb2 chained response | |
371 | * @work: smb work containing smb response buffer | |
372 | */ | |
373 | static void init_chained_smb2_rsp(struct ksmbd_work *work) | |
374 | { | |
8a893315 NJ |
375 | struct smb2_hdr *req = ksmbd_req_buf_next(work); |
376 | struct smb2_hdr *rsp = ksmbd_resp_buf_next(work); | |
e2f34481 NJ |
377 | struct smb2_hdr *rsp_hdr; |
378 | struct smb2_hdr *rcv_hdr; | |
379 | int next_hdr_offset = 0; | |
380 | int len, new_len; | |
381 | ||
382 | /* Len of this response = updated RFC len - offset of previous cmd | |
383 | * in the compound rsp | |
384 | */ | |
385 | ||
386 | /* Storing the current local FID which may be needed by subsequent | |
387 | * command in the compound request | |
388 | */ | |
389 | if (req->Command == SMB2_CREATE && rsp->Status == STATUS_SUCCESS) { | |
2d004c6c PA |
390 | work->compound_fid = ((struct smb2_create_rsp *)rsp)->VolatileFileId; |
391 | work->compound_pfid = ((struct smb2_create_rsp *)rsp)->PersistentFileId; | |
e2f34481 NJ |
392 | work->compound_sid = le64_to_cpu(rsp->SessionId); |
393 | } | |
394 | ||
e5066499 | 395 | len = get_rfc1002_len(work->response_buf) - work->next_smb2_rsp_hdr_off; |
e2f34481 NJ |
396 | next_hdr_offset = le32_to_cpu(req->NextCommand); |
397 | ||
398 | new_len = ALIGN(len, 8); | |
e2b76ab8 NJ |
399 | work->iov[work->iov_idx].iov_len += (new_len - len); |
400 | inc_rfc1001_len(work->response_buf, new_len - len); | |
e2f34481 NJ |
401 | rsp->NextCommand = cpu_to_le32(new_len); |
402 | ||
403 | work->next_smb2_rcv_hdr_off += next_hdr_offset; | |
e2b76ab8 | 404 | work->curr_smb2_rsp_hdr_off = work->next_smb2_rsp_hdr_off; |
e2f34481 NJ |
405 | work->next_smb2_rsp_hdr_off += new_len; |
406 | ksmbd_debug(SMB, | |
070fb21e NJ |
407 | "Compound req new_len = %d rcv off = %d rsp off = %d\n", |
408 | new_len, work->next_smb2_rcv_hdr_off, | |
409 | work->next_smb2_rsp_hdr_off); | |
e2f34481 | 410 | |
8a893315 NJ |
411 | rsp_hdr = ksmbd_resp_buf_next(work); |
412 | rcv_hdr = ksmbd_req_buf_next(work); | |
e2f34481 NJ |
413 | |
414 | if (!(rcv_hdr->Flags & SMB2_FLAGS_RELATED_OPERATIONS)) { | |
415 | ksmbd_debug(SMB, "related flag should be set\n"); | |
416 | work->compound_fid = KSMBD_NO_FID; | |
417 | work->compound_pfid = KSMBD_NO_FID; | |
418 | } | |
cb451720 | 419 | memset((char *)rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); |
18a015bc | 420 | rsp_hdr->ProtocolId = SMB2_PROTO_NUMBER; |
e2f34481 NJ |
421 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; |
422 | rsp_hdr->Command = rcv_hdr->Command; | |
423 | ||
424 | /* | |
425 | * Message is response. We don't grant oplock yet. | |
426 | */ | |
427 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR | | |
428 | SMB2_FLAGS_RELATED_OPERATIONS); | |
429 | rsp_hdr->NextCommand = 0; | |
430 | rsp_hdr->MessageId = rcv_hdr->MessageId; | |
431 | rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId; | |
432 | rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId; | |
433 | rsp_hdr->SessionId = rcv_hdr->SessionId; | |
434 | memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16); | |
435 | } | |
436 | ||
437 | /** | |
438 | * is_chained_smb2_message() - check for chained command | |
439 | * @work: smb work containing smb request buffer | |
440 | * | |
441 | * Return: true if chained request, otherwise false | |
442 | */ | |
443 | bool is_chained_smb2_message(struct ksmbd_work *work) | |
444 | { | |
cb451720 | 445 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
d72a9c15 | 446 | unsigned int len, next_cmd; |
e2f34481 NJ |
447 | |
448 | if (hdr->ProtocolId != SMB2_PROTO_NUMBER) | |
449 | return false; | |
450 | ||
8a893315 | 451 | hdr = ksmbd_req_buf_next(work); |
d72a9c15 NJ |
452 | next_cmd = le32_to_cpu(hdr->NextCommand); |
453 | if (next_cmd > 0) { | |
454 | if ((u64)work->next_smb2_rcv_hdr_off + next_cmd + | |
455 | __SMB2_HEADER_STRUCTURE_SIZE > | |
456 | get_rfc1002_len(work->request_buf)) { | |
457 | pr_err("next command(%u) offset exceeds smb msg size\n", | |
458 | next_cmd); | |
459 | return false; | |
460 | } | |
461 | ||
dbad6300 NJ |
462 | if ((u64)get_rfc1002_len(work->response_buf) + MAX_CIFS_SMALL_BUFFER_SIZE > |
463 | work->response_sz) { | |
464 | pr_err("next response offset exceeds response buffer size\n"); | |
465 | return false; | |
466 | } | |
467 | ||
e2f34481 NJ |
468 | ksmbd_debug(SMB, "got SMB2 chained command\n"); |
469 | init_chained_smb2_rsp(work); | |
470 | return true; | |
471 | } else if (work->next_smb2_rcv_hdr_off) { | |
472 | /* | |
473 | * This is last request in chained command, | |
474 | * align response to 8 byte | |
475 | */ | |
e5066499 NJ |
476 | len = ALIGN(get_rfc1002_len(work->response_buf), 8); |
477 | len = len - get_rfc1002_len(work->response_buf); | |
e2f34481 NJ |
478 | if (len) { |
479 | ksmbd_debug(SMB, "padding len %u\n", len); | |
e2b76ab8 | 480 | work->iov[work->iov_idx].iov_len += len; |
e5066499 | 481 | inc_rfc1001_len(work->response_buf, len); |
e2f34481 | 482 | } |
e2b76ab8 | 483 | work->curr_smb2_rsp_hdr_off = work->next_smb2_rsp_hdr_off; |
e2f34481 NJ |
484 | } |
485 | return false; | |
486 | } | |
487 | ||
488 | /** | |
489 | * init_smb2_rsp_hdr() - initialize smb2 response | |
490 | * @work: smb work containing smb request buffer | |
491 | * | |
492 | * Return: 0 | |
493 | */ | |
494 | int init_smb2_rsp_hdr(struct ksmbd_work *work) | |
495 | { | |
cb451720 NJ |
496 | struct smb2_hdr *rsp_hdr = smb2_get_msg(work->response_buf); |
497 | struct smb2_hdr *rcv_hdr = smb2_get_msg(work->request_buf); | |
e2f34481 NJ |
498 | |
499 | memset(rsp_hdr, 0, sizeof(struct smb2_hdr) + 2); | |
e2f34481 NJ |
500 | rsp_hdr->ProtocolId = rcv_hdr->ProtocolId; |
501 | rsp_hdr->StructureSize = SMB2_HEADER_STRUCTURE_SIZE; | |
502 | rsp_hdr->Command = rcv_hdr->Command; | |
503 | ||
504 | /* | |
505 | * Message is response. We don't grant oplock yet. | |
506 | */ | |
507 | rsp_hdr->Flags = (SMB2_FLAGS_SERVER_TO_REDIR); | |
508 | rsp_hdr->NextCommand = 0; | |
509 | rsp_hdr->MessageId = rcv_hdr->MessageId; | |
510 | rsp_hdr->Id.SyncId.ProcessId = rcv_hdr->Id.SyncId.ProcessId; | |
511 | rsp_hdr->Id.SyncId.TreeId = rcv_hdr->Id.SyncId.TreeId; | |
512 | rsp_hdr->SessionId = rcv_hdr->SessionId; | |
513 | memcpy(rsp_hdr->Signature, rcv_hdr->Signature, 16); | |
514 | ||
e2f34481 NJ |
515 | return 0; |
516 | } | |
517 | ||
518 | /** | |
519 | * smb2_allocate_rsp_buf() - allocate smb2 response buffer | |
520 | * @work: smb work containing smb request buffer | |
521 | * | |
522 | * Return: 0 on success, otherwise -ENOMEM | |
523 | */ | |
524 | int smb2_allocate_rsp_buf(struct ksmbd_work *work) | |
525 | { | |
cb451720 | 526 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
e2f34481 | 527 | size_t small_sz = MAX_CIFS_SMALL_BUFFER_SIZE; |
4bc59477 | 528 | size_t large_sz = small_sz + work->conn->vals->max_trans_size; |
e2f34481 NJ |
529 | size_t sz = small_sz; |
530 | int cmd = le16_to_cpu(hdr->Command); | |
531 | ||
c30f4eb8 | 532 | if (cmd == SMB2_IOCTL_HE || cmd == SMB2_QUERY_DIRECTORY_HE) |
e2f34481 | 533 | sz = large_sz; |
e2f34481 NJ |
534 | |
535 | if (cmd == SMB2_QUERY_INFO_HE) { | |
536 | struct smb2_query_info_req *req; | |
537 | ||
cb451720 | 538 | req = smb2_get_msg(work->request_buf); |
8f054118 NJ |
539 | if ((req->InfoType == SMB2_O_INFO_FILE && |
540 | (req->FileInfoClass == FILE_FULL_EA_INFORMATION || | |
541 | req->FileInfoClass == FILE_ALL_INFORMATION)) || | |
542 | req->InfoType == SMB2_O_INFO_SECURITY) | |
e2f34481 | 543 | sz = large_sz; |
e2f34481 NJ |
544 | } |
545 | ||
546 | /* allocate large response buf for chained commands */ | |
547 | if (le32_to_cpu(hdr->NextCommand) > 0) | |
548 | sz = large_sz; | |
549 | ||
81a94b27 | 550 | work->response_buf = kvzalloc(sz, GFP_KERNEL); |
63c454f8 | 551 | if (!work->response_buf) |
e2f34481 | 552 | return -ENOMEM; |
e2f34481 NJ |
553 | |
554 | work->response_sz = sz; | |
555 | return 0; | |
556 | } | |
557 | ||
558 | /** | |
559 | * smb2_check_user_session() - check for valid session for a user | |
560 | * @work: smb work containing smb request buffer | |
561 | * | |
562 | * Return: 0 on success, otherwise error | |
563 | */ | |
564 | int smb2_check_user_session(struct ksmbd_work *work) | |
565 | { | |
3df0411e | 566 | struct smb2_hdr *req_hdr = ksmbd_req_buf_next(work); |
e2f34481 | 567 | struct ksmbd_conn *conn = work->conn; |
3df0411e | 568 | unsigned int cmd = le16_to_cpu(req_hdr->Command); |
e2f34481 NJ |
569 | unsigned long long sess_id; |
570 | ||
e2f34481 NJ |
571 | /* |
572 | * SMB2_ECHO, SMB2_NEGOTIATE, SMB2_SESSION_SETUP command do not | |
573 | * require a session id, so no need to validate user session's for | |
574 | * these commands. | |
575 | */ | |
576 | if (cmd == SMB2_ECHO_HE || cmd == SMB2_NEGOTIATE_HE || | |
64b39f4a | 577 | cmd == SMB2_SESSION_SETUP_HE) |
e2f34481 NJ |
578 | return 0; |
579 | ||
f5c779b7 | 580 | if (!ksmbd_conn_good(conn)) |
5005bcb4 | 581 | return -EIO; |
e2f34481 NJ |
582 | |
583 | sess_id = le64_to_cpu(req_hdr->SessionId); | |
5005bcb4 NJ |
584 | |
585 | /* | |
586 | * If request is not the first in Compound request, | |
587 | * Just validate session id in header with work->sess->id. | |
588 | */ | |
589 | if (work->next_smb2_rcv_hdr_off) { | |
590 | if (!work->sess) { | |
591 | pr_err("The first operation in the compound does not have sess\n"); | |
592 | return -EINVAL; | |
593 | } | |
3df0411e | 594 | if (sess_id != ULLONG_MAX && work->sess->id != sess_id) { |
5005bcb4 NJ |
595 | pr_err("session id(%llu) is different with the first operation(%lld)\n", |
596 | sess_id, work->sess->id); | |
597 | return -EINVAL; | |
598 | } | |
599 | return 1; | |
600 | } | |
601 | ||
e2f34481 | 602 | /* Check for validity of user session */ |
f5a544e3 | 603 | work->sess = ksmbd_session_lookup_all(conn, sess_id); |
e2f34481 NJ |
604 | if (work->sess) |
605 | return 1; | |
606 | ksmbd_debug(SMB, "Invalid user session, Uid %llu\n", sess_id); | |
5005bcb4 | 607 | return -ENOENT; |
e2f34481 NJ |
608 | } |
609 | ||
e4d3e6b5 NJ |
610 | static void destroy_previous_session(struct ksmbd_conn *conn, |
611 | struct ksmbd_user *user, u64 id) | |
e2f34481 NJ |
612 | { |
613 | struct ksmbd_session *prev_sess = ksmbd_session_lookup_slowpath(id); | |
614 | struct ksmbd_user *prev_user; | |
17ea92a9 | 615 | struct channel *chann; |
1d9c4172 | 616 | long index; |
e2f34481 NJ |
617 | |
618 | if (!prev_sess) | |
619 | return; | |
620 | ||
621 | prev_user = prev_sess->user; | |
622 | ||
1fca8038 MM |
623 | if (!prev_user || |
624 | strcmp(user->name, prev_user->name) || | |
e2f34481 | 625 | user->passkey_sz != prev_user->passkey_sz || |
af7c39d9 | 626 | memcmp(user->passkey, prev_user->passkey, user->passkey_sz)) |
e2f34481 | 627 | return; |
e2f34481 | 628 | |
17ea92a9 | 629 | prev_sess->state = SMB2_SESSION_EXPIRED; |
1d9c4172 | 630 | xa_for_each(&prev_sess->ksmbd_chann_list, index, chann) |
f5c779b7 | 631 | ksmbd_conn_set_exiting(chann->conn); |
e2f34481 NJ |
632 | } |
633 | ||
634 | /** | |
635 | * smb2_get_name() - get filename string from on the wire smb format | |
636 | * @src: source buffer | |
637 | * @maxlen: maxlen of source string | |
d4eeb826 | 638 | * @local_nls: nls_table pointer |
e2f34481 NJ |
639 | * |
640 | * Return: matching converted filename on success, otherwise error ptr | |
641 | */ | |
642 | static char * | |
80917f17 | 643 | smb2_get_name(const char *src, const int maxlen, struct nls_table *local_nls) |
e2f34481 | 644 | { |
265fd199 | 645 | char *name; |
e2f34481 | 646 | |
64b39f4a | 647 | name = smb_strndup_from_utf16(src, maxlen, 1, local_nls); |
e2f34481 | 648 | if (IS_ERR(name)) { |
bde1694a | 649 | pr_err("failed to get name %ld\n", PTR_ERR(name)); |
e2f34481 NJ |
650 | return name; |
651 | } | |
652 | ||
265fd199 HL |
653 | ksmbd_conv_path_to_unix(name); |
654 | ksmbd_strip_last_slash(name); | |
655 | return name; | |
e2f34481 NJ |
656 | } |
657 | ||
e2f34481 NJ |
658 | int setup_async_work(struct ksmbd_work *work, void (*fn)(void **), void **arg) |
659 | { | |
e2f34481 NJ |
660 | struct ksmbd_conn *conn = work->conn; |
661 | int id; | |
662 | ||
d40012a8 | 663 | id = ksmbd_acquire_async_msg_id(&conn->async_ida); |
e2f34481 | 664 | if (id < 0) { |
bde1694a | 665 | pr_err("Failed to alloc async message id\n"); |
e2f34481 NJ |
666 | return id; |
667 | } | |
3a9b557f | 668 | work->asynchronous = true; |
e2f34481 | 669 | work->async_id = id; |
e2f34481 NJ |
670 | |
671 | ksmbd_debug(SMB, | |
070fb21e NJ |
672 | "Send interim Response to inform async request id : %d\n", |
673 | work->async_id); | |
e2f34481 NJ |
674 | |
675 | work->cancel_fn = fn; | |
676 | work->cancel_argv = arg; | |
677 | ||
6c4e675a NJ |
678 | if (list_empty(&work->async_request_entry)) { |
679 | spin_lock(&conn->request_lock); | |
680 | list_add_tail(&work->async_request_entry, &conn->async_requests); | |
681 | spin_unlock(&conn->request_lock); | |
682 | } | |
e2f34481 NJ |
683 | |
684 | return 0; | |
685 | } | |
686 | ||
3a9b557f NJ |
687 | void release_async_work(struct ksmbd_work *work) |
688 | { | |
689 | struct ksmbd_conn *conn = work->conn; | |
690 | ||
691 | spin_lock(&conn->request_lock); | |
692 | list_del_init(&work->async_request_entry); | |
693 | spin_unlock(&conn->request_lock); | |
694 | ||
695 | work->asynchronous = 0; | |
696 | work->cancel_fn = NULL; | |
697 | kfree(work->cancel_argv); | |
698 | work->cancel_argv = NULL; | |
699 | if (work->async_id) { | |
700 | ksmbd_release_id(&conn->async_ida, work->async_id); | |
701 | work->async_id = 0; | |
702 | } | |
703 | } | |
704 | ||
e2f34481 NJ |
705 | void smb2_send_interim_resp(struct ksmbd_work *work, __le32 status) |
706 | { | |
707 | struct smb2_hdr *rsp_hdr; | |
041bba44 | 708 | struct ksmbd_work *in_work = ksmbd_alloc_work_struct(); |
e2f34481 | 709 | |
041bba44 NJ |
710 | if (allocate_interim_rsp_buf(in_work)) { |
711 | pr_err("smb_allocate_rsp_buf failed!\n"); | |
712 | ksmbd_free_work_struct(in_work); | |
713 | return; | |
714 | } | |
715 | ||
716 | in_work->conn = work->conn; | |
717 | memcpy(smb2_get_msg(in_work->response_buf), ksmbd_resp_buf_next(work), | |
718 | __SMB2_HEADER_STRUCTURE_SIZE); | |
719 | ||
720 | rsp_hdr = smb2_get_msg(in_work->response_buf); | |
9ac45ac7 NJ |
721 | rsp_hdr->Flags |= SMB2_FLAGS_ASYNC_COMMAND; |
722 | rsp_hdr->Id.AsyncId = cpu_to_le64(work->async_id); | |
041bba44 | 723 | smb2_set_err_rsp(in_work); |
e2f34481 NJ |
724 | rsp_hdr->Status = status; |
725 | ||
041bba44 NJ |
726 | ksmbd_conn_write(in_work); |
727 | ksmbd_free_work_struct(in_work); | |
e2f34481 NJ |
728 | } |
729 | ||
730 | static __le32 smb2_get_reparse_tag_special_file(umode_t mode) | |
731 | { | |
732 | if (S_ISDIR(mode) || S_ISREG(mode)) | |
733 | return 0; | |
734 | ||
735 | if (S_ISLNK(mode)) | |
736 | return IO_REPARSE_TAG_LX_SYMLINK_LE; | |
737 | else if (S_ISFIFO(mode)) | |
738 | return IO_REPARSE_TAG_LX_FIFO_LE; | |
739 | else if (S_ISSOCK(mode)) | |
740 | return IO_REPARSE_TAG_AF_UNIX_LE; | |
741 | else if (S_ISCHR(mode)) | |
742 | return IO_REPARSE_TAG_LX_CHR_LE; | |
743 | else if (S_ISBLK(mode)) | |
744 | return IO_REPARSE_TAG_LX_BLK_LE; | |
745 | ||
746 | return 0; | |
747 | } | |
748 | ||
749 | /** | |
750 | * smb2_get_dos_mode() - get file mode in dos format from unix mode | |
751 | * @stat: kstat containing file mode | |
95fa1ce9 | 752 | * @attribute: attribute flags |
e2f34481 NJ |
753 | * |
754 | * Return: converted dos mode | |
755 | */ | |
756 | static int smb2_get_dos_mode(struct kstat *stat, int attribute) | |
757 | { | |
758 | int attr = 0; | |
759 | ||
64b39f4a | 760 | if (S_ISDIR(stat->mode)) { |
26a2787d RS |
761 | attr = FILE_ATTRIBUTE_DIRECTORY | |
762 | (attribute & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)); | |
64b39f4a | 763 | } else { |
26a2787d RS |
764 | attr = (attribute & 0x00005137) | FILE_ATTRIBUTE_ARCHIVE; |
765 | attr &= ~(FILE_ATTRIBUTE_DIRECTORY); | |
e2f34481 NJ |
766 | if (S_ISREG(stat->mode) && (server_conf.share_fake_fscaps & |
767 | FILE_SUPPORTS_SPARSE_FILES)) | |
26a2787d | 768 | attr |= FILE_ATTRIBUTE_SPARSE_FILE; |
e2f34481 NJ |
769 | |
770 | if (smb2_get_reparse_tag_special_file(stat->mode)) | |
26a2787d | 771 | attr |= FILE_ATTRIBUTE_REPARSE_POINT; |
e2f34481 NJ |
772 | } |
773 | ||
774 | return attr; | |
775 | } | |
776 | ||
777 | static void build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt, | |
070fb21e | 778 | __le16 hash_id) |
e2f34481 NJ |
779 | { |
780 | pneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES; | |
781 | pneg_ctxt->DataLength = cpu_to_le16(38); | |
782 | pneg_ctxt->HashAlgorithmCount = cpu_to_le16(1); | |
783 | pneg_ctxt->Reserved = cpu_to_le32(0); | |
784 | pneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE); | |
785 | get_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE); | |
786 | pneg_ctxt->HashAlgorithms = hash_id; | |
787 | } | |
788 | ||
789 | static void build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt, | |
070fb21e | 790 | __le16 cipher_type) |
e2f34481 NJ |
791 | { |
792 | pneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES; | |
793 | pneg_ctxt->DataLength = cpu_to_le16(4); | |
794 | pneg_ctxt->Reserved = cpu_to_le32(0); | |
795 | pneg_ctxt->CipherCount = cpu_to_le16(1); | |
796 | pneg_ctxt->Ciphers[0] = cipher_type; | |
797 | } | |
798 | ||
378087cd NJ |
799 | static void build_sign_cap_ctxt(struct smb2_signing_capabilities *pneg_ctxt, |
800 | __le16 sign_algo) | |
801 | { | |
802 | pneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES; | |
803 | pneg_ctxt->DataLength = | |
804 | cpu_to_le16((sizeof(struct smb2_signing_capabilities) + 2) | |
805 | - sizeof(struct smb2_neg_context)); | |
806 | pneg_ctxt->Reserved = cpu_to_le32(0); | |
807 | pneg_ctxt->SigningAlgorithmCount = cpu_to_le16(1); | |
808 | pneg_ctxt->SigningAlgorithms[0] = sign_algo; | |
809 | } | |
810 | ||
64b39f4a | 811 | static void build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt) |
e2f34481 NJ |
812 | { |
813 | pneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE; | |
814 | pneg_ctxt->DataLength = cpu_to_le16(POSIX_CTXT_DATA_LEN); | |
815 | /* SMB2_CREATE_TAG_POSIX is "0x93AD25509CB411E7B42383DE968BCD7C" */ | |
816 | pneg_ctxt->Name[0] = 0x93; | |
817 | pneg_ctxt->Name[1] = 0xAD; | |
818 | pneg_ctxt->Name[2] = 0x25; | |
819 | pneg_ctxt->Name[3] = 0x50; | |
820 | pneg_ctxt->Name[4] = 0x9C; | |
821 | pneg_ctxt->Name[5] = 0xB4; | |
822 | pneg_ctxt->Name[6] = 0x11; | |
823 | pneg_ctxt->Name[7] = 0xE7; | |
824 | pneg_ctxt->Name[8] = 0xB4; | |
825 | pneg_ctxt->Name[9] = 0x23; | |
826 | pneg_ctxt->Name[10] = 0x83; | |
827 | pneg_ctxt->Name[11] = 0xDE; | |
828 | pneg_ctxt->Name[12] = 0x96; | |
829 | pneg_ctxt->Name[13] = 0x8B; | |
830 | pneg_ctxt->Name[14] = 0xCD; | |
831 | pneg_ctxt->Name[15] = 0x7C; | |
832 | } | |
833 | ||
e2b76ab8 NJ |
834 | static unsigned int assemble_neg_contexts(struct ksmbd_conn *conn, |
835 | struct smb2_negotiate_rsp *rsp) | |
e2f34481 | 836 | { |
a12a07a8 | 837 | char * const pneg_ctxt = (char *)rsp + |
cb451720 | 838 | le32_to_cpu(rsp->NegotiateContextOffset); |
e2f34481 NJ |
839 | int neg_ctxt_cnt = 1; |
840 | int ctxt_size; | |
841 | ||
842 | ksmbd_debug(SMB, | |
070fb21e | 843 | "assemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n"); |
e2f34481 | 844 | build_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt, |
070fb21e | 845 | conn->preauth_info->Preauth_HashId); |
e2f34481 | 846 | ctxt_size = sizeof(struct smb2_preauth_neg_context); |
e2f34481 NJ |
847 | |
848 | if (conn->cipher_type) { | |
a12a07a8 | 849 | /* Round to 8 byte boundary */ |
e2f34481 NJ |
850 | ctxt_size = round_up(ctxt_size, 8); |
851 | ksmbd_debug(SMB, | |
070fb21e | 852 | "assemble SMB2_ENCRYPTION_CAPABILITIES context\n"); |
a12a07a8 DD |
853 | build_encrypt_ctxt((struct smb2_encryption_neg_context *) |
854 | (pneg_ctxt + ctxt_size), | |
070fb21e | 855 | conn->cipher_type); |
34e8ccf9 | 856 | neg_ctxt_cnt++; |
af320a73 | 857 | ctxt_size += sizeof(struct smb2_encryption_neg_context) + 2; |
e2f34481 NJ |
858 | } |
859 | ||
af36c51e DD |
860 | /* compression context not yet supported */ |
861 | WARN_ON(conn->compress_algorithm != SMB3_COMPRESS_NONE); | |
e2f34481 NJ |
862 | |
863 | if (conn->posix_ext_supported) { | |
864 | ctxt_size = round_up(ctxt_size, 8); | |
865 | ksmbd_debug(SMB, | |
070fb21e | 866 | "assemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n"); |
a12a07a8 DD |
867 | build_posix_ctxt((struct smb2_posix_neg_context *) |
868 | (pneg_ctxt + ctxt_size)); | |
34e8ccf9 | 869 | neg_ctxt_cnt++; |
e2f34481 | 870 | ctxt_size += sizeof(struct smb2_posix_neg_context); |
378087cd NJ |
871 | } |
872 | ||
873 | if (conn->signing_negotiated) { | |
874 | ctxt_size = round_up(ctxt_size, 8); | |
875 | ksmbd_debug(SMB, | |
876 | "assemble SMB2_SIGNING_CAPABILITIES context\n"); | |
a12a07a8 DD |
877 | build_sign_cap_ctxt((struct smb2_signing_capabilities *) |
878 | (pneg_ctxt + ctxt_size), | |
378087cd | 879 | conn->signing_algorithm); |
34e8ccf9 | 880 | neg_ctxt_cnt++; |
378087cd | 881 | ctxt_size += sizeof(struct smb2_signing_capabilities) + 2; |
e2f34481 NJ |
882 | } |
883 | ||
34e8ccf9 | 884 | rsp->NegotiateContextCount = cpu_to_le16(neg_ctxt_cnt); |
e2b76ab8 | 885 | return ctxt_size + AUTH_GSS_PADDING; |
e2f34481 NJ |
886 | } |
887 | ||
64b39f4a | 888 | static __le32 decode_preauth_ctxt(struct ksmbd_conn *conn, |
e7067a44 | 889 | struct smb2_preauth_neg_context *pneg_ctxt, |
0512a5f8 | 890 | int ctxt_len) |
e2f34481 | 891 | { |
e7067a44 DD |
892 | /* |
893 | * sizeof(smb2_preauth_neg_context) assumes SMB311_SALT_SIZE Salt, | |
894 | * which may not be present. Only check for used HashAlgorithms[1]. | |
895 | */ | |
0512a5f8 KTC |
896 | if (ctxt_len < |
897 | sizeof(struct smb2_neg_context) + MIN_PREAUTH_CTXT_DATA_LEN) | |
e7067a44 | 898 | return STATUS_INVALID_PARAMETER; |
e2f34481 | 899 | |
e7067a44 DD |
900 | if (pneg_ctxt->HashAlgorithms != SMB2_PREAUTH_INTEGRITY_SHA512) |
901 | return STATUS_NO_PREAUTH_INTEGRITY_HASH_OVERLAP; | |
e2f34481 | 902 | |
e7067a44 DD |
903 | conn->preauth_info->Preauth_HashId = SMB2_PREAUTH_INTEGRITY_SHA512; |
904 | return STATUS_SUCCESS; | |
e2f34481 NJ |
905 | } |
906 | ||
af320a73 NJ |
907 | static void decode_encrypt_ctxt(struct ksmbd_conn *conn, |
908 | struct smb2_encryption_neg_context *pneg_ctxt, | |
0512a5f8 | 909 | int ctxt_len) |
e2f34481 | 910 | { |
0512a5f8 KTC |
911 | int cph_cnt; |
912 | int i, cphs_size; | |
913 | ||
914 | if (sizeof(struct smb2_encryption_neg_context) > ctxt_len) { | |
915 | pr_err("Invalid SMB2_ENCRYPTION_CAPABILITIES context size\n"); | |
916 | return; | |
917 | } | |
e2f34481 NJ |
918 | |
919 | conn->cipher_type = 0; | |
920 | ||
0512a5f8 KTC |
921 | cph_cnt = le16_to_cpu(pneg_ctxt->CipherCount); |
922 | cphs_size = cph_cnt * sizeof(__le16); | |
923 | ||
af320a73 | 924 | if (sizeof(struct smb2_encryption_neg_context) + cphs_size > |
0512a5f8 | 925 | ctxt_len) { |
af320a73 NJ |
926 | pr_err("Invalid cipher count(%d)\n", cph_cnt); |
927 | return; | |
928 | } | |
929 | ||
37ba7b00 | 930 | if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION_OFF) |
af320a73 | 931 | return; |
e2f34481 NJ |
932 | |
933 | for (i = 0; i < cph_cnt; i++) { | |
934 | if (pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_GCM || | |
5a0ca770 NJ |
935 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES128_CCM || |
936 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_CCM || | |
937 | pneg_ctxt->Ciphers[i] == SMB2_ENCRYPTION_AES256_GCM) { | |
e2f34481 | 938 | ksmbd_debug(SMB, "Cipher ID = 0x%x\n", |
070fb21e | 939 | pneg_ctxt->Ciphers[i]); |
e2f34481 NJ |
940 | conn->cipher_type = pneg_ctxt->Ciphers[i]; |
941 | break; | |
942 | } | |
943 | } | |
e2f34481 NJ |
944 | } |
945 | ||
83912d6d MDSV |
946 | /** |
947 | * smb3_encryption_negotiated() - checks if server and client agreed on enabling encryption | |
948 | * @conn: smb connection | |
949 | * | |
950 | * Return: true if connection should be encrypted, else false | |
951 | */ | |
5bedae90 | 952 | bool smb3_encryption_negotiated(struct ksmbd_conn *conn) |
83912d6d MDSV |
953 | { |
954 | if (!conn->ops->generate_encryptionkey) | |
955 | return false; | |
956 | ||
957 | /* | |
958 | * SMB 3.0 and 3.0.2 dialects use the SMB2_GLOBAL_CAP_ENCRYPTION flag. | |
959 | * SMB 3.1.1 uses the cipher_type field. | |
960 | */ | |
961 | return (conn->vals->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) || | |
962 | conn->cipher_type; | |
963 | } | |
964 | ||
af320a73 | 965 | static void decode_compress_ctxt(struct ksmbd_conn *conn, |
d6c9ad23 | 966 | struct smb2_compression_capabilities_context *pneg_ctxt) |
e2f34481 | 967 | { |
e2f34481 | 968 | conn->compress_algorithm = SMB3_COMPRESS_NONE; |
e2f34481 NJ |
969 | } |
970 | ||
378087cd NJ |
971 | static void decode_sign_cap_ctxt(struct ksmbd_conn *conn, |
972 | struct smb2_signing_capabilities *pneg_ctxt, | |
0512a5f8 | 973 | int ctxt_len) |
378087cd | 974 | { |
0512a5f8 KTC |
975 | int sign_algo_cnt; |
976 | int i, sign_alos_size; | |
977 | ||
978 | if (sizeof(struct smb2_signing_capabilities) > ctxt_len) { | |
979 | pr_err("Invalid SMB2_SIGNING_CAPABILITIES context length\n"); | |
980 | return; | |
981 | } | |
378087cd NJ |
982 | |
983 | conn->signing_negotiated = false; | |
0512a5f8 KTC |
984 | sign_algo_cnt = le16_to_cpu(pneg_ctxt->SigningAlgorithmCount); |
985 | sign_alos_size = sign_algo_cnt * sizeof(__le16); | |
378087cd NJ |
986 | |
987 | if (sizeof(struct smb2_signing_capabilities) + sign_alos_size > | |
0512a5f8 | 988 | ctxt_len) { |
378087cd NJ |
989 | pr_err("Invalid signing algorithm count(%d)\n", sign_algo_cnt); |
990 | return; | |
991 | } | |
992 | ||
993 | for (i = 0; i < sign_algo_cnt; i++) { | |
d6c9ad23 RS |
994 | if (pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_HMAC_SHA256_LE || |
995 | pneg_ctxt->SigningAlgorithms[i] == SIGNING_ALG_AES_CMAC_LE) { | |
378087cd NJ |
996 | ksmbd_debug(SMB, "Signing Algorithm ID = 0x%x\n", |
997 | pneg_ctxt->SigningAlgorithms[i]); | |
998 | conn->signing_negotiated = true; | |
999 | conn->signing_algorithm = | |
1000 | pneg_ctxt->SigningAlgorithms[i]; | |
1001 | break; | |
1002 | } | |
1003 | } | |
1004 | } | |
1005 | ||
e2f34481 | 1006 | static __le32 deassemble_neg_contexts(struct ksmbd_conn *conn, |
cb451720 | 1007 | struct smb2_negotiate_req *req, |
f1a41187 | 1008 | unsigned int len_of_smb) |
e2f34481 | 1009 | { |
e2f34481 | 1010 | /* +4 is to account for the RFC1001 len field */ |
cb451720 | 1011 | struct smb2_neg_context *pctx = (struct smb2_neg_context *)req; |
af320a73 | 1012 | int i = 0, len_of_ctxts; |
f1a41187 NJ |
1013 | unsigned int offset = le32_to_cpu(req->NegotiateContextOffset); |
1014 | unsigned int neg_ctxt_cnt = le16_to_cpu(req->NegotiateContextCount); | |
af320a73 NJ |
1015 | __le32 status = STATUS_INVALID_PARAMETER; |
1016 | ||
1017 | ksmbd_debug(SMB, "decoding %d negotiate contexts\n", neg_ctxt_cnt); | |
1018 | if (len_of_smb <= offset) { | |
1019 | ksmbd_debug(SMB, "Invalid response: negotiate context offset\n"); | |
1020 | return status; | |
1021 | } | |
1022 | ||
1023 | len_of_ctxts = len_of_smb - offset; | |
e2f34481 | 1024 | |
e2f34481 | 1025 | while (i++ < neg_ctxt_cnt) { |
0512a5f8 | 1026 | int clen, ctxt_len; |
af320a73 | 1027 | |
f1a41187 | 1028 | if (len_of_ctxts < (int)sizeof(struct smb2_neg_context)) |
af320a73 NJ |
1029 | break; |
1030 | ||
1031 | pctx = (struct smb2_neg_context *)((char *)pctx + offset); | |
1032 | clen = le16_to_cpu(pctx->DataLength); | |
0512a5f8 KTC |
1033 | ctxt_len = clen + sizeof(struct smb2_neg_context); |
1034 | ||
1035 | if (ctxt_len > len_of_ctxts) | |
af320a73 NJ |
1036 | break; |
1037 | ||
1038 | if (pctx->ContextType == SMB2_PREAUTH_INTEGRITY_CAPABILITIES) { | |
e2f34481 | 1039 | ksmbd_debug(SMB, |
070fb21e | 1040 | "deassemble SMB2_PREAUTH_INTEGRITY_CAPABILITIES context\n"); |
e2f34481 NJ |
1041 | if (conn->preauth_info->Preauth_HashId) |
1042 | break; | |
1043 | ||
1044 | status = decode_preauth_ctxt(conn, | |
e7067a44 | 1045 | (struct smb2_preauth_neg_context *)pctx, |
0512a5f8 | 1046 | ctxt_len); |
af320a73 NJ |
1047 | if (status != STATUS_SUCCESS) |
1048 | break; | |
1049 | } else if (pctx->ContextType == SMB2_ENCRYPTION_CAPABILITIES) { | |
e2f34481 | 1050 | ksmbd_debug(SMB, |
070fb21e | 1051 | "deassemble SMB2_ENCRYPTION_CAPABILITIES context\n"); |
e2f34481 NJ |
1052 | if (conn->cipher_type) |
1053 | break; | |
1054 | ||
af320a73 NJ |
1055 | decode_encrypt_ctxt(conn, |
1056 | (struct smb2_encryption_neg_context *)pctx, | |
0512a5f8 | 1057 | ctxt_len); |
af320a73 | 1058 | } else if (pctx->ContextType == SMB2_COMPRESSION_CAPABILITIES) { |
e2f34481 | 1059 | ksmbd_debug(SMB, |
070fb21e | 1060 | "deassemble SMB2_COMPRESSION_CAPABILITIES context\n"); |
e2f34481 NJ |
1061 | if (conn->compress_algorithm) |
1062 | break; | |
1063 | ||
af320a73 | 1064 | decode_compress_ctxt(conn, |
d6c9ad23 | 1065 | (struct smb2_compression_capabilities_context *)pctx); |
af320a73 | 1066 | } else if (pctx->ContextType == SMB2_NETNAME_NEGOTIATE_CONTEXT_ID) { |
e2f34481 | 1067 | ksmbd_debug(SMB, |
070fb21e | 1068 | "deassemble SMB2_NETNAME_NEGOTIATE_CONTEXT_ID context\n"); |
af320a73 | 1069 | } else if (pctx->ContextType == SMB2_POSIX_EXTENSIONS_AVAILABLE) { |
e2f34481 | 1070 | ksmbd_debug(SMB, |
070fb21e | 1071 | "deassemble SMB2_POSIX_EXTENSIONS_AVAILABLE context\n"); |
e2f34481 | 1072 | conn->posix_ext_supported = true; |
378087cd NJ |
1073 | } else if (pctx->ContextType == SMB2_SIGNING_CAPABILITIES) { |
1074 | ksmbd_debug(SMB, | |
1075 | "deassemble SMB2_SIGNING_CAPABILITIES context\n"); | |
0512a5f8 | 1076 | |
378087cd NJ |
1077 | decode_sign_cap_ctxt(conn, |
1078 | (struct smb2_signing_capabilities *)pctx, | |
0512a5f8 | 1079 | ctxt_len); |
e2f34481 | 1080 | } |
e2f34481 | 1081 | |
af320a73 | 1082 | /* offsets must be 8 byte aligned */ |
f1a41187 NJ |
1083 | offset = (ctxt_len + 7) & ~0x7; |
1084 | len_of_ctxts -= offset; | |
e2f34481 NJ |
1085 | } |
1086 | return status; | |
1087 | } | |
1088 | ||
1089 | /** | |
1090 | * smb2_handle_negotiate() - handler for smb2 negotiate command | |
1091 | * @work: smb work containing smb request buffer | |
1092 | * | |
1093 | * Return: 0 | |
1094 | */ | |
1095 | int smb2_handle_negotiate(struct ksmbd_work *work) | |
1096 | { | |
1097 | struct ksmbd_conn *conn = work->conn; | |
cb451720 NJ |
1098 | struct smb2_negotiate_req *req = smb2_get_msg(work->request_buf); |
1099 | struct smb2_negotiate_rsp *rsp = smb2_get_msg(work->response_buf); | |
e2f34481 | 1100 | int rc = 0; |
e2b76ab8 | 1101 | unsigned int smb2_buf_len, smb2_neg_size, neg_ctxt_len = 0; |
e2f34481 NJ |
1102 | __le32 status; |
1103 | ||
1104 | ksmbd_debug(SMB, "Received negotiate request\n"); | |
1105 | conn->need_neg = false; | |
f5c779b7 | 1106 | if (ksmbd_conn_good(conn)) { |
bde1694a | 1107 | pr_err("conn->tcp_status is already in CifsGood State\n"); |
e2f34481 NJ |
1108 | work->send_no_response = 1; |
1109 | return rc; | |
1110 | } | |
1111 | ||
d738950f KTC |
1112 | smb2_buf_len = get_rfc1002_len(work->request_buf); |
1113 | smb2_neg_size = offsetof(struct smb2_negotiate_req, Dialects); | |
1114 | if (smb2_neg_size > smb2_buf_len) { | |
e2f34481 NJ |
1115 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
1116 | rc = -EINVAL; | |
1117 | goto err_out; | |
1118 | } | |
1119 | ||
d738950f KTC |
1120 | if (req->DialectCount == 0) { |
1121 | pr_err("malformed packet\n"); | |
442ff9eb NJ |
1122 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
1123 | rc = -EINVAL; | |
1124 | goto err_out; | |
1125 | } | |
1126 | ||
1127 | if (conn->dialect == SMB311_PROT_ID) { | |
1128 | unsigned int nego_ctxt_off = le32_to_cpu(req->NegotiateContextOffset); | |
1129 | ||
1130 | if (smb2_buf_len < nego_ctxt_off) { | |
1131 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
1132 | rc = -EINVAL; | |
1133 | goto err_out; | |
1134 | } | |
1135 | ||
1136 | if (smb2_neg_size > nego_ctxt_off) { | |
1137 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
1138 | rc = -EINVAL; | |
1139 | goto err_out; | |
1140 | } | |
1141 | ||
1142 | if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) > | |
1143 | nego_ctxt_off) { | |
1144 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
1145 | rc = -EINVAL; | |
1146 | goto err_out; | |
1147 | } | |
1148 | } else { | |
1149 | if (smb2_neg_size + le16_to_cpu(req->DialectCount) * sizeof(__le16) > | |
1150 | smb2_buf_len) { | |
1151 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
1152 | rc = -EINVAL; | |
1153 | goto err_out; | |
1154 | } | |
1155 | } | |
1156 | ||
e2f34481 NJ |
1157 | conn->cli_cap = le32_to_cpu(req->Capabilities); |
1158 | switch (conn->dialect) { | |
1159 | case SMB311_PROT_ID: | |
1160 | conn->preauth_info = | |
1161 | kzalloc(sizeof(struct preauth_integrity_info), | |
070fb21e | 1162 | GFP_KERNEL); |
e2f34481 NJ |
1163 | if (!conn->preauth_info) { |
1164 | rc = -ENOMEM; | |
1165 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
1166 | goto err_out; | |
1167 | } | |
1168 | ||
cb451720 NJ |
1169 | status = deassemble_neg_contexts(conn, req, |
1170 | get_rfc1002_len(work->request_buf)); | |
e2f34481 | 1171 | if (status != STATUS_SUCCESS) { |
bde1694a NJ |
1172 | pr_err("deassemble_neg_contexts error(0x%x)\n", |
1173 | status); | |
e2f34481 NJ |
1174 | rsp->hdr.Status = status; |
1175 | rc = -EINVAL; | |
aa7253c2 NJ |
1176 | kfree(conn->preauth_info); |
1177 | conn->preauth_info = NULL; | |
e2f34481 NJ |
1178 | goto err_out; |
1179 | } | |
1180 | ||
1181 | rc = init_smb3_11_server(conn); | |
1182 | if (rc < 0) { | |
1183 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
aa7253c2 NJ |
1184 | kfree(conn->preauth_info); |
1185 | conn->preauth_info = NULL; | |
e2f34481 NJ |
1186 | goto err_out; |
1187 | } | |
1188 | ||
1189 | ksmbd_gen_preauth_integrity_hash(conn, | |
070fb21e NJ |
1190 | work->request_buf, |
1191 | conn->preauth_info->Preauth_HashValue); | |
e2f34481 NJ |
1192 | rsp->NegotiateContextOffset = |
1193 | cpu_to_le32(OFFSET_OF_NEG_CONTEXT); | |
e2b76ab8 | 1194 | neg_ctxt_len = assemble_neg_contexts(conn, rsp); |
e2f34481 NJ |
1195 | break; |
1196 | case SMB302_PROT_ID: | |
1197 | init_smb3_02_server(conn); | |
1198 | break; | |
1199 | case SMB30_PROT_ID: | |
1200 | init_smb3_0_server(conn); | |
1201 | break; | |
1202 | case SMB21_PROT_ID: | |
1203 | init_smb2_1_server(conn); | |
1204 | break; | |
e2f34481 NJ |
1205 | case SMB2X_PROT_ID: |
1206 | case BAD_PROT_ID: | |
1207 | default: | |
1208 | ksmbd_debug(SMB, "Server dialect :0x%x not supported\n", | |
070fb21e | 1209 | conn->dialect); |
e2f34481 NJ |
1210 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; |
1211 | rc = -EINVAL; | |
1212 | goto err_out; | |
1213 | } | |
1214 | rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); | |
1215 | ||
1216 | /* For stats */ | |
1217 | conn->connection_type = conn->dialect; | |
1218 | ||
1219 | rsp->MaxTransactSize = cpu_to_le32(conn->vals->max_trans_size); | |
1220 | rsp->MaxReadSize = cpu_to_le32(conn->vals->max_read_size); | |
1221 | rsp->MaxWriteSize = cpu_to_le32(conn->vals->max_write_size); | |
1222 | ||
51a13873 NJ |
1223 | memcpy(conn->ClientGUID, req->ClientGUID, |
1224 | SMB2_CLIENT_GUID_SIZE); | |
1225 | conn->cli_sec_mode = le16_to_cpu(req->SecurityMode); | |
e2f34481 NJ |
1226 | |
1227 | rsp->StructureSize = cpu_to_le16(65); | |
1228 | rsp->DialectRevision = cpu_to_le16(conn->dialect); | |
1229 | /* Not setting conn guid rsp->ServerGUID, as it | |
1230 | * not used by client for identifying server | |
1231 | */ | |
1232 | memset(rsp->ServerGUID, 0, SMB2_CLIENT_GUID_SIZE); | |
1233 | ||
1234 | rsp->SystemTime = cpu_to_le64(ksmbd_systime()); | |
1235 | rsp->ServerStartTime = 0; | |
1236 | ksmbd_debug(SMB, "negotiate context offset %d, count %d\n", | |
070fb21e NJ |
1237 | le32_to_cpu(rsp->NegotiateContextOffset), |
1238 | le16_to_cpu(rsp->NegotiateContextCount)); | |
e2f34481 NJ |
1239 | |
1240 | rsp->SecurityBufferOffset = cpu_to_le16(128); | |
1241 | rsp->SecurityBufferLength = cpu_to_le16(AUTH_GSS_LENGTH); | |
cb451720 NJ |
1242 | ksmbd_copy_gss_neg_header((char *)(&rsp->hdr) + |
1243 | le16_to_cpu(rsp->SecurityBufferOffset)); | |
e2b76ab8 | 1244 | |
e2f34481 NJ |
1245 | rsp->SecurityMode = SMB2_NEGOTIATE_SIGNING_ENABLED_LE; |
1246 | conn->use_spnego = true; | |
1247 | ||
1248 | if ((server_conf.signing == KSMBD_CONFIG_OPT_AUTO || | |
64b39f4a NJ |
1249 | server_conf.signing == KSMBD_CONFIG_OPT_DISABLED) && |
1250 | req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED_LE) | |
e2f34481 NJ |
1251 | conn->sign = true; |
1252 | else if (server_conf.signing == KSMBD_CONFIG_OPT_MANDATORY) { | |
1253 | server_conf.enforced_signing = true; | |
1254 | rsp->SecurityMode |= SMB2_NEGOTIATE_SIGNING_REQUIRED_LE; | |
1255 | conn->sign = true; | |
1256 | } | |
1257 | ||
1258 | conn->srv_sec_mode = le16_to_cpu(rsp->SecurityMode); | |
f5c779b7 | 1259 | ksmbd_conn_set_need_negotiate(conn); |
e2f34481 NJ |
1260 | |
1261 | err_out: | |
e2b76ab8 NJ |
1262 | if (rc) |
1263 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; | |
1264 | ||
1265 | if (!rc) | |
1266 | rc = ksmbd_iov_pin_rsp(work, rsp, | |
1267 | sizeof(struct smb2_negotiate_rsp) + | |
1268 | AUTH_GSS_LENGTH + neg_ctxt_len); | |
e2f34481 NJ |
1269 | if (rc < 0) |
1270 | smb2_set_err_rsp(work); | |
e2f34481 NJ |
1271 | return rc; |
1272 | } | |
1273 | ||
1274 | static int alloc_preauth_hash(struct ksmbd_session *sess, | |
070fb21e | 1275 | struct ksmbd_conn *conn) |
e2f34481 NJ |
1276 | { |
1277 | if (sess->Preauth_HashValue) | |
1278 | return 0; | |
1279 | ||
86f52978 | 1280 | sess->Preauth_HashValue = kmemdup(conn->preauth_info->Preauth_HashValue, |
070fb21e | 1281 | PREAUTH_HASHVALUE_SIZE, GFP_KERNEL); |
e2f34481 NJ |
1282 | if (!sess->Preauth_HashValue) |
1283 | return -ENOMEM; | |
1284 | ||
e2f34481 NJ |
1285 | return 0; |
1286 | } | |
1287 | ||
1288 | static int generate_preauth_hash(struct ksmbd_work *work) | |
1289 | { | |
1290 | struct ksmbd_conn *conn = work->conn; | |
1291 | struct ksmbd_session *sess = work->sess; | |
f5a544e3 | 1292 | u8 *preauth_hash; |
e2f34481 NJ |
1293 | |
1294 | if (conn->dialect != SMB311_PROT_ID) | |
1295 | return 0; | |
1296 | ||
f5a544e3 NJ |
1297 | if (conn->binding) { |
1298 | struct preauth_session *preauth_sess; | |
1299 | ||
1300 | preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); | |
1301 | if (!preauth_sess) { | |
1302 | preauth_sess = ksmbd_preauth_session_alloc(conn, sess->id); | |
1303 | if (!preauth_sess) | |
1304 | return -ENOMEM; | |
1305 | } | |
1306 | ||
1307 | preauth_hash = preauth_sess->Preauth_HashValue; | |
1308 | } else { | |
1309 | if (!sess->Preauth_HashValue) | |
1310 | if (alloc_preauth_hash(sess, conn)) | |
1311 | return -ENOMEM; | |
1312 | preauth_hash = sess->Preauth_HashValue; | |
e2f34481 NJ |
1313 | } |
1314 | ||
f5a544e3 | 1315 | ksmbd_gen_preauth_integrity_hash(conn, work->request_buf, preauth_hash); |
e2f34481 NJ |
1316 | return 0; |
1317 | } | |
1318 | ||
0d994cd4 MM |
1319 | static int decode_negotiation_token(struct ksmbd_conn *conn, |
1320 | struct negotiate_message *negblob, | |
1321 | size_t sz) | |
e2f34481 | 1322 | { |
e2f34481 NJ |
1323 | if (!conn->use_spnego) |
1324 | return -EINVAL; | |
1325 | ||
fad4161b HL |
1326 | if (ksmbd_decode_negTokenInit((char *)negblob, sz, conn)) { |
1327 | if (ksmbd_decode_negTokenTarg((char *)negblob, sz, conn)) { | |
e2f34481 NJ |
1328 | conn->auth_mechs |= KSMBD_AUTH_NTLMSSP; |
1329 | conn->preferred_auth_mech = KSMBD_AUTH_NTLMSSP; | |
1330 | conn->use_spnego = false; | |
1331 | } | |
1332 | } | |
1333 | return 0; | |
1334 | } | |
1335 | ||
1336 | static int ntlm_negotiate(struct ksmbd_work *work, | |
0d994cd4 | 1337 | struct negotiate_message *negblob, |
98422bdd | 1338 | size_t negblob_len, struct smb2_sess_setup_rsp *rsp) |
e2f34481 | 1339 | { |
e2f34481 NJ |
1340 | struct challenge_message *chgblob; |
1341 | unsigned char *spnego_blob = NULL; | |
1342 | u16 spnego_blob_len; | |
1343 | char *neg_blob; | |
1344 | int sz, rc; | |
1345 | ||
1346 | ksmbd_debug(SMB, "negotiate phase\n"); | |
ce53d365 | 1347 | rc = ksmbd_decode_ntlmssp_neg_blob(negblob, negblob_len, work->conn); |
e2f34481 NJ |
1348 | if (rc) |
1349 | return rc; | |
1350 | ||
1351 | sz = le16_to_cpu(rsp->SecurityBufferOffset); | |
1352 | chgblob = | |
1353 | (struct challenge_message *)((char *)&rsp->hdr.ProtocolId + sz); | |
1354 | memset(chgblob, 0, sizeof(struct challenge_message)); | |
1355 | ||
1356 | if (!work->conn->use_spnego) { | |
ce53d365 | 1357 | sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->conn); |
e2f34481 NJ |
1358 | if (sz < 0) |
1359 | return -ENOMEM; | |
1360 | ||
1361 | rsp->SecurityBufferLength = cpu_to_le16(sz); | |
1362 | return 0; | |
1363 | } | |
1364 | ||
1365 | sz = sizeof(struct challenge_message); | |
1366 | sz += (strlen(ksmbd_netbios_name()) * 2 + 1 + 4) * 6; | |
1367 | ||
1368 | neg_blob = kzalloc(sz, GFP_KERNEL); | |
1369 | if (!neg_blob) | |
1370 | return -ENOMEM; | |
1371 | ||
1372 | chgblob = (struct challenge_message *)neg_blob; | |
ce53d365 | 1373 | sz = ksmbd_build_ntlmssp_challenge_blob(chgblob, work->conn); |
e2f34481 NJ |
1374 | if (sz < 0) { |
1375 | rc = -ENOMEM; | |
1376 | goto out; | |
1377 | } | |
1378 | ||
070fb21e NJ |
1379 | rc = build_spnego_ntlmssp_neg_blob(&spnego_blob, &spnego_blob_len, |
1380 | neg_blob, sz); | |
e2f34481 NJ |
1381 | if (rc) { |
1382 | rc = -ENOMEM; | |
1383 | goto out; | |
1384 | } | |
1385 | ||
1386 | sz = le16_to_cpu(rsp->SecurityBufferOffset); | |
1387 | memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len); | |
1388 | rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len); | |
1389 | ||
1390 | out: | |
1391 | kfree(spnego_blob); | |
1392 | kfree(neg_blob); | |
1393 | return rc; | |
1394 | } | |
1395 | ||
1396 | static struct authenticate_message *user_authblob(struct ksmbd_conn *conn, | |
070fb21e | 1397 | struct smb2_sess_setup_req *req) |
e2f34481 NJ |
1398 | { |
1399 | int sz; | |
1400 | ||
1401 | if (conn->use_spnego && conn->mechToken) | |
1402 | return (struct authenticate_message *)conn->mechToken; | |
1403 | ||
1404 | sz = le16_to_cpu(req->SecurityBufferOffset); | |
1405 | return (struct authenticate_message *)((char *)&req->hdr.ProtocolId | |
1406 | + sz); | |
1407 | } | |
1408 | ||
1409 | static struct ksmbd_user *session_user(struct ksmbd_conn *conn, | |
070fb21e | 1410 | struct smb2_sess_setup_req *req) |
e2f34481 NJ |
1411 | { |
1412 | struct authenticate_message *authblob; | |
1413 | struct ksmbd_user *user; | |
1414 | char *name; | |
f0a96d1a | 1415 | unsigned int name_off, name_len, secbuf_len; |
e2f34481 | 1416 | |
92e47016 NJ |
1417 | if (conn->use_spnego && conn->mechToken) |
1418 | secbuf_len = conn->mechTokenLen; | |
1419 | else | |
1420 | secbuf_len = le16_to_cpu(req->SecurityBufferLength); | |
0d994cd4 MM |
1421 | if (secbuf_len < sizeof(struct authenticate_message)) { |
1422 | ksmbd_debug(SMB, "blob len %d too small\n", secbuf_len); | |
1423 | return NULL; | |
1424 | } | |
e2f34481 | 1425 | authblob = user_authblob(conn, req); |
0d994cd4 MM |
1426 | name_off = le32_to_cpu(authblob->UserName.BufferOffset); |
1427 | name_len = le16_to_cpu(authblob->UserName.Length); | |
0d994cd4 | 1428 | |
f0a96d1a | 1429 | if (secbuf_len < (u64)name_off + name_len) |
0d994cd4 MM |
1430 | return NULL; |
1431 | ||
1432 | name = smb_strndup_from_utf16((const char *)authblob + name_off, | |
1433 | name_len, | |
e2f34481 NJ |
1434 | true, |
1435 | conn->local_nls); | |
1436 | if (IS_ERR(name)) { | |
bde1694a | 1437 | pr_err("cannot allocate memory\n"); |
e2f34481 NJ |
1438 | return NULL; |
1439 | } | |
1440 | ||
1441 | ksmbd_debug(SMB, "session setup request for user %s\n", name); | |
1442 | user = ksmbd_login_user(name); | |
1443 | kfree(name); | |
1444 | return user; | |
1445 | } | |
1446 | ||
98422bdd NJ |
1447 | static int ntlm_authenticate(struct ksmbd_work *work, |
1448 | struct smb2_sess_setup_req *req, | |
1449 | struct smb2_sess_setup_rsp *rsp) | |
e2f34481 | 1450 | { |
e2f34481 NJ |
1451 | struct ksmbd_conn *conn = work->conn; |
1452 | struct ksmbd_session *sess = work->sess; | |
1453 | struct channel *chann = NULL; | |
1454 | struct ksmbd_user *user; | |
64b39f4a | 1455 | u64 prev_id; |
e2f34481 NJ |
1456 | int sz, rc; |
1457 | ||
1458 | ksmbd_debug(SMB, "authenticate phase\n"); | |
1459 | if (conn->use_spnego) { | |
1460 | unsigned char *spnego_blob; | |
1461 | u16 spnego_blob_len; | |
1462 | ||
1463 | rc = build_spnego_ntlmssp_auth_blob(&spnego_blob, | |
1464 | &spnego_blob_len, | |
1465 | 0); | |
1466 | if (rc) | |
1467 | return -ENOMEM; | |
1468 | ||
1469 | sz = le16_to_cpu(rsp->SecurityBufferOffset); | |
64b39f4a | 1470 | memcpy((char *)&rsp->hdr.ProtocolId + sz, spnego_blob, spnego_blob_len); |
e2f34481 NJ |
1471 | rsp->SecurityBufferLength = cpu_to_le16(spnego_blob_len); |
1472 | kfree(spnego_blob); | |
e2f34481 NJ |
1473 | } |
1474 | ||
1475 | user = session_user(conn, req); | |
1476 | if (!user) { | |
1477 | ksmbd_debug(SMB, "Unknown user name or an error\n"); | |
58090b17 | 1478 | return -EPERM; |
e2f34481 NJ |
1479 | } |
1480 | ||
1481 | /* Check for previous session */ | |
1482 | prev_id = le64_to_cpu(req->PreviousSessionId); | |
1483 | if (prev_id && prev_id != sess->id) | |
e4d3e6b5 | 1484 | destroy_previous_session(conn, user, prev_id); |
e2f34481 NJ |
1485 | |
1486 | if (sess->state == SMB2_SESSION_VALID) { | |
1487 | /* | |
1488 | * Reuse session if anonymous try to connect | |
1489 | * on reauthetication. | |
1490 | */ | |
3353ab2d | 1491 | if (conn->binding == false && ksmbd_anonymous_user(user)) { |
e2f34481 NJ |
1492 | ksmbd_free_user(user); |
1493 | return 0; | |
1494 | } | |
a58b45a4 NJ |
1495 | |
1496 | if (!ksmbd_compare_user(sess->user, user)) { | |
1497 | ksmbd_free_user(user); | |
1498 | return -EPERM; | |
1499 | } | |
1500 | ksmbd_free_user(user); | |
1501 | } else { | |
1502 | sess->user = user; | |
e2f34481 NJ |
1503 | } |
1504 | ||
3353ab2d | 1505 | if (conn->binding == false && user_guest(sess->user)) { |
e2f34481 NJ |
1506 | rsp->SessionFlags = SMB2_SESSION_FLAG_IS_GUEST_LE; |
1507 | } else { | |
1508 | struct authenticate_message *authblob; | |
1509 | ||
1510 | authblob = user_authblob(conn, req); | |
92e47016 NJ |
1511 | if (conn->use_spnego && conn->mechToken) |
1512 | sz = conn->mechTokenLen; | |
1513 | else | |
1514 | sz = le16_to_cpu(req->SecurityBufferLength); | |
ce53d365 | 1515 | rc = ksmbd_decode_ntlmssp_auth_blob(authblob, sz, conn, sess); |
e2f34481 NJ |
1516 | if (rc) { |
1517 | set_user_flag(sess->user, KSMBD_USER_FLAG_BAD_PASSWORD); | |
1518 | ksmbd_debug(SMB, "authentication failed\n"); | |
58090b17 | 1519 | return -EPERM; |
e2f34481 | 1520 | } |
ac090d9c | 1521 | } |
e2f34481 | 1522 | |
ac090d9c NJ |
1523 | /* |
1524 | * If session state is SMB2_SESSION_VALID, We can assume | |
1525 | * that it is reauthentication. And the user/password | |
1526 | * has been verified, so return it here. | |
1527 | */ | |
1528 | if (sess->state == SMB2_SESSION_VALID) { | |
1529 | if (conn->binding) | |
1530 | goto binding_session; | |
1531 | return 0; | |
1532 | } | |
e2f34481 | 1533 | |
ac090d9c NJ |
1534 | if ((rsp->SessionFlags != SMB2_SESSION_FLAG_IS_GUEST_LE && |
1535 | (conn->sign || server_conf.enforced_signing)) || | |
1536 | (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) | |
1537 | sess->sign = true; | |
1538 | ||
1539 | if (smb3_encryption_negotiated(conn) && | |
1540 | !(req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { | |
af7c39d9 | 1541 | rc = conn->ops->generate_encryptionkey(conn, sess); |
ac090d9c NJ |
1542 | if (rc) { |
1543 | ksmbd_debug(SMB, | |
1544 | "SMB3 encryption key generation failed\n"); | |
1545 | return -EINVAL; | |
e2f34481 | 1546 | } |
ac090d9c | 1547 | sess->enc = true; |
37ba7b00 NJ |
1548 | if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION) |
1549 | rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE; | |
ac090d9c NJ |
1550 | /* |
1551 | * signing is disable if encryption is enable | |
1552 | * on this session | |
1553 | */ | |
1554 | sess->sign = false; | |
e2f34481 NJ |
1555 | } |
1556 | ||
f5a544e3 | 1557 | binding_session: |
e2f34481 | 1558 | if (conn->dialect >= SMB30_PROT_ID) { |
f5a544e3 | 1559 | chann = lookup_chann_list(sess, conn); |
e2f34481 NJ |
1560 | if (!chann) { |
1561 | chann = kmalloc(sizeof(struct channel), GFP_KERNEL); | |
1562 | if (!chann) | |
1563 | return -ENOMEM; | |
1564 | ||
1565 | chann->conn = conn; | |
1d9c4172 | 1566 | xa_store(&sess->ksmbd_chann_list, (long)conn, chann, GFP_KERNEL); |
e2f34481 NJ |
1567 | } |
1568 | } | |
1569 | ||
1570 | if (conn->ops->generate_signingkey) { | |
f5a544e3 | 1571 | rc = conn->ops->generate_signingkey(sess, conn); |
e2f34481 | 1572 | if (rc) { |
64b39f4a | 1573 | ksmbd_debug(SMB, "SMB3 signing key generation failed\n"); |
58090b17 | 1574 | return -EINVAL; |
e2f34481 NJ |
1575 | } |
1576 | } | |
1577 | ||
51a13873 NJ |
1578 | if (!ksmbd_conn_lookup_dialect(conn)) { |
1579 | pr_err("fail to verify the dialect\n"); | |
1580 | return -ENOENT; | |
e2f34481 NJ |
1581 | } |
1582 | return 0; | |
1583 | } | |
1584 | ||
1585 | #ifdef CONFIG_SMB_SERVER_KERBEROS5 | |
98422bdd NJ |
1586 | static int krb5_authenticate(struct ksmbd_work *work, |
1587 | struct smb2_sess_setup_req *req, | |
1588 | struct smb2_sess_setup_rsp *rsp) | |
e2f34481 | 1589 | { |
e2f34481 NJ |
1590 | struct ksmbd_conn *conn = work->conn; |
1591 | struct ksmbd_session *sess = work->sess; | |
1592 | char *in_blob, *out_blob; | |
1593 | struct channel *chann = NULL; | |
64b39f4a | 1594 | u64 prev_sess_id; |
e2f34481 NJ |
1595 | int in_len, out_len; |
1596 | int retval; | |
1597 | ||
1598 | in_blob = (char *)&req->hdr.ProtocolId + | |
1599 | le16_to_cpu(req->SecurityBufferOffset); | |
1600 | in_len = le16_to_cpu(req->SecurityBufferLength); | |
1601 | out_blob = (char *)&rsp->hdr.ProtocolId + | |
1602 | le16_to_cpu(rsp->SecurityBufferOffset); | |
1603 | out_len = work->response_sz - | |
cb451720 | 1604 | (le16_to_cpu(rsp->SecurityBufferOffset) + 4); |
e2f34481 NJ |
1605 | |
1606 | /* Check previous session */ | |
1607 | prev_sess_id = le64_to_cpu(req->PreviousSessionId); | |
1608 | if (prev_sess_id && prev_sess_id != sess->id) | |
e4d3e6b5 | 1609 | destroy_previous_session(conn, sess->user, prev_sess_id); |
e2f34481 NJ |
1610 | |
1611 | if (sess->state == SMB2_SESSION_VALID) | |
1612 | ksmbd_free_user(sess->user); | |
1613 | ||
1614 | retval = ksmbd_krb5_authenticate(sess, in_blob, in_len, | |
070fb21e | 1615 | out_blob, &out_len); |
e2f34481 NJ |
1616 | if (retval) { |
1617 | ksmbd_debug(SMB, "krb5 authentication failed\n"); | |
58090b17 | 1618 | return -EINVAL; |
e2f34481 NJ |
1619 | } |
1620 | rsp->SecurityBufferLength = cpu_to_le16(out_len); | |
e2f34481 NJ |
1621 | |
1622 | if ((conn->sign || server_conf.enforced_signing) || | |
64b39f4a | 1623 | (req->SecurityMode & SMB2_NEGOTIATE_SIGNING_REQUIRED)) |
e2f34481 NJ |
1624 | sess->sign = true; |
1625 | ||
83912d6d | 1626 | if (smb3_encryption_negotiated(conn)) { |
af7c39d9 | 1627 | retval = conn->ops->generate_encryptionkey(conn, sess); |
e2f34481 NJ |
1628 | if (retval) { |
1629 | ksmbd_debug(SMB, | |
070fb21e | 1630 | "SMB3 encryption key generation failed\n"); |
58090b17 | 1631 | return -EINVAL; |
e2f34481 NJ |
1632 | } |
1633 | sess->enc = true; | |
37ba7b00 NJ |
1634 | if (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB2_ENCRYPTION) |
1635 | rsp->SessionFlags = SMB2_SESSION_FLAG_ENCRYPT_DATA_LE; | |
e2f34481 NJ |
1636 | sess->sign = false; |
1637 | } | |
1638 | ||
1639 | if (conn->dialect >= SMB30_PROT_ID) { | |
f5a544e3 | 1640 | chann = lookup_chann_list(sess, conn); |
e2f34481 NJ |
1641 | if (!chann) { |
1642 | chann = kmalloc(sizeof(struct channel), GFP_KERNEL); | |
1643 | if (!chann) | |
1644 | return -ENOMEM; | |
1645 | ||
1646 | chann->conn = conn; | |
1d9c4172 | 1647 | xa_store(&sess->ksmbd_chann_list, (long)conn, chann, GFP_KERNEL); |
e2f34481 NJ |
1648 | } |
1649 | } | |
1650 | ||
1651 | if (conn->ops->generate_signingkey) { | |
f5a544e3 | 1652 | retval = conn->ops->generate_signingkey(sess, conn); |
e2f34481 | 1653 | if (retval) { |
64b39f4a | 1654 | ksmbd_debug(SMB, "SMB3 signing key generation failed\n"); |
58090b17 | 1655 | return -EINVAL; |
e2f34481 NJ |
1656 | } |
1657 | } | |
1658 | ||
51a13873 NJ |
1659 | if (!ksmbd_conn_lookup_dialect(conn)) { |
1660 | pr_err("fail to verify the dialect\n"); | |
1661 | return -ENOENT; | |
e2f34481 NJ |
1662 | } |
1663 | return 0; | |
1664 | } | |
1665 | #else | |
98422bdd NJ |
1666 | static int krb5_authenticate(struct ksmbd_work *work, |
1667 | struct smb2_sess_setup_req *req, | |
1668 | struct smb2_sess_setup_rsp *rsp) | |
e2f34481 NJ |
1669 | { |
1670 | return -EOPNOTSUPP; | |
1671 | } | |
1672 | #endif | |
1673 | ||
1674 | int smb2_sess_setup(struct ksmbd_work *work) | |
1675 | { | |
1676 | struct ksmbd_conn *conn = work->conn; | |
98422bdd NJ |
1677 | struct smb2_sess_setup_req *req; |
1678 | struct smb2_sess_setup_rsp *rsp; | |
e2f34481 NJ |
1679 | struct ksmbd_session *sess; |
1680 | struct negotiate_message *negblob; | |
0d994cd4 | 1681 | unsigned int negblob_len, negblob_off; |
e2f34481 NJ |
1682 | int rc = 0; |
1683 | ||
1684 | ksmbd_debug(SMB, "Received request for session setup\n"); | |
1685 | ||
98422bdd NJ |
1686 | WORK_BUFFERS(work, req, rsp); |
1687 | ||
e2f34481 NJ |
1688 | rsp->StructureSize = cpu_to_le16(9); |
1689 | rsp->SessionFlags = 0; | |
1690 | rsp->SecurityBufferOffset = cpu_to_le16(72); | |
1691 | rsp->SecurityBufferLength = 0; | |
e2f34481 | 1692 | |
f5c779b7 | 1693 | ksmbd_conn_lock(conn); |
e2f34481 NJ |
1694 | if (!req->hdr.SessionId) { |
1695 | sess = ksmbd_smb2_session_create(); | |
1696 | if (!sess) { | |
1697 | rc = -ENOMEM; | |
1698 | goto out_err; | |
1699 | } | |
1700 | rsp->hdr.SessionId = cpu_to_le64(sess->id); | |
e4d3e6b5 NJ |
1701 | rc = ksmbd_session_register(conn, sess); |
1702 | if (rc) | |
1703 | goto out_err; | |
f5a544e3 NJ |
1704 | } else if (conn->dialect >= SMB30_PROT_ID && |
1705 | (server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) && | |
1706 | req->Flags & SMB2_SESSION_REQ_FLAG_BINDING) { | |
1707 | u64 sess_id = le64_to_cpu(req->hdr.SessionId); | |
1708 | ||
1709 | sess = ksmbd_session_lookup_slowpath(sess_id); | |
1710 | if (!sess) { | |
1711 | rc = -ENOENT; | |
1712 | goto out_err; | |
1713 | } | |
1714 | ||
af7c39d9 | 1715 | if (conn->dialect != sess->dialect) { |
f5a544e3 NJ |
1716 | rc = -EINVAL; |
1717 | goto out_err; | |
1718 | } | |
1719 | ||
1720 | if (!(req->hdr.Flags & SMB2_FLAGS_SIGNED)) { | |
1721 | rc = -EINVAL; | |
1722 | goto out_err; | |
1723 | } | |
1724 | ||
af7c39d9 | 1725 | if (strncmp(conn->ClientGUID, sess->ClientGUID, |
f5a544e3 NJ |
1726 | SMB2_CLIENT_GUID_SIZE)) { |
1727 | rc = -ENOENT; | |
1728 | goto out_err; | |
1729 | } | |
1730 | ||
1731 | if (sess->state == SMB2_SESSION_IN_PROGRESS) { | |
1732 | rc = -EACCES; | |
1733 | goto out_err; | |
1734 | } | |
1735 | ||
1736 | if (sess->state == SMB2_SESSION_EXPIRED) { | |
1737 | rc = -EFAULT; | |
1738 | goto out_err; | |
1739 | } | |
1740 | ||
f5c779b7 NJ |
1741 | if (ksmbd_conn_need_reconnect(conn)) { |
1742 | rc = -EFAULT; | |
1743 | sess = NULL; | |
1744 | goto out_err; | |
1745 | } | |
1746 | ||
f5a544e3 NJ |
1747 | if (ksmbd_session_lookup(conn, sess_id)) { |
1748 | rc = -EACCES; | |
1749 | goto out_err; | |
1750 | } | |
1751 | ||
3353ab2d NJ |
1752 | if (user_guest(sess->user)) { |
1753 | rc = -EOPNOTSUPP; | |
1754 | goto out_err; | |
1755 | } | |
1756 | ||
f5a544e3 NJ |
1757 | conn->binding = true; |
1758 | } else if ((conn->dialect < SMB30_PROT_ID || | |
1759 | server_conf.flags & KSMBD_GLOBAL_FLAG_SMB3_MULTICHANNEL) && | |
1760 | (req->Flags & SMB2_SESSION_REQ_FLAG_BINDING)) { | |
4951a84f | 1761 | sess = NULL; |
f5a544e3 NJ |
1762 | rc = -EACCES; |
1763 | goto out_err; | |
e2f34481 NJ |
1764 | } else { |
1765 | sess = ksmbd_session_lookup(conn, | |
070fb21e | 1766 | le64_to_cpu(req->hdr.SessionId)); |
e2f34481 NJ |
1767 | if (!sess) { |
1768 | rc = -ENOENT; | |
e2f34481 NJ |
1769 | goto out_err; |
1770 | } | |
f5c779b7 NJ |
1771 | |
1772 | if (sess->state == SMB2_SESSION_EXPIRED) { | |
1773 | rc = -EFAULT; | |
1774 | goto out_err; | |
1775 | } | |
1776 | ||
1777 | if (ksmbd_conn_need_reconnect(conn)) { | |
1778 | rc = -EFAULT; | |
1779 | sess = NULL; | |
1780 | goto out_err; | |
1781 | } | |
e2f34481 NJ |
1782 | } |
1783 | work->sess = sess; | |
1784 | ||
0d994cd4 MM |
1785 | negblob_off = le16_to_cpu(req->SecurityBufferOffset); |
1786 | negblob_len = le16_to_cpu(req->SecurityBufferLength); | |
92e47016 | 1787 | if (negblob_off < offsetof(struct smb2_sess_setup_req, Buffer)) { |
f8fbfd85 CJ |
1788 | rc = -EINVAL; |
1789 | goto out_err; | |
1790 | } | |
0d994cd4 | 1791 | |
e2f34481 | 1792 | negblob = (struct negotiate_message *)((char *)&req->hdr.ProtocolId + |
0d994cd4 | 1793 | negblob_off); |
e2f34481 | 1794 | |
0d994cd4 | 1795 | if (decode_negotiation_token(conn, negblob, negblob_len) == 0) { |
92e47016 | 1796 | if (conn->mechToken) { |
e2f34481 | 1797 | negblob = (struct negotiate_message *)conn->mechToken; |
92e47016 NJ |
1798 | negblob_len = conn->mechTokenLen; |
1799 | } | |
1800 | } | |
1801 | ||
1802 | if (negblob_len < offsetof(struct negotiate_message, NegotiateFlags)) { | |
1803 | rc = -EINVAL; | |
1804 | goto out_err; | |
e2f34481 NJ |
1805 | } |
1806 | ||
1807 | if (server_conf.auth_mechs & conn->auth_mechs) { | |
f5a544e3 NJ |
1808 | rc = generate_preauth_hash(work); |
1809 | if (rc) | |
1810 | goto out_err; | |
1811 | ||
e2f34481 NJ |
1812 | if (conn->preferred_auth_mech & |
1813 | (KSMBD_AUTH_KRB5 | KSMBD_AUTH_MSKRB5)) { | |
98422bdd | 1814 | rc = krb5_authenticate(work, req, rsp); |
e2f34481 | 1815 | if (rc) { |
f5a544e3 | 1816 | rc = -EINVAL; |
e2f34481 NJ |
1817 | goto out_err; |
1818 | } | |
1819 | ||
f5c779b7 NJ |
1820 | if (!ksmbd_conn_need_reconnect(conn)) { |
1821 | ksmbd_conn_set_good(conn); | |
1822 | sess->state = SMB2_SESSION_VALID; | |
1823 | } | |
822bc8ea | 1824 | kfree(sess->Preauth_HashValue); |
e2f34481 NJ |
1825 | sess->Preauth_HashValue = NULL; |
1826 | } else if (conn->preferred_auth_mech == KSMBD_AUTH_NTLMSSP) { | |
e2f34481 | 1827 | if (negblob->MessageType == NtLmNegotiate) { |
98422bdd | 1828 | rc = ntlm_negotiate(work, negblob, negblob_len, rsp); |
e2f34481 NJ |
1829 | if (rc) |
1830 | goto out_err; | |
1831 | rsp->hdr.Status = | |
1832 | STATUS_MORE_PROCESSING_REQUIRED; | |
e2f34481 | 1833 | } else if (negblob->MessageType == NtLmAuthenticate) { |
98422bdd | 1834 | rc = ntlm_authenticate(work, req, rsp); |
e2f34481 NJ |
1835 | if (rc) |
1836 | goto out_err; | |
1837 | ||
f5c779b7 NJ |
1838 | if (!ksmbd_conn_need_reconnect(conn)) { |
1839 | ksmbd_conn_set_good(conn); | |
1840 | sess->state = SMB2_SESSION_VALID; | |
1841 | } | |
f5a544e3 NJ |
1842 | if (conn->binding) { |
1843 | struct preauth_session *preauth_sess; | |
1844 | ||
1845 | preauth_sess = | |
1846 | ksmbd_preauth_session_lookup(conn, sess->id); | |
1847 | if (preauth_sess) { | |
1848 | list_del(&preauth_sess->preauth_entry); | |
1849 | kfree(preauth_sess); | |
1850 | } | |
1851 | } | |
822bc8ea | 1852 | kfree(sess->Preauth_HashValue); |
e2f34481 | 1853 | sess->Preauth_HashValue = NULL; |
6d7cb549 NJ |
1854 | } else { |
1855 | pr_info_ratelimited("Unknown NTLMSSP message type : 0x%x\n", | |
1856 | le32_to_cpu(negblob->MessageType)); | |
1857 | rc = -EINVAL; | |
e2f34481 NJ |
1858 | } |
1859 | } else { | |
1860 | /* TODO: need one more negotiation */ | |
bde1694a | 1861 | pr_err("Not support the preferred authentication\n"); |
e2f34481 | 1862 | rc = -EINVAL; |
e2f34481 NJ |
1863 | } |
1864 | } else { | |
bde1694a | 1865 | pr_err("Not support authentication\n"); |
e2f34481 | 1866 | rc = -EINVAL; |
e2f34481 NJ |
1867 | } |
1868 | ||
1869 | out_err: | |
f5a544e3 NJ |
1870 | if (rc == -EINVAL) |
1871 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
1872 | else if (rc == -ENOENT) | |
1873 | rsp->hdr.Status = STATUS_USER_SESSION_DELETED; | |
1874 | else if (rc == -EACCES) | |
1875 | rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; | |
1876 | else if (rc == -EFAULT) | |
1877 | rsp->hdr.Status = STATUS_NETWORK_SESSION_EXPIRED; | |
58090b17 NJ |
1878 | else if (rc == -ENOMEM) |
1879 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; | |
3353ab2d NJ |
1880 | else if (rc == -EOPNOTSUPP) |
1881 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; | |
f5a544e3 NJ |
1882 | else if (rc) |
1883 | rsp->hdr.Status = STATUS_LOGON_FAILURE; | |
1884 | ||
e2f34481 NJ |
1885 | if (conn->use_spnego && conn->mechToken) { |
1886 | kfree(conn->mechToken); | |
1887 | conn->mechToken = NULL; | |
1888 | } | |
1889 | ||
621be84a NJ |
1890 | if (rc < 0) { |
1891 | /* | |
1892 | * SecurityBufferOffset should be set to zero | |
1893 | * in session setup error response. | |
1894 | */ | |
1895 | rsp->SecurityBufferOffset = 0; | |
1896 | ||
1897 | if (sess) { | |
1898 | bool try_delay = false; | |
1899 | ||
1900 | /* | |
1901 | * To avoid dictionary attacks (repeated session setups rapidly sent) to | |
1902 | * connect to server, ksmbd make a delay of a 5 seconds on session setup | |
1903 | * failure to make it harder to send enough random connection requests | |
1904 | * to break into a server. | |
1905 | */ | |
1906 | if (sess->user && sess->user->flags & KSMBD_USER_FLAG_DELAY_SESSION) | |
1907 | try_delay = true; | |
1908 | ||
ea174a91 | 1909 | sess->last_active = jiffies; |
f5c779b7 | 1910 | sess->state = SMB2_SESSION_EXPIRED; |
b096d97f NJ |
1911 | if (try_delay) { |
1912 | ksmbd_conn_set_need_reconnect(conn); | |
621be84a | 1913 | ssleep(5); |
b096d97f NJ |
1914 | ksmbd_conn_set_need_negotiate(conn); |
1915 | } | |
621be84a | 1916 | } |
0e2378ea | 1917 | smb2_set_err_rsp(work); |
e2b76ab8 NJ |
1918 | } else { |
1919 | unsigned int iov_len; | |
1920 | ||
1921 | if (rsp->SecurityBufferLength) | |
1922 | iov_len = offsetof(struct smb2_sess_setup_rsp, Buffer) + | |
1923 | le16_to_cpu(rsp->SecurityBufferLength); | |
1924 | else | |
1925 | iov_len = sizeof(struct smb2_sess_setup_rsp); | |
1926 | rc = ksmbd_iov_pin_rsp(work, rsp, iov_len); | |
1927 | if (rc) | |
1928 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; | |
e2f34481 NJ |
1929 | } |
1930 | ||
f5c779b7 | 1931 | ksmbd_conn_unlock(conn); |
e2f34481 NJ |
1932 | return rc; |
1933 | } | |
1934 | ||
1935 | /** | |
1936 | * smb2_tree_connect() - handler for smb2 tree connect command | |
1937 | * @work: smb work containing smb request buffer | |
1938 | * | |
1939 | * Return: 0 on success, otherwise error | |
1940 | */ | |
1941 | int smb2_tree_connect(struct ksmbd_work *work) | |
1942 | { | |
1943 | struct ksmbd_conn *conn = work->conn; | |
7b7d709e NJ |
1944 | struct smb2_tree_connect_req *req; |
1945 | struct smb2_tree_connect_rsp *rsp; | |
e2f34481 NJ |
1946 | struct ksmbd_session *sess = work->sess; |
1947 | char *treename = NULL, *name = NULL; | |
1948 | struct ksmbd_tree_conn_status status; | |
1949 | struct ksmbd_share_config *share; | |
1950 | int rc = -EINVAL; | |
1951 | ||
7b7d709e NJ |
1952 | WORK_BUFFERS(work, req, rsp); |
1953 | ||
e2f34481 | 1954 | treename = smb_strndup_from_utf16(req->Buffer, |
070fb21e NJ |
1955 | le16_to_cpu(req->PathLength), true, |
1956 | conn->local_nls); | |
e2f34481 | 1957 | if (IS_ERR(treename)) { |
bde1694a | 1958 | pr_err("treename is NULL\n"); |
e2f34481 NJ |
1959 | status.ret = KSMBD_TREE_CONN_STATUS_ERROR; |
1960 | goto out_err1; | |
1961 | } | |
1962 | ||
16b5f54e | 1963 | name = ksmbd_extract_sharename(conn->um, treename); |
e2f34481 NJ |
1964 | if (IS_ERR(name)) { |
1965 | status.ret = KSMBD_TREE_CONN_STATUS_ERROR; | |
1966 | goto out_err1; | |
1967 | } | |
1968 | ||
1969 | ksmbd_debug(SMB, "tree connect request for tree %s treename %s\n", | |
070fb21e | 1970 | name, treename); |
e2f34481 | 1971 | |
af7c39d9 | 1972 | status = ksmbd_tree_conn_connect(conn, sess, name); |
e2f34481 NJ |
1973 | if (status.ret == KSMBD_TREE_CONN_STATUS_OK) |
1974 | rsp->hdr.Id.SyncId.TreeId = cpu_to_le32(status.tree_conn->id); | |
1975 | else | |
1976 | goto out_err1; | |
1977 | ||
1978 | share = status.tree_conn->share_conf; | |
1979 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { | |
1980 | ksmbd_debug(SMB, "IPC share path request\n"); | |
1981 | rsp->ShareType = SMB2_SHARE_TYPE_PIPE; | |
1982 | rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | | |
1983 | FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE | | |
1984 | FILE_DELETE_LE | FILE_READ_CONTROL_LE | | |
1985 | FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | | |
1986 | FILE_SYNCHRONIZE_LE; | |
1987 | } else { | |
1988 | rsp->ShareType = SMB2_SHARE_TYPE_DISK; | |
1989 | rsp->MaximalAccess = FILE_READ_DATA_LE | FILE_READ_EA_LE | | |
1990 | FILE_EXECUTE_LE | FILE_READ_ATTRIBUTES_LE; | |
1991 | if (test_tree_conn_flag(status.tree_conn, | |
1992 | KSMBD_TREE_CONN_FLAG_WRITABLE)) { | |
1993 | rsp->MaximalAccess |= FILE_WRITE_DATA_LE | | |
1994 | FILE_APPEND_DATA_LE | FILE_WRITE_EA_LE | | |
3aefd54d WJ |
1995 | FILE_DELETE_LE | FILE_WRITE_ATTRIBUTES_LE | |
1996 | FILE_DELETE_CHILD_LE | FILE_READ_CONTROL_LE | | |
1997 | FILE_WRITE_DAC_LE | FILE_WRITE_OWNER_LE | | |
1998 | FILE_SYNCHRONIZE_LE; | |
e2f34481 NJ |
1999 | } |
2000 | } | |
2001 | ||
2002 | status.tree_conn->maximal_access = le32_to_cpu(rsp->MaximalAccess); | |
2003 | if (conn->posix_ext_supported) | |
2004 | status.tree_conn->posix_extensions = true; | |
2005 | ||
33b235a6 NJ |
2006 | write_lock(&sess->tree_conns_lock); |
2007 | status.tree_conn->t_state = TREE_CONNECTED; | |
2008 | write_unlock(&sess->tree_conns_lock); | |
e2f34481 | 2009 | rsp->StructureSize = cpu_to_le16(16); |
cdfb2fef | 2010 | out_err1: |
e2f34481 NJ |
2011 | rsp->Capabilities = 0; |
2012 | rsp->Reserved = 0; | |
2013 | /* default manual caching */ | |
2014 | rsp->ShareFlags = SMB2_SHAREFLAG_MANUAL_CACHING; | |
e2f34481 | 2015 | |
e2b76ab8 NJ |
2016 | rc = ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_tree_connect_rsp)); |
2017 | if (rc) | |
2018 | status.ret = KSMBD_TREE_CONN_STATUS_NOMEM; | |
2019 | ||
e2f34481 NJ |
2020 | if (!IS_ERR(treename)) |
2021 | kfree(treename); | |
2022 | if (!IS_ERR(name)) | |
2023 | kfree(name); | |
2024 | ||
2025 | switch (status.ret) { | |
2026 | case KSMBD_TREE_CONN_STATUS_OK: | |
2027 | rsp->hdr.Status = STATUS_SUCCESS; | |
2028 | rc = 0; | |
2029 | break; | |
4963d74f | 2030 | case -ESTALE: |
fe54833d | 2031 | case -ENOENT: |
e2f34481 | 2032 | case KSMBD_TREE_CONN_STATUS_NO_SHARE: |
fe54833d | 2033 | rsp->hdr.Status = STATUS_BAD_NETWORK_NAME; |
e2f34481 NJ |
2034 | break; |
2035 | case -ENOMEM: | |
2036 | case KSMBD_TREE_CONN_STATUS_NOMEM: | |
2037 | rsp->hdr.Status = STATUS_NO_MEMORY; | |
2038 | break; | |
2039 | case KSMBD_TREE_CONN_STATUS_ERROR: | |
2040 | case KSMBD_TREE_CONN_STATUS_TOO_MANY_CONNS: | |
2041 | case KSMBD_TREE_CONN_STATUS_TOO_MANY_SESSIONS: | |
2042 | rsp->hdr.Status = STATUS_ACCESS_DENIED; | |
2043 | break; | |
2044 | case -EINVAL: | |
2045 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
2046 | break; | |
2047 | default: | |
2048 | rsp->hdr.Status = STATUS_ACCESS_DENIED; | |
2049 | } | |
2050 | ||
cdfb2fef MM |
2051 | if (status.ret != KSMBD_TREE_CONN_STATUS_OK) |
2052 | smb2_set_err_rsp(work); | |
2053 | ||
e2f34481 NJ |
2054 | return rc; |
2055 | } | |
2056 | ||
2057 | /** | |
2058 | * smb2_create_open_flags() - convert smb open flags to unix open flags | |
2059 | * @file_present: is file already present | |
2060 | * @access: file access flags | |
2061 | * @disposition: file disposition flags | |
6c5e36d1 | 2062 | * @may_flags: set with MAY_ flags |
e2f34481 NJ |
2063 | * |
2064 | * Return: file open flags | |
2065 | */ | |
2066 | static int smb2_create_open_flags(bool file_present, __le32 access, | |
6c5e36d1 HL |
2067 | __le32 disposition, |
2068 | int *may_flags) | |
e2f34481 NJ |
2069 | { |
2070 | int oflags = O_NONBLOCK | O_LARGEFILE; | |
2071 | ||
2072 | if (access & FILE_READ_DESIRED_ACCESS_LE && | |
6c5e36d1 | 2073 | access & FILE_WRITE_DESIRE_ACCESS_LE) { |
e2f34481 | 2074 | oflags |= O_RDWR; |
6c5e36d1 HL |
2075 | *may_flags = MAY_OPEN | MAY_READ | MAY_WRITE; |
2076 | } else if (access & FILE_WRITE_DESIRE_ACCESS_LE) { | |
e2f34481 | 2077 | oflags |= O_WRONLY; |
6c5e36d1 HL |
2078 | *may_flags = MAY_OPEN | MAY_WRITE; |
2079 | } else { | |
e2f34481 | 2080 | oflags |= O_RDONLY; |
6c5e36d1 HL |
2081 | *may_flags = MAY_OPEN | MAY_READ; |
2082 | } | |
e2f34481 NJ |
2083 | |
2084 | if (access == FILE_READ_ATTRIBUTES_LE) | |
2085 | oflags |= O_PATH; | |
2086 | ||
2087 | if (file_present) { | |
2088 | switch (disposition & FILE_CREATE_MASK_LE) { | |
2089 | case FILE_OPEN_LE: | |
2090 | case FILE_CREATE_LE: | |
2091 | break; | |
2092 | case FILE_SUPERSEDE_LE: | |
2093 | case FILE_OVERWRITE_LE: | |
2094 | case FILE_OVERWRITE_IF_LE: | |
2095 | oflags |= O_TRUNC; | |
2096 | break; | |
2097 | default: | |
2098 | break; | |
2099 | } | |
2100 | } else { | |
2101 | switch (disposition & FILE_CREATE_MASK_LE) { | |
2102 | case FILE_SUPERSEDE_LE: | |
2103 | case FILE_CREATE_LE: | |
2104 | case FILE_OPEN_IF_LE: | |
2105 | case FILE_OVERWRITE_IF_LE: | |
2106 | oflags |= O_CREAT; | |
2107 | break; | |
2108 | case FILE_OPEN_LE: | |
2109 | case FILE_OVERWRITE_LE: | |
2110 | oflags &= ~O_CREAT; | |
2111 | break; | |
2112 | default: | |
2113 | break; | |
2114 | } | |
2115 | } | |
6c5e36d1 | 2116 | |
e2f34481 NJ |
2117 | return oflags; |
2118 | } | |
2119 | ||
2120 | /** | |
2121 | * smb2_tree_disconnect() - handler for smb tree connect request | |
2122 | * @work: smb work containing request buffer | |
2123 | * | |
2124 | * Return: 0 | |
2125 | */ | |
2126 | int smb2_tree_disconnect(struct ksmbd_work *work) | |
2127 | { | |
7b7d709e NJ |
2128 | struct smb2_tree_disconnect_rsp *rsp; |
2129 | struct smb2_tree_disconnect_req *req; | |
e2f34481 NJ |
2130 | struct ksmbd_session *sess = work->sess; |
2131 | struct ksmbd_tree_connect *tcon = work->tcon; | |
e2b76ab8 | 2132 | int err; |
e2f34481 | 2133 | |
7b7d709e NJ |
2134 | WORK_BUFFERS(work, req, rsp); |
2135 | ||
e2f34481 NJ |
2136 | ksmbd_debug(SMB, "request\n"); |
2137 | ||
33b235a6 NJ |
2138 | if (!tcon) { |
2139 | ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); | |
2140 | ||
2141 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; | |
2142 | err = -ENOENT; | |
2143 | goto err_out; | |
2144 | } | |
2145 | ||
2146 | ksmbd_close_tree_conn_fds(work); | |
2147 | ||
2148 | write_lock(&sess->tree_conns_lock); | |
2149 | if (tcon->t_state == TREE_DISCONNECTED) { | |
2150 | write_unlock(&sess->tree_conns_lock); | |
2151 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; | |
2152 | err = -ENOENT; | |
2153 | goto err_out; | |
2154 | } | |
2155 | ||
2156 | WARN_ON_ONCE(atomic_dec_and_test(&tcon->refcount)); | |
2157 | tcon->t_state = TREE_DISCONNECTED; | |
2158 | write_unlock(&sess->tree_conns_lock); | |
2159 | ||
2160 | err = ksmbd_tree_conn_disconnect(sess, tcon); | |
2161 | if (err) { | |
2162 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; | |
2163 | goto err_out; | |
2164 | } | |
2165 | ||
2166 | work->tcon = NULL; | |
2167 | ||
e2b76ab8 NJ |
2168 | rsp->StructureSize = cpu_to_le16(4); |
2169 | err = ksmbd_iov_pin_rsp(work, rsp, | |
2170 | sizeof(struct smb2_tree_disconnect_rsp)); | |
2171 | if (err) { | |
2172 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; | |
33b235a6 | 2173 | goto err_out; |
e2b76ab8 NJ |
2174 | } |
2175 | ||
33b235a6 | 2176 | return 0; |
30210947 | 2177 | |
33b235a6 NJ |
2178 | err_out: |
2179 | smb2_set_err_rsp(work); | |
2180 | return err; | |
e2f34481 | 2181 | |
e2f34481 NJ |
2182 | } |
2183 | ||
2184 | /** | |
2185 | * smb2_session_logoff() - handler for session log off request | |
2186 | * @work: smb work containing request buffer | |
2187 | * | |
2188 | * Return: 0 | |
2189 | */ | |
2190 | int smb2_session_logoff(struct ksmbd_work *work) | |
2191 | { | |
2192 | struct ksmbd_conn *conn = work->conn; | |
7b7d709e NJ |
2193 | struct smb2_logoff_req *req; |
2194 | struct smb2_logoff_rsp *rsp; | |
f5c779b7 | 2195 | struct ksmbd_session *sess; |
7b7d709e | 2196 | u64 sess_id; |
e2b76ab8 | 2197 | int err; |
7b7d709e NJ |
2198 | |
2199 | WORK_BUFFERS(work, req, rsp); | |
2200 | ||
e2b76ab8 NJ |
2201 | ksmbd_debug(SMB, "request\n"); |
2202 | ||
7ca9da7d NJ |
2203 | ksmbd_conn_lock(conn); |
2204 | if (!ksmbd_conn_good(conn)) { | |
2205 | ksmbd_conn_unlock(conn); | |
2206 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; | |
e2b76ab8 | 2207 | smb2_set_err_rsp(work); |
7ca9da7d | 2208 | return -ENOENT; |
e2b76ab8 | 2209 | } |
7ca9da7d | 2210 | sess_id = le64_to_cpu(req->hdr.SessionId); |
abcc506a | 2211 | ksmbd_all_conn_set_status(sess_id, KSMBD_SESS_NEED_RECONNECT); |
7ca9da7d NJ |
2212 | ksmbd_conn_unlock(conn); |
2213 | ||
e2f34481 | 2214 | ksmbd_close_session_fds(work); |
abcc506a | 2215 | ksmbd_conn_wait_idle(conn, sess_id); |
e2f34481 | 2216 | |
f5c779b7 NJ |
2217 | /* |
2218 | * Re-lookup session to validate if session is deleted | |
2219 | * while waiting request complete | |
2220 | */ | |
abcc506a | 2221 | sess = ksmbd_session_lookup_all(conn, sess_id); |
e2f34481 | 2222 | if (ksmbd_tree_conn_session_logoff(sess)) { |
e2f34481 NJ |
2223 | ksmbd_debug(SMB, "Invalid tid %d\n", req->hdr.Id.SyncId.TreeId); |
2224 | rsp->hdr.Status = STATUS_NETWORK_NAME_DELETED; | |
2225 | smb2_set_err_rsp(work); | |
e2b76ab8 | 2226 | return -ENOENT; |
e2f34481 NJ |
2227 | } |
2228 | ||
2229 | ksmbd_destroy_file_table(&sess->file_table); | |
2230 | sess->state = SMB2_SESSION_EXPIRED; | |
2231 | ||
2232 | ksmbd_free_user(sess->user); | |
2233 | sess->user = NULL; | |
abcc506a | 2234 | ksmbd_all_conn_set_status(sess_id, KSMBD_SESS_NEED_NEGOTIATE); |
7ca9da7d NJ |
2235 | |
2236 | rsp->StructureSize = cpu_to_le16(4); | |
2237 | err = ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_logoff_rsp)); | |
2238 | if (err) { | |
2239 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; | |
2240 | smb2_set_err_rsp(work); | |
2241 | return err; | |
2242 | } | |
e2f34481 NJ |
2243 | return 0; |
2244 | } | |
2245 | ||
2246 | /** | |
2247 | * create_smb2_pipe() - create IPC pipe | |
2248 | * @work: smb work containing request buffer | |
2249 | * | |
2250 | * Return: 0 on success, otherwise error | |
2251 | */ | |
2252 | static noinline int create_smb2_pipe(struct ksmbd_work *work) | |
2253 | { | |
7b7d709e NJ |
2254 | struct smb2_create_rsp *rsp; |
2255 | struct smb2_create_req *req; | |
e2f34481 NJ |
2256 | int id; |
2257 | int err; | |
2258 | char *name; | |
2259 | ||
7b7d709e NJ |
2260 | WORK_BUFFERS(work, req, rsp); |
2261 | ||
e2f34481 | 2262 | name = smb_strndup_from_utf16(req->Buffer, le16_to_cpu(req->NameLength), |
070fb21e | 2263 | 1, work->conn->local_nls); |
e2f34481 NJ |
2264 | if (IS_ERR(name)) { |
2265 | rsp->hdr.Status = STATUS_NO_MEMORY; | |
2266 | err = PTR_ERR(name); | |
2267 | goto out; | |
2268 | } | |
2269 | ||
2270 | id = ksmbd_session_rpc_open(work->sess, name); | |
79caa960 | 2271 | if (id < 0) { |
bde1694a | 2272 | pr_err("Unable to open RPC pipe: %d\n", id); |
79caa960 MM |
2273 | err = id; |
2274 | goto out; | |
2275 | } | |
e2f34481 | 2276 | |
79caa960 | 2277 | rsp->hdr.Status = STATUS_SUCCESS; |
e2f34481 NJ |
2278 | rsp->StructureSize = cpu_to_le16(89); |
2279 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_NONE; | |
26a2787d | 2280 | rsp->Flags = 0; |
e2f34481 NJ |
2281 | rsp->CreateAction = cpu_to_le32(FILE_OPENED); |
2282 | ||
2283 | rsp->CreationTime = cpu_to_le64(0); | |
2284 | rsp->LastAccessTime = cpu_to_le64(0); | |
2285 | rsp->ChangeTime = cpu_to_le64(0); | |
2286 | rsp->AllocationSize = cpu_to_le64(0); | |
2287 | rsp->EndofFile = cpu_to_le64(0); | |
26a2787d | 2288 | rsp->FileAttributes = FILE_ATTRIBUTE_NORMAL_LE; |
e2f34481 | 2289 | rsp->Reserved2 = 0; |
2d004c6c | 2290 | rsp->VolatileFileId = id; |
e2f34481 NJ |
2291 | rsp->PersistentFileId = 0; |
2292 | rsp->CreateContextsOffset = 0; | |
2293 | rsp->CreateContextsLength = 0; | |
2294 | ||
e2b76ab8 NJ |
2295 | err = ksmbd_iov_pin_rsp(work, rsp, offsetof(struct smb2_create_rsp, Buffer)); |
2296 | if (err) | |
2297 | goto out; | |
2298 | ||
e2f34481 NJ |
2299 | kfree(name); |
2300 | return 0; | |
2301 | ||
2302 | out: | |
79caa960 MM |
2303 | switch (err) { |
2304 | case -EINVAL: | |
2305 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
2306 | break; | |
2307 | case -ENOSPC: | |
2308 | case -ENOMEM: | |
2309 | rsp->hdr.Status = STATUS_NO_MEMORY; | |
2310 | break; | |
2311 | } | |
2312 | ||
2313 | if (!IS_ERR(name)) | |
2314 | kfree(name); | |
2315 | ||
e2f34481 NJ |
2316 | smb2_set_err_rsp(work); |
2317 | return err; | |
2318 | } | |
2319 | ||
e2f34481 NJ |
2320 | /** |
2321 | * smb2_set_ea() - handler for setting extended attributes using set | |
2322 | * info command | |
2323 | * @eabuf: set info command buffer | |
9496e268 | 2324 | * @buf_len: set info command buffer length |
e2f34481 | 2325 | * @path: dentry path for get ea |
6fc0a265 | 2326 | * @get_write: get write access to a mount |
e2f34481 NJ |
2327 | * |
2328 | * Return: 0 on success, otherwise error | |
2329 | */ | |
9496e268 | 2330 | static int smb2_set_ea(struct smb2_ea_info *eabuf, unsigned int buf_len, |
6fc0a265 | 2331 | const struct path *path, bool get_write) |
e2f34481 | 2332 | { |
4609e1f1 | 2333 | struct mnt_idmap *idmap = mnt_idmap(path->mnt); |
e2f34481 NJ |
2334 | char *attr_name = NULL, *value; |
2335 | int rc = 0; | |
9496e268 NJ |
2336 | unsigned int next = 0; |
2337 | ||
2338 | if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength + | |
2339 | le16_to_cpu(eabuf->EaValueLength)) | |
2340 | return -EINVAL; | |
e2f34481 NJ |
2341 | |
2342 | attr_name = kmalloc(XATTR_NAME_MAX + 1, GFP_KERNEL); | |
2343 | if (!attr_name) | |
2344 | return -ENOMEM; | |
2345 | ||
2346 | do { | |
2347 | if (!eabuf->EaNameLength) | |
2348 | goto next; | |
2349 | ||
2350 | ksmbd_debug(SMB, | |
070fb21e NJ |
2351 | "name : <%s>, name_len : %u, value_len : %u, next : %u\n", |
2352 | eabuf->name, eabuf->EaNameLength, | |
2353 | le16_to_cpu(eabuf->EaValueLength), | |
2354 | le32_to_cpu(eabuf->NextEntryOffset)); | |
e2f34481 NJ |
2355 | |
2356 | if (eabuf->EaNameLength > | |
070fb21e | 2357 | (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN)) { |
e2f34481 NJ |
2358 | rc = -EINVAL; |
2359 | break; | |
2360 | } | |
2361 | ||
2362 | memcpy(attr_name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); | |
2363 | memcpy(&attr_name[XATTR_USER_PREFIX_LEN], eabuf->name, | |
070fb21e | 2364 | eabuf->EaNameLength); |
e2f34481 NJ |
2365 | attr_name[XATTR_USER_PREFIX_LEN + eabuf->EaNameLength] = '\0'; |
2366 | value = (char *)&eabuf->name + eabuf->EaNameLength + 1; | |
2367 | ||
2368 | if (!eabuf->EaValueLength) { | |
4609e1f1 | 2369 | rc = ksmbd_vfs_casexattr_len(idmap, |
af34983e | 2370 | path->dentry, |
e2f34481 NJ |
2371 | attr_name, |
2372 | XATTR_USER_PREFIX_LEN + | |
2373 | eabuf->EaNameLength); | |
2374 | ||
2375 | /* delete the EA only when it exits */ | |
2376 | if (rc > 0) { | |
4609e1f1 | 2377 | rc = ksmbd_vfs_remove_xattr(idmap, |
40b268d3 | 2378 | path, |
e2f34481 NJ |
2379 | attr_name); |
2380 | ||
2381 | if (rc < 0) { | |
2382 | ksmbd_debug(SMB, | |
070fb21e NJ |
2383 | "remove xattr failed(%d)\n", |
2384 | rc); | |
e2f34481 NJ |
2385 | break; |
2386 | } | |
2387 | } | |
2388 | ||
2389 | /* if the EA doesn't exist, just do nothing. */ | |
2390 | rc = 0; | |
2391 | } else { | |
40b268d3 | 2392 | rc = ksmbd_vfs_setxattr(idmap, path, attr_name, value, |
864fb5d3 NJ |
2393 | le16_to_cpu(eabuf->EaValueLength), |
2394 | 0, true); | |
e2f34481 NJ |
2395 | if (rc < 0) { |
2396 | ksmbd_debug(SMB, | |
070fb21e NJ |
2397 | "ksmbd_vfs_setxattr is failed(%d)\n", |
2398 | rc); | |
e2f34481 NJ |
2399 | break; |
2400 | } | |
2401 | } | |
2402 | ||
2403 | next: | |
2404 | next = le32_to_cpu(eabuf->NextEntryOffset); | |
9496e268 NJ |
2405 | if (next == 0 || buf_len < next) |
2406 | break; | |
2407 | buf_len -= next; | |
e2f34481 | 2408 | eabuf = (struct smb2_ea_info *)((char *)eabuf + next); |
79ed288c NJ |
2409 | if (buf_len < sizeof(struct smb2_ea_info)) { |
2410 | rc = -EINVAL; | |
9496e268 | 2411 | break; |
79ed288c | 2412 | } |
9496e268 | 2413 | |
79ed288c NJ |
2414 | if (buf_len < sizeof(struct smb2_ea_info) + eabuf->EaNameLength + |
2415 | le16_to_cpu(eabuf->EaValueLength)) { | |
2416 | rc = -EINVAL; | |
2417 | break; | |
2418 | } | |
e2f34481 NJ |
2419 | } while (next != 0); |
2420 | ||
2421 | kfree(attr_name); | |
2422 | return rc; | |
2423 | } | |
2424 | ||
c22180a5 | 2425 | static noinline int smb2_set_stream_name_xattr(const struct path *path, |
070fb21e NJ |
2426 | struct ksmbd_file *fp, |
2427 | char *stream_name, int s_type) | |
e2f34481 | 2428 | { |
4609e1f1 | 2429 | struct mnt_idmap *idmap = mnt_idmap(path->mnt); |
e2f34481 NJ |
2430 | size_t xattr_stream_size; |
2431 | char *xattr_stream_name; | |
2432 | int rc; | |
2433 | ||
2434 | rc = ksmbd_vfs_xattr_stream_name(stream_name, | |
2435 | &xattr_stream_name, | |
2436 | &xattr_stream_size, | |
2437 | s_type); | |
2438 | if (rc) | |
2439 | return rc; | |
2440 | ||
2441 | fp->stream.name = xattr_stream_name; | |
2442 | fp->stream.size = xattr_stream_size; | |
2443 | ||
2444 | /* Check if there is stream prefix in xattr space */ | |
4609e1f1 | 2445 | rc = ksmbd_vfs_casexattr_len(idmap, |
af34983e | 2446 | path->dentry, |
e2f34481 NJ |
2447 | xattr_stream_name, |
2448 | xattr_stream_size); | |
2449 | if (rc >= 0) | |
2450 | return 0; | |
2451 | ||
2452 | if (fp->cdoption == FILE_OPEN_LE) { | |
2453 | ksmbd_debug(SMB, "XATTR stream name lookup failed: %d\n", rc); | |
2454 | return -EBADF; | |
2455 | } | |
2456 | ||
864fb5d3 | 2457 | rc = ksmbd_vfs_setxattr(idmap, path, xattr_stream_name, NULL, 0, 0, false); |
e2f34481 | 2458 | if (rc < 0) |
bde1694a | 2459 | pr_err("Failed to store XATTR stream name :%d\n", rc); |
e2f34481 NJ |
2460 | return 0; |
2461 | } | |
2462 | ||
c22180a5 | 2463 | static int smb2_remove_smb_xattrs(const struct path *path) |
e2f34481 | 2464 | { |
4609e1f1 | 2465 | struct mnt_idmap *idmap = mnt_idmap(path->mnt); |
e2f34481 NJ |
2466 | char *name, *xattr_list = NULL; |
2467 | ssize_t xattr_list_len; | |
2468 | int err = 0; | |
2469 | ||
ef24c962 | 2470 | xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
e2f34481 NJ |
2471 | if (xattr_list_len < 0) { |
2472 | goto out; | |
2473 | } else if (!xattr_list_len) { | |
2474 | ksmbd_debug(SMB, "empty xattr in the file\n"); | |
2475 | goto out; | |
2476 | } | |
2477 | ||
2478 | for (name = xattr_list; name - xattr_list < xattr_list_len; | |
2479 | name += strlen(name) + 1) { | |
2480 | ksmbd_debug(SMB, "%s, len %zd\n", name, strlen(name)); | |
2481 | ||
17661ecf NJ |
2482 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) && |
2483 | !strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, | |
2484 | STREAM_PREFIX_LEN)) { | |
40b268d3 | 2485 | err = ksmbd_vfs_remove_xattr(idmap, path, |
17661ecf NJ |
2486 | name); |
2487 | if (err) | |
2488 | ksmbd_debug(SMB, "remove xattr failed : %s\n", | |
2489 | name); | |
2490 | } | |
e2f34481 NJ |
2491 | } |
2492 | out: | |
79f6b11a | 2493 | kvfree(xattr_list); |
e2f34481 NJ |
2494 | return err; |
2495 | } | |
2496 | ||
c22180a5 | 2497 | static int smb2_create_truncate(const struct path *path) |
e2f34481 NJ |
2498 | { |
2499 | int rc = vfs_truncate(path, 0); | |
2500 | ||
2501 | if (rc) { | |
bde1694a | 2502 | pr_err("vfs_truncate failed, rc %d\n", rc); |
e2f34481 NJ |
2503 | return rc; |
2504 | } | |
2505 | ||
ef24c962 | 2506 | rc = smb2_remove_smb_xattrs(path); |
e2f34481 NJ |
2507 | if (rc == -EOPNOTSUPP) |
2508 | rc = 0; | |
2509 | if (rc) | |
2510 | ksmbd_debug(SMB, | |
070fb21e NJ |
2511 | "ksmbd_truncate_stream_name_xattr failed, rc %d\n", |
2512 | rc); | |
e2f34481 NJ |
2513 | return rc; |
2514 | } | |
2515 | ||
c22180a5 | 2516 | static void smb2_new_xattrs(struct ksmbd_tree_connect *tcon, const struct path *path, |
070fb21e | 2517 | struct ksmbd_file *fp) |
e2f34481 NJ |
2518 | { |
2519 | struct xattr_dos_attrib da = {0}; | |
2520 | int rc; | |
2521 | ||
2522 | if (!test_share_config_flag(tcon->share_conf, | |
2523 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) | |
2524 | return; | |
2525 | ||
2526 | da.version = 4; | |
2527 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); | |
2528 | da.itime = da.create_time = fp->create_time; | |
2529 | da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | | |
2530 | XATTR_DOSINFO_ITIME; | |
2531 | ||
a9f106c7 | 2532 | rc = ksmbd_vfs_set_dos_attrib_xattr(mnt_idmap(path->mnt), path, &da, true); |
e2f34481 NJ |
2533 | if (rc) |
2534 | ksmbd_debug(SMB, "failed to store file attribute into xattr\n"); | |
2535 | } | |
2536 | ||
2537 | static void smb2_update_xattrs(struct ksmbd_tree_connect *tcon, | |
c22180a5 | 2538 | const struct path *path, struct ksmbd_file *fp) |
e2f34481 NJ |
2539 | { |
2540 | struct xattr_dos_attrib da; | |
2541 | int rc; | |
2542 | ||
26a2787d | 2543 | fp->f_ci->m_fattr &= ~(FILE_ATTRIBUTE_HIDDEN_LE | FILE_ATTRIBUTE_SYSTEM_LE); |
e2f34481 NJ |
2544 | |
2545 | /* get FileAttributes from XATTR_NAME_DOS_ATTRIBUTE */ | |
2546 | if (!test_share_config_flag(tcon->share_conf, | |
64b39f4a | 2547 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) |
e2f34481 NJ |
2548 | return; |
2549 | ||
4609e1f1 | 2550 | rc = ksmbd_vfs_get_dos_attrib_xattr(mnt_idmap(path->mnt), |
af34983e | 2551 | path->dentry, &da); |
e2f34481 NJ |
2552 | if (rc > 0) { |
2553 | fp->f_ci->m_fattr = cpu_to_le32(da.attr); | |
2554 | fp->create_time = da.create_time; | |
2555 | fp->itime = da.itime; | |
2556 | } | |
2557 | } | |
2558 | ||
2b57a432 NJ |
2559 | static int smb2_creat(struct ksmbd_work *work, struct path *parent_path, |
2560 | struct path *path, char *name, int open_flags, | |
2561 | umode_t posix_mode, bool is_dir) | |
e2f34481 NJ |
2562 | { |
2563 | struct ksmbd_tree_connect *tcon = work->tcon; | |
2564 | struct ksmbd_share_config *share = tcon->share_conf; | |
2565 | umode_t mode; | |
2566 | int rc; | |
2567 | ||
2568 | if (!(open_flags & O_CREAT)) | |
2569 | return -EBADF; | |
2570 | ||
2571 | ksmbd_debug(SMB, "file does not exist, so creating\n"); | |
2572 | if (is_dir == true) { | |
2573 | ksmbd_debug(SMB, "creating directory\n"); | |
2574 | ||
2575 | mode = share_config_directory_mode(share, posix_mode); | |
2576 | rc = ksmbd_vfs_mkdir(work, name, mode); | |
2577 | if (rc) | |
2578 | return rc; | |
2579 | } else { | |
2580 | ksmbd_debug(SMB, "creating regular file\n"); | |
2581 | ||
2582 | mode = share_config_create_mode(share, posix_mode); | |
2583 | rc = ksmbd_vfs_create(work, name, mode); | |
2584 | if (rc) | |
2585 | return rc; | |
2586 | } | |
2587 | ||
2b57a432 | 2588 | rc = ksmbd_vfs_kern_path_locked(work, name, 0, parent_path, path, 0); |
e2f34481 | 2589 | if (rc) { |
bde1694a NJ |
2590 | pr_err("cannot get linux path (%s), err = %d\n", |
2591 | name, rc); | |
e2f34481 NJ |
2592 | return rc; |
2593 | } | |
2594 | return 0; | |
2595 | } | |
2596 | ||
2597 | static int smb2_create_sd_buffer(struct ksmbd_work *work, | |
070fb21e | 2598 | struct smb2_create_req *req, |
c22180a5 | 2599 | const struct path *path) |
e2f34481 NJ |
2600 | { |
2601 | struct create_context *context; | |
21dd1fd6 | 2602 | struct create_sd_buf_req *sd_buf; |
e2f34481 NJ |
2603 | |
2604 | if (!req->CreateContextsOffset) | |
21dd1fd6 | 2605 | return -ENOENT; |
e2f34481 NJ |
2606 | |
2607 | /* Parse SD BUFFER create contexts */ | |
02f76c40 | 2608 | context = smb2_find_context_vals(req, SMB2_CREATE_SD_BUFFER, 4); |
21dd1fd6 HL |
2609 | if (!context) |
2610 | return -ENOENT; | |
2611 | else if (IS_ERR(context)) | |
2612 | return PTR_ERR(context); | |
e2f34481 | 2613 | |
21dd1fd6 HL |
2614 | ksmbd_debug(SMB, |
2615 | "Set ACLs using SMB2_CREATE_SD_BUFFER context\n"); | |
2616 | sd_buf = (struct create_sd_buf_req *)context; | |
8f77150c HL |
2617 | if (le16_to_cpu(context->DataOffset) + |
2618 | le32_to_cpu(context->DataLength) < | |
2619 | sizeof(struct create_sd_buf_req)) | |
2620 | return -EINVAL; | |
21dd1fd6 | 2621 | return set_info_sec(work->conn, work->tcon, path, &sd_buf->ntsd, |
864fb5d3 | 2622 | le32_to_cpu(sd_buf->ccontext.DataLength), true, false); |
e2f34481 NJ |
2623 | } |
2624 | ||
43205ca7 | 2625 | static void ksmbd_acls_fattr(struct smb_fattr *fattr, |
e67fe633 | 2626 | struct mnt_idmap *idmap, |
43205ca7 | 2627 | struct inode *inode) |
3d47e546 | 2628 | { |
e67fe633 CB |
2629 | vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); |
2630 | vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); | |
276a3f7c CB |
2631 | |
2632 | fattr->cf_uid = vfsuid_into_kuid(vfsuid); | |
2633 | fattr->cf_gid = vfsgid_into_kgid(vfsgid); | |
3d47e546 | 2634 | fattr->cf_mode = inode->i_mode; |
777cad16 | 2635 | fattr->cf_acls = NULL; |
3d47e546 NJ |
2636 | fattr->cf_dacls = NULL; |
2637 | ||
777cad16 | 2638 | if (IS_ENABLED(CONFIG_FS_POSIX_ACL)) { |
cac2f8b8 | 2639 | fattr->cf_acls = get_inode_acl(inode, ACL_TYPE_ACCESS); |
777cad16 | 2640 | if (S_ISDIR(inode->i_mode)) |
cac2f8b8 | 2641 | fattr->cf_dacls = get_inode_acl(inode, ACL_TYPE_DEFAULT); |
777cad16 | 2642 | } |
3d47e546 NJ |
2643 | } |
2644 | ||
e2f34481 NJ |
2645 | /** |
2646 | * smb2_open() - handler for smb file open request | |
2647 | * @work: smb work containing request buffer | |
2648 | * | |
2649 | * Return: 0 on success, otherwise error | |
2650 | */ | |
2651 | int smb2_open(struct ksmbd_work *work) | |
2652 | { | |
2653 | struct ksmbd_conn *conn = work->conn; | |
2654 | struct ksmbd_session *sess = work->sess; | |
2655 | struct ksmbd_tree_connect *tcon = work->tcon; | |
2656 | struct smb2_create_req *req; | |
cb451720 | 2657 | struct smb2_create_rsp *rsp; |
2b57a432 | 2658 | struct path path, parent_path; |
e2f34481 NJ |
2659 | struct ksmbd_share_config *share = tcon->share_conf; |
2660 | struct ksmbd_file *fp = NULL; | |
2661 | struct file *filp = NULL; | |
13e83a49 | 2662 | struct mnt_idmap *idmap = NULL; |
e2f34481 NJ |
2663 | struct kstat stat; |
2664 | struct create_context *context; | |
2665 | struct lease_ctx_info *lc = NULL; | |
2666 | struct create_ea_buf_req *ea_buf = NULL; | |
2667 | struct oplock_info *opinfo; | |
2668 | __le32 *next_ptr = NULL; | |
6c5e36d1 | 2669 | int req_op_level = 0, open_flags = 0, may_flags = 0, file_info = 0; |
265fd199 | 2670 | int rc = 0; |
e2f34481 NJ |
2671 | int contxt_cnt = 0, query_disk_id = 0; |
2672 | int maximal_access_ctxt = 0, posix_ctxt = 0; | |
2673 | int s_type = 0; | |
2674 | int next_off = 0; | |
2675 | char *name = NULL; | |
2676 | char *stream_name = NULL; | |
2677 | bool file_present = false, created = false, already_permitted = false; | |
e2f34481 NJ |
2678 | int share_ret, need_truncate = 0; |
2679 | u64 time; | |
2680 | umode_t posix_mode = 0; | |
2681 | __le32 daccess, maximal_access = 0; | |
e2b76ab8 | 2682 | int iov_len = 0; |
e2f34481 | 2683 | |
e2f34481 NJ |
2684 | WORK_BUFFERS(work, req, rsp); |
2685 | ||
2686 | if (req->hdr.NextCommand && !work->next_smb2_rcv_hdr_off && | |
64b39f4a | 2687 | (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS)) { |
e2f34481 NJ |
2688 | ksmbd_debug(SMB, "invalid flag in chained command\n"); |
2689 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
2690 | smb2_set_err_rsp(work); | |
2691 | return -EINVAL; | |
2692 | } | |
2693 | ||
2694 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_PIPE)) { | |
2695 | ksmbd_debug(SMB, "IPC pipe create request\n"); | |
2696 | return create_smb2_pipe(work); | |
2697 | } | |
2698 | ||
2699 | if (req->NameLength) { | |
2700 | if ((req->CreateOptions & FILE_DIRECTORY_FILE_LE) && | |
64b39f4a | 2701 | *(char *)req->Buffer == '\\') { |
bde1694a | 2702 | pr_err("not allow directory name included leading slash\n"); |
e2f34481 | 2703 | rc = -EINVAL; |
2e450920 | 2704 | goto err_out2; |
e2f34481 NJ |
2705 | } |
2706 | ||
80917f17 | 2707 | name = smb2_get_name(req->Buffer, |
e2f34481 NJ |
2708 | le16_to_cpu(req->NameLength), |
2709 | work->conn->local_nls); | |
2710 | if (IS_ERR(name)) { | |
2711 | rc = PTR_ERR(name); | |
2712 | if (rc != -ENOMEM) | |
2713 | rc = -ENOENT; | |
8b99f350 | 2714 | name = NULL; |
2e450920 | 2715 | goto err_out2; |
e2f34481 NJ |
2716 | } |
2717 | ||
2718 | ksmbd_debug(SMB, "converted name = %s\n", name); | |
2719 | if (strchr(name, ':')) { | |
2720 | if (!test_share_config_flag(work->tcon->share_conf, | |
64b39f4a | 2721 | KSMBD_SHARE_FLAG_STREAMS)) { |
e2f34481 | 2722 | rc = -EBADF; |
2e450920 | 2723 | goto err_out2; |
e2f34481 NJ |
2724 | } |
2725 | rc = parse_stream_name(name, &stream_name, &s_type); | |
2726 | if (rc < 0) | |
2e450920 | 2727 | goto err_out2; |
e2f34481 NJ |
2728 | } |
2729 | ||
2730 | rc = ksmbd_validate_filename(name); | |
2731 | if (rc < 0) | |
2e450920 | 2732 | goto err_out2; |
e2f34481 NJ |
2733 | |
2734 | if (ksmbd_share_veto_filename(share, name)) { | |
2735 | rc = -ENOENT; | |
2736 | ksmbd_debug(SMB, "Reject open(), vetoed file: %s\n", | |
070fb21e | 2737 | name); |
2e450920 | 2738 | goto err_out2; |
e2f34481 NJ |
2739 | } |
2740 | } else { | |
265fd199 | 2741 | name = kstrdup("", GFP_KERNEL); |
e2f34481 | 2742 | if (!name) { |
e2f34481 | 2743 | rc = -ENOMEM; |
2e450920 | 2744 | goto err_out2; |
e2f34481 | 2745 | } |
e2f34481 NJ |
2746 | } |
2747 | ||
26a2787d | 2748 | if (le32_to_cpu(req->ImpersonationLevel) > le32_to_cpu(IL_DELEGATE)) { |
bde1694a NJ |
2749 | pr_err("Invalid impersonationlevel : 0x%x\n", |
2750 | le32_to_cpu(req->ImpersonationLevel)); | |
e2f34481 NJ |
2751 | rc = -EIO; |
2752 | rsp->hdr.Status = STATUS_BAD_IMPERSONATION_LEVEL; | |
2e450920 | 2753 | goto err_out2; |
e2f34481 NJ |
2754 | } |
2755 | ||
26a2787d | 2756 | if (req->CreateOptions && !(req->CreateOptions & CREATE_OPTIONS_MASK_LE)) { |
bde1694a NJ |
2757 | pr_err("Invalid create options : 0x%x\n", |
2758 | le32_to_cpu(req->CreateOptions)); | |
e2f34481 | 2759 | rc = -EINVAL; |
2e450920 | 2760 | goto err_out2; |
e2f34481 | 2761 | } else { |
e2f34481 | 2762 | if (req->CreateOptions & FILE_SEQUENTIAL_ONLY_LE && |
64b39f4a | 2763 | req->CreateOptions & FILE_RANDOM_ACCESS_LE) |
e2f34481 NJ |
2764 | req->CreateOptions = ~(FILE_SEQUENTIAL_ONLY_LE); |
2765 | ||
070fb21e NJ |
2766 | if (req->CreateOptions & |
2767 | (FILE_OPEN_BY_FILE_ID_LE | CREATE_TREE_CONNECTION | | |
2768 | FILE_RESERVE_OPFILTER_LE)) { | |
e2f34481 | 2769 | rc = -EOPNOTSUPP; |
2e450920 | 2770 | goto err_out2; |
e2f34481 NJ |
2771 | } |
2772 | ||
2773 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { | |
2774 | if (req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE) { | |
2775 | rc = -EINVAL; | |
2e450920 | 2776 | goto err_out2; |
64b39f4a | 2777 | } else if (req->CreateOptions & FILE_NO_COMPRESSION_LE) { |
e2f34481 | 2778 | req->CreateOptions = ~(FILE_NO_COMPRESSION_LE); |
64b39f4a | 2779 | } |
e2f34481 NJ |
2780 | } |
2781 | } | |
2782 | ||
2783 | if (le32_to_cpu(req->CreateDisposition) > | |
070fb21e | 2784 | le32_to_cpu(FILE_OVERWRITE_IF_LE)) { |
bde1694a NJ |
2785 | pr_err("Invalid create disposition : 0x%x\n", |
2786 | le32_to_cpu(req->CreateDisposition)); | |
e2f34481 | 2787 | rc = -EINVAL; |
2e450920 | 2788 | goto err_out2; |
e2f34481 NJ |
2789 | } |
2790 | ||
2791 | if (!(req->DesiredAccess & DESIRED_ACCESS_MASK)) { | |
bde1694a NJ |
2792 | pr_err("Invalid desired access : 0x%x\n", |
2793 | le32_to_cpu(req->DesiredAccess)); | |
e2f34481 | 2794 | rc = -EACCES; |
2e450920 | 2795 | goto err_out2; |
e2f34481 NJ |
2796 | } |
2797 | ||
26a2787d | 2798 | if (req->FileAttributes && !(req->FileAttributes & FILE_ATTRIBUTE_MASK_LE)) { |
bde1694a NJ |
2799 | pr_err("Invalid file attribute : 0x%x\n", |
2800 | le32_to_cpu(req->FileAttributes)); | |
e2f34481 | 2801 | rc = -EINVAL; |
2e450920 | 2802 | goto err_out2; |
e2f34481 NJ |
2803 | } |
2804 | ||
2805 | if (req->CreateContextsOffset) { | |
2806 | /* Parse non-durable handle create contexts */ | |
02f76c40 | 2807 | context = smb2_find_context_vals(req, SMB2_CREATE_EA_BUFFER, 4); |
f19b3967 NJ |
2808 | if (IS_ERR(context)) { |
2809 | rc = PTR_ERR(context); | |
2e450920 | 2810 | goto err_out2; |
f19b3967 | 2811 | } else if (context) { |
e2f34481 | 2812 | ea_buf = (struct create_ea_buf_req *)context; |
8f77150c HL |
2813 | if (le16_to_cpu(context->DataOffset) + |
2814 | le32_to_cpu(context->DataLength) < | |
2815 | sizeof(struct create_ea_buf_req)) { | |
2816 | rc = -EINVAL; | |
2e450920 | 2817 | goto err_out2; |
8f77150c | 2818 | } |
e2f34481 NJ |
2819 | if (req->CreateOptions & FILE_NO_EA_KNOWLEDGE_LE) { |
2820 | rsp->hdr.Status = STATUS_ACCESS_DENIED; | |
2821 | rc = -EACCES; | |
2e450920 | 2822 | goto err_out2; |
e2f34481 NJ |
2823 | } |
2824 | } | |
2825 | ||
2826 | context = smb2_find_context_vals(req, | |
02f76c40 | 2827 | SMB2_CREATE_QUERY_MAXIMAL_ACCESS_REQUEST, 4); |
f19b3967 NJ |
2828 | if (IS_ERR(context)) { |
2829 | rc = PTR_ERR(context); | |
2e450920 | 2830 | goto err_out2; |
f19b3967 | 2831 | } else if (context) { |
e2f34481 | 2832 | ksmbd_debug(SMB, |
070fb21e | 2833 | "get query maximal access context\n"); |
e2f34481 NJ |
2834 | maximal_access_ctxt = 1; |
2835 | } | |
2836 | ||
2837 | context = smb2_find_context_vals(req, | |
02f76c40 | 2838 | SMB2_CREATE_TIMEWARP_REQUEST, 4); |
f19b3967 NJ |
2839 | if (IS_ERR(context)) { |
2840 | rc = PTR_ERR(context); | |
2e450920 | 2841 | goto err_out2; |
f19b3967 | 2842 | } else if (context) { |
e2f34481 NJ |
2843 | ksmbd_debug(SMB, "get timewarp context\n"); |
2844 | rc = -EBADF; | |
2e450920 | 2845 | goto err_out2; |
e2f34481 NJ |
2846 | } |
2847 | ||
2848 | if (tcon->posix_extensions) { | |
2849 | context = smb2_find_context_vals(req, | |
02f76c40 | 2850 | SMB2_CREATE_TAG_POSIX, 16); |
f19b3967 NJ |
2851 | if (IS_ERR(context)) { |
2852 | rc = PTR_ERR(context); | |
2e450920 | 2853 | goto err_out2; |
f19b3967 | 2854 | } else if (context) { |
e2f34481 NJ |
2855 | struct create_posix *posix = |
2856 | (struct create_posix *)context; | |
8f77150c HL |
2857 | if (le16_to_cpu(context->DataOffset) + |
2858 | le32_to_cpu(context->DataLength) < | |
9ca8581e | 2859 | sizeof(struct create_posix) - 4) { |
8f77150c | 2860 | rc = -EINVAL; |
2e450920 | 2861 | goto err_out2; |
8f77150c | 2862 | } |
e2f34481 NJ |
2863 | ksmbd_debug(SMB, "get posix context\n"); |
2864 | ||
2865 | posix_mode = le32_to_cpu(posix->Mode); | |
2866 | posix_ctxt = 1; | |
2867 | } | |
2868 | } | |
2869 | } | |
2870 | ||
2871 | if (ksmbd_override_fsids(work)) { | |
2872 | rc = -ENOMEM; | |
2e450920 | 2873 | goto err_out2; |
e2f34481 NJ |
2874 | } |
2875 | ||
2b57a432 NJ |
2876 | rc = ksmbd_vfs_kern_path_locked(work, name, LOOKUP_NO_SYMLINKS, |
2877 | &parent_path, &path, 1); | |
4ea47798 | 2878 | if (!rc) { |
74d7970f NJ |
2879 | file_present = true; |
2880 | ||
4ea47798 | 2881 | if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) { |
e2f34481 NJ |
2882 | /* |
2883 | * If file exists with under flags, return access | |
2884 | * denied error. | |
2885 | */ | |
2886 | if (req->CreateDisposition == FILE_OVERWRITE_IF_LE || | |
64b39f4a | 2887 | req->CreateDisposition == FILE_OPEN_IF_LE) { |
e2f34481 | 2888 | rc = -EACCES; |
e2f34481 NJ |
2889 | goto err_out; |
2890 | } | |
2891 | ||
64b39f4a | 2892 | if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
e2f34481 | 2893 | ksmbd_debug(SMB, |
070fb21e | 2894 | "User does not have write permission\n"); |
e2f34481 | 2895 | rc = -EACCES; |
e2f34481 NJ |
2896 | goto err_out; |
2897 | } | |
4ea47798 NJ |
2898 | } else if (d_is_symlink(path.dentry)) { |
2899 | rc = -EACCES; | |
4ea47798 | 2900 | goto err_out; |
e2f34481 | 2901 | } |
e2f34481 | 2902 | |
74d7970f NJ |
2903 | file_present = true; |
2904 | idmap = mnt_idmap(path.mnt); | |
2905 | } else { | |
265fd199 | 2906 | if (rc != -ENOENT) |
e2f34481 | 2907 | goto err_out; |
e2f34481 | 2908 | ksmbd_debug(SMB, "can not get linux path for %s, rc = %d\n", |
070fb21e | 2909 | name, rc); |
e2f34481 | 2910 | rc = 0; |
e2f34481 | 2911 | } |
74d7970f | 2912 | |
e2f34481 NJ |
2913 | if (stream_name) { |
2914 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE) { | |
2915 | if (s_type == DATA_STREAM) { | |
2916 | rc = -EIO; | |
2917 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; | |
2918 | } | |
2919 | } else { | |
823d0d3e NJ |
2920 | if (file_present && S_ISDIR(d_inode(path.dentry)->i_mode) && |
2921 | s_type == DATA_STREAM) { | |
e2f34481 NJ |
2922 | rc = -EIO; |
2923 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; | |
2924 | } | |
2925 | } | |
2926 | ||
2927 | if (req->CreateOptions & FILE_DIRECTORY_FILE_LE && | |
26a2787d | 2928 | req->FileAttributes & FILE_ATTRIBUTE_NORMAL_LE) { |
e2f34481 NJ |
2929 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
2930 | rc = -EIO; | |
2931 | } | |
2932 | ||
2933 | if (rc < 0) | |
2934 | goto err_out; | |
2935 | } | |
2936 | ||
64b39f4a | 2937 | if (file_present && req->CreateOptions & FILE_NON_DIRECTORY_FILE_LE && |
823d0d3e NJ |
2938 | S_ISDIR(d_inode(path.dentry)->i_mode) && |
2939 | !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { | |
e2f34481 | 2940 | ksmbd_debug(SMB, "open() argument is a directory: %s, %x\n", |
070fb21e | 2941 | name, req->CreateOptions); |
e2f34481 NJ |
2942 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; |
2943 | rc = -EIO; | |
2944 | goto err_out; | |
2945 | } | |
2946 | ||
2947 | if (file_present && (req->CreateOptions & FILE_DIRECTORY_FILE_LE) && | |
64b39f4a | 2948 | !(req->CreateDisposition == FILE_CREATE_LE) && |
823d0d3e | 2949 | !S_ISDIR(d_inode(path.dentry)->i_mode)) { |
e2f34481 NJ |
2950 | rsp->hdr.Status = STATUS_NOT_A_DIRECTORY; |
2951 | rc = -EIO; | |
2952 | goto err_out; | |
2953 | } | |
2954 | ||
2955 | if (!stream_name && file_present && | |
64b39f4a | 2956 | req->CreateDisposition == FILE_CREATE_LE) { |
e2f34481 NJ |
2957 | rc = -EEXIST; |
2958 | goto err_out; | |
2959 | } | |
2960 | ||
e2f34481 NJ |
2961 | daccess = smb_map_generic_desired_access(req->DesiredAccess); |
2962 | ||
2963 | if (file_present && !(req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { | |
ef24c962 | 2964 | rc = smb_check_perm_dacl(conn, &path, &daccess, |
070fb21e | 2965 | sess->user->uid); |
e2f34481 NJ |
2966 | if (rc) |
2967 | goto err_out; | |
2968 | } | |
2969 | ||
2970 | if (daccess & FILE_MAXIMAL_ACCESS_LE) { | |
2971 | if (!file_present) { | |
2972 | daccess = cpu_to_le32(GENERIC_ALL_FLAGS); | |
2973 | } else { | |
ccb5889a | 2974 | ksmbd_vfs_query_maximal_access(idmap, |
af34983e | 2975 | path.dentry, |
e2f34481 | 2976 | &daccess); |
e2f34481 NJ |
2977 | already_permitted = true; |
2978 | } | |
2979 | maximal_access = daccess; | |
2980 | } | |
2981 | ||
070fb21e | 2982 | open_flags = smb2_create_open_flags(file_present, daccess, |
6c5e36d1 HL |
2983 | req->CreateDisposition, |
2984 | &may_flags); | |
e2f34481 NJ |
2985 | |
2986 | if (!test_tree_conn_flag(tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { | |
d592a915 | 2987 | if (open_flags & (O_CREAT | O_TRUNC)) { |
e2f34481 | 2988 | ksmbd_debug(SMB, |
070fb21e | 2989 | "User does not have write permission\n"); |
e2f34481 NJ |
2990 | rc = -EACCES; |
2991 | goto err_out; | |
2992 | } | |
2993 | } | |
2994 | ||
2995 | /*create file if not present */ | |
2996 | if (!file_present) { | |
2b57a432 NJ |
2997 | rc = smb2_creat(work, &parent_path, &path, name, open_flags, |
2998 | posix_mode, | |
070fb21e | 2999 | req->CreateOptions & FILE_DIRECTORY_FILE_LE); |
d337a44e MM |
3000 | if (rc) { |
3001 | if (rc == -ENOENT) { | |
3002 | rc = -EIO; | |
3003 | rsp->hdr.Status = STATUS_OBJECT_PATH_NOT_FOUND; | |
3004 | } | |
e2f34481 | 3005 | goto err_out; |
d337a44e | 3006 | } |
e2f34481 NJ |
3007 | |
3008 | created = true; | |
13e83a49 | 3009 | idmap = mnt_idmap(path.mnt); |
e2f34481 | 3010 | if (ea_buf) { |
9496e268 NJ |
3011 | if (le32_to_cpu(ea_buf->ccontext.DataLength) < |
3012 | sizeof(struct smb2_ea_info)) { | |
3013 | rc = -EINVAL; | |
3014 | goto err_out; | |
3015 | } | |
3016 | ||
3017 | rc = smb2_set_ea(&ea_buf->ea, | |
3018 | le32_to_cpu(ea_buf->ccontext.DataLength), | |
6fc0a265 | 3019 | &path, false); |
e2f34481 NJ |
3020 | if (rc == -EOPNOTSUPP) |
3021 | rc = 0; | |
3022 | else if (rc) | |
3023 | goto err_out; | |
3024 | } | |
3025 | } else if (!already_permitted) { | |
e2f34481 NJ |
3026 | /* FILE_READ_ATTRIBUTE is allowed without inode_permission, |
3027 | * because execute(search) permission on a parent directory, | |
3028 | * is already granted. | |
3029 | */ | |
64b39f4a | 3030 | if (daccess & ~(FILE_READ_ATTRIBUTES_LE | FILE_READ_CONTROL_LE)) { |
4609e1f1 | 3031 | rc = inode_permission(idmap, |
6c5e36d1 HL |
3032 | d_inode(path.dentry), |
3033 | may_flags); | |
ff1d5727 | 3034 | if (rc) |
e2f34481 | 3035 | goto err_out; |
6c5e36d1 HL |
3036 | |
3037 | if ((daccess & FILE_DELETE_LE) || | |
3038 | (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE)) { | |
74d7970f NJ |
3039 | rc = inode_permission(idmap, |
3040 | d_inode(path.dentry->d_parent), | |
3041 | MAY_EXEC | MAY_WRITE); | |
6c5e36d1 HL |
3042 | if (rc) |
3043 | goto err_out; | |
3044 | } | |
e2f34481 NJ |
3045 | } |
3046 | } | |
3047 | ||
4274a9dc | 3048 | rc = ksmbd_query_inode_status(path.dentry->d_parent); |
e2f34481 NJ |
3049 | if (rc == KSMBD_INODE_STATUS_PENDING_DELETE) { |
3050 | rc = -EBUSY; | |
3051 | goto err_out; | |
3052 | } | |
3053 | ||
3054 | rc = 0; | |
3055 | filp = dentry_open(&path, open_flags, current_cred()); | |
3056 | if (IS_ERR(filp)) { | |
3057 | rc = PTR_ERR(filp); | |
bde1694a | 3058 | pr_err("dentry open for dir failed, rc %d\n", rc); |
e2f34481 NJ |
3059 | goto err_out; |
3060 | } | |
3061 | ||
3062 | if (file_present) { | |
3063 | if (!(open_flags & O_TRUNC)) | |
3064 | file_info = FILE_OPENED; | |
3065 | else | |
3066 | file_info = FILE_OVERWRITTEN; | |
3067 | ||
070fb21e NJ |
3068 | if ((req->CreateDisposition & FILE_CREATE_MASK_LE) == |
3069 | FILE_SUPERSEDE_LE) | |
e2f34481 | 3070 | file_info = FILE_SUPERSEDED; |
64b39f4a | 3071 | } else if (open_flags & O_CREAT) { |
e2f34481 | 3072 | file_info = FILE_CREATED; |
64b39f4a | 3073 | } |
e2f34481 NJ |
3074 | |
3075 | ksmbd_vfs_set_fadvise(filp, req->CreateOptions); | |
3076 | ||
3077 | /* Obtain Volatile-ID */ | |
3078 | fp = ksmbd_open_fd(work, filp); | |
3079 | if (IS_ERR(fp)) { | |
3080 | fput(filp); | |
3081 | rc = PTR_ERR(fp); | |
3082 | fp = NULL; | |
3083 | goto err_out; | |
3084 | } | |
3085 | ||
3086 | /* Get Persistent-ID */ | |
3087 | ksmbd_open_durable_fd(fp); | |
3867369e | 3088 | if (!has_file_id(fp->persistent_id)) { |
e2f34481 NJ |
3089 | rc = -ENOMEM; |
3090 | goto err_out; | |
3091 | } | |
3092 | ||
e2f34481 NJ |
3093 | fp->cdoption = req->CreateDisposition; |
3094 | fp->daccess = daccess; | |
3095 | fp->saccess = req->ShareAccess; | |
3096 | fp->coption = req->CreateOptions; | |
3097 | ||
3098 | /* Set default windows and posix acls if creating new file */ | |
3099 | if (created) { | |
3100 | int posix_acl_rc; | |
fba08fa0 | 3101 | struct inode *inode = d_inode(path.dentry); |
e2f34481 | 3102 | |
13e83a49 | 3103 | posix_acl_rc = ksmbd_vfs_inherit_posix_acl(idmap, |
40b268d3 | 3104 | &path, |
af34983e | 3105 | d_inode(path.dentry->d_parent)); |
e2f34481 NJ |
3106 | if (posix_acl_rc) |
3107 | ksmbd_debug(SMB, "inherit posix acl failed : %d\n", posix_acl_rc); | |
3108 | ||
3109 | if (test_share_config_flag(work->tcon->share_conf, | |
64b39f4a | 3110 | KSMBD_SHARE_FLAG_ACL_XATTR)) { |
ef24c962 | 3111 | rc = smb_inherit_dacl(conn, &path, sess->user->uid, |
070fb21e | 3112 | sess->user->gid); |
e2f34481 NJ |
3113 | } |
3114 | ||
3115 | if (rc) { | |
ef24c962 | 3116 | rc = smb2_create_sd_buffer(work, req, &path); |
e2f34481 NJ |
3117 | if (rc) { |
3118 | if (posix_acl_rc) | |
13e83a49 | 3119 | ksmbd_vfs_set_init_posix_acl(idmap, |
40b268d3 | 3120 | &path); |
e2f34481 NJ |
3121 | |
3122 | if (test_share_config_flag(work->tcon->share_conf, | |
64b39f4a | 3123 | KSMBD_SHARE_FLAG_ACL_XATTR)) { |
e2f34481 NJ |
3124 | struct smb_fattr fattr; |
3125 | struct smb_ntsd *pntsd; | |
3d47e546 | 3126 | int pntsd_size, ace_num = 0; |
e2f34481 | 3127 | |
e67fe633 | 3128 | ksmbd_acls_fattr(&fattr, idmap, inode); |
e6b1059f MM |
3129 | if (fattr.cf_acls) |
3130 | ace_num = fattr.cf_acls->a_count; | |
3d47e546 NJ |
3131 | if (fattr.cf_dacls) |
3132 | ace_num += fattr.cf_dacls->a_count; | |
e2f34481 NJ |
3133 | |
3134 | pntsd = kmalloc(sizeof(struct smb_ntsd) + | |
64b39f4a | 3135 | sizeof(struct smb_sid) * 3 + |
e2f34481 | 3136 | sizeof(struct smb_acl) + |
64b39f4a | 3137 | sizeof(struct smb_ace) * ace_num * 2, |
e2f34481 | 3138 | GFP_KERNEL); |
2624b445 C |
3139 | if (!pntsd) { |
3140 | posix_acl_release(fattr.cf_acls); | |
3141 | posix_acl_release(fattr.cf_dacls); | |
e2f34481 | 3142 | goto err_out; |
2624b445 | 3143 | } |
e2f34481 | 3144 | |
4d7ca409 | 3145 | rc = build_sec_desc(idmap, |
8f054118 | 3146 | pntsd, NULL, 0, |
070fb21e | 3147 | OWNER_SECINFO | |
af34983e HL |
3148 | GROUP_SECINFO | |
3149 | DACL_SECINFO, | |
070fb21e | 3150 | &pntsd_size, &fattr); |
e2f34481 NJ |
3151 | posix_acl_release(fattr.cf_acls); |
3152 | posix_acl_release(fattr.cf_dacls); | |
f2e78aff NJ |
3153 | if (rc) { |
3154 | kfree(pntsd); | |
3155 | goto err_out; | |
3156 | } | |
e2f34481 NJ |
3157 | |
3158 | rc = ksmbd_vfs_set_sd_xattr(conn, | |
4609e1f1 | 3159 | idmap, |
40b268d3 | 3160 | &path, |
070fb21e | 3161 | pntsd, |
864fb5d3 NJ |
3162 | pntsd_size, |
3163 | false); | |
3d47e546 | 3164 | kfree(pntsd); |
e2f34481 | 3165 | if (rc) |
bde1694a NJ |
3166 | pr_err("failed to store ntacl in xattr : %d\n", |
3167 | rc); | |
e2f34481 NJ |
3168 | } |
3169 | } | |
3170 | } | |
3171 | rc = 0; | |
3172 | } | |
3173 | ||
3174 | if (stream_name) { | |
3175 | rc = smb2_set_stream_name_xattr(&path, | |
3176 | fp, | |
3177 | stream_name, | |
3178 | s_type); | |
3179 | if (rc) | |
3180 | goto err_out; | |
3181 | file_info = FILE_CREATED; | |
3182 | } | |
3183 | ||
3184 | fp->attrib_only = !(req->DesiredAccess & ~(FILE_READ_ATTRIBUTES_LE | | |
3185 | FILE_WRITE_ATTRIBUTES_LE | FILE_SYNCHRONIZE_LE)); | |
e2f34481 NJ |
3186 | |
3187 | /* fp should be searchable through ksmbd_inode.m_fp_list | |
3188 | * after daccess, saccess, attrib_only, and stream are | |
3189 | * initialized. | |
3190 | */ | |
3191 | write_lock(&fp->f_ci->m_lock); | |
3192 | list_add(&fp->node, &fp->f_ci->m_fp_list); | |
3193 | write_unlock(&fp->f_ci->m_lock); | |
3194 | ||
e2f34481 NJ |
3195 | /* Check delete pending among previous fp before oplock break */ |
3196 | if (ksmbd_inode_pending_delete(fp)) { | |
3197 | rc = -EBUSY; | |
3198 | goto err_out; | |
3199 | } | |
3200 | ||
2e450920 NJ |
3201 | if (file_present || created) |
3202 | ksmbd_vfs_kern_path_unlock(&parent_path, &path); | |
3203 | ||
3204 | if (!S_ISDIR(file_inode(filp)->i_mode) && open_flags & O_TRUNC && | |
3205 | !fp->attrib_only && !stream_name) { | |
3206 | smb_break_all_oplock(work, fp); | |
3207 | need_truncate = 1; | |
3208 | } | |
3209 | ||
eb547407 NJ |
3210 | req_op_level = req->RequestedOplockLevel; |
3211 | if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) | |
3212 | lc = parse_lease_state(req, S_ISDIR(file_inode(filp)->i_mode)); | |
3213 | ||
e2f34481 | 3214 | share_ret = ksmbd_smb_check_shared_mode(fp->filp, fp); |
64b39f4a NJ |
3215 | if (!test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_OPLOCKS) || |
3216 | (req_op_level == SMB2_OPLOCK_LEVEL_LEASE && | |
3217 | !(conn->vals->capabilities & SMB2_GLOBAL_CAP_LEASING))) { | |
ab0b263b | 3218 | if (share_ret < 0 && !S_ISDIR(file_inode(fp->filp)->i_mode)) { |
e2f34481 | 3219 | rc = share_ret; |
2e450920 | 3220 | goto err_out1; |
e2f34481 NJ |
3221 | } |
3222 | } else { | |
3223 | if (req_op_level == SMB2_OPLOCK_LEVEL_LEASE) { | |
d47d9886 NJ |
3224 | /* |
3225 | * Compare parent lease using parent key. If there is no | |
3226 | * a lease that has same parent key, Send lease break | |
3227 | * notification. | |
3228 | */ | |
3229 | smb_send_parent_lease_break_noti(fp, lc); | |
3230 | ||
e2f34481 NJ |
3231 | req_op_level = smb2_map_lease_to_oplock(lc->req_state); |
3232 | ksmbd_debug(SMB, | |
070fb21e NJ |
3233 | "lease req for(%s) req oplock state 0x%x, lease state 0x%x\n", |
3234 | name, req_op_level, lc->req_state); | |
e2f34481 NJ |
3235 | rc = find_same_lease_key(sess, fp->f_ci, lc); |
3236 | if (rc) | |
2e450920 | 3237 | goto err_out1; |
e2f34481 | 3238 | } else if (open_flags == O_RDONLY && |
64b39f4a NJ |
3239 | (req_op_level == SMB2_OPLOCK_LEVEL_BATCH || |
3240 | req_op_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE)) | |
e2f34481 NJ |
3241 | req_op_level = SMB2_OPLOCK_LEVEL_II; |
3242 | ||
3243 | rc = smb_grant_oplock(work, req_op_level, | |
3244 | fp->persistent_id, fp, | |
3245 | le32_to_cpu(req->hdr.Id.SyncId.TreeId), | |
3246 | lc, share_ret); | |
3247 | if (rc < 0) | |
2e450920 | 3248 | goto err_out1; |
e2f34481 NJ |
3249 | } |
3250 | ||
3251 | if (req->CreateOptions & FILE_DELETE_ON_CLOSE_LE) | |
3252 | ksmbd_fd_set_delete_on_close(fp, file_info); | |
3253 | ||
2e450920 NJ |
3254 | if (need_truncate) { |
3255 | rc = smb2_create_truncate(&fp->filp->f_path); | |
3256 | if (rc) | |
3257 | goto err_out1; | |
3258 | } | |
3259 | ||
e2f34481 NJ |
3260 | if (req->CreateContextsOffset) { |
3261 | struct create_alloc_size_req *az_req; | |
3262 | ||
070fb21e | 3263 | az_req = (struct create_alloc_size_req *)smb2_find_context_vals(req, |
02f76c40 | 3264 | SMB2_CREATE_ALLOCATION_SIZE, 4); |
f19b3967 NJ |
3265 | if (IS_ERR(az_req)) { |
3266 | rc = PTR_ERR(az_req); | |
2e450920 | 3267 | goto err_out1; |
f19b3967 | 3268 | } else if (az_req) { |
8f77150c | 3269 | loff_t alloc_size; |
e2f34481 NJ |
3270 | int err; |
3271 | ||
8f77150c HL |
3272 | if (le16_to_cpu(az_req->ccontext.DataOffset) + |
3273 | le32_to_cpu(az_req->ccontext.DataLength) < | |
3274 | sizeof(struct create_alloc_size_req)) { | |
3275 | rc = -EINVAL; | |
2e450920 | 3276 | goto err_out1; |
8f77150c HL |
3277 | } |
3278 | alloc_size = le64_to_cpu(az_req->AllocationSize); | |
e2f34481 | 3279 | ksmbd_debug(SMB, |
070fb21e NJ |
3280 | "request smb2 create allocate size : %llu\n", |
3281 | alloc_size); | |
e8c06191 NJ |
3282 | smb_break_all_levII_oplock(work, fp, 1); |
3283 | err = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, | |
3284 | alloc_size); | |
e2f34481 NJ |
3285 | if (err < 0) |
3286 | ksmbd_debug(SMB, | |
e8c06191 | 3287 | "vfs_fallocate is failed : %d\n", |
070fb21e | 3288 | err); |
e2f34481 NJ |
3289 | } |
3290 | ||
02f76c40 | 3291 | context = smb2_find_context_vals(req, SMB2_CREATE_QUERY_ON_DISK_ID, 4); |
f19b3967 NJ |
3292 | if (IS_ERR(context)) { |
3293 | rc = PTR_ERR(context); | |
2e450920 | 3294 | goto err_out1; |
f19b3967 | 3295 | } else if (context) { |
e2f34481 NJ |
3296 | ksmbd_debug(SMB, "get query on disk id context\n"); |
3297 | query_disk_id = 1; | |
3298 | } | |
3299 | } | |
3300 | ||
a9f106c7 NJ |
3301 | rc = ksmbd_vfs_getattr(&path, &stat); |
3302 | if (rc) | |
3303 | goto err_out1; | |
3304 | ||
3305 | if (stat.result_mask & STATX_BTIME) | |
3306 | fp->create_time = ksmbd_UnixTimeToNT(stat.btime); | |
3307 | else | |
3308 | fp->create_time = ksmbd_UnixTimeToNT(stat.ctime); | |
3309 | if (req->FileAttributes || fp->f_ci->m_fattr == 0) | |
3310 | fp->f_ci->m_fattr = | |
3311 | cpu_to_le32(smb2_get_dos_mode(&stat, le32_to_cpu(req->FileAttributes))); | |
3312 | ||
3313 | if (!created) | |
3314 | smb2_update_xattrs(tcon, &path, fp); | |
3315 | else | |
3316 | smb2_new_xattrs(tcon, &path, fp); | |
3317 | ||
e2f34481 NJ |
3318 | memcpy(fp->client_guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE); |
3319 | ||
e2f34481 NJ |
3320 | rsp->StructureSize = cpu_to_le16(89); |
3321 | rcu_read_lock(); | |
3322 | opinfo = rcu_dereference(fp->f_opinfo); | |
3323 | rsp->OplockLevel = opinfo != NULL ? opinfo->level : 0; | |
3324 | rcu_read_unlock(); | |
26a2787d | 3325 | rsp->Flags = 0; |
e2f34481 NJ |
3326 | rsp->CreateAction = cpu_to_le32(file_info); |
3327 | rsp->CreationTime = cpu_to_le64(fp->create_time); | |
3328 | time = ksmbd_UnixTimeToNT(stat.atime); | |
3329 | rsp->LastAccessTime = cpu_to_le64(time); | |
3330 | time = ksmbd_UnixTimeToNT(stat.mtime); | |
3331 | rsp->LastWriteTime = cpu_to_le64(time); | |
3332 | time = ksmbd_UnixTimeToNT(stat.ctime); | |
3333 | rsp->ChangeTime = cpu_to_le64(time); | |
3334 | rsp->AllocationSize = S_ISDIR(stat.mode) ? 0 : | |
3335 | cpu_to_le64(stat.blocks << 9); | |
3336 | rsp->EndofFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); | |
3337 | rsp->FileAttributes = fp->f_ci->m_fattr; | |
3338 | ||
3339 | rsp->Reserved2 = 0; | |
3340 | ||
2d004c6c PA |
3341 | rsp->PersistentFileId = fp->persistent_id; |
3342 | rsp->VolatileFileId = fp->volatile_id; | |
e2f34481 NJ |
3343 | |
3344 | rsp->CreateContextsOffset = 0; | |
3345 | rsp->CreateContextsLength = 0; | |
e2b76ab8 | 3346 | iov_len = offsetof(struct smb2_create_rsp, Buffer); |
e2f34481 NJ |
3347 | |
3348 | /* If lease is request send lease context response */ | |
3349 | if (opinfo && opinfo->is_lease) { | |
3350 | struct create_context *lease_ccontext; | |
3351 | ||
3352 | ksmbd_debug(SMB, "lease granted on(%s) lease state 0x%x\n", | |
070fb21e | 3353 | name, opinfo->o_lease->state); |
e2f34481 NJ |
3354 | rsp->OplockLevel = SMB2_OPLOCK_LEVEL_LEASE; |
3355 | ||
3356 | lease_ccontext = (struct create_context *)rsp->Buffer; | |
3357 | contxt_cnt++; | |
3358 | create_lease_buf(rsp->Buffer, opinfo->o_lease); | |
3359 | le32_add_cpu(&rsp->CreateContextsLength, | |
3360 | conn->vals->create_lease_size); | |
e2b76ab8 | 3361 | iov_len += conn->vals->create_lease_size; |
e2f34481 NJ |
3362 | next_ptr = &lease_ccontext->Next; |
3363 | next_off = conn->vals->create_lease_size; | |
3364 | } | |
3365 | ||
e2f34481 NJ |
3366 | if (maximal_access_ctxt) { |
3367 | struct create_context *mxac_ccontext; | |
3368 | ||
3369 | if (maximal_access == 0) | |
4609e1f1 | 3370 | ksmbd_vfs_query_maximal_access(idmap, |
af34983e | 3371 | path.dentry, |
e2f34481 NJ |
3372 | &maximal_access); |
3373 | mxac_ccontext = (struct create_context *)(rsp->Buffer + | |
3374 | le32_to_cpu(rsp->CreateContextsLength)); | |
3375 | contxt_cnt++; | |
3376 | create_mxac_rsp_buf(rsp->Buffer + | |
3377 | le32_to_cpu(rsp->CreateContextsLength), | |
3378 | le32_to_cpu(maximal_access)); | |
3379 | le32_add_cpu(&rsp->CreateContextsLength, | |
3380 | conn->vals->create_mxac_size); | |
e2b76ab8 | 3381 | iov_len += conn->vals->create_mxac_size; |
e2f34481 NJ |
3382 | if (next_ptr) |
3383 | *next_ptr = cpu_to_le32(next_off); | |
3384 | next_ptr = &mxac_ccontext->Next; | |
3385 | next_off = conn->vals->create_mxac_size; | |
3386 | } | |
3387 | ||
3388 | if (query_disk_id) { | |
3389 | struct create_context *disk_id_ccontext; | |
3390 | ||
3391 | disk_id_ccontext = (struct create_context *)(rsp->Buffer + | |
3392 | le32_to_cpu(rsp->CreateContextsLength)); | |
3393 | contxt_cnt++; | |
3394 | create_disk_id_rsp_buf(rsp->Buffer + | |
3395 | le32_to_cpu(rsp->CreateContextsLength), | |
3396 | stat.ino, tcon->id); | |
3397 | le32_add_cpu(&rsp->CreateContextsLength, | |
3398 | conn->vals->create_disk_id_size); | |
e2b76ab8 | 3399 | iov_len += conn->vals->create_disk_id_size; |
e2f34481 NJ |
3400 | if (next_ptr) |
3401 | *next_ptr = cpu_to_le32(next_off); | |
3402 | next_ptr = &disk_id_ccontext->Next; | |
3403 | next_off = conn->vals->create_disk_id_size; | |
3404 | } | |
3405 | ||
3406 | if (posix_ctxt) { | |
e2f34481 NJ |
3407 | contxt_cnt++; |
3408 | create_posix_rsp_buf(rsp->Buffer + | |
3409 | le32_to_cpu(rsp->CreateContextsLength), | |
3410 | fp); | |
3411 | le32_add_cpu(&rsp->CreateContextsLength, | |
3412 | conn->vals->create_posix_size); | |
e2b76ab8 | 3413 | iov_len += conn->vals->create_posix_size; |
e2f34481 NJ |
3414 | if (next_ptr) |
3415 | *next_ptr = cpu_to_le32(next_off); | |
3416 | } | |
3417 | ||
3418 | if (contxt_cnt > 0) { | |
3419 | rsp->CreateContextsOffset = | |
cb451720 | 3420 | cpu_to_le32(offsetof(struct smb2_create_rsp, Buffer)); |
e2f34481 NJ |
3421 | } |
3422 | ||
3423 | err_out: | |
2e450920 | 3424 | if (rc && (file_present || created)) |
864fb5d3 NJ |
3425 | ksmbd_vfs_kern_path_unlock(&parent_path, &path); |
3426 | ||
e2f34481 | 3427 | err_out1: |
2e450920 NJ |
3428 | ksmbd_revert_fsids(work); |
3429 | ||
3430 | err_out2: | |
5a7ee91d NJ |
3431 | if (!rc) { |
3432 | ksmbd_update_fstate(&work->sess->file_table, fp, FP_INITED); | |
e2b76ab8 | 3433 | rc = ksmbd_iov_pin_rsp(work, (void *)rsp, iov_len); |
5a7ee91d | 3434 | } |
e2f34481 NJ |
3435 | if (rc) { |
3436 | if (rc == -EINVAL) | |
3437 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
3438 | else if (rc == -EOPNOTSUPP) | |
3439 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; | |
265fd199 | 3440 | else if (rc == -EACCES || rc == -ESTALE || rc == -EXDEV) |
e2f34481 NJ |
3441 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
3442 | else if (rc == -ENOENT) | |
3443 | rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; | |
3444 | else if (rc == -EPERM) | |
3445 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; | |
3446 | else if (rc == -EBUSY) | |
3447 | rsp->hdr.Status = STATUS_DELETE_PENDING; | |
3448 | else if (rc == -EBADF) | |
3449 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; | |
3450 | else if (rc == -ENOEXEC) | |
3451 | rsp->hdr.Status = STATUS_DUPLICATE_OBJECTID; | |
3452 | else if (rc == -ENXIO) | |
3453 | rsp->hdr.Status = STATUS_NO_SUCH_DEVICE; | |
3454 | else if (rc == -EEXIST) | |
3455 | rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; | |
3456 | else if (rc == -EMFILE) | |
3457 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; | |
3458 | if (!rsp->hdr.Status) | |
3459 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; | |
3460 | ||
e2f34481 NJ |
3461 | if (fp) |
3462 | ksmbd_fd_put(work, fp); | |
3463 | smb2_set_err_rsp(work); | |
3464 | ksmbd_debug(SMB, "Error response: %x\n", rsp->hdr.Status); | |
3465 | } | |
3466 | ||
50f500b7 | 3467 | kfree(name); |
e2f34481 NJ |
3468 | kfree(lc); |
3469 | ||
3470 | return 0; | |
3471 | } | |
3472 | ||
3473 | static int readdir_info_level_struct_sz(int info_level) | |
3474 | { | |
3475 | switch (info_level) { | |
3476 | case FILE_FULL_DIRECTORY_INFORMATION: | |
3477 | return sizeof(struct file_full_directory_info); | |
3478 | case FILE_BOTH_DIRECTORY_INFORMATION: | |
3479 | return sizeof(struct file_both_directory_info); | |
3480 | case FILE_DIRECTORY_INFORMATION: | |
3481 | return sizeof(struct file_directory_info); | |
3482 | case FILE_NAMES_INFORMATION: | |
3483 | return sizeof(struct file_names_info); | |
3484 | case FILEID_FULL_DIRECTORY_INFORMATION: | |
3485 | return sizeof(struct file_id_full_dir_info); | |
3486 | case FILEID_BOTH_DIRECTORY_INFORMATION: | |
3487 | return sizeof(struct file_id_both_directory_info); | |
3488 | case SMB_FIND_FILE_POSIX_INFO: | |
3489 | return sizeof(struct smb2_posix_info); | |
3490 | default: | |
3491 | return -EOPNOTSUPP; | |
3492 | } | |
3493 | } | |
3494 | ||
3495 | static int dentry_name(struct ksmbd_dir_info *d_info, int info_level) | |
3496 | { | |
3497 | switch (info_level) { | |
3498 | case FILE_FULL_DIRECTORY_INFORMATION: | |
3499 | { | |
3500 | struct file_full_directory_info *ffdinfo; | |
3501 | ||
3502 | ffdinfo = (struct file_full_directory_info *)d_info->rptr; | |
3503 | d_info->rptr += le32_to_cpu(ffdinfo->NextEntryOffset); | |
3504 | d_info->name = ffdinfo->FileName; | |
3505 | d_info->name_len = le32_to_cpu(ffdinfo->FileNameLength); | |
3506 | return 0; | |
3507 | } | |
3508 | case FILE_BOTH_DIRECTORY_INFORMATION: | |
3509 | { | |
3510 | struct file_both_directory_info *fbdinfo; | |
3511 | ||
3512 | fbdinfo = (struct file_both_directory_info *)d_info->rptr; | |
3513 | d_info->rptr += le32_to_cpu(fbdinfo->NextEntryOffset); | |
3514 | d_info->name = fbdinfo->FileName; | |
3515 | d_info->name_len = le32_to_cpu(fbdinfo->FileNameLength); | |
3516 | return 0; | |
3517 | } | |
3518 | case FILE_DIRECTORY_INFORMATION: | |
3519 | { | |
3520 | struct file_directory_info *fdinfo; | |
3521 | ||
3522 | fdinfo = (struct file_directory_info *)d_info->rptr; | |
3523 | d_info->rptr += le32_to_cpu(fdinfo->NextEntryOffset); | |
3524 | d_info->name = fdinfo->FileName; | |
3525 | d_info->name_len = le32_to_cpu(fdinfo->FileNameLength); | |
3526 | return 0; | |
3527 | } | |
3528 | case FILE_NAMES_INFORMATION: | |
3529 | { | |
3530 | struct file_names_info *fninfo; | |
3531 | ||
3532 | fninfo = (struct file_names_info *)d_info->rptr; | |
3533 | d_info->rptr += le32_to_cpu(fninfo->NextEntryOffset); | |
3534 | d_info->name = fninfo->FileName; | |
3535 | d_info->name_len = le32_to_cpu(fninfo->FileNameLength); | |
3536 | return 0; | |
3537 | } | |
3538 | case FILEID_FULL_DIRECTORY_INFORMATION: | |
3539 | { | |
3540 | struct file_id_full_dir_info *dinfo; | |
3541 | ||
3542 | dinfo = (struct file_id_full_dir_info *)d_info->rptr; | |
3543 | d_info->rptr += le32_to_cpu(dinfo->NextEntryOffset); | |
3544 | d_info->name = dinfo->FileName; | |
3545 | d_info->name_len = le32_to_cpu(dinfo->FileNameLength); | |
3546 | return 0; | |
3547 | } | |
3548 | case FILEID_BOTH_DIRECTORY_INFORMATION: | |
3549 | { | |
3550 | struct file_id_both_directory_info *fibdinfo; | |
3551 | ||
3552 | fibdinfo = (struct file_id_both_directory_info *)d_info->rptr; | |
3553 | d_info->rptr += le32_to_cpu(fibdinfo->NextEntryOffset); | |
3554 | d_info->name = fibdinfo->FileName; | |
3555 | d_info->name_len = le32_to_cpu(fibdinfo->FileNameLength); | |
3556 | return 0; | |
3557 | } | |
3558 | case SMB_FIND_FILE_POSIX_INFO: | |
3559 | { | |
3560 | struct smb2_posix_info *posix_info; | |
3561 | ||
3562 | posix_info = (struct smb2_posix_info *)d_info->rptr; | |
3563 | d_info->rptr += le32_to_cpu(posix_info->NextEntryOffset); | |
3564 | d_info->name = posix_info->name; | |
3565 | d_info->name_len = le32_to_cpu(posix_info->name_len); | |
3566 | return 0; | |
3567 | } | |
3568 | default: | |
3569 | return -EINVAL; | |
3570 | } | |
3571 | } | |
3572 | ||
3573 | /** | |
3574 | * smb2_populate_readdir_entry() - encode directory entry in smb2 response | |
3575 | * buffer | |
3576 | * @conn: connection instance | |
3577 | * @info_level: smb information level | |
3578 | * @d_info: structure included variables for query dir | |
3579 | * @ksmbd_kstat: ksmbd wrapper of dirent stat information | |
3580 | * | |
3581 | * if directory has many entries, find first can't read it fully. | |
3582 | * find next might be called multiple times to read remaining dir entries | |
3583 | * | |
3584 | * Return: 0 on success, otherwise error | |
3585 | */ | |
64b39f4a | 3586 | static int smb2_populate_readdir_entry(struct ksmbd_conn *conn, int info_level, |
070fb21e NJ |
3587 | struct ksmbd_dir_info *d_info, |
3588 | struct ksmbd_kstat *ksmbd_kstat) | |
e2f34481 NJ |
3589 | { |
3590 | int next_entry_offset = 0; | |
3591 | char *conv_name; | |
3592 | int conv_len; | |
3593 | void *kstat; | |
dac0ec6e | 3594 | int struct_sz, rc = 0; |
e2f34481 NJ |
3595 | |
3596 | conv_name = ksmbd_convert_dir_info_name(d_info, | |
3597 | conn->local_nls, | |
3598 | &conv_len); | |
3599 | if (!conv_name) | |
3600 | return -ENOMEM; | |
3601 | ||
3602 | /* Somehow the name has only terminating NULL bytes */ | |
3603 | if (conv_len < 0) { | |
dac0ec6e NJ |
3604 | rc = -EINVAL; |
3605 | goto free_conv_name; | |
e2f34481 NJ |
3606 | } |
3607 | ||
d272e01f | 3608 | struct_sz = readdir_info_level_struct_sz(info_level) + conv_len; |
04e26094 NJ |
3609 | next_entry_offset = ALIGN(struct_sz, KSMBD_DIR_INFO_ALIGNMENT); |
3610 | d_info->last_entry_off_align = next_entry_offset - struct_sz; | |
e2f34481 NJ |
3611 | |
3612 | if (next_entry_offset > d_info->out_buf_len) { | |
3613 | d_info->out_buf_len = 0; | |
dac0ec6e NJ |
3614 | rc = -ENOSPC; |
3615 | goto free_conv_name; | |
e2f34481 NJ |
3616 | } |
3617 | ||
3618 | kstat = d_info->wptr; | |
3619 | if (info_level != FILE_NAMES_INFORMATION) | |
3620 | kstat = ksmbd_vfs_init_kstat(&d_info->wptr, ksmbd_kstat); | |
3621 | ||
3622 | switch (info_level) { | |
3623 | case FILE_FULL_DIRECTORY_INFORMATION: | |
3624 | { | |
3625 | struct file_full_directory_info *ffdinfo; | |
3626 | ||
3627 | ffdinfo = (struct file_full_directory_info *)kstat; | |
3628 | ffdinfo->FileNameLength = cpu_to_le32(conv_len); | |
3629 | ffdinfo->EaSize = | |
3630 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); | |
3631 | if (ffdinfo->EaSize) | |
26a2787d | 3632 | ffdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
e2f34481 | 3633 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
26a2787d | 3634 | ffdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
e2f34481 NJ |
3635 | memcpy(ffdinfo->FileName, conv_name, conv_len); |
3636 | ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); | |
3637 | break; | |
3638 | } | |
3639 | case FILE_BOTH_DIRECTORY_INFORMATION: | |
3640 | { | |
3641 | struct file_both_directory_info *fbdinfo; | |
3642 | ||
3643 | fbdinfo = (struct file_both_directory_info *)kstat; | |
3644 | fbdinfo->FileNameLength = cpu_to_le32(conv_len); | |
3645 | fbdinfo->EaSize = | |
3646 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); | |
3647 | if (fbdinfo->EaSize) | |
26a2787d | 3648 | fbdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
e2f34481 NJ |
3649 | fbdinfo->ShortNameLength = 0; |
3650 | fbdinfo->Reserved = 0; | |
3651 | if (d_info->hide_dot_file && d_info->name[0] == '.') | |
26a2787d | 3652 | fbdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
e2f34481 NJ |
3653 | memcpy(fbdinfo->FileName, conv_name, conv_len); |
3654 | fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); | |
3655 | break; | |
3656 | } | |
3657 | case FILE_DIRECTORY_INFORMATION: | |
3658 | { | |
3659 | struct file_directory_info *fdinfo; | |
3660 | ||
3661 | fdinfo = (struct file_directory_info *)kstat; | |
3662 | fdinfo->FileNameLength = cpu_to_le32(conv_len); | |
3663 | if (d_info->hide_dot_file && d_info->name[0] == '.') | |
26a2787d | 3664 | fdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
e2f34481 NJ |
3665 | memcpy(fdinfo->FileName, conv_name, conv_len); |
3666 | fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); | |
3667 | break; | |
3668 | } | |
3669 | case FILE_NAMES_INFORMATION: | |
3670 | { | |
3671 | struct file_names_info *fninfo; | |
3672 | ||
3673 | fninfo = (struct file_names_info *)kstat; | |
3674 | fninfo->FileNameLength = cpu_to_le32(conv_len); | |
3675 | memcpy(fninfo->FileName, conv_name, conv_len); | |
3676 | fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); | |
3677 | break; | |
3678 | } | |
3679 | case FILEID_FULL_DIRECTORY_INFORMATION: | |
3680 | { | |
3681 | struct file_id_full_dir_info *dinfo; | |
3682 | ||
3683 | dinfo = (struct file_id_full_dir_info *)kstat; | |
3684 | dinfo->FileNameLength = cpu_to_le32(conv_len); | |
3685 | dinfo->EaSize = | |
3686 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); | |
3687 | if (dinfo->EaSize) | |
26a2787d | 3688 | dinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
e2f34481 NJ |
3689 | dinfo->Reserved = 0; |
3690 | dinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); | |
3691 | if (d_info->hide_dot_file && d_info->name[0] == '.') | |
26a2787d | 3692 | dinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
e2f34481 NJ |
3693 | memcpy(dinfo->FileName, conv_name, conv_len); |
3694 | dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); | |
3695 | break; | |
3696 | } | |
3697 | case FILEID_BOTH_DIRECTORY_INFORMATION: | |
3698 | { | |
3699 | struct file_id_both_directory_info *fibdinfo; | |
3700 | ||
3701 | fibdinfo = (struct file_id_both_directory_info *)kstat; | |
3702 | fibdinfo->FileNameLength = cpu_to_le32(conv_len); | |
3703 | fibdinfo->EaSize = | |
3704 | smb2_get_reparse_tag_special_file(ksmbd_kstat->kstat->mode); | |
3705 | if (fibdinfo->EaSize) | |
26a2787d | 3706 | fibdinfo->ExtFileAttributes = FILE_ATTRIBUTE_REPARSE_POINT_LE; |
e2f34481 NJ |
3707 | fibdinfo->UniqueId = cpu_to_le64(ksmbd_kstat->kstat->ino); |
3708 | fibdinfo->ShortNameLength = 0; | |
3709 | fibdinfo->Reserved = 0; | |
3710 | fibdinfo->Reserved2 = cpu_to_le16(0); | |
3711 | if (d_info->hide_dot_file && d_info->name[0] == '.') | |
26a2787d | 3712 | fibdinfo->ExtFileAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
e2f34481 NJ |
3713 | memcpy(fibdinfo->FileName, conv_name, conv_len); |
3714 | fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); | |
3715 | break; | |
3716 | } | |
3717 | case SMB_FIND_FILE_POSIX_INFO: | |
3718 | { | |
3719 | struct smb2_posix_info *posix_info; | |
3720 | u64 time; | |
3721 | ||
3722 | posix_info = (struct smb2_posix_info *)kstat; | |
3723 | posix_info->Ignored = 0; | |
3724 | posix_info->CreationTime = cpu_to_le64(ksmbd_kstat->create_time); | |
3725 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->ctime); | |
3726 | posix_info->ChangeTime = cpu_to_le64(time); | |
3727 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->atime); | |
3728 | posix_info->LastAccessTime = cpu_to_le64(time); | |
3729 | time = ksmbd_UnixTimeToNT(ksmbd_kstat->kstat->mtime); | |
3730 | posix_info->LastWriteTime = cpu_to_le64(time); | |
3731 | posix_info->EndOfFile = cpu_to_le64(ksmbd_kstat->kstat->size); | |
3732 | posix_info->AllocationSize = cpu_to_le64(ksmbd_kstat->kstat->blocks << 9); | |
3733 | posix_info->DeviceId = cpu_to_le32(ksmbd_kstat->kstat->rdev); | |
3734 | posix_info->HardLinks = cpu_to_le32(ksmbd_kstat->kstat->nlink); | |
f6c2b201 | 3735 | posix_info->Mode = cpu_to_le32(ksmbd_kstat->kstat->mode & 0777); |
e2f34481 NJ |
3736 | posix_info->Inode = cpu_to_le64(ksmbd_kstat->kstat->ino); |
3737 | posix_info->DosAttributes = | |
26a2787d RS |
3738 | S_ISDIR(ksmbd_kstat->kstat->mode) ? |
3739 | FILE_ATTRIBUTE_DIRECTORY_LE : FILE_ATTRIBUTE_ARCHIVE_LE; | |
e2f34481 | 3740 | if (d_info->hide_dot_file && d_info->name[0] == '.') |
26a2787d | 3741 | posix_info->DosAttributes |= FILE_ATTRIBUTE_HIDDEN_LE; |
5609bdd9 NJ |
3742 | /* |
3743 | * SidBuffer(32) contain two sids(Domain sid(16), UNIX group sid(16)). | |
3744 | * UNIX sid(16) = revision(1) + num_subauth(1) + authority(6) + | |
3745 | * sub_auth(4 * 1(num_subauth)) + RID(4). | |
3746 | */ | |
475d6f98 | 3747 | id_to_sid(from_kuid_munged(&init_user_ns, ksmbd_kstat->kstat->uid), |
5609bdd9 | 3748 | SIDUNIX_USER, (struct smb_sid *)&posix_info->SidBuffer[0]); |
475d6f98 | 3749 | id_to_sid(from_kgid_munged(&init_user_ns, ksmbd_kstat->kstat->gid), |
5609bdd9 | 3750 | SIDUNIX_GROUP, (struct smb_sid *)&posix_info->SidBuffer[16]); |
e2f34481 NJ |
3751 | memcpy(posix_info->name, conv_name, conv_len); |
3752 | posix_info->name_len = cpu_to_le32(conv_len); | |
3753 | posix_info->NextEntryOffset = cpu_to_le32(next_entry_offset); | |
3754 | break; | |
3755 | } | |
3756 | ||
3757 | } /* switch (info_level) */ | |
3758 | ||
3759 | d_info->last_entry_offset = d_info->data_count; | |
3760 | d_info->data_count += next_entry_offset; | |
e7735c85 | 3761 | d_info->out_buf_len -= next_entry_offset; |
e2f34481 | 3762 | d_info->wptr += next_entry_offset; |
e2f34481 NJ |
3763 | |
3764 | ksmbd_debug(SMB, | |
070fb21e NJ |
3765 | "info_level : %d, buf_len :%d, next_offset : %d, data_count : %d\n", |
3766 | info_level, d_info->out_buf_len, | |
3767 | next_entry_offset, d_info->data_count); | |
e2f34481 | 3768 | |
dac0ec6e NJ |
3769 | free_conv_name: |
3770 | kfree(conv_name); | |
3771 | return rc; | |
e2f34481 NJ |
3772 | } |
3773 | ||
3774 | struct smb2_query_dir_private { | |
3775 | struct ksmbd_work *work; | |
3776 | char *search_pattern; | |
3777 | struct ksmbd_file *dir_fp; | |
3778 | ||
3779 | struct ksmbd_dir_info *d_info; | |
3780 | int info_level; | |
3781 | }; | |
3782 | ||
3783 | static void lock_dir(struct ksmbd_file *dir_fp) | |
3784 | { | |
3785 | struct dentry *dir = dir_fp->filp->f_path.dentry; | |
3786 | ||
3787 | inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); | |
3788 | } | |
3789 | ||
3790 | static void unlock_dir(struct ksmbd_file *dir_fp) | |
3791 | { | |
3792 | struct dentry *dir = dir_fp->filp->f_path.dentry; | |
3793 | ||
3794 | inode_unlock(d_inode(dir)); | |
3795 | } | |
3796 | ||
3797 | static int process_query_dir_entries(struct smb2_query_dir_private *priv) | |
3798 | { | |
b74d24f7 | 3799 | struct mnt_idmap *idmap = file_mnt_idmap(priv->dir_fp->filp); |
e2f34481 NJ |
3800 | struct kstat kstat; |
3801 | struct ksmbd_kstat ksmbd_kstat; | |
3802 | int rc; | |
3803 | int i; | |
3804 | ||
3805 | for (i = 0; i < priv->d_info->num_entry; i++) { | |
3806 | struct dentry *dent; | |
3807 | ||
3808 | if (dentry_name(priv->d_info, priv->info_level)) | |
3809 | return -EINVAL; | |
3810 | ||
3811 | lock_dir(priv->dir_fp); | |
4609e1f1 | 3812 | dent = lookup_one(idmap, priv->d_info->name, |
da1e7ada CB |
3813 | priv->dir_fp->filp->f_path.dentry, |
3814 | priv->d_info->name_len); | |
e2f34481 NJ |
3815 | unlock_dir(priv->dir_fp); |
3816 | ||
3817 | if (IS_ERR(dent)) { | |
3818 | ksmbd_debug(SMB, "Cannot lookup `%s' [%ld]\n", | |
070fb21e NJ |
3819 | priv->d_info->name, |
3820 | PTR_ERR(dent)); | |
e2f34481 NJ |
3821 | continue; |
3822 | } | |
3823 | if (unlikely(d_is_negative(dent))) { | |
3824 | dput(dent); | |
3825 | ksmbd_debug(SMB, "Negative dentry `%s'\n", | |
3826 | priv->d_info->name); | |
3827 | continue; | |
3828 | } | |
3829 | ||
3830 | ksmbd_kstat.kstat = &kstat; | |
3831 | if (priv->info_level != FILE_NAMES_INFORMATION) | |
3832 | ksmbd_vfs_fill_dentry_attrs(priv->work, | |
b74d24f7 | 3833 | idmap, |
e2f34481 NJ |
3834 | dent, |
3835 | &ksmbd_kstat); | |
3836 | ||
3837 | rc = smb2_populate_readdir_entry(priv->work->conn, | |
3838 | priv->info_level, | |
3839 | priv->d_info, | |
3840 | &ksmbd_kstat); | |
3841 | dput(dent); | |
3842 | if (rc) | |
3843 | return rc; | |
3844 | } | |
3845 | return 0; | |
3846 | } | |
3847 | ||
3848 | static int reserve_populate_dentry(struct ksmbd_dir_info *d_info, | |
070fb21e | 3849 | int info_level) |
e2f34481 NJ |
3850 | { |
3851 | int struct_sz; | |
3852 | int conv_len; | |
3853 | int next_entry_offset; | |
3854 | ||
3855 | struct_sz = readdir_info_level_struct_sz(info_level); | |
3856 | if (struct_sz == -EOPNOTSUPP) | |
3857 | return -EOPNOTSUPP; | |
3858 | ||
3859 | conv_len = (d_info->name_len + 1) * 2; | |
d272e01f | 3860 | next_entry_offset = ALIGN(struct_sz + conv_len, |
e2f34481 NJ |
3861 | KSMBD_DIR_INFO_ALIGNMENT); |
3862 | ||
3863 | if (next_entry_offset > d_info->out_buf_len) { | |
3864 | d_info->out_buf_len = 0; | |
3865 | return -ENOSPC; | |
3866 | } | |
3867 | ||
3868 | switch (info_level) { | |
3869 | case FILE_FULL_DIRECTORY_INFORMATION: | |
3870 | { | |
3871 | struct file_full_directory_info *ffdinfo; | |
3872 | ||
3873 | ffdinfo = (struct file_full_directory_info *)d_info->wptr; | |
3874 | memcpy(ffdinfo->FileName, d_info->name, d_info->name_len); | |
3875 | ffdinfo->FileName[d_info->name_len] = 0x00; | |
3876 | ffdinfo->FileNameLength = cpu_to_le32(d_info->name_len); | |
3877 | ffdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); | |
3878 | break; | |
3879 | } | |
3880 | case FILE_BOTH_DIRECTORY_INFORMATION: | |
3881 | { | |
3882 | struct file_both_directory_info *fbdinfo; | |
3883 | ||
3884 | fbdinfo = (struct file_both_directory_info *)d_info->wptr; | |
3885 | memcpy(fbdinfo->FileName, d_info->name, d_info->name_len); | |
3886 | fbdinfo->FileName[d_info->name_len] = 0x00; | |
3887 | fbdinfo->FileNameLength = cpu_to_le32(d_info->name_len); | |
3888 | fbdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); | |
3889 | break; | |
3890 | } | |
3891 | case FILE_DIRECTORY_INFORMATION: | |
3892 | { | |
3893 | struct file_directory_info *fdinfo; | |
3894 | ||
3895 | fdinfo = (struct file_directory_info *)d_info->wptr; | |
3896 | memcpy(fdinfo->FileName, d_info->name, d_info->name_len); | |
3897 | fdinfo->FileName[d_info->name_len] = 0x00; | |
3898 | fdinfo->FileNameLength = cpu_to_le32(d_info->name_len); | |
3899 | fdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); | |
3900 | break; | |
3901 | } | |
3902 | case FILE_NAMES_INFORMATION: | |
3903 | { | |
3904 | struct file_names_info *fninfo; | |
3905 | ||
3906 | fninfo = (struct file_names_info *)d_info->wptr; | |
3907 | memcpy(fninfo->FileName, d_info->name, d_info->name_len); | |
3908 | fninfo->FileName[d_info->name_len] = 0x00; | |
3909 | fninfo->FileNameLength = cpu_to_le32(d_info->name_len); | |
3910 | fninfo->NextEntryOffset = cpu_to_le32(next_entry_offset); | |
3911 | break; | |
3912 | } | |
3913 | case FILEID_FULL_DIRECTORY_INFORMATION: | |
3914 | { | |
3915 | struct file_id_full_dir_info *dinfo; | |
3916 | ||
3917 | dinfo = (struct file_id_full_dir_info *)d_info->wptr; | |
3918 | memcpy(dinfo->FileName, d_info->name, d_info->name_len); | |
3919 | dinfo->FileName[d_info->name_len] = 0x00; | |
3920 | dinfo->FileNameLength = cpu_to_le32(d_info->name_len); | |
3921 | dinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); | |
3922 | break; | |
3923 | } | |
3924 | case FILEID_BOTH_DIRECTORY_INFORMATION: | |
3925 | { | |
3926 | struct file_id_both_directory_info *fibdinfo; | |
3927 | ||
3928 | fibdinfo = (struct file_id_both_directory_info *)d_info->wptr; | |
3929 | memcpy(fibdinfo->FileName, d_info->name, d_info->name_len); | |
3930 | fibdinfo->FileName[d_info->name_len] = 0x00; | |
3931 | fibdinfo->FileNameLength = cpu_to_le32(d_info->name_len); | |
3932 | fibdinfo->NextEntryOffset = cpu_to_le32(next_entry_offset); | |
3933 | break; | |
3934 | } | |
3935 | case SMB_FIND_FILE_POSIX_INFO: | |
3936 | { | |
3937 | struct smb2_posix_info *posix_info; | |
3938 | ||
3939 | posix_info = (struct smb2_posix_info *)d_info->wptr; | |
3940 | memcpy(posix_info->name, d_info->name, d_info->name_len); | |
3941 | posix_info->name[d_info->name_len] = 0x00; | |
3942 | posix_info->name_len = cpu_to_le32(d_info->name_len); | |
3943 | posix_info->NextEntryOffset = | |
3944 | cpu_to_le32(next_entry_offset); | |
3945 | break; | |
3946 | } | |
3947 | } /* switch (info_level) */ | |
3948 | ||
3949 | d_info->num_entry++; | |
3950 | d_info->out_buf_len -= next_entry_offset; | |
3951 | d_info->wptr += next_entry_offset; | |
3952 | return 0; | |
3953 | } | |
3954 | ||
25885a35 | 3955 | static bool __query_dir(struct dir_context *ctx, const char *name, int namlen, |
070fb21e | 3956 | loff_t offset, u64 ino, unsigned int d_type) |
e2f34481 NJ |
3957 | { |
3958 | struct ksmbd_readdir_data *buf; | |
3959 | struct smb2_query_dir_private *priv; | |
3960 | struct ksmbd_dir_info *d_info; | |
3961 | int rc; | |
3962 | ||
3963 | buf = container_of(ctx, struct ksmbd_readdir_data, ctx); | |
3964 | priv = buf->private; | |
3965 | d_info = priv->d_info; | |
3966 | ||
3967 | /* dot and dotdot entries are already reserved */ | |
3968 | if (!strcmp(".", name) || !strcmp("..", name)) | |
25885a35 | 3969 | return true; |
e2f34481 | 3970 | if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name)) |
25885a35 | 3971 | return true; |
b24c9335 | 3972 | if (!match_pattern(name, namlen, priv->search_pattern)) |
25885a35 | 3973 | return true; |
e2f34481 NJ |
3974 | |
3975 | d_info->name = name; | |
3976 | d_info->name_len = namlen; | |
3977 | rc = reserve_populate_dentry(d_info, priv->info_level); | |
3978 | if (rc) | |
25885a35 AV |
3979 | return false; |
3980 | if (d_info->flags & SMB2_RETURN_SINGLE_ENTRY) | |
e2f34481 | 3981 | d_info->out_buf_len = 0; |
25885a35 | 3982 | return true; |
e2f34481 NJ |
3983 | } |
3984 | ||
e2f34481 NJ |
3985 | static int verify_info_level(int info_level) |
3986 | { | |
3987 | switch (info_level) { | |
3988 | case FILE_FULL_DIRECTORY_INFORMATION: | |
3989 | case FILE_BOTH_DIRECTORY_INFORMATION: | |
3990 | case FILE_DIRECTORY_INFORMATION: | |
3991 | case FILE_NAMES_INFORMATION: | |
3992 | case FILEID_FULL_DIRECTORY_INFORMATION: | |
3993 | case FILEID_BOTH_DIRECTORY_INFORMATION: | |
3994 | case SMB_FIND_FILE_POSIX_INFO: | |
3995 | break; | |
3996 | default: | |
3997 | return -EOPNOTSUPP; | |
3998 | } | |
3999 | ||
4000 | return 0; | |
4001 | } | |
4002 | ||
8f054118 NJ |
4003 | static int smb2_resp_buf_len(struct ksmbd_work *work, unsigned short hdr2_len) |
4004 | { | |
4005 | int free_len; | |
4006 | ||
4007 | free_len = (int)(work->response_sz - | |
4008 | (get_rfc1002_len(work->response_buf) + 4)) - hdr2_len; | |
4009 | return free_len; | |
4010 | } | |
4011 | ||
34061d6b HL |
4012 | static int smb2_calc_max_out_buf_len(struct ksmbd_work *work, |
4013 | unsigned short hdr2_len, | |
4014 | unsigned int out_buf_len) | |
4015 | { | |
4016 | int free_len; | |
4017 | ||
4018 | if (out_buf_len > work->conn->vals->max_trans_size) | |
4019 | return -EINVAL; | |
4020 | ||
8f054118 | 4021 | free_len = smb2_resp_buf_len(work, hdr2_len); |
34061d6b HL |
4022 | if (free_len < 0) |
4023 | return -EINVAL; | |
4024 | ||
4025 | return min_t(int, out_buf_len, free_len); | |
4026 | } | |
4027 | ||
e2f34481 NJ |
4028 | int smb2_query_dir(struct ksmbd_work *work) |
4029 | { | |
4030 | struct ksmbd_conn *conn = work->conn; | |
4031 | struct smb2_query_directory_req *req; | |
cb451720 | 4032 | struct smb2_query_directory_rsp *rsp; |
e2f34481 NJ |
4033 | struct ksmbd_share_config *share = work->tcon->share_conf; |
4034 | struct ksmbd_file *dir_fp = NULL; | |
4035 | struct ksmbd_dir_info d_info; | |
4036 | int rc = 0; | |
4037 | char *srch_ptr = NULL; | |
4038 | unsigned char srch_flag; | |
4039 | int buffer_sz; | |
4040 | struct smb2_query_dir_private query_dir_private = {NULL, }; | |
4041 | ||
e2f34481 NJ |
4042 | WORK_BUFFERS(work, req, rsp); |
4043 | ||
4044 | if (ksmbd_override_fsids(work)) { | |
4045 | rsp->hdr.Status = STATUS_NO_MEMORY; | |
4046 | smb2_set_err_rsp(work); | |
4047 | return -ENOMEM; | |
4048 | } | |
4049 | ||
4050 | rc = verify_info_level(req->FileInformationClass); | |
4051 | if (rc) { | |
4052 | rc = -EFAULT; | |
4053 | goto err_out2; | |
4054 | } | |
4055 | ||
2d004c6c | 4056 | dir_fp = ksmbd_lookup_fd_slow(work, req->VolatileFileId, req->PersistentFileId); |
e2f34481 NJ |
4057 | if (!dir_fp) { |
4058 | rc = -EBADF; | |
4059 | goto err_out2; | |
4060 | } | |
4061 | ||
4062 | if (!(dir_fp->daccess & FILE_LIST_DIRECTORY_LE) || | |
4609e1f1 | 4063 | inode_permission(file_mnt_idmap(dir_fp->filp), |
af34983e | 4064 | file_inode(dir_fp->filp), |
070fb21e | 4065 | MAY_READ | MAY_EXEC)) { |
369c1634 | 4066 | pr_err("no right to enumerate directory (%pD)\n", dir_fp->filp); |
e2f34481 NJ |
4067 | rc = -EACCES; |
4068 | goto err_out2; | |
4069 | } | |
4070 | ||
4071 | if (!S_ISDIR(file_inode(dir_fp->filp)->i_mode)) { | |
bde1694a | 4072 | pr_err("can't do query dir for a file\n"); |
e2f34481 NJ |
4073 | rc = -EINVAL; |
4074 | goto err_out2; | |
4075 | } | |
4076 | ||
4077 | srch_flag = req->Flags; | |
4078 | srch_ptr = smb_strndup_from_utf16(req->Buffer, | |
070fb21e NJ |
4079 | le16_to_cpu(req->FileNameLength), 1, |
4080 | conn->local_nls); | |
e2f34481 NJ |
4081 | if (IS_ERR(srch_ptr)) { |
4082 | ksmbd_debug(SMB, "Search Pattern not found\n"); | |
4083 | rc = -EINVAL; | |
4084 | goto err_out2; | |
64b39f4a | 4085 | } else { |
e2f34481 | 4086 | ksmbd_debug(SMB, "Search pattern is %s\n", srch_ptr); |
64b39f4a | 4087 | } |
e2f34481 | 4088 | |
e2f34481 NJ |
4089 | if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) { |
4090 | ksmbd_debug(SMB, "Restart directory scan\n"); | |
4091 | generic_file_llseek(dir_fp->filp, 0, SEEK_SET); | |
e2f34481 NJ |
4092 | } |
4093 | ||
4094 | memset(&d_info, 0, sizeof(struct ksmbd_dir_info)); | |
4095 | d_info.wptr = (char *)rsp->Buffer; | |
4096 | d_info.rptr = (char *)rsp->Buffer; | |
34061d6b HL |
4097 | d_info.out_buf_len = |
4098 | smb2_calc_max_out_buf_len(work, 8, | |
4099 | le32_to_cpu(req->OutputBufferLength)); | |
4100 | if (d_info.out_buf_len < 0) { | |
4101 | rc = -EINVAL; | |
4102 | goto err_out; | |
4103 | } | |
e2f34481 NJ |
4104 | d_info.flags = srch_flag; |
4105 | ||
4106 | /* | |
4107 | * reserve dot and dotdot entries in head of buffer | |
4108 | * in first response | |
4109 | */ | |
4110 | rc = ksmbd_populate_dot_dotdot_entries(work, req->FileInformationClass, | |
070fb21e NJ |
4111 | dir_fp, &d_info, srch_ptr, |
4112 | smb2_populate_readdir_entry); | |
e2f34481 NJ |
4113 | if (rc == -ENOSPC) |
4114 | rc = 0; | |
4115 | else if (rc) | |
4116 | goto err_out; | |
4117 | ||
4118 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_HIDE_DOT_FILES)) | |
4119 | d_info.hide_dot_file = true; | |
4120 | ||
4121 | buffer_sz = d_info.out_buf_len; | |
4122 | d_info.rptr = d_info.wptr; | |
4123 | query_dir_private.work = work; | |
4124 | query_dir_private.search_pattern = srch_ptr; | |
4125 | query_dir_private.dir_fp = dir_fp; | |
4126 | query_dir_private.d_info = &d_info; | |
4127 | query_dir_private.info_level = req->FileInformationClass; | |
4128 | dir_fp->readdir_data.private = &query_dir_private; | |
4129 | set_ctx_actor(&dir_fp->readdir_data.ctx, __query_dir); | |
4130 | ||
e8c06191 | 4131 | rc = iterate_dir(dir_fp->filp, &dir_fp->readdir_data.ctx); |
65ca7a3f NJ |
4132 | /* |
4133 | * req->OutputBufferLength is too small to contain even one entry. | |
4134 | * In this case, it immediately returns OutputBufferLength 0 to client. | |
4135 | */ | |
4136 | if (!d_info.out_buf_len && !d_info.num_entry) | |
4137 | goto no_buf_len; | |
88541cb4 | 4138 | if (rc > 0 || rc == -ENOSPC) |
e2f34481 | 4139 | rc = 0; |
88541cb4 | 4140 | else if (rc) |
e2f34481 NJ |
4141 | goto err_out; |
4142 | ||
4143 | d_info.wptr = d_info.rptr; | |
4144 | d_info.out_buf_len = buffer_sz; | |
4145 | rc = process_query_dir_entries(&query_dir_private); | |
4146 | if (rc) | |
4147 | goto err_out; | |
4148 | ||
4149 | if (!d_info.data_count && d_info.out_buf_len >= 0) { | |
64b39f4a | 4150 | if (srch_flag & SMB2_RETURN_SINGLE_ENTRY && !is_asterisk(srch_ptr)) { |
e2f34481 | 4151 | rsp->hdr.Status = STATUS_NO_SUCH_FILE; |
64b39f4a | 4152 | } else { |
e2f34481 NJ |
4153 | dir_fp->dot_dotdot[0] = dir_fp->dot_dotdot[1] = 0; |
4154 | rsp->hdr.Status = STATUS_NO_MORE_FILES; | |
4155 | } | |
4156 | rsp->StructureSize = cpu_to_le16(9); | |
4157 | rsp->OutputBufferOffset = cpu_to_le16(0); | |
4158 | rsp->OutputBufferLength = cpu_to_le32(0); | |
4159 | rsp->Buffer[0] = 0; | |
e2b76ab8 NJ |
4160 | rc = ksmbd_iov_pin_rsp(work, (void *)rsp, |
4161 | sizeof(struct smb2_query_directory_rsp)); | |
4162 | if (rc) | |
4163 | goto err_out; | |
e2f34481 | 4164 | } else { |
65ca7a3f | 4165 | no_buf_len: |
e2f34481 NJ |
4166 | ((struct file_directory_info *) |
4167 | ((char *)rsp->Buffer + d_info.last_entry_offset)) | |
4168 | ->NextEntryOffset = 0; | |
65ca7a3f NJ |
4169 | if (d_info.data_count >= d_info.last_entry_off_align) |
4170 | d_info.data_count -= d_info.last_entry_off_align; | |
e2f34481 NJ |
4171 | |
4172 | rsp->StructureSize = cpu_to_le16(9); | |
4173 | rsp->OutputBufferOffset = cpu_to_le16(72); | |
4174 | rsp->OutputBufferLength = cpu_to_le32(d_info.data_count); | |
e2b76ab8 NJ |
4175 | rc = ksmbd_iov_pin_rsp(work, (void *)rsp, |
4176 | offsetof(struct smb2_query_directory_rsp, Buffer) + | |
4177 | d_info.data_count); | |
4178 | if (rc) | |
4179 | goto err_out; | |
e2f34481 NJ |
4180 | } |
4181 | ||
4182 | kfree(srch_ptr); | |
4183 | ksmbd_fd_put(work, dir_fp); | |
4184 | ksmbd_revert_fsids(work); | |
4185 | return 0; | |
4186 | ||
4187 | err_out: | |
bde1694a | 4188 | pr_err("error while processing smb2 query dir rc = %d\n", rc); |
e2f34481 NJ |
4189 | kfree(srch_ptr); |
4190 | ||
4191 | err_out2: | |
4192 | if (rc == -EINVAL) | |
4193 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
4194 | else if (rc == -EACCES) | |
4195 | rsp->hdr.Status = STATUS_ACCESS_DENIED; | |
4196 | else if (rc == -ENOENT) | |
4197 | rsp->hdr.Status = STATUS_NO_SUCH_FILE; | |
4198 | else if (rc == -EBADF) | |
4199 | rsp->hdr.Status = STATUS_FILE_CLOSED; | |
4200 | else if (rc == -ENOMEM) | |
4201 | rsp->hdr.Status = STATUS_NO_MEMORY; | |
4202 | else if (rc == -EFAULT) | |
4203 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; | |
88541cb4 NJ |
4204 | else if (rc == -EIO) |
4205 | rsp->hdr.Status = STATUS_FILE_CORRUPT_ERROR; | |
e2f34481 NJ |
4206 | if (!rsp->hdr.Status) |
4207 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; | |
4208 | ||
4209 | smb2_set_err_rsp(work); | |
4210 | ksmbd_fd_put(work, dir_fp); | |
4211 | ksmbd_revert_fsids(work); | |
4212 | return 0; | |
4213 | } | |
4214 | ||
4215 | /** | |
4216 | * buffer_check_err() - helper function to check buffer errors | |
4217 | * @reqOutputBufferLength: max buffer length expected in command response | |
4218 | * @rsp: query info response buffer contains output buffer length | |
e230d013 | 4219 | * @rsp_org: base response buffer pointer in case of chained response |
e2f34481 NJ |
4220 | * |
4221 | * Return: 0 on success, otherwise error | |
4222 | */ | |
4223 | static int buffer_check_err(int reqOutputBufferLength, | |
cb451720 | 4224 | struct smb2_query_info_rsp *rsp, |
e2b76ab8 | 4225 | void *rsp_org) |
e2f34481 NJ |
4226 | { |
4227 | if (reqOutputBufferLength < le32_to_cpu(rsp->OutputBufferLength)) { | |
e2b76ab8 NJ |
4228 | pr_err("Invalid Buffer Size Requested\n"); |
4229 | rsp->hdr.Status = STATUS_INFO_LENGTH_MISMATCH; | |
4230 | *(__be32 *)rsp_org = cpu_to_be32(sizeof(struct smb2_hdr)); | |
4231 | return -EINVAL; | |
e2f34481 NJ |
4232 | } |
4233 | return 0; | |
4234 | } | |
4235 | ||
cb451720 NJ |
4236 | static void get_standard_info_pipe(struct smb2_query_info_rsp *rsp, |
4237 | void *rsp_org) | |
e2f34481 NJ |
4238 | { |
4239 | struct smb2_file_standard_info *sinfo; | |
4240 | ||
4241 | sinfo = (struct smb2_file_standard_info *)rsp->Buffer; | |
4242 | ||
4243 | sinfo->AllocationSize = cpu_to_le64(4096); | |
4244 | sinfo->EndOfFile = cpu_to_le64(0); | |
4245 | sinfo->NumberOfLinks = cpu_to_le32(1); | |
4246 | sinfo->DeletePending = 1; | |
4247 | sinfo->Directory = 0; | |
4248 | rsp->OutputBufferLength = | |
4249 | cpu_to_le32(sizeof(struct smb2_file_standard_info)); | |
e2f34481 NJ |
4250 | } |
4251 | ||
cb451720 NJ |
4252 | static void get_internal_info_pipe(struct smb2_query_info_rsp *rsp, u64 num, |
4253 | void *rsp_org) | |
e2f34481 NJ |
4254 | { |
4255 | struct smb2_file_internal_info *file_info; | |
4256 | ||
4257 | file_info = (struct smb2_file_internal_info *)rsp->Buffer; | |
4258 | ||
4259 | /* any unique number */ | |
4260 | file_info->IndexNumber = cpu_to_le64(num | (1ULL << 63)); | |
4261 | rsp->OutputBufferLength = | |
4262 | cpu_to_le32(sizeof(struct smb2_file_internal_info)); | |
e2f34481 NJ |
4263 | } |
4264 | ||
e2f34481 | 4265 | static int smb2_get_info_file_pipe(struct ksmbd_session *sess, |
070fb21e | 4266 | struct smb2_query_info_req *req, |
cb451720 NJ |
4267 | struct smb2_query_info_rsp *rsp, |
4268 | void *rsp_org) | |
e2f34481 | 4269 | { |
64b39f4a | 4270 | u64 id; |
e2f34481 NJ |
4271 | int rc; |
4272 | ||
4273 | /* | |
4274 | * Windows can sometime send query file info request on | |
4275 | * pipe without opening it, checking error condition here | |
4276 | */ | |
2d004c6c | 4277 | id = req->VolatileFileId; |
e2f34481 NJ |
4278 | if (!ksmbd_session_rpc_method(sess, id)) |
4279 | return -ENOENT; | |
4280 | ||
4281 | ksmbd_debug(SMB, "FileInfoClass %u, FileId 0x%llx\n", | |
2d004c6c | 4282 | req->FileInfoClass, req->VolatileFileId); |
e2f34481 NJ |
4283 | |
4284 | switch (req->FileInfoClass) { | |
4285 | case FILE_STANDARD_INFORMATION: | |
cb451720 | 4286 | get_standard_info_pipe(rsp, rsp_org); |
e2f34481 | 4287 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
e2b76ab8 | 4288 | rsp, rsp_org); |
e2f34481 NJ |
4289 | break; |
4290 | case FILE_INTERNAL_INFORMATION: | |
cb451720 | 4291 | get_internal_info_pipe(rsp, id, rsp_org); |
e2f34481 | 4292 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), |
e2b76ab8 | 4293 | rsp, rsp_org); |
e2f34481 NJ |
4294 | break; |
4295 | default: | |
4296 | ksmbd_debug(SMB, "smb2_info_file_pipe for %u not supported\n", | |
070fb21e | 4297 | req->FileInfoClass); |
e2f34481 NJ |
4298 | rc = -EOPNOTSUPP; |
4299 | } | |
4300 | return rc; | |
4301 | } | |
4302 | ||
4303 | /** | |
4304 | * smb2_get_ea() - handler for smb2 get extended attribute command | |
4305 | * @work: smb work containing query info command buffer | |
95fa1ce9 HL |
4306 | * @fp: ksmbd_file pointer |
4307 | * @req: get extended attribute request | |
4308 | * @rsp: response buffer pointer | |
4309 | * @rsp_org: base response buffer pointer in case of chained response | |
e2f34481 NJ |
4310 | * |
4311 | * Return: 0 on success, otherwise error | |
4312 | */ | |
64b39f4a | 4313 | static int smb2_get_ea(struct ksmbd_work *work, struct ksmbd_file *fp, |
070fb21e NJ |
4314 | struct smb2_query_info_req *req, |
4315 | struct smb2_query_info_rsp *rsp, void *rsp_org) | |
e2f34481 NJ |
4316 | { |
4317 | struct smb2_ea_info *eainfo, *prev_eainfo; | |
4318 | char *name, *ptr, *xattr_list = NULL, *buf; | |
4319 | int rc, name_len, value_len, xattr_list_len, idx; | |
4320 | ssize_t buf_free_len, alignment_bytes, next_offset, rsp_data_cnt = 0; | |
4321 | struct smb2_ea_info_req *ea_req = NULL; | |
c22180a5 | 4322 | const struct path *path; |
4609e1f1 | 4323 | struct mnt_idmap *idmap = file_mnt_idmap(fp->filp); |
e2f34481 NJ |
4324 | |
4325 | if (!(fp->daccess & FILE_READ_EA_LE)) { | |
bde1694a NJ |
4326 | pr_err("Not permitted to read ext attr : 0x%x\n", |
4327 | fp->daccess); | |
e2f34481 NJ |
4328 | return -EACCES; |
4329 | } | |
4330 | ||
4331 | path = &fp->filp->f_path; | |
4332 | /* single EA entry is requested with given user.* name */ | |
64b39f4a | 4333 | if (req->InputBufferLength) { |
6d56262c NJ |
4334 | if (le32_to_cpu(req->InputBufferLength) < |
4335 | sizeof(struct smb2_ea_info_req)) | |
4336 | return -EINVAL; | |
4337 | ||
e2f34481 | 4338 | ea_req = (struct smb2_ea_info_req *)req->Buffer; |
64b39f4a | 4339 | } else { |
e2f34481 NJ |
4340 | /* need to send all EAs, if no specific EA is requested*/ |
4341 | if (le32_to_cpu(req->Flags) & SL_RETURN_SINGLE_ENTRY) | |
4342 | ksmbd_debug(SMB, | |
070fb21e NJ |
4343 | "All EAs are requested but need to send single EA entry in rsp flags 0x%x\n", |
4344 | le32_to_cpu(req->Flags)); | |
e2f34481 NJ |
4345 | } |
4346 | ||
34061d6b HL |
4347 | buf_free_len = |
4348 | smb2_calc_max_out_buf_len(work, 8, | |
4349 | le32_to_cpu(req->OutputBufferLength)); | |
4350 | if (buf_free_len < 0) | |
4351 | return -EINVAL; | |
e2f34481 NJ |
4352 | |
4353 | rc = ksmbd_vfs_listxattr(path->dentry, &xattr_list); | |
4354 | if (rc < 0) { | |
4355 | rsp->hdr.Status = STATUS_INVALID_HANDLE; | |
4356 | goto out; | |
4357 | } else if (!rc) { /* there is no EA in the file */ | |
4358 | ksmbd_debug(SMB, "no ea data in the file\n"); | |
4359 | goto done; | |
4360 | } | |
4361 | xattr_list_len = rc; | |
4362 | ||
4363 | ptr = (char *)rsp->Buffer; | |
4364 | eainfo = (struct smb2_ea_info *)ptr; | |
4365 | prev_eainfo = eainfo; | |
4366 | idx = 0; | |
4367 | ||
4368 | while (idx < xattr_list_len) { | |
4369 | name = xattr_list + idx; | |
4370 | name_len = strlen(name); | |
4371 | ||
4372 | ksmbd_debug(SMB, "%s, len %d\n", name, name_len); | |
4373 | idx += name_len + 1; | |
4374 | ||
4375 | /* | |
4376 | * CIFS does not support EA other than user.* namespace, | |
4377 | * still keep the framework generic, to list other attrs | |
4378 | * in future. | |
4379 | */ | |
4380 | if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) | |
4381 | continue; | |
4382 | ||
4383 | if (!strncmp(&name[XATTR_USER_PREFIX_LEN], STREAM_PREFIX, | |
64b39f4a | 4384 | STREAM_PREFIX_LEN)) |
e2f34481 NJ |
4385 | continue; |
4386 | ||
4387 | if (req->InputBufferLength && | |
64b39f4a NJ |
4388 | strncmp(&name[XATTR_USER_PREFIX_LEN], ea_req->name, |
4389 | ea_req->EaNameLength)) | |
e2f34481 NJ |
4390 | continue; |
4391 | ||
4392 | if (!strncmp(&name[XATTR_USER_PREFIX_LEN], | |
64b39f4a | 4393 | DOS_ATTRIBUTE_PREFIX, DOS_ATTRIBUTE_PREFIX_LEN)) |
e2f34481 NJ |
4394 | continue; |
4395 | ||
4396 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) | |
4397 | name_len -= XATTR_USER_PREFIX_LEN; | |
4398 | ||
0ba5439d | 4399 | ptr = eainfo->name + name_len + 1; |
e2f34481 NJ |
4400 | buf_free_len -= (offsetof(struct smb2_ea_info, name) + |
4401 | name_len + 1); | |
4402 | /* bailout if xattr can't fit in buf_free_len */ | |
4609e1f1 | 4403 | value_len = ksmbd_vfs_getxattr(idmap, path->dentry, |
465d7204 | 4404 | name, &buf); |
e2f34481 NJ |
4405 | if (value_len <= 0) { |
4406 | rc = -ENOENT; | |
4407 | rsp->hdr.Status = STATUS_INVALID_HANDLE; | |
4408 | goto out; | |
4409 | } | |
4410 | ||
4411 | buf_free_len -= value_len; | |
4412 | if (buf_free_len < 0) { | |
79f6b11a | 4413 | kfree(buf); |
e2f34481 NJ |
4414 | break; |
4415 | } | |
4416 | ||
4417 | memcpy(ptr, buf, value_len); | |
79f6b11a | 4418 | kfree(buf); |
e2f34481 NJ |
4419 | |
4420 | ptr += value_len; | |
4421 | eainfo->Flags = 0; | |
4422 | eainfo->EaNameLength = name_len; | |
4423 | ||
64b39f4a | 4424 | if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) |
e2f34481 | 4425 | memcpy(eainfo->name, &name[XATTR_USER_PREFIX_LEN], |
070fb21e | 4426 | name_len); |
e2f34481 NJ |
4427 | else |
4428 | memcpy(eainfo->name, name, name_len); | |
4429 | ||
4430 | eainfo->name[name_len] = '\0'; | |
4431 | eainfo->EaValueLength = cpu_to_le16(value_len); | |
4432 | next_offset = offsetof(struct smb2_ea_info, name) + | |
4433 | name_len + 1 + value_len; | |
4434 | ||
4435 | /* align next xattr entry at 4 byte bundary */ | |
4436 | alignment_bytes = ((next_offset + 3) & ~3) - next_offset; | |
4437 | if (alignment_bytes) { | |
4438 | memset(ptr, '\0', alignment_bytes); | |
4439 | ptr += alignment_bytes; | |
4440 | next_offset += alignment_bytes; | |
4441 | buf_free_len -= alignment_bytes; | |
4442 | } | |
4443 | eainfo->NextEntryOffset = cpu_to_le32(next_offset); | |
4444 | prev_eainfo = eainfo; | |
4445 | eainfo = (struct smb2_ea_info *)ptr; | |
4446 | rsp_data_cnt += next_offset; | |
4447 | ||
4448 | if (req->InputBufferLength) { | |
4449 | ksmbd_debug(SMB, "single entry requested\n"); | |
4450 | break; | |
4451 | } | |
4452 | } | |
4453 | ||
4454 | /* no more ea entries */ | |
4455 | prev_eainfo->NextEntryOffset = 0; | |
4456 | done: | |
4457 | rc = 0; | |
4458 | if (rsp_data_cnt == 0) | |
4459 | rsp->hdr.Status = STATUS_NO_EAS_ON_FILE; | |
4460 | rsp->OutputBufferLength = cpu_to_le32(rsp_data_cnt); | |
e2f34481 | 4461 | out: |
79f6b11a | 4462 | kvfree(xattr_list); |
e2f34481 NJ |
4463 | return rc; |
4464 | } | |
4465 | ||
4466 | static void get_file_access_info(struct smb2_query_info_rsp *rsp, | |
070fb21e | 4467 | struct ksmbd_file *fp, void *rsp_org) |
e2f34481 NJ |
4468 | { |
4469 | struct smb2_file_access_info *file_info; | |
4470 | ||
4471 | file_info = (struct smb2_file_access_info *)rsp->Buffer; | |
4472 | file_info->AccessFlags = fp->daccess; | |
4473 | rsp->OutputBufferLength = | |
4474 | cpu_to_le32(sizeof(struct smb2_file_access_info)); | |
e2f34481 NJ |
4475 | } |
4476 | ||
4477 | static int get_file_basic_info(struct smb2_query_info_rsp *rsp, | |
070fb21e | 4478 | struct ksmbd_file *fp, void *rsp_org) |
e2f34481 | 4479 | { |
88d30052 | 4480 | struct smb2_file_basic_info *basic_info; |
e2f34481 NJ |
4481 | struct kstat stat; |
4482 | u64 time; | |
4483 | ||
4484 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { | |
bde1694a NJ |
4485 | pr_err("no right to read the attributes : 0x%x\n", |
4486 | fp->daccess); | |
e2f34481 NJ |
4487 | return -EACCES; |
4488 | } | |
4489 | ||
88d30052 | 4490 | basic_info = (struct smb2_file_basic_info *)rsp->Buffer; |
0d72b928 JL |
4491 | generic_fillattr(file_mnt_idmap(fp->filp), STATX_BASIC_STATS, |
4492 | file_inode(fp->filp), &stat); | |
e2f34481 NJ |
4493 | basic_info->CreationTime = cpu_to_le64(fp->create_time); |
4494 | time = ksmbd_UnixTimeToNT(stat.atime); | |
4495 | basic_info->LastAccessTime = cpu_to_le64(time); | |
4496 | time = ksmbd_UnixTimeToNT(stat.mtime); | |
4497 | basic_info->LastWriteTime = cpu_to_le64(time); | |
4498 | time = ksmbd_UnixTimeToNT(stat.ctime); | |
4499 | basic_info->ChangeTime = cpu_to_le64(time); | |
4500 | basic_info->Attributes = fp->f_ci->m_fattr; | |
4501 | basic_info->Pad1 = 0; | |
4502 | rsp->OutputBufferLength = | |
88d30052 | 4503 | cpu_to_le32(sizeof(struct smb2_file_basic_info)); |
e2f34481 NJ |
4504 | return 0; |
4505 | } | |
4506 | ||
e2f34481 | 4507 | static void get_file_standard_info(struct smb2_query_info_rsp *rsp, |
070fb21e | 4508 | struct ksmbd_file *fp, void *rsp_org) |
e2f34481 NJ |
4509 | { |
4510 | struct smb2_file_standard_info *sinfo; | |
4511 | unsigned int delete_pending; | |
4512 | struct inode *inode; | |
4513 | struct kstat stat; | |
4514 | ||
ab0b263b | 4515 | inode = file_inode(fp->filp); |
0d72b928 | 4516 | generic_fillattr(file_mnt_idmap(fp->filp), STATX_BASIC_STATS, inode, &stat); |
e2f34481 NJ |
4517 | |
4518 | sinfo = (struct smb2_file_standard_info *)rsp->Buffer; | |
4519 | delete_pending = ksmbd_inode_pending_delete(fp); | |
4520 | ||
6cc2268f | 4521 | sinfo->AllocationSize = cpu_to_le64(inode->i_blocks << 9); |
e2f34481 NJ |
4522 | sinfo->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
4523 | sinfo->NumberOfLinks = cpu_to_le32(get_nlink(&stat) - delete_pending); | |
4524 | sinfo->DeletePending = delete_pending; | |
4525 | sinfo->Directory = S_ISDIR(stat.mode) ? 1 : 0; | |
4526 | rsp->OutputBufferLength = | |
4527 | cpu_to_le32(sizeof(struct smb2_file_standard_info)); | |
e2f34481 NJ |
4528 | } |
4529 | ||
4530 | static void get_file_alignment_info(struct smb2_query_info_rsp *rsp, | |
070fb21e | 4531 | void *rsp_org) |
e2f34481 NJ |
4532 | { |
4533 | struct smb2_file_alignment_info *file_info; | |
4534 | ||
4535 | file_info = (struct smb2_file_alignment_info *)rsp->Buffer; | |
4536 | file_info->AlignmentRequirement = 0; | |
4537 | rsp->OutputBufferLength = | |
4538 | cpu_to_le32(sizeof(struct smb2_file_alignment_info)); | |
e2f34481 NJ |
4539 | } |
4540 | ||
4541 | static int get_file_all_info(struct ksmbd_work *work, | |
070fb21e NJ |
4542 | struct smb2_query_info_rsp *rsp, |
4543 | struct ksmbd_file *fp, | |
4544 | void *rsp_org) | |
e2f34481 NJ |
4545 | { |
4546 | struct ksmbd_conn *conn = work->conn; | |
4547 | struct smb2_file_all_info *file_info; | |
4548 | unsigned int delete_pending; | |
4549 | struct inode *inode; | |
4550 | struct kstat stat; | |
4551 | int conv_len; | |
4552 | char *filename; | |
4553 | u64 time; | |
4554 | ||
4555 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { | |
4556 | ksmbd_debug(SMB, "no right to read the attributes : 0x%x\n", | |
070fb21e | 4557 | fp->daccess); |
e2f34481 NJ |
4558 | return -EACCES; |
4559 | } | |
4560 | ||
50f500b7 NJ |
4561 | filename = convert_to_nt_pathname(work->tcon->share_conf, &fp->filp->f_path); |
4562 | if (IS_ERR(filename)) | |
4563 | return PTR_ERR(filename); | |
e2f34481 | 4564 | |
ab0b263b | 4565 | inode = file_inode(fp->filp); |
0d72b928 | 4566 | generic_fillattr(file_mnt_idmap(fp->filp), STATX_BASIC_STATS, inode, &stat); |
e2f34481 NJ |
4567 | |
4568 | ksmbd_debug(SMB, "filename = %s\n", filename); | |
4569 | delete_pending = ksmbd_inode_pending_delete(fp); | |
4570 | file_info = (struct smb2_file_all_info *)rsp->Buffer; | |
4571 | ||
4572 | file_info->CreationTime = cpu_to_le64(fp->create_time); | |
4573 | time = ksmbd_UnixTimeToNT(stat.atime); | |
4574 | file_info->LastAccessTime = cpu_to_le64(time); | |
4575 | time = ksmbd_UnixTimeToNT(stat.mtime); | |
4576 | file_info->LastWriteTime = cpu_to_le64(time); | |
4577 | time = ksmbd_UnixTimeToNT(stat.ctime); | |
4578 | file_info->ChangeTime = cpu_to_le64(time); | |
4579 | file_info->Attributes = fp->f_ci->m_fattr; | |
4580 | file_info->Pad1 = 0; | |
4581 | file_info->AllocationSize = | |
6cc2268f | 4582 | cpu_to_le64(inode->i_blocks << 9); |
e2f34481 NJ |
4583 | file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
4584 | file_info->NumberOfLinks = | |
4585 | cpu_to_le32(get_nlink(&stat) - delete_pending); | |
4586 | file_info->DeletePending = delete_pending; | |
4587 | file_info->Directory = S_ISDIR(stat.mode) ? 1 : 0; | |
4588 | file_info->Pad2 = 0; | |
4589 | file_info->IndexNumber = cpu_to_le64(stat.ino); | |
4590 | file_info->EASize = 0; | |
4591 | file_info->AccessFlags = fp->daccess; | |
4592 | file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); | |
4593 | file_info->Mode = fp->coption; | |
4594 | file_info->AlignmentRequirement = 0; | |
070fb21e NJ |
4595 | conv_len = smbConvertToUTF16((__le16 *)file_info->FileName, filename, |
4596 | PATH_MAX, conn->local_nls, 0); | |
e2f34481 NJ |
4597 | conv_len *= 2; |
4598 | file_info->FileNameLength = cpu_to_le32(conv_len); | |
4599 | rsp->OutputBufferLength = | |
4600 | cpu_to_le32(sizeof(struct smb2_file_all_info) + conv_len - 1); | |
4601 | kfree(filename); | |
e2f34481 NJ |
4602 | return 0; |
4603 | } | |
4604 | ||
4605 | static void get_file_alternate_info(struct ksmbd_work *work, | |
070fb21e NJ |
4606 | struct smb2_query_info_rsp *rsp, |
4607 | struct ksmbd_file *fp, | |
4608 | void *rsp_org) | |
e2f34481 NJ |
4609 | { |
4610 | struct ksmbd_conn *conn = work->conn; | |
4611 | struct smb2_file_alt_name_info *file_info; | |
493fa2fb | 4612 | struct dentry *dentry = fp->filp->f_path.dentry; |
e2f34481 | 4613 | int conv_len; |
e2f34481 | 4614 | |
493fa2fb | 4615 | spin_lock(&dentry->d_lock); |
e2f34481 NJ |
4616 | file_info = (struct smb2_file_alt_name_info *)rsp->Buffer; |
4617 | conv_len = ksmbd_extract_shortname(conn, | |
493fa2fb | 4618 | dentry->d_name.name, |
e2f34481 | 4619 | file_info->FileName); |
493fa2fb | 4620 | spin_unlock(&dentry->d_lock); |
e2f34481 NJ |
4621 | file_info->FileNameLength = cpu_to_le32(conv_len); |
4622 | rsp->OutputBufferLength = | |
4623 | cpu_to_le32(sizeof(struct smb2_file_alt_name_info) + conv_len); | |
e2f34481 NJ |
4624 | } |
4625 | ||
4626 | static void get_file_stream_info(struct ksmbd_work *work, | |
070fb21e NJ |
4627 | struct smb2_query_info_rsp *rsp, |
4628 | struct ksmbd_file *fp, | |
4629 | void *rsp_org) | |
e2f34481 NJ |
4630 | { |
4631 | struct ksmbd_conn *conn = work->conn; | |
4632 | struct smb2_file_stream_info *file_info; | |
4633 | char *stream_name, *xattr_list = NULL, *stream_buf; | |
4634 | struct kstat stat; | |
c22180a5 | 4635 | const struct path *path = &fp->filp->f_path; |
e2f34481 NJ |
4636 | ssize_t xattr_list_len; |
4637 | int nbytes = 0, streamlen, stream_name_len, next, idx = 0; | |
34061d6b HL |
4638 | int buf_free_len; |
4639 | struct smb2_query_info_req *req = ksmbd_req_buf_next(work); | |
e2f34481 | 4640 | |
0d72b928 JL |
4641 | generic_fillattr(file_mnt_idmap(fp->filp), STATX_BASIC_STATS, |
4642 | file_inode(fp->filp), &stat); | |
e2f34481 NJ |
4643 | file_info = (struct smb2_file_stream_info *)rsp->Buffer; |
4644 | ||
1ec72153 NJ |
4645 | buf_free_len = |
4646 | smb2_calc_max_out_buf_len(work, 8, | |
4647 | le32_to_cpu(req->OutputBufferLength)); | |
4648 | if (buf_free_len < 0) | |
4649 | goto out; | |
4650 | ||
e2f34481 NJ |
4651 | xattr_list_len = ksmbd_vfs_listxattr(path->dentry, &xattr_list); |
4652 | if (xattr_list_len < 0) { | |
4653 | goto out; | |
4654 | } else if (!xattr_list_len) { | |
4655 | ksmbd_debug(SMB, "empty xattr in the file\n"); | |
4656 | goto out; | |
4657 | } | |
4658 | ||
4659 | while (idx < xattr_list_len) { | |
4660 | stream_name = xattr_list + idx; | |
4661 | streamlen = strlen(stream_name); | |
4662 | idx += streamlen + 1; | |
4663 | ||
4664 | ksmbd_debug(SMB, "%s, len %d\n", stream_name, streamlen); | |
4665 | ||
4666 | if (strncmp(&stream_name[XATTR_USER_PREFIX_LEN], | |
64b39f4a | 4667 | STREAM_PREFIX, STREAM_PREFIX_LEN)) |
e2f34481 NJ |
4668 | continue; |
4669 | ||
4670 | stream_name_len = streamlen - (XATTR_USER_PREFIX_LEN + | |
4671 | STREAM_PREFIX_LEN); | |
4672 | streamlen = stream_name_len; | |
4673 | ||
4674 | /* plus : size */ | |
4675 | streamlen += 1; | |
4676 | stream_buf = kmalloc(streamlen + 1, GFP_KERNEL); | |
4677 | if (!stream_buf) | |
4678 | break; | |
4679 | ||
4680 | streamlen = snprintf(stream_buf, streamlen + 1, | |
070fb21e | 4681 | ":%s", &stream_name[XATTR_NAME_STREAM_LEN]); |
e2f34481 | 4682 | |
34061d6b | 4683 | next = sizeof(struct smb2_file_stream_info) + streamlen * 2; |
178ca6f8 NJ |
4684 | if (next > buf_free_len) { |
4685 | kfree(stream_buf); | |
34061d6b | 4686 | break; |
178ca6f8 | 4687 | } |
34061d6b | 4688 | |
070fb21e | 4689 | file_info = (struct smb2_file_stream_info *)&rsp->Buffer[nbytes]; |
e2f34481 | 4690 | streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, |
070fb21e NJ |
4691 | stream_buf, streamlen, |
4692 | conn->local_nls, 0); | |
e2f34481 NJ |
4693 | streamlen *= 2; |
4694 | kfree(stream_buf); | |
4695 | file_info->StreamNameLength = cpu_to_le32(streamlen); | |
4696 | file_info->StreamSize = cpu_to_le64(stream_name_len); | |
4697 | file_info->StreamAllocationSize = cpu_to_le64(stream_name_len); | |
4698 | ||
e2f34481 | 4699 | nbytes += next; |
34061d6b | 4700 | buf_free_len -= next; |
e2f34481 NJ |
4701 | file_info->NextEntryOffset = cpu_to_le32(next); |
4702 | } | |
4703 | ||
1ec72153 | 4704 | out: |
34061d6b HL |
4705 | if (!S_ISDIR(stat.mode) && |
4706 | buf_free_len >= sizeof(struct smb2_file_stream_info) + 7 * 2) { | |
e2f34481 NJ |
4707 | file_info = (struct smb2_file_stream_info *) |
4708 | &rsp->Buffer[nbytes]; | |
4709 | streamlen = smbConvertToUTF16((__le16 *)file_info->StreamName, | |
070fb21e | 4710 | "::$DATA", 7, conn->local_nls, 0); |
e2f34481 NJ |
4711 | streamlen *= 2; |
4712 | file_info->StreamNameLength = cpu_to_le32(streamlen); | |
1ec72153 NJ |
4713 | file_info->StreamSize = cpu_to_le64(stat.size); |
4714 | file_info->StreamAllocationSize = cpu_to_le64(stat.blocks << 9); | |
e2f34481 NJ |
4715 | nbytes += sizeof(struct smb2_file_stream_info) + streamlen; |
4716 | } | |
4717 | ||
4718 | /* last entry offset should be 0 */ | |
4719 | file_info->NextEntryOffset = 0; | |
79f6b11a | 4720 | kvfree(xattr_list); |
e2f34481 NJ |
4721 | |
4722 | rsp->OutputBufferLength = cpu_to_le32(nbytes); | |
e2f34481 NJ |
4723 | } |
4724 | ||
4725 | static void get_file_internal_info(struct smb2_query_info_rsp *rsp, | |
070fb21e | 4726 | struct ksmbd_file *fp, void *rsp_org) |
e2f34481 NJ |
4727 | { |
4728 | struct smb2_file_internal_info *file_info; | |
4729 | struct kstat stat; | |
4730 | ||
0d72b928 JL |
4731 | generic_fillattr(file_mnt_idmap(fp->filp), STATX_BASIC_STATS, |
4732 | file_inode(fp->filp), &stat); | |
e2f34481 NJ |
4733 | file_info = (struct smb2_file_internal_info *)rsp->Buffer; |
4734 | file_info->IndexNumber = cpu_to_le64(stat.ino); | |
4735 | rsp->OutputBufferLength = | |
4736 | cpu_to_le32(sizeof(struct smb2_file_internal_info)); | |
e2f34481 NJ |
4737 | } |
4738 | ||
4739 | static int get_file_network_open_info(struct smb2_query_info_rsp *rsp, | |
070fb21e | 4740 | struct ksmbd_file *fp, void *rsp_org) |
e2f34481 NJ |
4741 | { |
4742 | struct smb2_file_ntwrk_info *file_info; | |
4743 | struct inode *inode; | |
4744 | struct kstat stat; | |
4745 | u64 time; | |
4746 | ||
4747 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { | |
bde1694a NJ |
4748 | pr_err("no right to read the attributes : 0x%x\n", |
4749 | fp->daccess); | |
e2f34481 NJ |
4750 | return -EACCES; |
4751 | } | |
4752 | ||
4753 | file_info = (struct smb2_file_ntwrk_info *)rsp->Buffer; | |
4754 | ||
ab0b263b | 4755 | inode = file_inode(fp->filp); |
0d72b928 | 4756 | generic_fillattr(file_mnt_idmap(fp->filp), STATX_BASIC_STATS, inode, &stat); |
e2f34481 NJ |
4757 | |
4758 | file_info->CreationTime = cpu_to_le64(fp->create_time); | |
4759 | time = ksmbd_UnixTimeToNT(stat.atime); | |
4760 | file_info->LastAccessTime = cpu_to_le64(time); | |
4761 | time = ksmbd_UnixTimeToNT(stat.mtime); | |
4762 | file_info->LastWriteTime = cpu_to_le64(time); | |
4763 | time = ksmbd_UnixTimeToNT(stat.ctime); | |
4764 | file_info->ChangeTime = cpu_to_le64(time); | |
4765 | file_info->Attributes = fp->f_ci->m_fattr; | |
4766 | file_info->AllocationSize = | |
6cc2268f | 4767 | cpu_to_le64(inode->i_blocks << 9); |
e2f34481 NJ |
4768 | file_info->EndOfFile = S_ISDIR(stat.mode) ? 0 : cpu_to_le64(stat.size); |
4769 | file_info->Reserved = cpu_to_le32(0); | |
4770 | rsp->OutputBufferLength = | |
4771 | cpu_to_le32(sizeof(struct smb2_file_ntwrk_info)); | |
e2f34481 NJ |
4772 | return 0; |
4773 | } | |
4774 | ||
64b39f4a | 4775 | static void get_file_ea_info(struct smb2_query_info_rsp *rsp, void *rsp_org) |
e2f34481 NJ |
4776 | { |
4777 | struct smb2_file_ea_info *file_info; | |
4778 | ||
4779 | file_info = (struct smb2_file_ea_info *)rsp->Buffer; | |
4780 | file_info->EASize = 0; | |
4781 | rsp->OutputBufferLength = | |
4782 | cpu_to_le32(sizeof(struct smb2_file_ea_info)); | |
e2f34481 NJ |
4783 | } |
4784 | ||
4785 | static void get_file_position_info(struct smb2_query_info_rsp *rsp, | |
070fb21e | 4786 | struct ksmbd_file *fp, void *rsp_org) |
e2f34481 NJ |
4787 | { |
4788 | struct smb2_file_pos_info *file_info; | |
4789 | ||
4790 | file_info = (struct smb2_file_pos_info *)rsp->Buffer; | |
4791 | file_info->CurrentByteOffset = cpu_to_le64(fp->filp->f_pos); | |
4792 | rsp->OutputBufferLength = | |
4793 | cpu_to_le32(sizeof(struct smb2_file_pos_info)); | |
e2f34481 NJ |
4794 | } |
4795 | ||
4796 | static void get_file_mode_info(struct smb2_query_info_rsp *rsp, | |
070fb21e | 4797 | struct ksmbd_file *fp, void *rsp_org) |
e2f34481 NJ |
4798 | { |
4799 | struct smb2_file_mode_info *file_info; | |
4800 | ||
4801 | file_info = (struct smb2_file_mode_info *)rsp->Buffer; | |
4802 | file_info->Mode = fp->coption & FILE_MODE_INFO_MASK; | |
4803 | rsp->OutputBufferLength = | |
4804 | cpu_to_le32(sizeof(struct smb2_file_mode_info)); | |
e2f34481 NJ |
4805 | } |
4806 | ||
4807 | static void get_file_compression_info(struct smb2_query_info_rsp *rsp, | |
070fb21e | 4808 | struct ksmbd_file *fp, void *rsp_org) |
e2f34481 NJ |
4809 | { |
4810 | struct smb2_file_comp_info *file_info; | |
4811 | struct kstat stat; | |
4812 | ||
0d72b928 JL |
4813 | generic_fillattr(file_mnt_idmap(fp->filp), STATX_BASIC_STATS, |
4814 | file_inode(fp->filp), &stat); | |
e2f34481 NJ |
4815 | |
4816 | file_info = (struct smb2_file_comp_info *)rsp->Buffer; | |
4817 | file_info->CompressedFileSize = cpu_to_le64(stat.blocks << 9); | |
4818 | file_info->CompressionFormat = COMPRESSION_FORMAT_NONE; | |
4819 | file_info->CompressionUnitShift = 0; | |
4820 | file_info->ChunkShift = 0; | |
4821 | file_info->ClusterShift = 0; | |
4822 | memset(&file_info->Reserved[0], 0, 3); | |
4823 | ||
4824 | rsp->OutputBufferLength = | |
4825 | cpu_to_le32(sizeof(struct smb2_file_comp_info)); | |
e2f34481 NJ |
4826 | } |
4827 | ||
4828 | static int get_file_attribute_tag_info(struct smb2_query_info_rsp *rsp, | |
070fb21e | 4829 | struct ksmbd_file *fp, void *rsp_org) |
e2f34481 NJ |
4830 | { |
4831 | struct smb2_file_attr_tag_info *file_info; | |
4832 | ||
4833 | if (!(fp->daccess & FILE_READ_ATTRIBUTES_LE)) { | |
bde1694a NJ |
4834 | pr_err("no right to read the attributes : 0x%x\n", |
4835 | fp->daccess); | |
e2f34481 NJ |
4836 | return -EACCES; |
4837 | } | |
4838 | ||
4839 | file_info = (struct smb2_file_attr_tag_info *)rsp->Buffer; | |
4840 | file_info->FileAttributes = fp->f_ci->m_fattr; | |
4841 | file_info->ReparseTag = 0; | |
4842 | rsp->OutputBufferLength = | |
4843 | cpu_to_le32(sizeof(struct smb2_file_attr_tag_info)); | |
e2f34481 NJ |
4844 | return 0; |
4845 | } | |
4846 | ||
e2b76ab8 | 4847 | static void find_file_posix_info(struct smb2_query_info_rsp *rsp, |
070fb21e | 4848 | struct ksmbd_file *fp, void *rsp_org) |
e2f34481 NJ |
4849 | { |
4850 | struct smb311_posix_qinfo *file_info; | |
ab0b263b | 4851 | struct inode *inode = file_inode(fp->filp); |
e67fe633 CB |
4852 | struct mnt_idmap *idmap = file_mnt_idmap(fp->filp); |
4853 | vfsuid_t vfsuid = i_uid_into_vfsuid(idmap, inode); | |
4854 | vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); | |
e2f34481 | 4855 | u64 time; |
d5919f2a | 4856 | int out_buf_len = sizeof(struct smb311_posix_qinfo) + 32; |
e2f34481 NJ |
4857 | |
4858 | file_info = (struct smb311_posix_qinfo *)rsp->Buffer; | |
4859 | file_info->CreationTime = cpu_to_le64(fp->create_time); | |
769cfc91 | 4860 | time = ksmbd_UnixTimeToNT(inode_get_atime(inode)); |
e2f34481 | 4861 | file_info->LastAccessTime = cpu_to_le64(time); |
769cfc91 | 4862 | time = ksmbd_UnixTimeToNT(inode_get_mtime(inode)); |
e2f34481 | 4863 | file_info->LastWriteTime = cpu_to_le64(time); |
94487653 | 4864 | time = ksmbd_UnixTimeToNT(inode_get_ctime(inode)); |
e2f34481 NJ |
4865 | file_info->ChangeTime = cpu_to_le64(time); |
4866 | file_info->DosAttributes = fp->f_ci->m_fattr; | |
4867 | file_info->Inode = cpu_to_le64(inode->i_ino); | |
4868 | file_info->EndOfFile = cpu_to_le64(inode->i_size); | |
4869 | file_info->AllocationSize = cpu_to_le64(inode->i_blocks << 9); | |
4870 | file_info->HardLinks = cpu_to_le32(inode->i_nlink); | |
f6c2b201 | 4871 | file_info->Mode = cpu_to_le32(inode->i_mode & 0777); |
e2f34481 | 4872 | file_info->DeviceId = cpu_to_le32(inode->i_rdev); |
d5919f2a NJ |
4873 | |
4874 | /* | |
4875 | * Sids(32) contain two sids(Domain sid(16), UNIX group sid(16)). | |
4876 | * UNIX sid(16) = revision(1) + num_subauth(1) + authority(6) + | |
4877 | * sub_auth(4 * 1(num_subauth)) + RID(4). | |
4878 | */ | |
4879 | id_to_sid(from_kuid_munged(&init_user_ns, vfsuid_into_kuid(vfsuid)), | |
4880 | SIDUNIX_USER, (struct smb_sid *)&file_info->Sids[0]); | |
4881 | id_to_sid(from_kgid_munged(&init_user_ns, vfsgid_into_kgid(vfsgid)), | |
4882 | SIDUNIX_GROUP, (struct smb_sid *)&file_info->Sids[16]); | |
4883 | ||
4884 | rsp->OutputBufferLength = cpu_to_le32(out_buf_len); | |
e2f34481 NJ |
4885 | } |
4886 | ||
e2f34481 | 4887 | static int smb2_get_info_file(struct ksmbd_work *work, |
070fb21e | 4888 | struct smb2_query_info_req *req, |
cb451720 | 4889 | struct smb2_query_info_rsp *rsp) |
e2f34481 NJ |
4890 | { |
4891 | struct ksmbd_file *fp; | |
4892 | int fileinfoclass = 0; | |
4893 | int rc = 0; | |
e2f34481 NJ |
4894 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
4895 | ||
4896 | if (test_share_config_flag(work->tcon->share_conf, | |
64b39f4a | 4897 | KSMBD_SHARE_FLAG_PIPE)) { |
e2f34481 | 4898 | /* smb2 info file called for pipe */ |
cb451720 NJ |
4899 | return smb2_get_info_file_pipe(work->sess, req, rsp, |
4900 | work->response_buf); | |
e2f34481 NJ |
4901 | } |
4902 | ||
4903 | if (work->next_smb2_rcv_hdr_off) { | |
2d004c6c | 4904 | if (!has_file_id(req->VolatileFileId)) { |
3867369e | 4905 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
070fb21e | 4906 | work->compound_fid); |
e2f34481 NJ |
4907 | id = work->compound_fid; |
4908 | pid = work->compound_pfid; | |
4909 | } | |
4910 | } | |
4911 | ||
3867369e | 4912 | if (!has_file_id(id)) { |
2d004c6c PA |
4913 | id = req->VolatileFileId; |
4914 | pid = req->PersistentFileId; | |
e2f34481 NJ |
4915 | } |
4916 | ||
4917 | fp = ksmbd_lookup_fd_slow(work, id, pid); | |
4918 | if (!fp) | |
4919 | return -ENOENT; | |
4920 | ||
4921 | fileinfoclass = req->FileInfoClass; | |
4922 | ||
4923 | switch (fileinfoclass) { | |
4924 | case FILE_ACCESS_INFORMATION: | |
cb451720 | 4925 | get_file_access_info(rsp, fp, work->response_buf); |
e2f34481 NJ |
4926 | break; |
4927 | ||
4928 | case FILE_BASIC_INFORMATION: | |
cb451720 | 4929 | rc = get_file_basic_info(rsp, fp, work->response_buf); |
e2f34481 NJ |
4930 | break; |
4931 | ||
4932 | case FILE_STANDARD_INFORMATION: | |
cb451720 | 4933 | get_file_standard_info(rsp, fp, work->response_buf); |
e2f34481 NJ |
4934 | break; |
4935 | ||
4936 | case FILE_ALIGNMENT_INFORMATION: | |
cb451720 | 4937 | get_file_alignment_info(rsp, work->response_buf); |
e2f34481 NJ |
4938 | break; |
4939 | ||
4940 | case FILE_ALL_INFORMATION: | |
cb451720 | 4941 | rc = get_file_all_info(work, rsp, fp, work->response_buf); |
e2f34481 NJ |
4942 | break; |
4943 | ||
4944 | case FILE_ALTERNATE_NAME_INFORMATION: | |
cb451720 | 4945 | get_file_alternate_info(work, rsp, fp, work->response_buf); |
e2f34481 NJ |
4946 | break; |
4947 | ||
4948 | case FILE_STREAM_INFORMATION: | |
cb451720 | 4949 | get_file_stream_info(work, rsp, fp, work->response_buf); |
e2f34481 NJ |
4950 | break; |
4951 | ||
4952 | case FILE_INTERNAL_INFORMATION: | |
cb451720 | 4953 | get_file_internal_info(rsp, fp, work->response_buf); |
e2f34481 NJ |
4954 | break; |
4955 | ||
4956 | case FILE_NETWORK_OPEN_INFORMATION: | |
cb451720 | 4957 | rc = get_file_network_open_info(rsp, fp, work->response_buf); |
e2f34481 NJ |
4958 | break; |
4959 | ||
4960 | case FILE_EA_INFORMATION: | |
cb451720 | 4961 | get_file_ea_info(rsp, work->response_buf); |
e2f34481 NJ |
4962 | break; |
4963 | ||
4964 | case FILE_FULL_EA_INFORMATION: | |
cb451720 | 4965 | rc = smb2_get_ea(work, fp, req, rsp, work->response_buf); |
e2f34481 NJ |
4966 | break; |
4967 | ||
4968 | case FILE_POSITION_INFORMATION: | |
cb451720 | 4969 | get_file_position_info(rsp, fp, work->response_buf); |
e2f34481 NJ |
4970 | break; |
4971 | ||
4972 | case FILE_MODE_INFORMATION: | |
cb451720 | 4973 | get_file_mode_info(rsp, fp, work->response_buf); |
e2f34481 NJ |
4974 | break; |
4975 | ||
4976 | case FILE_COMPRESSION_INFORMATION: | |
cb451720 | 4977 | get_file_compression_info(rsp, fp, work->response_buf); |
e2f34481 NJ |
4978 | break; |
4979 | ||
4980 | case FILE_ATTRIBUTE_TAG_INFORMATION: | |
cb451720 | 4981 | rc = get_file_attribute_tag_info(rsp, fp, work->response_buf); |
e2f34481 NJ |
4982 | break; |
4983 | case SMB_FIND_FILE_POSIX_INFO: | |
4984 | if (!work->tcon->posix_extensions) { | |
bde1694a | 4985 | pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n"); |
e2f34481 NJ |
4986 | rc = -EOPNOTSUPP; |
4987 | } else { | |
e2b76ab8 | 4988 | find_file_posix_info(rsp, fp, work->response_buf); |
e2f34481 NJ |
4989 | } |
4990 | break; | |
4991 | default: | |
4992 | ksmbd_debug(SMB, "fileinfoclass %d not supported yet\n", | |
4993 | fileinfoclass); | |
4994 | rc = -EOPNOTSUPP; | |
4995 | } | |
4996 | if (!rc) | |
4997 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), | |
e2b76ab8 | 4998 | rsp, work->response_buf); |
e2f34481 NJ |
4999 | ksmbd_fd_put(work, fp); |
5000 | return rc; | |
5001 | } | |
5002 | ||
e2f34481 | 5003 | static int smb2_get_info_filesystem(struct ksmbd_work *work, |
070fb21e | 5004 | struct smb2_query_info_req *req, |
cb451720 | 5005 | struct smb2_query_info_rsp *rsp) |
e2f34481 NJ |
5006 | { |
5007 | struct ksmbd_session *sess = work->sess; | |
af7c39d9 | 5008 | struct ksmbd_conn *conn = work->conn; |
e2f34481 NJ |
5009 | struct ksmbd_share_config *share = work->tcon->share_conf; |
5010 | int fsinfoclass = 0; | |
5011 | struct kstatfs stfs; | |
5012 | struct path path; | |
5013 | int rc = 0, len; | |
a6a5fa77 | 5014 | |
3ac00a2a NJ |
5015 | if (!share->path) |
5016 | return -EIO; | |
5017 | ||
265fd199 | 5018 | rc = kern_path(share->path, LOOKUP_NO_SYMLINKS, &path); |
e2f34481 | 5019 | if (rc) { |
bde1694a | 5020 | pr_err("cannot create vfs path\n"); |
e2f34481 NJ |
5021 | return -EIO; |
5022 | } | |
5023 | ||
5024 | rc = vfs_statfs(&path, &stfs); | |
5025 | if (rc) { | |
bde1694a | 5026 | pr_err("cannot do stat of path %s\n", share->path); |
e2f34481 NJ |
5027 | path_put(&path); |
5028 | return -EIO; | |
5029 | } | |
5030 | ||
5031 | fsinfoclass = req->FileInfoClass; | |
5032 | ||
5033 | switch (fsinfoclass) { | |
5034 | case FS_DEVICE_INFORMATION: | |
5035 | { | |
5036 | struct filesystem_device_info *info; | |
5037 | ||
5038 | info = (struct filesystem_device_info *)rsp->Buffer; | |
5039 | ||
5040 | info->DeviceType = cpu_to_le32(stfs.f_type); | |
5041 | info->DeviceCharacteristics = cpu_to_le32(0x00000020); | |
5042 | rsp->OutputBufferLength = cpu_to_le32(8); | |
e2f34481 NJ |
5043 | break; |
5044 | } | |
5045 | case FS_ATTRIBUTE_INFORMATION: | |
5046 | { | |
5047 | struct filesystem_attribute_info *info; | |
5048 | size_t sz; | |
5049 | ||
5050 | info = (struct filesystem_attribute_info *)rsp->Buffer; | |
5051 | info->Attributes = cpu_to_le32(FILE_SUPPORTS_OBJECT_IDS | | |
5052 | FILE_PERSISTENT_ACLS | | |
5053 | FILE_UNICODE_ON_DISK | | |
5054 | FILE_CASE_PRESERVED_NAMES | | |
eb817368 NJ |
5055 | FILE_CASE_SENSITIVE_SEARCH | |
5056 | FILE_SUPPORTS_BLOCK_REFCOUNTING); | |
e2f34481 NJ |
5057 | |
5058 | info->Attributes |= cpu_to_le32(server_conf.share_fake_fscaps); | |
5059 | ||
728f14c7 NJ |
5060 | if (test_share_config_flag(work->tcon->share_conf, |
5061 | KSMBD_SHARE_FLAG_STREAMS)) | |
5062 | info->Attributes |= cpu_to_le32(FILE_NAMED_STREAMS); | |
5063 | ||
e2f34481 NJ |
5064 | info->MaxPathNameComponentLength = cpu_to_le32(stfs.f_namelen); |
5065 | len = smbConvertToUTF16((__le16 *)info->FileSystemName, | |
5066 | "NTFS", PATH_MAX, conn->local_nls, 0); | |
5067 | len = len * 2; | |
5068 | info->FileSystemNameLen = cpu_to_le32(len); | |
5069 | sz = sizeof(struct filesystem_attribute_info) - 2 + len; | |
5070 | rsp->OutputBufferLength = cpu_to_le32(sz); | |
e2f34481 NJ |
5071 | break; |
5072 | } | |
5073 | case FS_VOLUME_INFORMATION: | |
5074 | { | |
5075 | struct filesystem_vol_info *info; | |
5076 | size_t sz; | |
5d2f0b10 | 5077 | unsigned int serial_crc = 0; |
e2f34481 NJ |
5078 | |
5079 | info = (struct filesystem_vol_info *)(rsp->Buffer); | |
5080 | info->VolumeCreationTime = 0; | |
5d2f0b10 NJ |
5081 | serial_crc = crc32_le(serial_crc, share->name, |
5082 | strlen(share->name)); | |
5083 | serial_crc = crc32_le(serial_crc, share->path, | |
5084 | strlen(share->path)); | |
5085 | serial_crc = crc32_le(serial_crc, ksmbd_netbios_name(), | |
5086 | strlen(ksmbd_netbios_name())); | |
e2f34481 | 5087 | /* Taking dummy value of serial number*/ |
5d2f0b10 | 5088 | info->SerialNumber = cpu_to_le32(serial_crc); |
e2f34481 NJ |
5089 | len = smbConvertToUTF16((__le16 *)info->VolumeLabel, |
5090 | share->name, PATH_MAX, | |
5091 | conn->local_nls, 0); | |
5092 | len = len * 2; | |
5093 | info->VolumeLabelSize = cpu_to_le32(len); | |
5094 | info->Reserved = 0; | |
5095 | sz = sizeof(struct filesystem_vol_info) - 2 + len; | |
5096 | rsp->OutputBufferLength = cpu_to_le32(sz); | |
e2f34481 NJ |
5097 | break; |
5098 | } | |
5099 | case FS_SIZE_INFORMATION: | |
5100 | { | |
5101 | struct filesystem_info *info; | |
e2f34481 NJ |
5102 | |
5103 | info = (struct filesystem_info *)(rsp->Buffer); | |
e2f34481 NJ |
5104 | info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); |
5105 | info->FreeAllocationUnits = cpu_to_le64(stfs.f_bfree); | |
ee81cae1 NJ |
5106 | info->SectorsPerAllocationUnit = cpu_to_le32(1); |
5107 | info->BytesPerSector = cpu_to_le32(stfs.f_bsize); | |
e2f34481 | 5108 | rsp->OutputBufferLength = cpu_to_le32(24); |
e2f34481 NJ |
5109 | break; |
5110 | } | |
5111 | case FS_FULL_SIZE_INFORMATION: | |
5112 | { | |
5113 | struct smb2_fs_full_size_info *info; | |
e2f34481 NJ |
5114 | |
5115 | info = (struct smb2_fs_full_size_info *)(rsp->Buffer); | |
e2f34481 NJ |
5116 | info->TotalAllocationUnits = cpu_to_le64(stfs.f_blocks); |
5117 | info->CallerAvailableAllocationUnits = | |
5118 | cpu_to_le64(stfs.f_bavail); | |
5119 | info->ActualAvailableAllocationUnits = | |
5120 | cpu_to_le64(stfs.f_bfree); | |
ee81cae1 NJ |
5121 | info->SectorsPerAllocationUnit = cpu_to_le32(1); |
5122 | info->BytesPerSector = cpu_to_le32(stfs.f_bsize); | |
e2f34481 | 5123 | rsp->OutputBufferLength = cpu_to_le32(32); |
e2f34481 NJ |
5124 | break; |
5125 | } | |
5126 | case FS_OBJECT_ID_INFORMATION: | |
5127 | { | |
5128 | struct object_id_info *info; | |
5129 | ||
5130 | info = (struct object_id_info *)(rsp->Buffer); | |
5131 | ||
5132 | if (!user_guest(sess->user)) | |
5133 | memcpy(info->objid, user_passkey(sess->user), 16); | |
5134 | else | |
5135 | memset(info->objid, 0, 16); | |
5136 | ||
5137 | info->extended_info.magic = cpu_to_le32(EXTENDED_INFO_MAGIC); | |
5138 | info->extended_info.version = cpu_to_le32(1); | |
5139 | info->extended_info.release = cpu_to_le32(1); | |
5140 | info->extended_info.rel_date = 0; | |
64b39f4a | 5141 | memcpy(info->extended_info.version_string, "1.1.0", strlen("1.1.0")); |
e2f34481 | 5142 | rsp->OutputBufferLength = cpu_to_le32(64); |
e2f34481 NJ |
5143 | break; |
5144 | } | |
5145 | case FS_SECTOR_SIZE_INFORMATION: | |
5146 | { | |
5147 | struct smb3_fs_ss_info *info; | |
02655a70 NJ |
5148 | unsigned int sector_size = |
5149 | min_t(unsigned int, path.mnt->mnt_sb->s_blocksize, 4096); | |
e2f34481 NJ |
5150 | |
5151 | info = (struct smb3_fs_ss_info *)(rsp->Buffer); | |
e2f34481 | 5152 | |
02655a70 | 5153 | info->LogicalBytesPerSector = cpu_to_le32(sector_size); |
e2f34481 | 5154 | info->PhysicalBytesPerSectorForAtomicity = |
02655a70 NJ |
5155 | cpu_to_le32(sector_size); |
5156 | info->PhysicalBytesPerSectorForPerf = cpu_to_le32(sector_size); | |
e2f34481 | 5157 | info->FSEffPhysicalBytesPerSectorForAtomicity = |
02655a70 | 5158 | cpu_to_le32(sector_size); |
e2f34481 NJ |
5159 | info->Flags = cpu_to_le32(SSINFO_FLAGS_ALIGNED_DEVICE | |
5160 | SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE); | |
5161 | info->ByteOffsetForSectorAlignment = 0; | |
5162 | info->ByteOffsetForPartitionAlignment = 0; | |
5163 | rsp->OutputBufferLength = cpu_to_le32(28); | |
e2f34481 NJ |
5164 | break; |
5165 | } | |
5166 | case FS_CONTROL_INFORMATION: | |
5167 | { | |
5168 | /* | |
5169 | * TODO : The current implementation is based on | |
5170 | * test result with win7(NTFS) server. It's need to | |
5171 | * modify this to get valid Quota values | |
5172 | * from Linux kernel | |
5173 | */ | |
5174 | struct smb2_fs_control_info *info; | |
5175 | ||
5176 | info = (struct smb2_fs_control_info *)(rsp->Buffer); | |
5177 | info->FreeSpaceStartFiltering = 0; | |
5178 | info->FreeSpaceThreshold = 0; | |
5179 | info->FreeSpaceStopFiltering = 0; | |
5180 | info->DefaultQuotaThreshold = cpu_to_le64(SMB2_NO_FID); | |
5181 | info->DefaultQuotaLimit = cpu_to_le64(SMB2_NO_FID); | |
5182 | info->Padding = 0; | |
5183 | rsp->OutputBufferLength = cpu_to_le32(48); | |
e2f34481 NJ |
5184 | break; |
5185 | } | |
5186 | case FS_POSIX_INFORMATION: | |
5187 | { | |
5188 | struct filesystem_posix_info *info; | |
e2f34481 NJ |
5189 | |
5190 | if (!work->tcon->posix_extensions) { | |
bde1694a | 5191 | pr_err("client doesn't negotiate with SMB3.1.1 POSIX Extensions\n"); |
e2f34481 NJ |
5192 | rc = -EOPNOTSUPP; |
5193 | } else { | |
5194 | info = (struct filesystem_posix_info *)(rsp->Buffer); | |
ee81cae1 | 5195 | info->OptimalTransferSize = cpu_to_le32(stfs.f_bsize); |
e2f34481 NJ |
5196 | info->BlockSize = cpu_to_le32(stfs.f_bsize); |
5197 | info->TotalBlocks = cpu_to_le64(stfs.f_blocks); | |
5198 | info->BlocksAvail = cpu_to_le64(stfs.f_bfree); | |
5199 | info->UserBlocksAvail = cpu_to_le64(stfs.f_bavail); | |
5200 | info->TotalFileNodes = cpu_to_le64(stfs.f_files); | |
5201 | info->FreeFileNodes = cpu_to_le64(stfs.f_ffree); | |
5202 | rsp->OutputBufferLength = cpu_to_le32(56); | |
e2f34481 NJ |
5203 | } |
5204 | break; | |
5205 | } | |
5206 | default: | |
5207 | path_put(&path); | |
5208 | return -EOPNOTSUPP; | |
5209 | } | |
5210 | rc = buffer_check_err(le32_to_cpu(req->OutputBufferLength), | |
e2b76ab8 | 5211 | rsp, work->response_buf); |
e2f34481 NJ |
5212 | path_put(&path); |
5213 | return rc; | |
5214 | } | |
5215 | ||
5216 | static int smb2_get_info_sec(struct ksmbd_work *work, | |
070fb21e | 5217 | struct smb2_query_info_req *req, |
cb451720 | 5218 | struct smb2_query_info_rsp *rsp) |
e2f34481 NJ |
5219 | { |
5220 | struct ksmbd_file *fp; | |
4609e1f1 | 5221 | struct mnt_idmap *idmap; |
e2f34481 NJ |
5222 | struct smb_ntsd *pntsd = (struct smb_ntsd *)rsp->Buffer, *ppntsd = NULL; |
5223 | struct smb_fattr fattr = {{0}}; | |
5224 | struct inode *inode; | |
8f054118 | 5225 | __u32 secdesclen = 0; |
e2f34481 NJ |
5226 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
5227 | int addition_info = le32_to_cpu(req->AdditionalInformation); | |
8f054118 | 5228 | int rc = 0, ppntsd_size = 0; |
e2f34481 | 5229 | |
e294f78d NJ |
5230 | if (addition_info & ~(OWNER_SECINFO | GROUP_SECINFO | DACL_SECINFO | |
5231 | PROTECTED_DACL_SECINFO | | |
5232 | UNPROTECTED_DACL_SECINFO)) { | |
8e537d14 | 5233 | ksmbd_debug(SMB, "Unsupported addition info: 0x%x)\n", |
e294f78d | 5234 | addition_info); |
ced2b26a SG |
5235 | |
5236 | pntsd->revision = cpu_to_le16(1); | |
5237 | pntsd->type = cpu_to_le16(SELF_RELATIVE | DACL_PROTECTED); | |
5238 | pntsd->osidoffset = 0; | |
5239 | pntsd->gsidoffset = 0; | |
5240 | pntsd->sacloffset = 0; | |
5241 | pntsd->dacloffset = 0; | |
5242 | ||
5243 | secdesclen = sizeof(struct smb_ntsd); | |
5244 | rsp->OutputBufferLength = cpu_to_le32(secdesclen); | |
ced2b26a SG |
5245 | |
5246 | return 0; | |
5247 | } | |
5248 | ||
e2f34481 | 5249 | if (work->next_smb2_rcv_hdr_off) { |
2d004c6c | 5250 | if (!has_file_id(req->VolatileFileId)) { |
3867369e | 5251 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
070fb21e | 5252 | work->compound_fid); |
e2f34481 NJ |
5253 | id = work->compound_fid; |
5254 | pid = work->compound_pfid; | |
5255 | } | |
5256 | } | |
5257 | ||
3867369e | 5258 | if (!has_file_id(id)) { |
2d004c6c PA |
5259 | id = req->VolatileFileId; |
5260 | pid = req->PersistentFileId; | |
e2f34481 NJ |
5261 | } |
5262 | ||
5263 | fp = ksmbd_lookup_fd_slow(work, id, pid); | |
5264 | if (!fp) | |
5265 | return -ENOENT; | |
5266 | ||
4609e1f1 | 5267 | idmap = file_mnt_idmap(fp->filp); |
ab0b263b | 5268 | inode = file_inode(fp->filp); |
e67fe633 | 5269 | ksmbd_acls_fattr(&fattr, idmap, inode); |
e2f34481 NJ |
5270 | |
5271 | if (test_share_config_flag(work->tcon->share_conf, | |
64b39f4a | 5272 | KSMBD_SHARE_FLAG_ACL_XATTR)) |
4609e1f1 | 5273 | ppntsd_size = ksmbd_vfs_get_sd_xattr(work->conn, idmap, |
8f054118 NJ |
5274 | fp->filp->f_path.dentry, |
5275 | &ppntsd); | |
5276 | ||
5277 | /* Check if sd buffer size exceeds response buffer size */ | |
5278 | if (smb2_resp_buf_len(work, 8) > ppntsd_size) | |
4d7ca409 | 5279 | rc = build_sec_desc(idmap, pntsd, ppntsd, ppntsd_size, |
8f054118 | 5280 | addition_info, &secdesclen, &fattr); |
e2f34481 NJ |
5281 | posix_acl_release(fattr.cf_acls); |
5282 | posix_acl_release(fattr.cf_dacls); | |
5283 | kfree(ppntsd); | |
5284 | ksmbd_fd_put(work, fp); | |
5285 | if (rc) | |
5286 | return rc; | |
5287 | ||
5288 | rsp->OutputBufferLength = cpu_to_le32(secdesclen); | |
e2f34481 NJ |
5289 | return 0; |
5290 | } | |
5291 | ||
5292 | /** | |
5293 | * smb2_query_info() - handler for smb2 query info command | |
5294 | * @work: smb work containing query info request buffer | |
5295 | * | |
5296 | * Return: 0 on success, otherwise error | |
5297 | */ | |
5298 | int smb2_query_info(struct ksmbd_work *work) | |
5299 | { | |
5300 | struct smb2_query_info_req *req; | |
cb451720 | 5301 | struct smb2_query_info_rsp *rsp; |
e2f34481 NJ |
5302 | int rc = 0; |
5303 | ||
e2f34481 NJ |
5304 | WORK_BUFFERS(work, req, rsp); |
5305 | ||
5306 | ksmbd_debug(SMB, "GOT query info request\n"); | |
5307 | ||
5308 | switch (req->InfoType) { | |
5309 | case SMB2_O_INFO_FILE: | |
5310 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); | |
cb451720 | 5311 | rc = smb2_get_info_file(work, req, rsp); |
e2f34481 NJ |
5312 | break; |
5313 | case SMB2_O_INFO_FILESYSTEM: | |
5314 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILESYSTEM\n"); | |
cb451720 | 5315 | rc = smb2_get_info_filesystem(work, req, rsp); |
e2f34481 NJ |
5316 | break; |
5317 | case SMB2_O_INFO_SECURITY: | |
5318 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); | |
cb451720 | 5319 | rc = smb2_get_info_sec(work, req, rsp); |
e2f34481 NJ |
5320 | break; |
5321 | default: | |
5322 | ksmbd_debug(SMB, "InfoType %d not supported yet\n", | |
070fb21e | 5323 | req->InfoType); |
e2f34481 NJ |
5324 | rc = -EOPNOTSUPP; |
5325 | } | |
5326 | ||
e2b76ab8 NJ |
5327 | if (!rc) { |
5328 | rsp->StructureSize = cpu_to_le16(9); | |
5329 | rsp->OutputBufferOffset = cpu_to_le16(72); | |
5330 | rc = ksmbd_iov_pin_rsp(work, (void *)rsp, | |
5331 | offsetof(struct smb2_query_info_rsp, Buffer) + | |
5332 | le32_to_cpu(rsp->OutputBufferLength)); | |
5333 | } | |
5334 | ||
e2f34481 NJ |
5335 | if (rc < 0) { |
5336 | if (rc == -EACCES) | |
5337 | rsp->hdr.Status = STATUS_ACCESS_DENIED; | |
5338 | else if (rc == -ENOENT) | |
5339 | rsp->hdr.Status = STATUS_FILE_CLOSED; | |
5340 | else if (rc == -EIO) | |
5341 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; | |
e2b76ab8 NJ |
5342 | else if (rc == -ENOMEM) |
5343 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; | |
e2f34481 NJ |
5344 | else if (rc == -EOPNOTSUPP || rsp->hdr.Status == 0) |
5345 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; | |
5346 | smb2_set_err_rsp(work); | |
5347 | ||
5348 | ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", | |
070fb21e | 5349 | rc); |
e2f34481 NJ |
5350 | return rc; |
5351 | } | |
e2f34481 NJ |
5352 | return 0; |
5353 | } | |
5354 | ||
5355 | /** | |
5356 | * smb2_close_pipe() - handler for closing IPC pipe | |
5357 | * @work: smb work containing close request buffer | |
5358 | * | |
5359 | * Return: 0 | |
5360 | */ | |
5361 | static noinline int smb2_close_pipe(struct ksmbd_work *work) | |
5362 | { | |
64b39f4a | 5363 | u64 id; |
7b7d709e NJ |
5364 | struct smb2_close_req *req; |
5365 | struct smb2_close_rsp *rsp; | |
5366 | ||
5367 | WORK_BUFFERS(work, req, rsp); | |
e2f34481 | 5368 | |
2d004c6c | 5369 | id = req->VolatileFileId; |
e2f34481 NJ |
5370 | ksmbd_session_rpc_close(work->sess, id); |
5371 | ||
5372 | rsp->StructureSize = cpu_to_le16(60); | |
5373 | rsp->Flags = 0; | |
5374 | rsp->Reserved = 0; | |
5375 | rsp->CreationTime = 0; | |
5376 | rsp->LastAccessTime = 0; | |
5377 | rsp->LastWriteTime = 0; | |
5378 | rsp->ChangeTime = 0; | |
5379 | rsp->AllocationSize = 0; | |
5380 | rsp->EndOfFile = 0; | |
5381 | rsp->Attributes = 0; | |
e2b76ab8 NJ |
5382 | |
5383 | return ksmbd_iov_pin_rsp(work, (void *)rsp, | |
5384 | sizeof(struct smb2_close_rsp)); | |
e2f34481 NJ |
5385 | } |
5386 | ||
5387 | /** | |
5388 | * smb2_close() - handler for smb2 close file command | |
5389 | * @work: smb work containing close request buffer | |
5390 | * | |
5391 | * Return: 0 | |
5392 | */ | |
5393 | int smb2_close(struct ksmbd_work *work) | |
5394 | { | |
3867369e | 5395 | u64 volatile_id = KSMBD_NO_FID; |
64b39f4a | 5396 | u64 sess_id; |
e2f34481 NJ |
5397 | struct smb2_close_req *req; |
5398 | struct smb2_close_rsp *rsp; | |
e2f34481 NJ |
5399 | struct ksmbd_conn *conn = work->conn; |
5400 | struct ksmbd_file *fp; | |
5401 | struct inode *inode; | |
5402 | u64 time; | |
5403 | int err = 0; | |
5404 | ||
e2f34481 NJ |
5405 | WORK_BUFFERS(work, req, rsp); |
5406 | ||
5407 | if (test_share_config_flag(work->tcon->share_conf, | |
5408 | KSMBD_SHARE_FLAG_PIPE)) { | |
5409 | ksmbd_debug(SMB, "IPC pipe close request\n"); | |
5410 | return smb2_close_pipe(work); | |
5411 | } | |
5412 | ||
5413 | sess_id = le64_to_cpu(req->hdr.SessionId); | |
5414 | if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) | |
5415 | sess_id = work->compound_sid; | |
5416 | ||
5417 | work->compound_sid = 0; | |
64b39f4a | 5418 | if (check_session_id(conn, sess_id)) { |
e2f34481 | 5419 | work->compound_sid = sess_id; |
64b39f4a | 5420 | } else { |
e2f34481 NJ |
5421 | rsp->hdr.Status = STATUS_USER_SESSION_DELETED; |
5422 | if (req->hdr.Flags & SMB2_FLAGS_RELATED_OPERATIONS) | |
5423 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
5424 | err = -EBADF; | |
5425 | goto out; | |
5426 | } | |
5427 | ||
5428 | if (work->next_smb2_rcv_hdr_off && | |
2d004c6c | 5429 | !has_file_id(req->VolatileFileId)) { |
3867369e | 5430 | if (!has_file_id(work->compound_fid)) { |
e2f34481 NJ |
5431 | /* file already closed, return FILE_CLOSED */ |
5432 | ksmbd_debug(SMB, "file already closed\n"); | |
5433 | rsp->hdr.Status = STATUS_FILE_CLOSED; | |
5434 | err = -EBADF; | |
5435 | goto out; | |
5436 | } else { | |
3867369e NJ |
5437 | ksmbd_debug(SMB, |
5438 | "Compound request set FID = %llu:%llu\n", | |
070fb21e NJ |
5439 | work->compound_fid, |
5440 | work->compound_pfid); | |
e2f34481 NJ |
5441 | volatile_id = work->compound_fid; |
5442 | ||
5443 | /* file closed, stored id is not valid anymore */ | |
5444 | work->compound_fid = KSMBD_NO_FID; | |
5445 | work->compound_pfid = KSMBD_NO_FID; | |
5446 | } | |
5447 | } else { | |
2d004c6c | 5448 | volatile_id = req->VolatileFileId; |
e2f34481 | 5449 | } |
3867369e | 5450 | ksmbd_debug(SMB, "volatile_id = %llu\n", volatile_id); |
e2f34481 NJ |
5451 | |
5452 | rsp->StructureSize = cpu_to_le16(60); | |
5453 | rsp->Reserved = 0; | |
5454 | ||
5455 | if (req->Flags == SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB) { | |
5456 | fp = ksmbd_lookup_fd_fast(work, volatile_id); | |
5457 | if (!fp) { | |
5458 | err = -ENOENT; | |
5459 | goto out; | |
5460 | } | |
5461 | ||
ab0b263b | 5462 | inode = file_inode(fp->filp); |
e2f34481 NJ |
5463 | rsp->Flags = SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB; |
5464 | rsp->AllocationSize = S_ISDIR(inode->i_mode) ? 0 : | |
5465 | cpu_to_le64(inode->i_blocks << 9); | |
5466 | rsp->EndOfFile = cpu_to_le64(inode->i_size); | |
5467 | rsp->Attributes = fp->f_ci->m_fattr; | |
5468 | rsp->CreationTime = cpu_to_le64(fp->create_time); | |
769cfc91 | 5469 | time = ksmbd_UnixTimeToNT(inode_get_atime(inode)); |
e2f34481 | 5470 | rsp->LastAccessTime = cpu_to_le64(time); |
769cfc91 | 5471 | time = ksmbd_UnixTimeToNT(inode_get_mtime(inode)); |
e2f34481 | 5472 | rsp->LastWriteTime = cpu_to_le64(time); |
94487653 | 5473 | time = ksmbd_UnixTimeToNT(inode_get_ctime(inode)); |
e2f34481 NJ |
5474 | rsp->ChangeTime = cpu_to_le64(time); |
5475 | ksmbd_fd_put(work, fp); | |
5476 | } else { | |
5477 | rsp->Flags = 0; | |
5478 | rsp->AllocationSize = 0; | |
5479 | rsp->EndOfFile = 0; | |
5480 | rsp->Attributes = 0; | |
5481 | rsp->CreationTime = 0; | |
5482 | rsp->LastAccessTime = 0; | |
5483 | rsp->LastWriteTime = 0; | |
5484 | rsp->ChangeTime = 0; | |
5485 | } | |
5486 | ||
5487 | err = ksmbd_close_fd(work, volatile_id); | |
5488 | out: | |
e2b76ab8 NJ |
5489 | if (!err) |
5490 | err = ksmbd_iov_pin_rsp(work, (void *)rsp, | |
5491 | sizeof(struct smb2_close_rsp)); | |
5492 | ||
e2f34481 NJ |
5493 | if (err) { |
5494 | if (rsp->hdr.Status == 0) | |
5495 | rsp->hdr.Status = STATUS_FILE_CLOSED; | |
5496 | smb2_set_err_rsp(work); | |
e2f34481 NJ |
5497 | } |
5498 | ||
e2b76ab8 | 5499 | return err; |
e2f34481 NJ |
5500 | } |
5501 | ||
5502 | /** | |
5503 | * smb2_echo() - handler for smb2 echo(ping) command | |
5504 | * @work: smb work containing echo request buffer | |
5505 | * | |
5506 | * Return: 0 | |
5507 | */ | |
5508 | int smb2_echo(struct ksmbd_work *work) | |
5509 | { | |
cb451720 | 5510 | struct smb2_echo_rsp *rsp = smb2_get_msg(work->response_buf); |
e2f34481 | 5511 | |
7b7d709e NJ |
5512 | if (work->next_smb2_rcv_hdr_off) |
5513 | rsp = ksmbd_resp_buf_next(work); | |
5514 | ||
e2f34481 NJ |
5515 | rsp->StructureSize = cpu_to_le16(4); |
5516 | rsp->Reserved = 0; | |
e2b76ab8 | 5517 | return ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_echo_rsp)); |
e2f34481 NJ |
5518 | } |
5519 | ||
da1e7ada CB |
5520 | static int smb2_rename(struct ksmbd_work *work, |
5521 | struct ksmbd_file *fp, | |
070fb21e NJ |
5522 | struct smb2_file_rename_info *file_info, |
5523 | struct nls_table *local_nls) | |
e2f34481 NJ |
5524 | { |
5525 | struct ksmbd_share_config *share = fp->tcon->share_conf; | |
74d7970f NJ |
5526 | char *new_name = NULL; |
5527 | int rc, flags = 0; | |
e2f34481 NJ |
5528 | |
5529 | ksmbd_debug(SMB, "setting FILE_RENAME_INFO\n"); | |
80917f17 | 5530 | new_name = smb2_get_name(file_info->FileName, |
e2f34481 NJ |
5531 | le32_to_cpu(file_info->FileNameLength), |
5532 | local_nls); | |
74d7970f NJ |
5533 | if (IS_ERR(new_name)) |
5534 | return PTR_ERR(new_name); | |
e2f34481 NJ |
5535 | |
5536 | if (strchr(new_name, ':')) { | |
5537 | int s_type; | |
5538 | char *xattr_stream_name, *stream_name = NULL; | |
5539 | size_t xattr_stream_size; | |
5540 | int len; | |
5541 | ||
5542 | rc = parse_stream_name(new_name, &stream_name, &s_type); | |
5543 | if (rc < 0) | |
5544 | goto out; | |
5545 | ||
5546 | len = strlen(new_name); | |
265fd199 | 5547 | if (len > 0 && new_name[len - 1] != '/') { |
bde1694a | 5548 | pr_err("not allow base filename in rename\n"); |
e2f34481 NJ |
5549 | rc = -ESHARE; |
5550 | goto out; | |
5551 | } | |
5552 | ||
5553 | rc = ksmbd_vfs_xattr_stream_name(stream_name, | |
5554 | &xattr_stream_name, | |
5555 | &xattr_stream_size, | |
5556 | s_type); | |
5557 | if (rc) | |
5558 | goto out; | |
5559 | ||
74d7970f | 5560 | rc = ksmbd_vfs_setxattr(file_mnt_idmap(fp->filp), |
40b268d3 | 5561 | &fp->filp->f_path, |
e2f34481 | 5562 | xattr_stream_name, |
864fb5d3 | 5563 | NULL, 0, 0, true); |
e2f34481 | 5564 | if (rc < 0) { |
bde1694a NJ |
5565 | pr_err("failed to store stream name in xattr: %d\n", |
5566 | rc); | |
e2f34481 NJ |
5567 | rc = -EINVAL; |
5568 | goto out; | |
5569 | } | |
5570 | ||
5571 | goto out; | |
5572 | } | |
5573 | ||
5574 | ksmbd_debug(SMB, "new name %s\n", new_name); | |
e2f34481 NJ |
5575 | if (ksmbd_share_veto_filename(share, new_name)) { |
5576 | rc = -ENOENT; | |
5577 | ksmbd_debug(SMB, "Can't rename vetoed file: %s\n", new_name); | |
5578 | goto out; | |
5579 | } | |
5580 | ||
74d7970f NJ |
5581 | if (!file_info->ReplaceIfExists) |
5582 | flags = RENAME_NOREPLACE; | |
e2f34481 | 5583 | |
3fc74c65 | 5584 | smb_break_all_levII_oplock(work, fp, 0); |
74d7970f | 5585 | rc = ksmbd_vfs_rename(work, &fp->filp->f_path, new_name, flags); |
e2f34481 | 5586 | out: |
74d7970f | 5587 | kfree(new_name); |
e2f34481 NJ |
5588 | return rc; |
5589 | } | |
5590 | ||
e2f34481 | 5591 | static int smb2_create_link(struct ksmbd_work *work, |
070fb21e NJ |
5592 | struct ksmbd_share_config *share, |
5593 | struct smb2_file_link_info *file_info, | |
9496e268 | 5594 | unsigned int buf_len, struct file *filp, |
070fb21e | 5595 | struct nls_table *local_nls) |
e2f34481 NJ |
5596 | { |
5597 | char *link_name = NULL, *target_name = NULL, *pathname = NULL; | |
2b57a432 | 5598 | struct path path, parent_path; |
df14afee | 5599 | bool file_present = false; |
e2f34481 NJ |
5600 | int rc; |
5601 | ||
9496e268 NJ |
5602 | if (buf_len < (u64)sizeof(struct smb2_file_link_info) + |
5603 | le32_to_cpu(file_info->FileNameLength)) | |
5604 | return -EINVAL; | |
5605 | ||
e2f34481 NJ |
5606 | ksmbd_debug(SMB, "setting FILE_LINK_INFORMATION\n"); |
5607 | pathname = kmalloc(PATH_MAX, GFP_KERNEL); | |
5608 | if (!pathname) | |
5609 | return -ENOMEM; | |
5610 | ||
80917f17 | 5611 | link_name = smb2_get_name(file_info->FileName, |
e2f34481 NJ |
5612 | le32_to_cpu(file_info->FileNameLength), |
5613 | local_nls); | |
5614 | if (IS_ERR(link_name) || S_ISDIR(file_inode(filp)->i_mode)) { | |
5615 | rc = -EINVAL; | |
5616 | goto out; | |
5617 | } | |
5618 | ||
5619 | ksmbd_debug(SMB, "link name is %s\n", link_name); | |
2f5930c1 | 5620 | target_name = file_path(filp, pathname, PATH_MAX); |
e2f34481 NJ |
5621 | if (IS_ERR(target_name)) { |
5622 | rc = -EINVAL; | |
5623 | goto out; | |
5624 | } | |
5625 | ||
5626 | ksmbd_debug(SMB, "target name is %s\n", target_name); | |
74d7970f | 5627 | rc = ksmbd_vfs_kern_path_locked(work, link_name, LOOKUP_NO_SYMLINKS, |
2b57a432 | 5628 | &parent_path, &path, 0); |
265fd199 HL |
5629 | if (rc) { |
5630 | if (rc != -ENOENT) | |
5631 | goto out; | |
df14afee NJ |
5632 | } else |
5633 | file_present = true; | |
e2f34481 NJ |
5634 | |
5635 | if (file_info->ReplaceIfExists) { | |
5636 | if (file_present) { | |
74d7970f | 5637 | rc = ksmbd_vfs_remove_file(work, &path); |
e2f34481 NJ |
5638 | if (rc) { |
5639 | rc = -EINVAL; | |
5640 | ksmbd_debug(SMB, "cannot delete %s\n", | |
070fb21e | 5641 | link_name); |
e2f34481 NJ |
5642 | goto out; |
5643 | } | |
5644 | } | |
5645 | } else { | |
5646 | if (file_present) { | |
5647 | rc = -EEXIST; | |
5648 | ksmbd_debug(SMB, "link already exists\n"); | |
5649 | goto out; | |
5650 | } | |
5651 | } | |
5652 | ||
5653 | rc = ksmbd_vfs_link(work, target_name, link_name); | |
5654 | if (rc) | |
5655 | rc = -EINVAL; | |
5656 | out: | |
864fb5d3 NJ |
5657 | if (file_present) |
5658 | ksmbd_vfs_kern_path_unlock(&parent_path, &path); | |
5659 | ||
e2f34481 | 5660 | if (!IS_ERR(link_name)) |
915f570a | 5661 | kfree(link_name); |
e2f34481 NJ |
5662 | kfree(pathname); |
5663 | return rc; | |
5664 | } | |
5665 | ||
9496e268 NJ |
5666 | static int set_file_basic_info(struct ksmbd_file *fp, |
5667 | struct smb2_file_basic_info *file_info, | |
070fb21e | 5668 | struct ksmbd_share_config *share) |
e2f34481 | 5669 | { |
e2f34481 | 5670 | struct iattr attrs; |
e2f34481 NJ |
5671 | struct file *filp; |
5672 | struct inode *inode; | |
abf08576 | 5673 | struct mnt_idmap *idmap; |
4ffd5264 | 5674 | int rc = 0; |
e2f34481 | 5675 | |
7adfd4f6 | 5676 | if (!(fp->daccess & FILE_WRITE_ATTRIBUTES_LE)) |
e2f34481 NJ |
5677 | return -EACCES; |
5678 | ||
e2f34481 NJ |
5679 | attrs.ia_valid = 0; |
5680 | filp = fp->filp; | |
5681 | inode = file_inode(filp); | |
abf08576 | 5682 | idmap = file_mnt_idmap(filp); |
e2f34481 NJ |
5683 | |
5684 | if (file_info->CreationTime) | |
5685 | fp->create_time = le64_to_cpu(file_info->CreationTime); | |
5686 | ||
5687 | if (file_info->LastAccessTime) { | |
5688 | attrs.ia_atime = ksmbd_NTtimeToUnix(file_info->LastAccessTime); | |
5689 | attrs.ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET); | |
5690 | } | |
5691 | ||
64e78755 NJ |
5692 | attrs.ia_valid |= ATTR_CTIME; |
5693 | if (file_info->ChangeTime) | |
db7fb6fe | 5694 | attrs.ia_ctime = ksmbd_NTtimeToUnix(file_info->ChangeTime); |
64e78755 | 5695 | else |
94487653 | 5696 | attrs.ia_ctime = inode_get_ctime(inode); |
e2f34481 NJ |
5697 | |
5698 | if (file_info->LastWriteTime) { | |
5699 | attrs.ia_mtime = ksmbd_NTtimeToUnix(file_info->LastWriteTime); | |
5700 | attrs.ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET); | |
5701 | } | |
5702 | ||
5703 | if (file_info->Attributes) { | |
5704 | if (!S_ISDIR(inode->i_mode) && | |
26a2787d | 5705 | file_info->Attributes & FILE_ATTRIBUTE_DIRECTORY_LE) { |
bde1694a | 5706 | pr_err("can't change a file to a directory\n"); |
e2f34481 NJ |
5707 | return -EINVAL; |
5708 | } | |
5709 | ||
26a2787d | 5710 | if (!(S_ISDIR(inode->i_mode) && file_info->Attributes == FILE_ATTRIBUTE_NORMAL_LE)) |
e2f34481 | 5711 | fp->f_ci->m_fattr = file_info->Attributes | |
26a2787d | 5712 | (fp->f_ci->m_fattr & FILE_ATTRIBUTE_DIRECTORY_LE); |
e2f34481 NJ |
5713 | } |
5714 | ||
5715 | if (test_share_config_flag(share, KSMBD_SHARE_FLAG_STORE_DOS_ATTRS) && | |
5716 | (file_info->CreationTime || file_info->Attributes)) { | |
5717 | struct xattr_dos_attrib da = {0}; | |
5718 | ||
5719 | da.version = 4; | |
5720 | da.itime = fp->itime; | |
5721 | da.create_time = fp->create_time; | |
5722 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); | |
5723 | da.flags = XATTR_DOSINFO_ATTRIB | XATTR_DOSINFO_CREATE_TIME | | |
5724 | XATTR_DOSINFO_ITIME; | |
5725 | ||
864fb5d3 NJ |
5726 | rc = ksmbd_vfs_set_dos_attrib_xattr(idmap, &filp->f_path, &da, |
5727 | true); | |
e2f34481 NJ |
5728 | if (rc) |
5729 | ksmbd_debug(SMB, | |
070fb21e | 5730 | "failed to restore file attribute in EA\n"); |
e2f34481 NJ |
5731 | rc = 0; |
5732 | } | |
5733 | ||
e2f34481 NJ |
5734 | if (attrs.ia_valid) { |
5735 | struct dentry *dentry = filp->f_path.dentry; | |
5736 | struct inode *inode = d_inode(dentry); | |
5737 | ||
5738 | if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) | |
5739 | return -EACCES; | |
5740 | ||
e2f34481 | 5741 | inode_lock(inode); |
94487653 | 5742 | inode_set_ctime_to_ts(inode, attrs.ia_ctime); |
64e78755 | 5743 | attrs.ia_valid &= ~ATTR_CTIME; |
abf08576 | 5744 | rc = notify_change(idmap, dentry, &attrs, NULL); |
e2f34481 NJ |
5745 | inode_unlock(inode); |
5746 | } | |
eb5784f0 | 5747 | return rc; |
e2f34481 NJ |
5748 | } |
5749 | ||
5750 | static int set_file_allocation_info(struct ksmbd_work *work, | |
9496e268 NJ |
5751 | struct ksmbd_file *fp, |
5752 | struct smb2_file_alloc_info *file_alloc_info) | |
e2f34481 NJ |
5753 | { |
5754 | /* | |
5755 | * TODO : It's working fine only when store dos attributes | |
5756 | * is not yes. need to implement a logic which works | |
5757 | * properly with any smb.conf option | |
5758 | */ | |
5759 | ||
e2f34481 NJ |
5760 | loff_t alloc_blks; |
5761 | struct inode *inode; | |
5762 | int rc; | |
5763 | ||
a299669b | 5764 | if (!(fp->daccess & FILE_WRITE_DATA_LE)) |
e2f34481 NJ |
5765 | return -EACCES; |
5766 | ||
e2f34481 NJ |
5767 | alloc_blks = (le64_to_cpu(file_alloc_info->AllocationSize) + 511) >> 9; |
5768 | inode = file_inode(fp->filp); | |
5769 | ||
5770 | if (alloc_blks > inode->i_blocks) { | |
e8c06191 NJ |
5771 | smb_break_all_levII_oplock(work, fp, 1); |
5772 | rc = vfs_fallocate(fp->filp, FALLOC_FL_KEEP_SIZE, 0, | |
5773 | alloc_blks * 512); | |
e2f34481 | 5774 | if (rc && rc != -EOPNOTSUPP) { |
e8c06191 | 5775 | pr_err("vfs_fallocate is failed : %d\n", rc); |
e2f34481 NJ |
5776 | return rc; |
5777 | } | |
5778 | } else if (alloc_blks < inode->i_blocks) { | |
5779 | loff_t size; | |
5780 | ||
5781 | /* | |
5782 | * Allocation size could be smaller than original one | |
5783 | * which means allocated blocks in file should be | |
5784 | * deallocated. use truncate to cut out it, but inode | |
5785 | * size is also updated with truncate offset. | |
5786 | * inode size is retained by backup inode size. | |
5787 | */ | |
5788 | size = i_size_read(inode); | |
265fd199 | 5789 | rc = ksmbd_vfs_truncate(work, fp, alloc_blks * 512); |
e2f34481 | 5790 | if (rc) { |
50f500b7 | 5791 | pr_err("truncate failed!, err %d\n", rc); |
e2f34481 NJ |
5792 | return rc; |
5793 | } | |
5794 | if (size < alloc_blks * 512) | |
5795 | i_size_write(inode, size); | |
5796 | } | |
5797 | return 0; | |
5798 | } | |
5799 | ||
64b39f4a | 5800 | static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp, |
9496e268 | 5801 | struct smb2_file_eof_info *file_eof_info) |
e2f34481 | 5802 | { |
e2f34481 NJ |
5803 | loff_t newsize; |
5804 | struct inode *inode; | |
5805 | int rc; | |
5806 | ||
a299669b | 5807 | if (!(fp->daccess & FILE_WRITE_DATA_LE)) |
e2f34481 NJ |
5808 | return -EACCES; |
5809 | ||
e2f34481 NJ |
5810 | newsize = le64_to_cpu(file_eof_info->EndOfFile); |
5811 | inode = file_inode(fp->filp); | |
5812 | ||
5813 | /* | |
5814 | * If FILE_END_OF_FILE_INFORMATION of set_info_file is called | |
5815 | * on FAT32 shared device, truncate execution time is too long | |
5816 | * and network error could cause from windows client. because | |
5817 | * truncate of some filesystem like FAT32 fill zero data in | |
5818 | * truncated range. | |
5819 | */ | |
5820 | if (inode->i_sb->s_magic != MSDOS_SUPER_MAGIC) { | |
50f500b7 | 5821 | ksmbd_debug(SMB, "truncated to newsize %lld\n", newsize); |
265fd199 | 5822 | rc = ksmbd_vfs_truncate(work, fp, newsize); |
e2f34481 | 5823 | if (rc) { |
50f500b7 | 5824 | ksmbd_debug(SMB, "truncate failed!, err %d\n", rc); |
e2f34481 NJ |
5825 | if (rc != -EAGAIN) |
5826 | rc = -EBADF; | |
5827 | return rc; | |
5828 | } | |
5829 | } | |
5830 | return 0; | |
5831 | } | |
5832 | ||
64b39f4a | 5833 | static int set_rename_info(struct ksmbd_work *work, struct ksmbd_file *fp, |
9496e268 NJ |
5834 | struct smb2_file_rename_info *rename_info, |
5835 | unsigned int buf_len) | |
e2f34481 | 5836 | { |
e2f34481 | 5837 | if (!(fp->daccess & FILE_DELETE_LE)) { |
bde1694a | 5838 | pr_err("no right to delete : 0x%x\n", fp->daccess); |
e2f34481 NJ |
5839 | return -EACCES; |
5840 | } | |
5841 | ||
9496e268 NJ |
5842 | if (buf_len < (u64)sizeof(struct smb2_file_rename_info) + |
5843 | le32_to_cpu(rename_info->FileNameLength)) | |
5844 | return -EINVAL; | |
5845 | ||
74d7970f NJ |
5846 | if (!le32_to_cpu(rename_info->FileNameLength)) |
5847 | return -EINVAL; | |
12202c05 | 5848 | |
74d7970f | 5849 | return smb2_rename(work, fp, rename_info, work->conn->local_nls); |
e2f34481 NJ |
5850 | } |
5851 | ||
9496e268 NJ |
5852 | static int set_file_disposition_info(struct ksmbd_file *fp, |
5853 | struct smb2_file_disposition_info *file_info) | |
e2f34481 | 5854 | { |
e2f34481 NJ |
5855 | struct inode *inode; |
5856 | ||
5857 | if (!(fp->daccess & FILE_DELETE_LE)) { | |
bde1694a | 5858 | pr_err("no right to delete : 0x%x\n", fp->daccess); |
e2f34481 NJ |
5859 | return -EACCES; |
5860 | } | |
5861 | ||
5862 | inode = file_inode(fp->filp); | |
e2f34481 NJ |
5863 | if (file_info->DeletePending) { |
5864 | if (S_ISDIR(inode->i_mode) && | |
64b39f4a | 5865 | ksmbd_vfs_empty_dir(fp) == -ENOTEMPTY) |
e2f34481 NJ |
5866 | return -EBUSY; |
5867 | ksmbd_set_inode_pending_delete(fp); | |
5868 | } else { | |
5869 | ksmbd_clear_inode_pending_delete(fp); | |
5870 | } | |
5871 | return 0; | |
5872 | } | |
5873 | ||
9496e268 NJ |
5874 | static int set_file_position_info(struct ksmbd_file *fp, |
5875 | struct smb2_file_pos_info *file_info) | |
e2f34481 | 5876 | { |
e2f34481 | 5877 | loff_t current_byte_offset; |
ee81cae1 | 5878 | unsigned long sector_size; |
e2f34481 NJ |
5879 | struct inode *inode; |
5880 | ||
5881 | inode = file_inode(fp->filp); | |
e2f34481 | 5882 | current_byte_offset = le64_to_cpu(file_info->CurrentByteOffset); |
ee81cae1 | 5883 | sector_size = inode->i_sb->s_blocksize; |
e2f34481 NJ |
5884 | |
5885 | if (current_byte_offset < 0 || | |
64b39f4a NJ |
5886 | (fp->coption == FILE_NO_INTERMEDIATE_BUFFERING_LE && |
5887 | current_byte_offset & (sector_size - 1))) { | |
bde1694a NJ |
5888 | pr_err("CurrentByteOffset is not valid : %llu\n", |
5889 | current_byte_offset); | |
e2f34481 NJ |
5890 | return -EINVAL; |
5891 | } | |
5892 | ||
5893 | fp->filp->f_pos = current_byte_offset; | |
5894 | return 0; | |
5895 | } | |
5896 | ||
9496e268 NJ |
5897 | static int set_file_mode_info(struct ksmbd_file *fp, |
5898 | struct smb2_file_mode_info *file_info) | |
e2f34481 | 5899 | { |
e2f34481 NJ |
5900 | __le32 mode; |
5901 | ||
e2f34481 NJ |
5902 | mode = file_info->Mode; |
5903 | ||
26a2787d | 5904 | if ((mode & ~FILE_MODE_INFO_MASK)) { |
bde1694a | 5905 | pr_err("Mode is not valid : 0x%x\n", le32_to_cpu(mode)); |
e2f34481 NJ |
5906 | return -EINVAL; |
5907 | } | |
5908 | ||
5909 | /* | |
5910 | * TODO : need to implement consideration for | |
5911 | * FILE_SYNCHRONOUS_IO_ALERT and FILE_SYNCHRONOUS_IO_NONALERT | |
5912 | */ | |
5913 | ksmbd_vfs_set_fadvise(fp->filp, mode); | |
5914 | fp->coption = mode; | |
5915 | return 0; | |
5916 | } | |
5917 | ||
5918 | /** | |
5919 | * smb2_set_info_file() - handler for smb2 set info command | |
5920 | * @work: smb work containing set info command buffer | |
95fa1ce9 | 5921 | * @fp: ksmbd_file pointer |
4bfd9eed | 5922 | * @req: request buffer pointer |
95fa1ce9 | 5923 | * @share: ksmbd_share_config pointer |
e2f34481 NJ |
5924 | * |
5925 | * Return: 0 on success, otherwise error | |
5926 | * TODO: need to implement an error handling for STATUS_INFO_LENGTH_MISMATCH | |
5927 | */ | |
64b39f4a | 5928 | static int smb2_set_info_file(struct ksmbd_work *work, struct ksmbd_file *fp, |
9496e268 | 5929 | struct smb2_set_info_req *req, |
070fb21e | 5930 | struct ksmbd_share_config *share) |
e2f34481 | 5931 | { |
9496e268 NJ |
5932 | unsigned int buf_len = le32_to_cpu(req->BufferLength); |
5933 | ||
5934 | switch (req->FileInfoClass) { | |
e2f34481 | 5935 | case FILE_BASIC_INFORMATION: |
9496e268 NJ |
5936 | { |
5937 | if (buf_len < sizeof(struct smb2_file_basic_info)) | |
5938 | return -EINVAL; | |
e2f34481 | 5939 | |
9496e268 NJ |
5940 | return set_file_basic_info(fp, (struct smb2_file_basic_info *)req->Buffer, share); |
5941 | } | |
e2f34481 | 5942 | case FILE_ALLOCATION_INFORMATION: |
9496e268 NJ |
5943 | { |
5944 | if (buf_len < sizeof(struct smb2_file_alloc_info)) | |
5945 | return -EINVAL; | |
e2f34481 | 5946 | |
9496e268 NJ |
5947 | return set_file_allocation_info(work, fp, |
5948 | (struct smb2_file_alloc_info *)req->Buffer); | |
5949 | } | |
e2f34481 | 5950 | case FILE_END_OF_FILE_INFORMATION: |
9496e268 NJ |
5951 | { |
5952 | if (buf_len < sizeof(struct smb2_file_eof_info)) | |
5953 | return -EINVAL; | |
e2f34481 | 5954 | |
9496e268 NJ |
5955 | return set_end_of_file_info(work, fp, |
5956 | (struct smb2_file_eof_info *)req->Buffer); | |
5957 | } | |
e2f34481 | 5958 | case FILE_RENAME_INFORMATION: |
9496e268 | 5959 | { |
9496e268 NJ |
5960 | if (buf_len < sizeof(struct smb2_file_rename_info)) |
5961 | return -EINVAL; | |
5962 | ||
5963 | return set_rename_info(work, fp, | |
5964 | (struct smb2_file_rename_info *)req->Buffer, | |
5965 | buf_len); | |
5966 | } | |
e2f34481 | 5967 | case FILE_LINK_INFORMATION: |
9496e268 NJ |
5968 | { |
5969 | if (buf_len < sizeof(struct smb2_file_link_info)) | |
5970 | return -EINVAL; | |
5971 | ||
e2f34481 | 5972 | return smb2_create_link(work, work->tcon->share_conf, |
9496e268 NJ |
5973 | (struct smb2_file_link_info *)req->Buffer, |
5974 | buf_len, fp->filp, | |
af7c39d9 | 5975 | work->conn->local_nls); |
9496e268 | 5976 | } |
e2f34481 | 5977 | case FILE_DISPOSITION_INFORMATION: |
9496e268 | 5978 | { |
9496e268 NJ |
5979 | if (buf_len < sizeof(struct smb2_file_disposition_info)) |
5980 | return -EINVAL; | |
5981 | ||
5982 | return set_file_disposition_info(fp, | |
5983 | (struct smb2_file_disposition_info *)req->Buffer); | |
5984 | } | |
e2f34481 NJ |
5985 | case FILE_FULL_EA_INFORMATION: |
5986 | { | |
5987 | if (!(fp->daccess & FILE_WRITE_EA_LE)) { | |
bde1694a NJ |
5988 | pr_err("Not permitted to write ext attr: 0x%x\n", |
5989 | fp->daccess); | |
e2f34481 NJ |
5990 | return -EACCES; |
5991 | } | |
5992 | ||
9496e268 NJ |
5993 | if (buf_len < sizeof(struct smb2_ea_info)) |
5994 | return -EINVAL; | |
e2f34481 | 5995 | |
9496e268 | 5996 | return smb2_set_ea((struct smb2_ea_info *)req->Buffer, |
6fc0a265 | 5997 | buf_len, &fp->filp->f_path, true); |
9496e268 | 5998 | } |
e2f34481 | 5999 | case FILE_POSITION_INFORMATION: |
9496e268 NJ |
6000 | { |
6001 | if (buf_len < sizeof(struct smb2_file_pos_info)) | |
6002 | return -EINVAL; | |
e2f34481 | 6003 | |
9496e268 NJ |
6004 | return set_file_position_info(fp, (struct smb2_file_pos_info *)req->Buffer); |
6005 | } | |
e2f34481 | 6006 | case FILE_MODE_INFORMATION: |
9496e268 NJ |
6007 | { |
6008 | if (buf_len < sizeof(struct smb2_file_mode_info)) | |
6009 | return -EINVAL; | |
6010 | ||
6011 | return set_file_mode_info(fp, (struct smb2_file_mode_info *)req->Buffer); | |
6012 | } | |
e2f34481 NJ |
6013 | } |
6014 | ||
9496e268 | 6015 | pr_err("Unimplemented Fileinfoclass :%d\n", req->FileInfoClass); |
e2f34481 NJ |
6016 | return -EOPNOTSUPP; |
6017 | } | |
6018 | ||
64b39f4a | 6019 | static int smb2_set_info_sec(struct ksmbd_file *fp, int addition_info, |
070fb21e | 6020 | char *buffer, int buf_len) |
e2f34481 NJ |
6021 | { |
6022 | struct smb_ntsd *pntsd = (struct smb_ntsd *)buffer; | |
6023 | ||
6024 | fp->saccess |= FILE_SHARE_DELETE_LE; | |
6025 | ||
ef24c962 | 6026 | return set_info_sec(fp->conn, fp->tcon, &fp->filp->f_path, pntsd, |
864fb5d3 | 6027 | buf_len, false, true); |
e2f34481 NJ |
6028 | } |
6029 | ||
6030 | /** | |
6031 | * smb2_set_info() - handler for smb2 set info command handler | |
6032 | * @work: smb work containing set info request buffer | |
6033 | * | |
6034 | * Return: 0 on success, otherwise error | |
6035 | */ | |
6036 | int smb2_set_info(struct ksmbd_work *work) | |
6037 | { | |
6038 | struct smb2_set_info_req *req; | |
cb451720 | 6039 | struct smb2_set_info_rsp *rsp; |
d592a915 | 6040 | struct ksmbd_file *fp = NULL; |
e2f34481 NJ |
6041 | int rc = 0; |
6042 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; | |
6043 | ||
6044 | ksmbd_debug(SMB, "Received set info request\n"); | |
6045 | ||
e2f34481 | 6046 | if (work->next_smb2_rcv_hdr_off) { |
8a893315 NJ |
6047 | req = ksmbd_req_buf_next(work); |
6048 | rsp = ksmbd_resp_buf_next(work); | |
2d004c6c | 6049 | if (!has_file_id(req->VolatileFileId)) { |
3867369e | 6050 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
070fb21e | 6051 | work->compound_fid); |
e2f34481 NJ |
6052 | id = work->compound_fid; |
6053 | pid = work->compound_pfid; | |
6054 | } | |
6055 | } else { | |
cb451720 NJ |
6056 | req = smb2_get_msg(work->request_buf); |
6057 | rsp = smb2_get_msg(work->response_buf); | |
e2f34481 NJ |
6058 | } |
6059 | ||
d592a915 NJ |
6060 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
6061 | ksmbd_debug(SMB, "User does not have write permission\n"); | |
6062 | pr_err("User does not have write permission\n"); | |
6063 | rc = -EACCES; | |
6064 | goto err_out; | |
6065 | } | |
6066 | ||
3867369e | 6067 | if (!has_file_id(id)) { |
2d004c6c PA |
6068 | id = req->VolatileFileId; |
6069 | pid = req->PersistentFileId; | |
e2f34481 NJ |
6070 | } |
6071 | ||
6072 | fp = ksmbd_lookup_fd_slow(work, id, pid); | |
6073 | if (!fp) { | |
6074 | ksmbd_debug(SMB, "Invalid id for close: %u\n", id); | |
6075 | rc = -ENOENT; | |
6076 | goto err_out; | |
6077 | } | |
6078 | ||
6079 | switch (req->InfoType) { | |
6080 | case SMB2_O_INFO_FILE: | |
6081 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_FILE\n"); | |
9496e268 | 6082 | rc = smb2_set_info_file(work, fp, req, work->tcon->share_conf); |
e2f34481 NJ |
6083 | break; |
6084 | case SMB2_O_INFO_SECURITY: | |
6085 | ksmbd_debug(SMB, "GOT SMB2_O_INFO_SECURITY\n"); | |
e70e392f NJ |
6086 | if (ksmbd_override_fsids(work)) { |
6087 | rc = -ENOMEM; | |
6088 | goto err_out; | |
6089 | } | |
e2f34481 | 6090 | rc = smb2_set_info_sec(fp, |
070fb21e NJ |
6091 | le32_to_cpu(req->AdditionalInformation), |
6092 | req->Buffer, | |
6093 | le32_to_cpu(req->BufferLength)); | |
e70e392f | 6094 | ksmbd_revert_fsids(work); |
e2f34481 NJ |
6095 | break; |
6096 | default: | |
6097 | rc = -EOPNOTSUPP; | |
6098 | } | |
6099 | ||
6100 | if (rc < 0) | |
6101 | goto err_out; | |
6102 | ||
6103 | rsp->StructureSize = cpu_to_le16(2); | |
e2b76ab8 NJ |
6104 | rc = ksmbd_iov_pin_rsp(work, (void *)rsp, |
6105 | sizeof(struct smb2_set_info_rsp)); | |
6106 | if (rc) | |
6107 | goto err_out; | |
e2f34481 NJ |
6108 | ksmbd_fd_put(work, fp); |
6109 | return 0; | |
6110 | ||
6111 | err_out: | |
265fd199 | 6112 | if (rc == -EACCES || rc == -EPERM || rc == -EXDEV) |
e2f34481 NJ |
6113 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
6114 | else if (rc == -EINVAL) | |
6115 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
6116 | else if (rc == -ESHARE) | |
6117 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; | |
6118 | else if (rc == -ENOENT) | |
6119 | rsp->hdr.Status = STATUS_OBJECT_NAME_INVALID; | |
6120 | else if (rc == -EBUSY || rc == -ENOTEMPTY) | |
6121 | rsp->hdr.Status = STATUS_DIRECTORY_NOT_EMPTY; | |
6122 | else if (rc == -EAGAIN) | |
6123 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; | |
ff1d5727 | 6124 | else if (rc == -EBADF || rc == -ESTALE) |
e2f34481 NJ |
6125 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
6126 | else if (rc == -EEXIST) | |
6127 | rsp->hdr.Status = STATUS_OBJECT_NAME_COLLISION; | |
6128 | else if (rsp->hdr.Status == 0 || rc == -EOPNOTSUPP) | |
6129 | rsp->hdr.Status = STATUS_INVALID_INFO_CLASS; | |
6130 | smb2_set_err_rsp(work); | |
6131 | ksmbd_fd_put(work, fp); | |
070fb21e | 6132 | ksmbd_debug(SMB, "error while processing smb2 query rc = %d\n", rc); |
e2f34481 NJ |
6133 | return rc; |
6134 | } | |
6135 | ||
6136 | /** | |
6137 | * smb2_read_pipe() - handler for smb2 read from IPC pipe | |
6138 | * @work: smb work containing read IPC pipe command buffer | |
6139 | * | |
6140 | * Return: 0 on success, otherwise error | |
6141 | */ | |
6142 | static noinline int smb2_read_pipe(struct ksmbd_work *work) | |
6143 | { | |
6144 | int nbytes = 0, err; | |
64b39f4a | 6145 | u64 id; |
e2f34481 | 6146 | struct ksmbd_rpc_command *rpc_resp; |
7b7d709e NJ |
6147 | struct smb2_read_req *req; |
6148 | struct smb2_read_rsp *rsp; | |
6149 | ||
6150 | WORK_BUFFERS(work, req, rsp); | |
e2f34481 | 6151 | |
2d004c6c | 6152 | id = req->VolatileFileId; |
e2f34481 | 6153 | |
e2f34481 NJ |
6154 | rpc_resp = ksmbd_rpc_read(work->sess, id); |
6155 | if (rpc_resp) { | |
e2b76ab8 NJ |
6156 | void *aux_payload_buf; |
6157 | ||
e2f34481 NJ |
6158 | if (rpc_resp->flags != KSMBD_RPC_OK) { |
6159 | err = -EINVAL; | |
6160 | goto out; | |
6161 | } | |
6162 | ||
e2b76ab8 | 6163 | aux_payload_buf = |
81a94b27 | 6164 | kvmalloc(rpc_resp->payload_sz, GFP_KERNEL); |
e2b76ab8 | 6165 | if (!aux_payload_buf) { |
e2f34481 NJ |
6166 | err = -ENOMEM; |
6167 | goto out; | |
6168 | } | |
6169 | ||
e2b76ab8 | 6170 | memcpy(aux_payload_buf, rpc_resp->payload, rpc_resp->payload_sz); |
e2f34481 NJ |
6171 | |
6172 | nbytes = rpc_resp->payload_sz; | |
e2b76ab8 NJ |
6173 | err = ksmbd_iov_pin_rsp_read(work, (void *)rsp, |
6174 | offsetof(struct smb2_read_rsp, Buffer), | |
6175 | aux_payload_buf, nbytes); | |
6176 | if (err) | |
6177 | goto out; | |
1903e6d0 | 6178 | kvfree(rpc_resp); |
e2b76ab8 NJ |
6179 | } else { |
6180 | err = ksmbd_iov_pin_rsp(work, (void *)rsp, | |
6181 | offsetof(struct smb2_read_rsp, Buffer)); | |
6182 | if (err) | |
6183 | goto out; | |
e2f34481 NJ |
6184 | } |
6185 | ||
6186 | rsp->StructureSize = cpu_to_le16(17); | |
6187 | rsp->DataOffset = 80; | |
6188 | rsp->Reserved = 0; | |
6189 | rsp->DataLength = cpu_to_le32(nbytes); | |
6190 | rsp->DataRemaining = 0; | |
699230f3 | 6191 | rsp->Flags = 0; |
e2f34481 NJ |
6192 | return 0; |
6193 | ||
6194 | out: | |
6195 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; | |
6196 | smb2_set_err_rsp(work); | |
79f6b11a | 6197 | kvfree(rpc_resp); |
e2f34481 NJ |
6198 | return err; |
6199 | } | |
6200 | ||
2fd5dcb1 HL |
6201 | static int smb2_set_remote_key_for_rdma(struct ksmbd_work *work, |
6202 | struct smb2_buffer_desc_v1 *desc, | |
6203 | __le32 Channel, | |
2fd5dcb1 | 6204 | __le16 ChannelInfoLength) |
e2f34481 | 6205 | { |
6d896d3b HL |
6206 | unsigned int i, ch_count; |
6207 | ||
64b39f4a | 6208 | if (work->conn->dialect == SMB30_PROT_ID && |
2fd5dcb1 | 6209 | Channel != SMB2_CHANNEL_RDMA_V1) |
e2f34481 NJ |
6210 | return -EINVAL; |
6211 | ||
6d896d3b HL |
6212 | ch_count = le16_to_cpu(ChannelInfoLength) / sizeof(*desc); |
6213 | if (ksmbd_debug_types & KSMBD_DEBUG_RDMA) { | |
6214 | for (i = 0; i < ch_count; i++) { | |
6215 | pr_info("RDMA r/w request %#x: token %#x, length %#x\n", | |
6216 | i, | |
6217 | le32_to_cpu(desc[i].token), | |
6218 | le32_to_cpu(desc[i].length)); | |
6219 | } | |
6220 | } | |
ee1b0558 | 6221 | if (!ch_count) |
e2f34481 NJ |
6222 | return -EINVAL; |
6223 | ||
6224 | work->need_invalidate_rkey = | |
2fd5dcb1 | 6225 | (Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE); |
1807abcf HL |
6226 | if (Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE) |
6227 | work->remote_key = le32_to_cpu(desc->token); | |
2fd5dcb1 HL |
6228 | return 0; |
6229 | } | |
6230 | ||
6231 | static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work, | |
6232 | struct smb2_read_req *req, void *data_buf, | |
6233 | size_t length) | |
6234 | { | |
2fd5dcb1 | 6235 | int err; |
e2f34481 | 6236 | |
64b39f4a | 6237 | err = ksmbd_conn_rdma_write(work->conn, data_buf, length, |
1807abcf HL |
6238 | (struct smb2_buffer_desc_v1 *) |
6239 | ((char *)req + le16_to_cpu(req->ReadChannelInfoOffset)), | |
6240 | le16_to_cpu(req->ReadChannelInfoLength)); | |
e2f34481 NJ |
6241 | if (err) |
6242 | return err; | |
6243 | ||
6244 | return length; | |
6245 | } | |
6246 | ||
6247 | /** | |
6248 | * smb2_read() - handler for smb2 read from file | |
6249 | * @work: smb work containing read command buffer | |
6250 | * | |
6251 | * Return: 0 on success, otherwise error | |
6252 | */ | |
6253 | int smb2_read(struct ksmbd_work *work) | |
6254 | { | |
6255 | struct ksmbd_conn *conn = work->conn; | |
6256 | struct smb2_read_req *req; | |
cb451720 | 6257 | struct smb2_read_rsp *rsp; |
2fd5dcb1 | 6258 | struct ksmbd_file *fp = NULL; |
e2f34481 NJ |
6259 | loff_t offset; |
6260 | size_t length, mincount; | |
6261 | ssize_t nbytes = 0, remain_bytes = 0; | |
6262 | int err = 0; | |
7a84399e NJ |
6263 | bool is_rdma_channel = false; |
6264 | unsigned int max_read_size = conn->vals->max_read_size; | |
e2b76ab8 NJ |
6265 | unsigned int id = KSMBD_NO_FID, pid = KSMBD_NO_FID; |
6266 | void *aux_payload_buf; | |
e2f34481 NJ |
6267 | |
6268 | if (test_share_config_flag(work->tcon->share_conf, | |
6269 | KSMBD_SHARE_FLAG_PIPE)) { | |
6270 | ksmbd_debug(SMB, "IPC pipe read request\n"); | |
6271 | return smb2_read_pipe(work); | |
6272 | } | |
6273 | ||
e2b76ab8 NJ |
6274 | if (work->next_smb2_rcv_hdr_off) { |
6275 | req = ksmbd_req_buf_next(work); | |
6276 | rsp = ksmbd_resp_buf_next(work); | |
6277 | if (!has_file_id(req->VolatileFileId)) { | |
6278 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", | |
6279 | work->compound_fid); | |
6280 | id = work->compound_fid; | |
6281 | pid = work->compound_pfid; | |
6282 | } | |
6283 | } else { | |
6284 | req = smb2_get_msg(work->request_buf); | |
6285 | rsp = smb2_get_msg(work->response_buf); | |
6286 | } | |
6287 | ||
6288 | if (!has_file_id(id)) { | |
6289 | id = req->VolatileFileId; | |
6290 | pid = req->PersistentFileId; | |
6291 | } | |
6292 | ||
2fd5dcb1 HL |
6293 | if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE || |
6294 | req->Channel == SMB2_CHANNEL_RDMA_V1) { | |
7a84399e NJ |
6295 | is_rdma_channel = true; |
6296 | max_read_size = get_smbd_max_read_write_size(); | |
6297 | } | |
6298 | ||
6299 | if (is_rdma_channel == true) { | |
6d896d3b HL |
6300 | unsigned int ch_offset = le16_to_cpu(req->ReadChannelInfoOffset); |
6301 | ||
6302 | if (ch_offset < offsetof(struct smb2_read_req, Buffer)) { | |
6303 | err = -EINVAL; | |
6304 | goto out; | |
6305 | } | |
2fd5dcb1 HL |
6306 | err = smb2_set_remote_key_for_rdma(work, |
6307 | (struct smb2_buffer_desc_v1 *) | |
6d896d3b | 6308 | ((char *)req + ch_offset), |
2fd5dcb1 | 6309 | req->Channel, |
2fd5dcb1 HL |
6310 | req->ReadChannelInfoLength); |
6311 | if (err) | |
6312 | goto out; | |
6313 | } | |
6314 | ||
e2b76ab8 | 6315 | fp = ksmbd_lookup_fd_slow(work, id, pid); |
e2f34481 | 6316 | if (!fp) { |
a4382db9 MM |
6317 | err = -ENOENT; |
6318 | goto out; | |
e2f34481 NJ |
6319 | } |
6320 | ||
6321 | if (!(fp->daccess & (FILE_READ_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { | |
bde1694a | 6322 | pr_err("Not permitted to read : 0x%x\n", fp->daccess); |
e2f34481 NJ |
6323 | err = -EACCES; |
6324 | goto out; | |
6325 | } | |
6326 | ||
6327 | offset = le64_to_cpu(req->Offset); | |
6328 | length = le32_to_cpu(req->Length); | |
6329 | mincount = le32_to_cpu(req->MinimumCount); | |
6330 | ||
7a84399e | 6331 | if (length > max_read_size) { |
e2f34481 | 6332 | ksmbd_debug(SMB, "limiting read size to max size(%u)\n", |
7a84399e | 6333 | max_read_size); |
e2f34481 NJ |
6334 | err = -EINVAL; |
6335 | goto out; | |
6336 | } | |
6337 | ||
369c1634 AV |
6338 | ksmbd_debug(SMB, "filename %pD, offset %lld, len %zu\n", |
6339 | fp->filp, offset, length); | |
e2f34481 | 6340 | |
e2b76ab8 NJ |
6341 | aux_payload_buf = kvzalloc(length, GFP_KERNEL); |
6342 | if (!aux_payload_buf) { | |
c1ea111f | 6343 | err = -ENOMEM; |
e2f34481 NJ |
6344 | goto out; |
6345 | } | |
6346 | ||
e2b76ab8 | 6347 | nbytes = ksmbd_vfs_read(work, fp, length, &offset, aux_payload_buf); |
e2f34481 NJ |
6348 | if (nbytes < 0) { |
6349 | err = nbytes; | |
6350 | goto out; | |
6351 | } | |
6352 | ||
6353 | if ((nbytes == 0 && length != 0) || nbytes < mincount) { | |
e2b76ab8 | 6354 | kvfree(aux_payload_buf); |
e2f34481 NJ |
6355 | rsp->hdr.Status = STATUS_END_OF_FILE; |
6356 | smb2_set_err_rsp(work); | |
6357 | ksmbd_fd_put(work, fp); | |
6358 | return 0; | |
6359 | } | |
6360 | ||
6361 | ksmbd_debug(SMB, "nbytes %zu, offset %lld mincount %zu\n", | |
070fb21e | 6362 | nbytes, offset, mincount); |
e2f34481 | 6363 | |
7a84399e | 6364 | if (is_rdma_channel == true) { |
e2f34481 NJ |
6365 | /* write data to the client using rdma channel */ |
6366 | remain_bytes = smb2_read_rdma_channel(work, req, | |
e2b76ab8 | 6367 | aux_payload_buf, |
070fb21e | 6368 | nbytes); |
e2b76ab8 | 6369 | kvfree(aux_payload_buf); |
59d8d24f | 6370 | aux_payload_buf = NULL; |
e2f34481 NJ |
6371 | nbytes = 0; |
6372 | if (remain_bytes < 0) { | |
6373 | err = (int)remain_bytes; | |
6374 | goto out; | |
6375 | } | |
6376 | } | |
6377 | ||
6378 | rsp->StructureSize = cpu_to_le16(17); | |
6379 | rsp->DataOffset = 80; | |
6380 | rsp->Reserved = 0; | |
6381 | rsp->DataLength = cpu_to_le32(nbytes); | |
6382 | rsp->DataRemaining = cpu_to_le32(remain_bytes); | |
699230f3 | 6383 | rsp->Flags = 0; |
e2b76ab8 NJ |
6384 | err = ksmbd_iov_pin_rsp_read(work, (void *)rsp, |
6385 | offsetof(struct smb2_read_rsp, Buffer), | |
6386 | aux_payload_buf, nbytes); | |
6387 | if (err) | |
6388 | goto out; | |
e2f34481 NJ |
6389 | ksmbd_fd_put(work, fp); |
6390 | return 0; | |
6391 | ||
6392 | out: | |
6393 | if (err) { | |
6394 | if (err == -EISDIR) | |
6395 | rsp->hdr.Status = STATUS_INVALID_DEVICE_REQUEST; | |
6396 | else if (err == -EAGAIN) | |
6397 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; | |
6398 | else if (err == -ENOENT) | |
6399 | rsp->hdr.Status = STATUS_FILE_CLOSED; | |
6400 | else if (err == -EACCES) | |
6401 | rsp->hdr.Status = STATUS_ACCESS_DENIED; | |
6402 | else if (err == -ESHARE) | |
6403 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; | |
6404 | else if (err == -EINVAL) | |
6405 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
6406 | else | |
6407 | rsp->hdr.Status = STATUS_INVALID_HANDLE; | |
6408 | ||
6409 | smb2_set_err_rsp(work); | |
6410 | } | |
6411 | ksmbd_fd_put(work, fp); | |
6412 | return err; | |
6413 | } | |
6414 | ||
6415 | /** | |
6416 | * smb2_write_pipe() - handler for smb2 write on IPC pipe | |
6417 | * @work: smb work containing write IPC pipe command buffer | |
6418 | * | |
6419 | * Return: 0 on success, otherwise error | |
6420 | */ | |
6421 | static noinline int smb2_write_pipe(struct ksmbd_work *work) | |
6422 | { | |
7b7d709e NJ |
6423 | struct smb2_write_req *req; |
6424 | struct smb2_write_rsp *rsp; | |
e2f34481 | 6425 | struct ksmbd_rpc_command *rpc_resp; |
64b39f4a | 6426 | u64 id = 0; |
e2f34481 NJ |
6427 | int err = 0, ret = 0; |
6428 | char *data_buf; | |
6429 | size_t length; | |
6430 | ||
7b7d709e NJ |
6431 | WORK_BUFFERS(work, req, rsp); |
6432 | ||
e2f34481 | 6433 | length = le32_to_cpu(req->Length); |
2d004c6c | 6434 | id = req->VolatileFileId; |
e2f34481 | 6435 | |
158a66b2 MM |
6436 | if ((u64)le16_to_cpu(req->DataOffset) + length > |
6437 | get_rfc1002_len(work->request_buf)) { | |
6438 | pr_err("invalid write data offset %u, smb_len %u\n", | |
6439 | le16_to_cpu(req->DataOffset), | |
6440 | get_rfc1002_len(work->request_buf)); | |
6441 | err = -EINVAL; | |
6442 | goto out; | |
e2f34481 NJ |
6443 | } |
6444 | ||
158a66b2 MM |
6445 | data_buf = (char *)(((char *)&req->hdr.ProtocolId) + |
6446 | le16_to_cpu(req->DataOffset)); | |
6447 | ||
e2f34481 NJ |
6448 | rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length); |
6449 | if (rpc_resp) { | |
6450 | if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { | |
6451 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; | |
79f6b11a | 6452 | kvfree(rpc_resp); |
e2f34481 NJ |
6453 | smb2_set_err_rsp(work); |
6454 | return -EOPNOTSUPP; | |
6455 | } | |
6456 | if (rpc_resp->flags != KSMBD_RPC_OK) { | |
6457 | rsp->hdr.Status = STATUS_INVALID_HANDLE; | |
6458 | smb2_set_err_rsp(work); | |
79f6b11a | 6459 | kvfree(rpc_resp); |
e2f34481 NJ |
6460 | return ret; |
6461 | } | |
79f6b11a | 6462 | kvfree(rpc_resp); |
e2f34481 NJ |
6463 | } |
6464 | ||
6465 | rsp->StructureSize = cpu_to_le16(17); | |
6466 | rsp->DataOffset = 0; | |
6467 | rsp->Reserved = 0; | |
6468 | rsp->DataLength = cpu_to_le32(length); | |
6469 | rsp->DataRemaining = 0; | |
6470 | rsp->Reserved2 = 0; | |
e2b76ab8 NJ |
6471 | err = ksmbd_iov_pin_rsp(work, (void *)rsp, |
6472 | offsetof(struct smb2_write_rsp, Buffer)); | |
e2f34481 NJ |
6473 | out: |
6474 | if (err) { | |
6475 | rsp->hdr.Status = STATUS_INVALID_HANDLE; | |
6476 | smb2_set_err_rsp(work); | |
6477 | } | |
6478 | ||
6479 | return err; | |
6480 | } | |
6481 | ||
6482 | static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work, | |
070fb21e NJ |
6483 | struct smb2_write_req *req, |
6484 | struct ksmbd_file *fp, | |
6485 | loff_t offset, size_t length, bool sync) | |
e2f34481 | 6486 | { |
e2f34481 NJ |
6487 | char *data_buf; |
6488 | int ret; | |
6489 | ssize_t nbytes; | |
6490 | ||
81a94b27 | 6491 | data_buf = kvzalloc(length, GFP_KERNEL); |
e2f34481 NJ |
6492 | if (!data_buf) |
6493 | return -ENOMEM; | |
6494 | ||
6495 | ret = ksmbd_conn_rdma_read(work->conn, data_buf, length, | |
1807abcf HL |
6496 | (struct smb2_buffer_desc_v1 *) |
6497 | ((char *)req + le16_to_cpu(req->WriteChannelInfoOffset)), | |
6498 | le16_to_cpu(req->WriteChannelInfoLength)); | |
e2f34481 | 6499 | if (ret < 0) { |
79f6b11a | 6500 | kvfree(data_buf); |
e2f34481 NJ |
6501 | return ret; |
6502 | } | |
6503 | ||
64b39f4a | 6504 | ret = ksmbd_vfs_write(work, fp, data_buf, length, &offset, sync, &nbytes); |
79f6b11a | 6505 | kvfree(data_buf); |
e2f34481 NJ |
6506 | if (ret < 0) |
6507 | return ret; | |
6508 | ||
6509 | return nbytes; | |
6510 | } | |
6511 | ||
6512 | /** | |
6513 | * smb2_write() - handler for smb2 write from file | |
6514 | * @work: smb work containing write command buffer | |
6515 | * | |
6516 | * Return: 0 on success, otherwise error | |
6517 | */ | |
6518 | int smb2_write(struct ksmbd_work *work) | |
6519 | { | |
6520 | struct smb2_write_req *req; | |
cb451720 | 6521 | struct smb2_write_rsp *rsp; |
bcd62a36 | 6522 | struct ksmbd_file *fp = NULL; |
e2f34481 NJ |
6523 | loff_t offset; |
6524 | size_t length; | |
6525 | ssize_t nbytes; | |
6526 | char *data_buf; | |
7a84399e | 6527 | bool writethrough = false, is_rdma_channel = false; |
e2f34481 | 6528 | int err = 0; |
7a84399e | 6529 | unsigned int max_write_size = work->conn->vals->max_write_size; |
e2f34481 | 6530 | |
e2f34481 NJ |
6531 | WORK_BUFFERS(work, req, rsp); |
6532 | ||
64b39f4a | 6533 | if (test_share_config_flag(work->tcon->share_conf, KSMBD_SHARE_FLAG_PIPE)) { |
e2f34481 NJ |
6534 | ksmbd_debug(SMB, "IPC pipe write request\n"); |
6535 | return smb2_write_pipe(work); | |
6536 | } | |
6537 | ||
7a84399e NJ |
6538 | offset = le64_to_cpu(req->Offset); |
6539 | length = le32_to_cpu(req->Length); | |
6540 | ||
2fd5dcb1 HL |
6541 | if (req->Channel == SMB2_CHANNEL_RDMA_V1 || |
6542 | req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE) { | |
7a84399e NJ |
6543 | is_rdma_channel = true; |
6544 | max_write_size = get_smbd_max_read_write_size(); | |
6545 | length = le32_to_cpu(req->RemainingBytes); | |
6546 | } | |
6547 | ||
6548 | if (is_rdma_channel == true) { | |
6d896d3b HL |
6549 | unsigned int ch_offset = le16_to_cpu(req->WriteChannelInfoOffset); |
6550 | ||
6551 | if (req->Length != 0 || req->DataOffset != 0 || | |
6552 | ch_offset < offsetof(struct smb2_write_req, Buffer)) { | |
6553 | err = -EINVAL; | |
6554 | goto out; | |
6555 | } | |
2fd5dcb1 HL |
6556 | err = smb2_set_remote_key_for_rdma(work, |
6557 | (struct smb2_buffer_desc_v1 *) | |
6d896d3b | 6558 | ((char *)req + ch_offset), |
2fd5dcb1 | 6559 | req->Channel, |
2fd5dcb1 HL |
6560 | req->WriteChannelInfoLength); |
6561 | if (err) | |
6562 | goto out; | |
6563 | } | |
6564 | ||
e2f34481 NJ |
6565 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
6566 | ksmbd_debug(SMB, "User does not have write permission\n"); | |
6567 | err = -EACCES; | |
6568 | goto out; | |
6569 | } | |
6570 | ||
2d004c6c | 6571 | fp = ksmbd_lookup_fd_slow(work, req->VolatileFileId, req->PersistentFileId); |
e2f34481 | 6572 | if (!fp) { |
a4382db9 MM |
6573 | err = -ENOENT; |
6574 | goto out; | |
e2f34481 NJ |
6575 | } |
6576 | ||
6577 | if (!(fp->daccess & (FILE_WRITE_DATA_LE | FILE_READ_ATTRIBUTES_LE))) { | |
bde1694a | 6578 | pr_err("Not permitted to write : 0x%x\n", fp->daccess); |
e2f34481 NJ |
6579 | err = -EACCES; |
6580 | goto out; | |
6581 | } | |
6582 | ||
7a84399e | 6583 | if (length > max_write_size) { |
e2f34481 | 6584 | ksmbd_debug(SMB, "limiting write size to max size(%u)\n", |
7a84399e | 6585 | max_write_size); |
e2f34481 NJ |
6586 | err = -EINVAL; |
6587 | goto out; | |
6588 | } | |
6589 | ||
745bbc09 | 6590 | ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags)); |
e2f34481 NJ |
6591 | if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH) |
6592 | writethrough = true; | |
6593 | ||
7a84399e | 6594 | if (is_rdma_channel == false) { |
ac60778b HL |
6595 | if (le16_to_cpu(req->DataOffset) < |
6596 | offsetof(struct smb2_write_req, Buffer)) { | |
158a66b2 MM |
6597 | err = -EINVAL; |
6598 | goto out; | |
e2f34481 | 6599 | } |
ac60778b | 6600 | |
158a66b2 MM |
6601 | data_buf = (char *)(((char *)&req->hdr.ProtocolId) + |
6602 | le16_to_cpu(req->DataOffset)); | |
e2f34481 | 6603 | |
369c1634 AV |
6604 | ksmbd_debug(SMB, "filename %pD, offset %lld, len %zu\n", |
6605 | fp->filp, offset, length); | |
e2f34481 NJ |
6606 | err = ksmbd_vfs_write(work, fp, data_buf, length, &offset, |
6607 | writethrough, &nbytes); | |
6608 | if (err < 0) | |
6609 | goto out; | |
6610 | } else { | |
6611 | /* read data from the client using rdma channel, and | |
6612 | * write the data. | |
6613 | */ | |
7a84399e | 6614 | nbytes = smb2_write_rdma_channel(work, req, fp, offset, length, |
070fb21e | 6615 | writethrough); |
e2f34481 NJ |
6616 | if (nbytes < 0) { |
6617 | err = (int)nbytes; | |
6618 | goto out; | |
6619 | } | |
6620 | } | |
6621 | ||
6622 | rsp->StructureSize = cpu_to_le16(17); | |
6623 | rsp->DataOffset = 0; | |
6624 | rsp->Reserved = 0; | |
6625 | rsp->DataLength = cpu_to_le32(nbytes); | |
6626 | rsp->DataRemaining = 0; | |
6627 | rsp->Reserved2 = 0; | |
e2b76ab8 NJ |
6628 | err = ksmbd_iov_pin_rsp(work, rsp, offsetof(struct smb2_write_rsp, Buffer)); |
6629 | if (err) | |
6630 | goto out; | |
e2f34481 NJ |
6631 | ksmbd_fd_put(work, fp); |
6632 | return 0; | |
6633 | ||
6634 | out: | |
6635 | if (err == -EAGAIN) | |
6636 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; | |
6637 | else if (err == -ENOSPC || err == -EFBIG) | |
6638 | rsp->hdr.Status = STATUS_DISK_FULL; | |
6639 | else if (err == -ENOENT) | |
6640 | rsp->hdr.Status = STATUS_FILE_CLOSED; | |
6641 | else if (err == -EACCES) | |
6642 | rsp->hdr.Status = STATUS_ACCESS_DENIED; | |
6643 | else if (err == -ESHARE) | |
6644 | rsp->hdr.Status = STATUS_SHARING_VIOLATION; | |
6645 | else if (err == -EINVAL) | |
6646 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
6647 | else | |
6648 | rsp->hdr.Status = STATUS_INVALID_HANDLE; | |
6649 | ||
6650 | smb2_set_err_rsp(work); | |
6651 | ksmbd_fd_put(work, fp); | |
6652 | return err; | |
6653 | } | |
6654 | ||
6655 | /** | |
6656 | * smb2_flush() - handler for smb2 flush file - fsync | |
6657 | * @work: smb work containing flush command buffer | |
6658 | * | |
6659 | * Return: 0 on success, otherwise error | |
6660 | */ | |
6661 | int smb2_flush(struct ksmbd_work *work) | |
6662 | { | |
6663 | struct smb2_flush_req *req; | |
cb451720 | 6664 | struct smb2_flush_rsp *rsp; |
e2f34481 NJ |
6665 | int err; |
6666 | ||
e2f34481 NJ |
6667 | WORK_BUFFERS(work, req, rsp); |
6668 | ||
2d004c6c | 6669 | ksmbd_debug(SMB, "SMB2_FLUSH called for fid %llu\n", req->VolatileFileId); |
e2f34481 | 6670 | |
2d004c6c | 6671 | err = ksmbd_vfs_fsync(work, req->VolatileFileId, req->PersistentFileId); |
e2f34481 NJ |
6672 | if (err) |
6673 | goto out; | |
6674 | ||
6675 | rsp->StructureSize = cpu_to_le16(4); | |
6676 | rsp->Reserved = 0; | |
e2b76ab8 | 6677 | return ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_flush_rsp)); |
e2f34481 NJ |
6678 | |
6679 | out: | |
e2b76ab8 NJ |
6680 | rsp->hdr.Status = STATUS_INVALID_HANDLE; |
6681 | smb2_set_err_rsp(work); | |
e2f34481 NJ |
6682 | return err; |
6683 | } | |
6684 | ||
6685 | /** | |
6686 | * smb2_cancel() - handler for smb2 cancel command | |
6687 | * @work: smb work containing cancel command buffer | |
6688 | * | |
6689 | * Return: 0 on success, otherwise error | |
6690 | */ | |
6691 | int smb2_cancel(struct ksmbd_work *work) | |
6692 | { | |
6693 | struct ksmbd_conn *conn = work->conn; | |
cb451720 | 6694 | struct smb2_hdr *hdr = smb2_get_msg(work->request_buf); |
e2f34481 | 6695 | struct smb2_hdr *chdr; |
d3ca9f7a | 6696 | struct ksmbd_work *iter; |
e2f34481 NJ |
6697 | struct list_head *command_list; |
6698 | ||
7b7d709e NJ |
6699 | if (work->next_smb2_rcv_hdr_off) |
6700 | hdr = ksmbd_resp_buf_next(work); | |
6701 | ||
e2f34481 | 6702 | ksmbd_debug(SMB, "smb2 cancel called on mid %llu, async flags 0x%x\n", |
070fb21e | 6703 | hdr->MessageId, hdr->Flags); |
e2f34481 NJ |
6704 | |
6705 | if (hdr->Flags & SMB2_FLAGS_ASYNC_COMMAND) { | |
6706 | command_list = &conn->async_requests; | |
6707 | ||
6708 | spin_lock(&conn->request_lock); | |
edf5f054 | 6709 | list_for_each_entry(iter, command_list, |
6f3d5eee | 6710 | async_request_entry) { |
edf5f054 | 6711 | chdr = smb2_get_msg(iter->request_buf); |
e2f34481 | 6712 | |
edf5f054 | 6713 | if (iter->async_id != |
64b39f4a | 6714 | le64_to_cpu(hdr->Id.AsyncId)) |
e2f34481 NJ |
6715 | continue; |
6716 | ||
6717 | ksmbd_debug(SMB, | |
070fb21e NJ |
6718 | "smb2 with AsyncId %llu cancelled command = 0x%x\n", |
6719 | le64_to_cpu(hdr->Id.AsyncId), | |
6720 | le16_to_cpu(chdr->Command)); | |
d3ca9f7a HH |
6721 | iter->state = KSMBD_WORK_CANCELLED; |
6722 | if (iter->cancel_fn) | |
6723 | iter->cancel_fn(iter->cancel_argv); | |
e2f34481 NJ |
6724 | break; |
6725 | } | |
6726 | spin_unlock(&conn->request_lock); | |
6727 | } else { | |
6728 | command_list = &conn->requests; | |
6729 | ||
6730 | spin_lock(&conn->request_lock); | |
edf5f054 JK |
6731 | list_for_each_entry(iter, command_list, request_entry) { |
6732 | chdr = smb2_get_msg(iter->request_buf); | |
e2f34481 NJ |
6733 | |
6734 | if (chdr->MessageId != hdr->MessageId || | |
edf5f054 | 6735 | iter == work) |
e2f34481 NJ |
6736 | continue; |
6737 | ||
6738 | ksmbd_debug(SMB, | |
070fb21e NJ |
6739 | "smb2 with mid %llu cancelled command = 0x%x\n", |
6740 | le64_to_cpu(hdr->MessageId), | |
6741 | le16_to_cpu(chdr->Command)); | |
d3ca9f7a | 6742 | iter->state = KSMBD_WORK_CANCELLED; |
e2f34481 NJ |
6743 | break; |
6744 | } | |
6745 | spin_unlock(&conn->request_lock); | |
6746 | } | |
6747 | ||
e2f34481 NJ |
6748 | /* For SMB2_CANCEL command itself send no response*/ |
6749 | work->send_no_response = 1; | |
6750 | return 0; | |
6751 | } | |
6752 | ||
6753 | struct file_lock *smb_flock_init(struct file *f) | |
6754 | { | |
6755 | struct file_lock *fl; | |
6756 | ||
6757 | fl = locks_alloc_lock(); | |
6758 | if (!fl) | |
6759 | goto out; | |
6760 | ||
6761 | locks_init_lock(fl); | |
6762 | ||
16f9ce81 JL |
6763 | fl->c.flc_owner = f; |
6764 | fl->c.flc_pid = current->tgid; | |
6765 | fl->c.flc_file = f; | |
6766 | fl->c.flc_flags = FL_POSIX; | |
e2f34481 NJ |
6767 | fl->fl_ops = NULL; |
6768 | fl->fl_lmops = NULL; | |
6769 | ||
6770 | out: | |
6771 | return fl; | |
6772 | } | |
6773 | ||
6774 | static int smb2_set_flock_flags(struct file_lock *flock, int flags) | |
6775 | { | |
6776 | int cmd = -EINVAL; | |
6777 | ||
6778 | /* Checking for wrong flag combination during lock request*/ | |
6779 | switch (flags) { | |
6780 | case SMB2_LOCKFLAG_SHARED: | |
6781 | ksmbd_debug(SMB, "received shared request\n"); | |
6782 | cmd = F_SETLKW; | |
16f9ce81 JL |
6783 | flock->c.flc_type = F_RDLCK; |
6784 | flock->c.flc_flags |= FL_SLEEP; | |
e2f34481 NJ |
6785 | break; |
6786 | case SMB2_LOCKFLAG_EXCLUSIVE: | |
6787 | ksmbd_debug(SMB, "received exclusive request\n"); | |
6788 | cmd = F_SETLKW; | |
16f9ce81 JL |
6789 | flock->c.flc_type = F_WRLCK; |
6790 | flock->c.flc_flags |= FL_SLEEP; | |
e2f34481 | 6791 | break; |
64b39f4a | 6792 | case SMB2_LOCKFLAG_SHARED | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: |
e2f34481 | 6793 | ksmbd_debug(SMB, |
070fb21e | 6794 | "received shared & fail immediately request\n"); |
e2f34481 | 6795 | cmd = F_SETLK; |
16f9ce81 | 6796 | flock->c.flc_type = F_RDLCK; |
e2f34481 | 6797 | break; |
64b39f4a | 6798 | case SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_FAIL_IMMEDIATELY: |
e2f34481 | 6799 | ksmbd_debug(SMB, |
070fb21e | 6800 | "received exclusive & fail immediately request\n"); |
e2f34481 | 6801 | cmd = F_SETLK; |
16f9ce81 | 6802 | flock->c.flc_type = F_WRLCK; |
e2f34481 NJ |
6803 | break; |
6804 | case SMB2_LOCKFLAG_UNLOCK: | |
6805 | ksmbd_debug(SMB, "received unlock request\n"); | |
16f9ce81 | 6806 | flock->c.flc_type = F_UNLCK; |
7ecbe926 | 6807 | cmd = F_SETLK; |
e2f34481 NJ |
6808 | break; |
6809 | } | |
6810 | ||
6811 | return cmd; | |
6812 | } | |
6813 | ||
6814 | static struct ksmbd_lock *smb2_lock_init(struct file_lock *flock, | |
070fb21e NJ |
6815 | unsigned int cmd, int flags, |
6816 | struct list_head *lock_list) | |
e2f34481 NJ |
6817 | { |
6818 | struct ksmbd_lock *lock; | |
6819 | ||
6820 | lock = kzalloc(sizeof(struct ksmbd_lock), GFP_KERNEL); | |
6821 | if (!lock) | |
6822 | return NULL; | |
6823 | ||
6824 | lock->cmd = cmd; | |
6825 | lock->fl = flock; | |
6826 | lock->start = flock->fl_start; | |
6827 | lock->end = flock->fl_end; | |
6828 | lock->flags = flags; | |
6829 | if (lock->start == lock->end) | |
6830 | lock->zero_len = 1; | |
d63528eb HL |
6831 | INIT_LIST_HEAD(&lock->clist); |
6832 | INIT_LIST_HEAD(&lock->flist); | |
e2f34481 | 6833 | INIT_LIST_HEAD(&lock->llist); |
e2f34481 NJ |
6834 | list_add_tail(&lock->llist, lock_list); |
6835 | ||
6836 | return lock; | |
6837 | } | |
6838 | ||
6839 | static void smb2_remove_blocked_lock(void **argv) | |
6840 | { | |
6841 | struct file_lock *flock = (struct file_lock *)argv[0]; | |
6842 | ||
6843 | ksmbd_vfs_posix_lock_unblock(flock); | |
6a277077 | 6844 | locks_wake_up(flock); |
e2f34481 NJ |
6845 | } |
6846 | ||
6847 | static inline bool lock_defer_pending(struct file_lock *fl) | |
6848 | { | |
6849 | /* check pending lock waiters */ | |
16f9ce81 | 6850 | return waitqueue_active(&fl->c.flc_wait); |
e2f34481 NJ |
6851 | } |
6852 | ||
6853 | /** | |
6854 | * smb2_lock() - handler for smb2 file lock command | |
6855 | * @work: smb work containing lock command buffer | |
6856 | * | |
6857 | * Return: 0 on success, otherwise error | |
6858 | */ | |
6859 | int smb2_lock(struct ksmbd_work *work) | |
6860 | { | |
7b7d709e NJ |
6861 | struct smb2_lock_req *req; |
6862 | struct smb2_lock_rsp *rsp; | |
e2f34481 NJ |
6863 | struct smb2_lock_element *lock_ele; |
6864 | struct ksmbd_file *fp = NULL; | |
6865 | struct file_lock *flock = NULL; | |
6866 | struct file *filp = NULL; | |
6867 | int lock_count; | |
6868 | int flags = 0; | |
6869 | int cmd = 0; | |
6c99dfc4 | 6870 | int err = -EIO, i, rc = 0; |
50bf80a5 | 6871 | u64 lock_start, lock_length; |
d63528eb HL |
6872 | struct ksmbd_lock *smb_lock = NULL, *cmp_lock, *tmp, *tmp2; |
6873 | struct ksmbd_conn *conn; | |
e2f34481 NJ |
6874 | int nolock = 0; |
6875 | LIST_HEAD(lock_list); | |
6876 | LIST_HEAD(rollback_list); | |
6877 | int prior_lock = 0; | |
6878 | ||
7b7d709e NJ |
6879 | WORK_BUFFERS(work, req, rsp); |
6880 | ||
e2f34481 | 6881 | ksmbd_debug(SMB, "Received lock request\n"); |
2d004c6c | 6882 | fp = ksmbd_lookup_fd_slow(work, req->VolatileFileId, req->PersistentFileId); |
e2f34481 | 6883 | if (!fp) { |
2d004c6c | 6884 | ksmbd_debug(SMB, "Invalid file id for lock : %llu\n", req->VolatileFileId); |
6c99dfc4 | 6885 | err = -ENOENT; |
e2f34481 NJ |
6886 | goto out2; |
6887 | } | |
6888 | ||
6889 | filp = fp->filp; | |
6890 | lock_count = le16_to_cpu(req->LockCount); | |
6891 | lock_ele = req->locks; | |
6892 | ||
6893 | ksmbd_debug(SMB, "lock count is %d\n", lock_count); | |
070fb21e | 6894 | if (!lock_count) { |
6c99dfc4 | 6895 | err = -EINVAL; |
e2f34481 NJ |
6896 | goto out2; |
6897 | } | |
6898 | ||
6899 | for (i = 0; i < lock_count; i++) { | |
6900 | flags = le32_to_cpu(lock_ele[i].Flags); | |
6901 | ||
6902 | flock = smb_flock_init(filp); | |
6c99dfc4 | 6903 | if (!flock) |
e2f34481 | 6904 | goto out; |
e2f34481 NJ |
6905 | |
6906 | cmd = smb2_set_flock_flags(flock, flags); | |
6907 | ||
50bf80a5 NJ |
6908 | lock_start = le64_to_cpu(lock_ele[i].Offset); |
6909 | lock_length = le64_to_cpu(lock_ele[i].Length); | |
6910 | if (lock_start > U64_MAX - lock_length) { | |
bde1694a | 6911 | pr_err("Invalid lock range requested\n"); |
e2f34481 | 6912 | rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; |
01f6c61b | 6913 | locks_free_lock(flock); |
e2f34481 NJ |
6914 | goto out; |
6915 | } | |
6916 | ||
50bf80a5 NJ |
6917 | if (lock_start > OFFSET_MAX) |
6918 | flock->fl_start = OFFSET_MAX; | |
6919 | else | |
6920 | flock->fl_start = lock_start; | |
6921 | ||
e2f34481 | 6922 | lock_length = le64_to_cpu(lock_ele[i].Length); |
50bf80a5 NJ |
6923 | if (lock_length > OFFSET_MAX - flock->fl_start) |
6924 | lock_length = OFFSET_MAX - flock->fl_start; | |
e2f34481 NJ |
6925 | |
6926 | flock->fl_end = flock->fl_start + lock_length; | |
6927 | ||
6928 | if (flock->fl_end < flock->fl_start) { | |
6929 | ksmbd_debug(SMB, | |
070fb21e NJ |
6930 | "the end offset(%llx) is smaller than the start offset(%llx)\n", |
6931 | flock->fl_end, flock->fl_start); | |
e2f34481 | 6932 | rsp->hdr.Status = STATUS_INVALID_LOCK_RANGE; |
01f6c61b | 6933 | locks_free_lock(flock); |
e2f34481 NJ |
6934 | goto out; |
6935 | } | |
6936 | ||
6937 | /* Check conflict locks in one request */ | |
6938 | list_for_each_entry(cmp_lock, &lock_list, llist) { | |
6939 | if (cmp_lock->fl->fl_start <= flock->fl_start && | |
64b39f4a | 6940 | cmp_lock->fl->fl_end >= flock->fl_end) { |
16f9ce81 JL |
6941 | if (cmp_lock->fl->c.flc_type != F_UNLCK && |
6942 | flock->c.flc_type != F_UNLCK) { | |
bde1694a | 6943 | pr_err("conflict two locks in one request\n"); |
6c99dfc4 | 6944 | err = -EINVAL; |
01f6c61b | 6945 | locks_free_lock(flock); |
e2f34481 NJ |
6946 | goto out; |
6947 | } | |
6948 | } | |
6949 | } | |
6950 | ||
6951 | smb_lock = smb2_lock_init(flock, cmd, flags, &lock_list); | |
6952 | if (!smb_lock) { | |
6c99dfc4 | 6953 | err = -EINVAL; |
01f6c61b | 6954 | locks_free_lock(flock); |
e2f34481 NJ |
6955 | goto out; |
6956 | } | |
6957 | } | |
6958 | ||
6959 | list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { | |
6960 | if (smb_lock->cmd < 0) { | |
6c99dfc4 | 6961 | err = -EINVAL; |
e2f34481 NJ |
6962 | goto out; |
6963 | } | |
6964 | ||
6965 | if (!(smb_lock->flags & SMB2_LOCKFLAG_MASK)) { | |
6c99dfc4 | 6966 | err = -EINVAL; |
e2f34481 NJ |
6967 | goto out; |
6968 | } | |
6969 | ||
64b39f4a NJ |
6970 | if ((prior_lock & (SMB2_LOCKFLAG_EXCLUSIVE | SMB2_LOCKFLAG_SHARED) && |
6971 | smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) || | |
6972 | (prior_lock == SMB2_LOCKFLAG_UNLOCK && | |
6973 | !(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK))) { | |
6c99dfc4 | 6974 | err = -EINVAL; |
e2f34481 NJ |
6975 | goto out; |
6976 | } | |
6977 | ||
6978 | prior_lock = smb_lock->flags; | |
6979 | ||
6980 | if (!(smb_lock->flags & SMB2_LOCKFLAG_UNLOCK) && | |
64b39f4a | 6981 | !(smb_lock->flags & SMB2_LOCKFLAG_FAIL_IMMEDIATELY)) |
d63528eb | 6982 | goto no_check_cl; |
e2f34481 NJ |
6983 | |
6984 | nolock = 1; | |
d63528eb | 6985 | /* check locks in connection list */ |
abcc506a | 6986 | down_read(&conn_list_lock); |
d63528eb HL |
6987 | list_for_each_entry(conn, &conn_list, conns_list) { |
6988 | spin_lock(&conn->llist_lock); | |
6989 | list_for_each_entry_safe(cmp_lock, tmp2, &conn->lock_list, clist) { | |
16f9ce81 JL |
6990 | if (file_inode(cmp_lock->fl->c.flc_file) != |
6991 | file_inode(smb_lock->fl->c.flc_file)) | |
d63528eb | 6992 | continue; |
e2f34481 | 6993 | |
6a277077 | 6994 | if (lock_is_unlock(smb_lock->fl)) { |
16f9ce81 | 6995 | if (cmp_lock->fl->c.flc_file == smb_lock->fl->c.flc_file && |
d63528eb HL |
6996 | cmp_lock->start == smb_lock->start && |
6997 | cmp_lock->end == smb_lock->end && | |
6998 | !lock_defer_pending(cmp_lock->fl)) { | |
6999 | nolock = 0; | |
7000 | list_del(&cmp_lock->flist); | |
7001 | list_del(&cmp_lock->clist); | |
7002 | spin_unlock(&conn->llist_lock); | |
abcc506a | 7003 | up_read(&conn_list_lock); |
d63528eb HL |
7004 | |
7005 | locks_free_lock(cmp_lock->fl); | |
7006 | kfree(cmp_lock); | |
7007 | goto out_check_cl; | |
7008 | } | |
7009 | continue; | |
e2f34481 | 7010 | } |
e2f34481 | 7011 | |
16f9ce81 | 7012 | if (cmp_lock->fl->c.flc_file == smb_lock->fl->c.flc_file) { |
d63528eb HL |
7013 | if (smb_lock->flags & SMB2_LOCKFLAG_SHARED) |
7014 | continue; | |
7015 | } else { | |
7016 | if (cmp_lock->flags & SMB2_LOCKFLAG_SHARED) | |
7017 | continue; | |
7018 | } | |
e2f34481 | 7019 | |
d63528eb HL |
7020 | /* check zero byte lock range */ |
7021 | if (cmp_lock->zero_len && !smb_lock->zero_len && | |
7022 | cmp_lock->start > smb_lock->start && | |
7023 | cmp_lock->start < smb_lock->end) { | |
7024 | spin_unlock(&conn->llist_lock); | |
abcc506a | 7025 | up_read(&conn_list_lock); |
d63528eb | 7026 | pr_err("previous lock conflict with zero byte lock range\n"); |
6c99dfc4 | 7027 | goto out; |
d63528eb | 7028 | } |
e2f34481 | 7029 | |
d63528eb HL |
7030 | if (smb_lock->zero_len && !cmp_lock->zero_len && |
7031 | smb_lock->start > cmp_lock->start && | |
7032 | smb_lock->start < cmp_lock->end) { | |
7033 | spin_unlock(&conn->llist_lock); | |
abcc506a | 7034 | up_read(&conn_list_lock); |
d63528eb | 7035 | pr_err("current lock conflict with zero byte lock range\n"); |
6c99dfc4 | 7036 | goto out; |
d63528eb | 7037 | } |
e2f34481 | 7038 | |
d63528eb HL |
7039 | if (((cmp_lock->start <= smb_lock->start && |
7040 | cmp_lock->end > smb_lock->start) || | |
7041 | (cmp_lock->start < smb_lock->end && | |
7042 | cmp_lock->end >= smb_lock->end)) && | |
7043 | !cmp_lock->zero_len && !smb_lock->zero_len) { | |
7044 | spin_unlock(&conn->llist_lock); | |
abcc506a | 7045 | up_read(&conn_list_lock); |
d63528eb | 7046 | pr_err("Not allow lock operation on exclusive lock range\n"); |
d63528eb HL |
7047 | goto out; |
7048 | } | |
e2f34481 | 7049 | } |
d63528eb | 7050 | spin_unlock(&conn->llist_lock); |
e2f34481 | 7051 | } |
abcc506a | 7052 | up_read(&conn_list_lock); |
d63528eb | 7053 | out_check_cl: |
6a277077 | 7054 | if (lock_is_unlock(smb_lock->fl) && nolock) { |
bde1694a | 7055 | pr_err("Try to unlock nolocked range\n"); |
e2f34481 NJ |
7056 | rsp->hdr.Status = STATUS_RANGE_NOT_LOCKED; |
7057 | goto out; | |
7058 | } | |
7059 | ||
d63528eb | 7060 | no_check_cl: |
e2f34481 NJ |
7061 | if (smb_lock->zero_len) { |
7062 | err = 0; | |
7063 | goto skip; | |
7064 | } | |
7065 | ||
7066 | flock = smb_lock->fl; | |
7067 | list_del(&smb_lock->llist); | |
7068 | retry: | |
6c99dfc4 | 7069 | rc = vfs_lock_file(filp, smb_lock->cmd, flock, NULL); |
e2f34481 NJ |
7070 | skip: |
7071 | if (flags & SMB2_LOCKFLAG_UNLOCK) { | |
6c99dfc4 | 7072 | if (!rc) { |
e2f34481 | 7073 | ksmbd_debug(SMB, "File unlocked\n"); |
6c99dfc4 | 7074 | } else if (rc == -ENOENT) { |
e2f34481 NJ |
7075 | rsp->hdr.Status = STATUS_NOT_LOCKED; |
7076 | goto out; | |
7077 | } | |
7078 | locks_free_lock(flock); | |
7079 | kfree(smb_lock); | |
7080 | } else { | |
6c99dfc4 | 7081 | if (rc == FILE_LOCK_DEFERRED) { |
e2f34481 NJ |
7082 | void **argv; |
7083 | ||
7084 | ksmbd_debug(SMB, | |
070fb21e | 7085 | "would have to wait for getting lock\n"); |
e2f34481 NJ |
7086 | list_add(&smb_lock->llist, &rollback_list); |
7087 | ||
7088 | argv = kmalloc(sizeof(void *), GFP_KERNEL); | |
7089 | if (!argv) { | |
7090 | err = -ENOMEM; | |
7091 | goto out; | |
7092 | } | |
7093 | argv[0] = flock; | |
7094 | ||
6c99dfc4 NJ |
7095 | rc = setup_async_work(work, |
7096 | smb2_remove_blocked_lock, | |
7097 | argv); | |
7098 | if (rc) { | |
8f175272 | 7099 | kfree(argv); |
6c99dfc4 | 7100 | err = -ENOMEM; |
e2f34481 NJ |
7101 | goto out; |
7102 | } | |
7103 | spin_lock(&fp->f_lock); | |
7104 | list_add(&work->fp_entry, &fp->blocked_works); | |
7105 | spin_unlock(&fp->f_lock); | |
7106 | ||
7107 | smb2_send_interim_resp(work, STATUS_PENDING); | |
7108 | ||
45a64e8b | 7109 | ksmbd_vfs_posix_lock_wait(flock); |
e2f34481 | 7110 | |
d3ca9f7a HH |
7111 | spin_lock(&fp->f_lock); |
7112 | list_del(&work->fp_entry); | |
d3ca9f7a | 7113 | spin_unlock(&fp->f_lock); |
d3ca9f7a | 7114 | |
d4075abb | 7115 | if (work->state != KSMBD_WORK_ACTIVE) { |
e2f34481 | 7116 | list_del(&smb_lock->llist); |
e2f34481 NJ |
7117 | locks_free_lock(flock); |
7118 | ||
d4075abb | 7119 | if (work->state == KSMBD_WORK_CANCELLED) { |
e2f34481 NJ |
7120 | rsp->hdr.Status = |
7121 | STATUS_CANCELLED; | |
7122 | kfree(smb_lock); | |
7123 | smb2_send_interim_resp(work, | |
070fb21e | 7124 | STATUS_CANCELLED); |
e2f34481 NJ |
7125 | work->send_no_response = 1; |
7126 | goto out; | |
7127 | } | |
3a9b557f | 7128 | |
e2f34481 NJ |
7129 | rsp->hdr.Status = |
7130 | STATUS_RANGE_NOT_LOCKED; | |
7131 | kfree(smb_lock); | |
7132 | goto out2; | |
7133 | } | |
7134 | ||
7135 | list_del(&smb_lock->llist); | |
3a9b557f | 7136 | release_async_work(work); |
e2f34481 | 7137 | goto retry; |
6c99dfc4 | 7138 | } else if (!rc) { |
75ac9a3d | 7139 | list_add(&smb_lock->llist, &rollback_list); |
d63528eb HL |
7140 | spin_lock(&work->conn->llist_lock); |
7141 | list_add_tail(&smb_lock->clist, | |
7142 | &work->conn->lock_list); | |
7143 | list_add_tail(&smb_lock->flist, | |
7144 | &fp->lock_list); | |
7145 | spin_unlock(&work->conn->llist_lock); | |
e2f34481 NJ |
7146 | ksmbd_debug(SMB, "successful in taking lock\n"); |
7147 | } else { | |
e2f34481 NJ |
7148 | goto out; |
7149 | } | |
7150 | } | |
7151 | } | |
7152 | ||
7153 | if (atomic_read(&fp->f_ci->op_count) > 1) | |
7154 | smb_break_all_oplock(work, fp); | |
7155 | ||
7156 | rsp->StructureSize = cpu_to_le16(4); | |
7157 | ksmbd_debug(SMB, "successful in taking lock\n"); | |
7158 | rsp->hdr.Status = STATUS_SUCCESS; | |
7159 | rsp->Reserved = 0; | |
e2b76ab8 NJ |
7160 | err = ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_lock_rsp)); |
7161 | if (err) | |
7162 | goto out; | |
7163 | ||
e2f34481 | 7164 | ksmbd_fd_put(work, fp); |
96ad4ec5 | 7165 | return 0; |
e2f34481 NJ |
7166 | |
7167 | out: | |
7168 | list_for_each_entry_safe(smb_lock, tmp, &lock_list, llist) { | |
7169 | locks_free_lock(smb_lock->fl); | |
7170 | list_del(&smb_lock->llist); | |
7171 | kfree(smb_lock); | |
7172 | } | |
7173 | ||
7174 | list_for_each_entry_safe(smb_lock, tmp, &rollback_list, llist) { | |
7175 | struct file_lock *rlock = NULL; | |
7176 | ||
7177 | rlock = smb_flock_init(filp); | |
16f9ce81 | 7178 | rlock->c.flc_type = F_UNLCK; |
e2f34481 NJ |
7179 | rlock->fl_start = smb_lock->start; |
7180 | rlock->fl_end = smb_lock->end; | |
7181 | ||
7ecbe926 | 7182 | rc = vfs_lock_file(filp, F_SETLK, rlock, NULL); |
96ad4ec5 NJ |
7183 | if (rc) |
7184 | pr_err("rollback unlock fail : %d\n", rc); | |
d63528eb | 7185 | |
e2f34481 | 7186 | list_del(&smb_lock->llist); |
d63528eb HL |
7187 | spin_lock(&work->conn->llist_lock); |
7188 | if (!list_empty(&smb_lock->flist)) | |
7189 | list_del(&smb_lock->flist); | |
7190 | list_del(&smb_lock->clist); | |
7191 | spin_unlock(&work->conn->llist_lock); | |
7192 | ||
e2f34481 NJ |
7193 | locks_free_lock(smb_lock->fl); |
7194 | locks_free_lock(rlock); | |
7195 | kfree(smb_lock); | |
7196 | } | |
7197 | out2: | |
6c99dfc4 NJ |
7198 | ksmbd_debug(SMB, "failed in taking lock(flags : %x), err : %d\n", flags, err); |
7199 | ||
7200 | if (!rsp->hdr.Status) { | |
7201 | if (err == -EINVAL) | |
7202 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
7203 | else if (err == -ENOMEM) | |
7204 | rsp->hdr.Status = STATUS_INSUFFICIENT_RESOURCES; | |
7205 | else if (err == -ENOENT) | |
7206 | rsp->hdr.Status = STATUS_FILE_CLOSED; | |
7207 | else | |
7208 | rsp->hdr.Status = STATUS_LOCK_NOT_GRANTED; | |
7209 | } | |
7210 | ||
e2f34481 NJ |
7211 | smb2_set_err_rsp(work); |
7212 | ksmbd_fd_put(work, fp); | |
96ad4ec5 | 7213 | return err; |
e2f34481 NJ |
7214 | } |
7215 | ||
f7db8fd0 NJ |
7216 | static int fsctl_copychunk(struct ksmbd_work *work, |
7217 | struct copychunk_ioctl_req *ci_req, | |
7218 | unsigned int cnt_code, | |
7219 | unsigned int input_count, | |
7220 | unsigned long long volatile_id, | |
7221 | unsigned long long persistent_id, | |
070fb21e | 7222 | struct smb2_ioctl_rsp *rsp) |
e2f34481 | 7223 | { |
e2f34481 NJ |
7224 | struct copychunk_ioctl_rsp *ci_rsp; |
7225 | struct ksmbd_file *src_fp = NULL, *dst_fp = NULL; | |
7226 | struct srv_copychunk *chunks; | |
7227 | unsigned int i, chunk_count, chunk_count_written = 0; | |
7228 | unsigned int chunk_size_written = 0; | |
7229 | loff_t total_size_written = 0; | |
f7db8fd0 | 7230 | int ret = 0; |
e2f34481 | 7231 | |
e2f34481 NJ |
7232 | ci_rsp = (struct copychunk_ioctl_rsp *)&rsp->Buffer[0]; |
7233 | ||
2d004c6c PA |
7234 | rsp->VolatileFileId = volatile_id; |
7235 | rsp->PersistentFileId = persistent_id; | |
64b39f4a NJ |
7236 | ci_rsp->ChunksWritten = |
7237 | cpu_to_le32(ksmbd_server_side_copy_max_chunk_count()); | |
7238 | ci_rsp->ChunkBytesWritten = | |
7239 | cpu_to_le32(ksmbd_server_side_copy_max_chunk_size()); | |
7240 | ci_rsp->TotalBytesWritten = | |
7241 | cpu_to_le32(ksmbd_server_side_copy_max_total_size()); | |
e2f34481 NJ |
7242 | |
7243 | chunks = (struct srv_copychunk *)&ci_req->Chunks[0]; | |
7244 | chunk_count = le32_to_cpu(ci_req->ChunkCount); | |
f7db8fd0 NJ |
7245 | if (chunk_count == 0) |
7246 | goto out; | |
e2f34481 NJ |
7247 | total_size_written = 0; |
7248 | ||
7249 | /* verify the SRV_COPYCHUNK_COPY packet */ | |
7250 | if (chunk_count > ksmbd_server_side_copy_max_chunk_count() || | |
f7db8fd0 | 7251 | input_count < offsetof(struct copychunk_ioctl_req, Chunks) + |
64b39f4a | 7252 | chunk_count * sizeof(struct srv_copychunk)) { |
e2f34481 NJ |
7253 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
7254 | return -EINVAL; | |
7255 | } | |
7256 | ||
7257 | for (i = 0; i < chunk_count; i++) { | |
7258 | if (le32_to_cpu(chunks[i].Length) == 0 || | |
64b39f4a | 7259 | le32_to_cpu(chunks[i].Length) > ksmbd_server_side_copy_max_chunk_size()) |
e2f34481 NJ |
7260 | break; |
7261 | total_size_written += le32_to_cpu(chunks[i].Length); | |
7262 | } | |
64b39f4a NJ |
7263 | |
7264 | if (i < chunk_count || | |
7265 | total_size_written > ksmbd_server_side_copy_max_total_size()) { | |
e2f34481 NJ |
7266 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
7267 | return -EINVAL; | |
7268 | } | |
7269 | ||
7270 | src_fp = ksmbd_lookup_foreign_fd(work, | |
070fb21e | 7271 | le64_to_cpu(ci_req->ResumeKey[0])); |
f7db8fd0 | 7272 | dst_fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); |
e2f34481 | 7273 | ret = -EINVAL; |
64b39f4a NJ |
7274 | if (!src_fp || |
7275 | src_fp->persistent_id != le64_to_cpu(ci_req->ResumeKey[1])) { | |
e2f34481 NJ |
7276 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; |
7277 | goto out; | |
7278 | } | |
64b39f4a | 7279 | |
e2f34481 NJ |
7280 | if (!dst_fp) { |
7281 | rsp->hdr.Status = STATUS_FILE_CLOSED; | |
7282 | goto out; | |
7283 | } | |
7284 | ||
7285 | /* | |
7286 | * FILE_READ_DATA should only be included in | |
7287 | * the FSCTL_COPYCHUNK case | |
7288 | */ | |
070fb21e NJ |
7289 | if (cnt_code == FSCTL_COPYCHUNK && |
7290 | !(dst_fp->daccess & (FILE_READ_DATA_LE | FILE_GENERIC_READ_LE))) { | |
e2f34481 NJ |
7291 | rsp->hdr.Status = STATUS_ACCESS_DENIED; |
7292 | goto out; | |
7293 | } | |
7294 | ||
7295 | ret = ksmbd_vfs_copy_file_ranges(work, src_fp, dst_fp, | |
070fb21e NJ |
7296 | chunks, chunk_count, |
7297 | &chunk_count_written, | |
7298 | &chunk_size_written, | |
7299 | &total_size_written); | |
e2f34481 NJ |
7300 | if (ret < 0) { |
7301 | if (ret == -EACCES) | |
7302 | rsp->hdr.Status = STATUS_ACCESS_DENIED; | |
7303 | if (ret == -EAGAIN) | |
7304 | rsp->hdr.Status = STATUS_FILE_LOCK_CONFLICT; | |
7305 | else if (ret == -EBADF) | |
7306 | rsp->hdr.Status = STATUS_INVALID_HANDLE; | |
7307 | else if (ret == -EFBIG || ret == -ENOSPC) | |
7308 | rsp->hdr.Status = STATUS_DISK_FULL; | |
7309 | else if (ret == -EINVAL) | |
7310 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
7311 | else if (ret == -EISDIR) | |
7312 | rsp->hdr.Status = STATUS_FILE_IS_A_DIRECTORY; | |
7313 | else if (ret == -E2BIG) | |
7314 | rsp->hdr.Status = STATUS_INVALID_VIEW_SIZE; | |
7315 | else | |
7316 | rsp->hdr.Status = STATUS_UNEXPECTED_IO_ERROR; | |
7317 | } | |
7318 | ||
7319 | ci_rsp->ChunksWritten = cpu_to_le32(chunk_count_written); | |
7320 | ci_rsp->ChunkBytesWritten = cpu_to_le32(chunk_size_written); | |
7321 | ci_rsp->TotalBytesWritten = cpu_to_le32(total_size_written); | |
7322 | out: | |
7323 | ksmbd_fd_put(work, src_fp); | |
7324 | ksmbd_fd_put(work, dst_fp); | |
7325 | return ret; | |
7326 | } | |
7327 | ||
7328 | static __be32 idev_ipv4_address(struct in_device *idev) | |
7329 | { | |
7330 | __be32 addr = 0; | |
7331 | ||
7332 | struct in_ifaddr *ifa; | |
7333 | ||
7334 | rcu_read_lock(); | |
7335 | in_dev_for_each_ifa_rcu(ifa, idev) { | |
7336 | if (ifa->ifa_flags & IFA_F_SECONDARY) | |
7337 | continue; | |
7338 | ||
7339 | addr = ifa->ifa_address; | |
7340 | break; | |
7341 | } | |
7342 | rcu_read_unlock(); | |
7343 | return addr; | |
7344 | } | |
7345 | ||
7346 | static int fsctl_query_iface_info_ioctl(struct ksmbd_conn *conn, | |
f7db8fd0 NJ |
7347 | struct smb2_ioctl_rsp *rsp, |
7348 | unsigned int out_buf_len) | |
e2f34481 NJ |
7349 | { |
7350 | struct network_interface_info_ioctl_rsp *nii_rsp = NULL; | |
7351 | int nbytes = 0; | |
7352 | struct net_device *netdev; | |
7353 | struct sockaddr_storage_rsp *sockaddr_storage; | |
7354 | unsigned int flags; | |
7355 | unsigned long long speed; | |
7356 | ||
7357 | rtnl_lock(); | |
7358 | for_each_netdev(&init_net, netdev) { | |
71cd9cb6 | 7359 | bool ipv4_set = false; |
f7db8fd0 | 7360 | |
e2f34481 NJ |
7361 | if (netdev->type == ARPHRD_LOOPBACK) |
7362 | continue; | |
7363 | ||
7364 | flags = dev_get_flags(netdev); | |
7365 | if (!(flags & IFF_RUNNING)) | |
7366 | continue; | |
71cd9cb6 NJ |
7367 | ipv6_retry: |
7368 | if (out_buf_len < | |
7369 | nbytes + sizeof(struct network_interface_info_ioctl_rsp)) { | |
7370 | rtnl_unlock(); | |
7371 | return -ENOSPC; | |
7372 | } | |
e2f34481 NJ |
7373 | |
7374 | nii_rsp = (struct network_interface_info_ioctl_rsp *) | |
7375 | &rsp->Buffer[nbytes]; | |
7376 | nii_rsp->IfIndex = cpu_to_le32(netdev->ifindex); | |
7377 | ||
03d8d4f1 | 7378 | nii_rsp->Capability = 0; |
a58b45a4 NJ |
7379 | if (netdev->real_num_tx_queues > 1) |
7380 | nii_rsp->Capability |= cpu_to_le32(RSS_CAPABLE); | |
03d8d4f1 HL |
7381 | if (ksmbd_rdma_capable_netdev(netdev)) |
7382 | nii_rsp->Capability |= cpu_to_le32(RDMA_CAPABLE); | |
e2f34481 NJ |
7383 | |
7384 | nii_rsp->Next = cpu_to_le32(152); | |
7385 | nii_rsp->Reserved = 0; | |
7386 | ||
7387 | if (netdev->ethtool_ops->get_link_ksettings) { | |
7388 | struct ethtool_link_ksettings cmd; | |
7389 | ||
7390 | netdev->ethtool_ops->get_link_ksettings(netdev, &cmd); | |
7391 | speed = cmd.base.speed; | |
7392 | } else { | |
d475866e PF |
7393 | ksmbd_debug(SMB, "%s %s\n", netdev->name, |
7394 | "speed is unknown, defaulting to 1Gb/sec"); | |
e2f34481 NJ |
7395 | speed = SPEED_1000; |
7396 | } | |
7397 | ||
7398 | speed *= 1000000; | |
7399 | nii_rsp->LinkSpeed = cpu_to_le64(speed); | |
7400 | ||
7401 | sockaddr_storage = (struct sockaddr_storage_rsp *) | |
7402 | nii_rsp->SockAddr_Storage; | |
7403 | memset(sockaddr_storage, 0, 128); | |
7404 | ||
71cd9cb6 | 7405 | if (!ipv4_set) { |
e2f34481 NJ |
7406 | struct in_device *idev; |
7407 | ||
7408 | sockaddr_storage->Family = cpu_to_le16(INTERNETWORK); | |
7409 | sockaddr_storage->addr4.Port = 0; | |
7410 | ||
7411 | idev = __in_dev_get_rtnl(netdev); | |
7412 | if (!idev) | |
7413 | continue; | |
7414 | sockaddr_storage->addr4.IPv4address = | |
7415 | idev_ipv4_address(idev); | |
71cd9cb6 NJ |
7416 | nbytes += sizeof(struct network_interface_info_ioctl_rsp); |
7417 | ipv4_set = true; | |
7418 | goto ipv6_retry; | |
e2f34481 NJ |
7419 | } else { |
7420 | struct inet6_dev *idev6; | |
7421 | struct inet6_ifaddr *ifa; | |
7422 | __u8 *ipv6_addr = sockaddr_storage->addr6.IPv6address; | |
7423 | ||
7424 | sockaddr_storage->Family = cpu_to_le16(INTERNETWORKV6); | |
7425 | sockaddr_storage->addr6.Port = 0; | |
7426 | sockaddr_storage->addr6.FlowInfo = 0; | |
7427 | ||
7428 | idev6 = __in6_dev_get(netdev); | |
7429 | if (!idev6) | |
7430 | continue; | |
7431 | ||
7432 | list_for_each_entry(ifa, &idev6->addr_list, if_list) { | |
7433 | if (ifa->flags & (IFA_F_TENTATIVE | | |
7434 | IFA_F_DEPRECATED)) | |
7435 | continue; | |
7436 | memcpy(ipv6_addr, ifa->addr.s6_addr, 16); | |
7437 | break; | |
7438 | } | |
7439 | sockaddr_storage->addr6.ScopeId = 0; | |
71cd9cb6 | 7440 | nbytes += sizeof(struct network_interface_info_ioctl_rsp); |
e2f34481 | 7441 | } |
e2f34481 NJ |
7442 | } |
7443 | rtnl_unlock(); | |
7444 | ||
7445 | /* zero if this is last one */ | |
7446 | if (nii_rsp) | |
7447 | nii_rsp->Next = 0; | |
7448 | ||
2d004c6c PA |
7449 | rsp->PersistentFileId = SMB2_NO_FID; |
7450 | rsp->VolatileFileId = SMB2_NO_FID; | |
e2f34481 NJ |
7451 | return nbytes; |
7452 | } | |
7453 | ||
e2f34481 | 7454 | static int fsctl_validate_negotiate_info(struct ksmbd_conn *conn, |
070fb21e | 7455 | struct validate_negotiate_info_req *neg_req, |
f7db8fd0 NJ |
7456 | struct validate_negotiate_info_rsp *neg_rsp, |
7457 | unsigned int in_buf_len) | |
e2f34481 NJ |
7458 | { |
7459 | int ret = 0; | |
7460 | int dialect; | |
7461 | ||
78f1688a | 7462 | if (in_buf_len < offsetof(struct validate_negotiate_info_req, Dialects) + |
f7db8fd0 NJ |
7463 | le16_to_cpu(neg_req->DialectCount) * sizeof(__le16)) |
7464 | return -EINVAL; | |
7465 | ||
e2f34481 | 7466 | dialect = ksmbd_lookup_dialect_by_id(neg_req->Dialects, |
070fb21e | 7467 | neg_req->DialectCount); |
e2f34481 NJ |
7468 | if (dialect == BAD_PROT_ID || dialect != conn->dialect) { |
7469 | ret = -EINVAL; | |
7470 | goto err_out; | |
7471 | } | |
7472 | ||
7473 | if (strncmp(neg_req->Guid, conn->ClientGUID, SMB2_CLIENT_GUID_SIZE)) { | |
7474 | ret = -EINVAL; | |
7475 | goto err_out; | |
7476 | } | |
7477 | ||
7478 | if (le16_to_cpu(neg_req->SecurityMode) != conn->cli_sec_mode) { | |
7479 | ret = -EINVAL; | |
7480 | goto err_out; | |
7481 | } | |
7482 | ||
7483 | if (le32_to_cpu(neg_req->Capabilities) != conn->cli_cap) { | |
7484 | ret = -EINVAL; | |
7485 | goto err_out; | |
7486 | } | |
7487 | ||
7488 | neg_rsp->Capabilities = cpu_to_le32(conn->vals->capabilities); | |
7489 | memset(neg_rsp->Guid, 0, SMB2_CLIENT_GUID_SIZE); | |
7490 | neg_rsp->SecurityMode = cpu_to_le16(conn->srv_sec_mode); | |
7491 | neg_rsp->Dialect = cpu_to_le16(conn->dialect); | |
7492 | err_out: | |
7493 | return ret; | |
7494 | } | |
7495 | ||
64b39f4a | 7496 | static int fsctl_query_allocated_ranges(struct ksmbd_work *work, u64 id, |
070fb21e NJ |
7497 | struct file_allocated_range_buffer *qar_req, |
7498 | struct file_allocated_range_buffer *qar_rsp, | |
f7db8fd0 | 7499 | unsigned int in_count, unsigned int *out_count) |
e2f34481 NJ |
7500 | { |
7501 | struct ksmbd_file *fp; | |
7502 | loff_t start, length; | |
7503 | int ret = 0; | |
7504 | ||
7505 | *out_count = 0; | |
7506 | if (in_count == 0) | |
7507 | return -EINVAL; | |
7508 | ||
342edb60 NJ |
7509 | start = le64_to_cpu(qar_req->file_offset); |
7510 | length = le64_to_cpu(qar_req->length); | |
7511 | ||
7512 | if (start < 0 || length < 0) | |
7513 | return -EINVAL; | |
7514 | ||
e2f34481 NJ |
7515 | fp = ksmbd_lookup_fd_fast(work, id); |
7516 | if (!fp) | |
7517 | return -ENOENT; | |
7518 | ||
e2f34481 | 7519 | ret = ksmbd_vfs_fqar_lseek(fp, start, length, |
070fb21e | 7520 | qar_rsp, in_count, out_count); |
e2f34481 NJ |
7521 | if (ret && ret != -E2BIG) |
7522 | *out_count = 0; | |
7523 | ||
7524 | ksmbd_fd_put(work, fp); | |
7525 | return ret; | |
7526 | } | |
7527 | ||
64b39f4a | 7528 | static int fsctl_pipe_transceive(struct ksmbd_work *work, u64 id, |
f7db8fd0 NJ |
7529 | unsigned int out_buf_len, |
7530 | struct smb2_ioctl_req *req, | |
070fb21e | 7531 | struct smb2_ioctl_rsp *rsp) |
e2f34481 NJ |
7532 | { |
7533 | struct ksmbd_rpc_command *rpc_resp; | |
7534 | char *data_buf = (char *)&req->Buffer[0]; | |
7535 | int nbytes = 0; | |
7536 | ||
64b39f4a | 7537 | rpc_resp = ksmbd_rpc_ioctl(work->sess, id, data_buf, |
070fb21e | 7538 | le32_to_cpu(req->InputCount)); |
e2f34481 NJ |
7539 | if (rpc_resp) { |
7540 | if (rpc_resp->flags == KSMBD_RPC_SOME_NOT_MAPPED) { | |
7541 | /* | |
7542 | * set STATUS_SOME_NOT_MAPPED response | |
7543 | * for unknown domain sid. | |
7544 | */ | |
7545 | rsp->hdr.Status = STATUS_SOME_NOT_MAPPED; | |
7546 | } else if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) { | |
7547 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; | |
7548 | goto out; | |
7549 | } else if (rpc_resp->flags != KSMBD_RPC_OK) { | |
7550 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
7551 | goto out; | |
7552 | } | |
7553 | ||
7554 | nbytes = rpc_resp->payload_sz; | |
7555 | if (rpc_resp->payload_sz > out_buf_len) { | |
7556 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; | |
7557 | nbytes = out_buf_len; | |
7558 | } | |
7559 | ||
7560 | if (!rpc_resp->payload_sz) { | |
7561 | rsp->hdr.Status = | |
7562 | STATUS_UNEXPECTED_IO_ERROR; | |
7563 | goto out; | |
7564 | } | |
7565 | ||
7566 | memcpy((char *)rsp->Buffer, rpc_resp->payload, nbytes); | |
7567 | } | |
7568 | out: | |
79f6b11a | 7569 | kvfree(rpc_resp); |
e2f34481 NJ |
7570 | return nbytes; |
7571 | } | |
7572 | ||
64b39f4a | 7573 | static inline int fsctl_set_sparse(struct ksmbd_work *work, u64 id, |
070fb21e | 7574 | struct file_sparse *sparse) |
e2f34481 NJ |
7575 | { |
7576 | struct ksmbd_file *fp; | |
4609e1f1 | 7577 | struct mnt_idmap *idmap; |
e2f34481 NJ |
7578 | int ret = 0; |
7579 | __le32 old_fattr; | |
7580 | ||
7581 | fp = ksmbd_lookup_fd_fast(work, id); | |
7582 | if (!fp) | |
7583 | return -ENOENT; | |
4609e1f1 | 7584 | idmap = file_mnt_idmap(fp->filp); |
e2f34481 NJ |
7585 | |
7586 | old_fattr = fp->f_ci->m_fattr; | |
7587 | if (sparse->SetSparse) | |
26a2787d | 7588 | fp->f_ci->m_fattr |= FILE_ATTRIBUTE_SPARSE_FILE_LE; |
e2f34481 | 7589 | else |
26a2787d | 7590 | fp->f_ci->m_fattr &= ~FILE_ATTRIBUTE_SPARSE_FILE_LE; |
e2f34481 NJ |
7591 | |
7592 | if (fp->f_ci->m_fattr != old_fattr && | |
64b39f4a NJ |
7593 | test_share_config_flag(work->tcon->share_conf, |
7594 | KSMBD_SHARE_FLAG_STORE_DOS_ATTRS)) { | |
e2f34481 NJ |
7595 | struct xattr_dos_attrib da; |
7596 | ||
4609e1f1 | 7597 | ret = ksmbd_vfs_get_dos_attrib_xattr(idmap, |
af34983e | 7598 | fp->filp->f_path.dentry, &da); |
e2f34481 NJ |
7599 | if (ret <= 0) |
7600 | goto out; | |
7601 | ||
7602 | da.attr = le32_to_cpu(fp->f_ci->m_fattr); | |
4609e1f1 | 7603 | ret = ksmbd_vfs_set_dos_attrib_xattr(idmap, |
864fb5d3 NJ |
7604 | &fp->filp->f_path, |
7605 | &da, true); | |
e2f34481 NJ |
7606 | if (ret) |
7607 | fp->f_ci->m_fattr = old_fattr; | |
7608 | } | |
7609 | ||
7610 | out: | |
7611 | ksmbd_fd_put(work, fp); | |
7612 | return ret; | |
7613 | } | |
7614 | ||
7615 | static int fsctl_request_resume_key(struct ksmbd_work *work, | |
070fb21e NJ |
7616 | struct smb2_ioctl_req *req, |
7617 | struct resume_key_ioctl_rsp *key_rsp) | |
e2f34481 NJ |
7618 | { |
7619 | struct ksmbd_file *fp; | |
7620 | ||
2d004c6c | 7621 | fp = ksmbd_lookup_fd_slow(work, req->VolatileFileId, req->PersistentFileId); |
e2f34481 NJ |
7622 | if (!fp) |
7623 | return -ENOENT; | |
7624 | ||
7625 | memset(key_rsp, 0, sizeof(*key_rsp)); | |
7626 | key_rsp->ResumeKey[0] = req->VolatileFileId; | |
7627 | key_rsp->ResumeKey[1] = req->PersistentFileId; | |
7628 | ksmbd_fd_put(work, fp); | |
7629 | ||
7630 | return 0; | |
7631 | } | |
7632 | ||
7633 | /** | |
7634 | * smb2_ioctl() - handler for smb2 ioctl command | |
7635 | * @work: smb work containing ioctl command buffer | |
7636 | * | |
7637 | * Return: 0 on success, otherwise error | |
7638 | */ | |
7639 | int smb2_ioctl(struct ksmbd_work *work) | |
7640 | { | |
7641 | struct smb2_ioctl_req *req; | |
cb451720 | 7642 | struct smb2_ioctl_rsp *rsp; |
f7db8fd0 | 7643 | unsigned int cnt_code, nbytes = 0, out_buf_len, in_buf_len; |
64b39f4a | 7644 | u64 id = KSMBD_NO_FID; |
e2f34481 NJ |
7645 | struct ksmbd_conn *conn = work->conn; |
7646 | int ret = 0; | |
7647 | ||
e2f34481 | 7648 | if (work->next_smb2_rcv_hdr_off) { |
8a893315 NJ |
7649 | req = ksmbd_req_buf_next(work); |
7650 | rsp = ksmbd_resp_buf_next(work); | |
2d004c6c | 7651 | if (!has_file_id(req->VolatileFileId)) { |
3867369e | 7652 | ksmbd_debug(SMB, "Compound request set FID = %llu\n", |
070fb21e | 7653 | work->compound_fid); |
e2f34481 NJ |
7654 | id = work->compound_fid; |
7655 | } | |
7656 | } else { | |
cb451720 NJ |
7657 | req = smb2_get_msg(work->request_buf); |
7658 | rsp = smb2_get_msg(work->response_buf); | |
e2f34481 NJ |
7659 | } |
7660 | ||
3867369e | 7661 | if (!has_file_id(id)) |
2d004c6c | 7662 | id = req->VolatileFileId; |
e2f34481 NJ |
7663 | |
7664 | if (req->Flags != cpu_to_le32(SMB2_0_IOCTL_IS_FSCTL)) { | |
7665 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; | |
7666 | goto out; | |
7667 | } | |
7668 | ||
15e7b6d7 | 7669 | cnt_code = le32_to_cpu(req->CtlCode); |
34061d6b HL |
7670 | ret = smb2_calc_max_out_buf_len(work, 48, |
7671 | le32_to_cpu(req->MaxOutputResponse)); | |
7672 | if (ret < 0) { | |
7673 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
7674 | goto out; | |
7675 | } | |
7676 | out_buf_len = (unsigned int)ret; | |
f7db8fd0 | 7677 | in_buf_len = le32_to_cpu(req->InputCount); |
e2f34481 NJ |
7678 | |
7679 | switch (cnt_code) { | |
7680 | case FSCTL_DFS_GET_REFERRALS: | |
7681 | case FSCTL_DFS_GET_REFERRALS_EX: | |
7682 | /* Not support DFS yet */ | |
7683 | rsp->hdr.Status = STATUS_FS_DRIVER_REQUIRED; | |
7684 | goto out; | |
7685 | case FSCTL_CREATE_OR_GET_OBJECT_ID: | |
7686 | { | |
7687 | struct file_object_buf_type1_ioctl_rsp *obj_buf; | |
7688 | ||
7689 | nbytes = sizeof(struct file_object_buf_type1_ioctl_rsp); | |
7690 | obj_buf = (struct file_object_buf_type1_ioctl_rsp *) | |
7691 | &rsp->Buffer[0]; | |
7692 | ||
7693 | /* | |
7694 | * TODO: This is dummy implementation to pass smbtorture | |
7695 | * Need to check correct response later | |
7696 | */ | |
7697 | memset(obj_buf->ObjectId, 0x0, 16); | |
7698 | memset(obj_buf->BirthVolumeId, 0x0, 16); | |
7699 | memset(obj_buf->BirthObjectId, 0x0, 16); | |
7700 | memset(obj_buf->DomainId, 0x0, 16); | |
7701 | ||
7702 | break; | |
7703 | } | |
7704 | case FSCTL_PIPE_TRANSCEIVE: | |
f7db8fd0 | 7705 | out_buf_len = min_t(u32, KSMBD_IPC_MAX_PAYLOAD, out_buf_len); |
e2f34481 NJ |
7706 | nbytes = fsctl_pipe_transceive(work, id, out_buf_len, req, rsp); |
7707 | break; | |
7708 | case FSCTL_VALIDATE_NEGOTIATE_INFO: | |
7709 | if (conn->dialect < SMB30_PROT_ID) { | |
7710 | ret = -EOPNOTSUPP; | |
7711 | goto out; | |
7712 | } | |
7713 | ||
b1763d26 ZX |
7714 | if (in_buf_len < offsetof(struct validate_negotiate_info_req, |
7715 | Dialects)) { | |
7716 | ret = -EINVAL; | |
7717 | goto out; | |
7718 | } | |
f7db8fd0 | 7719 | |
b1763d26 ZX |
7720 | if (out_buf_len < sizeof(struct validate_negotiate_info_rsp)) { |
7721 | ret = -EINVAL; | |
7722 | goto out; | |
7723 | } | |
f7db8fd0 | 7724 | |
e2f34481 NJ |
7725 | ret = fsctl_validate_negotiate_info(conn, |
7726 | (struct validate_negotiate_info_req *)&req->Buffer[0], | |
f7db8fd0 NJ |
7727 | (struct validate_negotiate_info_rsp *)&rsp->Buffer[0], |
7728 | in_buf_len); | |
e2f34481 NJ |
7729 | if (ret < 0) |
7730 | goto out; | |
7731 | ||
7732 | nbytes = sizeof(struct validate_negotiate_info_rsp); | |
2d004c6c PA |
7733 | rsp->PersistentFileId = SMB2_NO_FID; |
7734 | rsp->VolatileFileId = SMB2_NO_FID; | |
e2f34481 NJ |
7735 | break; |
7736 | case FSCTL_QUERY_NETWORK_INTERFACE_INFO: | |
f7db8fd0 NJ |
7737 | ret = fsctl_query_iface_info_ioctl(conn, rsp, out_buf_len); |
7738 | if (ret < 0) | |
e2f34481 | 7739 | goto out; |
f7db8fd0 | 7740 | nbytes = ret; |
e2f34481 NJ |
7741 | break; |
7742 | case FSCTL_REQUEST_RESUME_KEY: | |
7743 | if (out_buf_len < sizeof(struct resume_key_ioctl_rsp)) { | |
7744 | ret = -EINVAL; | |
7745 | goto out; | |
7746 | } | |
7747 | ||
7748 | ret = fsctl_request_resume_key(work, req, | |
070fb21e | 7749 | (struct resume_key_ioctl_rsp *)&rsp->Buffer[0]); |
e2f34481 NJ |
7750 | if (ret < 0) |
7751 | goto out; | |
7752 | rsp->PersistentFileId = req->PersistentFileId; | |
7753 | rsp->VolatileFileId = req->VolatileFileId; | |
7754 | nbytes = sizeof(struct resume_key_ioctl_rsp); | |
7755 | break; | |
7756 | case FSCTL_COPYCHUNK: | |
7757 | case FSCTL_COPYCHUNK_WRITE: | |
64b39f4a | 7758 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
e2f34481 | 7759 | ksmbd_debug(SMB, |
070fb21e | 7760 | "User does not have write permission\n"); |
e2f34481 NJ |
7761 | ret = -EACCES; |
7762 | goto out; | |
7763 | } | |
7764 | ||
f7db8fd0 NJ |
7765 | if (in_buf_len < sizeof(struct copychunk_ioctl_req)) { |
7766 | ret = -EINVAL; | |
7767 | goto out; | |
7768 | } | |
7769 | ||
e2f34481 NJ |
7770 | if (out_buf_len < sizeof(struct copychunk_ioctl_rsp)) { |
7771 | ret = -EINVAL; | |
7772 | goto out; | |
7773 | } | |
7774 | ||
7775 | nbytes = sizeof(struct copychunk_ioctl_rsp); | |
f7db8fd0 NJ |
7776 | rsp->VolatileFileId = req->VolatileFileId; |
7777 | rsp->PersistentFileId = req->PersistentFileId; | |
7778 | fsctl_copychunk(work, | |
7779 | (struct copychunk_ioctl_req *)&req->Buffer[0], | |
15e7b6d7 | 7780 | le32_to_cpu(req->CtlCode), |
f7db8fd0 | 7781 | le32_to_cpu(req->InputCount), |
2d004c6c PA |
7782 | req->VolatileFileId, |
7783 | req->PersistentFileId, | |
f7db8fd0 | 7784 | rsp); |
e2f34481 NJ |
7785 | break; |
7786 | case FSCTL_SET_SPARSE: | |
f7db8fd0 NJ |
7787 | if (in_buf_len < sizeof(struct file_sparse)) { |
7788 | ret = -EINVAL; | |
7789 | goto out; | |
7790 | } | |
7791 | ||
e2f34481 | 7792 | ret = fsctl_set_sparse(work, id, |
070fb21e | 7793 | (struct file_sparse *)&req->Buffer[0]); |
e2f34481 NJ |
7794 | if (ret < 0) |
7795 | goto out; | |
7796 | break; | |
7797 | case FSCTL_SET_ZERO_DATA: | |
7798 | { | |
7799 | struct file_zero_data_information *zero_data; | |
7800 | struct ksmbd_file *fp; | |
b5e5f9df | 7801 | loff_t off, len, bfz; |
e2f34481 | 7802 | |
64b39f4a | 7803 | if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) { |
e2f34481 | 7804 | ksmbd_debug(SMB, |
070fb21e | 7805 | "User does not have write permission\n"); |
e2f34481 NJ |
7806 | ret = -EACCES; |
7807 | goto out; | |
7808 | } | |
7809 | ||
f7db8fd0 NJ |
7810 | if (in_buf_len < sizeof(struct file_zero_data_information)) { |
7811 | ret = -EINVAL; | |
7812 | goto out; | |
7813 | } | |
7814 | ||
e2f34481 NJ |
7815 | zero_data = |
7816 | (struct file_zero_data_information *)&req->Buffer[0]; | |
7817 | ||
b5e5f9df NJ |
7818 | off = le64_to_cpu(zero_data->FileOffset); |
7819 | bfz = le64_to_cpu(zero_data->BeyondFinalZero); | |
2d74ec97 | 7820 | if (off < 0 || bfz < 0 || off > bfz) { |
b5e5f9df | 7821 | ret = -EINVAL; |
e2f34481 NJ |
7822 | goto out; |
7823 | } | |
7824 | ||
b5e5f9df NJ |
7825 | len = bfz - off; |
7826 | if (len) { | |
7827 | fp = ksmbd_lookup_fd_fast(work, id); | |
7828 | if (!fp) { | |
7829 | ret = -ENOENT; | |
7830 | goto out; | |
7831 | } | |
e2f34481 | 7832 | |
b5e5f9df NJ |
7833 | ret = ksmbd_vfs_zero_data(work, fp, off, len); |
7834 | ksmbd_fd_put(work, fp); | |
7835 | if (ret < 0) | |
7836 | goto out; | |
7837 | } | |
e2f34481 NJ |
7838 | break; |
7839 | } | |
7840 | case FSCTL_QUERY_ALLOCATED_RANGES: | |
f7db8fd0 NJ |
7841 | if (in_buf_len < sizeof(struct file_allocated_range_buffer)) { |
7842 | ret = -EINVAL; | |
7843 | goto out; | |
7844 | } | |
7845 | ||
e2f34481 NJ |
7846 | ret = fsctl_query_allocated_ranges(work, id, |
7847 | (struct file_allocated_range_buffer *)&req->Buffer[0], | |
7848 | (struct file_allocated_range_buffer *)&rsp->Buffer[0], | |
7849 | out_buf_len / | |
7850 | sizeof(struct file_allocated_range_buffer), &nbytes); | |
7851 | if (ret == -E2BIG) { | |
7852 | rsp->hdr.Status = STATUS_BUFFER_OVERFLOW; | |
7853 | } else if (ret < 0) { | |
7854 | nbytes = 0; | |
7855 | goto out; | |
7856 | } | |
7857 | ||
7858 | nbytes *= sizeof(struct file_allocated_range_buffer); | |
7859 | break; | |
7860 | case FSCTL_GET_REPARSE_POINT: | |
7861 | { | |
7862 | struct reparse_data_buffer *reparse_ptr; | |
7863 | struct ksmbd_file *fp; | |
7864 | ||
7865 | reparse_ptr = (struct reparse_data_buffer *)&rsp->Buffer[0]; | |
7866 | fp = ksmbd_lookup_fd_fast(work, id); | |
7867 | if (!fp) { | |
bde1694a | 7868 | pr_err("not found fp!!\n"); |
e2f34481 NJ |
7869 | ret = -ENOENT; |
7870 | goto out; | |
7871 | } | |
7872 | ||
7873 | reparse_ptr->ReparseTag = | |
ab0b263b | 7874 | smb2_get_reparse_tag_special_file(file_inode(fp->filp)->i_mode); |
e2f34481 NJ |
7875 | reparse_ptr->ReparseDataLength = 0; |
7876 | ksmbd_fd_put(work, fp); | |
7877 | nbytes = sizeof(struct reparse_data_buffer); | |
7878 | break; | |
7879 | } | |
eb817368 NJ |
7880 | case FSCTL_DUPLICATE_EXTENTS_TO_FILE: |
7881 | { | |
7882 | struct ksmbd_file *fp_in, *fp_out = NULL; | |
7883 | struct duplicate_extents_to_file *dup_ext; | |
7884 | loff_t src_off, dst_off, length, cloned; | |
7885 | ||
f7db8fd0 NJ |
7886 | if (in_buf_len < sizeof(struct duplicate_extents_to_file)) { |
7887 | ret = -EINVAL; | |
7888 | goto out; | |
7889 | } | |
7890 | ||
eb817368 NJ |
7891 | dup_ext = (struct duplicate_extents_to_file *)&req->Buffer[0]; |
7892 | ||
7893 | fp_in = ksmbd_lookup_fd_slow(work, dup_ext->VolatileFileHandle, | |
070fb21e | 7894 | dup_ext->PersistentFileHandle); |
eb817368 | 7895 | if (!fp_in) { |
bde1694a | 7896 | pr_err("not found file handle in duplicate extent to file\n"); |
eb817368 NJ |
7897 | ret = -ENOENT; |
7898 | goto out; | |
7899 | } | |
7900 | ||
7901 | fp_out = ksmbd_lookup_fd_fast(work, id); | |
7902 | if (!fp_out) { | |
bde1694a | 7903 | pr_err("not found fp\n"); |
eb817368 NJ |
7904 | ret = -ENOENT; |
7905 | goto dup_ext_out; | |
7906 | } | |
7907 | ||
7908 | src_off = le64_to_cpu(dup_ext->SourceFileOffset); | |
7909 | dst_off = le64_to_cpu(dup_ext->TargetFileOffset); | |
7910 | length = le64_to_cpu(dup_ext->ByteCount); | |
868f9f2f AG |
7911 | /* |
7912 | * XXX: It is not clear if FSCTL_DUPLICATE_EXTENTS_TO_FILE | |
7913 | * should fall back to vfs_copy_file_range(). This could be | |
7914 | * beneficial when re-exporting nfs/smb mount, but note that | |
7915 | * this can result in partial copy that returns an error status. | |
7916 | * If/when FSCTL_DUPLICATE_EXTENTS_TO_FILE_EX is implemented, | |
7917 | * fall back to vfs_copy_file_range(), should be avoided when | |
7918 | * the flag DUPLICATE_EXTENTS_DATA_EX_SOURCE_ATOMIC is set. | |
7919 | */ | |
7920 | cloned = vfs_clone_file_range(fp_in->filp, src_off, | |
7921 | fp_out->filp, dst_off, length, 0); | |
eb817368 NJ |
7922 | if (cloned == -EXDEV || cloned == -EOPNOTSUPP) { |
7923 | ret = -EOPNOTSUPP; | |
7924 | goto dup_ext_out; | |
7925 | } else if (cloned != length) { | |
f8524776 | 7926 | cloned = vfs_copy_file_range(fp_in->filp, src_off, |
868f9f2f AG |
7927 | fp_out->filp, dst_off, |
7928 | length, 0); | |
eb817368 NJ |
7929 | if (cloned != length) { |
7930 | if (cloned < 0) | |
7931 | ret = cloned; | |
7932 | else | |
7933 | ret = -EINVAL; | |
7934 | } | |
7935 | } | |
7936 | ||
7937 | dup_ext_out: | |
7938 | ksmbd_fd_put(work, fp_in); | |
7939 | ksmbd_fd_put(work, fp_out); | |
7940 | if (ret < 0) | |
7941 | goto out; | |
7942 | break; | |
7943 | } | |
e2f34481 NJ |
7944 | default: |
7945 | ksmbd_debug(SMB, "not implemented yet ioctl command 0x%x\n", | |
070fb21e | 7946 | cnt_code); |
e2f34481 NJ |
7947 | ret = -EOPNOTSUPP; |
7948 | goto out; | |
7949 | } | |
7950 | ||
15e7b6d7 | 7951 | rsp->CtlCode = cpu_to_le32(cnt_code); |
e2f34481 NJ |
7952 | rsp->InputCount = cpu_to_le32(0); |
7953 | rsp->InputOffset = cpu_to_le32(112); | |
7954 | rsp->OutputOffset = cpu_to_le32(112); | |
7955 | rsp->OutputCount = cpu_to_le32(nbytes); | |
7956 | rsp->StructureSize = cpu_to_le16(49); | |
7957 | rsp->Reserved = cpu_to_le16(0); | |
7958 | rsp->Flags = cpu_to_le32(0); | |
7959 | rsp->Reserved2 = cpu_to_le32(0); | |
e2b76ab8 NJ |
7960 | ret = ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_ioctl_rsp) + nbytes); |
7961 | if (!ret) | |
7962 | return ret; | |
e2f34481 NJ |
7963 | |
7964 | out: | |
7965 | if (ret == -EACCES) | |
7966 | rsp->hdr.Status = STATUS_ACCESS_DENIED; | |
7967 | else if (ret == -ENOENT) | |
7968 | rsp->hdr.Status = STATUS_OBJECT_NAME_NOT_FOUND; | |
7969 | else if (ret == -EOPNOTSUPP) | |
7970 | rsp->hdr.Status = STATUS_NOT_SUPPORTED; | |
f7db8fd0 NJ |
7971 | else if (ret == -ENOSPC) |
7972 | rsp->hdr.Status = STATUS_BUFFER_TOO_SMALL; | |
e2f34481 NJ |
7973 | else if (ret < 0 || rsp->hdr.Status == 0) |
7974 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; | |
7975 | smb2_set_err_rsp(work); | |
7976 | return 0; | |
7977 | } | |
7978 | ||
7979 | /** | |
7980 | * smb20_oplock_break_ack() - handler for smb2.0 oplock break command | |
7981 | * @work: smb work containing oplock break command buffer | |
7982 | * | |
7983 | * Return: 0 | |
7984 | */ | |
7985 | static void smb20_oplock_break_ack(struct ksmbd_work *work) | |
7986 | { | |
7b7d709e NJ |
7987 | struct smb2_oplock_break *req; |
7988 | struct smb2_oplock_break *rsp; | |
e2f34481 NJ |
7989 | struct ksmbd_file *fp; |
7990 | struct oplock_info *opinfo = NULL; | |
7991 | __le32 err = 0; | |
7992 | int ret = 0; | |
64b39f4a | 7993 | u64 volatile_id, persistent_id; |
e2f34481 NJ |
7994 | char req_oplevel = 0, rsp_oplevel = 0; |
7995 | unsigned int oplock_change_type; | |
7996 | ||
7b7d709e NJ |
7997 | WORK_BUFFERS(work, req, rsp); |
7998 | ||
c7803b05 SF |
7999 | volatile_id = req->VolatileFid; |
8000 | persistent_id = req->PersistentFid; | |
e2f34481 NJ |
8001 | req_oplevel = req->OplockLevel; |
8002 | ksmbd_debug(OPLOCK, "v_id %llu, p_id %llu request oplock level %d\n", | |
8003 | volatile_id, persistent_id, req_oplevel); | |
8004 | ||
8005 | fp = ksmbd_lookup_fd_slow(work, volatile_id, persistent_id); | |
8006 | if (!fp) { | |
8007 | rsp->hdr.Status = STATUS_FILE_CLOSED; | |
8008 | smb2_set_err_rsp(work); | |
8009 | return; | |
8010 | } | |
8011 | ||
8012 | opinfo = opinfo_get(fp); | |
8013 | if (!opinfo) { | |
bde1694a | 8014 | pr_err("unexpected null oplock_info\n"); |
e2f34481 NJ |
8015 | rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; |
8016 | smb2_set_err_rsp(work); | |
8017 | ksmbd_fd_put(work, fp); | |
8018 | return; | |
8019 | } | |
8020 | ||
8021 | if (opinfo->level == SMB2_OPLOCK_LEVEL_NONE) { | |
8022 | rsp->hdr.Status = STATUS_INVALID_OPLOCK_PROTOCOL; | |
8023 | goto err_out; | |
8024 | } | |
8025 | ||
8026 | if (opinfo->op_state == OPLOCK_STATE_NONE) { | |
8027 | ksmbd_debug(SMB, "unexpected oplock state 0x%x\n", opinfo->op_state); | |
8028 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; | |
8029 | goto err_out; | |
8030 | } | |
8031 | ||
64b39f4a NJ |
8032 | if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
8033 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && | |
8034 | (req_oplevel != SMB2_OPLOCK_LEVEL_II && | |
8035 | req_oplevel != SMB2_OPLOCK_LEVEL_NONE)) { | |
e2f34481 NJ |
8036 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
8037 | oplock_change_type = OPLOCK_WRITE_TO_NONE; | |
64b39f4a NJ |
8038 | } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && |
8039 | req_oplevel != SMB2_OPLOCK_LEVEL_NONE) { | |
e2f34481 NJ |
8040 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
8041 | oplock_change_type = OPLOCK_READ_TO_NONE; | |
64b39f4a NJ |
8042 | } else if (req_oplevel == SMB2_OPLOCK_LEVEL_II || |
8043 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { | |
e2f34481 | 8044 | err = STATUS_INVALID_DEVICE_STATE; |
64b39f4a NJ |
8045 | if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
8046 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && | |
8047 | req_oplevel == SMB2_OPLOCK_LEVEL_II) { | |
e2f34481 | 8048 | oplock_change_type = OPLOCK_WRITE_TO_READ; |
64b39f4a NJ |
8049 | } else if ((opinfo->level == SMB2_OPLOCK_LEVEL_EXCLUSIVE || |
8050 | opinfo->level == SMB2_OPLOCK_LEVEL_BATCH) && | |
8051 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { | |
e2f34481 | 8052 | oplock_change_type = OPLOCK_WRITE_TO_NONE; |
64b39f4a NJ |
8053 | } else if (opinfo->level == SMB2_OPLOCK_LEVEL_II && |
8054 | req_oplevel == SMB2_OPLOCK_LEVEL_NONE) { | |
e2f34481 | 8055 | oplock_change_type = OPLOCK_READ_TO_NONE; |
64b39f4a | 8056 | } else { |
e2f34481 | 8057 | oplock_change_type = 0; |
64b39f4a NJ |
8058 | } |
8059 | } else { | |
e2f34481 | 8060 | oplock_change_type = 0; |
64b39f4a | 8061 | } |
e2f34481 NJ |
8062 | |
8063 | switch (oplock_change_type) { | |
8064 | case OPLOCK_WRITE_TO_READ: | |
8065 | ret = opinfo_write_to_read(opinfo); | |
8066 | rsp_oplevel = SMB2_OPLOCK_LEVEL_II; | |
8067 | break; | |
8068 | case OPLOCK_WRITE_TO_NONE: | |
8069 | ret = opinfo_write_to_none(opinfo); | |
8070 | rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; | |
8071 | break; | |
8072 | case OPLOCK_READ_TO_NONE: | |
8073 | ret = opinfo_read_to_none(opinfo); | |
8074 | rsp_oplevel = SMB2_OPLOCK_LEVEL_NONE; | |
8075 | break; | |
8076 | default: | |
bde1694a NJ |
8077 | pr_err("unknown oplock change 0x%x -> 0x%x\n", |
8078 | opinfo->level, rsp_oplevel); | |
e2f34481 NJ |
8079 | } |
8080 | ||
8081 | if (ret < 0) { | |
8082 | rsp->hdr.Status = err; | |
8083 | goto err_out; | |
8084 | } | |
8085 | ||
e2f34481 NJ |
8086 | opinfo->op_state = OPLOCK_STATE_NONE; |
8087 | wake_up_interruptible_all(&opinfo->oplock_q); | |
c6981347 | 8088 | opinfo_put(opinfo); |
8089 | ksmbd_fd_put(work, fp); | |
e2f34481 NJ |
8090 | |
8091 | rsp->StructureSize = cpu_to_le16(24); | |
8092 | rsp->OplockLevel = rsp_oplevel; | |
8093 | rsp->Reserved = 0; | |
8094 | rsp->Reserved2 = 0; | |
c7803b05 SF |
8095 | rsp->VolatileFid = volatile_id; |
8096 | rsp->PersistentFid = persistent_id; | |
e2b76ab8 NJ |
8097 | ret = ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_oplock_break)); |
8098 | if (!ret) | |
8099 | return; | |
e2f34481 NJ |
8100 | |
8101 | err_out: | |
8102 | opinfo->op_state = OPLOCK_STATE_NONE; | |
8103 | wake_up_interruptible_all(&opinfo->oplock_q); | |
8104 | ||
8105 | opinfo_put(opinfo); | |
8106 | ksmbd_fd_put(work, fp); | |
8107 | smb2_set_err_rsp(work); | |
8108 | } | |
8109 | ||
8110 | static int check_lease_state(struct lease *lease, __le32 req_state) | |
8111 | { | |
8112 | if ((lease->new_state == | |
64b39f4a NJ |
8113 | (SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE)) && |
8114 | !(req_state & SMB2_LEASE_WRITE_CACHING_LE)) { | |
e2f34481 NJ |
8115 | lease->new_state = req_state; |
8116 | return 0; | |
8117 | } | |
8118 | ||
8119 | if (lease->new_state == req_state) | |
8120 | return 0; | |
8121 | ||
8122 | return 1; | |
8123 | } | |
8124 | ||
8125 | /** | |
8126 | * smb21_lease_break_ack() - handler for smb2.1 lease break command | |
8127 | * @work: smb work containing lease break command buffer | |
8128 | * | |
8129 | * Return: 0 | |
8130 | */ | |
8131 | static void smb21_lease_break_ack(struct ksmbd_work *work) | |
8132 | { | |
8133 | struct ksmbd_conn *conn = work->conn; | |
7b7d709e NJ |
8134 | struct smb2_lease_ack *req; |
8135 | struct smb2_lease_ack *rsp; | |
e2f34481 NJ |
8136 | struct oplock_info *opinfo; |
8137 | __le32 err = 0; | |
8138 | int ret = 0; | |
8139 | unsigned int lease_change_type; | |
8140 | __le32 lease_state; | |
8141 | struct lease *lease; | |
8142 | ||
7b7d709e NJ |
8143 | WORK_BUFFERS(work, req, rsp); |
8144 | ||
e2f34481 | 8145 | ksmbd_debug(OPLOCK, "smb21 lease break, lease state(0x%x)\n", |
070fb21e | 8146 | le32_to_cpu(req->LeaseState)); |
e2f34481 NJ |
8147 | opinfo = lookup_lease_in_table(conn, req->LeaseKey); |
8148 | if (!opinfo) { | |
8149 | ksmbd_debug(OPLOCK, "file not opened\n"); | |
8150 | smb2_set_err_rsp(work); | |
8151 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; | |
8152 | return; | |
8153 | } | |
8154 | lease = opinfo->o_lease; | |
8155 | ||
8156 | if (opinfo->op_state == OPLOCK_STATE_NONE) { | |
bde1694a NJ |
8157 | pr_err("unexpected lease break state 0x%x\n", |
8158 | opinfo->op_state); | |
e2f34481 NJ |
8159 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; |
8160 | goto err_out; | |
8161 | } | |
8162 | ||
8163 | if (check_lease_state(lease, req->LeaseState)) { | |
8164 | rsp->hdr.Status = STATUS_REQUEST_NOT_ACCEPTED; | |
8165 | ksmbd_debug(OPLOCK, | |
070fb21e NJ |
8166 | "req lease state: 0x%x, expected state: 0x%x\n", |
8167 | req->LeaseState, lease->new_state); | |
e2f34481 NJ |
8168 | goto err_out; |
8169 | } | |
8170 | ||
8171 | if (!atomic_read(&opinfo->breaking_cnt)) { | |
8172 | rsp->hdr.Status = STATUS_UNSUCCESSFUL; | |
8173 | goto err_out; | |
8174 | } | |
8175 | ||
8176 | /* check for bad lease state */ | |
070fb21e NJ |
8177 | if (req->LeaseState & |
8178 | (~(SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE))) { | |
e2f34481 NJ |
8179 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
8180 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) | |
8181 | lease_change_type = OPLOCK_WRITE_TO_NONE; | |
8182 | else | |
8183 | lease_change_type = OPLOCK_READ_TO_NONE; | |
8184 | ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", | |
070fb21e NJ |
8185 | le32_to_cpu(lease->state), |
8186 | le32_to_cpu(req->LeaseState)); | |
64b39f4a NJ |
8187 | } else if (lease->state == SMB2_LEASE_READ_CACHING_LE && |
8188 | req->LeaseState != SMB2_LEASE_NONE_LE) { | |
e2f34481 NJ |
8189 | err = STATUS_INVALID_OPLOCK_PROTOCOL; |
8190 | lease_change_type = OPLOCK_READ_TO_NONE; | |
8191 | ksmbd_debug(OPLOCK, "handle bad lease state 0x%x -> 0x%x\n", | |
070fb21e NJ |
8192 | le32_to_cpu(lease->state), |
8193 | le32_to_cpu(req->LeaseState)); | |
e2f34481 NJ |
8194 | } else { |
8195 | /* valid lease state changes */ | |
8196 | err = STATUS_INVALID_DEVICE_STATE; | |
8197 | if (req->LeaseState == SMB2_LEASE_NONE_LE) { | |
8198 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) | |
8199 | lease_change_type = OPLOCK_WRITE_TO_NONE; | |
8200 | else | |
8201 | lease_change_type = OPLOCK_READ_TO_NONE; | |
8202 | } else if (req->LeaseState & SMB2_LEASE_READ_CACHING_LE) { | |
8203 | if (lease->state & SMB2_LEASE_WRITE_CACHING_LE) | |
8204 | lease_change_type = OPLOCK_WRITE_TO_READ; | |
8205 | else | |
8206 | lease_change_type = OPLOCK_READ_HANDLE_TO_READ; | |
64b39f4a | 8207 | } else { |
e2f34481 | 8208 | lease_change_type = 0; |
64b39f4a | 8209 | } |
e2f34481 NJ |
8210 | } |
8211 | ||
8212 | switch (lease_change_type) { | |
8213 | case OPLOCK_WRITE_TO_READ: | |
8214 | ret = opinfo_write_to_read(opinfo); | |
8215 | break; | |
8216 | case OPLOCK_READ_HANDLE_TO_READ: | |
8217 | ret = opinfo_read_handle_to_read(opinfo); | |
8218 | break; | |
8219 | case OPLOCK_WRITE_TO_NONE: | |
8220 | ret = opinfo_write_to_none(opinfo); | |
8221 | break; | |
8222 | case OPLOCK_READ_TO_NONE: | |
8223 | ret = opinfo_read_to_none(opinfo); | |
8224 | break; | |
8225 | default: | |
8226 | ksmbd_debug(OPLOCK, "unknown lease change 0x%x -> 0x%x\n", | |
070fb21e NJ |
8227 | le32_to_cpu(lease->state), |
8228 | le32_to_cpu(req->LeaseState)); | |
e2f34481 NJ |
8229 | } |
8230 | ||
658609d9 NJ |
8231 | if (ret < 0) { |
8232 | rsp->hdr.Status = err; | |
8233 | goto err_out; | |
8234 | } | |
8235 | ||
e2f34481 NJ |
8236 | lease_state = lease->state; |
8237 | opinfo->op_state = OPLOCK_STATE_NONE; | |
8238 | wake_up_interruptible_all(&opinfo->oplock_q); | |
8239 | atomic_dec(&opinfo->breaking_cnt); | |
8240 | wake_up_interruptible_all(&opinfo->oplock_brk); | |
8241 | opinfo_put(opinfo); | |
8242 | ||
e2f34481 NJ |
8243 | rsp->StructureSize = cpu_to_le16(36); |
8244 | rsp->Reserved = 0; | |
8245 | rsp->Flags = 0; | |
8246 | memcpy(rsp->LeaseKey, req->LeaseKey, 16); | |
8247 | rsp->LeaseState = lease_state; | |
8248 | rsp->LeaseDuration = 0; | |
e2b76ab8 NJ |
8249 | ret = ksmbd_iov_pin_rsp(work, rsp, sizeof(struct smb2_lease_ack)); |
8250 | if (!ret) | |
8251 | return; | |
e2f34481 NJ |
8252 | |
8253 | err_out: | |
e2f34481 NJ |
8254 | wake_up_interruptible_all(&opinfo->oplock_q); |
8255 | atomic_dec(&opinfo->breaking_cnt); | |
8256 | wake_up_interruptible_all(&opinfo->oplock_brk); | |
8257 | ||
8258 | opinfo_put(opinfo); | |
8259 | smb2_set_err_rsp(work); | |
8260 | } | |
8261 | ||
8262 | /** | |
8263 | * smb2_oplock_break() - dispatcher for smb2.0 and 2.1 oplock/lease break | |
8264 | * @work: smb work containing oplock/lease break command buffer | |
8265 | * | |
8266 | * Return: 0 | |
8267 | */ | |
8268 | int smb2_oplock_break(struct ksmbd_work *work) | |
8269 | { | |
7b7d709e NJ |
8270 | struct smb2_oplock_break *req; |
8271 | struct smb2_oplock_break *rsp; | |
8272 | ||
8273 | WORK_BUFFERS(work, req, rsp); | |
e2f34481 NJ |
8274 | |
8275 | switch (le16_to_cpu(req->StructureSize)) { | |
8276 | case OP_BREAK_STRUCT_SIZE_20: | |
8277 | smb20_oplock_break_ack(work); | |
8278 | break; | |
8279 | case OP_BREAK_STRUCT_SIZE_21: | |
8280 | smb21_lease_break_ack(work); | |
8281 | break; | |
8282 | default: | |
8283 | ksmbd_debug(OPLOCK, "invalid break cmd %d\n", | |
070fb21e | 8284 | le16_to_cpu(req->StructureSize)); |
e2f34481 NJ |
8285 | rsp->hdr.Status = STATUS_INVALID_PARAMETER; |
8286 | smb2_set_err_rsp(work); | |
8287 | } | |
8288 | ||
8289 | return 0; | |
8290 | } | |
8291 | ||
8292 | /** | |
8293 | * smb2_notify() - handler for smb2 notify request | |
95fa1ce9 | 8294 | * @work: smb work containing notify command buffer |
e2f34481 NJ |
8295 | * |
8296 | * Return: 0 | |
8297 | */ | |
8298 | int smb2_notify(struct ksmbd_work *work) | |
8299 | { | |
699230f3 RS |
8300 | struct smb2_change_notify_req *req; |
8301 | struct smb2_change_notify_rsp *rsp; | |
e2f34481 NJ |
8302 | |
8303 | WORK_BUFFERS(work, req, rsp); | |
8304 | ||
8305 | if (work->next_smb2_rcv_hdr_off && req->hdr.NextCommand) { | |
8306 | rsp->hdr.Status = STATUS_INTERNAL_ERROR; | |
8307 | smb2_set_err_rsp(work); | |
8308 | return 0; | |
8309 | } | |
8310 | ||
8311 | smb2_set_err_rsp(work); | |
8312 | rsp->hdr.Status = STATUS_NOT_IMPLEMENTED; | |
8313 | return 0; | |
8314 | } | |
8315 | ||
8316 | /** | |
8317 | * smb2_is_sign_req() - handler for checking packet signing status | |
95fa1ce9 HL |
8318 | * @work: smb work containing notify command buffer |
8319 | * @command: SMB2 command id | |
e2f34481 NJ |
8320 | * |
8321 | * Return: true if packed is signed, false otherwise | |
8322 | */ | |
8323 | bool smb2_is_sign_req(struct ksmbd_work *work, unsigned int command) | |
8324 | { | |
cb451720 | 8325 | struct smb2_hdr *rcv_hdr2 = smb2_get_msg(work->request_buf); |
e2f34481 NJ |
8326 | |
8327 | if ((rcv_hdr2->Flags & SMB2_FLAGS_SIGNED) && | |
64b39f4a NJ |
8328 | command != SMB2_NEGOTIATE_HE && |
8329 | command != SMB2_SESSION_SETUP_HE && | |
8330 | command != SMB2_OPLOCK_BREAK_HE) | |
e2f34481 NJ |
8331 | return true; |
8332 | ||
5616015f | 8333 | return false; |
e2f34481 NJ |
8334 | } |
8335 | ||
8336 | /** | |
8337 | * smb2_check_sign_req() - handler for req packet sign processing | |
8338 | * @work: smb work containing notify command buffer | |
8339 | * | |
8340 | * Return: 1 on success, 0 otherwise | |
8341 | */ | |
8342 | int smb2_check_sign_req(struct ksmbd_work *work) | |
8343 | { | |
cb451720 | 8344 | struct smb2_hdr *hdr; |
e2f34481 NJ |
8345 | char signature_req[SMB2_SIGNATURE_SIZE]; |
8346 | char signature[SMB2_HMACSHA256_SIZE]; | |
8347 | struct kvec iov[1]; | |
8348 | size_t len; | |
8349 | ||
cb451720 | 8350 | hdr = smb2_get_msg(work->request_buf); |
e2f34481 | 8351 | if (work->next_smb2_rcv_hdr_off) |
8a893315 | 8352 | hdr = ksmbd_req_buf_next(work); |
e2f34481 NJ |
8353 | |
8354 | if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) | |
cb451720 | 8355 | len = get_rfc1002_len(work->request_buf); |
e2f34481 NJ |
8356 | else if (hdr->NextCommand) |
8357 | len = le32_to_cpu(hdr->NextCommand); | |
8358 | else | |
cb451720 | 8359 | len = get_rfc1002_len(work->request_buf) - |
e2f34481 NJ |
8360 | work->next_smb2_rcv_hdr_off; |
8361 | ||
8362 | memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); | |
8363 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); | |
8364 | ||
8365 | iov[0].iov_base = (char *)&hdr->ProtocolId; | |
8366 | iov[0].iov_len = len; | |
8367 | ||
8368 | if (ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, 1, | |
64b39f4a | 8369 | signature)) |
e2f34481 NJ |
8370 | return 0; |
8371 | ||
8372 | if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { | |
bde1694a | 8373 | pr_err("bad smb2 signature\n"); |
e2f34481 NJ |
8374 | return 0; |
8375 | } | |
8376 | ||
8377 | return 1; | |
8378 | } | |
8379 | ||
8380 | /** | |
8381 | * smb2_set_sign_rsp() - handler for rsp packet sign processing | |
8382 | * @work: smb work containing notify command buffer | |
8383 | * | |
8384 | */ | |
8385 | void smb2_set_sign_rsp(struct ksmbd_work *work) | |
8386 | { | |
cb451720 | 8387 | struct smb2_hdr *hdr; |
e2f34481 | 8388 | char signature[SMB2_HMACSHA256_SIZE]; |
e2b76ab8 | 8389 | struct kvec *iov; |
e2f34481 NJ |
8390 | int n_vec = 1; |
8391 | ||
e2b76ab8 | 8392 | hdr = ksmbd_resp_buf_curr(work); |
e2f34481 NJ |
8393 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
8394 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); | |
8395 | ||
e2b76ab8 NJ |
8396 | if (hdr->Command == SMB2_READ) { |
8397 | iov = &work->iov[work->iov_idx - 1]; | |
e2f34481 | 8398 | n_vec++; |
e2b76ab8 NJ |
8399 | } else { |
8400 | iov = &work->iov[work->iov_idx]; | |
e2f34481 NJ |
8401 | } |
8402 | ||
8403 | if (!ksmbd_sign_smb2_pdu(work->conn, work->sess->sess_key, iov, n_vec, | |
64b39f4a | 8404 | signature)) |
e2f34481 NJ |
8405 | memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); |
8406 | } | |
8407 | ||
8408 | /** | |
8409 | * smb3_check_sign_req() - handler for req packet sign processing | |
8410 | * @work: smb work containing notify command buffer | |
8411 | * | |
8412 | * Return: 1 on success, 0 otherwise | |
8413 | */ | |
8414 | int smb3_check_sign_req(struct ksmbd_work *work) | |
8415 | { | |
f5a544e3 | 8416 | struct ksmbd_conn *conn = work->conn; |
e2f34481 | 8417 | char *signing_key; |
cb451720 | 8418 | struct smb2_hdr *hdr; |
e2f34481 NJ |
8419 | struct channel *chann; |
8420 | char signature_req[SMB2_SIGNATURE_SIZE]; | |
8421 | char signature[SMB2_CMACAES_SIZE]; | |
8422 | struct kvec iov[1]; | |
8423 | size_t len; | |
8424 | ||
cb451720 | 8425 | hdr = smb2_get_msg(work->request_buf); |
e2f34481 | 8426 | if (work->next_smb2_rcv_hdr_off) |
8a893315 | 8427 | hdr = ksmbd_req_buf_next(work); |
e2f34481 NJ |
8428 | |
8429 | if (!hdr->NextCommand && !work->next_smb2_rcv_hdr_off) | |
cb451720 | 8430 | len = get_rfc1002_len(work->request_buf); |
e2f34481 NJ |
8431 | else if (hdr->NextCommand) |
8432 | len = le32_to_cpu(hdr->NextCommand); | |
8433 | else | |
cb451720 | 8434 | len = get_rfc1002_len(work->request_buf) - |
e2f34481 NJ |
8435 | work->next_smb2_rcv_hdr_off; |
8436 | ||
8437 | if (le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { | |
8438 | signing_key = work->sess->smb3signingkey; | |
e2f34481 | 8439 | } else { |
f5a544e3 | 8440 | chann = lookup_chann_list(work->sess, conn); |
8e06b31e | 8441 | if (!chann) { |
e2f34481 | 8442 | return 0; |
8e06b31e | 8443 | } |
e2f34481 | 8444 | signing_key = chann->smb3signingkey; |
e2f34481 NJ |
8445 | } |
8446 | ||
8447 | if (!signing_key) { | |
bde1694a | 8448 | pr_err("SMB3 signing key is not generated\n"); |
e2f34481 NJ |
8449 | return 0; |
8450 | } | |
8451 | ||
8452 | memcpy(signature_req, hdr->Signature, SMB2_SIGNATURE_SIZE); | |
8453 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); | |
8454 | iov[0].iov_base = (char *)&hdr->ProtocolId; | |
8455 | iov[0].iov_len = len; | |
8456 | ||
8457 | if (ksmbd_sign_smb3_pdu(conn, signing_key, iov, 1, signature)) | |
8458 | return 0; | |
8459 | ||
8460 | if (memcmp(signature, signature_req, SMB2_SIGNATURE_SIZE)) { | |
bde1694a | 8461 | pr_err("bad smb2 signature\n"); |
e2f34481 NJ |
8462 | return 0; |
8463 | } | |
8464 | ||
8465 | return 1; | |
8466 | } | |
8467 | ||
8468 | /** | |
8469 | * smb3_set_sign_rsp() - handler for rsp packet sign processing | |
8470 | * @work: smb work containing notify command buffer | |
8471 | * | |
8472 | */ | |
8473 | void smb3_set_sign_rsp(struct ksmbd_work *work) | |
8474 | { | |
f5a544e3 | 8475 | struct ksmbd_conn *conn = work->conn; |
e2b76ab8 | 8476 | struct smb2_hdr *hdr; |
e2f34481 NJ |
8477 | struct channel *chann; |
8478 | char signature[SMB2_CMACAES_SIZE]; | |
e2b76ab8 | 8479 | struct kvec *iov; |
e2f34481 | 8480 | int n_vec = 1; |
e2f34481 NJ |
8481 | char *signing_key; |
8482 | ||
e2b76ab8 | 8483 | hdr = ksmbd_resp_buf_curr(work); |
e2f34481 | 8484 | |
08bdbc6e NJ |
8485 | if (conn->binding == false && |
8486 | le16_to_cpu(hdr->Command) == SMB2_SESSION_SETUP_HE) { | |
e2f34481 | 8487 | signing_key = work->sess->smb3signingkey; |
e2f34481 | 8488 | } else { |
f5a544e3 | 8489 | chann = lookup_chann_list(work->sess, work->conn); |
8e06b31e | 8490 | if (!chann) { |
e2f34481 | 8491 | return; |
8e06b31e | 8492 | } |
e2f34481 | 8493 | signing_key = chann->smb3signingkey; |
e2f34481 NJ |
8494 | } |
8495 | ||
8496 | if (!signing_key) | |
8497 | return; | |
8498 | ||
e2f34481 NJ |
8499 | hdr->Flags |= SMB2_FLAGS_SIGNED; |
8500 | memset(hdr->Signature, 0, SMB2_SIGNATURE_SIZE); | |
e2b76ab8 NJ |
8501 | |
8502 | if (hdr->Command == SMB2_READ) { | |
8503 | iov = &work->iov[work->iov_idx - 1]; | |
e2f34481 | 8504 | n_vec++; |
e2b76ab8 NJ |
8505 | } else { |
8506 | iov = &work->iov[work->iov_idx]; | |
e2f34481 NJ |
8507 | } |
8508 | ||
e2b76ab8 NJ |
8509 | if (!ksmbd_sign_smb3_pdu(conn, signing_key, iov, n_vec, |
8510 | signature)) | |
e2f34481 NJ |
8511 | memcpy(hdr->Signature, signature, SMB2_SIGNATURE_SIZE); |
8512 | } | |
8513 | ||
8514 | /** | |
8515 | * smb3_preauth_hash_rsp() - handler for computing preauth hash on response | |
8516 | * @work: smb work containing response buffer | |
8517 | * | |
8518 | */ | |
8519 | void smb3_preauth_hash_rsp(struct ksmbd_work *work) | |
8520 | { | |
8521 | struct ksmbd_conn *conn = work->conn; | |
8522 | struct ksmbd_session *sess = work->sess; | |
8523 | struct smb2_hdr *req, *rsp; | |
8524 | ||
8525 | if (conn->dialect != SMB311_PROT_ID) | |
8526 | return; | |
8527 | ||
8528 | WORK_BUFFERS(work, req, rsp); | |
8529 | ||
442ff9eb NJ |
8530 | if (le16_to_cpu(req->Command) == SMB2_NEGOTIATE_HE && |
8531 | conn->preauth_info) | |
cb451720 | 8532 | ksmbd_gen_preauth_integrity_hash(conn, work->response_buf, |
070fb21e | 8533 | conn->preauth_info->Preauth_HashValue); |
e2f34481 | 8534 | |
f5a544e3 | 8535 | if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && sess) { |
e2f34481 NJ |
8536 | __u8 *hash_value; |
8537 | ||
f5a544e3 NJ |
8538 | if (conn->binding) { |
8539 | struct preauth_session *preauth_sess; | |
8540 | ||
8541 | preauth_sess = ksmbd_preauth_session_lookup(conn, sess->id); | |
8542 | if (!preauth_sess) | |
8543 | return; | |
8544 | hash_value = preauth_sess->Preauth_HashValue; | |
8545 | } else { | |
8546 | hash_value = sess->Preauth_HashValue; | |
8547 | if (!hash_value) | |
8548 | return; | |
8549 | } | |
cb451720 | 8550 | ksmbd_gen_preauth_integrity_hash(conn, work->response_buf, |
070fb21e | 8551 | hash_value); |
e2f34481 NJ |
8552 | } |
8553 | } | |
8554 | ||
2dd9129f | 8555 | static void fill_transform_hdr(void *tr_buf, char *old_buf, __le16 cipher_type) |
e2f34481 | 8556 | { |
2dd9129f NJ |
8557 | struct smb2_transform_hdr *tr_hdr = tr_buf + 4; |
8558 | struct smb2_hdr *hdr = smb2_get_msg(old_buf); | |
e2f34481 NJ |
8559 | unsigned int orig_len = get_rfc1002_len(old_buf); |
8560 | ||
56b401fb | 8561 | /* tr_buf must be cleared by the caller */ |
e2f34481 NJ |
8562 | tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM; |
8563 | tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len); | |
4355a8fd | 8564 | tr_hdr->Flags = cpu_to_le16(TRANSFORM_FLAG_ENCRYPTED); |
5a0ca770 NJ |
8565 | if (cipher_type == SMB2_ENCRYPTION_AES128_GCM || |
8566 | cipher_type == SMB2_ENCRYPTION_AES256_GCM) | |
8567 | get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE); | |
e2f34481 | 8568 | else |
5a0ca770 | 8569 | get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE); |
e2f34481 | 8570 | memcpy(&tr_hdr->SessionId, &hdr->SessionId, 8); |
2dd9129f NJ |
8571 | inc_rfc1001_len(tr_buf, sizeof(struct smb2_transform_hdr)); |
8572 | inc_rfc1001_len(tr_buf, orig_len); | |
e2f34481 NJ |
8573 | } |
8574 | ||
8575 | int smb3_encrypt_resp(struct ksmbd_work *work) | |
8576 | { | |
e2b76ab8 | 8577 | struct kvec *iov = work->iov; |
e2f34481 | 8578 | int rc = -ENOMEM; |
e2b76ab8 | 8579 | void *tr_buf; |
e2f34481 | 8580 | |
e2b76ab8 NJ |
8581 | tr_buf = kzalloc(sizeof(struct smb2_transform_hdr) + 4, GFP_KERNEL); |
8582 | if (!tr_buf) | |
e2f34481 NJ |
8583 | return rc; |
8584 | ||
8585 | /* fill transform header */ | |
e2b76ab8 | 8586 | fill_transform_hdr(tr_buf, work->response_buf, work->conn->cipher_type); |
e2f34481 | 8587 | |
e2b76ab8 | 8588 | iov[0].iov_base = tr_buf; |
2dd9129f | 8589 | iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4; |
e2b76ab8 | 8590 | work->tr_buf = tr_buf; |
e2f34481 | 8591 | |
e2b76ab8 | 8592 | return ksmbd_crypt_message(work, iov, work->iov_idx + 1, 1); |
e2f34481 NJ |
8593 | } |
8594 | ||
f4228b67 | 8595 | bool smb3_is_transform_hdr(void *buf) |
e2f34481 | 8596 | { |
2dd9129f | 8597 | struct smb2_transform_hdr *trhdr = smb2_get_msg(buf); |
e2f34481 NJ |
8598 | |
8599 | return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM; | |
8600 | } | |
8601 | ||
8602 | int smb3_decrypt_req(struct ksmbd_work *work) | |
8603 | { | |
e2f34481 | 8604 | struct ksmbd_session *sess; |
e5066499 | 8605 | char *buf = work->request_buf; |
e2f34481 NJ |
8606 | unsigned int pdu_length = get_rfc1002_len(buf); |
8607 | struct kvec iov[2]; | |
2dd9129f NJ |
8608 | int buf_data_size = pdu_length - sizeof(struct smb2_transform_hdr); |
8609 | struct smb2_transform_hdr *tr_hdr = smb2_get_msg(buf); | |
e2f34481 NJ |
8610 | int rc = 0; |
8611 | ||
dc318846 NJ |
8612 | if (pdu_length < sizeof(struct smb2_transform_hdr) || |
8613 | buf_data_size < sizeof(struct smb2_hdr)) { | |
bde1694a NJ |
8614 | pr_err("Transform message is too small (%u)\n", |
8615 | pdu_length); | |
e2f34481 NJ |
8616 | return -ECONNABORTED; |
8617 | } | |
8618 | ||
c7705eec | 8619 | if (buf_data_size < le32_to_cpu(tr_hdr->OriginalMessageSize)) { |
bde1694a | 8620 | pr_err("Transform message is broken\n"); |
e2f34481 NJ |
8621 | return -ECONNABORTED; |
8622 | } | |
8623 | ||
af705ef2 | 8624 | sess = ksmbd_session_lookup_all(work->conn, le64_to_cpu(tr_hdr->SessionId)); |
4227f811 NJ |
8625 | if (!sess) { |
8626 | pr_err("invalid session id(%llx) in transform header\n", | |
8627 | le64_to_cpu(tr_hdr->SessionId)); | |
8628 | return -ECONNABORTED; | |
8629 | } | |
8630 | ||
e2f34481 | 8631 | iov[0].iov_base = buf; |
2dd9129f NJ |
8632 | iov[0].iov_len = sizeof(struct smb2_transform_hdr) + 4; |
8633 | iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr) + 4; | |
e2f34481 | 8634 | iov[1].iov_len = buf_data_size; |
af705ef2 | 8635 | rc = ksmbd_crypt_message(work, iov, 2, 0); |
e2f34481 NJ |
8636 | if (rc) |
8637 | return rc; | |
8638 | ||
8639 | memmove(buf + 4, iov[1].iov_base, buf_data_size); | |
cb451720 | 8640 | *(__be32 *)buf = cpu_to_be32(buf_data_size); |
e2f34481 NJ |
8641 | |
8642 | return rc; | |
8643 | } | |
8644 | ||
8645 | bool smb3_11_final_sess_setup_resp(struct ksmbd_work *work) | |
8646 | { | |
8647 | struct ksmbd_conn *conn = work->conn; | |
5fde3c21 | 8648 | struct ksmbd_session *sess = work->sess; |
cb451720 | 8649 | struct smb2_hdr *rsp = smb2_get_msg(work->response_buf); |
e2f34481 NJ |
8650 | |
8651 | if (conn->dialect < SMB30_PROT_ID) | |
8652 | return false; | |
8653 | ||
8654 | if (work->next_smb2_rcv_hdr_off) | |
8a893315 | 8655 | rsp = ksmbd_resp_buf_next(work); |
e2f34481 NJ |
8656 | |
8657 | if (le16_to_cpu(rsp->Command) == SMB2_SESSION_SETUP_HE && | |
5fde3c21 | 8658 | sess->user && !user_guest(sess->user) && |
64b39f4a | 8659 | rsp->Status == STATUS_SUCCESS) |
e2f34481 NJ |
8660 | return true; |
8661 | return false; | |
8662 | } |