2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "smbd/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../lib/util/tevent_ntstatus.h"
26 #include "include/ntioctl.h"
27 #include "smb2_ioctl_private.h"
28 #include "librpc/gen_ndr/ioctl.h"
31 #define DBGC_CLASS DBGC_SMB2
33 static struct tevent_req *smbd_smb2_ioctl_send(TALLOC_CTX *mem_ctx,
34 struct tevent_context *ev,
35 struct smbd_smb2_request *smb2req,
36 struct files_struct *in_fsp,
39 uint32_t in_max_output,
41 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
43 DATA_BLOB *out_output,
46 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq);
47 NTSTATUS smbd_smb2_request_process_ioctl(struct smbd_smb2_request *req)
50 const uint8_t *inbody;
51 uint32_t min_buffer_offset;
52 uint32_t max_buffer_offset;
53 uint32_t min_output_offset;
54 uint32_t allowed_length_in;
55 uint32_t allowed_length_out;
57 uint64_t in_file_id_persistent;
58 uint64_t in_file_id_volatile;
59 struct files_struct *in_fsp = NULL;
60 uint32_t in_input_offset;
61 uint32_t in_input_length;
62 DATA_BLOB in_input_buffer = data_blob_null;
63 uint32_t in_max_input_length;
64 uint32_t in_output_offset;
65 uint32_t in_output_length;
66 DATA_BLOB in_output_buffer = data_blob_null;
67 uint32_t in_max_output_length;
69 uint32_t data_length_in;
70 uint32_t data_length_out;
71 uint32_t data_length_tmp;
72 uint32_t data_length_max;
73 struct tevent_req *subreq;
75 status = smbd_smb2_request_verify_sizes(req, 0x39);
76 if (!NT_STATUS_IS_OK(status)) {
77 return smbd_smb2_request_error(req, status);
79 inbody = SMBD_SMB2_IN_BODY_PTR(req);
81 in_ctl_code = IVAL(inbody, 0x04);
82 in_file_id_persistent = BVAL(inbody, 0x08);
83 in_file_id_volatile = BVAL(inbody, 0x10);
84 in_input_offset = IVAL(inbody, 0x18);
85 in_input_length = IVAL(inbody, 0x1C);
86 in_max_input_length = IVAL(inbody, 0x20);
87 in_output_offset = IVAL(inbody, 0x24);
88 in_output_length = IVAL(inbody, 0x28);
89 in_max_output_length = IVAL(inbody, 0x2C);
90 in_flags = IVAL(inbody, 0x30);
92 min_buffer_offset = SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(req);
93 max_buffer_offset = min_buffer_offset + SMBD_SMB2_IN_DYN_LEN(req);
94 min_output_offset = min_buffer_offset;
97 * InputOffset (4 bytes): The offset, in bytes, from the beginning of
98 * the SMB2 header to the input data buffer. If no input data is
99 * required for the FSCTL/IOCTL command being issued, the client SHOULD
100 * set this value to 0.<49>
101 * <49> If no input data is required for the FSCTL/IOCTL command being
102 * issued, Windows-based clients set this field to any value.
104 allowed_length_in = 0;
105 if ((in_input_offset > 0) && (in_input_length > 0)) {
108 if (in_input_offset < min_buffer_offset) {
109 return smbd_smb2_request_error(req,
110 NT_STATUS_INVALID_PARAMETER);
112 if (in_input_offset > max_buffer_offset) {
113 return smbd_smb2_request_error(req,
114 NT_STATUS_INVALID_PARAMETER);
116 allowed_length_in = max_buffer_offset - in_input_offset;
118 tmp_ofs = in_input_offset - min_buffer_offset;
119 in_input_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
120 in_input_buffer.data += tmp_ofs;
121 in_input_buffer.length = in_input_length;
122 min_output_offset += tmp_ofs;
123 min_output_offset += in_input_length;
126 if (in_input_length > allowed_length_in) {
127 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
130 allowed_length_out = 0;
131 if (in_output_offset > 0) {
132 if (in_output_offset < min_buffer_offset) {
133 return smbd_smb2_request_error(req,
134 NT_STATUS_INVALID_PARAMETER);
136 if (in_output_offset > max_buffer_offset) {
137 return smbd_smb2_request_error(req,
138 NT_STATUS_INVALID_PARAMETER);
140 allowed_length_out = max_buffer_offset - in_output_offset;
143 if (in_output_length > allowed_length_out) {
144 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
147 if (in_output_length > 0) {
150 if (in_output_offset < min_output_offset) {
151 return smbd_smb2_request_error(req,
152 NT_STATUS_INVALID_PARAMETER);
155 tmp_ofs = in_output_offset - min_buffer_offset;
156 in_output_buffer.data = SMBD_SMB2_IN_DYN_PTR(req);
157 in_output_buffer.data += tmp_ofs;
158 in_output_buffer.length = in_output_length;
162 * verify the credits and avoid overflows
163 * in_input_buffer.length and in_output_buffer.length
164 * are already verified.
166 data_length_in = in_input_buffer.length + in_output_buffer.length;
168 data_length_out = in_max_input_length;
169 data_length_tmp = UINT32_MAX - data_length_out;
170 if (data_length_tmp < in_max_output_length) {
171 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
173 data_length_out += in_max_output_length;
175 data_length_max = MAX(data_length_in, data_length_out);
177 status = smbd_smb2_request_verify_creditcharge(req, data_length_max);
178 if (!NT_STATUS_IS_OK(status)) {
179 return smbd_smb2_request_error(req, status);
183 * If the Flags field of the request is not SMB2_0_IOCTL_IS_FSCTL the
184 * server MUST fail the request with STATUS_NOT_SUPPORTED.
186 if (in_flags != SMB2_IOCTL_FLAG_IS_FSCTL) {
187 return smbd_smb2_request_error(req, NT_STATUS_NOT_SUPPORTED);
190 switch (in_ctl_code) {
191 case FSCTL_DFS_GET_REFERRALS:
192 case FSCTL_DFS_GET_REFERRALS_EX:
193 case FSCTL_PIPE_WAIT:
194 case FSCTL_VALIDATE_NEGOTIATE_INFO_224:
195 case FSCTL_VALIDATE_NEGOTIATE_INFO:
196 case FSCTL_QUERY_NETWORK_INTERFACE_INFO:
197 case FSCTL_SMBTORTURE_FORCE_UNACKED_TIMEOUT:
199 * Some SMB2 specific CtlCodes like FSCTL_DFS_GET_REFERRALS or
200 * FSCTL_PIPE_WAIT does not take a file handle.
202 * If FileId in the SMB2 Header of the request is not
203 * 0xFFFFFFFFFFFFFFFF, then the server MUST fail the request
204 * with STATUS_INVALID_PARAMETER.
206 if (in_file_id_persistent != UINT64_MAX ||
207 in_file_id_volatile != UINT64_MAX) {
208 return smbd_smb2_request_error(req,
209 NT_STATUS_INVALID_PARAMETER);
213 in_fsp = file_fsp_smb2(req, in_file_id_persistent,
214 in_file_id_volatile);
215 if (in_fsp == NULL) {
216 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
221 subreq = smbd_smb2_ioctl_send(req, req->sconn->ev_ctx,
225 in_max_output_length,
227 if (subreq == NULL) {
228 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
230 tevent_req_set_callback(subreq, smbd_smb2_request_ioctl_done, req);
232 return smbd_smb2_request_pending_queue(req, subreq, 1000);
236 * 3.3.4.4 Sending an Error Response
237 * An error code other than one of the following indicates a failure:
239 static bool smbd_smb2_ioctl_is_failure(uint32_t ctl_code, NTSTATUS status,
242 if (NT_STATUS_IS_OK(status)) {
247 * STATUS_BUFFER_OVERFLOW in a FSCTL_PIPE_TRANSCEIVE, FSCTL_PIPE_PEEK or
248 * FSCTL_DFS_GET_REFERRALS Response specified in section 2.2.32.<153>
250 if (NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)
251 && ((ctl_code == FSCTL_PIPE_TRANSCEIVE)
252 || (ctl_code == FSCTL_PIPE_PEEK)
253 || (ctl_code == FSCTL_DFS_GET_REFERRALS))) {
258 * Any status other than STATUS_SUCCESS in a FSCTL_SRV_COPYCHUNK or
259 * FSCTL_SRV_COPYCHUNK_WRITE Response, when returning an
260 * SRV_COPYCHUNK_RESPONSE as described in section 2.2.32.1.
262 if (((ctl_code == FSCTL_SRV_COPYCHUNK)
263 || (ctl_code == FSCTL_SRV_COPYCHUNK_WRITE))
264 && (data_size == sizeof(struct srv_copychunk_rsp))) {
271 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq)
273 struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
274 struct smbd_smb2_request);
275 const uint8_t *inbody;
278 uint32_t in_ctl_code;
279 uint64_t in_file_id_persistent;
280 uint64_t in_file_id_volatile;
281 uint32_t out_input_offset;
282 uint32_t out_output_offset;
283 DATA_BLOB out_output_buffer = data_blob_null;
285 NTSTATUS error; /* transport error */
286 bool disconnect = false;
288 status = smbd_smb2_ioctl_recv(subreq, req,
292 DEBUG(10,("smbd_smb2_request_ioctl_done: smbd_smb2_ioctl_recv returned "
294 (unsigned int)out_output_buffer.length,
295 nt_errstr(status) ));
300 smbd_server_connection_terminate(req->xconn,
305 inbody = SMBD_SMB2_IN_BODY_PTR(req);
307 in_ctl_code = IVAL(inbody, 0x04);
308 in_file_id_persistent = BVAL(inbody, 0x08);
309 in_file_id_volatile = BVAL(inbody, 0x10);
311 if (smbd_smb2_ioctl_is_failure(in_ctl_code, status,
312 out_output_buffer.length)) {
313 error = smbd_smb2_request_error(req, status);
314 if (!NT_STATUS_IS_OK(error)) {
315 smbd_server_connection_terminate(req->xconn,
322 out_input_offset = SMB2_HDR_BODY + 0x30;
323 out_output_offset = SMB2_HDR_BODY + 0x30;
325 outbody = smbd_smb2_generate_outbody(req, 0x30);
326 if (outbody.data == NULL) {
327 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
328 if (!NT_STATUS_IS_OK(error)) {
329 smbd_server_connection_terminate(req->xconn,
336 SSVAL(outbody.data, 0x00, 0x30 + 1); /* struct size */
337 SSVAL(outbody.data, 0x02, 0); /* reserved */
338 SIVAL(outbody.data, 0x04,
339 in_ctl_code); /* ctl code */
340 SBVAL(outbody.data, 0x08,
341 in_file_id_persistent); /* file id (persistent) */
342 SBVAL(outbody.data, 0x10,
343 in_file_id_volatile); /* file id (volatile) */
344 SIVAL(outbody.data, 0x18,
345 out_input_offset); /* input offset */
346 SIVAL(outbody.data, 0x1C, 0); /* input count */
347 SIVAL(outbody.data, 0x20,
348 out_output_offset); /* output offset */
349 SIVAL(outbody.data, 0x24,
350 out_output_buffer.length); /* output count */
351 SIVAL(outbody.data, 0x28, 0); /* flags */
352 SIVAL(outbody.data, 0x2C, 0); /* reserved */
355 * Note: Windows Vista and 2008 send back also the
356 * input from the request. But it was fixed in
359 outdyn = out_output_buffer;
361 error = smbd_smb2_request_done_ex(req, status, outbody, &outdyn,
363 if (!NT_STATUS_IS_OK(error)) {
364 smbd_server_connection_terminate(req->xconn,
370 static struct tevent_req *smb2_ioctl_smbtorture(uint32_t ctl_code,
371 struct tevent_context *ev,
372 struct tevent_req *req,
373 struct smbd_smb2_ioctl_state *state)
378 ok = lp_parm_bool(-1, "smbd", "FSCTL_SMBTORTURE", false);
384 case FSCTL_SMBTORTURE_FORCE_UNACKED_TIMEOUT:
385 if (state->in_input.length != 0) {
386 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
387 return tevent_req_post(req, ev);
390 state->smb2req->xconn->ack.force_unacked_timeout = true;
391 tevent_req_done(req);
392 return tevent_req_post(req, ev);
399 if (IS_IPC(state->smbreq->conn)) {
400 status = NT_STATUS_FS_DRIVER_REQUIRED;
402 status = NT_STATUS_INVALID_DEVICE_REQUEST;
405 tevent_req_nterror(req, status);
406 return tevent_req_post(req, ev);
409 static struct tevent_req *smbd_smb2_ioctl_send(TALLOC_CTX *mem_ctx,
410 struct tevent_context *ev,
411 struct smbd_smb2_request *smb2req,
412 struct files_struct *fsp,
413 uint32_t in_ctl_code,
415 uint32_t in_max_output,
418 struct tevent_req *req;
419 struct smbd_smb2_ioctl_state *state;
420 struct smb_request *smbreq;
422 req = tevent_req_create(mem_ctx, &state,
423 struct smbd_smb2_ioctl_state);
427 state->smb2req = smb2req;
428 state->smbreq = NULL;
430 state->in_input = in_input;
431 state->in_max_output = in_max_output;
432 state->out_output = data_blob_null;
434 DEBUG(10, ("smbd_smb2_ioctl: ctl_code[0x%08x] %s, %s\n",
435 (unsigned)in_ctl_code,
436 fsp ? fsp_str_dbg(fsp) : "<no handle>",
439 smbreq = smbd_smb2_fake_smb_request(smb2req);
440 if (tevent_req_nomem(smbreq, req)) {
441 return tevent_req_post(req, ev);
443 state->smbreq = smbreq;
445 switch (in_ctl_code & IOCTL_DEV_TYPE_MASK) {
447 return smb2_ioctl_dfs(in_ctl_code, ev, req, state);
449 case FSCTL_FILESYSTEM:
450 return smb2_ioctl_filesys(in_ctl_code, ev, req, state);
452 case FSCTL_NAMED_PIPE:
453 return smb2_ioctl_named_pipe(in_ctl_code, ev, req, state);
455 case FSCTL_NETWORK_FILESYSTEM:
456 return smb2_ioctl_network_fs(in_ctl_code, ev, req, state);
458 case FSCTL_SMBTORTURE:
459 return smb2_ioctl_smbtorture(in_ctl_code, ev, req, state);
462 if (IS_IPC(smbreq->conn)) {
463 tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
465 tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
468 return tevent_req_post(req, ev);
472 tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
473 return tevent_req_post(req, ev);
476 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
478 DATA_BLOB *out_output,
481 NTSTATUS status = NT_STATUS_OK;
482 struct smbd_smb2_ioctl_state *state = tevent_req_data(req,
483 struct smbd_smb2_ioctl_state);
484 enum tevent_req_state req_state;
487 *disconnect = state->disconnect;
489 if ((tevent_req_is_error(req, &req_state, &err) == false)
490 || (req_state == TEVENT_REQ_USER_ERROR)) {
492 * Return output buffer to caller if the ioctl was successfully
493 * processed, even if a user error occurred. Some ioctls return
496 *out_output = state->out_output;
497 talloc_steal(mem_ctx, out_output->data);
500 tevent_req_is_nterror(req, &status);
501 tevent_req_received(req);