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