Commit Graph

2523 Commits

Author SHA1 Message Date
Rich Felker 60158bff74 fix fd leak in tmpfile when the fdopen operation fails
this condition could only happen due to malloc failure.

the fdopen operation is also moved to take place after the unlink to
minimize the window during which a link to the file exists in the
directory table.
2014-06-06 03:17:47 -04:00
Szabolcs Nagy 2abb70c302 fix the domain name length limit checks
A domain name is at most 255 bytes long (RFC 1035), but the string
representation is two bytes smaller so the strlen maximum is 253.
2014-06-05 23:06:37 +02:00
Szabolcs Nagy b3d9e0b94e fix multiple validation issues in dns response label parsing
Due to an error introduced in commit fcc522c923,
checking of the remaining output buffer space was not performed correctly,
allowing malformed input to write past the end of the buffer.

In addition, the loop detection logic failed to account for the possibility
of infinite loops with no output, which would hang the function.

The output size is now limited more strictly so only names with valid length
are accepted.
2014-06-05 23:01:46 +02:00
Rich Felker 7e222f46a8 fix missing function declarations in refactored ip literal parsing code 2014-06-05 15:55:20 -04:00
Rich Felker fde3c29de4 add support for reverse port lookups from services file to getnameinfo
this also affects the legacy getservbyport family, which uses
getnameinfo as its backend.
2014-06-04 19:33:20 -04:00
Rich Felker 01dc3f4fea add support for reverse name lookups from hosts file to getnameinfo
this also affects the legacy gethostbyaddr family, which uses
getnameinfo as its backend.

some other minor changes associated with the refactoring of source
files are also made; in particular, the resolv.conf parser now uses
the same code that's used elsewhere to handle ip literals, so as a
side effect it can now accept a scope id for nameserver addressed with
link-local scope.
2014-06-04 16:53:39 -04:00
Rich Felker 934aa1350b remove some dummy "ent" function aliases that duplicated real ones
the service and protocol functions are defined also in other files,
and the protocol ones are actually non-nops elsewhere, so the weak
definitions in ent.c could have prevented the strong definitions from
getting pulled in and used in some static programs.
2014-06-04 04:39:34 -04:00
Rich Felker 6a25313c11 simplify vasprintf implementation
the old implementation preallocated a buffer in order to try to avoid
calling vsnprintf more than once. not only did this potentially lead
to memory fragmentation from trimming with realloc; it also pulled in
realloc/free, which otherwise might not be needed in a static linked
program.
2014-06-04 03:39:22 -04:00
Rich Felker bdad2fefb2 add support for ipv6 scope_id to getaddrinfo and getnameinfo
for all address types, a scope_id specified as a decimal value is
accepted. for addresses with link-local scope, a string containing the
interface name is also accepted.

some changes are made to error handling to avoid unwanted fallbacks in
the case where the scope_id is invalid: if an earlier name lookup
backend fails with an error rather than simply "0 results", this
failure now suppresses any later attempts with other backends.

in getnameinfo, a light "itoa" type function is added for generating
decimal scope_id results, and decimal port strings for services are
also generated using this function now so as not to pull in the
dependency on snprintf.

in netdb.h, a definition for the NI_NUMERICSCOPE flag is added. this
is required by POSIX (it was previously missing) and needed to allow
callers to suppress interface-name lookups.
2014-06-04 02:24:38 -04:00
Rich Felker 8041af5988 fix if_nametoindex return value when interface does not exist
the return value is unsigned, so negative results for "errors" do not
make sense; 0 is the value reserved for when the interface name does
not exist.
2014-06-03 17:53:11 -04:00
Rich Felker d85d261ee6 fix negative response and non-response handling for dns queries
previously, all failures to obtain at least one address were treated
as nonexistant names (EAI_NONAME). this failed to account for the
possibility of transient failures (no response at all, or a response
with rcode of 2, server failure) or permanent failures that do not
indicate the nonexistence of the requested name. only an rcode of 3
should be treated as an indication of nonexistence.
2014-06-03 01:46:40 -04:00
Rich Felker ac2a789342 fix some validation checks in dns response parsing code
since the buffer passed always has an actual size of 512 bytes, the
maximum possible response packet size, no out-of-bounds access was
possible; however, reading past the end of the valid portion of the
packet could cause the parser to attempt to process junk as answer
content.
2014-06-03 01:43:29 -04:00
Rich Felker 8fba4458af fix incorrect end pointer in some cases when wcsrtombs stops early
when wcsrtombs stopped due to hitting zero remaining space in the
output buffer, it was wrongly clearing the position pointer as if it
had completed the conversion successfully.

