Commit Graph

376 Commits

Author SHA1 Message Date
Simon Kelley
3ae7f1ab0d Add --dnssec-limits option. 2024-02-12 23:11:35 +00:00
Simon Kelley
3c91bca943 Better stats and logging from DNSSEC resource limiting. 2024-02-12 23:11:35 +00:00
Simon Kelley
6f23a0a75e Rework validate-by-DS to avoid DoS vuln without arbitrary limits.
By calculating the hash of a DNSKEY once for each digest algo,
we reduce the hashing work from (no. DS) x (no. DNSKEY) to
(no. DNSKEY) x (no. distinct digests)

The number of distinct digests can never be more than 255 and
it's limited by which hashes we implement, so currently only 4.
2024-02-12 23:11:35 +00:00
Simon Kelley
c5aa221e44 Parameterise work limits for DNSSEC validation. 2024-02-12 23:11:35 +00:00
Simon Kelley
59d30390c9 Measure cryptographic work done by DNSSEC. 2024-02-12 23:11:34 +00:00
Simon Kelley
40595f80d9 Protection against pathalogical DNSSEC domains.
An attacker can create DNSSEC signed domains which need a lot of
work to verfify. We limit the number of crypto operations to
avoid DoS attacks by CPU exhaustion.
2024-02-12 23:11:34 +00:00
Simon Kelley
6d35601da4 Refactor the accumulated crud of years in process_reply(). 2024-02-05 22:33:09 +00:00
Simon Kelley
d4a6f3a93e Fix logic error in signed RR handling.
In extract_addresses() the "secure" argument is only set if the
whole reply is validated (ie the AD bit can be set). Even without
that, some records may be validated, and should be marked
as such in the cache.

Related, the DNS doctor code has to update the flags for individual
RRs as it works, not the global "secure" flag.
2024-02-02 21:36:56 +00:00
Simon Kelley
12ddb2a4b9 Cache SOAs and return them with cached NXDOMAIN/NODATA replies.
Now we can cache arbirary RRs, give more correct answers when
replying negative answers from cache.

To implement this needed the DNS-doctor code to be untangled from
find_soa(), so it should be under suspicion for any regresssions
in that department.
2024-02-01 23:37:11 +00:00
Simon Kelley
b5820d1fd8 Bump copyright to 2024. 2024-01-13 22:20:04 +00:00
Simon Kelley
768b45a023 Remove two-decade old hack.
answer_request() builds answers in the same packet buffer
as the request.  This means that any EDNS0 header from the
original request is overwritten. If the answer is in cache, that's
fine: dnsmasq adds its own EDNS0 header, but if the cache lookup fails
partially and the request needs to be sent upstream, it's a problem.

This was fixed a long, long time ago by running the cache
lookup twice if the request included an EDNS0 header. The first time,
nothing would be written to the answer packet, nad if the cache
lookup failed, the untouched question packet was still available
to forward upstream. If cache lookup succeeded, the whole thing
was done again, this time writing the data into the reply packet.
In a world where EDNS0 was rare and so was memory, this was a
reasonable solution. Today EDNS0 is ubiquitous so basically
every query is being looked up twice in the cache. There's also
the problem that any code change which makes successive cache lookups
for a query possibly return different answers adds a subtle hidden
bug, because this hack depends on absence of that behaviour.

This commit removes the lookup-twice hack entirely. answer_request()
can now return zero and overwrite the question packet. The code which
was previously added to support stale caching by saving a copy of the
query in the block-storage system is extended to always be active.
This handles the case where answer_request() returns no answer OR
a stale answer and a copy of the original query is needed to forward
upstream.
2023-09-11 22:11:50 +01:00
Simon Kelley
9bbf098a97 =/== typo in last commit. 2023-05-26 18:19:15 +01:00
Simon Kelley
6536187b62 Merge branch 'master' of ssh://thekelleys.org.uk/var/local/git/dnsmasq 2023-05-26 18:01:58 +01:00
Simon Kelley
50adf82199 Behave better when attempting to contact unresponsive TCP servers.
By default TCP connect takes minutes to fail when trying to
connect a server which is not responding and for which the
network layer doesn't generate HOSTUNREACH errors.

This is doubled because having failed to connect in FASTOPEN
mode, the code then tries again with a call to connect().

