blob: 9cab90a8a3f0ac6a8521d3fb1cf50e054e457fb0 [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
80 res |= execIptables(V4V6, "-A", LOCAL_INPUT, "-j", "DROP", NULL);
81 res |= execIptables(V4V6, "-A", LOCAL_OUTPUT, "-j", "REJECT", NULL);
82 res |= execIptables(V4V6, "-A", LOCAL_FORWARD, "-j", "REJECT", NULL);
83 }
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070084
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070085 // Set this after calling disableFirewall(), since it defaults to WHITELIST there
86 mFirewallType = ftype;
Amith Yamasani390e4ea2015-04-25 19:08:57 -070087 }
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070088 return res;
89}
90
91int FirewallController::disableFirewall(void) {
92 int res = 0;
93
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070094 mFirewallType = WHITELIST;
Amith Yamasani390e4ea2015-04-25 19:08:57 -070095
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070096 // flush any existing rules
97 res |= execIptables(V4V6, "-F", LOCAL_INPUT, NULL);
98 res |= execIptables(V4V6, "-F", LOCAL_OUTPUT, NULL);
99 res |= execIptables(V4V6, "-F", LOCAL_FORWARD, NULL);
100
101 return res;
102}
103
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700104int FirewallController::enableChildChains(ChildChain chain, bool enable) {
105 int res = 0;
106 const char* name;
107 switch(chain) {
108 case DOZABLE:
109 name = LOCAL_DOZABLE;
110 break;
111 case STANDBY:
112 name = LOCAL_STANDBY;
113 break;
Felipe Leme3f624342016-02-10 18:12:39 -0800114 case POWERSAVE:
115 name = LOCAL_POWERSAVE;
116 break;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700117 default:
118 return res;
119 }
120
121 if (enable) {
122 res |= attachChain(name, LOCAL_INPUT);
123 res |= attachChain(name, LOCAL_OUTPUT);
124 } else {
125 res |= detachChain(name, LOCAL_INPUT);
126 res |= detachChain(name, LOCAL_OUTPUT);
127 }
128 return res;
129}
130
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700131int FirewallController::isFirewallEnabled(void) {
132 // TODO: verify that rules are still in place near top
133 return -1;
134}
135
136int FirewallController::setInterfaceRule(const char* iface, FirewallRule rule) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700137 if (mFirewallType == BLACKLIST) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700138 // Unsupported in BLACKLIST mode
139 return -1;
140 }
141
JP Abgrall69261cb2014-06-19 18:35:24 -0700142 if (!isIfaceName(iface)) {
143 errno = ENOENT;
144 return -1;
145 }
146
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700147 const char* op;
148 if (rule == ALLOW) {
149 op = "-I";
150 } else {
151 op = "-D";
152 }
153
154 int res = 0;
155 res |= execIptables(V4V6, op, LOCAL_INPUT, "-i", iface, "-j", "RETURN", NULL);
156 res |= execIptables(V4V6, op, LOCAL_OUTPUT, "-o", iface, "-j", "RETURN", NULL);
157 return res;
158}
159
160int FirewallController::setEgressSourceRule(const char* addr, FirewallRule rule) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700161 if (mFirewallType == BLACKLIST) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700162 // Unsupported in BLACKLIST mode
163 return -1;
164 }
165
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700166 IptablesTarget target = V4;
167 if (strchr(addr, ':')) {
168 target = V6;
169 }
170
171 const char* op;
172 if (rule == ALLOW) {
173 op = "-I";
174 } else {
175 op = "-D";
176 }
177
178 int res = 0;
179 res |= execIptables(target, op, LOCAL_INPUT, "-d", addr, "-j", "RETURN", NULL);
180 res |= execIptables(target, op, LOCAL_OUTPUT, "-s", addr, "-j", "RETURN", NULL);
181 return res;
182}
183
184int FirewallController::setEgressDestRule(const char* addr, int protocol, int port,
185 FirewallRule rule) {
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700186 if (mFirewallType == BLACKLIST) {
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700187 // Unsupported in BLACKLIST mode
188 return -1;
189 }
190
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700191 IptablesTarget target = V4;
192 if (strchr(addr, ':')) {
193 target = V6;
194 }
195
196 char protocolStr[16];
197 sprintf(protocolStr, "%d", protocol);
198
199 char portStr[16];
200 sprintf(portStr, "%d", port);
201
202 const char* op;
203 if (rule == ALLOW) {
204 op = "-I";
205 } else {
206 op = "-D";
207 }
208
209 int res = 0;
210 res |= execIptables(target, op, LOCAL_INPUT, "-s", addr, "-p", protocolStr,
211 "--sport", portStr, "-j", "RETURN", NULL);
212 res |= execIptables(target, op, LOCAL_OUTPUT, "-d", addr, "-p", protocolStr,
213 "--dport", portStr, "-j", "RETURN", NULL);
214 return res;
215}
216
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700217FirewallType FirewallController::getFirewallType(ChildChain chain) {
218 switch(chain) {
219 case DOZABLE:
220 return WHITELIST;
221 case STANDBY:
222 return BLACKLIST;
Felipe Leme3f624342016-02-10 18:12:39 -0800223 case POWERSAVE:
224 return WHITELIST;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700225 case NONE:
226 return mFirewallType;
227 default:
228 return BLACKLIST;
229 }
230}
231
232int FirewallController::setUidRule(ChildChain chain, int uid, FirewallRule rule) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700233 char uidStr[16];
234 sprintf(uidStr, "%d", uid);
235
236 const char* op;
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700237 const char* target;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700238 FirewallType firewallType = getFirewallType(chain);
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700239 if (firewallType == WHITELIST) {
240 target = "RETURN";
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900241 // When adding, insert RETURN rules at the front, before the catch-all DROP at the end.
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700242 op = (rule == ALLOW)? "-I" : "-D";
243 } else { // BLACKLIST mode
244 target = "DROP";
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900245 // When adding, append DROP rules at the end, after the RETURN rule that matches TCP RSTs.
246 op = (rule == DENY)? "-A" : "-D";
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700247 }
248
249 int res = 0;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700250 switch(chain) {
251 case DOZABLE:
252 res |= execIptables(V4V6, op, LOCAL_DOZABLE, "-m", "owner", "--uid-owner",
253 uidStr, "-j", target, NULL);
254 break;
255 case STANDBY:
256 res |= execIptables(V4V6, op, LOCAL_STANDBY, "-m", "owner", "--uid-owner",
257 uidStr, "-j", target, NULL);
258 break;
Felipe Leme3f624342016-02-10 18:12:39 -0800259 case POWERSAVE:
260 res |= execIptables(V4V6, op, LOCAL_POWERSAVE, "-m", "owner", "--uid-owner",
261 uidStr, "-j", target, NULL);
262 break;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700263 case NONE:
264 res |= execIptables(V4V6, op, LOCAL_INPUT, "-m", "owner", "--uid-owner", uidStr,
265 "-j", target, NULL);
266 res |= execIptables(V4V6, op, LOCAL_OUTPUT, "-m", "owner", "--uid-owner", uidStr,
267 "-j", target, NULL);
268 break;
269 default:
270 ALOGW("Unknown child chain: %d", chain);
271 break;
272 }
273 return res;
274}
275
276int FirewallController::attachChain(const char* childChain, const char* parentChain) {
277 return execIptables(V4V6, "-t", TABLE, "-A", parentChain, "-j", childChain, NULL);
278}
279
280int FirewallController::detachChain(const char* childChain, const char* parentChain) {
281 return execIptables(V4V6, "-t", TABLE, "-D", parentChain, "-j", childChain, NULL);
282}
283
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900284int FirewallController::createChain(const char* chain, FirewallType type) {
285 static const std::vector<int32_t> NO_UIDS;
286 return replaceUidChain(chain, type == WHITELIST, NO_UIDS);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700287}
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900288
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900289std::string FirewallController::makeUidRules(IptablesTarget target, const char *name,
290 bool isWhitelist, const std::vector<int32_t>& uids) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900291 std::string commands;
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900292 StringAppendF(&commands, "*filter\n:%s -\n", name);
293
Lorenzo Colitti238e8182016-07-26 17:59:41 +0900294 // Always allow networking on loopback.
295 StringAppendF(&commands, "-A %s -i lo -o lo -j RETURN\n", name);
296
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900297 // Allow TCP RSTs so we can cleanly close TCP connections of apps that no longer have network
298 // access. Both incoming and outgoing RSTs are allowed.
299 StringAppendF(&commands, "-A %s -p tcp --tcp-flags RST RST -j RETURN\n", name);
300
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900301 if (isWhitelist) {
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900302 // Allow ICMPv6 packets necessary to make IPv6 connectivity work. http://b/23158230 .
303 if (target == V6) {
304 for (size_t i = 0; i < ARRAY_SIZE(ICMPV6_TYPES); i++) {
305 StringAppendF(&commands, "-A %s -p icmpv6 --icmpv6-type %s -j RETURN\n",
306 name, ICMPV6_TYPES[i]);
307 }
308 }
309
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900310 // Always whitelist system UIDs.
311 StringAppendF(&commands,
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900312 "-A %s -m owner --uid-owner %d-%d -j RETURN\n", name, 0, MAX_SYSTEM_UID);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900313 }
314
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900315 // Whitelist or blacklist the specified UIDs.
316 const char *action = isWhitelist ? "RETURN" : "DROP";
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900317 for (auto uid : uids) {
318 StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j %s\n", name, uid, action);
319 }
320
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900321 // If it's a whitelist chain, add a default DROP at the end. This is not necessary for a
322 // blacklist chain, because all user-defined chains implicitly RETURN at the end.
323 if (isWhitelist) {
324 StringAppendF(&commands, "-A %s -j DROP\n", name);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900325 }
326
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900327 StringAppendF(&commands, "COMMIT\n");
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900328
329 return commands;
330}
331
332int FirewallController::replaceUidChain(
333 const char *name, bool isWhitelist, const std::vector<int32_t>& uids) {
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900334 std::string commands4 = makeUidRules(V4, name, isWhitelist, uids);
335 std::string commands6 = makeUidRules(V6, name, isWhitelist, uids);
336 return execIptablesRestore(V4, commands4.c_str()) | execIptablesRestore(V6, commands6.c_str());
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900337}