2 * Check decoding of name_to_handle_at and open_by_handle_at syscalls.
4 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
6 * Copyright (c) 2015-2017 The strace developers.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
33 #include <asm/unistd.h>
35 #if defined __NR_name_to_handle_at && defined __NR_open_by_handle_at
39 # include <inttypes.h>
50 # ifndef MAX_HANDLE_SZ
52 # define MAX_HANDLE_SZ 128
55 unsigned int handle_bytes;
57 unsigned char f_handle[0];
59 # endif /* !MAX_HANDLE_SZ */
63 print_handle_data(unsigned char *bytes, unsigned int size)
67 if (size > MAX_HANDLE_SZ)
71 for (i = 0; i < size; ++i)
72 printf("%02x", bytes[i]);
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)
86 rc = syscall(__NR_name_to_handle_at, dirfd, pathname, handle, mount_id,
88 errstr = sprintrc(rc);
90 if (assert_rc != ASSERT_NONE)
91 assert(rc == (assert_rc == ASSERT_SUCCESS ? 0 : -1));
93 assert(errno != assert_errno);
95 printf("name_to_handle_at(%s, %s, %s",
96 dirfd_str, pathname_str, handle_str);
99 struct file_handle *fh =
100 (struct file_handle *) (uintptr_t) handle;
101 int *mount_id_ptr = (int *) (uintptr_t) mount_id;
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),
108 printf("}, [%d]", *mount_id_ptr);
111 printf(", %#llx", (unsigned long long) mount_id);
116 printf(", %s) = %s\n", flags_str, errstr);
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)
126 printf("open_by_handle_at(%d, ", (int) mount_fd);
128 struct file_handle *fh =
129 (struct file_handle *) (uintptr_t) handle;
131 printf("{handle_bytes=%u, handle_type=%d", fh->handle_bytes,
135 printf(", f_handle=");
136 print_handle_data((unsigned char *) fh +
137 sizeof(struct file_handle),
144 printf("%#llx", (unsigned long long) handle);
148 printf(", %s) = ", flags_str);
150 rc = syscall(__NR_open_by_handle_at, mount_fd, handle, flags);
152 printf("%s\n", sprintrc(rc));
160 #define STR16 "0123456789abcdef"
161 #define STR64 STR16 STR16 STR16 STR16
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" },
176 static const struct strval name_flags[] = {
177 { (kernel_ulong_t) 0xdeadf15700000000ULL, "0" },
178 { (kernel_ulong_t) 0xbadc0ded00001000ULL,
180 { (kernel_ulong_t) 0xdeadc0deda7a1457ULL,
181 "AT_SYMLINK_FOLLOW|AT_EMPTY_PATH|0xda7a0057" },
182 { (kernel_ulong_t) 0xdefaced1ffffebffULL,
183 "0xffffebff /* AT_??? */" },
185 static const kernel_ulong_t mount_fds[] = {
186 (kernel_ulong_t) 0xdeadca5701234567ULL,
187 (kernel_ulong_t) 0x12345678ffffff9cULL,
189 static const struct strval open_flags[] = {
190 { F8ILL_KULONG_MASK, "O_RDONLY" },
191 { (kernel_ulong_t) 0xdeadbeef80000001ULL,
192 "O_WRONLY|0x80000000" }
195 static const char str64[] = STR64;
198 char *bogus_path1 = tail_memdup(str64, PATH1_SIZE);
199 char *bogus_path2 = tail_memdup(str64, sizeof(str64));
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);
213 char handle_0_addr[sizeof("0x") + sizeof(void *) * 2];
215 char bogus_path1_addr[sizeof("0x") + sizeof(void *) * 2];
216 char bogus_path1_after_addr[sizeof("0x") + sizeof(void *) * 2];
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 "\"" },
226 struct strval name_handles[] = {
227 { (uintptr_t) (handle_0 + sizeof(struct file_handle)),
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}" },
240 { (uintptr_t) (handle_0 + sizeof(struct file_handle)),
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 },
248 kernel_ulong_t mount_ids[] = {
250 (kernel_ulong_t) (uintptr_t) (bogus_mount_id + 1),
251 (kernel_ulong_t) (uintptr_t) bogus_mount_id,
254 const int flags = 0x400;
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);
267 handle_0->handle_bytes = 256;
268 handle_8->handle_bytes = 0;
269 handle_128->handle_bytes = 128;
270 handle_256->handle_bytes = 256;
272 fill_memory((char *) handle_128 + sizeof(struct file_handle), 128);
273 fill_memory((char *) handle_256 + sizeof(struct file_handle), 256);
275 snprintf(handle_0_addr, sizeof(handle_0_addr), "%p",
276 handle_0 + sizeof(struct file_handle));
278 handle->handle_bytes = 0;
280 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
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);
287 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
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);
295 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
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);
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());
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);
318 do_name_to_handle_at(
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,
348 puts("+++ exited with 0 +++");
354 SKIP_MAIN_UNDEFINED("__NR_name_to_handle_at && __NR_open_by_handle_at")