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