Commit graph

183 commits

Author SHA1 Message Date
Dan Albert
2237fcf1fa Revert "Use inline keyword without underscores consistently"
This reverts commit 58f06e1112.

Bare `inline` is not ANSI C compatible, and NDK users may be using
that.

Test: treehugger
Change-Id: I82c5424522142001cd59da63ef3fd440014451ad
2024-05-14 17:55:54 +00:00
Nick Desaulniers
58f06e1112 Use inline keyword without underscores consistently
s/__inline__/inline
s/__inline/inline

Change-Id: I5d8d930a7a41d678a0795e7a608dacbb9eb8cb37
2024-04-17 10:51:09 -07:00
wuhaitao3
12f8ec4004 Closing the stream when the function returns
Test: build
Signed-off-by: wuhaitao3 <wuhaitao3@xiaomi.corp-partner.google.com>
Change-Id: I85a7935d911bf19c85c51a4c52a43e2efed34929
2023-12-05 03:29:10 +00:00
Yi Kong
7eeb11da01 Rewrite function definitions with identifier lists
This syntax is removed in C2x proposal N2432.

Test: presubmit
Change-Id: Ic1309841423143e48513dd8b67166b4fde2d67fb
2022-07-13 10:58:42 +08:00
Elliott Hughes
d0bbfa8327 Make res_init() work again.
Change 75830fb836 to fix _nres
initialization to be thread safe accidentally introduced a behavior
change whereby res_init() became a no-op. It also failed to remove all
direct accesses to _nres.

Move the file over to C++ so we can let RAII ensure we're always holding
a lock while using the global state, make all callers access the global
state via this class, and restore the previous behavior of res_init().

Test: atest DnsResolverTest
Bug: 166235340
Change-Id: Ib390a7eac063bc0ff5eeba755e8c74ef1383004e
2021-04-08 12:04:05 -07:00
Luke Huang
75830fb836 Initialize _nres only once
Currently, the initialization of _nres is not thread-saferes_mkquery is not thread-safe,
which might cause memory double free problem if caller ran under multithread.

To fix it, only initialize _nres once.
Also remove the redundant code.

Test: atest DnsResolverTest
Bug: 166235340
Change-Id: I9caa6eab37cb530fc60dae9bcca9650973a4536a
2021-04-07 02:05:50 +08:00
Elliott Hughes
e1dc4f62eb Fewer copies of ALIGN()/ALIGNBYTES.
Noticed while updating fts.c.

Bug: http://b/177003648
Test: treehugger
Change-Id: Ic3625c1c3af47c4dafb8ad686bbbddbc82b69b70
2021-01-11 11:51:29 -08:00
Xin Li
218b4e5a0b Merge rvc-qpr-dev-plus-aosp-without-vendor@6881855
Bug: 172690556
Merged-In: I25ee3022fb26e744eae9e3f14595ce52e76b337e
Change-Id: I7359621eca1148d6c3f3287fcaecdab8c03fa7f6
2020-11-28 19:22:49 -08:00
Lev Rumyantsev
814f38fc28 Make more functions weak for native bridge
Test: CtsNetTestCases android.net.cts.DnsTest#testDnsWorks
Bug: 170665833
Change-Id: I83242e336f6ee5a0115252a12eddf9def26db90f
2020-10-13 17:03:15 -07:00
Ken Chen
cf6b3ecd4d Fix OOB read in DNS resolver am: 43264bc365 am: c5ea7569a8 am: 2c2546b158
Original change: https://googleplex-android-review.googlesource.com/c/platform/bionic/+/12329970

Change-Id: I5e167b6717d7f90ba782e26ef553fb6d302b493a
2020-08-30 05:25:44 +00:00
Ken Chen
c5ea7569a8 Fix OOB read in DNS resolver am: 43264bc365
Original change: https://googleplex-android-review.googlesource.com/c/platform/bionic/+/12329970

Change-Id: Ief989a779e7fea19a92fb90a863017ecdb1bc7a3
2020-08-30 04:49:07 +00:00
Ken Chen
43264bc365 Fix OOB read in DNS resolver
The remote server specifies resplen, the length of the response it
intends to send. anssiz represents the size of the destination buffer.
If the reported resplen is larger than the anssiz, the code correctly
only reads up to anssiz bytes, but returns resplen. so later functions
will access far out of bounds.

The fix ensures that the length of send_vc return does not exceed the
buffer size.

