6729ff
From ea6e3de5ae7c79b3badf9cfa8fcbaebf76ec5bae Mon Sep 17 00:00:00 2001
6729ff
From: Stefan Metzmacher <metze@samba.org>
6729ff
Date: Tue, 24 Sep 2013 05:03:40 +0200
6729ff
Subject: [PATCH 01/16] CVE-2013-4408:librpc: check for invalid frag_len
6729ff
 within dcerpc_read_ncacn_packet_done()
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
Reviewed-by: Jeremy Allison <jra@samba.org>
6729ff
---
6729ff
 librpc/rpc/dcerpc_util.c |    5 +++++
6729ff
 1 file changed, 5 insertions(+)
6729ff
6729ff
diff --git a/librpc/rpc/dcerpc_util.c b/librpc/rpc/dcerpc_util.c
6729ff
index de292c8..458ecc5 100644
6729ff
--- a/librpc/rpc/dcerpc_util.c
6729ff
+++ b/librpc/rpc/dcerpc_util.c
6729ff
@@ -292,6 +292,11 @@ static void dcerpc_read_ncacn_packet_done(struct tevent_req *subreq)
6729ff
 		return;
6729ff
 	}
6729ff
 
6729ff
+	if (state->pkt->frag_length != state->buffer.length) {
6729ff
+		tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
6729ff
+		return;
6729ff
+	}
6729ff
+
6729ff
 	tevent_req_done(req);
6729ff
 }
6729ff
 
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From d405a5afc175c5a936511f861a35cffd3be43fd9 Mon Sep 17 00:00:00 2001
6729ff
From: Stefan Metzmacher <metze@samba.org>
6729ff
Date: Tue, 24 Sep 2013 05:03:40 +0200
6729ff
Subject: [PATCH 02/16] CVE-2013-4408:librpc: check for invalid frag_len
6729ff
 within dcerpc_read_ncacn_packet_next_vector()
6729ff
6729ff
We should do this explicit instead of relying on
6729ff
tstream_readv_pdu_ask_for_next_vector() to catch the overflow.
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
Reviewed-by: Jeremy Allison <jra@samba.org>
6729ff
---
6729ff
 librpc/rpc/dcerpc_util.c |    9 +++++++++
6729ff
 1 file changed, 9 insertions(+)
6729ff
6729ff
diff --git a/librpc/rpc/dcerpc_util.c b/librpc/rpc/dcerpc_util.c
6729ff
index 458ecc5..0b9cca3 100644
6729ff
--- a/librpc/rpc/dcerpc_util.c
6729ff
+++ b/librpc/rpc/dcerpc_util.c
6729ff
@@ -223,6 +223,15 @@ static int dcerpc_read_ncacn_packet_next_vector(struct tstream_context *stream,
6729ff
 
6729ff
 		ofs = state->buffer.length;
6729ff
 
6729ff
+		if (frag_len < ofs) {
6729ff
+			/*
6729ff
+			 * something is wrong, let the caller deal with it
6729ff
+			 */
6729ff
+			*_vector = NULL;
6729ff
+			*_count = 0;
6729ff
+			return 0;
6729ff
+		}
6729ff
+
6729ff
 		state->buffer.data = talloc_realloc(state,
6729ff
 						    state->buffer.data,
6729ff
 						    uint8_t, frag_len);
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From db3a2292a096d95ce672950e738c35e902077604 Mon Sep 17 00:00:00 2001
6729ff
From: Stefan Metzmacher <metze@samba.org>
6729ff
Date: Wed, 25 Sep 2013 23:25:12 +0200
6729ff
Subject: [PATCH 03/16] CVE-2013-4408:s3:rpc_client: check for invalid
6729ff
 frag_len in dcerpc_pull_ncacn_packet()
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
Reviewed-by: Jeremy Allison <jra@samba.org>
6729ff
---
6729ff
 source3/librpc/rpc/dcerpc_helpers.c |    4 ++++
6729ff
 1 file changed, 4 insertions(+)
6729ff
6729ff
diff --git a/source3/librpc/rpc/dcerpc_helpers.c b/source3/librpc/rpc/dcerpc_helpers.c
6729ff
index d36c2da..a55e419 100644
6729ff
--- a/source3/librpc/rpc/dcerpc_helpers.c
6729ff
+++ b/source3/librpc/rpc/dcerpc_helpers.c
6729ff
@@ -127,6 +127,10 @@ NTSTATUS dcerpc_pull_ncacn_packet(TALLOC_CTX *mem_ctx,
6729ff
 		NDR_PRINT_DEBUG(ncacn_packet, r);
6729ff
 	}
6729ff
 
6729ff
+	if (r->frag_length != blob->length) {
6729ff
+		return NT_STATUS_RPC_PROTOCOL_ERROR;
6729ff
+	}
6729ff
+
6729ff
 	return NT_STATUS_OK;
6729ff
 }
6729ff
 
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From 1e1b1d587a056d898f0f5bb99235e488a06079f1 Mon Sep 17 00:00:00 2001
6729ff
From: Stefan Metzmacher <metze@samba.org>
6729ff
Date: Wed, 25 Sep 2013 23:25:12 +0200
6729ff
Subject: [PATCH 04/16] CVE-2013-4408:s3:rpc_client: verify frag_len at least
6729ff
 contains the header size
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
Reviewed-by: Jeremy Allison <jra@samba.org>
6729ff
---
6729ff
 source3/rpc_client/cli_pipe.c |    8 ++++++++
6729ff
 1 file changed, 8 insertions(+)
6729ff
6729ff
diff --git a/source3/rpc_client/cli_pipe.c b/source3/rpc_client/cli_pipe.c
6729ff
index 2e978ef..fd854c4 100644
6729ff
--- a/source3/rpc_client/cli_pipe.c
6729ff
+++ b/source3/rpc_client/cli_pipe.c
6729ff
@@ -284,6 +284,10 @@ static struct tevent_req *get_complete_frag_send(TALLOC_CTX *mem_ctx,
6729ff
 	}
6729ff
 
6729ff
 	state->frag_len = dcerpc_get_frag_length(pdu);
6729ff
+	if (state->frag_len < RPC_HEADER_LEN) {
6729ff
+		tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
6729ff
+		return tevent_req_post(req, ev);
6729ff
+	}
6729ff
 
6729ff
 	/*
6729ff
 	 * Ensure we have frag_len bytes of data.
6729ff
@@ -332,6 +336,10 @@ static void get_complete_frag_got_header(struct tevent_req *subreq)
6729ff
 	}
6729ff
 
6729ff
 	state->frag_len = dcerpc_get_frag_length(state->pdu);
6729ff
+	if (state->frag_len < RPC_HEADER_LEN) {
6729ff
+		tevent_req_nterror(req, NT_STATUS_RPC_PROTOCOL_ERROR);
6729ff
+		return;
6729ff
+	}
6729ff
 
6729ff
 	if (!data_blob_realloc(NULL, state->pdu, state->frag_len)) {
6729ff
 		tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From 33d4dc2f0512d845cc6e00174b870833dd055862 Mon Sep 17 00:00:00 2001
6729ff
From: Stefan Metzmacher <metze@samba.org>
6729ff
Date: Wed, 25 Sep 2013 23:25:12 +0200
6729ff
Subject: [PATCH 05/16] CVE-2013-4408:s4:dcerpc: check for invalid frag_len in
6729ff
 ncacn_pull()
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
Reviewed-by: Jeremy Allison <jra@samba.org>
6729ff
---
6729ff
 source4/librpc/rpc/dcerpc.c |    4 ++++
6729ff
 1 file changed, 4 insertions(+)
6729ff
6729ff
diff --git a/source4/librpc/rpc/dcerpc.c b/source4/librpc/rpc/dcerpc.c
6729ff
index e653cba..2826160 100644
6729ff
--- a/source4/librpc/rpc/dcerpc.c
6729ff
+++ b/source4/librpc/rpc/dcerpc.c
6729ff
@@ -693,6 +693,10 @@ static NTSTATUS ncacn_pull(struct dcecli_connection *c, DATA_BLOB *blob, TALLOC_
6729ff
 		return ndr_map_error2ntstatus(ndr_err);
6729ff
 	}
6729ff
 
6729ff
+	if (pkt->frag_length != blob->length) {
6729ff
+		return NT_STATUS_RPC_PROTOCOL_ERROR;
6729ff
+	}
6729ff
+
6729ff
 	return NT_STATUS_OK;
6729ff
 }
6729ff
 
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From 7ff27a03ddcb2aaa6b130266a9262dca79a324c5 Mon Sep 17 00:00:00 2001
6729ff
From: Stefan Metzmacher <metze@samba.org>
6729ff
Date: Wed, 25 Sep 2013 23:25:12 +0200
6729ff
Subject: [PATCH 06/16] CVE-2013-4408:s4:dcerpc_smb: check for invalid
6729ff
 frag_len in send_read_request_continue()
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
Reviewed-by: Jeremy Allison <jra@samba.org>
6729ff
---
6729ff
 source4/librpc/rpc/dcerpc_smb.c |    6 ++++++
6729ff
 1 file changed, 6 insertions(+)
6729ff
6729ff
diff --git a/source4/librpc/rpc/dcerpc_smb.c b/source4/librpc/rpc/dcerpc_smb.c
6729ff
index e02631e..efb76cf 100644
6729ff
--- a/source4/librpc/rpc/dcerpc_smb.c
6729ff
+++ b/source4/librpc/rpc/dcerpc_smb.c
6729ff
@@ -163,6 +163,12 @@ static NTSTATUS send_read_request_continue(struct dcecli_connection *c, DATA_BLO
6729ff
 	} else {
6729ff
 		uint32_t frag_length = blob->length>=16?
6729ff
 			dcerpc_get_frag_length(blob):0x2000;
6729ff
+
6729ff
+		if (frag_length < state->data.length) {
6729ff
+			talloc_free(state);
6729ff
+			return NT_STATUS_RPC_PROTOCOL_ERROR;
6729ff
+		}
6729ff
+
6729ff
 		state->received = blob->length;
6729ff
 		state->data = data_blob_talloc(state, NULL, frag_length);
6729ff
 		if (!state->data.data) {
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From 2e01567db620407a9bfdee28efa61ddccc1357f8 Mon Sep 17 00:00:00 2001
6729ff
From: Stefan Metzmacher <metze@samba.org>
6729ff
Date: Wed, 25 Sep 2013 23:25:12 +0200
6729ff
Subject: [PATCH 07/16] CVE-2013-4408:s4:dcerpc_smb2: check for invalid
6729ff
 frag_len in send_read_request_continue()
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
Reviewed-by: Jeremy Allison <jra@samba.org>
6729ff
---
6729ff
 source4/librpc/rpc/dcerpc_smb2.c |    6 ++++++
6729ff
 1 file changed, 6 insertions(+)
6729ff
6729ff
diff --git a/source4/librpc/rpc/dcerpc_smb2.c b/source4/librpc/rpc/dcerpc_smb2.c
6729ff
index 473ca78..2b1c66e 100644
6729ff
--- a/source4/librpc/rpc/dcerpc_smb2.c
6729ff
+++ b/source4/librpc/rpc/dcerpc_smb2.c
6729ff
@@ -173,6 +173,12 @@ static NTSTATUS send_read_request_continue(struct dcecli_connection *c, DATA_BLO
6729ff
 
6729ff
 	if (state->data.length >= 16) {
6729ff
 		uint16_t frag_length = dcerpc_get_frag_length(&state->data);
6729ff
+
6729ff
+		if (frag_length < state->data.length) {
6729ff
+			talloc_free(state);
6729ff
+			return NT_STATUS_RPC_PROTOCOL_ERROR;
6729ff
+		}
6729ff
+
6729ff
 		io.in.length = frag_length - state->data.length;
6729ff
 	} else {
6729ff
 		io.in.length = 0x2000;
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From 2a94a9db0086582119bbcb7b4c82e86e8a3f8137 Mon Sep 17 00:00:00 2001
6729ff
From: Stefan Metzmacher <metze@samba.org>
6729ff
Date: Tue, 24 Sep 2013 05:03:40 +0200
6729ff
Subject: [PATCH 08/16] CVE-2013-4408:s4:dcerpc_sock: check for invalid
6729ff
 frag_len within sock_complete_packet()
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
Reviewed-by: Jeremy Allison <jra@samba.org>
6729ff
---
6729ff
 source4/librpc/rpc/dcerpc_sock.c |    6 ++++++
6729ff
 1 file changed, 6 insertions(+)
6729ff
6729ff
diff --git a/source4/librpc/rpc/dcerpc_sock.c b/source4/librpc/rpc/dcerpc_sock.c
6729ff
index 58fca4c..d36af11 100644
6729ff
--- a/source4/librpc/rpc/dcerpc_sock.c
6729ff
+++ b/source4/librpc/rpc/dcerpc_sock.c
6729ff
@@ -102,6 +102,12 @@ static NTSTATUS sock_complete_packet(void *private_data, DATA_BLOB blob, size_t
6729ff
 		return STATUS_MORE_ENTRIES;
6729ff
 	}
6729ff
 	*size = dcerpc_get_frag_length(&blob;;
6729ff
+	if (*size < blob.length) {
6729ff
+		/*
6729ff
+		 * something is wrong, let the caller deal with it
6729ff
+		 */
6729ff
+		*size = blob.length;
6729ff
+	}
6729ff
 	if (*size > blob.length) {
6729ff
 		return STATUS_MORE_ENTRIES;
6729ff
 	}
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From 79c3d6df2fc63e188ae6091d6f0bd0a55f3fe548 Mon Sep 17 00:00:00 2001
6729ff
From: Stefan Metzmacher <metze@samba.org>
6729ff
Date: Wed, 16 Oct 2013 14:17:49 +0200
6729ff
Subject: [PATCH 09/16] CVE-2013-4408:async_sock: add some overflow detection
6729ff
 to read_packet_handler()
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
Reviewed-by: Jeremy Allison <jra@samba.org>
6729ff
---
6729ff
 lib/async_req/async_sock.c |    5 +++++
