blob: 26c2126da6959b96bf995e1ea81a0b25d6aab83f [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>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070030#include <cutils/log.h>
31
32#include "NetdConstants.h"
33#include "FirewallController.h"
34
Lorenzo Colitti1411d452017-07-17 22:12:15 +090035using android::base::Join;
Lorenzo Colitti89faa342016-02-26 11:38:47 +090036using android::base::StringAppendF;
Lorenzo Colitti1411d452017-07-17 22:12:15 +090037using android::base::StringPrintf;
Lorenzo Colitti89faa342016-02-26 11:38:47 +090038
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090039auto FirewallController::execIptablesRestore = ::execIptablesRestore;
40
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070041const char* FirewallController::TABLE = "filter";
42
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070043const char* FirewallController::LOCAL_INPUT = "fw_INPUT";
44const char* FirewallController::LOCAL_OUTPUT = "fw_OUTPUT";
45const char* FirewallController::LOCAL_FORWARD = "fw_FORWARD";
46
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070047const char* FirewallController::LOCAL_DOZABLE = "fw_dozable";
48const char* FirewallController::LOCAL_STANDBY = "fw_standby";
Felipe Leme3f624342016-02-10 18:12:39 -080049const char* FirewallController::LOCAL_POWERSAVE = "fw_powersave";
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070050
Lorenzo Colittic8683d72015-09-01 16:53:35 +090051// ICMPv6 types that are required for any form of IPv6 connectivity to work. Note that because the
52// fw_dozable chain is called from both INPUT and OUTPUT, this includes both packets that we need
53// to be able to send (e.g., RS, NS), and packets that we need to receive (e.g., RA, NA).
54const char* FirewallController::ICMPV6_TYPES[] = {
55 "packet-too-big",
56 "router-solicitation",
57 "router-advertisement",
58 "neighbour-solicitation",
59 "neighbour-advertisement",
60 "redirect",
61};
62
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070063FirewallController::FirewallController(void) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -070064 // If no rules are set, it's in BLACKLIST mode
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070065 mFirewallType = BLACKLIST;
Lorenzo Colitti1411d452017-07-17 22:12:15 +090066 mIfaceRules = {};
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070067}
68
69int FirewallController::setupIptablesHooks(void) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070070 int res = 0;
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090071 res |= createChain(LOCAL_DOZABLE, getFirewallType(DOZABLE));
72 res |= createChain(LOCAL_STANDBY, getFirewallType(STANDBY));
73 res |= createChain(LOCAL_POWERSAVE, getFirewallType(POWERSAVE));
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070074 return res;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070075}
76
Amith Yamasani390e4ea2015-04-25 19:08:57 -070077int FirewallController::enableFirewall(FirewallType ftype) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070078 int res = 0;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070079 if (mFirewallType != ftype) {
80 // flush any existing rules
81 disableFirewall();
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070082
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070083 if (ftype == WHITELIST) {
84 // create default rule to drop all traffic
Lorenzo Colittid351bea2017-07-16 22:52:30 +090085 std::string command =
86 "*filter\n"
87 "-A fw_INPUT -j DROP\n"
88 "-A fw_OUTPUT -j REJECT\n"
89 "-A fw_FORWARD -j REJECT\n"
90 "COMMIT\n";
91 res = execIptablesRestore(V4V6, command.c_str());
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070092 }
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070093
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070094 // Set this after calling disableFirewall(), since it defaults to WHITELIST there
95 mFirewallType = ftype;
Amith Yamasani390e4ea2015-04-25 19:08:57 -070096 }
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070097 return res;
98}
99
100int FirewallController::disableFirewall(void) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700101 mFirewallType = WHITELIST;
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900102 mIfaceRules.clear();
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700103
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700104 // flush any existing rules
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900105 std::string command =
106 "*filter\n"
107 ":fw_INPUT -\n"
108 ":fw_OUTPUT -\n"
109 ":fw_FORWARD -\n"
110 "COMMIT\n";
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700111
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900112 return execIptablesRestore(V4V6, command.c_str());
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700113}
114
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700115int FirewallController::enableChildChains(ChildChain chain, bool enable) {
116 int res = 0;
117 const char* name;
118 switch(chain) {
119 case DOZABLE:
120 name = LOCAL_DOZABLE;
121 break;
122 case STANDBY:
123 name = LOCAL_STANDBY;
124 break;
Felipe Leme3f624342016-02-10 18:12:39 -0800125 case POWERSAVE:
126 name = LOCAL_POWERSAVE;
127 break;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700128 default:
129 return res;
130 }
131
Lorenzo Colittia1611962017-04-26 16:30:39 +0900132 std::string command = "*filter\n";
133 for (const char *parent : { LOCAL_INPUT, LOCAL_OUTPUT }) {
134 StringAppendF(&command, "%s %s -j %s\n", (enable ? "-A" : "-D"), parent, name);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700135 }
Lorenzo Colittia1611962017-04-26 16:30:39 +0900136 StringAppendF(&command, "COMMIT\n");
137
138 return execIptablesRestore(V4V6, command);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700139}
140
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700141int FirewallController::isFirewallEnabled(void) {
142 // TODO: verify that rules are still in place near top
143 return -1;
144}
145
146int FirewallController::setInterfaceRule(const char* iface, FirewallRule rule) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700147 if (mFirewallType == BLACKLIST) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700148 // Unsupported in BLACKLIST mode
149 return -1;
150 }
151
JP Abgrall69261cb2014-06-19 18:35:24 -0700152 if (!isIfaceName(iface)) {
153 errno = ENOENT;
154 return -1;
155 }
156
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900157 // Only delete rules if we actually added them, because otherwise our iptables-restore
158 // processes will terminate with "no such rule" errors and cause latency penalties while we
159 // spin up new ones.
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700160 const char* op;
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900161 if (rule == ALLOW && mIfaceRules.find(iface) == mIfaceRules.end()) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700162 op = "-I";
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900163 mIfaceRules.insert(iface);
164 } else if (rule == DENY && mIfaceRules.find(iface) != mIfaceRules.end()) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700165 op = "-D";
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900166 mIfaceRules.erase(iface);
167 } else {
168 return 0;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700169 }
170
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900171 std::string command = Join(std::vector<std::string> {
172 "*filter",
173 StringPrintf("%s fw_INPUT -i %s -j RETURN", op, iface),
174 StringPrintf("%s fw_OUTPUT -o %s -j RETURN", op, iface),
175 "COMMIT\n"
176 }, "\n");
177 return execIptablesRestore(V4V6, command);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700178}
179
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700180FirewallType FirewallController::getFirewallType(ChildChain chain) {
181 switch(chain) {
182 case DOZABLE:
183 return WHITELIST;
184 case STANDBY:
185 return BLACKLIST;
Felipe Leme3f624342016-02-10 18:12:39 -0800186 case POWERSAVE:
187 return WHITELIST;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700188 case NONE:
189 return mFirewallType;
190 default:
191 return BLACKLIST;
192 }
193}
194
195int FirewallController::setUidRule(ChildChain chain, int uid, FirewallRule rule) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700196 const char* op;
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700197 const char* target;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700198 FirewallType firewallType = getFirewallType(chain);
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700199 if (firewallType == WHITELIST) {
200 target = "RETURN";
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900201 // When adding, insert RETURN rules at the front, before the catch-all DROP at the end.
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700202 op = (rule == ALLOW)? "-I" : "-D";
203 } else { // BLACKLIST mode
204 target = "DROP";
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900205 // When adding, append DROP rules at the end, after the RETURN rule that matches TCP RSTs.
206 op = (rule == DENY)? "-A" : "-D";
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700207 }
208
Lorenzo Colittia7357652017-04-25 00:16:36 +0900209 std::vector<std::string> chainNames;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700210 switch(chain) {
211 case DOZABLE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900212 chainNames = { LOCAL_DOZABLE };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700213 break;
214 case STANDBY:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900215 chainNames = { LOCAL_STANDBY };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700216 break;
Felipe Leme3f624342016-02-10 18:12:39 -0800217 case POWERSAVE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900218 chainNames = { LOCAL_POWERSAVE };
Felipe Leme3f624342016-02-10 18:12:39 -0800219 break;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700220 case NONE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900221 chainNames = { LOCAL_INPUT, LOCAL_OUTPUT };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700222 break;
223 default:
224 ALOGW("Unknown child chain: %d", chain);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900225 return -1;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700226 }
Lorenzo Colittia7357652017-04-25 00:16:36 +0900227
228 std::string command = "*filter\n";
229 for (std::string chainName : chainNames) {
230 StringAppendF(&command, "%s %s -m owner --uid-owner %d -j %s\n",
231 op, chainName.c_str(), uid, target);
232 }
233 StringAppendF(&command, "COMMIT\n");
234
235 return execIptablesRestore(V4V6, command);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700236}
237
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900238int FirewallController::createChain(const char* chain, FirewallType type) {
239 static const std::vector<int32_t> NO_UIDS;
240 return replaceUidChain(chain, type == WHITELIST, NO_UIDS);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700241}
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900242
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900243/* static */
244std::string FirewallController::makeCriticalCommands(IptablesTarget target, const char* chainName) {
245 // Allow ICMPv6 packets necessary to make IPv6 connectivity work. http://b/23158230 .
246 std::string commands;
247 if (target == V6) {
248 for (size_t i = 0; i < ARRAY_SIZE(ICMPV6_TYPES); i++) {
249 StringAppendF(&commands, "-A %s -p icmpv6 --icmpv6-type %s -j RETURN\n",
250 chainName, ICMPV6_TYPES[i]);
251 }
252 }
253 return commands;
254}
255
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900256std::string FirewallController::makeUidRules(IptablesTarget target, const char *name,
257 bool isWhitelist, const std::vector<int32_t>& uids) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900258 std::string commands;
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900259 StringAppendF(&commands, "*filter\n:%s -\n", name);
260
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +0900261 // Whitelist chains have UIDs at the beginning, and new UIDs are added with '-I'.
262 if (isWhitelist) {
263 for (auto uid : uids) {
264 StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j RETURN\n", name, uid);
265 }
266
267 // Always whitelist system UIDs.
268 StringAppendF(&commands,
269 "-A %s -m owner --uid-owner %d-%d -j RETURN\n", name, 0, MAX_SYSTEM_UID);
Benedict Wongb2daefb2017-12-06 22:05:46 -0800270
271 // This rule inverts the match for all UIDs; ie, if there is no UID match here,
272 // there is no socket to be found
273 StringAppendF(&commands,
274 "-A %s -m owner ! --uid-owner %d-%u -j RETURN\n", name, 0, UINT32_MAX-1);
275
276 // Always whitelist traffic with protocol ESP, or no known socket - required for IPSec
277 StringAppendF(&commands, "-A %s -p esp -j RETURN\n", name);
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +0900278 }
279
Lorenzo Colitti238e8182016-07-26 17:59:41 +0900280 // Always allow networking on loopback.
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900281 StringAppendF(&commands, "-A %s -i lo -j RETURN\n", name);
282 StringAppendF(&commands, "-A %s -o lo -j RETURN\n", name);
Lorenzo Colitti238e8182016-07-26 17:59:41 +0900283
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900284 // Allow TCP RSTs so we can cleanly close TCP connections of apps that no longer have network
285 // access. Both incoming and outgoing RSTs are allowed.
286 StringAppendF(&commands, "-A %s -p tcp --tcp-flags RST RST -j RETURN\n", name);
287
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900288 if (isWhitelist) {
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900289 commands.append(makeCriticalCommands(target, name));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900290 }
291
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +0900292 // Blacklist chains have UIDs at the end, and new UIDs are added with '-A'.
293 if (!isWhitelist) {
294 for (auto uid : uids) {
295 StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j DROP\n", name, uid);
296 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900297 }
298
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900299 // If it's a whitelist chain, add a default DROP at the end. This is not necessary for a
300 // blacklist chain, because all user-defined chains implicitly RETURN at the end.
301 if (isWhitelist) {
302 StringAppendF(&commands, "-A %s -j DROP\n", name);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900303 }
304
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900305 StringAppendF(&commands, "COMMIT\n");
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900306
307 return commands;
308}
309
310int FirewallController::replaceUidChain(
Erik Klinef52d4522018-03-14 15:01:46 +0900311 const std::string &name, bool isWhitelist, const std::vector<int32_t>& uids) {
312 std::string commands4 = makeUidRules(V4, name.c_str(), isWhitelist, uids);
313 std::string commands6 = makeUidRules(V6, name.c_str(), isWhitelist, uids);
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900314 return execIptablesRestore(V4, commands4.c_str()) | execIptablesRestore(V6, commands6.c_str());
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900315}