X-Git-Url: https://git.exim.org/exim.git/blobdiff_plain/1e1ddfac79fbcd052f199500a6493c7f79cb8462..c6a290f4d8df3734b3cdc2232b4334ff8386c1da:/src/src/dkim.c diff --git a/src/src/dkim.c b/src/src/dkim.c index 92adb3589..0c5e84788 100644 --- a/src/src/dkim.c +++ b/src/src/dkim.c @@ -3,7 +3,7 @@ *************************************************/ /* Copyright (c) University of Cambridge, 1995 - 2018 */ -/* Copyright (c) The Exim Maintainers 2020 */ +/* Copyright (c) The Exim Maintainers 2020 - 2021*/ /* See the file NOTICE for conditions of use and distribution. */ /* Code for DKIM support. Other DKIM relevant code is in @@ -50,11 +50,11 @@ dkim_exim_query_dns_txt(const uschar * name) dns_answer * dnsa = store_get_dns_answer(); dns_scan dnss; rmark reset_point = store_mark(); -gstring * g = NULL; +gstring * g = string_get_tainted(256, TRUE); lookup_dnssec_authenticated = NULL; if (dns_lookup(dnsa, name, T_TXT, NULL) != DNS_SUCCEED) - return NULL; /*XXX better error detail? logging? */ + goto bad; /* Search for TXT record */ @@ -62,12 +62,8 @@ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); rr; rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)) if (rr->type == T_TXT) - { - int rr_offset = 0; - - /* Copy record content to the answer buffer */ - - while (rr_offset < rr->size) + { /* Copy record content to the answer buffer */ + for (int rr_offset = 0; rr_offset < rr->size; ) { uschar len = rr->data[rr_offset++]; @@ -78,18 +74,20 @@ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); rr_offset += len; } - /* check if this looks like a DKIM record */ + /* Check if this looks like a DKIM record */ if (Ustrncmp(g->s, "v=", 2) != 0 || strncasecmp(CS g->s, "v=dkim", 6) == 0) { + store_free_dns_answer(dnsa); gstring_release_unused(g); return string_from_gstring(g); } - if (g) g->ptr = 0; /* overwrite previous record */ + g->ptr = 0; /* overwrite previous record */ } bad: store_reset(reset_point); +store_free_dns_answer(dnsa); return NULL; /*XXX better error detail? logging? */ } @@ -109,12 +107,15 @@ dkim_exim_verify_init(BOOL dot_stuffing) { dkim_exim_init(); -/* There is a store-reset between header & body reception -so cannot use the main pool. Any allocs done by Exim -memory-handling must use the perm pool. */ +/* There is a store-reset between header & body reception for the main pool +(actually, after every header line) so cannot use that as we need the data we +store per-header, during header processing, at the end of body reception +for evaluating the signature. Any allocs done for dkim verify +memory-handling must use a different pool. We use a separate one that we +can reset per message. */ dkim_verify_oldpool = store_pool; -store_pool = POOL_PERM; +store_pool = POOL_MESSAGE; /* Free previous context if there is one */ @@ -127,19 +128,22 @@ dkim_verify_ctx = pdkim_init_verify(&dkim_exim_query_dns_txt, dot_stuffing); dkim_collect_input = dkim_verify_ctx ? DKIM_MAX_SIGNATURES : 0; dkim_collect_error = NULL; -/* Start feed up with any cached data */ -receive_get_cache(); +/* Start feed up with any cached data, but limited to message data */ +receive_get_cache(chunking_state == CHUNKING_LAST + ? chunking_data_left : GETC_BUFFER_UNLIMITED); store_pool = dkim_verify_oldpool; } +/* Submit a chunk of data for verification input. +Only use the data when the feed is activated. */ void dkim_exim_verify_feed(uschar * data, int len) { int rc; -store_pool = POOL_PERM; +store_pool = POOL_MESSAGE; if ( dkim_collect_input && (rc = pdkim_feed(dkim_verify_ctx, data, len)) != PDKIM_OK) { @@ -305,7 +309,7 @@ int rc; gstring * g = NULL; const uschar * errstr = NULL; -store_pool = POOL_PERM; +store_pool = POOL_MESSAGE; /* Delete eventual previous signature chain */