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-2019 The strace developers.
9 * SPDX-License-Identifier: GPL-2.0-or-later
15 #if defined __NR_name_to_handle_at && defined __NR_open_by_handle_at
19 # include <inttypes.h>
30 # ifndef MAX_HANDLE_SZ
32 # define MAX_HANDLE_SZ 128
35 unsigned int handle_bytes;
37 unsigned char f_handle[0];
39 # endif /* !MAX_HANDLE_SZ */
43 print_handle_data(unsigned char *bytes, unsigned int size)
47 if (size > MAX_HANDLE_SZ)
51 for (i = 0; i < size; ++i)
52 printf("%02x", bytes[i]);
56 do_name_to_handle_at(kernel_ulong_t dirfd, const char *dirfd_str,
57 kernel_ulong_t pathname, const char *pathname_str,
58 kernel_ulong_t handle, const char *handle_str,
59 kernel_ulong_t mount_id,
60 kernel_ulong_t flags, const char *flags_str,
61 enum assert_rc assert_rc, long assert_errno)
66 rc = syscall(__NR_name_to_handle_at, dirfd, pathname, handle, mount_id,
68 errstr = sprintrc(rc);
70 if (assert_rc != ASSERT_NONE)
71 assert(rc == (assert_rc == ASSERT_SUCCESS ? 0 : -1));
73 assert(errno != assert_errno);
75 printf("name_to_handle_at(%s, %s, %s",
76 dirfd_str, pathname_str, handle_str);
79 struct file_handle *fh =
80 (struct file_handle *) (uintptr_t) handle;
81 int *mount_id_ptr = (int *) (uintptr_t) mount_id;
83 printf(" => %u, handle_type=%d, f_handle=",
84 fh->handle_bytes, fh->handle_type);
85 print_handle_data((unsigned char *) fh +
86 sizeof(struct file_handle),
88 printf("}, [%d]", *mount_id_ptr);
91 printf(", %#llx", (unsigned long long) mount_id);
96 printf(", %s) = %s\n", flags_str, errstr);
100 do_open_by_handle_at(kernel_ulong_t mount_fd,
101 kernel_ulong_t handle, bool valid_handle, bool valid_data,
102 kernel_ulong_t flags, const char *flags_str)
106 printf("open_by_handle_at(%d, ", (int) mount_fd);
108 struct file_handle *fh =
109 (struct file_handle *) (uintptr_t) handle;
111 printf("{handle_bytes=%u, handle_type=%d", fh->handle_bytes,
115 printf(", f_handle=");
116 print_handle_data((unsigned char *) fh +
117 sizeof(struct file_handle),
124 printf("%#llx", (unsigned long long) handle);
128 printf(", %s) = ", flags_str);
130 rc = syscall(__NR_open_by_handle_at, mount_fd, handle, flags);
132 printf("%s\n", sprintrc(rc));
140 # define STR16 "0123456789abcdef"
141 # define STR64 STR16 STR16 STR16 STR16
150 static const kernel_ulong_t fdcwd =
151 (kernel_ulong_t) 0x87654321ffffff9cULL;
152 static const struct strval dirfds[] = {
153 { (kernel_ulong_t) 0xdeadca57badda7a1ULL, "-1159878751" },
154 { (kernel_ulong_t) 0x12345678ffffff9cULL, "AT_FDCWD" },
156 static const struct strval name_flags[] = {
157 { (kernel_ulong_t) 0xdeadf15700000000ULL, "0" },
158 { (kernel_ulong_t) 0xbadc0ded00001000ULL,
160 { (kernel_ulong_t) 0xdeadc0deda7a1457ULL,
161 "AT_SYMLINK_FOLLOW|AT_EMPTY_PATH|0xda7a0057" },
162 { (kernel_ulong_t) 0xdefaced1ffffebffULL,
163 "0xffffebff /* AT_??? */" },
165 static const kernel_ulong_t mount_fds[] = {
166 (kernel_ulong_t) 0xdeadca5701234567ULL,
167 (kernel_ulong_t) 0x12345678ffffff9cULL,
169 static const struct strval open_flags[] = {
170 { F8ILL_KULONG_MASK, "O_RDONLY" },
171 { (kernel_ulong_t) 0xdeadbeef80000001ULL,
172 "O_WRONLY|0x80000000" }
175 static const char str64[] = STR64;
178 char *bogus_path1 = tail_memdup(str64, PATH1_SIZE);
179 char *bogus_path2 = tail_memdup(str64, sizeof(str64));
181 struct file_handle *handle =
182 tail_alloc(sizeof(struct file_handle) + MAX_HANDLE_SZ);
183 struct file_handle *handle_0 =
184 tail_alloc(sizeof(struct file_handle) + 0);
185 struct file_handle *handle_8 =
186 tail_alloc(sizeof(struct file_handle) + 8);
187 struct file_handle *handle_128 =
188 tail_alloc(sizeof(struct file_handle) + 128);
189 struct file_handle *handle_256 =
190 tail_alloc(sizeof(struct file_handle) + 256);
191 TAIL_ALLOC_OBJECT_CONST_PTR(int, bogus_mount_id);
193 char handle_0_addr[sizeof("0x") + sizeof(void *) * 2];
195 char bogus_path1_addr[sizeof("0x") + sizeof(void *) * 2];
196 char bogus_path1_after_addr[sizeof("0x") + sizeof(void *) * 2];
199 struct strval paths[] = {
200 { (kernel_ulong_t) 0, "NULL" },
201 { (kernel_ulong_t) (uintptr_t) (bogus_path1 + PATH1_SIZE),
202 bogus_path1_after_addr },
203 { (kernel_ulong_t) (uintptr_t) bogus_path1, bogus_path1_addr },
204 { (kernel_ulong_t) (uintptr_t) bogus_path2, "\"" STR64 "\"" },
206 struct strval name_handles[] = {
207 { (uintptr_t) (handle_0 + sizeof(struct file_handle)),
209 { (uintptr_t) handle_0, "{handle_bytes=256}" },
210 { (uintptr_t) handle_8, "{handle_bytes=0}" },
211 { (uintptr_t) handle_128, "{handle_bytes=128}" },
212 { (uintptr_t) handle_256, "{handle_bytes=256}" },
220 { (uintptr_t) (handle_0 + sizeof(struct file_handle)),
222 { (uintptr_t) handle_0 + 4, false, false },
223 { (uintptr_t) handle_0, true, false },
224 { (uintptr_t) handle_8, true, true },
225 { (uintptr_t) handle_128, true, true },
226 { (uintptr_t) handle_256, true, true },
228 kernel_ulong_t mount_ids[] = {
230 (kernel_ulong_t) (uintptr_t) (bogus_mount_id + 1),
231 (kernel_ulong_t) (uintptr_t) bogus_mount_id,
234 const int flags = 0x400;
243 snprintf(bogus_path1_addr, sizeof(bogus_path1_addr), "%p", bogus_path1);
244 snprintf(bogus_path1_after_addr, sizeof(bogus_path1_after_addr), "%p",
245 bogus_path1 + PATH1_SIZE);
247 handle_0->handle_bytes = 256;
248 handle_8->handle_bytes = 0;
249 handle_128->handle_bytes = 128;
250 handle_256->handle_bytes = 256;
252 fill_memory((char *) handle_128 + sizeof(struct file_handle), 128);
253 fill_memory((char *) handle_256 + sizeof(struct file_handle), 256);
255 snprintf(handle_0_addr, sizeof(handle_0_addr), "%p",
256 handle_0 + sizeof(struct file_handle));
258 handle->handle_bytes = 0;
260 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
263 perror_msg_and_skip("name_to_handle_at");
264 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0}, %p"
265 ", AT_SYMLINK_FOLLOW|0x1) = -1 EINVAL (%m)\n", &mount_id);
267 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
269 if (EOVERFLOW != errno)
270 perror_msg_and_skip("name_to_handle_at");
271 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=0 => %u}"
272 ", %p, AT_SYMLINK_FOLLOW) = -1 EOVERFLOW (%m)\n",
273 handle->handle_bytes, &mount_id);
275 assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
277 printf("name_to_handle_at(AT_FDCWD, \".\", {handle_bytes=%u"
278 ", handle_type=%d, f_handle=0x",
279 handle->handle_bytes, handle->handle_type);
280 for (i = 0; i < handle->handle_bytes; ++i)
281 printf("%02x", handle->f_handle[i]);
282 printf("}, [%d], AT_SYMLINK_FOLLOW) = 0\n", mount_id);
284 printf("open_by_handle_at(-1, {handle_bytes=%u, handle_type=%d"
285 ", f_handle=0x", handle->handle_bytes, handle->handle_type);
286 for (i = 0; i < handle->handle_bytes; ++i)
287 printf("%02x", handle->f_handle[i]);
288 int rc = syscall(__NR_open_by_handle_at, -1, handle,
289 O_RDONLY | O_DIRECTORY);
290 printf("}, O_RDONLY|O_DIRECTORY) = %d %s (%m)\n", rc, errno2name());
292 for (i = 0; i < ARRAY_SIZE(dirfds); i++) {
293 for (j = 0; j < ARRAY_SIZE(paths); j++) {
294 for (k = 0; k < ARRAY_SIZE(name_handles); k++) {
295 for (l = 0; l < ARRAY_SIZE(mount_ids); l++) {
296 for (m = 0; m < ARRAY_SIZE(name_flags);
298 do_name_to_handle_at(
315 for (i = 0; i < ARRAY_SIZE(mount_fds); i++) {
316 for (j = 0; j < ARRAY_SIZE(open_handles); j++) {
317 for (k = 0; k < ARRAY_SIZE(open_flags); k++) {
318 do_open_by_handle_at(mount_fds[i],
319 open_handles[j].addr,
320 open_handles[j].valid,
321 open_handles[j].valid_data,
328 puts("+++ exited with 0 +++");
334 SKIP_MAIN_UNDEFINED("__NR_name_to_handle_at && __NR_open_by_handle_at")