OSDN Git Service

FORTIFY_SOURCE: add fgets support.
authorNick Kralevich <nnk@google.com>
Tue, 3 Jul 2012 18:45:31 +0000 (11:45 -0700)
committerNick Kralevich <nnk@google.com>
Mon, 9 Jul 2012 16:57:18 +0000 (09:57 -0700)
Change-Id: I8c3410a90c71a3336c4ac8581618fa9330edf5e3

libc/Android.mk
libc/include/stdio.h
libc/stdio/__fgets_chk.c [new file with mode: 0644]

index 06d36fe..6b1bba4 100644 (file)
@@ -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 \
index 18b19bf..c12ddb8 100644 (file)
@@ -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 (file)
index 0000000..19123b9
--- /dev/null
@@ -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 <stdio.h>
+#include <stdlib.h>
+#include <private/logd.h>
+
+/*
+ * __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);
+}