1 /*
2 * Check decoding of name_to_handle_at and open_by_handle_at syscalls.
3 *
4 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
6 * Copyright (c) 2015-2018 The strace developers.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "tests.h"
33 #include <asm/unistd.h>
34
35 #if defined __NR_name_to_handle_at && defined __NR_open_by_handle_at
36
37 # include <assert.h>
38 # include <errno.h>
39 # include <inttypes.h>
40 # include <fcntl.h>
41 # include <stdio.h>
42 # include <unistd.h>
43
44 enum assert_rc {
45 ASSERT_NONE,
46 ASSERT_SUCCESS,
47 ASSERT_ERROR,
48 };
49
50 # ifndef MAX_HANDLE_SZ
51
52 # define MAX_HANDLE_SZ 128
53
54 struct file_handle {
55 unsigned int handle_bytes;
56 int handle_type;
57 unsigned char f_handle[0];
58 };
59 # endif /* !MAX_HANDLE_SZ */
60
61
62 void
print_handle_data(unsigned char * bytes,unsigned int size)63 print_handle_data(unsigned char *bytes, unsigned int size)
64 {
65 unsigned int i;
66
67 if (size > MAX_HANDLE_SZ)
68 size = MAX_HANDLE_SZ;
69
70 printf("0x");
71 for (i = 0; i < size; ++i)
72 printf("%02x", bytes[i]);
73 }
74
75 void
do_name_to_handle_at(kernel_ulong_t dirfd,const char * dirfd_str,kernel_ulong_t pathname,const char * pathname_str,kernel_ulong_t handle,const char * handle_str,kernel_ulong_t mount_id,kernel_ulong_t flags,const char * flags_str,enum assert_rc assert_rc,long assert_errno)76 do_name_to_handle_at(kernel_ulong_t dirfd, const char *dirfd_str,
77 kernel_ulong_t pathname, const char *pathname_str,
78 kernel_ulong_t handle, const char *handle_str,
79 kernel_ulong_t mount_id,
80 kernel_ulong_t flags, const char *flags_str,
81 enum assert_rc assert_rc, long assert_errno)
82 {
83 long rc;
84 const char *errstr;
85
86 rc = syscall(__NR_name_to_handle_at, dirfd, pathname, handle, mount_id,
87 flags);
88 errstr = sprintrc(rc);
89
90 if (assert_rc != ASSERT_NONE)
91 assert(rc == (assert_rc == ASSERT_SUCCESS ? 0 : -1));
92 if (assert_errno)
93 assert(errno != assert_errno);
94
95 printf("name_to_handle_at(%s, %s, %s",
96 dirfd_str, pathname_str, handle_str);
97
98 if (rc != -1) {
99 struct file_handle *fh =
100 (struct file_handle *) (uintptr_t) handle;
101 int *mount_id_ptr = (int *) (uintptr_t) mount_id;
102
103 printf(" => %u, handle_type=%d, f_handle=",
104 fh->handle_bytes, fh->handle_type);
105 print_handle_data((unsigned char *) fh +
106 sizeof(struct file_handle),
107 fh->handle_bytes);
108 printf("}, [%d]", *mount_id_ptr);
109 } else {
110 if (mount_id)
111 printf(", %#llx", (unsigned long long) mount_id);
112 else
113 printf(", NULL");
114 }
115
116 printf(", %s) = %s\n", flags_str, errstr);
117 }
118
119 void
do_open_by_handle_at(kernel_ulong_t mount_fd,kernel_ulong_t handle,bool valid_handle,bool valid_data,kernel_ulong_t flags,const char * flags_str)120 do_open_by_handle_at(kernel_ulong_t mount_fd,
121 kernel_ulong_t handle, bool valid_handle, bool valid_data,
122 kernel_ulong_t flags, const char *flags_str)
123 {
124 long rc;
125
126 printf("open_by_handle_at(%d, ", (int) mount_fd);
127 if (valid_handle) {
128 struct file_handle *fh =
129 (struct file_handle *) (uintptr_t) handle;
130
131 printf("{handle_bytes=%u, handle_type=%d", fh->handle_bytes,
132 fh->handle_type);
133
134 if (valid_data) {
135 printf(", f_handle=");
136 print_handle_data((unsigned char *) fh +
137 sizeof(struct file_handle),
138 fh->handle_bytes);
139 }
140
141 printf("}");
142 } else {
143 if (handle)
144 printf("%#llx", (unsigned long long) handle);
145 else
146 printf("NULL");
147 }
148 printf(", %s) = ", flags_str);
149
150 rc = syscall(__NR_open_by_handle_at, mount_fd, handle, flags);
151
152 printf("%s\n", sprintrc(rc));
153 }
154
155 struct strval {
156 kernel_ulong_t val;
157 const char *str;
158 };
159
160 #define STR16 "0123456789abcdef"
161 #define STR64 STR16 STR16 STR16 STR16
162
163 int
main(void)164 main(void)
165 {
166 enum {
167 PATH1_SIZE = 64,
168 };
169
170 static const kernel_ulong_t fdcwd =
171 (kernel_ulong_t) 0x87654321ffffff9cULL;
172 static const struct strval dirfds[] = {
173 { (kernel_ulong_t) 0xdeadca57badda7a1ULL, "-1159878751" },
174 { (kernel_ulong_t) 0x12345678ffffff9cULL, "AT_FDCWD" },
175 };
176 static const struct strval name_flags[] = {
177 { (kernel_ulong_t) 0xdeadf15700000000ULL, "0" },
178 { (kernel_ulong_t) 0xbadc0ded00001000ULL,
179 "AT_EMPTY_PATH" },
180 { (kernel_ulong_t) 0xdeadc0deda7a1457ULL,
181 "AT_SYMLINK_FOLLOW|AT_EMPTY_PATH|0xda7a0057" },
182 { (kernel_ulong_t) 0xdefaced1ffffebffULL,
183 "0xffffebff /* AT_??? */" },
184 };
185 static const kernel_ulong_t mount_fds[] = {
186 (kernel_ulong_t) 0xdeadca5701234567ULL,
187 (kernel_ulong_t) 0x12345678ffffff9cULL,
188 };
189 static const struct strval open_flags[] = {
190 { F8ILL_KULONG_MASK, "O_RDONLY" },
191 { (kernel_ulong_t) 0xdeadbeef80000001ULL,
192 "O_WRONLY|0x80000000" }
193 };
194
195 static const char str64[] = STR64;
196
197
198 char *bogus_path1 = tail_memdup(str64, PATH1_SIZE);
199 char *bogus_path2 = tail_memdup(str64, sizeof(str64));
200
201 struct file_handle *handle =
202 tail_alloc(sizeof(struct file_handle) + MAX_HANDLE_SZ);
203 struct file_handle *handle_0 =
204 tail_alloc(sizeof(struct file_handle) + 0);
205 struct file_handle *handle_8 =
206 tail_alloc(sizeof(struct file_handle) + 8);
207 struct file_handle *handle_128 =
208 tail_alloc(sizeof(struct file_handle) + 128);
209 struct file_handle *handle_256 =
210 tail_alloc(sizeof(struct file_handle) + 256);
211 TAIL_ALLOC_OBJECT_CONST_PTR(int, bogus_mount_id);
212
213 char handle_0_addr[sizeof("0x") + sizeof(void *) * 2];
214
215 char bogus_path1_addr[sizeof("0x") + sizeof(void *) * 2];
216 char bogus_path1_after_addr[sizeof("0x") + sizeof(void *) * 2];
217
218
219 struct strval paths[] = {
220 { (kernel_ulong_t) 0, "NULL" },
221 { (kernel_ulong_t) (uintptr_t) (bogus_path1 + PATH1_SIZE),
222 bogus_path1_after_addr },
223 { (kernel_ulong_t) (uintptr_t) bogus_path1, bogus_path1_addr },
224 { (kernel_ulong_t) (uintptr_t) bogus_path2, "\"" STR64 "\"" },
225 };
226 struct strval name_handles[] = {
227 { (uintptr_t) (handle_0 + sizeof(struct file_handle)),
228 handle_0_addr },
229 { (uintptr_t) handle_0, "{handle_bytes=256}" },
230 { (uintptr_t) handle_8, "{handle_bytes=0}" },
231 { (uintptr_t) handle_128, "{handle_bytes=128}" },
232 { (uintptr_t) handle_256, "{handle_bytes=256}" },
233 };
234 struct {
235 kernel_ulong_t addr;
236 bool valid;
237 bool valid_data;
238 } open_handles[] = {
239 { 0, false, false },
240 { (uintptr_t) (handle_0 + sizeof(struct file_handle)),
241 false, false },
242 { (uintptr_t) handle_0 + 4, false, false },
243 { (uintptr_t) handle_0, true, false },
244 { (uintptr_t) handle_8, true, true },
245 { (uintptr_t) handle_128, true, true },
246 { (uintptr_t) handle_256, true, true },
247 };
248 kernel_ulong_t mount_ids[] = {
249 0,
250 (kernel_ulong_t) (uintptr_t) (bogus_mount_id + 1),
251 (kernel_ulong_t) (uintptr_t) bogus_mount_id,
252 };
253
254 const int flags = 0x400;
255 int mount_id;
256 unsigned int i;
257 unsigned int j;
258 unsigned int k;
259 unsigned int l;
260 unsigned int m;
261
262
263 snprintf(bogus_path1_addr, sizeof(bogus_path1_addr), "%p", bogus_path1);
264 snprintf(bogus_path1_after_addr, sizeof(bogus_path1_after_addr), "%p",
265 bogus_path1 + PATH1_SIZE);
266
267 handle_0->handle_bytes = 256;
268 handle_8->handle_bytes = 0;
269 handle_128->handle_bytes = 128;
270 handle_256->handle_bytes = 256;
271
272 fill_memory((char *) handle_128 + sizeof(struct file_handle), 128);
273 fill_memory((char *) handle_256 + sizeof(struct file_handle), 256);
274
275 snprintf(handle_0_addr, sizeof(handle_0_addr), "%p",
276 handle_0 + sizeof(struct file_handle));
277
278 handle->handle_bytes = 0;
279
280 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
281 flags | 1) == -1);
282 if (EINVAL != errno)
283 perror_msg_and_skip("name_to_handle_at");
284 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0}, %p"
285 ", AT_SYMLINK_FOLLOW|0x1) = -1 EINVAL (%m)\n", &mount_id);
286
287 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
288 flags) == -1);
289 if (EOVERFLOW != errno)
290 perror_msg_and_skip("name_to_handle_at");
291 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0 => %u}"
292 ", %p, AT_SYMLINK_FOLLOW) = -1 EOVERFLOW (%m)\n",
293 handle->handle_bytes, &mount_id);
294
295 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
296 flags) == 0);
297 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=%u"
298 ", handle_type=%d, f_handle=0x",
299 handle->handle_bytes, handle->handle_type);
300 for (i = 0; i < handle->handle_bytes; ++i)
301 printf("%02x", handle->f_handle[i]);
302 printf("}, [%d], AT_SYMLINK_FOLLOW) = 0\n", mount_id);
303
304 printf("open_by_handle_at(-1, {handle_bytes=%u, handle_type=%d"
305 ", f_handle=0x", handle->handle_bytes, handle->handle_type);
306 for (i = 0; i < handle->handle_bytes; ++i)
307 printf("%02x", handle->f_handle[i]);
308 int rc = syscall(__NR_open_by_handle_at, -1, handle,
309 O_RDONLY | O_DIRECTORY);
310 printf("}, O_RDONLY|O_DIRECTORY) = %d %s (%m)\n", rc, errno2name());
311
312 for (i = 0; i < ARRAY_SIZE(dirfds); i++) {
313 for (j = 0; j < ARRAY_SIZE(paths); j++) {
314 for (k = 0; k < ARRAY_SIZE(name_handles); k++) {
315 for (l = 0; l < ARRAY_SIZE(mount_ids); l++) {
316 for (m = 0; m < ARRAY_SIZE(name_flags);
317 m++) {
318 do_name_to_handle_at(
319 dirfds[i].val,
320 dirfds[i].str,
321 paths[j].val,
322 paths[j].str,
323 name_handles[k].val,
324 name_handles[k].str,
325 mount_ids[l],
326 name_flags[m].val,
327 name_flags[m].str,
328 ASSERT_ERROR, 0);
329 }
330 }
331 }
332 }
333 }
334
335 for (i = 0; i < ARRAY_SIZE(mount_fds); i++) {
336 for (j = 0; j < ARRAY_SIZE(open_handles); j++) {
337 for (k = 0; k < ARRAY_SIZE(open_flags); k++) {
338 do_open_by_handle_at(mount_fds[i],
339 open_handles[j].addr,
340 open_handles[j].valid,
341 open_handles[j].valid_data,
342 open_flags[k].val,
343 open_flags[k].str);
344 }
345 }
346 }
347
348 puts("+++ exited with 0 +++");
349 return 0;
350 }
351
352 #else
353
354 SKIP_MAIN_UNDEFINED("__NR_name_to_handle_at && __NR_open_by_handle_at")
355
356 #endif
357