| /** | |
| * Copyright (c) 2026, The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #define LOG_TAG "resolv" | |
| #include "DnsResolverUnstableService.h" | |
| #include <aidl/android/net/resolv/unstable/BnDnsResolverUnstableCallback.h> | |
| #include <android-base/logging.h> | |
| #include <android/binder_process.h> | |
| #include <private/android_filesystem_config.h> | |
| #include <string.h> | |
| #include "DnsResolver.h" | |
| #include "PrivateDnsConfiguration.h" | |
| #include "binder_util.h" | |
| #include "doh.h" | |
| #include "res_debug.h" | |
| #include "util.h" | |
| namespace android { | |
| namespace net { | |
| using aidl::android::net::resolv::unstable::BnDnsResolverUnstableCallback; | |
| using aidl::android::net::resolv::unstable::DnsForwardingParamsParcel; | |
| using aidl::android::net::resolv::unstable::IDnsResolverUnstableCallback; | |
| using aidl::android::net::resolv::unstable::QueryParamsAssignmentParcel; | |
| using aidl::android::net::resolv::unstable::QueryParamsParcel; | |
| using aidl::android::net::resolv::unstable::ResolverParamsParcel; | |
| using ::ndk::ScopedAStatus; | |
| namespace { | |
| class TestUnstableCallback : public BnDnsResolverUnstableCallback { | |
| public: | |
| ::ndk::ScopedAStatus checkPermission(const std::string& /*permission*/, int32_t /*pid*/, | |
| int32_t /*uid*/, bool* _aidl_return) override { | |
| *_aidl_return = true; | |
| return ::ndk::ScopedAStatus::ok(); | |
| } | |
| }; | |
| #define ENFORCE_SYSTEM() \ | |
| do { \ | |
| uid_t uid = AIBinder_getCallingUid(); \ | |
| if (uid != AID_SYSTEM) return ::ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY); \ | |
| } while (0) | |
| #define ENFORCE_PERMISSIONS() \ | |
| do { \ | |
| uid_t uid = AIBinder_getCallingUid(); \ | |
| if (uid == AID_SYSTEM || uid == AID_NETWORK_STACK) break; \ | |
| if (uid == AID_ROOT && isDebuggable()) break; \ | |
| return ::ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY); \ | |
| } while (0) | |
| #define ENFORCE_TEST() \ | |
| do { \ | |
| if (AIBinder_getCallingUid() == AID_ROOT && isDebuggable()) break; \ | |
| return ::ndk::ScopedAStatus::fromExceptionCode(EX_SECURITY); \ | |
| } while (0) | |
| #define ENFORCE_INITIALIZED() \ | |
| do { \ | |
| if (!isInitialized()) { \ | |
| return ::ndk::ScopedAStatus::fromExceptionCodeWithMessage( \ | |
| EX_ILLEGAL_STATE, "Resolver is not initialized"); \ | |
| } \ | |
| } while (0) | |
| } // namespace | |
| bool DnsResolverUnstableService::isInitialized() const { | |
| std::lock_guard guard(mInitializeMutex); | |
| return mInitialized; | |
| } | |
| void DnsResolverUnstableService::initializeForTest() { | |
| std::lock_guard guard(mInitializeMutex); | |
| mInitialized = true; | |
| mCallback = ::ndk::SharedRefBase::make<TestUnstableCallback>(); | |
| } | |
| ScopedAStatus DnsResolverUnstableService::initialize( | |
| const std::shared_ptr<IDnsResolverUnstableCallback>& callback, | |
| const std::string& dohLibName, bool useNewDnsProxy) { | |
| ENFORCE_SYSTEM(); | |
| if (callback.get() == nullptr) { | |
| return ScopedAStatus::fromExceptionCode(EX_NULL_POINTER); | |
| } | |
| if (dohLibName != "libresolvrs_doh_ffi.so" && dohLibName != "libresolvrs_doh_ffi_q24.so") { | |
| return ScopedAStatus::fromExceptionCodeWithMessage(EX_SECURITY, "Invalid DoH library name"); | |
| } | |
| std::lock_guard guard(mInitializeMutex); | |
| if (mInitialized) { | |
| return ScopedAStatus::fromExceptionCodeWithMessage( | |
| EX_ILLEGAL_STATE, "IDnsResolverUnstable already initialized"); | |
| } | |
| mInitialized = true; | |
| mCallback = callback; | |
| ::loadDohLib(dohLibName.c_str()); | |
| doh_init_logger(isDebuggable() ? DOH_LOG_LEVEL_INFO : DOH_LOG_LEVEL_WARN); | |
| PrivateDnsConfiguration::getInstance().initDoh(); | |
| if (useNewDnsProxy) { | |
| int ret = gDnsResolv->resolverCtrl.enableProxy(); | |
| if (ret != 0) { | |
| LOG(ERROR) << "Failed to enable DNS proxy during initialize: " << strerror(-ret); | |
| } | |
| } | |
| return ScopedAStatus::ok(); | |
| } | |
| std::shared_ptr<aidl::android::net::resolv::unstable::IDnsResolverUnstableCallback> | |
| DnsResolverUnstableService::getCallback() { | |
| std::lock_guard guard(mInitializeMutex); | |
| return mCallback; | |
| } | |
| ScopedAStatus DnsResolverUnstableService::isAlive(bool* alive) { | |
| *alive = true; | |
| return ScopedAStatus::ok(); | |
| } | |
| ScopedAStatus DnsResolverUnstableService::setDnsForwarding( | |
| int32_t downstreamIfIndex, | |
| const std::optional<DnsForwardingParamsParcel>& forwardingParams) { | |
| ENFORCE_INITIALIZED(); | |
| ENFORCE_PERMISSIONS(); | |
| int res; | |
| if (forwardingParams.has_value()) { | |
| const auto& params = forwardingParams.value(); | |
| res = gDnsResolv->resolverCtrl.configureDnsForwarding(downstreamIfIndex, params.netId, | |
| params.uid); | |
| } else { | |
| res = gDnsResolv->resolverCtrl.stopDnsForwarding(downstreamIfIndex); | |
| } | |
| return statusFromErrcode(res); | |
| } | |
| ::ndk::ScopedAStatus DnsResolverUnstableService::enableProxyForTest() { | |
| ENFORCE_INITIALIZED(); | |
| ENFORCE_TEST(); | |
| int res = gDnsResolv->resolverCtrl.enableProxy(); | |
| return statusFromErrcode(res); | |
| } | |
| ::ndk::ScopedAStatus DnsResolverUnstableService::disableProxyForTest() { | |
| ENFORCE_INITIALIZED(); | |
| ENFORCE_TEST(); | |
| int res = gDnsResolv->resolverCtrl.disableProxyForTest(); | |
| return statusFromErrcode(res); | |
| } | |
| ::ndk::ScopedAStatus DnsResolverUnstableService::isProxyEnabled(bool* _aidl_return) { | |
| ENFORCE_INITIALIZED(); | |
| ENFORCE_PERMISSIONS(); | |
| *_aidl_return = gDnsResolv->resolverCtrl.isProxyEnabled(); | |
| return ::ndk::ScopedAStatus::ok(); | |
| } | |
| ::ndk::ScopedAStatus DnsResolverUnstableService::setQueryParamsListForNetwork( | |
| int32_t /*netId*/, const ResolverParamsParcel& /*resolverParams*/, | |
| const std::vector<QueryParamsParcel>& /*queryParamsList*/) { | |
| ENFORCE_INITIALIZED(); | |
| ENFORCE_PERMISSIONS(); | |
| // Stub implementation | |
| return ::ndk::ScopedAStatus::ok(); | |
| } | |
| ::ndk::ScopedAStatus DnsResolverUnstableService::setQueryParamsAssignmentsForNetwork( | |
| int32_t netId, const std::vector<QueryParamsAssignmentParcel>& assignments) { | |
| ENFORCE_INITIALIZED(); | |
| ENFORCE_PERMISSIONS(); | |
| int res = gDnsResolv->resolverCtrl.setQueryParamsAssignmentsForNetwork(netId, assignments); | |
| return statusFromErrcode(res); | |
| } | |
| } // namespace net | |
| } // namespace android |