Bug: 161362564
Test: build, flash, boot
Test: atest netd_integration_test
Merged-In: Id4b5df1be4652e4623847b0b0bad0af65b80fdd5
Change-Id: Id4b5df1be4652e4623847b0b0bad0af65b80fdd5
2020-08-07 20:19:43 +08:00
Elliott Hughes
4aef93acff android_getaddrinfo_proxy: fix memory leak on failure.
The shadowing of `ai` meant that the freeaddrinfo() call outside the
loop would never see anything but NULL.

Bug: https://issuetracker.google.com/143928781
Test: treehugger
Change-Id: I1bf137f7933201eb8024603bfd569ff7bbc7f9b7
2019-11-05 12:42:14 -08:00
chenbruce
2cc76462e5 Test for ns_parserr() and got a heap-buffer-overflow
In ns_name_skip of ns_name.c, there is a possible out of bounds read due to
a missing bounds check. This could lead to remote denial of service with no
additional execution privileges needed. User interaction is not needed for
exploitation.

Bug: 137346580
Test: built, flashed, booted
Test: ns_parserr_fuzzer test
Change-Id: Ib023bd9b2ae97795d64195de9922a6a32d7b2f37
2019-08-29 22:18:37 +08:00
Luke Huang
f47e89f29b Merge "Replace android_open_proxy with dns_open_proxy" 2018-11-28 12:21:33 +00:00
Luke Huang
e3ed892faa Replace android_open_proxy with dns_open_proxy
remove android_open_proxy and use dns_open_proxy instead of it.
dns_open_proxy is in libnetd_client and
it does the same thing as android_open_proxy except return value.
It returns fd directly now.

Test: build, dns works fine
Change-Id: I984743fb50b23eeb9a7d24e9fc347832acfe2afe
2018-11-19 18:00:37 +08:00
Ken Chen
f945716bf9 Free memory when it is no more used
Memory leak happen when Android data framework updates identical DNS
information to netd.

(cherrypick of c466fe95dc641d879683700aea4233ffd42bc762.)

Bug: 115595601
Test: netd_unit_test, netd_integration_test
Change-Id: Idcc0956729286563603f482258f488a1ccd75e6a
2018-11-08 13:29:13 -08:00
Ken Chen
c466fe95dc Free memory when it is no more used
Memory leak happen when Android data framework updates identical DNS
information to netd.

Bug: 115595601
Test: netd_unit_test, netd_integration_test
Change-Id: Idcc0956729286563603f482258f488a1ccd75e6a
2018-11-08 19:53:34 +08:00
nuccachen
8d65a81fb6 getnameinfo: Don't add flag NI_NUMERICHOST for well-known prefix address
Currently, getnameinfo() will never issue a reverse DNS lookup if the first
byte of the IPv6 address is 0x00. This means it is not possible to do a
reverse DNS lookup for a NAT64 address if the NAT64 prefix is the well-known
prefix. Make this possible by treating the well-known prefix specially. This
is not needed for unicast NAT64 prefixes because they do not start with 0x00.

Bug: 78545619
Test: netd_{unit,integration}_test pass
Test: bionic-unit-tests --gtest_filter='net*' pass
Change-Id: I176d30dcf411a5ffe1eec110db99cd73b48e956f
2018-11-01 16:40:34 +08:00
Bernie Innocenti
96c619f7db Clear the resolver timeout when setting default __res_params
This is currently a non-functional change, because base_timeout_msec is
never set to a non-default value (other than in unit tests).

Test: netd_unit_test --gtest_filter='ResolverTest.*'
Change-Id: I21989bab5cc9f55ee1392e30965eb405d720a28e
2018-08-09 17:58:35 +09:00
Bernie Innocenti
58d13622a5 Make the resolver retry timeout configurable via __res_params
The primary motivation is speeding up tests for non-responsive
nameservers. It's also base infrastructure for future work to compute
retry timeouts dynamically.

Using default timeouts (RES_TIMEOUT = 5 seconds):
  ResolverTest.GetAddrInfoV6_nonresponsive (45141 ms)

With base_timeout_msec=100:
  ResolverTest.GetAddrInfoV6_nonresponsive (1264 ms)

Test: executed unsubmitted test from aosp/713993
Change-Id: Id902089ca69ca8d28032180ba51e8937262ef490
2018-08-08 15:45:19 +09:00
Bernie Innocenti
b664724525 bionic: Convert res_send.c to poll()
Bug: 5854418
Test: adb shell /data/nativetest64/bionic-unit-tests-static/bionic-unit-tests-static
Test: manually:
    - flash and boot device
    - connect to wifi
    - launch WebView and resolv a few domains
