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 | |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 17 | #include <set> |
| 18 | |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 19 | #include <cstdint> |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #define LOG_TAG "FirewallController" |
| 26 | #define LOG_NDEBUG 0 |
| 27 | |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 28 | #include <android-base/strings.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 29 | #include <android-base/stringprintf.h> |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 30 | #include <cutils/log.h> |
| 31 | |
| 32 | #include "NetdConstants.h" |
| 33 | #include "FirewallController.h" |
| 34 | |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 35 | using android::base::Join; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 36 | using android::base::StringAppendF; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 37 | using android::base::StringPrintf; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 38 | |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 39 | auto FirewallController::execIptablesRestore = ::execIptablesRestore; |
| 40 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 41 | const char* FirewallController::TABLE = "filter"; |
| 42 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 43 | const char* FirewallController::LOCAL_INPUT = "fw_INPUT"; |
| 44 | const char* FirewallController::LOCAL_OUTPUT = "fw_OUTPUT"; |
| 45 | const char* FirewallController::LOCAL_FORWARD = "fw_FORWARD"; |
| 46 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 47 | const char* FirewallController::LOCAL_DOZABLE = "fw_dozable"; |
| 48 | const char* FirewallController::LOCAL_STANDBY = "fw_standby"; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 49 | const char* FirewallController::LOCAL_POWERSAVE = "fw_powersave"; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 50 | |
Lorenzo Colitti | c8683d7 | 2015-09-01 16:53:35 +0900 | [diff] [blame] | 51 | // ICMPv6 types that are required for any form of IPv6 connectivity to work. Note that because the |
| 52 | // fw_dozable chain is called from both INPUT and OUTPUT, this includes both packets that we need |
| 53 | // to be able to send (e.g., RS, NS), and packets that we need to receive (e.g., RA, NA). |
| 54 | const char* FirewallController::ICMPV6_TYPES[] = { |
| 55 | "packet-too-big", |
| 56 | "router-solicitation", |
| 57 | "router-advertisement", |
| 58 | "neighbour-solicitation", |
| 59 | "neighbour-advertisement", |
| 60 | "redirect", |
| 61 | }; |
| 62 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 63 | FirewallController::FirewallController(void) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 64 | // If no rules are set, it's in BLACKLIST mode |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 65 | mFirewallType = BLACKLIST; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 66 | mIfaceRules = {}; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | int FirewallController::setupIptablesHooks(void) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 70 | int res = 0; |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 71 | res |= createChain(LOCAL_DOZABLE, getFirewallType(DOZABLE)); |
| 72 | res |= createChain(LOCAL_STANDBY, getFirewallType(STANDBY)); |
| 73 | res |= createChain(LOCAL_POWERSAVE, getFirewallType(POWERSAVE)); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 74 | return res; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 77 | int FirewallController::enableFirewall(FirewallType ftype) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 78 | int res = 0; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 79 | if (mFirewallType != ftype) { |
| 80 | // flush any existing rules |
| 81 | disableFirewall(); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 82 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 83 | if (ftype == WHITELIST) { |
| 84 | // create default rule to drop all traffic |
Lorenzo Colitti | d351bea | 2017-07-16 22:52:30 +0900 | [diff] [blame] | 85 | std::string command = |
| 86 | "*filter\n" |
| 87 | "-A fw_INPUT -j DROP\n" |
| 88 | "-A fw_OUTPUT -j REJECT\n" |
| 89 | "-A fw_FORWARD -j REJECT\n" |
| 90 | "COMMIT\n"; |
| 91 | res = execIptablesRestore(V4V6, command.c_str()); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 92 | } |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 93 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 94 | // Set this after calling disableFirewall(), since it defaults to WHITELIST there |
| 95 | mFirewallType = ftype; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 96 | } |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 97 | return res; |
| 98 | } |
| 99 | |
| 100 | int FirewallController::disableFirewall(void) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 101 | mFirewallType = WHITELIST; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 102 | mIfaceRules.clear(); |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 103 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 104 | // flush any existing rules |
Lorenzo Colitti | d351bea | 2017-07-16 22:52:30 +0900 | [diff] [blame] | 105 | std::string command = |
| 106 | "*filter\n" |
| 107 | ":fw_INPUT -\n" |
| 108 | ":fw_OUTPUT -\n" |
| 109 | ":fw_FORWARD -\n" |
| 110 | "COMMIT\n"; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 111 | |
Lorenzo Colitti | d351bea | 2017-07-16 22:52:30 +0900 | [diff] [blame] | 112 | return execIptablesRestore(V4V6, command.c_str()); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 115 | int FirewallController::enableChildChains(ChildChain chain, bool enable) { |
| 116 | int res = 0; |
| 117 | const char* name; |
| 118 | switch(chain) { |
| 119 | case DOZABLE: |
| 120 | name = LOCAL_DOZABLE; |
| 121 | break; |
| 122 | case STANDBY: |
| 123 | name = LOCAL_STANDBY; |
| 124 | break; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 125 | case POWERSAVE: |
| 126 | name = LOCAL_POWERSAVE; |
| 127 | break; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 128 | default: |
| 129 | return res; |
| 130 | } |
| 131 | |
Lorenzo Colitti | a161196 | 2017-04-26 16:30:39 +0900 | [diff] [blame] | 132 | std::string command = "*filter\n"; |
| 133 | for (const char *parent : { LOCAL_INPUT, LOCAL_OUTPUT }) { |
| 134 | StringAppendF(&command, "%s %s -j %s\n", (enable ? "-A" : "-D"), parent, name); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 135 | } |
Lorenzo Colitti | a161196 | 2017-04-26 16:30:39 +0900 | [diff] [blame] | 136 | StringAppendF(&command, "COMMIT\n"); |
| 137 | |
| 138 | return execIptablesRestore(V4V6, command); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 141 | int FirewallController::isFirewallEnabled(void) { |
| 142 | // TODO: verify that rules are still in place near top |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | int FirewallController::setInterfaceRule(const char* iface, FirewallRule rule) { |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 147 | if (mFirewallType == BLACKLIST) { |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 148 | // Unsupported in BLACKLIST mode |
| 149 | return -1; |
| 150 | } |
| 151 | |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 152 | if (!isIfaceName(iface)) { |
| 153 | errno = ENOENT; |
| 154 | return -1; |
| 155 | } |
| 156 | |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 157 | // Only delete rules if we actually added them, because otherwise our iptables-restore |
| 158 | // processes will terminate with "no such rule" errors and cause latency penalties while we |
| 159 | // spin up new ones. |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 160 | const char* op; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 161 | if (rule == ALLOW && mIfaceRules.find(iface) == mIfaceRules.end()) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 162 | op = "-I"; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 163 | mIfaceRules.insert(iface); |
| 164 | } else if (rule == DENY && mIfaceRules.find(iface) != mIfaceRules.end()) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 165 | op = "-D"; |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 166 | mIfaceRules.erase(iface); |
| 167 | } else { |
| 168 | return 0; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 171 | std::string command = Join(std::vector<std::string> { |
| 172 | "*filter", |
| 173 | StringPrintf("%s fw_INPUT -i %s -j RETURN", op, iface), |
| 174 | StringPrintf("%s fw_OUTPUT -o %s -j RETURN", op, iface), |
| 175 | "COMMIT\n" |
| 176 | }, "\n"); |
| 177 | return execIptablesRestore(V4V6, command); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 180 | FirewallType FirewallController::getFirewallType(ChildChain chain) { |
| 181 | switch(chain) { |
| 182 | case DOZABLE: |
| 183 | return WHITELIST; |
| 184 | case STANDBY: |
| 185 | return BLACKLIST; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 186 | case POWERSAVE: |
| 187 | return WHITELIST; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 188 | case NONE: |
| 189 | return mFirewallType; |
| 190 | default: |
| 191 | return BLACKLIST; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | int FirewallController::setUidRule(ChildChain chain, int uid, FirewallRule rule) { |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 196 | const char* op; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 197 | const char* target; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 198 | FirewallType firewallType = getFirewallType(chain); |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 199 | if (firewallType == WHITELIST) { |
| 200 | target = "RETURN"; |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 201 | // 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] | 202 | op = (rule == ALLOW)? "-I" : "-D"; |
| 203 | } else { // BLACKLIST mode |
| 204 | target = "DROP"; |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 205 | // When adding, append DROP rules at the end, after the RETURN rule that matches TCP RSTs. |
| 206 | op = (rule == DENY)? "-A" : "-D"; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 209 | std::vector<std::string> chainNames; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 210 | switch(chain) { |
| 211 | case DOZABLE: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 212 | chainNames = { LOCAL_DOZABLE }; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 213 | break; |
| 214 | case STANDBY: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 215 | chainNames = { LOCAL_STANDBY }; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 216 | break; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 217 | case POWERSAVE: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 218 | chainNames = { LOCAL_POWERSAVE }; |
Felipe Leme | 3f62434 | 2016-02-10 18:12:39 -0800 | [diff] [blame] | 219 | break; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 220 | case NONE: |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 221 | chainNames = { LOCAL_INPUT, LOCAL_OUTPUT }; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 222 | break; |
| 223 | default: |
| 224 | ALOGW("Unknown child chain: %d", chain); |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 225 | return -1; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 226 | } |
Lorenzo Colitti | a735765 | 2017-04-25 00:16:36 +0900 | [diff] [blame] | 227 | |
| 228 | std::string command = "*filter\n"; |
| 229 | for (std::string chainName : chainNames) { |
| 230 | StringAppendF(&command, "%s %s -m owner --uid-owner %d -j %s\n", |
| 231 | op, chainName.c_str(), uid, target); |
| 232 | } |
| 233 | StringAppendF(&command, "COMMIT\n"); |
| 234 | |
| 235 | return execIptablesRestore(V4V6, command); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 238 | int FirewallController::createChain(const char* chain, FirewallType type) { |
| 239 | static const std::vector<int32_t> NO_UIDS; |
| 240 | return replaceUidChain(chain, type == WHITELIST, NO_UIDS); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 241 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 242 | |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 243 | /* static */ |
| 244 | std::string FirewallController::makeCriticalCommands(IptablesTarget target, const char* chainName) { |
| 245 | // Allow ICMPv6 packets necessary to make IPv6 connectivity work. http://b/23158230 . |
| 246 | std::string commands; |
| 247 | if (target == V6) { |
| 248 | for (size_t i = 0; i < ARRAY_SIZE(ICMPV6_TYPES); i++) { |
| 249 | StringAppendF(&commands, "-A %s -p icmpv6 --icmpv6-type %s -j RETURN\n", |
| 250 | chainName, ICMPV6_TYPES[i]); |
| 251 | } |
| 252 | } |
| 253 | return commands; |
| 254 | } |
| 255 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 256 | std::string FirewallController::makeUidRules(IptablesTarget target, const char *name, |
| 257 | bool isWhitelist, const std::vector<int32_t>& uids) { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 258 | std::string commands; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 259 | StringAppendF(&commands, "*filter\n:%s -\n", name); |
| 260 | |
Lorenzo Colitti | 8bcb1f4 | 2017-04-25 00:17:48 +0900 | [diff] [blame] | 261 | // Whitelist chains have UIDs at the beginning, and new UIDs are added with '-I'. |
| 262 | if (isWhitelist) { |
| 263 | for (auto uid : uids) { |
| 264 | StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j RETURN\n", name, uid); |
| 265 | } |
| 266 | |
| 267 | // Always whitelist system UIDs. |
| 268 | StringAppendF(&commands, |
| 269 | "-A %s -m owner --uid-owner %d-%d -j RETURN\n", name, 0, MAX_SYSTEM_UID); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 270 | |
| 271 | // This rule inverts the match for all UIDs; ie, if there is no UID match here, |
| 272 | // there is no socket to be found |
| 273 | StringAppendF(&commands, |
| 274 | "-A %s -m owner ! --uid-owner %d-%u -j RETURN\n", name, 0, UINT32_MAX-1); |
| 275 | |
| 276 | // Always whitelist traffic with protocol ESP, or no known socket - required for IPSec |
| 277 | StringAppendF(&commands, "-A %s -p esp -j RETURN\n", name); |
Lorenzo Colitti | 8bcb1f4 | 2017-04-25 00:17:48 +0900 | [diff] [blame] | 278 | } |
| 279 | |
Lorenzo Colitti | 238e818 | 2016-07-26 17:59:41 +0900 | [diff] [blame] | 280 | // Always allow networking on loopback. |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 281 | StringAppendF(&commands, "-A %s -i lo -j RETURN\n", name); |
| 282 | StringAppendF(&commands, "-A %s -o lo -j RETURN\n", name); |
Lorenzo Colitti | 238e818 | 2016-07-26 17:59:41 +0900 | [diff] [blame] | 283 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 284 | // Allow TCP RSTs so we can cleanly close TCP connections of apps that no longer have network |
| 285 | // access. Both incoming and outgoing RSTs are allowed. |
| 286 | StringAppendF(&commands, "-A %s -p tcp --tcp-flags RST RST -j RETURN\n", name); |
| 287 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 288 | if (isWhitelist) { |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 289 | commands.append(makeCriticalCommands(target, name)); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 290 | } |
| 291 | |
Lorenzo Colitti | 8bcb1f4 | 2017-04-25 00:17:48 +0900 | [diff] [blame] | 292 | // Blacklist chains have UIDs at the end, and new UIDs are added with '-A'. |
| 293 | if (!isWhitelist) { |
| 294 | for (auto uid : uids) { |
| 295 | StringAppendF(&commands, "-A %s -m owner --uid-owner %d -j DROP\n", name, uid); |
| 296 | } |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 297 | } |
| 298 | |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 299 | // If it's a whitelist chain, add a default DROP at the end. This is not necessary for a |
| 300 | // blacklist chain, because all user-defined chains implicitly RETURN at the end. |
| 301 | if (isWhitelist) { |
| 302 | StringAppendF(&commands, "-A %s -j DROP\n", name); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 303 | } |
| 304 | |
Lorenzo Colitti | 03b23fe | 2017-02-03 18:46:53 +0900 | [diff] [blame] | 305 | StringAppendF(&commands, "COMMIT\n"); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 306 | |
| 307 | return commands; |
| 308 | } |
| 309 | |
| 310 | int FirewallController::replaceUidChain( |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame^] | 311 | const std::string &name, bool isWhitelist, const std::vector<int32_t>& uids) { |
| 312 | std::string commands4 = makeUidRules(V4, name.c_str(), isWhitelist, uids); |
| 313 | std::string commands6 = makeUidRules(V6, name.c_str(), isWhitelist, uids); |
Lorenzo Colitti | f157caf | 2016-05-13 11:25:54 +0900 | [diff] [blame] | 314 | return execIptablesRestore(V4, commands4.c_str()) | execIptablesRestore(V6, commands6.c_str()); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 315 | } |