xref: /aosp_15_r20/system/netd/include/binder_utils/BinderUtil.h (revision 8542734a0dd1db395a4d42aae09c37f3c3c3e7a1)
1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*8542734aSAndroid Build Coastguard Worker  *
4*8542734aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8542734aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8542734aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8542734aSAndroid Build Coastguard Worker  *
8*8542734aSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8542734aSAndroid Build Coastguard Worker  *
10*8542734aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8542734aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8542734aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8542734aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8542734aSAndroid Build Coastguard Worker  * limitations under the License.
15*8542734aSAndroid Build Coastguard Worker  */
16*8542734aSAndroid Build Coastguard Worker 
17*8542734aSAndroid Build Coastguard Worker #pragma once
18*8542734aSAndroid Build Coastguard Worker 
19*8542734aSAndroid Build Coastguard Worker #include "NetdPermissions.h"
20*8542734aSAndroid Build Coastguard Worker 
21*8542734aSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
22*8542734aSAndroid Build Coastguard Worker #include <android-base/strings.h>
23*8542734aSAndroid Build Coastguard Worker #include <binder/IPCThreadState.h>
24*8542734aSAndroid Build Coastguard Worker #include <binder/IServiceManager.h>
25*8542734aSAndroid Build Coastguard Worker #include <binder/Status.h>
26*8542734aSAndroid Build Coastguard Worker #include <fmt/format.h>
27*8542734aSAndroid Build Coastguard Worker #include <private/android_filesystem_config.h>
28*8542734aSAndroid Build Coastguard Worker 
29*8542734aSAndroid Build Coastguard Worker #ifdef ANDROID_BINDER_STATUS_H
30*8542734aSAndroid Build Coastguard Worker #define IS_BINDER_OK(__ex__) (__ex__ == ::android::binder::Status::EX_NONE)
31*8542734aSAndroid Build Coastguard Worker 
32*8542734aSAndroid Build Coastguard Worker #define EXCEPTION_TO_STRING(__ex__, str)    \
33*8542734aSAndroid Build Coastguard Worker     case ::android::binder::Status::__ex__: \
34*8542734aSAndroid Build Coastguard Worker         return str;
35*8542734aSAndroid Build Coastguard Worker 
36*8542734aSAndroid Build Coastguard Worker #define TO_EXCEPTION(__ex__) __ex__;
37*8542734aSAndroid Build Coastguard Worker 
38*8542734aSAndroid Build Coastguard Worker #else
39*8542734aSAndroid Build Coastguard Worker #define IS_BINDER_OK(__ex__) (AStatus_isOk(AStatus_fromExceptionCode(__ex__)))
40*8542734aSAndroid Build Coastguard Worker 
41*8542734aSAndroid Build Coastguard Worker #define EXCEPTION_TO_STRING(__ex__, str) \
42*8542734aSAndroid Build Coastguard Worker     case __ex__:                         \
43*8542734aSAndroid Build Coastguard Worker         return str;
44*8542734aSAndroid Build Coastguard Worker 
45*8542734aSAndroid Build Coastguard Worker #define TO_EXCEPTION(__ex__) AStatus_getExceptionCode(AStatus_fromExceptionCode(__ex__));
46*8542734aSAndroid Build Coastguard Worker 
47*8542734aSAndroid Build Coastguard Worker #endif
48*8542734aSAndroid Build Coastguard Worker 
exceptionToString(int32_t exception)49*8542734aSAndroid Build Coastguard Worker inline std::string exceptionToString(int32_t exception) {
50*8542734aSAndroid Build Coastguard Worker     switch (exception) {
51*8542734aSAndroid Build Coastguard Worker         EXCEPTION_TO_STRING(EX_SECURITY, "SecurityException")
52*8542734aSAndroid Build Coastguard Worker         EXCEPTION_TO_STRING(EX_BAD_PARCELABLE, "BadParcelableException")
53*8542734aSAndroid Build Coastguard Worker         EXCEPTION_TO_STRING(EX_ILLEGAL_ARGUMENT, "IllegalArgumentException")
54*8542734aSAndroid Build Coastguard Worker         EXCEPTION_TO_STRING(EX_NULL_POINTER, "NullPointerException")
55*8542734aSAndroid Build Coastguard Worker         EXCEPTION_TO_STRING(EX_ILLEGAL_STATE, "IllegalStateException")
56*8542734aSAndroid Build Coastguard Worker         EXCEPTION_TO_STRING(EX_NETWORK_MAIN_THREAD, "NetworkMainThreadException")
57*8542734aSAndroid Build Coastguard Worker         EXCEPTION_TO_STRING(EX_UNSUPPORTED_OPERATION, "UnsupportedOperationException")
58*8542734aSAndroid Build Coastguard Worker         EXCEPTION_TO_STRING(EX_SERVICE_SPECIFIC, "ServiceSpecificException")
59*8542734aSAndroid Build Coastguard Worker         EXCEPTION_TO_STRING(EX_PARCELABLE, "ParcelableException")
60*8542734aSAndroid Build Coastguard Worker         EXCEPTION_TO_STRING(EX_TRANSACTION_FAILED, "TransactionFailedException")
61*8542734aSAndroid Build Coastguard Worker         default:
62*8542734aSAndroid Build Coastguard Worker             return "UnknownException";
63*8542734aSAndroid Build Coastguard Worker     }
64*8542734aSAndroid Build Coastguard Worker }
65*8542734aSAndroid Build Coastguard Worker 
66*8542734aSAndroid Build Coastguard Worker using LogFn = std::function<void(const std::string& msg)>;
67*8542734aSAndroid Build Coastguard Worker 
68*8542734aSAndroid Build Coastguard Worker template <typename LogType>
binderCallLogFn(const LogType & log,const LogFn & logFn)69*8542734aSAndroid Build Coastguard Worker void binderCallLogFn(const LogType& log, const LogFn& logFn) {
70*8542734aSAndroid Build Coastguard Worker     using namespace std::string_literals;
71*8542734aSAndroid Build Coastguard Worker 
72*8542734aSAndroid Build Coastguard Worker     bool hasReturnArgs;
73*8542734aSAndroid Build Coastguard Worker     std::string output;
74*8542734aSAndroid Build Coastguard Worker 
75*8542734aSAndroid Build Coastguard Worker     hasReturnArgs = !log.result.empty();
76*8542734aSAndroid Build Coastguard Worker     output.append(log.method_name + "("s);
77*8542734aSAndroid Build Coastguard Worker 
78*8542734aSAndroid Build Coastguard Worker     // input args
79*8542734aSAndroid Build Coastguard Worker     for (size_t i = 0; i < log.input_args.size(); ++i) {
80*8542734aSAndroid Build Coastguard Worker         output.append(log.input_args[i].second);
81*8542734aSAndroid Build Coastguard Worker         if (i != log.input_args.size() - 1) {
82*8542734aSAndroid Build Coastguard Worker             output.append(", "s);
83*8542734aSAndroid Build Coastguard Worker         }
84*8542734aSAndroid Build Coastguard Worker     }
85*8542734aSAndroid Build Coastguard Worker     output.append(")"s);
86*8542734aSAndroid Build Coastguard Worker 
87*8542734aSAndroid Build Coastguard Worker     const int exceptionCode = TO_EXCEPTION(log.exception_code);
88*8542734aSAndroid Build Coastguard Worker 
89*8542734aSAndroid Build Coastguard Worker     if (hasReturnArgs || !IS_BINDER_OK(exceptionCode)) {
90*8542734aSAndroid Build Coastguard Worker         output.append(" -> "s);
91*8542734aSAndroid Build Coastguard Worker     }
92*8542734aSAndroid Build Coastguard Worker 
93*8542734aSAndroid Build Coastguard Worker     // return status
94*8542734aSAndroid Build Coastguard Worker     if (!IS_BINDER_OK(exceptionCode)) {
95*8542734aSAndroid Build Coastguard Worker         // an exception occurred
96*8542734aSAndroid Build Coastguard Worker         const int errCode = log.service_specific_error_code;
97*8542734aSAndroid Build Coastguard Worker         output.append(fmt::format("{}({}, \"{}\")", exceptionToString(exceptionCode),
98*8542734aSAndroid Build Coastguard Worker                                   (errCode != 0) ? errCode : exceptionCode, log.exception_message));
99*8542734aSAndroid Build Coastguard Worker     }
100*8542734aSAndroid Build Coastguard Worker     // return args
101*8542734aSAndroid Build Coastguard Worker     if (hasReturnArgs) {
102*8542734aSAndroid Build Coastguard Worker         output.append("{" + log.result + "}");
103*8542734aSAndroid Build Coastguard Worker     }
104*8542734aSAndroid Build Coastguard Worker     // duration time
105*8542734aSAndroid Build Coastguard Worker     output.append(fmt::format(" <{:.2f}ms>", log.duration_ms));
106*8542734aSAndroid Build Coastguard Worker 
107*8542734aSAndroid Build Coastguard Worker     // escape newline characters to avoid multiline log entries
108*8542734aSAndroid Build Coastguard Worker     logFn(::android::base::StringReplace(output, "\n", "\\n", true));
109*8542734aSAndroid Build Coastguard Worker }
110*8542734aSAndroid Build Coastguard Worker 
111*8542734aSAndroid Build Coastguard Worker // The input permissions should be equivalent that this function would return ok if any of them is
112*8542734aSAndroid Build Coastguard Worker // granted.
checkAnyPermission(const std::vector<const char * > & permissions)113*8542734aSAndroid Build Coastguard Worker inline android::binder::Status checkAnyPermission(const std::vector<const char*>& permissions) {
114*8542734aSAndroid Build Coastguard Worker     pid_t pid = android::IPCThreadState::self()->getCallingPid();
115*8542734aSAndroid Build Coastguard Worker     uid_t uid = android::IPCThreadState::self()->getCallingUid();
116*8542734aSAndroid Build Coastguard Worker 
117*8542734aSAndroid Build Coastguard Worker     // TODO: Do the pure permission check in this function. Have another method
118*8542734aSAndroid Build Coastguard Worker     // (e.g. checkNetworkStackPermission) to wrap AID_SYSTEM and
119*8542734aSAndroid Build Coastguard Worker     // AID_NETWORK_STACK uid check.
120*8542734aSAndroid Build Coastguard Worker     // If the caller is the system UID, don't check permissions.
121*8542734aSAndroid Build Coastguard Worker     // Otherwise, if the system server's binder thread pool is full, and all the threads are
122*8542734aSAndroid Build Coastguard Worker     // blocked on a thread that's waiting for us to complete, we deadlock. http://b/69389492
123*8542734aSAndroid Build Coastguard Worker     //
124*8542734aSAndroid Build Coastguard Worker     // From a security perspective, there is currently no difference, because:
125*8542734aSAndroid Build Coastguard Worker     // 1. The system server has the NETWORK_STACK permission, which grants access to all the
126*8542734aSAndroid Build Coastguard Worker     //    IPCs in this file.
127*8542734aSAndroid Build Coastguard Worker     // 2. AID_SYSTEM always has all permissions. See ActivityManager#checkComponentPermission.
128*8542734aSAndroid Build Coastguard Worker     if (uid == AID_SYSTEM) {
129*8542734aSAndroid Build Coastguard Worker         return android::binder::Status::ok();
130*8542734aSAndroid Build Coastguard Worker     }
131*8542734aSAndroid Build Coastguard Worker     // AID_NETWORK_STACK own MAINLINE_NETWORK_STACK permission, don't IPC to system server to check
132*8542734aSAndroid Build Coastguard Worker     // MAINLINE_NETWORK_STACK permission. Cross-process(netd, networkstack and system server)
133*8542734aSAndroid Build Coastguard Worker     // deadlock: http://b/149766727
134*8542734aSAndroid Build Coastguard Worker     if (uid == AID_NETWORK_STACK) {
135*8542734aSAndroid Build Coastguard Worker         for (const char* permission : permissions) {
136*8542734aSAndroid Build Coastguard Worker             if (std::strcmp(permission, PERM_MAINLINE_NETWORK_STACK) == 0) {
137*8542734aSAndroid Build Coastguard Worker                 return android::binder::Status::ok();
138*8542734aSAndroid Build Coastguard Worker             }
139*8542734aSAndroid Build Coastguard Worker         }
140*8542734aSAndroid Build Coastguard Worker     }
141*8542734aSAndroid Build Coastguard Worker 
142*8542734aSAndroid Build Coastguard Worker     for (const char* permission : permissions) {
143*8542734aSAndroid Build Coastguard Worker         if (checkPermission(android::String16(permission), pid, uid)) {
144*8542734aSAndroid Build Coastguard Worker             return android::binder::Status::ok();
145*8542734aSAndroid Build Coastguard Worker         }
146*8542734aSAndroid Build Coastguard Worker     }
147*8542734aSAndroid Build Coastguard Worker 
148*8542734aSAndroid Build Coastguard Worker     auto err = android::base::StringPrintf(
149*8542734aSAndroid Build Coastguard Worker             "UID %d / PID %d does not have any of the following permissions: %s", uid, pid,
150*8542734aSAndroid Build Coastguard Worker             android::base::Join(permissions, ',').c_str());
151*8542734aSAndroid Build Coastguard Worker     return android::binder::Status::fromExceptionCode(android::binder::Status::EX_SECURITY,
152*8542734aSAndroid Build Coastguard Worker                                                       err.c_str());
153*8542734aSAndroid Build Coastguard Worker }
154*8542734aSAndroid Build Coastguard Worker 
statusFromErrcode(int ret)155*8542734aSAndroid Build Coastguard Worker inline android::binder::Status statusFromErrcode(int ret) {
156*8542734aSAndroid Build Coastguard Worker     if (ret) {
157*8542734aSAndroid Build Coastguard Worker         return android::binder::Status::fromServiceSpecificError(-ret, strerror(-ret));
158*8542734aSAndroid Build Coastguard Worker     }
159*8542734aSAndroid Build Coastguard Worker     return android::binder::Status::ok();
160*8542734aSAndroid Build Coastguard Worker }
161*8542734aSAndroid Build Coastguard Worker 
162*8542734aSAndroid Build Coastguard Worker #define DEPRECATED \
163*8542734aSAndroid Build Coastguard Worker     return binder::Status::fromExceptionCode(binder::Status::EX_UNSUPPORTED_OPERATION)
164