Garrick Evans | 64a2df3 | 2018-12-12 16:53:46 +0900 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_MINIJAILED_PROCESS_RUNNER_H_ |
| 6 | #define PATCHPANEL_MINIJAILED_PROCESS_RUNNER_H_ |
Garrick Evans | 64a2df3 | 2018-12-12 16:53:46 +0900 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <brillo/minijail/minijail.h> |
| 12 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 13 | namespace patchpanel { |
Garrick Evans | 64a2df3 | 2018-12-12 16:53:46 +0900 | [diff] [blame] | 14 | |
Jason Jeremy Iman | d89b5f5 | 2019-10-24 10:39:17 +0900 | [diff] [blame] | 15 | // Runs the current process with minimal privileges. This function is expected |
| 16 | // to be used by child processes that need only CAP_NET_RAW and to run as the |
Garrick Evans | 6776b50 | 2020-05-01 10:41:56 +0900 | [diff] [blame] | 17 | // patchpaneld user. |
Jason Jeremy Iman | d89b5f5 | 2019-10-24 10:39:17 +0900 | [diff] [blame] | 18 | void EnterChildProcessJail(); |
| 19 | |
Garrick Evans | 64a2df3 | 2018-12-12 16:53:46 +0900 | [diff] [blame] | 20 | // Enforces the expected processes are run with the correct privileges. |
| 21 | class MinijailedProcessRunner { |
| 22 | public: |
| 23 | // Ownership of |mj| is not assumed and must be managed by the caller. |
| 24 | // If |mj| is null, the default instance will be used. |
| 25 | explicit MinijailedProcessRunner(brillo::Minijail* mj = nullptr); |
| 26 | virtual ~MinijailedProcessRunner() = default; |
| 27 | |
Garrick Evans | 2470caa | 2020-03-04 14:15:41 +0900 | [diff] [blame] | 28 | // Moves interface |ifname| back into the default namespace |
| 29 | // |pid| identifies the pid of the current namespace. |
| 30 | virtual int RestoreDefaultNamespace(const std::string& ifname, pid_t pid); |
Garrick Evans | 64a2df3 | 2018-12-12 16:53:46 +0900 | [diff] [blame] | 31 | |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 32 | // Runs brctl. |
| 33 | virtual int brctl(const std::string& cmd, |
| 34 | const std::vector<std::string>& argv, |
| 35 | bool log_failures = true); |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 36 | |
| 37 | // Runs chown to update file ownership. |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 38 | virtual int chown(const std::string& uid, |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 39 | const std::string& gid, |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 40 | const std::string& file, |
| 41 | bool log_failures = true); |
| 42 | |
Garrick Evans | 8e8e347 | 2020-01-23 14:03:50 +0900 | [diff] [blame] | 43 | // Runs ip. |
| 44 | virtual int ip(const std::string& obj, |
| 45 | const std::string& cmd, |
| 46 | const std::vector<std::string>& args, |
| 47 | bool log_failures = true); |
| 48 | virtual int ip6(const std::string& obj, |
| 49 | const std::string& cmd, |
| 50 | const std::vector<std::string>& args, |
| 51 | bool log_failures = true); |
| 52 | |
| 53 | // Runs iptables. |
| 54 | virtual int iptables(const std::string& table, |
| 55 | const std::vector<std::string>& argv, |
| 56 | bool log_failures = true); |
| 57 | |
| 58 | virtual int ip6tables(const std::string& table, |
| 59 | const std::vector<std::string>& argv, |
| 60 | bool log_failures = true); |
| 61 | |
| 62 | // Installs all |modules| via modprobe. |
| 63 | virtual int modprobe_all(const std::vector<std::string>& modules, |
| 64 | bool log_failures = true); |
| 65 | |
| 66 | // Updates kernel parameter |key| to |value| using sysctl. |
| 67 | virtual int sysctl_w(const std::string& key, |
| 68 | const std::string& value, |
| 69 | bool log_failures = true); |
| 70 | |
| 71 | protected: |
| 72 | // Runs a process (argv[0]) with optional arguments (argv[1]...) |
| 73 | // in a minijail as an unprivileged user with CAP_NET_ADMIN and |
| 74 | // CAP_NET_RAW capabilities. |
| 75 | virtual int Run(const std::vector<std::string>& argv, |
| 76 | bool log_failures = true); |
Garrick Evans | 6d227b9 | 2019-12-03 16:11:29 +0900 | [diff] [blame] | 77 | |
Garrick Evans | 64a2df3 | 2018-12-12 16:53:46 +0900 | [diff] [blame] | 78 | private: |
| 79 | brillo::Minijail* mj_; |
| 80 | |
| 81 | DISALLOW_COPY_AND_ASSIGN(MinijailedProcessRunner); |
| 82 | }; |
| 83 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 84 | } // namespace patchpanel |
Garrick Evans | 64a2df3 | 2018-12-12 16:53:46 +0900 | [diff] [blame] | 85 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 86 | #endif // PATCHPANEL_MINIJAILED_PROCESS_RUNNER_H_ |