]> granicus.if.org Git - strace/blob - tests/file_handle.c
Remove XLAT_END
[strace] / tests / file_handle.c
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-2019 The strace developers.
7  * All rights reserved.
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11
12 #include "tests.h"
13 #include "scno.h"
14
15 #if defined __NR_name_to_handle_at && defined __NR_open_by_handle_at
16
17 # include <assert.h>
18 # include <errno.h>
19 # include <inttypes.h>
20 # include <fcntl.h>
21 # include <stdio.h>
22 # include <unistd.h>
23
24 enum assert_rc {
25         ASSERT_NONE,
26         ASSERT_SUCCESS,
27         ASSERT_ERROR,
28 };
29
30 # ifndef MAX_HANDLE_SZ
31
32 #  define MAX_HANDLE_SZ 128
33
34 struct file_handle {
35         unsigned int handle_bytes;
36         int handle_type;
37         unsigned char f_handle[0];
38 };
39 # endif /* !MAX_HANDLE_SZ */
40
41
42 void
43 print_handle_data(unsigned char *bytes, unsigned int size)
44 {
45         unsigned int i;
46
47         if (size > MAX_HANDLE_SZ)
48                 size = MAX_HANDLE_SZ;
49
50         printf("0x");
51         for (i = 0; i < size; ++i)
52                 printf("%02x", bytes[i]);
53 }
54
55 void
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)
62 {
63         long rc;
64         const char *errstr;
65
66         rc = syscall(__NR_name_to_handle_at, dirfd, pathname, handle, mount_id,
67                 flags);
68         errstr = sprintrc(rc);
69
70         if (assert_rc != ASSERT_NONE)
71                 assert(rc == (assert_rc == ASSERT_SUCCESS ? 0 : -1));
72         if (assert_errno)
73                 assert(errno != assert_errno);
74
75         printf("name_to_handle_at(%s, %s, %s",
76                dirfd_str, pathname_str, handle_str);
77
78         if (rc != -1) {
79                 struct file_handle *fh =
80                         (struct file_handle *) (uintptr_t) handle;
81                 int *mount_id_ptr = (int *) (uintptr_t) mount_id;
82
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),
87                                   fh->handle_bytes);
88                 printf("}, [%d]", *mount_id_ptr);
89         } else {
90                 if (mount_id)
91                         printf(", %#llx", (unsigned long long) mount_id);
92                 else
93                         printf(", NULL");
94         }
95
96         printf(", %s) = %s\n", flags_str, errstr);
97 }
98
99 void
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)
103 {
104         long rc;
105
106         printf("open_by_handle_at(%d, ", (int) mount_fd);
107         if (valid_handle) {
108                 struct file_handle *fh =
109                         (struct file_handle *) (uintptr_t) handle;
110
111                 printf("{handle_bytes=%u, handle_type=%d", fh->handle_bytes,
112                        fh->handle_type);
113
114                 if (valid_data) {
115                         printf(", f_handle=");
116                         print_handle_data((unsigned char *) fh +
117                                           sizeof(struct file_handle),
118                                           fh->handle_bytes);
119                 }
120
121                 printf("}");
122         } else {
123                 if (handle)
124                         printf("%#llx", (unsigned long long) handle);
125                 else
126                         printf("NULL");
127         }
128         printf(", %s) = ", flags_str);
129
130         rc = syscall(__NR_open_by_handle_at, mount_fd, handle, flags);
131
132         printf("%s\n", sprintrc(rc));
133 }
134
135 struct strval {
136         kernel_ulong_t val;
137         const char *str;
138 };
139
140 # define STR16 "0123456789abcdef"
141 # define STR64 STR16 STR16 STR16 STR16
142
143 int
144 main(void)
145 {
146         enum {
147                 PATH1_SIZE = 64,
148         };
149
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" },
155         };
156         static const struct strval name_flags[] = {
157                 { (kernel_ulong_t) 0xdeadf15700000000ULL, "0" },
158                 { (kernel_ulong_t) 0xbadc0ded00001000ULL,
159                         "AT_EMPTY_PATH" },
160                 { (kernel_ulong_t) 0xdeadc0deda7a1457ULL,
161                         "AT_SYMLINK_FOLLOW|AT_EMPTY_PATH|0xda7a0057" },
162                 { (kernel_ulong_t) 0xdefaced1ffffebffULL,
163                         "0xffffebff /* AT_??? */" },
164         };
165         static const kernel_ulong_t mount_fds[] = {
166                 (kernel_ulong_t) 0xdeadca5701234567ULL,
167                 (kernel_ulong_t) 0x12345678ffffff9cULL,
168         };
169         static const struct strval open_flags[] = {
170                 { F8ILL_KULONG_MASK, "O_RDONLY" },
171                 { (kernel_ulong_t) 0xdeadbeef80000001ULL,
172                         "O_WRONLY|0x80000000" }
173         };
174
175         static const char str64[] = STR64;
176
177
178         char *bogus_path1 = tail_memdup(str64, PATH1_SIZE);
179         char *bogus_path2 = tail_memdup(str64, sizeof(str64));
180
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);
192
193         char handle_0_addr[sizeof("0x") + sizeof(void *) * 2];
194
195         char bogus_path1_addr[sizeof("0x") + sizeof(void *) * 2];
196         char bogus_path1_after_addr[sizeof("0x") + sizeof(void *) * 2];
197
198
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 "\"" },
205         };
206         struct strval name_handles[] = {
207                 { (uintptr_t) (handle_0 + sizeof(struct file_handle)),
208                         handle_0_addr },
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}" },
213         };
214         struct {
215                 kernel_ulong_t addr;
216                 bool valid;
217                 bool valid_data;
218         } open_handles[] = {
219                 { 0, false, false },
220                 { (uintptr_t) (handle_0 + sizeof(struct file_handle)),
221                         false, false },
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 },
227         };
228         kernel_ulong_t mount_ids[] = {
229                 0,
230                 (kernel_ulong_t) (uintptr_t) (bogus_mount_id + 1),
231                 (kernel_ulong_t) (uintptr_t) bogus_mount_id,
232         };
233
234         const int flags = 0x400;
235         int mount_id;
236         unsigned int i;
237         unsigned int j;
238         unsigned int k;
239         unsigned int l;
240         unsigned int m;
241
242
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);
246
247         handle_0->handle_bytes = 256;
248         handle_8->handle_bytes = 0;
249         handle_128->handle_bytes = 128;
250         handle_256->handle_bytes = 256;
251
252         fill_memory((char *) handle_128 + sizeof(struct file_handle), 128);
253         fill_memory((char *) handle_256 + sizeof(struct file_handle), 256);
254
255         snprintf(handle_0_addr, sizeof(handle_0_addr), "%p",
256                 handle_0 + sizeof(struct file_handle));
257
258         handle->handle_bytes = 0;
259
260         assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
261                 flags | 1) == -1);
262         if (EINVAL != errno)
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);
266
267         assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
268                 flags) == -1);
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);
274
275         assert(syscall(__NR_name_to_handle_at, fdcwd, ".", handle, &mount_id,
276                 flags) == 0);
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);
283
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());
291
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);
297                                             m++) {
298                                                 do_name_to_handle_at(
299                                                         dirfds[i].val,
300                                                         dirfds[i].str,
301                                                         paths[j].val,
302                                                         paths[j].str,
303                                                         name_handles[k].val,
304                                                         name_handles[k].str,
305                                                         mount_ids[l],
306                                                         name_flags[m].val,
307                                                         name_flags[m].str,
308                                                         ASSERT_ERROR, 0);
309                                         }
310                                 }
311                         }
312                 }
313         }
314
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,
322                                                      open_flags[k].val,
323                                                      open_flags[k].str);
324                         }
325                 }
326         }
327
328         puts("+++ exited with 0 +++");
329         return 0;
330 }
331
332 #else
333
334 SKIP_MAIN_UNDEFINED("__NR_name_to_handle_at && __NR_open_by_handle_at")
335
336 #endif