summaryrefslogtreecommitdiff
path: root/src/network
AgeCommit message (Collapse)AuthorLines
2019-03-13handle labels with 8-bit byte values in dn_skipnameRyan Fairfax-2/+5
The original logic considered each byte until it either found a 0 value or a value >= 192. This means if a string segment contained any byte >= 192 it was interepretted as a compressed segment marker even if it wasn't in a position where it should be interpretted as such. The fix is to adjust dn_skipname to increment by each segments size rather than look at each character. This avoids misinterpretting string segment characters by not considering those bytes.
2019-02-20fix spurious undefined behavior in getaddrinfoRich Felker-3/+2
addressing &out[k].sa was arguably undefined, despite &out[k] being defined the slot one past the end of an array, since the member access .sa is intervening between the [] operator and the & operator.
2019-02-20fix invalid free of partial addrinfo list with multiple servicesRich Felker-1/+1
the backindex stored by getaddrinfo to allow freeaddrinfo to perform partial-free wrongly used the address result index, rather than the output slot index, and thus was only valid when they were equal (nservs==1). patch based on report with proposed fix by Markus Wichmann.
2018-10-04allow freeaddrinfo of arbitrary sublists of addrinfo listRich Felker-8/+25
the specification for freeaddrinfo allows it to be used to free "arbitrary sublists" of the list returned by getaddrinfo. it's not clearly stated how such sublists come into existence, but the interpretation seems to be that the application can edit the ai_next pointers to cut off a portion of the list and then free it. actual freeing of individual list slots is contrary to the design of our getaddrinfo implementation, which has no failure paths after making a single allocation, so that light callers can avoid linking realloc/free. freeing individual slots is also incompatible with sharing the string for ai_canonname, which the current implementation does despite no requirement that it be present except on the first result. so, rather than actually freeing individual slots, provide a way to find the start of the allocated array, and reference-count it, freeing the memory all at once after the last slot has been freed. since the language in the spec is "arbitrary sublists", no provision for handling other constructs like multiple lists glued together, circular links, etc. is made. presumably passing such a construct to freeaddrinfo produces undefined behavior.
2018-09-19fix getaddrinfo regression with AI_ADDRCONFIG on some configurationsRich Felker-1/+10
despite not being documented to do so in the standard or Linux documentation, attempts to udp connect to 127.0.0.1 or ::1 generate EADDRNOTAVAIL when the loopback device is not configured and there is no default route for IPv6. this caused getaddrinfo with AI_ADDRCONFIG to fail with EAI_SYSTEM and EADDRNOTAVAIL on some no-IPv6 configurations, rather than the intended behavior of detecting IPv6 as unsuppported and producing IPv4-only results. previously, only EAFNOSUPPORT was treated as unavailability of the address family being probed. instead, treat all errors related to inability to get an address or route as conclusive that the family being probed is unsupported, and only fail with EAI_SYSTEM on other errors. further improvements may be desirable, such as reporting EAI_AGAIN instead of EAI_SYSTEM for errors which are expected to be transient, but this patch should suffice to fix the serious regression.
2018-09-12reduce spurious inclusion of libc.hRich Felker-13/+4
libc.h was intended to be a header for access to global libc state and related interfaces, but ended up included all over the place because it was the way to get the weak_alias macro. most of the inclusions removed here are places where weak_alias was needed. a few were recently introduced for hidden. some go all the way back to when libc.h defined CANCELPT_BEGIN and _END, and all (wrongly implemented) cancellation points had to include it. remaining spurious users are mostly callers of the LOCK/UNLOCK macros and files that use the LFS64 macro to define the awful *64 aliases. in a few places, new inclusion of libc.h is added because several internal headers no longer implicitly include libc.h. declarations for __lockfile and __unlockfile are moved from libc.h to stdio_impl.h so that the latter does not need libc.h. putting them in libc.h made no sense at all, since the macros in stdio_impl.h are needed to use them correctly anyway.
2018-09-12remove or make static various unused __-prefixed symbolsRich Felker-7/+3
2018-09-12apply hidden visibility to various remaining internal interfacesRich Felker-7/+8
2018-09-12overhaul internally-public declarations using wrapper headersRich Felker-16/+2
commits leading up to this one have moved the vast majority of libc-internal interface declarations to appropriate internal headers, allowing them to be type-checked and setting the stage to limit their visibility. the ones that have not yet been moved are mostly namespace-protected aliases for standard/public interfaces, which exist to facilitate implementing plain C functions in terms of POSIX functionality, or C or POSIX functionality in terms of extensions that are not standardized. some don't quite fit this description, but are "internally public" interfacs between subsystems of libc. rather than create a number of newly-named headers to declare these functions, and having to add explicit include directives for them to every source file where they're needed, I have introduced a method of wrapping the corresponding public headers. parallel to the public headers in $(srcdir)/include, we now have wrappers in $(srcdir)/src/include that come earlier in the include path order. they include the public header they're wrapping, then add declarations for namespace-protected versions of the same interfaces and any "internally public" interfaces for the subsystem they correspond to. along these lines, the wrapper for features.h is now responsible for the definition of the hidden, weak, and weak_alias macros. this means source files will no longer need to include any special headers to access these features. over time, it is my expectation that the scope of what is "internally public" will expand, reducing the number of source files which need to include *_impl.h and related headers down to those which are actually implementing the corresponding subsystems, not just using them.
2018-09-12move __res_msend_rc declaration to lookup.hRich Felker-1/+1
unlike the other res/dn functions, this one is tied to struct resolvconf which is not a public interface, so put it in the private header for its subsystem.
2018-09-12move and deduplicate declarations of __dns_parse to make it checkableRich Felker-2/+3
the source file for this function is completely standalone, but it doesn't seem worth adding a header just for it, so declare it in lookup.h for now.
2018-09-12fix issues from public functions defined without declaration visibleRich Felker-2/+7
policy is that all public functions which have a public declaration should be defined in a context where that public declaration is visible, to avoid preventable type mismatches. an audit performed using GCC's -Wmissing-declarations turned up the violations corrected here. in some cases the public header had not been included; in others, a feature test macro needed to make the declaration visible had been omitted. in the case of gethostent and getnetent, the omission seems to have been intentional, as a hack to admit a single stub definition for both functions. this kind of hack is no longer acceptable; it's UB and would not fly with LTO or advanced toolchains. the hack is undone to make exposure of the declarations possible.
2018-09-02fix stack-based oob memory clobber in resolver's result sortingRich Felker-1/+1
commit 4f35eb7591031a1e5ef9828f9304361f282f28b9 introduced this bug. it is not present in any released versions. inadvertent use of the & operator on an array into which we're indexing produced arithmetic on the wrong-type pointer, with undefined behavior.
2018-07-14implement getaddrinfo's AI_ADDRCONFIG flagRich Felker-0/+39
this flag is notoriously under-/mis-specified, and in the past it was implemented as a nop, essentially considering the absence of a loopback interface with 127.0.0.1 and ::1 addresses an unsupported configuration. however, common real-world container environments omit IPv6 support (even for the network-namespaced loopback interface), and some kernels omit IPv6 support entirely. future systems on the other hand might omit IPv4 entirely. treat these as supported configurations and suppress results of the unconfigured/unsupported address families when AI_ADDRCONFIG is requested. use routability of the loopback address to make the determination; unlike other implementations, we do not exclude loopback from the "an address is configured" condition, since there is no basis in the specification for such exclusion. obtaining a result with AI_ADDRCONFIG does not imply routability of the result, and applications must still be able to cope with unroutable results even if they pass AI_ADDRCONFIG.
2018-07-11resolver: don't depend on v4mapped ipv6 to probe routability of v4 addrsRich Felker-15/+32
to produce sorted results roughly corresponding to RFC 3484/6724, __lookup_name computes routability and choice of source address via dummy UDP connect operations (which do not produce any packets). since at the logical level, the properties fed into the sort key are computed on ipv6 addresses, the code was written to use the v4mapped ipv6 form of ipv4 addresses and share a common code path for them all. however, on kernels where ipv6 support has been completely omitted, this causes ipv4 to appear equally unroutable as ipv6, thereby putting unreachable ipv6 addresses before ipv4 addresses in the results. instead, use only ipv4 sockets to compute routability for ipv4 addresses. some gratuitous conversion back and forth is left so that the logic is not affected by these changes. it may be possible to simplify the ipv4 case considerably, thereby reducing code size and complexity.
2018-06-26inet_ntop: do not compress single zeros in IPv6Arthur Jones-1/+1
maintainer's note: this change is for conformance with RFC 5952, 4.2.2, which explicitly forbids use of :: to shorten a single 16-bit 0 field when producing the canonical text representation for an IPv6 address. fixes a test failure reported by Philip Homburg, who also submitted a patch, but this fix is simpler and should produce smaller code.
2018-06-26resolver: omit final dot (root/suppress-search) in canonical nameRich Felker-0/+4
if a final dot was included in the queried host name to anchor it to the dns root/suppress search domains, and the result was not a CNAME, the returned canonical name included the final dot. this was not consistent with other implementations, confused some applications, and does not seem desirable. POSIX specifies returning a pointer to, or to a copy of, the input nodename, when the canonical name is not available, but does not attempt to specify what constitutes "not available". in the case of search, we already have an implementation-defined "availability" of a canonical name as the fully-qualified name resulting from search, so defining it similarly in the no-search case seems reasonable in addition to being consistent with other implementations. as a bonus, fix the case where more than one trailing dot is included, since otherwise the changes made here would wrongly cause lookups with two trailing dots to succeed. previously this case resulted in malformed dns queries and produced EAI_AGAIN after a timeout. now it fails immediately with EAI_NONAME.
2017-11-09fix getaddrinfo error code for non-numeric service with AI_NUMERICSERVA. Wilcox-1/+1
If AI_NUMERICSERV is specified and a numeric service was not provided, POSIX mandates getaddrinfo return EAI_NONAME. EAI_SERVICE is only for services that cannot be used on the specified socket type.
2017-10-18in dns parsing callback, enforce MAXADDRS to preclude overflowRich Felker-0/+1
MAXADDRS was chosen not to need enforcement, but the logic used to compute it assumes the answers received match the RR types of the queries. specifically, it assumes that only one replu contains A record answers. if the replies to both the A and the AAAA query have their answer sections filled with A records, MAXADDRS can be exceeded and clobber the stack of the calling function. this bug was found and reported by Felix Wilhelm.
2017-09-06don't treat numeric port strings as servent records in getservby*()Rich Felker-0/+10
some applications use getservbyport to find port numbers that are not assigned to a service; if getservbyport always succeeds with a numeric string as the result, they fail to find any available ports. POSIX doesn't seem to mandate the behavior one way or another. it specifies an abstract service database, which an implementation could define to include numeric port strings, but it makes more sense to align behavior with traditional implementations. based on patch by A. Wilcox. the original patch only changed getservbyport[_r]. to maintain a consistent view of the "service database", I have also modified getservbyname[_r] to exclude numeric port strings.
2017-04-21fix regression in support for resolv.conf attempts optionRich Felker-2/+2
commit d6cb08bcaca4ff1f921375510ca72bccea969c75 moved the code and introduced an incorrect string offset for the new parsing, probably due to a copy-and-paste error. patch by Stefan Sedich.
2017-04-11fix read past end of buffer in getaddrinfo backendRich Felker-2/+2
due to testing buf[i].family==AF_INET before checking i==cnt, it was possible to read past the end of the array, or past the valid part. in practice, without active bounds/indeterminate-value checking by the compiler, the worst that happened was failure to return early and optimize out the sorting that's unneeded for v4-only results. returning on i==cnt-1 rather than i==cnt would be an alternate fix, but the approach this patch takes is more idiomatic and less error-prone. patch by Timo Teräs.
2017-03-14fix possible fd leak, unrestored cancellation state on dns socket failRich Felker-1/+5
2016-09-24fix getservby*_r result pointer value on errorDaniel Sabogal-0/+3
this is a clone of the fix to the gethostby*_r functions in commit fe82bb9b921be34370e6b71a1c6f062c20999ae0. the man pages document that the getservby*_r functions set this pointer to NULL if there was an error or if no record was found.
2016-09-24remove dead case in gethostbyname2_rDaniel Sabogal-2/+0
this case statement was accidently left behind when this function was refactored in commit e8f39ca4898237cf71657500f0b11534c47a0521.
2016-09-16fix if_indextoname error caseDaniel Sabogal-1/+6
posix requires errno to be set to ENXIO if the interface does not exist. linux returns ENODEV instead so we handle this.
2016-07-06remove obsolete and unused gethostbyaddr implementationRich Felker-52/+0
this code was already under #if 0, but could be confusing if a reader didn't notice that, and it's almost surely full of bugs and/or inconsistencies with the current code that uses the gethostbyname2_r backend.
2016-06-29refactor name_from_dns in hostname lookup backendNatanael Copa-14/+13
loop over an address family / resource record mapping to avoid repetitive code.
2016-06-29in performing dns lookups, check result from res_mkqueryNatanael Copa-0/+4
don't send a query that may be malformed.
2016-06-27fix misaligned address buffers in gethostbyname[2][_r] resultsRich Felker-7/+7
mistakenly ordering strings before addresses in the result buffer broke the alignment that the preceding code had set up.
2016-05-04fix incorrect protocol name and number for egpAndrew Kelley-1/+1
previously if you called getprotobyname("egp") you would get NULL because \008 is invalid octal and so the protocol id was interpreted as 0 and name as "8egp".
2016-04-18remove dead store in res_msendPetr Vaněk-1/+0
The variable nss is set to zero in following line.
2016-03-24fix gethostbyaddr_r to fill struct hostent.h_length as appropriateTimo Teräs-0/+1
2016-03-02handle non-matching address family entries in hosts fileRich Felker-3/+11
name_from_hosts failed to account for the possibility of an address family error from name_from_numeric, wrongly counting such a return as success and using the uninitialized address data as part of the results passed up to the caller. non-matching address family entries cannot simply be ignored or results would be inconsistent with respect to whether AF_UNSPEC or a specific address family is queried. instead, record that a non-matching entry was seen, and fail the lookup with EAI_NONAME of no matching-family entries are found.
2016-01-28reuse parsed resolv.conf in dns core to avoid re-reading/re-parsingRich Felker-16/+22
2016-01-28fix uninitialized variable in new resolv.conf parserRich Felker-1/+1
2016-01-28add support for search domains to dns resolverRich Felker-1/+41
search is only performed if the search or domain keyword is used in resolv.conf and the queried name has fewer than ndots dots. there is no default domain and names with >=ndots dots are never subjected to search; failure in the root scope is final. the (non-POSIX) res_search API presently does not honor search. this may be added at some point in the future if needed. resolv.conf is now parsed twice, at two different layers of the code involved. this will be fixed in a subsequent patch.
2016-01-28fix handling of dns response codesRich Felker-1/+2
rcode of 3 (NxDomain) was treated as a hard EAI_NONAME failure, but it should instead return 0 (no results) so the caller can continue searching. this will be important for adding search domain support. the top-level caller will automatically return EAI_NONAME if there are zero results at the end. also, the case where rcode is 0 (success) but there are no results was not handled. this happens when the domain exists but there are no A or AAAA records for it. in this case a hard EAI_NONAME should be imposed to inhibit further search, since the name was defined and just does not have any address associated with it. previously a misleading hard failure of EAI_FAIL was reported.
2016-01-28fix logic for matching search/domain keywords in resolv.confRich Felker-1/+1
2016-01-28factor resolv.conf parsing out of res_msend to its own fileRich Felker-60/+126
this change is made in preparation for adding search domains, for which higher-level code will need to parse resolv.conf. simply parsing it twice for each lookup would be one reasonable option, but the existing parser code was buggy anyway, which suggested to me that it's a bad idea to have two variants of this code in two different places. the old code in res_msend potentially misinterpreted overly long lines in resolv.conf, and stopped parsing after it found 3 nameservers, even if there were relevant options left to be parsed later in the file.
2016-01-17fix if_nametoindex return value when socket open failsRon Yorston-1/+1
The return value of if_nametoindex is unsigned; it should return 0 on error.
2016-01-06add missing protocols to protoent lookup functionsTimo Teräs-1/+16
2015-11-30properly handle point-to-point interfaces in getifaddrs()Jo-Philipp Wich-3/+16
With point-to-point interfaces, the IFA_ADDRESS netlink attribute contains the peer address while an extra attribute IFA_LOCAL carries the actual local interface address. Both the glibc and uclibc implementations of getifaddrs() handle this case by moving the ifa_addr contents to the broadcast/remote address union and overwriting ifa_addr upon receipt of an IFA_LOCAL attribute. This patch adds the same special treatment logic of IFA_LOCAL to musl's implementation of getifaddrs() in order to align its behaviour with that of uclibc and glibc. Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
2015-10-26getnameinfo: make size check not fail for bigger sizesHauke Mehrtens-2/+2
getnameinfo() compares the size of the given struct sockaddr with sizeof(struct sockaddr_in) and sizeof(struct sockaddr_in6) depending on the net family. When you add a sockaddr of size sizeof(struct sockaddr_storage) this function will fail because the size of the sockaddr is too big. Change the check that it only fails if the size is too small, but make it work when it is too big for example when someone calls this function with a struct sockaddr_storage and its size. This fixes a problem with IoTivity 1.0.0 and musl. glibc and bionic are only failing if it is smaller, net/freebsd implemented the != check. Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
2015-10-26safely handle failure to open hosts, services, resolv.conf filesRich Felker-4/+29
previously, transient failures like fd exhaustion or other resource-related errors were treated the same as non-existence of these files, leading to fallbacks or false-negative results. in particular: - failure to open hosts resulted in fallback to dns, possibly yielding EAI_NONAME for a hostname that should be defined locally, or an unwanted result from dns that the hosts file was intended to replace. - failure to open services resulted in EAI_SERVICE. - failure to open resolv.conf resulted in querying localhost rather than the configured nameservers. now, only permanent errors trigger the fallback behaviors above; all other errors are reportable to the caller as EAI_SYSTEM.
2015-09-25avoid attempting to lookup IP literals as hostnamesRich Felker-27/+32
previously, __lookup_ipliteral only checked its argument against the requested address family, so IPv4 literals passed through to __lookup_name if the caller asked for only IPv6 results, and likewise for IPv6 literals when the caller asked for only IPv4. this resulted in spurious DNS lookups that reportedly even succeeded with some nameservers. now, __lookup_ipliteral attempts to parse its argument as both IPv4 and IPv6, and returns an error (to stop further search) rather than 0 (no results yet) if the form of the argument mismatches the requested address family. based on patch by Julien Ramseier.
2015-09-25make getaddrinfo return error if both host and service name are nullRich Felker-0/+2
this case is specified as a mandatory ("shall fail") error. based on patch by Julien Ramseier.
2015-09-11fix uninitialized scopeid in lookups from hosts file and ip literalsTimo Teräs-2/+2
2015-07-08fix negated return value of ns_skiprr, breakage in related functionsRich Felker-1/+1
due to a reversed pointer difference computation, ns_skiprr always returned a negative value, which functions using it would interpret as an error. patch by Yu Lu.
2015-03-23fix internal buffer overrun in inet_ptonRich Felker-2/+3
one stop condition for parsing abbreviated ipv6 addressed was missed, allowing the internal ip[] buffer to overflow. this patch adds the missing stop condition and masks the array index so that, in case there are any remaining stop conditions missing, overflowing the buffer is not possible.