Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef _FIREWALL_CONTROLLER_H |
| 18 | #define _FIREWALL_CONTROLLER_H |
| 19 | |
Hugo Benichi | 528d3d0 | 2018-06-20 13:35:58 +0900 | [diff] [blame] | 20 | #include <sys/types.h> |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 21 | #include <mutex> |
Lorenzo Colitti | 1411d45 | 2017-07-17 22:12:15 +0900 | [diff] [blame] | 22 | #include <set> |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 23 | #include <string> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 24 | #include <vector> |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 25 | |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 26 | #include "android/net/INetd.h" |
| 27 | |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 28 | #include "NetdConstants.h" |
Chenbo Feng | 47dd073 | 2018-12-11 12:23:24 -0800 | [diff] [blame] | 29 | #include "bpf/BpfUtils.h" |
Lorenzo Colitti | 932c44c | 2016-04-24 16:58:02 +0900 | [diff] [blame] | 30 | |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace net { |
| 33 | |
| 34 | enum FirewallRule { ALLOW = INetd::FIREWALL_RULE_ALLOW, DENY = INetd::FIREWALL_RULE_DENY }; |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 35 | |
Lorenzo Colitti | cdd79f1 | 2020-07-30 12:03:40 +0900 | [diff] [blame] | 36 | // ALLOWLIST means the firewall denies all by default, uids must be explicitly ALLOWed |
| 37 | // DENYLIST means the firewall allows all by default, uids must be explicitly DENYed |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 38 | |
Jooyung Han | f3e8bbc | 2021-01-07 15:38:40 +0900 | [diff] [blame] | 39 | enum FirewallType { ALLOWLIST = INetd::FIREWALL_ALLOWLIST, DENYLIST = INetd::FIREWALL_DENYLIST }; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 40 | |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 41 | enum ChildChain { |
| 42 | NONE = INetd::FIREWALL_CHAIN_NONE, |
| 43 | DOZABLE = INetd::FIREWALL_CHAIN_DOZABLE, |
| 44 | STANDBY = INetd::FIREWALL_CHAIN_STANDBY, |
| 45 | POWERSAVE = INetd::FIREWALL_CHAIN_POWERSAVE, |
Patrick Rohr | fa0036f | 2020-12-02 16:22:28 +0100 | [diff] [blame] | 46 | RESTRICTED = INetd::FIREWALL_CHAIN_RESTRICTED, |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 47 | INVALID_CHAIN |
| 48 | }; |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 49 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 50 | /* |
| 51 | * Simple firewall that drops all packets except those matching explicitly |
| 52 | * defined ALLOW rules. |
Lorenzo Colitti | ddf2d5b | 2016-02-26 11:30:59 +0900 | [diff] [blame] | 53 | * |
| 54 | * Methods in this class must be called when holding a write lock on |lock|, and may not call |
| 55 | * any other controller without explicitly managing that controller's lock. There are currently |
| 56 | * no such methods. |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 57 | */ |
| 58 | class FirewallController { |
| 59 | public: |
Maciej Żenczykowski | 350dbdb | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 60 | FirewallController(); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 61 | |
Maciej Żenczykowski | 350dbdb | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 62 | int setupIptablesHooks(void); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 63 | |
Maciej Żenczykowski | 350dbdb | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 64 | int setFirewallType(FirewallType); |
| 65 | int resetFirewall(void); |
| 66 | int isFirewallEnabled(void); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 67 | |
Maciej Żenczykowski | 350dbdb | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 68 | /* Match traffic going in/out over the given iface. */ |
| 69 | int setInterfaceRule(const char*, FirewallRule); |
| 70 | /* Match traffic owned by given UID. This is specific to a particular chain. */ |
| 71 | int setUidRule(ChildChain, int, FirewallRule); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 72 | |
Maciej Żenczykowski | 350dbdb | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 73 | int enableChildChains(ChildChain, bool); |
Xiaohui Chen | 1cdfa9a | 2015-06-08 16:28:12 -0700 | [diff] [blame] | 74 | |
Maciej Żenczykowski | 350dbdb | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 75 | int replaceUidChain(const std::string&, bool, const std::vector<int32_t>&); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 76 | |
Maciej Żenczykowski | 350dbdb | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 77 | static std::string makeCriticalCommands(IptablesTarget target, const char* chainName); |
| 78 | static uid_t discoverMaximumValidUid(const std::string& fileName); |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 79 | |
Maciej Żenczykowski | 350dbdb | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 80 | static const char* TABLE; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 81 | |
Maciej Żenczykowski | 350dbdb | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 82 | static const char* LOCAL_INPUT; |
| 83 | static const char* LOCAL_OUTPUT; |
| 84 | static const char* LOCAL_FORWARD; |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 85 | |
Maciej Żenczykowski | 350dbdb | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 86 | static const char* LOCAL_DOZABLE; |
| 87 | static const char* LOCAL_STANDBY; |
| 88 | static const char* LOCAL_POWERSAVE; |
| 89 | static const char* LOCAL_RESTRICTED; |
Lorenzo Colitti | c8683d7 | 2015-09-01 16:53:35 +0900 | [diff] [blame] | 90 | |
Maciej Żenczykowski | 350dbdb | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 91 | static const char* ICMPV6_TYPES[]; |
Lorenzo Colitti | c8683d7 | 2015-09-01 16:53:35 +0900 | [diff] [blame] | 92 | |
Maciej Żenczykowski | 350dbdb | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 93 | std::mutex lock; |
Lorenzo Colitti | ddf2d5b | 2016-02-26 11:30:59 +0900 | [diff] [blame] | 94 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 95 | protected: |
Maciej Żenczykowski | 350dbdb | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 96 | friend class FirewallControllerTest; |
| 97 | std::string makeUidRules(IptablesTarget target, const char* name, bool isAllowlist, |
| 98 | const std::vector<int32_t>& uids); |
| 99 | static int (*execIptablesRestore)(IptablesTarget target, const std::string& commands); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 100 | |
Amith Yamasani | 390e4ea | 2015-04-25 19:08:57 -0700 | [diff] [blame] | 101 | private: |
Hugo Benichi | 528d3d0 | 2018-06-20 13:35:58 +0900 | [diff] [blame] | 102 | // Netd supports two cases, in both of which mMaxUid that derives from the uid mapping is const: |
| 103 | // - netd runs in a root namespace which contains all UIDs. |
| 104 | // - netd runs in a user namespace where the uid mapping is written once before netd starts. |
| 105 | // In that case, an attempt to write more than once to a uid_map file in a user namespace |
| 106 | // fails with EPERM. Netd can therefore assumes the max valid uid to be const. |
| 107 | const uid_t mMaxUid; |
| 108 | FirewallType mFirewallType; |
Maciej Żenczykowski | 10a5892 | 2020-02-11 15:40:19 -0800 | [diff] [blame] | 109 | bool mUseBpfOwnerMatch; |
Hugo Benichi | 528d3d0 | 2018-06-20 13:35:58 +0900 | [diff] [blame] | 110 | std::set<std::string> mIfaceRules; |
Maciej Żenczykowski | 350dbdb | 2021-10-14 20:22:23 -0700 | [diff] [blame] | 111 | int flushRules(void); |
Hugo Benichi | 528d3d0 | 2018-06-20 13:35:58 +0900 | [diff] [blame] | 112 | int attachChain(const char*, const char*); |
| 113 | int detachChain(const char*, const char*); |
| 114 | int createChain(const char*, FirewallType); |
| 115 | FirewallType getFirewallType(ChildChain); |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Luke Huang | e64fa38 | 2018-07-24 16:38:22 +0800 | [diff] [blame] | 118 | } // namespace net |
| 119 | } // namespace android |
| 120 | |
Jeff Sharkey | d8c6402 | 2012-07-13 18:04:07 -0700 | [diff] [blame] | 121 | #endif |