blob: f5da0691b41d8265bdba6e447c5e11c209cce8b4 [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
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070019#include <errno.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#define LOG_TAG "FirewallController"
25#define LOG_NDEBUG 0
26
Lorenzo Colitti1411d452017-07-17 22:12:15 +090027#include <android-base/strings.h>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090028#include <android-base/stringprintf.h>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070029#include <cutils/log.h>
30
31#include "NetdConstants.h"
32#include "FirewallController.h"
33
Lorenzo Colitti1411d452017-07-17 22:12:15 +090034using android::base::Join;
Lorenzo Colitti89faa342016-02-26 11:38:47 +090035using android::base::StringAppendF;
Lorenzo Colitti1411d452017-07-17 22:12:15 +090036using android::base::StringPrintf;
Lorenzo Colitti89faa342016-02-26 11:38:47 +090037
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090038auto FirewallController::execIptablesRestore = ::execIptablesRestore;
39
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070040const char* FirewallController::TABLE = "filter";
41
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070042const char* FirewallController::LOCAL_INPUT = "fw_INPUT";
43const char* FirewallController::LOCAL_OUTPUT = "fw_OUTPUT";
44const char* FirewallController::LOCAL_FORWARD = "fw_FORWARD";
45
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070046const char* FirewallController::LOCAL_DOZABLE = "fw_dozable";
47const char* FirewallController::LOCAL_STANDBY = "fw_standby";
Felipe Leme3f624342016-02-10 18:12:39 -080048const char* FirewallController::LOCAL_POWERSAVE = "fw_powersave";
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070049
Lorenzo Colittic8683d72015-09-01 16:53:35 +090050// ICMPv6 types that are required for any form of IPv6 connectivity to work. Note that because the
51// fw_dozable chain is called from both INPUT and OUTPUT, this includes both packets that we need
52// to be able to send (e.g., RS, NS), and packets that we need to receive (e.g., RA, NA).
53const char* FirewallController::ICMPV6_TYPES[] = {
54 "packet-too-big",
55 "router-solicitation",
56 "router-advertisement",
57 "neighbour-solicitation",
58 "neighbour-advertisement",
59 "redirect",
60};
61
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070062FirewallController::FirewallController(void) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -070063 // If no rules are set, it's in BLACKLIST mode
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070064 mFirewallType = BLACKLIST;
Lorenzo Colitti1411d452017-07-17 22:12:15 +090065 mIfaceRules = {};
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070066}
67
68int FirewallController::setupIptablesHooks(void) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070069 int res = 0;
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090070 res |= createChain(LOCAL_DOZABLE, getFirewallType(DOZABLE));
71 res |= createChain(LOCAL_STANDBY, getFirewallType(STANDBY));
72 res |= createChain(LOCAL_POWERSAVE, getFirewallType(POWERSAVE));
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070073 return res;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070074}
75
Amith Yamasani390e4ea2015-04-25 19:08:57 -070076int FirewallController::enableFirewall(FirewallType ftype) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070077 int res = 0;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070078 if (mFirewallType != ftype) {
79 // flush any existing rules
80 disableFirewall();
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070081
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070082 if (ftype == WHITELIST) {
83 // create default rule to drop all traffic
Lorenzo Colittid351bea2017-07-16 22:52:30 +090084 std::string command =
85 "*filter\n"
86 "-A fw_INPUT -j DROP\n"
87 "-A fw_OUTPUT -j REJECT\n"
88 "-A fw_FORWARD -j REJECT\n"
89 "COMMIT\n";
90 res = execIptablesRestore(V4V6, command.c_str());
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070091 }
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070092
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070093 // Set this after calling disableFirewall(), since it defaults to WHITELIST there
94 mFirewallType = ftype;
Amith Yamasani390e4ea2015-04-25 19:08:57 -070095 }
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070096 return res;
97}
98
99int FirewallController::disableFirewall(void) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700100 mFirewallType = WHITELIST;
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900101 mIfaceRules.clear();
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700102
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700103 // flush any existing rules
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900104 std::string command =
105 "*filter\n"
106 ":fw_INPUT -\n"
107 ":fw_OUTPUT -\n"
108 ":fw_FORWARD -\n"
109 "COMMIT\n";
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700110
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900111 return execIptablesRestore(V4V6, command.c_str());
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700112}
113
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700114int FirewallController::enableChildChains(ChildChain chain, bool enable) {
115 int res = 0;
116 const char* name;
117 switch(chain) {
118 case DOZABLE:
119 name = LOCAL_DOZABLE;
120 break;
121 case STANDBY:
122 name = LOCAL_STANDBY;
123 break;
Felipe Leme3f624342016-02-10 18:12:39 -0800124 case POWERSAVE:
125 name = LOCAL_POWERSAVE;
126 break;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700127 default:
128 return res;
129 }
130
Lorenzo Colittia1611962017-04-26 16:30:39 +0900131 std::string command = "*filter\n";
132 for (const char *parent : { LOCAL_INPUT, LOCAL_OUTPUT }) {
133 StringAppendF(&command, "%s %s -j %s\n", (enable ? "-A" : "-D"), parent, name);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700134 }
Lorenzo Colittia1611962017-04-26 16:30:39 +0900135 StringAppendF(&command, "COMMIT\n");
136
137 return execIptablesRestore(V4V6, command);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700138}
139
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700140int FirewallController::isFirewallEnabled(void) {
141 // TODO: verify that rules are still in place near top
142 return -1;
143}
144
145int FirewallController::setInterfaceRule(const char* iface, FirewallRule rule) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700146 if (mFirewallType == BLACKLIST) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700147 // Unsupported in BLACKLIST mode
148 return -1;
149 }
150
JP Abgrall69261cb2014-06-19 18:35:24 -0700151 if (!isIfaceName(iface)) {
152 errno = ENOENT;
153 return -1;
154 }
155
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900156 // Only delete rules if we actually added them, because otherwise our iptables-restore
157 // processes will terminate with "no such rule" errors and cause latency penalties while we
158 // spin up new ones.
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700159 const char* op;
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900160 if (rule == ALLOW && mIfaceRules.find(iface) == mIfaceRules.end()) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700161 op = "-I";
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900162 mIfaceRules.insert(iface);
163 } else if (rule == DENY && mIfaceRules.find(iface) != mIfaceRules.end()) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700164 op = "-D";
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900165 mIfaceRules.erase(iface);
166 } else {
167 return 0;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700168 }
169
Lorenzo Colitti1411d452017-07-17 22:12:15 +0900170 std::string command = Join(std::vector<std::string> {
171 "*filter",
172 StringPrintf("%s fw_INPUT -i %s -j RETURN", op, iface),
173 StringPrintf("%s fw_OUTPUT -o %s -j RETURN", op, iface),
174 "COMMIT\n"
175 }, "\n");
176 return execIptablesRestore(V4V6, command);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700177}
178
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700179FirewallType FirewallController::getFirewallType(ChildChain chain) {
180 switch(chain) {
181 case DOZABLE:
182 return WHITELIST;
183 case STANDBY:
184 return BLACKLIST;
Felipe Leme3f624342016-02-10 18:12:39 -0800185 case POWERSAVE:
186 return WHITELIST;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700187 case NONE:
188 return mFirewallType;
189 default:
190 return BLACKLIST;
191 }
192}
193
194int FirewallController::setUidRule(ChildChain chain, int uid, FirewallRule rule) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700195 const char* op;
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700196 const char* target;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700197 FirewallType firewallType = getFirewallType(chain);
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700198 if (firewallType == WHITELIST) {
199 target = "RETURN";
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900200 // When adding, insert RETURN rules at the front, before the catch-all DROP at the end.
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700201 op = (rule == ALLOW)? "-I" : "-D";
202 } else { // BLACKLIST mode
203 target = "DROP";
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900204 // When adding, append DROP rules at the end, after the RETURN rule that matches TCP RSTs.
205 op = (rule == DENY)? "-A" : "-D";
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700206 }
207
Lorenzo Colittia7357652017-04-25 00:16:36 +0900208 std::vector<std::string> chainNames;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700209 switch(chain) {
210 case DOZABLE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900211 chainNames = { LOCAL_DOZABLE };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700212 break;
213 case STANDBY:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900214 chainNames = { LOCAL_STANDBY };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700215 break;
Felipe Leme3f624342016-02-10 18:12:39 -0800216 case POWERSAVE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900217 chainNames = { LOCAL_POWERSAVE };
Felipe Leme3f624342016-02-10 18:12:39 -0800218 break;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700219 case NONE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900220 chainNames = { LOCAL_INPUT, LOCAL_OUTPUT };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700221 break;
222 default:
223 ALOGW("Unknown child chain: %d", chain);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900224 return -1;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700225 }
Lorenzo Colittia7357652017-04-25 00:16:36 +0900226
227 std::string command = "*filter\n";
228 for (std::string chainName : chainNames) {
229 StringAppendF(&command, "%s %s -m owner --uid-owner %d -j %s\n",
230 op, chainName.c_str(), uid, target);
231 }
232 StringAppendF(&command, "COMMIT\n");
233
234 return execIptablesRestore(V4V6, command);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700235}
236
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900237int FirewallController::createChain(const char* chain, FirewallType type) {
238 static const std::vector<int32_t> NO_UIDS;
239 return replaceUidChain(chain, type == WHITELIST, NO_UIDS);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700240}
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900241
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900242/* static */
243std::string FirewallController::makeCriticalCommands(IptablesTarget target, const char* chainName) {
244 // Allow ICMPv6 packets necessary to make IPv6 connectivity work. http://b/23158230 .
245 std::string commands;
246 if (target == V6) {
247 for (size_t i = 0; i < ARRAY_SIZE(ICMPV6_TYPES); i++) {
248 StringAppendF(&commands, "-A %s -p icmpv6 --icmpv6-type %s -j RETURN\n",
249 chainName, ICMPV6_TYPES[i]);
250 }
251 }
252 return commands;
253}
254
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900255std::string FirewallController::makeUidRules(IptablesTarget target, const char *name,
256 bool isWhitelist, const std::vector<int32_t>& uids) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900257 std::string commands;
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900258 StringAppendF(&commands, "*filter\n:%s -\n", name);
259
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +0900260 // Whitelist chains have UIDs at the beginning, and new UIDs are added with '-I'.
261 if (isWhitelist) {
262 for (auto uid : uids) {
263 StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j RETURN\n", name, uid);
264 }
265
266 // Always whitelist system UIDs.
267 StringAppendF(&commands,
268 "-A %s -m owner --uid-owner %d-%d -j RETURN\n", name, 0, MAX_SYSTEM_UID);
269 }
270
Lorenzo Colitti238e8182016-07-26 17:59:41 +0900271 // Always allow networking on loopback.
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900272 StringAppendF(&commands, "-A %s -i lo -j RETURN\n", name);
273 StringAppendF(&commands, "-A %s -o lo -j RETURN\n", name);
Lorenzo Colitti238e8182016-07-26 17:59:41 +0900274
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900275 // Allow TCP RSTs so we can cleanly close TCP connections of apps that no longer have network
276 // access. Both incoming and outgoing RSTs are allowed.
277 StringAppendF(&commands, "-A %s -p tcp --tcp-flags RST RST -j RETURN\n", name);
278
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900279 if (isWhitelist) {
Lorenzo Colittiaff28792017-09-26 17:46:18 +0900280 commands.append(makeCriticalCommands(target, name));
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900281 }
282
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +0900283 // Blacklist chains have UIDs at the end, and new UIDs are added with '-A'.
284 if (!isWhitelist) {
285 for (auto uid : uids) {
286 StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j DROP\n", name, uid);
287 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900288 }
289
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900290 // If it's a whitelist chain, add a default DROP at the end. This is not necessary for a
291 // blacklist chain, because all user-defined chains implicitly RETURN at the end.
292 if (isWhitelist) {
293 StringAppendF(&commands, "-A %s -j DROP\n", name);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900294 }
295
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900296 StringAppendF(&commands, "COMMIT\n");
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900297
298 return commands;
299}
300
301int FirewallController::replaceUidChain(
302 const char *name, bool isWhitelist, const std::vector<int32_t>& uids) {
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900303 std::string commands4 = makeUidRules(V4, name, isWhitelist, uids);
304 std::string commands6 = makeUidRules(V6, name, isWhitelist, uids);
305 return execIptablesRestore(V4, commands4.c_str()) | execIptablesRestore(V6, commands6.c_str());
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900306}