blob: 6eccb02c39564d14020e7884a9d62253a47c3cdc [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
33/* Allocates a new minijail with no restrictions. */
34struct minijail *minijail_new(void);
35
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080036/*
37 * These functions add restrictions to the minijail. They are not applied until
Elly Jonescd7a9042011-07-22 13:56:51 -040038 * minijail_enter() is called. See the documentation in minijail0.1 for
Elly Jonese1749eb2011-10-07 13:54:59 -040039 * explanations in detail of what the restrictions do.
40 */
Elly Jonescd7a9042011-07-22 13:56:51 -040041void minijail_change_uid(struct minijail *j, uid_t uid);
42void minijail_change_gid(struct minijail *j, gid_t gid);
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -080043/* Copies |list|. */
Jorge Lucangeli Obesbc67f442016-01-08 14:43:45 -080044void minijail_set_supplementary_gids(struct minijail *j, size_t size,
45 const gid_t *list);
Will Drewry2ddaad02011-09-16 11:36:08 -050046/* Stores user to change to and copies |user| for internal consistency. */
Elly Jonescd7a9042011-07-22 13:56:51 -040047int minijail_change_user(struct minijail *j, const char *user);
Will Drewry2ddaad02011-09-16 11:36:08 -050048/* Does not take ownership of |group|. */
Elly Jonescd7a9042011-07-22 13:56:51 -040049int minijail_change_group(struct minijail *j, const char *group);
50void minijail_use_seccomp(struct minijail *j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070051void minijail_no_new_privs(struct minijail *j);
Will Drewry32ac9f52011-08-18 21:36:27 -050052void minijail_use_seccomp_filter(struct minijail *j);
53void minijail_parse_seccomp_filters(struct minijail *j, const char *path);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070054void minijail_log_seccomp_filter_failures(struct minijail *j);
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -080055/* 'minijail_use_caps' and 'minijail_capbset_drop' are mutually exclusive. */
Elly Jonescd7a9042011-07-22 13:56:51 -040056void minijail_use_caps(struct minijail *j, uint64_t capmask);
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -080057void minijail_capbset_drop(struct minijail *j, uint64_t capmask);
Peter Qiu2860c462015-12-16 15:13:06 -080058void minijail_reset_signal_mask(struct minijail *j);
Elly Jonescd7a9042011-07-22 13:56:51 -040059void minijail_namespace_vfs(struct minijail *j);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070060void minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path);
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -080061/*
62 * This option is *dangerous* as it negates most of the functionality of
63 * minijail_namespace_vfs(). You very likely don't need this.
64 */
65void minijail_skip_remount_private(struct minijail *j);
Dylan Reidf7942472015-11-18 17:55:26 -080066void minijail_namespace_ipc(struct minijail *j);
Elly Fong-Jones6c086302013-03-20 17:15:28 -040067void minijail_namespace_net(struct minijail *j);
Dylan Reid1102f5a2015-09-15 11:52:20 -070068void minijail_namespace_enter_net(struct minijail *j, const char *ns_path);
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080069/*
70 * Implies namespace_vfs and remount_proc_readonly.
Elly Jones761b7412012-06-13 15:49:52 -040071 * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>.
72 */
Elly Jonescd7a9042011-07-22 13:56:51 -040073void minijail_namespace_pids(struct minijail *j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080074void minijail_namespace_user(struct minijail *j);
75int minijail_uidmap(struct minijail *j, const char *uidmap);
76int minijail_gidmap(struct minijail *j, const char *gidmap);
Dylan Reid791f5772015-09-14 20:02:42 -070077void minijail_remount_proc_readonly(struct minijail *j);
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +080078void minijail_run_as_init(struct minijail *j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +080079int minijail_write_pid_file(struct minijail *j, const char *path);
Elly Jonescd7a9042011-07-22 13:56:51 -040080void minijail_inherit_usergroups(struct minijail *j);
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080081/*
82 * Changes the jailed process's syscall table to the alt_syscall table
Andrew Brestickereac28942015-11-11 16:04:46 -080083 * named |table|.
84 */
85int minijail_use_alt_syscall(struct minijail *j, const char *table);
Elly Jonescd7a9042011-07-22 13:56:51 -040086
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080087/*
Dylan Reid605ce7f2016-01-19 19:21:00 -080088 * Adds the jailed process to the cgroup given by |path|. |path| should be the
89 * full path to the cgroups "tasks" file.
90 * Example: /sys/fs/cgroup/cpu/jailed_procs/tasks adds to the "jailed_procs" cpu
91 * cgroup.
92 */
93int minijail_add_to_cgroup(struct minijail *j, const char *path);
94
95/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080096 * minijail_enter_chroot: enables chroot() restriction for @j
Elly Jones51a5b6c2011-10-12 19:09:26 -040097 * @j minijail to apply restriction to
98 * @dir directory to chroot() to. Owned by caller.
99 *
100 * Enters @dir, binding all bind mounts specified with minijail_bind() into
101 * place. Requires @dir to contain all necessary directories for bind mounts
102 * (i.e., if you have requested a bind mount at /etc, /etc must exist in @dir.)
103 *
104 * Returns 0 on success.
105 */
106int minijail_enter_chroot(struct minijail *j, const char *dir);
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800107int minijail_enter_pivot_root(struct minijail *j, const char *dir);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400108
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800109/*
110 * minijail_get_original_path: returns the path of a given file outside of the
Dylan Reid08946cc2015-09-16 19:10:57 -0700111 * chroot.
112 * @j minijail to obtain the path from.
113 * @chroot_path path inside of the chroot() to.
114 *
115 * When executing a binary in a chroot or pivot_root, return path to the binary
116 * outside of the chroot.
117 *
118 * Returns a string containing the path. This must be freed by the caller.
119 */
120char *minijail_get_original_path(struct minijail *j, const char *chroot_path);
121
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800122/*
123 * minijail_mount_tmp: enables mounting of a tmpfs filesystem on /tmp.
Lee Campbell11af0622014-05-22 12:36:04 -0700124 * As be rules of bind mounts, /tmp must exist in chroot.
125 */
126void minijail_mount_tmp(struct minijail *j);
127
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800128/*
129 * minijail_mount: when entering minijail @j, mounts @src at @dst with @flags
Dylan Reid648b2202015-10-23 00:50:00 -0700130 * @j minijail to bind inside
131 * @src source to bind
132 * @dest location to bind (inside chroot)
133 * @type type of filesystem
134 * @flags flags passed to mount
135 *
136 * This may be called multiple times; all bindings will be applied in the order
137 * of minijail_mount() calls.
138 */
139int minijail_mount(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800140 const char *type, unsigned long flags);
Dylan Reid648b2202015-10-23 00:50:00 -0700141
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800142/*
143 * minijail_bind: bind-mounts @src into @j as @dest, optionally writeable
Elly Jones51a5b6c2011-10-12 19:09:26 -0400144 * @j minijail to bind inside
145 * @src source to bind
146 * @dest location to bind (inside chroot)
147 * @writeable 1 if the bind mount should be writeable
148 *
149 * This may be called multiple times; all bindings will be applied in the order
150 * of minijail_bind() calls.
151 */
152int minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700153 int writeable);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400154
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800155/*
156 * Lock this process into the given minijail. Note that this procedure cannot fail,
Elly Jonescd7a9042011-07-22 13:56:51 -0400157 * since there is no way to undo privilege-dropping; therefore, if any part of
158 * the privilege-drop fails, minijail_enter() will abort the entire process.
159 *
160 * Some restrictions cannot be enabled this way (pid namespaces) and attempting
161 * to do so will cause an abort.
162 */
163void minijail_enter(const struct minijail *j);
164
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800165/*
166 * Run the specified command in the given minijail, execve(2)-style. This is
Elly Jonese1749eb2011-10-07 13:54:59 -0400167 * required if minijail_namespace_pids() was used.
168 */
169int minijail_run(struct minijail *j, const char *filename,
170 char *const argv[]);
Elly Jonescd7a9042011-07-22 13:56:51 -0400171
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800172/*
173 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700174 * Used with static binaries, or on systems without support for LD_PRELOAD.
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700175 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700176int minijail_run_no_preload(struct minijail *j, const char *filename,
177 char *const argv[]);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700178
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800179/*
180 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700181 * Update |*pchild_pid| with the pid of the child.
182 */
183int minijail_run_pid(struct minijail *j, const char *filename,
184 char *const argv[], pid_t *pchild_pid);
185
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800186/*
187 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700188 * Update |*pstdin_fd| with a fd that allows writing to the child's
189 * standard input.
190 */
191int minijail_run_pipe(struct minijail *j, const char *filename,
192 char *const argv[], int *pstdin_fd);
193
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800194/*
195 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700196 * Update |*pchild_pid| with the pid of the child.
197 * Update |*pstdin_fd| with a fd that allows writing to the child's
198 * standard input.
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800199 * Update |*pstdout_fd| with a fd that allows reading from the child's
200 * standard output.
201 * Update |*pstderr_fd| with a fd that allows reading from the child's
202 * standard error.
203 */
204int minijail_run_pid_pipes(struct minijail *j, const char *filename,
205 char *const argv[], pid_t *pchild_pid,
206 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd);
207
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800208/*
209 * Run the specified command in the given minijail, execve(2)-style.
Samuel Tan63187f42015-10-16 13:01:53 -0700210 * Update |*pchild_pid| with the pid of the child.
211 * Update |*pstdin_fd| with a fd that allows writing to the child's
212 * standard input.
213 * Update |*pstdout_fd| with a fd that allows reading from the child's
214 * standard output.
215 * Update |*pstderr_fd| with a fd that allows reading from the child's
216 * standard error.
217 * Used with static binaries, or on systems without support for LD_PRELOAD.
218 */
219int minijail_run_pid_pipes_no_preload(struct minijail *j, const char *filename,
220 char *const argv[], pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800221 int *pstdin_fd, int *pstdout_fd,
222 int *pstderr_fd);
Samuel Tan63187f42015-10-16 13:01:53 -0700223
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800224/*
225 * Kill the specified minijail. The minijail must have been created with pid
Elly Jonese1749eb2011-10-07 13:54:59 -0400226 * namespacing; if it was, all processes inside it are atomically killed.
227 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400228int minijail_kill(struct minijail *j);
229
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800230/*
231 * Wait for all processed in the specified minijail to exit. Returns the exit
Elly Jonese1749eb2011-10-07 13:54:59 -0400232 * status of the _first_ process spawned in the jail.
233 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400234int minijail_wait(struct minijail *j);
235
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800236/*
237 * Frees the given minijail. It does not matter if the process is inside the minijail or
238 * not.
239 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400240void minijail_destroy(struct minijail *j);
241
242#ifdef __cplusplus
Elly Jonese1749eb2011-10-07 13:54:59 -0400243}; /* extern "C" */
Elly Jonescd7a9042011-07-22 13:56:51 -0400244#endif
245
Elly Jonese1749eb2011-10-07 13:54:59 -0400246#endif /* !_LIBMINIJAIL_H_ */