blob: c73b165b5dacdc51923af30ada578fb97c2954e9 [file] [edit]
/*
* Copyright (C) 2019 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.
*
*/
#include "util.h"
#define LOG_TAG "resolv"
#include <android-base/logging.h>
#include "DnsResolver.h"
#include "DnsResolverService.h"
#include "DnsResolverUnstableService.h"
#include "binder_util.h"
#include <android-base/format.h>
#include <android-base/parseint.h>
#include <private/android_filesystem_config.h> // AID_SYSTEM
#include <server_configurable_flags/get_flags.h>
using aidl::android::net::resolv::unstable::IDnsResolverUnstableCallback;
using android::base::ParseInt;
using server_configurable_flags::GetServerConfigurableFlag;
socklen_t sockaddrSize(const sockaddr* sa) {
if (sa == nullptr) return 0;
switch (sa->sa_family) {
case AF_INET:
return sizeof(sockaddr_in);
case AF_INET6:
return sizeof(sockaddr_in6);
default:
return 0;
}
}
socklen_t sockaddrSize(const sockaddr_storage& ss) {
return sockaddrSize(reinterpret_cast<const sockaddr*>(&ss));
}
int getExperimentFlagInt(const std::string& flagName, int defaultValue) {
int val = defaultValue;
ParseInt(GetServerConfigurableFlag("netd_native", flagName, ""), &val);
return val;
}
std::string timestampToString(const std::chrono::system_clock::time_point& ts) {
using std::chrono::duration_cast;
using std::chrono::milliseconds;
const auto time_sec = std::chrono::system_clock::to_time_t(ts);
char buf[32];
std::strftime(buf, sizeof(buf), "%H:%M:%S", std::localtime(&time_sec));
int ms = duration_cast<milliseconds>(ts.time_since_epoch()).count() % 1000;
return fmt::format("{}.{:03d}", buf, ms);
}
namespace android {
namespace net {
bool checkCallingPermission(const char* permission, pid_t pid, uid_t uid,
IDnsResolverUnstableCallback* callback) {
// If the caller is the system UID, don't check permissions.
// If the caller is root and the system is debuggable, don't check permissions.
// Otherwise, if the system server's binder thread pool is full, and all the threads are
// blocked on a thread that's waiting for us to complete, we deadlock. http://b/69389492
//
// From a security perspective, there is currently no difference, because:
// 1. The only permissions we check in netd's binder interface are CONNECTIVITY_INTERNAL
// and NETWORK_STACK, which the system server always has (or MAINLINE_NETWORK_STACK, which
// is equivalent to having both CONNECTIVITY_INTERNAL and NETWORK_STACK).
// 2. AID_SYSTEM always has all permissions. See ActivityManager#checkComponentPermission.
if (uid == AID_SYSTEM || (uid == AID_ROOT && isDebuggable())) {
return true;
}
if (callback == nullptr) {
LOG(ERROR) << "IDnsResolverUnstable::initialize() not called, can't check permissions";
return false;
}
bool hasPerm = false;
auto status = callback->checkPermission(permission, pid, uid, &hasPerm);
if (!status.isOk()) {
LOG(ERROR) << "IDnsResolverUnstableCallback::checkPermission failed: "
<< status.getMessage();
return false;
}
return hasPerm;
}
} // namespace net
} // namespace android