From 2891ba09d5c3636ffc15afa9df5700f6764cbd06 Mon Sep 17 00:00:00 2001 From: Tri Vo Date: Mon, 28 Jan 2019 17:56:43 -0800 Subject: [PATCH] libcutils: route to ashmemd If libcutils is not a VNDK version and /dev/ashmem is not available, then ask ashmemd for opened fd to /dev/ashmem. We rely on SELinux policy to determine what's allowed to open /dev/ashmem directly. Bug: 113362644 Test: device boots (in selinux permissive mode) Change-Id: I93c4fa6044b0bfa5282097c6c32139a8d8f67deb --- libcutils/ashmem-dev.cpp | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/libcutils/ashmem-dev.cpp b/libcutils/ashmem-dev.cpp index 0cc4fc084..f65dc8b02 100644 --- a/libcutils/ashmem-dev.cpp +++ b/libcutils/ashmem-dev.cpp @@ -23,6 +23,9 @@ */ #define LOG_TAG "ashmem" +#ifndef __ANDROID_VNDK__ +#include +#endif #include #include #include @@ -45,13 +48,46 @@ static dev_t __ashmem_rdev; */ static pthread_mutex_t __ashmem_lock = PTHREAD_MUTEX_INITIALIZER; +/* + * We use ashmemd to enforce that apps don't open /dev/ashmem directly. Vendor + * code can't access system aidl services per Treble requirements. So we limit + * ashmemd access to the system variant of libcutils. + */ +#ifndef __ANDROID_VNDK__ +using openFdType = int (*)(); + +openFdType initOpenAshmemFd() { + openFdType openFd = nullptr; + void* handle = dlopen("libashmemd_client.so", RTLD_NOW); + if (!handle) { + ALOGE("Failed to dlopen() libashmemd_client.so: %s", dlerror()); + return openFd; + } + + openFd = reinterpret_cast(dlsym(handle, "openAshmemdFd")); + if (!openFd) { + ALOGE("Failed to dlsym() openAshmemdFd() function: %s", dlerror()); + } + return openFd; +} +#endif + /* logistics of getting file descriptor for ashmem */ static int __ashmem_open_locked() { int ret; struct stat st; - int fd = TEMP_FAILURE_RETRY(open(ASHMEM_DEVICE, O_RDWR | O_CLOEXEC)); + int fd = -1; +#ifndef __ANDROID_VNDK__ + static auto openFd = initOpenAshmemFd(); + if (openFd) { + fd = openFd(); + } +#endif + if (fd < 0) { + fd = TEMP_FAILURE_RETRY(open(ASHMEM_DEVICE, O_RDWR | O_CLOEXEC)); + } if (fd < 0) { return fd; }