blob: fd92f9530c25fb7b19e30d62affc7dc94000e19a [file] [log] [blame]
Elly Jonescd7a9042011-07-22 13:56:51 -04001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
Will Drewry32ac9f52011-08-18 21:36:27 -05003 * found in the LICENSE file.
4 */
Elly Jonescd7a9042011-07-22 13:56:51 -04005
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 Jonese1749eb2011-10-07 13:54:59 -040014#ifndef _LIBMINIJAIL_H_
15#define _LIBMINIJAIL_H_
Elly Jonescd7a9042011-07-22 13:56:51 -040016
17#include <stdint.h>
18#include <sys/types.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24enum {
Elly Jonese1749eb2011-10-07 13:54:59 -040025 MINIJAIL_ERR_PRELOAD = 252,
26 MINIJAIL_ERR_JAIL = 253,
27 MINIJAIL_ERR_INIT = 254,
Elly Jonescd7a9042011-07-22 13:56:51 -040028};
29
30struct minijail;
31
32/* Allocates a new minijail with no restrictions. */
33struct 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 Jonese1749eb2011-10-07 13:54:59 -040037 * explanations in detail of what the restrictions do.
38 */
Elly Jonescd7a9042011-07-22 13:56:51 -040039void minijail_change_uid(struct minijail *j, uid_t uid);
40void minijail_change_gid(struct minijail *j, gid_t gid);
Will Drewry2ddaad02011-09-16 11:36:08 -050041/* Stores user to change to and copies |user| for internal consistency. */
Elly Jonescd7a9042011-07-22 13:56:51 -040042int minijail_change_user(struct minijail *j, const char *user);
Will Drewry2ddaad02011-09-16 11:36:08 -050043/* Does not take ownership of |group|. */
Elly Jonescd7a9042011-07-22 13:56:51 -040044int minijail_change_group(struct minijail *j, const char *group);
45void minijail_use_seccomp(struct minijail *j);
Will Drewry32ac9f52011-08-18 21:36:27 -050046void minijail_use_seccomp_filter(struct minijail *j);
47void minijail_parse_seccomp_filters(struct minijail *j, const char *path);
48int minijail_add_seccomp_filter(struct minijail *j, int nr,
Elly Jonese1749eb2011-10-07 13:54:59 -040049 const char *filter);
Elly Jonescd7a9042011-07-22 13:56:51 -040050void minijail_use_caps(struct minijail *j, uint64_t capmask);
51void minijail_namespace_vfs(struct minijail *j);
52void minijail_namespace_pids(struct minijail *j);
53void minijail_remount_readonly(struct minijail *j);
54void minijail_inherit_usergroups(struct minijail *j);
55void minijail_disable_ptrace(struct minijail *j);
56
Will Drewry32ac9f52011-08-18 21:36:27 -050057/* Exposes minijail's name-to-int mapping for system calls for the
58 * architecture it was built on. This is primarily exposed for
59 * minijail_add_seccomp_filter() and testing.
60 * Returns the system call number on success or -1 on failure.
61 */
62int minijail_lookup_syscall(const char *name);
63
Elly Jonescd7a9042011-07-22 13:56:51 -040064/* Lock this process into the given minijail. Note that this procedure cannot fail,
65 * since there is no way to undo privilege-dropping; therefore, if any part of
66 * the privilege-drop fails, minijail_enter() will abort the entire process.
67 *
68 * Some restrictions cannot be enabled this way (pid namespaces) and attempting
69 * to do so will cause an abort.
70 */
71void minijail_enter(const struct minijail *j);
72
73/* Run the specified command in the given minijail, execve(3)-style. This is
Elly Jonese1749eb2011-10-07 13:54:59 -040074 * required if minijail_namespace_pids() was used.
75 */
76int minijail_run(struct minijail *j, const char *filename,
77 char *const argv[]);
Elly Jonescd7a9042011-07-22 13:56:51 -040078
79/* Kill the specified minijail. The minijail must have been created with pid
Elly Jonese1749eb2011-10-07 13:54:59 -040080 * namespacing; if it was, all processes inside it are atomically killed.
81 */
Elly Jonescd7a9042011-07-22 13:56:51 -040082int minijail_kill(struct minijail *j);
83
84/* Wait for all processed in the specified minijail to exit. Returns the exit
Elly Jonese1749eb2011-10-07 13:54:59 -040085 * status of the _first_ process spawned in the jail.
86 */
Elly Jonescd7a9042011-07-22 13:56:51 -040087int minijail_wait(struct minijail *j);
88
89/* Frees the given minijail. It does not matter if the process is inside the minijail or
90 * not. */
91void minijail_destroy(struct minijail *j);
92
93#ifdef __cplusplus
Elly Jonese1749eb2011-10-07 13:54:59 -040094}; /* extern "C" */
Elly Jonescd7a9042011-07-22 13:56:51 -040095#endif
96
Elly Jonese1749eb2011-10-07 13:54:59 -040097#endif /* !_LIBMINIJAIL_H_ */