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