blob: a9041a0a966b8775bd6fe2a1f7562d421b1c2ad8 [file] [log] [blame]
Tri Vodf205792019-01-30 09:24:19 -08001/*
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 Vo8411a3b2019-02-08 09:45:56 -080017#include <android-base/logging.h>
Tri Vodf205792019-01-30 09:24:19 -080018#include <android/ashmemd/IAshmemDeviceService.h>
19#include <binder/IServiceManager.h>
Tri Vo540d5792019-04-08 10:41:10 -070020#include <cutils/android_filesystem_config.h>
Tri Vodf205792019-01-30 09:24:19 -080021
22using android::IBinder;
23using android::IServiceManager;
24using android::String16;
25using android::ashmemd::IAshmemDeviceService;
26using android::os::ParcelFileDescriptor;
27
28namespace android {
29namespace ashmemd {
30
Tri Vo540d5792019-04-08 10:41:10 -070031static 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 Vodf205792019-01-30 09:24:19 -080044sp<IAshmemDeviceService> getAshmemService() {
Tri Vo540d5792019-04-08 10:41:10 -070045 // 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 Vodf205792019-01-30 09:24:19 -080050 sp<IServiceManager> sm = android::defaultServiceManager();
Tri Vo8411a3b2019-02-08 09:45:56 -080051 sp<IBinder> binder = sm->checkService(String16("ashmem_device_service"));
Tri Vodf205792019-01-30 09:24:19 -080052 return interface_cast<IAshmemDeviceService>(binder);
53}
54
55extern "C" int openAshmemdFd() {
56 static sp<IAshmemDeviceService> ashmemService = getAshmemService();
Tri Vo8411a3b2019-02-08 09:45:56 -080057 if (!ashmemService) {
58 LOG(ERROR) << "Failed to get IAshmemDeviceService.";
59 return -1;
60 }
Tri Vodf205792019-01-30 09:24:19 -080061
62 ParcelFileDescriptor fd;
63 auto status = ashmemService->open(&fd);
Tri Vo8411a3b2019-02-08 09:45:56 -080064 if (!status.isOk()) {
65 LOG(ERROR) << "Failed IAshmemDeviceService::open()";
66 return -1;
67 }
Tri Vodf205792019-01-30 09:24:19 -080068
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