blob: 46932067d190c63ecc8ae99257786497294c2e58 [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
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700160FirewallType FirewallController::getFirewallType(ChildChain chain) {
161 switch(chain) {
162 case DOZABLE:
163 return WHITELIST;
164 case STANDBY:
165 return BLACKLIST;
Felipe Leme3f624342016-02-10 18:12:39 -0800166 case POWERSAVE:
167 return WHITELIST;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700168 case NONE:
169 return mFirewallType;
170 default:
171 return BLACKLIST;
172 }
173}
174
175int FirewallController::setUidRule(ChildChain chain, int uid, FirewallRule rule) {
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700176 const char* op;
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700177 const char* target;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700178 FirewallType firewallType = getFirewallType(chain);
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700179 if (firewallType == WHITELIST) {
180 target = "RETURN";
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900181 // When adding, insert RETURN rules at the front, before the catch-all DROP at the end.
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700182 op = (rule == ALLOW)? "-I" : "-D";
183 } else { // BLACKLIST mode
184 target = "DROP";
Lorenzo Colitti932c44c2016-04-24 16:58:02 +0900185 // When adding, append DROP rules at the end, after the RETURN rule that matches TCP RSTs.
186 op = (rule == DENY)? "-A" : "-D";
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700187 }
188
Lorenzo Colittia7357652017-04-25 00:16:36 +0900189 std::vector<std::string> chainNames;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700190 switch(chain) {
191 case DOZABLE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900192 chainNames = { LOCAL_DOZABLE };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700193 break;
194 case STANDBY:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900195 chainNames = { LOCAL_STANDBY };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700196 break;
Felipe Leme3f624342016-02-10 18:12:39 -0800197 case POWERSAVE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900198 chainNames = { LOCAL_POWERSAVE };
Felipe Leme3f624342016-02-10 18:12:39 -0800199 break;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700200 case NONE:
Lorenzo Colittia7357652017-04-25 00:16:36 +0900201 chainNames = { LOCAL_INPUT, LOCAL_OUTPUT };
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700202 break;
203 default:
204 ALOGW("Unknown child chain: %d", chain);
Lorenzo Colittia7357652017-04-25 00:16:36 +0900205 return -1;
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700206 }
Lorenzo Colittia7357652017-04-25 00:16:36 +0900207
208 std::string command = "*filter\n";
209 for (std::string chainName : chainNames) {
210 StringAppendF(&command, "%s %s -m owner --uid-owner %d -j %s\n",
211 op, chainName.c_str(), uid, target);
212 }
213 StringAppendF(&command, "COMMIT\n");
214
215 return execIptablesRestore(V4V6, command);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -0700216}
217
218int FirewallController::attachChain(const char* childChain, const char* parentChain) {
219 return execIptables(V4V6, "-t", TABLE, "-A", parentChain, "-j", childChain, NULL);
220}
221
222int FirewallController::detachChain(const char* childChain, const char* parentChain) {
223 return execIptables(V4V6, "-t", TABLE, "-D", parentChain, "-j", childChain, NULL);
224}
225
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900226int FirewallController::createChain(const char* chain, FirewallType type) {
227 static const std::vector<int32_t> NO_UIDS;
228 return replaceUidChain(chain, type == WHITELIST, NO_UIDS);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700229}
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900230
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900231std::string FirewallController::makeUidRules(IptablesTarget target, const char *name,
232 bool isWhitelist, const std::vector<int32_t>& uids) {
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900233 std::string commands;
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900234 StringAppendF(&commands, "*filter\n:%s -\n", name);
235
Lorenzo Colitti238e8182016-07-26 17:59:41 +0900236 // Always allow networking on loopback.
Lorenzo Colitti50b198a2017-03-30 02:50:09 +0900237 StringAppendF(&commands, "-A %s -i lo -j RETURN\n", name);
238 StringAppendF(&commands, "-A %s -o lo -j RETURN\n", name);
Lorenzo Colitti238e8182016-07-26 17:59:41 +0900239
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900240 // Allow TCP RSTs so we can cleanly close TCP connections of apps that no longer have network
241 // access. Both incoming and outgoing RSTs are allowed.
242 StringAppendF(&commands, "-A %s -p tcp --tcp-flags RST RST -j RETURN\n", name);
243
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900244 if (isWhitelist) {
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900245 // Allow ICMPv6 packets necessary to make IPv6 connectivity work. http://b/23158230 .
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 name, ICMPV6_TYPES[i]);
250 }
251 }
252
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900253 // Always whitelist system UIDs.
254 StringAppendF(&commands,
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900255 "-A %s -m owner --uid-owner %d-%d -j RETURN\n", name, 0, MAX_SYSTEM_UID);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900256 }
257
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900258 // Whitelist or blacklist the specified UIDs.
259 const char *action = isWhitelist ? "RETURN" : "DROP";
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900260 for (auto uid : uids) {
261 StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j %s\n", name, uid, action);
262 }
263
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900264 // If it's a whitelist chain, add a default DROP at the end. This is not necessary for a
265 // blacklist chain, because all user-defined chains implicitly RETURN at the end.
266 if (isWhitelist) {
267 StringAppendF(&commands, "-A %s -j DROP\n", name);
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900268 }
269
Lorenzo Colitti03b23fe2017-02-03 18:46:53 +0900270 StringAppendF(&commands, "COMMIT\n");
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900271
272 return commands;
273}
274
275int FirewallController::replaceUidChain(
276 const char *name, bool isWhitelist, const std::vector<int32_t>& uids) {
Lorenzo Colittif157caf2016-05-13 11:25:54 +0900277 std::string commands4 = makeUidRules(V4, name, isWhitelist, uids);
278 std::string commands6 = makeUidRules(V6, name, isWhitelist, uids);
279 return execIptablesRestore(V4, commands4.c_str()) | execIptablesRestore(V6, commands6.c_str());
Lorenzo Colitti89faa342016-02-26 11:38:47 +0900280}