Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Malformed DNS RRs shall result in parse errors #5145

Merged
merged 2 commits into from
Aug 30, 2021
Merged

Malformed DNS RRs shall result in parse errors #5145

merged 2 commits into from
Aug 30, 2021

Conversation

snar
Copy link
Contributor

@snar snar commented Aug 23, 2021

For now, when DNS RR is malformed it results in 'parse ok, however, some records are not parsed':

3> inet_res:resolve("kinolupa.net", in, aaaa, [], 5000).
{ok,#dns_rec{header = #dns_header{id = 1,qr = true, opcode = query,aa = false,tc = false, rd = true,ra = true,pr = false,rcode = 0}, qdlist = [#dns_query{domain = "kinolupa.net",type = aaaa, class = in}], anlist = [#dns_rr{domain = "kinolupa.net",type = aaaa, class = in,cnt = 0,ttl = 600, data = <<171,22,127,201>>, tm = undefined,bm = [],func = false}, #dns_rr{domain = "kinolupa.net",type = aaaa,class = in, cnt = 0,ttl = 600, data = <<171,22,127,201>>, tm = undefined,bm = [],func = false}], nslist = [],arlist = []}}

As you can see, some broken DNS server returned two malformed (too short) AAAA records that
erlang was not able to parse and so they are returned as binary <<171,22,127,201>>.
With this pull applied, such broken records results in {error, formerr}.

@CLAassistant
Copy link

CLAassistant commented Aug 23, 2021

CLA assistant check
All committers have signed the CLA.

@rickard-green rickard-green added the team:PS Assigned to OTP team PS label Aug 23, 2021
@RaimoNiskanen RaimoNiskanen self-assigned this Aug 26, 2021
@RaimoNiskanen
Copy link
Contributor

Thank you for finding this!

Although your change seems correct and fairly minimal, I did not like what it does to the structure of the code. Now all RR types have to have code in two distinct clauses for decode_data/4; for decode, and for format error. And the clauses are rather far apart.

So I did a rewrite, which, to avoid repeating throw(?DECODE_ERROR) in umpteen places, instead uses a match macro that does the throw. And I fixed some more structural issues. See the commit comment.

The diff is rather large, but I hope the code becomes more maintainable.

If you hate it I can do this on a branch of my own instead of piggybacking on your PR.

* Use one clause per RR type when decoding.  I.e do not
  use a separate error clause where a bunch of RR types
  needs to be repeated, and then can be missed in
  a future rewrite.
* Introduce a macro ?MATCH_ELSE_DECODE_ERROR to throw(?DECODE_ERROR)
  for no match, to avoid cluttering the decode code with such throw:s.
  Use that macro consistently.
* Group RRs into class IN vs. standard (class agnostic),
  for both decoding and encoding.
* Remove erroneous ?S_URI clauses that remained after fixing
  encode/decode in 355eeac.
* Remove incorrect matching for class 'in' when encoding
  standard (class agnostic) RRs.
* Introduce explicit clauses for ?S_OPT instead of falling
  through to the default clause.
@RaimoNiskanen
Copy link
Contributor

I discovered I had used an old name for the macro in the commit comment, so I force pushed a new.

@snar
Copy link
Contributor Author

snar commented Aug 27, 2021 via email

@RaimoNiskanen
Copy link
Contributor

@snar: Can you test the code with my rewrite to verify that it seems to work as expected?

@snar
Copy link
Contributor Author

snar commented Aug 30, 2021 via email

@RaimoNiskanen RaimoNiskanen merged commit 1c63b20 into erlang:maint Aug 30, 2021
@RaimoNiskanen
Copy link
Contributor

Good to know! Thank you for your pull request!

@snar snar deleted the kernel/malformed-dns-rr branch August 30, 2021 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team:PS Assigned to OTP team PS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants