Skip to content

Service for getaddrinfo#257

Merged
bradh352 merged 13 commits intoc-ares:masterfrom
ki11roy:service_for_getaddrinfo
Jun 18, 2019
Merged

Service for getaddrinfo#257
bradh352 merged 13 commits intoc-ares:masterfrom
ki11roy:service_for_getaddrinfo

Conversation

@ki11roy
Copy link
Copy Markdown
Contributor

@ki11roy ki11roy commented May 19, 2019

This branch is a development of #232.
Some major changes are support of search domains and combining of ares_parse_a/aaaa_reply into ares__parse_into_addrinfo, though I haven't touched the original functions yet.
I had to introduce additional flag ARES_AI_NOSORT to disable connecting to mock addresses while sorting. My first idea was to use ares_socket_functions, but there is no "getsockname" mock, so flag seems a better variant.
Struct ares_addrinfo extended to keep ai_ttl and ai_cname_ttl.
The latest ares_getaddrinfo fixes are not actual anymore, because the code is removed.

@coveralls
Copy link
Copy Markdown

coveralls commented May 19, 2019

Coverage Status

Coverage increased (+0.1%) to 89.573% when pulling 6426f1f on ki11roy:service_for_getaddrinfo into ba1aa06 on c-ares:master.

@bradh352
Copy link
Copy Markdown
Member

Before I do a full review on this, can you go ahead and clean up the legacy code? Think you referred to ares_parse_a/aaaa_reply, probably those just need to be made into wrappers right?

@ki11roy
Copy link
Copy Markdown
Contributor Author

ki11roy commented May 21, 2019

@bradh352 sure

@ki11roy
Copy link
Copy Markdown
Contributor Author

ki11roy commented May 21, 2019

I have tried and there is a problem. I need to add aliases to ares_addrinfo to completely replace the functions, but it seems too much, because there are already two extra fields ai_ttl and ai_cname_ttl (and ai_cname itself). So there are two separate parts which badly fit into the same struct.
I thought about moving from linked list as in classic addrinfo to more like something like good old hostent with cname, aliases, cname_ttl and a linked list of addrinfo with ttl’s. How about this?

@bradh352
Copy link
Copy Markdown
Member

Again, I wouldn't worry about standards conformance since we're already making modifications to the struct and it is namespaced. And memory these days is cheap, especially what we're talking about here.

I also think its probably silly for getaddrinfo to return technically less information than gethostbyname in any area at all.

My vote would be to make the ai_canonname an array structure holding the name and ttl

`
struct ares_addrinfo_cname {
char *name;
int ttl;
};

struct ares_addrinfo {
...
struct ares_addrinfo_cname *ai_canonname;
size_t ai_canonname_len;
...
};
`

@ki11roy
Copy link
Copy Markdown
Contributor Author

ki11roy commented May 22, 2019

I need to fix the tests and wrap the functions, finally.

@bradh352
Copy link
Copy Markdown
Member

Hmm, I'd think the cname + ttl struct would be an array since the recursive DNS server being queried could return multiple CNAMEs which are the aliases. I'm not sure why you have a char **aliases at all.

We might as well save the ttl for each if we're already saving the cname for each.

Comment thread ares.h Outdated
char *ai_canonname;
struct sockaddr *ai_addr;
struct ares_addrinfo *ai_next;
struct ares_addrinfo_cname cname;
Copy link
Copy Markdown
Member

@bradh352 bradh352 May 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this an array, also need a length member for the array length. Or for consistency, I guess it could be a NULL-term linked list.

Comment thread ares.h Outdated
struct sockaddr *ai_addr;
struct ares_addrinfo *ai_next;
struct ares_addrinfo_cname cname;
char **aliases;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this

Comment thread ares__parse_into_addrinfo.c Outdated
/* Take the min of the TTLs we see in the CNAME chain. */
if (ai->cname.ttl > rr_ttl)
ai->cname.ttl = rr_ttl;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no reason to record a minimum, instead record the ttl associated with the cname entry in the array

Comment thread ares__parse_into_addrinfo.c Outdated
if (ai->cname.ttl > rr_ttl)
ai->cname.ttl = rr_ttl;

ares_free(ai->cname.name);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to free prior cname, should be a new array entry

@ki11roy
Copy link
Copy Markdown
Contributor Author

ki11roy commented May 23, 2019

Got your point, but for the aliases I need them to populate hostent structure in ares_parse_a_reply. I see no similar structure in regular addrinfo and it is not CNAME itself, but a label of CNAME as per RFC2181 section 10.1.1. So I wanted to have labels as aliases and one value (multiple values as you suggested) as CNAME (that part is unfinished).

@ki11roy
Copy link
Copy Markdown
Contributor Author

