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 | |
| 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 Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 14 | #ifndef _LIBMINIJAIL_H_ |
| 15 | #define _LIBMINIJAIL_H_ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 16 | |
| 17 | #include <stdint.h> |
| 18 | #include <sys/types.h> |
| 19 | |
| 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | enum { |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 25 | MINIJAIL_ERR_PRELOAD = 252, |
| 26 | MINIJAIL_ERR_JAIL = 253, |
| 27 | MINIJAIL_ERR_INIT = 254, |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | struct minijail; |
| 31 | |
| 32 | /* Allocates a new minijail with no restrictions. */ |
| 33 | struct 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 Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 37 | * explanations in detail of what the restrictions do. |
| 38 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 39 | void minijail_change_uid(struct minijail *j, uid_t uid); |
| 40 | void minijail_change_gid(struct minijail *j, gid_t gid); |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 41 | /* Stores user to change to and copies |user| for internal consistency. */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 42 | int minijail_change_user(struct minijail *j, const char *user); |
Will Drewry | 2ddaad0 | 2011-09-16 11:36:08 -0500 | [diff] [blame] | 43 | /* Does not take ownership of |group|. */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 44 | int minijail_change_group(struct minijail *j, const char *group); |
| 45 | void minijail_use_seccomp(struct minijail *j); |
Jorge Lucangeli Obes | c2c9bcc | 2012-05-01 09:30:24 -0700 | [diff] [blame] | 46 | void minijail_no_new_privs(struct minijail *j); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 47 | void minijail_use_seccomp_filter(struct minijail *j); |
| 48 | void minijail_parse_seccomp_filters(struct minijail *j, const char *path); |
Jorge Lucangeli Obes | bda833c | 2012-07-31 16:25:56 -0700 | [diff] [blame] | 49 | void minijail_log_seccomp_filter_failures(struct minijail *j); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 50 | void minijail_use_caps(struct minijail *j, uint64_t capmask); |
| 51 | void minijail_namespace_vfs(struct minijail *j); |
Elly Fong-Jones | 6c08630 | 2013-03-20 17:15:28 -0400 | [diff] [blame] | 52 | void minijail_namespace_net(struct minijail *j); |
Elly Jones | 761b741 | 2012-06-13 15:49:52 -0400 | [diff] [blame] | 53 | /* Implies namespace_vfs and remount_readonly. |
| 54 | * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>. |
| 55 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 56 | void minijail_namespace_pids(struct minijail *j); |
| 57 | void minijail_remount_readonly(struct minijail *j); |
| 58 | void minijail_inherit_usergroups(struct minijail *j); |
| 59 | void minijail_disable_ptrace(struct minijail *j); |
| 60 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 61 | /* minijail_enter_chroot: enables chroot() restriction for @j |
| 62 | * @j minijail to apply restriction to |
| 63 | * @dir directory to chroot() to. Owned by caller. |
| 64 | * |
| 65 | * Enters @dir, binding all bind mounts specified with minijail_bind() into |
| 66 | * place. Requires @dir to contain all necessary directories for bind mounts |
| 67 | * (i.e., if you have requested a bind mount at /etc, /etc must exist in @dir.) |
| 68 | * |
| 69 | * Returns 0 on success. |
| 70 | */ |
| 71 | int minijail_enter_chroot(struct minijail *j, const char *dir); |
| 72 | |
| 73 | /* minijail_bind: bind-mounts @src into @j as @dest, optionally writeable |
| 74 | * @j minijail to bind inside |
| 75 | * @src source to bind |
| 76 | * @dest location to bind (inside chroot) |
| 77 | * @writeable 1 if the bind mount should be writeable |
| 78 | * |
| 79 | * This may be called multiple times; all bindings will be applied in the order |
| 80 | * of minijail_bind() calls. |
| 81 | */ |
| 82 | int minijail_bind(struct minijail *j, const char *src, const char *dest, |
| 83 | int writeable); |
| 84 | |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 85 | /* Lock this process into the given minijail. Note that this procedure cannot fail, |
| 86 | * since there is no way to undo privilege-dropping; therefore, if any part of |
| 87 | * the privilege-drop fails, minijail_enter() will abort the entire process. |
| 88 | * |
| 89 | * Some restrictions cannot be enabled this way (pid namespaces) and attempting |
| 90 | * to do so will cause an abort. |
| 91 | */ |
| 92 | void minijail_enter(const struct minijail *j); |
| 93 | |
| 94 | /* Run the specified command in the given minijail, execve(3)-style. This is |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 95 | * required if minijail_namespace_pids() was used. |
| 96 | */ |
| 97 | int minijail_run(struct minijail *j, const char *filename, |
| 98 | char *const argv[]); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 99 | |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 100 | /* Run the specified command in the given minijail, execve(3)-style. |
| 101 | * Update |*pchild_pid| with the pid of the child. |
| 102 | */ |
| 103 | int minijail_run_pid(struct minijail *j, const char *filename, |
| 104 | char *const argv[], pid_t *pchild_pid); |
| 105 | |
Jorge Lucangeli Obes | df4bd35 | 2012-08-29 19:12:28 -0700 | [diff] [blame] | 106 | /* Run the specified command in the given minijail, execve(3)-style. |
| 107 | * Update |*pstdin_fd| with a fd that allows writing to the child's |
| 108 | * standard input. |
| 109 | */ |
| 110 | int minijail_run_pipe(struct minijail *j, const char *filename, |
| 111 | char *const argv[], int *pstdin_fd); |
| 112 | |
| 113 | /* Run the specified command in the given minijail, execve(3)-style. |
| 114 | * Update |*pchild_pid| with the pid of the child. |
| 115 | * Update |*pstdin_fd| with a fd that allows writing to the child's |
| 116 | * standard input. |
| 117 | */ |
| 118 | int minijail_run_pid_pipe(struct minijail *j, const char *filename, |
| 119 | char *const argv[], pid_t *pchild_pid, |
| 120 | int *pstdin_fd); |
| 121 | |
Jorge Lucangeli Obes | 339a113 | 2013-02-15 16:53:47 -0800 | [diff] [blame] | 122 | /* Run the specified command in the given minijail, execve(3)-style. |
| 123 | * Update |*pchild_pid| with the pid of the child. |
| 124 | * Update |*pstdin_fd| with a fd that allows writing to the child's |
| 125 | * standard input. |
| 126 | * Update |*pstdout_fd| with a fd that allows reading from the child's |
| 127 | * standard output. |
| 128 | * Update |*pstderr_fd| with a fd that allows reading from the child's |
| 129 | * standard error. |
| 130 | */ |
| 131 | int minijail_run_pid_pipes(struct minijail *j, const char *filename, |
| 132 | char *const argv[], pid_t *pchild_pid, |
| 133 | int *pstdin_fd, int *pstdout_fd, int *pstderr_fd); |
| 134 | |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 135 | /* Kill the specified minijail. The minijail must have been created with pid |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 136 | * namespacing; if it was, all processes inside it are atomically killed. |
| 137 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 138 | int minijail_kill(struct minijail *j); |
| 139 | |
| 140 | /* 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] | 141 | * status of the _first_ process spawned in the jail. |
| 142 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 143 | int minijail_wait(struct minijail *j); |
| 144 | |
| 145 | /* Frees the given minijail. It does not matter if the process is inside the minijail or |
| 146 | * not. */ |
| 147 | void minijail_destroy(struct minijail *j); |
| 148 | |
| 149 | #ifdef __cplusplus |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 150 | }; /* extern "C" */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 151 | #endif |
| 152 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 153 | #endif /* !_LIBMINIJAIL_H_ */ |