George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | #ifndef _UNISTD_H_ |
| 29 | #error "Never include this file directly; instead, include <unistd.h>" |
| 30 | #endif |
| 31 | |
| 32 | char* __getcwd_chk(char*, size_t, size_t) __INTRODUCED_IN(24); |
| 33 | |
| 34 | ssize_t __pread_chk(int, void*, size_t, off_t, size_t) __INTRODUCED_IN(23); |
| 35 | ssize_t __pread_real(int, void*, size_t, off_t) __RENAME(pread); |
| 36 | |
| 37 | ssize_t __pread64_chk(int, void*, size_t, off64_t, size_t) __INTRODUCED_IN(23); |
Elliott Hughes | 0e14c5a | 2019-10-03 20:35:38 -0700 | [diff] [blame] | 38 | ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 39 | |
| 40 | ssize_t __pwrite_chk(int, const void*, size_t, off_t, size_t) __INTRODUCED_IN(24); |
| 41 | ssize_t __pwrite_real(int, const void*, size_t, off_t) __RENAME(pwrite); |
| 42 | |
| 43 | ssize_t __pwrite64_chk(int, const void*, size_t, off64_t, size_t) __INTRODUCED_IN(24); |
Elliott Hughes | 0e14c5a | 2019-10-03 20:35:38 -0700 | [diff] [blame] | 44 | ssize_t __pwrite64_real(int, const void*, size_t, off64_t) __RENAME(pwrite64); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 45 | |
| 46 | ssize_t __read_chk(int, void*, size_t, size_t) __INTRODUCED_IN(21); |
| 47 | ssize_t __write_chk(int, const void*, size_t, size_t) __INTRODUCED_IN(24); |
| 48 | ssize_t __readlink_chk(const char*, char*, size_t, size_t) __INTRODUCED_IN(23); |
| 49 | ssize_t __readlinkat_chk(int dirfd, const char*, char*, size_t, size_t) __INTRODUCED_IN(23); |
| 50 | |
| 51 | #if defined(__BIONIC_FORTIFY) |
| 52 | |
| 53 | #if defined(__USE_FILE_OFFSET64) |
| 54 | #define __PREAD_PREFIX(x) __pread64_ ## x |
| 55 | #define __PWRITE_PREFIX(x) __pwrite64_ ## x |
| 56 | #else |
| 57 | #define __PREAD_PREFIX(x) __pread_ ## x |
| 58 | #define __PWRITE_PREFIX(x) __pwrite_ ## x |
| 59 | #endif |
| 60 | |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 61 | #define __error_if_overflows_ssizet(what, fn) \ |
| 62 | __clang_error_if((what) > SSIZE_MAX, "in call to '" #fn "', '" #what "' must be <= SSIZE_MAX") |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 63 | |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 64 | #define __error_if_overflows_objectsize(what, objsize, fn) \ |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame] | 65 | __clang_error_if(__bos_unevaluated_lt((objsize), (what)), \ |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 66 | "in call to '" #fn "', '" #what "' bytes overflows the given object") |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 67 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 68 | #define __bos_trivially_ge_no_overflow(bos_val, index) \ |
George Burgess IV | 74519e7 | 2019-06-06 17:54:00 -0700 | [diff] [blame] | 69 | ((__bos_dynamic_check_impl_and((bos_val), >=, (index), (bos_val) <= SSIZE_MAX) && \ |
| 70 | __builtin_constant_p(index) && (index) <= SSIZE_MAX)) |
George Burgess IV | d9865e7 | 2019-05-13 17:20:04 -0700 | [diff] [blame] | 71 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 72 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 73 | char* getcwd(char* const __pass_object_size buf, size_t size) |
| 74 | __overloadable |
| 75 | __error_if_overflows_objectsize(size, __bos(buf), getcwd) { |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 76 | #if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 77 | size_t bos = __bos(buf); |
| 78 | |
George Burgess IV | 3aedee9 | 2019-09-24 11:46:22 -0700 | [diff] [blame] | 79 | if (!__bos_trivially_ge(bos, size)) { |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 80 | return __getcwd_chk(buf, size, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 81 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 82 | #endif |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 83 | return __call_bypassing_fortify(getcwd)(buf, size); |
| 84 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 85 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 86 | #if !defined(__USE_FILE_OFFSET64) |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 87 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 88 | ssize_t pread(int fd, void* const __pass_object_size0 buf, size_t count, off_t offset) |
| 89 | __overloadable |
| 90 | __error_if_overflows_ssizet(count, pread) |
| 91 | __error_if_overflows_objectsize(count, __bos0(buf), pread) { |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 92 | #if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 93 | size_t bos = __bos0(buf); |
| 94 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 95 | if (!__bos_trivially_ge_no_overflow(bos, count)) { |
| 96 | return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 97 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 98 | #endif |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 99 | return __PREAD_PREFIX(real)(fd, buf, count, offset); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 100 | } |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 101 | #endif /* !defined(__USE_FILE_OFFSET64) */ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 102 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 103 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 104 | ssize_t pread64(int fd, void* const __pass_object_size0 buf, size_t count, off64_t offset) |
| 105 | __overloadable |
| 106 | __error_if_overflows_ssizet(count, pread64) |
| 107 | __error_if_overflows_objectsize(count, __bos0(buf), pread64) { |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 108 | #if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 109 | size_t bos = __bos0(buf); |
| 110 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 111 | if (!__bos_trivially_ge_no_overflow(bos, count)) { |
| 112 | return __pread64_chk(fd, buf, count, offset, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 113 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 114 | #endif |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 115 | return __pread64_real(fd, buf, count, offset); |
| 116 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 117 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 118 | #if !defined(__USE_FILE_OFFSET64) |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 119 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 120 | ssize_t pwrite(int fd, const void* const __pass_object_size0 buf, size_t count, off_t offset) |
| 121 | __overloadable |
| 122 | __error_if_overflows_ssizet(count, pwrite) |
| 123 | __error_if_overflows_objectsize(count, __bos0(buf), pwrite) { |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 124 | #if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 125 | size_t bos = __bos0(buf); |
| 126 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 127 | if (!__bos_trivially_ge_no_overflow(bos, count)) { |
| 128 | return __PWRITE_PREFIX(chk)(fd, buf, count, offset, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 129 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 130 | #endif |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 131 | return __PWRITE_PREFIX(real)(fd, buf, count, offset); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 132 | } |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 133 | #endif /* !defined(__USE_FILE_OFFSET64) */ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 134 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 135 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 136 | ssize_t pwrite64(int fd, const void* const __pass_object_size0 buf, size_t count, off64_t offset) |
| 137 | __overloadable |
| 138 | __error_if_overflows_ssizet(count, pwrite64) |
| 139 | __error_if_overflows_objectsize(count, __bos0(buf), pwrite64) { |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 140 | #if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 141 | size_t bos = __bos0(buf); |
| 142 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 143 | if (!__bos_trivially_ge_no_overflow(bos, count)) { |
| 144 | return __pwrite64_chk(fd, buf, count, offset, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 145 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 146 | #endif |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 147 | return __pwrite64_real(fd, buf, count, offset); |
| 148 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 149 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 150 | __BIONIC_FORTIFY_INLINE |
| 151 | ssize_t read(int fd, void* const __pass_object_size0 buf, size_t count) |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 152 | __overloadable |
| 153 | __error_if_overflows_ssizet(count, read) |
| 154 | __error_if_overflows_objectsize(count, __bos0(buf), read) { |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 155 | #if __ANDROID_API__ >= 21 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 156 | size_t bos = __bos0(buf); |
| 157 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 158 | if (!__bos_trivially_ge_no_overflow(bos, count)) { |
| 159 | return __read_chk(fd, buf, count, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 160 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 161 | #endif |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 162 | return __call_bypassing_fortify(read)(fd, buf, count); |
| 163 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 164 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 165 | __BIONIC_FORTIFY_INLINE |
| 166 | ssize_t write(int fd, const void* const __pass_object_size0 buf, size_t count) |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 167 | __overloadable |
| 168 | __error_if_overflows_ssizet(count, write) |
| 169 | __error_if_overflows_objectsize(count, __bos0(buf), write) { |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 170 | #if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 171 | size_t bos = __bos0(buf); |
| 172 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 173 | if (!__bos_trivially_ge_no_overflow(bos, count)) { |
| 174 | return __write_chk(fd, buf, count, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 175 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 176 | #endif |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 177 | return __call_bypassing_fortify(write)(fd, buf, count); |
| 178 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 179 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 180 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 181 | ssize_t readlink(const char* path, char* const __pass_object_size buf, size_t size) |
| 182 | __overloadable |
| 183 | __error_if_overflows_ssizet(size, readlink) |
| 184 | __error_if_overflows_objectsize(size, __bos(buf), readlink) { |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 185 | #if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 186 | size_t bos = __bos(buf); |
| 187 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 188 | if (!__bos_trivially_ge_no_overflow(bos, size)) { |
| 189 | return __readlink_chk(path, buf, size, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 190 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 191 | #endif |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 192 | return __call_bypassing_fortify(readlink)(path, buf, size); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 195 | #if __ANDROID_API__ >= 21 |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 196 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 197 | ssize_t readlinkat(int dirfd, const char* path, char* const __pass_object_size buf, size_t size) |
| 198 | __overloadable |
| 199 | __error_if_overflows_ssizet(size, readlinkat) |
| 200 | __error_if_overflows_objectsize(size, __bos(buf), readlinkat) { |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 201 | #if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 202 | size_t bos = __bos(buf); |
| 203 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 204 | if (!__bos_trivially_ge_no_overflow(bos, size)) { |
| 205 | return __readlinkat_chk(dirfd, path, buf, size, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 206 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame] | 207 | #endif |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 208 | return __call_bypassing_fortify(readlinkat)(dirfd, path, buf, size); |
| 209 | } |
Elliott Hughes | 95c6cd7 | 2019-12-20 13:26:14 -0800 | [diff] [blame] | 210 | #endif /* __ANDROID_API__ >= 21 */ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 211 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame] | 212 | #undef __bos_trivially_ge_no_overflow |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 213 | #undef __enable_if_no_overflow_ssizet |
| 214 | #undef __error_if_overflows_objectsize |
| 215 | #undef __error_if_overflows_ssizet |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 216 | #undef __PREAD_PREFIX |
| 217 | #undef __PWRITE_PREFIX |
| 218 | #endif /* defined(__BIONIC_FORTIFY) */ |