blob: 547a1686c3e0a0d9a067fc4a3d9deb34b6fa650b [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
32char* __getcwd_chk(char*, size_t, size_t) __INTRODUCED_IN(24);
33
34ssize_t __pread_chk(int, void*, size_t, off_t, size_t) __INTRODUCED_IN(23);
35ssize_t __pread_real(int, void*, size_t, off_t) __RENAME(pread);
36
37ssize_t __pread64_chk(int, void*, size_t, off64_t, size_t) __INTRODUCED_IN(23);
38ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64) __INTRODUCED_IN(12);
39
40ssize_t __pwrite_chk(int, const void*, size_t, off_t, size_t) __INTRODUCED_IN(24);
41ssize_t __pwrite_real(int, const void*, size_t, off_t) __RENAME(pwrite);
42
43ssize_t __pwrite64_chk(int, const void*, size_t, off64_t, size_t) __INTRODUCED_IN(24);
44ssize_t __pwrite64_real(int, const void*, size_t, off64_t) __RENAME(pwrite64)
45 __INTRODUCED_IN(12);
46
47ssize_t __read_chk(int, void*, size_t, size_t) __INTRODUCED_IN(21);
48ssize_t __write_chk(int, const void*, size_t, size_t) __INTRODUCED_IN(24);
49ssize_t __readlink_chk(const char*, char*, size_t, size_t) __INTRODUCED_IN(23);
50ssize_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 IV16c17392017-07-31 21:30:47 -070062#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 IVb97049c2017-07-24 15:05:05 -070064
George Burgess IV16c17392017-07-31 21:30:47 -070065#define __error_if_overflows_objectsize(what, objsize, fn) \
George Burgess IV5273dc52019-05-09 13:46:57 -070066 __clang_error_if(__bos_unevaluated_lt((objsize), (what)), \
George Burgess IV16c17392017-07-31 21:30:47 -070067 "in call to '" #fn "', '" #what "' bytes overflows the given object")
George Burgess IVb97049c2017-07-24 15:05:05 -070068
George Burgess IVd9865e72019-05-13 17:20:04 -070069#define __bos_trivially_not_lt_no_overflow(bos_val, index) \
George Burgess IV74519e72019-06-06 17:54:00 -070070 ((__bos_dynamic_check_impl_and((bos_val), >=, (index), (bos_val) <= SSIZE_MAX) && \
71 __builtin_constant_p(index) && (index) <= SSIZE_MAX))
George Burgess IVd9865e72019-05-13 17:20:04 -070072
George Burgess IVb97049c2017-07-24 15:05:05 -070073#if __ANDROID_API__ >= __ANDROID_API_N__
74__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -070075char* getcwd(char* const __pass_object_size buf, size_t size)
76 __overloadable
77 __error_if_overflows_objectsize(size, __bos(buf), getcwd) {
George Burgess IVb97049c2017-07-24 15:05:05 -070078 size_t bos = __bos(buf);
79
George Burgess IVd9865e72019-05-13 17:20:04 -070080 if (__bos_trivially_not_lt(bos, size)) {
George Burgess IVb97049c2017-07-24 15:05:05 -070081 return __call_bypassing_fortify(getcwd)(buf, size);
82 }
83
84 return __getcwd_chk(buf, size, bos);
85}
86#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
87
88#if __ANDROID_API__ >= __ANDROID_API_M__
George Burgess IVb97049c2017-07-24 15:05:05 -070089__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -070090ssize_t pread(int fd, void* const __pass_object_size0 buf, size_t count, off_t offset)
91 __overloadable
92 __error_if_overflows_ssizet(count, pread)
93 __error_if_overflows_objectsize(count, __bos0(buf), pread) {
George Burgess IVb97049c2017-07-24 15:05:05 -070094 size_t bos = __bos0(buf);
95
George Burgess IVd9865e72019-05-13 17:20:04 -070096 if (__bos_trivially_not_lt_no_overflow(bos, count)) {
George Burgess IVb97049c2017-07-24 15:05:05 -070097 return __PREAD_PREFIX(real)(fd, buf, count, offset);
98 }
99
100 return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos);
101}
102
George Burgess IVb97049c2017-07-24 15:05:05 -0700103__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700104ssize_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) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700108 size_t bos = __bos0(buf);
109
George Burgess IVd9865e72019-05-13 17:20:04 -0700110 if (__bos_trivially_not_lt_no_overflow(bos, count)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700111 return __pread64_real(fd, buf, count, offset);
112 }
113
114 return __pread64_chk(fd, buf, count, offset, bos);
115}
116#endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
117
118#if __ANDROID_API__ >= __ANDROID_API_N__
George Burgess IVb97049c2017-07-24 15:05:05 -0700119__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700120ssize_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) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700124 size_t bos = __bos0(buf);
125
George Burgess IVd9865e72019-05-13 17:20:04 -0700126 if (__bos_trivially_not_lt_no_overflow(bos, count)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700127 return __PWRITE_PREFIX(real)(fd, buf, count, offset);
128 }
129
130 return __PWRITE_PREFIX(chk)(fd, buf, count, offset, bos);
131}
132
George Burgess IVb97049c2017-07-24 15:05:05 -0700133__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700134ssize_t pwrite64(int fd, const void* const __pass_object_size0 buf, size_t count, off64_t offset)
135 __overloadable
136 __error_if_overflows_ssizet(count, pwrite64)
137 __error_if_overflows_objectsize(count, __bos0(buf), pwrite64) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700138 size_t bos = __bos0(buf);
139
George Burgess IVd9865e72019-05-13 17:20:04 -0700140 if (__bos_trivially_not_lt_no_overflow(bos, count)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700141 return __pwrite64_real(fd, buf, count, offset);
142 }
143
144 return __pwrite64_chk(fd, buf, count, offset, bos);
145}
146#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
147
148#if __ANDROID_API__ >= __ANDROID_API_L__
George Burgess IVb97049c2017-07-24 15:05:05 -0700149__BIONIC_FORTIFY_INLINE
150ssize_t read(int fd, void* const __pass_object_size0 buf, size_t count)
George Burgess IV16c17392017-07-31 21:30:47 -0700151 __overloadable
152 __error_if_overflows_ssizet(count, read)
153 __error_if_overflows_objectsize(count, __bos0(buf), read) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700154 size_t bos = __bos0(buf);
155
George Burgess IVd9865e72019-05-13 17:20:04 -0700156 if (__bos_trivially_not_lt_no_overflow(bos, count)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700157 return __call_bypassing_fortify(read)(fd, buf, count);
158 }
159
160 return __read_chk(fd, buf, count, bos);
161}
162#endif /* __ANDROID_API__ >= __ANDROID_API_L__ */
163
164#if __ANDROID_API__ >= __ANDROID_API_N__
George Burgess IVb97049c2017-07-24 15:05:05 -0700165__BIONIC_FORTIFY_INLINE
166ssize_t write(int fd, const void* const __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) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700170 size_t bos = __bos0(buf);
171
George Burgess IVd9865e72019-05-13 17:20:04 -0700172 if (__bos_trivially_not_lt_no_overflow(bos, count)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700173 return __call_bypassing_fortify(write)(fd, buf, count);
174 }
175
176 return __write_chk(fd, buf, count, bos);
177}
178#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
179
180#if __ANDROID_API__ >= __ANDROID_API_M__
George Burgess IVb97049c2017-07-24 15:05:05 -0700181__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700182ssize_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 IVb97049c2017-07-24 15:05:05 -0700186 size_t bos = __bos(buf);
187
George Burgess IVd9865e72019-05-13 17:20:04 -0700188 if (__bos_trivially_not_lt_no_overflow(bos, size)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700189 return __call_bypassing_fortify(readlink)(path, buf, size);
190 }
191
192 return __readlink_chk(path, buf, size, bos);
193}
194
George Burgess IVb97049c2017-07-24 15:05:05 -0700195__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700196ssize_t readlinkat(int dirfd, const char* path, char* const __pass_object_size buf, size_t size)
197 __overloadable
198 __error_if_overflows_ssizet(size, readlinkat)
199 __error_if_overflows_objectsize(size, __bos(buf), readlinkat) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700200 size_t bos = __bos(buf);
201
George Burgess IVd9865e72019-05-13 17:20:04 -0700202 if (__bos_trivially_not_lt_no_overflow(bos, size)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700203 return __call_bypassing_fortify(readlinkat)(dirfd, path, buf, size);
204 }
205
206 return __readlinkat_chk(dirfd, path, buf, size, bos);
207}
208#endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
209
George Burgess IVd9865e72019-05-13 17:20:04 -0700210#undef __bos_trivially_not_lt_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) */