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