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> |
Xiaohui Chen | feb2b61 | 2015-06-25 21:19:38 -0700 | [diff] [blame] | 27 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 28 | |
| 29 | #include "NetdConstants.h" |
| 30 | #include "FirewallController.h" |
| 31 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 32 | using android::base::StringAppendF; |
| 33 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 34 | const char* FirewallController::TABLE = "filter"; |
| 35 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 36 | const char* FirewallController::LOCAL_INPUT = "fw_INPUT"; |
| 37 | const char* FirewallController::LOCAL_OUTPUT = "fw_OUTPUT"; |
| 38 | const char* FirewallController::LOCAL_FORWARD = "fw_FORWARD"; |
| 39 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 40 | const char* FirewallController::LOCAL_DOZABLE = "fw_dozable"; |
| 41 | const char* FirewallController::LOCAL_STANDBY = "fw_standby"; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 42 | const char* FirewallController::LOCAL_POWERSAVE = "fw_powersave"; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 43 | |
Lorenzo Colitti | c8683d7 | 2015-09-01 16:53:35 +0900 | [diff] [blame] | 44 | // ICMPv6 types that are required for any form of IPv6 connectivity to work. Note that because the |
| 45 | // fw_dozable chain is called from both INPUT and OUTPUT, this includes both packets that we need |
| 46 | // to be able to send (e.g., RS, NS), and packets that we need to receive (e.g., RA, NA). |
| 47 | const char* FirewallController::ICMPV6_TYPES[] = { |
| 48 | "packet-too-big", |
| 49 | "router-solicitation", |
| 50 | "router-advertisement", |
| 51 | "neighbour-solicitation", |
| 52 | "neighbour-advertisement", |
| 53 | "redirect", |
| 54 | }; |
| 55 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 56 | const int MAX_SYSTEM_UID = AID_APP - 1; |
| 57 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 58 | FirewallController::FirewallController(void) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 59 | // If no rules are set, it's in BLACKLIST mode |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 60 | mFirewallType = BLACKLIST; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | int FirewallController::setupIptablesHooks(void) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 64 | int res = 0; |
| 65 | // child chains are created but not attached, they will be attached explicitly. |
| 66 | FirewallType firewallType = getFirewallType(DOZABLE); |
| 67 | res |= createChain(LOCAL_DOZABLE, LOCAL_INPUT, firewallType); |
| 68 | |
| 69 | firewallType = getFirewallType(STANDBY); |
| 70 | res |= createChain(LOCAL_STANDBY, LOCAL_INPUT, firewallType); |
| 71 | |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 72 | firewallType = getFirewallType(POWERSAVE); |
| 73 | res |= createChain(LOCAL_POWERSAVE, LOCAL_INPUT, firewallType); |
| 74 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 75 | return res; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 78 | int FirewallController::enableFirewall(FirewallType ftype) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 79 | int res = 0; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 80 | if (mFirewallType != ftype) { |
| 81 | // flush any existing rules |
| 82 | disableFirewall(); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 83 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 84 | if (ftype == WHITELIST) { |
| 85 | // create default rule to drop all traffic |
| 86 | res |= execIptables(V4V6, "-A", LOCAL_INPUT, "-j", "DROP", NULL); |
| 87 | res |= execIptables(V4V6, "-A", LOCAL_OUTPUT, "-j", "REJECT", NULL); |
| 88 | res |= execIptables(V4V6, "-A", LOCAL_FORWARD, "-j", "REJECT", NULL); |
| 89 | } |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 90 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 91 | // Set this after calling disableFirewall(), since it defaults to WHITELIST there |
| 92 | mFirewallType = ftype; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 93 | } |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 94 | return res; |
| 95 | } |
| 96 | |
| 97 | int FirewallController::disableFirewall(void) { |
| 98 | int res = 0; |
| 99 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 100 | mFirewallType = WHITELIST; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 101 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 102 | // flush any existing rules |
| 103 | res |= execIptables(V4V6, "-F", LOCAL_INPUT, NULL); |
| 104 | res |= execIptables(V4V6, "-F", LOCAL_OUTPUT, NULL); |
| 105 | res |= execIptables(V4V6, "-F", LOCAL_FORWARD, NULL); |
| 106 | |
| 107 | return res; |
| 108 | } |
| 109 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 110 | int FirewallController::enableChildChains(ChildChain chain, bool enable) { |
| 111 | int res = 0; |
| 112 | const char* name; |
| 113 | switch(chain) { |
| 114 | case DOZABLE: |
| 115 | name = LOCAL_DOZABLE; |
| 116 | break; |
| 117 | case STANDBY: |
| 118 | name = LOCAL_STANDBY; |
| 119 | break; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 120 | case POWERSAVE: |
| 121 | name = LOCAL_POWERSAVE; |
| 122 | break; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 123 | default: |
| 124 | return res; |
| 125 | } |
| 126 | |
| 127 | if (enable) { |
| 128 | res |= attachChain(name, LOCAL_INPUT); |
| 129 | res |= attachChain(name, LOCAL_OUTPUT); |
| 130 | } else { |
| 131 | res |= detachChain(name, LOCAL_INPUT); |
| 132 | res |= detachChain(name, LOCAL_OUTPUT); |
| 133 | } |
| 134 | return res; |
| 135 | } |
| 136 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 137 | int FirewallController::isFirewallEnabled(void) { |
| 138 | // TODO: verify that rules are still in place near top |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | int FirewallController::setInterfaceRule(const char* iface, FirewallRule rule) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 143 | if (mFirewallType == BLACKLIST) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 144 | // Unsupported in BLACKLIST mode |
| 145 | return -1; |
| 146 | } |
| 147 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 148 | if (!isIfaceName(iface)) { |
| 149 | errno = ENOENT; |
| 150 | return -1; |
| 151 | } |
| 152 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 153 | const char* op; |
| 154 | if (rule == ALLOW) { |
| 155 | op = "-I"; |
| 156 | } else { |
| 157 | op = "-D"; |
| 158 | } |
| 159 | |
| 160 | int res = 0; |
| 161 | res |= execIptables(V4V6, op, LOCAL_INPUT, "-i", iface, "-j", "RETURN", NULL); |
| 162 | res |= execIptables(V4V6, op, LOCAL_OUTPUT, "-o", iface, "-j", "RETURN", NULL); |
| 163 | return res; |
| 164 | } |
| 165 | |
| 166 | int FirewallController::setEgressSourceRule(const char* addr, FirewallRule rule) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 167 | if (mFirewallType == BLACKLIST) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 168 | // Unsupported in BLACKLIST mode |
| 169 | return -1; |
| 170 | } |
| 171 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 172 | IptablesTarget target = V4; |
| 173 | if (strchr(addr, ':')) { |
| 174 | target = V6; |
| 175 | } |
| 176 | |
| 177 | const char* op; |
| 178 | if (rule == ALLOW) { |
| 179 | op = "-I"; |
| 180 | } else { |
| 181 | op = "-D"; |
| 182 | } |
| 183 | |
| 184 | int res = 0; |
| 185 | res |= execIptables(target, op, LOCAL_INPUT, "-d", addr, "-j", "RETURN", NULL); |
| 186 | res |= execIptables(target, op, LOCAL_OUTPUT, "-s", addr, "-j", "RETURN", NULL); |
| 187 | return res; |
| 188 | } |
| 189 | |
| 190 | int FirewallController::setEgressDestRule(const char* addr, int protocol, int port, |
| 191 | FirewallRule rule) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 192 | if (mFirewallType == BLACKLIST) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 193 | // Unsupported in BLACKLIST mode |
| 194 | return -1; |
| 195 | } |
| 196 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 197 | IptablesTarget target = V4; |
| 198 | if (strchr(addr, ':')) { |
| 199 | target = V6; |
| 200 | } |
| 201 | |
| 202 | char protocolStr[16]; |
| 203 | sprintf(protocolStr, "%d", protocol); |
| 204 | |
| 205 | char portStr[16]; |
| 206 | sprintf(portStr, "%d", port); |
| 207 | |
| 208 | const char* op; |
| 209 | if (rule == ALLOW) { |
| 210 | op = "-I"; |
| 211 | } else { |
| 212 | op = "-D"; |
| 213 | } |
| 214 | |
| 215 | int res = 0; |
| 216 | res |= execIptables(target, op, LOCAL_INPUT, "-s", addr, "-p", protocolStr, |
| 217 | "--sport", portStr, "-j", "RETURN", NULL); |
| 218 | res |= execIptables(target, op, LOCAL_OUTPUT, "-d", addr, "-p", protocolStr, |
| 219 | "--dport", portStr, "-j", "RETURN", NULL); |
| 220 | return res; |
| 221 | } |
| 222 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 223 | FirewallType FirewallController::getFirewallType(ChildChain chain) { |
| 224 | switch(chain) { |
| 225 | case DOZABLE: |
| 226 | return WHITELIST; |
| 227 | case STANDBY: |
| 228 | return BLACKLIST; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 229 | case POWERSAVE: |
| 230 | return WHITELIST; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 231 | case NONE: |
| 232 | return mFirewallType; |
| 233 | default: |
| 234 | return BLACKLIST; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | int FirewallController::setUidRule(ChildChain chain, int uid, FirewallRule rule) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 239 | char uidStr[16]; |
| 240 | sprintf(uidStr, "%d", uid); |
| 241 | |
| 242 | const char* op; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 243 | const char* target; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 244 | FirewallType firewallType = getFirewallType(chain); |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 245 | if (firewallType == WHITELIST) { |
| 246 | target = "RETURN"; |
| 247 | op = (rule == ALLOW)? "-I" : "-D"; |
| 248 | } else { // BLACKLIST mode |
| 249 | target = "DROP"; |
| 250 | op = (rule == DENY)? "-I" : "-D"; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | int res = 0; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 254 | switch(chain) { |
| 255 | case DOZABLE: |
| 256 | res |= execIptables(V4V6, op, LOCAL_DOZABLE, "-m", "owner", "--uid-owner", |
| 257 | uidStr, "-j", target, NULL); |
| 258 | break; |
| 259 | case STANDBY: |
| 260 | res |= execIptables(V4V6, op, LOCAL_STANDBY, "-m", "owner", "--uid-owner", |
| 261 | uidStr, "-j", target, NULL); |
| 262 | break; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 263 | case POWERSAVE: |
| 264 | res |= execIptables(V4V6, op, LOCAL_POWERSAVE, "-m", "owner", "--uid-owner", |
| 265 | uidStr, "-j", target, NULL); |
| 266 | break; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 267 | case NONE: |
| 268 | res |= execIptables(V4V6, op, LOCAL_INPUT, "-m", "owner", "--uid-owner", uidStr, |
| 269 | "-j", target, NULL); |
| 270 | res |= execIptables(V4V6, op, LOCAL_OUTPUT, "-m", "owner", "--uid-owner", uidStr, |
| 271 | "-j", target, NULL); |
| 272 | break; |
| 273 | default: |
| 274 | ALOGW("Unknown child chain: %d", chain); |
| 275 | break; |
| 276 | } |
| 277 | return res; |
| 278 | } |
| 279 | |
| 280 | int FirewallController::attachChain(const char* childChain, const char* parentChain) { |
| 281 | return execIptables(V4V6, "-t", TABLE, "-A", parentChain, "-j", childChain, NULL); |
| 282 | } |
| 283 | |
| 284 | int FirewallController::detachChain(const char* childChain, const char* parentChain) { |
| 285 | return execIptables(V4V6, "-t", TABLE, "-D", parentChain, "-j", childChain, NULL); |
| 286 | } |
| 287 | |
| 288 | int FirewallController::createChain(const char* childChain, |
| 289 | const char* parentChain, FirewallType type) { |
| 290 | // Order is important, otherwise later steps may fail. |
| 291 | execIptablesSilently(V4V6, "-t", TABLE, "-D", parentChain, "-j", childChain, NULL); |
| 292 | execIptablesSilently(V4V6, "-t", TABLE, "-F", childChain, NULL); |
| 293 | execIptablesSilently(V4V6, "-t", TABLE, "-X", childChain, NULL); |
| 294 | int res = 0; |
| 295 | res |= execIptables(V4V6, "-t", TABLE, "-N", childChain, NULL); |
| 296 | if (type == WHITELIST) { |
Lorenzo Colitti | c8683d7 | 2015-09-01 16:53:35 +0900 | [diff] [blame] | 297 | // Allow ICMPv6 packets necessary to make IPv6 connectivity work. http://b/23158230 . |
| 298 | for (size_t i = 0; i < ARRAY_SIZE(ICMPV6_TYPES); i++) { |
| 299 | res |= execIptables(V6, "-A", childChain, "-p", "icmpv6", "--icmpv6-type", |
| 300 | ICMPV6_TYPES[i], "-j", "RETURN", NULL); |
| 301 | } |
| 302 | |
Xiaohui Chen | feb2b61 | 2015-06-25 21:19:38 -0700 | [diff] [blame] | 303 | // create default white list for system uid range |
| 304 | char uidStr[16]; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 305 | sprintf(uidStr, "0-%d", MAX_SYSTEM_UID); |
Xiaohui Chen | feb2b61 | 2015-06-25 21:19:38 -0700 | [diff] [blame] | 306 | res |= execIptables(V4V6, "-A", childChain, "-m", "owner", "--uid-owner", |
| 307 | uidStr, "-j", "RETURN", NULL); |
Lorenzo Colitti | c8683d7 | 2015-09-01 16:53:35 +0900 | [diff] [blame] | 308 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 309 | // create default rule to drop all traffic |
| 310 | res |= execIptables(V4V6, "-A", childChain, "-j", "DROP", NULL); |
| 311 | } |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 312 | return res; |
| 313 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 314 | |
| 315 | std::string FirewallController::makeUidRules( |
| 316 | const char *name, bool isWhitelist, const std::vector<int32_t>& uids) { |
| 317 | const char *action = isWhitelist ? "RETURN" : "DROP"; |
| 318 | const char *defaultAction = isWhitelist ? "DROP" : "RETURN"; |
| 319 | |
| 320 | std::string commands; |
| 321 | |
| 322 | StringAppendF(&commands, "*filter\n:%s -\n", name); |
| 323 | |
| 324 | if (isWhitelist) { |
| 325 | // Always whitelist system UIDs. |
| 326 | StringAppendF(&commands, |
| 327 | "-A %s -m owner --uid-owner %d-%d -j %s\n", name, 0, MAX_SYSTEM_UID, action); |
| 328 | } |
| 329 | |
| 330 | for (auto uid : uids) { |
| 331 | StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j %s\n", name, uid, action); |
| 332 | } |
| 333 | |
| 334 | // If it's a blacklist chain that blacklists nothing, then don't add a default action. |
| 335 | if (isWhitelist || uids.size() > 0) { |
| 336 | StringAppendF(&commands, "-A %s -j %s\n", name, defaultAction); |
| 337 | } |
| 338 | |
| 339 | StringAppendF(&commands, "COMMIT\n\x04"); // EOT. |
| 340 | |
| 341 | return commands; |
| 342 | } |
| 343 | |
| 344 | int FirewallController::replaceUidChain( |
| 345 | const char *name, bool isWhitelist, const std::vector<int32_t>& uids) { |
| 346 | std::string commands = makeUidRules(name, isWhitelist, uids); |
| 347 | return execIptablesRestore(V4V6, commands.c_str()); |
| 348 | } |