Commit Graph

2550 Commits

Author SHA1 Message Date
Rich Felker
4e0b4a5de7 add tlsdesc support for i386 2014-06-19 02:50:45 -04:00
Rich Felker
880c479f0e optimize i386 ___tls_get_addr asm 2014-06-19 02:48:45 -04:00
Rich Felker
e75b16cf93 change dynamic TLS installation strategy to optimize access
previously, accesses to dynamic TLS had to check two conditions before
being able to use a dtv slot: (1) that the module index was within the
bounds of the current dtv size, and (2) that the dynamic tls for the
requested module index was already installed in the dtv.

this commit changes the installation strategy so that, whenever an
attempt is made to access dynamic TLS that's not yet installed in the
dtv, the dynamic TLS for all lower-index modules is also installed.
thus it provides a new invariant: if a given module index is within
the bounds of the current dtv size, we automatically know that its TLS
is installed and directly available. the requirement that the second
condition (above) be checked is eliminated.
2014-06-19 02:16:57 -04:00
Rich Felker
9d15d5e753 add arch-generic support for tlsdesc relocations to dynamic linker
this code is non-functional without further changes to link up the
arch-specific reloc types for tlsdesc and add asm implementations of
__tlsdesc_static and __tlsdesc_dynamic.
2014-06-19 02:01:06 -04:00
Rich Felker
cef0f289f6 fix incorrect comparison loop condition in memmem
the logic for this loop was copied from null-terminated-string logic
in strstr without properly adapting it to work with explicit lengths.

presumably this error could result in false negatives (wrongly
comparing past the end of the needle/haystack), false positives
(stopping comparison early when the needle contains null bytes), and
crashes (from runaway reads past the end of mapped memory).
2014-06-19 00:42:28 -04:00
Rich Felker
7c73cacd09 reduce code duplication in dynamic linker error paths
eventually this should help making dlerror thread-safe too.
2014-06-18 03:05:42 -04:00
Rich Felker
adf94c1966 refactor to remove arch-specific relocation code from dynamic linker
this was one of the main instances of ugly code duplication: all archs
use basically the same types of relocations, but roughly equivalent
logic was duplicated for each arch to account for the different naming
and numbering of relocation types and variation in whether REL or RELA
records are used.

as an added bonus, both REL and RELA are now supported on all archs,
regardless of which is used by the standard toolchain.
2014-06-18 02:44:02 -04:00
Rich Felker
94cf991bf4 fix powerpc dynamic linker thread-pointer-relative relocations
processing of R_PPC_TPREL32 was ignoring the addend provided by the
RELA-style relocation and instead using the inline value as the
addend. this presumably broke dynamic-linked access to initial TLS in
cases where the addend was nonzero.
2014-06-17 14:32:43 -04:00
Rich Felker
f4cc276018 multiple fixes to sh (superh) dynamic linker relocations
the following issues are fixed:

- R_SH_REL32 was adding the load address of the module being relocated
  to the result. this seems to have been a mistake in the original
  port, since it does not match other dynamic linker implementations
  and since adding a difference between two addresses (the symbol
  value and the relocation address) to a load address does not make
  sense.

- R_SH_TLS_DTPMOD32 was wrongly accepting an inline addend (i.e. using
  += rather than = on *reloc_addr) which makes no sense; addition is
  not an operation that's defined on module ids.

- R_SH_TLS_DTPOFF32 and R_SH_TLS_TPOFF32 were wrongly using inline
  addends rather than the RELA-provided addends.

in addition, handling of R_SH_GLOB_DAT, R_SH_JMP_SLOT, and R_SH_DIR32
are merged to all honor the addend. the first two should not need it
for correct usage generated by toolchains, but other dynamic linkers
allow addends here, and it simplifies the code anyway.

these issues were spotted while reviewing the code for the purpose of
refactoring this part of the dynamic linker. no testing was performed.
2014-06-17 13:56:54 -04:00
Rich Felker
bfa09700b9 dynamic linker: permit error returns from arch-specific reloc function
the immediate motivation is supporting TLSDESC relocations which
require allocation and thus may fail (unless we pre-allocate), but
this mechanism should also be used for throwing an error on
unsupported or invalid relocation types, and perhaps in certain cases,
for reporting when a relocation is not satisfiable.
2014-06-16 03:09:07 -04:00
Clément Vasseur
4e5c7a2176 fix missing argument to syscall in fanotify_mark 2014-06-14 00:10:16 -04:00
Rich Felker
66fcde4ae4 support optional-argument extension to getopt via double-colon
this extension is not incompatible with the standard behavior of the
function, not expensive, and avoids requiring a replacement getopt
with full GNU extensions for a few important apps including busybox's
sed with the -i option.
2014-06-11 23:38:44 -04:00
Rich Felker
d79b27785f fail configure on --enable-shared if -Bsymbolic-functions doesn't work
previously, a warning was issued in this case no matter what, even if
--disable-shared was used. now, the default for --enable-shared is
changed from "yes" to "auto", and the warning is issued by default,
but becomes an error if --enable-shared is used, and the test is
suppressed completely if --disable-shared is used.
2014-06-10 12:11:12 -04:00
Rich Felker
ac31bf278d simplify errno implementation
the motivation for the errno_ptr field in the thread structure, which
this commit removes, was to allow the main thread's errno to keep its
address when lazy thread pointer initialization was used. &errno was
evaluated prior to setting up the thread pointer and stored in
errno_ptr for the main thread; subsequently created threads would have
errno_ptr pointing to their own errno_val in the thread structure.

