blob: bcfd995b4a90409bf180baea5efb94824838abfb [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>
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -080019#include <sys/resource.h>
Elly Jonescd7a9042011-07-22 13:56:51 -040020#include <sys/types.h>
21
Stephen Barber27c58232019-12-09 17:20:28 -080022/*
23 * Rust's bindgen needs the actual definition of sock_fprog in order to
24 * generate usable bindings.
25 */
26#ifdef USE_BINDGEN
27#include <linux/filter.h>
28#endif
29
Elly Jonescd7a9042011-07-22 13:56:51 -040030#ifdef __cplusplus
31extern "C" {
32#endif
33
François Degros08b10f72019-10-09 12:44:05 +110034/* Possible exit status codes returned by minijail_wait(). */
Elly Jonescd7a9042011-07-22 13:56:51 -040035enum {
François Degros08b10f72019-10-09 12:44:05 +110036 /* Command can be found but cannot be run */
37 MINIJAIL_ERR_NO_ACCESS = 126,
38
39 /* Command cannot be found */
40 MINIJAIL_ERR_NO_COMMAND = 127,
41
42 /* (MINIJAIL_ERR_SIG_BASE + n) if process killed by signal n != SIGSYS */
43 MINIJAIL_ERR_SIG_BASE = 128,
44
François Degrosa42182d2020-04-29 00:41:52 +100045 /* Cannot mount a file or folder in mount namespace */
46 MINIJAIL_ERR_MOUNT = 251,
47
Elly Jonese1749eb2011-10-07 13:54:59 -040048 MINIJAIL_ERR_PRELOAD = 252,
François Degros08b10f72019-10-09 12:44:05 +110049
50 /* Process killed by SIGSYS */
Elly Jonese1749eb2011-10-07 13:54:59 -040051 MINIJAIL_ERR_JAIL = 253,
François Degros08b10f72019-10-09 12:44:05 +110052
Elly Jonese1749eb2011-10-07 13:54:59 -040053 MINIJAIL_ERR_INIT = 254,
Elly Jonescd7a9042011-07-22 13:56:51 -040054};
55
56struct minijail;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -070057struct sock_fprog;
Elly Jonescd7a9042011-07-22 13:56:51 -040058
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -070059/*
60 * A hook that can be used to execute code at various events during minijail
61 * setup in the forked process. These can only be used if the jailed process is
62 * not going to be invoked with LD_PRELOAD.
63 *
64 * If the return value is non-zero, it will be interpreted as -errno and the
65 * process will abort.
66 */
67typedef int (*minijail_hook_t)(void *context);
68
69/*
70 * The events during minijail setup in which hooks can run. All the events are
71 * run in the new process.
72 */
73typedef enum {
74 /* The hook will run just before dropping capabilities. */
75 MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS,
76
77 /* The hook will run just before calling execve(2). */
78 MINIJAIL_HOOK_EVENT_PRE_EXECVE,
79
Jorge Lucangeli Obes06bba012019-01-25 11:12:30 -050080 /* The hook will run just before calling chroot(2) / pivot_root(2). */
Luis Hector Chavez64730af2017-09-13 13:18:59 -070081 MINIJAIL_HOOK_EVENT_PRE_CHROOT,
82
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -070083 /* Sentinel for error checking. Must be last. */
84 MINIJAIL_HOOK_EVENT_MAX,
85} minijail_hook_event_t;
86
Elly Jonescd7a9042011-07-22 13:56:51 -040087/* Allocates a new minijail with no restrictions. */
88struct minijail *minijail_new(void);
89
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080090/*
91 * These functions add restrictions to the minijail. They are not applied until
Elly Jonescd7a9042011-07-22 13:56:51 -040092 * minijail_enter() is called. See the documentation in minijail0.1 for
Elly Jonese1749eb2011-10-07 13:54:59 -040093 * explanations in detail of what the restrictions do.
94 */
Elly Jonescd7a9042011-07-22 13:56:51 -040095void minijail_change_uid(struct minijail *j, uid_t uid);
96void minijail_change_gid(struct minijail *j, gid_t gid);
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -080097/* Copies |list|. */
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -080098void minijail_set_supplementary_gids(struct minijail *j, size_t size,
99 const gid_t *list);
Lutz Justen13807cb2017-01-03 17:11:55 +0100100void minijail_keep_supplementary_gids(struct minijail *j);
Will Drewry2ddaad02011-09-16 11:36:08 -0500101/* Stores user to change to and copies |user| for internal consistency. */
Elly Jonescd7a9042011-07-22 13:56:51 -0400102int minijail_change_user(struct minijail *j, const char *user);
Will Drewry2ddaad02011-09-16 11:36:08 -0500103/* Does not take ownership of |group|. */
Elly Jonescd7a9042011-07-22 13:56:51 -0400104int minijail_change_group(struct minijail *j, const char *group);
105void minijail_use_seccomp(struct minijail *j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700106void minijail_no_new_privs(struct minijail *j);
Will Drewry32ac9f52011-08-18 21:36:27 -0500107void minijail_use_seccomp_filter(struct minijail *j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400108void minijail_set_seccomp_filter_tsync(struct minijail *j);
Anand K Mistry31adc6c2020-11-26 11:39:46 +1100109/*
110 * Allow speculative execution features that may cause data leaks across
111 * processes, by setting the SECCOMP_FILTER_FLAG_SPEC_ALLOW seccomp flag.
112 *
113 * WARNING: Enabling this may make the process vulnerable to speculative
114 * execution attacks (Branch Target Injection, and Speculative Store Bypass).
115 * This is only safe to use for processes that do not execute untrusted code.
116 */
117void minijail_set_seccomp_filter_allow_speculation(struct minijail *j);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700118/* Does not take ownership of |filter|. */
119void minijail_set_seccomp_filters(struct minijail *j,
120 const struct sock_fprog *filter);
Will Drewry32ac9f52011-08-18 21:36:27 -0500121void minijail_parse_seccomp_filters(struct minijail *j, const char *path);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400122void minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700123void minijail_log_seccomp_filter_failures(struct minijail *j);
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800124/* 'minijail_use_caps' and 'minijail_capbset_drop' are mutually exclusive. */
Elly Jonescd7a9042011-07-22 13:56:51 -0400125void minijail_use_caps(struct minijail *j, uint64_t capmask);
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800126void minijail_capbset_drop(struct minijail *j, uint64_t capmask);
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400127/* 'minijail_set_ambient_caps' requires 'minijail_use_caps'. */
128void minijail_set_ambient_caps(struct minijail *j);
Peter Qiu2860c462015-12-16 15:13:06 -0800129void minijail_reset_signal_mask(struct minijail *j);
Luis Hector Chaveza27118a2018-04-04 08:18:01 -0700130void minijail_reset_signal_handlers(struct minijail *j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400131void minijail_namespace_vfs(struct minijail *j);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700132void minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path);
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800133void minijail_new_session_keyring(struct minijail *j);
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700134void minijail_skip_setting_securebits(struct minijail *j,
135 uint64_t securebits_skip_mask);
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800136
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800137/*
138 * This option is *dangerous* as it negates most of the functionality of
139 * minijail_namespace_vfs(). You very likely don't need this.
140 */
141void minijail_skip_remount_private(struct minijail *j);
Mike Frysinger785b1c32018-02-23 15:47:24 -0500142void minijail_remount_mode(struct minijail *j, unsigned long mode);
Dylan Reidf7942472015-11-18 17:55:26 -0800143void minijail_namespace_ipc(struct minijail *j);
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400144void minijail_namespace_uts(struct minijail *j);
145int minijail_namespace_set_hostname(struct minijail *j, const char *name);
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400146void minijail_namespace_net(struct minijail *j);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700147void minijail_namespace_enter_net(struct minijail *j, const char *ns_path);
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700148void minijail_namespace_cgroups(struct minijail *j);
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700149/* Closes all open file descriptors after forking. */
150void minijail_close_open_fds(struct minijail *j);
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800151/*
152 * Implies namespace_vfs and remount_proc_readonly.
Elly Jones761b7412012-06-13 15:49:52 -0400153 * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>.
154 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400155void minijail_namespace_pids(struct minijail *j);
Jorge Lucangeli Obes2fa96d12019-02-05 10:51:57 -0500156/*
157 * Implies namespace_vfs.
158 * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>.
159 * Minijail will by default remount /proc read-only when using a PID namespace.
160 * Certain complex applications expect to be able to do their own sandboxing
161 * which might require writing to /proc, so support a weaker version of PID
162 * namespacing with a RW /proc.
163 */
164void minijail_namespace_pids_rw_proc(struct minijail *j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800165void minijail_namespace_user(struct minijail *j);
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400166void minijail_namespace_user_disable_setgroups(struct minijail *j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800167int minijail_uidmap(struct minijail *j, const char *uidmap);
168int minijail_gidmap(struct minijail *j, const char *gidmap);
Dylan Reid791f5772015-09-14 20:02:42 -0700169void minijail_remount_proc_readonly(struct minijail *j);
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800170void minijail_run_as_init(struct minijail *j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800171int minijail_write_pid_file(struct minijail *j, const char *path);
Elly Jonescd7a9042011-07-22 13:56:51 -0400172void minijail_inherit_usergroups(struct minijail *j);
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800173/*
174 * Changes the jailed process's syscall table to the alt_syscall table
Andrew Brestickereac28942015-11-11 16:04:46 -0800175 * named |table|.
176 */
177int minijail_use_alt_syscall(struct minijail *j, const char *table);
Elly Jonescd7a9042011-07-22 13:56:51 -0400178
Dylan Reid0f72ef42017-06-06 15:42:49 -0700179/* Sets the given runtime limit. See getrlimit(2). */
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800180int minijail_rlimit(struct minijail *j, int type, rlim_t cur, rlim_t max);
Dylan Reid0f72ef42017-06-06 15:42:49 -0700181
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800182/*
Dylan Reid605ce7f2016-01-19 19:21:00 -0800183 * Adds the jailed process to the cgroup given by |path|. |path| should be the
184 * full path to the cgroups "tasks" file.
185 * Example: /sys/fs/cgroup/cpu/jailed_procs/tasks adds to the "jailed_procs" cpu
186 * cgroup.
187 */
188int minijail_add_to_cgroup(struct minijail *j, const char *path);
189
190/*
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400191 * Install signal handlers in the minijail process that forward received
192 * signals to the jailed child process.
193 */
194int minijail_forward_signals(struct minijail *j);
195
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700196/* The jailed child process should call setsid() to create a new session. */
197int minijail_create_session(struct minijail *j);
198
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400199/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800200 * minijail_enter_chroot: enables chroot() restriction for @j
Elly Jones51a5b6c2011-10-12 19:09:26 -0400201 * @j minijail to apply restriction to
202 * @dir directory to chroot() to. Owned by caller.
203 *
204 * Enters @dir, binding all bind mounts specified with minijail_bind() into
205 * place. Requires @dir to contain all necessary directories for bind mounts
206 * (i.e., if you have requested a bind mount at /etc, /etc must exist in @dir.)
207 *
208 * Returns 0 on success.
209 */
210int minijail_enter_chroot(struct minijail *j, const char *dir);
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800211int minijail_enter_pivot_root(struct minijail *j, const char *dir);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400212
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800213/*
214 * minijail_get_original_path: returns the path of a given file outside of the
Dylan Reid08946cc2015-09-16 19:10:57 -0700215 * chroot.
216 * @j minijail to obtain the path from.
217 * @chroot_path path inside of the chroot() to.
218 *
219 * When executing a binary in a chroot or pivot_root, return path to the binary
220 * outside of the chroot.
221 *
222 * Returns a string containing the path. This must be freed by the caller.
223 */
224char *minijail_get_original_path(struct minijail *j, const char *chroot_path);
225
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800226/*
Martin Pelikánab9eb442017-01-25 11:53:58 +1100227 * minijail_mount_tmp: enables mounting of a 64M tmpfs filesystem on /tmp.
Lee Campbell11af0622014-05-22 12:36:04 -0700228 * As be rules of bind mounts, /tmp must exist in chroot.
229 */
230void minijail_mount_tmp(struct minijail *j);
231
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800232/*
Martin Pelikánab9eb442017-01-25 11:53:58 +1100233 * minijail_mount_tmp_size: enables mounting of a tmpfs filesystem on /tmp.
234 * As be rules of bind mounts, /tmp must exist in chroot. Size is in bytes.
235 */
236void minijail_mount_tmp_size(struct minijail *j, size_t size);
237
238/*
Mike Frysinger33ffef32017-01-13 19:53:19 -0500239 * minijail_mount_dev: enables mounting of a tmpfs filesystem on /dev.
240 * It will then be seeded with a basic set of device nodes. For the exact
241 * list, consult the minijail(0) man page.
242 */
243void minijail_mount_dev(struct minijail *j);
244
245/*
Dylan Reid81e23972016-05-18 14:06:35 -0700246 * minijail_mount_with_data: when entering minijail @j,
247 * mounts @src at @dst with @flags and @data.
248 * @j minijail to bind inside
249 * @src source to bind
250 * @dest location to bind (inside chroot)
251 * @type type of filesystem
252 * @flags flags passed to mount
253 * @data data arguments passed to mount(2), e.g. "mode=755"
254 *
255 * This may be called multiple times; all mounts will be applied in the order
256 * of minijail_mount() calls.
Mike Frysingercb8674d2018-08-12 00:53:35 -0400257 * If @flags is 0, then MS_NODEV | MS_NOEXEC | MS_NOSUID will be used instead.
Mike Frysingerb7803c82018-08-23 15:43:15 -0400258 * If @data is NULL or "", and @type is tmpfs, then "mode=0755,size=10M" will
259 * be used instead.
Dylan Reid81e23972016-05-18 14:06:35 -0700260 */
261int minijail_mount_with_data(struct minijail *j, const char *src,
262 const char *dest, const char *type,
263 unsigned long flags, const char *data);
264
265/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800266 * minijail_mount: when entering minijail @j, mounts @src at @dst with @flags
Dylan Reid648b2202015-10-23 00:50:00 -0700267 * @j minijail to bind inside
268 * @src source to bind
269 * @dest location to bind (inside chroot)
270 * @type type of filesystem
271 * @flags flags passed to mount
272 *
Dylan Reid81e23972016-05-18 14:06:35 -0700273 * This may be called multiple times; all mounts will be applied in the order
Dylan Reid648b2202015-10-23 00:50:00 -0700274 * of minijail_mount() calls.
275 */
276int minijail_mount(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800277 const char *type, unsigned long flags);
Dylan Reid648b2202015-10-23 00:50:00 -0700278
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800279/*
280 * minijail_bind: bind-mounts @src into @j as @dest, optionally writeable
Elly Jones51a5b6c2011-10-12 19:09:26 -0400281 * @j minijail to bind inside
282 * @src source to bind
283 * @dest location to bind (inside chroot)
284 * @writeable 1 if the bind mount should be writeable
285 *
286 * This may be called multiple times; all bindings will be applied in the order
287 * of minijail_bind() calls.
288 */
289int minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700290 int writeable);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400291
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800292/*
Nicole Anderson-Au835f7172021-01-13 21:18:13 +0000293 * minijail_add_remount: when entering minijail @j, remounts @mount_name and all
294 * subdirectories as @remount_mode rather than the default MS_PRIVATE
295 * @j minijail to bind inside
296 * @mount_name mount to remount
297 * @remount_mode remount mode to use
298 *
299 * This may be called multiple times; this overrides |j->remount_mode| for the
300 * given mount.
301 */
302int minijail_add_remount(struct minijail *j, const char *mount_name,
303 unsigned long remount_mode);
304/*
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700305 * minijail_add_hook: adds @hook to the list of hooks that will be
306 * invoked when @event is reached during minijail setup. The caller is
307 * responsible for the lifetime of @payload.
308 * @j minijail to add the hook to
309 * @hook the function that will be invoked
310 * @payload an opaque pointer
311 * @event the event that will trigger the hook
312 */
313int minijail_add_hook(struct minijail *j,
314 minijail_hook_t hook, void *payload,
315 minijail_hook_event_t event);
316
317/*
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700318 * minijail_preserve_fd: preserves @parent_fd and makes it available as
319 * @child_fd in the child process. @parent_fd will be closed if no other
320 * redirect has claimed it as a @child_fd. This works even if
321 * minijail_close_open_fds() is invoked.
322 * @j minijail to add the fd to
323 * @parent_fd the fd in the parent process
324 * @child_fd the fd that will be available in the child process
325 */
326int minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd);
327
328/*
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700329 * minijail_set_preload_path: overrides the default path for
330 * libminijailpreload.so.
331 */
332int minijail_set_preload_path(struct minijail *j, const char *preload_path);
333
334/*
Jorge Lucangeli Obesdeca9f22016-07-01 09:49:39 -0400335 * Lock this process into the given minijail. Note that this procedure cannot
336 * fail, since there is no way to undo privilege-dropping; therefore, if any
337 * part of the privilege-drop fails, minijail_enter() will abort the entire
338 * process.
Elly Jonescd7a9042011-07-22 13:56:51 -0400339 *
340 * Some restrictions cannot be enabled this way (pid namespaces) and attempting
341 * to do so will cause an abort.
342 */
343void minijail_enter(const struct minijail *j);
344
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800345/*
Dylan Reid0412dcc2017-08-24 11:33:15 -0700346 * Run the specified command in the given minijail, execve(2)-style.
347 * If minijail_namespace_pids() or minijail_namespace_user() are used,
348 * this or minijail_fork() is required instead of minijail_enter().
Elly Jonese1749eb2011-10-07 13:54:59 -0400349 */
350int minijail_run(struct minijail *j, const char *filename,
351 char *const argv[]);
Elly Jonescd7a9042011-07-22 13:56:51 -0400352
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800353/*
354 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -0500355 * Don't use LD_PRELOAD to do privilege dropping. This is useful when sandboxing
356 * static binaries, or on systems without support for LD_PRELOAD.
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700357 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700358int minijail_run_no_preload(struct minijail *j, const char *filename,
359 char *const argv[]);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700360
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800361/*
362 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700363 * Update |*pchild_pid| with the pid of the child.
364 */
365int minijail_run_pid(struct minijail *j, const char *filename,
366 char *const argv[], pid_t *pchild_pid);
367
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800368/*
369 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700370 * Update |*pstdin_fd| with a fd that allows writing to the child's
371 * standard input.
372 */
373int minijail_run_pipe(struct minijail *j, const char *filename,
374 char *const argv[], int *pstdin_fd);
375
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800376/*
377 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700378 * Update |*pchild_pid| with the pid of the child.
379 * Update |*pstdin_fd| with a fd that allows writing to the child's
380 * standard input.
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800381 * Update |*pstdout_fd| with a fd that allows reading from the child's
382 * standard output.
383 * Update |*pstderr_fd| with a fd that allows reading from the child's
384 * standard error.
385 */
386int minijail_run_pid_pipes(struct minijail *j, const char *filename,
387 char *const argv[], pid_t *pchild_pid,
388 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd);
389
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800390/*
391 * Run the specified command in the given minijail, execve(2)-style.
Mattias Nisslerb35f2c12020-02-07 13:37:36 +0100392 * Pass |envp| as the full environment for the child.
393 * Update |*pchild_pid| with the pid of the child.
394 * Update |*pstdin_fd| with a fd that allows writing to the child's
395 * standard input.
396 * Update |*pstdout_fd| with a fd that allows reading from the child's
397 * standard output.
398 * Update |*pstderr_fd| with a fd that allows reading from the child's
399 * standard error.
400 */
401int minijail_run_env_pid_pipes(struct minijail *j, const char *filename,
402 char *const argv[], char *const envp[],
403 pid_t *pchild_pid, int *pstdin_fd,
404 int *pstdout_fd, int *pstderr_fd);
405
406/*
Allen Webb05af7762021-07-16 12:56:44 -0500407 * Execute the specified file descriptor in the given minijail,
408 * fexecve(3)-style.
409 * Pass |envp| as the full environment for the child or NULL to inherit.
410 * Update |*pchild_pid| with the pid of the child.
411 * Update |*pstdin_fd| with a fd that allows writing to the child's
412 * standard input.
413 * Update |*pstdout_fd| with a fd that allows reading from the child's
414 * standard output.
415 * Update |*pstderr_fd| with a fd that allows reading from the child's
416 * standard error.
417 */
418int minijail_run_fd_env_pid_pipes(struct minijail *j, int elf_fd,
419 char *const argv[], char *const envp[],
420 pid_t *pchild_pid, int *pstdin_fd,
421 int *pstdout_fd, int *pstderr_fd);
422
423/*
Mattias Nisslerb35f2c12020-02-07 13:37:36 +0100424 * Run the specified command in the given minijail, execve(2)-style.
Samuel Tan63187f42015-10-16 13:01:53 -0700425 * Update |*pchild_pid| with the pid of the child.
426 * Update |*pstdin_fd| with a fd that allows writing to the child's
427 * standard input.
428 * Update |*pstdout_fd| with a fd that allows reading from the child's
429 * standard output.
430 * Update |*pstderr_fd| with a fd that allows reading from the child's
431 * standard error.
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -0500432 * Don't use LD_PRELOAD to do privilege dropping. This is useful when sandboxing
433 * static binaries, or on systems without support for LD_PRELOAD.
Samuel Tan63187f42015-10-16 13:01:53 -0700434 */
435int minijail_run_pid_pipes_no_preload(struct minijail *j, const char *filename,
436 char *const argv[], pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800437 int *pstdin_fd, int *pstdout_fd,
438 int *pstderr_fd);
Samuel Tan63187f42015-10-16 13:01:53 -0700439
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800440/*
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -0500441 * Run the specified command in the given minijail, execve(2)-style.
442 * Pass |envp| as the full environment for the child.
443 * Update |*pchild_pid| with the pid of the child.
444 * Update |*pstdin_fd| with a fd that allows writing to the child's
445 * standard input.
446 * Update |*pstdout_fd| with a fd that allows reading from the child's
447 * standard output.
448 * Update |*pstderr_fd| with a fd that allows reading from the child's
449 * standard error.
450 * Don't use LD_PRELOAD to do privilege dropping. This is useful when sandboxing
451 * static binaries, or on systems without support for LD_PRELOAD.
452 */
453int minijail_run_env_pid_pipes_no_preload(struct minijail *j,
454 const char *filename,
455 char *const argv[],
456 char *const envp[], pid_t *pchild_pid,
457 int *pstdin_fd, int *pstdout_fd,
458 int *pstderr_fd);
459
460/*
Dylan Reid0412dcc2017-08-24 11:33:15 -0700461 * Fork, jail the child, and return. This behaves similar to fork(2), except it
462 * puts the child process in a jail before returning.
463 * `minijail_fork` returns in both the parent and the child. The pid of the
464 * child is returned to the parent. Zero is returned in the child. LD_PRELOAD
465 * is not supported.
466 * If minijail_namespace_pids() or minijail_namespace_user() are used,
467 * this or minijail_run*() is required instead of minijail_enter().
468 */
469pid_t minijail_fork(struct minijail *j);
470
471/*
François Degros47e63352019-10-01 13:01:53 +1000472 * Send SIGTERM to the process in the minijail and wait for it to terminate.
473 *
474 * Return the same nonnegative exit status as minijail_wait(), or a negative
475 * error code (eg -ESRCH if the process has already been waited for).
476 *
477 * This is most useful if the minijail has been created with PID namespacing
478 * since, in this case, all processes inside it are atomically killed.
Elly Jonese1749eb2011-10-07 13:54:59 -0400479 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400480int minijail_kill(struct minijail *j);
481
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800482/*
François Degros08b10f72019-10-09 12:44:05 +1100483 * Wait for the first process spawned in the specified minijail to exit, and
François Degros47e63352019-10-01 13:01:53 +1000484 * return its exit status. A process can only be waited once.
François Degros08b10f72019-10-09 12:44:05 +1100485 *
486 * Return:
François Degros47e63352019-10-01 13:01:53 +1000487 * A negative error code if the process cannot be waited for (eg -ECHILD if no
488 * process has been started or if the process has already been waited for).
François Degros08b10f72019-10-09 12:44:05 +1100489 * MINIJAIL_ERR_NO_COMMAND if command cannot be found.
490 * MINIJAIL_ERR_NO_ACCESS if command cannot be run.
491 * MINIJAIL_ERR_JAIL if process was killed by SIGSYS.
492 * (MINIJAIL_ERR_SIG_BASE + n) if process was killed by signal n != SIGSYS.
493 * (n & 0xFF) if process finished by returning code n.
Elly Jonese1749eb2011-10-07 13:54:59 -0400494 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400495int minijail_wait(struct minijail *j);
496
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800497/*
Jorge Lucangeli Obesdeca9f22016-07-01 09:49:39 -0400498 * Frees the given minijail. It does not matter if the process is inside the
499 * minijail or not.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800500 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400501void minijail_destroy(struct minijail *j);
502
Luis Hector Chavez114a9302017-09-05 20:36:58 -0700503/*
Dylan Reid6dc224f2021-05-12 17:06:25 -0700504 * Deep copies the minijail in |from| to |out| providing two identical jails
505 * that can be used to contain separate children created with minijail_fork().
506 *
507 * Duplicating a jail is invalid after a jail has been passed to
508 * minijail_fork(). Many minijail_*() calls will yield undefined
509 * results when called on a jail duplicated post-fork.
510 */
511int minijail_copy_jail(const struct minijail *from, struct minijail *out);
512
513/*
Luis Hector Chavez114a9302017-09-05 20:36:58 -0700514 * minijail_log_to_fd: redirects the module-wide logging to an FD instead of
515 * syslog.
516 * @fd FD to log to. Caller must ensure this is available after
517 * jailing (e.g. with minijail_preserve_fd()).
518 * @min_priority the minimum logging priority. Same as the priority argument
519 * to syslog(2).
520 */
521void minijail_log_to_fd(int fd, int min_priority);
522
Elly Jonescd7a9042011-07-22 13:56:51 -0400523#ifdef __cplusplus
Christopher Di Bellac942ed02020-10-16 23:11:35 +0000524} /* extern "C" */
Elly Jonescd7a9042011-07-22 13:56:51 -0400525#endif
526
Elly Jonese1749eb2011-10-07 13:54:59 -0400527#endif /* !_LIBMINIJAIL_H_ */