From: Nick Kralevich Date: Tue, 3 Jul 2012 18:45:31 +0000 (-0700) Subject: FORTIFY_SOURCE: add fgets support. X-Git-Tag: android-x86-4.4-r1~749^2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=965dbc6405aa2c3170270cfc53a8d4416444fddb;p=android-x86%2Fbionic.git FORTIFY_SOURCE: add fgets support. Change-Id: I8c3410a90c71a3336c4ac8581618fa9330edf5e3 --- diff --git a/libc/Android.mk b/libc/Android.mk index 06d36fefa..6b1bba464 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -140,6 +140,7 @@ libc_common_src_files := \ stdio/vsscanf.c \ stdio/wbuf.c \ stdio/wsetup.c \ + stdio/__fgets_chk.c \ stdio/__snprintf_chk.c \ stdio/__sprintf_chk.c \ stdio/__vsnprintf_chk.c \ diff --git a/libc/include/stdio.h b/libc/include/stdio.h index 18b19bf7c..c12ddb812 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -527,6 +527,45 @@ int sprintf(char *dest, const char *format, ...) # endif /* !defined(__clang__) */ +extern char *__fgets_real(char *, int, FILE *) + __asm__(__USER_LABEL_PREFIX__ "fgets"); +extern void __fgets_too_big_error() + __attribute__((__error__("fgets called with size bigger than buffer"))); +extern void __fgets_too_small_error() + __attribute__((__error__("fgets called with size less than zero"))); +extern char *__fgets_chk(char *, int, FILE *, size_t); + +__BIONIC_FORTIFY_INLINE +char *fgets(char *dest, int size, FILE *stream) +{ + size_t bos = __builtin_object_size(dest, 0); + + // Compiler can prove, at compile time, that the passed in size + // is always negative. Force a compiler error. + if (__builtin_constant_p(size) && (size < 0)) { + __fgets_too_small_error(); + } + + // Compiler doesn't know destination size. Don't call __fgets_chk + if (bos == (size_t) -1) { + return __fgets_real(dest, size, stream); + } + + // Compiler can prove, at compile time, that the passed in size + // is always <= the actual object size. Don't call __fgets_chk + if (__builtin_constant_p(size) && (size <= bos)) { + return __fgets_real(dest, size, stream); + } + + // Compiler can prove, at compile time, that the passed in size + // is always > the actual object size. Force a compiler error. + if (__builtin_constant_p(size) && (size > bos)) { + __fgets_too_big_error(); + } + + return __fgets_chk(dest, size, stream, bos); +} + #endif /* defined(__BIONIC_FORTIFY_INLINE) */ #endif /* _STDIO_H_ */ diff --git a/libc/stdio/__fgets_chk.c b/libc/stdio/__fgets_chk.c new file mode 100644 index 000000000..19123b970 --- /dev/null +++ b/libc/stdio/__fgets_chk.c @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include +#include + +/* + * __fgets_chk. Called in place of fgets() when we know the + * size of the buffer we're writing into. + * + * See + * http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html + * for details. + * + * This fgets check is called if _FORTIFY_SOURCE is defined and + * greater than 0. + */ +char *__fgets_chk(char *dest, int supplied_size, + FILE *stream, size_t dest_len_from_compiler) +{ + if (supplied_size < 0) { + __libc_android_log_print(ANDROID_LOG_FATAL, "libc", + "*** fgets buffer size less than 0 ***\n"); + abort(); + } + + if (((size_t) supplied_size) > dest_len_from_compiler) { + __libc_android_log_print(ANDROID_LOG_FATAL, "libc", + "*** fgets buffer overflow detected ***\n"); + abort(); + } + + return fgets(dest, supplied_size, stream); +}