blob: 9177ca872ea68840c048c584a166a044769821db [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);
Lutz Justen13807cb2017-01-03 17:11:55 +010046void minijail_keep_supplementary_gids(struct minijail *j);
Will Drewry2ddaad02011-09-16 11:36:08 -050047/* Stores user to change to and copies |user| for internal consistency. */
Elly Jonescd7a9042011-07-22 13:56:51 -040048int minijail_change_user(struct minijail *j, const char *user);
Will Drewry2ddaad02011-09-16 11:36:08 -050049/* Does not take ownership of |group|. */
Elly Jonescd7a9042011-07-22 13:56:51 -040050int minijail_change_group(struct minijail *j, const char *group);
51void minijail_use_seccomp(struct minijail *j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070052void minijail_no_new_privs(struct minijail *j);
Will Drewry32ac9f52011-08-18 21:36:27 -050053void minijail_use_seccomp_filter(struct minijail *j);
Jorge Lucangeli Obes13650612016-09-02 11:27:29 -040054void minijail_set_seccomp_filter_tsync(struct minijail *j);
Will Drewry32ac9f52011-08-18 21:36:27 -050055void minijail_parse_seccomp_filters(struct minijail *j, const char *path);
Jorge Lucangeli Obes4d4b3be2016-08-16 16:58:14 -040056void minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070057void minijail_log_seccomp_filter_failures(struct minijail *j);
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -080058/* 'minijail_use_caps' and 'minijail_capbset_drop' are mutually exclusive. */
Elly Jonescd7a9042011-07-22 13:56:51 -040059void minijail_use_caps(struct minijail *j, uint64_t capmask);
Jorge Lucangeli Obesf9fcdbe2016-02-19 15:04:09 -080060void minijail_capbset_drop(struct minijail *j, uint64_t capmask);
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -040061/* 'minijail_set_ambient_caps' requires 'minijail_use_caps'. */
62void minijail_set_ambient_caps(struct minijail *j);
Peter Qiu2860c462015-12-16 15:13:06 -080063void minijail_reset_signal_mask(struct minijail *j);
Elly Jonescd7a9042011-07-22 13:56:51 -040064void minijail_namespace_vfs(struct minijail *j);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070065void minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path);
Chirantan Ekbote866bb3a2017-02-07 12:26:42 -080066void minijail_new_session_keyring(struct minijail *j);
67
Jorge Lucangeli Obesa521bee2016-03-03 13:47:57 -080068/*
69 * This option is *dangerous* as it negates most of the functionality of
70 * minijail_namespace_vfs(). You very likely don't need this.
71 */
72void minijail_skip_remount_private(struct minijail *j);
Dylan Reidf7942472015-11-18 17:55:26 -080073void minijail_namespace_ipc(struct minijail *j);
Elly Fong-Jones6c086302013-03-20 17:15:28 -040074void minijail_namespace_net(struct minijail *j);
Dylan Reid1102f5a2015-09-15 11:52:20 -070075void minijail_namespace_enter_net(struct minijail *j, const char *ns_path);
Dylan Reid4cbc2a52016-06-17 19:06:07 -070076void minijail_namespace_cgroups(struct minijail *j);
Luis Hector Chavez43ff0802016-10-07 12:21:07 -070077/* Closes all open file descriptors after forking. */
78void minijail_close_open_fds(struct minijail *j);
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080079/*
80 * Implies namespace_vfs and remount_proc_readonly.
Elly Jones761b7412012-06-13 15:49:52 -040081 * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>.
82 */
Elly Jonescd7a9042011-07-22 13:56:51 -040083void minijail_namespace_pids(struct minijail *j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080084void minijail_namespace_user(struct minijail *j);
Jorge Lucangeli Obes200299c2016-09-23 15:21:57 -040085void minijail_namespace_user_disable_setgroups(struct minijail *j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080086int minijail_uidmap(struct minijail *j, const char *uidmap);
87int minijail_gidmap(struct minijail *j, const char *gidmap);
Dylan Reid791f5772015-09-14 20:02:42 -070088void minijail_remount_proc_readonly(struct minijail *j);
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +080089void minijail_run_as_init(struct minijail *j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +080090int minijail_write_pid_file(struct minijail *j, const char *path);
Elly Jonescd7a9042011-07-22 13:56:51 -040091void minijail_inherit_usergroups(struct minijail *j);
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080092/*
93 * Changes the jailed process's syscall table to the alt_syscall table
Andrew Brestickereac28942015-11-11 16:04:46 -080094 * named |table|.
95 */
96int minijail_use_alt_syscall(struct minijail *j, const char *table);
Elly Jonescd7a9042011-07-22 13:56:51 -040097
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080098/*
Dylan Reid605ce7f2016-01-19 19:21:00 -080099 * Adds the jailed process to the cgroup given by |path|. |path| should be the
100 * full path to the cgroups "tasks" file.
101 * Example: /sys/fs/cgroup/cpu/jailed_procs/tasks adds to the "jailed_procs" cpu
102 * cgroup.
103 */
104int minijail_add_to_cgroup(struct minijail *j, const char *path);
105
106/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800107 * minijail_enter_chroot: enables chroot() restriction for @j
Elly Jones51a5b6c2011-10-12 19:09:26 -0400108 * @j minijail to apply restriction to
109 * @dir directory to chroot() to. Owned by caller.
110 *
111 * Enters @dir, binding all bind mounts specified with minijail_bind() into
112 * place. Requires @dir to contain all necessary directories for bind mounts
113 * (i.e., if you have requested a bind mount at /etc, /etc must exist in @dir.)
114 *
115 * Returns 0 on success.
116 */
117int minijail_enter_chroot(struct minijail *j, const char *dir);
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +0800118int minijail_enter_pivot_root(struct minijail *j, const char *dir);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400119
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800120/*
121 * minijail_get_original_path: returns the path of a given file outside of the
Dylan Reid08946cc2015-09-16 19:10:57 -0700122 * chroot.
123 * @j minijail to obtain the path from.
124 * @chroot_path path inside of the chroot() to.
125 *
126 * When executing a binary in a chroot or pivot_root, return path to the binary
127 * outside of the chroot.
128 *
129 * Returns a string containing the path. This must be freed by the caller.
130 */
131char *minijail_get_original_path(struct minijail *j, const char *chroot_path);
132
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800133/*
Martin Pelikánab9eb442017-01-25 11:53:58 +1100134 * minijail_mount_tmp: enables mounting of a 64M tmpfs filesystem on /tmp.
Lee Campbell11af0622014-05-22 12:36:04 -0700135 * As be rules of bind mounts, /tmp must exist in chroot.
136 */
137void minijail_mount_tmp(struct minijail *j);
138
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800139/*
Martin Pelikánab9eb442017-01-25 11:53:58 +1100140 * minijail_mount_tmp_size: enables mounting of a tmpfs filesystem on /tmp.
141 * As be rules of bind mounts, /tmp must exist in chroot. Size is in bytes.
142 */
143void minijail_mount_tmp_size(struct minijail *j, size_t size);
144
145/*
Dylan Reid81e23972016-05-18 14:06:35 -0700146 * minijail_mount_with_data: when entering minijail @j,
147 * mounts @src at @dst with @flags and @data.
148 * @j minijail to bind inside
149 * @src source to bind
150 * @dest location to bind (inside chroot)
151 * @type type of filesystem
152 * @flags flags passed to mount
153 * @data data arguments passed to mount(2), e.g. "mode=755"
154 *
155 * This may be called multiple times; all mounts will be applied in the order
156 * of minijail_mount() calls.
157 */
158int minijail_mount_with_data(struct minijail *j, const char *src,
159 const char *dest, const char *type,
160 unsigned long flags, const char *data);
161
162/*
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800163 * minijail_mount: when entering minijail @j, mounts @src at @dst with @flags
Dylan Reid648b2202015-10-23 00:50:00 -0700164 * @j minijail to bind inside
165 * @src source to bind
166 * @dest location to bind (inside chroot)
167 * @type type of filesystem
168 * @flags flags passed to mount
169 *
Dylan Reid81e23972016-05-18 14:06:35 -0700170 * This may be called multiple times; all mounts will be applied in the order
Dylan Reid648b2202015-10-23 00:50:00 -0700171 * of minijail_mount() calls.
172 */
173int minijail_mount(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obesd16ac492015-12-03 14:44:35 -0800174 const char *type, unsigned long flags);
Dylan Reid648b2202015-10-23 00:50:00 -0700175
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800176/*
177 * minijail_bind: bind-mounts @src into @j as @dest, optionally writeable
Elly Jones51a5b6c2011-10-12 19:09:26 -0400178 * @j minijail to bind inside
179 * @src source to bind
180 * @dest location to bind (inside chroot)
181 * @writeable 1 if the bind mount should be writeable
182 *
183 * This may be called multiple times; all bindings will be applied in the order
184 * of minijail_bind() calls.
185 */
186int minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700187 int writeable);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400188
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800189/*
Jorge Lucangeli Obesdeca9f22016-07-01 09:49:39 -0400190 * Lock this process into the given minijail. Note that this procedure cannot
191 * fail, since there is no way to undo privilege-dropping; therefore, if any
192 * part of the privilege-drop fails, minijail_enter() will abort the entire
193 * process.
Elly Jonescd7a9042011-07-22 13:56:51 -0400194 *
195 * Some restrictions cannot be enabled this way (pid namespaces) and attempting
196 * to do so will cause an abort.
197 */
198void minijail_enter(const struct minijail *j);
199
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800200/*
201 * Run the specified command in the given minijail, execve(2)-style. This is
Elly Jonese1749eb2011-10-07 13:54:59 -0400202 * required if minijail_namespace_pids() was used.
203 */
204int minijail_run(struct minijail *j, const char *filename,
205 char *const argv[]);
Elly Jonescd7a9042011-07-22 13:56:51 -0400206
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800207/*
208 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700209 * Used with static binaries, or on systems without support for LD_PRELOAD.
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700210 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700211int minijail_run_no_preload(struct minijail *j, const char *filename,
212 char *const argv[]);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700213
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800214/*
215 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700216 * Update |*pchild_pid| with the pid of the child.
217 */
218int minijail_run_pid(struct minijail *j, const char *filename,
219 char *const argv[], pid_t *pchild_pid);
220
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800221/*
222 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700223 * Update |*pstdin_fd| with a fd that allows writing to the child's
224 * standard input.
225 */
226int minijail_run_pipe(struct minijail *j, const char *filename,
227 char *const argv[], int *pstdin_fd);
228
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800229/*
230 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700231 * Update |*pchild_pid| with the pid of the child.
232 * Update |*pstdin_fd| with a fd that allows writing to the child's
233 * standard input.
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800234 * Update |*pstdout_fd| with a fd that allows reading from the child's
235 * standard output.
236 * Update |*pstderr_fd| with a fd that allows reading from the child's
237 * standard error.
238 */
239int minijail_run_pid_pipes(struct minijail *j, const char *filename,
240 char *const argv[], pid_t *pchild_pid,
241 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd);
242
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800243/*
244 * Run the specified command in the given minijail, execve(2)-style.
Samuel Tan63187f42015-10-16 13:01:53 -0700245 * Update |*pchild_pid| with the pid of the child.
246 * Update |*pstdin_fd| with a fd that allows writing to the child's
247 * standard input.
248 * Update |*pstdout_fd| with a fd that allows reading from the child's
249 * standard output.
250 * Update |*pstderr_fd| with a fd that allows reading from the child's
251 * standard error.
252 * Used with static binaries, or on systems without support for LD_PRELOAD.
253 */
254int minijail_run_pid_pipes_no_preload(struct minijail *j, const char *filename,
255 char *const argv[], pid_t *pchild_pid,
Jorge Lucangeli Obes43a6a862015-12-04 14:53:36 -0800256 int *pstdin_fd, int *pstdout_fd,
257 int *pstderr_fd);
Samuel Tan63187f42015-10-16 13:01:53 -0700258
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800259/*
260 * Kill the specified minijail. The minijail must have been created with pid
Elly Jonese1749eb2011-10-07 13:54:59 -0400261 * namespacing; if it was, all processes inside it are atomically killed.
262 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400263int minijail_kill(struct minijail *j);
264
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800265/*
Jorge Lucangeli Obesdeca9f22016-07-01 09:49:39 -0400266 * Wait for all processes in the specified minijail to exit. Returns the exit
Elly Jonese1749eb2011-10-07 13:54:59 -0400267 * status of the _first_ process spawned in the jail.
268 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400269int minijail_wait(struct minijail *j);
270
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800271/*
Jorge Lucangeli Obesdeca9f22016-07-01 09:49:39 -0400272 * Frees the given minijail. It does not matter if the process is inside the
273 * minijail or not.
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800274 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400275void minijail_destroy(struct minijail *j);
276
277#ifdef __cplusplus
Elly Jonese1749eb2011-10-07 13:54:59 -0400278}; /* extern "C" */
Elly Jonescd7a9042011-07-22 13:56:51 -0400279#endif
280
Elly Jonese1749eb2011-10-07 13:54:59 -0400281#endif /* !_LIBMINIJAIL_H_ */