blob: 6de1b45a4cf8e5b86d3cd0a4e4a7762b86925212 [file] [log] [blame]
Jeff Sharkeyd8c64022012-07-13 18:04:07 -07001/*
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 Benichi528d3d02018-06-20 13:35:58 +090020#include <sys/types.h>
Luke Huangd1ee4622018-06-29 13:49:58 +080021#include <mutex>
Lorenzo Colitti1411d452017-07-17 22:12:15 +090022#include <set>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070023#include <string>
Lorenzo Colitti89faa342016-02-26 11:38:47 +090024#include <vector>
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070025
Luke Huange64fa382018-07-24 16:38:22 +080026#include "android/net/INetd.h"
27
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090028#include "NetdConstants.h"
Chenbo Feng47dd0732018-12-11 12:23:24 -080029#include "bpf/BpfUtils.h"
Lorenzo Colitti932c44c2016-04-24 16:58:02 +090030
Luke Huange64fa382018-07-24 16:38:22 +080031namespace android {
32namespace net {
33
34enum FirewallRule { ALLOW = INetd::FIREWALL_RULE_ALLOW, DENY = INetd::FIREWALL_RULE_DENY };
Amith Yamasani390e4ea2015-04-25 19:08:57 -070035
Lorenzo Colitticdd79f12020-07-30 12:03:40 +090036// 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 Yamasani390e4ea2015-04-25 19:08:57 -070038
Jooyung Hanf3e8bbc2021-01-07 15:38:40 +090039enum FirewallType { ALLOWLIST = INetd::FIREWALL_ALLOWLIST, DENYLIST = INetd::FIREWALL_DENYLIST };
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070040
Luke Huange64fa382018-07-24 16:38:22 +080041enum 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 Rohrfa0036f2020-12-02 16:22:28 +010046 RESTRICTED = INetd::FIREWALL_CHAIN_RESTRICTED,
Luke Huange64fa382018-07-24 16:38:22 +080047 INVALID_CHAIN
48};
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070049
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070050/*
51 * Simple firewall that drops all packets except those matching explicitly
52 * defined ALLOW rules.
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090053 *
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 Sharkeyd8c64022012-07-13 18:04:07 -070057 */
58class FirewallController {
59public:
Maciej Żenczykowski350dbdb2021-10-14 20:22:23 -070060 FirewallController();
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070061
Maciej Żenczykowski350dbdb2021-10-14 20:22:23 -070062 int setupIptablesHooks(void);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070063
Maciej Żenczykowski350dbdb2021-10-14 20:22:23 -070064 int setFirewallType(FirewallType);
65 int resetFirewall(void);
66 int isFirewallEnabled(void);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070067
Maciej Żenczykowski350dbdb2021-10-14 20:22:23 -070068 /* 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 Chen1cdfa9a2015-06-08 16:28:12 -070072
Maciej Żenczykowski350dbdb2021-10-14 20:22:23 -070073 int enableChildChains(ChildChain, bool);
Xiaohui Chen1cdfa9a2015-06-08 16:28:12 -070074
Maciej Żenczykowski350dbdb2021-10-14 20:22:23 -070075 int replaceUidChain(const std::string&, bool, const std::vector<int32_t>&);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090076
Maciej Żenczykowski350dbdb2021-10-14 20:22:23 -070077 static std::string makeCriticalCommands(IptablesTarget target, const char* chainName);
78 static uid_t discoverMaximumValidUid(const std::string& fileName);
Lorenzo Colittiaff28792017-09-26 17:46:18 +090079
Maciej Żenczykowski350dbdb2021-10-14 20:22:23 -070080 static const char* TABLE;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070081
Maciej Żenczykowski350dbdb2021-10-14 20:22:23 -070082 static const char* LOCAL_INPUT;
83 static const char* LOCAL_OUTPUT;
84 static const char* LOCAL_FORWARD;
Jeff Sharkeyd8c64022012-07-13 18:04:07 -070085
Maciej Żenczykowski350dbdb2021-10-14 20:22:23 -070086 static const char* LOCAL_DOZABLE;
87 static const char* LOCAL_STANDBY;
88 static const char* LOCAL_POWERSAVE;
89 static const char* LOCAL_RESTRICTED;
Lorenzo Colittic8683d72015-09-01 16:53:35 +090090
Maciej Żenczykowski350dbdb2021-10-14 20:22:23 -070091 static const char* ICMPV6_TYPES[];
Lorenzo Colittic8683d72015-09-01 16:53:35 +090092
Maciej Żenczykowski350dbdb2021-10-14 20:22:23 -070093 std::mutex lock;
Lorenzo Colittiddf2d5b2016-02-26 11:30:59 +090094
Lorenzo Colitti89faa342016-02-26 11:38:47 +090095protected:
Maciej Żenczykowski350dbdb2021-10-14 20:22:23 -070096 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 Colitti89faa342016-02-26 11:38:47 +0900100
Amith Yamasani390e4ea2015-04-25 19:08:57 -0700101private:
Hugo Benichi528d3d02018-06-20 13:35:58 +0900102 // 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 Żenczykowski10a58922020-02-11 15:40:19 -0800109 bool mUseBpfOwnerMatch;
Hugo Benichi528d3d02018-06-20 13:35:58 +0900110 std::set<std::string> mIfaceRules;
Maciej Żenczykowski350dbdb2021-10-14 20:22:23 -0700111 int flushRules(void);
Hugo Benichi528d3d02018-06-20 13:35:58 +0900112 int attachChain(const char*, const char*);
113 int detachChain(const char*, const char*);
114 int createChain(const char*, FirewallType);
115 FirewallType getFirewallType(ChildChain);
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700116};
117
Luke Huange64fa382018-07-24 16:38:22 +0800118} // namespace net
119} // namespace android
120
Jeff Sharkeyd8c64022012-07-13 18:04:07 -0700121#endif