blob: 9eefe0ab53d38697ff32d619b5eea137223d982e [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
Elly Jonese1749eb2011-10-07 13:54:59 -040045 MINIJAIL_ERR_PRELOAD = 252,
François Degros08b10f72019-10-09 12:44:05 +110046
47 /* Process killed by SIGSYS */
Elly Jonese1749eb2011-10-07 13:54:59 -040048 MINIJAIL_ERR_JAIL = 253,
François Degros08b10f72019-10-09 12:44:05 +110049
Elly Jonese1749eb2011-10-07 13:54:59 -040050 MINIJAIL_ERR_INIT = 254,
Elly Jonescd7a9042011-07-22 13:56:51 -040051};
52
53struct minijail;
Luis Hector Chavezc3e17722018-10-16 20:43:12 -070054struct sock_fprog;
Elly Jonescd7a9042011-07-22 13:56:51 -040055
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -070056/*
57 * A hook that can be used to execute code at various events during minijail
58 * setup in the forked process. These can only be used if the jailed process is
59 * not going to be invoked with LD_PRELOAD.
60 *
61 * If the return value is non-zero, it will be interpreted as -errno and the
62 * process will abort.
63 */
64typedef int (*minijail_hook_t)(void *context);
65
66/*
67 * The events during minijail setup in which hooks can run. All the events are
68 * run in the new process.
69 */
70typedef enum {
71 /* The hook will run just before dropping capabilities. */
72 MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS,
73
74 /* The hook will run just before calling execve(2). */
75 MINIJAIL_HOOK_EVENT_PRE_EXECVE,
76
Jorge Lucangeli Obes06bba012019-01-25 11:12:30 -050077 /* The hook will run just before calling chroot(2) / pivot_root(2). */
Luis Hector Chavez64730af2017-09-13 13:18:59 -070078 MINIJAIL_HOOK_EVENT_PRE_CHROOT,
79
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -070080 /* Sentinel for error checking. Must be last. */
81 MINIJAIL_HOOK_EVENT_MAX,
82} minijail_hook_event_t;
83
Elly Jonescd7a9042011-07-22 13:56:51 -040084/* Allocates a new minijail with no restrictions. */
85struct minijail *minijail_new(void);
86
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080087/*
88 * These functions add restrictions to the minijail. They are not applied until
Elly Jonescd7a9042011-07-22 13:56:51 -040089 * minijail_enter() is called. See the documentation in minijail0.1 for
Elly Jonese1749eb2011-10-07 13:54:59 -040090 * explanations in detail of what the restrictions do.
91 */
Elly Jonescd7a9042011-07-22 13:56:51 -040092void minijail_change_uid(struct minijail *j, uid_t uid);
93void minijail_change_gid(struct minijail *j, gid_t gid);
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -080094/* Copies |list|. */
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -080095void minijail_set_supplementary_gids(struct minijail *j, size_t size,
96 const gid_t *list);
Lutz Justen13807cb2017-01-03 17:11:55 +010097void minijail_keep_supplementary_gids(struct minijail *j);
Will Drewry2ddaad02011-09-16 11:36:08 -050098/* Stores user to change to and copies |user| for internal consistency. */
Elly Jonescd7a9042011-07-22 13:56:51 -040099int minijail_change_user(struct minijail *j, const char *user);
Will Drewry2ddaad02011-09-16 11:36:08 -0500100/* Does not take ownership of |group|. */
Elly Jonescd7a9042011-07-22 13:56:51 -0400101int minijail_change_group(struct minijail *j, const char *group);
102void minijail_use_seccomp(struct minijail *j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -0700103void minijail_no_new_privs(struct minijail *j);
Will Drewry32ac9f52011-08-18 21:36:27 -0500104void minijail_use_seccomp_filter(struct minijail *j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -0400105void minijail_set_seccomp_filter_tsync(struct minijail *j);
Luis Hector Chavezc3e17722018-10-16 20:43:12 -0700106/* Does not take ownership of |filter|. */
107void minijail_set_seccomp_filters(struct minijail *j,
108 const struct sock_fprog *filter);
Will Drewry32ac9f52011-08-18 21:36:27 -0500109void minijail_parse_seccomp_filters(struct minijail *j, const char *path);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -0400110void minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -0700111void minijail_log_seccomp_filter_failures(struct minijail *j);
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800112/* 'minijail_use_caps' and 'minijail_capbset_drop' are mutually exclusive. */
Elly Jonescd7a9042011-07-22 13:56:51 -0400113void minijail_use_caps(struct minijail *j, uint64_t capmask);
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -0800114void minijail_capbset_drop(struct minijail *j, uint64_t capmask);
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400115/* 'minijail_set_ambient_caps' requires 'minijail_use_caps'. */
116void minijail_set_ambient_caps(struct minijail *j);
Peter Qiu2860c462015-12-16 15:13:06 -0800117void minijail_reset_signal_mask(struct minijail *j);
Luis Hector Chaveza27118a2018-04-04 08:18:01 -0700118void minijail_reset_signal_handlers(struct minijail *j);
Elly Jonescd7a9042011-07-22 13:56:51 -0400119void minijail_namespace_vfs(struct minijail *j);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -0700120void minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path);
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800121void minijail_new_session_keyring(struct minijail *j);
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700122void minijail_skip_setting_securebits(struct minijail *j,
123 uint64_t securebits_skip_mask);
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -0800124
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -0800125/*
126 * This option is *dangerous* as it negates most of the functionality of
127 * minijail_namespace_vfs(). You very likely don't need this.
128 */
129void minijail_skip_remount_private(struct minijail *j);
Mike Frysinger785b1c32018-02-23 15:47:24 -0500130void minijail_remount_mode(struct minijail *j, unsigned long mode);
Dylan Reidf7942472015-11-18 17:55:26 -0800131void minijail_namespace_ipc(struct minijail *j);
Mike Frysingerb9a7b162017-05-30 15:25:49 -0400132void minijail_namespace_uts(struct minijail *j);
133int minijail_namespace_set_hostname(struct minijail *j, const char *name);
Elly Fong-Jones6c086302013-03-20 17:15:28 -0400134void minijail_namespace_net(struct minijail *j);
Dylan Reid1102f5a2015-09-15 11:52:20 -0700135void minijail_namespace_enter_net(struct minijail *j, const char *ns_path);
Dylan Reid4cbc2a52016-06-17 19:06:07 -0700136void minijail_namespace_cgroups(struct minijail *j);
Luis Hector Chavez43ff0802016-10-07 12:21:07 -0700137/* Closes all open file descriptors after forking. */
138void minijail_close_open_fds(struct minijail *j);
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800139/*
140 * Implies namespace_vfs and remount_proc_readonly.
Elly Jones761b7412012-06-13 15:49:52 -0400141 * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>.
142 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400143void minijail_namespace_pids(struct minijail *j);
Jorge Lucangeli Obes2fa96d12019-02-05 10:51:57 -0500144/*
145 * Implies namespace_vfs.
146 * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>.
147 * Minijail will by default remount /proc read-only when using a PID namespace.
148 * Certain complex applications expect to be able to do their own sandboxing
149 * which might require writing to /proc, so support a weaker version of PID
150 * namespacing with a RW /proc.
151 */
152void minijail_namespace_pids_rw_proc(struct minijail *j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800153void minijail_namespace_user(struct minijail *j);
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -0400154void minijail_namespace_user_disable_setgroups(struct minijail *j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +0800155int minijail_uidmap(struct minijail *j, const char *uidmap);
156int minijail_gidmap(struct minijail *j, const char *gidmap);
Dylan Reid791f5772015-09-14 20:02:42 -0700157void minijail_remount_proc_readonly(struct minijail *j);
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +0800158void minijail_run_as_init(struct minijail *j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +0800159int minijail_write_pid_file(struct minijail *j, const char *path);
Elly Jonescd7a9042011-07-22 13:56:51 -0400160void minijail_inherit_usergroups(struct minijail *j);
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800161/*
162 * Changes the jailed process's syscall table to the alt_syscall table
Andrew Brestickereac28942015-11-11 16:04:46 -0800163 * named |table|.
164 */
165int minijail_use_alt_syscall(struct minijail *j, const char *table);
Elly Jonescd7a9042011-07-22 13:56:51 -0400166
Dylan Reid0f72ef42017-06-06 15:42:49 -0700167/* Sets the given runtime limit. See getrlimit(2). */
Luis Hector Chavez7058a2d2018-01-29 08:41:34 -0800168int minijail_rlimit(struct minijail *j, int type, rlim_t cur, rlim_t max);
Dylan Reid0f72ef42017-06-06 15:42:49 -0700169
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800170/*
Dylan Reid605ce7f2016-01-19 19:21:00 -0800171 * Adds the jailed process to the cgroup given by |path|. |path| should be the
172 * full path to the cgroups "tasks" file.
173 * Example: /sys/fs/cgroup/cpu/jailed_procs/tasks adds to the "jailed_procs" cpu
174 * cgroup.
175 */
176int minijail_add_to_cgroup(struct minijail *j, const char *path);
177
178/*
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400179 * Install signal handlers in the minijail process that forward received
180 * signals to the jailed child process.
181 */
182int minijail_forward_signals(struct minijail *j);
183
Xiyuan Xia9b41e652019-05-23 11:03:04 -0700184/* The jailed child process should call setsid() to create a new session. */
185int minijail_create_session(struct minijail *j);
186
Jorge Lucangeli Obesdba62092017-05-18 17:10:23 -0400187/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800188 * minijail_enter_chroot: enables chroot() restriction for @j
Elly Jones51a5b6c2011-10-12 19:09:26 -0400189 * @j minijail to apply restriction to
190 * @dir directory to chroot() to. Owned by caller.
191 *
192 * Enters @dir, binding all bind mounts specified with minijail_bind() into
193 * place. Requires @dir to contain all necessary directories for bind mounts
194 * (i.e., if you have requested a bind mount at /etc, /etc must exist in @dir.)
195 *
196 * Returns 0 on success.
197 */
198int minijail_enter_chroot(struct minijail *j, const char *dir);
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800199int minijail_enter_pivot_root(struct minijail *j, const char *dir);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400200
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800201/*
202 * minijail_get_original_path: returns the path of a given file outside of the
Dylan Reid08946cc2015-09-16 19:10:57 -0700203 * chroot.
204 * @j minijail to obtain the path from.
205 * @chroot_path path inside of the chroot() to.
206 *
207 * When executing a binary in a chroot or pivot_root, return path to the binary
208 * outside of the chroot.
209 *
210 * Returns a string containing the path. This must be freed by the caller.
211 */
212char *minijail_get_original_path(struct minijail *j, const char *chroot_path);
213
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800214/*
Martin Pelikánab9eb442017-01-25 11:53:58 +1100215 * minijail_mount_tmp: enables mounting of a 64M tmpfs filesystem on /tmp.
Lee Campbell11af0622014-05-22 12:36:04 -0700216 * As be rules of bind mounts, /tmp must exist in chroot.
217 */
218void minijail_mount_tmp(struct minijail *j);
219
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800220/*
Martin Pelikánab9eb442017-01-25 11:53:58 +1100221 * minijail_mount_tmp_size: enables mounting of a tmpfs filesystem on /tmp.
222 * As be rules of bind mounts, /tmp must exist in chroot. Size is in bytes.
223 */
224void minijail_mount_tmp_size(struct minijail *j, size_t size);
225
226/*
Mike Frysinger33ffef32017-01-13 19:53:19 -0500227 * minijail_mount_dev: enables mounting of a tmpfs filesystem on /dev.
228 * It will then be seeded with a basic set of device nodes. For the exact
229 * list, consult the minijail(0) man page.
230 */
231void minijail_mount_dev(struct minijail *j);
232
233/*
Dylan Reid81e23972016-05-18 14:06:35 -0700234 * minijail_mount_with_data: when entering minijail @j,
235 * mounts @src at @dst with @flags and @data.
236 * @j minijail to bind inside
237 * @src source to bind
238 * @dest location to bind (inside chroot)
239 * @type type of filesystem
240 * @flags flags passed to mount
241 * @data data arguments passed to mount(2), e.g. "mode=755"
242 *
243 * This may be called multiple times; all mounts will be applied in the order
244 * of minijail_mount() calls.
Mike Frysingercb8674d2018-08-12 00:53:35 -0400245 * If @flags is 0, then MS_NODEV | MS_NOEXEC | MS_NOSUID will be used instead.
Mike Frysingerb7803c82018-08-23 15:43:15 -0400246 * If @data is NULL or "", and @type is tmpfs, then "mode=0755,size=10M" will
247 * be used instead.
Dylan Reid81e23972016-05-18 14:06:35 -0700248 */
249int minijail_mount_with_data(struct minijail *j, const char *src,
250 const char *dest, const char *type,
251 unsigned long flags, const char *data);
252
253/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800254 * minijail_mount: when entering minijail @j, mounts @src at @dst with @flags
Dylan Reid648b2202015-10-23 00:50:00 -0700255 * @j minijail to bind inside
256 * @src source to bind
257 * @dest location to bind (inside chroot)
258 * @type type of filesystem
259 * @flags flags passed to mount
260 *
Dylan Reid81e23972016-05-18 14:06:35 -0700261 * This may be called multiple times; all mounts will be applied in the order
Dylan Reid648b2202015-10-23 00:50:00 -0700262 * of minijail_mount() calls.
263 */
264int minijail_mount(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800265 const char *type, unsigned long flags);
Dylan Reid648b2202015-10-23 00:50:00 -0700266
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800267/*
268 * minijail_bind: bind-mounts @src into @j as @dest, optionally writeable
Elly Jones51a5b6c2011-10-12 19:09:26 -0400269 * @j minijail to bind inside
270 * @src source to bind
271 * @dest location to bind (inside chroot)
272 * @writeable 1 if the bind mount should be writeable
273 *
274 * This may be called multiple times; all bindings will be applied in the order
275 * of minijail_bind() calls.
276 */
277int minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700278 int writeable);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400279
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800280/*
Luis Hector Chaveze0ba4ce2017-07-20 15:12:22 -0700281 * minijail_add_hook: adds @hook to the list of hooks that will be
282 * invoked when @event is reached during minijail setup. The caller is
283 * responsible for the lifetime of @payload.
284 * @j minijail to add the hook to
285 * @hook the function that will be invoked
286 * @payload an opaque pointer
287 * @event the event that will trigger the hook
288 */
289int minijail_add_hook(struct minijail *j,
290 minijail_hook_t hook, void *payload,
291 minijail_hook_event_t event);
292
293/*
Luis Hector Chavez1617f632017-08-01 18:32:30 -0700294 * minijail_preserve_fd: preserves @parent_fd and makes it available as
295 * @child_fd in the child process. @parent_fd will be closed if no other
296 * redirect has claimed it as a @child_fd. This works even if
297 * minijail_close_open_fds() is invoked.
298 * @j minijail to add the fd to
299 * @parent_fd the fd in the parent process
300 * @child_fd the fd that will be available in the child process
301 */
302int minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd);
303
304/*
Luis Hector Chavez9acba452018-10-11 10:13:25 -0700305 * minijail_set_preload_path: overrides the default path for
306 * libminijailpreload.so.
307 */
308int minijail_set_preload_path(struct minijail *j, const char *preload_path);
309
310/*
Jorge Lucangeli Obesdeca9f22016-07-01 09:49:39 -0400311 * Lock this process into the given minijail. Note that this procedure cannot
312 * fail, since there is no way to undo privilege-dropping; therefore, if any
313 * part of the privilege-drop fails, minijail_enter() will abort the entire
314 * process.
Elly Jonescd7a9042011-07-22 13:56:51 -0400315 *
316 * Some restrictions cannot be enabled this way (pid namespaces) and attempting
317 * to do so will cause an abort.
318 */
319void minijail_enter(const struct minijail *j);
320
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800321/*
Dylan Reid0412dcc2017-08-24 11:33:15 -0700322 * Run the specified command in the given minijail, execve(2)-style.
323 * If minijail_namespace_pids() or minijail_namespace_user() are used,
324 * this or minijail_fork() is required instead of minijail_enter().
Elly Jonese1749eb2011-10-07 13:54:59 -0400325 */
326int minijail_run(struct minijail *j, const char *filename,
327 char *const argv[]);
Elly Jonescd7a9042011-07-22 13:56:51 -0400328
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800329/*
330 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -0500331 * Don't use LD_PRELOAD to do privilege dropping. This is useful when sandboxing
332 * static binaries, or on systems without support for LD_PRELOAD.
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700333 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700334int minijail_run_no_preload(struct minijail *j, const char *filename,
335 char *const argv[]);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700336
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800337/*
338 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700339 * Update |*pchild_pid| with the pid of the child.
340 */
341int minijail_run_pid(struct minijail *j, const char *filename,
342 char *const argv[], pid_t *pchild_pid);
343
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800344/*
345 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700346 * Update |*pstdin_fd| with a fd that allows writing to the child's
347 * standard input.
348 */
349int minijail_run_pipe(struct minijail *j, const char *filename,
350 char *const argv[], int *pstdin_fd);
351
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800352/*
353 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700354 * Update |*pchild_pid| with the pid of the child.
355 * Update |*pstdin_fd| with a fd that allows writing to the child's
356 * standard input.
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800357 * Update |*pstdout_fd| with a fd that allows reading from the child's
358 * standard output.
359 * Update |*pstderr_fd| with a fd that allows reading from the child's
360 * standard error.
361 */
362int minijail_run_pid_pipes(struct minijail *j, const char *filename,
363 char *const argv[], pid_t *pchild_pid,
364 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd);
365
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800366/*
367 * Run the specified command in the given minijail, execve(2)-style.
Mattias Nisslerb35f2c12020-02-07 13:37:36 +0100368 * Pass |envp| as the full environment for the child.
369 * Update |*pchild_pid| with the pid of the child.
370 * Update |*pstdin_fd| with a fd that allows writing to the child's
371 * standard input.
372 * Update |*pstdout_fd| with a fd that allows reading from the child's
373 * standard output.
374 * Update |*pstderr_fd| with a fd that allows reading from the child's
375 * standard error.
376 */
377int minijail_run_env_pid_pipes(struct minijail *j, const char *filename,
378 char *const argv[], char *const envp[],
379 pid_t *pchild_pid, int *pstdin_fd,
380 int *pstdout_fd, int *pstderr_fd);
381
382/*
383 * Run the specified command in the given minijail, execve(2)-style.
Samuel Tan63187f42015-10-16 13:01:53 -0700384 * Update |*pchild_pid| with the pid of the child.
385 * Update |*pstdin_fd| with a fd that allows writing to the child's
386 * standard input.
387 * Update |*pstdout_fd| with a fd that allows reading from the child's
388 * standard output.
389 * Update |*pstderr_fd| with a fd that allows reading from the child's
390 * standard error.
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -0500391 * Don't use LD_PRELOAD to do privilege dropping. This is useful when sandboxing
392 * static binaries, or on systems without support for LD_PRELOAD.
Samuel Tan63187f42015-10-16 13:01:53 -0700393 */
394int minijail_run_pid_pipes_no_preload(struct minijail *j, const char *filename,
395 char *const argv[], pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800396 int *pstdin_fd, int *pstdout_fd,
397 int *pstderr_fd);
Samuel Tan63187f42015-10-16 13:01:53 -0700398
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800399/*
Jorge Lucangeli Obesd2c951d2019-02-01 15:43:36 -0500400 * Run the specified command in the given minijail, execve(2)-style.
401 * Pass |envp| as the full environment for the child.
402 * Update |*pchild_pid| with the pid of the child.
403 * Update |*pstdin_fd| with a fd that allows writing to the child's
404 * standard input.
405 * Update |*pstdout_fd| with a fd that allows reading from the child's
406 * standard output.
407 * Update |*pstderr_fd| with a fd that allows reading from the child's
408 * standard error.
409 * Don't use LD_PRELOAD to do privilege dropping. This is useful when sandboxing
410 * static binaries, or on systems without support for LD_PRELOAD.
411 */
412int minijail_run_env_pid_pipes_no_preload(struct minijail *j,
413 const char *filename,
414 char *const argv[],
415 char *const envp[], pid_t *pchild_pid,
416 int *pstdin_fd, int *pstdout_fd,
417 int *pstderr_fd);
418
419/*
Dylan Reid0412dcc2017-08-24 11:33:15 -0700420 * Fork, jail the child, and return. This behaves similar to fork(2), except it
421 * puts the child process in a jail before returning.
422 * `minijail_fork` returns in both the parent and the child. The pid of the
423 * child is returned to the parent. Zero is returned in the child. LD_PRELOAD
424 * is not supported.
425 * If minijail_namespace_pids() or minijail_namespace_user() are used,
426 * this or minijail_run*() is required instead of minijail_enter().
427 */
428pid_t minijail_fork(struct minijail *j);
429
430/*
François Degros47e63352019-10-01 13:01:53 +1000431 * Send SIGTERM to the process in the minijail and wait for it to terminate.
432 *
433 * Return the same nonnegative exit status as minijail_wait(), or a negative
434 * error code (eg -ESRCH if the process has already been waited for).
435 *
436 * This is most useful if the minijail has been created with PID namespacing
437 * since, in this case, all processes inside it are atomically killed.
Elly Jonese1749eb2011-10-07 13:54:59 -0400438 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400439int minijail_kill(struct minijail *j);
440
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800441/*
François Degros08b10f72019-10-09 12:44:05 +1100442 * Wait for the first process spawned in the specified minijail to exit, and
François Degros47e63352019-10-01 13:01:53 +1000443 * return its exit status. A process can only be waited once.
François Degros08b10f72019-10-09 12:44:05 +1100444 *
445 * Return:
François Degros47e63352019-10-01 13:01:53 +1000446 * A negative error code if the process cannot be waited for (eg -ECHILD if no
447 * process has been started or if the process has already been waited for).
François Degros08b10f72019-10-09 12:44:05 +1100448 * MINIJAIL_ERR_NO_COMMAND if command cannot be found.
449 * MINIJAIL_ERR_NO_ACCESS if command cannot be run.
450 * MINIJAIL_ERR_JAIL if process was killed by SIGSYS.
451 * (MINIJAIL_ERR_SIG_BASE + n) if process was killed by signal n != SIGSYS.
452 * (n & 0xFF) if process finished by returning code n.
Elly Jonese1749eb2011-10-07 13:54:59 -0400453 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400454int minijail_wait(struct minijail *j);
455
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800456/*
Jorge Lucangeli Obesdeca9f22016-07-01 09:49:39 -0400457 * Frees the given minijail. It does not matter if the process is inside the
458 * minijail or not.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800459 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400460void minijail_destroy(struct minijail *j);
461
Luis Hector Chavez114a9302017-09-05 20:36:58 -0700462/*
463 * minijail_log_to_fd: redirects the module-wide logging to an FD instead of
464 * syslog.
465 * @fd FD to log to. Caller must ensure this is available after
466 * jailing (e.g. with minijail_preserve_fd()).
467 * @min_priority the minimum logging priority. Same as the priority argument
468 * to syslog(2).
469 */
470void minijail_log_to_fd(int fd, int min_priority);
471
Elly Jonescd7a9042011-07-22 13:56:51 -0400472#ifdef __cplusplus
Elly Jonese1749eb2011-10-07 13:54:59 -0400473}; /* extern "C" */
Elly Jonescd7a9042011-07-22 13:56:51 -0400474#endif
475
Elly Jonese1749eb2011-10-07 13:54:59 -0400476#endif /* !_LIBMINIJAIL_H_ */