35 points · by blechschmidt · 10y ago · 53 comments
well at least it's pretty clear up front. No malloc result checks, invalid format in printfs, probably more.
(I don't mean to be disparaging, please don't take it that way OP. We clearly need more/better tooling in this area in C).
(Edited for clarity)
The last time I worked with the DNS protocol, I seem to remember that everything had a well-defined (and small) limit on its size, and it wasn't really necessary to use dynamic allocation at all. Strings are length-prefixed, not null-terminated, and the longest length fields are only 16 bits.
A look at the RFC shows the strict limits on size:
https://www.ietf.org/rfc/rfc1035.txt
In comparison to text protocols like HTTP, I see far less opportunities to make errors like buffer overflows with DNS. It's a simple and relatively easy to parse binary protocol.
Prefixed strings can even be a source of buffer overflows, if you don't check if the length is smaller than the actual length of the rest of the data. In fact this was actually the source of the heartbleed bug.
We clearly need more/better tooling in this area in C).
I think the tooling we have nowadays is better than we give it credit for. Between clang's analyser, ASAN, Infer and tis-interpreter, I often find when someone points out a vulnerability, I can get one of these tools to make an issue of it.Note I'm not suggesting this is enough to make C safe.
Edit: A quick demonstration, based on 30 seconds with valgrind. If I run with ./bin/massdns --help, it makes a malloc() on line 595, then exits on 605 without free()ing. And yes I know this isn't really an issue in practice, but it demonstrates that the tools can see issues.
%x takes unsigned int, not a char. (Although I guess you could just rely on promotion here if you can rely on the unsignedness of char).
> Could you point out a line that is prone to integer overflows?
Never said that it was an overflow (just C&P that comment from the file). Just said its invalid use. Which I'm totally willing to agree on the compiler mostly getting correct behavior. Just not really something I'd want to see landing in something that we all need a really good, secure implementation of.
Best of luck though, I hope you iterate on this to the point we can have a really good DNS implementation in C, in user-space, with optional async processing.
If compilers did not do promotion, they would be very obviously not compliant with the C standard and unable to compile correctly most existing code, so I'm willing to bet that is something you can rely on. It's defined by the standard and widely used.
I believe a better description for this program would be a "stub resolver".
Some of the resolvers might ban/rate-limit you indeed and even send abuse complaints to your ISP.
EDIT: You might have a look at the newly implemented --norecurse option.
Not if the resolver rejects non-recursive queries, as do dnscache and dqcache.
There is a way to resolve DNS domainnames to IP addresses using only non-recursive queries. I do it everyday.
But I've never seen anyone release any program that did this. Your program does not even attempt to do this -- you need to send the queries to authoritative nameservers not public resolvers. But you used the term "non-recursive" so I thought maybe someone had finally tried.
One of the shortcomings of DNS IMO is that the specification allows for the possibility of including more than one name in a query. But no one has ever implemented this, as far as I know.
Despite the design of the DNS, most of the information stored in it is more static than dynamic, and much of it is centralized. Most dommainnames do not change IP addresses very often and there are very large numbers of domainnames sharing the same authoritative nameservers.
Having implemented the --norecurse option, the title is at least not wrong anymore. One can have non-recursive, non-iterative resolver (which is what you use when you want to perform DNS cache snooping) and the title does not suggest that the tool supports iterative lookups.
Handling multiple questions within one packet is difficult because response codes such as NXDOMAIN are only included once per packet. AFAIK, bind does not support handling such queries.
https://lobste.rs/s/edrihm/sad_news_today_systemd_resolved_b...
http://seclists.org/oss-sec/2014/q4/595
(Please understand, I use systemd, just not resolved yet; this is not meant to start a tangential flame; I just realize even though I am one of those idiots who preaches better security and runs his own dnsmasq settings per recommendations of known bloggers, I need more detailed background on DNS.)
if (packet->flags & DNS_RESPONSE_FLAG == 0 || packet->questioncount != 1)
I'm not sure that it does what you want. if ((packet->flags & (DNS_RESPONSE_FLAG == 0)) || (packet->questioncount != 1))
which at least does not read as intended.Are you sure tinydns, which has existed for ages without security vulnerabilities, can't trivially handle 27k requests/second on modern hardware?
The tool has mainly been tested on a Hetzner EX41 server. (Ubuntu, Intel® Core™ i7-6700, 32 GB RAM)