blob: aceb69bdef32e60be8275f41866c23126a5bdca4 [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);
Will Drewry2ddaad02011-09-16 11:36:08 -050043/* Stores user to change to and copies |user| for internal consistency. */
Elly Jonescd7a9042011-07-22 13:56:51 -040044int minijail_change_user(struct minijail *j, const char *user);
Will Drewry2ddaad02011-09-16 11:36:08 -050045/* Does not take ownership of |group|. */
Elly Jonescd7a9042011-07-22 13:56:51 -040046int minijail_change_group(struct minijail *j, const char *group);
47void minijail_use_seccomp(struct minijail *j);
Jorge Lucangeli Obesc2c9bcc2012-05-01 09:30:24 -070048void minijail_no_new_privs(struct minijail *j);
Will Drewry32ac9f52011-08-18 21:36:27 -050049void minijail_use_seccomp_filter(struct minijail *j);
50void minijail_parse_seccomp_filters(struct minijail *j, const char *path);
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070051void minijail_log_seccomp_filter_failures(struct minijail *j);
Elly Jonescd7a9042011-07-22 13:56:51 -040052void minijail_use_caps(struct minijail *j, uint64_t capmask);
53void minijail_namespace_vfs(struct minijail *j);
Jorge Lucangeli Obes1563b5b2014-07-10 07:01:53 -070054void minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path);
Dylan Reidf7942472015-11-18 17:55:26 -080055void minijail_namespace_ipc(struct minijail *j);
Elly Fong-Jones6c086302013-03-20 17:15:28 -040056void minijail_namespace_net(struct minijail *j);
Dylan Reid1102f5a2015-09-15 11:52:20 -070057void minijail_namespace_enter_net(struct minijail *j, const char *ns_path);
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080058/*
59 * Implies namespace_vfs and remount_proc_readonly.
Elly Jones761b7412012-06-13 15:49:52 -040060 * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>.
61 */
Elly Jonescd7a9042011-07-22 13:56:51 -040062void minijail_namespace_pids(struct minijail *j);
Yu-Hsi Chiang10e91232015-08-05 14:40:45 +080063void minijail_namespace_user(struct minijail *j);
64int minijail_uidmap(struct minijail *j, const char *uidmap);
65int minijail_gidmap(struct minijail *j, const char *gidmap);
Dylan Reid791f5772015-09-14 20:02:42 -070066void minijail_remount_proc_readonly(struct minijail *j);
Yu-Hsi Chiang3e954ec2015-07-28 16:48:14 +080067void minijail_run_as_init(struct minijail *j);
Yu-Hsi Chiang3cc05ea2015-08-11 11:23:17 +080068int minijail_write_pid_file(struct minijail *j, const char *path);
Elly Jonescd7a9042011-07-22 13:56:51 -040069void minijail_inherit_usergroups(struct minijail *j);
70void minijail_disable_ptrace(struct minijail *j);
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080071/*
72 * Changes the jailed process's syscall table to the alt_syscall table
Andrew Brestickereac28942015-11-11 16:04:46 -080073 * named |table|.
74 */
75int minijail_use_alt_syscall(struct minijail *j, const char *table);
Elly Jonescd7a9042011-07-22 13:56:51 -040076
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080077/*
78 * minijail_enter_chroot: enables chroot() restriction for @j
Elly Jones51a5b6c2011-10-12 19:09:26 -040079 * @j minijail to apply restriction to
80 * @dir directory to chroot() to. Owned by caller.
81 *
82 * Enters @dir, binding all bind mounts specified with minijail_bind() into
83 * place. Requires @dir to contain all necessary directories for bind mounts
84 * (i.e., if you have requested a bind mount at /etc, /etc must exist in @dir.)
85 *
86 * Returns 0 on success.
87 */
88int minijail_enter_chroot(struct minijail *j, const char *dir);
Yu-Hsi Chiang64d65a72015-08-13 17:43:27 +080089int minijail_enter_pivot_root(struct minijail *j, const char *dir);
Elly Jones51a5b6c2011-10-12 19:09:26 -040090
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -080091/*
92 * minijail_get_original_path: returns the path of a given file outside of the
Dylan Reid08946cc2015-09-16 19:10:57 -070093 * chroot.
94 * @j minijail to obtain the path from.
95 * @chroot_path path inside of the chroot() to.
96 *
97 * When executing a binary in a chroot or pivot_root, return path to the binary
98 * outside of the chroot.
99 *
100 * Returns a string containing the path. This must be freed by the caller.
101 */
102char *minijail_get_original_path(struct minijail *j, const char *chroot_path);
103
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800104/*
105 * minijail_mount_tmp: enables mounting of a tmpfs filesystem on /tmp.
Lee Campbell11af0622014-05-22 12:36:04 -0700106 * As be rules of bind mounts, /tmp must exist in chroot.
107 */
108void minijail_mount_tmp(struct minijail *j);
109
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800110/*
111 * minijail_mount: when entering minijail @j, mounts @src at @dst with @flags
Dylan Reid648b2202015-10-23 00:50:00 -0700112 * @j minijail to bind inside
113 * @src source to bind
114 * @dest location to bind (inside chroot)
115 * @type type of filesystem
116 * @flags flags passed to mount
117 *
118 * This may be called multiple times; all bindings will be applied in the order
119 * of minijail_mount() calls.
120 */
121int minijail_mount(struct minijail *j, const char *src, const char *dest,
122 const char *type, unsigned long flags);
123
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800124/*
125 * minijail_bind: bind-mounts @src into @j as @dest, optionally writeable
Elly Jones51a5b6c2011-10-12 19:09:26 -0400126 * @j minijail to bind inside
127 * @src source to bind
128 * @dest location to bind (inside chroot)
129 * @writeable 1 if the bind mount should be writeable
130 *
131 * This may be called multiple times; all bindings will be applied in the order
132 * of minijail_bind() calls.
133 */
134int minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -0700135 int writeable);
Elly Jones51a5b6c2011-10-12 19:09:26 -0400136
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800137/*
138 * Lock this process into the given minijail. Note that this procedure cannot fail,
Elly Jonescd7a9042011-07-22 13:56:51 -0400139 * since there is no way to undo privilege-dropping; therefore, if any part of
140 * the privilege-drop fails, minijail_enter() will abort the entire process.
141 *
142 * Some restrictions cannot be enabled this way (pid namespaces) and attempting
143 * to do so will cause an abort.
144 */
145void minijail_enter(const struct minijail *j);
146
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800147/*
148 * Run the specified command in the given minijail, execve(2)-style. This is
Elly Jonese1749eb2011-10-07 13:54:59 -0400149 * required if minijail_namespace_pids() was used.
150 */
151int minijail_run(struct minijail *j, const char *filename,
152 char *const argv[]);
Elly Jonescd7a9042011-07-22 13:56:51 -0400153
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800154/*
155 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700156 * Used with static binaries, or on systems without support for LD_PRELOAD.
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700157 */
Jorge Lucangeli Obes54714502015-09-30 10:08:45 -0700158int minijail_run_no_preload(struct minijail *j, const char *filename,
159 char *const argv[]);
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700160
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800161/*
162 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700163 * Update |*pchild_pid| with the pid of the child.
164 */
165int minijail_run_pid(struct minijail *j, const char *filename,
166 char *const argv[], pid_t *pchild_pid);
167
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800168/*
169 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700170 * Update |*pstdin_fd| with a fd that allows writing to the child's
171 * standard input.
172 */
173int minijail_run_pipe(struct minijail *j, const char *filename,
174 char *const argv[], int *pstdin_fd);
175
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800176/*
177 * Run the specified command in the given minijail, execve(2)-style.
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700178 * Update |*pchild_pid| with the pid of the child.
179 * Update |*pstdin_fd| with a fd that allows writing to the child's
180 * standard input.
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800181 * Update |*pstdout_fd| with a fd that allows reading from the child's
182 * standard output.
183 * Update |*pstderr_fd| with a fd that allows reading from the child's
184 * standard error.
185 */
186int minijail_run_pid_pipes(struct minijail *j, const char *filename,
187 char *const argv[], pid_t *pchild_pid,
188 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd);
189
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800190/*
191 * Run the specified command in the given minijail, execve(2)-style.
Samuel Tan63187f42015-10-16 13:01:53 -0700192 * Update |*pchild_pid| with the pid of the child.
193 * Update |*pstdin_fd| with a fd that allows writing to the child's
194 * standard input.
195 * Update |*pstdout_fd| with a fd that allows reading from the child's
196 * standard output.
197 * Update |*pstderr_fd| with a fd that allows reading from the child's
198 * standard error.
199 * Used with static binaries, or on systems without support for LD_PRELOAD.
200 */
201int minijail_run_pid_pipes_no_preload(struct minijail *j, const char *filename,
202 char *const argv[], pid_t *pchild_pid,
203 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd);
204
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800205/*
206 * Kill the specified minijail. The minijail must have been created with pid
Elly Jonese1749eb2011-10-07 13:54:59 -0400207 * namespacing; if it was, all processes inside it are atomically killed.
208 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400209int minijail_kill(struct minijail *j);
210
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800211/*
212 * Wait for all processed in the specified minijail to exit. Returns the exit
Elly Jonese1749eb2011-10-07 13:54:59 -0400213 * status of the _first_ process spawned in the jail.
214 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400215int minijail_wait(struct minijail *j);
216
Jorge Lucangeli Obesd0a6e2f2015-11-24 14:21:21 -0800217/*
218 * Frees the given minijail. It does not matter if the process is inside the minijail or
219 * not.
220 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400221void minijail_destroy(struct minijail *j);
222
223#ifdef __cplusplus
Elly Jonese1749eb2011-10-07 13:54:59 -0400224}; /* extern "C" */
Elly Jonescd7a9042011-07-22 13:56:51 -0400225#endif
226
Elly Jonese1749eb2011-10-07 13:54:59 -0400227#endif /* !_LIBMINIJAIL_H_ */