ki11roy commented Jun 16, 2019

Fixed the tests and replaced ares_parse_a/aaaa_reply with ares__parse_into_addrinfo. It is private, but considering #238 and #259 I think it is would be better to make it public?

There are some new structures, like:

/*
 * alias - label of the resource record.
 * name - value (canonical name) of the resource record.
 * See RFC2181 10.1.1. CNAME terminology.
 */
struct ares_addrinfo_cname {
  int                         ttl;
  char                       *alias;
  char                       *name;
  struct ares_addrinfo_cname *next;
};

I need the structure to populate hostent as it wants both aliases and canonical name in ares_parse_a/aaaa_record.

@bradh352
Copy link
Copy Markdown
Member

I'm inclined to merge this and deal with any tweaks or fallout later. It looks pretty good and provides a very nice implementation with more features (mainly ttls) than a native getaddrinfo which is nice.

@bagder any problems with this?

@daviddrysdale
Copy link
Copy Markdown
Member

Looks like there's a Windows build failure with this (AppVeyor):

	cl.exe /nologo /MDd /D_DEBUG /Od /Zi /D_CRT_NONSTDC_NO_DEPRECATE /D_CRT_SECURE_NO_WARNINGS /RTCsu /DWIN32 /I. /I.. /I gmock-1.8.0 /W3 /EHsc /FD /DCARES_STATICLIB /Fo.\msvc\arestest\lib-debug\obj\ares-test-internal.obj /Fd.\msvc\arestest\lib-debug\obj\ /c .\ares-test-internal.cc
ares-test-internal.cc
C:\Program Files (x86)\Windows Kits\8.1\include\shared\winerror.h(26345) : warning C4005: 'NOERROR' : macro redefinition
        C:\projects\c-ares\nameser.h(144) : see previous definition of 'NOERROR'
.\ares-test-internal.cc(366) : error C2065: 'EnvValue' : undeclared identifier
.\ares-test-internal.cc(366) : error C2146: syntax error : missing ';' before identifier 'with_env'
.\ares-test-internal.cc(366) : error C3861: 'with_env': identifier not found
.\ares-test-internal.cc(386) : error C2065: 'EnvValue' : undeclared identifier
.\ares-test-internal.cc(386) : error C2146: syntax error : missing ';' before identifier 'with_env'
.\ares-test-internal.cc(386) : error C3861: 'with_env': identifier not found
.\ares-test-internal.cc(406) : error C2065: 'EnvValue' : undeclared identifier
.\ares-test-internal.cc(406) : error C2146: syntax error : missing ';' before identifier 'with_env'
.\ares-test-internal.cc(406) : error C3861: 'with_env': identifier not found
.\ares-test-internal.cc(426) : error C2065: 'EnvValue' : undeclared identifier
.\ares-test-internal.cc(426) : error C2146: syntax error : missing ';' before identifier 'with_env'
.\ares-test-internal.cc(426) : error C3861: 'with_env': identifier not found
.\ares-test-internal.cc(575) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
NMAKE : fatal error

@daviddrysdale
Copy link
Copy Markdown
Member

Also, I'm getting some OSS-Fuzz failure reports from this, in a couple of variants:

@ki11roy
Copy link
Copy Markdown
Contributor Author

ki11roy commented Jun 19, 2019

@daviddrysdale thanks! i'll check it.

@bradh352
Copy link
Copy Markdown
Member

Looks like there's a Windows build failure with this (AppVeyor):

	cl.exe /nologo /MDd /D_DEBUG /Od /Zi /D_CRT_NONSTDC_NO_DEPRECATE /D_CRT_SECURE_NO_WARNINGS /RTCsu /DWIN32 /I. /I.. /I gmock-1.8.0 /W3 /EHsc /FD /DCARES_STATICLIB /Fo.\msvc\arestest\lib-debug\obj\ares-test-internal.obj /Fd.\msvc\arestest\lib-debug\obj\ /c .\ares-test-internal.cc
ares-test-internal.cc
C:\Program Files (x86)\Windows Kits\8.1\include\shared\winerror.h(26345) : warning C4005: 'NOERROR' : macro redefinition
        C:\projects\c-ares\nameser.h(144) : see previous definition of 'NOERROR'
