blob: a783067ad2e4d5a7ac478ecda219570afec9b5c7 [file] [log] [blame]
Elly Jonese58176c2012-01-23 11:46:17 -05001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jonescd7a9042011-07-22 13:56:51 -04002 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -08006/*
7 * The general pattern of use here:
Elly Jonescd7a9042011-07-22 13:56:51 -04008 * 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
Elly Jonese1749eb2011-10-07 13:54:59 -040015#ifndef _LIBMINIJAIL_H_
16#define _LIBMINIJAIL_H_
Elly Jonescd7a9042011-07-22 13:56:51 -040017
18#include <stdint.h>
19#include <sys/types.h>
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25enum {
Elly Jonese1749eb2011-10-07 13:54:59 -040026 MINIJAIL_ERR_PRELOAD = 252,
27 MINIJAIL_ERR_JAIL = 253,
28 MINIJAIL_ERR_INIT = 254,
Elly Jonescd7a9042011-07-22 13:56:51 -040029};
30
31struct minijail;
32
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -070033/*
34 * A hook that can be used to execute code at various events during minijail
35 * setup in the forked process. These can only be used if the jailed process is
36 * not going to be invoked with LD_PRELOAD.
37 *
38 * If the return value is non-zero, it will be interpreted as -errno and the
39 * process will abort.
40 */
41typedef int (*minijail_hook_t)(void *context);
42
43/*
44 * The events during minijail setup in which hooks can run. All the events are
45 * run in the new process.
46 */
47typedef enum {
48 /* The hook will run just before dropping capabilities. */
49 MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS,
50
51 /* The hook will run just before calling execve(2). */
52 MINIJAIL_HOOK_EVENT_PRE_EXECVE,
53
54 /* Sentinel for error checking. Must be last. */
55 MINIJAIL_HOOK_EVENT_MAX,
56} minijail_hook_event_t;
57
Elly Jonescd7a9042011-07-22 13:56:51 -040058/* Allocates a new minijail with no restrictions. */
59struct minijail *minijail_new(void);
60
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080061/*
62 * These functions add restrictions to the minijail. They are not applied until
Elly Jonescd7a9042011-07-22 13:56:51 -040063 * minijail_enter() is called. See the documentation in minijail0.1 for
Elly Jonese1749eb2011-10-07 13:54:59 -040064 * explanations in detail of what the restrictions do.
65 */
Elly Jonescd7a9042011-07-22 13:56:51 -040066void minijail_change_uid(struct minijail *j, uid_t uid);
67void minijail_change_gid(struct minijail *j, gid_t gid);
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -080068/* Copies |list|. */
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -080069void minijail_set_supplementary_gids(struct minijail *j, size_t size,
70 const gid_t *list);
Lutz Justen13807cb2017-01-03 17:11:55 +010071void minijail_keep_supplementary_gids(struct minijail *j);
Will Drewry2ddaad02011-09-16 11:36:08 -050072/* Stores user to change to and copies |user| for internal consistency. */
Elly Jonescd7a9042011-07-22 13:56:51 -040073int minijail_change_user(struct minijail *j, const char *user);
Will Drewry2ddaad02011-09-16 11:36:08 -050074/* Does not take ownership of |group|. */
Elly Jonescd7a9042011-07-22 13:56:51 -040075int minijail_change_group(struct minijail *j, const char *group);
76void minijail_use_seccomp(struct minijail *j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070077void minijail_no_new_privs(struct minijail *j);
Will Drewry32ac9f52011-08-18 21:36:27 -050078void minijail_use_seccomp_filter(struct minijail *j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040079void minijail_set_seccomp_filter_tsync(struct minijail *j);
Will Drewry32ac9f52011-08-18 21:36:27 -050080void minijail_parse_seccomp_filters(struct minijail *j, const char *path);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -040081void minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070082void minijail_log_seccomp_filter_failures(struct minijail *j);
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -080083/* 'minijail_use_caps' and 'minijail_capbset_drop' are mutually exclusive. */
Elly Jonescd7a9042011-07-22 13:56:51 -040084void minijail_use_caps(struct minijail *j, uint64_t capmask);
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -080085void minijail_capbset_drop(struct minijail *j, uint64_t capmask);
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -040086/* 'minijail_set_ambient_caps' requires 'minijail_use_caps'. */
87void minijail_set_ambient_caps(struct minijail *j);
Peter Qiu2860c462015-12-16 15:13:06 -080088void minijail_reset_signal_mask(struct minijail *j);
Elly Jonescd7a9042011-07-22 13:56:51 -040089void minijail_namespace_vfs(struct minijail *j);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070090void minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path);
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080091void minijail_new_session_keyring(struct minijail *j);
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -070092void minijail_skip_setting_securebits(struct minijail *j,
93 uint64_t securebits_skip_mask);
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080094
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -080095/*
96 * This option is *dangerous* as it negates most of the functionality of
97 * minijail_namespace_vfs(). You very likely don't need this.
98 */
99void minijail_skip_remount_private(struct minijail *j);
Dylan Reidf7942472015-11-18 17:55:26 -0800100void minijail_namespace_ipc(struct minijail *j);
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400101void minijail_namespace_uts(struct minijail *j);
102int minijail_namespace_set_hostname(struct minijail *j, const char *name);
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400103void minijail_namespace_net(struct minijail *j);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700104void minijail_namespace_enter_net(struct minijail *j, const char *ns_path);
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700105void minijail_namespace_cgroups(struct minijail *j);
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700106/* Closes all open file descriptors after forking. */
107void minijail_close_open_fds(struct minijail *j);
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800108/*
109 * Implies namespace_vfs and remount_proc_readonly.
Elly Jones761b7412012-06-13 15:49:52 -0400110 * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>.
111 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400112void minijail_namespace_pids(struct minijail *j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800113void minijail_namespace_user(struct minijail *j);
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400114void minijail_namespace_user_disable_setgroups(struct minijail *j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800115int minijail_uidmap(struct minijail *j, const char *uidmap);
116int minijail_gidmap(struct minijail *j, const char *gidmap);
Dylan Reid791f5772015-09-14 20:02:42 -0700117void minijail_remount_proc_readonly(struct minijail *j);
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800118void minijail_run_as_init(struct minijail *j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800119int minijail_write_pid_file(struct minijail *j, const char *path);
Elly Jonescd7a9042011-07-22 13:56:51 -0400120void minijail_inherit_usergroups(struct minijail *j);
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800121/*
122 * Changes the jailed process's syscall table to the alt_syscall table
Andrew Brestickereac28942015-11-11 16:04:46 -0800123 * named |table|.
124 */
125int minijail_use_alt_syscall(struct minijail *j, const char *table);
Elly Jonescd7a9042011-07-22 13:56:51 -0400126
Dylan Reid0f72ef42017-06-06 15:42:49 -0700127/* Sets the given runtime limit. See getrlimit(2). */
128int minijail_rlimit(struct minijail *j, int type, uint32_t cur, uint32_t max);
129
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800130/*
Dylan Reid605ce7f2016-01-19 19:21:00 -0800131 * Adds the jailed process to the cgroup given by |path|. |path| should be the
132 * full path to the cgroups "tasks" file.
133 * Example: /sys/fs/cgroup/cpu/jailed_procs/tasks adds to the "jailed_procs" cpu
134 * cgroup.
135 */
136int minijail_add_to_cgroup(struct minijail *j, const char *path);
137
138/*
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400139 * Install signal handlers in the minijail process that forward received
140 * signals to the jailed child process.
141 */
142int minijail_forward_signals(struct minijail *j);
143
144/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800145 * minijail_enter_chroot: enables chroot() restriction for @j
Elly Jones51a5b6c2011-10-12 19:09:26 -0400146 * @j minijail to apply restriction to
147 * @dir directory to chroot() to. Owned by caller.
148 *
149 * Enters @dir, binding all bind mounts specified with minijail_bind() into
150 * place. Requires @dir to contain all necessary directories for bind mounts
151 * (i.e., if you have requested a bind mount at /etc, /etc must exist in @dir.)
152 *
153 * Returns 0 on success.
154 */
155int minijail_enter_chroot(struct minijail *j, const char *dir);
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800156int minijail_enter_pivot_root(struct minijail *j, const char *dir);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400157
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800158/*
159 * minijail_get_original_path: returns the path of a given file outside of the
Dylan Reid08946cc2015-09-16 19:10:57 -0700160 * chroot.
161 * @j minijail to obtain the path from.
162 * @chroot_path path inside of the chroot() to.
163 *
164 * When executing a binary in a chroot or pivot_root, return path to the binary
165 * outside of the chroot.
166 *
167 * Returns a string containing the path. This must be freed by the caller.
168 */
169char *minijail_get_original_path(struct minijail *j, const char *chroot_path);
170
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800171/*
Martin Pelikánab9eb442017-01-25 11:53:58 +1100172 * minijail_mount_tmp: enables mounting of a 64M tmpfs filesystem on /tmp.
Lee Campbell11af0622014-05-22 12:36:04 -0700173 * As be rules of bind mounts, /tmp must exist in chroot.
174 */
175void minijail_mount_tmp(struct minijail *j);
176
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800177/*
Martin Pelikánab9eb442017-01-25 11:53:58 +1100178 * minijail_mount_tmp_size: enables mounting of a tmpfs filesystem on /tmp.
179 * As be rules of bind mounts, /tmp must exist in chroot. Size is in bytes.
180 */
181void minijail_mount_tmp_size(struct minijail *j, size_t size);
182
183/*
Dylan Reid81e23972016-05-18 14:06:35 -0700184 * minijail_mount_with_data: when entering minijail @j,
185 * mounts @src at @dst with @flags and @data.
186 * @j minijail to bind inside
187 * @src source to bind
188 * @dest location to bind (inside chroot)
189 * @type type of filesystem
190 * @flags flags passed to mount
191 * @data data arguments passed to mount(2), e.g. "mode=755"
192 *
193 * This may be called multiple times; all mounts will be applied in the order
194 * of minijail_mount() calls.
195 */
196int minijail_mount_with_data(struct minijail *j, const char *src,
197 const char *dest, const char *type,
198 unsigned long flags, const char *data);
199
200/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800201 * minijail_mount: when entering minijail @j, mounts @src at @dst with @flags
Dylan Reid648b2202015-10-23 00:50:00 -0700202 * @j minijail to bind inside
203 * @src source to bind
204 * @dest location to bind (inside chroot)
205 * @type type of filesystem
206 * @flags flags passed to mount
207 *
Dylan Reid81e23972016-05-18 14:06:35 -0700208 * This may be called multiple times; all mounts will be applied in the order
Dylan Reid648b2202015-10-23 00:50:00 -0700209 * of minijail_mount() calls.
210 */
211int minijail_mount(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800212 const char *type, unsigned long flags);
Dylan Reid648b2202015-10-23 00:50:00 -0700213
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800214/*
215 * minijail_bind: bind-mounts @src into @j as @dest, optionally writeable
Elly Jones51a5b6c2011-10-12 19:09:26 -0400216 * @j minijail to bind inside
217 * @src source to bind
218 * @dest location to bind (inside chroot)
219 * @writeable 1 if the bind mount should be writeable
220 *
221 * This may be called multiple times; all bindings will be applied in the order
222 * of minijail_bind() calls.
223 */
224int minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700225 int writeable);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400226
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800227/*
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700228 * minijail_add_hook: adds @hook to the list of hooks that will be
229 * invoked when @event is reached during minijail setup. The caller is
230 * responsible for the lifetime of @payload.
231 * @j minijail to add the hook to
232 * @hook the function that will be invoked
233 * @payload an opaque pointer
234 * @event the event that will trigger the hook
235 */
236int minijail_add_hook(struct minijail *j,
237 minijail_hook_t hook, void *payload,
238 minijail_hook_event_t event);
239
240/*
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700241 * minijail_preserve_fd: preserves @parent_fd and makes it available as
242 * @child_fd in the child process. @parent_fd will be closed if no other
243 * redirect has claimed it as a @child_fd. This works even if
244 * minijail_close_open_fds() is invoked.
245 * @j minijail to add the fd to
246 * @parent_fd the fd in the parent process
247 * @child_fd the fd that will be available in the child process
248 */
249int minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd);
250
251/*
Jorge Lucangeli Obesdeca9f22016-07-01 09:49:39 -0400252 * Lock this process into the given minijail. Note that this procedure cannot
253 * fail, since there is no way to undo privilege-dropping; therefore, if any
254 * part of the privilege-drop fails, minijail_enter() will abort the entire
255 * process.
Elly Jonescd7a9042011-07-22 13:56:51 -0400256 *
257 * Some restrictions cannot be enabled this way (pid namespaces) and attempting
258 * to do so will cause an abort.
259 */
260void minijail_enter(const struct minijail *j);
261
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800262/*
263 * Run the specified command in the given minijail, execve(2)-style. This is
Elly Jonese1749eb2011-10-07 13:54:59 -0400264 * required if minijail_namespace_pids() was used.
265 */
266int minijail_run(struct minijail *j, const char *filename,
267 char *const argv[]);
Elly Jonescd7a9042011-07-22 13:56:51 -0400268
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800269/*
270 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700271 * Used with static binaries, or on systems without support for LD_PRELOAD.
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700272 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700273int minijail_run_no_preload(struct minijail *j, const char *filename,
274 char *const argv[]);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700275
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800276/*
277 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700278 * Update |*pchild_pid| with the pid of the child.
279 */
280int minijail_run_pid(struct minijail *j, const char *filename,
281 char *const argv[], pid_t *pchild_pid);
282
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800283/*
284 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700285 * Update |*pstdin_fd| with a fd that allows writing to the child's
286 * standard input.
287 */
288int minijail_run_pipe(struct minijail *j, const char *filename,
289 char *const argv[], int *pstdin_fd);
290
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800291/*
292 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700293 * Update |*pchild_pid| with the pid of the child.
294 * Update |*pstdin_fd| with a fd that allows writing to the child's
295 * standard input.
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800296 * Update |*pstdout_fd| with a fd that allows reading from the child's
297 * standard output.
298 * Update |*pstderr_fd| with a fd that allows reading from the child's
299 * standard error.
300 */
301int minijail_run_pid_pipes(struct minijail *j, const char *filename,
302 char *const argv[], pid_t *pchild_pid,
303 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd);
304
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800305/*
306 * Run the specified command in the given minijail, execve(2)-style.
Samuel Tan63187f42015-10-16 13:01:53 -0700307 * Update |*pchild_pid| with the pid of the child.
308 * Update |*pstdin_fd| with a fd that allows writing to the child's
309 * standard input.
310 * Update |*pstdout_fd| with a fd that allows reading from the child's
311 * standard output.
312 * Update |*pstderr_fd| with a fd that allows reading from the child's
313 * standard error.
314 * Used with static binaries, or on systems without support for LD_PRELOAD.
315 */
316int minijail_run_pid_pipes_no_preload(struct minijail *j, const char *filename,
317 char *const argv[], pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800318 int *pstdin_fd, int *pstdout_fd,
319 int *pstderr_fd);
Samuel Tan63187f42015-10-16 13:01:53 -0700320
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800321/*
322 * Kill the specified minijail. The minijail must have been created with pid
Elly Jonese1749eb2011-10-07 13:54:59 -0400323 * namespacing; if it was, all processes inside it are atomically killed.
324 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400325int minijail_kill(struct minijail *j);
326
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800327/*
Jorge Lucangeli Obesdeca9f22016-07-01 09:49:39 -0400328 * Wait for all processes in the specified minijail to exit. Returns the exit
Elly Jonese1749eb2011-10-07 13:54:59 -0400329 * status of the _first_ process spawned in the jail.
330 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400331int minijail_wait(struct minijail *j);
332
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800333/*
Jorge Lucangeli Obesdeca9f22016-07-01 09:49:39 -0400334 * Frees the given minijail. It does not matter if the process is inside the
335 * minijail or not.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800336 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400337void minijail_destroy(struct minijail *j);
338
339#ifdef __cplusplus
Elly Jonese1749eb2011-10-07 13:54:59 -0400340}; /* extern "C" */
Elly Jonescd7a9042011-07-22 13:56:51 -0400341#endif
342
Elly Jonese1749eb2011-10-07 13:54:59 -0400343#endif /* !_LIBMINIJAIL_H_ */