CVE-2015-5370: s3:librpc/rpc: remove auth trailer and possible padding within dcerpc_...
[Samba.git] / source3 / librpc / rpc / dcerpc_helpers.c
blobbb1da467ccc55766b47c52c25563363b557c4fdb
1 /*
2 * DCERPC Helper routines
3 * Günther Deschner <gd@samba.org> 2010.
4 * Simo Sorce <idra@samba.org> 2010.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "librpc/rpc/dcerpc.h"
23 #include "librpc/gen_ndr/ndr_dcerpc.h"
24 #include "librpc/crypto/gse.h"
25 #include "auth/gensec/gensec.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_RPC_PARSE
30 /**
31 * @brief NDR Encodes a ncacn_packet
33 * @param mem_ctx The memory context the blob will be allocated on
34 * @param ptype The DCERPC packet type
35 * @param pfc_flags The DCERPC PFC Falgs
36 * @param auth_length The length of the trailing auth blob
37 * @param call_id The call ID
38 * @param u The payload of the packet
39 * @param blob [out] The encoded blob if successful
41 * @return an NTSTATUS error code
43 NTSTATUS dcerpc_push_ncacn_packet(TALLOC_CTX *mem_ctx,
44 enum dcerpc_pkt_type ptype,
45 uint8_t pfc_flags,
46 uint16_t auth_length,
47 uint32_t call_id,
48 union dcerpc_payload *u,
49 DATA_BLOB *blob)
51 struct ncacn_packet r;
52 enum ndr_err_code ndr_err;
54 r.rpc_vers = 5;
55 r.rpc_vers_minor = 0;
56 r.ptype = ptype;
57 r.pfc_flags = pfc_flags;
58 r.drep[0] = DCERPC_DREP_LE;
59 r.drep[1] = 0;
60 r.drep[2] = 0;
61 r.drep[3] = 0;
62 r.auth_length = auth_length;
63 r.call_id = call_id;
64 r.u = *u;
66 ndr_err = ndr_push_struct_blob(blob, mem_ctx, &r,
67 (ndr_push_flags_fn_t)ndr_push_ncacn_packet);
68 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
69 return ndr_map_error2ntstatus(ndr_err);
72 dcerpc_set_frag_length(blob, blob->length);
75 if (DEBUGLEVEL >= 10) {
76 /* set frag len for print function */
77 r.frag_length = blob->length;
78 NDR_PRINT_DEBUG(ncacn_packet, &r);
81 return NT_STATUS_OK;
84 /**
85 * @brief Decodes a ncacn_packet
87 * @param mem_ctx The memory context on which to allocate the packet
88 * elements
89 * @param blob The blob of data to decode
90 * @param r An empty ncacn_packet, must not be NULL
91 * @param bigendian Whether the packet is bignedian encoded
93 * @return a NTSTATUS error code
95 NTSTATUS dcerpc_pull_ncacn_packet(TALLOC_CTX *mem_ctx,
96 const DATA_BLOB *blob,
97 struct ncacn_packet *r,
98 bool bigendian)
100 enum ndr_err_code ndr_err;
101 struct ndr_pull *ndr;
103 ndr = ndr_pull_init_blob(blob, mem_ctx);
104 if (!ndr) {
105 return NT_STATUS_NO_MEMORY;
107 if (bigendian) {
108 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
111 if (CVAL(blob->data, DCERPC_PFC_OFFSET) & DCERPC_PFC_FLAG_OBJECT_UUID) {
112 ndr->flags |= LIBNDR_FLAG_OBJECT_PRESENT;
115 ndr_err = ndr_pull_ncacn_packet(ndr, NDR_SCALARS|NDR_BUFFERS, r);
117 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
118 talloc_free(ndr);
119 return ndr_map_error2ntstatus(ndr_err);
121 talloc_free(ndr);
123 if (DEBUGLEVEL >= 10) {
124 NDR_PRINT_DEBUG(ncacn_packet, r);
127 if (r->frag_length != blob->length) {
128 return NT_STATUS_RPC_PROTOCOL_ERROR;
131 return NT_STATUS_OK;
135 * @brief NDR Encodes a dcerpc_auth structure
137 * @param mem_ctx The memory context the blob will be allocated on
138 * @param auth_type The DCERPC Authentication Type
139 * @param auth_level The DCERPC Authentication Level
140 * @param auth_pad_length The padding added to the packet this blob will be
141 * appended to.
142 * @param auth_context_id The context id
143 * @param credentials The authentication credentials blob (signature)
144 * @param blob [out] The encoded blob if successful
146 * @return a NTSTATUS error code
148 NTSTATUS dcerpc_push_dcerpc_auth(TALLOC_CTX *mem_ctx,
149 enum dcerpc_AuthType auth_type,
150 enum dcerpc_AuthLevel auth_level,
151 uint8_t auth_pad_length,
152 uint32_t auth_context_id,
153 const DATA_BLOB *credentials,
154 DATA_BLOB *blob)
156 struct dcerpc_auth r;
157 enum ndr_err_code ndr_err;
159 r.auth_type = auth_type;
160 r.auth_level = auth_level;
161 r.auth_pad_length = auth_pad_length;
162 r.auth_reserved = 0;
163 r.auth_context_id = auth_context_id;
164 r.credentials = *credentials;
166 ndr_err = ndr_push_struct_blob(blob, mem_ctx, &r,
167 (ndr_push_flags_fn_t)ndr_push_dcerpc_auth);
168 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
169 return ndr_map_error2ntstatus(ndr_err);
172 if (DEBUGLEVEL >= 10) {
173 NDR_PRINT_DEBUG(dcerpc_auth, &r);
176 return NT_STATUS_OK;
180 * @brief Decodes a dcerpc_auth blob
182 * @param mem_ctx The memory context on which to allocate the packet
183 * elements
184 * @param blob The blob of data to decode
185 * @param r An empty dcerpc_auth structure, must not be NULL
187 * @return a NTSTATUS error code
189 NTSTATUS dcerpc_pull_dcerpc_auth(TALLOC_CTX *mem_ctx,
190 const DATA_BLOB *blob,
191 struct dcerpc_auth *r,
192 bool bigendian)
194 enum ndr_err_code ndr_err;
195 struct ndr_pull *ndr;
197 ndr = ndr_pull_init_blob(blob, mem_ctx);
198 if (!ndr) {
199 return NT_STATUS_NO_MEMORY;
201 if (bigendian) {
202 ndr->flags |= LIBNDR_FLAG_BIGENDIAN;
205 ndr_err = ndr_pull_dcerpc_auth(ndr, NDR_SCALARS|NDR_BUFFERS, r);
207 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
208 talloc_free(ndr);
209 return ndr_map_error2ntstatus(ndr_err);
211 talloc_free(ndr);
213 if (DEBUGLEVEL >= 10) {
214 NDR_PRINT_DEBUG(dcerpc_auth, r);
217 return NT_STATUS_OK;
221 * @brief Calculate how much data we can in a packet, including calculating
222 * auth token and pad lengths.
224 * @param auth The pipe_auth_data structure for this pipe.
225 * @param header_len The length of the packet header
226 * @param data_left The data left in the send buffer
227 * @param max_xmit_frag The max fragment size.
228 * @param data_to_send [out] The max data we will send in the pdu
229 * @param frag_len [out] The total length of the fragment
230 * @param auth_len [out] The length of the auth trailer
231 * @param pad_len [out] The padding to be applied
233 * @return A NT Error status code.
235 NTSTATUS dcerpc_guess_sizes(struct pipe_auth_data *auth,
236 size_t header_len, size_t data_left,
237 size_t max_xmit_frag,
238 size_t *data_to_send, size_t *frag_len,
239 size_t *auth_len, size_t *pad_len)
241 size_t max_len;
242 size_t mod_len;
243 struct gensec_security *gensec_security;
245 /* no auth token cases first */
246 switch (auth->auth_level) {
247 case DCERPC_AUTH_LEVEL_NONE:
248 case DCERPC_AUTH_LEVEL_CONNECT:
249 case DCERPC_AUTH_LEVEL_PACKET:
250 max_len = max_xmit_frag - header_len;
251 *data_to_send = MIN(max_len, data_left);
252 *pad_len = 0;
253 *auth_len = 0;
254 *frag_len = header_len + *data_to_send;
255 return NT_STATUS_OK;
257 case DCERPC_AUTH_LEVEL_PRIVACY:
258 break;
260 case DCERPC_AUTH_LEVEL_INTEGRITY:
261 break;
263 default:
264 return NT_STATUS_INVALID_PARAMETER;
268 /* Sign/seal case, calculate auth and pad lengths */
270 max_len = max_xmit_frag - header_len - DCERPC_AUTH_TRAILER_LENGTH;
272 /* Treat the same for all authenticated rpc requests. */
273 switch (auth->auth_type) {
274 case DCERPC_AUTH_TYPE_SPNEGO:
275 case DCERPC_AUTH_TYPE_NTLMSSP:
276 case DCERPC_AUTH_TYPE_KRB5:
277 case DCERPC_AUTH_TYPE_SCHANNEL:
278 gensec_security = auth->auth_ctx;
279 mod_len = (max_len % DCERPC_AUTH_PAD_ALIGNMENT);
280 *auth_len = gensec_sig_size(gensec_security, max_len - mod_len);
281 if (*auth_len == 0) {
282 return NT_STATUS_INTERNAL_ERROR;
284 break;
285 default:
286 return NT_STATUS_INVALID_PARAMETER;
289 max_len -= *auth_len;
290 mod_len = (max_len % DCERPC_AUTH_PAD_ALIGNMENT);
291 max_len -= mod_len;
293 *data_to_send = MIN(max_len, data_left);
295 *pad_len = DCERPC_AUTH_PAD_LENGTH(*data_to_send);
297 *frag_len = header_len + *data_to_send + *pad_len
298 + DCERPC_AUTH_TRAILER_LENGTH + *auth_len;
300 return NT_STATUS_OK;
303 /*******************************************************************
304 Create and add the NTLMSSP sign/seal auth data.
305 ********************************************************************/
307 static NTSTATUS add_generic_auth_footer(struct gensec_security *gensec_security,
308 enum dcerpc_AuthLevel auth_level,
309 DATA_BLOB *rpc_out)
311 uint16_t data_and_pad_len = rpc_out->length
312 - DCERPC_RESPONSE_LENGTH
313 - DCERPC_AUTH_TRAILER_LENGTH;
314 DATA_BLOB auth_blob;
315 NTSTATUS status;
317 if (!gensec_security) {
318 return NT_STATUS_INVALID_PARAMETER;
321 switch (auth_level) {
322 case DCERPC_AUTH_LEVEL_PRIVACY:
323 /* Data portion is encrypted. */
324 status = gensec_seal_packet(gensec_security,
325 rpc_out->data,
326 rpc_out->data
327 + DCERPC_RESPONSE_LENGTH,
328 data_and_pad_len,
329 rpc_out->data,
330 rpc_out->length,
331 &auth_blob);
332 if (!NT_STATUS_IS_OK(status)) {
333 return status;
335 break;
337 case DCERPC_AUTH_LEVEL_INTEGRITY:
338 /* Data is signed. */
339 status = gensec_sign_packet(gensec_security,
340 rpc_out->data,
341 rpc_out->data
342 + DCERPC_RESPONSE_LENGTH,
343 data_and_pad_len,
344 rpc_out->data,
345 rpc_out->length,
346 &auth_blob);
347 if (!NT_STATUS_IS_OK(status)) {
348 return status;
350 break;
352 default:
353 /* Can't happen. */
354 smb_panic("bad auth level");
355 /* Notreached. */
356 return NT_STATUS_INVALID_PARAMETER;
359 /* Finally attach the blob. */
360 if (!data_blob_append(NULL, rpc_out,
361 auth_blob.data, auth_blob.length)) {
362 DEBUG(0, ("Failed to add %u bytes auth blob.\n",
363 (unsigned int)auth_blob.length));
364 return NT_STATUS_NO_MEMORY;
366 data_blob_free(&auth_blob);
368 return NT_STATUS_OK;
371 /*******************************************************************
372 Check/unseal the NTLMSSP auth data. (Unseal in place).
373 ********************************************************************/
375 static NTSTATUS get_generic_auth_footer(struct gensec_security *gensec_security,
376 enum dcerpc_AuthLevel auth_level,
377 DATA_BLOB *data, DATA_BLOB *full_pkt,
378 DATA_BLOB *auth_token)
380 if (gensec_security == NULL) {
381 return NT_STATUS_INVALID_PARAMETER;
384 switch (auth_level) {
385 case DCERPC_AUTH_LEVEL_PRIVACY:
386 /* Data portion is encrypted. */
387 return gensec_unseal_packet(gensec_security,
388 data->data,
389 data->length,
390 full_pkt->data,
391 full_pkt->length,
392 auth_token);
394 case DCERPC_AUTH_LEVEL_INTEGRITY:
395 /* Data is signed. */
396 return gensec_check_packet(gensec_security,
397 data->data,
398 data->length,
399 full_pkt->data,
400 full_pkt->length,
401 auth_token);
403 default:
404 return NT_STATUS_INVALID_PARAMETER;
409 * @brief Append an auth footer according to what is the current mechanism
411 * @param auth The pipe_auth_data associated with the connection
412 * @param pad_len The padding used in the packet
413 * @param rpc_out Packet blob up to and including the auth header
415 * @return A NTSTATUS error code.
417 NTSTATUS dcerpc_add_auth_footer(struct pipe_auth_data *auth,
418 size_t pad_len, DATA_BLOB *rpc_out)
420 struct gensec_security *gensec_security;
421 const char pad[DCERPC_AUTH_PAD_ALIGNMENT] = { 0, };
422 DATA_BLOB auth_info;
423 DATA_BLOB auth_blob;
424 NTSTATUS status;
426 if (auth->auth_type == DCERPC_AUTH_TYPE_NONE) {
427 return NT_STATUS_OK;
430 if (pad_len) {
431 SMB_ASSERT(pad_len <= ARRAY_SIZE(pad));
433 /* Copy the sign/seal padding data. */
434 if (!data_blob_append(NULL, rpc_out, pad, pad_len)) {
435 return NT_STATUS_NO_MEMORY;
439 /* marshall the dcerpc_auth with an actually empty auth_blob.
440 * This is needed because the ntmlssp signature includes the
441 * auth header. We will append the actual blob later. */
442 auth_blob = data_blob_null;
443 status = dcerpc_push_dcerpc_auth(rpc_out->data,
444 auth->auth_type,
445 auth->auth_level,
446 pad_len,
447 1 /* context id. */,
448 &auth_blob,
449 &auth_info);
450 if (!NT_STATUS_IS_OK(status)) {
451 return status;
454 /* append the header */
455 if (!data_blob_append(NULL, rpc_out,
456 auth_info.data, auth_info.length)) {
457 DEBUG(0, ("Failed to add %u bytes auth blob.\n",
458 (unsigned int)auth_info.length));
459 return NT_STATUS_NO_MEMORY;
461 data_blob_free(&auth_info);
463 /* Generate any auth sign/seal and add the auth footer. */
464 switch (auth->auth_type) {
465 case DCERPC_AUTH_TYPE_NONE:
466 status = NT_STATUS_OK;
467 break;
468 default:
469 gensec_security = auth->auth_ctx;
470 status = add_generic_auth_footer(gensec_security,
471 auth->auth_level,
472 rpc_out);
473 break;
476 return status;
480 * @brief Check authentication for request/response packets
482 * @param auth The auth data for the connection
483 * @param pkt The actual ncacn_packet
484 * @param pkt_trailer [in][out] The stub_and_verifier part of the packet,
485 * the auth_trailer and padding will be removed.
486 * @param header_size The header size
487 * @param raw_pkt The whole raw packet data blob
489 * @return A NTSTATUS error code
491 NTSTATUS dcerpc_check_auth(struct pipe_auth_data *auth,
492 struct ncacn_packet *pkt,
493 DATA_BLOB *pkt_trailer,
494 uint8_t header_size,
495 DATA_BLOB *raw_pkt)
497 struct gensec_security *gensec_security;
498 NTSTATUS status;
499 struct dcerpc_auth auth_info;
500 uint32_t auth_length;
501 DATA_BLOB full_pkt;
502 DATA_BLOB data;
505 * These check should be done in the caller.
507 SMB_ASSERT(raw_pkt->length == pkt->frag_length);
508 SMB_ASSERT(header_size <= pkt->frag_length);
509 SMB_ASSERT(pkt_trailer->length < pkt->frag_length);
510 SMB_ASSERT((pkt_trailer->length + header_size) <= pkt->frag_length);
512 switch (auth->auth_level) {
513 case DCERPC_AUTH_LEVEL_PRIVACY:
514 DEBUG(10, ("Requested Privacy.\n"));
515 break;
517 case DCERPC_AUTH_LEVEL_INTEGRITY:
518 DEBUG(10, ("Requested Integrity.\n"));
519 break;
521 case DCERPC_AUTH_LEVEL_CONNECT:
522 if (pkt->auth_length != 0) {
523 break;
525 return NT_STATUS_OK;
527 case DCERPC_AUTH_LEVEL_NONE:
528 if (pkt->auth_length != 0) {
529 DEBUG(3, ("Got non-zero auth len on non "
530 "authenticated connection!\n"));
531 return NT_STATUS_INVALID_PARAMETER;
533 return NT_STATUS_OK;
535 default:
536 DEBUG(3, ("Unimplemented Auth Level %d",
537 auth->auth_level));
538 return NT_STATUS_INVALID_PARAMETER;
541 if (pkt->auth_length == 0) {
542 return NT_STATUS_INVALID_PARAMETER;
545 status = dcerpc_pull_auth_trailer(pkt, pkt, pkt_trailer,
546 &auth_info, &auth_length, false);
547 if (!NT_STATUS_IS_OK(status)) {
548 return status;
551 pkt_trailer->length -= auth_length;
552 data = data_blob_const(raw_pkt->data + header_size,
553 pkt_trailer->length);
554 full_pkt = data_blob_const(raw_pkt->data, raw_pkt->length);
555 full_pkt.length -= auth_info.credentials.length;
557 switch (auth->auth_type) {
558 case DCERPC_AUTH_TYPE_NONE:
559 return NT_STATUS_OK;
561 default:
562 DEBUG(10, ("GENSEC auth\n"));
564 gensec_security = auth->auth_ctx;
565 status = get_generic_auth_footer(gensec_security,
566 auth->auth_level,
567 &data, &full_pkt,
568 &auth_info.credentials);
569 if (!NT_STATUS_IS_OK(status)) {
570 return status;
572 break;
575 /* TODO: remove later
576 * this is still needed because in the server code the
577 * pkt_trailer actually has a copy of the raw data, and they
578 * are still both used in later calls */
579 if (auth->auth_level == DCERPC_AUTH_LEVEL_PRIVACY) {
580 if (pkt_trailer->length != data.length) {
581 return NT_STATUS_INVALID_PARAMETER;
583 memcpy(pkt_trailer->data, data.data, data.length);
586 pkt_trailer->length -= auth_info.auth_pad_length;
587 data_blob_free(&auth_info.credentials);
588 return NT_STATUS_OK;