this commit rearranges the code somewhat to make a clear separation
between the cases of ending due to running out of output buffer space,
and ending due to reaching the end of input or an illegal sequence in
the input. the new branches have been arranged with the hope of
optimizing more common cases, too.
2014-06-02 16:21:47 -04:00
Rich Felker 76f440cff7 remove cruft from old resolver and numeric ip parsing
the old resolver code used a function __ipparse which contained the
logic for inet_addr and inet_aton, which is needed in getaddrinfo.
this was phased out in the resolver overhaul in favor of directly
using inet_aton and inet_pton as appropriate.

this commit cleans up some stuff that was left behind.
2014-06-02 05:00:48 -04:00
Rich Felker 3330198060 switch standard resolver functions to use the new dns backend
this is the third phase of the "resolver overhaul" project.

this commit removes all of the old dns code, and switches the
__lookup_name backend (used by getaddrinfo, etc.) and the getnameinfo
function to use the newly implemented __res_mkquery and __res_msend
interfaces. for parsing the results, a new callback-based __dns_parse
function, based on __dns_get_rr from the old dns code, is used.
2014-06-02 04:47:45 -04:00
Rich Felker a2a328bd89 fix uninitialized variable in new __res_msend dns function 2014-06-02 04:27:54 -04:00
Rich Felker 8312f7f60f implement new dns backend, res_send and other legacy resolver functions
this is the second phase of the "resolver overhaul" project.

the key additions in this commit are the __res_msend and __res_mkquery
functions, which have been factored so as to provide a backend for
both the legacy res_* functions and the standard getaddrinfo and
getnameinfo functions. the latter however are still using the old
backend code; there is code duplication which still needs to be
removed, and this will be the next phase of the resolver overhaul.

__res_msend is derived from the old __dns_doqueries function, but
generalized to send arbitrary caller-provided packets in parallel
rather than producing the parallel queries itself. this allows it to
be used (completely trivially) as a backend for res_send. the
factored-out query generation code, with slightly more generality, is
now part of __res_mkquery.
2014-06-02 02:13:57 -04:00
Timo Teräs 1871f583f4 add ipsec and tunneling protocols to getprotoent-family functions
iptables and ipsec-tools among others require these to function
properly.
2014-06-02 01:44:47 -04:00
Rich Felker bb9af59bba fix off-by-one in checking hostname length in new resolver backend
this bug was introduced in the recent resolver overhaul commits. it
likely had visible symptoms. these were probably limited to wrongly
accepting truncated versions of over-long names (vs rejecting them),
as opposed to stack-based overflows or anything more severe, but no
extensive checks were made. there have been no releases where this bug
was present.
2014-06-02 01:31:28 -04:00
Rich Felker af7c308ee6 improve getservbyname_r using new resolver backend
now that host and service lookup have been separated in the backend,
there's no need for service lookup functions to pull in the host
lookup code. moreover, dynamic allocation is no longer needed, so this
function should now be async-signal-safe. it's also significantly
smaller.

one change in getservbyname is also made: knowing that getservbyname_r
needs only two character pointers in the caller-provided buffer, some
wasted bss can be avoided.
2014-06-01 00:45:04 -04:00
Rich Felker e8f39ca489 improve gethostbyname2_r using new resolver backend
these changes reduce the size of the function somewhat and remove many
of its dependencies, including free. in principle it should now be
async-signal-safe, but this has not been verified in detail.

minor changes to error handling are also made.
2014-06-01 00:09:28 -04:00
Rich Felker 6f409bff00 refactor getaddrinfo and add support for most remaining features
this is the first phase of the "resolver overhaul" project.

conceptually, the results of getaddrinfo are a direct product of a
list of address results and a list of service results. the new code
makes this explicit by computing these lists separately and combining
the results. this adds support for services that have both tcp and udp
versions, where the caller has not specified which it wants, and
eliminates a number of duplicate code paths which were all producing
the final output addrinfo structures, but in subtly different ways,
making it difficult to implement any of the features which were
missing.

in addition to the above benefits, the refactoring allows for legacy
functions like gethostbyname to be implemented without using the
getaddrinfo function itself. such changes to the legacy functions have
not yet been made, however.

further improvements include matching of service alias names from
/etc/services (previously only the primary name was supported),
returning multiple results from /etc/hosts (previously only the first
matching line was honored), and support for the AI_V4MAPPED and AI_ALL
flags.

