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