Change-Id: I7b488e6d061d99a8a3c4bba69c2ac8609257eb6a
2018-07-05 11:39:09 +09:00
Lorenzo Colitti
5089e87559 fchown DNS lookup sockets to AID_DNS.
Currently DNS lookups are all sent as UID 0. In N we added an
AID_DNS user, but we never used it. A separate UID allows us to
account for and route DNS traffic differently from other UID 0
traffic such as kernel-originated traffic.

(cherry picked from commit 5f02720c88)

Fix: 29420831
Bug: 70673901
Test: DNS lookups continue to work
Test: netd_{unit,integration}_test pass
Change-Id: Ife3ca7df45de51b92437d2cfe627fb4703b672fb
Merged-In: I1c76fac51bdd4804620fe8a8567c9a650b8734d9
2018-05-11 00:56:51 +09:00
Lorenzo Colitti
5f02720c88 fchown DNS lookup sockets to AID_DNS.
Currently DNS lookups are all sent as UID 0. In N we added an
AID_DNS user, but we never used it. A separate UID allows us to
account for and route DNS traffic differently from other UID 0
traffic such as kernel-originated traffic.

Bug: 70673901
Test: DNS lookups continue to work
Test: netd_{unit,integration}_test pass
Change-Id: Ife3ca7df45de51b92437d2cfe627fb4703b672fb
2018-05-09 01:04:05 +09:00
Ben Schwartz
622a36923e Pad queries when EDNS0 is enabled.
Behavior is compliant with
https://tools.ietf.org/html/draft-ietf-dprive-padding-policy-04

EDNS0 is only enabled when the netcontext requests it, i.e. in DNS-over-TLS
mode.

Bug: 69623036
Bug: 64133961
Test: Wireshark verified. Integration tests echo padding and pass.
Merged-In: Ie5439b0ad505ebf393a83c87845fd02549afc4a2
Merged-In: I274d659782870818274526d23d3a3c4640cad92d
Change-Id: I5ef600e02a572d281441e890cc981614f150629b
(cherry picked from commit 23e4081009bb58d9c09e615186208b77749e72de)
2018-04-02 15:31:30 -07:00
Ben Schwartz
27dd915147 Pad queries when EDNS0 is enabled.
Behavior is compliant with
https://tools.ietf.org/html/draft-ietf-dprive-padding-policy-04

EDNS0 is only enabled when the netcontext requests it, i.e. in DNS-over-TLS
mode.

Bug: 69623036
Bug: 64133961
Test: Wireshark verified. Integration tests echo padding and pass.
Change-Id: I5ef600e02a572d281441e890cc981614f150629b
2018-04-02 05:28:16 +00:00
Victor Khimenko
5a6b9565c0 Mark getaddrinfo functions with __BIONIC_WEAK_FOR_NATIVE_BRIDGE mark
System libc talks to out-of-process daemon thus it's better to not
duplicate that functionality in native bridge.

Bug: 67993967

Test: make

Change-Id: Iabc887a94082806d048bc98bf8d3b21c7531d60d
(cherry picked from commit 5956847acc)
2018-03-27 17:32:38 +02:00
Ben Schwartz
a56793c238 Enable EDNS and the DO-bit if the netcontext asks
This effectively enables these features only in TLS mode
Test: Integration tests pass
Bug: 69623036
Bug: 64133961
Merged-In: Idbfbc0343e21ad713d9b4204fc48d63fb5b8dcd4
Merged-In: Ie3ae2390f01dbdcccf55fdd4e87b8a258702cfbc
Change-Id: I153a259b0c07717f664ee268b43da867a9b42152
(cherry picked from commit e7190ce3f2)
2018-03-21 07:57:14 -07:00
Ben Schwartz
e7190ce3f2 Enable EDNS and the DO-bit if the netcontext asks
This effectively enables these features only in TLS mode
Test: Integration tests pass
Bug:69623036

Change-Id: I153a259b0c07717f664ee268b43da867a9b42152
2018-03-21 01:01:29 +00:00
Ben Schwartz
9864564fca Lay the groundwork for enabling EDNS0 in queries.
This change does not enable EDNS0, so it should not
result in any behavior change.  However, enabling EDNS0
should now be possible with only a small additional change
to "flip the switch".