6729ff
 1 file changed, 5 insertions(+)
6729ff
6729ff
diff --git a/lib/async_req/async_sock.c b/lib/async_req/async_sock.c
6729ff
index 59dde88..74b2cb7 100644
6729ff
--- a/lib/async_req/async_sock.c
6729ff
+++ b/lib/async_req/async_sock.c
6729ff
@@ -667,6 +667,11 @@ static void read_packet_handler(struct tevent_context *ev,
6729ff
 		return;
6729ff
 	}
6729ff
 
6729ff
+	if (total + more < total) {
6729ff
+		tevent_req_error(req, EMSGSIZE);
6729ff
+		return;
6729ff
+	}
6729ff
+
6729ff
 	tmp = talloc_realloc(state, state->buf, uint8_t, total+more);
6729ff
 	if (tevent_req_nomem(tmp, req)) {
6729ff
 		return;
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From ebfad1eadb3fc492f31c35a9d2585fe27ed00ad5 Mon Sep 17 00:00:00 2001
6729ff
From: Stefan Metzmacher <metze@samba.org>
6729ff
Date: Wed, 16 Oct 2013 14:17:49 +0200
6729ff
Subject: [PATCH 10/16] CVE-2013-4408:s3:util_tsock: add some overflow
6729ff
 detection to tstream_read_packet_done()
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
Reviewed-by: Jeremy Allison <jra@samba.org>
6729ff
---
6729ff
 source3/lib/util_tsock.c |    5 +++++
6729ff
 1 file changed, 5 insertions(+)
6729ff
6729ff
diff --git a/source3/lib/util_tsock.c b/source3/lib/util_tsock.c
6729ff
index 35a97f5..03380ef 100644
6729ff
--- a/source3/lib/util_tsock.c
6729ff
+++ b/source3/lib/util_tsock.c
6729ff
@@ -110,6 +110,11 @@ static void tstream_read_packet_done(struct tevent_req *subreq)
6729ff
 		return;
6729ff
 	}
6729ff
 
6729ff
+	if (total + more < total) {
6729ff
+		tevent_req_error(req, EMSGSIZE);
6729ff
+		return;
6729ff
+	}
6729ff
+
6729ff
 	tmp = talloc_realloc(state, state->buf, uint8_t, total+more);
6729ff
 	if (tevent_req_nomem(tmp, req)) {
6729ff
 		return;
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From 1108ce2b996cde47a653cdfca27e059e3986da5f Mon Sep 17 00:00:00 2001
6729ff
From: Stefan Metzmacher <metze@samba.org>
6729ff
Date: Wed, 16 Oct 2013 14:17:49 +0200
6729ff
Subject: [PATCH 11/16] CVE-2013-4408:libcli/util: add some size verification
6729ff
 to tstream_read_pdu_blob_done()
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
Reviewed-by: Jeremy Allison <jra@samba.org>
6729ff
---
6729ff
 libcli/util/tstream.c |    5 +++++
6729ff
 1 file changed, 5 insertions(+)
6729ff
6729ff
diff --git a/libcli/util/tstream.c b/libcli/util/tstream.c
6729ff
index 12cef9b..dd830e2 100644
6729ff
--- a/libcli/util/tstream.c
6729ff
+++ b/libcli/util/tstream.c
6729ff
@@ -129,6 +129,11 @@ static void tstream_read_pdu_blob_done(struct tevent_req *subreq)
6729ff
 		return;
6729ff
 	}
6729ff
 
6729ff
+	if (new_buf_size <= old_buf_size) {
6729ff
+		tevent_req_nterror(req, NT_STATUS_INVALID_BUFFER_SIZE);
6729ff
+		return;
6729ff
+	}
6729ff
+
6729ff
 	buf = talloc_realloc(state, state->pdu_blob.data, uint8_t, new_buf_size);
6729ff
 	if (tevent_req_nomem(buf, req)) {
6729ff
 		return;
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From 72d5d302c5ed46f3ca465653f6c595ea500b3063 Mon Sep 17 00:00:00 2001
6729ff
From: Stefan Metzmacher <metze@samba.org>
6729ff
Date: Wed, 16 Oct 2013 16:26:58 +0200
6729ff
Subject: [PATCH 12/16] CVE-2013-4408:s3:ctdb_conn: add some length
6729ff
 verification to ctdb_packet_more()
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
Reviewed-by: Jeremy Allison <jra@samba.org>
6729ff
---
6729ff
 source3/lib/ctdb_conn.c |    5 +++++
6729ff
 1 file changed, 5 insertions(+)
6729ff
6729ff
diff --git a/source3/lib/ctdb_conn.c b/source3/lib/ctdb_conn.c
6729ff
index 90930eb..40071d4 100644
6729ff
--- a/source3/lib/ctdb_conn.c
6729ff
+++ b/source3/lib/ctdb_conn.c
6729ff
@@ -233,6 +233,11 @@ static ssize_t ctdb_packet_more(uint8_t *buf, size_t buflen, void *p)
6729ff
 		return 0;
6729ff
 	}
6729ff
 	memcpy(&len, buf, sizeof(len));
6729ff
+
6729ff
+	if (len < sizeof(uint32_t)) {
6729ff
+		return -1;
6729ff
+	}
6729ff
+
6729ff
 	return (len - sizeof(uint32_t));
6729ff
 }
6729ff
 
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From 5335358922eb2d86f00c73bb9e11940cc0cec7ce Mon Sep 17 00:00:00 2001
6729ff
From: Jeremy Allison <jra@samba.org>
6729ff
Date: Thu, 17 Oct 2013 14:44:35 -0700
6729ff
Subject: [PATCH 13/16] CVE-2013-4408:s3:Ensure we always check call_id when
6729ff
 validating an RPC reply.
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Jeremy Allison <jra@samba.org>
6729ff
Reviewed-by: Stefan Metzmacher <metze@samba.org>
6729ff
---
6729ff
 source3/rpc_client/cli_pipe.c |   33 +++++++++++++++++++++++++--------
6729ff
 1 file changed, 25 insertions(+), 8 deletions(-)
6729ff
6729ff
diff --git a/source3/rpc_client/cli_pipe.c b/source3/rpc_client/cli_pipe.c
6729ff
index fd854c4..385ae25 100644
6729ff
--- a/source3/rpc_client/cli_pipe.c
6729ff
+++ b/source3/rpc_client/cli_pipe.c
6729ff
@@ -389,6 +389,7 @@ static NTSTATUS cli_pipe_validate_current_pdu(TALLOC_CTX *mem_ctx,
6729ff
 						struct ncacn_packet *pkt,
6729ff
 						DATA_BLOB *pdu,
6729ff
 						uint8_t expected_pkt_type,
6729ff
+						uint32_t call_id,
6729ff
 						DATA_BLOB *rdata,
6729ff
 						DATA_BLOB *reply_pdu)
6729ff
 {
6729ff
@@ -487,7 +488,7 @@ static NTSTATUS cli_pipe_validate_current_pdu(TALLOC_CTX *mem_ctx,
6729ff
 			  "from %s!\n",
6729ff
 			  (unsigned int)pkt->ptype,
6729ff
 			  rpccli_pipe_txt(talloc_tos(), cli)));
6729ff
-		return NT_STATUS_INVALID_INFO_CLASS;
6729ff
+		return NT_STATUS_RPC_PROTOCOL_ERROR;
6729ff
 	}
