blob: 04c7495ae1375891017978e21b716df10bbcd833 [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
69#if __ANDROID_API__ >= __ANDROID_API_N__
70__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -070071char* getcwd(char* const __pass_object_size buf, size_t size)
72 __overloadable
73 __error_if_overflows_objectsize(size, __bos(buf), getcwd) {
George Burgess IVb97049c2017-07-24 15:05:05 -070074 size_t bos = __bos(buf);
75
George Burgess IV16c17392017-07-31 21:30:47 -070076 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
George Burgess IVb97049c2017-07-24 15:05:05 -070077 return __call_bypassing_fortify(getcwd)(buf, size);
78 }
79
80 return __getcwd_chk(buf, size, bos);
81}
82#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
83
84#if __ANDROID_API__ >= __ANDROID_API_M__
George Burgess IVb97049c2017-07-24 15:05:05 -070085__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -070086ssize_t pread(int fd, void* const __pass_object_size0 buf, size_t count, off_t offset)
87 __overloadable
88 __error_if_overflows_ssizet(count, pread)
89 __error_if_overflows_objectsize(count, __bos0(buf), pread) {
George Burgess IVb97049c2017-07-24 15:05:05 -070090 size_t bos = __bos0(buf);
91
92 if (count == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
93 return __PREAD_PREFIX(real)(fd, buf, count, offset);
94 }
95
96 return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos);
97}
98
George Burgess IVb97049c2017-07-24 15:05:05 -070099__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700100ssize_t pread64(int fd, void* const __pass_object_size0 buf, size_t count, off64_t offset)
101 __overloadable
102 __error_if_overflows_ssizet(count, pread64)
103 __error_if_overflows_objectsize(count, __bos0(buf), pread64) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700104 size_t bos = __bos0(buf);
105
106 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
107 return __pread64_real(fd, buf, count, offset);
108 }
109
110 return __pread64_chk(fd, buf, count, offset, bos);
111}
112#endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
113
114#if __ANDROID_API__ >= __ANDROID_API_N__
George Burgess IVb97049c2017-07-24 15:05:05 -0700115__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700116ssize_t pwrite(int fd, const void* const __pass_object_size0 buf, size_t count, off_t offset)
117 __overloadable
118 __error_if_overflows_ssizet(count, pwrite)
119 __error_if_overflows_objectsize(count, __bos0(buf), pwrite) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700120 size_t bos = __bos0(buf);
121
122 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
123 return __PWRITE_PREFIX(real)(fd, buf, count, offset);
124 }
125
126 return __PWRITE_PREFIX(chk)(fd, buf, count, offset, bos);
127}
128
George Burgess IVb97049c2017-07-24 15:05:05 -0700129__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700130ssize_t pwrite64(int fd, const void* const __pass_object_size0 buf, size_t count, off64_t offset)
131 __overloadable
132 __error_if_overflows_ssizet(count, pwrite64)
133 __error_if_overflows_objectsize(count, __bos0(buf), pwrite64) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700134 size_t bos = __bos0(buf);
135
136 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
137 return __pwrite64_real(fd, buf, count, offset);
138 }
139
140 return __pwrite64_chk(fd, buf, count, offset, bos);
141}
142#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
143
144#if __ANDROID_API__ >= __ANDROID_API_L__
George Burgess IVb97049c2017-07-24 15:05:05 -0700145__BIONIC_FORTIFY_INLINE
146ssize_t read(int fd, void* const __pass_object_size0 buf, size_t count)
George Burgess IV16c17392017-07-31 21:30:47 -0700147 __overloadable
148 __error_if_overflows_ssizet(count, read)
149 __error_if_overflows_objectsize(count, __bos0(buf), read) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700150 size_t bos = __bos0(buf);
151
152 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
153 return __call_bypassing_fortify(read)(fd, buf, count);
154 }
155
156 return __read_chk(fd, buf, count, bos);
157}
158#endif /* __ANDROID_API__ >= __ANDROID_API_L__ */
159
160#if __ANDROID_API__ >= __ANDROID_API_N__
George Burgess IVb97049c2017-07-24 15:05:05 -0700161__BIONIC_FORTIFY_INLINE
162ssize_t write(int fd, const void* const __pass_object_size0 buf, size_t count)
George Burgess IV16c17392017-07-31 21:30:47 -0700163 __overloadable
164 __error_if_overflows_ssizet(count, write)
165 __error_if_overflows_objectsize(count, __bos0(buf), write) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700166 size_t bos = __bos0(buf);
167
168 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
169 return __call_bypassing_fortify(write)(fd, buf, count);
170 }
171
172 return __write_chk(fd, buf, count, bos);
173}
174#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
175
176#if __ANDROID_API__ >= __ANDROID_API_M__
George Burgess IVb97049c2017-07-24 15:05:05 -0700177__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700178ssize_t readlink(const char* path, char* const __pass_object_size buf, size_t size)
179 __overloadable
180 __error_if_overflows_ssizet(size, readlink)
181 __error_if_overflows_objectsize(size, __bos(buf), readlink) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700182 size_t bos = __bos(buf);
183
184 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
185 return __call_bypassing_fortify(readlink)(path, buf, size);
186 }
187
188 return __readlink_chk(path, buf, size, bos);
189}
190
George Burgess IVb97049c2017-07-24 15:05:05 -0700191__BIONIC_FORTIFY_INLINE
George Burgess IV16c17392017-07-31 21:30:47 -0700192ssize_t readlinkat(int dirfd, const char* path, char* const __pass_object_size buf, size_t size)
193 __overloadable
194 __error_if_overflows_ssizet(size, readlinkat)
195 __error_if_overflows_objectsize(size, __bos(buf), readlinkat) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700196 size_t bos = __bos(buf);
197
198 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
199 return __call_bypassing_fortify(readlinkat)(dirfd, path, buf, size);
200 }
201
202 return __readlinkat_chk(dirfd, path, buf, size, bos);
203}
204#endif /* __ANDROID_API__ >= __ANDROID_API_M__ */
205
206#undef __enable_if_no_overflow_ssizet
207#undef __error_if_overflows_objectsize
208#undef __error_if_overflows_ssizet
George Burgess IVb97049c2017-07-24 15:05:05 -0700209#undef __PREAD_PREFIX
210#undef __PWRITE_PREFIX
211#endif /* defined(__BIONIC_FORTIFY) */