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
|
/*
* Copyright (C) 2014-2022 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 <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
void netfilter_netlock(pid_t pid) {
EUID_ASSERT();
// give the sandbox a chance to start up before entering the network namespace
sleep(1);
enter_network_namespace(pid);
char *flog;
if (asprintf(&flog, "/run/firejail/network/%d-netlock", getpid()) == -1)
errExit("asprintf");
FILE *fp = fopen(flog, "w");
if (!fp)
errExit("fopen");
fclose(fp);
// try to find a X terminal
char *terminal = NULL;
if (access("/usr/bin/xterm", X_OK) == 0)
terminal = "/usr/bin/xterm";
else if (access("/usr/bin/lxterminal", X_OK) == 0)
terminal = "/usr/bin/lxterminal";
else if (access("/usr/bin/xfce4-terminal", X_OK) == 0)
terminal = "/usr/bin/xfce4-terminal";
else if (access("/usr/bin/konsole", X_OK) == 0)
terminal = "/usr/bin/konsole";
// problem: newer gnome-terminal versions don't support -e command line option???
// same for mate-terminal
if (isatty(STDIN_FILENO))
terminal = NULL;
if (terminal) {
pid_t p = fork();
if (p == -1)
; // run without terminal logger
else if (p == 0) { // child
drop_privs(0);
env_apply_all();
umask(orig_umask);
char *cmd;
if (asprintf(&cmd, "%s -e \"%s/firejail/fnettrace --tail --log=%s\"", terminal, LIBDIR, flog) == -1)
errExit("asprintf");
int rv = system(cmd);
(void) rv;
exit(0);
}
}
char *cmd;
if (asprintf(&cmd, "%s/firejail/fnettrace --netfilter --log=%s", LIBDIR, flog) == -1)
errExit("asprintf");
free(flog);
//************************
// build command
//************************
char *arg[4];
arg[0] = "/bin/sh";
arg[1] = "-c";
arg[2] = cmd;
arg[3] = NULL;
clearenv();
sbox_exec_v(SBOX_ROOT | SBOX_CAPS_NETWORK | SBOX_SECCOMP, arg);
// it will never get here!!
}
void netfilter_trace(pid_t pid, const char *cmd) {
EUID_ASSERT();
// a pid of 0 means the main system network namespace
if (pid)
enter_network_namespace(pid);
//************************
// build command
//************************
char *arg[4];
arg[0] = "/bin/sh";
arg[1] = "-c";
arg[2] = (char *) cmd;
arg[3] = NULL;
clearenv();
sbox_exec_v(SBOX_ROOT | SBOX_CAPS_NETWORK | SBOX_SECCOMP, arg);
// it will never get here!!
}
void check_netfilter_file(const char *fname) {
EUID_ASSERT();
char *tmp = strdup(fname);
if (!tmp)
errExit("strdup");
char *ptr = strchr(tmp, ',');
if (ptr)
*ptr = '\0';
invalid_filename(tmp, 0); // no globbing
if (is_dir(tmp) || is_link(tmp) || strstr(tmp, "..") || access(tmp, R_OK )) {
fprintf(stderr, "Error: invalid network filter file %s\n", tmp);
exit(1);
}
free(tmp);
}
void netfilter(const char *fname) {
// find iptables command
struct stat s;
char *iptables = NULL;
char *iptables_restore = NULL;
if (stat("/sbin/iptables", &s) == 0) {
iptables = "/sbin/iptables";
iptables_restore = "/sbin/iptables-restore";
}
else if (stat("/usr/sbin/iptables", &s) == 0) {
iptables = "/usr/sbin/iptables";
iptables_restore = "/usr/sbin/iptables-restore";
}
if (iptables == NULL || iptables_restore == NULL) {
fprintf(stderr, "Error: iptables command not found, netfilter not configured\n");
return;
}
if (arg_debug)
printf("Installing firewall\n");
// create an empty user-owned SBOX_STDIN_FILE
create_empty_file_as_root(SBOX_STDIN_FILE, 0644);
if (set_perms(SBOX_STDIN_FILE, getuid(), getgid(), 0644))
errExit("set_perms");
if (fname == NULL) {
if (netfilter_default)
sbox_run(SBOX_USER| SBOX_CAPS_NONE | SBOX_SECCOMP, 3, PATH_FNETFILTER, netfilter_default, SBOX_STDIN_FILE);
else
sbox_run(SBOX_USER| SBOX_CAPS_NONE | SBOX_SECCOMP, 2, PATH_FNETFILTER, SBOX_STDIN_FILE);
}
else
sbox_run(SBOX_USER| SBOX_CAPS_NONE | SBOX_SECCOMP, 3, PATH_FNETFILTER, fname, SBOX_STDIN_FILE);
// first run of iptables on this platform installs a number of kernel modules such as ip_tables, x_tables, iptable_filter
// we run this command with caps and seccomp disabled in order to allow the loading of these modules
sbox_run(SBOX_ROOT | SBOX_STDIN_FROM_FILE, 1, iptables_restore);
unlink(SBOX_STDIN_FILE);
// debug
if (arg_debug)
sbox_run(SBOX_ROOT | SBOX_CAPS_NETWORK | SBOX_SECCOMP, 2, iptables, "-vL");
return;
}
void netfilter6(const char *fname) {
if (fname == NULL)
return;
// find iptables command
char *ip6tables = NULL;
char *ip6tables_restore = NULL;
struct stat s;
if (stat("/sbin/ip6tables", &s) == 0) {
ip6tables = "/sbin/ip6tables";
ip6tables_restore = "/sbin/ip6tables-restore";
}
else if (stat("/usr/sbin/ip6tables", &s) == 0) {
ip6tables = "/usr/sbin/ip6tables";
ip6tables_restore = "/usr/sbin/ip6tables-restore";
}
if (ip6tables == NULL || ip6tables_restore == NULL) {
fprintf(stderr, "Error: ip6tables command not found, netfilter6 not configured\n");
return;
}
if (arg_debug)
printf("Installing IPv6 firewall\n");
// create an empty user-owned SBOX_STDIN_FILE
create_empty_file_as_root(SBOX_STDIN_FILE, 0644);
if (set_perms(SBOX_STDIN_FILE, getuid(), getgid(), 0644))
errExit("set_perms");
sbox_run(SBOX_USER| SBOX_CAPS_NONE | SBOX_SECCOMP, 3, PATH_FNETFILTER, fname, SBOX_STDIN_FILE);
// first run of iptables on this platform installs a number of kernel modules such as ip_tables, x_tables, iptable_filter
// we run this command with caps and seccomp disabled in order to allow the loading of these modules
sbox_run(SBOX_ROOT | SBOX_STDIN_FROM_FILE, 1, ip6tables_restore);
unlink(SBOX_STDIN_FILE);
// debug
if (arg_debug)
sbox_run(SBOX_ROOT | SBOX_CAPS_NETWORK | SBOX_SECCOMP, 2, ip6tables, "-vL");
return;
}
void netfilter_print(pid_t pid, int ipv6) {
EUID_ASSERT();
enter_network_namespace(pid);
// find iptables executable
char *iptables = NULL;
// char *iptables_restore = NULL;
struct stat s;
if (ipv6) {
if (stat("/sbin/ip6tables", &s) == 0)
iptables = "/sbin/ip6tables";
else if (stat("/usr/sbin/ip6tables", &s) == 0)
iptables = "/usr/sbin/ip6tables";
}
else {
if (stat("/sbin/iptables", &s) == 0)
iptables = "/sbin/iptables";
else if (stat("/usr/sbin/iptables", &s) == 0)
iptables = "/usr/sbin/iptables";
}
if (iptables == NULL) {
fprintf(stderr, "Error: iptables command not found\n");
exit(1);
}
sbox_run(SBOX_ROOT | SBOX_CAPS_NETWORK | SBOX_SECCOMP, 2, iptables, "-vL");
}
|