File: move_mount.c

package info (click to toggle)
strace 6.13%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 71,488 kB
  • sloc: ansic: 176,497; sh: 9,675; makefile: 4,133; cpp: 885; awk: 353; perl: 267; exp: 62; sed: 9
file content (120 lines) | stat: -rw-r--r-- 4,003 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Check decoding of move_mount syscall.
 *
 * Copyright (c) 2019-2021 Dmitry V. Levin <ldv@strace.io>
 * Copyright (c) 2019-2023 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include "scno.h"

#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>

static const char *errstr;

static long
k_move_mount(const unsigned int from_dfd, const void *from_fname,
	     const unsigned int to_dfd, const void *to_fname,
	     const unsigned int flags)
{
	const kernel_ulong_t fill = (kernel_ulong_t) 0xdefaced00000000ULL;
	const kernel_ulong_t bad = (kernel_ulong_t) 0xbadc0dedbadc0dedULL;
	const kernel_ulong_t arg1 = fill | from_dfd;
	const kernel_ulong_t arg2 = (uintptr_t) from_fname;
	const kernel_ulong_t arg3 = fill | to_dfd;
	const kernel_ulong_t arg4 = (uintptr_t) to_fname;
	const kernel_ulong_t arg5 = fill | flags;
	const long rc = syscall(__NR_move_mount,
				arg1, arg2, arg3, arg4, arg5, bad);
	errstr = sprintrc(rc);
	return rc;
}

int
main(void)
{
	skip_if_unavailable("/proc/self/fd/");

	char *cwd = get_fd_path(get_dir_fd("."));
	static const char path_full[] = "/dev/full";
	const char *const path = tail_memdup(path_full, sizeof(path_full));
	const void *const efault = path + sizeof(path_full);
	const char *const empty = efault - 1;
	char *const fname = tail_alloc(PATH_MAX);
	fill_memory_ex(fname, PATH_MAX, '0', 10);

        int dfd = open(path, O_WRONLY);
        if (dfd < 0)
                perror_msg_and_fail("open: %s", path);

	k_move_mount(-1, 0, -100, efault, 0);
#ifndef PATH_TRACING
	printf("move_mount(-1, NULL, AT_FDCWD<%s>, %p, 0) = %s\n",
	       cwd, efault, errstr);
#endif

	k_move_mount(-100, efault, -1, 0, 0);
#ifndef PATH_TRACING
	printf("move_mount(AT_FDCWD<%s>, %p, -1, NULL, 0) = %s\n",
	       cwd, efault, errstr);
#endif

	k_move_mount(dfd, fname, -100, empty, 1);
	printf("move_mount(%d<%s>, \"%.*s\"..., AT_FDCWD<%s>, \"\", %s) = %s\n",
	       dfd, path, (int) PATH_MAX - 1, fname, cwd,
	       "MOVE_MOUNT_F_SYMLINKS", errstr);

	k_move_mount(-100, empty, dfd, fname, 0x10);
	printf("move_mount(AT_FDCWD<%s>, \"\", %d<%s>, \"%.*s\"..., %s) = %s\n",
	       cwd, dfd, path, (int) PATH_MAX - 1, fname,
	       "MOVE_MOUNT_T_SYMLINKS", errstr);

	k_move_mount(-100, empty, dfd, fname, 0x100);
	printf("move_mount(AT_FDCWD<%s>, \"\", %d<%s>, \"%.*s\"..., %s) = %s\n",
	       cwd, dfd, path, (int) PATH_MAX - 1, fname,
	       "MOVE_MOUNT_SET_GROUP", errstr);

#define f_flags_str "MOVE_MOUNT_F_SYMLINKS|MOVE_MOUNT_F_AUTOMOUNTS|MOVE_MOUNT_F_EMPTY_PATH"
	fname[PATH_MAX - 1] = '\0';
	k_move_mount(dfd, fname, -100, empty, 7);
	printf("move_mount(%d<%s>, \"%s\", AT_FDCWD<%s>, \"\", %s) = %s\n",
	       dfd, path, fname, cwd, f_flags_str, errstr);

#define t_flags_str "MOVE_MOUNT_T_SYMLINKS|MOVE_MOUNT_T_AUTOMOUNTS|MOVE_MOUNT_T_EMPTY_PATH"
	k_move_mount(-100, empty, dfd, fname, 0x70);
	printf("move_mount(AT_FDCWD<%s>, \"\", %d<%s>, \"%s\", %s) = %s\n",
	       cwd, dfd, path, fname, t_flags_str, errstr);

#define set_group_str "MOVE_MOUNT_SET_GROUP"
	k_move_mount(-100, empty, dfd, fname, 0x100);
	printf("move_mount(AT_FDCWD<%s>, \"\", %d<%s>, \"%s\", %s) = %s\n",
	       cwd, dfd, path, fname, set_group_str, errstr);

#define beneath_str "MOVE_MOUNT_BENEATH"
	k_move_mount(-100, empty, dfd, fname, 0x200);
	printf("move_mount(AT_FDCWD<%s>, \"\", %d<%s>, \"%s\", %s) = %s\n",
	       cwd, dfd, path, fname, beneath_str, errstr);

	k_move_mount(-1, path, -100, empty, 0x377);
	printf("move_mount(-1, \"%s\", AT_FDCWD<%s>, \"\", %s) = %s\n",
	       path, cwd,
	       f_flags_str "|" t_flags_str "|" set_group_str "|" beneath_str,
	       errstr);

	k_move_mount(-100, empty, -1, path, -1);
	printf("move_mount(AT_FDCWD<%s>, \"\", -1, \"%s\", %s) = %s\n",
	       cwd, path,
	       f_flags_str "|" t_flags_str "|" set_group_str "|" beneath_str
	       "|0xfffffc88",
	       errstr);

	puts("+++ exited with 0 +++");
	return 0;
}