blob: 9d9246233385be9220c1a624f96fa99a61e72abd [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
6/* The general pattern of use here:
7 * 1) Construct a minijail with minijail_new()
8 * 2) Apply the desired restrictions to it
9 * 3) Enter it, which locks the current process inside it, or:
10 * 3) Run a process inside it
11 * 4) Destroy it.
12 */
13
Elly Jonese1749eb2011-10-07 13:54:59 -040014#ifndef _LIBMINIJAIL_H_
15#define _LIBMINIJAIL_H_
Elly Jonescd7a9042011-07-22 13:56:51 -040016
17#include <stdint.h>
18#include <sys/types.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24enum {
Elly Jonese1749eb2011-10-07 13:54:59 -040025 MINIJAIL_ERR_PRELOAD = 252,
26 MINIJAIL_ERR_JAIL = 253,
27 MINIJAIL_ERR_INIT = 254,
Elly Jonescd7a9042011-07-22 13:56:51 -040028};
29
30struct minijail;
31
32/* Allocates a new minijail with no restrictions. */
33struct minijail *minijail_new(void);
34
35/* These functions add restrictions to the minijail. They are not applied until
36 * minijail_enter() is called. See the documentation in minijail0.1 for
Elly Jonese1749eb2011-10-07 13:54:59 -040037 * explanations in detail of what the restrictions do.
38 */
Elly Jonescd7a9042011-07-22 13:56:51 -040039void minijail_change_uid(struct minijail *j, uid_t uid);
40void minijail_change_gid(struct minijail *j, gid_t gid);
Will Drewry2ddaad02011-09-16 11:36:08 -050041/* Stores user to change to and copies |user| for internal consistency. */
Elly Jonescd7a9042011-07-22 13:56:51 -040042int minijail_change_user(struct minijail *j, const char *user);
Will Drewry2ddaad02011-09-16 11:36:08 -050043/* Does not take ownership of |group|. */
Elly Jonescd7a9042011-07-22 13:56:51 -040044int minijail_change_group(struct minijail *j, const char *group);
45void minijail_use_seccomp(struct minijail *j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070046void minijail_no_new_privs(struct minijail *j);
Will Drewry32ac9f52011-08-18 21:36:27 -050047void minijail_use_seccomp_filter(struct minijail *j);
48void minijail_parse_seccomp_filters(struct minijail *j, const char *path);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070049void minijail_log_seccomp_filter_failures(struct minijail *j);
Elly Jonescd7a9042011-07-22 13:56:51 -040050void minijail_use_caps(struct minijail *j, uint64_t capmask);
51void minijail_namespace_vfs(struct minijail *j);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070052void minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path);
Dylan Reidf7942472015-11-18 17:55:26 -080053void minijail_namespace_ipc(struct minijail *j);
Elly Fong-Jones6c086302013-03-20 17:15:28 -040054void minijail_namespace_net(struct minijail *j);
Dylan Reid1102f5a2015-09-15 11:52:20 -070055void minijail_namespace_enter_net(struct minijail *j, const char *ns_path);
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -070056/* Implies namespace_vfs and remount_proc_readonly.
Elly Jones761b7412012-06-13 15:49:52 -040057 * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>.
58 */
Elly Jonescd7a9042011-07-22 13:56:51 -040059void minijail_namespace_pids(struct minijail *j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080060void minijail_namespace_user(struct minijail *j);
61int minijail_uidmap(struct minijail *j, const char *uidmap);
62int minijail_gidmap(struct minijail *j, const char *gidmap);
Dylan Reid791f5772015-09-14 20:02:42 -070063void minijail_remount_proc_readonly(struct minijail *j);
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +080064void minijail_run_as_init(struct minijail *j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +080065int minijail_write_pid_file(struct minijail *j, const char *path);
Elly Jonescd7a9042011-07-22 13:56:51 -040066void minijail_inherit_usergroups(struct minijail *j);
67void minijail_disable_ptrace(struct minijail *j);
Andrew Brestickereac28942015-11-11 16:04:46 -080068/* Changes the jailed process's syscall table to the alt_syscall table
69 * named |table|.
70 */
71int minijail_use_alt_syscall(struct minijail *j, const char *table);
Elly Jonescd7a9042011-07-22 13:56:51 -040072
Elly Jones51a5b6c2011-10-12 19:09:26 -040073/* minijail_enter_chroot: enables chroot() restriction for @j
74 * @j minijail to apply restriction to
75 * @dir directory to chroot() to. Owned by caller.
76 *
77 * Enters @dir, binding all bind mounts specified with minijail_bind() into
78 * place. Requires @dir to contain all necessary directories for bind mounts
79 * (i.e., if you have requested a bind mount at /etc, /etc must exist in @dir.)
80 *
81 * Returns 0 on success.
82 */
83int minijail_enter_chroot(struct minijail *j, const char *dir);
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +080084int minijail_enter_pivot_root(struct minijail *j, const char *dir);
Elly Jones51a5b6c2011-10-12 19:09:26 -040085
Dylan Reid08946cc2015-09-16 19:10:57 -070086/* minijail_get_original_path: returns the path of a given file outside of the
87 * chroot.
88 * @j minijail to obtain the path from.
89 * @chroot_path path inside of the chroot() to.
90 *
91 * When executing a binary in a chroot or pivot_root, return path to the binary
92 * outside of the chroot.
93 *
94 * Returns a string containing the path. This must be freed by the caller.
95 */
96char *minijail_get_original_path(struct minijail *j, const char *chroot_path);
97
Lee Campbell11af0622014-05-22 12:36:04 -070098/* minijail_mount_tmp: enables mounting of a tmpfs filesystem on /tmp.
99 * As be rules of bind mounts, /tmp must exist in chroot.
100 */
101void minijail_mount_tmp(struct minijail *j);
102
Dylan Reid648b2202015-10-23 00:50:00 -0700103/* minijail_mount: when entering minijail @j, mounts @src at @dst with @flags
104 * @j minijail to bind inside
105 * @src source to bind
106 * @dest location to bind (inside chroot)
107 * @type type of filesystem
108 * @flags flags passed to mount
109 *
110 * This may be called multiple times; all bindings will be applied in the order
111 * of minijail_mount() calls.
112 */
113int minijail_mount(struct minijail *j, const char *src, const char *dest,
114 const char *type, unsigned long flags);
115
Elly Jones51a5b6c2011-10-12 19:09:26 -0400116/* minijail_bind: bind-mounts @src into @j as @dest, optionally writeable
117 * @j minijail to bind inside
118 * @src source to bind
119 * @dest location to bind (inside chroot)
120 * @writeable 1 if the bind mount should be writeable
121 *
122 * This may be called multiple times; all bindings will be applied in the order
123 * of minijail_bind() calls.
124 */
125int minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700126 int writeable);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400127
Elly Jonescd7a9042011-07-22 13:56:51 -0400128/* Lock this process into the given minijail. Note that this procedure cannot fail,
129 * since there is no way to undo privilege-dropping; therefore, if any part of
130 * the privilege-drop fails, minijail_enter() will abort the entire process.
131 *
132 * Some restrictions cannot be enabled this way (pid namespaces) and attempting
133 * to do so will cause an abort.
134 */
135void minijail_enter(const struct minijail *j);
136
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700137/* Run the specified command in the given minijail, execve(2)-style. This is
Elly Jonese1749eb2011-10-07 13:54:59 -0400138 * required if minijail_namespace_pids() was used.
139 */
140int minijail_run(struct minijail *j, const char *filename,
141 char *const argv[]);
Elly Jonescd7a9042011-07-22 13:56:51 -0400142
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700143/* Run the specified command in the given minijail, execve(2)-style.
144 * Used with static binaries, or on systems without support for LD_PRELOAD.
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700145 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700146int minijail_run_no_preload(struct minijail *j, const char *filename,
147 char *const argv[]);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700148
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700149/* Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700150 * Update |*pchild_pid| with the pid of the child.
151 */
152int minijail_run_pid(struct minijail *j, const char *filename,
153 char *const argv[], pid_t *pchild_pid);
154
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700155/* Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700156 * Update |*pstdin_fd| with a fd that allows writing to the child's
157 * standard input.
158 */
159int minijail_run_pipe(struct minijail *j, const char *filename,
160 char *const argv[], int *pstdin_fd);
161
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700162/* Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700163 * Update |*pchild_pid| with the pid of the child.
164 * Update |*pstdin_fd| with a fd that allows writing to the child's
165 * standard input.
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800166 * Update |*pstdout_fd| with a fd that allows reading from the child's
167 * standard output.
168 * Update |*pstderr_fd| with a fd that allows reading from the child's
169 * standard error.
170 */
171int minijail_run_pid_pipes(struct minijail *j, const char *filename,
172 char *const argv[], pid_t *pchild_pid,
173 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd);
174
Samuel Tan63187f42015-10-16 13:01:53 -0700175/* Run the specified command in the given minijail, execve(2)-style.
176 * Update |*pchild_pid| with the pid of the child.
177 * Update |*pstdin_fd| with a fd that allows writing to the child's
178 * standard input.
179 * Update |*pstdout_fd| with a fd that allows reading from the child's
180 * standard output.
181 * Update |*pstderr_fd| with a fd that allows reading from the child's
182 * standard error.
183 * Used with static binaries, or on systems without support for LD_PRELOAD.
184 */
185int minijail_run_pid_pipes_no_preload(struct minijail *j, const char *filename,
186 char *const argv[], pid_t *pchild_pid,
187 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd);
188
Elly Jonescd7a9042011-07-22 13:56:51 -0400189/* Kill the specified minijail. The minijail must have been created with pid
Elly Jonese1749eb2011-10-07 13:54:59 -0400190 * namespacing; if it was, all processes inside it are atomically killed.
191 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400192int minijail_kill(struct minijail *j);
193
194/* Wait for all processed in the specified minijail to exit. Returns the exit
Elly Jonese1749eb2011-10-07 13:54:59 -0400195 * status of the _first_ process spawned in the jail.
196 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400197int minijail_wait(struct minijail *j);
198
199/* Frees the given minijail. It does not matter if the process is inside the minijail or
200 * not. */
201void minijail_destroy(struct minijail *j);
202
203#ifdef __cplusplus
Elly Jonese1749eb2011-10-07 13:54:59 -0400204}; /* extern "C" */
Elly Jonescd7a9042011-07-22 13:56:51 -0400205#endif
206
Elly Jonese1749eb2011-10-07 13:54:59 -0400207#endif /* !_LIBMINIJAIL_H_ */