summaryrefslogtreecommitdiff
path: root/src/detached_mounts_propagation.c
blob: 17db2c026ebfa283a7e761b7664f32ddf136372e (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* SPDX-License-Identifier: LGPL-2.1+ */
/*
 * Copyright (c) 2021 Christian Brauner <christian.brauner@ubuntu.com>
 * All Rights Reserved.
 *
 * Regression test to verify that creating a series of detached mounts,
 * attaching them to the filesystem, and unmounting them does not trigger an
 * integer overflow in ns->mounts causing the kernel to block any new mounts in
 * count_mounts() and returning ENOSPC because it falsely assumes that the
 * maximum number of mounts in the mount namespace has been reached, i.e. it
 * thinks it can't fit the new mounts into the mount namespace anymore.
 */

#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <sched.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

#include "vfs/missing.h"

static bool is_shared_mountpoint(const char *path)
{
	bool shared = false;
	FILE *f = NULL;
	char *line = NULL;
	int i;
	size_t len = 0;

	f = fopen("/proc/self/mountinfo", "re");
	if (!f)
		return 0;

	while (getline(&line, &len, f) > 0) {
		char *slider1, *slider2;

		for (slider1 = line, i = 0; slider1 && i < 4; i++)
			slider1 = strchr(slider1 + 1, ' ');

		if (!slider1)
			continue;

		slider2 = strchr(slider1 + 1, ' ');
		if (!slider2)
			continue;

		*slider2 = '\0';
		if (strcmp(slider1 + 1, path) == 0) {
			/* This is the path. Is it shared? */
			slider1 = strchr(slider2 + 1, ' ');
			if (slider1 && strstr(slider1, "shared:")) {
				shared = true;
				break;
			}
		}
	}
	fclose(f);
	free(line);

	return shared;
}

static void usage(void)
{
	const char *text = "mount-new [--recursive] <base-dir>\n";
	fprintf(stderr, "%s", text);
	_exit(EXIT_SUCCESS);
}

#define exit_usage(format, ...)                              \
	({                                                   \
		fprintf(stderr, format "\n", ##__VA_ARGS__); \
		usage();                                     \
	})

#define exit_log(format, ...)                                \
	({                                                   \
		fprintf(stderr, format "\n", ##__VA_ARGS__); \
		exit(EXIT_FAILURE);                          \
	})

static const struct option longopts[] = {
	{"help",	no_argument,		0,	'a'},
	{ NULL,		no_argument,		0,	 0 },
};

int main(int argc, char *argv[])
{
	int exit_code = EXIT_SUCCESS, index = 0;
	int dfd, fd_tree, new_argc, ret, i;
	char *base_dir;
	char *const *new_argv;
	char target[PATH_MAX];

	while ((ret = getopt_long_only(argc, argv, "", longopts, &index)) != -1) {
		switch (ret) {
		case 'a':
			/* fallthrough */
		default:
			usage();
		}
	}

	new_argv = &argv[optind];
	new_argc = argc - optind;
	if (new_argc < 1)
		exit_usage("Missing base directory\n");
	base_dir = new_argv[0];

	if (*base_dir != '/')
		exit_log("Please specify an absolute path");

	/* Ensure that target is a shared mountpoint. */
	if (!is_shared_mountpoint(base_dir))
		exit_log("Please ensure that \"%s\" is a shared mountpoint", base_dir);

	ret = unshare(CLONE_NEWNS);
	if (ret < 0)
		exit_log("%m - Failed to create new mount namespace");

	ret = mount(NULL, base_dir, NULL, MS_REC | MS_SHARED, NULL);
	if (ret < 0)
		exit_log("%m - Failed to make base_dir shared mountpoint");

	dfd = open(base_dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
	if (dfd < 0)
		exit_log("%m - Failed to open base directory \"%s\"", base_dir);

	ret = mkdirat(dfd, "detached-move-mount", 0755);
	if (ret < 0)
		exit_log("%m - Failed to create required temporary directories");

	ret = snprintf(target, sizeof(target), "%s/detached-move-mount", base_dir);
	if (ret < 0 || (size_t)ret >= sizeof(target))
		exit_log("%m - Failed to assemble target path");

	/*
	 * Having a mount table with 10000 mounts is already quite excessive
	 * and shoult account even for weird test systems.
	 */
	for (i = 0; i < 10000; i++) {
		fd_tree = sys_open_tree(dfd, "detached-move-mount",
					OPEN_TREE_CLONE |
					OPEN_TREE_CLOEXEC |
					AT_EMPTY_PATH);
		if (fd_tree < 0) {
			if (errno == ENOSYS) /* New mount API not (fully) supported. */
				break;

			fprintf(stderr, "%m - Failed to open %d(detached-move-mount)", dfd);
			exit_code = EXIT_FAILURE;
			break;
		}

		ret = sys_move_mount(fd_tree, "", dfd, "detached-move-mount", MOVE_MOUNT_F_EMPTY_PATH);
		if (ret < 0) {
			if (errno == ENOSPC)
				fprintf(stderr, "%m - Buggy mount counting");
			else if (errno == ENOSYS) /* New mount API not (fully) supported. */
				break;
			else
				fprintf(stderr, "%m - Failed to attach mount to %d(detached-move-mount)", dfd);
			exit_code = EXIT_FAILURE;
			break;
		}
		close(fd_tree);

		ret = umount2(target, MNT_DETACH);
		if (ret < 0) {
			fprintf(stderr, "%m - Failed to unmount %s", target);
			exit_code = EXIT_FAILURE;
			break;
		}
	}

	(void)unlinkat(dfd, "detached-move-mount", AT_REMOVEDIR);
	close(dfd);

	exit(exit_code);
}