features which remain unimplemented are IDN translations (encoding
non-ASCII hostnames for DNS lookup) and the AI_ADDRCONFIG flag.

at this point, the DNS-based name resolving code is still based on the
old interfaces in __dns.c, albeit somewhat simpler in its use of them.
there may be some dead code which could already be removed, but
changes to this layer will be a later phase of the resolver overhaul.
2014-05-31 20:57:54 -04:00
Szabolcs Nagy 5f4c496693 fix POSIX namespace pollution in sys/types.h
int8_t, u_int8_t, etc types are moved under _BSD_SOURCE
2014-05-30 13:06:51 -04:00
Szabolcs Nagy bff6095d91 use cleaner code for handling float rounding in vfprintf
CONCAT(0x1p,LDBL_MANT_DIG) is not safe outside of libc,
use 2/LDBL_EPSILON instead.

fix was proposed by Morten Welinder.
2014-05-30 13:06:51 -04:00
Szabolcs Nagy c350468658 update netinet/in.h to match the current kernel uapi
from linux/in.h and linux/in6.h uapi headers the following
missing socket options were added:

IP_NODEFRAG - used with customized ipv4 headers
IPV6_RECVPATHMTU - for ipv6 path mtu
IPV6_PATHMTU - for ipv6 path mtu
IPV6_DONTFRAG - for ipv6 path mtu
IPV6_ADDR_PREFERENCES - RFC5014 Source Address Selection
IPV6_MINHOPCOUNT - RFC5082 Generalized TTL Security Mechanism
IPV6_ORIGDSTADDR - used by tproxy
IPV6_RECVORIGDSTADDR - used by tproxy
IPV6_TRANSPARENT - used by tproxy
IPV6_UNICAST_IF - ipv6 version of IP_UNICAST_IF

and socket option values:

IP_PMTUDISC_OMIT - value for IP_MTU_DISCOVER option, new in linux 3.14
IPV6_PMTUDISC_OMIT - same for IPV6_MTU_DISCOVER
IPV6_PMTUDISC_INTERFACE - ipv6 version of IP_PMTUDISC_INTERFACE
IPV6_PREFER_* - flags for IPV6_ADDR_PREFERENCES

not added: ipv6 flow info and flow label related definitions.
(it's unclear if libc should define these and namespace polluting
type name is involved so they are not provided for now)
2014-05-30 13:06:51 -04:00
Szabolcs Nagy c8ea212668 add SO_BPF_EXTENSIONS socket option
this was introduced to query BPF extension support with getsockopt
in linux 3.14, commit ea02f9411d9faa3553ed09ce0ec9f00ceae9885e
2014-05-30 13:06:51 -04:00
Szabolcs Nagy fd9571e20a add sched_{get,set}attr syscall numbers and SCHED_DEADLINE macro
linux 3.14 introduced sched_getattr and sched_setattr syscalls in
commit d50dde5a10f305253cbc3855307f608f8a3c5f73

and the related SCHED_DEADLINE scheduling policy in
commit aab03e05e8f7e26f51dee792beddcb5cca9215a5

but struct sched_attr "extended scheduling parameters data structure"
is not yet exported to userspace (necessary for using the syscalls)
so related uapi definitions are not added yet.
2014-05-30 13:06:51 -04:00
Rich Felker 24df8b09cf fix breakage from recent syscall commits due to missing errno macros 2014-05-30 12:59:36 -04:00
Szabolcs Nagy 8258014fd1 fix for broken kernel side RLIM_INFINITY on mips
On 32 bit mips the kernel uses -1UL/2 to mark RLIM_INFINITY (and
this is the definition in the userspace api), but since it is in
the middle of the valid range of limits and limits are often
compared with relational operators, various kernel side logic is
broken if larger than -1UL/2 limits are used. So we truncate the
limits to -1UL/2 in get/setrlimit and prlimit.

