aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKen Chen <cken@google.com>2023-06-15 17:46:16 +0800
committerCherrypicker Worker <android-build-cherrypicker-worker@google.com>2023-08-10 08:47:21 +0000
commitf441eb6b3c46a4f6a2879b5c7a46b89135bf9532 (patch)
tree77b91a5237ffcdcc5b19010cdf927a685e931382
parent228f77676f03c359f134113be211f61932eab672 (diff)
downloadDnsResolver-f441eb6b3c46a4f6a2879b5c7a46b89135bf9532.tar.gz
Fix use-after-free in DNS64 discovery thread
DNS64 discovery thread is detached from binder requesting thread. But the discovery thread references resources not belongs to itself, which can be destroyed in dnsresolver destruction. Holds a strong pointer of Dns64Configuration in DNS64 discovery thread so that the instance of Dns64Configuration will keep until the DNS64 thread is force terminated. Ignore-AOSP-First: Fix security vulnerability Bug: 278303745 Test: atest (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:254115584ff558fb87ee6ec5f5bb043f76219910) Merged-In: Id74ea4e6f54a00805d3cc8a9d7e15e58a473b7d3 Change-Id: Id74ea4e6f54a00805d3cc8a9d7e15e58a473b7d3
-rw-r--r--Android.bp1
-rw-r--r--Dns64Configuration.cpp17
-rw-r--r--Dns64Configuration.h3
-rw-r--r--ResolverController.cpp14
-rw-r--r--ResolverController.h6
-rw-r--r--tests/Android.bp15
6 files changed, 36 insertions, 20 deletions
diff --git a/Android.bp b/Android.bp
index f2b5b4eb..679b50fa 100644
--- a/Android.bp
+++ b/Android.bp
@@ -230,6 +230,7 @@ cc_library {
"libprotobuf-cpp-lite",
"libstatslog_resolv",
"libsysutils",
+ "libutils",
"netd_event_listener_interface-lateststable-ndk",
"server_configurable_flags",
"stats_proto",
diff --git a/Dns64Configuration.cpp b/Dns64Configuration.cpp
index a1fe8717..fc1428db 100644
--- a/Dns64Configuration.cpp
+++ b/Dns64Configuration.cpp
@@ -24,6 +24,7 @@
#include <netdutils/DumpWriter.h>
#include <netdutils/InternetAddresses.h>
#include <netdutils/ThreadUtil.h>
+#include <utils/StrongPointer.h>
#include <thread>
#include <utility>
@@ -36,6 +37,7 @@
namespace android {
+using android::sp;
using netdutils::DumpWriter;
using netdutils::IPAddress;
using netdutils::IPPrefix;
@@ -61,8 +63,9 @@ void Dns64Configuration::startPrefixDiscovery(unsigned netId) {
// Emplace a copy of |cfg| in the map.
mDns64Configs.emplace(std::make_pair(netId, cfg));
+ const sp<Dns64Configuration> thiz = sp<Dns64Configuration>::fromExisting(this);
// Note that capturing |cfg| in this lambda creates a copy.
- std::thread discovery_thread([this, cfg, netId] {
+ std::thread discovery_thread([thiz, cfg, netId] {
setThreadName(fmt::format("Nat64Pfx_{}", netId));
// Make a mutable copy rather than mark the whole lambda mutable.
@@ -75,28 +78,28 @@ void Dns64Configuration::startPrefixDiscovery(unsigned netId) {
.build();
while (true) {
- if (!this->shouldContinueDiscovery(evalCfg)) break;
+ if (!thiz->shouldContinueDiscovery(evalCfg)) break;
android_net_context netcontext{};
- mGetNetworkContextCallback(evalCfg.netId, 0, &netcontext);
+ thiz->mGetNetworkContextCallback(evalCfg.netId, 0, &netcontext);
// Prefix discovery must bypass private DNS because in strict mode
// the server generally won't know the NAT64 prefix.
netcontext.flags |= NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS;
if (doRfc7050PrefixDiscovery(netcontext, &evalCfg)) {
- this->recordDns64Config(evalCfg);
+ thiz->recordDns64Config(evalCfg);
break;
}
- if (!this->shouldContinueDiscovery(evalCfg)) break;
+ if (!thiz->shouldContinueDiscovery(evalCfg)) break;
if (!backoff.hasNextTimeout()) break;
{
- std::unique_lock<std::mutex> cvGuard(mMutex);
+ std::unique_lock<std::mutex> cvGuard(thiz->mMutex);
// TODO: Consider some chrono math, combined with wait_until()
// perhaps, to prevent early re-resolves from the removal of
// other netids with IPv6-only nameservers.
- mCv.wait_for(cvGuard, backoff.getNextTimeout());
+ thiz->mCv.wait_for(cvGuard, backoff.getNextTimeout());
}
}
});
diff --git a/Dns64Configuration.h b/Dns64Configuration.h
index 7f982989..4170e32d 100644
--- a/Dns64Configuration.h
+++ b/Dns64Configuration.h
@@ -27,6 +27,7 @@
#include <android-base/thread_annotations.h>
#include <netdutils/DumpWriter.h>
#include <netdutils/InternetAddresses.h>
+#include <utils/RefBase.h>
struct android_net_context;
@@ -48,7 +49,7 @@ namespace net {
* Thread-safety: All public methods in this class MUST be thread-safe.
* (In other words: this class handles all its locking privately.)
*/
-class Dns64Configuration {
+class Dns64Configuration : virtual public RefBase {
public:
// Simple data struct for passing back packet NAT64 prefix event information to the
// Dns64PrefixCallback callback.
diff --git a/ResolverController.cpp b/ResolverController.cpp
index 7fe01d48..66361143 100644
--- a/ResolverController.cpp
+++ b/ResolverController.cpp
@@ -155,11 +155,11 @@ int getDnsInfo(unsigned netId, std::vector<std::string>* servers, std::vector<st
} // namespace
ResolverController::ResolverController()
- : mDns64Configuration(
+ : mDns64Configuration(android::sp<Dns64Configuration>::make(
[](uint32_t netId, uint32_t uid, android_net_context* netcontext) {
gResNetdCallbacks.get_network_context(netId, uid, netcontext);
},
- std::bind(sendNat64PrefixEvent, std::placeholders::_1)) {}
+ std::bind(sendNat64PrefixEvent, std::placeholders::_1))) {}
void ResolverController::destroyNetworkCache(unsigned netId) {
LOG(VERBOSE) << __func__ << ": netId = " << netId;
@@ -173,7 +173,7 @@ void ResolverController::destroyNetworkCache(unsigned netId) {
event.network_type(), event.private_dns_modes(), bytesField);
resolv_delete_cache_for_net(netId);
- mDns64Configuration.stopPrefixDiscovery(netId);
+ mDns64Configuration->stopPrefixDiscovery(netId);
privateDnsConfiguration.clear(netId);
// Don't get this instance in PrivateDnsConfiguration. It's probe to deadlock.
@@ -276,16 +276,16 @@ int ResolverController::getResolverInfo(int32_t netId, std::vector<std::string>*
}
void ResolverController::startPrefix64Discovery(int32_t netId) {
- mDns64Configuration.startPrefixDiscovery(netId);
+ mDns64Configuration->startPrefixDiscovery(netId);
}
void ResolverController::stopPrefix64Discovery(int32_t netId) {
- return mDns64Configuration.stopPrefixDiscovery(netId);
+ return mDns64Configuration->stopPrefixDiscovery(netId);
}
// TODO: use StatusOr<T> to wrap the result.
int ResolverController::getPrefix64(unsigned netId, netdutils::IPPrefix* prefix) {
- netdutils::IPPrefix p = mDns64Configuration.getPrefix64(netId);
+ netdutils::IPPrefix p = mDns64Configuration->getPrefix64(netId);
if (p.family() != AF_INET6 || p.length() == 0) {
return -ENOENT;
}
@@ -352,7 +352,7 @@ void ResolverController::dump(DumpWriter& dw, unsigned netId) {
params.max_samples, params.base_timeout_msec, params.retry_count);
}
- mDns64Configuration.dump(dw, netId);
+ mDns64Configuration->dump(dw, netId);
const auto privateDnsStatus = PrivateDnsConfiguration::getInstance().getStatus(netId);
dw.println("Private DNS mode: %s", getPrivateDnsModeString(privateDnsStatus.mode));
if (privateDnsStatus.dotServersMap.size() == 0) {
diff --git a/ResolverController.h b/ResolverController.h
index 0af830ee..b74cff92 100644
--- a/ResolverController.h
+++ b/ResolverController.h
@@ -55,10 +55,10 @@ class ResolverController {
// Set or clear a NAT64 prefix discovered by other sources (e.g., RA).
int setPrefix64(unsigned netId, const netdutils::IPPrefix& prefix) {
- return mDns64Configuration.setPrefix64(netId, prefix);
+ return mDns64Configuration->setPrefix64(netId, prefix);
}
- int clearPrefix64(unsigned netId) { return mDns64Configuration.clearPrefix64(netId); }
+ int clearPrefix64(unsigned netId) { return mDns64Configuration->clearPrefix64(netId); }
// Return the current NAT64 prefix network, regardless of how it was discovered.
int getPrefix64(unsigned netId, netdutils::IPPrefix* prefix);
@@ -66,7 +66,7 @@ class ResolverController {
void dump(netdutils::DumpWriter& dw, unsigned netId);
private:
- Dns64Configuration mDns64Configuration;
+ android::sp<Dns64Configuration> mDns64Configuration;
};
} // namespace net
} // namespace android
diff --git a/tests/Android.bp b/tests/Android.bp
index 72fb008b..9d8f1e7f 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -271,6 +271,7 @@ cc_test {
"libstatslog_resolv",
"libstatspush_compat",
"libsysutils",
+ "libutils",
"resolv_stats_test_utils",
"server_configurable_flags",
"stats_proto",
@@ -377,6 +378,18 @@ cc_defaults {
"server_configurable_flags",
"stats_proto",
],
+ target: {
+ android: {
+ shared_libs: [
+ "libutils",
+ ],
+ },
+ host: {
+ static_libs: [
+ "libutils",
+ ],
+ },
+ },
fuzz_config: {
cc: [
"cken@google.com",
@@ -421,14 +434,12 @@ cc_fuzz {
shared_libs: [
"libbinder_ndk",
"libbinder",
- "libutils",
],
},
host: {
static_libs: [
"libbinder_ndk",
"libbinder",
- "libutils",
],
},
darwin: {