We set TCP_SYNCNT to 2, which make the timeout about 10 seconds.
This in an unportable Linux feature, so it doesn't work on other
platforms.

No longer try connect() if sendmsg in fastopen mode fails with
ETIMEDOUT or EHOSTUNREACH since the story will just be the same.
2023-05-26 17:55:35 +01:00
Simon Kelley
1419de285f Log truncated DNS replies. 2023-05-17 23:19:30 +01:00
Simon Kelley
31c91b40bd Handle SERVFAIL responses to DS queries better.
On 15/5/2023 8.8.8.8 was returning SERVFAIL for a query on ec.europa.eu

ec.europa.eu is not a domain cut, that happens at jrc.ec.europa.eu. which
does return a signed proof of non-existance for a DS record.
Abandoning the search for a DS or proof of non existence at ec.europa.eu
renders everything within that domain BOGUS, since nothing is signed.

This code changes behaviour on a SERVFAIL to continue looking
deeper for a DS or proof of its nonexistence.
2023-05-17 12:20:50 +01:00
Simon Kelley
d774add784 Fix issue with stale caching.
After replying with stale data, dnsmasq sends the query upstream to
refresh the cache asynchronously and sometimes sends the wrong packet:
packet length can be wrong, and if an EDE marking stale data is added
to the answer that can end up in the query also. This bug only seems
to cause problems when the usptream server is a DOH/DOT proxy. Thanks
to Justin He for the bug report.
2023-05-01 20:42:30 +01:00
Simon Kelley
df242de5c6 Bump copyrights to 2023. 2023-04-05 12:34:34 +01:00
Simon Kelley
a3c8b75972 Add filtering of arbitrary RR-types. 2023-03-29 22:43:21 +01:00
Simon Kelley
042c64273d Remove code for caching SRV.
Function replaced by the ability to cache any RR type.

For backwards compatibilty SRV records are always on the
list of cacheable RR-types.
2023-03-28 18:24:22 +01:00
Simon Kelley
88fc6c8023 Fold F_NOERR and F_DNSSEC to make space for new F_RR. 2023-03-20 23:11:38 +00:00
Simon Kelley
1f0f86a0d0 Add EDE "filtered" extended error when --filter-A or --filter-AAAA act.
If a NODATA answer is returned instead of actual data for A or AAAA
queries because of the existence of --filter-A or --filter-AAAA
config options, then mark the replies with an EDE "filtered" tag.

Basic patch by Petr Menšík, tweaked by Simon Kelley to apply onto
the preceding caching patches.
2023-03-20 18:32:14 +00:00
Simon Kelley
2842972035 More --filter-AAAA caching improvements.
Cache answers before filtering and filter coming out of the cache.
2023-03-20 17:14:17 +00:00
Simon Kelley
e939b45c9f Handle malformed DNS replies better.
If we detect that that reply from usptream is malformed,
transform it into a SERVFAIL reply before sending to the
original requestor.
2022-11-26 22:19:29 +00:00
Simon Kelley
fdd9a96a8c Merge branch 'aws' 2022-10-13 15:37:52 +01:00
Simon Kelley
f32498465d Don't exclude stale-cache answers from "local answered" metric. 2022-09-16 09:35:44 +01:00
Simon Kelley
fa45e06431 Initialise modified-moving-average latency calc better.
Use the first value, rather than initialising at zero,
which takes many queries to converge.
2022-09-16 00:16:18 +01:00
Simon Kelley
6722ec6c78 Split failed queries in retries in stat counting. 2022-09-16 00:07:36 +01:00
Simon Kelley
d882dfdae9 Tweak server-selection logic in the fast-retry case. 2022-09-15 23:54:53 +01:00
Simon Kelley
a2ee2426bf Keep a per-DNS-server moving average of query latency. 2022-09-15 23:22:02 +01:00
Simon Kelley
271790685a Count NXDOMAIN replies from each server. 2022-09-15 22:29:44 +01:00
Simon Kelley
7a74037267 Add metric for queries which never see an answer. 2022-09-15 22:06:39 +01:00
Simon Kelley
9a9f6e147c Make fast-retry more configurable and do exponential backoff. 2022-09-15 19:29:49 +01:00
Simon Kelley
92eab03b12 Return EDE_STALE extended error when returning stale data from cache. 2022-09-12 15:28:46 +01:00
Simon Kelley
1ba4ae2830 Add stale cache replies to metrics. 2022-09-12 14:50:17 +01:00
Simon Kelley
c0e731d545 Further optimisation of --port-limit.
No longer try and fail to open every port when the port range
is in complete use; go straight to re-using an existing socket.