Even if the kernel side logic consistently treated -1UL/2 as greater
than any other limit value, there wouldn't be any clean workaround
that allowed using large limits:
* using -1UL/2 as RLIM_INFINITY in userspace would mean different
infinity value for get/setrlimt and prlimit (where infinity is always
-1ULL) and userspace logic could break easily (just like the kernel
is broken now) and more special case code would be needed for mips.
* translating -1UL/2 kernel side value to -1ULL in userspace would
mean that -1UL/2 limit cannot be set (eg. -1UL/2+1 had to be passed
to the kernel instead).
2014-05-30 03:09:26 -04:00
Rich Felker 106e65d6f6 break down coarse-grained 64-bit-off_t syscall remappings
using the existence of SYS_stat64 as the condition for remapping other
related syscalls is no longer valid, since new archs that omit the old
syscalls will not have SYS_stat or SYS_stat64, but still potentially
need SYS_fstat and others remapped. it would probably be possible to
get by with just one or two extra conditionals, but just breaking them
all down into separate conditions is robust and not significantly
heavier for the preprocessor.
2014-05-30 02:05:59 -04:00
Rich Felker 5cf9e8f860 additional fixes for linux kernel apis with old syscalls removed 2014-05-30 01:51:23 -04:00
Rich Felker 55f45bc722 fix sendfile syscall to use 64-bit off_t
somehow the remapping of this syscall to the 64-bit version was
overlooked. the issue was found, and patch provided, by Stefan
Kristiansson. presumably the reason this bug was not caught earlier is
that the syscall takes a pointer to off_t rather than a value, so on
little-endian systems, everything appears to work as long as the
offset value fits in the low 31 bits. on big-endian systems, though,
sendfile was presumably completely non-functional.
2014-05-30 01:12:42 -04:00
Rich Felker dd5f50da6f support linux kernel apis (new archs) with old syscalls removed
such archs are expected to omit definitions of the SYS_* macros for
syscalls their kernels lack from arch/$ARCH/bits/syscall.h. the
preprocessor is then able to select the an appropriate implementation
for affected functions. two basic strategies are used on a
case-by-case basis:

where the old syscalls correspond to deprecated library-level
functions, the deprecated functions have been converted to wrappers
for the modern function, and the modern function has fallback code
(omitted at the preprocessor level on new archs) to make use of the
old syscalls if the new syscall fails with ENOSYS. this also improves
functionality on older kernels and eliminates the incentive to program
with deprecated library-level functions for the sake of compatibility
with older kernels.

in other situations where the old syscalls correspond to library-level
functions which are not deprecated but merely lack some new features,
such as the *at functions, the old syscalls are still used on archs
which support them. this may change at some point in the future if or
when fallback code is added to the new functions to make them usable
(possibly with reduced functionality) on old kernels.
2014-05-29 21:01:32 -04:00
Rich Felker 2e55da9118 remove useless synchronization in exit and quick_exit
calling exit more than once invokes undefined behavior. in some cases
it's desirable to detect undefined behavior and diagnose it via a
predictable crash, but the code here was silently covering up an
uncommon case (exit from more than one thread) and turning a much more
common case (recursive calls to exit) into a permanent hang.
2014-05-29 16:35:09 -04:00
Rich Felker d677d3fe83 fix missing declaration of strcpy in implementation of tmpnam 2014-05-27 14:21:46 -04:00
Rich Felker 2fe6579125 overhaul tmpfile, tmpnam, and tempnam functions
these all now use the shared __randname function internally, rather
than duplicating logic for producing a random name. incorrect usage of
the access syscall (which works with real uid/gid, not effective) has
been removed, along with unnecessary heavy dependencies like snprintf.
2014-05-27 00:44:23 -04:00
Rich Felker 9b880a6b41 fix sys_open macro for archs without the plain open syscall 2014-05-27 00:11:37 -04:00
Rich Felker 9289931446 fix placement of multiple inclusion guard endif in internal syscall.h
this was messed up during a recent commit when the socketcall macros
were moved to the common internal/syscall.h, and the following commit
expanded the problem by adding more new content outside the guard.
2014-05-27 00:09:05 -04:00
Rich Felker 4ef04a27c7 fix type of extended argument array to pselect6 syscall
this only matters on x32 (and perhaps future 32-on-64 abis for other
archs); otherwise the type is long anyway. the cast through uintptr_t
prevents nonsensical "sign extension" of pointers, and follows the
principle that uintptr_t is the canonical integer type to which
pointer conversion is safe.
2014-05-26 21:26:46 -04:00
Rich Felker 594c827a22 support kernels with no SYS_open syscall, only SYS_openat
open is handled specially because it is used from so many places, in
so many variants (2 or 3 arguments, setting errno or not, and
cancellable or not). trying to do it as a function would not only
increase bloat, but would also risk subtle breakage.

this is the first step towards supporting "new" archs where linux
lacks "old" syscalls.
2014-05-24 22:54:05 -04:00
Rich Felker 44d28e5512 overhaul siginfo_t definition in signal.h
the main motivation for this change is that, with the previous
definition, it was arguably illegal, in standard C, to initialize both
si_value and si_pid/si_uid with designated initializers, due to the
rule that only one member of a union can have an initializer. whether
or not this affected real-world application code, it affected some
internal code, and clang was producing warnings (and possibly
generating incorrect code).

