File: mountcomposefs.c

package info (click to toggle)
composefs 1.0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,040 kB
  • sloc: ansic: 9,004; sh: 416; python: 225; makefile: 5
file content (279 lines) | stat: -rw-r--r-- 6,812 bytes parent folder | download
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* lcfs
   Copyright (C) 2023 Alexander Larsson <alexl@redhat.com>

   SPDX-License-Identifier: GPL-2.0-or-later OR Apache-2.0
*/

#define _GNU_SOURCE

#include "config.h"

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <linux/limits.h>
#include <linux/loop.h>
#include <linux/mount.h>
#include <linux/fsverity.h>

#include "libcomposefs/lcfs-mount.h"
#include "libcomposefs/lcfs-utils.h"

static void usage(const char *argv0)
{
	const char *bin = gnu_basename(argv0);
	fprintf(stderr,
		"usage: %s [-t type] [-o opt[,opts..]] IMAGE MOUNTPOINT\n"
		"Example:\n"
		"  %s -o basedir=/composefs/objects exampleimage.cfs /mnt/exampleimage\n"
		"or, as a mount helper:\n"
		"  mount -t composefs -o basedir=/composefs/objects exampleimage.cfs /mnt/exampleimage\n"
		"\n"
		"Supported options:\n"
		"  basedir=PATH[:PATH]    Specify location of basedir(s)\n"
		"  digest=DIGEST          Specify required image digest\n"
		"  idmap=PATH             Specify path to a user namespace whose ID mapping should be used\n"
		"  verity                 Require all files to have specified and valid fs-verity digests\n"
		"  tryverity              If supported by kernel, require fs-verity\n"
		"  ro                     Read only\n"
		"  rw                     Read/write\n"
		"  upperdir               Overlayfs upperdir\n"
		"  workdir                Overlayfs workdir\n",
		bin, bin);
}

static void unescape_option(char *s)
{
	char *d = s;

	for (;; s++, d++) {
		if (*s == '\\')
			s++;
		*d = *s;
		if (!*s)
			break;
	}
}

static char *parse_option(char *options, char **key, char **value)
{
	char *p, *equal, *next;
	;

	equal = NULL;
	for (p = options; *p; p++) {
		if (*p == '=' && equal == NULL)
			equal = p;
		else if (*p == '\\' && p[1] != 0)
			p++;
		else if (*p == ',')
			break;
	}

	if (*p)
		next = p + 1;
	else
		next = NULL;
	*p = 0;

	*key = options;
	if (equal) {
		*equal = 0;
		*value = equal + 1;
		unescape_option(*value);
	} else {
		*value = NULL;
	}

	return next;
}