.\ares-test-internal.cc(366) : error C2065: 'EnvValue' : undeclared identifier
.\ares-test-internal.cc(366) : error C2146: syntax error : missing ';' before identifier 'with_env'
.\ares-test-internal.cc(366) : error C3861: 'with_env': identifier not found
.\ares-test-internal.cc(386) : error C2065: 'EnvValue' : undeclared identifier
.\ares-test-internal.cc(386) : error C2146: syntax error : missing ';' before identifier 'with_env'
.\ares-test-internal.cc(386) : error C3861: 'with_env': identifier not found
.\ares-test-internal.cc(406) : error C2065: 'EnvValue' : undeclared identifier
.\ares-test-internal.cc(406) : error C2146: syntax error : missing ';' before identifier 'with_env'
.\ares-test-internal.cc(406) : error C3861: 'with_env': identifier not found
.\ares-test-internal.cc(426) : error C2065: 'EnvValue' : undeclared identifier
.\ares-test-internal.cc(426) : error C2146: syntax error : missing ';' before identifier 'with_env'
.\ares-test-internal.cc(426) : error C3861: 'with_env': identifier not found
.\ares-test-internal.cc(575) : warning C4267: 'argument' : conversion from 'size_t' to 'int', possible loss of data
NMAKE : fatal error

Looks like EnvValue is conditionalized against Windows for some reason. I've removed that condition and fixed the build.

@ki11roy
Copy link
Copy Markdown
Contributor Author

ki11roy commented Jun 20, 2019

Also, I'm getting some OSS-Fuzz failure reports from this, in a couple of variants:

#262 should fix this one