the new definition uses a more complex hierarchy of structs and unions
to avoid the need to initialize more than one member of a single union
in usage cases that make sense. further work would be needed to
eliminate even the ones with no practical applications.

at the same time, some fixes are made to the exposed names for
nonstandard fields, to match what software using them expects.
2014-05-24 20:39:46 -04:00
Rich Felker 0b4e0732db release 1.1.1 2014-05-20 18:19:53 -04:00
Rich Felker 7c6db373a5 trivial formatting fix for the config.mak generated by configure 2014-05-20 15:49:21 -04:00
Rich Felker dec66750b8 fix unhandled cases in strptime
%C, %U, %W, and %y handling were completely missing; %C wrongly
fell-through to unrelated cases, and the rest returned failure. for
now, they all parse numbers in the proper forms and range-check the
values, but they do not store the value anywhere.

it's not clear to me whether, as "derived" fields, %U and %W should
produce any result. they certainly cannot produce a result unless the
year and weekday are also converted, but in this case it might be
desirable for them to do so. clarification is needed on the intended
behavior of strptime in cases like this.

%C and %y have well-defined behavior as long as they are used together
(and %y is defined by itself but may change in the future).
implementing them (including their correct interaction) is left as a
later change to be made.

finally, strptime now rejects unknown/invalid format characters
instead of ignoring them.
2014-05-19 22:14:09 -04:00
Rich Felker 93be56ba88 remove unsupported nonstandard sysconf macros and their table entries
some of these may have been from ancient (pre-SUSv2) POSIX versions;
more likely, they were from POSIX drafts or glibc interpretations of
what ancient versions of POSIX should have added (instead they made
they described functionality mandatory and/or dropped it completely).
others are purely glibc-isms, many of them ill-thought-out, like
providing ways to lookup the min/max values of types at runtime
(despite the impossibility of them changing at runtime and the
impossibility of representing ULONG_MAX in a return value of type
long).

since our sysconf implementation does not support or return meaningful
values for any of these, it's harmful to have the macros around;
applications' build scripts may detect and attempt to use them, only
to get -1/EINVAL as a result.

if removing them does break some applications, and it's determined
that the usage was reasonable, some of these could be added back on an
as-needed basis, but they should return actual meaningful values, not
junk like they were returning before.
2014-05-19 12:18:16 -04:00
Rich Felker ff78355bc1 rework sysconf table to treat zero entries as invalid
based on patch by Timo Teräs. previously, the value zero was used as a
literal zero, meaning that all invalid sysconf "names", which should
result in sysconf returning -1, had to be explicitly listed. (in
addition, it was not possible for sysconf to set errno to EINVAL, as
there was no distinction between -1 as an error and -1 as a valid
result.)

now, the value 0 is used for invalid/undefined slots in the table and
a new switch table entry is used for returning literal zeros.

in addition, an off-by-one error in checking against the table size is
fixed.
2014-05-19 11:19:49 -04:00
Rich Felker 9ca4dae5d8 add configure check for broken gcc 4.9.0 and possibly other versions
this is gcc bug #61144. the broken compiler is detected, but the user
must manually work around it. this is partly to avoid complex logic
for adding workaround CFLAGS and attempting to recheck with them, and
partly for the sake of letting the user know the compiler is broken
(since the workaround will result in less-efficient code production).

some refactoring was also needed to move the check for gcc outside of
the check for whether to build the compiler wrapper.
2014-05-19 10:33:28 -04:00
Rich Felker 8a2d871987 add cp437 and cp850 to available iconv conversions
perhaps some additional legacy DOS-era codepages would also be useful
to have, but these are the ones for which there has been demand. the
size of the diff is due to the fact that legacychars.h is updated in
such a way that new characters are inserted into the table in unicode
codepoint order; thus other mappings in codepages.h have changed to
reflect the new table indices of their characters.
2014-05-13 19:24:48 -04:00
Rich Felker 8945667fad add configure check for working compiler
without this, broken choices of CC/CPPFLAGS/CFLAGS don't show up until
late in the configure process where they are confusingly reported as a
different failure such as incorrect long double type.
2014-05-12 14:22:57 -04:00
Szabolcs Nagy ac0acd569e fix strftime %s not to zero pad with default width=2 2014-05-08 19:04:48 +02:00