This change has also landed in NetBSD upstream
(http://gnats.netbsd.org/52578) so this change reduces
divergence from upstream.

Most of the code in this change is for caching of queries that contain
an additional section.

Bug: 15132200
Bug: 64133961
Test: Added integration tests for fallback to the netd suite.
Merged-In: I9b0abc6e4f3ff7f9c23a5cb921d392d2d3065d99
Merged-In: I9c06bbaf1883de4bdebd822f9a052901bf3efc04
Merged-In: Ie438b77bc1aeea0b0c700a90998cec31ac9a0015
Change-Id: Ic64bed0754e1d529dc0c0ab6a5e2f1ea201ff0d5
(cherry picked from commit 6eed8e1bb6)
2018-03-19 08:22:07 -07:00
Victor Khimenko
5956847acc Mark getaddrinfo functions with __BIONIC_WEAK_FOR_NATIVE_BRIDGE mark
System libc talks to out-of-process daemon thus it's better to not
duplicate that functionality in native bridge.

Bug: 67993967

Test: make

Change-Id: Iabc887a94082806d048bc98bf8d3b21c7531d60d
2018-03-14 17:42:15 +01:00
Erik Kline
b85d0bd2b9 Add flags field to struct android_net_context
This is for passing additional instructions through stages of the
DNS resolution pipeling.

Test: as follows
    - builds, flashes, boots
    - system/netd/tests/runtests.sh passes
Bug: 34953048
Bug: 64133961
Bug: 70694619
Bug: 72345192
Merged-In: I81c49586d675160b66dc2c51b76ad183cc418213
Merged-In: Ie8a8ee5784407d4ec304b3f40a6b234ebeaf6376
Merged-In: Icdec53cf5b210db5ad833dd3b64fa3d8ae74100a
Change-Id: I5a86cc5192e6cfa09c375ac9f5810102da3aba20
(cherry picked from commit 391143e51b)
2018-03-14 16:18:25 +09:00
Treehugger Robot
35e620cf98 Merge "Lay the groundwork for enabling EDNS0 in queries." 2018-03-13 20:48:34 +00:00
Erik Kline
391143e51b Add flags field to struct android_net_context
This is for passing additional instructions through stages of the
DNS resolution pipeling.

Test: as follows
    - builds, flashes, boots
    - system/netd/tests/runtests.sh passes
Bug: 34953048
Bug: 70694619
Bug: 72345192
Change-Id: I5a86cc5192e6cfa09c375ac9f5810102da3aba20
2018-03-12 16:34:56 +09:00
Ben Schwartz
47fb0e826d Reduce max DNS response from 64K to 8K
No reasonable response is larger than about 4K, and this should reduce the ability of misbehaving DNS servers to waste system resources.

Test: Integration tests pass
Change-Id: I43da94f57f9b376d08a1b176e0aedb6720148833
2018-02-27 23:30:34 +00:00
Ben Schwartz
6eed8e1bb6 Lay the groundwork for enabling EDNS0 in queries.
This change does not enable EDNS0, so it should not
result in any behavior change.  However, enabling EDNS0
should now be possible with only a small additional change
to "flip the switch".

This change has also landed in NetBSD upstream
(http://gnats.netbsd.org/52578) so this change reduces
divergence from upstream.

Most of the code in this change is for caching of queries that contain
an additional section.

Bug: 15132200
Test: Added integration tests for fallback to the netd suite.
Change-Id: Ic64bed0754e1d529dc0c0ab6a5e2f1ea201ff0d5
2018-02-05 14:41:01 -05:00
Elliott Hughes
bb7d9fb5d4 Improve glibc compatibility of gethostby*_r functions.
And add more tests.

Bug: N/A (but I'm here because a recent test broke existing tests)
Test: ran tests
Change-Id: Ib78430f179b43484a49bb50ff447ea6870c1ee3a
2017-10-23 17:38:35 -07:00
Elliott Hughes
5033918092 Complete <netdb.h>.
Add all the missing <netdb.h> functions.

Also fix getservbyport to handle a null protocol correctly.

Also fix getservbyname/getservbyport to not interfere with getservent.

Also fix endservent to reset getservent iteration.

Also reduce unnecessary differences from upstream NetBSD sethostent.c.

The servent implementation is still horrific, and we should
probably support protoent too so that debugging tools can use
getprotobyname/getprotobynumber.

Bug: N/A
Test: ran tests
Change-Id: I639108c46df0a768af297cf3bbce857cb1bef9d9
2017-10-15 10:37:51 -07:00
Ben Schwartz
90a83beb4d Make use of the DNS query hook in the netcontext
This will allow netd to run queries over TLS:
https://android-review.googlesource.com/#/c/391513/
https://android-review.googlesource.com/#/c/380593/

Bug: 34953048
Test: Device tests pass.  No issues during manual testing.
Change-Id: I8d613322307fc40cdba59b82599eda753697278f
2017-05-22 18:37:25 -04:00
Ben Schwartz
dd878fe129 Add a netcontext variant of gethostbyname
Bug: 34953048
Test: Integration tests pass
Change-Id: I670427d67fde09d8e76ea6a920c90a1969230c4f
2017-05-22 10:19:25 -04:00
Treehugger Robot
4767bc4a6c Merge "Add a netcontext variant of gethostbyaddr" 2017-05-22 10:06:31 +00:00
Treehugger Robot
761bb2be4d Merge "Add the DNS query hook to net context" 2017-05-22 02:51:36 +00:00
Ben Schwartz
50178056f7 Add a netcontext variant of gethostbyaddr
Bug: 34953048
Test: Integration tests pass
Change-Id: Id04830345be56cd7a077981c6e2acfb5568e7a88
2017-05-18 14:32:21 -04:00
Ben Schwartz
f0305dcee9 Add the DNS query hook to net context
This change
 - adds a query hook to android_net_context
 - exposes relevant definitions to netd
 - corrects a bug in query hooks' interaction with the cache

This change does not introduce any code to read the query hook
from the net context or make use of it.

Bug: 34953048
Test: Netd test suite passes
Change-Id: Ie091980e22ce9da07a3c4d387b371e544379d762
2017-05-18 12:03:11 -04:00
Christopher Ferris
7a3681e5b6 Move libc_log code into libasync_safe.
This library is used by a number of different libraries in the system.
Make it easy for platform libraries to use this library and create
an actual exported include file.

Change the names of the functions to reflect the new name of the library.

Run clang_format on the async_safe_log.cpp file since the formatting is
all over the place.

Bug: 31919199

Test: Compiled for angler/bullhead, and booted.
Test: Ran bionic unit tests.
Test: Ran the malloc debug tests.
Change-Id: I8071bf690c17b0ea3bc8dc5749cdd5b6ad58478a
2017-05-03 08:50:43 -07:00
Nick Kralevich
1df65daf9a Merge "Cleanup ANDROID_DNS_MODE and BIONIC_DNSCACHE"
am: 4df5ca1e02

Change-Id: I899ca838e59469348cd9bd25296ab9ac8e298291
2017-04-03 17:26:26 +00:00
Nick Kralevich
62c03a4ff4 Cleanup ANDROID_DNS_MODE and BIONIC_DNSCACHE
For security reasons, when a binary is executed which causes a security
transition (eg, a setuid binary, setgid binary, filesystem capabilities,
or SELinux domain transition), the AT_SECURE flag is set. This causes
certain blacklisted environment variables to be stripped before the
process is executed. The list of blacklisted environment variables is
stored in UNSAFE_VARIABLE_NAMES. Generally speaking, most environment
variables used internally by libc show up in this list.

Add ANDROID_DNS_MODE to the list of unsafe variables.
Similar to RESOLV_HOST_CONF and RES_OPTIONS (which are already
blacklisted), this variable controls how name resolution requests are
handled. Allowing ANDROID_DNS_MODE to be set across a security
boundary could induce resolution failures or otherwise impact
name resolution.

Remove BIONIC_DNSCACHE. This does not appear to be used, and setting
this variable across a security boundary could cause name resolution
problems.

Test: Android compiles and runs with no obvious problems.
Change-Id: I835a7b42d6afbc9c67866594c7951cfd9b355d81
2017-04-03 08:44:38 -07:00
Stephen Hines
fbb7c0e765 Merge "Don't mark __res_params or android_net_context as packed." am: 0c45c7dc5e
am: 6776efa346

Change-Id: I6b83f36d0e344c9174f7fcdd210ceecbafdd3027
2017-01-26 07:24:57 +00:00
Stephen Hines
9935e0fcee Don't mark __res_params or android_net_context as packed.
Bug: http://b/31532493

These internal API structures are never actually used anywhere that
would change behavior because it is "packed", so it is safe to remove.

Test: Builds correctly with the latest toolchain.
Change-Id: I5431992d840777605be75ab91dc21158d6d22c26
2017-01-25 00:42:45 -08:00
Elliott Hughes
8b114bde2d Check for bad packets in getaddrinfo.c's getanswer. am: 87c0dba7b1 am: dc0492ac4f am: f277bb86c6 am: 389e3e92e2 am: 0ffd7a5fb1 am: 0bf10a656b am: 2646bfc213
am: 9eea4e2864

Change-Id: I410415128a6b6f2d1c818d7b639d4cc45dc3ecd0
2016-12-08 21:27:19 +00:00