Tri Vo | df20579 | 2019-01-30 09:24:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Tri Vo | 8411a3b | 2019-02-08 09:45:56 -0800 | [diff] [blame] | 17 | #include <android-base/logging.h> |
Tri Vo | df20579 | 2019-01-30 09:24:19 -0800 | [diff] [blame] | 18 | #include <android/ashmemd/IAshmemDeviceService.h> |
| 19 | #include <binder/IServiceManager.h> |
Tri Vo | 540d579 | 2019-04-08 10:41:10 -0700 | [diff] [blame] | 20 | #include <cutils/android_filesystem_config.h> |
Tri Vo | df20579 | 2019-01-30 09:24:19 -0800 | [diff] [blame] | 21 | |
| 22 | using android::IBinder; |
| 23 | using android::IServiceManager; |
| 24 | using android::String16; |
| 25 | using android::ashmemd::IAshmemDeviceService; |
| 26 | using android::os::ParcelFileDescriptor; |
| 27 | |
| 28 | namespace android { |
| 29 | namespace ashmemd { |
| 30 | |
Tri Vo | 540d579 | 2019-04-08 10:41:10 -0700 | [diff] [blame] | 31 | static bool checkBinderAccess() { |
| 32 | // Isolated apps are potentially subject to seccomp policy that restricts use of access() |
| 33 | // (b/129483782). However, apps always have access to binder, so return true. |
| 34 | auto uid = getuid() % AID_USER; |
| 35 | if (AID_ISOLATED_START <= uid && uid <= AID_ISOLATED_END) { |
| 36 | return true; |
| 37 | } |
| 38 | if (access("/dev/binder", R_OK | W_OK) == 0) { |
| 39 | return true; |
| 40 | } |
| 41 | return false; |
| 42 | } |
| 43 | |
Tri Vo | df20579 | 2019-01-30 09:24:19 -0800 | [diff] [blame] | 44 | sp<IAshmemDeviceService> getAshmemService() { |
Tri Vo | 540d579 | 2019-04-08 10:41:10 -0700 | [diff] [blame] | 45 | // Calls to defaultServiceManager() crash the process if it doesn't have appropriate |
| 46 | // binder permissions. Check these permissions proactively. |
| 47 | if (!checkBinderAccess()) { |
| 48 | return nullptr; |
| 49 | } |
Tri Vo | df20579 | 2019-01-30 09:24:19 -0800 | [diff] [blame] | 50 | sp<IServiceManager> sm = android::defaultServiceManager(); |
Tri Vo | 8411a3b | 2019-02-08 09:45:56 -0800 | [diff] [blame] | 51 | sp<IBinder> binder = sm->checkService(String16("ashmem_device_service")); |
Tri Vo | df20579 | 2019-01-30 09:24:19 -0800 | [diff] [blame] | 52 | return interface_cast<IAshmemDeviceService>(binder); |
| 53 | } |
| 54 | |
| 55 | extern "C" int openAshmemdFd() { |
| 56 | static sp<IAshmemDeviceService> ashmemService = getAshmemService(); |
Tri Vo | 8411a3b | 2019-02-08 09:45:56 -0800 | [diff] [blame] | 57 | if (!ashmemService) { |
| 58 | LOG(ERROR) << "Failed to get IAshmemDeviceService."; |
| 59 | return -1; |
| 60 | } |
Tri Vo | df20579 | 2019-01-30 09:24:19 -0800 | [diff] [blame] | 61 | |
| 62 | ParcelFileDescriptor fd; |
| 63 | auto status = ashmemService->open(&fd); |
Tri Vo | 8411a3b | 2019-02-08 09:45:56 -0800 | [diff] [blame] | 64 | if (!status.isOk()) { |
| 65 | LOG(ERROR) << "Failed IAshmemDeviceService::open()"; |
| 66 | return -1; |
| 67 | } |
Tri Vo | df20579 | 2019-01-30 09:24:19 -0800 | [diff] [blame] | 68 | |
| 69 | // unique_fd is the underlying type of ParcelFileDescriptor, i.e. fd is |
| 70 | // closed when it falls out of scope, so we make a dup. |
| 71 | return dup(fd.get()); |
| 72 | } |
| 73 | |
| 74 | } // namespace ashmemd |
| 75 | } // namespace android |