since lazy initialization was removed, there is no need for this extra
level of indirection; __errno_location can simply return the address
of the thread's errno_val directly. this does cause &errno to change,
but the change happens before entry to application code, and thus is
not observable.
2014-06-10 04:49:49 -04:00
Rich Felker
df15168cf8 replace all remaining internal uses of pthread_self with __pthread_self
prior to version 1.1.0, the difference between pthread_self (the
public function) and __pthread_self (the internal macro or inline
function) was that the former would lazily initialize the thread
pointer if it was not already initialized, whereas the latter would
crash in this case. since lazy initialization is no longer supported,
use of pthread_self no longer makes sense; it simply generates larger,
slower code.
2014-06-10 04:02:40 -04:00
Rich Felker
64e32287f9 add thread-pointer support for pre-2.6 kernels on i386
such kernels cannot support threads, but the thread pointer is also
important for other purposes, most notably stack protector. without a
valid thread pointer, all code compiled with stack protector will
crash. the same applies to any use of thread-local storage by
applications or libraries.

the concept of this patch is to fall back to using the modify_ldt
syscall, which has been around since linux 1.0, to setup the gs
segment register. since the kernel does not have a way to
automatically assign ldt entries, use of slot zero is hard-coded. if
this fallback path is used, __set_thread_area returns a positive value
(rather than the usual zero for success, or negative for error)
indicating to the caller that the thread pointer was successfully set,
but only for the main thread, and that thread creation will not work
properly. the code in __init_tp has been changed accordingly to record
this result for later use by pthread_create.
2014-06-10 03:36:56 -04:00
Rich Felker
246e752d9e avoid spurious lookup failures from badly-behaved nameservers
the results of a dns query, whether it's performed as part of one of
the standard name-resolving functions or directly by res_send, should
be a function of the query, not of the particular nameserver that
responds to it. thus, all responses which indicate a failure or
refusal by the nameserver, as opposed to a positive or negative result
for the query, should be ignored.

the strategy used is to re-issue the query immediately (but with a
limit on the number of retries, in case the server is really broken)
when a response code of 2 (server failure, typically transient) is
seen, and otherwise take no action on bad responses (which generally
indicate a misconfigured nameserver or one which the client does not
have permission to use), allowing the normal retry interval to apply
and of course accepting responses from other nameservers queried in
parallel.

empirically this matches the traditional resolver behavior for
nameservers that respond with a code of 2 in the case where there is
just a single nameserver configured. the behavior diverges when
multiple nameservers are available, since musl is querying them in
parallel. in this case we are mildly more aggressive at retrying.
2014-06-07 04:09:21 -04:00
Rich Felker
f616294914 use default timezone from /etc/localtime if $TZ is unset/blank
the way this is implemented, it also allows explicit setting of
TZ=/etc/localtime even for suid programs. this is not a problem
because /etc/localtime is a trusted path, much like the trusted
zoneinfo search path.
2014-06-06 20:46:05 -04:00
Rich Felker
3ff3a1d541 release 1.1.2 2014-06-06 18:36:00 -04:00
Timo Teräs
348fc5561c implement %y and %C specifiers in strptime 2014-06-06 17:41:12 -04:00
Rich Felker
9c2d437cb3 avoid invalid use of va_arg in open
reading the variadic mode argument is only valid when the O_CREAT flag
is present. this probably does not matter, but is needed for formal
correctness, and could affect LTO or other full-program analysis.
2014-06-06 15:43:16 -04:00
Rich Felker
7765706c05 add O_CLOEXEC fallback for open and related functions
since there is no easy way to detect whether open honored or ignored
the O_CLOEXEC flag, the optimal solution to providing a fallback is
simply to make the fcntl syscall to set the close-on-exec flag
immediately after open returns.
2014-06-06 15:42:42 -04:00
Rich Felker
1cacc01dc0 optimize SOCK_CLOEXEC fallback for socket function
the fcntl function is heavy, so make the syscall directly instead.
also, avoid the code size and runtime overhead of querying the old
flags, since it's reasonable to assume nothing will be set on a
newly-created socket. this code is only used on old kernels which lack
proper atomic close-on-exec support, so future changes that might
invalidate such an assumption do not need to be considered.
2014-06-06 15:30:28 -04:00
Rich Felker
1d348566e6 add SOCK_CLOEXEC fallback for socketpair on old kernels
as usual, this is non-atomic, but better than producing an error or
failing to set the close-on-exec flag at all.
2014-06-06 15:29:00 -04:00
Szabolcs Nagy
abce315639 implement dn_comp RFC 1035 domain name compression
the input name is validated, the other parameters are assumed to be
valid (the list of already compressed names are not checked for
infinite reference loops or out-of-bound offsets).

names are handled case-sensitively for now.
2014-06-06 20:44:54 +02:00
Szabolcs Nagy
07355f503a accept trailing . and empty domain names
trailing . should be accepted in domain name strings by convention
(RFC 1034), host name lookup accepts "." but rejects empty "", res_*
interfaces also accept empty name following existing practice.
2014-06-06 20:41:15 +02:00
Rich Felker
9ce2860fa3 add vdso clock_gettime acceleration support to i386 2014-06-06 03:29:36 -04:00
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