George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 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 | |
| 29 | #ifndef _STDIO_H_ |
| 30 | #error "Never include this file directly; instead, include <stdio.h>" |
| 31 | #endif |
| 32 | |
| 33 | char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17); |
Elliott Hughes | ec6850d | 2017-08-01 08:28:46 -0700 | [diff] [blame] | 34 | size_t __fread_chk(void*, size_t, size_t, FILE*, size_t) __INTRODUCED_IN(24); |
| 35 | size_t __fwrite_chk(const void*, size_t, size_t, FILE*, size_t) __INTRODUCED_IN(24); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 36 | |
| 37 | #if defined(__BIONIC_FORTIFY) && !defined(__BIONIC_NO_STDIO_FORTIFY) |
| 38 | |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame^] | 39 | #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | 36926f4 | 2019-09-15 16:57:00 -0700 | [diff] [blame] | 40 | /* No diag -- clang diagnoses misuses of this on its own. */ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 41 | __BIONIC_FORTIFY_INLINE __printflike(3, 0) |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 42 | int vsnprintf(char* const __pass_object_size dest, size_t size, const char* format, va_list ap) |
| 43 | __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 44 | return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap); |
| 45 | } |
| 46 | |
| 47 | __BIONIC_FORTIFY_INLINE __printflike(2, 0) |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 48 | int vsprintf(char* const __pass_object_size dest, const char* format, va_list ap) __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 49 | return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap); |
| 50 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame^] | 51 | #endif |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 52 | |
George Burgess IV | 113d6fa | 2019-09-18 17:06:14 -0700 | [diff] [blame] | 53 | __BIONIC_ERROR_FUNCTION_VISIBILITY |
| 54 | int sprintf(char* dest, const char* format) |
| 55 | __overloadable |
| 56 | __enable_if(__bos_unevaluated_lt(__bos(dest), __builtin_strlen(format)), |
| 57 | "format string will always overflow destination buffer") |
| 58 | __errorattr("format string will always overflow destination buffer"); |
| 59 | |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame^] | 60 | #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | 113d6fa | 2019-09-18 17:06:14 -0700 | [diff] [blame] | 61 | __BIONIC_FORTIFY_VARIADIC __printflike(2, 3) |
| 62 | int sprintf(char* const __pass_object_size dest, const char* format, ...) __overloadable { |
| 63 | va_list va; |
| 64 | va_start(va, format); |
| 65 | int result = __builtin___vsprintf_chk(dest, 0, __bos(dest), format, va); |
| 66 | va_end(va); |
| 67 | return result; |
| 68 | } |
| 69 | |
George Burgess IV | 36926f4 | 2019-09-15 16:57:00 -0700 | [diff] [blame] | 70 | /* No diag -- clang diagnoses misuses of this on its own. */ |
Chih-Hung Hsieh | f81abef | 2018-01-25 15:30:27 -0800 | [diff] [blame] | 71 | __BIONIC_FORTIFY_VARIADIC __printflike(3, 4) |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 72 | int snprintf(char* const __pass_object_size dest, size_t size, const char* format, ...) |
| 73 | __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 74 | va_list va; |
| 75 | va_start(va, format); |
| 76 | int result = __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, va); |
| 77 | va_end(va); |
| 78 | return result; |
| 79 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame^] | 80 | #endif |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 81 | |
George Burgess IV | 113d6fa | 2019-09-18 17:06:14 -0700 | [diff] [blame] | 82 | #define __bos_trivially_ge_mul(bos_val, size, count) \ |
George Burgess IV | a1a09b2 | 2019-05-13 17:16:20 -0700 | [diff] [blame] | 83 | __bos_dynamic_check_impl_and(bos_val, >=, (size) * (count), \ |
| 84 | !__unsafe_check_mul_overflow(size, count)) |
| 85 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 86 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 87 | size_t fread(void* const __pass_object_size0 buf, size_t size, size_t count, FILE* stream) |
| 88 | __overloadable |
| 89 | __clang_error_if(__unsafe_check_mul_overflow(size, count), |
| 90 | "in call to 'fread', size * count overflows") |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame] | 91 | __clang_error_if(__bos_unevaluated_lt(__bos0(buf), size * count), |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 92 | "in call to 'fread', size * count is too large for the given buffer") { |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame^] | 93 | #if __ANDROID_API__ >= __ANDROID_API_N__ && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 94 | size_t bos = __bos0(buf); |
| 95 | |
George Burgess IV | 113d6fa | 2019-09-18 17:06:14 -0700 | [diff] [blame] | 96 | if (!__bos_trivially_ge_mul(bos, size, count)) { |
| 97 | return __fread_chk(buf, size, count, stream, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 98 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame^] | 99 | #endif |
George Burgess IV | 113d6fa | 2019-09-18 17:06:14 -0700 | [diff] [blame] | 100 | return __call_bypassing_fortify(fread)(buf, size, count, stream); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 101 | } |
| 102 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 103 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | ec6850d | 2017-08-01 08:28:46 -0700 | [diff] [blame] | 104 | size_t fwrite(const void* const __pass_object_size0 buf, size_t size, size_t count, FILE* stream) |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 105 | __overloadable |
| 106 | __clang_error_if(__unsafe_check_mul_overflow(size, count), |
| 107 | "in call to 'fwrite', size * count overflows") |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame] | 108 | __clang_error_if(__bos_unevaluated_lt(__bos0(buf), size * count), |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 109 | "in call to 'fwrite', size * count is too large for the given buffer") { |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame^] | 110 | #if __ANDROID_API__ >= __ANDROID_API_N__ && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 111 | size_t bos = __bos0(buf); |
| 112 | |
George Burgess IV | 113d6fa | 2019-09-18 17:06:14 -0700 | [diff] [blame] | 113 | if (!__bos_trivially_ge_mul(bos, size, count)) { |
| 114 | return __fwrite_chk(buf, size, count, stream, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 115 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame^] | 116 | #endif |
George Burgess IV | 113d6fa | 2019-09-18 17:06:14 -0700 | [diff] [blame] | 117 | return __call_bypassing_fortify(fwrite)(buf, size, count, stream); |
| 118 | } |
| 119 | #undef __bos_trivially_ge_mul |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 120 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 121 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 122 | char* fgets(char* const __pass_object_size dest, int size, FILE* stream) |
| 123 | __overloadable |
| 124 | __clang_error_if(size < 0, "in call to 'fgets', size should not be negative") |
George Burgess IV | 5273dc5 | 2019-05-09 13:46:57 -0700 | [diff] [blame] | 125 | __clang_error_if(__bos_unevaluated_lt(__bos(dest), size), |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame] | 126 | "in call to 'fgets', size is larger than the destination buffer") { |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame^] | 127 | #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 128 | size_t bos = __bos(dest); |
| 129 | |
George Burgess IV | 113d6fa | 2019-09-18 17:06:14 -0700 | [diff] [blame] | 130 | if (!__bos_dynamic_check_impl_and(bos, >=, (size_t)size, size >= 0)) { |
| 131 | return __fgets_chk(dest, size, stream, bos); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 132 | } |
George Burgess IV | 8a0cdb1 | 2019-10-21 13:27:57 -0700 | [diff] [blame^] | 133 | #endif |
George Burgess IV | 113d6fa | 2019-09-18 17:06:14 -0700 | [diff] [blame] | 134 | return __call_bypassing_fortify(fgets)(dest, size, stream); |
| 135 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 136 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 137 | #endif /* defined(__BIONIC_FORTIFY) */ |