-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Add MSAN unpoison annotations to recv funcs #109844
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
Anywhere a struct is returned from the kernel, we need to explicitly unpoison it for MSAN. This patch does that for the recv, recvfrom, recvmsg, and socketpair functions.
@llvm/pr-subscribers-libc Author: Michael Jones (michaelrj-google) ChangesAnywhere a struct is returned from the kernel, we need to explicitly Full diff: https://github.com/llvm/llvm-project/pull/109844.diff 6 Files Affected:
diff --git a/libc/src/sys/socket/linux/CMakeLists.txt b/libc/src/sys/socket/linux/CMakeLists.txt
index f21679b5f8d3ca..e1226aaad381fe 100644
--- a/libc/src/sys/socket/linux/CMakeLists.txt
+++ b/libc/src/sys/socket/linux/CMakeLists.txt
@@ -33,6 +33,7 @@ add_entrypoint_object(
DEPENDS
libc.include.sys_syscall
libc.include.sys_socket
+ libc.src.__support.macros.sanitizer
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
@@ -87,6 +88,7 @@ add_entrypoint_object(
libc.include.sys_syscall
libc.hdr.types.struct_sockaddr
libc.hdr.types.socklen_t
+ libc.src.__support.macros.sanitizer
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
@@ -101,6 +103,7 @@ add_entrypoint_object(
libc.include.sys_syscall
libc.hdr.types.struct_sockaddr
libc.hdr.types.socklen_t
+ libc.src.__support.macros.sanitizer
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
@@ -114,6 +117,7 @@ add_entrypoint_object(
DEPENDS
libc.include.sys_syscall
libc.hdr.types.struct_msghdr
+ libc.src.__support.macros.sanitizer
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)
diff --git a/libc/src/sys/socket/linux/recv.cpp b/libc/src/sys/socket/linux/recv.cpp
index 96acf449dc4bfd..55a766aec0e77f 100644
--- a/libc/src/sys/socket/linux/recv.cpp
+++ b/libc/src/sys/socket/linux/recv.cpp
@@ -13,6 +13,7 @@
#include "hdr/types/struct_sockaddr.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
+#include "src/__support/macros/sanitizer.h"
#include "src/errno/libc_errno.h"
#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.
@@ -41,6 +42,9 @@ LLVM_LIBC_FUNCTION(ssize_t, recv,
libc_errno = static_cast<int>(-ret);
return -1;
}
+
+ MSAN_UNPOISON(buf, ret);
+
return ret;
}
diff --git a/libc/src/sys/socket/linux/recvfrom.cpp b/libc/src/sys/socket/linux/recvfrom.cpp
index 17489a99c922dc..990e58da3c1b64 100644
--- a/libc/src/sys/socket/linux/recvfrom.cpp
+++ b/libc/src/sys/socket/linux/recvfrom.cpp
@@ -13,6 +13,7 @@
#include "hdr/types/struct_sockaddr.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
+#include "src/__support/macros/sanitizer.h"
#include "src/errno/libc_errno.h"
#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.
@@ -43,6 +44,9 @@ LLVM_LIBC_FUNCTION(ssize_t, recvfrom,
libc_errno = static_cast<int>(-ret);
return -1;
}
+
+ MSAN_UNPOISON(buf, ret);
+
return ret;
}
diff --git a/libc/src/sys/socket/linux/recvmsg.cpp b/libc/src/sys/socket/linux/recvmsg.cpp
index 60045d6a80f535..f44e5800d817f2 100644
--- a/libc/src/sys/socket/linux/recvmsg.cpp
+++ b/libc/src/sys/socket/linux/recvmsg.cpp
@@ -12,6 +12,7 @@
#include "hdr/types/struct_msghdr.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
+#include "src/__support/macros/sanitizer.h"
#include "src/errno/libc_errno.h"
#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.
@@ -36,6 +37,14 @@ LLVM_LIBC_FUNCTION(ssize_t, recvmsg,
libc_errno = static_cast<int>(-ret);
return -1;
}
+
+ // Unpoison the msghdr, as well as all its components.
+ MSAN_UNPOISON(msg->msg_name, msg->msg_namelen);
+ for (size_t i = 0; i < msg->msg_iovlen; ++i) {
+ MSAN_UNPOISON(msg->msg_iov->iov_base, msg->msg_iov->iov_len);
+ }
+ MSAN_UNPOISON(msg->msg_control, msg->msg_controllen);
+
return ret;
}
diff --git a/libc/src/sys/socket/linux/socketpair.cpp b/libc/src/sys/socket/linux/socketpair.cpp
index d459a74433906d..60612ac04d6138 100644
--- a/libc/src/sys/socket/linux/socketpair.cpp
+++ b/libc/src/sys/socket/linux/socketpair.cpp
@@ -10,10 +10,9 @@
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
-
#include "src/__support/macros/config.h"
+#include "src/__support/macros/sanitizer.h"
#include "src/errno/libc_errno.h"
-
#include <linux/net.h> // For SYS_SOCKET socketcall number.
#include <sys/syscall.h> // For syscall numbers.
@@ -37,6 +36,9 @@ LLVM_LIBC_FUNCTION(int, socketpair,
libc_errno = -ret;
return -1;
}
+
+ MSAN_UNPOISON(sv, sizeof(int) * 2);
+
return ret;
}
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel
index 865f5e6f496179..f7bce45d07da6d 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel
@@ -2,7 +2,7 @@
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-# Tests for LLVM libc string.h functions.
+# Tests for LLVM libc socket.h functions.
load("//libc/test:libc_test_rules.bzl", "libc_test")
|
lntue
approved these changes
Sep 24, 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.
Anywhere a struct is returned from the kernel, we need to explicitly
unpoison it for MSAN. This patch does that for the recv, recvfrom,
recvmsg, and socketpair functions.