CVE-2013-4408:s4:dcerpc_smb2: check for invalid frag_len in send_read_request_continue()
[Samba.git] / source4 / librpc / rpc / dcerpc_smb2.c
blob2b1c66e232fc64263d541521e97940f91dd61e3e
1 /*
2 Unix SMB/CIFS implementation.
4 dcerpc over SMB2 transport
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/composite/composite.h"
25 #include "libcli/smb2/smb2.h"
26 #include "libcli/smb2/smb2_calls.h"
27 #include "../libcli/smb/smb_constants.h"
28 #include "librpc/rpc/dcerpc.h"
29 #include "librpc/rpc/dcerpc_proto.h"
30 #include "librpc/rpc/rpc_common.h"
31 #include "../libcli/smb/smbXcli_base.h"
33 /* transport private information used by SMB2 pipe transport */
34 struct smb2_private {
35 struct smb2_handle handle;
36 struct smb2_tree *tree;
37 DATA_BLOB session_key;
38 const char *server_name;
39 bool dead;
44 tell the dcerpc layer that the transport is dead
46 static void pipe_dead(struct dcecli_connection *c, NTSTATUS status)
48 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
50 if (smb->dead) {
51 return;
54 smb->dead = true;
56 if (NT_STATUS_EQUAL(NT_STATUS_UNSUCCESSFUL, status)) {
57 status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
60 if (NT_STATUS_EQUAL(NT_STATUS_OK, status)) {
61 status = NT_STATUS_END_OF_FILE;
64 if (c->transport.recv_data) {
65 c->transport.recv_data(c, NULL, status);
70 /*
71 this holds the state of an in-flight call
73 struct smb2_read_state {
74 struct dcecli_connection *c;
75 DATA_BLOB data;
79 called when a read request has completed
81 static void smb2_read_callback(struct smb2_request *req)
83 struct dcecli_connection *c;
84 struct smb2_private *smb;
85 struct smb2_read_state *state;
86 struct smb2_read io;
87 uint16_t frag_length;
88 NTSTATUS status;
90 state = talloc_get_type(req->async.private_data, struct smb2_read_state);
91 smb = talloc_get_type(state->c->transport.private_data, struct smb2_private);
92 c = state->c;
94 status = smb2_read_recv(req, state, &io);
95 if (NT_STATUS_IS_ERR(status)) {
96 talloc_free(state);
97 pipe_dead(c, status);
98 return;
101 if (!data_blob_append(state, &state->data,
102 io.out.data.data, io.out.data.length)) {
103 talloc_free(state);
104 pipe_dead(c, NT_STATUS_NO_MEMORY);
105 return;
108 if (state->data.length < 16) {
109 DEBUG(0,("dcerpc_smb2: short packet (length %d) in read callback!\n",
110 (int)state->data.length));
111 talloc_free(state);
112 pipe_dead(c, NT_STATUS_INFO_LENGTH_MISMATCH);
113 return;
116 frag_length = dcerpc_get_frag_length(&state->data);
118 if (frag_length <= state->data.length) {
119 DATA_BLOB data = state->data;
120 talloc_steal(c, data.data);
121 talloc_free(state);
122 c->transport.recv_data(c, &data, NT_STATUS_OK);
123 return;
126 /* initiate another read request, as we only got part of a fragment */
127 ZERO_STRUCT(io);
128 io.in.file.handle = smb->handle;
129 io.in.length = MIN(state->c->srv_max_xmit_frag,
130 frag_length - state->data.length);
131 if (io.in.length < 16) {
132 io.in.length = 16;
135 req = smb2_read_send(smb->tree, &io);
136 if (req == NULL) {
137 talloc_free(state);
138 pipe_dead(c, NT_STATUS_NO_MEMORY);
139 return;
142 req->async.fn = smb2_read_callback;
143 req->async.private_data = state;
148 trigger a read request from the server, possibly with some initial
149 data in the read buffer
151 static NTSTATUS send_read_request_continue(struct dcecli_connection *c, DATA_BLOB *blob)
153 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
154 struct smb2_read io;
155 struct smb2_read_state *state;
156 struct smb2_request *req;
158 state = talloc(c, struct smb2_read_state);
159 if (state == NULL) {
160 return NT_STATUS_NO_MEMORY;
163 state->c = c;
164 if (blob == NULL) {
165 state->data = data_blob(NULL, 0);
166 } else {
167 state->data = *blob;
168 talloc_steal(state, state->data.data);
171 ZERO_STRUCT(io);
172 io.in.file.handle = smb->handle;
174 if (state->data.length >= 16) {
175 uint16_t frag_length = dcerpc_get_frag_length(&state->data);
177 if (frag_length < state->data.length) {
178 talloc_free(state);
179 return NT_STATUS_RPC_PROTOCOL_ERROR;
182 io.in.length = frag_length - state->data.length;
183 } else {
184 io.in.length = 0x2000;
187 req = smb2_read_send(smb->tree, &io);
188 if (req == NULL) {
189 return NT_STATUS_NO_MEMORY;
192 req->async.fn = smb2_read_callback;
193 req->async.private_data = state;
195 return NT_STATUS_OK;
200 trigger a read request from the server
202 static NTSTATUS send_read_request(struct dcecli_connection *c)
204 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
206 if (smb->dead) {
207 return NT_STATUS_CONNECTION_DISCONNECTED;
210 return send_read_request_continue(c, NULL);
214 this holds the state of an in-flight trans call
216 struct smb2_trans_state {
217 struct dcecli_connection *c;
221 called when a trans request has completed
223 static void smb2_trans_callback(struct smb2_request *req)
225 struct smb2_trans_state *state = talloc_get_type(req->async.private_data,
226 struct smb2_trans_state);
227 struct dcecli_connection *c = state->c;
228 NTSTATUS status;
229 struct smb2_ioctl io;
231 status = smb2_ioctl_recv(req, state, &io);
232 if (NT_STATUS_IS_ERR(status)) {
233 pipe_dead(c, status);
234 return;
237 if (!NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)) {
238 DATA_BLOB data = io.out.out;
239 talloc_steal(c, data.data);
240 talloc_free(state);
241 c->transport.recv_data(c, &data, NT_STATUS_OK);
242 return;
245 /* there is more to receive - setup a read */
246 send_read_request_continue(c, &io.out.out);
247 talloc_free(state);
251 send a SMBtrans style request, using a named pipe read_write fsctl
253 static NTSTATUS smb2_send_trans_request(struct dcecli_connection *c, DATA_BLOB *blob)
255 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
256 struct smb2_private);
257 struct smb2_ioctl io;
258 struct smb2_trans_state *state;
259 struct smb2_request *req;
261 state = talloc(smb, struct smb2_trans_state);
262 if (state == NULL) {
263 return NT_STATUS_NO_MEMORY;
266 state->c = c;
268 ZERO_STRUCT(io);
269 io.in.file.handle = smb->handle;
270 io.in.function = FSCTL_NAMED_PIPE_READ_WRITE;
271 io.in.max_response_size = 0x2000;
272 io.in.flags = 1;
273 io.in.out = *blob;
275 req = smb2_ioctl_send(smb->tree, &io);
276 if (req == NULL) {
277 talloc_free(state);
278 return NT_STATUS_NO_MEMORY;
281 req->async.fn = smb2_trans_callback;
282 req->async.private_data = state;
284 talloc_steal(state, req);
286 return NT_STATUS_OK;
290 called when a write request has completed
292 static void smb2_write_callback(struct smb2_request *req)
294 struct dcecli_connection *c = (struct dcecli_connection *)req->async.private_data;
295 struct smb2_write io;
296 NTSTATUS status;
298 ZERO_STRUCT(io);
300 status = smb2_write_recv(req, &io);
301 if (!NT_STATUS_IS_OK(status)) {
302 DEBUG(0,("dcerpc_smb2: write callback error: %s\n",
303 nt_errstr(status)));
304 pipe_dead(c, status);
309 send a packet to the server
311 static NTSTATUS smb2_send_request(struct dcecli_connection *c, DATA_BLOB *blob,
312 bool trigger_read)
314 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
315 struct smb2_write io;
316 struct smb2_request *req;
318 if (smb->dead) {
319 return NT_STATUS_CONNECTION_DISCONNECTED;
322 if (trigger_read) {
323 return smb2_send_trans_request(c, blob);
326 ZERO_STRUCT(io);
327 io.in.file.handle = smb->handle;
328 io.in.data = *blob;
330 req = smb2_write_send(smb->tree, &io);
331 if (req == NULL) {
332 return NT_STATUS_NO_MEMORY;
335 req->async.fn = smb2_write_callback;
336 req->async.private_data = c;
338 return NT_STATUS_OK;
341 static void free_request(struct smb2_request *req)
343 talloc_free(req);
347 shutdown SMB pipe connection
349 static NTSTATUS smb2_shutdown_pipe(struct dcecli_connection *c, NTSTATUS status)
351 struct smb2_private *smb = (struct smb2_private *)c->transport.private_data;
352 struct smb2_close io;
353 struct smb2_request *req;
355 /* maybe we're still starting up */
356 if (!smb) return status;
358 ZERO_STRUCT(io);
359 io.in.file.handle = smb->handle;
360 req = smb2_close_send(smb->tree, &io);
361 if (req != NULL) {
362 /* we don't care if this fails, so just free it if it succeeds */
363 req->async.fn = free_request;
366 talloc_free(smb);
368 return status;
372 return SMB server name
374 static const char *smb2_peer_name(struct dcecli_connection *c)
376 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
377 struct smb2_private);
378 return smb->server_name;
382 return remote name we make the actual connection (good for kerberos)
384 static const char *smb2_target_hostname(struct dcecli_connection *c)
386 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
387 struct smb2_private);
388 return smbXcli_conn_remote_name(smb->tree->session->transport->conn);
392 fetch the user session key
394 static NTSTATUS smb2_session_key(struct dcecli_connection *c, DATA_BLOB *session_key)
396 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
397 struct smb2_private);
399 if (smb == NULL) return NT_STATUS_CONNECTION_DISCONNECTED;
401 if (smb->session_key.length == 0) {
402 return NT_STATUS_NO_USER_SESSION_KEY;
405 *session_key = smb->session_key;
406 return NT_STATUS_OK;
409 struct pipe_open_smb2_state {
410 struct dcecli_connection *c;
411 struct composite_context *ctx;
414 static void pipe_open_recv(struct smb2_request *req);
416 struct composite_context *dcerpc_pipe_open_smb2_send(struct dcerpc_pipe *p,
417 struct smb2_tree *tree,
418 const char *pipe_name)
420 struct composite_context *ctx;
421 struct pipe_open_smb2_state *state;
422 struct smb2_create io;
423 struct smb2_request *req;
424 struct dcecli_connection *c = p->conn;
426 /* if we don't have a binding on this pipe yet, then create one */
427 if (p->binding == NULL) {
428 NTSTATUS status;
429 const char *r = smbXcli_conn_remote_name(tree->session->transport->conn);
430 char *s;
431 SMB_ASSERT(r != NULL);
432 s = talloc_asprintf(p, "ncacn_np:%s", r);
433 if (s == NULL) return NULL;
434 status = dcerpc_parse_binding(p, s, &p->binding);
435 talloc_free(s);
436 if (!NT_STATUS_IS_OK(status)) {
437 return NULL;
441 ctx = composite_create(c, c->event_ctx);
442 if (ctx == NULL) return NULL;
444 state = talloc(ctx, struct pipe_open_smb2_state);
445 if (composite_nomem(state, ctx)) return ctx;
446 ctx->private_data = state;
448 state->c = c;
449 state->ctx = ctx;
451 ZERO_STRUCT(io);
452 io.in.desired_access =
453 SEC_STD_READ_CONTROL |
454 SEC_FILE_READ_ATTRIBUTE |
455 SEC_FILE_WRITE_ATTRIBUTE |
456 SEC_STD_SYNCHRONIZE |
457 SEC_FILE_READ_EA |
458 SEC_FILE_WRITE_EA |
459 SEC_FILE_READ_DATA |
460 SEC_FILE_WRITE_DATA |
461 SEC_FILE_APPEND_DATA;
462 io.in.share_access =
463 NTCREATEX_SHARE_ACCESS_READ |
464 NTCREATEX_SHARE_ACCESS_WRITE;
465 io.in.create_disposition = NTCREATEX_DISP_OPEN;
466 io.in.create_options =
467 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
468 NTCREATEX_OPTIONS_NO_RECALL;
469 io.in.impersonation_level = NTCREATEX_IMPERSONATION_IMPERSONATION;
471 if ((strncasecmp(pipe_name, "/pipe/", 6) == 0) ||
472 (strncasecmp(pipe_name, "\\pipe\\", 6) == 0)) {
473 pipe_name += 6;
475 io.in.fname = pipe_name;
477 req = smb2_create_send(tree, &io);
478 composite_continue_smb2(ctx, req, pipe_open_recv, state);
479 return ctx;
482 static void pipe_open_recv(struct smb2_request *req)
484 struct pipe_open_smb2_state *state =
485 talloc_get_type(req->async.private_data,
486 struct pipe_open_smb2_state);
487 struct composite_context *ctx = state->ctx;
488 struct dcecli_connection *c = state->c;
489 struct smb2_tree *tree = req->tree;
490 struct smb2_private *smb;
491 struct smb2_create io;
493 ctx->status = smb2_create_recv(req, state, &io);
494 if (!composite_is_ok(ctx)) return;
497 fill in the transport methods
499 c->transport.transport = NCACN_NP;
500 c->transport.private_data = NULL;
501 c->transport.shutdown_pipe = smb2_shutdown_pipe;
502 c->transport.peer_name = smb2_peer_name;
503 c->transport.target_hostname = smb2_target_hostname;
505 c->transport.send_request = smb2_send_request;
506 c->transport.send_read = send_read_request;
507 c->transport.recv_data = NULL;
509 /* Over-ride the default session key with the SMB session key */
510 c->security_state.session_key = smb2_session_key;
512 smb = talloc(c, struct smb2_private);
513 if (composite_nomem(smb, ctx)) return;
515 smb->handle = io.out.file.handle;
516 smb->tree = talloc_reference(smb, tree);
517 smb->server_name= strupper_talloc(smb,
518 smbXcli_conn_remote_name(tree->session->transport->conn));
519 if (composite_nomem(smb->server_name, ctx)) return;
520 smb->dead = false;
522 ctx->status = smbXcli_session_application_key(tree->session->smbXcli,
523 smb, &smb->session_key);
524 if (NT_STATUS_EQUAL(ctx->status, NT_STATUS_NO_USER_SESSION_KEY)) {
525 smb->session_key = data_blob_null;
526 ctx->status = NT_STATUS_OK;
528 if (!composite_is_ok(ctx)) return;
530 c->transport.private_data = smb;
532 composite_done(ctx);
535 NTSTATUS dcerpc_pipe_open_smb2_recv(struct composite_context *c)
537 NTSTATUS status = composite_wait(c);
538 talloc_free(c);
539 return status;
542 NTSTATUS dcerpc_pipe_open_smb2(struct dcerpc_pipe *p,
543 struct smb2_tree *tree,
544 const char *pipe_name)
546 struct composite_context *ctx = dcerpc_pipe_open_smb2_send(p, tree, pipe_name);
547 return dcerpc_pipe_open_smb2_recv(ctx);
551 return the SMB2 tree used for a dcerpc over SMB2 pipe
553 struct smb2_tree *dcerpc_smb2_tree(struct dcecli_connection *c)
555 struct smb2_private *smb = talloc_get_type(c->transport.private_data,
556 struct smb2_private);
557 return smb->tree;