blob: 48894c6926c8d865360b4fbf6ada9964d3a0f741 [file] [log] [blame]
Alex Buynytskyy96e350b2020-04-02 20:03:47 -07001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <android-base/stringprintf.h>
20#include <binder/IPCThreadState.h>
21#include <binder/PermissionCache.h>
22#include <binder/PermissionController.h>
23#include <binder/Status.h>
24
25namespace android::incremental {
26
27inline binder::Status Ok() {
28 return binder::Status::ok();
29}
30
31inline binder::Status Exception(uint32_t code, const std::string& msg) {
32 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
33}
34
35inline int fromBinderStatus(const binder::Status& status) {
36 return status.exceptionCode() == binder::Status::EX_SERVICE_SPECIFIC
37 ? status.serviceSpecificErrorCode() > 0 ? -status.serviceSpecificErrorCode()
38 : status.serviceSpecificErrorCode() == 0
39 ? -EFAULT
40 : status.serviceSpecificErrorCode()
41 : -EIO;
42}
43
Alex Buynytskyy1d892162020-04-03 23:00:19 -070044inline binder::Status CheckPermissionForDataDelivery(const char* permission, const char* operation,
45 const char* package) {
Alex Buynytskyy96e350b2020-04-02 20:03:47 -070046 using android::base::StringPrintf;
47
48 int32_t pid;
49 int32_t uid;
50
51 if (!PermissionCache::checkCallingPermission(String16(permission), &pid, &uid)) {
52 return Exception(binder::Status::EX_SECURITY,
53 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
54 }
55
Alex Buynytskyy1d892162020-04-03 23:00:19 -070056 String16 packageName{package};
57
Alex Buynytskyy96e350b2020-04-02 20:03:47 -070058 // Caller must also have op granted.
59 PermissionController pc;
Alex Buynytskyy1d892162020-04-03 23:00:19 -070060 if (auto packageUid = pc.getPackageUid(packageName, 0); packageUid != uid) {
Alex Buynytskyy96e350b2020-04-02 20:03:47 -070061 return Exception(binder::Status::EX_SECURITY,
Alex Buynytskyy1d892162020-04-03 23:00:19 -070062 StringPrintf("UID %d / PID %d does not own package %s", uid, pid,
63 package));
Alex Buynytskyy96e350b2020-04-02 20:03:47 -070064 }
Alex Buynytskyy1d892162020-04-03 23:00:19 -070065 switch (auto result = pc.noteOp(String16(operation), uid, packageName); result) {
Alex Buynytskyy96e350b2020-04-02 20:03:47 -070066 case PermissionController::MODE_ALLOWED:
67 case PermissionController::MODE_DEFAULT:
68 return binder::Status::ok();
69 default:
70 return Exception(binder::Status::EX_SECURITY,
Alex Buynytskyy1d892162020-04-03 23:00:19 -070071 StringPrintf("UID %d / PID %d / package %s lacks app-op %s, error %d",
72 uid, pid, package, operation, result));
Alex Buynytskyy96e350b2020-04-02 20:03:47 -070073 }
74}
75
76} // namespace android::incremental