diff --git a/third_party/ashmem/ashmem-dev.c b/third_party/ashmem/ashmem-dev.c index fcdfd1b77ef94..a8b8e3f59106a 100644 --- a/third_party/ashmem/ashmem-dev.c +++ b/third_party/ashmem/ashmem-dev.c @@ -93,7 +93,6 @@ typedef enum { static AshmemStatus s_ashmem_status = ASHMEM_STATUS_INIT; static dev_t s_ashmem_dev; -static bool s_use_memfd; /* Return the dev_t of a given file path, or 0 if not available, */ static dev_t ashmem_find_dev(const char* path) { @@ -129,41 +128,6 @@ static int ashmem_dev_fd_check(int fd) { return 0; } -/* - * ashmem_create_region - creates a new ashmem region and returns the file - * descriptor, or <0 on error - * - * `name' is an optional label to give the region (visible in /proc/pid/maps) - * `size' is the size of the region, in page-aligned bytes - */ -static int ashmem_dev_create_region(const char *name, size_t size) { - int fd = open(ASHMEM_DEVICE, O_RDWR); - if (fd < 0) - return fd; - - int ret; - if (name) { - char buf[ASHMEM_NAME_LEN]; - strlcpy(buf, name, sizeof(buf)); - ret = ioctl(fd, ASHMEM_SET_NAME, buf); - if (ret < 0) - goto error; - } - ret = ioctl(fd, ASHMEM_SET_SIZE, size); - if (ret < 0) - goto error; - - return fd; - -error: - close(fd); - return ret; -} - -static int ashmem_dev_set_prot_region(int fd, int prot) { - return ioctl(fd, ASHMEM_SET_PROT_MASK, prot); -} - static int ashmem_dev_get_prot_region(int fd) { return ioctl(fd, ASHMEM_GET_PROT_MASK); } @@ -277,30 +241,26 @@ static int memfd_get_prot_region(int fd) { static void ashmem_init_funcs() { ASharedMemoryFuncs* funcs = &s_ashmem_funcs; - if (device_api_level() >= __ANDROID_API_O__) { + /* + * When a device conforms to the VSR for API level 202604 (Android 17), + * ASharedMemory will allocate memfds and attempt to relabel them by using + * fsetxattr() to workaround how SELinux handles memfds. + * + * fsetxattr() is not allowlisted in our seccomp filter, and allowlisting + * it may be unsafe. Since memfds from Chromium should be accessible with + * the existing sepolicy for appdomain_tmpfs files, just allocate memfds + * directly if the device conforms to the VSR for API level 202604. + */ + if (vendor_api_level() >= 202604) { + funcs->create = &memfd_create_region; + funcs->setProt = &memfd_set_prot_region; + } else { /* Leaked intentionally! */ void* lib = dlopen("libandroid.so", RTLD_NOW); - /* - * When a device conforms to the VSR for API level 202604 (Android 17), - * ASharedMemory will allocate memfds and attempt to relabel them by using - * fsetxattr() to workaround how SELinux handles memfds. - * - * fsetxattr() is not allowlisted in our seccomp filter, and allowlisting - * it may be unsafe. Since memfds from Chromium should be accessible with - * the existing sepolicy for appdomain_tmpfs files, just allocate memfds - * directly if the device conforms to the VSR for API level 202604. - */ - if (vendor_api_level() >= 202604) { - s_use_memfd = true; - return; - } funcs->create = (ASharedMemory_createFunc)dlsym(lib, "ASharedMemory_create"); funcs->setProt = (ASharedMemory_setProtFunc)dlsym(lib, "ASharedMemory_setProt"); - } else { - funcs->create = &ashmem_dev_create_region; - funcs->setProt = &ashmem_dev_set_prot_region; } } @@ -310,16 +270,10 @@ static const ASharedMemoryFuncs* ashmem_get_funcs() { } int ashmem_create_region(const char* name, size_t size) { - if (s_use_memfd) - return memfd_create_region(name, size); - return ashmem_get_funcs()->create(name, size); } int ashmem_set_prot_region(int fd, int prot) { - if (s_use_memfd) - return memfd_set_prot_region(fd, prot); - return ashmem_get_funcs()->setProt(fd, prot); }