-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Adds getaddrinfo, getnameinfo, bind, listen, accept and connect
@llvm/pr-subscribers-compiler-rt-sanitizer Author: Chris Apple (cjappl) ChangesAdds getaddrinfo, getnameinfo, bind, listen, accept and connect Full diff: https://github.com/llvm/llvm-project/pull/115020.diff 2 Files Affected:
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");
|
davidtrevelyan
approved these changes
Nov 5, 2024
fmayer
approved these changes
Nov 5, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds getaddrinfo, getnameinfo, bind, listen, accept and connect