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