Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | namespace android::incremental { |
| 26 | |
| 27 | inline binder::Status Ok() { |
| 28 | return binder::Status::ok(); |
| 29 | } |
| 30 | |
| 31 | inline binder::Status Exception(uint32_t code, const std::string& msg) { |
| 32 | return binder::Status::fromExceptionCode(code, String8(msg.c_str())); |
| 33 | } |
| 34 | |
| 35 | inline 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 Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 44 | inline binder::Status CheckPermissionForDataDelivery(const char* permission, const char* operation, |
| 45 | const char* package) { |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 46 | 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 Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 56 | String16 packageName{package}; |
| 57 | |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 58 | // Caller must also have op granted. |
| 59 | PermissionController pc; |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 60 | if (auto packageUid = pc.getPackageUid(packageName, 0); packageUid != uid) { |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 61 | return Exception(binder::Status::EX_SECURITY, |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 62 | StringPrintf("UID %d / PID %d does not own package %s", uid, pid, |
| 63 | package)); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 64 | } |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 65 | switch (auto result = pc.noteOp(String16(operation), uid, packageName); result) { |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 66 | case PermissionController::MODE_ALLOWED: |
| 67 | case PermissionController::MODE_DEFAULT: |
| 68 | return binder::Status::ok(); |
| 69 | default: |
| 70 | return Exception(binder::Status::EX_SECURITY, |
Alex Buynytskyy | 1d89216 | 2020-04-03 23:00:19 -0700 | [diff] [blame^] | 71 | StringPrintf("UID %d / PID %d / package %s lacks app-op %s, error %d", |
| 72 | uid, pid, package, operation, result)); |
Alex Buynytskyy | 96e350b | 2020-04-02 20:03:47 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | } // namespace android::incremental |