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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
49 changes: 49 additions & 0 deletions compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#endif

#include <fcntl.h>
#include <netdb.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/mman.h>
Expand All @@ -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 {
Expand Down Expand Up @@ -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_LT(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");
Expand Down
Loading