blob: 6738a3250bf787d4da8cdaf93a9ff6aa7aabc4a9 [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);
Elly Fong-Jones6c086302013-03-20 17:15:28 -040053void minijail_namespace_net(struct minijail *j);
Elly Jones761b7412012-06-13 15:49:52 -040054/* Implies namespace_vfs and remount_readonly.
55 * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>.
56 */
Elly Jonescd7a9042011-07-22 13:56:51 -040057void minijail_namespace_pids(struct minijail *j);
58void minijail_remount_readonly(struct minijail *j);
59void minijail_inherit_usergroups(struct minijail *j);
60void minijail_disable_ptrace(struct minijail *j);
61
Elly Jones51a5b6c2011-10-12 19:09:26 -040062/* minijail_enter_chroot: enables chroot() restriction for @j
63 * @j minijail to apply restriction to
64 * @dir directory to chroot() to. Owned by caller.
65 *
66 * Enters @dir, binding all bind mounts specified with minijail_bind() into
67 * place. Requires @dir to contain all necessary directories for bind mounts
68 * (i.e., if you have requested a bind mount at /etc, /etc must exist in @dir.)
69 *
70 * Returns 0 on success.
71 */
72int minijail_enter_chroot(struct minijail *j, const char *dir);
73
Lee Campbell11af0622014-05-22 12:36:04 -070074/* minijail_mount_tmp: enables mounting of a tmpfs filesystem on /tmp.
75 * As be rules of bind mounts, /tmp must exist in chroot.
76 */
77void minijail_mount_tmp(struct minijail *j);
78
Elly Jones51a5b6c2011-10-12 19:09:26 -040079/* minijail_bind: bind-mounts @src into @j as @dest, optionally writeable
80 * @j minijail to bind inside
81 * @src source to bind
82 * @dest location to bind (inside chroot)
83 * @writeable 1 if the bind mount should be writeable
84 *
85 * This may be called multiple times; all bindings will be applied in the order
86 * of minijail_bind() calls.
87 */
88int minijail_bind(struct minijail *j, const char *src, const char *dest,
Jorge Lucangeli Obes2f61ee42014-06-16 11:08:18 -070089 int writeable);
Elly Jones51a5b6c2011-10-12 19:09:26 -040090
Elly Jonescd7a9042011-07-22 13:56:51 -040091/* Lock this process into the given minijail. Note that this procedure cannot fail,
92 * since there is no way to undo privilege-dropping; therefore, if any part of
93 * the privilege-drop fails, minijail_enter() will abort the entire process.
94 *
95 * Some restrictions cannot be enabled this way (pid namespaces) and attempting
96 * to do so will cause an abort.
97 */
98void minijail_enter(const struct minijail *j);
99
100/* Run the specified command in the given minijail, execve(3)-style. This is
Elly Jonese1749eb2011-10-07 13:54:59 -0400101 * required if minijail_namespace_pids() was used.
102 */
103int minijail_run(struct minijail *j, const char *filename,
104 char *const argv[]);
Elly Jonescd7a9042011-07-22 13:56:51 -0400105
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700106/* Run the specified command in the given minijail, execve(3)-style.
Lee Campbell1e4fc6a2014-06-06 17:40:02 -0700107 * Used with static binaries.
108 */
109int minijail_run_static(struct minijail *j, const char *filename,
110 char *const argv[]);
111
112/* Run the specified command in the given minijail, execve(3)-style.
Jorge Lucangeli Obes9807d032012-04-17 13:36:00 -0700113 * Update |*pchild_pid| with the pid of the child.
114 */
115int minijail_run_pid(struct minijail *j, const char *filename,
116 char *const argv[], pid_t *pchild_pid);
117
Jorge Lucangeli Obesdf4bd352012-08-29 19:12:28 -0700118/* Run the specified command in the given minijail, execve(3)-style.
119 * Update |*pstdin_fd| with a fd that allows writing to the child's
120 * standard input.
121 */
122int minijail_run_pipe(struct minijail *j, const char *filename,
123 char *const argv[], int *pstdin_fd);
124
125/* Run the specified command in the given minijail, execve(3)-style.
126 * Update |*pchild_pid| with the pid of the child.
127 * Update |*pstdin_fd| with a fd that allows writing to the child's
128 * standard input.
129 */
130int minijail_run_pid_pipe(struct minijail *j, const char *filename,
131 char *const argv[], pid_t *pchild_pid,
132 int *pstdin_fd);
133
Jorge Lucangeli Obes339a1132013-02-15 16:53:47 -0800134/* Run the specified command in the given minijail, execve(3)-style.
135 * Update |*pchild_pid| with the pid of the child.
136 * Update |*pstdin_fd| with a fd that allows writing to the child's
137 * standard input.
138 * Update |*pstdout_fd| with a fd that allows reading from the child's
139 * standard output.
140 * Update |*pstderr_fd| with a fd that allows reading from the child's
141 * standard error.
142 */
143int minijail_run_pid_pipes(struct minijail *j, const char *filename,
144 char *const argv[], pid_t *pchild_pid,
145 int *pstdin_fd, int *pstdout_fd, int *pstderr_fd);
146
Elly Jonescd7a9042011-07-22 13:56:51 -0400147/* Kill the specified minijail. The minijail must have been created with pid
Elly Jonese1749eb2011-10-07 13:54:59 -0400148 * namespacing; if it was, all processes inside it are atomically killed.
149 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400150int minijail_kill(struct minijail *j);
151
152/* Wait for all processed in the specified minijail to exit. Returns the exit
Elly Jonese1749eb2011-10-07 13:54:59 -0400153 * status of the _first_ process spawned in the jail.
154 */
Elly Jonescd7a9042011-07-22 13:56:51 -0400155int minijail_wait(struct minijail *j);
156
157/* Frees the given minijail. It does not matter if the process is inside the minijail or
158 * not. */
159void minijail_destroy(struct minijail *j);
160
161#ifdef __cplusplus
Elly Jonese1749eb2011-10-07 13:54:59 -0400162}; /* extern "C" */
Elly Jonescd7a9042011-07-22 13:56:51 -0400163#endif
164
Elly Jonese1749eb2011-10-07 13:54:59 -0400165#endif /* !_LIBMINIJAIL_H_ */