Skip to content

Commit 4eedc2e

Browse files
committed
[DFSan] Add custom wrapper for getsockopt.
The wrapper clears shadow for optval and optlen when written. Reviewed By: stephan.yichao.zhao, vitalybuka Differential Revision: https://reviews.llvm.org/D92961
1 parent b0d02b6 commit 4eedc2e

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

compiler-rt/lib/dfsan/dfsan_custom.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,20 @@ __dfsw_socketpair(int domain, int type, int protocol, int sv[2],
913913
return ret;
914914
}
915915

916+
SANITIZER_INTERFACE_ATTRIBUTE int __dfsw_getsockopt(
917+
int sockfd, int level, int optname, void *optval, socklen_t *optlen,
918+
dfsan_label sockfd_label, dfsan_label level_label,
919+
dfsan_label optname_label, dfsan_label optval_label,
920+
dfsan_label optlen_label, dfsan_label *ret_label) {
921+
int ret = getsockopt(sockfd, level, optname, optval, optlen);
922+
if (ret != -1 && optval && optlen) {
923+
dfsan_set_label(0, optlen, sizeof(*optlen));
924+
dfsan_set_label(0, optval, *optlen);
925+
}
926+
*ret_label = 0;
927+
return ret;
928+
}
929+
916930
// Type of the trampoline function passed to the custom version of
917931
// dfsan_set_write_callback.
918932
typedef void (*write_trampoline_t)(

compiler-rt/lib/dfsan/done_abilist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ fun:get_current_dir_name=custom
194194
fun:gethostname=custom
195195
fun:getrlimit=custom
196196
fun:getrusage=custom
197+
fun:getsockopt=custom
197198
fun:nanosleep=custom
198199
fun:pread=custom
199200
fun:read=custom

compiler-rt/test/dfsan/custom.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,27 @@ void test_socketpair() {
931931
ASSERT_READ_ZERO_LABEL(fd, sizeof(fd));
932932
}
933933

934+
void test_getsockopt() {
935+
int sockfd = socket(AF_UNIX, SOCK_DGRAM, 0);
936+
assert(sockfd != -1);
937+
938+
int optval[2] = {-1, -1};
939+
socklen_t optlen = sizeof(optval);
940+
dfsan_set_label(i_label, &optval, sizeof(optval));
941+
dfsan_set_label(i_label, &optlen, sizeof(optlen));
942+
int ret = getsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, &optlen);
943+
assert(ret != -1);
944+
assert(optlen == sizeof(int));
945+
assert(optval[0] == 0);
946+
assert(optval[1] == -1);
947+
ASSERT_ZERO_LABEL(ret);
948+
ASSERT_ZERO_LABEL(optlen);
949+
ASSERT_ZERO_LABEL(optval[0]);
950+
ASSERT_LABEL(optval[1], i_label);
951+
952+
close(sockfd);
953+
}
954+
934955
void test_write() {
935956
int fd = open("/dev/null", O_WRONLY);
936957

@@ -1113,6 +1134,7 @@ int main(void) {
11131134
test_getpwuid_r();
11141135
test_getrlimit();
11151136
test_getrusage();
1137+
test_getsockopt();
11161138
test_gettimeofday();
11171139
test_inet_pton();
11181140
test_localtime_r();

0 commit comments

Comments
 (0)