Skip to content

Commit a342752

Browse files
committed
validator: similarly also limit excessive NSEC3 salt length
Limit combination of iterations and salt length, based on estimated expense of the computation. Note that the result only differs for salt length > 44 which is rather nonsensical and very rare: https://chat.dns-oarc.net/community/pl/h58qx9sjkbgt9dajb7x988p78a
1 parent 0d31cd0 commit a342752

File tree

6 files changed

+28
-9
lines changed

6 files changed

+28
-9
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Improvements
55
------------
66
- update addresses of B.root-servers.net (!1478)
77
- validator: lower the NSEC3 iteration limit (150 -> 50; !1481)
8+
- validator: similarly also limit excessive NSEC3 salt length (!1481)
89

910
Bugfixes
1011
--------

lib/cache/api.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ static ssize_t stash_rrset(struct kr_cache *cache, const struct kr_query *qry,
500500
return kr_ok();
501501
}
502502
if (rr->type == KNOT_RRTYPE_NSEC3 && rr->rrs.count
503-
&& knot_nsec3_iters(rr->rrs.rdata) > KR_NSEC3_MAX_ITERATIONS) {
503+
&& kr_nsec3_limited_rdata(rr->rrs.rdata)) {
504504
/* This shouldn't happen often, thanks to downgrades during validation. */
505505
VERBOSE_MSG(qry, "=> skipping NSEC3 with too many iterations\n");
506506
return kr_ok();

lib/cache/nsec3.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static knot_db_val_t key_NSEC3_name(struct key *k, const knot_dname_t *name,
8484
.data = (uint8_t *)/*const-cast*/name,
8585
};
8686

87-
if (kr_fails_assert(nsec_p->libknot.iterations <= KR_NSEC3_MAX_ITERATIONS)) {
87+
if (kr_fails_assert(!kr_nsec3_limited_params(&nsec_p->libknot))) {
8888
/* This is mainly defensive; it shouldn't happen thanks to downgrades. */
8989
return VAL_EMPTY;
9090
}

lib/dnssec/nsec3.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static int hash_name(dnssec_binary_t *hash, const dnssec_nsec3_params_t *params,
7171
return kr_error(EINVAL);
7272
if (!name)
7373
return kr_error(EINVAL);
74-
if (kr_fails_assert(params->iterations <= KR_NSEC3_MAX_ITERATIONS)) {
74+
if (kr_fails_assert(!kr_nsec3_limited_params(params))) {
7575
/* This if is mainly defensive; it shouldn't happen. */
7676
return kr_error(EINVAL);
7777
}
@@ -565,7 +565,7 @@ int kr_nsec3_wildcard_answer_response_check(const knot_pkt_t *pkt, knot_section_
565565
const knot_rrset_t *rrset = knot_pkt_rr(sec, i);
566566
if (rrset->type != KNOT_RRTYPE_NSEC3)
567567
continue;
568-
if (knot_nsec3_iters(rrset->rrs.rdata) > KR_NSEC3_MAX_ITERATIONS) {
568+
if (kr_nsec3_limited_rdata(rrset->rrs.rdata)) {
569569
/* Avoid hashing with too many iterations.
570570
* If we get here, the `sname` wildcard probably ends up bogus,
571571
* but it gets downgraded to KR_RANK_INSECURE when validator

lib/dnssec/nsec3.h

+19-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#pragma once
66

77
#include <libknot/packet/pkt.h>
8+
#include <libknot/rrtype/nsec3.h>
9+
#include <libdnssec/nsec.h>
810

911
/** High numbers in NSEC3 iterations don't really help security
1012
*
@@ -13,7 +15,22 @@
1315
*
1416
https://datatracker.ietf.org/doc/html/rfc9276#name-recommendation-for-validati
1517
*/
16-
#define KR_NSEC3_MAX_ITERATIONS 50
18+
static inline bool kr_nsec3_limited(unsigned int iterations, unsigned int salt_len)
19+
{
20+
const int MAX_ITERATIONS = 50; // limit with short salt length
21+
// SHA1 works on 64-byte chunks.
22+
// On iterating we hash the salt + 20 bytes of the previous hash.
23+
int chunks_per_iter = (20 + salt_len - 1) / 64 + 1;
24+
return (iterations + 1) * chunks_per_iter > MAX_ITERATIONS + 1;
25+
}
26+
static inline bool kr_nsec3_limited_rdata(const knot_rdata_t *rd)
27+
{
28+
return kr_nsec3_limited(knot_nsec3_iters(rd), knot_nsec3_salt_len(rd));
29+
}
30+
static inline bool kr_nsec3_limited_params(const dnssec_nsec3_params_t *params)
31+
{
32+
return kr_nsec3_limited(params->iterations, params->salt.size);
33+
}
1734

1835
/**
1936
* Name error response check (RFC5155 7.2.2).
@@ -36,7 +53,7 @@ int kr_nsec3_name_error_response_check(const knot_pkt_t *pkt, knot_section_t sec
3653
* KNOT_ERANGE - NSEC3 RR that covers a wildcard
3754
* has been found, but has opt-out flag set;
3855
* otherwise - error.
39-
* Records over KR_NSEC3_MAX_ITERATIONS are skipped, so you probably get kr_error(ENOENT).
56+
* Too expensive NSEC3 records are skipped, so you probably get kr_error(ENOENT).
4057
*/
4158
int kr_nsec3_wildcard_answer_response_check(const knot_pkt_t *pkt, knot_section_t section_id,
4259
const knot_dname_t *sname, int trim_to_next);

lib/layer/validate.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,15 @@ static bool maybe_downgrade_nsec3(const ranked_rr_array_entry_t *e, struct kr_qu
128128
const knot_rdataset_t *rrs = &e->rr->rrs;
129129
knot_rdata_t *rd = rrs->rdata;
130130
for (int j = 0; j < rrs->count; ++j, rd = knot_rdataset_next(rd)) {
131-
if (knot_nsec3_iters(rd) > KR_NSEC3_MAX_ITERATIONS)
131+
if (kr_nsec3_limited_rdata(rd))
132132
goto do_downgrade;
133133
}
134134
return false;
135135

136136
do_downgrade: // we do this deep inside calls because of having signer name available
137-
VERBOSE_MSG(qry, "<= DNSSEC downgraded due to NSEC3 iterations %d > %d\n",
138-
(int)knot_nsec3_iters(rd), (int)KR_NSEC3_MAX_ITERATIONS);
137+
VERBOSE_MSG(qry,
138+
"<= DNSSEC downgraded due to expensive NSEC3: %d iterations, %d salt length\n",
139+
(int)knot_nsec3_iters(rd), (int)knot_nsec3_salt_len(rd));
139140
qry->flags.DNSSEC_WANT = false;
140141
qry->flags.DNSSEC_INSECURE = true;
141142
rank_records(qry, true, KR_RANK_INSECURE, vctx->zone_name);

0 commit comments

Comments
 (0)