• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2012 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  */
5 
6 /*
7  * The general pattern of use here:
8  * 1) Construct a minijail with minijail_new()
9  * 2) Apply the desired restrictions to it
10  * 3) Enter it, which locks the current process inside it, or:
11  * 3) Run a process inside it
12  * 4) Destroy it.
13  */
14 
15 #ifndef _LIBMINIJAIL_H_
16 #define _LIBMINIJAIL_H_
17 
18 #include <stdint.h>
19 #include <sys/types.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 enum {
26 	MINIJAIL_ERR_PRELOAD = 252,
27 	MINIJAIL_ERR_JAIL = 253,
28 	MINIJAIL_ERR_INIT = 254,
29 };
30 
31 struct minijail;
32 
33 /* Allocates a new minijail with no restrictions. */
34 struct minijail *minijail_new(void);
35 
36 /*
37  * These functions add restrictions to the minijail. They are not applied until
38  * minijail_enter() is called. See the documentation in minijail0.1 for
39  * explanations in detail of what the restrictions do.
40  */
41 void minijail_change_uid(struct minijail *j, uid_t uid);
42 void minijail_change_gid(struct minijail *j, gid_t gid);
43 /* Copies |list|. */
44 void minijail_set_supplementary_gids(struct minijail *j, size_t size,
45 				     const gid_t *list);
46 void minijail_keep_supplementary_gids(struct minijail *j);
47 /* Stores user to change to and copies |user| for internal consistency. */
48 int minijail_change_user(struct minijail *j, const char *user);
49 /* Does not take ownership of |group|. */
50 int minijail_change_group(struct minijail *j, const char *group);
51 void minijail_use_seccomp(struct minijail *j);
52 void minijail_no_new_privs(struct minijail *j);
53 void minijail_use_seccomp_filter(struct minijail *j);
54 void minijail_set_seccomp_filter_tsync(struct minijail *j);
55 void minijail_parse_seccomp_filters(struct minijail *j, const char *path);
56 void minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd);
57 void minijail_log_seccomp_filter_failures(struct minijail *j);
58 /* 'minijail_use_caps' and 'minijail_capbset_drop' are mutually exclusive. */
59 void minijail_use_caps(struct minijail *j, uint64_t capmask);
60 void minijail_capbset_drop(struct minijail *j, uint64_t capmask);
61 /* 'minijail_set_ambient_caps' requires 'minijail_use_caps'. */
62 void minijail_set_ambient_caps(struct minijail *j);
63 void minijail_reset_signal_mask(struct minijail *j);
64 void minijail_namespace_vfs(struct minijail *j);
65 void minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path);
66 void minijail_new_session_keyring(struct minijail *j);
67 void minijail_skip_setting_securebits(struct minijail *j,
68 				      uint64_t securebits_skip_mask);
69 
70 /*
71  * This option is *dangerous* as it negates most of the functionality of
72  * minijail_namespace_vfs(). You very likely don't need this.
73  */
74 void minijail_skip_remount_private(struct minijail *j);
75 void minijail_namespace_ipc(struct minijail *j);
76 void minijail_namespace_uts(struct minijail *j);
77 int minijail_namespace_set_hostname(struct minijail *j, const char *name);
78 void minijail_namespace_net(struct minijail *j);
79 void minijail_namespace_enter_net(struct minijail *j, const char *ns_path);
80 void minijail_namespace_cgroups(struct minijail *j);
81 /* Closes all open file descriptors after forking. */
82 void minijail_close_open_fds(struct minijail *j);
83 /*
84  * Implies namespace_vfs and remount_proc_readonly.
85  * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>.
86  */
87 void minijail_namespace_pids(struct minijail *j);
88 void minijail_namespace_user(struct minijail *j);
89 void minijail_namespace_user_disable_setgroups(struct minijail *j);
90 int minijail_uidmap(struct minijail *j, const char *uidmap);
91 int minijail_gidmap(struct minijail *j, const char *gidmap);
92 void minijail_remount_proc_readonly(struct minijail *j);
93 void minijail_run_as_init(struct minijail *j);
94 int minijail_write_pid_file(struct minijail *j, const char *path);
95 void minijail_inherit_usergroups(struct minijail *j);
96 /*
97  * Changes the jailed process's syscall table to the alt_syscall table
98  * named |table|.
99  */
100 int minijail_use_alt_syscall(struct minijail *j, const char *table);
101 
102 /* Sets the given runtime limit. See getrlimit(2). */
103 int minijail_rlimit(struct minijail *j, int type, uint32_t cur, uint32_t max);
104 
105 /*
106  * Adds the jailed process to the cgroup given by |path|.  |path| should be the
107  * full path to the cgroups "tasks" file.
108  * Example: /sys/fs/cgroup/cpu/jailed_procs/tasks adds to the "jailed_procs" cpu
109  * cgroup.
110  */
111 int minijail_add_to_cgroup(struct minijail *j, const char *path);
112 
113 /*
114  * Install signal handlers in the minijail process that forward received
115  * signals to the jailed child process.
116  */
117 int minijail_forward_signals(struct minijail *j);
118 
119 /*
120  * minijail_enter_chroot: enables chroot() restriction for @j
121  * @j   minijail to apply restriction to
122  * @dir directory to chroot() to. Owned by caller.
123  *
124  * Enters @dir, binding all bind mounts specified with minijail_bind() into
125  * place. Requires @dir to contain all necessary directories for bind mounts
126  * (i.e., if you have requested a bind mount at /etc, /etc must exist in @dir.)
127  *
128  * Returns 0 on success.
129  */
130 int minijail_enter_chroot(struct minijail *j, const char *dir);
131 int minijail_enter_pivot_root(struct minijail *j, const char *dir);
132 
133 /*
134  * minijail_get_original_path: returns the path of a given file outside of the
135  * chroot.
136  * @j           minijail to obtain the path from.
137  * @chroot_path path inside of the chroot() to.
138  *
139  * When executing a binary in a chroot or pivot_root, return path to the binary
140  * outside of the chroot.
141  *
142  * Returns a string containing the path.  This must be freed by the caller.
143  */
144 char *minijail_get_original_path(struct minijail *j, const char *chroot_path);
145 
146 /*
147  * minijail_mount_tmp: enables mounting of a 64M tmpfs filesystem on /tmp.
148  * As be rules of bind mounts, /tmp must exist in chroot.
149  */
150 void minijail_mount_tmp(struct minijail *j);
151 
152 /*
153  * minijail_mount_tmp_size: enables mounting of a tmpfs filesystem on /tmp.
154  * As be rules of bind mounts, /tmp must exist in chroot.  Size is in bytes.
155  */
156 void minijail_mount_tmp_size(struct minijail *j, size_t size);
157 
158 /*
159  * minijail_mount_with_data: when entering minijail @j,
160  *   mounts @src at @dst with @flags and @data.
161  * @j         minijail to bind inside
162  * @src       source to bind
163  * @dest      location to bind (inside chroot)
164  * @type      type of filesystem
165  * @flags     flags passed to mount
166  * @data      data arguments passed to mount(2), e.g. "mode=755"
167  *
168  * This may be called multiple times; all mounts will be applied in the order
169  * of minijail_mount() calls.
170  */
171 int minijail_mount_with_data(struct minijail *j, const char *src,
172 			     const char *dest, const char *type,
173 			     unsigned long flags, const char *data);
174 
175 /*
176  * minijail_mount: when entering minijail @j, mounts @src at @dst with @flags
177  * @j         minijail to bind inside
178  * @src       source to bind
179  * @dest      location to bind (inside chroot)
180  * @type      type of filesystem
181  * @flags     flags passed to mount
182  *
183  * This may be called multiple times; all mounts will be applied in the order
184  * of minijail_mount() calls.
185  */
186 int minijail_mount(struct minijail *j, const char *src, const char *dest,
187 		   const char *type, unsigned long flags);
188 
189 /*
190  * minijail_bind: bind-mounts @src into @j as @dest, optionally writeable
191  * @j         minijail to bind inside
192  * @src       source to bind
193  * @dest      location to bind (inside chroot)
194  * @writeable 1 if the bind mount should be writeable
195  *
196  * This may be called multiple times; all bindings will be applied in the order
197  * of minijail_bind() calls.
198  */
199 int minijail_bind(struct minijail *j, const char *src, const char *dest,
200 		  int writeable);
201 
202 /*
203  * Lock this process into the given minijail. Note that this procedure cannot
204  * fail, since there is no way to undo privilege-dropping; therefore, if any
205  * part of the privilege-drop fails, minijail_enter() will abort the entire
206  * process.
207  *
208  * Some restrictions cannot be enabled this way (pid namespaces) and attempting
209  * to do so will cause an abort.
210  */
211 void minijail_enter(const struct minijail *j);
212 
213 /*
214  * Run the specified command in the given minijail, execve(2)-style. This is
215  * required if minijail_namespace_pids() was used.
216  */
217 int minijail_run(struct minijail *j, const char *filename,
218 		 char *const argv[]);
219 
220 /*
221  * Run the specified command in the given minijail, execve(2)-style.
222  * Used with static binaries, or on systems without support for LD_PRELOAD.
223  */
224 int minijail_run_no_preload(struct minijail *j, const char *filename,
225 			    char *const argv[]);
226 
227 /*
228  * Run the specified command in the given minijail, execve(2)-style.
229  * Update |*pchild_pid| with the pid of the child.
230  */
231 int minijail_run_pid(struct minijail *j, const char *filename,
232 		     char *const argv[], pid_t *pchild_pid);
233 
234 /*
235  * Run the specified command in the given minijail, execve(2)-style.
236  * Update |*pstdin_fd| with a fd that allows writing to the child's
237  * standard input.
238  */
239 int minijail_run_pipe(struct minijail *j, const char *filename,
240 		      char *const argv[], int *pstdin_fd);
241 
242 /*
243  * Run the specified command in the given minijail, execve(2)-style.
244  * Update |*pchild_pid| with the pid of the child.
245  * Update |*pstdin_fd| with a fd that allows writing to the child's
246  * standard input.
247  * Update |*pstdout_fd| with a fd that allows reading from the child's
248  * standard output.
249  * Update |*pstderr_fd| with a fd that allows reading from the child's
250  * standard error.
251  */
252 int minijail_run_pid_pipes(struct minijail *j, const char *filename,
253 			   char *const argv[], pid_t *pchild_pid,
254 			   int *pstdin_fd, int *pstdout_fd, int *pstderr_fd);
255 
256 /*
257  * Run the specified command in the given minijail, execve(2)-style.
258  * Update |*pchild_pid| with the pid of the child.
259  * Update |*pstdin_fd| with a fd that allows writing to the child's
260  * standard input.
261  * Update |*pstdout_fd| with a fd that allows reading from the child's
262  * standard output.
263  * Update |*pstderr_fd| with a fd that allows reading from the child's
264  * standard error.
265  * Used with static binaries, or on systems without support for LD_PRELOAD.
266  */
267 int minijail_run_pid_pipes_no_preload(struct minijail *j, const char *filename,
268 				      char *const argv[], pid_t *pchild_pid,
269 				      int *pstdin_fd, int *pstdout_fd,
270 				      int *pstderr_fd);
271 
272 /*
273  * Kill the specified minijail. The minijail must have been created with pid
274  * namespacing; if it was, all processes inside it are atomically killed.
275  */
276 int minijail_kill(struct minijail *j);
277 
278 /*
279  * Wait for all processes in the specified minijail to exit. Returns the exit
280  * status of the _first_ process spawned in the jail.
281  */
282 int minijail_wait(struct minijail *j);
283 
284 /*
285  * Frees the given minijail. It does not matter if the process is inside the
286  * minijail or not.
287  */
288 void minijail_destroy(struct minijail *j);
289 
290 #ifdef __cplusplus
291 }; /* extern "C" */
292 #endif
293 
294 #endif /* !_LIBMINIJAIL_H_ */
295