1*4b9c6d91SCole Faust /* Copyright 2012 The ChromiumOS Authors 2*4b9c6d91SCole Faust * Use of this source code is governed by a BSD-style license that can be 3*4b9c6d91SCole Faust * found in the LICENSE file. 4*4b9c6d91SCole Faust */ 5*4b9c6d91SCole Faust 6*4b9c6d91SCole Faust /* 7*4b9c6d91SCole Faust * The general pattern of use here: 8*4b9c6d91SCole Faust * 1) Construct a minijail with minijail_new() 9*4b9c6d91SCole Faust * 2) Apply the desired restrictions to it 10*4b9c6d91SCole Faust * 3) Enter it, which locks the current process inside it, or: 11*4b9c6d91SCole Faust * 3) Run a process inside it 12*4b9c6d91SCole Faust * 4) Destroy it. 13*4b9c6d91SCole Faust */ 14*4b9c6d91SCole Faust 15*4b9c6d91SCole Faust #ifndef _LIBMINIJAIL_H_ 16*4b9c6d91SCole Faust #define _LIBMINIJAIL_H_ 17*4b9c6d91SCole Faust 18*4b9c6d91SCole Faust #include <stdint.h> 19*4b9c6d91SCole Faust #include <sys/resource.h> 20*4b9c6d91SCole Faust #include <sys/types.h> 21*4b9c6d91SCole Faust 22*4b9c6d91SCole Faust /* 23*4b9c6d91SCole Faust * Rust's bindgen needs the actual definition of sock_fprog in order to 24*4b9c6d91SCole Faust * generate usable bindings. 25*4b9c6d91SCole Faust */ 26*4b9c6d91SCole Faust #ifdef USE_BINDGEN 27*4b9c6d91SCole Faust #include <linux/filter.h> 28*4b9c6d91SCole Faust #endif 29*4b9c6d91SCole Faust 30*4b9c6d91SCole Faust #ifdef __cplusplus 31*4b9c6d91SCole Faust extern "C" { 32*4b9c6d91SCole Faust #endif 33*4b9c6d91SCole Faust 34*4b9c6d91SCole Faust /* Possible exit status codes returned by minijail_wait(). */ 35*4b9c6d91SCole Faust enum { 36*4b9c6d91SCole Faust /* Command can be found but cannot be run */ 37*4b9c6d91SCole Faust MINIJAIL_ERR_NO_ACCESS = 126, 38*4b9c6d91SCole Faust 39*4b9c6d91SCole Faust /* Command cannot be found */ 40*4b9c6d91SCole Faust MINIJAIL_ERR_NO_COMMAND = 127, 41*4b9c6d91SCole Faust 42*4b9c6d91SCole Faust /* (MINIJAIL_ERR_SIG_BASE + n) if process killed by signal n != SIGSYS */ 43*4b9c6d91SCole Faust MINIJAIL_ERR_SIG_BASE = 128, 44*4b9c6d91SCole Faust 45*4b9c6d91SCole Faust /* Cannot mount a file or folder in mount namespace */ 46*4b9c6d91SCole Faust MINIJAIL_ERR_MOUNT = 251, 47*4b9c6d91SCole Faust 48*4b9c6d91SCole Faust MINIJAIL_ERR_PRELOAD = 252, 49*4b9c6d91SCole Faust 50*4b9c6d91SCole Faust /* Process killed by SIGSYS */ 51*4b9c6d91SCole Faust MINIJAIL_ERR_JAIL = 253, 52*4b9c6d91SCole Faust 53*4b9c6d91SCole Faust MINIJAIL_ERR_INIT = 254, 54*4b9c6d91SCole Faust }; 55*4b9c6d91SCole Faust 56*4b9c6d91SCole Faust struct minijail; 57*4b9c6d91SCole Faust struct sock_fprog; 58*4b9c6d91SCole Faust 59*4b9c6d91SCole Faust /* 60*4b9c6d91SCole Faust * A hook that can be used to execute code at various events during minijail 61*4b9c6d91SCole Faust * setup in the forked process. These can only be used if the jailed process is 62*4b9c6d91SCole Faust * not going to be invoked with LD_PRELOAD. 63*4b9c6d91SCole Faust * 64*4b9c6d91SCole Faust * If the return value is non-zero, it will be interpreted as -errno and the 65*4b9c6d91SCole Faust * process will abort. 66*4b9c6d91SCole Faust */ 67*4b9c6d91SCole Faust typedef int (*minijail_hook_t)(void *context); 68*4b9c6d91SCole Faust 69*4b9c6d91SCole Faust /* 70*4b9c6d91SCole Faust * The events during minijail setup in which hooks can run. All the events are 71*4b9c6d91SCole Faust * run in the new process. 72*4b9c6d91SCole Faust */ 73*4b9c6d91SCole Faust typedef enum { 74*4b9c6d91SCole Faust /* The hook will run just before dropping capabilities. */ 75*4b9c6d91SCole Faust MINIJAIL_HOOK_EVENT_PRE_DROP_CAPS, 76*4b9c6d91SCole Faust 77*4b9c6d91SCole Faust /* The hook will run just before calling execve(2). */ 78*4b9c6d91SCole Faust MINIJAIL_HOOK_EVENT_PRE_EXECVE, 79*4b9c6d91SCole Faust 80*4b9c6d91SCole Faust /* The hook will run just before calling chroot(2) / pivot_root(2). */ 81*4b9c6d91SCole Faust MINIJAIL_HOOK_EVENT_PRE_CHROOT, 82*4b9c6d91SCole Faust 83*4b9c6d91SCole Faust /* Sentinel for error checking. Must be last. */ 84*4b9c6d91SCole Faust MINIJAIL_HOOK_EVENT_MAX, 85*4b9c6d91SCole Faust } minijail_hook_event_t; 86*4b9c6d91SCole Faust 87*4b9c6d91SCole Faust /* Allocates a new minijail with no restrictions. */ 88*4b9c6d91SCole Faust struct minijail *minijail_new(void); 89*4b9c6d91SCole Faust 90*4b9c6d91SCole Faust /* 91*4b9c6d91SCole Faust * These functions add restrictions to the minijail. They are not applied until 92*4b9c6d91SCole Faust * minijail_enter() is called. See the documentation in minijail0.1 for 93*4b9c6d91SCole Faust * explanations in detail of what the restrictions do. 94*4b9c6d91SCole Faust */ 95*4b9c6d91SCole Faust void minijail_change_uid(struct minijail *j, uid_t uid); 96*4b9c6d91SCole Faust void minijail_change_gid(struct minijail *j, gid_t gid); 97*4b9c6d91SCole Faust /* Copies |list|. */ 98*4b9c6d91SCole Faust void minijail_set_supplementary_gids(struct minijail *j, size_t size, 99*4b9c6d91SCole Faust const gid_t *list); 100*4b9c6d91SCole Faust void minijail_keep_supplementary_gids(struct minijail *j); 101*4b9c6d91SCole Faust /* Stores user to change to and copies |user| for internal consistency. */ 102*4b9c6d91SCole Faust int minijail_change_user(struct minijail *j, const char *user); 103*4b9c6d91SCole Faust /* Does not take ownership of |group|. */ 104*4b9c6d91SCole Faust int minijail_change_group(struct minijail *j, const char *group); 105*4b9c6d91SCole Faust void minijail_use_seccomp(struct minijail *j); 106*4b9c6d91SCole Faust void minijail_no_new_privs(struct minijail *j); 107*4b9c6d91SCole Faust void minijail_use_seccomp_filter(struct minijail *j); 108*4b9c6d91SCole Faust void minijail_set_seccomp_filter_tsync(struct minijail *j); 109*4b9c6d91SCole Faust /* Sets using_minimalistic_mountns to true. */ 110*4b9c6d91SCole Faust void minijail_set_using_minimalistic_mountns(struct minijail *j); 111*4b9c6d91SCole Faust void minijail_add_minimalistic_mountns_fs_rules(struct minijail *j); 112*4b9c6d91SCole Faust void minijail_enable_default_fs_restrictions(struct minijail *j); 113*4b9c6d91SCole Faust /* 114*4b9c6d91SCole Faust * Allow speculative execution features that may cause data leaks across 115*4b9c6d91SCole Faust * processes, by setting the SECCOMP_FILTER_FLAG_SPEC_ALLOW seccomp flag. 116*4b9c6d91SCole Faust * 117*4b9c6d91SCole Faust * WARNING: Enabling this may make the process vulnerable to speculative 118*4b9c6d91SCole Faust * execution attacks (Branch Target Injection, and Speculative Store Bypass). 119*4b9c6d91SCole Faust * This is only safe to use for processes that do not execute untrusted code. 120*4b9c6d91SCole Faust */ 121*4b9c6d91SCole Faust void minijail_set_seccomp_filter_allow_speculation(struct minijail *j); 122*4b9c6d91SCole Faust /* Does not take ownership of |filter|. */ 123*4b9c6d91SCole Faust void minijail_set_seccomp_filters(struct minijail *j, 124*4b9c6d91SCole Faust const struct sock_fprog *filter); 125*4b9c6d91SCole Faust void minijail_parse_seccomp_filters(struct minijail *j, const char *path); 126*4b9c6d91SCole Faust void minijail_parse_seccomp_filters_from_fd(struct minijail *j, int fd); 127*4b9c6d91SCole Faust void minijail_log_seccomp_filter_failures(struct minijail *j); 128*4b9c6d91SCole Faust /* 'minijail_use_caps' and 'minijail_capbset_drop' are mutually exclusive. */ 129*4b9c6d91SCole Faust void minijail_use_caps(struct minijail *j, uint64_t capmask); 130*4b9c6d91SCole Faust void minijail_capbset_drop(struct minijail *j, uint64_t capmask); 131*4b9c6d91SCole Faust /* 'minijail_set_ambient_caps' requires 'minijail_use_caps'. */ 132*4b9c6d91SCole Faust void minijail_set_ambient_caps(struct minijail *j); 133*4b9c6d91SCole Faust void minijail_reset_signal_mask(struct minijail *j); 134*4b9c6d91SCole Faust void minijail_reset_signal_handlers(struct minijail *j); 135*4b9c6d91SCole Faust void minijail_namespace_vfs(struct minijail *j); 136*4b9c6d91SCole Faust void minijail_namespace_enter_vfs(struct minijail *j, const char *ns_path); 137*4b9c6d91SCole Faust void minijail_new_session_keyring(struct minijail *j); 138*4b9c6d91SCole Faust void minijail_skip_setting_securebits(struct minijail *j, 139*4b9c6d91SCole Faust uint64_t securebits_skip_mask); 140*4b9c6d91SCole Faust 141*4b9c6d91SCole Faust /* 142*4b9c6d91SCole Faust * This option is *dangerous* as it negates most of the functionality of 143*4b9c6d91SCole Faust * minijail_namespace_vfs(). You very likely don't need this. 144*4b9c6d91SCole Faust */ 145*4b9c6d91SCole Faust void minijail_skip_remount_private(struct minijail *j); 146*4b9c6d91SCole Faust void minijail_remount_mode(struct minijail *j, unsigned long mode); 147*4b9c6d91SCole Faust void minijail_namespace_ipc(struct minijail *j); 148*4b9c6d91SCole Faust void minijail_namespace_uts(struct minijail *j); 149*4b9c6d91SCole Faust int minijail_namespace_set_hostname(struct minijail *j, const char *name); 150*4b9c6d91SCole Faust void minijail_namespace_net(struct minijail *j); 151*4b9c6d91SCole Faust void minijail_namespace_enter_net(struct minijail *j, const char *ns_path); 152*4b9c6d91SCole Faust void minijail_namespace_cgroups(struct minijail *j); 153*4b9c6d91SCole Faust /* Closes all open file descriptors after forking. */ 154*4b9c6d91SCole Faust void minijail_close_open_fds(struct minijail *j); 155*4b9c6d91SCole Faust /* 156*4b9c6d91SCole Faust * Implies namespace_vfs and remount_proc_readonly. 157*4b9c6d91SCole Faust * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>. 158*4b9c6d91SCole Faust */ 159*4b9c6d91SCole Faust void minijail_namespace_pids(struct minijail *j); 160*4b9c6d91SCole Faust /* 161*4b9c6d91SCole Faust * Implies namespace_vfs. 162*4b9c6d91SCole Faust * WARNING: this is NOT THREAD SAFE. See the block comment in </libminijail.c>. 163*4b9c6d91SCole Faust * Minijail will by default remount /proc read-only when using a PID namespace. 164*4b9c6d91SCole Faust * Certain complex applications expect to be able to do their own sandboxing 165*4b9c6d91SCole Faust * which might require writing to /proc, so support a weaker version of PID 166*4b9c6d91SCole Faust * namespacing with a RW /proc. 167*4b9c6d91SCole Faust */ 168*4b9c6d91SCole Faust void minijail_namespace_pids_rw_proc(struct minijail *j); 169*4b9c6d91SCole Faust void minijail_namespace_user(struct minijail *j); 170*4b9c6d91SCole Faust void minijail_namespace_user_disable_setgroups(struct minijail *j); 171*4b9c6d91SCole Faust int minijail_uidmap(struct minijail *j, const char *uidmap); 172*4b9c6d91SCole Faust int minijail_gidmap(struct minijail *j, const char *gidmap); 173*4b9c6d91SCole Faust void minijail_remount_proc_readonly(struct minijail *j); 174*4b9c6d91SCole Faust void minijail_run_as_init(struct minijail *j); 175*4b9c6d91SCole Faust int minijail_write_pid_file(struct minijail *j, const char *path); 176*4b9c6d91SCole Faust void minijail_inherit_usergroups(struct minijail *j); 177*4b9c6d91SCole Faust /* 178*4b9c6d91SCole Faust * Changes the jailed process's syscall table to the alt_syscall table 179*4b9c6d91SCole Faust * named |table|. 180*4b9c6d91SCole Faust */ 181*4b9c6d91SCole Faust int minijail_use_alt_syscall(struct minijail *j, const char *table); 182*4b9c6d91SCole Faust 183*4b9c6d91SCole Faust /* Sets the given runtime limit. See getrlimit(2). */ 184*4b9c6d91SCole Faust int minijail_rlimit(struct minijail *j, int type, rlim_t cur, rlim_t max); 185*4b9c6d91SCole Faust 186*4b9c6d91SCole Faust /* 187*4b9c6d91SCole Faust * Adds the jailed process to the cgroup given by |path|. |path| should be the 188*4b9c6d91SCole Faust * full path to the cgroups "tasks" file. 189*4b9c6d91SCole Faust * Example: /sys/fs/cgroup/cpu/jailed_procs/tasks adds to the "jailed_procs" cpu 190*4b9c6d91SCole Faust * cgroup. 191*4b9c6d91SCole Faust */ 192*4b9c6d91SCole Faust int minijail_add_to_cgroup(struct minijail *j, const char *path); 193*4b9c6d91SCole Faust 194*4b9c6d91SCole Faust /* 195*4b9c6d91SCole Faust * These functions are used for filesystem restrictions. 196*4b9c6d91SCole Faust */ 197*4b9c6d91SCole Faust 198*4b9c6d91SCole Faust /* Adds a read-execute path. */ 199*4b9c6d91SCole Faust int minijail_add_fs_restriction_rx(struct minijail *j, const char *path); 200*4b9c6d91SCole Faust 201*4b9c6d91SCole Faust /* Adds a read-only path. */ 202*4b9c6d91SCole Faust int minijail_add_fs_restriction_ro(struct minijail *j, const char *path); 203*4b9c6d91SCole Faust 204*4b9c6d91SCole Faust /* Adds a path with read and basic write permissions. */ 205*4b9c6d91SCole Faust int minijail_add_fs_restriction_rw(struct minijail *j, const char *path); 206*4b9c6d91SCole Faust 207*4b9c6d91SCole Faust /* Adds a path with read and advanced write permissions. */ 208*4b9c6d91SCole Faust int minijail_add_fs_restriction_advanced_rw(struct minijail *j, 209*4b9c6d91SCole Faust const char *path); 210*4b9c6d91SCole Faust 211*4b9c6d91SCole Faust /* Adds a path with read and write permissions that exclude create. */ 212*4b9c6d91SCole Faust int minijail_add_fs_restriction_edit(struct minijail *j, const char *path); 213*4b9c6d91SCole Faust 214*4b9c6d91SCole Faust /* 215*4b9c6d91SCole Faust * Install signal handlers in the minijail process that forward received 216*4b9c6d91SCole Faust * signals to the jailed child process. 217*4b9c6d91SCole Faust */ 218*4b9c6d91SCole Faust int minijail_forward_signals(struct minijail *j); 219*4b9c6d91SCole Faust 220*4b9c6d91SCole Faust /* The jailed child process should call setsid() to create a new session. */ 221*4b9c6d91SCole Faust int minijail_create_session(struct minijail *j); 222*4b9c6d91SCole Faust 223*4b9c6d91SCole Faust /* 224*4b9c6d91SCole Faust * minijail_enter_chroot: enables chroot() restriction for @j 225*4b9c6d91SCole Faust * @j minijail to apply restriction to 226*4b9c6d91SCole Faust * @dir directory to chroot() to. Owned by caller. 227*4b9c6d91SCole Faust * 228*4b9c6d91SCole Faust * Enters @dir, binding all bind mounts specified with minijail_bind() into 229*4b9c6d91SCole Faust * place. Requires @dir to contain all necessary directories for bind mounts 230*4b9c6d91SCole Faust * (i.e., if you have requested a bind mount at /etc, /etc must exist in @dir.) 231*4b9c6d91SCole Faust * 232*4b9c6d91SCole Faust * Returns 0 on success. 233*4b9c6d91SCole Faust */ 234*4b9c6d91SCole Faust int minijail_enter_chroot(struct minijail *j, const char *dir); 235*4b9c6d91SCole Faust int minijail_enter_pivot_root(struct minijail *j, const char *dir); 236*4b9c6d91SCole Faust 237*4b9c6d91SCole Faust /* 238*4b9c6d91SCole Faust * minijail_get_original_path: returns the path of a given file outside of the 239*4b9c6d91SCole Faust * chroot. 240*4b9c6d91SCole Faust * @j minijail to obtain the path from. 241*4b9c6d91SCole Faust * @chroot_path path inside of the chroot() to. 242*4b9c6d91SCole Faust * 243*4b9c6d91SCole Faust * When executing a binary in a chroot or pivot_root, return path to the binary 244*4b9c6d91SCole Faust * outside of the chroot. 245*4b9c6d91SCole Faust * 246*4b9c6d91SCole Faust * Returns a string containing the path. This must be freed by the caller. 247*4b9c6d91SCole Faust */ 248*4b9c6d91SCole Faust char *minijail_get_original_path(struct minijail *j, const char *chroot_path); 249*4b9c6d91SCole Faust 250*4b9c6d91SCole Faust /* 251*4b9c6d91SCole Faust * minijail_mount_tmp: enables mounting of a 64M tmpfs filesystem on /tmp. 252*4b9c6d91SCole Faust * As be rules of bind mounts, /tmp must exist in chroot. 253*4b9c6d91SCole Faust */ 254*4b9c6d91SCole Faust void minijail_mount_tmp(struct minijail *j); 255*4b9c6d91SCole Faust 256*4b9c6d91SCole Faust /* 257*4b9c6d91SCole Faust * minijail_mount_tmp_size: enables mounting of a tmpfs filesystem on /tmp. 258*4b9c6d91SCole Faust * As be rules of bind mounts, /tmp must exist in chroot. Size is in bytes. 259*4b9c6d91SCole Faust */ 260*4b9c6d91SCole Faust void minijail_mount_tmp_size(struct minijail *j, size_t size); 261*4b9c6d91SCole Faust 262*4b9c6d91SCole Faust /* 263*4b9c6d91SCole Faust * minijail_mount_dev: enables mounting of a tmpfs filesystem on /dev. 264*4b9c6d91SCole Faust * It will then be seeded with a basic set of device nodes. For the exact 265*4b9c6d91SCole Faust * list, consult the minijail(0) man page. 266*4b9c6d91SCole Faust */ 267*4b9c6d91SCole Faust void minijail_mount_dev(struct minijail *j); 268*4b9c6d91SCole Faust 269*4b9c6d91SCole Faust /* 270*4b9c6d91SCole Faust * minijail_mount_with_data: when entering minijail @j, 271*4b9c6d91SCole Faust * mounts @src at @dst with @flags and @data. 272*4b9c6d91SCole Faust * @j minijail to bind inside 273*4b9c6d91SCole Faust * @src source to bind 274*4b9c6d91SCole Faust * @dest location to bind (inside chroot) 275*4b9c6d91SCole Faust * @type type of filesystem 276*4b9c6d91SCole Faust * @flags flags passed to mount 277*4b9c6d91SCole Faust * @data data arguments passed to mount(2), e.g. "mode=755" 278*4b9c6d91SCole Faust * 279*4b9c6d91SCole Faust * This may be called multiple times; all mounts will be applied in the order 280*4b9c6d91SCole Faust * of minijail_mount() calls. 281*4b9c6d91SCole Faust * If @flags is 0, then MS_NODEV | MS_NOEXEC | MS_NOSUID will be used instead. 282*4b9c6d91SCole Faust * If @data is NULL or "", and @type is tmpfs, then "mode=0755,size=10M" will 283*4b9c6d91SCole Faust * be used instead. 284*4b9c6d91SCole Faust */ 285*4b9c6d91SCole Faust int minijail_mount_with_data(struct minijail *j, const char *src, 286*4b9c6d91SCole Faust const char *dest, const char *type, 287*4b9c6d91SCole Faust unsigned long flags, const char *data); 288*4b9c6d91SCole Faust 289*4b9c6d91SCole Faust /* 290*4b9c6d91SCole Faust * minijail_mount: when entering minijail @j, mounts @src at @dst with @flags 291*4b9c6d91SCole Faust * @j minijail to bind inside 292*4b9c6d91SCole Faust * @src source to bind 293*4b9c6d91SCole Faust * @dest location to bind (inside chroot) 294*4b9c6d91SCole Faust * @type type of filesystem 295*4b9c6d91SCole Faust * @flags flags passed to mount 296*4b9c6d91SCole Faust * 297*4b9c6d91SCole Faust * This may be called multiple times; all mounts will be applied in the order 298*4b9c6d91SCole Faust * of minijail_mount() calls. 299*4b9c6d91SCole Faust */ 300*4b9c6d91SCole Faust int minijail_mount(struct minijail *j, const char *src, const char *dest, 301*4b9c6d91SCole Faust const char *type, unsigned long flags); 302*4b9c6d91SCole Faust 303*4b9c6d91SCole Faust /* 304*4b9c6d91SCole Faust * minijail_bind: bind-mounts @src into @j as @dest, optionally writeable 305*4b9c6d91SCole Faust * @j minijail to bind inside 306*4b9c6d91SCole Faust * @src source to bind 307*4b9c6d91SCole Faust * @dest location to bind (inside chroot) 308*4b9c6d91SCole Faust * @writeable 1 if the bind mount should be writeable 309*4b9c6d91SCole Faust * 310*4b9c6d91SCole Faust * This may be called multiple times; all bindings will be applied in the order 311*4b9c6d91SCole Faust * of minijail_bind() calls. 312*4b9c6d91SCole Faust */ 313*4b9c6d91SCole Faust int minijail_bind(struct minijail *j, const char *src, const char *dest, 314*4b9c6d91SCole Faust int writeable); 315*4b9c6d91SCole Faust 316*4b9c6d91SCole Faust /* 317*4b9c6d91SCole Faust * minijail_add_remount: when entering minijail @j, remounts @mount_name and all 318*4b9c6d91SCole Faust * subdirectories as @remount_mode rather than the default MS_PRIVATE 319*4b9c6d91SCole Faust * @j minijail to bind inside 320*4b9c6d91SCole Faust * @mount_name mount to remount 321*4b9c6d91SCole Faust * @remount_mode remount mode to use 322*4b9c6d91SCole Faust * 323*4b9c6d91SCole Faust * This may be called multiple times; this overrides |j->remount_mode| for the 324*4b9c6d91SCole Faust * given mount. 325*4b9c6d91SCole Faust */ 326*4b9c6d91SCole Faust int minijail_add_remount(struct minijail *j, const char *mount_name, 327*4b9c6d91SCole Faust unsigned long remount_mode); 328*4b9c6d91SCole Faust /* 329*4b9c6d91SCole Faust * minijail_add_hook: adds @hook to the list of hooks that will be 330*4b9c6d91SCole Faust * invoked when @event is reached during minijail setup. The caller is 331*4b9c6d91SCole Faust * responsible for the lifetime of @payload. 332*4b9c6d91SCole Faust * @j minijail to add the hook to 333*4b9c6d91SCole Faust * @hook the function that will be invoked 334*4b9c6d91SCole Faust * @payload an opaque pointer 335*4b9c6d91SCole Faust * @event the event that will trigger the hook 336*4b9c6d91SCole Faust */ 337*4b9c6d91SCole Faust int minijail_add_hook(struct minijail *j, 338*4b9c6d91SCole Faust minijail_hook_t hook, void *payload, 339*4b9c6d91SCole Faust minijail_hook_event_t event); 340*4b9c6d91SCole Faust 341*4b9c6d91SCole Faust /* 342*4b9c6d91SCole Faust * minijail_preserve_fd: preserves @parent_fd and makes it available as 343*4b9c6d91SCole Faust * @child_fd in the child process. @parent_fd will be closed if no other 344*4b9c6d91SCole Faust * redirect has claimed it as a @child_fd. This works even if 345*4b9c6d91SCole Faust * minijail_close_open_fds() is invoked. 346*4b9c6d91SCole Faust * @j minijail to add the fd to 347*4b9c6d91SCole Faust * @parent_fd the fd in the parent process 348*4b9c6d91SCole Faust * @child_fd the fd that will be available in the child process 349*4b9c6d91SCole Faust */ 350*4b9c6d91SCole Faust int minijail_preserve_fd(struct minijail *j, int parent_fd, int child_fd); 351*4b9c6d91SCole Faust 352*4b9c6d91SCole Faust /* 353*4b9c6d91SCole Faust * minijail_set_preload_path: overrides the default path for 354*4b9c6d91SCole Faust * libminijailpreload.so. 355*4b9c6d91SCole Faust */ 356*4b9c6d91SCole Faust int minijail_set_preload_path(struct minijail *j, const char *preload_path); 357*4b9c6d91SCole Faust 358*4b9c6d91SCole Faust /* 359*4b9c6d91SCole Faust * Lock this process into the given minijail. Note that this procedure cannot 360*4b9c6d91SCole Faust * fail, since there is no way to undo privilege-dropping; therefore, if any 361*4b9c6d91SCole Faust * part of the privilege-drop fails, minijail_enter() will abort the entire 362*4b9c6d91SCole Faust * process. 363*4b9c6d91SCole Faust * 364*4b9c6d91SCole Faust * Some restrictions cannot be enabled this way (pid namespaces) and attempting 365*4b9c6d91SCole Faust * to do so will cause an abort. 366*4b9c6d91SCole Faust */ 367*4b9c6d91SCole Faust void minijail_enter(const struct minijail *j); 368*4b9c6d91SCole Faust 369*4b9c6d91SCole Faust /* 370*4b9c6d91SCole Faust * Run the specified command in the given minijail, execve(2)-style. 371*4b9c6d91SCole Faust * Pass |envp| as the full environment for the child. 372*4b9c6d91SCole Faust */ 373*4b9c6d91SCole Faust int minijail_run_env(struct minijail *j, const char *filename, 374*4b9c6d91SCole Faust char *const argv[], char *const envp[]); 375*4b9c6d91SCole Faust 376*4b9c6d91SCole Faust /* 377*4b9c6d91SCole Faust * Run the specified command in the given minijail, execve(2)-style. 378*4b9c6d91SCole Faust * If minijail_namespace_pids() or minijail_namespace_user() are used, 379*4b9c6d91SCole Faust * this or minijail_fork() is required instead of minijail_enter(). 380*4b9c6d91SCole Faust */ 381*4b9c6d91SCole Faust int minijail_run(struct minijail *j, const char *filename, 382*4b9c6d91SCole Faust char *const argv[]); 383*4b9c6d91SCole Faust 384*4b9c6d91SCole Faust /* 385*4b9c6d91SCole Faust * Run the specified command in the given minijail, execve(2)-style. 386*4b9c6d91SCole Faust * Don't use LD_PRELOAD to do privilege dropping. This is useful when sandboxing 387*4b9c6d91SCole Faust * static binaries, or on systems without support for LD_PRELOAD. 388*4b9c6d91SCole Faust */ 389*4b9c6d91SCole Faust int minijail_run_no_preload(struct minijail *j, const char *filename, 390*4b9c6d91SCole Faust char *const argv[]); 391*4b9c6d91SCole Faust 392*4b9c6d91SCole Faust /* 393*4b9c6d91SCole Faust * Run the specified command in the given minijail, execve(2)-style. 394*4b9c6d91SCole Faust * Update |*pchild_pid| with the pid of the child. 395*4b9c6d91SCole Faust */ 396*4b9c6d91SCole Faust int minijail_run_pid(struct minijail *j, const char *filename, 397*4b9c6d91SCole Faust char *const argv[], pid_t *pchild_pid); 398*4b9c6d91SCole Faust 399*4b9c6d91SCole Faust /* 400*4b9c6d91SCole Faust * Run the specified command in the given minijail, execve(2)-style. 401*4b9c6d91SCole Faust * Update |*pstdin_fd| with a fd that allows writing to the child's 402*4b9c6d91SCole Faust * standard input. 403*4b9c6d91SCole Faust */ 404*4b9c6d91SCole Faust int minijail_run_pipe(struct minijail *j, const char *filename, 405*4b9c6d91SCole Faust char *const argv[], int *pstdin_fd); 406*4b9c6d91SCole Faust 407*4b9c6d91SCole Faust /* 408*4b9c6d91SCole Faust * Run the specified command in the given minijail, execve(2)-style. 409*4b9c6d91SCole Faust * Update |*pchild_pid| with the pid of the child. 410*4b9c6d91SCole Faust * Update |*pstdin_fd| with a fd that allows writing to the child's 411*4b9c6d91SCole Faust * standard input. 412*4b9c6d91SCole Faust * Update |*pstdout_fd| with a fd that allows reading from the child's 413*4b9c6d91SCole Faust * standard output. 414*4b9c6d91SCole Faust * Update |*pstderr_fd| with a fd that allows reading from the child's 415*4b9c6d91SCole Faust * standard error. 416*4b9c6d91SCole Faust */ 417*4b9c6d91SCole Faust int minijail_run_pid_pipes(struct minijail *j, const char *filename, 418*4b9c6d91SCole Faust char *const argv[], pid_t *pchild_pid, 419*4b9c6d91SCole Faust int *pstdin_fd, int *pstdout_fd, int *pstderr_fd); 420*4b9c6d91SCole Faust 421*4b9c6d91SCole Faust /* 422*4b9c6d91SCole Faust * Run the specified command in the given minijail, execve(2)-style. 423*4b9c6d91SCole Faust * Pass |envp| as the full environment for the child. 424*4b9c6d91SCole Faust * Update |*pchild_pid| with the pid of the child. 425*4b9c6d91SCole Faust * Update |*pstdin_fd| with a fd that allows writing to the child's 426*4b9c6d91SCole Faust * standard input. 427*4b9c6d91SCole Faust * Update |*pstdout_fd| with a fd that allows reading from the child's 428*4b9c6d91SCole Faust * standard output. 429*4b9c6d91SCole Faust * Update |*pstderr_fd| with a fd that allows reading from the child's 430*4b9c6d91SCole Faust * standard error. 431*4b9c6d91SCole Faust */ 432*4b9c6d91SCole Faust int minijail_run_env_pid_pipes(struct minijail *j, const char *filename, 433*4b9c6d91SCole Faust char *const argv[], char *const envp[], 434*4b9c6d91SCole Faust pid_t *pchild_pid, int *pstdin_fd, 435*4b9c6d91SCole Faust int *pstdout_fd, int *pstderr_fd); 436*4b9c6d91SCole Faust 437*4b9c6d91SCole Faust /* 438*4b9c6d91SCole Faust * Execute the specified file descriptor in the given minijail, 439*4b9c6d91SCole Faust * fexecve(3)-style. 440*4b9c6d91SCole Faust * Pass |envp| as the full environment for the child or NULL to inherit. 441*4b9c6d91SCole Faust * Update |*pchild_pid| with the pid of the child. 442*4b9c6d91SCole Faust * Update |*pstdin_fd| with a fd that allows writing to the child's 443*4b9c6d91SCole Faust * standard input. 444*4b9c6d91SCole Faust * Update |*pstdout_fd| with a fd that allows reading from the child's 445*4b9c6d91SCole Faust * standard output. 446*4b9c6d91SCole Faust * Update |*pstderr_fd| with a fd that allows reading from the child's 447*4b9c6d91SCole Faust * standard error. 448*4b9c6d91SCole Faust */ 449*4b9c6d91SCole Faust int minijail_run_fd_env_pid_pipes(struct minijail *j, int elf_fd, 450*4b9c6d91SCole Faust char *const argv[], char *const envp[], 451*4b9c6d91SCole Faust pid_t *pchild_pid, int *pstdin_fd, 452*4b9c6d91SCole Faust int *pstdout_fd, int *pstderr_fd); 453*4b9c6d91SCole Faust 454*4b9c6d91SCole Faust /* 455*4b9c6d91SCole Faust * Run the specified command in the given minijail, execve(2)-style. 456*4b9c6d91SCole Faust * Update |*pchild_pid| with the pid of the child. 457*4b9c6d91SCole Faust * Update |*pstdin_fd| with a fd that allows writing to the child's 458*4b9c6d91SCole Faust * standard input. 459*4b9c6d91SCole Faust * Update |*pstdout_fd| with a fd that allows reading from the child's 460*4b9c6d91SCole Faust * standard output. 461*4b9c6d91SCole Faust * Update |*pstderr_fd| with a fd that allows reading from the child's 462*4b9c6d91SCole Faust * standard error. 463*4b9c6d91SCole Faust * Don't use LD_PRELOAD to do privilege dropping. This is useful when sandboxing 464*4b9c6d91SCole Faust * static binaries, or on systems without support for LD_PRELOAD. 465*4b9c6d91SCole Faust */ 466*4b9c6d91SCole Faust int minijail_run_pid_pipes_no_preload(struct minijail *j, const char *filename, 467*4b9c6d91SCole Faust char *const argv[], pid_t *pchild_pid, 468*4b9c6d91SCole Faust int *pstdin_fd, int *pstdout_fd, 469*4b9c6d91SCole Faust int *pstderr_fd); 470*4b9c6d91SCole Faust 471*4b9c6d91SCole Faust /* 472*4b9c6d91SCole Faust * Run the specified command in the given minijail, execve(2)-style. 473*4b9c6d91SCole Faust * Pass |envp| as the full environment for the child. 474*4b9c6d91SCole Faust * Update |*pchild_pid| with the pid of the child. 475*4b9c6d91SCole Faust * Update |*pstdin_fd| with a fd that allows writing to the child's 476*4b9c6d91SCole Faust * standard input. 477*4b9c6d91SCole Faust * Update |*pstdout_fd| with a fd that allows reading from the child's 478*4b9c6d91SCole Faust * standard output. 479*4b9c6d91SCole Faust * Update |*pstderr_fd| with a fd that allows reading from the child's 480*4b9c6d91SCole Faust * standard error. 481*4b9c6d91SCole Faust * Don't use LD_PRELOAD to do privilege dropping. This is useful when sandboxing 482*4b9c6d91SCole Faust * static binaries, or on systems without support for LD_PRELOAD. 483*4b9c6d91SCole Faust */ 484*4b9c6d91SCole Faust int minijail_run_env_pid_pipes_no_preload(struct minijail *j, 485*4b9c6d91SCole Faust const char *filename, 486*4b9c6d91SCole Faust char *const argv[], 487*4b9c6d91SCole Faust char *const envp[], pid_t *pchild_pid, 488*4b9c6d91SCole Faust int *pstdin_fd, int *pstdout_fd, 489*4b9c6d91SCole Faust int *pstderr_fd); 490*4b9c6d91SCole Faust 491*4b9c6d91SCole Faust /* 492*4b9c6d91SCole Faust * Fork, jail the child, and return. This behaves similar to fork(2), except it 493*4b9c6d91SCole Faust * puts the child process in a jail before returning. 494*4b9c6d91SCole Faust * `minijail_fork` returns in both the parent and the child. The pid of the 495*4b9c6d91SCole Faust * child is returned to the parent. Zero is returned in the child. LD_PRELOAD 496*4b9c6d91SCole Faust * is not supported. 497*4b9c6d91SCole Faust * If minijail_namespace_pids() or minijail_namespace_user() are used, 498*4b9c6d91SCole Faust * this or minijail_run*() is required instead of minijail_enter(). 499*4b9c6d91SCole Faust */ 500*4b9c6d91SCole Faust pid_t minijail_fork(struct minijail *j); 501*4b9c6d91SCole Faust 502*4b9c6d91SCole Faust /* 503*4b9c6d91SCole Faust * Send SIGTERM to the process in the minijail and wait for it to terminate. 504*4b9c6d91SCole Faust * 505*4b9c6d91SCole Faust * Return the same nonnegative exit status as minijail_wait(), or a negative 506*4b9c6d91SCole Faust * error code (eg -ESRCH if the process has already been waited for). 507*4b9c6d91SCole Faust * 508*4b9c6d91SCole Faust * This is most useful if the minijail has been created with PID namespacing 509*4b9c6d91SCole Faust * since, in this case, all processes inside it are atomically killed. 510*4b9c6d91SCole Faust */ 511*4b9c6d91SCole Faust int minijail_kill(struct minijail *j); 512*4b9c6d91SCole Faust 513*4b9c6d91SCole Faust /* 514*4b9c6d91SCole Faust * Wait for the first process spawned in the specified minijail to exit, and 515*4b9c6d91SCole Faust * return its exit status. A process can only be waited once. 516*4b9c6d91SCole Faust * 517*4b9c6d91SCole Faust * Return: 518*4b9c6d91SCole Faust * A negative error code if the process cannot be waited for (eg -ECHILD if no 519*4b9c6d91SCole Faust * process has been started or if the process has already been waited for). 520*4b9c6d91SCole Faust * MINIJAIL_ERR_NO_COMMAND if command cannot be found. 521*4b9c6d91SCole Faust * MINIJAIL_ERR_NO_ACCESS if command cannot be run. 522*4b9c6d91SCole Faust * MINIJAIL_ERR_JAIL if process was killed by SIGSYS. 523*4b9c6d91SCole Faust * (MINIJAIL_ERR_SIG_BASE + n) if process was killed by signal n != SIGSYS. 524*4b9c6d91SCole Faust * (n & 0xFF) if process finished by returning code n. 525*4b9c6d91SCole Faust */ 526*4b9c6d91SCole Faust int minijail_wait(struct minijail *j); 527*4b9c6d91SCole Faust 528*4b9c6d91SCole Faust /* 529*4b9c6d91SCole Faust * Frees the given minijail. It does not matter if the process is inside the 530*4b9c6d91SCole Faust * minijail or not. It will not kill the process, see minijail_kill() if that is 531*4b9c6d91SCole Faust * desired. 532*4b9c6d91SCole Faust */ 533*4b9c6d91SCole Faust void minijail_destroy(struct minijail *j); 534*4b9c6d91SCole Faust 535*4b9c6d91SCole Faust /* 536*4b9c6d91SCole Faust * Deep copies the minijail in |from| to |out| providing two identical jails 537*4b9c6d91SCole Faust * that can be used to contain separate children created with minijail_fork(). 538*4b9c6d91SCole Faust * 539*4b9c6d91SCole Faust * Duplicating a jail is invalid after a jail has been passed to 540*4b9c6d91SCole Faust * minijail_fork(). Many minijail_*() calls will yield undefined 541*4b9c6d91SCole Faust * results when called on a jail duplicated post-fork. 542*4b9c6d91SCole Faust */ 543*4b9c6d91SCole Faust int minijail_copy_jail(const struct minijail *from, struct minijail *out); 544*4b9c6d91SCole Faust 545*4b9c6d91SCole Faust /* 546*4b9c6d91SCole Faust * minijail_log_to_fd: redirects the module-wide logging to an FD instead of 547*4b9c6d91SCole Faust * syslog. 548*4b9c6d91SCole Faust * @fd FD to log to. Caller must ensure this is available after 549*4b9c6d91SCole Faust * jailing (e.g. with minijail_preserve_fd()). 550*4b9c6d91SCole Faust * @min_priority the minimum logging priority. Same as the priority argument 551*4b9c6d91SCole Faust * to syslog(2). 552*4b9c6d91SCole Faust */ 553*4b9c6d91SCole Faust void minijail_log_to_fd(int fd, int min_priority); 554*4b9c6d91SCole Faust 555*4b9c6d91SCole Faust #ifdef __cplusplus 556*4b9c6d91SCole Faust } /* extern "C" */ 557*4b9c6d91SCole Faust #endif 558*4b9c6d91SCole Faust 559*4b9c6d91SCole Faust #endif /* !_LIBMINIJAIL_H_ */ 560