s3:smbd: implement FSCTL_SMBTORTURE_FORCE_UNACKED_TIMEOUT
[gd/samba-autobuild/.git] / source3 / smbd / smb2_ioctl.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
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"
29
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_SMB2
32
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,
37                                                uint32_t in_ctl_code,
38                                                DATA_BLOB in_input,
39                                                uint32_t in_max_output,
40                                                uint32_t in_flags);
41 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
42                                      TALLOC_CTX *mem_ctx,
43                                      DATA_BLOB *out_output,
44                                      bool *disconnect);
45
46 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq);
47 NTSTATUS smbd_smb2_request_process_ioctl(struct smbd_smb2_request *req)
48 {
49         NTSTATUS status;
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;
56         uint32_t in_ctl_code;
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;
68         uint32_t in_flags;
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;
74
75         status = smbd_smb2_request_verify_sizes(req, 0x39);
76         if (!NT_STATUS_IS_OK(status)) {
77                 return smbd_smb2_request_error(req, status);
78         }
79         inbody = SMBD_SMB2_IN_BODY_PTR(req);
80
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);
91
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;
95
96         /*
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.
103          */
104         allowed_length_in = 0;
105         if ((in_input_offset > 0) && (in_input_length > 0)) {
106                 uint32_t tmp_ofs;
107
108                 if (in_input_offset < min_buffer_offset) {
109                         return smbd_smb2_request_error(req,
110                                         NT_STATUS_INVALID_PARAMETER);
111                 }
112                 if (in_input_offset > max_buffer_offset) {
113                         return smbd_smb2_request_error(req,
114                                         NT_STATUS_INVALID_PARAMETER);
115                 }
116                 allowed_length_in = max_buffer_offset - in_input_offset;
117
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;
124         }
125
126         if (in_input_length > allowed_length_in) {
127                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
128         }
129
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);
135                 }
136                 if (in_output_offset > max_buffer_offset) {
137                         return smbd_smb2_request_error(req,
138                                         NT_STATUS_INVALID_PARAMETER);
139                 }
140                 allowed_length_out = max_buffer_offset - in_output_offset;
141         }
142
143         if (in_output_length > allowed_length_out) {
144                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
145         }
146
147         if (in_output_length > 0) {
148                 uint32_t tmp_ofs;
149
150                 if (in_output_offset < min_output_offset) {
151                         return smbd_smb2_request_error(req,
152                                         NT_STATUS_INVALID_PARAMETER);
153                 }
154
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;
159         }
160
161         /*
162          * verify the credits and avoid overflows
163          * in_input_buffer.length and in_output_buffer.length
164          * are already verified.
165          */
166         data_length_in = in_input_buffer.length + in_output_buffer.length;
167
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);
172         }
173         data_length_out += in_max_output_length;
174
175         data_length_max = MAX(data_length_in, data_length_out);
176
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);
180         }
181
182         /*
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.
185          */
186         if (in_flags != SMB2_IOCTL_FLAG_IS_FSCTL) {
187                 return smbd_smb2_request_error(req, NT_STATUS_NOT_SUPPORTED);
188         }
189
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:
198                 /*
199                  * Some SMB2 specific CtlCodes like FSCTL_DFS_GET_REFERRALS or
200                  * FSCTL_PIPE_WAIT does not take a file handle.
201                  *
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.
205                  */
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);
210                 }
211                 break;
212         default:
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);
217                 }
218                 break;
219         }
220
221         subreq = smbd_smb2_ioctl_send(req, req->sconn->ev_ctx,
222                                       req, in_fsp,
223                                       in_ctl_code,
224                                       in_input_buffer,
225                                       in_max_output_length,
226                                       in_flags);
227         if (subreq == NULL) {
228                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
229         }
230         tevent_req_set_callback(subreq, smbd_smb2_request_ioctl_done, req);
231
232         return smbd_smb2_request_pending_queue(req, subreq, 1000);
233 }
234
235 /*
236  * 3.3.4.4 Sending an Error Response
237  * An error code other than one of the following indicates a failure:
238  */
239 static bool smbd_smb2_ioctl_is_failure(uint32_t ctl_code, NTSTATUS status,
240                                        size_t data_size)
241 {
242         if (NT_STATUS_IS_OK(status)) {
243                 return false;
244         }
245
246         /*
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>
249          */
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))) {
254                 return false;
255         }
256
257         /*
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.
261          */
262         if (((ctl_code == FSCTL_SRV_COPYCHUNK)
263                                 || (ctl_code == FSCTL_SRV_COPYCHUNK_WRITE))
264          && (data_size == sizeof(struct srv_copychunk_rsp))) {
265                 return false;
266         }
267
268         return true;
269 }
270
271 static void smbd_smb2_request_ioctl_done(struct tevent_req *subreq)
272 {
273         struct smbd_smb2_request *req = tevent_req_callback_data(subreq,
274                                         struct smbd_smb2_request);
275         const uint8_t *inbody;
276         DATA_BLOB outbody;
277         DATA_BLOB outdyn;
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;
284         NTSTATUS status;
285         NTSTATUS error; /* transport error */
286         bool disconnect = false;
287
288         status = smbd_smb2_ioctl_recv(subreq, req,
289                                       &out_output_buffer,
290                                       &disconnect);
291
292         DEBUG(10,("smbd_smb2_request_ioctl_done: smbd_smb2_ioctl_recv returned "
293                 "%u status %s\n",
294                 (unsigned int)out_output_buffer.length,
295                 nt_errstr(status) ));
296
297         TALLOC_FREE(subreq);
298         if (disconnect) {
299                 error = status;
300                 smbd_server_connection_terminate(req->xconn,
301                                                  nt_errstr(error));
302                 return;
303         }
304
305         inbody = SMBD_SMB2_IN_BODY_PTR(req);
306
307         in_ctl_code             = IVAL(inbody, 0x04);
308         in_file_id_persistent   = BVAL(inbody, 0x08);
309         in_file_id_volatile     = BVAL(inbody, 0x10);
310
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,
316                                                          nt_errstr(error));
317                         return;
318                 }
319                 return;
320         }
321
322         out_input_offset = SMB2_HDR_BODY + 0x30;
323         out_output_offset = SMB2_HDR_BODY + 0x30;
324
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,
330                                                          nt_errstr(error));
331                         return;
332                 }
333                 return;
334         }
335
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 */
353
354         /*
355          * Note: Windows Vista and 2008 send back also the
356          *       input from the request. But it was fixed in
357          *       Windows 7.
358          */
359         outdyn = out_output_buffer;
360
361         error = smbd_smb2_request_done_ex(req, status, outbody, &outdyn,
362                                           __location__);
363         if (!NT_STATUS_IS_OK(error)) {
364                 smbd_server_connection_terminate(req->xconn,
365                                                  nt_errstr(error));
366                 return;
367         }
368 }
369
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)
374 {
375         NTSTATUS status;
376         bool ok;
377
378         ok = lp_parm_bool(-1, "smbd", "FSCTL_SMBTORTURE", false);
379         if (!ok) {
380                 goto not_supported;
381         }
382
383         switch (ctl_code) {
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);
388                 }
389
390                 state->smb2req->xconn->ack.force_unacked_timeout = true;
391                 tevent_req_done(req);
392                 return tevent_req_post(req, ev);
393
394         default:
395                 goto not_supported;
396         }
397
398 not_supported:
399         if (IS_IPC(state->smbreq->conn)) {
400                 status = NT_STATUS_FS_DRIVER_REQUIRED;
401         } else {
402                 status = NT_STATUS_INVALID_DEVICE_REQUEST;
403         }
404
405         tevent_req_nterror(req, status);
406         return tevent_req_post(req, ev);
407 }
408
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,
414                                                DATA_BLOB in_input,
415                                                uint32_t in_max_output,
416                                                uint32_t in_flags)
417 {
418         struct tevent_req *req;
419         struct smbd_smb2_ioctl_state *state;
420         struct smb_request *smbreq;
421
422         req = tevent_req_create(mem_ctx, &state,
423                                 struct smbd_smb2_ioctl_state);
424         if (req == NULL) {
425                 return NULL;
426         }
427         state->smb2req = smb2req;
428         state->smbreq = NULL;
429         state->fsp = fsp;
430         state->in_input = in_input;
431         state->in_max_output = in_max_output;
432         state->out_output = data_blob_null;
433
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>",
437                    fsp_fnum_dbg(fsp)));
438
439         smbreq = smbd_smb2_fake_smb_request(smb2req);
440         if (tevent_req_nomem(smbreq, req)) {
441                 return tevent_req_post(req, ev);
442         }
443         state->smbreq = smbreq;
444
445         switch (in_ctl_code & IOCTL_DEV_TYPE_MASK) {
446         case FSCTL_DFS:
447                 return smb2_ioctl_dfs(in_ctl_code, ev, req, state);
448                 break;
449         case FSCTL_FILESYSTEM:
450                 return smb2_ioctl_filesys(in_ctl_code, ev, req, state);
451                 break;
452         case FSCTL_NAMED_PIPE:
453                 return smb2_ioctl_named_pipe(in_ctl_code, ev, req, state);
454                 break;
455         case FSCTL_NETWORK_FILESYSTEM:
456                 return smb2_ioctl_network_fs(in_ctl_code, ev, req, state);
457                 break;
458         case FSCTL_SMBTORTURE:
459                 return smb2_ioctl_smbtorture(in_ctl_code, ev, req, state);
460                 break;
461         default:
462                 if (IS_IPC(smbreq->conn)) {
463                         tevent_req_nterror(req, NT_STATUS_FS_DRIVER_REQUIRED);
464                 } else {
465                         tevent_req_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
466                 }
467
468                 return tevent_req_post(req, ev);
469                 break;
470         }
471
472         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
473         return tevent_req_post(req, ev);
474 }
475
476 static NTSTATUS smbd_smb2_ioctl_recv(struct tevent_req *req,
477                                      TALLOC_CTX *mem_ctx,
478                                      DATA_BLOB *out_output,
479                                      bool *disconnect)
480 {
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;
485         uint64_t err;
486
487         *disconnect = state->disconnect;
488
489         if ((tevent_req_is_error(req, &req_state, &err) == false)
490          || (req_state == TEVENT_REQ_USER_ERROR)) {
491                 /*
492                  * Return output buffer to caller if the ioctl was successfully
493                  * processed, even if a user error occurred. Some ioctls return
494                  * data on failure.
495                  */
496                 *out_output = state->out_output;
497                 talloc_steal(mem_ctx, out_output->data);
498         }
499
500         tevent_req_is_nterror(req, &status);
501         tevent_req_received(req);
502         return status;
503 }