Die at startup if port range is smaller than --port-limit, since
the code behaves badly in this case.
2022-09-09 23:15:50 +01:00
Simon Kelley
3f56bb8ba1 Second try at port-limit option.
1) It's expected to fail to bind a new source port when they
   are scarce, suppress warning in log in this case.

2) Optimse bind_local when max_port - min_port is small. There's no
   randomness in this case, so we try all possible source ports
   rather than poking at random ones for an arbitrary number of tries.

3) In allocate_rfd() handle the case that all available source ports
   are already open. In this case we need to pick an existing
   socket/port to use, such that it has a different port from any we
   already hold. This gives the required property that the set of ports
   utilised by any given query is set by --port-limit and we don't
   re-use any until we have port-limit different ones.
2022-09-09 17:09:32 +01:00
Simon Kelley
e518e87533 Fix namebuff overwrite leading to wrong log after socket bind warning. 2022-09-09 15:57:39 +01:00
Simon Kelley
c4b9bc63e0 Fix a problem in overload handling.
Sending the same query repeatedly to a dnsmasq instance which
doesn't get replies from upstream will eventually hit the
hard limit on frec_src structures and start gettin REFUSED
replies. This is OK, except that since the queries are no longer
being forwarded, an upstream server coming back doesn't reset the
situation. If there is any other traffic, frec allocation will
eventually delete the timed-out frec and get things moving again,
but that's not guaranteed.

To fix this we explicitly delete the frec once timed out in this case.

Thanks to Filip Jenicek for noticing and characterising this problem.
2022-09-09 12:53:49 +01:00
Simon Kelley
1d53d958bb Remove fast-retry development logging. 2022-09-06 22:43:33 +01:00
Simon Kelley
d334e7c34f Add --use-stale-cache option. 2022-09-06 22:43:33 +01:00
Simon Kelley
d21438a7df Add --fast-dns-retry option.
This gives dnsmasq the ability to originate retries for upstream DNS
queries itself, rather than relying on the downstream client. This is
most useful when doing DNSSEC over unreliable upstream network. It
comes with some cost in memory usage and network bandwidth.
2022-09-06 22:43:33 +01:00
Simon Kelley
24c3b5b3d4 Add --port-limit option.
By default, when sending a query via random ports to multiple upstream servers or
retrying a query dnsmasq will use a single random port for all the tries/retries.
This option allows a larger number of ports to be used, which can increase robustness
in certain network configurations. Note that increasing this to more than
two or three can have security and resource implications and should only
be done with understanding of those.
2022-09-06 22:43:33 +01:00
Simon Kelley
04cc2ae1a6 Fix logic when a SERVFAIL reply is received after good replt for DNSSEC.
If we get a SERVFAIL or REFUSED answer to a DNSSEC query for which
we already have a good answer, just ignore it.
2022-09-06 18:31:59 +01:00
Simon Kelley
d6c69f6bdb Free sockets awaiting upstream DNS replies ASAP.
Once we have a good answer, close the socket so that the fd can
be reused during DNSSEC validation and we don't have to read and
discard more replies from other servers.
2022-09-06 15:35:54 +01:00
Simon Kelley
ce372917fe Tweak packet dump code to make port numbers more accurate.
Also add query-ids with log-queries=extra.
2022-09-05 18:04:35 +01:00
Bertie, Taylor
5586934da0 Bound the value of UDP packet size in the EDNS0 header of
forwarded queries to the configured or default value of
edns-packet-max. There's no point letting a client set a larger
value if we're unable to return the answer.
2022-07-31 17:20:21 +01:00
Dominik Derigs
9b801c4e72 Also log upstream port for dnssec-retry
Signed-off-by: DL6ER <dl6er@dl6er.de>
2022-04-18 15:28:27 +01:00
Simon Kelley
fc664d114d Extend packet-dump system to DHCP and TFTP. 2022-01-29 15:55:04 +00:00
Simon Kelley
c6d4c33d61 Bump copyright to 2022. 2022-01-24 15:19:00 +00:00