blob: c42bd0c2a28a929c8e7cf95008be22a7c5ca6c76 [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);
Elliott Hughes0e14c5a2019-10-03 20:35:38 -070038ssize_t __pread64_real(int, void*, size_t, off64_t) __RENAME(pread64);
George Burgess IVb97049c2017-07-24 15:05:05 -070039
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);
Elliott Hughes0e14c5a2019-10-03 20:35:38 -070044ssize_t __pwrite64_real(int, const void*, size_t, off64_t) __RENAME(pwrite64);
George Burgess IVb97049c2017-07-24 15:05:05 -070045
46ssize_t __read_chk(int, void*, size_t, size_t) __INTRODUCED_IN(21);
47ssize_t __write_chk(int, const void*, size_t, size_t) __INTRODUCED_IN(24);
48ssize_t __readlink_chk(const char*, char*, size_t, size_t) __INTRODUCED_IN(23);
49ssize_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 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 IVd9865e72019-05-13 17:20:04 -070068#define __bos_trivially_not_lt_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#if __ANDROID_API__ >= __ANDROID_API_N__
73__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -070074char* getcwd(char* const __pass_object_size buf, size_t size)
75 __overloadable
76 __error_if_overflows_objectsize(size, __bos(buf), getcwd) {
George Burgess IVb97049c2017-07-24 15:05:05 -070077 size_t bos = __bos(buf);
78
George Burgess IVd9865e72019-05-13 17:20:04 -070079 if (__bos_trivially_not_lt(bos, size)) {
George Burgess IVb97049c2017-07-24 15:05:05 -070080 return __call_bypassing_fortify(getcwd)(buf, size);
81 }
82
83 return __getcwd_chk(buf, size, bos);
84}
85#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
86
87#if __ANDROID_API__ >= __ANDROID_API_M__
George Burgess IVb97049c2017-07-24 15:05:05 -070088__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -070089ssize_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 IVb97049c2017-07-24 15:05:05 -070093 size_t bos = __bos0(buf);
94
George Burgess IVd9865e72019-05-13 17:20:04 -070095 if (__bos_trivially_not_lt_no_overflow(bos, count)) {
George Burgess IVb97049c2017-07-24 15:05:05 -070096 return __PREAD_PREFIX(real)(fd, buf, count, offset);
97 }
98
99 return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos);
100}
101
George Burgess IVb97049c2017-07-24 15:05:05 -0700102__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700103ssize_t pread64(int fd, void* const __pass_object_size0 buf, size_t count, off64_t offset)
104 __overloadable
105 __error_if_overflows_ssizet(count, pread64)
106 __error_if_overflows_objectsize(count, __bos0(buf), pread64) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700107 size_t bos = __bos0(buf);
108
George Burgess IVd9865e72019-05-13 17:20:04 -0700109 if (__bos_trivially_not_lt_no_overflow(bos, count)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700110 return __pread64_real(fd, buf, count, offset);
111 }
112
113 return __pread64_chk(fd, buf, count, offset, bos);
114}
115#endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
116
117#if __ANDROID_API__ >= __ANDROID_API_N__
George Burgess IVb97049c2017-07-24 15:05:05 -0700118__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700119ssize_t pwrite(int fd, const void* const __pass_object_size0 buf, size_t count, off_t offset)
120 __overloadable
121 __error_if_overflows_ssizet(count, pwrite)
122 __error_if_overflows_objectsize(count, __bos0(buf), pwrite) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700123 size_t bos = __bos0(buf);
124
George Burgess IVd9865e72019-05-13 17:20:04 -0700125 if (__bos_trivially_not_lt_no_overflow(bos, count)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700126 return __PWRITE_PREFIX(real)(fd, buf, count, offset);
127 }
128
129 return __PWRITE_PREFIX(chk)(fd, buf, count, offset, bos);
130}
131
George Burgess IVb97049c2017-07-24 15:05:05 -0700132__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700133ssize_t pwrite64(int fd, const void* const __pass_object_size0 buf, size_t count, off64_t offset)
134 __overloadable
135 __error_if_overflows_ssizet(count, pwrite64)
136 __error_if_overflows_objectsize(count, __bos0(buf), pwrite64) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700137 size_t bos = __bos0(buf);
138
George Burgess IVd9865e72019-05-13 17:20:04 -0700139 if (__bos_trivially_not_lt_no_overflow(bos, count)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700140 return __pwrite64_real(fd, buf, count, offset);
141 }
142
143 return __pwrite64_chk(fd, buf, count, offset, bos);
144}
145#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
146
147#if __ANDROID_API__ >= __ANDROID_API_L__
George Burgess IVb97049c2017-07-24 15:05:05 -0700148__BIONIC_FORTIFY_INLINE
149ssize_t read(int fd, void* const __pass_object_size0 buf, size_t count)
George Burgess IV16c17392017-07-31 21:30:47 -0700150 __overloadable
151 __error_if_overflows_ssizet(count, read)
152 __error_if_overflows_objectsize(count, __bos0(buf), read) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700153 size_t bos = __bos0(buf);
154
George Burgess IVd9865e72019-05-13 17:20:04 -0700155 if (__bos_trivially_not_lt_no_overflow(bos, count)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700156 return __call_bypassing_fortify(read)(fd, buf, count);
157 }
158
159 return __read_chk(fd, buf, count, bos);
160}
161#endif /* __ANDROID_API__ >= __ANDROID_API_L__ */
162
163#if __ANDROID_API__ >= __ANDROID_API_N__
George Burgess IVb97049c2017-07-24 15:05:05 -0700164__BIONIC_FORTIFY_INLINE
165ssize_t write(int fd, const void* const __pass_object_size0 buf, size_t count)
George Burgess IV16c17392017-07-31 21:30:47 -0700166 __overloadable
167 __error_if_overflows_ssizet(count, write)
168 __error_if_overflows_objectsize(count, __bos0(buf), write) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700169 size_t bos = __bos0(buf);
170
George Burgess IVd9865e72019-05-13 17:20:04 -0700171 if (__bos_trivially_not_lt_no_overflow(bos, count)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700172 return __call_bypassing_fortify(write)(fd, buf, count);
173 }
174
175 return __write_chk(fd, buf, count, bos);
176}
177#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
178
179#if __ANDROID_API__ >= __ANDROID_API_M__
George Burgess IVb97049c2017-07-24 15:05:05 -0700180__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700181ssize_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) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700185 size_t bos = __bos(buf);
186
George Burgess IVd9865e72019-05-13 17:20:04 -0700187 if (__bos_trivially_not_lt_no_overflow(bos, size)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700188 return __call_bypassing_fortify(readlink)(path, buf, size);
189 }
190
191 return __readlink_chk(path, buf, size, bos);
192}
193
George Burgess IVb97049c2017-07-24 15:05:05 -0700194__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700195ssize_t readlinkat(int dirfd, const char* path, char* const __pass_object_size buf, size_t size)
196 __overloadable
197 __error_if_overflows_ssizet(size, readlinkat)
198 __error_if_overflows_objectsize(size, __bos(buf), readlinkat) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700199 size_t bos = __bos(buf);
200
George Burgess IVd9865e72019-05-13 17:20:04 -0700201 if (__bos_trivially_not_lt_no_overflow(bos, size)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700202 return __call_bypassing_fortify(readlinkat)(dirfd, path, buf, size);
203 }
204
205 return __readlinkat_chk(dirfd, path, buf, size, bos);
206}
207#endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
208
George Burgess IVd9865e72019-05-13 17:20:04 -0700209#undef __bos_trivially_not_lt_no_overflow
George Burgess IVb97049c2017-07-24 15:05:05 -0700210#undef __enable_if_no_overflow_ssizet
211#undef __error_if_overflows_objectsize
212#undef __error_if_overflows_ssizet
George Burgess IVb97049c2017-07-24 15:05:05 -0700213#undef __PREAD_PREFIX
214#undef __PWRITE_PREFIX
215#endif /* defined(__BIONIC_FORTIFY) */