aboutsummaryrefslogtreecommitdiffstats
path: root/src/firejail/mountinfo.c
blob: 64a94bd849a71fe5f80c73b2f32ea1465b1e020a (plain) (blame)
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
280
281
/*
 * Copyright (C) 2014-2021 Firejail Authors
 *
 * This file is part of firejail project
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "firejail.h"

#include <fcntl.h>
#ifndef O_PATH
#define O_PATH 010000000
#endif

#define MAX_BUF 4096

static char mbuf[MAX_BUF];
static MountData mdata;


// Convert octal escape sequence to decimal value
static int read_oct(const char *path) {
	int dec = 0;
	int digit, i;
	// there are always exactly three octal digits
	for (i = 1; i < 4; i++) {
		digit = *(path + i);
		if (digit < '0' || digit > '7') {
			fprintf(stderr, "Error: cannot read /proc/self/mountinfo\n");
			exit(1);
		}
		dec = (dec << 3) + (digit - '0');
	}
	return dec;
}

// Restore empty spaces in pathnames extracted from /proc/self/mountinfo
static void unmangle_path(char *path) {
	char *p = strchr(path, '\\');
	if (p && read_oct(p) == ' ') {
		*p = ' ';
		int i = 3;
		do {
			p++;
			if (*(p + i) == '\\' && read_oct(p + i) == ' ') {
				*p = ' ';
				i += 3;
			}
			else
				*p = *(p + i);
		} while (*p);
	}
}

// Parse a line from /proc/self/mountinfo,
// the function does an exit(1) if anything goes wrong.
static void parse_line(char *line, MountData *output) {
	assert(line && output);
	memset(output, 0, sizeof(*output));
	// extract mount id, filesystem name, directory and filesystem types
	// examples:
	//	587 543 8:1 /tmp /etc rw,relatime master:1 - ext4 /dev/sda1 rw,errors=remount-ro,data=ordered
	//		output.mountid: 587
	//		output.fsname: /tmp
	//		output.dir: /etc
	//		output.fstype: ext4
	//	585 564 0:76 / /home/netblue/.cache rw,nosuid,nodev - tmpfs tmpfs rw
	//		output.mountid: 585
	//		output.fsname: /
	//		output.dir: /home/netblue/.cache
	//		output.fstype: tmpfs

	char *ptr = strtok(line, " ");
	if (!ptr)
		goto errexit;
	if (ptr != line)
		goto errexit;
	output->mountid = atoi(ptr);
	int cnt = 1;

	while ((ptr = strtok(NULL, " ")) != NULL) {
		cnt++;
		if (cnt == 4)
			output->fsname = ptr;
		else if (cnt == 5) {
			output->dir = ptr;
			break;
		}
	}

	ptr = strtok(NULL, "-");
	if (!ptr)
		goto errexit;

	ptr = strtok(NULL, " ");
	if (!ptr)
		goto errexit;
	output->fstype = ptr++;


	if (output->mountid == 0 ||
	    output->fsname == NULL ||
	    output->dir == NULL ||
	    output->fstype == NULL)
		goto errexit;

	// restore empty spaces
	unmangle_path(output->fsname);
	unmangle_path(output->dir);

	return;

errexit:
	fprintf(stderr, "Error: cannot read /proc/self/mountinfo\n");
	exit(1);
}

// The return value points to a static area, and will be overwritten by subsequent calls.
MountData *get_last_mount(void) {
	// open /proc/self/mountinfo
	FILE *fp = fopen("/proc/self/mountinfo", "re");
	if (!fp) {
		fprintf(stderr, "Error: cannot read /proc/self/mountinfo\n");
		exit(1);
	}

	mbuf[0] = '\0';
	// go to the last line
	while (fgets(mbuf, MAX_BUF, fp));
	fclose(fp);
	if (arg_debug)
		printf("%s", mbuf);

	parse_line(mbuf, &mdata);

	if (arg_debug)
		printf("mountid=%d fsname=%s dir=%s fstype=%s\n", mdata.mountid, mdata.fsname, mdata.dir, mdata.fstype);
	return &mdata;
}

// Extract the mount id from /proc/self/fdinfo and return it.
int get_mount_id(const char *path) {
	EUID_ASSERT();
	assert(path);

	int fd = open(path, O_PATH|O_CLOEXEC);
	if (fd == -1)
		return -1;

	char *fdinfo;
	if (asprintf(&fdinfo, "/proc/self/fdinfo/%d", fd) == -1)
		errExit("asprintf");
	EUID_ROOT();
	FILE *fp = fopen(fdinfo, "re");
	EUID_USER();
	free(fdinfo);
	if (!fp)
		goto errexit;

	// read the file
	char buf[MAX_BUF];
	if (fgets(buf, MAX_BUF, fp) == NULL)
		goto errexit;
	do {
		if (strncmp(buf, "mnt_id:", 7) == 0) {
			char *ptr = buf + 7;
			while (*ptr != '\0' && (*ptr == ' ' || *ptr == '\t')) {
				ptr++;
			}
			if (*ptr == '\0')
				goto errexit;
			fclose(fp);
			close(fd);
			return atoi(ptr);
		}
	} while (fgets(buf, MAX_BUF, fp));

	// fallback, kernels older than 3.15 don't expose the mount id in this place
	fclose(fp);
	close(fd);
	return -2;

errexit:
	fprintf(stderr, "Error: cannot read proc file\n");
	exit(1);
}

// Check /proc/self/mountinfo if path contains any mounts points.
// Returns an array that can be iterated over for recursive remounting.
char **build_mount_array(const int mount_id, const char *path) {
	assert(path);

	// open /proc/self/mountinfo
	FILE *fp = fopen("/proc/self/mountinfo", "re");
	if (!fp) {
		fprintf(stderr, "Error: cannot read /proc/self/mountinfo\n");
		exit(1);
	}

	// array to be returned
	size_t cnt = 0;
	size_t size = 32;
	char **rv = malloc(size * sizeof(*rv));
	if (!rv)
		errExit("malloc");

	// read /proc/self/mountinfo
	size_t pathlen = strlen(path);
	char buf[MAX_BUF];
	MountData mntp;
	int found = 0;

	if (fgets(buf, MAX_BUF, fp) == NULL) {
		fprintf(stderr, "Error: cannot read /proc/self/mountinfo\n");
		exit(1);
	}
	do {
		parse_line(buf, &mntp);
		// find mount point with mount id
		if (!found) {
			if (mntp.mountid == mount_id) {
				// give up if mount id has been reassigned,
				// don't remount blacklisted path
				if (strncmp(mntp.dir, path, strlen(mntp.dir)) ||
				    strstr(mntp.fsname, "firejail.ro.dir") ||
				    strstr(mntp.fsname, "firejail.ro.file"))
					    break;

				rv[cnt] = strdup(path);
				if (rv[cnt] == NULL)
					errExit("strdup");
				cnt++;
				found = 1;
				continue;
			}
			continue;
		}
		// from here on add all mount points below path,
		// don't remount blacklisted paths
		if (strncmp(mntp.dir, path, pathlen) == 0 &&
		    mntp.dir[pathlen] == '/' &&
		    strstr(mntp.fsname, "firejail.ro.dir") == NULL &&
		    strstr(mntp.fsname, "firejail.ro.file") == NULL) {

			if (cnt == size) {
				size *= 2;
				rv = realloc(rv, size * sizeof(*rv));
				if (!rv)
					errExit("realloc");
			}
			rv[cnt] = strdup(mntp.dir);
			if (rv[cnt] == NULL)
				errExit("strdup");
			cnt++;
		}
	} while (fgets(buf, MAX_BUF, fp));

	if (cnt == size) {
		size++;
		rv = realloc(rv, size * sizeof(*rv));
		if (!rv)
			errExit("realloc");
	}
	rv[cnt] = NULL; // end of the array

	fclose(fp);
	return rv;
}