1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_socket.c,v 1.4 2015/02/27 08:30:30 martin Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc #include <sys/types.h>
411be35a1SLionel Sambuc #include <sys/mount.h>
511be35a1SLionel Sambuc #include <sys/socket.h>
611be35a1SLionel Sambuc #include <sys/un.h>
711be35a1SLionel Sambuc
811be35a1SLionel Sambuc #include <rump/rump.h>
911be35a1SLionel Sambuc #include <rump/rump_syscalls.h>
1011be35a1SLionel Sambuc
1111be35a1SLionel Sambuc #include <atf-c.h>
1211be35a1SLionel Sambuc #include <fcntl.h>
1311be35a1SLionel Sambuc #include <err.h>
1411be35a1SLionel Sambuc #include <errno.h>
1511be35a1SLionel Sambuc #include <stdio.h>
1611be35a1SLionel Sambuc #include <stdlib.h>
1711be35a1SLionel Sambuc #include <string.h>
1811be35a1SLionel Sambuc #include <unistd.h>
1911be35a1SLionel Sambuc #include <util.h>
2011be35a1SLionel Sambuc
2111be35a1SLionel Sambuc #include "../../h_macros.h"
2211be35a1SLionel Sambuc
2311be35a1SLionel Sambuc ATF_TC(cmsg_sendfd_bounds);
ATF_TC_HEAD(cmsg_sendfd_bounds,tc)2411be35a1SLionel Sambuc ATF_TC_HEAD(cmsg_sendfd_bounds, tc)
2511be35a1SLionel Sambuc {
2611be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks that attempting to pass an "
2711be35a1SLionel Sambuc "invalid fd returns an error");
2811be35a1SLionel Sambuc }
2911be35a1SLionel Sambuc
ATF_TC_BODY(cmsg_sendfd_bounds,tc)3011be35a1SLionel Sambuc ATF_TC_BODY(cmsg_sendfd_bounds, tc)
3111be35a1SLionel Sambuc {
3211be35a1SLionel Sambuc struct cmsghdr *cmp;
3311be35a1SLionel Sambuc struct msghdr msg;
3411be35a1SLionel Sambuc struct iovec iov;
3511be35a1SLionel Sambuc int s[2];
3611be35a1SLionel Sambuc int fd;
3711be35a1SLionel Sambuc
3811be35a1SLionel Sambuc rump_init();
3911be35a1SLionel Sambuc
4011be35a1SLionel Sambuc if (rump_sys_socketpair(AF_LOCAL, SOCK_STREAM, 0, s) == -1)
4111be35a1SLionel Sambuc atf_tc_fail("rump_sys_socket");
4211be35a1SLionel Sambuc
4311be35a1SLionel Sambuc cmp = malloc(CMSG_SPACE(sizeof(int)));
4411be35a1SLionel Sambuc
4511be35a1SLionel Sambuc iov.iov_base = &fd;
4611be35a1SLionel Sambuc iov.iov_len = sizeof(int);
4711be35a1SLionel Sambuc
4811be35a1SLionel Sambuc cmp->cmsg_level = SOL_SOCKET;
4911be35a1SLionel Sambuc cmp->cmsg_type = SCM_RIGHTS;
5011be35a1SLionel Sambuc cmp->cmsg_len = CMSG_LEN(sizeof(int));
5111be35a1SLionel Sambuc
5211be35a1SLionel Sambuc msg.msg_iov = &iov;
5311be35a1SLionel Sambuc msg.msg_iovlen = 1;
5411be35a1SLionel Sambuc msg.msg_name = NULL;
5511be35a1SLionel Sambuc msg.msg_namelen = 0;
5611be35a1SLionel Sambuc msg.msg_control = cmp;
5711be35a1SLionel Sambuc msg.msg_controllen = CMSG_SPACE(sizeof(int));
5811be35a1SLionel Sambuc
5911be35a1SLionel Sambuc /*
6011be35a1SLionel Sambuc * ERROR HERE: trying to pass invalid fd
6111be35a1SLionel Sambuc * (This value was previously directly used to index the fd
6211be35a1SLionel Sambuc * array and therefore we are passing a hyperspace index)
6311be35a1SLionel Sambuc */
6411be35a1SLionel Sambuc *(int *)CMSG_DATA(cmp) = 0x12345678;
6511be35a1SLionel Sambuc
6611be35a1SLionel Sambuc rump_sys_sendmsg(s[0], &msg, 0);
6711be35a1SLionel Sambuc if (errno != EBADF)
6811be35a1SLionel Sambuc atf_tc_fail("descriptor passing failed: expected EBADF (9), "
6911be35a1SLionel Sambuc "got %d\n(%s)", errno, strerror(errno));
7011be35a1SLionel Sambuc }
7111be35a1SLionel Sambuc
7211be35a1SLionel Sambuc
7311be35a1SLionel Sambuc ATF_TC(cmsg_sendfd);
ATF_TC_HEAD(cmsg_sendfd,tc)7411be35a1SLionel Sambuc ATF_TC_HEAD(cmsg_sendfd, tc)
7511be35a1SLionel Sambuc {
7611be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks that fd passing works");
77*0a6a1f1dSLionel Sambuc atf_tc_set_md_var(tc, "timeout", "10");
7811be35a1SLionel Sambuc }
7911be35a1SLionel Sambuc
ATF_TC_BODY(cmsg_sendfd,tc)8011be35a1SLionel Sambuc ATF_TC_BODY(cmsg_sendfd, tc)
8111be35a1SLionel Sambuc {
8211be35a1SLionel Sambuc char buf[128];
8311be35a1SLionel Sambuc struct cmsghdr *cmp;
8411be35a1SLionel Sambuc struct msghdr msg;
8511be35a1SLionel Sambuc struct sockaddr_un sun;
8684d9c625SLionel Sambuc struct lwp *l1;
8711be35a1SLionel Sambuc struct iovec iov;
8811be35a1SLionel Sambuc socklen_t sl;
8911be35a1SLionel Sambuc int s1, s2, sgot;
9011be35a1SLionel Sambuc int rfd, fd[2], storage;
9111be35a1SLionel Sambuc
9211be35a1SLionel Sambuc rump_init();
9311be35a1SLionel Sambuc
9411be35a1SLionel Sambuc RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
9511be35a1SLionel Sambuc l1 = rump_pub_lwproc_curlwp();
9611be35a1SLionel Sambuc
9711be35a1SLionel Sambuc /* create unix socket and bind it to a path */
9811be35a1SLionel Sambuc memset(&sun, 0, sizeof(sun));
9911be35a1SLionel Sambuc sun.sun_family = AF_LOCAL;
10011be35a1SLionel Sambuc #define SOCKPATH "/com"
10111be35a1SLionel Sambuc strncpy(sun.sun_path, SOCKPATH, sizeof(SOCKPATH));
10211be35a1SLionel Sambuc s1 = rump_sys_socket(AF_LOCAL, SOCK_STREAM, 0);
10311be35a1SLionel Sambuc if (s1 == -1)
10411be35a1SLionel Sambuc atf_tc_fail_errno("socket 1");
10511be35a1SLionel Sambuc if (rump_sys_bind(s1, (struct sockaddr *)&sun, SUN_LEN(&sun)) == -1)
10611be35a1SLionel Sambuc atf_tc_fail_errno("socket 1 bind");
10711be35a1SLionel Sambuc if (rump_sys_listen(s1, 1) == -1)
10811be35a1SLionel Sambuc atf_tc_fail_errno("socket 1 listen");
10911be35a1SLionel Sambuc
11011be35a1SLionel Sambuc /* create second process for test */
11111be35a1SLionel Sambuc RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
11284d9c625SLionel Sambuc (void)rump_pub_lwproc_curlwp();
11311be35a1SLionel Sambuc
11411be35a1SLionel Sambuc /* connect to unix domain socket */
11511be35a1SLionel Sambuc memset(&sun, 0, sizeof(sun));
11611be35a1SLionel Sambuc sun.sun_family = AF_LOCAL;
11711be35a1SLionel Sambuc strncpy(sun.sun_path, SOCKPATH, sizeof(SOCKPATH));
11811be35a1SLionel Sambuc s2 = rump_sys_socket(AF_LOCAL, SOCK_STREAM, 0);
11911be35a1SLionel Sambuc if (s2 == -1)
12011be35a1SLionel Sambuc atf_tc_fail_errno("socket 2");
12111be35a1SLionel Sambuc if (rump_sys_connect(s2, (struct sockaddr *)&sun, SUN_LEN(&sun)) == -1)
12211be35a1SLionel Sambuc atf_tc_fail_errno("socket 2 connect");
12311be35a1SLionel Sambuc
12411be35a1SLionel Sambuc /* open a pipe and write stuff to it */
12511be35a1SLionel Sambuc if (rump_sys_pipe(fd) == -1)
12611be35a1SLionel Sambuc atf_tc_fail_errno("can't open pipe");
12711be35a1SLionel Sambuc #define MAGICSTRING "duam xnaht"
12811be35a1SLionel Sambuc if (rump_sys_write(fd[1], MAGICSTRING, sizeof(MAGICSTRING)) !=
12911be35a1SLionel Sambuc sizeof(MAGICSTRING))
13011be35a1SLionel Sambuc atf_tc_fail_errno("pipe write"); /* XXX: errno */
13111be35a1SLionel Sambuc
13211be35a1SLionel Sambuc cmp = malloc(CMSG_SPACE(sizeof(int)));
13311be35a1SLionel Sambuc
13411be35a1SLionel Sambuc iov.iov_base = &storage;
13511be35a1SLionel Sambuc iov.iov_len = sizeof(int);
13611be35a1SLionel Sambuc
13711be35a1SLionel Sambuc cmp->cmsg_level = SOL_SOCKET;
13811be35a1SLionel Sambuc cmp->cmsg_type = SCM_RIGHTS;
13911be35a1SLionel Sambuc cmp->cmsg_len = CMSG_LEN(sizeof(int));
14011be35a1SLionel Sambuc
14111be35a1SLionel Sambuc msg.msg_iov = &iov;
14211be35a1SLionel Sambuc msg.msg_iovlen = 1;
14311be35a1SLionel Sambuc msg.msg_name = NULL;
14411be35a1SLionel Sambuc msg.msg_namelen = 0;
14511be35a1SLionel Sambuc msg.msg_control = cmp;
14611be35a1SLionel Sambuc msg.msg_controllen = CMSG_SPACE(sizeof(int));
14711be35a1SLionel Sambuc *(int *)CMSG_DATA(cmp) = fd[0];
14811be35a1SLionel Sambuc
14911be35a1SLionel Sambuc /* pass the fd */
15011be35a1SLionel Sambuc if (rump_sys_sendmsg(s2, &msg, 0) == -1)
15111be35a1SLionel Sambuc atf_tc_fail_errno("sendmsg failed");
15211be35a1SLionel Sambuc
15311be35a1SLionel Sambuc /*
15411be35a1SLionel Sambuc * We will read to the same cmsg space. Overwrite the space
15511be35a1SLionel Sambuc * with an invalid fd to make sure we get an explicit error
15611be35a1SLionel Sambuc * if we don't manage to read the fd.
15711be35a1SLionel Sambuc */
15811be35a1SLionel Sambuc *(int *)CMSG_DATA(cmp) = -1;
15911be35a1SLionel Sambuc
16011be35a1SLionel Sambuc /* switch back to original proc */
16111be35a1SLionel Sambuc rump_pub_lwproc_switch(l1);
16211be35a1SLionel Sambuc
16311be35a1SLionel Sambuc /* accept connection and read fd */
16411be35a1SLionel Sambuc sl = sizeof(sun);
16511be35a1SLionel Sambuc sgot = rump_sys_accept(s1, (struct sockaddr *)&sun, &sl);
16611be35a1SLionel Sambuc if (sgot == -1)
16711be35a1SLionel Sambuc atf_tc_fail_errno("accept");
16811be35a1SLionel Sambuc if (rump_sys_recvmsg(sgot, &msg, 0) == -1)
16911be35a1SLionel Sambuc atf_tc_fail_errno("recvmsg failed");
17011be35a1SLionel Sambuc rfd = *(int *)CMSG_DATA(cmp);
17111be35a1SLionel Sambuc
17211be35a1SLionel Sambuc /* read from the fd */
17311be35a1SLionel Sambuc memset(buf, 0, sizeof(buf));
17411be35a1SLionel Sambuc if (rump_sys_read(rfd, buf, sizeof(buf)) == -1)
17511be35a1SLionel Sambuc atf_tc_fail_errno("read rfd");
17611be35a1SLionel Sambuc
17711be35a1SLionel Sambuc /* check that we got the right stuff */
17811be35a1SLionel Sambuc if (strcmp(buf, MAGICSTRING) != 0)
17911be35a1SLionel Sambuc atf_tc_fail("expected \"%s\", got \"%s\"", MAGICSTRING, buf);
18011be35a1SLionel Sambuc }
18111be35a1SLionel Sambuc
18284d9c625SLionel Sambuc ATF_TC(sock_cloexec);
ATF_TC_HEAD(sock_cloexec,tc)18384d9c625SLionel Sambuc ATF_TC_HEAD(sock_cloexec, tc)
18484d9c625SLionel Sambuc {
18584d9c625SLionel Sambuc atf_tc_set_md_var(tc, "descr", "SOCK_CLOEXEC kernel invariant failure");
18684d9c625SLionel Sambuc }
18784d9c625SLionel Sambuc
ATF_TC_BODY(sock_cloexec,tc)18884d9c625SLionel Sambuc ATF_TC_BODY(sock_cloexec, tc)
18984d9c625SLionel Sambuc {
19084d9c625SLionel Sambuc
19184d9c625SLionel Sambuc rump_init();
19284d9c625SLionel Sambuc rump_pub_lwproc_rfork(RUMP_RFFDG);
19384d9c625SLionel Sambuc if (rump_sys_socket(-1, SOCK_CLOEXEC, 0) != -1)
19484d9c625SLionel Sambuc atf_tc_fail("invalid socket parameters unexpectedly worked");
19584d9c625SLionel Sambuc rump_pub_lwproc_releaselwp();
19684d9c625SLionel Sambuc }
19784d9c625SLionel Sambuc
ATF_TP_ADD_TCS(tp)19811be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
19911be35a1SLionel Sambuc {
20011be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, cmsg_sendfd);
20111be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, cmsg_sendfd_bounds);
20284d9c625SLionel Sambuc ATF_TP_ADD_TC(tp, sock_cloexec);
20311be35a1SLionel Sambuc
20411be35a1SLionel Sambuc return atf_no_error();
20511be35a1SLionel Sambuc }
206