blob: ddca46b2274ad917e24e4e7eec1da0685af0a429 [file] [log] [blame]
Garrick Evans64a2df32018-12-12 16:53:46 +09001// 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 Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_MINIJAILED_PROCESS_RUNNER_H_
6#define PATCHPANEL_MINIJAILED_PROCESS_RUNNER_H_
Garrick Evans64a2df32018-12-12 16:53:46 +09007
8#include <string>
9#include <vector>
10
11#include <brillo/minijail/minijail.h>
12
Garrick Evans3388a032020-03-24 11:25:55 +090013namespace patchpanel {
Garrick Evans64a2df32018-12-12 16:53:46 +090014
Jason Jeremy Imand89b5f52019-10-24 10:39:17 +090015// 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 Evans6776b502020-05-01 10:41:56 +090017// patchpaneld user.
Jason Jeremy Imand89b5f52019-10-24 10:39:17 +090018void EnterChildProcessJail();
19
Garrick Evans64a2df32018-12-12 16:53:46 +090020// Enforces the expected processes are run with the correct privileges.
21class 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 Evans2470caa2020-03-04 14:15:41 +090028 // 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 Evans64a2df32018-12-12 16:53:46 +090031
Garrick Evans8e8e3472020-01-23 14:03:50 +090032 // Runs brctl.
33 virtual int brctl(const std::string& cmd,
34 const std::vector<std::string>& argv,
35 bool log_failures = true);
Garrick Evans6d227b92019-12-03 16:11:29 +090036
37 // Runs chown to update file ownership.
Garrick Evans8e8e3472020-01-23 14:03:50 +090038 virtual int chown(const std::string& uid,
Garrick Evans6d227b92019-12-03 16:11:29 +090039 const std::string& gid,
Garrick Evans8e8e3472020-01-23 14:03:50 +090040 const std::string& file,
41 bool log_failures = true);
42
Garrick Evans8e8e3472020-01-23 14:03:50 +090043 // 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 Evans6d227b92019-12-03 16:11:29 +090077
Garrick Evans64a2df32018-12-12 16:53:46 +090078 private:
79 brillo::Minijail* mj_;
80
81 DISALLOW_COPY_AND_ASSIGN(MinijailedProcessRunner);
82};
83
Garrick Evans3388a032020-03-24 11:25:55 +090084} // namespace patchpanel
Garrick Evans64a2df32018-12-12 16:53:46 +090085
Garrick Evans3388a032020-03-24 11:25:55 +090086#endif // PATCHPANEL_MINIJAILED_PROCESS_RUNNER_H_