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>
51 # ifndef MAX_HANDLE_SZ
53 # define MAX_HANDLE_SZ 128
56 unsigned int handle_bytes;
58 unsigned char f_handle[0];
60 # endif /* !MAX_HANDLE_SZ */
64 print_handle_data(unsigned char *bytes, unsigned int size)
68 if (size > MAX_HANDLE_SZ)
72 for (i = 0; i < size; ++i)
73 printf("%02x", bytes[i]);
77 do_name_to_handle_at(kernel_ulong_t dirfd, const char *dirfd_str,
78 kernel_ulong_t pathname, const char *pathname_str,
79 kernel_ulong_t handle, const char *handle_str,
80 kernel_ulong_t mount_id,
81 kernel_ulong_t flags, const char *flags_str,
82 enum assert_rc assert_rc, long assert_errno)
87 rc = syscall(__NR_name_to_handle_at, dirfd, pathname, handle, mount_id,
89 errstr = sprintrc(rc);
91 if (assert_rc != ASSERT_NONE)
92 assert(rc == (assert_rc == ASSERT_SUCCESS ? 0 : -1));
94 assert(errno != assert_errno);
96 printf("name_to_handle_at(%s, %s, %s",
97 dirfd_str, pathname_str, handle_str);
100 struct file_handle *fh =
101 (struct file_handle *) (uintptr_t) handle;
102 int *mount_id_ptr = (int *) (uintptr_t) mount_id;
104 printf(" => %u, handle_type=%d, f_handle=",
105 fh->handle_bytes, fh->handle_type);
106 print_handle_data((unsigned char *) fh +
107 sizeof(struct file_handle),
109 printf("}, [%d]", *mount_id_ptr);
112 printf(", %#llx", (unsigned long long) mount_id);
117 printf(", %s) = %s\n", flags_str, errstr);
121 do_open_by_handle_at(kernel_ulong_t mount_fd,
122 kernel_ulong_t handle, bool valid_handle, bool valid_data,
123 kernel_ulong_t flags, const char *flags_str)
127 printf("open_by_handle_at(%d, ", (int) mount_fd);
129 struct file_handle *fh =
130 (struct file_handle *) (uintptr_t) handle;
132 printf("{handle_bytes=%u, handle_type=%d", fh->handle_bytes,
136 printf(", f_handle=");
137 print_handle_data((unsigned char *) fh +
138 sizeof(struct file_handle),
145 printf("%#llx", (unsigned long long) handle);
149 printf(", %s) = ", flags_str);
151 rc = syscall(__NR_open_by_handle_at, mount_fd, handle, flags);
153 printf("%s\n", sprintrc(rc));
161 #define STR16 "0123456789abcdef"
162 #define STR64 STR16 STR16 STR16 STR16
171 static const kernel_ulong_t fdcwd =
172 (kernel_ulong_t) 0x87654321ffffff9cULL;
173 static const struct strval dirfds[] = {
174 { (kernel_ulong_t) 0xdeadca57badda7a1ULL, "-1159878751" },
175 { (kernel_ulong_t) 0x12345678ffffff9cULL, "AT_FDCWD" },
177 static const struct strval name_flags[] = {
178 { (kernel_ulong_t) 0xdeadf15700000000ULL, "0" },
179 { (kernel_ulong_t) 0xbadc0ded00001000ULL,
181 { (kernel_ulong_t) 0xdeadc0deda7a1457ULL,
182 "AT_SYMLINK_FOLLOW|AT_EMPTY_PATH|0xda7a0057" },
183 { (kernel_ulong_t) 0xdefaced1ffffebffULL,
184 "0xffffebff /* AT_??? */" },
186 static const kernel_ulong_t mount_fds[] = {
187 (kernel_ulong_t) 0xdeadca5701234567ULL,
188 (kernel_ulong_t) 0x12345678ffffff9cULL,
190 static const struct strval open_flags[] = {
191 { F8ILL_KULONG_MASK, "O_RDONLY" },
192 { (kernel_ulong_t) 0xdeadbeef80000001ULL,
193 "O_WRONLY|0x80000000" }
196 static const char str64[] = STR64;
199 char *bogus_path1 = tail_memdup(str64, PATH1_SIZE);
200 char *bogus_path2 = tail_memdup(str64, sizeof(str64));
202 struct file_handle *handle =
203 tail_alloc(sizeof(struct file_handle) + MAX_HANDLE_SZ);
204 struct file_handle *handle_0 =
205 tail_alloc(sizeof(struct file_handle) + 0);
206 struct file_handle *handle_8 =
207 tail_alloc(sizeof(struct file_handle) + 8);
208 struct file_handle *handle_128 =
209 tail_alloc(sizeof(struct file_handle) + 128);
210 struct file_handle *handle_256 =
211 tail_alloc(sizeof(struct file_handle) + 256);
212 TAIL_ALLOC_OBJECT_CONST_PTR(int, bogus_mount_id);
214 char handle_0_addr[sizeof("0x") + sizeof(void *) * 2];
216 char bogus_path1_addr[sizeof("0x") + sizeof(void *) * 2];
217 char bogus_path1_after_addr[sizeof("0x") + sizeof(void *) * 2];
220 struct strval paths[] = {
221 { (kernel_ulong_t) 0, "NULL" },
222 { (kernel_ulong_t) (uintptr_t) (bogus_path1 + PATH1_SIZE),
223 bogus_path1_after_addr },
224 { (kernel_ulong_t) (uintptr_t) bogus_path1, bogus_path1_addr },
225 { (kernel_ulong_t) (uintptr_t) bogus_path2, "\"" STR64 "\"" },
227 struct strval name_handles[] = {
228 { (uintptr_t) (handle_0 + sizeof(struct file_handle)),
230 { (uintptr_t) handle_0, "{handle_bytes=256}" },
231 { (uintptr_t) handle_8, "{handle_bytes=0}" },
232 { (uintptr_t) handle_128, "{handle_bytes=128}" },
233 { (uintptr_t) handle_256, "{handle_bytes=256}" },
241 { (uintptr_t) (handle_0 + sizeof(struct file_handle)),
243 { (uintptr_t) handle_0 + 4, false, false },
244 { (uintptr_t) handle_0, true, false },
245 { (uintptr_t) handle_8, true, true },
246 { (uintptr_t) handle_128, true, true },
247 { (uintptr_t) handle_256, true, true },
249 kernel_ulong_t mount_ids[] = {
251 (kernel_ulong_t) (uintptr_t) (bogus_mount_id + 1),
252 (kernel_ulong_t) (uintptr_t) bogus_mount_id,
255 const int flags = 0x400;
264 snprintf(bogus_path1_addr, sizeof(bogus_path1_addr), "%p", bogus_path1);
265 snprintf(bogus_path1_after_addr, sizeof(bogus_path1_after_addr), "%p",
266 bogus_path1 + PATH1_SIZE);
268 handle_0->handle_bytes = 256;
269 handle_8->handle_bytes = 0;
270 handle_128->handle_bytes = 128;
271 handle_256->handle_bytes = 256;
273 fill_memory((char *) handle_128 + sizeof(struct file_handle), 128);
274 fill_memory((char *) handle_256 + sizeof(struct file_handle), 256);
276 snprintf(handle_0_addr, sizeof(handle_0_addr), "%p",
277 handle_0 + sizeof(struct file_handle));
279 handle->handle_bytes = 0;
281 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
284 perror_msg_and_skip("name_to_handle_at");
285 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0}, %p"
286 ", AT_SYMLINK_FOLLOW|0x1) = -1 EINVAL (%m)\n", &mount_id);
288 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
290 if (EOVERFLOW != errno)
291 perror_msg_and_skip("name_to_handle_at");
292 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0 => %u}"
293 ", %p, AT_SYMLINK_FOLLOW) = -1 EOVERFLOW (%m)\n",
294 handle->handle_bytes, &mount_id);
296 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
298 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=%u"
299 ", handle_type=%d, f_handle=0x",
300 handle->handle_bytes, handle->handle_type);
301 for (i = 0; i < handle->handle_bytes; ++i)
302 printf("%02x", handle->f_handle[i]);
303 printf("}, [%d], AT_SYMLINK_FOLLOW) = 0\n", mount_id);
305 printf("open_by_handle_at(-1, {handle_bytes=%u, handle_type=%d"
306 ", f_handle=0x", handle->handle_bytes, handle->handle_type);
307 for (i = 0; i < handle->handle_bytes; ++i)
308 printf("%02x", handle->f_handle[i]);
309 int rc = syscall(__NR_open_by_handle_at, -1, handle,
310 O_RDONLY | O_DIRECTORY);
311 printf("}, O_RDONLY|O_DIRECTORY) = %d %s (%m)\n", rc, errno2name());
313 for (i = 0; i < ARRAY_SIZE(dirfds); i++) {
314 for (j = 0; j < ARRAY_SIZE(paths); j++) {
315 for (k = 0; k < ARRAY_SIZE(name_handles); k++) {
316 for (l = 0; l < ARRAY_SIZE(mount_ids); l++) {
317 for (m = 0; m < ARRAY_SIZE(name_flags);
319 do_name_to_handle_at(
336 for (i = 0; i < ARRAY_SIZE(mount_fds); i++) {
337 for (j = 0; j < ARRAY_SIZE(open_handles); j++) {
338 for (k = 0; k < ARRAY_SIZE(open_flags); k++) {
339 do_open_by_handle_at(mount_fds[i],
340 open_handles[j].addr,
341 open_handles[j].valid,
342 open_handles[j].valid_data,
349 puts("+++ exited with 0 +++");
355 SKIP_MAIN_UNDEFINED("__NR_name_to_handle_at && __NR_open_by_handle_at")