blob: 7eda1a6d88cfed90bba2f765ea8dd90006776234 [file] [log] [blame]
George Burgess IVb97049c2017-07-24 15:05:05 -07001/*
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
zijunzhao30cf6d02023-06-01 19:23:18 +000032char* _Nullable __getcwd_chk(char* _Nullable, size_t, size_t) __INTRODUCED_IN(24);
George Burgess IVb97049c2017-07-24 15:05:05 -070033
zijunzhao30cf6d02023-06-01 19:23:18 +000034ssize_t __pread_chk(int, void* _Nonnull, size_t, off_t, size_t) __INTRODUCED_IN(23);
35ssize_t __pread_real(int, void* _Nonnull, size_t, off_t) __RENAME(pread);
George Burgess IVb97049c2017-07-24 15:05:05 -070036
zijunzhao30cf6d02023-06-01 19:23:18 +000037ssize_t __pread64_chk(int, void* _Nonnull, size_t, off64_t, size_t) __INTRODUCED_IN(23);
38ssize_t __pread64_real(int, void* _Nonnull, size_t, off64_t) __RENAME(pread64);
George Burgess IVb97049c2017-07-24 15:05:05 -070039
zijunzhao30cf6d02023-06-01 19:23:18 +000040ssize_t __pwrite_chk(int, const void* _Nonnull, size_t, off_t, size_t) __INTRODUCED_IN(24);
41ssize_t __pwrite_real(int, const void* _Nonnull, size_t, off_t) __RENAME(pwrite);
George Burgess IVb97049c2017-07-24 15:05:05 -070042
zijunzhao30cf6d02023-06-01 19:23:18 +000043ssize_t __pwrite64_chk(int, const void* _Nonnull, size_t, off64_t, size_t) __INTRODUCED_IN(24);
44ssize_t __pwrite64_real(int, const void* _Nonnull, size_t, off64_t) __RENAME(pwrite64);
George Burgess IVb97049c2017-07-24 15:05:05 -070045
Elliott Hughes655e4302023-06-16 12:39:33 -070046ssize_t __read_chk(int, void* __BIONIC_COMPLICATED_NULLNESS, size_t, size_t);
zijunzhao30cf6d02023-06-01 19:23:18 +000047ssize_t __write_chk(int, const void* __BIONIC_COMPLICATED_NULLNESS, size_t, size_t) __INTRODUCED_IN(24);
48ssize_t __readlink_chk(const char* _Nonnull, char* _Nonnull, size_t, size_t) __INTRODUCED_IN(23);
49ssize_t __readlinkat_chk(int dirfd, const char* _Nonnull, char* _Nonnull, size_t, size_t) __INTRODUCED_IN(23);
George Burgess IVb97049c2017-07-24 15:05:05 -070050
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 IV16c17392017-07-31 21:30:47 -070061#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 IVb97049c2017-07-24 15:05:05 -070063
George Burgess IV16c17392017-07-31 21:30:47 -070064#define __error_if_overflows_objectsize(what, objsize, fn) \
George Burgess IV5273dc52019-05-09 13:46:57 -070065 __clang_error_if(__bos_unevaluated_lt((objsize), (what)), \
George Burgess IV16c17392017-07-31 21:30:47 -070066 "in call to '" #fn "', '" #what "' bytes overflows the given object")
George Burgess IVb97049c2017-07-24 15:05:05 -070067
George Burgess IVda8d30f2019-09-18 17:09:43 -070068#define __bos_trivially_ge_no_overflow(bos_val, index) \
George Burgess IV74519e72019-06-06 17:54:00 -070069 ((__bos_dynamic_check_impl_and((bos_val), >=, (index), (bos_val) <= SSIZE_MAX) && \
70 __builtin_constant_p(index) && (index) <= SSIZE_MAX))
George Burgess IVd9865e72019-05-13 17:20:04 -070071
George Burgess IVb97049c2017-07-24 15:05:05 -070072__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +000073char* _Nullable getcwd(char* const _Nullable __pass_object_size buf, size_t size)
George Burgess IV16c17392017-07-31 21:30:47 -070074 __overloadable
75 __error_if_overflows_objectsize(size, __bos(buf), getcwd) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -080076#if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -070077 size_t bos = __bos(buf);
78
George Burgess IV3aedee92019-09-24 11:46:22 -070079 if (!__bos_trivially_ge(bos, size)) {
George Burgess IVda8d30f2019-09-18 17:09:43 -070080 return __getcwd_chk(buf, size, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -070081 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -070082#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -070083 return __call_bypassing_fortify(getcwd)(buf, size);
84}
George Burgess IVb97049c2017-07-24 15:05:05 -070085
George Burgess IVda8d30f2019-09-18 17:09:43 -070086#if !defined(__USE_FILE_OFFSET64)
George Burgess IVb97049c2017-07-24 15:05:05 -070087__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +000088ssize_t pread(int fd, void* const _Nonnull __pass_object_size0 buf, size_t count, off_t offset)
George Burgess IV16c17392017-07-31 21:30:47 -070089 __overloadable
90 __error_if_overflows_ssizet(count, pread)
91 __error_if_overflows_objectsize(count, __bos0(buf), pread) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -080092#if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -070093 size_t bos = __bos0(buf);
94
George Burgess IVda8d30f2019-09-18 17:09:43 -070095 if (!__bos_trivially_ge_no_overflow(bos, count)) {
96 return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -070097 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -070098#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -070099 return __PREAD_PREFIX(real)(fd, buf, count, offset);
George Burgess IVb97049c2017-07-24 15:05:05 -0700100}
George Burgess IVda8d30f2019-09-18 17:09:43 -0700101#endif /* !defined(__USE_FILE_OFFSET64) */
George Burgess IVb97049c2017-07-24 15:05:05 -0700102
George Burgess IVb97049c2017-07-24 15:05:05 -0700103__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000104ssize_t pread64(int fd, void* const _Nonnull __pass_object_size0 buf, size_t count, off64_t offset)
George Burgess IV16c17392017-07-31 21:30:47 -0700105 __overloadable
106 __error_if_overflows_ssizet(count, pread64)
107 __error_if_overflows_objectsize(count, __bos0(buf), pread64) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800108#if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700109 size_t bos = __bos0(buf);
110
George Burgess IVda8d30f2019-09-18 17:09:43 -0700111 if (!__bos_trivially_ge_no_overflow(bos, count)) {
112 return __pread64_chk(fd, buf, count, offset, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700113 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700114#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700115 return __pread64_real(fd, buf, count, offset);
116}
George Burgess IVb97049c2017-07-24 15:05:05 -0700117
George Burgess IVda8d30f2019-09-18 17:09:43 -0700118#if !defined(__USE_FILE_OFFSET64)
George Burgess IVb97049c2017-07-24 15:05:05 -0700119__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000120ssize_t pwrite(int fd, const void* const _Nonnull __pass_object_size0 buf, size_t count, off_t offset)
George Burgess IV16c17392017-07-31 21:30:47 -0700121 __overloadable
122 __error_if_overflows_ssizet(count, pwrite)
123 __error_if_overflows_objectsize(count, __bos0(buf), pwrite) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800124#if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700125 size_t bos = __bos0(buf);
126
George Burgess IVda8d30f2019-09-18 17:09:43 -0700127 if (!__bos_trivially_ge_no_overflow(bos, count)) {
128 return __PWRITE_PREFIX(chk)(fd, buf, count, offset, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700129 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700130#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700131 return __PWRITE_PREFIX(real)(fd, buf, count, offset);
George Burgess IVb97049c2017-07-24 15:05:05 -0700132}
George Burgess IVda8d30f2019-09-18 17:09:43 -0700133#endif /* !defined(__USE_FILE_OFFSET64) */
George Burgess IVb97049c2017-07-24 15:05:05 -0700134
George Burgess IVb97049c2017-07-24 15:05:05 -0700135__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000136ssize_t pwrite64(int fd, const void* const _Nonnull __pass_object_size0 buf, size_t count, off64_t offset)
George Burgess IV16c17392017-07-31 21:30:47 -0700137 __overloadable
138 __error_if_overflows_ssizet(count, pwrite64)
139 __error_if_overflows_objectsize(count, __bos0(buf), pwrite64) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800140#if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700141 size_t bos = __bos0(buf);
142
George Burgess IVda8d30f2019-09-18 17:09:43 -0700143 if (!__bos_trivially_ge_no_overflow(bos, count)) {
144 return __pwrite64_chk(fd, buf, count, offset, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700145 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700146#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700147 return __pwrite64_real(fd, buf, count, offset);
148}
George Burgess IVb97049c2017-07-24 15:05:05 -0700149
George Burgess IVb97049c2017-07-24 15:05:05 -0700150__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000151ssize_t read(int fd, void* const __BIONIC_COMPLICATED_NULLNESS __pass_object_size0 buf, size_t count)
George Burgess IV16c17392017-07-31 21:30:47 -0700152 __overloadable
153 __error_if_overflows_ssizet(count, read)
154 __error_if_overflows_objectsize(count, __bos0(buf), read) {
Elliott Hughesf4ace9d2023-02-23 17:38:37 +0000155#if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700156 size_t bos = __bos0(buf);
157
George Burgess IVda8d30f2019-09-18 17:09:43 -0700158 if (!__bos_trivially_ge_no_overflow(bos, count)) {
159 return __read_chk(fd, buf, count, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700160 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700161#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700162 return __call_bypassing_fortify(read)(fd, buf, count);
163}
George Burgess IVb97049c2017-07-24 15:05:05 -0700164
George Burgess IVb97049c2017-07-24 15:05:05 -0700165__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000166ssize_t write(int fd, const void* const __BIONIC_COMPLICATED_NULLNESS __pass_object_size0 buf, size_t count)
George Burgess IV16c17392017-07-31 21:30:47 -0700167 __overloadable
168 __error_if_overflows_ssizet(count, write)
169 __error_if_overflows_objectsize(count, __bos0(buf), write) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800170#if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700171 size_t bos = __bos0(buf);
172
George Burgess IVda8d30f2019-09-18 17:09:43 -0700173 if (!__bos_trivially_ge_no_overflow(bos, count)) {
174 return __write_chk(fd, buf, count, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700175 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700176#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700177 return __call_bypassing_fortify(write)(fd, buf, count);
178}
George Burgess IVb97049c2017-07-24 15:05:05 -0700179
George Burgess IVb97049c2017-07-24 15:05:05 -0700180__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000181ssize_t readlink(const char* _Nonnull path, char* _Nonnull const __pass_object_size buf, size_t size)
George Burgess IV16c17392017-07-31 21:30:47 -0700182 __overloadable
183 __error_if_overflows_ssizet(size, readlink)
184 __error_if_overflows_objectsize(size, __bos(buf), readlink) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800185#if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700186 size_t bos = __bos(buf);
187
George Burgess IVda8d30f2019-09-18 17:09:43 -0700188 if (!__bos_trivially_ge_no_overflow(bos, size)) {
189 return __readlink_chk(path, buf, size, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700190 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700191#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700192 return __call_bypassing_fortify(readlink)(path, buf, size);
George Burgess IVb97049c2017-07-24 15:05:05 -0700193}
194
George Burgess IVb97049c2017-07-24 15:05:05 -0700195__BIONIC_FORTIFY_INLINE
zijunzhao30cf6d02023-06-01 19:23:18 +0000196ssize_t readlinkat(int dirfd, const char* _Nonnull path, char* const _Nonnull __pass_object_size buf, size_t size)
George Burgess IV16c17392017-07-31 21:30:47 -0700197 __overloadable
198 __error_if_overflows_ssizet(size, readlinkat)
199 __error_if_overflows_objectsize(size, __bos(buf), readlinkat) {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800200#if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700201 size_t bos = __bos(buf);
202
George Burgess IVda8d30f2019-09-18 17:09:43 -0700203 if (!__bos_trivially_ge_no_overflow(bos, size)) {
204 return __readlinkat_chk(dirfd, path, buf, size, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700205 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700206#endif
George Burgess IVda8d30f2019-09-18 17:09:43 -0700207 return __call_bypassing_fortify(readlinkat)(dirfd, path, buf, size);
208}
George Burgess IVb97049c2017-07-24 15:05:05 -0700209
George Burgess IVda8d30f2019-09-18 17:09:43 -0700210#undef __bos_trivially_ge_no_overflow
George Burgess IVb97049c2017-07-24 15:05:05 -0700211#undef __enable_if_no_overflow_ssizet
212#undef __error_if_overflows_objectsize
213#undef __error_if_overflows_ssizet
George Burgess IVb97049c2017-07-24 15:05:05 -0700214#undef __PREAD_PREFIX
215#undef __PWRITE_PREFIX
216#endif /* defined(__BIONIC_FORTIFY) */