xref: /llvm-project/compiler-rt/test/msan/Linux/cmsghdr.cpp (revision d21b3d346af2f6189638d853182e389555e7ccb9)
1*d21b3d34SFangrui Song // RUN: %clangxx_msan %s -std=c++11 -DSENDMSG -DPOISONFD -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=SENDMSG
2*d21b3d34SFangrui Song // RUN: %clangxx_msan %s -std=c++11 -DSENDMSG -DPOISONCRED -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=SENDMSG
3*d21b3d34SFangrui Song // RUN: %clangxx_msan %s -std=c++11 -DSENDMSG -DPOISONLEN -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=SENDMSG
4*d21b3d34SFangrui Song // RUN: %clangxx_msan %s -std=c++11 -DSENDMSG -DPOISONLEVEL -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=SENDMSG
5*d21b3d34SFangrui Song // RUN: %clangxx_msan %s -std=c++11 -DSENDMSG -DPOISONTYPE -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=SENDMSG
6*d21b3d34SFangrui Song // RUN: %clangxx_msan %s -std=c++11 -DSENDMSG -DPOISONLEN2 -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=SENDMSG
7*d21b3d34SFangrui Song // RUN: %clangxx_msan %s -std=c++11 -DSENDMSG -DPOISONLEVEL2 -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=SENDMSG
8*d21b3d34SFangrui Song // RUN: %clangxx_msan %s -std=c++11 -DSENDMSG -DPOISONTYPE2 -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=SENDMSG
9*d21b3d34SFangrui Song // RUN: %clangxx_msan %s -std=c++11 -DSENDMSG -o %t && %run %t 2>&1 | FileCheck %s --check-prefix=NEGATIVE
10*d21b3d34SFangrui Song 
11*d21b3d34SFangrui Song // UNSUPPORTED: android
12*d21b3d34SFangrui Song 
13*d21b3d34SFangrui Song #include <assert.h>
14*d21b3d34SFangrui Song #include <stdio.h>
15*d21b3d34SFangrui Song #include <unistd.h>
16*d21b3d34SFangrui Song #include <stdlib.h>
17*d21b3d34SFangrui Song #include <string.h>
18*d21b3d34SFangrui Song #include <errno.h>
19*d21b3d34SFangrui Song #include <netdb.h>
20*d21b3d34SFangrui Song #include <sys/types.h>
21*d21b3d34SFangrui Song #include <sys/socket.h>
22*d21b3d34SFangrui Song #include <sys/un.h>
23*d21b3d34SFangrui Song #include <sanitizer/msan_interface.h>
24*d21b3d34SFangrui Song 
25*d21b3d34SFangrui Song const int kBufSize = 10;
26*d21b3d34SFangrui Song 
main()27*d21b3d34SFangrui Song int main() {
28*d21b3d34SFangrui Song   int ret;
29*d21b3d34SFangrui Song   char buf[kBufSize] = {0};
30*d21b3d34SFangrui Song   pthread_t client_thread;
31*d21b3d34SFangrui Song   struct sockaddr_un serveraddr;
32*d21b3d34SFangrui Song 
33*d21b3d34SFangrui Song   int sock[2];
34*d21b3d34SFangrui Song   ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
35*d21b3d34SFangrui Song   assert(ret == 0);
36*d21b3d34SFangrui Song 
37*d21b3d34SFangrui Song   int sockfd = sock[0];
38*d21b3d34SFangrui Song 
39*d21b3d34SFangrui Song   struct iovec iov[] = {{buf, 10}};
40*d21b3d34SFangrui Song   struct msghdr msg = {0};
41*d21b3d34SFangrui Song   msg.msg_iov = iov;
42*d21b3d34SFangrui Song   msg.msg_iovlen = 1;
43*d21b3d34SFangrui Song   msg.msg_flags = 0;
44*d21b3d34SFangrui Song 
45*d21b3d34SFangrui Song   static const int kNumFds = 3;
46*d21b3d34SFangrui Song   char controlbuf[CMSG_SPACE(kNumFds * sizeof(int)) +
47*d21b3d34SFangrui Song                   CMSG_SPACE(sizeof(struct ucred))];
48*d21b3d34SFangrui Song   msg.msg_control = &controlbuf;
49*d21b3d34SFangrui Song   msg.msg_controllen = sizeof(controlbuf);
50*d21b3d34SFangrui Song 
51*d21b3d34SFangrui Song   struct cmsghdr *cmsg = (struct cmsghdr *)&controlbuf;
52*d21b3d34SFangrui Song   assert(cmsg);
53*d21b3d34SFangrui Song   int myfds[kNumFds];
54*d21b3d34SFangrui Song   for (int &fd : myfds)
55*d21b3d34SFangrui Song     fd = sockfd;
56*d21b3d34SFangrui Song #ifdef POISONFD
57*d21b3d34SFangrui Song   __msan_poison(&myfds[1], sizeof(int));
58*d21b3d34SFangrui Song #endif
59*d21b3d34SFangrui Song   cmsg->cmsg_level = SOL_SOCKET;
60*d21b3d34SFangrui Song   cmsg->cmsg_type = SCM_RIGHTS;
61*d21b3d34SFangrui Song   cmsg->cmsg_len = CMSG_LEN(kNumFds * sizeof(int));
62*d21b3d34SFangrui Song   memcpy(CMSG_DATA(cmsg), myfds, kNumFds * sizeof(int));
63*d21b3d34SFangrui Song #ifdef POISONLEVEL
64*d21b3d34SFangrui Song   __msan_poison(&cmsg->cmsg_level, sizeof(cmsg->cmsg_level));
65*d21b3d34SFangrui Song #endif
66*d21b3d34SFangrui Song #ifdef POISONTYPE
67*d21b3d34SFangrui Song   __msan_poison(&cmsg->cmsg_type, sizeof(cmsg->cmsg_type));
68*d21b3d34SFangrui Song #endif
69*d21b3d34SFangrui Song #ifdef POISONLEN
70*d21b3d34SFangrui Song   __msan_poison(&cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
71*d21b3d34SFangrui Song #endif
72*d21b3d34SFangrui Song 
73*d21b3d34SFangrui Song   cmsg = (struct cmsghdr *)(&controlbuf[CMSG_SPACE(kNumFds * sizeof(int))]);
74*d21b3d34SFangrui Song   assert(cmsg);
75*d21b3d34SFangrui Song   struct ucred cred = {getpid(), getuid(), getgid()};
76*d21b3d34SFangrui Song #ifdef POISONCRED
77*d21b3d34SFangrui Song   __msan_poison(&cred.uid, sizeof(cred.uid));
78*d21b3d34SFangrui Song #endif
79*d21b3d34SFangrui Song   cmsg->cmsg_level = SOL_SOCKET;
80*d21b3d34SFangrui Song   cmsg->cmsg_type = SCM_CREDENTIALS;
81*d21b3d34SFangrui Song   cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
82*d21b3d34SFangrui Song   memcpy(CMSG_DATA(cmsg), &cred, sizeof(struct ucred));
83*d21b3d34SFangrui Song #ifdef POISONLEVEL2
84*d21b3d34SFangrui Song   __msan_poison(&cmsg->cmsg_level, sizeof(cmsg->cmsg_level));
85*d21b3d34SFangrui Song #endif
86*d21b3d34SFangrui Song #ifdef POISONTYPE2
87*d21b3d34SFangrui Song   __msan_poison(&cmsg->cmsg_type, sizeof(cmsg->cmsg_type));
88*d21b3d34SFangrui Song #endif
89*d21b3d34SFangrui Song #ifdef POISONLEN2
90*d21b3d34SFangrui Song   __msan_poison(&cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
91*d21b3d34SFangrui Song #endif
92*d21b3d34SFangrui Song 
93*d21b3d34SFangrui Song   ret = sendmsg(sockfd, &msg, 0);
94*d21b3d34SFangrui Song   // SENDMSG: MemorySanitizer: use-of-uninitialized-value
95*d21b3d34SFangrui Song   if (ret == -1) printf("%d: %s\n", errno, strerror(errno));
96*d21b3d34SFangrui Song   assert(ret > 0);
97*d21b3d34SFangrui Song 
98*d21b3d34SFangrui Song   fprintf(stderr, "== done\n");
99*d21b3d34SFangrui Song   // NEGATIVE: == done
100*d21b3d34SFangrui Song   return 0;
101*d21b3d34SFangrui Song }
102