Skip to content

[rtsan] Add more socket interceptors #115020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 6, 2024
Merged

Conversation

cjappl
Copy link
Contributor

@cjappl cjappl commented Nov 5, 2024

Adds getaddrinfo, getnameinfo, bind, listen, accept and connect

Adds getaddrinfo, getnameinfo, bind, listen, accept and connect
@llvmbot
Copy link
Member

llvmbot commented Nov 5, 2024

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Chris Apple (cjappl)

Changes

Adds getaddrinfo, getnameinfo, bind, listen, accept and connect


Full diff: https://github.com/llvm/llvm-project/pull/115020.diff

2 Files Affected:

  • (modified) compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp (+46-4)
  • (modified) compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp (+49)
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index 55241972167191..c3fcd4f2da85ce 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -535,6 +535,42 @@ INTERCEPTOR(int, shm_unlink, const char *name) {
 }
 
 // Sockets
+INTERCEPTOR(int, getaddrinfo, const char *node, const char *service,
+            const struct addrinfo *hints, struct addrinfo **res) {
+  __rtsan_notify_intercepted_call("getaddrinfo");
+  return REAL(getaddrinfo)(node, service, hints, res);
+}
+
+INTERCEPTOR(int, getnameinfo, const struct sockaddr *sa, socklen_t salen,
+            char *host, socklen_t hostlen, char *serv, socklen_t servlen,
+            int flags) {
+  __rtsan_notify_intercepted_call("getnameinfo");
+  return REAL(getnameinfo)(sa, salen, host, hostlen, serv, servlen, flags);
+}
+
+INTERCEPTOR(int, bind, int socket, const struct sockaddr *address,
+            socklen_t address_len) {
+  __rtsan_notify_intercepted_call("bind");
+  return REAL(bind)(socket, address, address_len);
+}
+
+INTERCEPTOR(int, listen, int socket, int backlog) {
+  __rtsan_notify_intercepted_call("listen");
+  return REAL(listen)(socket, backlog);
+}
+
+INTERCEPTOR(int, accept, int socket, struct sockaddr *address,
+            socklen_t *address_len) {
+  __rtsan_notify_intercepted_call("accept");
+  return REAL(accept)(socket, address, address_len);
+}
+
+INTERCEPTOR(int, connect, int socket, const struct sockaddr *address,
+            socklen_t address_len) {
+  __rtsan_notify_intercepted_call("connect");
+  return REAL(connect)(socket, address, address_len);
+}
+
 INTERCEPTOR(int, socket, int domain, int type, int protocol) {
   __rtsan_notify_intercepted_call("socket");
   return REAL(socket)(domain, type, protocol);
@@ -648,14 +684,20 @@ void __rtsan::InitializeInterceptors() {
   INTERCEPT_FUNCTION(usleep);
   INTERCEPT_FUNCTION(nanosleep);
 
-  INTERCEPT_FUNCTION(socket);
+  INTERCEPT_FUNCTION(accept);
+  INTERCEPT_FUNCTION(bind);
+  INTERCEPT_FUNCTION(connect);
+  INTERCEPT_FUNCTION(getaddrinfo);
+  INTERCEPT_FUNCTION(getnameinfo);
+  INTERCEPT_FUNCTION(listen);
+  INTERCEPT_FUNCTION(recv);
+  INTERCEPT_FUNCTION(recvfrom);
+  INTERCEPT_FUNCTION(recvmsg);
   INTERCEPT_FUNCTION(send);
   INTERCEPT_FUNCTION(sendmsg);
   INTERCEPT_FUNCTION(sendto);
-  INTERCEPT_FUNCTION(recv);
-  INTERCEPT_FUNCTION(recvmsg);
-  INTERCEPT_FUNCTION(recvfrom);
   INTERCEPT_FUNCTION(shutdown);
+  INTERCEPT_FUNCTION(socket);
 }
 
 #endif // SANITIZER_POSIX
diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
index 1850f4fa30f9cb..c7907d155cf615 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -29,6 +29,7 @@
 #endif
 
 #include <fcntl.h>
+#include <netdb.h>
 #include <pthread.h>
 #include <stdio.h>
 #include <sys/mman.h>
@@ -46,6 +47,11 @@ using namespace testing;
 using namespace rtsan_testing;
 using namespace std::chrono_literals;
 
+// NOTE: In the socket tests we pass in bad info to the calls to ensure they
+//       fail which is why we EXPECT_NE 0 for their return codes.
+//       We just care that the call is intercepted
+const int kNotASocketFd = 0;
+
 void *FakeThreadEntryPoint(void *) { return nullptr; }
 
 class RtsanFileTest : public ::testing::Test {
@@ -676,6 +682,49 @@ TEST_F(PthreadRwlockTest, PthreadRwlockWrlockSurvivesWhenNonRealtime) {
 /*
     Sockets
 */
+TEST(TestRtsanInterceptors, GetAddrInfoDiesWhenRealtime) {
+  auto Func = []() {
+    addrinfo *info{};
+    getaddrinfo("localhost", "http", nullptr, &info);
+  };
+  ExpectRealtimeDeath(Func, "getaddrinfo");
+  ExpectNonRealtimeSurvival(Func);
+}
+
+TEST(TestRtsanInterceptors, GetNameInfoDiesWhenRealtime) {
+  auto Func = []() {
+    char host[NI_MAXHOST];
+    char serv[NI_MAXSERV];
+    getnameinfo(nullptr, 0, host, NI_MAXHOST, serv, NI_MAXSERV, 0);
+  };
+  ExpectRealtimeDeath(Func, "getnameinfo");
+  ExpectNonRealtimeSurvival(Func);
+}
+
+TEST(TestRtsanInterceptors, BindingASocketDiesWhenRealtime) {
+  auto Func = []() { EXPECT_NE(bind(kNotASocketFd, nullptr, 0), 0); };
+  ExpectRealtimeDeath(Func, "bind");
+  ExpectNonRealtimeSurvival(Func);
+}
+
+TEST(TestRtsanInterceptors, ListeningOnASocketDiesWhenRealtime) {
+  auto Func = []() { EXPECT_NE(listen(kNotASocketFd, 0), 0); };
+  ExpectRealtimeDeath(Func, "listen");
+  ExpectNonRealtimeSurvival(Func);
+}
+
+TEST(TestRtsanInterceptors, AcceptingASocketDiesWhenRealtime) {
+  auto Func = []() { EXPECT_NE(accept(kNotASocketFd, nullptr, nullptr), 0); };
+  ExpectRealtimeDeath(Func, "accept");
+  ExpectNonRealtimeSurvival(Func);
+}
+
+TEST(TestRtsanInterceptors, ConnectingASocketDiesWhenRealtime) {
+  auto Func = []() { EXPECT_NE(connect(kNotASocketFd, nullptr, 0), 0); };
+  ExpectRealtimeDeath(Func, "connect");
+  ExpectNonRealtimeSurvival(Func);
+}
+
 TEST(TestRtsanInterceptors, OpeningASocketDiesWhenRealtime) {
   auto Func = []() { socket(PF_INET, SOCK_STREAM, 0); };
   ExpectRealtimeDeath(Func, "socket");

@cjappl cjappl merged commit 8699f30 into llvm:main Nov 6, 2024
7 checks passed
@cjappl cjappl deleted the add_more_intercepted branch November 6, 2024 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants