xref: /aosp_15_r20/bionic/libc/include/bits/fortify/unistd.h (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
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 
33 #if __BIONIC_AVAILABILITY_GUARD(24)
34 char* _Nullable __getcwd_chk(char* _Nullable, size_t, size_t) __INTRODUCED_IN(24);
35 #endif /* __BIONIC_AVAILABILITY_GUARD(24) */
36 
37 
38 
39 #if __BIONIC_AVAILABILITY_GUARD(23)
40 ssize_t __pread_chk(int, void* _Nonnull, size_t, off_t, size_t) __INTRODUCED_IN(23);
41 #endif /* __BIONIC_AVAILABILITY_GUARD(23) */
42 
43 ssize_t __pread_real(int, void* _Nonnull, size_t, off_t) __RENAME(pread);
44 
45 
46 #if __BIONIC_AVAILABILITY_GUARD(23)
47 ssize_t __pread64_chk(int, void* _Nonnull, size_t, off64_t, size_t) __INTRODUCED_IN(23);
48 #endif /* __BIONIC_AVAILABILITY_GUARD(23) */
49 
50 ssize_t __pread64_real(int, void* _Nonnull, size_t, off64_t) __RENAME(pread64);
51 
52 
53 #if __BIONIC_AVAILABILITY_GUARD(24)
54 ssize_t __pwrite_chk(int, const void* _Nonnull, size_t, off_t, size_t) __INTRODUCED_IN(24);
55 #endif /* __BIONIC_AVAILABILITY_GUARD(24) */
56 
57 ssize_t __pwrite_real(int, const void* _Nonnull, size_t, off_t) __RENAME(pwrite);
58 
59 
60 #if __BIONIC_AVAILABILITY_GUARD(24)
61 ssize_t __pwrite64_chk(int, const void* _Nonnull, size_t, off64_t, size_t) __INTRODUCED_IN(24);
62 #endif /* __BIONIC_AVAILABILITY_GUARD(24) */
63 
64 ssize_t __pwrite64_real(int, const void* _Nonnull, size_t, off64_t) __RENAME(pwrite64);
65 
66 ssize_t __read_chk(int, void* __BIONIC_COMPLICATED_NULLNESS, size_t, size_t);
67 
68 #if __BIONIC_AVAILABILITY_GUARD(24)
69 ssize_t __write_chk(int, const void* __BIONIC_COMPLICATED_NULLNESS, size_t, size_t) __INTRODUCED_IN(24);
70 #endif /* __BIONIC_AVAILABILITY_GUARD(24) */
71 
72 
73 #if __BIONIC_AVAILABILITY_GUARD(23)
74 ssize_t __readlink_chk(const char* _Nonnull, char* _Nonnull, size_t, size_t) __INTRODUCED_IN(23);
75 ssize_t __readlinkat_chk(int dirfd, const char* _Nonnull, char* _Nonnull, size_t, size_t) __INTRODUCED_IN(23);
76 #endif /* __BIONIC_AVAILABILITY_GUARD(23) */
77 
78 
79 #if defined(__BIONIC_FORTIFY)
80 
81 #if defined(__USE_FILE_OFFSET64)
82 #define __PREAD_PREFIX(x) __pread64_ ## x
83 #define __PWRITE_PREFIX(x) __pwrite64_ ## x
84 #else
85 #define __PREAD_PREFIX(x) __pread_ ## x
86 #define __PWRITE_PREFIX(x) __pwrite_ ## x
87 #endif
88 
89 #define __error_if_overflows_ssizet(what, fn) \
90     __clang_error_if((what) > SSIZE_MAX, "in call to '" #fn "', '" #what "' must be <= SSIZE_MAX")
91 
92 #define __error_if_overflows_objectsize(what, objsize, fn) \
93     __clang_error_if(__bos_unevaluated_lt((objsize), (what)), \
94                      "in call to '" #fn "', '" #what "' bytes overflows the given object")
95 
96 #define __bos_trivially_ge_no_overflow(bos_val, index)  \
97       ((__bos_dynamic_check_impl_and((bos_val), >=, (index), (bos_val) <= SSIZE_MAX) && \
98         __builtin_constant_p(index) && (index) <= SSIZE_MAX))
99 
100 __BIONIC_FORTIFY_INLINE
getcwd(char * const _Nullable __pass_object_size buf,size_t size)101 char* _Nullable getcwd(char* const _Nullable __pass_object_size buf, size_t size)
102         __overloadable
103         __error_if_overflows_objectsize(size, __bos(buf), getcwd) {
104 #if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
105     size_t bos = __bos(buf);
106 
107     if (!__bos_trivially_ge(bos, size)) {
108         return __getcwd_chk(buf, size, bos);
109     }
110 #endif
111     return __call_bypassing_fortify(getcwd)(buf, size);
112 }
113 
114 #if !defined(__USE_FILE_OFFSET64)
115 __BIONIC_FORTIFY_INLINE
pread(int fd,void * const _Nonnull __pass_object_size0 buf,size_t count,off_t offset)116 ssize_t pread(int fd, void* const _Nonnull __pass_object_size0 buf, size_t count, off_t offset)
117         __overloadable
118         __error_if_overflows_ssizet(count, pread)
119         __error_if_overflows_objectsize(count, __bos0(buf), pread) {
120 #if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
121     size_t bos = __bos0(buf);
122 
123     if (!__bos_trivially_ge_no_overflow(bos, count)) {
124         return __PREAD_PREFIX(chk)(fd, buf, count, offset, bos);
125     }
126 #endif
127     return __PREAD_PREFIX(real)(fd, buf, count, offset);
128 }
129 #endif /* !defined(__USE_FILE_OFFSET64) */
130 
131 __BIONIC_FORTIFY_INLINE
pread64(int fd,void * const _Nonnull __pass_object_size0 buf,size_t count,off64_t offset)132 ssize_t pread64(int fd, void* const _Nonnull __pass_object_size0 buf, size_t count, off64_t offset)
133         __overloadable
134         __error_if_overflows_ssizet(count, pread64)
135         __error_if_overflows_objectsize(count, __bos0(buf), pread64) {
136 #if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
137     size_t bos = __bos0(buf);
138 
139     if (!__bos_trivially_ge_no_overflow(bos, count)) {
140         return __pread64_chk(fd, buf, count, offset, bos);
141     }
142 #endif
143     return __pread64_real(fd, buf, count, offset);
144 }
145 
146 #if !defined(__USE_FILE_OFFSET64)
147 __BIONIC_FORTIFY_INLINE
pwrite(int fd,const void * const _Nonnull __pass_object_size0 buf,size_t count,off_t offset)148 ssize_t pwrite(int fd, const void* const _Nonnull __pass_object_size0 buf, size_t count, off_t offset)
149         __overloadable
150         __error_if_overflows_ssizet(count, pwrite)
151         __error_if_overflows_objectsize(count, __bos0(buf), pwrite) {
152 #if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
153     size_t bos = __bos0(buf);
154 
155     if (!__bos_trivially_ge_no_overflow(bos, count)) {
156         return __PWRITE_PREFIX(chk)(fd, buf, count, offset, bos);
157     }
158 #endif
159     return __PWRITE_PREFIX(real)(fd, buf, count, offset);
160 }
161 #endif /* !defined(__USE_FILE_OFFSET64) */
162 
163 __BIONIC_FORTIFY_INLINE
pwrite64(int fd,const void * const _Nonnull __pass_object_size0 buf,size_t count,off64_t offset)164 ssize_t pwrite64(int fd, const void* const _Nonnull __pass_object_size0 buf, size_t count, off64_t offset)
165         __overloadable
166         __error_if_overflows_ssizet(count, pwrite64)
167         __error_if_overflows_objectsize(count, __bos0(buf), pwrite64) {
168 #if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
169     size_t bos = __bos0(buf);
170 
171     if (!__bos_trivially_ge_no_overflow(bos, count)) {
172         return __pwrite64_chk(fd, buf, count, offset, bos);
173     }
174 #endif
175     return __pwrite64_real(fd, buf, count, offset);
176 }
177 
178 __BIONIC_FORTIFY_INLINE
read(int fd,void * const __BIONIC_COMPLICATED_NULLNESS __pass_object_size0 buf,size_t count)179 ssize_t read(int fd, void* const __BIONIC_COMPLICATED_NULLNESS __pass_object_size0 buf, size_t count)
180         __overloadable
181         __error_if_overflows_ssizet(count, read)
182         __error_if_overflows_objectsize(count, __bos0(buf), read) {
183 #if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
184     size_t bos = __bos0(buf);
185 
186     if (!__bos_trivially_ge_no_overflow(bos, count)) {
187         return __read_chk(fd, buf, count, bos);
188     }
189 #endif
190     return __call_bypassing_fortify(read)(fd, buf, count);
191 }
192 
193 __BIONIC_FORTIFY_INLINE
write(int fd,const void * const __BIONIC_COMPLICATED_NULLNESS __pass_object_size0 buf,size_t count)194 ssize_t write(int fd, const void* const __BIONIC_COMPLICATED_NULLNESS __pass_object_size0 buf, size_t count)
195         __overloadable
196         __error_if_overflows_ssizet(count, write)
197         __error_if_overflows_objectsize(count, __bos0(buf), write) {
198 #if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
199     size_t bos = __bos0(buf);
200 
201     if (!__bos_trivially_ge_no_overflow(bos, count)) {
202         return __write_chk(fd, buf, count, bos);
203     }
204 #endif
205     return __call_bypassing_fortify(write)(fd, buf, count);
206 }
207 
208 __BIONIC_FORTIFY_INLINE
readlink(const char * _Nonnull path,char * _Nonnull const __pass_object_size buf,size_t size)209 ssize_t readlink(const char* _Nonnull path, char* _Nonnull const __pass_object_size buf, size_t size)
210         __overloadable
211         __error_if_overflows_ssizet(size, readlink)
212         __error_if_overflows_objectsize(size, __bos(buf), readlink) {
213 #if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
214     size_t bos = __bos(buf);
215 
216     if (!__bos_trivially_ge_no_overflow(bos, size)) {
217         return __readlink_chk(path, buf, size, bos);
218     }
219 #endif
220     return __call_bypassing_fortify(readlink)(path, buf, size);
221 }
222 
223 __BIONIC_FORTIFY_INLINE
readlinkat(int dirfd,const char * _Nonnull path,char * const _Nonnull __pass_object_size buf,size_t size)224 ssize_t readlinkat(int dirfd, const char* _Nonnull path, char* const _Nonnull __pass_object_size buf, size_t size)
225         __overloadable
226         __error_if_overflows_ssizet(size, readlinkat)
227         __error_if_overflows_objectsize(size, __bos(buf), readlinkat) {
228 #if __ANDROID_API__ >= 23 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
229     size_t bos = __bos(buf);
230 
231     if (!__bos_trivially_ge_no_overflow(bos, size)) {
232         return __readlinkat_chk(dirfd, path, buf, size, bos);
233     }
234 #endif
235     return __call_bypassing_fortify(readlinkat)(dirfd, path, buf, size);
236 }
237 
238 #undef __bos_trivially_ge_no_overflow
239 #undef __enable_if_no_overflow_ssizet
240 #undef __error_if_overflows_objectsize
241 #undef __error_if_overflows_ssizet
242 #undef __PREAD_PREFIX
243 #undef __PWRITE_PREFIX
244 #endif /* defined(__BIONIC_FORTIFY) */
245