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 |
Lorenzo Colitti | d351bea | 2017-07-16 22:52:30 +0900 | [diff] [blame] | 80 | 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 Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 87 | } |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 88 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 89 | // Set this after calling disableFirewall(), since it defaults to WHITELIST there |
| 90 | mFirewallType = ftype; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 91 | } |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 92 | return res; |
| 93 | } |
| 94 | |
| 95 | int FirewallController::disableFirewall(void) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 96 | mFirewallType = WHITELIST; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 97 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 98 | // flush any existing rules |
Lorenzo Colitti | d351bea | 2017-07-16 22:52:30 +0900 | [diff] [blame] | 99 | std::string command = |
| 100 | "*filter\n" |
| 101 | ":fw_INPUT -\n" |
| 102 | ":fw_OUTPUT -\n" |
| 103 | ":fw_FORWARD -\n" |
| 104 | "COMMIT\n"; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 105 | |
Lorenzo Colitti | d351bea | 2017-07-16 22:52:30 +0900 | [diff] [blame] | 106 | return execIptablesRestore(V4V6, command.c_str()); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 109 | int 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 Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 119 | case POWERSAVE: |
| 120 | name = LOCAL_POWERSAVE; |
| 121 | break; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 122 | default: |
| 123 | return res; |
| 124 | } |
| 125 | |
Lorenzo Colitti | a161196 | 2017-04-26 16:30:39 +0900 | [diff] [blame] | 126 | 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 Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 129 | } |
Lorenzo Colitti | a161196 | 2017-04-26 16:30:39 +0900 | [diff] [blame] | 130 | StringAppendF(&command, "COMMIT\n"); |
| 131 | |
| 132 | return execIptablesRestore(V4V6, command); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 135 | int FirewallController::isFirewallEnabled(void) { |
| 136 | // TODO: verify that rules are still in place near top |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | int FirewallController::setInterfaceRule(const char* iface, FirewallRule rule) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 141 | if (mFirewallType == BLACKLIST) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 142 | // Unsupported in BLACKLIST mode |
| 143 | return -1; |
| 144 | } |
| 145 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 146 | if (!isIfaceName(iface)) { |
| 147 | errno = ENOENT; |
| 148 | return -1; |
| 149 | } |
| 150 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 151 | 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 Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 164 | FirewallType FirewallController::getFirewallType(ChildChain chain) { |
| 165 | switch(chain) { |
| 166 | case DOZABLE: |
| 167 | return WHITELIST; |
| 168 | case STANDBY: |
| 169 | return BLACKLIST; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 170 | case POWERSAVE: |
| 171 | return WHITELIST; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 172 | case NONE: |
| 173 | return mFirewallType; |
| 174 | default: |
| 175 | return BLACKLIST; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | int FirewallController::setUidRule(ChildChain chain, int uid, FirewallRule rule) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 180 | const char* op; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 181 | const char* target; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 182 | FirewallType firewallType = getFirewallType(chain); |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 183 | if (firewallType == WHITELIST) { |
| 184 | target = "RETURN"; |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 185 | // 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] | 186 | op = (rule == ALLOW)? "-I" : "-D"; |
| 187 | } else { // BLACKLIST mode |
| 188 | target = "DROP"; |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 189 | // When adding, append DROP rules at the end, after the RETURN rule that matches TCP RSTs. |
| 190 | op = (rule == DENY)? "-A" : "-D"; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 193 | std::vector<std::string> chainNames; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 194 | switch(chain) { |
| 195 | case DOZABLE: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 196 | chainNames = { LOCAL_DOZABLE }; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 197 | break; |
| 198 | case STANDBY: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 199 | chainNames = { LOCAL_STANDBY }; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 200 | break; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 201 | case POWERSAVE: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 202 | chainNames = { LOCAL_POWERSAVE }; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 203 | break; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 204 | case NONE: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 205 | chainNames = { LOCAL_INPUT, LOCAL_OUTPUT }; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 206 | break; |
| 207 | default: |
| 208 | ALOGW("Unknown child chain: %d", chain); |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 209 | return -1; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 210 | } |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 211 | |
| 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 Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 222 | int 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 Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 225 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 226 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 227 | std::string FirewallController::makeUidRules(IptablesTarget target, const char *name, |
| 228 | bool isWhitelist, const std::vector<int32_t>& uids) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 229 | std::string commands; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 230 | StringAppendF(&commands, "*filter\n:%s -\n", name); |
| 231 | |
Lorenzo Colitti | 8bcb1f4 | 2017-04-25 00:17:48 +0900 | [diff] [blame] | 232 | // 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 Colitti | 238e818 | 2016-07-26 17:59:41 +0900 | [diff] [blame] | 243 | // Always allow networking on loopback. |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 244 | StringAppendF(&commands, "-A %s -i lo -j RETURN\n", name); |
| 245 | StringAppendF(&commands, "-A %s -o lo -j RETURN\n", name); |
Lorenzo Colitti | 238e818 | 2016-07-26 17:59:41 +0900 | [diff] [blame] | 246 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 247 | // 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 Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 251 | if (isWhitelist) { |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 252 | // 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 Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 259 | } |
| 260 | |
Lorenzo Colitti | 8bcb1f4 | 2017-04-25 00:17:48 +0900 | [diff] [blame] | 261 | // 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 Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 266 | } |
| 267 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 268 | // 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 Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 272 | } |
| 273 | |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 274 | StringAppendF(&commands, "COMMIT\n"); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 275 | |
| 276 | return commands; |
| 277 | } |
| 278 | |
| 279 | int FirewallController::replaceUidChain( |
| 280 | const char *name, bool isWhitelist, const std::vector<int32_t>& uids) { |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 281 | 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 Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 284 | } |