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