aboutsummaryrefslogtreecommitdiffstats
path: root/src/firejail/fs_lib.c
blob: d865887929166b0b4fe6c771420707ae093f6aec (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
/*
 * Copyright (C) 2017 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 <elf.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#ifdef __LP64__
#define Elf_Ehdr Elf64_Ehdr
#define Elf_Phdr Elf64_Phdr
#define Elf_Shdr Elf64_Shdr
#define Elf_Dyn Elf64_Dyn
#else
#define Elf_Ehdr Elf32_Ehdr
#define Elf_Phdr Elf32_Phdr
#define Elf_Shdr Elf32_Shdr
#define Elf_Dyn Elf32_Dyn
#endif

static const char * const lib_paths[] = {
	"/lib",
	"/lib/x86_64-linux-gnu",
	"/lib64",
	"/usr/lib",
	"/usr/lib/x86_64-linux-gnu",
	LIBDIR,
	"/usr/local/lib",
	NULL
};

static void copy_libs_for_lib(const char *lib, const char *private_run_dir);

static void duplicate(const char *fname, const char *private_run_dir) {
	if (arg_debug)
		printf("copying %s to private %s\n", fname, private_run_dir);
	sbox_run(SBOX_ROOT| SBOX_SECCOMP, 4, PATH_FCOPY, "--follow-link", fname, private_run_dir);
}

static void copy_libs_for_exe(const char *exe, const char *private_run_dir) {
	if (arg_debug)
		printf("copy libs for %s\n", exe);
	int f;
	f = open(exe, O_RDONLY);
	if (f < 0)
		return;
	struct stat s;
	char *base = NULL;
	if (fstat(f, &s) == -1)
		goto error_close;
	base = mmap(0, s.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, f, 0);
	if (base == MAP_FAILED)
		goto error_close;

	Elf_Ehdr *ebuf = (Elf_Ehdr *)base;
	if (strncmp((const char *)ebuf->e_ident, ELFMAG, SELFMAG) != 0)
		goto close;

	Elf_Phdr *pbuf = (Elf_Phdr *)(base + sizeof(*ebuf));
	while (ebuf->e_phnum-- > 0) {
		switch (pbuf->p_type) {
		case PT_INTERP:
			// dynamic loader ld-linux.so
			duplicate(base + pbuf->p_offset, private_run_dir);
			break;
		}
		pbuf++;
	}

	Elf_Shdr *sbuf = (Elf_Shdr *)(base + ebuf->e_shoff);

	// Find strings section
	char *strbase = NULL;
	int sections = ebuf->e_shnum;
	while (sections-- > 0) {
		if (sbuf->sh_type == SHT_STRTAB) {
			strbase = base + sbuf->sh_offset;
			break;
		}
		sbuf++;
	}
	if (strbase == NULL)
		goto error_close;

	// Find dynamic section
	sections = ebuf->e_shnum;
	while (sections-- > 0) {
		if (sbuf->sh_type == SHT_DYNAMIC) {
			// Find DT_NEEDED tags
			Elf_Dyn *dbuf = (Elf_Dyn *)(base + sbuf->sh_offset);
			while (sbuf->sh_size >= sizeof(*dbuf)) {
				if (dbuf->d_tag == DT_NEEDED) {
					const char *lib = strbase + dbuf->d_un.d_ptr;
					copy_libs_for_lib(lib, private_run_dir);
				}
				sbuf->sh_size -= sizeof(*dbuf);
				dbuf++;
			}
		}
		sbuf++;
	}
	goto close;

 error_close:
	perror("copy libs");
 close:
	if (base)
		munmap(base, s.st_size);
	close(f);
}

static void copy_libs_for_lib(const char *lib, const char *private_run_dir) {
	int i;
	for (i = 0; lib_paths[i]; i++) {
		char *fname;
		if (asprintf(&fname, "%s/%s", lib_paths[i], lib) == -1)
			errExit("asprintf");
		if (access(fname, R_OK) == 0) {
			char *dst;
			if (asprintf(&dst, "%s/%s", private_run_dir, lib) == -1)
				errExit("asprintf");

			if (access(dst, R_OK) != 0) {
				duplicate(fname, private_run_dir);
				// libs may need other libs
				copy_libs_for_exe(fname, private_run_dir);
			}
			free(dst);
			free(fname);
			return;
		}
		free(fname);
	}
	errExit("library not found");
}

void fs_private_lib(void) {
	char *private_list = cfg.lib_private_keep;

	// create /run/firejail/mnt/lib directory
	mkdir_attr(RUN_LIB_DIR, 0755, 0, 0);

	// copy the libs in the new lib directory for the main exe
	if (cfg.original_program_index > 0)
		copy_libs_for_exe(cfg.original_argv[cfg.original_program_index], RUN_LIB_DIR);

	// for the shell
	if (!arg_shell_none)
		copy_libs_for_exe(cfg.shell, RUN_LIB_DIR);

	// for the listed libs
	if (private_list && *private_list != '\0') {
		if (arg_debug)
			printf("Copying extra files (%s) in the new lib directory:\n", private_list);

		char *dlist = strdup(private_list);
		if (!dlist)
			errExit("strdup");

		char *ptr = strtok(dlist, ",");
		copy_libs_for_lib(ptr, RUN_LIB_DIR);

		while ((ptr = strtok(NULL, ",")) != NULL)
			copy_libs_for_lib(ptr, RUN_LIB_DIR);
		free(dlist);
		fs_logger_print();
	}

	// for our trace and tracelog libs
	if (arg_trace)
		duplicate(LIBDIR "/firejail/libtrace.so", RUN_LIB_DIR);
	else if (arg_tracelog)
		duplicate(LIBDIR "/firejail/libtracelog.so", RUN_LIB_DIR);

	if (arg_debug)
		printf("Mount-bind %s on top of /lib /lib64 /usr/lib\n", RUN_LIB_DIR);
	if (mount(RUN_LIB_DIR, "/lib", NULL, MS_BIND|MS_REC, NULL) < 0 ||
	    mount(NULL, "/lib", NULL, MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NODEV|MS_REC, NULL) < 0)
		errExit("mount bind");
	fs_logger("mount /lib");
	if (mount(RUN_LIB_DIR, "/lib64", NULL, MS_BIND|MS_REC, NULL) < 0 ||
	    mount(NULL, "/lib64", NULL, MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NODEV|MS_REC, NULL) < 0)
		errExit("mount bind");
	fs_logger("mount /lib64");
	if (mount(RUN_LIB_DIR, "/usr/lib", NULL, MS_BIND|MS_REC, NULL) < 0 ||
	    mount(NULL, "/usr/lib", NULL, MS_BIND|MS_REMOUNT|MS_NOSUID|MS_NODEV|MS_REC, NULL) < 0)
		errExit("mount bind");
	fs_logger("mount /usr/lib");
}