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