The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 17 | #include <cutils/ashmem.h> |
| 18 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | /* |
| 20 | * Implementation of the user-space ashmem API for devices, which have our |
| 21 | * ashmem-enabled kernel. See ashmem-sim.c for the "fake" tmp-based version, |
| 22 | * used by the simulator. |
| 23 | */ |
Mark Salyzyn | e37111d | 2016-02-02 09:19:39 -0800 | [diff] [blame] | 24 | #define LOG_TAG "ashmem" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 26 | #include <errno.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | #include <fcntl.h> |
Mark Salyzyn | 23ed4c2 | 2016-09-28 13:33:27 -0700 | [diff] [blame] | 28 | #include <linux/ashmem.h> |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 29 | #include <linux/memfd.h> |
| 30 | #include <log/log.h> |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 31 | #include <pthread.h> |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 32 | #include <stdio.h> |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <sys/ioctl.h> |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 35 | #include <sys/mman.h> |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 36 | #include <sys/stat.h> |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 37 | #include <sys/syscall.h> |
Elliott Hughes | d77b537 | 2017-05-17 11:36:51 -0700 | [diff] [blame] | 38 | #include <sys/sysmacros.h> |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 39 | #include <sys/types.h> |
| 40 | #include <unistd.h> |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 41 | |
Tri Vo | 92fd3ca | 2019-09-24 14:06:38 -0700 | [diff] [blame] | 42 | #include <android-base/file.h> |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 43 | #include <android-base/properties.h> |
Tri Vo | 92fd3ca | 2019-09-24 14:06:38 -0700 | [diff] [blame] | 44 | #include <android-base/strings.h> |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 45 | #include <android-base/unique_fd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | |
Isaac J. Manjarres | 78541b7 | 2025-03-03 16:06:58 -0800 | [diff] [blame] | 47 | #include "ashmem-internal.h" |
| 48 | |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 49 | /* ashmem identity */ |
| 50 | static dev_t __ashmem_rdev; |
| 51 | /* |
| 52 | * If we trigger a signal handler in the middle of locked activity and the |
| 53 | * signal handler calls ashmem, we could get into a deadlock state. |
| 54 | */ |
| 55 | static pthread_mutex_t __ashmem_lock = PTHREAD_MUTEX_INITIALIZER; |
| 56 | |
Tri Vo | 2891ba0 | 2019-01-28 17:56:43 -0800 | [diff] [blame] | 57 | /* |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 58 | * has_memfd_support() determines if the device can use memfd. memfd support |
| 59 | * has been there for long time, but certain things in it may be missing. We |
| 60 | * check for needed support in it. Also we check if the VNDK version of |
| 61 | * libcutils being used is new enough, if its not, then we cannot use memfd |
| 62 | * since the older copies may be using ashmem so we just use ashmem. Once all |
| 63 | * Android devices that are getting updates are new enough (ex, they were |
| 64 | * originally shipped with Android release > P), then we can just use memfd and |
| 65 | * delete all ashmem code from libcutils (while preserving the interface). |
| 66 | * |
| 67 | * NOTE: |
| 68 | * The sys.use_memfd property is set by default to false in Android |
| 69 | * to temporarily disable memfd, till vendor and apps are ready for it. |
| 70 | * The main issue: either apps or vendor processes can directly make ashmem |
| 71 | * IOCTLs on FDs they receive by assuming they are ashmem, without going |
| 72 | * through libcutils. Such fds could have very well be originally created with |
| 73 | * libcutils hence they could be memfd. Thus the IOCTLs will break. |
| 74 | * |
| 75 | * Set default value of sys.use_memfd property to true once the issue is |
| 76 | * resolved, so that the code can then self-detect if kernel support is present |
| 77 | * on the device. The property can also set to true from adb shell, for |
| 78 | * debugging. |
| 79 | */ |
| 80 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 81 | /* set to true for verbose logging and other debug */ |
| 82 | static bool debug_log = false; |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 83 | |
| 84 | /* Determine if vendor processes would be ok with memfd in the system: |
| 85 | * |
Kiyoung Kim | 45d88d4 | 2023-09-21 16:03:41 +0900 | [diff] [blame] | 86 | * Previously this function checked if memfd is supported by checking if |
| 87 | * vendor VNDK version is greater than Q. As we can assume all treblelized |
| 88 | * device using this code is up to date enough to use memfd, memfd is allowed |
| 89 | * if the device is treblelized. |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 90 | */ |
| 91 | static bool check_vendor_memfd_allowed() { |
Kiyoung Kim | 45d88d4 | 2023-09-21 16:03:41 +0900 | [diff] [blame] | 92 | static bool is_treblelized = android::base::GetBoolProperty("ro.treble.enabled", false); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 93 | |
Kiyoung Kim | 45d88d4 | 2023-09-21 16:03:41 +0900 | [diff] [blame] | 94 | return is_treblelized; |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 97 | /* Determine if memfd can be supported. This is just one-time hardwork |
| 98 | * which will be cached by the caller. |
| 99 | */ |
| 100 | static bool __has_memfd_support() { |
| 101 | if (check_vendor_memfd_allowed() == false) { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | /* Used to turn on/off the detection at runtime, in the future this |
| 106 | * property will be removed once we switch everything over to ashmem. |
| 107 | * Currently it is used only for debugging to switch the system over. |
| 108 | */ |
| 109 | if (!android::base::GetBoolProperty("sys.use_memfd", false)) { |
| 110 | if (debug_log) { |
Elliott Hughes | da4ac7c | 2025-01-08 15:17:26 -0800 | [diff] [blame] | 111 | ALOGD("sys.use_memfd=false so memfd disabled"); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
Elliott Hughes | 790ef05 | 2020-09-03 10:53:16 -0700 | [diff] [blame] | 116 | // Check if kernel support exists, otherwise fall back to ashmem. |
| 117 | // This code needs to build on old API levels, so we can't use the libc |
| 118 | // wrapper. |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 119 | android::base::unique_fd fd( |
Isaac Manjarres | 7b1de50 | 2025-03-06 05:56:40 -0800 | [diff] [blame] | 120 | syscall(__NR_memfd_create, "test_android_memfd", MFD_CLOEXEC | MFD_ALLOW_SEALING)); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 121 | if (fd == -1) { |
Elliott Hughes | da4ac7c | 2025-01-08 15:17:26 -0800 | [diff] [blame] | 122 | ALOGE("memfd_create failed: %m, no memfd support"); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | |
| 126 | if (fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE) == -1) { |
Elliott Hughes | da4ac7c | 2025-01-08 15:17:26 -0800 | [diff] [blame] | 127 | ALOGE("fcntl(F_ADD_SEALS) failed: %m, no memfd support"); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 131 | size_t buf_size = getpagesize(); |
Isaac J. Manjarres | 01f60e4 | 2025-02-05 01:08:20 -0800 | [diff] [blame] | 132 | if (ftruncate(fd, buf_size) == -1) { |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 133 | ALOGE("ftruncate(%zd) failed to set memfd buffer size: %m, no memfd support", buf_size); |
Isaac J. Manjarres | 01f60e4 | 2025-02-05 01:08:20 -0800 | [diff] [blame] | 134 | return false; |
| 135 | } |
| 136 | |
Isaac J. Manjarres | f518d46 | 2024-12-26 11:34:21 -0800 | [diff] [blame] | 137 | /* |
| 138 | * Ensure that the kernel supports ashmem ioctl commands on memfds. If not, |
| 139 | * fall back to using ashmem. |
| 140 | */ |
Isaac J. Manjarres | 01f60e4 | 2025-02-05 01:08:20 -0800 | [diff] [blame] | 141 | int ashmem_size = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, 0)); |
| 142 | if (ashmem_size != static_cast<int>(buf_size)) { |
| 143 | ALOGE("ioctl(ASHMEM_GET_SIZE): %d != buf_size: %zd , no ashmem-memfd compat support", |
| 144 | ashmem_size, buf_size); |
Isaac J. Manjarres | f518d46 | 2024-12-26 11:34:21 -0800 | [diff] [blame] | 145 | return false; |
| 146 | } |
| 147 | |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 148 | if (debug_log) { |
Elliott Hughes | da4ac7c | 2025-01-08 15:17:26 -0800 | [diff] [blame] | 149 | ALOGD("memfd: device has memfd support, using it"); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 150 | } |
| 151 | return true; |
| 152 | } |
| 153 | |
Isaac J. Manjarres | 78541b7 | 2025-03-03 16:06:58 -0800 | [diff] [blame] | 154 | bool has_memfd_support() { |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 155 | static bool memfd_supported = __has_memfd_support(); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 156 | return memfd_supported; |
| 157 | } |
| 158 | |
Tri Vo | 92fd3ca | 2019-09-24 14:06:38 -0700 | [diff] [blame] | 159 | static std::string get_ashmem_device_path() { |
| 160 | static const std::string boot_id_path = "/proc/sys/kernel/random/boot_id"; |
| 161 | std::string boot_id; |
| 162 | if (!android::base::ReadFileToString(boot_id_path, &boot_id)) { |
Elliott Hughes | da4ac7c | 2025-01-08 15:17:26 -0800 | [diff] [blame] | 163 | ALOGE("Failed to read %s: %m", boot_id_path.c_str()); |
Tri Vo | 92fd3ca | 2019-09-24 14:06:38 -0700 | [diff] [blame] | 164 | return ""; |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 165 | } |
Tri Vo | 92fd3ca | 2019-09-24 14:06:38 -0700 | [diff] [blame] | 166 | boot_id = android::base::Trim(boot_id); |
| 167 | |
| 168 | return "/dev/ashmem" + boot_id; |
| 169 | } |
| 170 | |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 171 | /* logistics of getting file descriptor for ashmem */ |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 172 | static int __ashmem_open_locked() { |
Tri Vo | 92fd3ca | 2019-09-24 14:06:38 -0700 | [diff] [blame] | 173 | static const std::string ashmem_device_path = get_ashmem_device_path(); |
| 174 | |
Tri Vo | 92fd3ca | 2019-09-24 14:06:38 -0700 | [diff] [blame] | 175 | if (ashmem_device_path.empty()) { |
| 176 | return -1; |
Tim Murray | 8879ed7 | 2019-04-04 09:16:32 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 179 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(ashmem_device_path.c_str(), O_RDWR | O_CLOEXEC))); |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 180 | if (!fd.ok()) { |
Isaac J. Manjarres | 6d4e75a | 2025-03-03 14:07:06 -0800 | [diff] [blame] | 181 | ALOGE("Unable to open ashmem device: %m"); |
| 182 | return -1; |
Steven Moreland | 4f99dd3 | 2020-01-09 14:42:32 -0800 | [diff] [blame] | 183 | } |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 184 | |
Steven Moreland | 4f99dd3 | 2020-01-09 14:42:32 -0800 | [diff] [blame] | 185 | struct stat st; |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 186 | if (TEMP_FAILURE_RETRY(fstat(fd, &st)) == -1) { |
| 187 | return -1; |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 188 | } |
| 189 | if (!S_ISCHR(st.st_mode) || !st.st_rdev) { |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 190 | errno = ENOTTY; |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | __ashmem_rdev = st.st_rdev; |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 195 | return fd.release(); |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 198 | static int __ashmem_open() { |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 199 | pthread_mutex_lock(&__ashmem_lock); |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 200 | int fd = __ashmem_open_locked(); |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 201 | pthread_mutex_unlock(&__ashmem_lock); |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 202 | return fd; |
| 203 | } |
| 204 | |
| 205 | /* Make sure file descriptor references ashmem, negative number means false */ |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 206 | static int __ashmem_is_ashmem(int fd, bool fatal) { |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 207 | struct stat st; |
Joel Fernandes | 56cd651 | 2018-07-17 13:00:17 -0700 | [diff] [blame] | 208 | if (fstat(fd, &st) < 0) { |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 209 | return -1; |
| 210 | } |
| 211 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 212 | dev_t rdev = 0; /* Too much complexity to sniff __ashmem_rdev */ |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 213 | if (S_ISCHR(st.st_mode) && st.st_rdev) { |
| 214 | pthread_mutex_lock(&__ashmem_lock); |
| 215 | rdev = __ashmem_rdev; |
| 216 | if (rdev) { |
| 217 | pthread_mutex_unlock(&__ashmem_lock); |
| 218 | } else { |
| 219 | int fd = __ashmem_open_locked(); |
| 220 | if (fd < 0) { |
| 221 | pthread_mutex_unlock(&__ashmem_lock); |
| 222 | return -1; |
| 223 | } |
| 224 | rdev = __ashmem_rdev; |
| 225 | pthread_mutex_unlock(&__ashmem_lock); |
| 226 | |
| 227 | close(fd); |
| 228 | } |
| 229 | |
| 230 | if (st.st_rdev == rdev) { |
| 231 | return 0; |
| 232 | } |
| 233 | } |
| 234 | |
Mark Salyzyn | ee43111 | 2016-08-23 13:58:37 -0700 | [diff] [blame] | 235 | if (fatal) { |
| 236 | if (rdev) { |
| 237 | LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o %d:%d", |
| 238 | fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev), |
| 239 | S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP, |
| 240 | major(rdev), minor(rdev)); |
| 241 | } else { |
| 242 | LOG_ALWAYS_FATAL("illegal fd=%d mode=0%o rdev=%d:%d expected 0%o", |
| 243 | fd, st.st_mode, major(st.st_rdev), minor(st.st_rdev), |
| 244 | S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IRGRP); |
| 245 | } |
| 246 | /* NOTREACHED */ |
Mark Salyzyn | e37111d | 2016-02-02 09:19:39 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Mark Salyzyn | 1186f3a | 2016-02-02 08:21:32 -0800 | [diff] [blame] | 249 | errno = ENOTTY; |
| 250 | return -1; |
| 251 | } |
| 252 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 253 | static int __ashmem_check_failure(int fd, int result) { |
| 254 | if (result == -1 && errno == ENOTTY) __ashmem_is_ashmem(fd, true); |
Joel Fernandes | 56cd651 | 2018-07-17 13:00:17 -0700 | [diff] [blame] | 255 | return result; |
| 256 | } |
| 257 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 258 | static bool is_ashmem_fd(int fd) { |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 259 | static bool fd_check_error_once = false; |
| 260 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 261 | if (__ashmem_is_ashmem(fd, false) == 0) { |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 262 | if (!fd_check_error_once) { |
Elliott Hughes | da4ac7c | 2025-01-08 15:17:26 -0800 | [diff] [blame] | 263 | ALOGE("memfd: memfd expected but ashmem fd used - please use libcutils"); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 264 | fd_check_error_once = true; |
| 265 | } |
| 266 | |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | return false; |
| 271 | } |
| 272 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 273 | static bool is_memfd_fd(int fd) { |
| 274 | return has_memfd_support() && !is_ashmem_fd(fd); |
| 275 | } |
| 276 | |
| 277 | int ashmem_valid(int fd) { |
| 278 | if (is_memfd_fd(fd)) { |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 279 | return 1; |
| 280 | } |
| 281 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 282 | return __ashmem_is_ashmem(fd, false) >= 0; |
Mark Salyzyn | ee43111 | 2016-08-23 13:58:37 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 285 | static int memfd_create_region(const char* name, size_t size) { |
Elliott Hughes | 790ef05 | 2020-09-03 10:53:16 -0700 | [diff] [blame] | 286 | // This code needs to build on old API levels, so we can't use the libc |
| 287 | // wrapper. |
Isaac Manjarres | 7b1de50 | 2025-03-06 05:56:40 -0800 | [diff] [blame] | 288 | android::base::unique_fd fd(syscall(__NR_memfd_create, name, MFD_CLOEXEC | MFD_ALLOW_SEALING)); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 289 | |
| 290 | if (fd == -1) { |
Elliott Hughes | da4ac7c | 2025-01-08 15:17:26 -0800 | [diff] [blame] | 291 | ALOGE("memfd_create(%s, %zd) failed: %m", name, size); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 292 | return -1; |
| 293 | } |
| 294 | |
| 295 | if (ftruncate(fd, size) == -1) { |
Elliott Hughes | da4ac7c | 2025-01-08 15:17:26 -0800 | [diff] [blame] | 296 | ALOGE("ftruncate(%s, %zd) failed for memfd creation: %m", name, size); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 297 | return -1; |
| 298 | } |
| 299 | |
Keith Mok | f83c5c8 | 2023-08-31 00:31:35 +0000 | [diff] [blame] | 300 | // forbid size changes to match ashmem behaviour |
| 301 | if (fcntl(fd, F_ADD_SEALS, F_SEAL_GROW | F_SEAL_SHRINK) == -1) { |
| 302 | ALOGE("memfd_create(%s, %zd) F_ADD_SEALS failed: %m", name, size); |
| 303 | return -1; |
| 304 | } |
| 305 | |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 306 | if (debug_log) { |
Elliott Hughes | da4ac7c | 2025-01-08 15:17:26 -0800 | [diff] [blame] | 307 | ALOGE("memfd_create(%s, %zd) success. fd=%d", name, size, fd.get()); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 308 | } |
| 309 | return fd.release(); |
| 310 | } |
| 311 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 312 | /* |
| 313 | * ashmem_create_region - creates a new ashmem region and returns the file |
| 314 | * descriptor, or <0 on error |
| 315 | * |
| 316 | * `name' is an optional label to give the region (visible in /proc/pid/maps) |
| 317 | * `size' is the size of the region, in page-aligned bytes |
| 318 | */ |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 319 | int ashmem_create_region(const char* name, size_t size) { |
| 320 | if (name == NULL) name = "none"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 321 | |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 322 | if (has_memfd_support()) { |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 323 | return memfd_create_region(name, size); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 326 | android::base::unique_fd fd(__ashmem_open()); |
| 327 | if (!fd.ok() || |
| 328 | TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_NAME, name) < 0) || |
| 329 | TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_SIZE, size) < 0)) { |
| 330 | return -1; |
Mark Salyzyn | c2d8aad | 2016-02-02 08:05:54 -0800 | [diff] [blame] | 331 | } |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 332 | return fd.release(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 335 | static int memfd_set_prot_region(int fd, int prot) { |
Keith Mok | f83c5c8 | 2023-08-31 00:31:35 +0000 | [diff] [blame] | 336 | int seals = fcntl(fd, F_GET_SEALS); |
| 337 | if (seals == -1) { |
Elliott Hughes | da4ac7c | 2025-01-08 15:17:26 -0800 | [diff] [blame] | 338 | ALOGE("memfd_set_prot_region(%d, %d): F_GET_SEALS failed: %m", fd, prot); |
Keith Mok | f83c5c8 | 2023-08-31 00:31:35 +0000 | [diff] [blame] | 339 | return -1; |
| 340 | } |
| 341 | |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 342 | if (prot & PROT_WRITE) { |
Keith Mok | f83c5c8 | 2023-08-31 00:31:35 +0000 | [diff] [blame] | 343 | /* Now we want the buffer to be read-write, let's check if the buffer |
| 344 | * has been previously marked as read-only before, if so return error |
| 345 | */ |
| 346 | if (seals & F_SEAL_FUTURE_WRITE) { |
Elliott Hughes | da4ac7c | 2025-01-08 15:17:26 -0800 | [diff] [blame] | 347 | ALOGE("memfd_set_prot_region(%d, %d): region is write protected", fd, prot); |
Keith Mok | f83c5c8 | 2023-08-31 00:31:35 +0000 | [diff] [blame] | 348 | errno = EINVAL; // inline with ashmem error code, if already in |
| 349 | // read-only mode |
| 350 | return -1; |
| 351 | } |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | |
Keith Mok | f83c5c8 | 2023-08-31 00:31:35 +0000 | [diff] [blame] | 355 | /* We would only allow read-only for any future file operations */ |
Isaac J. Manjarres | 2a76095 | 2024-12-26 18:14:24 -0800 | [diff] [blame] | 356 | if (fcntl(fd, F_ADD_SEALS, F_SEAL_FUTURE_WRITE) == -1) { |
Elliott Hughes | da4ac7c | 2025-01-08 15:17:26 -0800 | [diff] [blame] | 357 | ALOGE("memfd_set_prot_region(%d, %d): F_SEAL_FUTURE_WRITE seal failed: %m", fd, prot); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 364 | int ashmem_set_prot_region(int fd, int prot) { |
| 365 | if (is_memfd_fd(fd)) { |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 366 | return memfd_set_prot_region(fd, prot); |
| 367 | } |
| 368 | |
Joel Fernandes | 56cd651 | 2018-07-17 13:00:17 -0700 | [diff] [blame] | 369 | return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_PROT_MASK, prot))); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 372 | static int do_pin(int op, int fd, size_t offset, size_t length) { |
| 373 | static bool already_warned_about_pin_deprecation = false; |
| 374 | if (!already_warned_about_pin_deprecation || debug_log) { |
Elliott Hughes | da4ac7c | 2025-01-08 15:17:26 -0800 | [diff] [blame] | 375 | ALOGE("Pinning is deprecated since Android Q. Please use trim or other methods."); |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 376 | already_warned_about_pin_deprecation = true; |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 379 | if (is_memfd_fd(fd)) { |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 380 | return 0; |
| 381 | } |
| 382 | |
Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 383 | // TODO: should LP64 reject too-large offset/len? |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 384 | ashmem_pin pin = { static_cast<uint32_t>(offset), static_cast<uint32_t>(length) }; |
| 385 | return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, op, &pin))); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 388 | int ashmem_pin_region(int fd, size_t offset, size_t length) { |
| 389 | return do_pin(ASHMEM_PIN, fd, offset, length); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 390 | } |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 391 | |
Elliott Hughes | c026325 | 2025-02-24 14:23:04 -0800 | [diff] [blame] | 392 | int ashmem_unpin_region(int fd, size_t offset, size_t length) { |
| 393 | return do_pin(ASHMEM_UNPIN, fd, offset, length); |
| 394 | } |
| 395 | |
| 396 | int ashmem_get_size_region(int fd) { |
| 397 | if (is_memfd_fd(fd)) { |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 398 | struct stat sb; |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 399 | if (fstat(fd, &sb) == -1) { |
Elliott Hughes | da4ac7c | 2025-01-08 15:17:26 -0800 | [diff] [blame] | 400 | ALOGE("ashmem_get_size_region(%d): fstat failed: %m", fd); |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 401 | return -1; |
| 402 | } |
Joel Fernandes | 5194404 | 2018-12-18 13:32:31 -0800 | [diff] [blame] | 403 | return sb.st_size; |
| 404 | } |
| 405 | |
Joel Fernandes | 56cd651 | 2018-07-17 13:00:17 -0700 | [diff] [blame] | 406 | return __ashmem_check_failure(fd, TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_GET_SIZE, NULL))); |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 407 | } |