Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | From 3fdcdd47c6a67585123a0a0c8fffabcc9f79a3a2 Mon Sep 17 00:00:00 2001 |
| 2 | From: "Dmitry V. Levin" <ldv@altlinux.org> |
| 3 | Date: Tue, 12 Jan 2016 14:47:12 +0000 |
| 4 | Subject: [PATCH] scm_rights-fd.test: rewrite without fork |
| 5 | |
| 6 | * tests/scm_rights.c (main): Rewrite without fork. |
| 7 | Place all objects passed to sendmsg and recvmsg at the end |
| 8 | of memory pages followed by inaccessible pages. |
| 9 | * tests/scm_rights-fd.test: Update. |
| 10 | --- |
| 11 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 12 | Upstream-Status: Backport |
| 13 | |
| 14 | tests/scm_rights-fd.test | 12 ++--- |
| 15 | tests/scm_rights.c | 122 ++++++++++++++++++++++------------------------- |
| 16 | 2 files changed, 63 insertions(+), 71 deletions(-) |
| 17 | |
| 18 | diff --git a/tests/scm_rights-fd.test b/tests/scm_rights-fd.test |
| 19 | index a32ef36..48c5028 100755 |
| 20 | --- a/tests/scm_rights-fd.test |
| 21 | +++ b/tests/scm_rights-fd.test |
| 22 | @@ -49,18 +49,18 @@ touch -- "$file" || |
| 23 | framework_skip_ 'failed to create a file' |
| 24 | |
| 25 | run_prog ./scm_rights /dev/zero |
| 26 | -run_strace_merge -y -x -enetwork $args "$file" |
| 27 | +run_strace -y -x -enetwork $args "$file" |
| 28 | |
| 29 | +sample='\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd\\xfe\\xff' |
| 30 | n='[1-9][0-9]*' |
| 31 | -msg='\{msg_name\(0\)=NULL, msg_iov\(1\)=\[\{"\\x00\\x00\\x00\\x00[^"]*", '"$n"'\}\], msg_controllen='"$n" |
| 32 | -rights='\{cmsg_len='"$n"', cmsg_level=SOL_SOCKET, cmsg_type=SCM_RIGHTS, \[3</dev/null>, 4</dev/zero>, 5</[^}>]*/(A\\n){127}Z>\]\}' |
| 33 | +msg='\{msg_name\(0\)=NULL, msg_iov\(1\)=\[\{"'"$sample"'", 15\}\], msg_controllen='"$n" |
| 34 | +rights='\{cmsg_len='"$n"', cmsg_level=SOL_SOCKET, cmsg_type=SCM_RIGHTS, \[4</dev/null>, 5</dev/zero>, 6</[^}>]*/(A\\n){127}Z>\]\}' |
| 35 | creds='\{cmsg_len='"$n"', cmsg_level=SOL_SOCKET, cmsg_type=SCM_CREDENTIALS, \{pid='"$n"', uid=[0-9]+, gid=[0-9]+\}\}' |
| 36 | -prefix='[1-9][0-9]* +[0-9]+:[0-9]+:[0-9]+\.[0-9]+ +' |
| 37 | EXPECTED="$LOG.expected" |
| 38 | |
| 39 | cat > "$EXPECTED" << __EOF__ |
| 40 | -${prefix}sendmsg\\(1<socket:\\[[0-9]+\\]>, $msg, \\[$rights\\], msg_flags=0\\}, 0\\) += $n |
| 41 | -${prefix}recvmsg\\(0<socket:\\[[0-9]+\\]>, $msg, \\[$creds, $rights\\], msg_flags=0\\}, 0\\) += $n |
| 42 | +sendmsg\\(3<socket:\\[[0-9]+\\]>, $msg, \\[$rights\\], msg_flags=0\\}, 0\\) = 15 |
| 43 | +recvmsg\\(0<socket:\\[[0-9]+\\]>, $msg, \\[$creds, $rights\\], msg_flags=0\\}, 0\\) = 15 |
| 44 | __EOF__ |
| 45 | |
| 46 | match_grep "$LOG" "$EXPECTED" |
| 47 | diff --git a/tests/scm_rights.c b/tests/scm_rights.c |
| 48 | index 1e5e850..00af4d5 100644 |
| 49 | --- a/tests/scm_rights.c |
| 50 | +++ b/tests/scm_rights.c |
| 51 | @@ -27,26 +27,39 @@ |
| 52 | |
| 53 | #include "tests.h" |
| 54 | #include <assert.h> |
| 55 | -#include <string.h> |
| 56 | -#include <stdlib.h> |
| 57 | -#include <unistd.h> |
| 58 | #include <errno.h> |
| 59 | #include <fcntl.h> |
| 60 | +#include <stdlib.h> |
| 61 | +#include <string.h> |
| 62 | +#include <unistd.h> |
| 63 | #include <sys/socket.h> |
| 64 | -#include <sys/wait.h> |
| 65 | |
| 66 | int main(int ac, const char **av) |
| 67 | { |
| 68 | - int i; |
| 69 | - int data = 0; |
| 70 | - struct iovec iov = { |
| 71 | - .iov_base = &data, |
| 72 | - .iov_len = sizeof(iov) |
| 73 | - }; |
| 74 | + assert(ac > 0); |
| 75 | + int fds[ac]; |
| 76 | + |
| 77 | + static const char sample[] = |
| 78 | + "\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"; |
| 79 | + const unsigned int data_size = sizeof(sample) - 1; |
| 80 | + void *data = tail_alloc(data_size); |
| 81 | + memcpy(data, sample, data_size); |
| 82 | + |
| 83 | + struct iovec *iov = tail_alloc(sizeof(struct iovec)); |
| 84 | + iov->iov_base = data; |
| 85 | + iov->iov_len = data_size; |
| 86 | |
| 87 | - while ((i = open("/dev/null", O_RDWR)) < 3) |
| 88 | + struct msghdr *mh = tail_alloc(sizeof(struct msghdr)); |
| 89 | + memset(mh, 0, sizeof(*mh)); |
| 90 | + mh->msg_iov = iov; |
| 91 | + mh->msg_iovlen = 1; |
| 92 | + |
| 93 | + int i; |
| 94 | + while ((i = open("/dev/null", O_RDWR)) <= ac + 2) |
| 95 | assert(i >= 0); |
| 96 | - (void) close(3); |
| 97 | + while (i > 2) |
| 98 | + assert(close(i--) == 0); |
| 99 | + assert(close(0) == 0); |
| 100 | |
| 101 | int sv[2]; |
| 102 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) |
| 103 | @@ -55,60 +68,39 @@ int main(int ac, const char **av) |
| 104 | if (setsockopt(sv[0], SOL_SOCKET, SO_PASSCRED, &one, sizeof(one))) |
| 105 | perror_msg_and_skip("setsockopt"); |
| 106 | |
| 107 | - pid_t pid = fork(); |
| 108 | - if (pid < 0) |
| 109 | - perror_msg_and_fail("fork"); |
| 110 | - |
| 111 | - if (pid) { |
| 112 | - assert(close(sv[0]) == 0); |
| 113 | - assert(dup2(sv[1], 1) == 1); |
| 114 | - assert(close(sv[1]) == 0); |
| 115 | - |
| 116 | - int fds[ac]; |
| 117 | - assert((fds[0] = open("/dev/null", O_RDWR)) == 3); |
| 118 | - for (i = 1; i < ac; ++i) |
| 119 | - assert((fds[i] = open(av[i], O_RDONLY)) == i + 3); |
| 120 | - |
| 121 | - union { |
| 122 | - struct cmsghdr cmsg; |
| 123 | - char buf[CMSG_LEN(sizeof(fds))]; |
| 124 | - } control; |
| 125 | - |
| 126 | - control.cmsg.cmsg_level = SOL_SOCKET; |
| 127 | - control.cmsg.cmsg_type = SCM_RIGHTS; |
| 128 | - control.cmsg.cmsg_len = CMSG_LEN(sizeof(fds)); |
| 129 | - memcpy(CMSG_DATA(&control.cmsg), fds, sizeof(fds)); |
| 130 | - |
| 131 | - struct msghdr mh = { |
| 132 | - .msg_iov = &iov, |
| 133 | - .msg_iovlen = 1, |
| 134 | - .msg_control = &control, |
| 135 | - .msg_controllen = sizeof(control) |
| 136 | - }; |
| 137 | - |
| 138 | - assert(sendmsg(1, &mh, 0) == sizeof(iov)); |
| 139 | - assert(close(1) == 0); |
| 140 | - |
| 141 | - int status; |
| 142 | - assert(waitpid(pid, &status, 0) == pid); |
| 143 | - assert(status == 0); |
| 144 | - } else { |
| 145 | - assert(close(sv[1]) == 0); |
| 146 | - assert(dup2(sv[0], 0) == 0); |
| 147 | - assert(close(sv[0]) == 0); |
| 148 | - |
| 149 | - struct cmsghdr control[4 + ac * sizeof(int) / sizeof(struct cmsghdr)]; |
| 150 | - |
| 151 | - struct msghdr mh = { |
| 152 | - .msg_iov = &iov, |
| 153 | - .msg_iovlen = 1, |
| 154 | - .msg_control = control, |
| 155 | - .msg_controllen = sizeof(control) |
| 156 | - }; |
| 157 | - |
| 158 | - assert(recvmsg(0, &mh, 0) == sizeof(iov)); |
| 159 | - assert(close(0) == 0); |
| 160 | + assert((fds[0] = open("/dev/null", O_RDWR)) == 4); |
| 161 | + for (i = 1; i < ac; ++i) |
| 162 | + assert((fds[i] = open(av[i], O_RDONLY)) == i + 4); |
| 163 | + |
| 164 | + unsigned int cmsg_size = CMSG_SPACE(sizeof(fds)); |
| 165 | + struct cmsghdr *cmsg = tail_alloc(cmsg_size); |
| 166 | + memset(cmsg, 0, cmsg_size); |
| 167 | + cmsg->cmsg_level = SOL_SOCKET; |
| 168 | + cmsg->cmsg_type = SCM_RIGHTS; |
| 169 | + cmsg->cmsg_len = CMSG_LEN(sizeof(fds)); |
| 170 | + memcpy(CMSG_DATA(cmsg), fds, sizeof(fds)); |
| 171 | + |
| 172 | + mh->msg_control = cmsg; |
| 173 | + mh->msg_controllen = cmsg_size; |
| 174 | + |
| 175 | + assert(sendmsg(sv[1], mh, 0) == (int) data_size); |
| 176 | + |
| 177 | + assert(close(sv[1]) == 0); |
| 178 | + assert(open("/dev/null", O_RDWR) == sv[1]); |
| 179 | + |
| 180 | + for (i = 0; i < ac; ++i) { |
| 181 | + assert(close(fds[i]) == 0); |
| 182 | + fds[i] = 0; |
| 183 | } |
| 184 | |
| 185 | + cmsg_size += CMSG_SPACE(sizeof(struct ucred)); |
| 186 | + cmsg = tail_alloc(cmsg_size); |
| 187 | + memset(cmsg, 0, cmsg_size); |
| 188 | + mh->msg_control = cmsg; |
| 189 | + mh->msg_controllen = cmsg_size; |
| 190 | + |
| 191 | + assert(recvmsg(0, mh, 0) == (int) data_size); |
| 192 | + assert(close(0) == 0); |
| 193 | + |
| 194 | return 0; |
| 195 | } |
| 196 | -- |
| 197 | 1.9.1 |
| 198 | |