1 /*
2 * Check decoding of kcmp syscall.
3 *
4 * Copyright (c) 2016-2017 Eugene Syromyatnikov <evgsyr@gmail.com>
5 * Copyright (c) 2016-2018 The strace developers.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "tests.h"
32
33 #include <asm/unistd.h>
34 #include "scno.h"
35
36 #ifdef __NR_kcmp
37
38 # include <fcntl.h>
39 # include <stdarg.h>
40 # include <stdint.h>
41 # include <stdio.h>
42 # include <string.h>
43 # include <unistd.h>
44
45 # ifndef VERBOSE_FD
46 # define VERBOSE_FD 0
47 # endif
48
49 /*
50 * We prefer to use system headers in order to catch some possible deviations in
51 * system's headers from our perception of reality, but happy to include our own
52 * definitions as well.
53 */
54 # ifdef HAVE_LINUX_KCMP_H
55 # include <linux/kcmp.h>
56 # else
57 # define KCMP_FILE 0
58 # define KCMP_VM 1
59 # define KCMP_FILES 2
60 # define KCMP_FS 3
61 # define KCMP_SIGHAND 4
62 # define KCMP_IO 5
63 # define KCMP_SYSVSEM 6
64 # endif
65
66 /* All other kcmp types have been added atomically */
67 # define KCMP_EPOLL_TFD 7
68
69 # ifndef HAVE_STRUCT_KCMP_EPOLL_SLOT
70 struct kcmp_epoll_slot {
71 uint32_t efd;
72 uint32_t tfd;
73 uint32_t toff;
74 };
75 # endif
76
77 static const kernel_ulong_t kcmp_max_type = KCMP_EPOLL_TFD;
78
79 static const char null_path[] = "/dev/null";
80 static const char zero_path[] = "/dev/zero";
81
82 # define NULL_FD 23
83 # define ZERO_FD 42
84
85 static void
printpidfd(const char * prefix,pid_t pid,unsigned fd)86 printpidfd(const char *prefix, pid_t pid, unsigned fd)
87 {
88 printf("%s%d", prefix, fd);
89 }
90
91 /*
92 * Last argument is optional and is used as follows:
93 * * When type is KCMP_EPOLL_TFD, it signalises whether idx2 is a valid
94 * pointer.
95 */
96 static void
do_kcmp(kernel_ulong_t pid1,kernel_ulong_t pid2,kernel_ulong_t type,const char * type_str,kernel_ulong_t idx1,kernel_ulong_t idx2,...)97 do_kcmp(kernel_ulong_t pid1, kernel_ulong_t pid2, kernel_ulong_t type,
98 const char *type_str, kernel_ulong_t idx1, kernel_ulong_t idx2, ...)
99 {
100 long rc;
101 const char *errstr;
102
103 rc = syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
104 errstr = sprintrc(rc);
105
106 printf("kcmp(%d, %d, ", (int) pid1, (int) pid2);
107
108 if (type_str)
109 printf("%s", type_str);
110 else
111 printf("%#x /* KCMP_??? */", (int) type);
112
113 if (type == KCMP_FILE) {
114 printpidfd(", ", pid1, idx1);
115 printpidfd(", ", pid2, idx2);
116 } else if (type == KCMP_EPOLL_TFD) {
117 va_list ap;
118 int valid_ptr;
119
120 va_start(ap, idx2);
121 valid_ptr = va_arg(ap, int);
122 va_end(ap);
123
124 printpidfd(", ", pid1, idx1);
125 printf(", ");
126
127 if (valid_ptr) {
128 struct kcmp_epoll_slot *slot =
129 (struct kcmp_epoll_slot *) (uintptr_t) idx2;
130
131 printpidfd("{efd=", pid2, slot->efd);
132 printpidfd(", tfd=", pid2, slot->tfd);
133 printf(", toff=%llu}", (unsigned long long) slot->toff);
134 } else {
135 if (idx2)
136 printf("%#llx", (unsigned long long) idx2);
137 else
138 printf("NULL");
139 }
140 } else if (type > kcmp_max_type) {
141 printf(", %#llx, %#llx",
142 (unsigned long long) idx1, (unsigned long long) idx2);
143 }
144
145 printf(") = %s\n", errstr);
146 }
147
148 int
main(void)149 main(void)
150 {
151 static const kernel_ulong_t bogus_pid1 =
152 (kernel_ulong_t) 0xdeadca75face1057ULL;
153 static const kernel_ulong_t bogus_pid2 =
154 (kernel_ulong_t) 0xdefaced1defaced2ULL;
155 static const kernel_ulong_t bogus_type =
156 (kernel_ulong_t) 0xbadc0dedda7adeadULL;
157 static const kernel_ulong_t bogus_idx1 =
158 (kernel_ulong_t) 0xdec0ded3dec0ded4ULL;
159 static const kernel_ulong_t bogus_idx2 =
160 (kernel_ulong_t) 0xba5e1e55deadc0deULL;
161 static const struct kcmp_epoll_slot slot_data[] = {
162 { 0xdeadc0de, 0xfacef157, 0xbadc0ded },
163 { NULL_FD, ZERO_FD, 0 },
164 { 0, 0, 0 },
165 };
166 static kernel_ulong_t ptr_check =
167 F8ILL_KULONG_SUPPORTED ? F8ILL_KULONG_MASK : 0;
168
169 int fd;
170 unsigned i;
171 TAIL_ALLOC_OBJECT_CONST_PTR(struct kcmp_epoll_slot, slot);
172
173 /* Open some files to test printpidfd */
174 fd = open(null_path, O_RDONLY);
175 if (fd < 0)
176 perror_msg_and_fail("open(\"%s\")", null_path);
177 if (fd != NULL_FD) {
178 if (dup2(fd, NULL_FD) < 0)
179 perror_msg_and_fail("dup2(fd, NULL_FD)");
180 close(fd);
181 }
182
183 fd = open(zero_path, O_RDONLY);
184 if (fd < 0)
185 perror_msg_and_fail("open(\"%s\")", zero_path);
186 if (fd != ZERO_FD) {
187 if (dup2(fd, ZERO_FD) < 0)
188 perror_msg_and_fail("dup2(fd, ZERO_FD)");
189 close(fd);
190 }
191
192 close(0);
193
194 /* Invalid values */
195 do_kcmp(bogus_pid1, bogus_pid2, bogus_type, NULL, bogus_idx1,
196 bogus_idx2);
197 do_kcmp(F8ILL_KULONG_MASK, F8ILL_KULONG_MASK, kcmp_max_type + 1, NULL,
198 0, 0);
199
200 /* KCMP_FILE is the only type which has additional args */
201 do_kcmp(3141592653U, 2718281828U, ARG_STR(KCMP_FILE), bogus_idx1,
202 bogus_idx2);
203 do_kcmp(-1, -1, ARG_STR(KCMP_FILE), NULL_FD, ZERO_FD);
204
205 /* Types without additional args */
206 do_kcmp(-1, -1, ARG_STR(KCMP_VM), bogus_idx1, bogus_idx2);
207 do_kcmp(-1, -1, ARG_STR(KCMP_FILES), bogus_idx1, bogus_idx2);
208 do_kcmp(-1, -1, ARG_STR(KCMP_FS), bogus_idx1, bogus_idx2);
209 do_kcmp(-1, -1, ARG_STR(KCMP_SIGHAND), bogus_idx1, bogus_idx2);
210 do_kcmp(-1, -1, ARG_STR(KCMP_IO), bogus_idx1, bogus_idx2);
211 do_kcmp(-1, -1, ARG_STR(KCMP_SYSVSEM), bogus_idx1, bogus_idx2);
212
213 /* KCMP_EPOLL_TFD checks */
214 do_kcmp(-1, -1, ARG_STR(KCMP_EPOLL_TFD),
215 F8ILL_KULONG_MASK | 2718281828U, ptr_check, 0);
216 do_kcmp(-1, -1, ARG_STR(KCMP_EPOLL_TFD),
217 3141592653U, (uintptr_t) slot + 1, 0);
218
219 for (i = 0; i < ARRAY_SIZE(slot_data); i++) {
220 memcpy(slot, slot_data + i, sizeof(*slot));
221
222 do_kcmp(getpid(), getppid(), ARG_STR(KCMP_EPOLL_TFD), NULL_FD,
223 (uintptr_t) slot, 1);
224 }
225
226 puts("+++ exited with 0 +++");
227
228 return 0;
229 }
230
231 #else
232
233 SKIP_MAIN_UNDEFINED("__NR_kcmp");
234
235 #endif
236