Skip to content

Commit

Permalink
[DFSan] Add custom wrapper for getsockopt.
Browse files Browse the repository at this point in the history
The wrapper clears shadow for optval and optlen when written.

Reviewed By: stephan.yichao.zhao, vitalybuka

Differential Revision: https://reviews.llvm.org/D92961
  • Loading branch information
morehouse committed Dec 9, 2020
1 parent b0d02b6 commit 4eedc2e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions compiler-rt/lib/dfsan/dfsan_custom.cpp
Expand Up @@ -913,6 +913,20 @@ __dfsw_socketpair(int domain, int type, int protocol, int sv[2],
return ret;
}

SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_getsockopt(
int sockfd, int level, int optname, void *optval, socklen_t *optlen,
dfsan_label sockfd_label, dfsan_label level_label,
dfsan_label optname_label, dfsan_label optval_label,
dfsan_label optlen_label, dfsan_label *ret_label) {
int ret = getsockopt(sockfd, level, optname, optval, optlen);
if (ret != -1 && optval && optlen) {
dfsan_set_label(0, optlen, sizeof(*optlen));
dfsan_set_label(0, optval, *optlen);
}
*ret_label = 0;
return ret;
}

// Type of the trampoline function passed to the custom version of
// dfsan_set_write_callback.
typedef void (*write_trampoline_t)(
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/dfsan/done_abilist.txt
Expand Up @@ -194,6 +194,7 @@ fun:get_current_dir_name=custom
fun:gethostname=custom
fun:getrlimit=custom
fun:getrusage=custom
fun:getsockopt=custom
fun:nanosleep=custom
fun:pread=custom
fun:read=custom
Expand Down
22 changes: 22 additions & 0 deletions compiler-rt/test/dfsan/custom.cpp
Expand Up @@ -931,6 +931,27 @@ void test_socketpair() {
ASSERT_READ_ZERO_LABEL(fd, sizeof(fd));
}

void test_getsockopt() {
int sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
assert(sockfd != -1);

int optval[2] = {-1, -1};
socklen_t optlen = sizeof(optval);
dfsan_set_label(i_label, &optval, sizeof(optval));
dfsan_set_label(i_label, &optlen, sizeof(optlen));
int ret = getsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, &optlen);
assert(ret != -1);
assert(optlen == sizeof(int));
assert(optval[0] == 0);
assert(optval[1] == -1);
ASSERT_ZERO_LABEL(ret);
ASSERT_ZERO_LABEL(optlen);
ASSERT_ZERO_LABEL(optval[0]);
ASSERT_LABEL(optval[1], i_label);

close(sockfd);
}

void test_write() {
int fd = open("/dev/null", O_WRONLY);

Expand Down Expand Up @@ -1113,6 +1134,7 @@ int main(void) {
test_getpwuid_r();
test_getrlimit();
test_getrusage();
test_getsockopt();
test_gettimeofday();
test_inet_pton();
test_localtime_r();
Expand Down

0 comments on commit 4eedc2e

Please sign in to comment.