6729ff
 
6729ff
 	if (pkt->ptype != expected_pkt_type) {
6729ff
@@ -495,7 +496,15 @@ static NTSTATUS cli_pipe_validate_current_pdu(TALLOC_CTX *mem_ctx,
6729ff
 			  "RPC packet type - %u, not %u\n",
6729ff
 			  rpccli_pipe_txt(talloc_tos(), cli),
6729ff
 			  pkt->ptype, expected_pkt_type));
6729ff
-		return NT_STATUS_INVALID_INFO_CLASS;
6729ff
+		return NT_STATUS_RPC_PROTOCOL_ERROR;
6729ff
+	}
6729ff
+
6729ff
+	if (pkt->call_id != call_id) {
6729ff
+		DEBUG(3, (__location__ ": Connection to %s got an unexpected "
6729ff
+			  "RPC call_id - %u, not %u\n",
6729ff
+			  rpccli_pipe_txt(talloc_tos(), cli),
6729ff
+			  pkt->call_id, call_id));
6729ff
+		return NT_STATUS_RPC_PROTOCOL_ERROR;
6729ff
 	}
6729ff
 
6729ff
 	/* Do this just before return - we don't want to modify any rpc header
6729ff
@@ -701,6 +710,7 @@ struct rpc_api_pipe_state {
6729ff
 	struct tevent_context *ev;
6729ff
 	struct rpc_pipe_client *cli;
6729ff
 	uint8_t expected_pkt_type;
6729ff
+	uint32_t call_id;
6729ff
 
6729ff
 	DATA_BLOB incoming_frag;
6729ff
 	struct ncacn_packet *pkt;
6729ff
@@ -719,7 +729,8 @@ static struct tevent_req *rpc_api_pipe_send(TALLOC_CTX *mem_ctx,
6729ff
 					    struct tevent_context *ev,
6729ff
 					    struct rpc_pipe_client *cli,
6729ff
 					    DATA_BLOB *data, /* Outgoing PDU */
6729ff
-					    uint8_t expected_pkt_type)
6729ff
+					    uint8_t expected_pkt_type,
6729ff
+					    uint32_t call_id)
6729ff
 {
6729ff
 	struct tevent_req *req, *subreq;
6729ff
 	struct rpc_api_pipe_state *state;
6729ff
@@ -733,6 +744,7 @@ static struct tevent_req *rpc_api_pipe_send(TALLOC_CTX *mem_ctx,
6729ff
 	state->ev = ev;
6729ff
 	state->cli = cli;
6729ff
 	state->expected_pkt_type = expected_pkt_type;
6729ff
+	state->call_id = call_id;
6729ff
 	state->incoming_frag = data_blob_null;
6729ff
 	state->reply_pdu = data_blob_null;
6729ff
 	state->reply_pdu_offset = 0;
6729ff
@@ -884,6 +896,7 @@ static void rpc_api_pipe_got_pdu(struct tevent_req *subreq)
6729ff
 						state->cli, state->pkt,
6729ff
 						&state->incoming_frag,
6729ff
 						state->expected_pkt_type,
6729ff
+						state->call_id,
6729ff
 						&rdata,
6729ff
 						&state->reply_pdu);
6729ff
 
6729ff
@@ -1226,7 +1239,8 @@ struct tevent_req *rpc_api_pipe_req_send(TALLOC_CTX *mem_ctx,
6729ff
 	if (is_last_frag) {
6729ff
 		subreq = rpc_api_pipe_send(state, ev, state->cli,
6729ff
 					   &state->rpc_out,
6729ff
-					   DCERPC_PKT_RESPONSE);
6729ff
+					   DCERPC_PKT_RESPONSE,
6729ff
+					   state->call_id);
6729ff
 		if (subreq == NULL) {
6729ff
 			goto fail;
6729ff
 		}
6729ff
@@ -1362,7 +1376,8 @@ static void rpc_api_pipe_req_write_done(struct tevent_req *subreq)
6729ff
 	if (is_last_frag) {
6729ff
 		subreq = rpc_api_pipe_send(state, state->ev, state->cli,
6729ff
 					   &state->rpc_out,
6729ff
-					   DCERPC_PKT_RESPONSE);
6729ff
+					   DCERPC_PKT_RESPONSE,
6729ff
+					   state->call_id);
6729ff
 		if (tevent_req_nomem(subreq, req)) {
6729ff
 			return;
6729ff
 		}
6729ff
@@ -1608,7 +1623,7 @@ struct tevent_req *rpc_pipe_bind_send(TALLOC_CTX *mem_ctx,
6729ff
 	}
6729ff
 
6729ff
 	subreq = rpc_api_pipe_send(state, ev, cli, &state->rpc_out,
6729ff
-				   DCERPC_PKT_BIND_ACK);
6729ff
+				   DCERPC_PKT_BIND_ACK, state->rpc_call_id);
6729ff
 	if (subreq == NULL) {
6729ff
 		goto fail;
6729ff
 	}
6729ff
@@ -1916,7 +1931,8 @@ static NTSTATUS rpc_bind_next_send(struct tevent_req *req,
6729ff
 	}
6729ff
 
6729ff
 	subreq = rpc_api_pipe_send(state, state->ev, state->cli,
6729ff
-				   &state->rpc_out, DCERPC_PKT_ALTER_RESP);
6729ff
+				   &state->rpc_out, DCERPC_PKT_ALTER_RESP,
6729ff
+				   state->rpc_call_id);
6729ff
 	if (subreq == NULL) {
6729ff
 		return NT_STATUS_NO_MEMORY;
6729ff
 	}
6729ff
@@ -1948,7 +1964,8 @@ static NTSTATUS rpc_bind_finish_send(struct tevent_req *req,
6729ff
 	}
6729ff
 
6729ff
 	subreq = rpc_api_pipe_send(state, state->ev, state->cli,
6729ff
-				   &state->rpc_out, DCERPC_PKT_AUTH3);
6729ff
+				   &state->rpc_out, DCERPC_PKT_AUTH3,
6729ff
+				   state->rpc_call_id);
6729ff
 	if (subreq == NULL) {
6729ff
 		return NT_STATUS_NO_MEMORY;
6729ff
 	}
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From 7d60c72841e8b2e4315e506fb1033508b43425f6 Mon Sep 17 00:00:00 2001
6729ff
From: Jeremy Allison <jra@samba.org>
6729ff
Date: Thu, 7 Nov 2013 20:38:01 -0800
6729ff
Subject: [PATCH 14/16] CVE-2013-4408:s3:Ensure LookupSids replies arrays are
6729ff
 range checked.
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
Signed-off-by: Jeremy Allison <jra@samba.org>
6729ff
---
6729ff
 nsswitch/libwbclient/wbc_sid.c     |    7 +++++++
6729ff
 nsswitch/wbinfo.c                  |   23 ++++++++++++++++++++---
6729ff
 source3/rpc_client/cli_lsarpc.c    |   17 ++++++++++++++++-
6729ff
 source3/rpcclient/cmd_lsarpc.c     |    7 +++++--
6729ff
 source3/winbindd/wb_lookupsids.c   |    3 +++
6729ff
 source3/winbindd/winbindd_rpc.c    |   32 ++++++++++++++++++++++++++++++++
6729ff
 source4/libcli/util/clilsa.c       |   16 +++++++++++++++-
6729ff
 source4/winbind/wb_async_helpers.c |   13 ++++++++++++-
6729ff
 8 files changed, 110 insertions(+), 8 deletions(-)
