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); |
| 38 | ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64) __INTRODUCED_IN(12); |
| 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); |
| 44 | ssize_t __pwrite64_real(int, const void*, size_t, off64_t) __RENAME(pwrite64) |
| 45 | __INTRODUCED_IN(12); |
| 46 | |
| 47 | ssize_t __read_chk(int, void*, size_t, size_t) __INTRODUCED_IN(21); |
| 48 | ssize_t __write_chk(int, const void*, size_t, size_t) __INTRODUCED_IN(24); |
| 49 | ssize_t __readlink_chk(const char*, char*, size_t, size_t) __INTRODUCED_IN(23); |
| 50 | ssize_t __readlinkat_chk(int dirfd, const char*, char*, size_t, size_t) __INTRODUCED_IN(23); |
| 51 | |
| 52 | #if defined(__BIONIC_FORTIFY) |
| 53 | |
| 54 | #if defined(__USE_FILE_OFFSET64) |
| 55 | #define __PREAD_PREFIX(x) __pread64_ ## x |
| 56 | #define __PWRITE_PREFIX(x) __pwrite64_ ## x |
| 57 | #else |
| 58 | #define __PREAD_PREFIX(x) __pread_ ## x |
| 59 | #define __PWRITE_PREFIX(x) __pwrite_ ## x |
| 60 | #endif |
| 61 | |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 62 | #define __error_if_overflows_ssizet(what, fn) \ |
| 63 | __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] | 64 | |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 65 | #define __error_if_overflows_objectsize(what, objsize, fn) \ |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame] | 66 | __clang_error_if(__bos_unevaluated_lt((objsize), (what)), \ |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 67 | "in call to '" #fn "', '" #what "' bytes overflows the given object") |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 68 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 69 | #define __bos_trivially_ge_no_overflow(bos_val, index) \ |
George Burgess IV | 74519e7 | 2019-06-06 17:54:00 -0700 | [diff] [blame] | 70 | ((__bos_dynamic_check_impl_and((bos_val), >=, (index), (bos_val) <= SSIZE_MAX) && \ |
| 71 | __builtin_constant_p(index) && (index) <= SSIZE_MAX)) |
George Burgess IV | d9865e7 | 2019-05-13 17:20:04 -0700 | [diff] [blame] | 72 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 73 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 74 | char* getcwd(char* const __pass_object_size buf, size_t size) |
| 75 | __overloadable |
| 76 | __error_if_overflows_objectsize(size, __bos(buf), getcwd) { |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 77 | #if __ANDROID_API__ >= __ANDROID_API_N__ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 78 | size_t bos = __bos(buf); |
| 79 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 80 | if (!__bos_trivially_not_lt(bos, size)) { |
| 81 | return __getcwd_chk(buf, size, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 82 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 83 | #endif /* __ANDROID_API__ >= __ANDROID_API_N__ */ |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 84 | return __call_bypassing_fortify(getcwd)(buf, size); |
| 85 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 86 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 87 | #if !defined(__USE_FILE_OFFSET64) |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 88 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 89 | ssize_t pread(int fd, void* const __pass_object_size0 buf, size_t count, off_t offset) |
| 90 | __overloadable |
| 91 | __error_if_overflows_ssizet(count, pread) |
| 92 | __error_if_overflows_objectsize(count, __bos0(buf), pread) { |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 93 | #if __ANDROID_API__ >= __ANDROID_API_M__ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 94 | size_t bos = __bos0(buf); |
| 95 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 96 | if (!__bos_trivially_ge_no_overflow(bos, count)) { |
| 97 | return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 98 | } |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 99 | #endif /* __ANDROID_API__ >= __ANDROID_API_M__ */ |
| 100 | return __PREAD_PREFIX(real)(fd, buf, count, offset); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 101 | } |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 102 | #endif /* !defined(__USE_FILE_OFFSET64) */ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 103 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 104 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 105 | ssize_t pread64(int fd, void* const __pass_object_size0 buf, size_t count, off64_t offset) |
| 106 | __overloadable |
| 107 | __error_if_overflows_ssizet(count, pread64) |
| 108 | __error_if_overflows_objectsize(count, __bos0(buf), pread64) { |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 109 | #if __ANDROID_API__ >= __ANDROID_API_M__ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 110 | size_t bos = __bos0(buf); |
| 111 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 112 | if (!__bos_trivially_ge_no_overflow(bos, count)) { |
| 113 | return __pread64_chk(fd, buf, count, offset, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 114 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 115 | #endif /* __ANDROID_API__ >= __ANDROID_API_M__ */ |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 116 | return __pread64_real(fd, buf, count, offset); |
| 117 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 118 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 119 | #if !defined(__USE_FILE_OFFSET64) |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 120 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 121 | ssize_t pwrite(int fd, const void* const __pass_object_size0 buf, size_t count, off_t offset) |
| 122 | __overloadable |
| 123 | __error_if_overflows_ssizet(count, pwrite) |
| 124 | __error_if_overflows_objectsize(count, __bos0(buf), pwrite) { |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 125 | #if __ANDROID_API__ >= __ANDROID_API_N__ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 126 | size_t bos = __bos0(buf); |
| 127 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 128 | if (!__bos_trivially_ge_no_overflow(bos, count)) { |
| 129 | return __PWRITE_PREFIX(chk)(fd, buf, count, offset, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 130 | } |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 131 | #endif /* __ANDROID_API__ >= __ANDROID_API_N__ */ |
| 132 | return __PWRITE_PREFIX(real)(fd, buf, count, offset); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 133 | } |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 134 | #endif /* !defined(__USE_FILE_OFFSET64) */ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 135 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 136 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 137 | ssize_t pwrite64(int fd, const void* const __pass_object_size0 buf, size_t count, off64_t offset) |
| 138 | __overloadable |
| 139 | __error_if_overflows_ssizet(count, pwrite64) |
| 140 | __error_if_overflows_objectsize(count, __bos0(buf), pwrite64) { |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 141 | #if __ANDROID_API__ >= __ANDROID_API_N__ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 142 | size_t bos = __bos0(buf); |
| 143 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 144 | if (!__bos_trivially_ge_no_overflow(bos, count)) { |
| 145 | return __pwrite64_chk(fd, buf, count, offset, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 146 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 147 | #endif /* __ANDROID_API__ >= __ANDROID_API_N__ */ |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 148 | return __pwrite64_real(fd, buf, count, offset); |
| 149 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 150 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 151 | __BIONIC_FORTIFY_INLINE |
| 152 | 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] | 153 | __overloadable |
| 154 | __error_if_overflows_ssizet(count, read) |
| 155 | __error_if_overflows_objectsize(count, __bos0(buf), read) { |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 156 | #if __ANDROID_API__ >= __ANDROID_API_L__ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 157 | size_t bos = __bos0(buf); |
| 158 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 159 | if (!__bos_trivially_ge_no_overflow(bos, count)) { |
| 160 | return __read_chk(fd, buf, count, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 161 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 162 | #endif /* __ANDROID_API__ >= __ANDROID_API_L__ */ |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 163 | return __call_bypassing_fortify(read)(fd, buf, count); |
| 164 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 165 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 166 | __BIONIC_FORTIFY_INLINE |
| 167 | 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] | 168 | __overloadable |
| 169 | __error_if_overflows_ssizet(count, write) |
| 170 | __error_if_overflows_objectsize(count, __bos0(buf), write) { |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 171 | #if __ANDROID_API__ >= __ANDROID_API_N__ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 172 | size_t bos = __bos0(buf); |
| 173 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 174 | if (!__bos_trivially_ge_no_overflow(bos, count)) { |
| 175 | return __write_chk(fd, buf, count, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 176 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 177 | #endif /* __ANDROID_API__ >= __ANDROID_API_N__ */ |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 178 | return __call_bypassing_fortify(write)(fd, buf, count); |
| 179 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 180 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 181 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 182 | ssize_t readlink(const char* path, char* const __pass_object_size buf, size_t size) |
| 183 | __overloadable |
| 184 | __error_if_overflows_ssizet(size, readlink) |
| 185 | __error_if_overflows_objectsize(size, __bos(buf), readlink) { |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 186 | #if __ANDROID_API__ >= __ANDROID_API_M__ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 187 | size_t bos = __bos(buf); |
| 188 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 189 | if (!__bos_trivially_ge_no_overflow(bos, size)) { |
| 190 | return __readlink_chk(path, buf, size, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 191 | } |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 192 | #endif /* __ANDROID_API__ >= __ANDROID_API_M__ */ |
| 193 | return __call_bypassing_fortify(readlink)(path, buf, size); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 194 | } |
| 195 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 196 | #if __ANDROID_API__ >= __ANDROID_API_L__ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 197 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 16c1739 | 2017-07-31 21:30:47 -0700 | [diff] [blame] | 198 | ssize_t readlinkat(int dirfd, const char* path, char* const __pass_object_size buf, size_t size) |
| 199 | __overloadable |
| 200 | __error_if_overflows_ssizet(size, readlinkat) |
| 201 | __error_if_overflows_objectsize(size, __bos(buf), readlinkat) { |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 202 | #if __ANDROID_API__ >= __ANDROID_API_M__ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 203 | size_t bos = __bos(buf); |
| 204 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 205 | if (!__bos_trivially_ge_no_overflow(bos, size)) { |
| 206 | return __readlinkat_chk(dirfd, path, buf, size, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 207 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 208 | #endif /* __ANDROID_API__ >= __ANDROID_API_M__ */ |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 209 | return __call_bypassing_fortify(readlinkat)(dirfd, path, buf, size); |
| 210 | } |
| 211 | #endif /* __ANDROID_API__ >= __ANDROID_API_L__ */ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 212 | |
George Burgess IV | da8d30f | 2019-09-18 17:09:43 -0700 | [diff] [blame^] | 213 | #undef __bos_trivially_ge_no_overflow |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 214 | #undef __enable_if_no_overflow_ssizet |
| 215 | #undef __error_if_overflows_objectsize |
| 216 | #undef __error_if_overflows_ssizet |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 217 | #undef __PREAD_PREFIX |
| 218 | #undef __PWRITE_PREFIX |
| 219 | #endif /* defined(__BIONIC_FORTIFY) */ |