[libc-commits] [libc] [llvm] [libc] Add MSAN unpoison annotations to recv funcs (PR #109844)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Tue Sep 24 11:20:39 PDT 2024


https://github.com/michaelrj-google created https://github.com/llvm/llvm-project/pull/109844

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.


>From b8c7a07271d8cc44dba0ebec83fc718c3980e6be Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Tue, 24 Sep 2024 11:19:22 -0700
Subject: [PATCH] [libc] Add MSAN unpoison annotations to recv funcs

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.
---
 libc/src/sys/socket/linux/CMakeLists.txt                 | 4 ++++
 libc/src/sys/socket/linux/recv.cpp                       | 4 ++++
 libc/src/sys/socket/linux/recvfrom.cpp                   | 4 ++++
 libc/src/sys/socket/linux/recvmsg.cpp                    | 9 +++++++++
 libc/src/sys/socket/linux/socketpair.cpp                 | 6 ++++--
 .../libc/test/src/sys/socket/BUILD.bazel                 | 2 +-
 6 files changed, 26 insertions(+), 3 deletions(-)

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")
 



More information about the libc-commits mailing list