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