[libc] Add MSAN unpoison annotations to recv funcs (#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.
This commit is contained in:
Michael Jones 2024-09-24 14:54:02 -07:00 committed by GitHub
parent 5d88fd33ee
commit aeb18ebbe0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 26 additions and 3 deletions

View File

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

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

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