blob: 4a7ff1367d9342690e423f62673d990e2ff4a83e [file] [log] [blame]
Elliott Hughesb83d6742016-02-25 20:33:47 -08001/*
2 * Copyright (C) 2012 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
29/*
30 * Copyright (c) 1988 Regents of the University of California.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
Elliott Hughesb83d6742016-02-25 20:33:47 -080058#include <poll.h>
59#include <stdarg.h>
60#include <stddef.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <sys/cdefs.h>
65#include <sys/select.h>
66#include <sys/socket.h>
67#include <sys/stat.h>
68#include <unistd.h>
69
70#include "private/bionic_fortify.h"
71
72//
Elliott Hughesef2b2fe2017-04-16 08:50:58 -070073// For more about FORTIFY see:
74//
75// https://android-developers.googleblog.com/2017/04/fortify-in-android.html
76//
Elliott Hughesb83d6742016-02-25 20:33:47 -080077// http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html
78// http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
79//
Elliott Hughesef2b2fe2017-04-16 08:50:58 -070080
81struct __bionic_zero_size_is_okay_t __bionic_zero_size_is_okay;
Elliott Hughesb83d6742016-02-25 20:33:47 -080082
83int __FD_ISSET_chk(int fd, fd_set* set, size_t set_size) {
84 __check_fd_set("FD_ISSET", fd, set_size);
85 return FD_ISSET(fd, set);
86}
87
88void __FD_CLR_chk(int fd, fd_set* set, size_t set_size) {
89 __check_fd_set("FD_CLR", fd, set_size);
90 FD_CLR(fd, set);
91}
92
93void __FD_SET_chk(int fd, fd_set* set, size_t set_size) {
94 __check_fd_set("FD_SET", fd, set_size);
95 FD_SET(fd, set);
96}
97
98char* __fgets_chk(char* dst, int supplied_size, FILE* stream, size_t dst_len_from_compiler) {
99 if (supplied_size < 0) {
100 __fortify_fatal("fgets: buffer size %d < 0", supplied_size);
101 }
102 __check_buffer_access("fgets", "write into", supplied_size, dst_len_from_compiler);
103 return fgets(dst, supplied_size, stream);
104}
105
106size_t __fread_chk(void* __restrict buf, size_t size, size_t count,
107 FILE* __restrict stream, size_t buf_size) {
108 size_t total;
109 if (__predict_false(__size_mul_overflow(size, count, &total))) {
110 // overflow: trigger the error path in fread
111 return fread(buf, size, count, stream);
112 }
113 __check_buffer_access("fread", "write into", total, buf_size);
114 return fread(buf, size, count, stream);
115}
116
117size_t __fwrite_chk(const void* __restrict buf, size_t size, size_t count,
118 FILE* __restrict stream, size_t buf_size) {
119 size_t total;
120 if (__predict_false(__size_mul_overflow(size, count, &total))) {
121 // overflow: trigger the error path in fwrite
122 return fwrite(buf, size, count, stream);
123 }
124 __check_buffer_access("fwrite", "read from", total, buf_size);
125 return fwrite(buf, size, count, stream);
126}
127
128extern char* __getcwd_chk(char* buf, size_t len, size_t actual_size) {
129 __check_buffer_access("getcwd", "write into", len, actual_size);
130 return getcwd(buf, len);
131}
132
133void* __memchr_chk(const void* s, int c, size_t n, size_t actual_size) {
134 __check_buffer_access("memchr", "read from", n, actual_size);
George Burgess IVbd3d2082017-04-04 17:34:02 -0700135 return const_cast<void*>(memchr(s, c, n));
Elliott Hughesb83d6742016-02-25 20:33:47 -0800136}
137
138// Runtime implementation of __builtin____memmove_chk (used directly by compiler, not in headers).
139extern "C" void* __memmove_chk(void* dst, const void* src, size_t len, size_t dst_len) {
140 __check_buffer_access("memmove", "write into", len, dst_len);
141 return memmove(dst, src, len);
142}
143
Elliott Hughes3c6016f2016-03-01 14:45:58 -0800144// memcpy is performance-critical enough that we have assembler __memcpy_chk implementations.
145// This function is used to give better diagnostics than we can easily do from assembler.
146extern "C" void* __memcpy_chk_fail(void* /*dst*/, const void* /*src*/, size_t count, size_t dst_len) {
147 __check_count("memcpy", "count", count);
148 __check_buffer_access("memcpy", "write into", count, dst_len);
149 abort(); // One of the above is supposed to have failed, otherwise we shouldn't have been called.
150}
151
Elliott Hughesb83d6742016-02-25 20:33:47 -0800152void* __memrchr_chk(const void* s, int c, size_t n, size_t actual_size) {
153 __check_buffer_access("memrchr", "read from", n, actual_size);
154 return memrchr(s, c, n);
155}
156
Elliott Hughes62e59642016-03-01 11:22:42 -0800157// memset is performance-critical enough that we have assembler __memset_chk implementations.
158// This function is used to give better diagnostics than we can easily do from assembler.
159extern "C" void* __memset_chk_fail(void* /*dst*/, int /*byte*/, size_t count, size_t dst_len) {
160 __check_count("memset", "count", count);
161 __check_buffer_access("memset", "write into", count, dst_len);
162 abort(); // One of the above is supposed to have failed, otherwise we shouldn't have been called.
163}
164
Elliott Hughesb83d6742016-02-25 20:33:47 -0800165int __poll_chk(pollfd* fds, nfds_t fd_count, int timeout, size_t fds_size) {
166 __check_pollfd_array("poll", fds_size, fd_count);
167 return poll(fds, fd_count, timeout);
168}
169
170int __ppoll_chk(pollfd* fds, nfds_t fd_count, const timespec* timeout,
171 const sigset_t* mask, size_t fds_size) {
172 __check_pollfd_array("ppoll", fds_size, fd_count);
173 return ppoll(fds, fd_count, timeout, mask);
174}
175
176ssize_t __pread64_chk(int fd, void* buf, size_t count, off64_t offset, size_t buf_size) {
177 __check_count("pread64", "count", count);
178 __check_buffer_access("pread64", "write into", count, buf_size);
179 return pread64(fd, buf, count, offset);
180}
181
182ssize_t __pread_chk(int fd, void* buf, size_t count, off_t offset, size_t buf_size) {
183 __check_count("pread", "count", count);
184 __check_buffer_access("pread", "write into", count, buf_size);
185 return pread(fd, buf, count, offset);
186}
187
188ssize_t __pwrite64_chk(int fd, const void* buf, size_t count, off64_t offset,
189 size_t buf_size) {
190 __check_count("pwrite64", "count", count);
191 __check_buffer_access("pwrite64", "read from", count, buf_size);
192 return pwrite64(fd, buf, count, offset);
193}
194
195ssize_t __pwrite_chk(int fd, const void* buf, size_t count, off_t offset,
196 size_t buf_size) {
197 __check_count("pwrite", "count", count);
198 __check_buffer_access("pwrite", "read from", count, buf_size);
199 return pwrite(fd, buf, count, offset);
200}
201
202ssize_t __read_chk(int fd, void* buf, size_t count, size_t buf_size) {
203 __check_count("read", "count", count);
204 __check_buffer_access("read", "write into", count, buf_size);
205 return read(fd, buf, count);
206}
207
208ssize_t __readlinkat_chk(int dirfd, const char* path, char* buf, size_t size, size_t buf_size) {
209 __check_count("readlinkat", "size", size);
210 __check_buffer_access("readlinkat", "write into", size, buf_size);
211 return readlinkat(dirfd, path, buf, size);
212}
213
214ssize_t __readlink_chk(const char* path, char* buf, size_t size, size_t buf_size) {
215 __check_count("readlink", "size", size);
216 __check_buffer_access("readlink", "write into", size, buf_size);
217 return readlink(path, buf, size);
218}
219
220ssize_t __recvfrom_chk(int socket, void* buf, size_t len, size_t buf_size,
Elliott Hughes8197aca2016-08-12 09:20:07 -0700221 int flags, sockaddr* src_addr, socklen_t* addrlen) {
Elliott Hughesb83d6742016-02-25 20:33:47 -0800222 __check_buffer_access("recvfrom", "write into", len, buf_size);
223 return recvfrom(socket, buf, len, flags, src_addr, addrlen);
224}
225
Daniel Micay95b59c52017-02-13 17:27:59 -0800226ssize_t __sendto_chk(int socket, const void* buf, size_t len, size_t buflen,
227 int flags, const struct sockaddr* dest_addr,
228 socklen_t addrlen) {
229 __check_buffer_access("sendto", "read from", len, buflen);
230 return sendto(socket, buf, len, flags, dest_addr, addrlen);
231}
232
Elliott Hughesb83d6742016-02-25 20:33:47 -0800233// Runtime implementation of __builtin____stpcpy_chk (used directly by compiler, not in headers)..
234extern "C" char* __stpcpy_chk(char* dst, const char* src, size_t dst_len) {
235 // TODO: optimize so we don't scan src twice.
236 size_t src_len = strlen(src) + 1;
237 __check_buffer_access("stpcpy", "write into", src_len, dst_len);
238 return stpcpy(dst, src);
239}
240
241// Runtime implementation of __builtin____stpncpy_chk (used directly by compiler, not in headers).
242extern "C" char* __stpncpy_chk(char* __restrict dst, const char* __restrict src,
243 size_t len, size_t dst_len) {
244 __check_buffer_access("stpncpy", "write into", len, dst_len);
245 return stpncpy(dst, src, len);
246}
247
248// This is a variant of __stpncpy_chk, but it also checks to make
249// sure we don't read beyond the end of "src". The code for this is
250// based on the original version of stpncpy, but modified to check
251// how much we read from "src" at the end of the copy operation.
252char* __stpncpy_chk2(char* __restrict dst, const char* __restrict src,
253 size_t n, size_t dst_len, size_t src_len) {
254 __check_buffer_access("stpncpy", "write into", n, dst_len);
255 if (n != 0) {
256 char* d = dst;
257 const char* s = src;
258
259 do {
260 if ((*d++ = *s++) == 0) {
261 // NUL pad the remaining n-1 bytes.
262 while (--n != 0) {
263 *d++ = 0;
264 }
265 break;
266 }
267 } while (--n != 0);
268
269 size_t s_copy_len = static_cast<size_t>(s - src);
270 if (__predict_false(s_copy_len > src_len)) {
271 __fortify_fatal("stpncpy: detected read past end of %zu-byte buffer", src_len);
272 }
273 }
274
275 return dst;
276}
277
Elliott Hughesc75da092016-05-25 17:01:31 -0700278// strcat is performance-critical enough that we have assembler __strcat_chk implementations.
279// This function is used to give better diagnostics than we can easily do from assembler.
280extern "C" void __strcat_chk_fail(size_t dst_buf_size) {
281 __fortify_fatal("strcat: prevented write past end of %zu-byte buffer", dst_buf_size);
282}
283
Elliott Hughesb83d6742016-02-25 20:33:47 -0800284char* __strchr_chk(const char* p, int ch, size_t s_len) {
285 for (;; ++p, s_len--) {
286 if (__predict_false(s_len == 0)) {
287 __fortify_fatal("strchr: prevented read past end of buffer");
288 }
289 if (*p == static_cast<char>(ch)) {
290 return const_cast<char*>(p);
291 }
292 if (*p == '\0') {
293 return NULL;
294 }
295 }
296}
297
Elliott Hughesbdd8f892016-05-26 16:38:34 -0700298// strcpy is performance-critical enough that we have assembler __strcpy_chk implementations.
299// This function is used to give better diagnostics than we can easily do from assembler.
300extern "C" void __strcpy_chk_fail(size_t dst_buf_size) {
301 __fortify_fatal("strcpy: prevented write past end of %zu-byte buffer", dst_buf_size);
302}
303
Elliott Hughesb83d6742016-02-25 20:33:47 -0800304size_t __strlcat_chk(char* dst, const char* src,
305 size_t supplied_size, size_t dst_len_from_compiler) {
306 __check_buffer_access("strlcat", "write into", supplied_size, dst_len_from_compiler);
307 return strlcat(dst, src, supplied_size);
308}
309
310size_t __strlcpy_chk(char* dst, const char* src,
311 size_t supplied_size, size_t dst_len_from_compiler) {
312 __check_buffer_access("strlcpy", "write into", supplied_size, dst_len_from_compiler);
313 return strlcpy(dst, src, supplied_size);
314}
315
316size_t __strlen_chk(const char* s, size_t s_len) {
317 // TODO: "prevented" here would be a lie because this strlen can run off the end.
318 // strlen is too important to be expensive, so we wanted to be able to call the optimized
319 // implementation, but I think we need to implement optimized assembler __strlen_chk routines.
320 size_t ret = strlen(s);
321 if (__predict_false(ret >= s_len)) {
322 __fortify_fatal("strlen: detected read past end of buffer");
323 }
324 return ret;
325}
326
327// Runtime implementation of __builtin____strncat_chk (used directly by compiler, not in headers).
328extern "C" char* __strncat_chk(char* __restrict dst, const char* __restrict src,
329 size_t len, size_t dst_buf_size) {
330 if (len == 0) {
331 return dst;
332 }
333
334 size_t dst_len = __strlen_chk(dst, dst_buf_size);
335 char* d = dst + dst_len;
336 dst_buf_size -= dst_len;
337
338 while (*src != '\0') {
339 *d++ = *src++;
340 len--; dst_buf_size--;
341
342 if (__predict_false(dst_buf_size == 0)) {
343 __fortify_fatal("strncat: prevented write past end of buffer");
344 }
345
346 if (len == 0) {
347 break;
348 }
349 }
350
351 *d = '\0';
352 return dst;
353}
354
355// Runtime implementation of __builtin____strncpy_chk (used directly by compiler, not in headers).
356extern "C" char* __strncpy_chk(char* __restrict dst, const char* __restrict src,
357 size_t len, size_t dst_len) {
358 __check_buffer_access("strncpy", "write into", len, dst_len);
359 return strncpy(dst, src, len);
360}
361
362// This is a variant of __strncpy_chk, but it also checks to make
363// sure we don't read beyond the end of "src". The code for this is
364// based on the original version of strncpy, but modified to check
365// how much we read from "src" at the end of the copy operation.
366char* __strncpy_chk2(char* __restrict dst, const char* __restrict src,
367 size_t n, size_t dst_len, size_t src_len) {
368 __check_buffer_access("strncpy", "write into", n, dst_len);
369 if (n != 0) {
370 char* d = dst;
371 const char* s = src;
372
373 do {
374 if ((*d++ = *s++) == 0) {
375 // NUL pad the remaining n-1 bytes.
376 while (--n != 0) {
377 *d++ = 0;
378 }
379 break;
380 }
381 } while (--n != 0);
382
383 size_t s_copy_len = static_cast<size_t>(s - src);
384 if (__predict_false(s_copy_len > src_len)) {
385 __fortify_fatal("strncpy: detected read past end of %zu-byte buffer", src_len);
386 }
387 }
388
389 return dst;
390}
391
392char* __strrchr_chk(const char* p, int ch, size_t s_len) {
393 for (const char* save = NULL;; ++p, s_len--) {
394 if (s_len == 0) {
395 __fortify_fatal("strrchr: prevented read past end of buffer");
396 }
397 if (*p == static_cast<char>(ch)) {
398 save = p;
399 }
400 if (!*p) {
401 return const_cast<char*>(save);
402 }
403 }
404}
405
406mode_t __umask_chk(mode_t mode) {
407 if (__predict_false((mode & 0777) != mode)) {
408 __fortify_fatal("umask: called with invalid mask %o", mode);
409 }
410
411 return umask(mode);
412}
413
414// Runtime implementation of __builtin____vsnprintf_chk (used directly by compiler, not in headers).
415extern "C" int __vsnprintf_chk(char* dst, size_t supplied_size, int /*flags*/,
416 size_t dst_len_from_compiler, const char* format, va_list va) {
417 __check_buffer_access("vsnprintf", "write into", supplied_size, dst_len_from_compiler);
418 return vsnprintf(dst, supplied_size, format, va);
419}
420
421// Runtime implementation of __builtin____snprintf_chk (used directly by compiler, not in headers).
422extern "C" int __snprintf_chk(char* dst, size_t supplied_size, int flags,
423 size_t dst_len_from_compiler, const char* format, ...) {
424 va_list va;
425 va_start(va, format);
426 int result = __vsnprintf_chk(dst, supplied_size, flags, dst_len_from_compiler, format, va);
427 va_end(va);
428 return result;
429}
430
431// Runtime implementation of __builtin____vsprintf_chk (used directly by compiler, not in headers).
432extern "C" int __vsprintf_chk(char* dst, int /*flags*/,
433 size_t dst_len_from_compiler, const char* format, va_list va) {
Elliott Hughesfb3873d2016-08-10 11:07:54 -0700434 // The compiler uses SIZE_MAX to mean "no idea", but our vsnprintf rejects sizes that large.
435 int result = vsnprintf(dst,
436 dst_len_from_compiler == SIZE_MAX ? SSIZE_MAX : dst_len_from_compiler,
437 format, va);
438
439 // Try to catch failures after the fact...
Elliott Hughesb83d6742016-02-25 20:33:47 -0800440 __check_buffer_access("vsprintf", "write into", result + 1, dst_len_from_compiler);
441 return result;
442}
443
444// Runtime implementation of __builtin____sprintf_chk (used directly by compiler, not in headers).
445extern "C" int __sprintf_chk(char* dst, int flags, size_t dst_len_from_compiler,
446 const char* format, ...) {
447 va_list va;
448 va_start(va, format);
449 int result = __vsprintf_chk(dst, flags, dst_len_from_compiler, format, va);
450 va_end(va);
451 return result;
452}
453
454ssize_t __write_chk(int fd, const void* buf, size_t count, size_t buf_size) {
455 __check_count("write", "count", count);
456 __check_buffer_access("write", "read from", count, buf_size);
457 return write(fd, buf, count);
458}
George Burgess IVd34b0a92017-07-25 11:43:39 -0700459
460#if !defined(NO___STRCAT_CHK)
461// Runtime implementation of __builtin____strcat_chk (used directly by compiler, not in headers).
462extern "C" char* __strcat_chk(char* __restrict dst, const char* __restrict src,
463 size_t dst_buf_size) {
464 char* save = dst;
465 size_t dst_len = __strlen_chk(dst, dst_buf_size);
466
467 dst += dst_len;
468 dst_buf_size -= dst_len;
469
470 while ((*dst++ = *src++) != '\0') {
471 dst_buf_size--;
472 if (__predict_false(dst_buf_size == 0)) {
473 __fortify_fatal("strcat: prevented write past end of %zu-byte buffer", dst_buf_size);
474 }
475 }
476
477 return save;
478}
479#endif // NO___STRCAT_CHK
480
481#if !defined(NO___STRCPY_CHK)
482// Runtime implementation of __builtin____strcpy_chk (used directly by compiler, not in headers).
483extern "C" char* __strcpy_chk(char* dst, const char* src, size_t dst_len) {
484 // TODO: optimize so we don't scan src twice.
485 size_t src_len = strlen(src) + 1;
486 __check_buffer_access("strcpy", "write into", src_len, dst_len);
487 return strcpy(dst, src);
488}
489#endif // NO___STRCPY_CHK
490
491#if !defined(NO___MEMCPY_CHK)
492// Runtime implementation of __memcpy_chk (used directly by compiler, not in headers).
493extern "C" void* __memcpy_chk(void* dst, const void* src, size_t count, size_t dst_len) {
494 __check_count("memcpy", "count", count);
495 __check_buffer_access("memcpy", "write into", count, dst_len);
496 return memcpy(dst, src, count);
497}
498#endif // NO___MEMCPY_CHK