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); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 46 | void minijail_use_seccomp_filter(struct minijail *j); |
Jorge Lucangeli Obes | 2343d83 | 2012-04-25 21:59:48 -0700 | [diff] [blame^] | 47 | void minijail_force_seccomp_filter(struct minijail *j); |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 48 | void minijail_parse_seccomp_filters(struct minijail *j, const char *path); |
| 49 | int minijail_add_seccomp_filter(struct minijail *j, int nr, |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 50 | const char *filter); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 51 | void minijail_use_caps(struct minijail *j, uint64_t capmask); |
| 52 | void minijail_namespace_vfs(struct minijail *j); |
Elly Jones | e58176c | 2012-01-23 11:46:17 -0500 | [diff] [blame] | 53 | /* Implies namespace_vfs and remount_readonly */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 54 | void minijail_namespace_pids(struct minijail *j); |
| 55 | void minijail_remount_readonly(struct minijail *j); |
| 56 | void minijail_inherit_usergroups(struct minijail *j); |
| 57 | void minijail_disable_ptrace(struct minijail *j); |
| 58 | |
Elly Jones | 51a5b6c | 2011-10-12 19:09:26 -0400 | [diff] [blame] | 59 | /* minijail_enter_chroot: enables chroot() restriction for @j |
| 60 | * @j minijail to apply restriction to |
| 61 | * @dir directory to chroot() to. Owned by caller. |
| 62 | * |
| 63 | * Enters @dir, binding all bind mounts specified with minijail_bind() into |
| 64 | * place. Requires @dir to contain all necessary directories for bind mounts |
| 65 | * (i.e., if you have requested a bind mount at /etc, /etc must exist in @dir.) |
| 66 | * |
| 67 | * Returns 0 on success. |
| 68 | */ |
| 69 | int minijail_enter_chroot(struct minijail *j, const char *dir); |
| 70 | |
| 71 | /* minijail_bind: bind-mounts @src into @j as @dest, optionally writeable |
| 72 | * @j minijail to bind inside |
| 73 | * @src source to bind |
| 74 | * @dest location to bind (inside chroot) |
| 75 | * @writeable 1 if the bind mount should be writeable |
| 76 | * |
| 77 | * This may be called multiple times; all bindings will be applied in the order |
| 78 | * of minijail_bind() calls. |
| 79 | */ |
| 80 | int minijail_bind(struct minijail *j, const char *src, const char *dest, |
| 81 | int writeable); |
| 82 | |
Will Drewry | 32ac9f5 | 2011-08-18 21:36:27 -0500 | [diff] [blame] | 83 | /* Exposes minijail's name-to-int mapping for system calls for the |
| 84 | * architecture it was built on. This is primarily exposed for |
| 85 | * minijail_add_seccomp_filter() and testing. |
| 86 | * Returns the system call number on success or -1 on failure. |
| 87 | */ |
| 88 | int minijail_lookup_syscall(const char *name); |
| 89 | |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 90 | /* Lock this process into the given minijail. Note that this procedure cannot fail, |
| 91 | * since there is no way to undo privilege-dropping; therefore, if any part of |
| 92 | * the privilege-drop fails, minijail_enter() will abort the entire process. |
| 93 | * |
| 94 | * Some restrictions cannot be enabled this way (pid namespaces) and attempting |
| 95 | * to do so will cause an abort. |
| 96 | */ |
| 97 | void minijail_enter(const struct minijail *j); |
| 98 | |
| 99 | /* 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] | 100 | * required if minijail_namespace_pids() was used. |
| 101 | */ |
| 102 | int minijail_run(struct minijail *j, const char *filename, |
| 103 | char *const argv[]); |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 104 | |
Jorge Lucangeli Obes | 9807d03 | 2012-04-17 13:36:00 -0700 | [diff] [blame] | 105 | /* Run the specified command in the given minijail, execve(3)-style. |
| 106 | * Update |*pchild_pid| with the pid of the child. |
| 107 | */ |
| 108 | int minijail_run_pid(struct minijail *j, const char *filename, |
| 109 | char *const argv[], pid_t *pchild_pid); |
| 110 | |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 111 | /* Kill the specified minijail. The minijail must have been created with pid |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 112 | * namespacing; if it was, all processes inside it are atomically killed. |
| 113 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 114 | int minijail_kill(struct minijail *j); |
| 115 | |
| 116 | /* 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] | 117 | * status of the _first_ process spawned in the jail. |
| 118 | */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 119 | int minijail_wait(struct minijail *j); |
| 120 | |
| 121 | /* Frees the given minijail. It does not matter if the process is inside the minijail or |
| 122 | * not. */ |
| 123 | void minijail_destroy(struct minijail *j); |
| 124 | |
| 125 | #ifdef __cplusplus |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 126 | }; /* extern "C" */ |
Elly Jones | cd7a904 | 2011-07-22 13:56:51 -0400 | [diff] [blame] | 127 | #endif |
| 128 | |
Elly Jones | e1749eb | 2011-10-07 13:54:59 -0400 | [diff] [blame] | 129 | #endif /* !_LIBMINIJAIL_H_ */ |