blob: 3d51cb60cb331abbf5820c304893399a36b9dcab [file] [log] [blame]
Jeff Sharkeyd8c64022012-07-13 18:04:07 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Lorenzo Colitti1411d452017-07-17 22:12:15 +090017#include <set>
18
Benedict Wongb2daefb2017-12-06 22:05:46 -080019#include <cstdint>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070020#include <errno.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#define LOG_TAG "FirewallController"
26#define LOG_NDEBUG 0
27
Lorenzo Colitti1411d452017-07-17 22:12:15 +090028#include <android-base/strings.h>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090029#include <android-base/stringprintf.h>
Logan Chien3f461482018-04-23 14:31:32 +080030#include <log/log.h>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070031
Chenbo Feng89c12f12018-03-21 10:29:18 -070032#include "Controllers.h"
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070033#include "FirewallController.h"
Chenbo Feng89c12f12018-03-21 10:29:18 -070034#include "NetdConstants.h"
35#include "bpf/BpfUtils.h"
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070036
Lorenzo Colitti1411d452017-07-17 22:12:15 +090037using android::base::Join;
Lorenzo Colitti89faa342016-02-26 11:38:47 +090038using android::base::StringAppendF;
Lorenzo Colitti1411d452017-07-17 22:12:15 +090039using android::base::StringPrintf;
Chenbo Feng89c12f12018-03-21 10:29:18 -070040using android::bpf::DOZABLE_UID_MAP_PATH;
41using android::bpf::POWERSAVE_UID_MAP_PATH;
42using android::bpf::STANDBY_UID_MAP_PATH;
43using android::net::gCtls;
Lorenzo Colitti89faa342016-02-26 11:38:47 +090044
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090045auto FirewallController::execIptablesRestore = ::execIptablesRestore;
46
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070047const char* FirewallController::TABLE = "filter";
48
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070049const char* FirewallController::LOCAL_INPUT = "fw_INPUT";
50const char* FirewallController::LOCAL_OUTPUT = "fw_OUTPUT";
51const char* FirewallController::LOCAL_FORWARD = "fw_FORWARD";
52
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070053const char* FirewallController::LOCAL_DOZABLE = "fw_dozable";
54const char* FirewallController::LOCAL_STANDBY = "fw_standby";
Felipe Leme3f624342016-02-10 18:12:39 -080055const char* FirewallController::LOCAL_POWERSAVE = "fw_powersave";
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070056
Lorenzo Colittic8683d72015-09-01 16:53:35 +090057// ICMPv6 types that are required for any form of IPv6 connectivity to work. Note that because the
58// fw_dozable chain is called from both INPUT and OUTPUT, this includes both packets that we need
59// to be able to send (e.g., RS, NS), and packets that we need to receive (e.g., RA, NA).
60const char* FirewallController::ICMPV6_TYPES[] = {
61 "packet-too-big",
62 "router-solicitation",
63 "router-advertisement",
64 "neighbour-solicitation",
65 "neighbour-advertisement",
66 "redirect",
67};
68
Chenbo Feng89c12f12018-03-21 10:29:18 -070069bool getBpfOwnerStatus() {
70 return gCtls->trafficCtrl.checkBpfStatsEnable();
71}
72
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070073FirewallController::FirewallController(void) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -070074 // If no rules are set, it's in BLACKLIST mode
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070075 mFirewallType = BLACKLIST;
Lorenzo Colitti1411d452017-07-17 22:12:15 +090076 mIfaceRules = {};
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070077}
78
79int FirewallController::setupIptablesHooks(void) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070080 int res = 0;
Chenbo Feng89c12f12018-03-21 10:29:18 -070081 mUseBpfOwnerMatch = getBpfOwnerStatus();
82 if (mUseBpfOwnerMatch) {
83 return res;
84 }
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090085 res |= createChain(LOCAL_DOZABLE, getFirewallType(DOZABLE));
86 res |= createChain(LOCAL_STANDBY, getFirewallType(STANDBY));
87 res |= createChain(LOCAL_POWERSAVE, getFirewallType(POWERSAVE));
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070088 return res;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070089}
90
Amith Yamasani390e4ea2015-04-25 19:08:57 -070091int FirewallController::enableFirewall(FirewallType ftype) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070092 int res = 0;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070093 if (mFirewallType != ftype) {
94 // flush any existing rules
95 disableFirewall();
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070096
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070097 if (ftype == WHITELIST) {
98 // create default rule to drop all traffic
Lorenzo Colittid351bea2017-07-16 22:52:30 +090099 std::string command =
100 "*filter\n"
101 "-A fw_INPUT -j DROP\n"
102 "-A fw_OUTPUT -j REJECT\n"
103 "-A fw_FORWARD -j REJECT\n"
104 "COMMIT\n";
105 res = execIptablesRestore(V4V6, command.c_str());
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700106 }
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700107
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700108 // Set this after calling disableFirewall(), since it defaults to WHITELIST there
109 mFirewallType = ftype;
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700110 }
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700111 return res;
112}
113
114int FirewallController::disableFirewall(void) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700115 mFirewallType = WHITELIST;
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900116 mIfaceRules.clear();
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700117
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700118 // flush any existing rules
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900119 std::string command =
120 "*filter\n"
121 ":fw_INPUT -\n"
122 ":fw_OUTPUT -\n"
123 ":fw_FORWARD -\n"
124 "COMMIT\n";
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700125
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900126 return execIptablesRestore(V4V6, command.c_str());
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700127}
128
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700129int FirewallController::enableChildChains(ChildChain chain, bool enable) {
130 int res = 0;
131 const char* name;
132 switch(chain) {
133 case DOZABLE:
134 name = LOCAL_DOZABLE;
135 break;
136 case STANDBY:
137 name = LOCAL_STANDBY;
138 break;
Felipe Leme3f624342016-02-10 18:12:39 -0800139 case POWERSAVE:
140 name = LOCAL_POWERSAVE;
141 break;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700142 default:
143 return res;
144 }
145
Chenbo Feng89c12f12018-03-21 10:29:18 -0700146 if (mUseBpfOwnerMatch) {
147 return gCtls->trafficCtrl.toggleUidOwnerMap(chain, enable);
148 }
149
Lorenzo Colittia1611962017-04-26 16:30:39 +0900150 std::string command = "*filter\n";
151 for (const char *parent : { LOCAL_INPUT, LOCAL_OUTPUT }) {
152 StringAppendF(&command, "%s %s -j %s\n", (enable ? "-A" : "-D"), parent, name);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700153 }
Lorenzo Colittia1611962017-04-26 16:30:39 +0900154 StringAppendF(&command, "COMMIT\n");
155
156 return execIptablesRestore(V4V6, command);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700157}
158
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700159int FirewallController::isFirewallEnabled(void) {
160 // TODO: verify that rules are still in place near top
161 return -1;
162}
163
164int FirewallController::setInterfaceRule(const char* iface, FirewallRule rule) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700165 if (mFirewallType == BLACKLIST) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700166 // Unsupported in BLACKLIST mode
167 return -1;
168 }
169
JP Abgrall69261cb2014-06-19 18:35:24 -0700170 if (!isIfaceName(iface)) {
171 errno = ENOENT;
172 return -1;
173 }
174
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900175 // Only delete rules if we actually added them, because otherwise our iptables-restore
176 // processes will terminate with "no such rule" errors and cause latency penalties while we
177 // spin up new ones.
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700178 const char* op;
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900179 if (rule == ALLOW && mIfaceRules.find(iface) == mIfaceRules.end()) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700180 op = "-I";
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900181 mIfaceRules.insert(iface);
182 } else if (rule == DENY && mIfaceRules.find(iface) != mIfaceRules.end()) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700183 op = "-D";
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900184 mIfaceRules.erase(iface);
185 } else {
186 return 0;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700187 }
188
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900189 std::string command = Join(std::vector<std::string> {
190 "*filter",
191 StringPrintf("%s fw_INPUT -i %s -j RETURN", op, iface),
192 StringPrintf("%s fw_OUTPUT -o %s -j RETURN", op, iface),
193 "COMMIT\n"
194 }, "\n");
195 return execIptablesRestore(V4V6, command);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700196}
197
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700198FirewallType FirewallController::getFirewallType(ChildChain chain) {
199 switch(chain) {
200 case DOZABLE:
201 return WHITELIST;
202 case STANDBY:
203 return BLACKLIST;
Felipe Leme3f624342016-02-10 18:12:39 -0800204 case POWERSAVE:
205 return WHITELIST;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700206 case NONE:
207 return mFirewallType;
208 default:
209 return BLACKLIST;
210 }
211}
212
213int FirewallController::setUidRule(ChildChain chain, int uid, FirewallRule rule) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700214 const char* op;
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700215 const char* target;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700216 FirewallType firewallType = getFirewallType(chain);
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700217 if (firewallType == WHITELIST) {
218 target = "RETURN";
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900219 // When adding, insert RETURN rules at the front, before the catch-all DROP at the end.
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700220 op = (rule == ALLOW)? "-I" : "-D";
221 } else { // BLACKLIST mode
222 target = "DROP";
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900223 // When adding, append DROP rules at the end, after the RETURN rule that matches TCP RSTs.
224 op = (rule == DENY)? "-A" : "-D";
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700225 }
226
Lorenzo Colittia7357652017-04-25 00:16:36 +0900227 std::vector<std::string> chainNames;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700228 switch(chain) {
229 case DOZABLE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900230 chainNames = { LOCAL_DOZABLE };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700231 break;
232 case STANDBY:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900233 chainNames = { LOCAL_STANDBY };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700234 break;
Felipe Leme3f624342016-02-10 18:12:39 -0800235 case POWERSAVE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900236 chainNames = { LOCAL_POWERSAVE };
Felipe Leme3f624342016-02-10 18:12:39 -0800237 break;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700238 case NONE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900239 chainNames = { LOCAL_INPUT, LOCAL_OUTPUT };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700240 break;
241 default:
242 ALOGW("Unknown child chain: %d", chain);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900243 return -1;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700244 }
Chenbo Feng89c12f12018-03-21 10:29:18 -0700245 if (mUseBpfOwnerMatch) {
246 return gCtls->trafficCtrl.changeUidOwnerRule(chain, uid, rule, firewallType);
247 }
Lorenzo Colittia7357652017-04-25 00:16:36 +0900248
249 std::string command = "*filter\n";
250 for (std::string chainName : chainNames) {
251 StringAppendF(&command, "%s %s -m owner --uid-owner %d -j %s\n",
252 op, chainName.c_str(), uid, target);
253 }
254 StringAppendF(&command, "COMMIT\n");
255
256 return execIptablesRestore(V4V6, command);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700257}
258
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900259int FirewallController::createChain(const char* chain, FirewallType type) {
260 static const std::vector<int32_t> NO_UIDS;
261 return replaceUidChain(chain, type == WHITELIST, NO_UIDS);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700262}
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900263
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900264/* static */
265std::string FirewallController::makeCriticalCommands(IptablesTarget target, const char* chainName) {
266 // Allow ICMPv6 packets necessary to make IPv6 connectivity work. http://b/23158230 .
267 std::string commands;
268 if (target == V6) {
269 for (size_t i = 0; i < ARRAY_SIZE(ICMPV6_TYPES); i++) {
270 StringAppendF(&commands, "-A %s -p icmpv6 --icmpv6-type %s -j RETURN\n",
271 chainName, ICMPV6_TYPES[i]);
272 }
273 }
274 return commands;
275}
276
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900277std::string FirewallController::makeUidRules(IptablesTarget target, const char *name,
278 bool isWhitelist, const std::vector<int32_t>& uids) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900279 std::string commands;
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900280 StringAppendF(&commands, "*filter\n:%s -\n", name);
281
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +0900282 // Whitelist chains have UIDs at the beginning, and new UIDs are added with '-I'.
283 if (isWhitelist) {
284 for (auto uid : uids) {
285 StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j RETURN\n", name, uid);
286 }
287
288 // Always whitelist system UIDs.
289 StringAppendF(&commands,
290 "-A %s -m owner --uid-owner %d-%d -j RETURN\n", name, 0, MAX_SYSTEM_UID);
Benedict Wongb2daefb2017-12-06 22:05:46 -0800291
292 // This rule inverts the match for all UIDs; ie, if there is no UID match here,
293 // there is no socket to be found
294 StringAppendF(&commands,
295 "-A %s -m owner ! --uid-owner %d-%u -j RETURN\n", name, 0, UINT32_MAX-1);
296
297 // Always whitelist traffic with protocol ESP, or no known socket - required for IPSec
298 StringAppendF(&commands, "-A %s -p esp -j RETURN\n", name);
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +0900299 }
300
Lorenzo Colitti238e8182016-07-26 17:59:41 +0900301 // Always allow networking on loopback.
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900302 StringAppendF(&commands, "-A %s -i lo -j RETURN\n", name);
303 StringAppendF(&commands, "-A %s -o lo -j RETURN\n", name);
Lorenzo Colitti238e8182016-07-26 17:59:41 +0900304
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900305 // Allow TCP RSTs so we can cleanly close TCP connections of apps that no longer have network
306 // access. Both incoming and outgoing RSTs are allowed.
307 StringAppendF(&commands, "-A %s -p tcp --tcp-flags RST RST -j RETURN\n", name);
308
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900309 if (isWhitelist) {
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900310 commands.append(makeCriticalCommands(target, name));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900311 }
312
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +0900313 // Blacklist chains have UIDs at the end, and new UIDs are added with '-A'.
314 if (!isWhitelist) {
315 for (auto uid : uids) {
316 StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j DROP\n", name, uid);
317 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900318 }
319
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900320 // If it's a whitelist chain, add a default DROP at the end. This is not necessary for a
321 // blacklist chain, because all user-defined chains implicitly RETURN at the end.
322 if (isWhitelist) {
323 StringAppendF(&commands, "-A %s -j DROP\n", name);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900324 }
325
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900326 StringAppendF(&commands, "COMMIT\n");
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900327
328 return commands;
329}
330
331int FirewallController::replaceUidChain(
Erik Klinef52d4522018-03-14 15:01:46 +0900332 const std::string &name, bool isWhitelist, const std::vector<int32_t>& uids) {
Chenbo Feng89c12f12018-03-21 10:29:18 -0700333 if (mUseBpfOwnerMatch) {
334 return gCtls->trafficCtrl.replaceUidOwnerMap(name, isWhitelist, uids);
335 }
Erik Klinef52d4522018-03-14 15:01:46 +0900336 std::string commands4 = makeUidRules(V4, name.c_str(), isWhitelist, uids);
337 std::string commands6 = makeUidRules(V6, name.c_str(), isWhitelist, uids);
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900338 return execIptablesRestore(V4, commands4.c_str()) | execIptablesRestore(V6, commands6.c_str());
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900339}