Skip to content

Commit aeb18eb

Browse files
[libc] Add MSAN unpoison annotations to recv funcs (llvm#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.
1 parent 5d88fd3 commit aeb18eb

File tree

6 files changed

+26
-3
lines changed

6 files changed

+26
-3
lines changed

libc/src/sys/socket/linux/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ add_entrypoint_object(
3333
DEPENDS
3434
libc.include.sys_syscall
3535
libc.include.sys_socket
36+
libc.src.__support.macros.sanitizer
3637
libc.src.__support.OSUtil.osutil
3738
libc.src.errno.errno
3839
)
@@ -87,6 +88,7 @@ add_entrypoint_object(
8788
libc.include.sys_syscall
8889
libc.hdr.types.struct_sockaddr
8990
libc.hdr.types.socklen_t
91+
libc.src.__support.macros.sanitizer
9092
libc.src.__support.OSUtil.osutil
9193
libc.src.errno.errno
9294
)
@@ -101,6 +103,7 @@ add_entrypoint_object(
101103
libc.include.sys_syscall
102104
libc.hdr.types.struct_sockaddr
103105
libc.hdr.types.socklen_t
106+
libc.src.__support.macros.sanitizer
104107
libc.src.__support.OSUtil.osutil
105108
libc.src.errno.errno
106109
)
@@ -114,6 +117,7 @@ add_entrypoint_object(
114117
DEPENDS
115118
libc.include.sys_syscall
116119
libc.hdr.types.struct_msghdr
120+
libc.src.__support.macros.sanitizer
117121
libc.src.__support.OSUtil.osutil
118122
libc.src.errno.errno
119123
)

libc/src/sys/socket/linux/recv.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "hdr/types/struct_sockaddr.h"
1414
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1515
#include "src/__support/common.h"
16+
#include "src/__support/macros/sanitizer.h"
1617
#include "src/errno/libc_errno.h"
1718
#include <linux/net.h> // For SYS_SOCKET socketcall number.
1819
#include <sys/syscall.h> // For syscall numbers.
@@ -41,6 +42,9 @@ LLVM_LIBC_FUNCTION(ssize_t, recv,
4142
libc_errno = static_cast<int>(-ret);
4243
return -1;
4344
}
45+
46+
MSAN_UNPOISON(buf, ret);
47+
4448
return ret;
4549
}
4650

libc/src/sys/socket/linux/recvfrom.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "hdr/types/struct_sockaddr.h"
1414
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1515
#include "src/__support/common.h"
16+
#include "src/__support/macros/sanitizer.h"
1617
#include "src/errno/libc_errno.h"
1718
#include <linux/net.h> // For SYS_SOCKET socketcall number.
1819
#include <sys/syscall.h> // For syscall numbers.
@@ -43,6 +44,9 @@ LLVM_LIBC_FUNCTION(ssize_t, recvfrom,
4344
libc_errno = static_cast<int>(-ret);
4445
return -1;
4546
}
47+
48+
MSAN_UNPOISON(buf, ret);
49+
4650
return ret;
4751
}
4852

libc/src/sys/socket/linux/recvmsg.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "hdr/types/struct_msghdr.h"
1313
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1414
#include "src/__support/common.h"
15+
#include "src/__support/macros/sanitizer.h"
1516
#include "src/errno/libc_errno.h"
1617
#include <linux/net.h> // For SYS_SOCKET socketcall number.
1718
#include <sys/syscall.h> // For syscall numbers.
@@ -36,6 +37,14 @@ LLVM_LIBC_FUNCTION(ssize_t, recvmsg,
3637
libc_errno = static_cast<int>(-ret);
3738
return -1;
3839
}
40+
41+
// Unpoison the msghdr, as well as all its components.
42+
MSAN_UNPOISON(msg->msg_name, msg->msg_namelen);
43+
for (size_t i = 0; i < msg->msg_iovlen; ++i) {
44+
MSAN_UNPOISON(msg->msg_iov->iov_base, msg->msg_iov->iov_len);
45+
}
46+
MSAN_UNPOISON(msg->msg_control, msg->msg_controllen);
47+
3948
return ret;
4049
}
4150

libc/src/sys/socket/linux/socketpair.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010

1111
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1212
#include "src/__support/common.h"
13-
1413
#include "src/__support/macros/config.h"
14+
#include "src/__support/macros/sanitizer.h"
1515
#include "src/errno/libc_errno.h"
16-
1716
#include <linux/net.h> // For SYS_SOCKET socketcall number.
1817
#include <sys/syscall.h> // For syscall numbers.
1918

@@ -37,6 +36,9 @@ LLVM_LIBC_FUNCTION(int, socketpair,
3736
libc_errno = -ret;
3837
return -1;
3938
}
39+
40+
MSAN_UNPOISON(sv, sizeof(int) * 2);
41+
4042
return ret;
4143
}
4244

utils/bazel/llvm-project-overlay/libc/test/src/sys/socket/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# See https://llvm.org/LICENSE.txt for license information.
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

5-
# Tests for LLVM libc string.h functions.
5+
# Tests for LLVM libc socket.h functions.
66

77
load("//libc/test:libc_test_rules.bzl", "libc_test")
88

0 commit comments

Comments
 (0)