-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfirewall.cc
More file actions
409 lines (356 loc) · 12.9 KB
/
firewall.cc
File metadata and controls
409 lines (356 loc) · 12.9 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "permission_broker/firewall.h"
#include <arpa/inet.h>
#include <linux/capability.h>
#include <netinet/in.h>
#include <string>
#include <vector>
#include <base/bind.h>
#include <base/bind_helpers.h>
#include <base/callback.h>
#include <base/logging.h>
#include <base/strings/string_number_conversions.h>
#include <base/strings/string_util.h>
#include <base/strings/stringprintf.h>
#include <brillo/minijail/minijail.h>
namespace {
// Interface names must be shorter than 'IFNAMSIZ' chars.
// See http://man7.org/linux/man-pages/man7/netdevice.7.html
// 'IFNAMSIZ' is 16 in recent kernels.
// See https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/if.h?h=v4.14#n33
constexpr size_t kInterfaceNameSize = 16;
// Interface names are passed directly to the 'iptables' command. Rather than
// auditing 'iptables' source code to see how it handles malformed names,
// do some sanitization on the names beforehand.
bool IsValidInterfaceName(const std::string& iface) {
// |iface| should be shorter than |kInterfaceNameSize| chars and have only
// alphanumeric characters (embedded hypens and periods are also permitted).
if (iface.length() >= kInterfaceNameSize) {
return false;
}
if (base::StartsWith(iface, "-", base::CompareCase::SENSITIVE) ||
base::EndsWith(iface, "-", base::CompareCase::SENSITIVE) ||
base::StartsWith(iface, ".", base::CompareCase::SENSITIVE) ||
base::EndsWith(iface, ".", base::CompareCase::SENSITIVE)) {
return false;
}
for (auto c : iface) {
if (!std::isalnum(c) && (c != '-') && (c != '.')) {
return false;
}
}
return true;
}
} // namespace
namespace permission_broker {
const char kIpTablesPath[] = "/sbin/iptables";
const char kIp6TablesPath[] = "/sbin/ip6tables";
const char* ProtocolName(ProtocolEnum proto) {
switch (proto) {
case kProtocolTcp:
return "tcp";
case kProtocolUdp:
return "udp";
default:
NOTREACHED() << "Unexpected L4 protocol value " << proto;
return "unknown";
}
}
bool Firewall::AddAcceptRules(ProtocolEnum protocol,
uint16_t port,
const std::string& interface) {
if (port == 0U) {
LOG(ERROR) << "Port 0 is not a valid port";
return false;
}
if (!IsValidInterfaceName(interface)) {
LOG(ERROR) << "Invalid interface name '" << interface << "'";
return false;
}
if (!AddAcceptRule(kIpTablesPath, protocol, port, interface)) {
LOG(ERROR) << "Could not add ACCEPT rule using '" << kIpTablesPath << "'";
return false;
}
if (!AddAcceptRule(kIp6TablesPath, protocol, port, interface)) {
LOG(ERROR) << "Could not add ACCEPT rule using '" << kIp6TablesPath
<< "', aborting operation";
DeleteAcceptRule(kIpTablesPath, protocol, port, interface);
return false;
}
return true;
}
bool Firewall::DeleteAcceptRules(ProtocolEnum protocol,
uint16_t port,
const std::string& interface) {
if (port == 0U) {
LOG(ERROR) << "Port 0 is not a valid port";
return false;
}
if (!IsValidInterfaceName(interface)) {
LOG(ERROR) << "Invalid interface name '" << interface << "'";
return false;
}
bool ip4_success = DeleteAcceptRule(kIpTablesPath, protocol, port,
interface);
bool ip6_success = DeleteAcceptRule(kIp6TablesPath, protocol,
port, interface);
return ip4_success && ip6_success;
}
bool Firewall::AddIpv4ForwardRule(ProtocolEnum protocol,
const std::string& input_ip,
uint16_t port,
const std::string& interface,
const std::string& dst_ip,
uint16_t dst_port) {
if (!ModifyIpv4DNATRule(protocol, input_ip, port, interface, dst_ip, dst_port, "-I")) {
return false;
}
if (!ModifyIpv4ForwardChain(protocol, interface, dst_ip, dst_port, "-A")) {
ModifyIpv4DNATRule(protocol, input_ip, port, interface, dst_ip, dst_port, "-D");
return false;
}
return true;
}
bool Firewall::DeleteIpv4ForwardRule(ProtocolEnum protocol,
const std::string& input_ip,
uint16_t port,
const std::string& interface,
const std::string& dst_ip,
uint16_t dst_port) {
bool success = true;
if (!ModifyIpv4DNATRule(protocol, input_ip, port, interface, dst_ip, dst_port, "-D")) {
success = false;
}
if (!ModifyIpv4ForwardChain(protocol, interface, dst_ip, dst_port, "-D")) {
success = false;
}
return success;
}
bool Firewall::ModifyIpv4DNATRule(ProtocolEnum protocol,
const std::string& input_ip,
uint16_t port,
const std::string& interface,
const std::string& dst_ip,
uint16_t dst_port,
const std::string& operation) {
struct in_addr addr;
if (!input_ip.empty() && inet_pton(AF_INET, input_ip.c_str(), &addr) != 1) {
LOG(ERROR) << "Invalid input IPv4 address '" << input_ip << "'";
return false;
}
if (port == 0U) {
LOG(ERROR) << "Port 0 is not a valid port";
return false;
}
if (!IsValidInterfaceName(interface) || interface.empty()) {
LOG(ERROR) << "Invalid interface name '" << interface << "'";
return false;
}
if (inet_pton(AF_INET, dst_ip.c_str(), &addr) != 1) {
LOG(ERROR) << "Invalid destination IPv4 address '" << dst_ip << "'";
return false;
}
if (dst_port == 0U) {
LOG(ERROR) << "Destination port 0 is not a valid port";
return false;
}
// Only support deleting existing forwarding rules or inserting rules in the
// first position: ARC++ generic inbound DNAT rule always need to go last.
if (operation != "-I" && operation != "-D") {
LOG(ERROR) << "Invalid chain operation '" << operation << "'";
return false;
}
std::vector<std::string> argv{kIpTablesPath,
"-t",
"nat",
operation,
"PREROUTING",
"-i",
interface,
"-p", // protocol
ProtocolName(protocol)};
if (!input_ip.empty()) {
argv.push_back("-d"); // input destination ip
argv.push_back(input_ip);
}
argv.push_back("--dport"); // input destination port
argv.push_back(std::to_string(port));
argv.push_back("-j");
argv.push_back("DNAT");
argv.push_back("--to-destination"); // new output destination ip:port
argv.push_back(dst_ip + ":" + std::to_string(dst_port));
argv.push_back("-w"); // Wait for xtables lock.
return RunInMinijail(argv) == 0;
}
bool Firewall::ModifyIpv4ForwardChain(ProtocolEnum protocol,
const std::string& interface,
const std::string& dst_ip,
uint16_t dst_port,
const std::string& operation) {
if (!IsValidInterfaceName(interface) || interface.empty()) {
LOG(ERROR) << "Invalid interface name '" << interface << "'";
return false;
}
struct in_addr addr;
if (inet_pton(AF_INET, dst_ip.c_str(), &addr) != 1) {
LOG(ERROR) << "Invalid IPv4 destination address '" << dst_ip << "'";
return false;
}
if (dst_port == 0U) {
LOG(ERROR) << "Destination port 0 is not a valid port";
return false;
}
// Order does not matter for the FORWARD chain: both -A or -I are possible.
if (operation != "-A" && operation != "-I" && operation != "-D") {
LOG(ERROR) << "Invalid chain operation '" << operation << "'";
return false;
}
std::vector<std::string> argv{
kIpTablesPath,
"-t",
"filter",
operation,
"FORWARD",
"-i",
interface,
"-p", // protocol
ProtocolName(protocol),
"-d", // destination ip
dst_ip,
"--dport", // destination port
std::to_string(dst_port),
"-j",
"ACCEPT",
"-w"}; // Wait for xtables lock.
return RunInMinijail(argv) == 0;
}
bool Firewall::AddLoopbackLockdownRules(ProtocolEnum protocol, uint16_t port) {
if (port == 0U) {
LOG(ERROR) << "Port 0 is not a valid port";
return false;
}
if (!AddLoopbackLockdownRule(kIpTablesPath, protocol, port)) {
LOG(ERROR) << "Could not add loopback REJECT rule using '" << kIpTablesPath
<< "'";
return false;
}
if (!AddLoopbackLockdownRule(kIp6TablesPath, protocol, port)) {
LOG(ERROR) << "Could not add loopback REJECT rule using '" << kIp6TablesPath
<< "', aborting operation";
DeleteLoopbackLockdownRule(kIpTablesPath, protocol, port);
return false;
}
return true;
}
bool Firewall::DeleteLoopbackLockdownRules(ProtocolEnum protocol,
uint16_t port) {
if (port == 0U) {
LOG(ERROR) << "Port 0 is not a valid port";
return false;
}
bool ip4_success = DeleteLoopbackLockdownRule(kIpTablesPath, protocol, port);
bool ip6_success = DeleteLoopbackLockdownRule(kIp6TablesPath, protocol, port);
return ip4_success && ip6_success;
}
bool Firewall::AddAcceptRule(const std::string& executable_path,
ProtocolEnum protocol,
uint16_t port,
const std::string& interface) {
std::vector<std::string> argv{executable_path,
"-I", // insert
"INPUT",
"-p", // protocol
ProtocolName(protocol),
"--dport", // destination port
std::to_string(port)};
if (!interface.empty()) {
argv.push_back("-i"); // interface
argv.push_back(interface);
}
argv.push_back("-j");
argv.push_back("ACCEPT");
argv.push_back("-w"); // Wait for xtables lock.
return RunInMinijail(argv) == 0;
}
bool Firewall::DeleteAcceptRule(const std::string& executable_path,
ProtocolEnum protocol,
uint16_t port,
const std::string& interface) {
std::vector<std::string> argv{executable_path,
"-D", // delete
"INPUT",
"-p", // protocol
ProtocolName(protocol),
"--dport", // destination port
std::to_string(port)};
if (!interface.empty()) {
argv.push_back("-i"); // interface
argv.push_back(interface);
}
argv.push_back("-j");
argv.push_back("ACCEPT");
argv.push_back("-w"); // Wait for xtables lock.
return RunInMinijail(argv) == 0;
}
bool Firewall::AddLoopbackLockdownRule(const std::string& executable_path,
ProtocolEnum protocol,
uint16_t port) {
std::vector<std::string> argv{
executable_path,
"-I", // insert
"OUTPUT",
"-p", // protocol
ProtocolName(protocol),
"--dport", // destination port
std::to_string(port),
"-o", // output interface
"lo",
"-m", // match extension
"owner",
"!",
"--uid-owner",
"chronos",
"-j",
"REJECT",
"-w", // Wait for xtables lock.
};
return RunInMinijail(argv) == 0;
}
bool Firewall::DeleteLoopbackLockdownRule(const std::string& executable_path,
ProtocolEnum protocol,
uint16_t port) {
std::vector<std::string> argv{
executable_path,
"-D", // delete
"OUTPUT",
"-p", // protocol
ProtocolName(protocol),
"--dport", // destination port
std::to_string(port),
"-o", // output interface
"lo",
"-m", // match extension
"owner",
"!",
"--uid-owner",
"chronos",
"-j",
"REJECT",
"-w", // Wait for xtables lock.
};
return RunInMinijail(argv) == 0;
}
int Firewall::RunInMinijail(const std::vector<std::string>& argv) {
brillo::Minijail* m = brillo::Minijail::GetInstance();
minijail* jail = m->New();
std::vector<char*> args;
for (const auto& arg : argv) {
args.push_back(const_cast<char*>(arg.c_str()));
}
args.push_back(nullptr);
int status;
return m->RunSyncAndDestroy(jail, args, &status) ? status : -1;
}
} // namespace permission_broker