blob: 4b6eca6706685d55101dbfcb91ff39b1187dd94b [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
17#include <errno.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#define LOG_TAG "FirewallController"
23#define LOG_NDEBUG 0
24
Lorenzo Colitti89faa342016-02-26 11:38:47 +090025#include <android-base/stringprintf.h>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070026#include <cutils/log.h>
27
28#include "NetdConstants.h"
29#include "FirewallController.h"
30
Lorenzo Colitti89faa342016-02-26 11:38:47 +090031using android::base::StringAppendF;
32
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090033auto FirewallController::execIptables = ::execIptables;
Pierre Imai62ffdb72016-05-27 15:51:55 +090034auto FirewallController::execIptablesSilently = ::execIptablesSilently;
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090035auto FirewallController::execIptablesRestore = ::execIptablesRestore;
36
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070037const char* FirewallController::TABLE = "filter";
38
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070039const char* FirewallController::LOCAL_INPUT = "fw_INPUT";
40const char* FirewallController::LOCAL_OUTPUT = "fw_OUTPUT";
41const char* FirewallController::LOCAL_FORWARD = "fw_FORWARD";
42
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070043const char* FirewallController::LOCAL_DOZABLE = "fw_dozable";
44const char* FirewallController::LOCAL_STANDBY = "fw_standby";
Felipe Leme3f624342016-02-10 18:12:39 -080045const char* FirewallController::LOCAL_POWERSAVE = "fw_powersave";
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070046
Lorenzo Colittic8683d72015-09-01 16:53:35 +090047// ICMPv6 types that are required for any form of IPv6 connectivity to work. Note that because the
48// fw_dozable chain is called from both INPUT and OUTPUT, this includes both packets that we need
49// to be able to send (e.g., RS, NS), and packets that we need to receive (e.g., RA, NA).
50const char* FirewallController::ICMPV6_TYPES[] = {
51 "packet-too-big",
52 "router-solicitation",
53 "router-advertisement",
54 "neighbour-solicitation",
55 "neighbour-advertisement",
56 "redirect",
57};
58
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070059FirewallController::FirewallController(void) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -070060 // If no rules are set, it's in BLACKLIST mode
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070061 mFirewallType = BLACKLIST;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070062}
63
64int FirewallController::setupIptablesHooks(void) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070065 int res = 0;
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +090066 res |= createChain(LOCAL_DOZABLE, getFirewallType(DOZABLE));
67 res |= createChain(LOCAL_STANDBY, getFirewallType(STANDBY));
68 res |= createChain(LOCAL_POWERSAVE, getFirewallType(POWERSAVE));
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070069 return res;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070070}
71
Amith Yamasani390e4ea2015-04-25 19:08:57 -070072int FirewallController::enableFirewall(FirewallType ftype) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070073 int res = 0;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070074 if (mFirewallType != ftype) {
75 // flush any existing rules
76 disableFirewall();
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070077
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070078 if (ftype == WHITELIST) {
79 // create default rule to drop all traffic
Lorenzo Colittid351bea2017-07-16 22:52:30 +090080 std::string command =
81 "*filter\n"
82 "-A fw_INPUT -j DROP\n"
83 "-A fw_OUTPUT -j REJECT\n"
84 "-A fw_FORWARD -j REJECT\n"
85 "COMMIT\n";
86 res = execIptablesRestore(V4V6, command.c_str());
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070087 }
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070088
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070089 // Set this after calling disableFirewall(), since it defaults to WHITELIST there
90 mFirewallType = ftype;
Amith Yamasani390e4ea2015-04-25 19:08:57 -070091 }
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070092 return res;
93}
94
95int FirewallController::disableFirewall(void) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070096 mFirewallType = WHITELIST;
Amith Yamasani390e4ea2015-04-25 19:08:57 -070097
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070098 // flush any existing rules
Lorenzo Colittid351bea2017-07-16 22:52:30 +090099 std::string command =
100 "*filter\n"
101 ":fw_INPUT -\n"
102 ":fw_OUTPUT -\n"
103 ":fw_FORWARD -\n"
104 "COMMIT\n";
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700105
Lorenzo Colittid351bea2017-07-16 22:52:30 +0900106 return execIptablesRestore(V4V6, command.c_str());
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700107}
108
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700109int FirewallController::enableChildChains(ChildChain chain, bool enable) {
110 int res = 0;
111 const char* name;
112 switch(chain) {
113 case DOZABLE:
114 name = LOCAL_DOZABLE;
115 break;
116 case STANDBY:
117 name = LOCAL_STANDBY;
118 break;
Felipe Leme3f624342016-02-10 18:12:39 -0800119 case POWERSAVE:
120 name = LOCAL_POWERSAVE;
121 break;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700122 default:
123 return res;
124 }
125
Lorenzo Colittia1611962017-04-26 16:30:39 +0900126 std::string command = "*filter\n";
127 for (const char *parent : { LOCAL_INPUT, LOCAL_OUTPUT }) {
128 StringAppendF(&command, "%s %s -j %s\n", (enable ? "-A" : "-D"), parent, name);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700129 }
Lorenzo Colittia1611962017-04-26 16:30:39 +0900130 StringAppendF(&command, "COMMIT\n");
131
132 return execIptablesRestore(V4V6, command);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700133}
134
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700135int FirewallController::isFirewallEnabled(void) {
136 // TODO: verify that rules are still in place near top
137 return -1;
138}
139
140int FirewallController::setInterfaceRule(const char* iface, FirewallRule rule) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700141 if (mFirewallType == BLACKLIST) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700142 // Unsupported in BLACKLIST mode
143 return -1;
144 }
145
JP Abgrall69261cb2014-06-19 18:35:24 -0700146 if (!isIfaceName(iface)) {
147 errno = ENOENT;
148 return -1;
149 }
150
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700151 const char* op;
152 if (rule == ALLOW) {
153 op = "-I";
154 } else {
155 op = "-D";
156 }
157
158 int res = 0;
159 res |= execIptables(V4V6, op, LOCAL_INPUT, "-i", iface, "-j", "RETURN", NULL);
160 res |= execIptables(V4V6, op, LOCAL_OUTPUT, "-o", iface, "-j", "RETURN", NULL);
161 return res;
162}
163
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700164FirewallType FirewallController::getFirewallType(ChildChain chain) {
165 switch(chain) {
166 case DOZABLE:
167 return WHITELIST;
168 case STANDBY:
169 return BLACKLIST;
Felipe Leme3f624342016-02-10 18:12:39 -0800170 case POWERSAVE:
171 return WHITELIST;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700172 case NONE:
173 return mFirewallType;
174 default:
175 return BLACKLIST;
176 }
177}
178
179int FirewallController::setUidRule(ChildChain chain, int uid, FirewallRule rule) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700180 const char* op;
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700181 const char* target;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700182 FirewallType firewallType = getFirewallType(chain);
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700183 if (firewallType == WHITELIST) {
184 target = "RETURN";
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900185 // When adding, insert RETURN rules at the front, before the catch-all DROP at the end.
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700186 op = (rule == ALLOW)? "-I" : "-D";
187 } else { // BLACKLIST mode
188 target = "DROP";
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900189 // When adding, append DROP rules at the end, after the RETURN rule that matches TCP RSTs.
190 op = (rule == DENY)? "-A" : "-D";
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700191 }
192
Lorenzo Colittia7357652017-04-25 00:16:36 +0900193 std::vector<std::string> chainNames;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700194 switch(chain) {
195 case DOZABLE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900196 chainNames = { LOCAL_DOZABLE };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700197 break;
198 case STANDBY:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900199 chainNames = { LOCAL_STANDBY };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700200 break;
Felipe Leme3f624342016-02-10 18:12:39 -0800201 case POWERSAVE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900202 chainNames = { LOCAL_POWERSAVE };
Felipe Leme3f624342016-02-10 18:12:39 -0800203 break;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700204 case NONE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900205 chainNames = { LOCAL_INPUT, LOCAL_OUTPUT };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700206 break;
207 default:
208 ALOGW("Unknown child chain: %d", chain);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900209 return -1;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700210 }
Lorenzo Colittia7357652017-04-25 00:16:36 +0900211
212 std::string command = "*filter\n";
213 for (std::string chainName : chainNames) {
214 StringAppendF(&command, "%s %s -m owner --uid-owner %d -j %s\n",
215 op, chainName.c_str(), uid, target);
216 }
217 StringAppendF(&command, "COMMIT\n");
218
219 return execIptablesRestore(V4V6, command);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700220}
221
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900222int FirewallController::createChain(const char* chain, FirewallType type) {
223 static const std::vector<int32_t> NO_UIDS;
224 return replaceUidChain(chain, type == WHITELIST, NO_UIDS);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700225}
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900226
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900227std::string FirewallController::makeUidRules(IptablesTarget target, const char *name,
228 bool isWhitelist, const std::vector<int32_t>& uids) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900229 std::string commands;
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900230 StringAppendF(&commands, "*filter\n:%s -\n", name);
231
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +0900232 // Whitelist chains have UIDs at the beginning, and new UIDs are added with '-I'.
233 if (isWhitelist) {
234 for (auto uid : uids) {
235 StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j RETURN\n", name, uid);
236 }
237
238 // Always whitelist system UIDs.
239 StringAppendF(&commands,
240 "-A %s -m owner --uid-owner %d-%d -j RETURN\n", name, 0, MAX_SYSTEM_UID);
241 }
242
Lorenzo Colitti238e8182016-07-26 17:59:41 +0900243 // Always allow networking on loopback.
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900244 StringAppendF(&commands, "-A %s -i lo -j RETURN\n", name);
245 StringAppendF(&commands, "-A %s -o lo -j RETURN\n", name);
Lorenzo Colitti238e8182016-07-26 17:59:41 +0900246
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900247 // Allow TCP RSTs so we can cleanly close TCP connections of apps that no longer have network
248 // access. Both incoming and outgoing RSTs are allowed.
249 StringAppendF(&commands, "-A %s -p tcp --tcp-flags RST RST -j RETURN\n", name);
250
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900251 if (isWhitelist) {
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900252 // Allow ICMPv6 packets necessary to make IPv6 connectivity work. http://b/23158230 .
253 if (target == V6) {
254 for (size_t i = 0; i < ARRAY_SIZE(ICMPV6_TYPES); i++) {
255 StringAppendF(&commands, "-A %s -p icmpv6 --icmpv6-type %s -j RETURN\n",
256 name, ICMPV6_TYPES[i]);
257 }
258 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900259 }
260
Lorenzo Colitti8bcb1f42017-04-25 00:17:48 +0900261 // Blacklist chains have UIDs at the end, and new UIDs are added with '-A'.
262 if (!isWhitelist) {
263 for (auto uid : uids) {
264 StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j DROP\n", name, uid);
265 }
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900266 }
267
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900268 // If it's a whitelist chain, add a default DROP at the end. This is not necessary for a
269 // blacklist chain, because all user-defined chains implicitly RETURN at the end.
270 if (isWhitelist) {
271 StringAppendF(&commands, "-A %s -j DROP\n", name);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900272 }
273
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900274 StringAppendF(&commands, "COMMIT\n");
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900275
276 return commands;
277}
278
279int FirewallController::replaceUidChain(
280 const char *name, bool isWhitelist, const std::vector<int32_t>& uids) {
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900281 std::string commands4 = makeUidRules(V4, name, isWhitelist, uids);
282 std::string commands6 = makeUidRules(V6, name, isWhitelist, uids);
283 return execIptablesRestore(V4, commands4.c_str()) | execIptablesRestore(V6, commands6.c_str());
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900284}