summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Kralevich <nnk@google.com>2013-09-27 09:11:00 -0700
committerAndroid Git Automerger <android-git-automerger@android.com>2013-09-27 09:11:00 -0700
commit9a622f874ceb90431623af140ca9a78eadc96dcb (patch)
tree007155429a0dac86cf2561b440c3af956f169561
parentb1542b09d95556bd2d7d91c2deba6cf753fdbbb2 (diff)
parentcc362291362f8183431eccb19267c8a625f36006 (diff)
downloadbionic-9a622f874ceb90431623af140ca9a78eadc96dcb.zip
bionic-9a622f874ceb90431623af140ca9a78eadc96dcb.tar.gz
bionic-9a622f874ceb90431623af140ca9a78eadc96dcb.tar.bz2
am cc362291: am 8427b745: Merge "libc: fortify recvfrom()"
* commit 'cc362291362f8183431eccb19267c8a625f36006': libc: fortify recvfrom()
-rw-r--r--libc/Android.mk1
-rw-r--r--libc/bionic/__recvfrom_chk.cpp44
-rw-r--r--libc/include/sys/socket.h34
-rw-r--r--tests/fortify_test.cpp8
4 files changed, 87 insertions, 0 deletions
diff --git a/libc/Android.mk b/libc/Android.mk
index fc6db67..ad45601 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -187,6 +187,7 @@ libc_common_src_files += \
bionic/__memcpy_chk.cpp \
bionic/__memmove_chk.cpp \
bionic/__memset_chk.cpp \
+ bionic/__recvfrom_chk.cpp \
bionic/__strcat_chk.cpp \
bionic/__strchr_chk.cpp \
bionic/__strcpy_chk.cpp \
diff --git a/libc/bionic/__recvfrom_chk.cpp b/libc/bionic/__recvfrom_chk.cpp
new file mode 100644
index 0000000..10a4263
--- /dev/null
+++ b/libc/bionic/__recvfrom_chk.cpp
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2013 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.
+ */
+
+#undef _FORTIFY_SOURCE
+
+#include <stddef.h>
+#include <sys/socket.h>
+#include "libc_logging.h"
+
+extern "C"
+ssize_t __recvfrom_chk(int socket, void* buf, size_t len, size_t buflen, unsigned int flags,
+ const struct sockaddr* src_addr, socklen_t* addrlen)
+{
+ if (__predict_false(len > buflen)) {
+ __fortify_chk_fail("recvfrom overflow", 0);
+ }
+
+ return recvfrom(socket, buf, len, flags, src_addr, addrlen);
+}
diff --git a/libc/include/sys/socket.h b/libc/include/sys/socket.h
index 17ba0a1..ed7c4a0 100644
--- a/libc/include/sys/socket.h
+++ b/libc/include/sys/socket.h
@@ -293,6 +293,40 @@ extern ssize_t recv(int, void *, size_t, unsigned int);
__socketcall ssize_t sendto(int, const void *, size_t, int, const struct sockaddr *, socklen_t);
__socketcall ssize_t recvfrom(int, void *, size_t, unsigned int, const struct sockaddr *, socklen_t *);
+#if defined(__BIONIC_FORTIFY)
+__errordecl(__recvfrom_error, "recvfrom called with size bigger than buffer");
+extern ssize_t __recvfrom_chk(int, void*, size_t, size_t, unsigned int, const struct sockaddr*, socklen_t *);
+extern ssize_t __recvfrom_real(int, void *, size_t, unsigned int, const struct sockaddr*, socklen_t*)
+ __asm__(__USER_LABEL_PREFIX__ "recvfrom");
+
+__BIONIC_FORTIFY_INLINE
+ssize_t recvfrom(int fd, void* buf, size_t len, unsigned int flags, const struct sockaddr* src_addr, socklen_t* addrlen) {
+ size_t bos = __bos0(buf);
+
+#if !defined(__clang__)
+ if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
+ return __recvfrom_real(fd, buf, len, flags, src_addr, addrlen);
+ }
+
+ if (__builtin_constant_p(len) && (len <= bos)) {
+ return __recvfrom_real(fd, buf, len, flags, src_addr, addrlen);
+ }
+
+ if (__builtin_constant_p(len) && (len > bos)) {
+ __recvfrom_error();
+ }
+#endif
+
+ return __recvfrom_chk(fd, buf, len, bos, flags, src_addr, addrlen);
+}
+
+__BIONIC_FORTIFY_INLINE
+ssize_t recv(int socket, void *buf, size_t buflen, unsigned int flags) {
+ return recvfrom(socket, buf, buflen, flags, NULL, 0);
+}
+
+#endif /* __BIONIC_FORTIFY */
+
#undef __socketcall
__END_DECLS
diff --git a/tests/fortify_test.cpp b/tests/fortify_test.cpp
index 5ec15b8..4ea868b 100644
--- a/tests/fortify_test.cpp
+++ b/tests/fortify_test.cpp
@@ -19,6 +19,7 @@
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/socket.h>
// We have to say "DeathTest" here so gtest knows to run this test (which exits)
// in its own process. Unfortunately, the C preprocessor doesn't give us an
@@ -525,6 +526,13 @@ TEST(DEATHTEST, umask_fortified) {
ASSERT_EXIT(umask(mask), testing::KilledBySignal(SIGABRT), "");
}
+TEST(DEATHTEST, recv_fortified) {
+ ::testing::FLAGS_gtest_death_test_style = "threadsafe";
+ size_t data_len = atoi("11"); // suppress compiler optimizations
+ char buf[10];
+ ASSERT_EXIT(recv(0, buf, data_len, 0), testing::KilledBySignal(SIGABRT), "");
+}
+
extern "C" char* __strncat_chk(char*, const char*, size_t, size_t);
extern "C" char* __strcat_chk(char*, const char*, size_t);