int main(int argc, char **argv)
{
	struct lcfs_mount_options_s options = { 0 };
	const char *bin = argv[0];
	char *mount_options = NULL;
	const char *image_path = NULL;
	const char *mount_path = NULL;
	const char *opt_basedir = NULL;
	const char *opt_digest = NULL;
	const char *opt_idmap = NULL;
	const char *opt_upperdir = NULL;
	const char *opt_workdir = NULL;
	bool opt_verity = false;
	bool opt_tryverity = false;
	bool opt_ro = false;
	int opt, fd, res, userns_fd;

	while ((opt = getopt(argc, argv, "ht:o:")) != -1) {
		switch (opt) {
		case 't':
			if (strcmp(optarg, "composefs") != 0)
				errx(EXIT_FAILURE, "Unsupported fs type '%s'\n",
				     optarg);
			break;
		case 'o':
			mount_options = optarg;
			break;
		case 'h':
			usage(bin);
			exit(0);
		default:
			usage(bin);
			exit(1);
		}
	}

	argv += optind;
	argc -= optind;

	if (argc < 1) {
		fprintf(stderr, "No source path specified\n");
		usage(bin);
		exit(1);
	}
	image_path = argv[0];

	if (argc < 2) {
		fprintf(stderr, "No mount path specified\n");
		usage(bin);
		exit(1);
	}
	mount_path = argv[1];

	while (mount_options) {
		char *key, *value;
		mount_options = parse_option(mount_options, &key, &value);

		if (strcmp("basedir", key) == 0) {
			if (value == NULL)
				errx(EXIT_FAILURE,
				     "No value specified for basedir option\n");
			opt_basedir = value;
		} else if (strcmp("digest", key) == 0) {
			if (value == NULL)
				errx(EXIT_FAILURE,
				     "No value specified for digest option\n");
			opt_digest = value;
		} else if (strcmp("verity", key) == 0) {
			opt_verity = true;
		} else if (strcmp("tryverity", key) == 0) {
			opt_tryverity = true;
		} else if (strcmp("upperdir", key) == 0) {
			if (value == NULL)
				errx(EXIT_FAILURE,
				     "No value specified for upperdir option\n");
			opt_upperdir = value;
		} else if (strcmp("workdir", key) == 0) {
			if (value == NULL)
				errx(EXIT_FAILURE,
				     "No value specified for workdir option\n");
			opt_workdir = value;
		} else if (strcmp("idmap", key) == 0) {
			if (value == NULL)
				errx(EXIT_FAILURE,
				     "No value specified for idmap option\n");
			opt_idmap = value;
		} else if (strcmp("rw", key) == 0) {
			opt_ro = false;
		} else if (strcmp("ro", key) == 0) {
			opt_ro = true;
		} else {
			errx(EXIT_FAILURE, "Unsupported option %s\n", key);
		}
	}

	if (opt_basedir != NULL) {
		int i;
		char *str, *token, *saveptr;

		options.n_objdirs = 1;
		for (str = (char *)opt_basedir; *str; str++) {
			if (*str == ':')
				options.n_objdirs++;
		}

		options.objdirs = calloc(options.n_objdirs, sizeof(char *));
		if (options.objdirs == NULL)
			errx(EXIT_FAILURE, "Out of memory\n");

		for (i = 0, str = (char *)opt_basedir; i < options.n_objdirs;
		     i++, str = NULL) {
			token = strtok_r(str, ":", &saveptr);
			if (token == NULL)
				errx(EXIT_FAILURE,
				     "Value for basedir option has incorrect format\n");
			options.objdirs[i] = token;
		}
	}

	if (options.n_objdirs == 0) {
		fprintf(stderr, "No object dirs specified\n");
		usage(bin);
		exit(1);
	}

	if ((opt_upperdir && !opt_workdir) || (!opt_upperdir && opt_workdir)) {
		errx(EXIT_FAILURE,
		     "Both workdir and upperdir must be specified if used\n");
	}
	options.upperdir = opt_upperdir;
	options.workdir = opt_workdir;

	options.expected_fsverity_digest = opt_digest;

	if (opt_verity || opt_digest != NULL)
		options.flags |= LCFS_MOUNT_FLAGS_REQUIRE_VERITY;
	else if (opt_tryverity)
		options.flags |= LCFS_MOUNT_FLAGS_TRY_VERITY;
	if (opt_ro)
		options.flags |= LCFS_MOUNT_FLAGS_READONLY;

	if (opt_idmap != NULL) {
		userns_fd = open(opt_idmap, O_RDONLY | O_CLOEXEC | O_NOCTTY);
		if (userns_fd < 0)
			errx(EXIT_FAILURE, "Failed to open userns %s: %s\n",
			     opt_idmap, strerror(errno));
		options.flags |= LCFS_MOUNT_FLAGS_IDMAP;
		options.idmap_fd = userns_fd;
	}

	fd = open(image_path, O_RDONLY | O_CLOEXEC);
	if (fd < 0)
		errx(EXIT_FAILURE, "Failed to open %s: %s\n", image_path,
		     strerror(errno));

	res = lcfs_mount_fd(fd, mount_path, &options);
	if (res < 0) {
		int errsv = errno;

		if (errsv == ENOVERITY)
			errx(EXIT_FAILURE,
			     "Failed to mount composefs %s: Image has no fs-verity\n",
			     image_path);
		else if (errsv == EWRONGVERITY)
			errx(EXIT_FAILURE,
			     "Failed to mount composefs %s: Image has wrong fs-verity\n",
			     image_path);
		else if (errsv == ENOSIGNATURE)
			errx(EXIT_FAILURE,
			     "Failed to mount composefs %s: Image was not signed\n",
			     image_path);

		errx(EXIT_FAILURE, "Failed to mount composefs %s: %s\n",
		     image_path, strerror(errno));
	}

	free(options.objdirs);

	return 0;
}