daviddrysdale added a commit to daviddrysdale/c-ares that referenced this pull request Jun 20, 2019
Suspect problems were introduced by 7d3591e ("getaddrinfo
enhancements (c-ares#257)"), and revolve around:
 - ares_parse_a_reply.c:160 : WRITE stack buffer overflow
 - ares_parse_aaaa_reply.c:156 : READ heap buffer overflow.

Reproduce by building with ASAN enabled and running (e.g.):

  ./test/aresfuzz ./test/fuzzinput/clusterfuzz-5650695891451904
daviddrysdale added a commit to daviddrysdale/c-ares that referenced this pull request Jun 20, 2019
Suspect problems were introduced by 7d3591e ("getaddrinfo
enhancements (c-ares#257)"), and revolve around:
 - ares_parse_a_reply.c:160 : WRITE stack buffer overflow
 - ares_parse_aaaa_reply.c:156 : READ heap buffer overflow.

Reproduce by building with ASAN enabled and running (e.g.):

  ./test/aresfuzz ./test/fuzzinput/clusterfuzz-5650695891451904
daviddrysdale added a commit to daviddrysdale/c-ares that referenced this pull request Jun 21, 2019
Suspect problems were introduced by 7d3591e ("getaddrinfo
enhancements (c-ares#257)"), and revolve around:
 - ares_parse_a_reply.c:160 : WRITE stack buffer overflow
 - ares_parse_aaaa_reply.c:156 : READ heap buffer overflow.

Reproduce by building with ASAN enabled and running (e.g.):

  ./test/aresfuzz ./test/fuzzinput/clusterfuzz-5650695891451904
daviddrysdale added a commit to daviddrysdale/c-ares that referenced this pull request Jun 21, 2019
Suspect problems were introduced by 7d3591e ("getaddrinfo
enhancements (c-ares#257)"), and include:
 - ares_parse_a_reply.c:170 : WRITE stack buffer overflow
 - ares_parse_aaaa_reply.c:170,172 : WRITE stack buffer overflow
 - ares_parse_a_reply:149 : leak
 - ares_parse_aaaa_reply.c:151 : leak

Reproduce by building with ASAN enabled and running (e.g.):

  ./test/aresfuzz ./test/fuzzinput/clusterfuzz-5650695891451904
daviddrysdale added a commit to daviddrysdale/c-ares that referenced this pull request Jun 24, 2019
Suspect problems were introduced by 7d3591e ("getaddrinfo
enhancements (c-ares#257)"), and include:
 - ares_parse_a_reply.c:170 : WRITE stack buffer overflow
 - ares_parse_aaaa_reply.c:170,172 : WRITE stack buffer overflow
 - ares_parse_a_reply:149 : leak
 - ares_parse_aaaa_reply.c:151 : leak

Reproduce by building with ASAN enabled and running (e.g.):

  ./test/aresfuzz ./test/fuzzinput/clusterfuzz-5650695891451904
@ChristianAmmer-zz
Copy link
Copy Markdown

Adding "Service for ares_getaddrinfo" also changed the order search domains for T_A and T_AAAA requests are tried (If I'm not wrong).

Before:
T_AAAA for host.suffix1 and parallel T_A for host.suffix1
T_AAAA for host.suffix2 and parallel T_A for host.suffix2

Now:
T_AAAA for host.suffix1
T_AAAA for host.suffix2
T_A for host.suffix1
T_A for host.suffix2

This slows down the entire lookup. In comparison to this, glibc uses parallel searches with the intention of not slowing down.

@bradh352
Copy link
Copy Markdown
Member

@ChristianAmmer I agree that queries for ipv4 & ipv6 should be done in parallel

@ki11roy
Copy link
Copy Markdown
Contributor Author

ki11roy commented Jul 10, 2019

As far as I remember the initial purpose of that code was to fixing the incorrect behavior, which was done in mostly invasive way. So I just fixed it the other way. Everything above this is an optimization. I do agree with the statement about the parallel queries, but in my opinion the first step is fixing the code (its done) and the second is implementing the optimization. I will think on how to return it in the most painless way.

vadorovsky pushed a commit to vadorovsky/c-ares that referenced this pull request Oct 21, 2019
* Service support has been added to getaddrinfo.
* ares_parse_a/aaaa_record now share code with the addrinfo parser.
* Private ares_addrinfo structure with useful extensions such as ttls (including cname ttls),
  as well as the ability to list multiple cnames in chain of lookups

Work By: Andrew Selivanov @ki11roy
@ChristianAmmer-zz
Copy link
Copy Markdown

Hi @bradh352 @ki11roy
I was working on a related issue today, this remembered me on this one. To not forget it, I created #285.

DronRathore pushed a commit to DronRathore/c-ares that referenced this pull request Mar 11, 2020
* Service support has been added to getaddrinfo.
* ares_parse_a/aaaa_record now share code with the addrinfo parser.
* Private ares_addrinfo structure with useful extensions such as ttls (including cname ttls),
  as well as the ability to list multiple cnames in chain of lookups

Work By: Andrew Selivanov @ki11roy
bradh352 added a commit that referenced this pull request Nov 12, 2020
If there are more ttls returned than the maximum provided by the requestor, then
the *naddrttls response would be larger than the actual number of elements in
the addrttls array.

This bug could lead to invalid memory accesses in applications using c-ares.

This behavior appeared to break with PR #257

Fixes: #371
Reported By: Momtchil Momtchev (@mmomtchev)
Fix By: Brad House (@bradh352)
targos pushed a commit to nodejs/node that referenced this pull request Nov 16, 2020
Original commit message:

  If there are more ttls returned than the maximum provided by the requestor, then
  the *naddrttls response would be larger than the actual number of elements in
  the addrttls array.

  This bug could lead to invalid memory accesses in applications using c-ares.

  This behavior appeared to break with PR c-ares/c-ares#257

  Fixes: c-ares/c-ares#371
  Reported By: Momtchil Momtchev (@mmomtchev)
  Fix By: Brad House (@bradh352)

Refs: https://github.com/nodejs/node/issues/36063

Signed-off-by: Michael Dawson <[email protected]>

CVE-ID: CVE-2020-8277
PR-URL: nodejs-private/node-private#231
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Beth Griggs <[email protected]>
sergepetrenko pushed a commit to tarantool/c-ares that referenced this pull request Jul 29, 2022
If there are more ttls returned than the maximum provided by the requestor, then
the *naddrttls response would be larger than the actual number of elements in
the addrttls array.

This bug could lead to invalid memory accesses in applications using c-ares.

This behavior appeared to break with PR c-ares#257

Fixes: c-ares#371
Reported By: Momtchil Momtchev (@mmomtchev)
Fix By: Brad House (@bradh352)
Pranay180420 pushed a commit to Pranay180420/Node.jsforme that referenced this pull request Feb 17, 2025
Original commit message:

  If there are more ttls returned than the maximum provided by the requestor, then
  the *naddrttls response would be larger than the actual number of elements in
  the addrttls array.

  This bug could lead to invalid memory accesses in applications using c-ares.

  This behavior appeared to break with PR c-ares/c-ares#257

  Fixes: c-ares/c-ares#371
  Reported By: Momtchil Momtchev (@mmomtchev)
  Fix By: Brad House (@bradh352)

Refs: https://github.com/nodejs/node/issues/36063

Signed-off-by: Michael Dawson <[email protected]>

CVE-ID: CVE-2020-8277
PR-URL: nodejs-private/node-private#231
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Beth Griggs <[email protected]>
QwireyInc pushed a commit to QwireyInc/node that referenced this pull request Apr 19, 2025
Original commit message:

  If there are more ttls returned than the maximum provided by the requestor, then
  the *naddrttls response would be larger than the actual number of elements in
  the addrttls array.

  This bug could lead to invalid memory accesses in applications using c-ares.

  This behavior appeared to break with PR c-ares/c-ares#257

  Fixes: c-ares/c-ares#371
  Reported By: Momtchil Momtchev (@mmomtchev)
  Fix By: Brad House (@bradh352)

Refs: https://github.com/nodejs/node/issues/36063

Signed-off-by: Michael Dawson <[email protected]>

CVE-ID: CVE-2020-8277
PR-URL: nodejs-private/node-private#231
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Beth Griggs <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants