blob: f9885d7a02786e6cc7f96d320cafa8b3a4cb3b2d [file] [log] [blame]
drewry@google.combd940e92009-12-07 19:13:27 +00001// Copyright (c) 2009 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// Some portions Copyright (c) 2009 The Chromium Authors.
5//
6// Options abstract class for minijails.
7#ifndef __CHROMEOS_MINIJAIL_OPTIONS_H
8#define __CHROMEOS_MINIJAIL_OPTIONS_H
9
10#include <base/basictypes.h>
11#include <base/logging.h>
Chris Masonefcd73d42011-05-12 11:10:55 -070012#include <base/memory/scoped_ptr.h>
drewry@google.combd940e92009-12-07 19:13:27 +000013
14#include "minijail/env.h"
15
16namespace chromeos {
17namespace minijail {
18
19class Options {
20 public:
21 Options() : env_(new Env),
22 executable_path_(NULL),
23 argument_count_(0),
24 arguments_(NULL),
25 environment_(NULL),
26 add_readonly_mounts_(false),
Chris Masone4463bea2011-04-27 08:22:40 -070027 caps_bitmask_(0),
drewry@google.combd940e92009-12-07 19:13:27 +000028 change_gid_(false),
29 change_uid_(false),
30 disable_tracing_(false),
31 enforce_syscalls_benchmark_(false),
32 enforce_syscalls_by_source_(false),
33 gid_(0),
34 namespace_vfs_(false),
35 namespace_pid_(false),
36 sanitize_environment_(false),
37 uid_(0),
38 use_capabilities_(false) { }
39
40 virtual ~Options() { }
41
42 // Takes ownership of an Env pointer
43 virtual const Env *env() const { return env_.get(); }
44 virtual void set_env(Env *env) { env_.reset(env); }
45
46 //// Methods for configuring the binary to be run.
47
48 // Sets the path to the executable when Run() is called in the jail.
49 // Pointer ownership is not taken.
50 virtual void set_executable_path(const char *exe) { executable_path_ = exe; }
51 virtual const char *executable_path() const { return executable_path_; }
52 // Sets an array of arguments to use for running the executable.
53 // Pointer ownership is not taken.
54 virtual void set_arguments(char * const *argv, int count)
55 { arguments_ = argv; argument_count_ = count; }
56 virtual char * const *arguments() const { return arguments_; }
57 virtual int argument_count() const { return argument_count_; }
58 // Sets the baseline environment for the executable.
59 // Pointer ownership is not taken.
60 virtual void set_environment(char * const *envp) { environment_ = envp; }
61 virtual char * const *environment() const { return environment_; }
62
63 //// Methods for configuring the jail.
64
65 // Determines if a read-only /proc will be mounted.
66 // This option requires namespace_vfs_ = true.
67 // If enabled, this option forcibly enables namespace_vfs_.
68 virtual void set_add_readonly_mounts(bool val) { add_readonly_mounts_ = val; }
69 virtual bool add_readonly_mounts() const { return add_readonly_mounts_; }
70 // Disables cross-process tracing and core dumps. This may cause problems
71 // when generating crash dumps. Options around that are TBD.
72 virtual void set_disable_tracing(bool val) { disable_tracing_ = val; }
73 virtual bool disable_tracing() const { return disable_tracing_; }
74 // Enable no-op syscall filtering for raw benchmarking.
75 virtual void set_enforce_syscalls_benchmark(bool val)
76 { enforce_syscalls_benchmark_ = val; }
77 virtual bool enforce_syscalls_benchmark() const
78 { return enforce_syscalls_benchmark_; }
79 // Enable kernel enforcement that all system calls originate from
80 // read-only memory areas.
81 virtual void set_enforce_syscalls_by_source(bool val)
82 { enforce_syscalls_by_source_ = val; }
83 virtual bool enforce_syscalls_by_source() const
84 { return enforce_syscalls_by_source_; }
85 // The value passed with this is numeric GID to transition to.
86 // Calling this implies a gid change will be attempted.
87 // TODO(wad) All supplementary groups are dropped.
88 virtual void set_gid(gid_t val) { gid_ = val; change_gid_ = true; }
89 virtual gid_t gid() const { return gid_; }
90 // Sets VFS namespacing. This is needed to have a custom
91 // filesystem view (read-only mounts, etc).
92 virtual void set_namespace_vfs(bool val) { namespace_vfs_ = val; }
93 virtual bool namespace_vfs() const { return namespace_vfs_; }
94 // Enable PID namespacing. This will result in the process being
95 // executed to be PID 1 in their own process tree. The process will
96 // not have visibility into other running processes (except via
97 // /proc if not remounted).
98 // TODO(wad) add init-like functionality and start the first process as pid 2.
99 virtual void set_namespace_pid(bool val) { namespace_pid_ = val; }
100 virtual bool namespace_pid() const { return namespace_pid_; }
101 // Enables environment variable scrubbing.
102 virtual void set_sanitize_environment(bool val)
103 { sanitize_environment_ = val; }
104 virtual bool sanitize_environment() const
105 { return sanitize_environment_; }
106 // The value passed with this is the numeric UID to transition to.
107 virtual void set_uid(uid_t val) { uid_ = val; change_uid_ = true; }
108 virtual uid_t uid() const { return uid_; }
109 // Enables the use and sanitization of POSIX capabilities.
110 // Without kKeepCapabilities, all capabilities save CAP_SETPCAP are
111 // removed from the effective, inherited, permitted and bounding sets.
112 virtual void set_use_capabilities(bool val) { use_capabilities_ = val; }
113 virtual bool use_capabilities() const { return use_capabilities_; }
Chris Masone4463bea2011-04-27 08:22:40 -0700114 // The set of caps to use when use_capabilities is set.
115 virtual void set_caps_bitmask(uint64 val) { caps_bitmask_ = val; }
116 virtual uint64 caps_bitmask() const { return caps_bitmask_; }
drewry@google.combd940e92009-12-07 19:13:27 +0000117
118#if 0
119 TODO(wad): additional functionality:
120 virtual void set_cgroup_dir(const string& val) { cgroup_dir_ = val; }
121 virtual const string& cgroup_dir() const { return cgroup_dir_; }
122
123 virtual void set_supplemental_groups(std::vector<std::string>& val)
124 { supplemental_groups_ = val; }
125 virtual const std::vector<std::string> *supplemental_groups() const
126 { return supplemental_groups_; }
127
drewry@google.combd940e92009-12-07 19:13:27 +0000128 virtual void set_use_delayed_chroot(bool val) { use_delayed_chroot_ = val; }
129 virtual bool use_delayed_chroot() const { return use_delayed_chroot_; }
130
131 virtual void set_memory_limit(int64 val) { memory_limit_ = val; }
132 virtual int64 memory_limit() const { return memory_limit_; }
133
134 virtual void set_cpu_limit(int64 val) { cpu_limit_ = val; }
135 virtual int64 cpu_limit() const { return cpu_limit_; }
136
137 virtual void set_open_file_limit(int32 val) { open_file_limit_ = val; }
138 virtual int32 open_file_limit() const { return open_file_limit_; }
139
140 TODO(wad) other rlimits
141
142 virtual void set_chroot(const std::string val) { chroot_ = val; }
143 virtual const std::string chroot() const { return chroot_; }
144
145 virtual void set_install_device_shims(bool val)
146 { install_device_shims_ = val; }
147 virtual bool install_device_shims() const { return install_device_shims_; }
148#endif
149
150 //// Helper methods
151 // Indicate if the uid was set.
152 virtual bool change_uid() const { return change_uid_; }
153 // Indicate if the gid was set.
154 virtual bool change_gid() const { return change_uid_; }
155 // Ensures that all inter-dependent options are properly set.
156 virtual bool FixUpDependencies();
157
158
159 private:
160 scoped_ptr<Env> env_;
161 const char *executable_path_;
162 int argument_count_;
163 char * const *arguments_;
164 char * const *environment_;
165
166 bool add_readonly_mounts_;
Chris Masone4463bea2011-04-27 08:22:40 -0700167 uint64 caps_bitmask_;
drewry@google.combd940e92009-12-07 19:13:27 +0000168 bool change_gid_;
169 bool change_uid_;
170 bool disable_tracing_;
171 bool enforce_syscalls_benchmark_;
172 bool enforce_syscalls_by_source_;
173 gid_t gid_;
174 bool namespace_vfs_;
175 bool namespace_pid_;
176 bool sanitize_environment_;
177 uid_t uid_;
178 bool use_capabilities_;
179
180 DISALLOW_COPY_AND_ASSIGN(Options);
181};
182
183} // namespace minijail
184} // namespace chromeos
185
186#endif // __CHROMEOS_MINIJAIL_OPTIONS_H