1 /*
2 * Check decoding of move_mount syscall.
3 *
4 * Copyright (c) 2019-2021 Dmitry V. Levin <ldv@strace.io>
5 * Copyright (c) 2019-2023 The strace developers.
6 * All rights reserved.
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #include "tests.h"
12 #include "scno.h"
13
14 #include <fcntl.h>
15 #include <limits.h>
16 #include <stdio.h>
17 #include <stdint.h>
18 #include <unistd.h>
19
20 static const char *errstr;
21
22 static long
23 k_move_mount(const unsigned int from_dfd, const void *from_fname,
24 const unsigned int to_dfd, const void *to_fname,
25 const unsigned int flags)
26 {
27 const kernel_ulong_t fill = (kernel_ulong_t) 0xdefaced00000000ULL;
28 const kernel_ulong_t bad = (kernel_ulong_t) 0xbadc0dedbadc0dedULL;
29 const kernel_ulong_t arg1 = fill | from_dfd;
30 const kernel_ulong_t arg2 = (uintptr_t) from_fname;
31 const kernel_ulong_t arg3 = fill | to_dfd;
32 const kernel_ulong_t arg4 = (uintptr_t) to_fname;
33 const kernel_ulong_t arg5 = fill | flags;
34 const long rc = syscall(__NR_move_mount,
35 arg1, arg2, arg3, arg4, arg5, bad);
36 errstr = sprintrc(rc);
37 return rc;
38 }
39
40 int
41 main(void)
42 {
43 skip_if_unavailable("/proc/self/fd/");
44
45 char *cwd = get_fd_path(get_dir_fd("."));
46 static const char path_full[] = "/dev/full";
47 const char *const path = tail_memdup(path_full, sizeof(path_full));
48 const void *const efault = path + sizeof(path_full);
49 const char *const empty = efault - 1;
50 char *const fname = tail_alloc(PATH_MAX);
51 fill_memory_ex(fname, PATH_MAX, '0', 10);
52
53 int dfd = open(path, O_WRONLY);
54 if (dfd < 0)
55 perror_msg_and_fail("open: %s", path);
56
57 k_move_mount(-1, 0, -100, efault, 0);
58 #ifndef PATH_TRACING
59 printf("move_mount(-1, NULL, AT_FDCWD<%s>, %p, 0) = %s\n",
60 cwd, efault, errstr);
61 #endif
62
63 k_move_mount(-100, efault, -1, 0, 0);
64 #ifndef PATH_TRACING
65 printf("move_mount(AT_FDCWD<%s>, %p, -1, NULL, 0) = %s\n",
66 cwd, efault, errstr);
67 #endif
68
69 k_move_mount(dfd, fname, -100, empty, 1);
70 printf("move_mount(%d<%s>, \"%.*s\"..., AT_FDCWD<%s>, \"\", %s) = %s\n",
71 dfd, path, (int) PATH_MAX - 1, fname, cwd,
72 "MOVE_MOUNT_F_SYMLINKS", errstr);
73
74 k_move_mount(-100, empty, dfd, fname, 0x10);
75 printf("move_mount(AT_FDCWD<%s>, \"\", %d<%s>, \"%.*s\"..., %s) = %s\n",
76 cwd, dfd, path, (int) PATH_MAX - 1, fname,
77 "MOVE_MOUNT_T_SYMLINKS", errstr);
78
79 k_move_mount(-100, empty, dfd, fname, 0x100);
80 printf("move_mount(AT_FDCWD<%s>, \"\", %d<%s>, \"%.*s\"..., %s) = %s\n",
81 cwd, dfd, path, (int) PATH_MAX - 1, fname,
82 "MOVE_MOUNT_SET_GROUP", errstr);
83
84 #define f_flags_str "MOVE_MOUNT_F_SYMLINKS|MOVE_MOUNT_F_AUTOMOUNTS|MOVE_MOUNT_F_EMPTY_PATH"
85 fname[PATH_MAX - 1] = '\0';
86 k_move_mount(dfd, fname, -100, empty, 7);
87 printf("move_mount(%d<%s>, \"%s\", AT_FDCWD<%s>, \"\", %s) = %s\n",
88 dfd, path, fname, cwd, f_flags_str, errstr);
89
90 #define t_flags_str "MOVE_MOUNT_T_SYMLINKS|MOVE_MOUNT_T_AUTOMOUNTS|MOVE_MOUNT_T_EMPTY_PATH"
91 k_move_mount(-100, empty, dfd, fname, 0x70);
92 printf("move_mount(AT_FDCWD<%s>, \"\", %d<%s>, \"%s\", %s) = %s\n",
93 cwd, dfd, path, fname, t_flags_str, errstr);
94
95 #define set_group_str "MOVE_MOUNT_SET_GROUP"
96 k_move_mount(-100, empty, dfd, fname, 0x100);
97 printf("move_mount(AT_FDCWD<%s>, \"\", %d<%s>, \"%s\", %s) = %s\n",
98 cwd, dfd, path, fname, set_group_str, errstr);
99
100 #define beneath_str "MOVE_MOUNT_BENEATH"
101 k_move_mount(-100, empty, dfd, fname, 0x200);
102 printf("move_mount(AT_FDCWD<%s>, \"\", %d<%s>, \"%s\", %s) = %s\n",
103 cwd, dfd, path, fname, beneath_str, errstr);
104
105 k_move_mount(-1, path, -100, empty, 0x377);
106 printf("move_mount(-1, \"%s\", AT_FDCWD<%s>, \"\", %s) = %s\n",
107 path, cwd,
108 f_flags_str "|" t_flags_str "|" set_group_str "|" beneath_str,
109 errstr);
110
111 k_move_mount(-100, empty, -1, path, -1);
112 printf("move_mount(AT_FDCWD<%s>, \"\", -1, \"%s\", %s) = %s\n",
113 cwd, path,
114 f_flags_str "|" t_flags_str "|" set_group_str "|" beneath_str
115 "|0xfffffc88",
116 errstr);
117
118 puts("+++ exited with 0 +++");
119 return 0;
120 }