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