Elly Jones | e58176c | 2012-01-23 11:46:17 -0500 | [diff] [blame] | 1 | /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 3 | * found in the LICENSE file. |
| 4 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 5 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 6 | /* |
| 7 | * The general pattern of use here: |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 8 | * 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 Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 15 | #ifndef _LIBMINIJAIL_H_ |
| 16 | #define _LIBMINIJAIL_H_ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 17 | |
| 18 | #include <stdint.h> |
| 19 | #include <sys/types.h> |
| 20 | |
| 21 | #ifdef __cplusplus |
| 22 | extern "C" { |
| 23 | #endif |
| 24 | |
| 25 | enum { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 26 | MINIJAIL_ERR_PRELOAD = 252, |
| 27 | MINIJAIL_ERR_JAIL = 253, |
| 28 | MINIJAIL_ERR_INIT = 254, |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | struct minijail; |
| 32 | |
| 33 | /* Allocates a new minijail with no restrictions. */ |
| 34 | struct minijail *minijail_new(void); |
| 35 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 36 | /* |
| 37 | * These functions add restrictions to the minijail. They are not applied until |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 38 | * minijail_enter() is called. See the documentation in minijail0.1 for |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 39 | * explanations in detail of what the restrictions do. |
| 40 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 41 | void minijail_change_uid(struct minijail *j, uid_t uid); |
| 42 | void minijail_change_gid(struct minijail *j, gid_t gid); |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 43 | /* Copies |list|. */ |
Jorge Lucangeli Obes | bc67f44 | 2016-01-08 14:43:45 -0800 | [diff] [blame] | 44 | void minijail_set_supplementary_gids(struct minijail *j, size_t size, |
| 45 | const gid_t *list); |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 46 | /* Stores user to change to and copies |user| for internal consistency. */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 47 | int minijail_change_user(struct minijail *j, const char *user); |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 48 | /* Does not take ownership of |group|. */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 49 | int minijail_change_group(struct minijail *j, const char *group); |
| 50 | void minijail_use_seccomp(struct minijail *j); |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 51 | void minijail_no_new_privs(struct minijail *j); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 52 | void minijail_use_seccomp_filter(struct minijail *j); |
| 53 | void minijail_parse_seccomp_filters(struct minijail *j, const char *path); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 54 | void minijail_log_seccomp_filter_failures(struct minijail *j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 55 | void minijail_use_caps(struct minijail *j, uint64_t capmask); |
Peter Qiu | 2860c46 | 2015-12-16 15:13:06 -0800 | [diff] [blame] | 56 | void minijail_reset_signal_mask(struct minijail *j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 57 | void minijail_namespace_vfs(struct minijail *j); |
Jorge Lucangeli Obes | 1563b5b | 2014-07-10 07:01:53 -0700 | [diff] [blame] | 58 | void minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path); |
Dylan Reid | f794247 | 2015-11-18 17:55:26 -0800 | [diff] [blame] | 59 | void minijail_namespace_ipc(struct minijail *j); |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 60 | void minijail_namespace_net(struct minijail *j); |
Dylan Reid | 1102f5a | 2015-09-15 11:52:20 -0700 | [diff] [blame] | 61 | void minijail_namespace_enter_net(struct minijail *j, const char *ns_path); |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 62 | /* |
| 63 | * Implies namespace_vfs and remount_proc_readonly. |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 64 | * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>. |
| 65 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 66 | void minijail_namespace_pids(struct minijail *j); |
Yu-Hsi Chiang | 10e9123 | 2015-08-05 14:40:45 +0800 | [diff] [blame] | 67 | void minijail_namespace_user(struct minijail *j); |
| 68 | int minijail_uidmap(struct minijail *j, const char *uidmap); |
| 69 | int minijail_gidmap(struct minijail *j, const char *gidmap); |
Dylan Reid | 791f577 | 2015-09-14 20:02:42 -0700 | [diff] [blame] | 70 | void minijail_remount_proc_readonly(struct minijail *j); |
Yu-Hsi Chiang | 3e954ec | 2015-07-28 16:48:14 +0800 | [diff] [blame] | 71 | void minijail_run_as_init(struct minijail *j); |
Yu-Hsi Chiang | 3cc05ea | 2015-08-11 11:23:17 +0800 | [diff] [blame] | 72 | int minijail_write_pid_file(struct minijail *j, const char *path); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 73 | void minijail_inherit_usergroups(struct minijail *j); |
| 74 | void minijail_disable_ptrace(struct minijail *j); |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 75 | /* |
| 76 | * Changes the jailed process's syscall table to the alt_syscall table |
Andrew Bresticker | eac2894 | 2015-11-11 16:04:46 -0800 | [diff] [blame] | 77 | * named |table|. |
| 78 | */ |
| 79 | int minijail_use_alt_syscall(struct minijail *j, const char *table); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 80 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 81 | /* |
Dylan Reid | 605ce7f | 2016-01-19 19:21:00 -0800 | [diff] [blame] | 82 | * Adds the jailed process to the cgroup given by |path|. |path| should be the |
| 83 | * full path to the cgroups "tasks" file. |
| 84 | * Example: /sys/fs/cgroup/cpu/jailed_procs/tasks adds to the "jailed_procs" cpu |
| 85 | * cgroup. |
| 86 | */ |
| 87 | int minijail_add_to_cgroup(struct minijail *j, const char *path); |
| 88 | |
| 89 | /* |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 90 | * minijail_enter_chroot: enables chroot() restriction for @j |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 91 | * @j minijail to apply restriction to |
| 92 | * @dir directory to chroot() to. Owned by caller. |
| 93 | * |
| 94 | * Enters @dir, binding all bind mounts specified with minijail_bind() into |
| 95 | * place. Requires @dir to contain all necessary directories for bind mounts |
| 96 | * (i.e., if you have requested a bind mount at /etc, /etc must exist in @dir.) |
| 97 | * |
| 98 | * Returns 0 on success. |
| 99 | */ |
| 100 | int minijail_enter_chroot(struct minijail *j, const char *dir); |
Yu-Hsi Chiang | 64d65a7 | 2015-08-13 17:43:27 +0800 | [diff] [blame] | 101 | int minijail_enter_pivot_root(struct minijail *j, const char *dir); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 102 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 103 | /* |
| 104 | * minijail_get_original_path: returns the path of a given file outside of the |
Dylan Reid | 08946cc | 2015-09-16 19:10:57 -0700 | [diff] [blame] | 105 | * chroot. |
| 106 | * @j minijail to obtain the path from. |
| 107 | * @chroot_path path inside of the chroot() to. |
| 108 | * |
| 109 | * When executing a binary in a chroot or pivot_root, return path to the binary |
| 110 | * outside of the chroot. |
| 111 | * |
| 112 | * Returns a string containing the path. This must be freed by the caller. |
| 113 | */ |
| 114 | char *minijail_get_original_path(struct minijail *j, const char *chroot_path); |
| 115 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 116 | /* |
| 117 | * minijail_mount_tmp: enables mounting of a tmpfs filesystem on /tmp. |
Lee Campbell | 11af062 | 2014-05-22 12:36:04 -0700 | [diff] [blame] | 118 | * As be rules of bind mounts, /tmp must exist in chroot. |
| 119 | */ |
| 120 | void minijail_mount_tmp(struct minijail *j); |
| 121 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 122 | /* |
| 123 | * minijail_mount: when entering minijail @j, mounts @src at @dst with @flags |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 124 | * @j minijail to bind inside |
| 125 | * @src source to bind |
| 126 | * @dest location to bind (inside chroot) |
| 127 | * @type type of filesystem |
| 128 | * @flags flags passed to mount |
| 129 | * |
| 130 | * This may be called multiple times; all bindings will be applied in the order |
| 131 | * of minijail_mount() calls. |
| 132 | */ |
| 133 | int minijail_mount(struct minijail *j, const char *src, const char *dest, |
Jorge Lucangeli Obes | d16ac49 | 2015-12-03 14:44:35 -0800 | [diff] [blame] | 134 | const char *type, unsigned long flags); |
Dylan Reid | 648b220 | 2015-10-23 00:50:00 -0700 | [diff] [blame] | 135 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 136 | /* |
| 137 | * minijail_bind: bind-mounts @src into @j as @dest, optionally writeable |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 138 | * @j minijail to bind inside |
| 139 | * @src source to bind |
| 140 | * @dest location to bind (inside chroot) |
| 141 | * @writeable 1 if the bind mount should be writeable |
| 142 | * |
| 143 | * This may be called multiple times; all bindings will be applied in the order |
| 144 | * of minijail_bind() calls. |
| 145 | */ |
| 146 | int minijail_bind(struct minijail *j, const char *src, const char *dest, |
Jorge Lucangeli Obes | 2f61ee4 | 2014-06-16 11:08:18 -0700 | [diff] [blame] | 147 | int writeable); |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 148 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 149 | /* |
| 150 | * Lock this process into the given minijail. Note that this procedure cannot fail, |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 151 | * since there is no way to undo privilege-dropping; therefore, if any part of |
| 152 | * the privilege-drop fails, minijail_enter() will abort the entire process. |
| 153 | * |
| 154 | * Some restrictions cannot be enabled this way (pid namespaces) and attempting |
| 155 | * to do so will cause an abort. |
| 156 | */ |
| 157 | void minijail_enter(const struct minijail *j); |
| 158 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 159 | /* |
| 160 | * Run the specified command in the given minijail, execve(2)-style. This is |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 161 | * required if minijail_namespace_pids() was used. |
| 162 | */ |
| 163 | int minijail_run(struct minijail *j, const char *filename, |
| 164 | char *const argv[]); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 165 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 166 | /* |
| 167 | * Run the specified command in the given minijail, execve(2)-style. |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 168 | * Used with static binaries, or on systems without support for LD_PRELOAD. |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 169 | */ |
Jorge Lucangeli Obes | 5471450 | 2015-09-30 10:08:45 -0700 | [diff] [blame] | 170 | int minijail_run_no_preload(struct minijail *j, const char *filename, |
| 171 | char *const argv[]); |
Lee Campbell | 1e4fc6a | 2014-06-06 17:40:02 -0700 | [diff] [blame] | 172 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 173 | /* |
| 174 | * Run the specified command in the given minijail, execve(2)-style. |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 175 | * Update |*pchild_pid| with the pid of the child. |
| 176 | */ |
| 177 | int minijail_run_pid(struct minijail *j, const char *filename, |
| 178 | char *const argv[], pid_t *pchild_pid); |
| 179 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 180 | /* |
| 181 | * Run the specified command in the given minijail, execve(2)-style. |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 182 | * Update |*pstdin_fd| with a fd that allows writing to the child's |
| 183 | * standard input. |
| 184 | */ |
| 185 | int minijail_run_pipe(struct minijail *j, const char *filename, |
| 186 | char *const argv[], int *pstdin_fd); |
| 187 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 188 | /* |
| 189 | * Run the specified command in the given minijail, execve(2)-style. |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 190 | * Update |*pchild_pid| with the pid of the child. |
| 191 | * Update |*pstdin_fd| with a fd that allows writing to the child's |
| 192 | * standard input. |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 193 | * Update |*pstdout_fd| with a fd that allows reading from the child's |
| 194 | * standard output. |
| 195 | * Update |*pstderr_fd| with a fd that allows reading from the child's |
| 196 | * standard error. |
| 197 | */ |
| 198 | int minijail_run_pid_pipes(struct minijail *j, const char *filename, |
| 199 | char *const argv[], pid_t *pchild_pid, |
| 200 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd); |
| 201 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 202 | /* |
| 203 | * Run the specified command in the given minijail, execve(2)-style. |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 204 | * Update |*pchild_pid| with the pid of the child. |
| 205 | * Update |*pstdin_fd| with a fd that allows writing to the child's |
| 206 | * standard input. |
| 207 | * Update |*pstdout_fd| with a fd that allows reading from the child's |
| 208 | * standard output. |
| 209 | * Update |*pstderr_fd| with a fd that allows reading from the child's |
| 210 | * standard error. |
| 211 | * Used with static binaries, or on systems without support for LD_PRELOAD. |
| 212 | */ |
| 213 | int minijail_run_pid_pipes_no_preload(struct minijail *j, const char *filename, |
| 214 | char *const argv[], pid_t *pchild_pid, |
Jorge Lucangeli Obes | 43a6a86 | 2015-12-04 14:53:36 -0800 | [diff] [blame] | 215 | int *pstdin_fd, int *pstdout_fd, |
| 216 | int *pstderr_fd); |
Samuel Tan | 63187f4 | 2015-10-16 13:01:53 -0700 | [diff] [blame] | 217 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 218 | /* |
| 219 | * Kill the specified minijail. The minijail must have been created with pid |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 220 | * namespacing; if it was, all processes inside it are atomically killed. |
| 221 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 222 | int minijail_kill(struct minijail *j); |
| 223 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 224 | /* |
| 225 | * Wait for all processed in the specified minijail to exit. Returns the exit |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 226 | * status of the _first_ process spawned in the jail. |
| 227 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 228 | int minijail_wait(struct minijail *j); |
| 229 | |
Jorge Lucangeli Obes | d0a6e2f | 2015-11-24 14:21:21 -0800 | [diff] [blame] | 230 | /* |
| 231 | * Frees the given minijail. It does not matter if the process is inside the minijail or |
| 232 | * not. |
| 233 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 234 | void minijail_destroy(struct minijail *j); |
| 235 | |
| 236 | #ifdef __cplusplus |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 237 | }; /* extern "C" */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 238 | #endif |
| 239 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 240 | #endif /* !_LIBMINIJAIL_H_ */ |