6729ff
6729ff
diff --git a/nsswitch/libwbclient/wbc_sid.c b/nsswitch/libwbclient/wbc_sid.c
6729ff
index bab6933..82ac339 100644
6729ff
--- a/nsswitch/libwbclient/wbc_sid.c
6729ff
+++ b/nsswitch/libwbclient/wbc_sid.c
6729ff
@@ -421,6 +421,13 @@ wbcErr wbcLookupSids(const struct wbcDomainSid *sids, int num_sids,
6729ff
 	for (i=0; i
6729ff
 
6729ff
 		names[i].domain_index = strtoul(p, &q, 10);
6729ff
+		if (names[i].domain_index < 0) {
6729ff
+			goto wbc_err_invalid;
6729ff
+		}
6729ff
+		if (names[i].domain_index >= num_domains) {
6729ff
+			goto wbc_err_invalid;
6729ff
+		}
6729ff
+
6729ff
 		if (*q != ' ') {
6729ff
 			goto wbc_err_invalid;
6729ff
 		}
6729ff
diff --git a/nsswitch/wbinfo.c b/nsswitch/wbinfo.c
6729ff
index 17977ed..3f0310a 100644
6729ff
--- a/nsswitch/wbinfo.c
6729ff
+++ b/nsswitch/wbinfo.c
6729ff
@@ -1391,11 +1391,28 @@ static bool wbinfo_lookup_sids(const char *arg)
6729ff
 	}
6729ff
 
6729ff
 	for (i=0; i
6729ff
+		const char *domain = NULL;
6729ff
+
6729ff
 		wbcSidToStringBuf(&sids[i], sidstr, sizeof(sidstr));
6729ff
 
6729ff
-		d_printf("%s -> %s\\%s %d\n", sidstr,
6729ff
-			 domains[names[i].domain_index].short_name,
6729ff
-			 names[i].name, names[i].type);
6729ff
+		if (names[i].domain_index >= num_domains) {
6729ff
+			domain = "<none>";
6729ff
+		} else if (names[i].domain_index < 0) {
6729ff
+			domain = "<none>";
6729ff
+		} else {
6729ff
+			domain = domains[names[i].domain_index].short_name;
6729ff
+		}
6729ff
+
6729ff
+		if (names[i].type == WBC_SID_NAME_DOMAIN) {
6729ff
+			d_printf("%s -> %s %d\n", sidstr,
6729ff
+				domain,
6729ff
+				names[i].type);
6729ff
+		} else {
6729ff
+			d_printf("%s -> %s%c%s %d\n", sidstr,
6729ff
+				domain,
6729ff
+				winbind_separator(),
6729ff
+				names[i].name, names[i].type);
6729ff
+		}
6729ff
 	}
6729ff
 	wbcFreeMemory(names);
6729ff
 	wbcFreeMemory(domains);
6729ff
diff --git a/source3/rpc_client/cli_lsarpc.c b/source3/rpc_client/cli_lsarpc.c
6729ff
index 126f370..7cadd6e 100644
6729ff
--- a/source3/rpc_client/cli_lsarpc.c
6729ff
+++ b/source3/rpc_client/cli_lsarpc.c
6729ff
@@ -279,11 +279,26 @@ static NTSTATUS dcerpc_lsa_lookup_sids_noalloc(struct dcerpc_binding_handle *h,
6729ff
 
6729ff
 	for (i = 0; i < num_sids; i++) {
6729ff
 		const char *name, *dom_name;
6729ff
-		uint32_t dom_idx = lsa_names.names[i].sid_index;
6729ff
+		uint32_t dom_idx;
6729ff
+
6729ff
+		if (i >= lsa_names.count) {
6729ff
+			*presult = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+			return status;
6729ff
+		}
6729ff
+
6729ff
+		dom_idx = lsa_names.names[i].sid_index;
6729ff
 
6729ff
 		/* Translate optimised name through domain index array */
6729ff
 
6729ff
 		if (dom_idx != 0xffffffff) {
6729ff
+			if (ref_domains == NULL) {
6729ff
+				*presult = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+				return status;
6729ff
+			}
6729ff
+			if (dom_idx >= ref_domains->count) {
6729ff
+				*presult = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+				return status;
6729ff
+			}
6729ff
 
6729ff
 			dom_name = ref_domains->domains[dom_idx].name.string;
6729ff
 			name = lsa_names.names[i].name.string;
6729ff
diff --git a/source3/rpcclient/cmd_lsarpc.c b/source3/rpcclient/cmd_lsarpc.c
6729ff
index cbc089f..a7ee4e4 100644
6729ff
--- a/source3/rpcclient/cmd_lsarpc.c
6729ff
+++ b/source3/rpcclient/cmd_lsarpc.c
6729ff
@@ -450,7 +450,7 @@ static NTSTATUS cmd_lsa_lookup_sids3(struct rpc_pipe_client *cli,
6729ff
 	NTSTATUS status = NT_STATUS_UNSUCCESSFUL, result;
6729ff
 	int i;
6729ff
 	struct lsa_SidArray sids;
6729ff
-	struct lsa_RefDomainList *domains;
6729ff
+	struct lsa_RefDomainList *domains = NULL;
6729ff
 	struct lsa_TransNameArray2 names;
6729ff
 	uint32_t count = 0;
6729ff
 	struct dcerpc_binding_handle *b = cli->binding_handle;
6729ff
@@ -506,9 +506,12 @@ static NTSTATUS cmd_lsa_lookup_sids3(struct rpc_pipe_client *cli,
6729ff
 
6729ff
 	/* Print results */
6729ff
 
6729ff
-	for (i = 0; i < count; i++) {
6729ff
+	for (i = 0; i < names.count; i++) {
6729ff
 		fstring sid_str;
6729ff
 
6729ff
+		if (i >= sids.num_sids) {
6729ff
+			break;
6729ff
+		}
6729ff
 		sid_to_fstring(sid_str, sids.sids[i].sid);
6729ff
 		printf("%s %s (%d)\n", sid_str,
6729ff
 		       names.names[i].name.string,
6729ff
diff --git a/source3/winbindd/wb_lookupsids.c b/source3/winbindd/wb_lookupsids.c
6729ff
index 2c4ebda..e10d511 100644
6729ff
--- a/source3/winbindd/wb_lookupsids.c
6729ff
+++ b/source3/winbindd/wb_lookupsids.c
6729ff
@@ -402,6 +402,9 @@ static bool wb_lookupsids_move_name(struct lsa_RefDomainList *src_domains,
6729ff
 	uint32_t src_domain_index, dst_domain_index;
6729ff
 
6729ff
 	src_domain_index = src_name->sid_index;
6729ff
+	if (src_domain_index >= src_domains->count) {
6729ff
+		return false;
6729ff
+	}
6729ff
 	src_domain = &src_domains->domains[src_domain_index];
6729ff
 
6729ff
 	if (!wb_lookupsids_find_dom_idx(
6729ff
diff --git a/source3/winbindd/winbindd_rpc.c b/source3/winbindd/winbindd_rpc.c
6729ff
index 44deeb0..7345ea7 100644
6729ff
--- a/source3/winbindd/winbindd_rpc.c
6729ff
+++ b/source3/winbindd/winbindd_rpc.c
6729ff
@@ -1084,6 +1084,10 @@ static NTSTATUS rpc_try_lookup_sids3(TALLOC_CTX *mem_ctx,
6729ff
 	if (NT_STATUS_IS_ERR(result)) {
6729ff
 		return result;
6729ff
 	}
6729ff
+	if (sids->num_sids != lsa_names2.count) {
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+	}
6729ff
+
6729ff
 	names = talloc_zero(mem_ctx, struct lsa_TransNameArray);
6729ff
 	if (names == NULL) {
6729ff
 		return NT_STATUS_NO_MEMORY;
6729ff
@@ -1099,6 +1103,16 @@ static NTSTATUS rpc_try_lookup_sids3(TALLOC_CTX *mem_ctx,
6729ff
 		names->names[i].name.string = talloc_move(
6729ff
 			names->names, &lsa_names2.names[i].name.string);
6729ff
 		names->names[i].sid_index = lsa_names2.names[i].sid_index;
6729ff
+
6729ff
+		if (names->names[i].sid_index == UINT32_MAX) {
6729ff
+			continue;
6729ff
+		}
6729ff
+		if ((*pdomains) == NULL) {
6729ff
+			return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		}
6729ff
+		if (names->names[i].sid_index >= (*pdomains)->count) {
6729ff
+			return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		}
6729ff
 	}
6729ff
 	*pnames = names;
6729ff
 	return result;
6729ff
@@ -1114,6 +1128,7 @@ NTSTATUS rpc_lookup_sids(TALLOC_CTX *mem_ctx,
6729ff
 	struct rpc_pipe_client *cli = NULL;
6729ff
 	struct policy_handle lsa_policy;
6729ff
 	uint32_t count;
6729ff
+	uint32_t i;
6729ff
 	NTSTATUS status, result;
6729ff
 
6729ff
 	status = cm_connect_lsat(domain, mem_ctx, &cli, &lsa_policy);
6729ff
@@ -1140,6 +1155,23 @@ NTSTATUS rpc_lookup_sids(TALLOC_CTX *mem_ctx,
6729ff
 	if (NT_STATUS_IS_ERR(result)) {
6729ff
 		return result;
6729ff
 	}
6729ff
+
6729ff
+	if (sids->num_sids != names->count) {
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+	}
6729ff
+
6729ff
+	for (i=0; i < names->count; i++) {
6729ff
+		if (names->names[i].sid_index == UINT32_MAX) {
6729ff
+			continue;
6729ff
+		}
6729ff
+		if ((*pdomains) == NULL) {
6729ff
+			return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		}
6729ff
+		if (names->names[i].sid_index >= (*pdomains)->count) {
6729ff
+			return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		}
6729ff
+	}
6729ff
+
6729ff
 	*pnames = names;
6729ff
 	return result;
6729ff
 }
6729ff
diff --git a/source4/libcli/util/clilsa.c b/source4/libcli/util/clilsa.c
6729ff
index 812f953..cc0dae5 100644
6729ff
--- a/source4/libcli/util/clilsa.c
6729ff
+++ b/source4/libcli/util/clilsa.c
6729ff
@@ -260,7 +260,21 @@ NTSTATUS smblsa_lookup_sid(struct smbcli_state *cli,
6729ff
 	}
6729ff
 	if (names.count != 1) {
6729ff
 		talloc_free(mem_ctx2);
6729ff
-		return NT_STATUS_UNSUCCESSFUL;
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+	}
6729ff
+	if (domains == NULL) {
6729ff
+		talloc_free(mem_ctx2);
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+	}
6729ff
+	if (domains->count != 1) {
6729ff
+		talloc_free(mem_ctx2);
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+	}
6729ff
+	if (names.names[0].sid_index != UINT32_MAX &&
6729ff
+	    names.names[0].sid_index >= domains->count)
6729ff
+	{
6729ff
+		talloc_free(mem_ctx2);
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
 	}
6729ff
 
6729ff
 	(*name) = talloc_asprintf(mem_ctx, "%s\\%s", 
6729ff
diff --git a/source4/winbind/wb_async_helpers.c b/source4/winbind/wb_async_helpers.c
6729ff
index 2af8567..e3de2eb 100644
6729ff
--- a/source4/winbind/wb_async_helpers.c
6729ff
+++ b/source4/winbind/wb_async_helpers.c
6729ff
@@ -122,6 +122,12 @@ static void lsa_lookupsids_recv_names(struct tevent_req *subreq)
6729ff
 		return;
6729ff
 	}
6729ff
 
6729ff
+	if (state->names.count != state->num_sids) {
6729ff
+		composite_error(state->ctx,
6729ff
+				NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
+		return;
6729ff
+	}
6729ff
+
6729ff
 	state->result = talloc_array(state, struct wb_sid_object *,
6729ff
 				     state->num_sids);
6729ff
 	if (composite_nomem(state->result, state->ctx)) return;
6729ff
@@ -142,9 +148,14 @@ static void lsa_lookupsids_recv_names(struct tevent_req *subreq)
6729ff
 			continue;
6729ff
 		}
6729ff
 
6729ff
+		if (domains == NULL) {
6729ff
+			composite_error(state->ctx,
6729ff
+					NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
+			return;
6729ff
+		}
6729ff
 		if (name->sid_index >= domains->count) {
6729ff
 			composite_error(state->ctx,
6729ff
-					NT_STATUS_INVALID_PARAMETER);
6729ff
+					NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
 			return;
6729ff
 		}
6729ff
 
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From e89cb29f386adf364fb953686eb34d0db7d62202 Mon Sep 17 00:00:00 2001
6729ff
From: Jeremy Allison <jra@samba.org>
6729ff
Date: Thu, 7 Nov 2013 21:40:55 -0800
6729ff
Subject: [PATCH 15/16] CVE-2013-4408:s3:Ensure LookupNames replies arrays are
6729ff
 range checked.
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
Signed-off-by: Jeremy Allison <jra@samba.org>
6729ff
---
6729ff
 source3/lib/netapi/group.c                  |   82 +++++++++++++++++++++++++++
6729ff
 source3/lib/netapi/localgroup.c             |    8 ++-
6729ff
 source3/lib/netapi/user.c                   |   56 ++++++++++++++++++
6729ff
 source3/libnet/libnet_join.c                |   16 ++++++
6729ff
 source3/rpc_client/cli_lsarpc.c             |   18 ++++++
6729ff
 source3/rpc_server/netlogon/srv_netlog_nt.c |    2 +-
6729ff
 source3/rpcclient/cmd_lsarpc.c              |    6 +-
6729ff
 source3/rpcclient/cmd_samr.c                |   58 ++++++++++++++++++-
6729ff
 source3/smbd/lanman.c                       |    8 +++
6729ff
 source3/utils/net_rpc.c                     |   40 ++++++++++++-
6729ff
 source3/utils/net_rpc_join.c                |    9 +++
6729ff
 source3/winbindd/winbindd_rpc.c             |   14 +----
6729ff
 source4/libcli/util/clilsa.c                |    6 +-
6729ff
 source4/libnet/groupinfo.c                  |    9 ++-
6729ff
 source4/libnet/groupman.c                   |   10 ++--
6729ff
 source4/libnet/libnet_join.c                |   12 +++-
6729ff
 source4/libnet/libnet_lookup.c              |    5 ++
6729ff
 source4/libnet/libnet_passwd.c              |   10 +++-
6729ff
 source4/libnet/userinfo.c                   |    8 ++-
6729ff
 source4/libnet/userman.c                    |   24 ++++----
6729ff
 source4/winbind/wb_async_helpers.c          |   13 ++++-
6729ff
 21 files changed, 368 insertions(+), 46 deletions(-)
6729ff
6729ff
diff --git a/source3/lib/netapi/group.c b/source3/lib/netapi/group.c
6729ff
index 9813f7e..69c7af4 100644
6729ff
--- a/source3/lib/netapi/group.c
6729ff
+++ b/source3/lib/netapi/group.c
6729ff
@@ -309,6 +309,15 @@ WERROR NetGroupDel_r(struct libnetapi_ctx *ctx,
6729ff
 		goto done;
6729ff
 	}
6729ff
 
6729ff
+	if (rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+
6729ff
 	if (types.ids[0] != SID_NAME_DOM_GRP) {
6729ff
 		werr = WERR_INVALID_DATATYPE;
6729ff
 		goto done;
6729ff
@@ -511,6 +520,14 @@ WERROR NetGroupSetInfo_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	if (types.ids[0] != SID_NAME_DOM_GRP) {
6729ff
 		werr = WERR_INVALID_DATATYPE;
6729ff
@@ -781,6 +798,14 @@ WERROR NetGroupGetInfo_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	if (types.ids[0] != SID_NAME_DOM_GRP) {
6729ff
 		werr = WERR_INVALID_DATATYPE;
6729ff
@@ -921,6 +946,14 @@ WERROR NetGroupAddUser_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = WERR_GROUPNOTFOUND;
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	if (types.ids[0] != SID_NAME_DOM_GRP) {
6729ff
 		werr = WERR_GROUPNOTFOUND;
6729ff
@@ -959,6 +992,14 @@ WERROR NetGroupAddUser_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = WERR_USER_NOT_FOUND;
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	if (types.ids[0] != SID_NAME_USER) {
6729ff
 		werr = WERR_USER_NOT_FOUND;
6729ff
@@ -1065,6 +1106,14 @@ WERROR NetGroupDelUser_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = WERR_GROUPNOTFOUND;
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	if (types.ids[0] != SID_NAME_DOM_GRP) {
6729ff
 		werr = WERR_GROUPNOTFOUND;
6729ff
@@ -1104,6 +1153,14 @@ WERROR NetGroupDelUser_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = WERR_USER_NOT_FOUND;
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	if (types.ids[0] != SID_NAME_USER) {
6729ff
 		werr = WERR_USER_NOT_FOUND;
6729ff
@@ -1515,6 +1572,14 @@ WERROR NetGroupGetUsers_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (group_rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (name_types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	status = dcerpc_samr_OpenGroup(b, talloc_tos(),
6729ff
 				       &domain_handle,
6729ff
@@ -1691,6 +1756,14 @@ WERROR NetGroupSetUsers_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (group_rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (group_types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	status = dcerpc_samr_OpenGroup(b, talloc_tos(),
6729ff
 				       &domain_handle,
6729ff
@@ -1769,6 +1842,15 @@ WERROR NetGroupSetUsers_r(struct libnetapi_ctx *ctx,
6729ff
 		goto done;
6729ff
 	}
6729ff
 
6729ff
+	if (r->in.num_entries != user_rids.count) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (r->in.num_entries != name_types.count) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+
6729ff
 	member_rids = user_rids.ids;
6729ff
 
6729ff
 	status = dcerpc_samr_QueryGroupMember(b, talloc_tos(),
6729ff
diff --git a/source3/lib/netapi/localgroup.c b/source3/lib/netapi/localgroup.c
6729ff
index 1a544ad..17cab68 100644
6729ff
--- a/source3/lib/netapi/localgroup.c
6729ff
+++ b/source3/lib/netapi/localgroup.c
6729ff
@@ -58,6 +58,12 @@ static NTSTATUS libnetapi_samr_lookup_and_open_alias(TALLOC_CTX *mem_ctx,
6729ff
 	if (!NT_STATUS_IS_OK(result)) {
6729ff
 		return result;
6729ff
 	}
6729ff
+	if (user_rids.count != 1) {
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+	}
6729ff
+	if (name_types.count != 1) {
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+	}
6729ff
 
6729ff
 	switch (name_types.ids[0]) {
6729ff
 		case SID_NAME_ALIAS:
6729ff
@@ -1041,7 +1047,7 @@ static NTSTATUS libnetapi_lsa_lookup_names3(TALLOC_CTX *mem_ctx,
6729ff
 	NT_STATUS_NOT_OK_RETURN(result);
6729ff
 
6729ff
 	if (count != 1 || sids.count != 1) {
6729ff
-		return NT_STATUS_NONE_MAPPED;
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
 	}
6729ff
 
6729ff
 	sid_copy(sid, sids.sids[0].sid);
6729ff
diff --git a/source3/lib/netapi/user.c b/source3/lib/netapi/user.c
6729ff
index df3914c..e699ad3 100644
6729ff
--- a/source3/lib/netapi/user.c
6729ff
+++ b/source3/lib/netapi/user.c
6729ff
@@ -604,6 +604,14 @@ WERROR NetUserDel_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (user_rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (name_types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	status = dcerpc_samr_OpenUser(b, talloc_tos(),
6729ff
 				      &domain_handle,
6729ff
@@ -1803,6 +1811,14 @@ WERROR NetUserGetInfo_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (user_rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (name_types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	status = libnetapi_samr_lookup_user_map_USER_INFO(ctx, pipe_cli,
6729ff
 							  domain_sid,
6729ff
@@ -1968,6 +1984,14 @@ WERROR NetUserSetInfo_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (user_rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (name_types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	status = dcerpc_samr_OpenUser(b, talloc_tos(),
6729ff
 				      &domain_handle,
6729ff
@@ -3027,6 +3051,14 @@ WERROR NetUserGetGroups_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (user_rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (name_types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	status = dcerpc_samr_OpenUser(b, talloc_tos(),
6729ff
 				      &domain_handle,
6729ff
@@ -3202,6 +3234,14 @@ WERROR NetUserSetGroups_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (user_rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (name_types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	status = dcerpc_samr_OpenUser(b, talloc_tos(),
6729ff
 				      &domain_handle,
6729ff
@@ -3262,6 +3302,14 @@ WERROR NetUserSetGroups_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (group_rids.count != r->in.num_entries) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (name_types.count != r->in.num_entries) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	member_rids = group_rids.ids;
6729ff
 
6729ff
@@ -3539,6 +3587,14 @@ WERROR NetUserGetLocalGroups_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (user_rids.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (name_types.count != 1) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	status = dcerpc_samr_OpenUser(b, talloc_tos(),
6729ff
 				      &domain_handle,
6729ff
diff --git a/source3/libnet/libnet_join.c b/source3/libnet/libnet_join.c
6729ff
index dfe7283..29d1944 100644
6729ff
--- a/source3/libnet/libnet_join.c
6729ff
+++ b/source3/libnet/libnet_join.c
6729ff
@@ -1023,6 +1023,14 @@ static NTSTATUS libnet_join_joindomain_rpc(TALLOC_CTX *mem_ctx,
6729ff
 		status = result;
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (user_rids.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (name_types.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	if (name_types.ids[0] != SID_NAME_USER) {
6729ff
 		DEBUG(0,("%s is not a user account (type=%d)\n",
6729ff
@@ -1416,6 +1424,14 @@ static NTSTATUS libnet_join_unjoindomain_rpc(TALLOC_CTX *mem_ctx,
6729ff
 		status = result;
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (user_rids.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (name_types.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	if (name_types.ids[0] != SID_NAME_USER) {
6729ff
 		DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name,
6729ff
diff --git a/source3/rpc_client/cli_lsarpc.c b/source3/rpc_client/cli_lsarpc.c
6729ff
index 7cadd6e..974538b 100644
6729ff
--- a/source3/rpc_client/cli_lsarpc.c
6729ff
+++ b/source3/rpc_client/cli_lsarpc.c
6729ff
@@ -662,9 +662,19 @@ NTSTATUS dcerpc_lsa_lookup_names_generic(struct dcerpc_binding_handle *h,
6729ff
 		struct dom_sid *sid = &(*sids)[i];
6729ff
 
6729ff
 		if (use_lookupnames4) {
6729ff
+			if (i >= sid_array3.count) {
6729ff
+				*presult = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+				goto done;
6729ff
+			}
6729ff
+
6729ff
 			dom_idx		= sid_array3.sids[i].sid_index;
6729ff
 			(*types)[i]	= sid_array3.sids[i].sid_type;
6729ff
 		} else {
6729ff
+			if (i >= sid_array.count) {
6729ff
+				*presult = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+				goto done;
6729ff
+			}
6729ff
+
6729ff
 			dom_idx		= sid_array.sids[i].sid_index;
6729ff
 			(*types)[i]	= sid_array.sids[i].sid_type;
6729ff
 		}
6729ff
@@ -677,6 +687,14 @@ NTSTATUS dcerpc_lsa_lookup_names_generic(struct dcerpc_binding_handle *h,
6729ff
 			(*types)[i] = SID_NAME_UNKNOWN;
6729ff
 			continue;
6729ff
 		}
6729ff
+		if (domains == NULL) {
6729ff
+			*presult = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+			goto done;
6729ff
+		}
6729ff
+		if (dom_idx >= domains->count) {
6729ff
+			*presult = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+			goto done;
6729ff
+		}
6729ff
 
6729ff
 		if (use_lookupnames4) {
6729ff
 			sid_copy(sid, sid_array3.sids[i].sid);
6729ff
diff --git a/source3/rpc_server/netlogon/srv_netlog_nt.c b/source3/rpc_server/netlogon/srv_netlog_nt.c
6729ff
index 6a6c125..e5ca474 100644
6729ff
--- a/source3/rpc_server/netlogon/srv_netlog_nt.c
6729ff
+++ b/source3/rpc_server/netlogon/srv_netlog_nt.c
6729ff
@@ -580,7 +580,7 @@ static NTSTATUS samr_find_machine_account(TALLOC_CTX *mem_ctx,
6729ff
 		status = NT_STATUS_NO_SUCH_USER;
6729ff
 		goto out;
6729ff
 	}
6729ff
-	if (rids.count != types.count) {
6729ff
+	if (types.count != 1) {
6729ff
 		status = NT_STATUS_INVALID_PARAMETER;
6729ff
 		goto out;
6729ff
 	}
6729ff
diff --git a/source3/rpcclient/cmd_lsarpc.c b/source3/rpcclient/cmd_lsarpc.c
6729ff
index a7ee4e4..5c28b1c 100644
6729ff
--- a/source3/rpcclient/cmd_lsarpc.c
6729ff
+++ b/source3/rpcclient/cmd_lsarpc.c
6729ff
@@ -323,7 +323,7 @@ static NTSTATUS cmd_lsa_lookup_names4(struct rpc_pipe_client *cli,
6729ff
 
6729ff
 	uint32_t num_names;
6729ff
 	struct lsa_String *names;
6729ff
-	struct lsa_RefDomainList *domains;
6729ff
+	struct lsa_RefDomainList *domains = NULL;
6729ff
 	struct lsa_TransSidArray3 sids;
6729ff
 	uint32_t count = 0;
6729ff
 	int i;
6729ff
@@ -361,6 +361,10 @@ static NTSTATUS cmd_lsa_lookup_names4(struct rpc_pipe_client *cli,
6729ff
 		return result;
6729ff
 	}
6729ff
 
6729ff
+	if (sids.count != num_names) {
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+	}
6729ff
+
6729ff
 	for (i = 0; i < sids.count; i++) {
6729ff
 		fstring sid_str;
6729ff
 		sid_to_fstring(sid_str, sids.sids[i].sid);
6729ff
diff --git a/source3/rpcclient/cmd_samr.c b/source3/rpcclient/cmd_samr.c
6729ff
index 022230b..5bc8c0b 100644
6729ff
--- a/source3/rpcclient/cmd_samr.c
6729ff
+++ b/source3/rpcclient/cmd_samr.c
6729ff
@@ -385,7 +385,17 @@ static NTSTATUS cmd_samr_query_user(struct rpc_pipe_client *cli,
6729ff
 		if (!NT_STATUS_IS_OK(status)) {
6729ff
 			goto done;
6729ff
 		}
6729ff
+
6729ff
 		if (NT_STATUS_IS_OK(result)) {
6729ff
+			if (rids.count != 1) {
6729ff
+				status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+				goto done;
6729ff
+			}
6729ff
+			if (types.count != 1) {
6729ff
+				status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+				goto done;
6729ff
+			}
6729ff
+
6729ff
 			status = dcerpc_samr_OpenUser(b, mem_ctx,
6729ff
 						      &domain_pol,
6729ff
 						      access_mask,
6729ff
@@ -1453,6 +1463,15 @@ static NTSTATUS cmd_samr_delete_alias(struct rpc_pipe_client *cli,
6729ff
 			goto done;
6729ff
 		}
6729ff
 		if (NT_STATUS_IS_OK(result)) {
6729ff
+			if (rids.count != 1) {
6729ff
+				status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+				goto done;
6729ff
+			}
6729ff
+			if (types.count != 1) {
6729ff
+				status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+				goto done;
6729ff
+			}
6729ff
+
6729ff
 			status = dcerpc_samr_OpenAlias(b, mem_ctx,
6729ff
 						       &domain_pol,
6729ff
 						       access_mask,
6729ff
@@ -2115,6 +2134,14 @@ static NTSTATUS cmd_samr_lookup_names(struct rpc_pipe_client *cli,
6729ff
 		status = result;
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (rids.count != num_names) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (name_types.count != num_names) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	/* Display results */
6729ff
 
6729ff
@@ -2272,6 +2299,14 @@ static NTSTATUS cmd_samr_delete_dom_group(struct rpc_pipe_client *cli,
6729ff
 			status = result;
6729ff
 			goto done;
6729ff
 		}
6729ff
+		if (group_rids.count != 1) {
6729ff
+			status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+			goto done;
6729ff
+		}
6729ff
+		if (name_types.count != 1) {
6729ff
+			status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+			goto done;
6729ff
+		}
6729ff
 
6729ff
 		status = dcerpc_samr_OpenGroup(b, mem_ctx,
6729ff
 					       &domain_pol,
6729ff
@@ -2375,6 +2410,14 @@ static NTSTATUS cmd_samr_delete_dom_user(struct rpc_pipe_client *cli,
6729ff
 			status = result;
6729ff
 			goto done;
6729ff
 		}
6729ff
+		if (user_rids.count != 1) {
6729ff
+			status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+			goto done;
6729ff
+		}
6729ff
+		if (name_types.count != 1) {
6729ff
+			status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+			goto done;
6729ff
+		}
6729ff
 
6729ff
 		status = dcerpc_samr_OpenUser(b, mem_ctx,
6729ff
 					      &domain_pol,
6729ff
@@ -2763,6 +2806,14 @@ static NTSTATUS cmd_samr_chgpasswd(struct rpc_pipe_client *cli,
6729ff
 		status = result;
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (rids.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (types.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	status = dcerpc_samr_OpenUser(b, mem_ctx,
6729ff
 				      &domain_pol,
6729ff
@@ -3166,7 +3217,12 @@ static NTSTATUS cmd_samr_setuserinfo_int(struct rpc_pipe_client *cli,
6729ff
 		if (!NT_STATUS_IS_OK(result)) {
6729ff
 			return result;
6729ff
 		}
6729ff
-
6729ff
+		if (rids.count != 1) {
6729ff
+			return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		}
6729ff
+		if (types.count != 1) {
6729ff
+			return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		}
6729ff
 
6729ff
 		status = dcerpc_samr_OpenUser(b, mem_ctx,
6729ff
 					      &domain_pol,
6729ff
diff --git a/source3/smbd/lanman.c b/source3/smbd/lanman.c
6729ff
index b5598a4..e6b9530 100644
6729ff
--- a/source3/smbd/lanman.c
6729ff
+++ b/source3/smbd/lanman.c
6729ff
@@ -2629,6 +2629,14 @@ static bool api_NetUserGetGroups(struct smbd_server_connection *sconn,
6729ff
 			  nt_errstr(result)));
6729ff
 		goto close_domain;
6729ff
 	}
6729ff
+	if (rid.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto close_domain;
6729ff
+	}
6729ff
+	if (type.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto close_domain;
6729ff
+	}
6729ff
 
6729ff
 	if (type.ids[0] != SID_NAME_USER) {
6729ff
 		DEBUG(10, ("%s is a %s, not a user\n", UserName,
6729ff
diff --git a/source3/utils/net_rpc.c b/source3/utils/net_rpc.c
6729ff
index 870ad22..e0c8eea 100644
6729ff
--- a/source3/utils/net_rpc.c
6729ff
+++ b/source3/utils/net_rpc.c
6729ff
@@ -1667,6 +1667,14 @@ static NTSTATUS rpc_group_delete_internals(struct net_context *c,
6729ff
 		d_fprintf(stderr, _("Lookup of '%s' failed\n"),argv[0]);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (group_rids.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (name_types.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	switch (name_types.ids[0])
6729ff
 	{
6729ff
@@ -2074,6 +2082,14 @@ static NTSTATUS rpc_add_groupmem(struct rpc_pipe_client *pipe_hnd,
6729ff
 			  member);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (rids.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (rid_types.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	status = dcerpc_samr_OpenGroup(b, mem_ctx,
6729ff
 				       &domain_pol,
6729ff
@@ -2329,6 +2345,14 @@ static NTSTATUS rpc_del_groupmem(struct net_context *c,
6729ff
 			  member);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (rids.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (rid_types.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	status = dcerpc_samr_OpenGroup(b, mem_ctx,
6729ff
 				       &domain_pol,
6729ff
@@ -3112,9 +3136,15 @@ static NTSTATUS rpc_group_members_internals(struct net_context *c,
6729ff
 	if (rids.count != 1) {
6729ff
 		d_fprintf(stderr, _("Couldn't find group %s\n"),
6729ff
 			  argv[0]);
6729ff
-		return result;
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+	}
6729ff
+	if (rid_types.count != 1) {
6729ff
+		d_fprintf(stderr, _("Couldn't find group %s\n"),
6729ff
+			  argv[0]);
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
 	}
6729ff
 
6729ff
+
6729ff
 	if (rid_types.ids[0] == SID_NAME_DOM_GRP) {
6729ff
 		return rpc_list_group_members(c, pipe_hnd, mem_ctx, domain_name,
6729ff
 					      domain_sid, &domain_pol,
6729ff
@@ -6064,6 +6094,14 @@ static NTSTATUS rpc_trustdom_del_internals(struct net_context *c,
6729ff
 			acct_name, nt_errstr(result) );
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (user_rids.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (name_types.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	status = dcerpc_samr_OpenUser(b, mem_ctx,
6729ff
 				      &domain_pol,
6729ff
diff --git a/source3/utils/net_rpc_join.c b/source3/utils/net_rpc_join.c
6729ff
index 7167cf9..56799cd 100644
6729ff
--- a/source3/utils/net_rpc_join.c
6729ff
+++ b/source3/utils/net_rpc_join.c
6729ff
@@ -380,6 +380,15 @@ int net_rpc_join_newstyle(struct net_context *c, int argc, const char **argv)
6729ff
 			    ("error looking up rid for user %s: %s/%s\n",
6729ff
 			     acct_name, nt_errstr(status), nt_errstr(result)));
6729ff
 
6729ff
+	if (user_rids.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (name_types.count != 1) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
+
6729ff
 	if (name_types.ids[0] != SID_NAME_USER) {
6729ff
 		DEBUG(0, ("%s is not a user account (type=%d)\n", acct_name, name_types.ids[0]));
6729ff
 		goto done;
6729ff
diff --git a/source3/winbindd/winbindd_rpc.c b/source3/winbindd/winbindd_rpc.c
6729ff
index 7345ea7..6b88c84 100644
6729ff
--- a/source3/winbindd/winbindd_rpc.c
6729ff
+++ b/source3/winbindd/winbindd_rpc.c
6729ff
@@ -1063,7 +1063,7 @@ static NTSTATUS rpc_try_lookup_sids3(TALLOC_CTX *mem_ctx,
6729ff
 				     struct lsa_TransNameArray **pnames)
6729ff
 {
6729ff
 	struct lsa_TransNameArray2 lsa_names2;
6729ff
-	struct lsa_TransNameArray *names;
6729ff
+	struct lsa_TransNameArray *names = *pnames;
6729ff
 	uint32_t i, count;
6729ff
 	NTSTATUS status, result;
6729ff
 
6729ff
@@ -1088,10 +1088,6 @@ static NTSTATUS rpc_try_lookup_sids3(TALLOC_CTX *mem_ctx,
6729ff
 		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
 	}
6729ff
 
6729ff
-	names = talloc_zero(mem_ctx, struct lsa_TransNameArray);
6729ff
-	if (names == NULL) {
6729ff
-		return NT_STATUS_NO_MEMORY;
6729ff
-	}
6729ff
 	names->count = lsa_names2.count;
6729ff
 	names->names = talloc_array(names, struct lsa_TranslatedName,
6729ff
 				    names->count);
6729ff
@@ -1114,7 +1110,6 @@ static NTSTATUS rpc_try_lookup_sids3(TALLOC_CTX *mem_ctx,
6729ff
 			return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
 		}
6729ff
 	}
6729ff
-	*pnames = names;
6729ff
 	return result;
6729ff
 }
6729ff
 
6729ff
@@ -1124,7 +1119,7 @@ NTSTATUS rpc_lookup_sids(TALLOC_CTX *mem_ctx,
6729ff
 			 struct lsa_RefDomainList **pdomains,
6729ff
 			 struct lsa_TransNameArray **pnames)
6729ff
 {
6729ff
-	struct lsa_TransNameArray *names;
6729ff
+	struct lsa_TransNameArray *names = *pnames;
6729ff
 	struct rpc_pipe_client *cli = NULL;
6729ff
 	struct policy_handle lsa_policy;
6729ff
 	uint32_t count;
6729ff
@@ -1141,10 +1136,6 @@ NTSTATUS rpc_lookup_sids(TALLOC_CTX *mem_ctx,
6729ff
 					    pdomains, pnames);
6729ff
 	}
6729ff
 
6729ff
-	names = talloc_zero(mem_ctx, struct lsa_TransNameArray);
6729ff
-	if (names == NULL) {
6729ff
-		return NT_STATUS_NO_MEMORY;
6729ff
-	}
6729ff
 	status = dcerpc_lsa_LookupSids(cli->binding_handle, mem_ctx,
6729ff
 				       &lsa_policy, sids, pdomains,
6729ff
 				       names, LSA_LOOKUP_NAMES_ALL,
6729ff
@@ -1172,6 +1163,5 @@ NTSTATUS rpc_lookup_sids(TALLOC_CTX *mem_ctx,
6729ff
 		}
6729ff
 	}
6729ff
 
6729ff
-	*pnames = names;
6729ff
 	return result;
6729ff
 }
6729ff
diff --git a/source4/libcli/util/clilsa.c b/source4/libcli/util/clilsa.c
6729ff
index cc0dae5..0437352 100644
6729ff
--- a/source4/libcli/util/clilsa.c
6729ff
+++ b/source4/libcli/util/clilsa.c
6729ff
@@ -335,7 +335,11 @@ NTSTATUS smblsa_lookup_name(struct smbcli_state *cli,
6729ff
 	}
6729ff
 	if (sids.count != 1) {
6729ff
 		talloc_free(mem_ctx2);
6729ff
-		return NT_STATUS_UNSUCCESSFUL;
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+	}
6729ff
+	if (domains->count != 1) {
6729ff
+		talloc_free(mem_ctx2);
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
 	}
6729ff
 
6729ff
 	sid = domains->domains[0].sid;
6729ff
diff --git a/source4/libnet/groupinfo.c b/source4/libnet/groupinfo.c
6729ff
index 9060973..5c8b0f7 100644
6729ff
--- a/source4/libnet/groupinfo.c
6729ff
+++ b/source4/libnet/groupinfo.c
6729ff
@@ -88,11 +88,14 @@ static void continue_groupinfo_lookup(struct tevent_req *subreq)
6729ff
 		s->monitor_fn(&msg;;
6729ff
 	}
6729ff
 	
6729ff
-
6729ff
 	/* have we actually got name resolved
6729ff
 	   - we're looking for only one at the moment */
6729ff
-	if (s->lookup.out.rids->count == 0) {
6729ff
-		composite_error(c, NT_STATUS_NO_SUCH_USER);
6729ff
+	if (s->lookup.out.rids->count != s->lookup.in.num_names) {
6729ff
+		composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
+		return;
6729ff
+	}
6729ff
+	if (s->lookup.out.types->count != s->lookup.in.num_names) {
6729ff
+		composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
 		return;
6729ff
 	}
6729ff
 
6729ff
diff --git a/source4/libnet/groupman.c b/source4/libnet/groupman.c
6729ff
index 9771ea5..59a3f35 100644
6729ff
--- a/source4/libnet/groupman.c
6729ff
+++ b/source4/libnet/groupman.c
6729ff
@@ -219,13 +219,13 @@ static void continue_groupdel_name_found(struct tevent_req *subreq)
6729ff
 
6729ff
 	/* what to do when there's no group account to delete
6729ff
 	   and what if there's more than one rid resolved */
6729ff
-	if (!s->lookupname.out.rids->count) {
6729ff
-		c->status = NT_STATUS_NO_SUCH_GROUP;
6729ff
+	if (s->lookupname.out.rids->count != s->lookupname.in.num_names) {
6729ff
+		c->status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
 		composite_error(c, c->status);
6729ff
 		return;
6729ff
-
6729ff
-	} else if (!s->lookupname.out.rids->count > 1) {
6729ff
-		c->status = NT_STATUS_INVALID_ACCOUNT_NAME;
6729ff
+	}
6729ff
+	if (s->lookupname.out.types->count != s->lookupname.in.num_names) {
6729ff
+		c->status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
 		composite_error(c, c->status);
6729ff
 		return;
6729ff
 	}
6729ff
diff --git a/source4/libnet/libnet_join.c b/source4/libnet/libnet_join.c
6729ff
index 0977403..8c1b57d 100644
6729ff
--- a/source4/libnet/libnet_join.c
6729ff
+++ b/source4/libnet/libnet_join.c
6729ff
@@ -656,9 +656,17 @@ NTSTATUS libnet_JoinDomain(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, stru
6729ff
 							      "samr_LookupNames for [%s] returns %d RIDs",
6729ff
 							      r->in.account_name, ln.out.rids->count);
6729ff
 			talloc_free(tmp_ctx);
6729ff
-			return NT_STATUS_INVALID_PARAMETER;
6729ff
+			return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
 		}
6729ff
-		
6729ff
+
6729ff
+		if (ln.out.types->count != 1) {
6729ff
+			r->out.error_string = talloc_asprintf(mem_ctx,
6729ff
+								"samr_LookupNames for [%s] returns %d RID TYPEs",
6729ff
+								r->in.account_name, ln.out.types->count);
6729ff
+			talloc_free(tmp_ctx);
6729ff
+			return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		}
6729ff
+
6729ff
 		/* prepare samr_OpenUser */
6729ff
 		ZERO_STRUCTP(u_handle);
6729ff
 		ou.in.domain_handle = &d_handle;
6729ff
diff --git a/source4/libnet/libnet_lookup.c b/source4/libnet/libnet_lookup.c
6729ff
index cf2d70c..77072b7 100644
6729ff
--- a/source4/libnet/libnet_lookup.c
6729ff
+++ b/source4/libnet/libnet_lookup.c
6729ff
@@ -365,6 +365,11 @@ static void continue_name_found(struct tevent_req *subreq)
6729ff
 	c->status = s->lookup.out.result;
6729ff
 	if (!composite_is_ok(c)) return;
6729ff
 
6729ff
+	if (s->lookup.out.sids->count != s->lookup.in.num_names) {
6729ff
+		composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
+		return;
6729ff
+	}
6729ff
+
6729ff
 	composite_done(c);
6729ff
 }
6729ff
 
6729ff
diff --git a/source4/libnet/libnet_passwd.c b/source4/libnet/libnet_passwd.c
6729ff
index 861d746..77176bc 100644
6729ff
--- a/source4/libnet/libnet_passwd.c
6729ff
+++ b/source4/libnet/libnet_passwd.c
6729ff
@@ -627,10 +627,18 @@ static NTSTATUS libnet_SetPassword_samr(struct libnet_context *ctx, TALLOC_CTX *
6729ff
 		r->samr.out.error_string = talloc_asprintf(mem_ctx,
6729ff
 						"samr_LookupNames for [%s] returns %d RIDs",
6729ff
 						r->samr.in.account_name, ln.out.rids->count);
6729ff
-		status = NT_STATUS_INVALID_PARAMETER;
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
 		goto disconnect;	
6729ff
 	}
6729ff
 
6729ff
+	if (ln.out.types->count != 1) {
6729ff
+		r->samr.out.error_string = talloc_asprintf(mem_ctx,
6729ff
+						"samr_LookupNames for [%s] returns %d RID TYPEs",
6729ff
+						r->samr.in.account_name, ln.out.types->count);
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto disconnect;
6729ff
+	}
6729ff
+
6729ff
 	/* prepare samr_OpenUser */
6729ff
 	ZERO_STRUCT(u_handle);
6729ff
 	ou.in.domain_handle = &d_handle;
6729ff
diff --git a/source4/libnet/userinfo.c b/source4/libnet/userinfo.c
6729ff
index 75c46e4..9530f9e 100644
6729ff
--- a/source4/libnet/userinfo.c
6729ff
+++ b/source4/libnet/userinfo.c
6729ff
@@ -90,8 +90,12 @@ static void continue_userinfo_lookup(struct tevent_req *subreq)
6729ff
 
6729ff
 	/* have we actually got name resolved
6729ff
 	   - we're looking for only one at the moment */
6729ff
-	if (s->lookup.out.rids->count == 0) {
6729ff
-		composite_error(c, NT_STATUS_NO_SUCH_USER);
6729ff
+	if (s->lookup.out.rids->count != s->lookup.in.num_names) {
6729ff
+		composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
+		return;
6729ff
+	}
6729ff
+	if (s->lookup.out.types->count != s->lookup.in.num_names) {
6729ff
+		composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
 		return;
6729ff
 	}
6729ff
 
6729ff
diff --git a/source4/libnet/userman.c b/source4/libnet/userman.c
6729ff
index c1ee017..a7301ea 100644
6729ff
--- a/source4/libnet/userman.c
6729ff
+++ b/source4/libnet/userman.c
6729ff
@@ -237,14 +237,12 @@ static void continue_userdel_name_found(struct tevent_req *subreq)
6729ff
 
6729ff
 	/* what to do when there's no user account to delete
6729ff
 	   and what if there's more than one rid resolved */
6729ff
-	if (!s->lookupname.out.rids->count) {
6729ff
-		c->status = NT_STATUS_NO_SUCH_USER;
6729ff
-		composite_error(c, c->status);
6729ff
+	if (s->lookupname.out.rids->count != s->lookupname.in.num_names) {
6729ff
+		composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
 		return;
6729ff
-
6729ff
-	} else if (!s->lookupname.out.rids->count > 1) {
6729ff
-		c->status = NT_STATUS_INVALID_ACCOUNT_NAME;
6729ff
-		composite_error(c, c->status);
6729ff
+	}
6729ff
+	if (s->lookupname.out.types->count != s->lookupname.in.num_names) {
6729ff
+		composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
 		return;
6729ff
 	}
6729ff
 
6729ff
@@ -513,14 +511,12 @@ static void continue_usermod_name_found(struct tevent_req *subreq)
6729ff
 
6729ff
 	/* what to do when there's no user account to delete
6729ff
 	   and what if there's more than one rid resolved */
6729ff
-	if (!s->lookupname.out.rids->count) {
6729ff
-		c->status = NT_STATUS_NO_SUCH_USER;
6729ff
-		composite_error(c, c->status);
6729ff
+	if (s->lookupname.out.rids->count != s->lookupname.in.num_names) {
6729ff
+		composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
 		return;
6729ff
-
6729ff
-	} else if (!s->lookupname.out.rids->count > 1) {
6729ff
-		c->status = NT_STATUS_INVALID_ACCOUNT_NAME;
6729ff
-		composite_error(c, c->status);
6729ff
+	}
6729ff
+	if (s->lookupname.out.types->count != s->lookupname.in.num_names) {
6729ff
+		composite_error(c, NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
 		return;
6729ff
 	}
6729ff
 
6729ff
diff --git a/source4/winbind/wb_async_helpers.c b/source4/winbind/wb_async_helpers.c
6729ff
index e3de2eb..2a05f12 100644
6729ff
--- a/source4/winbind/wb_async_helpers.c
6729ff
+++ b/source4/winbind/wb_async_helpers.c
6729ff
@@ -285,6 +285,12 @@ static void lsa_lookupnames_recv_sids(struct tevent_req *subreq)
6729ff
 		return;
6729ff
 	}
6729ff
 
6729ff
+	if (state->sids.count != state->num_names) {
6729ff
+		composite_error(state->ctx,
6729ff
+				NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
+		return;
6729ff
+	}
6729ff
+
6729ff
 	state->result = talloc_array(state, struct wb_sid_object *,
6729ff
 				     state->num_names);
6729ff
 	if (composite_nomem(state->result, state->ctx)) return;
6729ff
@@ -303,9 +309,14 @@ static void lsa_lookupnames_recv_sids(struct tevent_req *subreq)
6729ff
 			continue;
6729ff
 		}
6729ff
 
6729ff
+		if (domains == NULL) {
6729ff
+			composite_error(state->ctx,
6729ff
+					NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
+			return;
6729ff
+		}
6729ff
 		if (sid->sid_index >= domains->count) {
6729ff
 			composite_error(state->ctx,
6729ff
-					NT_STATUS_INVALID_PARAMETER);
6729ff
+					NT_STATUS_INVALID_NETWORK_RESPONSE);
6729ff
 			return;
6729ff
 		}
6729ff
 
6729ff
-- 
6729ff
1.7.9.5
6729ff
6729ff
6729ff
From 4e96ec86265ec504555f8542b735782589f73b17 Mon Sep 17 00:00:00 2001
6729ff
From: Jeremy Allison <jra@samba.org>
6729ff
Date: Thu, 7 Nov 2013 22:41:22 -0800
6729ff
Subject: [PATCH 16/16] CVE-2013-4408:s3:Ensure LookupRids() replies arrays
6729ff
 are range checked.
6729ff
6729ff
Bug: https://bugzilla.samba.org/show_bug.cgi?id=10185
6729ff
6729ff
Signed-off-by: Jeremy Allison <jra@samba.org>
6729ff
Signed-off-by: Stefan Metzmacher <metze@samba.org>
6729ff
---
6729ff
 source3/lib/netapi/group.c        |   16 ++++++++++++++++
6729ff
 source3/lib/netapi/user.c         |   16 ++++++++++++++++
6729ff
 source3/rpcclient/cmd_samr.c      |    8 ++++++++
6729ff
 source3/utils/net_rpc.c           |    7 ++++++-
6729ff
 source3/winbindd/winbindd_msrpc.c |   10 ++++++++--
6729ff
 source3/winbindd/winbindd_rpc.c   |   10 ++++++++--
6729ff
 6 files changed, 62 insertions(+), 5 deletions(-)
6729ff
6729ff
diff --git a/source3/lib/netapi/group.c b/source3/lib/netapi/group.c
6729ff
index 69c7af4..b806fc4 100644
6729ff
--- a/source3/lib/netapi/group.c
6729ff
+++ b/source3/lib/netapi/group.c
6729ff
@@ -395,6 +395,14 @@ WERROR NetGroupDel_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (names.count != rid_array->count) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (member_types.count != rid_array->count) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 	}
6729ff
 
6729ff
 	for (i=0; i < rid_array->count; i++) {
6729ff
@@ -1624,6 +1632,14 @@ WERROR NetGroupGetUsers_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (names.count != rid_array->count) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (member_types.count != rid_array->count) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	for (i=0; i < names.count; i++) {
6729ff
 
6729ff
diff --git a/source3/lib/netapi/user.c b/source3/lib/netapi/user.c
6729ff
index e699ad3..a971e2d 100644
6729ff
--- a/source3/lib/netapi/user.c
6729ff
+++ b/source3/lib/netapi/user.c
6729ff
@@ -3114,6 +3114,14 @@ WERROR NetUserGetGroups_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (names.count != rid_array->count) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (types.count != rid_array->count) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	for (i=0; i < names.count; i++) {
6729ff
 		status = add_GROUP_USERS_INFO_X_buffer(ctx,
6729ff
@@ -3716,6 +3724,14 @@ WERROR NetUserGetLocalGroups_r(struct libnetapi_ctx *ctx,
6729ff
 		werr = ntstatus_to_werror(result);
6729ff
 		goto done;
6729ff
 	}
6729ff
+	if (names.count != num_rids) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (types.count != num_rids) {
6729ff
+		werr = WERR_BAD_NET_RESP;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	for (i=0; i < names.count; i++) {
6729ff
 		status = add_LOCALGROUP_USERS_INFO_X_buffer(ctx,
6729ff
diff --git a/source3/rpcclient/cmd_samr.c b/source3/rpcclient/cmd_samr.c
6729ff
index 5bc8c0b..87882c3 100644
6729ff
--- a/source3/rpcclient/cmd_samr.c
6729ff
+++ b/source3/rpcclient/cmd_samr.c
6729ff
@@ -2223,6 +2223,14 @@ static NTSTATUS cmd_samr_lookup_rids(struct rpc_pipe_client *cli,
6729ff
 		goto done;
6729ff
 
6729ff
 	/* Display results */
6729ff
+	if (num_rids != names.count) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
+	if (num_rids != types.count) {
6729ff
+		status = NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		goto done;
6729ff
+	}
6729ff
 
6729ff
 	for (i = 0; i < num_rids; i++) {
6729ff
 		printf("rid 0x%x: %s (%d)\n",
6729ff
diff --git a/source3/utils/net_rpc.c b/source3/utils/net_rpc.c
6729ff
index e0c8eea..92964b5 100644
6729ff
--- a/source3/utils/net_rpc.c
6729ff
+++ b/source3/utils/net_rpc.c
6729ff
@@ -2900,7 +2900,12 @@ static NTSTATUS rpc_list_group_members(struct net_context *c,
6729ff
 		if (!NT_STATUS_IS_OK(result)) {
6729ff
 			return result;
6729ff
 		}
6729ff
-
6729ff
+		if (names.count != this_time) {
6729ff
+			return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		}
6729ff
+		if (types.count != this_time) {
6729ff
+			return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		}
6729ff
 		/* We only have users as members, but make the output
6729ff
 		   the same as the output of alias members */
6729ff
 
6729ff
diff --git a/source3/winbindd/winbindd_msrpc.c b/source3/winbindd/winbindd_msrpc.c
6729ff
index e86838c..0dc69c7 100644
6729ff
--- a/source3/winbindd/winbindd_msrpc.c
6729ff
+++ b/source3/winbindd/winbindd_msrpc.c
6729ff
@@ -744,14 +744,20 @@ static NTSTATUS msrpc_lookup_groupmem(struct winbindd_domain *domain,
6729ff
 		/* Copy result into array.  The talloc system will take
6729ff
 		   care of freeing the temporary arrays later on. */
6729ff
 
6729ff
-		if (tmp_names.count != tmp_types.count) {
6729ff
-			return NT_STATUS_UNSUCCESSFUL;
6729ff
+		if (tmp_names.count != num_lookup_rids) {
6729ff
+			return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+		}
6729ff
+		if (tmp_types.count != num_lookup_rids) {
6729ff
+			return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
 		}
6729ff
 
6729ff
 		for (r=0; r
6729ff
 			if (tmp_types.ids[r] == SID_NAME_UNKNOWN) {
6729ff
 				continue;
6729ff
 			}
6729ff
+			if (total_names >= *num_names) {
6729ff
+				break;
6729ff
+			}
6729ff
 			(*names)[total_names] = fill_domain_username_talloc(
6729ff
 				mem_ctx, domain->name,
6729ff
 				tmp_names.names[r].string, true);
6729ff
diff --git a/source3/winbindd/winbindd_rpc.c b/source3/winbindd/winbindd_rpc.c
6729ff
index 6b88c84..0986d82 100644
6729ff
--- a/source3/winbindd/winbindd_rpc.c
6729ff
+++ b/source3/winbindd/winbindd_rpc.c
6729ff
@@ -871,14 +871,20 @@ NTSTATUS rpc_lookup_groupmem(TALLOC_CTX *mem_ctx,
6729ff
 
6729ff
 	/* Copy result into array.  The talloc system will take
6729ff
 	   care of freeing the temporary arrays later on. */
6729ff
-	if (tmp_names.count != tmp_types.count) {
6729ff
-		return NT_STATUS_UNSUCCESSFUL;
6729ff
+	if (tmp_names.count != num_names) {
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
+	}
6729ff
+	if (tmp_types.count != num_names) {
6729ff
+		return NT_STATUS_INVALID_NETWORK_RESPONSE;
6729ff
 	}
6729ff
 
6729ff
 	for (r = 0; r < tmp_names.count; r++) {
6729ff
 		if (tmp_types.ids[r] == SID_NAME_UNKNOWN) {
6729ff
 			continue;
6729ff
 		}
6729ff
+		if (total_names >= num_names) {
6729ff
+			break;
6729ff
+		}
6729ff
 		names[total_names] = fill_domain_username_talloc(names,
6729ff
 								 domain_name,
6729ff
 								 tmp_names.names[r].string,
6729ff
-- 
6729ff
1.7.9.5
6729ff