Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 1 | /* |
| 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 Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 26 | #include <cutils/log.h> |
| 27 | |
| 28 | #include "NetdConstants.h" |
| 29 | #include "FirewallController.h" |
| 30 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 31 | using android::base::StringAppendF; |
| 32 | |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 33 | auto FirewallController::execIptables = ::execIptables; |
Pierre Imai | 62ffdb7 | 2016-05-27 15:51:55 +0900 | [diff] [blame] | 34 | auto FirewallController::execIptablesSilently = ::execIptablesSilently; |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 35 | auto FirewallController::execIptablesRestore = ::execIptablesRestore; |
| 36 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 37 | const char* FirewallController::TABLE = "filter"; |
| 38 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 39 | const char* FirewallController::LOCAL_INPUT = "fw_INPUT"; |
| 40 | const char* FirewallController::LOCAL_OUTPUT = "fw_OUTPUT"; |
| 41 | const char* FirewallController::LOCAL_FORWARD = "fw_FORWARD"; |
| 42 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 43 | const char* FirewallController::LOCAL_DOZABLE = "fw_dozable"; |
| 44 | const char* FirewallController::LOCAL_STANDBY = "fw_standby"; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 45 | const char* FirewallController::LOCAL_POWERSAVE = "fw_powersave"; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 46 | |
Lorenzo Colitti | c8683d7 | 2015-09-01 16:53:35 +0900 | [diff] [blame] | 47 | // 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). |
| 50 | const 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 Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 59 | FirewallController::FirewallController(void) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 60 | // If no rules are set, it's in BLACKLIST mode |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 61 | mFirewallType = BLACKLIST; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | int FirewallController::setupIptablesHooks(void) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 65 | int res = 0; |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 66 | res |= createChain(LOCAL_DOZABLE, getFirewallType(DOZABLE)); |
| 67 | res |= createChain(LOCAL_STANDBY, getFirewallType(STANDBY)); |
| 68 | res |= createChain(LOCAL_POWERSAVE, getFirewallType(POWERSAVE)); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 69 | return res; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 72 | int FirewallController::enableFirewall(FirewallType ftype) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 73 | int res = 0; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 74 | if (mFirewallType != ftype) { |
| 75 | // flush any existing rules |
| 76 | disableFirewall(); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 77 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 78 | 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 Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 84 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 85 | // Set this after calling disableFirewall(), since it defaults to WHITELIST there |
| 86 | mFirewallType = ftype; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 87 | } |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 88 | return res; |
| 89 | } |
| 90 | |
| 91 | int FirewallController::disableFirewall(void) { |
| 92 | int res = 0; |
| 93 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 94 | mFirewallType = WHITELIST; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 95 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 96 | // 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 Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 104 | int 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 Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 114 | case POWERSAVE: |
| 115 | name = LOCAL_POWERSAVE; |
| 116 | break; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 117 | 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 Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 131 | int FirewallController::isFirewallEnabled(void) { |
| 132 | // TODO: verify that rules are still in place near top |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | int FirewallController::setInterfaceRule(const char* iface, FirewallRule rule) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 137 | if (mFirewallType == BLACKLIST) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 138 | // Unsupported in BLACKLIST mode |
| 139 | return -1; |
| 140 | } |
| 141 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 142 | if (!isIfaceName(iface)) { |
| 143 | errno = ENOENT; |
| 144 | return -1; |
| 145 | } |
| 146 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 147 | 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 Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 160 | FirewallType FirewallController::getFirewallType(ChildChain chain) { |
| 161 | switch(chain) { |
| 162 | case DOZABLE: |
| 163 | return WHITELIST; |
| 164 | case STANDBY: |
| 165 | return BLACKLIST; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 166 | case POWERSAVE: |
| 167 | return WHITELIST; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 168 | case NONE: |
| 169 | return mFirewallType; |
| 170 | default: |
| 171 | return BLACKLIST; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | int FirewallController::setUidRule(ChildChain chain, int uid, FirewallRule rule) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 176 | const char* op; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 177 | const char* target; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 178 | FirewallType firewallType = getFirewallType(chain); |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 179 | if (firewallType == WHITELIST) { |
| 180 | target = "RETURN"; |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 181 | // When adding, insert RETURN rules at the front, before the catch-all DROP at the end. |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 182 | op = (rule == ALLOW)? "-I" : "-D"; |
| 183 | } else { // BLACKLIST mode |
| 184 | target = "DROP"; |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 185 | // When adding, append DROP rules at the end, after the RETURN rule that matches TCP RSTs. |
| 186 | op = (rule == DENY)? "-A" : "-D"; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 189 | std::vector<std::string> chainNames; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 190 | switch(chain) { |
| 191 | case DOZABLE: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 192 | chainNames = { LOCAL_DOZABLE }; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 193 | break; |
| 194 | case STANDBY: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 195 | chainNames = { LOCAL_STANDBY }; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 196 | break; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 197 | case POWERSAVE: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 198 | chainNames = { LOCAL_POWERSAVE }; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 199 | break; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 200 | case NONE: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 201 | chainNames = { LOCAL_INPUT, LOCAL_OUTPUT }; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 202 | break; |
| 203 | default: |
| 204 | ALOGW("Unknown child chain: %d", chain); |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 205 | return -1; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 206 | } |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 207 | |
| 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 Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | int FirewallController::attachChain(const char* childChain, const char* parentChain) { |
| 219 | return execIptables(V4V6, "-t", TABLE, "-A", parentChain, "-j", childChain, NULL); |
| 220 | } |
| 221 | |
| 222 | int FirewallController::detachChain(const char* childChain, const char* parentChain) { |
| 223 | return execIptables(V4V6, "-t", TABLE, "-D", parentChain, "-j", childChain, NULL); |
| 224 | } |
| 225 | |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 226 | int 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 Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 229 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 230 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 231 | std::string FirewallController::makeUidRules(IptablesTarget target, const char *name, |
| 232 | bool isWhitelist, const std::vector<int32_t>& uids) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 233 | std::string commands; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 234 | StringAppendF(&commands, "*filter\n:%s -\n", name); |
| 235 | |
Lorenzo Colitti | 8bcb1f4 | 2017-04-25 00:17:48 +0900 | [diff] [blame^] | 236 | // Whitelist chains have UIDs at the beginning, and new UIDs are added with '-I'. |
| 237 | if (isWhitelist) { |
| 238 | for (auto uid : uids) { |
| 239 | StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j RETURN\n", name, uid); |
| 240 | } |
| 241 | |
| 242 | // Always whitelist system UIDs. |
| 243 | StringAppendF(&commands, |
| 244 | "-A %s -m owner --uid-owner %d-%d -j RETURN\n", name, 0, MAX_SYSTEM_UID); |
| 245 | } |
| 246 | |
Lorenzo Colitti | 238e818 | 2016-07-26 17:59:41 +0900 | [diff] [blame] | 247 | // Always allow networking on loopback. |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 248 | StringAppendF(&commands, "-A %s -i lo -j RETURN\n", name); |
| 249 | StringAppendF(&commands, "-A %s -o lo -j RETURN\n", name); |
Lorenzo Colitti | 238e818 | 2016-07-26 17:59:41 +0900 | [diff] [blame] | 250 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 251 | // Allow TCP RSTs so we can cleanly close TCP connections of apps that no longer have network |
| 252 | // access. Both incoming and outgoing RSTs are allowed. |
| 253 | StringAppendF(&commands, "-A %s -p tcp --tcp-flags RST RST -j RETURN\n", name); |
| 254 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 255 | if (isWhitelist) { |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 256 | // Allow ICMPv6 packets necessary to make IPv6 connectivity work. http://b/23158230 . |
| 257 | if (target == V6) { |
| 258 | for (size_t i = 0; i < ARRAY_SIZE(ICMPV6_TYPES); i++) { |
| 259 | StringAppendF(&commands, "-A %s -p icmpv6 --icmpv6-type %s -j RETURN\n", |
| 260 | name, ICMPV6_TYPES[i]); |
| 261 | } |
| 262 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 263 | } |
| 264 | |
Lorenzo Colitti | 8bcb1f4 | 2017-04-25 00:17:48 +0900 | [diff] [blame^] | 265 | // Blacklist chains have UIDs at the end, and new UIDs are added with '-A'. |
| 266 | if (!isWhitelist) { |
| 267 | for (auto uid : uids) { |
| 268 | StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j DROP\n", name, uid); |
| 269 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 270 | } |
| 271 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 272 | // If it's a whitelist chain, add a default DROP at the end. This is not necessary for a |
| 273 | // blacklist chain, because all user-defined chains implicitly RETURN at the end. |
| 274 | if (isWhitelist) { |
| 275 | StringAppendF(&commands, "-A %s -j DROP\n", name); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 276 | } |
| 277 | |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 278 | StringAppendF(&commands, "COMMIT\n"); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 279 | |
| 280 | return commands; |
| 281 | } |
| 282 | |
| 283 | int FirewallController::replaceUidChain( |
| 284 | const char *name, bool isWhitelist, const std::vector<int32_t>& uids) { |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 285 | std::string commands4 = makeUidRules(V4, name, isWhitelist, uids); |
| 286 | std::string commands6 = makeUidRules(V6, name, isWhitelist, uids); |
| 287 | return execIptablesRestore(V4, commands4.c_str()) | execIptablesRestore(V6, commands6.c_str()); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 288 | } |