The Samba-Bugzilla – Attachment 15914 Details for
Bug 14334
CVE-2020-10704 [FUZZING][SECURITY] Stack overflow in AD DC (C)LDAP server
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Patch for Master (v5)
CVE-2020-10704-master-v5.patch (text/plain), 80.33 KB, created by
Gary Lockyer
on 2020-04-15 20:54:01 UTC
(
hide
)
Description:
Patch for Master (v5)
Filename:
MIME Type:
Creator:
Gary Lockyer
Created:
2020-04-15 20:54:01 UTC
Size:
80.33 KB
patch
obsolete
>From 3714dfe08572d040416094ad9d0e22c527c35777 Mon Sep 17 00:00:00 2001 >From: Gary Lockyer <gary@catalyst.net.nz> >Date: Fri, 3 Apr 2020 12:18:03 +1300 >Subject: [PATCH 1/8] CVE-2020-10704: lib util asn1: Add ASN.1 max tree depth > >Add maximum parse tree depth to the call to asn1_init, which will be >used to limit the depth of the ASN.1 parse tree. > >Credit to OSS-Fuzz > >REF: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=20454 >BUG: https://bugzilla.samba.org/show_bug.cgi?id=14334 > >Signed-off-by: Gary Lockyer <gary@catalyst.net.nz> >Reviewed-by: Andrew Bartlett <abartlet@samba.org> >--- > auth/gensec/gensec_util.c | 2 +- > lib/fuzzing/fuzz_ldap_decode.c | 6 +++- > lib/util/asn1.c | 17 +++++++++- > lib/util/asn1.h | 9 +++++- > lib/util/tests/asn1_tests.c | 2 +- > libcli/auth/spnego_parse.c | 6 ++-- > libcli/cldap/cldap.c | 2 +- > libcli/ldap/ldap_message.c | 2 +- > source3/lib/tldap.c | 4 +-- > source3/lib/tldap_util.c | 4 +-- > source3/libsmb/clispnego.c | 4 +-- > source3/torture/torture.c | 2 +- > source4/auth/gensec/gensec_krb5.c | 4 +-- > source4/ldap_server/ldap_server.c | 2 +- > source4/libcli/ldap/ldap_client.c | 2 +- > source4/libcli/ldap/ldap_controls.c | 48 ++++++++++++++--------------- > 16 files changed, 71 insertions(+), 45 deletions(-) > >diff --git a/auth/gensec/gensec_util.c b/auth/gensec/gensec_util.c >index 20c9c2a1fbb..e185acc0c20 100644 >--- a/auth/gensec/gensec_util.c >+++ b/auth/gensec/gensec_util.c >@@ -76,7 +76,7 @@ NTSTATUS gensec_generate_session_info_pac(TALLOC_CTX *mem_ctx, > static bool gensec_gssapi_check_oid(const DATA_BLOB *blob, const char *oid) > { > bool ret = false; >- struct asn1_data *data = asn1_init(NULL); >+ struct asn1_data *data = asn1_init(NULL, ASN1_MAX_TREE_DEPTH); > > if (!data) return false; > >diff --git a/lib/fuzzing/fuzz_ldap_decode.c b/lib/fuzzing/fuzz_ldap_decode.c >index 659169aca96..d89ba637061 100644 >--- a/lib/fuzzing/fuzz_ldap_decode.c >+++ b/lib/fuzzing/fuzz_ldap_decode.c >@@ -34,7 +34,11 @@ int LLVMFuzzerTestOneInput(uint8_t *buf, size_t len) > struct ldap_message *ldap_msg; > NTSTATUS status; > >- asn1 = asn1_init(mem_ctx); >+ /* >+ * Need to limit the max parse tree depth to 250 to prevent >+ * ASAN detecting stack overflows. >+ */ >+ asn1 = asn1_init(mem_ctx, 250); > if (!asn1) { > goto out; > } >diff --git a/lib/util/asn1.c b/lib/util/asn1.c >index 6ae54d4cf20..868bd218c9e 100644 >--- a/lib/util/asn1.c >+++ b/lib/util/asn1.c >@@ -36,15 +36,19 @@ struct asn1_data { > off_t ofs; > struct nesting *nesting; > bool has_error; >+ unsigned depth; >+ unsigned max_depth; > }; > > /* allocate an asn1 structure */ >-struct asn1_data *asn1_init(TALLOC_CTX *mem_ctx) >+struct asn1_data *asn1_init(TALLOC_CTX *mem_ctx, unsigned max_depth) > { > struct asn1_data *ret = talloc_zero(mem_ctx, struct asn1_data); > if (ret == NULL) { > DEBUG(0,("asn1_init failed! out of memory\n")); >+ return ret; > } >+ ret->max_depth = max_depth; > return ret; > } > >@@ -480,6 +484,11 @@ bool asn1_check_BOOLEAN(struct asn1_data *data, bool v) > /* load a struct asn1_data structure with a lump of data, ready to be parsed */ > bool asn1_load(struct asn1_data *data, DATA_BLOB blob) > { >+ /* >+ * Save the maximum depth >+ */ >+ unsigned max_depth = data->max_depth; >+ > ZERO_STRUCTP(data); > data->data = (uint8_t *)talloc_memdup(data, blob.data, blob.length); > if (!data->data) { >@@ -487,6 +496,7 @@ bool asn1_load(struct asn1_data *data, DATA_BLOB blob) > return false; > } > data->length = blob.length; >+ data->max_depth = max_depth; > return true; > } > >@@ -1119,9 +1129,14 @@ bool asn1_extract_blob(struct asn1_data *asn1, TALLOC_CTX *mem_ctx, > */ > void asn1_load_nocopy(struct asn1_data *data, uint8_t *buf, size_t len) > { >+ /* >+ * Save max_depth >+ */ >+ unsigned max_depth = data->max_depth; > ZERO_STRUCTP(data); > data->data = buf; > data->length = len; >+ data->max_depth = max_depth; > } > > int asn1_peek_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size) >diff --git a/lib/util/asn1.h b/lib/util/asn1.h >index ddd69863574..fc365724e93 100644 >--- a/lib/util/asn1.h >+++ b/lib/util/asn1.h >@@ -45,7 +45,14 @@ typedef struct asn1_data ASN1_DATA; > > #define ASN1_MAX_OIDS 20 > >-struct asn1_data *asn1_init(TALLOC_CTX *mem_ctx); >+/* >+ * The maximum permitted depth for an ASN.1 parse tree, the limit is chosen >+ * to align with the value for windows. Note that this value will trigger >+ * ASAN stack overflow errors. >+ */ >+#define ASN1_MAX_TREE_DEPTH 512 >+ >+struct asn1_data *asn1_init(TALLOC_CTX *mem_ctx, unsigned max_depth); > void asn1_free(struct asn1_data *data); > bool asn1_has_error(const struct asn1_data *data); > void asn1_set_error(struct asn1_data *data); >diff --git a/lib/util/tests/asn1_tests.c b/lib/util/tests/asn1_tests.c >index e4b386ad785..ab5262c4ffb 100644 >--- a/lib/util/tests/asn1_tests.c >+++ b/lib/util/tests/asn1_tests.c >@@ -330,7 +330,7 @@ static bool test_asn1_Integer(struct torture_context *tctx) > DATA_BLOB blob; > int val; > >- data = asn1_init(mem_ctx); >+ data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > if (!data) { > goto err; > } >diff --git a/libcli/auth/spnego_parse.c b/libcli/auth/spnego_parse.c >index f538b44552c..f7f19b10778 100644 >--- a/libcli/auth/spnego_parse.c >+++ b/libcli/auth/spnego_parse.c >@@ -296,7 +296,7 @@ ssize_t spnego_read_data(TALLOC_CTX *mem_ctx, DATA_BLOB data, struct spnego_data > return ret; > } > >- asn1 = asn1_init(mem_ctx); >+ asn1 = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > if (asn1 == NULL) { > return -1; > } >@@ -339,7 +339,7 @@ ssize_t spnego_read_data(TALLOC_CTX *mem_ctx, DATA_BLOB data, struct spnego_data > > ssize_t spnego_write_data(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, struct spnego_data *spnego) > { >- struct asn1_data *asn1 = asn1_init(mem_ctx); >+ struct asn1_data *asn1 = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > ssize_t ret = -1; > > if (asn1 == NULL) { >@@ -411,7 +411,7 @@ bool spnego_write_mech_types(TALLOC_CTX *mem_ctx, > DATA_BLOB *blob) > { > bool ret = false; >- struct asn1_data *asn1 = asn1_init(mem_ctx); >+ struct asn1_data *asn1 = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > > if (asn1 == NULL) { > return false; >diff --git a/libcli/cldap/cldap.c b/libcli/cldap/cldap.c >index f609bf278e4..3f687728517 100644 >--- a/libcli/cldap/cldap.c >+++ b/libcli/cldap/cldap.c >@@ -233,7 +233,7 @@ static bool cldap_socket_recv_dgram(struct cldap_socket *c, > goto error; > } > >- asn1 = asn1_init(in); >+ asn1 = asn1_init(in, ASN1_MAX_TREE_DEPTH); > if (!asn1) { > goto nomem; > } >diff --git a/libcli/ldap/ldap_message.c b/libcli/ldap/ldap_message.c >index f21598374a1..ba82bddeab1 100644 >--- a/libcli/ldap/ldap_message.c >+++ b/libcli/ldap/ldap_message.c >@@ -390,7 +390,7 @@ _PUBLIC_ bool ldap_encode(struct ldap_message *msg, > const struct ldap_control_handler *control_handlers, > DATA_BLOB *result, TALLOC_CTX *mem_ctx) > { >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > int i, j; > > if (!data) return false; >diff --git a/source3/lib/tldap.c b/source3/lib/tldap.c >index d6c6e8859a6..bf5fc05d785 100644 >--- a/source3/lib/tldap.c >+++ b/source3/lib/tldap.c >@@ -632,7 +632,7 @@ static void tldap_msg_received(struct tevent_req *subreq) > goto fail; > } > >- data = asn1_init(talloc_tos()); >+ data = asn1_init(talloc_tos(), ASN1_MAX_TREE_DEPTH); > if (data == NULL) { > status = TLDAP_NO_MEMORY; > goto fail; >@@ -763,7 +763,7 @@ static struct tevent_req *tldap_req_create(TALLOC_CTX *mem_ctx, > if (req == NULL) { > return NULL; > } >- state->out = asn1_init(state); >+ state->out = asn1_init(state, ASN1_MAX_TREE_DEPTH); > if (state->out == NULL) { > goto err; > } >diff --git a/source3/lib/tldap_util.c b/source3/lib/tldap_util.c >index 1b86962a32e..168932a8a96 100644 >--- a/source3/lib/tldap_util.c >+++ b/source3/lib/tldap_util.c >@@ -644,7 +644,7 @@ static struct tevent_req *tldap_ship_paged_search( > struct tldap_control *pgctrl; > struct asn1_data *asn1 = NULL; > >- asn1 = asn1_init(state); >+ asn1 = asn1_init(state, ASN1_MAX_TREE_DEPTH); > if (asn1 == NULL) { > return NULL; > } >@@ -783,7 +783,7 @@ static void tldap_search_paged_done(struct tevent_req *subreq) > > TALLOC_FREE(state->cookie.data); > >- asn1 = asn1_init(talloc_tos()); >+ asn1 = asn1_init(talloc_tos(), ASN1_MAX_TREE_DEPTH); > if (tevent_req_nomem(asn1, req)) { > return; > } >diff --git a/source3/libsmb/clispnego.c b/source3/libsmb/clispnego.c >index 4a0fbcd73af..1608f6a9960 100644 >--- a/source3/libsmb/clispnego.c >+++ b/source3/libsmb/clispnego.c >@@ -50,7 +50,7 @@ bool spnego_parse_negTokenInit(TALLOC_CTX *ctx, > *secblob = data_blob_null; > } > >- data = asn1_init(talloc_tos()); >+ data = asn1_init(talloc_tos(), ASN1_MAX_TREE_DEPTH); > if (data == NULL) { > return false; > } >@@ -171,7 +171,7 @@ DATA_BLOB spnego_gen_krb5_wrap(TALLOC_CTX *ctx, const DATA_BLOB ticket, const ui > ASN1_DATA *data; > DATA_BLOB ret = data_blob_null; > >- data = asn1_init(talloc_tos()); >+ data = asn1_init(talloc_tos(), ASN1_MAX_TREE_DEPTH); > if (data == NULL) { > return data_blob_null; > } >diff --git a/source3/torture/torture.c b/source3/torture/torture.c >index 562957a34b0..d4db09d8408 100644 >--- a/source3/torture/torture.c >+++ b/source3/torture/torture.c >@@ -11881,7 +11881,7 @@ tldap_build_extended_control(enum tldap_extended_val val) > ZERO_STRUCT(empty_control); > > if (val != EXTENDED_NONE) { >- data = asn1_init(talloc_tos()); >+ data = asn1_init(talloc_tos(), ASN1_MAX_TREE_DEPTH); > > if (!data) { > return NULL; >diff --git a/source4/auth/gensec/gensec_krb5.c b/source4/auth/gensec/gensec_krb5.c >index 866ecc82133..45abbb97b6b 100644 >--- a/source4/auth/gensec/gensec_krb5.c >+++ b/source4/auth/gensec/gensec_krb5.c >@@ -438,7 +438,7 @@ static DATA_BLOB gensec_gssapi_gen_krb5_wrap(TALLOC_CTX *mem_ctx, const DATA_BLO > struct asn1_data *data; > DATA_BLOB ret = data_blob_null; > >- data = asn1_init(mem_ctx); >+ data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > if (!data || !ticket->data) { > return ret; > } >@@ -472,7 +472,7 @@ static DATA_BLOB gensec_gssapi_gen_krb5_wrap(TALLOC_CTX *mem_ctx, const DATA_BLO > static bool gensec_gssapi_parse_krb5_wrap(TALLOC_CTX *mem_ctx, const DATA_BLOB *blob, DATA_BLOB *ticket, uint8_t tok_id[2]) > { > bool ret = false; >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > int data_remaining; > > if (!data) { >diff --git a/source4/ldap_server/ldap_server.c b/source4/ldap_server/ldap_server.c >index 709b7bcacfa..6d329329909 100644 >--- a/source4/ldap_server/ldap_server.c >+++ b/source4/ldap_server/ldap_server.c >@@ -560,7 +560,7 @@ static void ldapsrv_call_read_done(struct tevent_req *subreq) > return; > } > >- asn1 = asn1_init(call); >+ asn1 = asn1_init(call, ASN1_MAX_TREE_DEPTH); > if (asn1 == NULL) { > ldapsrv_terminate_connection(conn, "no memory"); > return; >diff --git a/source4/libcli/ldap/ldap_client.c b/source4/libcli/ldap/ldap_client.c >index a5defbcb4e3..319ef3a60a7 100644 >--- a/source4/libcli/ldap/ldap_client.c >+++ b/source4/libcli/ldap/ldap_client.c >@@ -284,7 +284,7 @@ static void ldap_connection_recv_done(struct tevent_req *subreq) > return; > } > >- asn1 = asn1_init(conn); >+ asn1 = asn1_init(conn, ASN1_MAX_TREE_DEPTH); > if (asn1 == NULL) { > TALLOC_FREE(msg); > ldap_error_handler(conn, NT_STATUS_NO_MEMORY); >diff --git a/source4/libcli/ldap/ldap_controls.c b/source4/libcli/ldap/ldap_controls.c >index 716ca148308..df012a158e0 100644 >--- a/source4/libcli/ldap/ldap_controls.c >+++ b/source4/libcli/ldap/ldap_controls.c >@@ -32,7 +32,7 @@ static bool decode_server_sort_response(void *mem_ctx, DATA_BLOB in, void *_out) > { > void **out = (void **)_out; > DATA_BLOB attr; >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > struct ldb_sort_resp_control *lsrc; > > if (!data) return false; >@@ -79,7 +79,7 @@ static bool decode_server_sort_request(void *mem_ctx, DATA_BLOB in, void *_out) > void **out = (void **)_out; > DATA_BLOB attr; > DATA_BLOB rule; >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > struct ldb_server_sort_control **lssc; > int num; > >@@ -166,7 +166,7 @@ static bool decode_extended_dn_request(void *mem_ctx, DATA_BLOB in, void *_out) > return true; > } > >- data = asn1_init(mem_ctx); >+ data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > if (!data) return false; > > if (!asn1_load(data, in)) { >@@ -198,7 +198,7 @@ static bool decode_extended_dn_request(void *mem_ctx, DATA_BLOB in, void *_out) > static bool decode_sd_flags_request(void *mem_ctx, DATA_BLOB in, void *_out) > { > void **out = (void **)_out; >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > struct ldb_sd_flags_control *lsdfc; > > if (!data) return false; >@@ -232,7 +232,7 @@ static bool decode_sd_flags_request(void *mem_ctx, DATA_BLOB in, void *_out) > static bool decode_search_options_request(void *mem_ctx, DATA_BLOB in, void *_out) > { > void **out = (void **)_out; >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > struct ldb_search_options_control *lsoc; > > if (!data) return false; >@@ -267,7 +267,7 @@ static bool decode_paged_results_request(void *mem_ctx, DATA_BLOB in, void *_out > { > void **out = (void **)_out; > DATA_BLOB cookie; >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > struct ldb_paged_control *lprc; > > if (!data) return false; >@@ -316,7 +316,7 @@ static bool decode_dirsync_request(void *mem_ctx, DATA_BLOB in, void *_out) > { > void **out = (void **)_out; > DATA_BLOB cookie; >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > struct ldb_dirsync_control *ldc; > > if (!data) return false; >@@ -372,7 +372,7 @@ static bool decode_asq_control(void *mem_ctx, DATA_BLOB in, void *_out) > { > void **out = (void **)_out; > DATA_BLOB source_attribute; >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > struct ldb_asq_control *lac; > > if (!data) return false; >@@ -433,7 +433,7 @@ static bool decode_verify_name_request(void *mem_ctx, DATA_BLOB in, void *_out) > { > void **out = (void **)_out; > DATA_BLOB name; >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > struct ldb_verify_name_control *lvnc; > int len; > >@@ -485,7 +485,7 @@ static bool decode_verify_name_request(void *mem_ctx, DATA_BLOB in, void *_out) > static bool encode_verify_name_request(void *mem_ctx, void *in, DATA_BLOB *out) > { > struct ldb_verify_name_control *lvnc = talloc_get_type(in, struct ldb_verify_name_control); >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > DATA_BLOB gc_utf16; > > if (!data) return false; >@@ -528,7 +528,7 @@ static bool decode_vlv_request(void *mem_ctx, DATA_BLOB in, void *_out) > { > void **out = (void **)_out; > DATA_BLOB assertion_value, context_id; >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > struct ldb_vlv_req_control *lvrc; > > if (!data) return false; >@@ -626,7 +626,7 @@ static bool decode_vlv_response(void *mem_ctx, DATA_BLOB in, void *_out) > { > void **out = (void **)_out; > DATA_BLOB context_id; >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > struct ldb_vlv_resp_control *lvrc; > > if (!data) return false; >@@ -682,7 +682,7 @@ static bool decode_vlv_response(void *mem_ctx, DATA_BLOB in, void *_out) > static bool encode_server_sort_response(void *mem_ctx, void *in, DATA_BLOB *out) > { > struct ldb_sort_resp_control *lsrc = talloc_get_type(in, struct ldb_sort_resp_control); >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > > if (!data) return false; > >@@ -716,7 +716,7 @@ static bool encode_server_sort_response(void *mem_ctx, void *in, DATA_BLOB *out) > static bool encode_server_sort_request(void *mem_ctx, void *in, DATA_BLOB *out) > { > struct ldb_server_sort_control **lssc = talloc_get_type(in, struct ldb_server_sort_control *); >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > int num; > > if (!data) return false; >@@ -782,7 +782,7 @@ static bool encode_extended_dn_request(void *mem_ctx, void *in, DATA_BLOB *out) > return true; > } > >- data = asn1_init(mem_ctx); >+ data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > > if (!data) return false; > >@@ -810,7 +810,7 @@ static bool encode_extended_dn_request(void *mem_ctx, void *in, DATA_BLOB *out) > static bool encode_sd_flags_request(void *mem_ctx, void *in, DATA_BLOB *out) > { > struct ldb_sd_flags_control *lsdfc = talloc_get_type(in, struct ldb_sd_flags_control); >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > > if (!data) return false; > >@@ -838,7 +838,7 @@ static bool encode_sd_flags_request(void *mem_ctx, void *in, DATA_BLOB *out) > static bool encode_search_options_request(void *mem_ctx, void *in, DATA_BLOB *out) > { > struct ldb_search_options_control *lsoc = talloc_get_type(in, struct ldb_search_options_control); >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > > if (!data) return false; > >@@ -866,7 +866,7 @@ static bool encode_search_options_request(void *mem_ctx, void *in, DATA_BLOB *ou > static bool encode_paged_results_request(void *mem_ctx, void *in, DATA_BLOB *out) > { > struct ldb_paged_control *lprc = talloc_get_type(in, struct ldb_paged_control); >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > > if (!data) return false; > >@@ -901,7 +901,7 @@ static bool encode_paged_results_request(void *mem_ctx, void *in, DATA_BLOB *out > static bool encode_asq_control(void *mem_ctx, void *in, DATA_BLOB *out) > { > struct ldb_asq_control *lac = talloc_get_type(in, struct ldb_asq_control); >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > > if (!data) return false; > >@@ -936,7 +936,7 @@ static bool encode_asq_control(void *mem_ctx, void *in, DATA_BLOB *out) > static bool encode_dirsync_request(void *mem_ctx, void *in, DATA_BLOB *out) > { > struct ldb_dirsync_control *ldc = talloc_get_type(in, struct ldb_dirsync_control); >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > > if (!data) return false; > >@@ -972,7 +972,7 @@ static bool encode_dirsync_request(void *mem_ctx, void *in, DATA_BLOB *out) > static bool encode_vlv_request(void *mem_ctx, void *in, DATA_BLOB *out) > { > struct ldb_vlv_req_control *lvrc = talloc_get_type(in, struct ldb_vlv_req_control); >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > > if (!data) return false; > >@@ -1040,7 +1040,7 @@ static bool encode_vlv_request(void *mem_ctx, void *in, DATA_BLOB *out) > static bool encode_vlv_response(void *mem_ctx, void *in, DATA_BLOB *out) > { > struct ldb_vlv_resp_control *lvrc = talloc_get_type(in, struct ldb_vlv_resp_control); >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > > if (!data) return false; > >@@ -1083,7 +1083,7 @@ static bool encode_openldap_dereference(void *mem_ctx, void *in, DATA_BLOB *out) > { > struct dsdb_openldap_dereference_control *control = talloc_get_type(in, struct dsdb_openldap_dereference_control); > int i,j; >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > > if (!data) return false; > >@@ -1132,7 +1132,7 @@ static bool encode_openldap_dereference(void *mem_ctx, void *in, DATA_BLOB *out) > static bool decode_openldap_dereference(void *mem_ctx, DATA_BLOB in, void *_out) > { > void **out = (void **)_out; >- struct asn1_data *data = asn1_init(mem_ctx); >+ struct asn1_data *data = asn1_init(mem_ctx, ASN1_MAX_TREE_DEPTH); > struct dsdb_openldap_dereference_result_control *control; > struct dsdb_openldap_dereference_result **r = NULL; > int i = 0; >-- >2.17.1 > > >From b970314492a03e0d28bd446240522d25e87a9b20 Mon Sep 17 00:00:00 2001 >From: Gary Lockyer <gary@catalyst.net.nz> >Date: Thu, 2 Apr 2020 15:25:53 +1300 >Subject: [PATCH 2/8] CVE-2020-10704: libcli ldap: test recursion depth in > ldap_decode_filter_tree > >Add tests to check that ASN.1 ldap requests with deeply nested elements >are rejected. Previously there was no check on the on the depth of >nesting and excessive nesting could cause a stack overflow. > >Credit to OSS-Fuzz > >REF: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=20454 >BUG: https://bugzilla.samba.org/show_bug.cgi?id=14334 > >Signed-off-by: Gary Lockyer <gary@catalyst.net.nz> >Reviewed-by: Andrew Bartlett <abartlet@samba.org> >--- > libcli/ldap/tests/data/10000-or.dat | Bin 0 -> 39875 bytes > libcli/ldap/tests/data/ldap-recursive.dat | Bin 0 -> 970 bytes > libcli/ldap/tests/ldap_message_test.c | 271 ++++++++++++++++++++++ > libcli/ldap/wscript_build | 15 ++ > selftest/knownfail.d/ldap_message | 2 + > source4/selftest/tests.py | 2 + > 6 files changed, 290 insertions(+) > create mode 100644 libcli/ldap/tests/data/10000-or.dat > create mode 100644 libcli/ldap/tests/data/ldap-recursive.dat > create mode 100644 libcli/ldap/tests/ldap_message_test.c > create mode 100644 selftest/knownfail.d/ldap_message > >diff --git a/libcli/ldap/tests/data/10000-or.dat b/libcli/ldap/tests/data/10000-or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+~vt~@2GHJrNu@L=Vi2gT3{}ZBfi2gf7{}rPD >z4AD!7{v$;H9-@B>(J4g#8lry*(Laai7@~g)(LaXhA40Sb(cg#Y??UvqA=-rKZ$k9f >zA^NKjEkpE|A^M9D{dtH&i2gK0e-ffU4$(A3e-xrW4ACEiXcD5|57F<1=y${Kgx?Oo >z6+Rn&GyF#Q_3&%qSHrJ_Uk<+%elh$)`1$a2;b+6ogr5#S6@D`OMELRWW8p``kAxo% >zKNNm2{6P5r@O|NX!}o;m4&N2NGkiz*_V8`tTf?`6Zw}uSzA=15`1<g5;cLU!gs%#p >j37-yM5xy*ZDtt-!;_%7vg@5+>pNM_-6S3j9ea`;_Gkj_X > >literal 0 >HcmV?d00001 > >diff --git a/libcli/ldap/tests/data/ldap-recursive.dat b/libcli/ldap/tests/data/ldap-recursive.dat >new file mode 100644 >index 0000000000000000000000000000000000000000..dd18d857660df41c30024264d4623680a9404613 >GIT binary patch >literal 970 >zcmXqLVm`*i$ei57yq|@EixGsFz=)BNVWC1hcYab<YI2ElPGWI!yI@LYaY<%gdTC~H >hMrw**Vs5HI6LSYk6LX73%_tlV0qTc<<|rUK1OSbr11$gm > >literal 0 >HcmV?d00001 > >diff --git a/libcli/ldap/tests/ldap_message_test.c b/libcli/ldap/tests/ldap_message_test.c >new file mode 100644 >index 00000000000..9cc9cc5d8a0 >--- /dev/null >+++ b/libcli/ldap/tests/ldap_message_test.c >@@ -0,0 +1,271 @@ >+/* >+ * Unit tests for ldap_message. >+ * >+ * Copyright (C) Catalyst.NET Ltd 2020 >+ * >+ * This program is free software; you can redistribute it and/or modify >+ * it under the terms of the GNU General Public License as published by >+ * the Free Software Foundation; either version 3 of the License, or >+ * (at your option) any later version. >+ * >+ * This program is distributed in the hope that it will be useful, >+ * but WITHOUT ANY WARRANTY; without even the implied warranty of >+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+ * GNU General Public License for more details. >+ * >+ * You should have received a copy of the GNU General Public License >+ * along with this program. If not, see <http://www.gnu.org/licenses/>. >+ * >+ */ >+ >+/* >+ * from cmocka.c: >+ * These headers or their equivalents should be included prior to >+ * including >+ * this header file. >+ * >+ * #include <stdarg.h> >+ * #include <stddef.h> >+ * #include <setjmp.h> >+ * >+ * This allows test applications to use custom definitions of C standard >+ * library functions and types. >+ * >+ */ >+#include <stdarg.h> >+#include <stddef.h> >+#include <setjmp.h> >+#include <cmocka.h> >+ >+#include "lib/util/attr.h" >+#include "includes.h" >+#include "lib/util/asn1.h" >+#include "libcli/ldap/ldap_message.h" >+#include "libcli/ldap/ldap_proto.h" >+ >+/* >+ * declare the internal cmocka cm_print so we can output messages in >+ * sub unit format >+ */ >+void cm_print_error(const char * const format, ...); >+/* >+ * helper function and macro to compare an ldap error code constant with the >+ * coresponding nt_status code >+ */ >+#define NT_STATUS_LDAP_V(code) (0xF2000000 | code) >+static void _assert_ldap_status_equal( >+ int a, >+ NTSTATUS b, >+ const char * const file, >+ const int line) >+{ >+ _assert_int_equal(NT_STATUS_LDAP_V(a), NT_STATUS_V(b), file, line); >+} >+ >+#define assert_ldap_status_equal(a, b) \ >+ _assert_ldap_status_equal((a), (b), __FILE__, __LINE__) >+ >+/* >+ * helper function and macro to assert there were no errors in the last >+ * file operation >+ */ >+static void _assert_not_ferror( >+ FILE *f, >+ const char * const file, >+ const int line) >+{ >+ if (f == NULL || ferror(f)) { >+ cm_print_error("ferror (%d) %s\n", errno, strerror(errno)); >+ _fail(file, line); >+ } >+} >+ >+#define assert_not_ferror(f) \ >+ _assert_not_ferror((f), __FILE__, __LINE__) >+ >+struct test_ctx { >+}; >+ >+static int setup(void **state) >+{ >+ struct test_ctx *test_ctx; >+ >+ test_ctx = talloc_zero(NULL, struct test_ctx); >+ *state = test_ctx; >+ return 0; >+} >+ >+static int teardown(void **state) >+{ >+ struct test_ctx *test_ctx = talloc_get_type_abort(*state, >+ struct test_ctx); >+ >+ TALLOC_FREE(test_ctx); >+ return 0; >+} >+ >+/* >+ * Test that an empty request is handled correctly >+ */ >+static void test_empty_input(void **state) >+{ >+ struct test_ctx *test_ctx = talloc_get_type_abort( >+ *state, >+ struct test_ctx); >+ struct asn1_data *asn1; >+ struct ldap_message *ldap_msg; >+ NTSTATUS status; >+ uint8_t buf[0]; >+ size_t len = 0; >+ >+ >+ asn1 = asn1_init(test_ctx, ASN1_MAX_TREE_DEPTH); >+ assert_non_null(asn1); >+ >+ asn1_load_nocopy(asn1, buf, len); >+ >+ ldap_msg = talloc(test_ctx, struct ldap_message); >+ assert_non_null(ldap_msg); >+ >+ status = ldap_decode(asn1, samba_ldap_control_handlers(), ldap_msg); >+ assert_ldap_status_equal(LDAP_PROTOCOL_ERROR, status); >+} >+ >+/* >+ * Check that a request is rejected it it's recursion depth exceeds >+ * the maximum value specified. This test uses a very deeply nested query, >+ * 10,000 or clauses. >+ * >+ */ >+static void test_recursion_depth_large(void **state) >+{ >+ struct test_ctx *test_ctx = talloc_get_type_abort( >+ *state, >+ struct test_ctx); >+ struct asn1_data *asn1; >+ struct ldap_message *ldap_msg; >+ NTSTATUS status; >+ FILE *f = NULL; >+ uint8_t *buffer = NULL; >+ const size_t BUFF_SIZE = 1048576; >+ size_t len; >+ >+ >+ /* >+ * Load a test data file containg 10,000 or clauses in encoded as >+ * an ASN.1 packet. >+ */ >+ buffer = talloc_zero_array(test_ctx, uint8_t, BUFF_SIZE); >+ f = fopen("./libcli/ldap/tests/data/10000-or.dat", "r"); >+ assert_not_ferror(f); >+ len = fread(buffer, sizeof(uint8_t), BUFF_SIZE, f); >+ assert_not_ferror(f); >+ assert_true(len > 0); >+ >+ asn1 = asn1_init(test_ctx, ASN1_MAX_TREE_DEPTH); >+ assert_non_null(asn1); >+ asn1_load_nocopy(asn1, buffer, len); >+ >+ ldap_msg = talloc(test_ctx, struct ldap_message); >+ assert_non_null(ldap_msg); >+ >+ status = ldap_decode(asn1, samba_ldap_control_handlers(), ldap_msg); >+ assert_ldap_status_equal(LDAP_PROTOCOL_ERROR, status); >+} >+ >+/* >+ * Check that a request is not rejected it it's recursion depth equals the >+ * maximum value >+ */ >+static void test_recursion_depth_equals_max(void **state) >+{ >+ struct test_ctx *test_ctx = talloc_get_type_abort( >+ *state, >+ struct test_ctx); >+ struct asn1_data *asn1; >+ struct ldap_message *ldap_msg; >+ NTSTATUS status; >+ FILE *f = NULL; >+ uint8_t *buffer = NULL; >+ const size_t BUFF_SIZE = 1048576; >+ size_t len; >+ >+ >+ buffer = talloc_zero_array(test_ctx, uint8_t, BUFF_SIZE); >+ f = fopen("./libcli/ldap/tests/data/ldap-recursive.dat", "r"); >+ assert_not_ferror(f); >+ len = fread(buffer, sizeof(uint8_t), BUFF_SIZE, f); >+ assert_not_ferror(f); >+ assert_true(len > 0); >+ >+ asn1 = asn1_init(test_ctx, 4); >+ assert_non_null(asn1); >+ asn1_load_nocopy(asn1, buffer, len); >+ >+ ldap_msg = talloc(test_ctx, struct ldap_message); >+ assert_non_null(ldap_msg); >+ >+ status = ldap_decode(asn1, samba_ldap_control_handlers(), ldap_msg); >+ assert_true(NT_STATUS_IS_OK(status)); >+} >+ >+/* >+ * Check that a request is rejected it it's recursion depth is greater than the >+ * maximum value >+ */ >+static void test_recursion_depth_greater_than_max(void **state) >+{ >+ struct test_ctx *test_ctx = talloc_get_type_abort( >+ *state, >+ struct test_ctx); >+ struct asn1_data *asn1; >+ struct ldap_message *ldap_msg; >+ NTSTATUS status; >+ FILE *f = NULL; >+ uint8_t *buffer = NULL; >+ const size_t BUFF_SIZE = 1048576; >+ size_t len; >+ >+ >+ buffer = talloc_zero_array(test_ctx, uint8_t, BUFF_SIZE); >+ f = fopen("./libcli/ldap/tests/data/ldap-recursive.dat", "r"); >+ assert_not_ferror(f); >+ len = fread(buffer, sizeof(uint8_t), BUFF_SIZE, f); >+ assert_not_ferror(f); >+ assert_true(len > 0); >+ >+ asn1 = asn1_init(test_ctx, 3); >+ assert_non_null(asn1); >+ asn1_load_nocopy(asn1, buffer, len); >+ >+ ldap_msg = talloc(test_ctx, struct ldap_message); >+ assert_non_null(ldap_msg); >+ >+ status = ldap_decode(asn1, samba_ldap_control_handlers(), ldap_msg); >+ assert_ldap_status_equal(LDAP_PROTOCOL_ERROR, status); >+} >+ >+int main(_UNUSED_ int argc, _UNUSED_ const char **argv) >+{ >+ const struct CMUnitTest tests[] = { >+ cmocka_unit_test_setup_teardown( >+ test_empty_input, >+ setup, >+ teardown), >+ cmocka_unit_test_setup_teardown( >+ test_recursion_depth_large, >+ setup, >+ teardown), >+ cmocka_unit_test_setup_teardown( >+ test_recursion_depth_equals_max, >+ setup, >+ teardown), >+ cmocka_unit_test_setup_teardown( >+ test_recursion_depth_greater_than_max, >+ setup, >+ teardown), >+ }; >+ >+ cmocka_set_message_output(CM_OUTPUT_SUBUNIT); >+ return cmocka_run_group_tests(tests, NULL, NULL); >+} >diff --git a/libcli/ldap/wscript_build b/libcli/ldap/wscript_build >index db5b1df497a..a646685c751 100644 >--- a/libcli/ldap/wscript_build >+++ b/libcli/ldap/wscript_build >@@ -6,3 +6,18 @@ bld.SAMBA_LIBRARY('cli-ldap-common', > private_headers='ldap_message.h ldap_errors.h ldap_ndr.h', > deps='samba-util asn1util NDR_SECURITY tevent', > private_library=True) >+ >+bld.SAMBA_BINARY( >+ 'test_ldap_message', >+ source='tests/ldap_message_test.c', >+ deps=''' >+ cmocka >+ talloc >+ ldb >+ samba-util >+ asn1util >+ NDR_SECURITY >+ cli-ldap >+ ''', >+ for_selftest=True >+) >diff --git a/selftest/knownfail.d/ldap_message b/selftest/knownfail.d/ldap_message >new file mode 100644 >index 00000000000..242eff45e59 >--- /dev/null >+++ b/selftest/knownfail.d/ldap_message >@@ -0,0 +1,2 @@ >+^libcli.ldap.ldap_message.test_recursion_depth_greater_than_max\(none\) >+^libcli.ldap.ldap_message.test_recursion_depth_large\(none\) >diff --git a/source4/selftest/tests.py b/source4/selftest/tests.py >index 8162f5e1fbc..92cf73b480e 100755 >--- a/source4/selftest/tests.py >+++ b/source4/selftest/tests.py >@@ -1374,6 +1374,8 @@ plantestsuite("librpc.ndr.ndr", "none", > [os.path.join(bindir(), "test_ndr")]) > plantestsuite("librpc.ndr.ndr_macros", "none", > [os.path.join(bindir(), "test_ndr_macros")]) >+plantestsuite("libcli.ldap.ldap_message", "none", >+ [os.path.join(bindir(), "test_ldap_message")]) > > # process restart and limit tests, these break the environment so need to run > # in their own specific environment >-- >2.17.1 > > >From 7acd1f5e96a231bd1692a8613e1131e0b8e76648 Mon Sep 17 00:00:00 2001 >From: Gary Lockyer <gary@catalyst.net.nz> >Date: Wed, 8 Apr 2020 15:30:52 +1200 >Subject: [PATCH 3/8] CVE-2020-10704: lib util asn1: Check parse tree depth > >Check the current depth of the parse tree and reject the input if the >depth exceeds that passed to asn1_init > >Credit to OSS-Fuzz > >REF: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=20454 >BUG: https://bugzilla.samba.org/show_bug.cgi?id=14334 > >Signed-off-by: Gary Lockyer <gary@catalyst.net.nz> >Reviewed-by: Andrew Bartlett <abartlet@samba.org> >--- > lib/util/asn1.c | 13 +++++++++++++ > selftest/knownfail.d/ldap_message | 2 -- > 2 files changed, 13 insertions(+), 2 deletions(-) > delete mode 100644 selftest/knownfail.d/ldap_message > >diff --git a/lib/util/asn1.c b/lib/util/asn1.c >index 868bd218c9e..a9caf6906f6 100644 >--- a/lib/util/asn1.c >+++ b/lib/util/asn1.c >@@ -647,6 +647,16 @@ bool asn1_start_tag(struct asn1_data *data, uint8_t tag) > uint8_t b; > struct nesting *nesting; > >+ /* >+ * Check the depth of the parse tree and prevent it from growing >+ * too large. >+ */ >+ data->depth++; >+ if (data->depth > data->max_depth) { >+ data->has_error = true; >+ return false; >+ } >+ > if (!asn1_read_uint8(data, &b)) > return false; > >@@ -703,6 +713,9 @@ bool asn1_end_tag(struct asn1_data *data) > { > struct nesting *nesting; > >+ if (data->depth > 0) { >+ data->depth--; >+ } > /* make sure we read it all */ > if (asn1_tag_remaining(data) != 0) { > data->has_error = true; >diff --git a/selftest/knownfail.d/ldap_message b/selftest/knownfail.d/ldap_message >deleted file mode 100644 >index 242eff45e59..00000000000 >--- a/selftest/knownfail.d/ldap_message >+++ /dev/null >@@ -1,2 +0,0 @@ >-^libcli.ldap.ldap_message.test_recursion_depth_greater_than_max\(none\) >-^libcli.ldap.ldap_message.test_recursion_depth_large\(none\) >-- >2.17.1 > > >From 2b473d938285535521a59e6d80ced5f6039fc052 Mon Sep 17 00:00:00 2001 >From: Gary Lockyer <gary@catalyst.net.nz> >Date: Tue, 14 Apr 2020 13:32:32 +1200 >Subject: [PATCH 4/8] CVE-2020-10704: ldapserver tests: Limit search request > sizes > >Add tests to ensure that overly long (> 256000 bytes) LDAP search >requests are rejected. > >Credit to OSS-Fuzz > >REF: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=20454 >BUG: https://bugzilla.samba.org/show_bug.cgi?id=14334 > >Signed-off-by: Gary Lockyer <gary@catalyst.net.nz> >Reviewed-by: Andrew Bartlett <abartlet@samba.org> >--- > python/samba/tests/ldap_raw.py | 234 +++++++++++++++++++++++++++++++++ > selftest/knownfail.d/ldap_raw | 1 + > source4/selftest/tests.py | 5 + > 3 files changed, 240 insertions(+) > create mode 100644 python/samba/tests/ldap_raw.py > create mode 100644 selftest/knownfail.d/ldap_raw > >diff --git a/python/samba/tests/ldap_raw.py b/python/samba/tests/ldap_raw.py >new file mode 100644 >index 00000000000..334fabce230 >--- /dev/null >+++ b/python/samba/tests/ldap_raw.py >@@ -0,0 +1,234 @@ >+# Integration tests for the ldap server, using raw socket IO >+# >+# Tests for handling of malformed or large packets. >+# >+# Copyright (C) Catalyst.Net Ltd 2020 >+# >+# This program is free software; you can redistribute it and/or modify >+# it under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# This program is distributed in the hope that it will be useful, >+# but WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License >+# along with this program. If not, see <http://www.gnu.org/licenses/>. >+# >+ >+import socket >+ >+import samba.tests >+from samba.tests import TestCase >+ >+ >+# >+# LDAP Operations >+# >+SEARCH = b'\x63' >+ >+EQUALS = b'\xa3' >+ >+ >+# >+# ASN.1 Element types >+# >+BOOLEAN = b'\x01' >+INTEGER = b'\x02' >+OCTET_STRING = b'\x04' >+NULL = b'\x05' >+ENUMERATED = b'\x0a' >+SEQUENCE = b'\x30' >+SET = b'\x31' >+ >+ >+# >+# ASN.1 Helper functions. >+# >+def encode_element(ber_type, data): >+ ''' Encode an ASN.1 BER element. ''' >+ if data is None: >+ return ber_type + encode_length(0) >+ return ber_type + encode_length(len(data)) + data >+ >+ >+def encode_length(length): >+ ''' Encode the length of an ASN.1 BER element. ''' >+ >+ if length > 0xFFFFFF: >+ return b'\x84' + length.to_bytes(4, "big") >+ if length > 0xFFFF: >+ return b'\x83' + length.to_bytes(3, "big") >+ if length > 0xFF: >+ return b'\x82' + length.to_bytes(2, "big") >+ if length > 0x7F: >+ return b'\x81' + length.to_bytes(1, "big") >+ return length.to_bytes(1, "big") >+ >+ >+def encode_string(string): >+ ''' Encode an octet string ''' >+ return encode_element(OCTET_STRING, string) >+ >+ >+def encode_boolean(boolean): >+ ''' Encode a boolean value ''' >+ if boolean: >+ return encode_element(BOOLEAN, b'\xFF') >+ return encode_element(BOOLEAN, b'\x00') >+ >+ >+def encode_integer(integer): >+ ''' Encode an integer value ''' >+ bit_len = integer.bit_length() >+ byte_len = (bit_len // 8) + 1 >+ return encode_element(INTEGER, integer.to_bytes(byte_len, "big")) >+ >+ >+def encode_enumerated(enum): >+ ''' Encode an enumerated value ''' >+ return encode_element(ENUMERATED, enum.to_bytes(1, "big")) >+ >+ >+def encode_sequence(sequence): >+ ''' Encode a sequence ''' >+ return encode_element(SEQUENCE, sequence) >+ >+ >+class RawLdapTest(TestCase): >+ """A raw Ldap Test case.""" >+ >+ def setUp(self): >+ super(RawLdapTest, self).setUp() >+ >+ self.host = samba.tests.env_get_var_value('SERVER') >+ self.port = 389 >+ self.socket = None >+ self.connect() >+ >+ def tearDown(self): >+ self.disconnect() >+ super(RawLdapTest, self).tearDown() >+ >+ def disconnect(self): >+ ''' Disconnect from and clean up the connection to the server ''' >+ if self.socket is None: >+ return >+ self.socket.close() >+ self.socket = None >+ >+ def connect(self): >+ ''' Open a socket stream connection to the server ''' >+ try: >+ self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >+ self.socket.settimeout(10) >+ self.socket.connect((self.host, self.port)) >+ except socket.error: >+ self.socket.close() >+ raise >+ >+ def send(self, req): >+ ''' Send the request to the server ''' >+ try: >+ self.socket.sendall(req) >+ except socket.error: >+ self.disconnect() >+ raise >+ >+ def recv(self, num_recv=0xffff, timeout=None): >+ ''' recv an array of bytes from the server ''' >+ data = None >+ try: >+ if timeout is not None: >+ self.socket.settimeout(timeout) >+ data = self.socket.recv(num_recv, 0) >+ self.socket.settimeout(10) >+ if len(data) == 0: >+ self.disconnect() >+ return None >+ except socket.timeout: >+ # We ignore timeout's as the ldap server will drop the connection >+ # on the errors we're testing. So returning None on a timeout is >+ # the desired behaviour. >+ self.socket.settimeout(10) >+ except socket.error: >+ self.disconnect() >+ raise >+ return data >+ >+ def test_search_equals_maximum_permitted_size(self): >+ ''' >+ Check that an LDAP search request equal to the maximum size is accepted >+ ''' >+ >+ # Lets build an ldap search packet to query the RootDSE >+ header = encode_string(None) # Base DN, "" >+ header += encode_enumerated(0) # Enumeration scope >+ header += encode_enumerated(0) # Enumeration dereference >+ header += encode_integer(0) # Integer size limit >+ header += encode_integer(0) # Integer time limit >+ header += encode_boolean(False) # Boolean attributes only >+ >+ # >+ # build an equality search of the form x...x=y...y >+ # With the length of x...x and y...y chosen to generate an >+ # ldap request of 256000 bytes. >+ x = encode_string(b'x' * 127974) >+ y = encode_string(b'y' * 127979) >+ equals = encode_element(EQUALS, x + y) >+ trailer = encode_sequence(None) >+ search = encode_element(SEARCH, header + equals + trailer) >+ >+ msg_no = encode_integer(1) >+ packet = encode_sequence(msg_no + search) >+ # >+ # The length of the packet should be equal to the >+ # Maximum length of a search query >+ self.assertEqual(256000, len(packet)) >+ >+ self.send(packet) >+ data = self.recv() >+ self.assertIsNotNone(data) >+ >+ # Should be a sequence >+ self.assertEqual(SEQUENCE, data[0:1]) >+ >+ def test_search_exceeds_maximum_permitted_size(self): >+ ''' >+ Test that a search query longer than the maximum permitted >+ size is rejected. >+ ''' >+ >+ # Lets build an ldap search packet to query the RootDSE >+ header = encode_string(None) # Base DN, "" >+ header += encode_enumerated(0) # Enumeration scope >+ header += encode_enumerated(0) # Enumeration dereference >+ header += encode_integer(0) # Integer size limit >+ header += encode_integer(0) # Integer time limit >+ header += encode_boolean(False) # Boolean attributes only >+ >+ # >+ # build an equality search of the form x...x=y...y >+ # With the length of x...x and y...y chosen to generate an >+ # ldap request of 256001 bytes. >+ x = encode_string(b'x' * 127979) >+ y = encode_string(b'y' * 127975) >+ equals = encode_element(EQUALS, x + y) >+ trailer = encode_sequence(None) >+ search = encode_element(SEARCH, header + equals + trailer) >+ >+ msg_no = encode_integer(1) >+ packet = encode_sequence(msg_no + search) >+ # >+ # The length of the sequence data should be one greater than the >+ # Maximum length of a search query >+ self.assertEqual(256001, len(packet)) >+ >+ self.send(packet) >+ data = self.recv() >+ # >+ # The connection should be closed by the server and we should not >+ # see any data. >+ self.assertIsNone(data) >diff --git a/selftest/knownfail.d/ldap_raw b/selftest/knownfail.d/ldap_raw >new file mode 100644 >index 00000000000..8bd2ee55166 >--- /dev/null >+++ b/selftest/knownfail.d/ldap_raw >@@ -0,0 +1 @@ >+^samba.tests.ldap_raw.samba.tests.ldap_raw.RawLdapTest.test_search_exceeds_maximum_permitted_size\(ad_dc\) >diff --git a/source4/selftest/tests.py b/source4/selftest/tests.py >index 92cf73b480e..89d416e169a 100755 >--- a/source4/selftest/tests.py >+++ b/source4/selftest/tests.py >@@ -930,6 +930,11 @@ plantestsuite_loadlist("samba4.ldap_modify_order.normal_user.python(ad_dc_defaul > '$LOADLIST', > '$LISTOPT']) > >+planoldpythontestsuite("ad_dc", >+ "samba.tests.ldap_raw", >+ extra_args=['-U"$USERNAME%$PASSWORD"'], >+ environ={'TEST_ENV': 'ad_dc'}) >+ > plantestsuite_loadlist("samba4.tokengroups.krb5.python(ad_dc_default)", "ad_dc_default:local", [python, os.path.join(DSDB_PYTEST_DIR, "token_group.py"), '$SERVER', '-U"$USERNAME%$PASSWORD"', '--workgroup=$DOMAIN', '-k', 'yes', '$LOADLIST', '$LISTOPT']) > plantestsuite_loadlist("samba4.tokengroups.ntlm.python(ad_dc_default)", "ad_dc_default:local", [python, os.path.join(DSDB_PYTEST_DIR, "token_group.py"), '$SERVER', '-U"$USERNAME%$PASSWORD"', '--workgroup=$DOMAIN', '-k', 'no', '$LOADLIST', '$LISTOPT']) > plantestsuite("samba4.sam.python(fl2008r2dc)", "fl2008r2dc", [python, os.path.join(DSDB_PYTEST_DIR, "sam.py"), '$SERVER', '-U"$USERNAME%$PASSWORD"', '--workgroup=$DOMAIN']) >-- >2.17.1 > > >From 5ba57999eed359ffba0ebd618c024e1478537408 Mon Sep 17 00:00:00 2001 >From: Gary Lockyer <gary@catalyst.net.nz> >Date: Tue, 7 Apr 2020 09:09:01 +1200 >Subject: [PATCH 5/8] CVE-2020-10704: smb.conf: Add max ldap request sizes > >Add two new smb.conf parameters to control the maximum permitted ldap >request size. > >Adds: > ldap max anonymous request size default 250Kb > ldap max authenticated request size default 16Mb > >Credit to OSS-Fuzz > >REF: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=20454 >BUG: https://bugzilla.samba.org/show_bug.cgi?id=14334 > >Signed-off-by: Gary Lockyer <gary@catalyst.net.nz> >Reviewed-by: Andrew Bartlett <abartlet@samba.org> >--- > .../smbdotconf/ldap/ldapmaxanonrequest.xml | 18 ++++++++++++++++++ > .../smbdotconf/ldap/ldapmaxauthrequest.xml | 18 ++++++++++++++++++ > lib/param/loadparm.c | 5 +++++ > source3/param/loadparm.c | 3 +++ > 4 files changed, 44 insertions(+) > create mode 100644 docs-xml/smbdotconf/ldap/ldapmaxanonrequest.xml > create mode 100644 docs-xml/smbdotconf/ldap/ldapmaxauthrequest.xml > >diff --git a/docs-xml/smbdotconf/ldap/ldapmaxanonrequest.xml b/docs-xml/smbdotconf/ldap/ldapmaxanonrequest.xml >new file mode 100644 >index 00000000000..61bdcec674d >--- /dev/null >+++ b/docs-xml/smbdotconf/ldap/ldapmaxanonrequest.xml >@@ -0,0 +1,18 @@ >+<samba:parameter name="ldap max anonymous request size" >+ context="G" >+ type="integer" >+ xmlns:samba="http://www.samba.org/samba/DTD/samba-doc"> >+<description> >+ <para> >+ This parameter specifies the maximum permitted size (in bytes) >+ for an LDAP request received on an anonymous connection. >+ </para> >+ >+ <para> >+ If the request size exceeds this limit the request will be >+ rejected. >+ </para> >+</description> >+<value type="default">256000</value> >+<value type="example">500000</value> >+</samba:parameter> >diff --git a/docs-xml/smbdotconf/ldap/ldapmaxauthrequest.xml b/docs-xml/smbdotconf/ldap/ldapmaxauthrequest.xml >new file mode 100644 >index 00000000000..c5934f73f95 >--- /dev/null >+++ b/docs-xml/smbdotconf/ldap/ldapmaxauthrequest.xml >@@ -0,0 +1,18 @@ >+<samba:parameter name="ldap max authenticated request size" >+ context="G" >+ type="integer" >+ xmlns:samba="http://www.samba.org/samba/DTD/samba-doc"> >+<description> >+ <para> >+ This parameter specifies the maximum permitted size (in bytes) >+ for an LDAP request received on an authenticated connection. >+ </para> >+ >+ <para> >+ If the request size exceeds this limit the request will be >+ rejected. >+ </para> >+</description> >+<value type="default">16777216</value> >+<value type="example">4194304</value> >+</samba:parameter> >diff --git a/lib/param/loadparm.c b/lib/param/loadparm.c >index 4bee921e3c7..813766a6b60 100644 >--- a/lib/param/loadparm.c >+++ b/lib/param/loadparm.c >@@ -3056,6 +3056,11 @@ struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx) > > lpcfg_do_global_parameter(lp_ctx, "spotlight backend", "noindex"); > >+ lpcfg_do_global_parameter( >+ lp_ctx, "ldap max anonymous request size", "256000"); >+ lpcfg_do_global_parameter( >+ lp_ctx, "ldap max authenticated request size", "16777216"); >+ > for (i = 0; parm_table[i].label; i++) { > if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) { > lp_ctx->flags[i] |= FLAG_DEFAULT; >diff --git a/source3/param/loadparm.c b/source3/param/loadparm.c >index 5a2c9983b47..cbdc132e2f5 100644 >--- a/source3/param/loadparm.c >+++ b/source3/param/loadparm.c >@@ -956,6 +956,9 @@ static void init_globals(struct loadparm_context *lp_ctx, bool reinit_globals) > Globals.prefork_backoff_increment = 10; > Globals.prefork_maximum_backoff = 120; > >+ Globals.ldap_max_anonymous_request_size = 256000; >+ Globals.ldap_max_authenticated_request_size = 16777216; >+ > /* Now put back the settings that were set with lp_set_cmdline() */ > apply_lp_set_cmdline(); > } >-- >2.17.1 > > >From 4a80555be4ccf42e750883dd5fabb04a6c86717c Mon Sep 17 00:00:00 2001 >From: Gary Lockyer <gary@catalyst.net.nz> >Date: Wed, 8 Apr 2020 15:32:22 +1200 >Subject: [PATCH 6/8] CVE-2020-10704: S4 ldap server: Limit request sizes > >Check the size of authenticated and anonymous ldap requests and reject >them if they exceed the limits in smb.conf > >Credit to OSS-Fuzz > >REF: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=20454 >BUG: https://bugzilla.samba.org/show_bug.cgi?id=14334 > >Signed-off-by: Gary Lockyer <gary@catalyst.net.nz> >Reviewed-by: Andrew Bartlett <abartlet@samba.org> >--- > selftest/knownfail.d/ldap_raw | 1 - > source4/ldap_server/ldap_server.c | 96 ++++++++++++++++++++++++++++++- > 2 files changed, 95 insertions(+), 2 deletions(-) > delete mode 100644 selftest/knownfail.d/ldap_raw > >diff --git a/selftest/knownfail.d/ldap_raw b/selftest/knownfail.d/ldap_raw >deleted file mode 100644 >index 8bd2ee55166..00000000000 >--- a/selftest/knownfail.d/ldap_raw >+++ /dev/null >@@ -1 +0,0 @@ >-^samba.tests.ldap_raw.samba.tests.ldap_raw.RawLdapTest.test_search_exceeds_maximum_permitted_size\(ad_dc\) >diff --git a/source4/ldap_server/ldap_server.c b/source4/ldap_server/ldap_server.c >index 6d329329909..a730667abb9 100644 >--- a/source4/ldap_server/ldap_server.c >+++ b/source4/ldap_server/ldap_server.c >@@ -441,6 +441,10 @@ static void ldapsrv_accept_tls_done(struct tevent_req *subreq) > } > > static void ldapsrv_call_read_done(struct tevent_req *subreq); >+static NTSTATUS ldapsrv_packet_check( >+ void *private_data, >+ DATA_BLOB blob, >+ size_t *packet_size); > > static bool ldapsrv_call_read_next(struct ldapsrv_connection *conn) > { >@@ -494,7 +498,7 @@ static bool ldapsrv_call_read_next(struct ldapsrv_connection *conn) > conn->connection->event.ctx, > conn->sockets.active, > 7, /* initial_read_size */ >- ldap_full_packet, >+ ldapsrv_packet_check, > conn); > if (subreq == NULL) { > ldapsrv_terminate_connection(conn, "ldapsrv_call_read_next: " >@@ -520,6 +524,9 @@ static bool ldapsrv_call_read_next(struct ldapsrv_connection *conn) > } > > static void ldapsrv_call_process_done(struct tevent_req *subreq); >+static int ldapsrv_check_packet_size( >+ struct ldapsrv_connection *conn, >+ size_t size); > > static void ldapsrv_call_read_done(struct tevent_req *subreq) > { >@@ -530,6 +537,7 @@ static void ldapsrv_call_read_done(struct tevent_req *subreq) > struct ldapsrv_call *call; > struct asn1_data *asn1; > DATA_BLOB blob; >+ int ret = LDAP_SUCCESS; > > conn->sockets.read_req = NULL; > >@@ -560,6 +568,14 @@ static void ldapsrv_call_read_done(struct tevent_req *subreq) > return; > } > >+ ret = ldapsrv_check_packet_size(conn, blob.length); >+ if (ret != LDAP_SUCCESS) { >+ ldapsrv_terminate_connection( >+ conn, >+ "Request packet too large"); >+ return; >+ } >+ > asn1 = asn1_init(call, ASN1_MAX_TREE_DEPTH); > if (asn1 == NULL) { > ldapsrv_terminate_connection(conn, "no memory"); >@@ -1362,6 +1378,84 @@ static void ldapsrv_post_fork(struct task_server *task, struct process_details * > } > } > >+/* >+ * Check the size of an ldap request packet. >+ * >+ * For authenticated connections the maximum packet size is controlled by >+ * the smb.conf parameter "ldap max authenticated request size" >+ * >+ * For anonymous connections the maximum packet size is controlled by >+ * the smb.conf parameter "ldap max anonymous request size" >+ */ >+static int ldapsrv_check_packet_size( >+ struct ldapsrv_connection *conn, >+ size_t size) >+{ >+ bool is_anonymous = false; >+ size_t max_size = 0; >+ >+ max_size = lpcfg_ldap_max_anonymous_request_size(conn->lp_ctx); >+ if (size <= max_size) { >+ return LDAP_SUCCESS; >+ } >+ >+ /* >+ * Request is larger than the maximum unauthenticated request size. >+ * As this code is called frequently we avoid calling >+ * security_token_is_anonymous if possible >+ */ >+ if (conn->session_info != NULL && >+ conn->session_info->security_token != NULL) { >+ is_anonymous = security_token_is_anonymous( >+ conn->session_info->security_token); >+ } >+ >+ if (is_anonymous) { >+ DBG_WARNING( >+ "LDAP request size (%zu) exceeds (%zu)\n", >+ size, >+ max_size); >+ return LDAP_UNWILLING_TO_PERFORM; >+ } >+ >+ max_size = lpcfg_ldap_max_authenticated_request_size(conn->lp_ctx); >+ if (size > max_size) { >+ DBG_WARNING( >+ "LDAP request size (%zu) exceeds (%zu)\n", >+ size, >+ max_size); >+ return LDAP_UNWILLING_TO_PERFORM; >+ } >+ return LDAP_SUCCESS; >+ >+} >+ >+/* >+ * Check that the blob contains enough data to be a valid packet >+ * If there is a packet header check the size to ensure that it does not >+ * exceed the maximum sizes. >+ * >+ */ >+static NTSTATUS ldapsrv_packet_check( >+ void *private_data, >+ DATA_BLOB blob, >+ size_t *packet_size) >+{ >+ NTSTATUS ret; >+ struct ldapsrv_connection *conn = private_data; >+ int result = LDB_SUCCESS; >+ >+ ret = ldap_full_packet(private_data, blob, packet_size); >+ if (!NT_STATUS_IS_OK(ret)) { >+ return ret; >+ } >+ result = ldapsrv_check_packet_size(conn, *packet_size); >+ if (result != LDAP_SUCCESS) { >+ return NT_STATUS_LDAP(result); >+ } >+ return NT_STATUS_OK; >+} >+ > NTSTATUS server_service_ldap_init(TALLOC_CTX *ctx) > { > static const struct service_details details = { >-- >2.17.1 > > >From 2b1431fe3802d879b12e944822778d38bde2f627 Mon Sep 17 00:00:00 2001 >From: Gary Lockyer <gary@catalyst.net.nz> >Date: Wed, 8 Apr 2020 08:49:23 +1200 >Subject: [PATCH 7/8] CVE-2020-10704: libcli ldap_message: Add search size > limits to ldap_decode > >Add search request size limits to ldap_decode calls. > >The ldap server uses the smb.conf variable >"ldap max search request size" which defaults to 250Kb. >For cldap the limit is hard coded as 4096. > >Credit to OSS-Fuzz > >REF: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=20454 >BUG: https://bugzilla.samba.org/show_bug.cgi?id=14334 > >Signed-off-by: Gary Lockyer <gary@catalyst.net.nz> >Reviewed-by: Andrew Bartlett <abartlet@samba.org> >--- > .../smbdotconf/ldap/ldapmaxsearchrequest.xml | 18 ++++++++++++++ > lib/fuzzing/fuzz_ldap_decode.c | 9 ++++++- > lib/param/loadparm.c | 2 ++ > libcli/cldap/cldap.c | 18 +++++++++++--- > libcli/ldap/ldap_message.c | 1 + > libcli/ldap/ldap_message.h | 5 ++++ > libcli/ldap/tests/ldap_message_test.c | 24 +++++++++++++++---- > source3/param/loadparm.c | 1 + > source4/ldap_server/ldap_server.c | 10 ++++++-- > source4/libcli/ldap/ldap_client.c | 3 ++- > 10 files changed, 80 insertions(+), 11 deletions(-) > create mode 100644 docs-xml/smbdotconf/ldap/ldapmaxsearchrequest.xml > >diff --git a/docs-xml/smbdotconf/ldap/ldapmaxsearchrequest.xml b/docs-xml/smbdotconf/ldap/ldapmaxsearchrequest.xml >new file mode 100644 >index 00000000000..ebeb0816c01 >--- /dev/null >+++ b/docs-xml/smbdotconf/ldap/ldapmaxsearchrequest.xml >@@ -0,0 +1,18 @@ >+<samba:parameter name="ldap max search request size" >+ context="G" >+ type="integer" >+ xmlns:samba="http://www.samba.org/samba/DTD/samba-doc"> >+<description> >+ <para> >+ This parameter specifies the maximum permitted size (in bytes) >+ for an LDAP search request. >+ </para> >+ >+ <para> >+ If the request size exceeds this limit the request will be >+ rejected. >+ </para> >+</description> >+<value type="default">256000</value> >+<value type="example">4194304</value> >+</samba:parameter> >diff --git a/lib/fuzzing/fuzz_ldap_decode.c b/lib/fuzzing/fuzz_ldap_decode.c >index d89ba637061..e3bcf7b9d0a 100644 >--- a/lib/fuzzing/fuzz_ldap_decode.c >+++ b/lib/fuzzing/fuzz_ldap_decode.c >@@ -32,6 +32,12 @@ int LLVMFuzzerTestOneInput(uint8_t *buf, size_t len) > TALLOC_CTX *mem_ctx = talloc_init(__FUNCTION__); > struct asn1_data *asn1; > struct ldap_message *ldap_msg; >+ struct ldap_request_limits limits = { >+ /* >+ * The default size is currently 256000 bytes >+ */ >+ .max_search_size = 256000 >+ }; > NTSTATUS status; > > /* >@@ -50,7 +56,8 @@ int LLVMFuzzerTestOneInput(uint8_t *buf, size_t len) > goto out; > } > >- status = ldap_decode(asn1, samba_ldap_control_handlers(), ldap_msg); >+ status = ldap_decode( >+ asn1, &limits, samba_ldap_control_handlers(), ldap_msg); > > out: > talloc_free(mem_ctx); >diff --git a/lib/param/loadparm.c b/lib/param/loadparm.c >index 813766a6b60..777999a3a76 100644 >--- a/lib/param/loadparm.c >+++ b/lib/param/loadparm.c >@@ -3060,6 +3060,8 @@ struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx) > lp_ctx, "ldap max anonymous request size", "256000"); > lpcfg_do_global_parameter( > lp_ctx, "ldap max authenticated request size", "16777216"); >+ lpcfg_do_global_parameter( >+ lp_ctx, "ldap max search request size", "256000"); > > for (i = 0; parm_table[i].label; i++) { > if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) { >diff --git a/libcli/cldap/cldap.c b/libcli/cldap/cldap.c >index 3f687728517..7de72b5e899 100644 >--- a/libcli/cldap/cldap.c >+++ b/libcli/cldap/cldap.c >@@ -111,6 +111,11 @@ struct cldap_search_state { > struct tevent_req *req; > }; > >+/* >+ * For CLDAP we limit the maximum search request size to 4kb >+ */ >+#define MAX_SEARCH_REQUEST 4096 >+ > static int cldap_socket_destructor(struct cldap_socket *c) > { > while (c->searches.list) { >@@ -228,6 +233,9 @@ static bool cldap_socket_recv_dgram(struct cldap_socket *c, > void *p; > struct cldap_search_state *search; > NTSTATUS status; >+ struct ldap_request_limits limits = { >+ .max_search_size = MAX_SEARCH_REQUEST >+ }; > > if (in->recv_errno != 0) { > goto error; >@@ -246,7 +254,7 @@ static bool cldap_socket_recv_dgram(struct cldap_socket *c, > } > > /* this initial decode is used to find the message id */ >- status = ldap_decode(asn1, NULL, in->ldap_msg); >+ status = ldap_decode(asn1, &limits, NULL, in->ldap_msg); > if (!NT_STATUS_IS_OK(status)) { > goto nterror; > } >@@ -774,6 +782,9 @@ NTSTATUS cldap_search_recv(struct tevent_req *req, > struct cldap_search_state); > struct ldap_message *ldap_msg; > NTSTATUS status; >+ struct ldap_request_limits limits = { >+ .max_search_size = MAX_SEARCH_REQUEST >+ }; > > if (tevent_req_is_nterror(req, &status)) { > goto failed; >@@ -784,7 +795,7 @@ NTSTATUS cldap_search_recv(struct tevent_req *req, > goto nomem; > } > >- status = ldap_decode(state->response.asn1, NULL, ldap_msg); >+ status = ldap_decode(state->response.asn1, &limits, NULL, ldap_msg); > if (!NT_STATUS_IS_OK(status)) { > goto failed; > } >@@ -800,7 +811,8 @@ NTSTATUS cldap_search_recv(struct tevent_req *req, > *io->out.response = ldap_msg->r.SearchResultEntry; > > /* decode the 2nd part */ >- status = ldap_decode(state->response.asn1, NULL, ldap_msg); >+ status = ldap_decode( >+ state->response.asn1, &limits, NULL, ldap_msg); > if (!NT_STATUS_IS_OK(status)) { > goto failed; > } >diff --git a/libcli/ldap/ldap_message.c b/libcli/ldap/ldap_message.c >index ba82bddeab1..d38fa0b3b61 100644 >--- a/libcli/ldap/ldap_message.c >+++ b/libcli/ldap/ldap_message.c >@@ -1162,6 +1162,7 @@ static bool ldap_decode_attribs(TALLOC_CTX *mem_ctx, struct asn1_data *data, > /* This routine returns LDAP status codes */ > > _PUBLIC_ NTSTATUS ldap_decode(struct asn1_data *data, >+ const struct ldap_request_limits *limits, > const struct ldap_control_handler *control_handlers, > struct ldap_message *msg) > { >diff --git a/libcli/ldap/ldap_message.h b/libcli/ldap/ldap_message.h >index 2f64881c053..19bfb99ac97 100644 >--- a/libcli/ldap/ldap_message.h >+++ b/libcli/ldap/ldap_message.h >@@ -213,10 +213,15 @@ struct ldap_control_handler { > bool (*encode)(void *mem_ctx, void *in, DATA_BLOB *out); > }; > >+struct ldap_request_limits { >+ unsigned max_search_size; >+}; >+ > struct asn1_data; > > struct ldap_message *new_ldap_message(TALLOC_CTX *mem_ctx); > NTSTATUS ldap_decode(struct asn1_data *data, >+ const struct ldap_request_limits *limits, > const struct ldap_control_handler *control_handlers, > struct ldap_message *msg); > bool ldap_encode(struct ldap_message *msg, >diff --git a/libcli/ldap/tests/ldap_message_test.c b/libcli/ldap/tests/ldap_message_test.c >index 9cc9cc5d8a0..c5aacd4bc6b 100644 >--- a/libcli/ldap/tests/ldap_message_test.c >+++ b/libcli/ldap/tests/ldap_message_test.c >@@ -117,6 +117,9 @@ static void test_empty_input(void **state) > NTSTATUS status; > uint8_t buf[0]; > size_t len = 0; >+ struct ldap_request_limits limits = { >+ .max_search_size = 256000, >+ }; > > > asn1 = asn1_init(test_ctx, ASN1_MAX_TREE_DEPTH); >@@ -127,7 +130,8 @@ static void test_empty_input(void **state) > ldap_msg = talloc(test_ctx, struct ldap_message); > assert_non_null(ldap_msg); > >- status = ldap_decode(asn1, samba_ldap_control_handlers(), ldap_msg); >+ status = ldap_decode( >+ asn1, &limits, samba_ldap_control_handlers(), ldap_msg); > assert_ldap_status_equal(LDAP_PROTOCOL_ERROR, status); > } > >@@ -149,6 +153,9 @@ static void test_recursion_depth_large(void **state) > uint8_t *buffer = NULL; > const size_t BUFF_SIZE = 1048576; > size_t len; >+ struct ldap_request_limits limits = { >+ .max_search_size = 256000, >+ }; > > > /* >@@ -169,7 +176,8 @@ static void test_recursion_depth_large(void **state) > ldap_msg = talloc(test_ctx, struct ldap_message); > assert_non_null(ldap_msg); > >- status = ldap_decode(asn1, samba_ldap_control_handlers(), ldap_msg); >+ status = ldap_decode( >+ asn1, &limits, samba_ldap_control_handlers(), ldap_msg); > assert_ldap_status_equal(LDAP_PROTOCOL_ERROR, status); > } > >@@ -189,6 +197,9 @@ static void test_recursion_depth_equals_max(void **state) > uint8_t *buffer = NULL; > const size_t BUFF_SIZE = 1048576; > size_t len; >+ struct ldap_request_limits limits = { >+ .max_search_size = 256000, >+ }; > > > buffer = talloc_zero_array(test_ctx, uint8_t, BUFF_SIZE); >@@ -205,7 +216,8 @@ static void test_recursion_depth_equals_max(void **state) > ldap_msg = talloc(test_ctx, struct ldap_message); > assert_non_null(ldap_msg); > >- status = ldap_decode(asn1, samba_ldap_control_handlers(), ldap_msg); >+ status = ldap_decode( >+ asn1, &limits, samba_ldap_control_handlers(), ldap_msg); > assert_true(NT_STATUS_IS_OK(status)); > } > >@@ -225,6 +237,9 @@ static void test_recursion_depth_greater_than_max(void **state) > uint8_t *buffer = NULL; > const size_t BUFF_SIZE = 1048576; > size_t len; >+ struct ldap_request_limits limits = { >+ .max_search_size = 256000, >+ }; > > > buffer = talloc_zero_array(test_ctx, uint8_t, BUFF_SIZE); >@@ -241,7 +256,8 @@ static void test_recursion_depth_greater_than_max(void **state) > ldap_msg = talloc(test_ctx, struct ldap_message); > assert_non_null(ldap_msg); > >- status = ldap_decode(asn1, samba_ldap_control_handlers(), ldap_msg); >+ status = ldap_decode( >+ asn1, &limits, samba_ldap_control_handlers(), ldap_msg); > assert_ldap_status_equal(LDAP_PROTOCOL_ERROR, status); > } > >diff --git a/source3/param/loadparm.c b/source3/param/loadparm.c >index cbdc132e2f5..ce2aca2c5a4 100644 >--- a/source3/param/loadparm.c >+++ b/source3/param/loadparm.c >@@ -958,6 +958,7 @@ static void init_globals(struct loadparm_context *lp_ctx, bool reinit_globals) > > Globals.ldap_max_anonymous_request_size = 256000; > Globals.ldap_max_authenticated_request_size = 16777216; >+ Globals.ldap_max_search_request_size = 256000; > > /* Now put back the settings that were set with lp_set_cmdline() */ > apply_lp_set_cmdline(); >diff --git a/source4/ldap_server/ldap_server.c b/source4/ldap_server/ldap_server.c >index a730667abb9..a9b162b284e 100644 >--- a/source4/ldap_server/ldap_server.c >+++ b/source4/ldap_server/ldap_server.c >@@ -538,6 +538,7 @@ static void ldapsrv_call_read_done(struct tevent_req *subreq) > struct asn1_data *asn1; > DATA_BLOB blob; > int ret = LDAP_SUCCESS; >+ struct ldap_request_limits limits = {0}; > > conn->sockets.read_req = NULL; > >@@ -593,8 +594,13 @@ static void ldapsrv_call_read_done(struct tevent_req *subreq) > return; > } > >- status = ldap_decode(asn1, samba_ldap_control_handlers(), >- call->request); >+ limits.max_search_size = >+ lpcfg_ldap_max_search_request_size(conn->lp_ctx); >+ status = ldap_decode( >+ asn1, >+ &limits, >+ samba_ldap_control_handlers(), >+ call->request); > if (!NT_STATUS_IS_OK(status)) { > ldapsrv_terminate_connection(conn, nt_errstr(status)); > return; >diff --git a/source4/libcli/ldap/ldap_client.c b/source4/libcli/ldap/ldap_client.c >index 319ef3a60a7..abe4e523585 100644 >--- a/source4/libcli/ldap/ldap_client.c >+++ b/source4/libcli/ldap/ldap_client.c >@@ -277,6 +277,7 @@ static void ldap_connection_recv_done(struct tevent_req *subreq) > struct ldap_message *msg; > struct asn1_data *asn1; > DATA_BLOB blob; >+ struct ldap_request_limits limits = {0}; > > msg = talloc_zero(conn, struct ldap_message); > if (msg == NULL) { >@@ -306,7 +307,7 @@ static void ldap_connection_recv_done(struct tevent_req *subreq) > > asn1_load_nocopy(asn1, blob.data, blob.length); > >- status = ldap_decode(asn1, samba_ldap_control_handlers(), msg); >+ status = ldap_decode(asn1, &limits, samba_ldap_control_handlers(), msg); > asn1_free(asn1); > if (!NT_STATUS_IS_OK(status)) { > TALLOC_FREE(msg); >-- >2.17.1 > > >From 647e9ff864a95f117cf95369bd1433392f459a66 Mon Sep 17 00:00:00 2001 >From: Gary Lockyer <gary@catalyst.net.nz> >Date: Wed, 8 Apr 2020 10:46:44 +1200 >Subject: [PATCH 8/8] CVE-2020-10704 libcli ldap: Check search request lengths. > >Check the search request lengths against the limits passed to >ldap_decode. > >Credit to OSS-Fuzz > >REF: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=20454 >BUG: https://bugzilla.samba.org/show_bug.cgi?id=14334 > >Signed-off-by: Gary Lockyer <gary@catalyst.net.nz> >Reviewed-by: Andrew Bartlett <abartlet@samba.org> >--- > lib/util/asn1.c | 7 +++++++ > lib/util/asn1.h | 1 + > libcli/ldap/ldap_message.c | 4 ++++ > 3 files changed, 12 insertions(+) > >diff --git a/lib/util/asn1.c b/lib/util/asn1.c >index a9caf6906f6..dc3f43f33f4 100644 >--- a/lib/util/asn1.c >+++ b/lib/util/asn1.c >@@ -1175,3 +1175,10 @@ int asn1_peek_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size) > *packet_size = size; > return 0; > } >+ >+/* >+ * Get the length of the ASN.1 data >+ */ >+size_t asn1_get_length(const struct asn1_data *asn1) { >+ return asn1->length; >+} >diff --git a/lib/util/asn1.h b/lib/util/asn1.h >index fc365724e93..de92a767f14 100644 >--- a/lib/util/asn1.h >+++ b/lib/util/asn1.h >@@ -106,5 +106,6 @@ bool asn1_extract_blob(struct asn1_data *asn1, TALLOC_CTX *mem_ctx, > DATA_BLOB *pblob); > void asn1_load_nocopy(struct asn1_data *data, uint8_t *buf, size_t len); > int asn1_peek_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size); >+size_t asn1_get_length(const struct asn1_data *asn1); > > #endif /* _ASN_1_H */ >diff --git a/libcli/ldap/ldap_message.c b/libcli/ldap/ldap_message.c >index d38fa0b3b61..69a48279532 100644 >--- a/libcli/ldap/ldap_message.c >+++ b/libcli/ldap/ldap_message.c >@@ -1259,7 +1259,11 @@ _PUBLIC_ NTSTATUS ldap_decode(struct asn1_data *data, > struct ldap_SearchRequest *r = &msg->r.SearchRequest; > int sizelimit, timelimit; > const char **attrs = NULL; >+ size_t request_size = asn1_get_length(data); > msg->type = LDAP_TAG_SearchRequest; >+ if (request_size > limits->max_search_size) { >+ goto prot_err; >+ } > if (!asn1_start_tag(data, tag)) goto prot_err; > if (!asn1_read_OctetString_talloc(msg, data, &r->basedn)) goto prot_err; > if (!asn1_read_enumerated(data, (int *)(void *)&(r->scope))) goto prot_err; >-- >2.17.1 >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Flags:
abartlet
:
review+
gary
:
ci-passed+
Actions:
View
Attachments on
bug 14334
:
15889
|
15893
|
15894
|
15895
|
15896
|
15906
|
15907
|
15910
| 15914 |
15915
|
15916
|
15917
|
15918
|
15920
|
15922
|
15923