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