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 | |
| 39 | #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ |
| 40 | __BIONIC_FORTIFY_INLINE __printflike(3, 0) |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame^] | 41 | int vsnprintf(char* const __pass_object_size dest, size_t size, const char* format, va_list ap) |
| 42 | __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 43 | return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap); |
| 44 | } |
| 45 | |
| 46 | __BIONIC_FORTIFY_INLINE __printflike(2, 0) |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame^] | 47 | 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] | 48 | return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap); |
| 49 | } |
| 50 | #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */ |
| 51 | |
| 52 | #if defined(__clang__) |
| 53 | #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ |
| 54 | /* |
| 55 | * Simple case: `format` can't have format specifiers, so we can just compare |
| 56 | * its length to the length of `dest` |
| 57 | */ |
| 58 | __BIONIC_ERROR_FUNCTION_VISIBILITY |
Elliott Hughes | ec6850d | 2017-08-01 08:28:46 -0700 | [diff] [blame] | 59 | int snprintf(char* dest, size_t size, const char* format) |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 60 | __overloadable |
| 61 | __enable_if(__bos(dest) != __BIONIC_FORTIFY_UNKNOWN_SIZE && |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame^] | 62 | __bos(dest) < __builtin_strlen(format), |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 63 | "format string will always overflow destination buffer") |
| 64 | __errorattr("format string will always overflow destination buffer"); |
| 65 | |
| 66 | __BIONIC_FORTIFY_INLINE |
| 67 | __printflike(3, 4) |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame^] | 68 | int snprintf(char* const __pass_object_size dest, size_t size, const char* format, ...) |
| 69 | __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 70 | va_list va; |
| 71 | va_start(va, format); |
| 72 | int result = __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, va); |
| 73 | va_end(va); |
| 74 | return result; |
| 75 | } |
| 76 | |
| 77 | __BIONIC_ERROR_FUNCTION_VISIBILITY |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame^] | 78 | int sprintf(char* dest, const char* format) |
| 79 | __overloadable |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 80 | __enable_if(__bos(dest) != __BIONIC_FORTIFY_UNKNOWN_SIZE && |
| 81 | __bos(dest) < __builtin_strlen(format), |
| 82 | "format string will always overflow destination buffer") |
| 83 | __errorattr("format string will always overflow destination buffer"); |
| 84 | |
| 85 | __BIONIC_FORTIFY_INLINE |
| 86 | __printflike(2, 3) |
Elliott Hughes | ec6850d | 2017-08-01 08:28:46 -0700 | [diff] [blame] | 87 | int sprintf(char* const __pass_object_size dest, const char* format, ...) __overloadable { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 88 | va_list va; |
| 89 | va_start(va, format); |
| 90 | int result = __builtin___vsprintf_chk(dest, 0, __bos(dest), format, va); |
| 91 | va_end(va); |
| 92 | return result; |
| 93 | } |
| 94 | #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */ |
| 95 | |
| 96 | #if __ANDROID_API__ >= __ANDROID_API_N__ |
| 97 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame^] | 98 | size_t fread(void* const __pass_object_size0 buf, size_t size, size_t count, FILE* stream) |
| 99 | __overloadable |
| 100 | __clang_error_if(__unsafe_check_mul_overflow(size, count), |
| 101 | "in call to 'fread', size * count overflows") |
| 102 | __clang_error_if(__bos(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE && size * count > __bos(buf), |
| 103 | "in call to 'fread', size * count is too large for the given buffer") { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 104 | size_t bos = __bos0(buf); |
| 105 | |
| 106 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 107 | return __call_bypassing_fortify(fread)(buf, size, count, stream); |
| 108 | } |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 109 | return __fread_chk(buf, size, count, stream, bos); |
| 110 | } |
| 111 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 112 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | ec6850d | 2017-08-01 08:28:46 -0700 | [diff] [blame] | 113 | 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^] | 114 | __overloadable |
| 115 | __clang_error_if(__unsafe_check_mul_overflow(size, count), |
| 116 | "in call to 'fwrite', size * count overflows") |
| 117 | __clang_error_if(__bos(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE && size * count > __bos(buf), |
| 118 | "in call to 'fwrite', size * count is too large for the given buffer") { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 119 | size_t bos = __bos0(buf); |
| 120 | |
| 121 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 122 | return __call_bypassing_fortify(fwrite)(buf, size, count, stream); |
| 123 | } |
| 124 | |
| 125 | return __fwrite_chk(buf, size, count, stream, bos); |
| 126 | } |
| 127 | #endif /* __ANDROID_API__ >= __ANDROID_API_N__ */ |
| 128 | |
| 129 | #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 130 | __BIONIC_FORTIFY_INLINE |
George Burgess IV | 23dbf82 | 2017-07-31 21:23:34 -0700 | [diff] [blame^] | 131 | char* fgets(char* const __pass_object_size dest, int size, FILE* stream) |
| 132 | __overloadable |
| 133 | __clang_error_if(size < 0, "in call to 'fgets', size should not be negative") |
| 134 | __clang_error_if(size > __bos(dest), |
| 135 | "in call to 'fgets', size is larger than the destination buffer") { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 136 | size_t bos = __bos(dest); |
| 137 | |
| 138 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 139 | return __call_bypassing_fortify(fgets)(dest, size, stream); |
| 140 | } |
| 141 | |
| 142 | return __fgets_chk(dest, size, stream, bos); |
| 143 | } |
| 144 | #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */ |
| 145 | |
| 146 | #else /* defined(__clang__) */ |
| 147 | |
Elliott Hughes | ec6850d | 2017-08-01 08:28:46 -0700 | [diff] [blame] | 148 | size_t __fread_real(void*, size_t, size_t, FILE*) __RENAME(fread); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 149 | __errordecl(__fread_too_big_error, "fread called with size * count bigger than buffer"); |
| 150 | __errordecl(__fread_overflow, "fread called with overflowing size * count"); |
| 151 | |
| 152 | char* __fgets_real(char*, int, FILE*) __RENAME(fgets); |
| 153 | __errordecl(__fgets_too_big_error, "fgets called with size bigger than buffer"); |
| 154 | __errordecl(__fgets_too_small_error, "fgets called with size less than zero"); |
| 155 | |
Elliott Hughes | ec6850d | 2017-08-01 08:28:46 -0700 | [diff] [blame] | 156 | size_t __fwrite_real(const void*, size_t, size_t, FILE*) __RENAME(fwrite); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 157 | __errordecl(__fwrite_too_big_error, "fwrite called with size * count bigger than buffer"); |
| 158 | __errordecl(__fwrite_overflow, "fwrite called with overflowing size * count"); |
| 159 | |
| 160 | |
| 161 | #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ |
| 162 | __BIONIC_FORTIFY_INLINE __printflike(3, 4) |
Elliott Hughes | 3f66e74 | 2017-08-01 13:24:40 -0700 | [diff] [blame] | 163 | int snprintf(char* dest, size_t size, const char* format, ...) { |
| 164 | return __builtin___snprintf_chk(dest, size, 0, __bos(dest), format, __builtin_va_arg_pack()); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | __BIONIC_FORTIFY_INLINE __printflike(2, 3) |
Elliott Hughes | 3f66e74 | 2017-08-01 13:24:40 -0700 | [diff] [blame] | 168 | int sprintf(char* dest, const char* format, ...) { |
| 169 | return __builtin___sprintf_chk(dest, 0, __bos(dest), format, __builtin_va_arg_pack()); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 170 | } |
| 171 | #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */ |
| 172 | |
| 173 | #if __ANDROID_API__ >= __ANDROID_API_N__ |
| 174 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | ec6850d | 2017-08-01 08:28:46 -0700 | [diff] [blame] | 175 | size_t fread(void* buf, size_t size, size_t count, FILE* stream) { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 176 | size_t bos = __bos0(buf); |
| 177 | |
| 178 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 179 | return __fread_real(buf, size, count, stream); |
| 180 | } |
| 181 | |
| 182 | if (__builtin_constant_p(size) && __builtin_constant_p(count)) { |
| 183 | size_t total; |
| 184 | if (__size_mul_overflow(size, count, &total)) { |
| 185 | __fread_overflow(); |
| 186 | } |
| 187 | |
| 188 | if (total > bos) { |
| 189 | __fread_too_big_error(); |
| 190 | } |
| 191 | |
| 192 | return __fread_real(buf, size, count, stream); |
| 193 | } |
| 194 | |
| 195 | return __fread_chk(buf, size, count, stream, bos); |
| 196 | } |
| 197 | |
| 198 | __BIONIC_FORTIFY_INLINE |
Elliott Hughes | ec6850d | 2017-08-01 08:28:46 -0700 | [diff] [blame] | 199 | size_t fwrite(const void* buf, size_t size, size_t count, FILE* stream) { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 200 | size_t bos = __bos0(buf); |
| 201 | |
| 202 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 203 | return __fwrite_real(buf, size, count, stream); |
| 204 | } |
| 205 | |
| 206 | if (__builtin_constant_p(size) && __builtin_constant_p(count)) { |
| 207 | size_t total; |
| 208 | if (__size_mul_overflow(size, count, &total)) { |
| 209 | __fwrite_overflow(); |
| 210 | } |
| 211 | |
| 212 | if (total > bos) { |
| 213 | __fwrite_too_big_error(); |
| 214 | } |
| 215 | |
| 216 | return __fwrite_real(buf, size, count, stream); |
| 217 | } |
| 218 | |
| 219 | return __fwrite_chk(buf, size, count, stream, bos); |
| 220 | } |
| 221 | #endif /* __ANDROID_API__ >= __ANDROID_API_N__ */ |
| 222 | |
| 223 | #if __ANDROID_API__ >= __ANDROID_API_J_MR1__ |
| 224 | __BIONIC_FORTIFY_INLINE |
| 225 | char *fgets(char* dest, int size, FILE* stream) { |
| 226 | size_t bos = __bos(dest); |
| 227 | |
| 228 | // Compiler can prove, at compile time, that the passed in size |
| 229 | // is always negative. Force a compiler error. |
| 230 | if (__builtin_constant_p(size) && (size < 0)) { |
| 231 | __fgets_too_small_error(); |
| 232 | } |
| 233 | |
| 234 | // Compiler doesn't know destination size. Don't call __fgets_chk |
| 235 | if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) { |
| 236 | return __fgets_real(dest, size, stream); |
| 237 | } |
| 238 | |
| 239 | // Compiler can prove, at compile time, that the passed in size |
| 240 | // is always <= the actual object size. Don't call __fgets_chk |
| 241 | if (__builtin_constant_p(size) && (size <= (int) bos)) { |
| 242 | return __fgets_real(dest, size, stream); |
| 243 | } |
| 244 | |
| 245 | // Compiler can prove, at compile time, that the passed in size |
| 246 | // is always > the actual object size. Force a compiler error. |
| 247 | if (__builtin_constant_p(size) && (size > (int) bos)) { |
| 248 | __fgets_too_big_error(); |
| 249 | } |
| 250 | |
| 251 | return __fgets_chk(dest, size, stream, bos); |
| 252 | } |
| 253 | #endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */ |
| 254 | |
| 255 | #endif /* defined(__clang__) */ |
| 256 | #endif /* defined(__BIONIC_FORTIFY) */ |