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