1 /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. 2 * Use of this source code is governed by a BSD-style license that can be 3 * found in the LICENSE file. 4 */ 5 6 /* 7 * The general pattern of use here: 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 15 #ifndef _LIBMINIJAIL_H_ 16 #define _LIBMINIJAIL_H_ 17 18 #include <stdint.h> 19 #include <sys/types.h> 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 enum { 26 MINIJAIL_ERR_PRELOAD = 252, 27 MINIJAIL_ERR_JAIL = 253, 28 MINIJAIL_ERR_INIT = 254, 29 }; 30 31 struct minijail; 32 33 /* Allocates a new minijail with no restrictions. */ 34 struct minijail *minijail_new(void); 35 36 /* 37 * These functions add restrictions to the minijail. They are not applied until 38 * minijail_enter() is called. See the documentation in minijail0.1 for 39 * explanations in detail of what the restrictions do. 40 */ 41 void minijail_change_uid(struct minijail *j, uid_t uid); 42 void minijail_change_gid(struct minijail *j, gid_t gid); 43 /* Copies |list|. */ 44 void minijail_set_supplementary_gids(struct minijail *j, size_t size, 45 const gid_t *list); 46 /* Stores user to change to and copies |user| for internal consistency. */ 47 int minijail_change_user(struct minijail *j, const char *user); 48 /* Does not take ownership of |group|. */ 49 int minijail_change_group(struct minijail *j, const char *group); 50 void minijail_use_seccomp(struct minijail *j); 51 void minijail_no_new_privs(struct minijail *j); 52 void minijail_use_seccomp_filter(struct minijail *j); 53 void minijail_parse_seccomp_filters(struct minijail *j, const char *path); 54 void minijail_log_seccomp_filter_failures(struct minijail *j); 55 void minijail_use_caps(struct minijail *j, uint64_t capmask); 56 void minijail_reset_signal_mask(struct minijail *j); 57 void minijail_namespace_vfs(struct minijail *j); 58 void minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path); 59 void minijail_namespace_ipc(struct minijail *j); 60 void minijail_namespace_net(struct minijail *j); 61 void minijail_namespace_enter_net(struct minijail *j, const char *ns_path); 62 /* 63 * Implies namespace_vfs and remount_proc_readonly. 64 * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>. 65 */ 66 void minijail_namespace_pids(struct minijail *j); 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); 70 void minijail_remount_proc_readonly(struct minijail *j); 71 void minijail_run_as_init(struct minijail *j); 72 int minijail_write_pid_file(struct minijail *j, const char *path); 73 void minijail_inherit_usergroups(struct minijail *j); 74 void minijail_disable_ptrace(struct minijail *j); 75 /* 76 * Changes the jailed process's syscall table to the alt_syscall table 77 * named |table|. 78 */ 79 int minijail_use_alt_syscall(struct minijail *j, const char *table); 80 81 /* 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 /* 90 * minijail_enter_chroot: enables chroot() restriction for @j 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); 101 int minijail_enter_pivot_root(struct minijail *j, const char *dir); 102 103 /* 104 * minijail_get_original_path: returns the path of a given file outside of the 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 116 /* 117 * minijail_mount_tmp: enables mounting of a tmpfs filesystem on /tmp. 118 * As be rules of bind mounts, /tmp must exist in chroot. 119 */ 120 void minijail_mount_tmp(struct minijail *j); 121 122 /* 123 * minijail_mount: when entering minijail @j, mounts @src at @dst with @flags 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, 134 const char *type, unsigned long flags); 135 136 /* 137 * minijail_bind: bind-mounts @src into @j as @dest, optionally writeable 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, 147 int writeable); 148 149 /* 150 * Lock this process into the given minijail. Note that this procedure cannot fail, 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 159 /* 160 * Run the specified command in the given minijail, execve(2)-style. This is 161 * required if minijail_namespace_pids() was used. 162 */ 163 int minijail_run(struct minijail *j, const char *filename, 164 char *const argv[]); 165 166 /* 167 * Run the specified command in the given minijail, execve(2)-style. 168 * Used with static binaries, or on systems without support for LD_PRELOAD. 169 */ 170 int minijail_run_no_preload(struct minijail *j, const char *filename, 171 char *const argv[]); 172 173 /* 174 * Run the specified command in the given minijail, execve(2)-style. 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 180 /* 181 * Run the specified command in the given minijail, execve(2)-style. 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 188 /* 189 * Run the specified command in the given minijail, execve(2)-style. 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. 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 202 /* 203 * Run the specified command in the given minijail, execve(2)-style. 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, 215 int *pstdin_fd, int *pstdout_fd, 216 int *pstderr_fd); 217 218 /* 219 * Kill the specified minijail. The minijail must have been created with pid 220 * namespacing; if it was, all processes inside it are atomically killed. 221 */ 222 int minijail_kill(struct minijail *j); 223 224 /* 225 * Wait for all processed in the specified minijail to exit. Returns the exit 226 * status of the _first_ process spawned in the jail. 227 */ 228 int minijail_wait(struct minijail *j); 229 230 /* 231 * Frees the given minijail. It does not matter if the process is inside the minijail or 232 * not. 233 */ 234 void minijail_destroy(struct minijail *j); 235 236 #ifdef __cplusplus 237 }; /* extern "C" */ 238 #endif 239 240 #endif /* !_LIBMINIJAIL_H_ */ 241