blob: d7011d28259d40b281936798cb5daa954e5eb403 [file] [log] [blame]
drewry@google.combd940e92009-12-07 19:13:27 +00001// Copyright (c) 2009 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// Some portions Copyright (c) 2009 The Chromium Authors.
5//
6// Implements MiniJail jailing logic.
7
8#include "minijail.h"
9
10#include <errno.h>
11
12namespace chromeos {
13
14bool MiniJail::Jail() const {
15 // XXX This is a very early implementation of the jailing logic.
16 // XXX Many features are missing or will be made more tunable.
17 const minijail::Options *opts = options();
Will Drewry8e7799c2009-12-07 15:50:16 -080018 if (!opts) {
19 LOG(ERROR) << "No Options given. Initialize must be called first "
20 << "with a valid Option pointer.";
21 return false;
22 }
drewry@google.combd940e92009-12-07 19:13:27 +000023 const minijail::Env *env = opts->env();
24
25 int namespaces = 0;
26 if (opts->namespace_pid())
27 namespaces |= CLONE_NEWPID;
28 if (opts->namespace_vfs())
29 namespaces |= CLONE_NEWNS;
Will Drewry4dd0e662010-01-19 14:43:50 -080030 if (namespaces && !env->EnterNamespace(namespaces)) {
31 return false;
drewry@google.combd940e92009-12-07 19:13:27 +000032 }
33
Will Drewry4dd0e662010-01-19 14:43:50 -080034 if (opts->namespace_vfs() && opts->add_readonly_mounts()) {
35 if (!env->Mount()) { // TODO(wad) add flags
36 return false;
37 }
38 }
39
40 if (opts->use_capabilities()) {
41 if (!env->KeepRootCapabilities()) {
42 return false;
43 }
44 if (!env->DisableDefaultRootPrivileges()) {
45 return false;
46 }
47 }
48
49 if (opts->disable_tracing()) {
50 if (!env->DisableTracing()) {
51 return false;
52 }
53 }
drewry@google.combd940e92009-12-07 19:13:27 +000054
55 uid_t uid = getuid();
56 if (opts->change_uid()) {
57 uid = opts->uid();
58 }
59 gid_t gid = getgid();
60 if (opts->change_gid()) {
61 gid = opts->gid();
62 }
63 // TODO(wad) separate group and user changes
64 if (opts->change_uid() || opts->change_gid()) {
Will Drewry4dd0e662010-01-19 14:43:50 -080065 DLOG(INFO) << "Attempting to change user and/or groups...";
66 if (!env->ChangeUser(uid, gid)) {
67 return false;
68 }
drewry@google.combd940e92009-12-07 19:13:27 +000069 }
70
71 if (opts->enforce_syscalls_by_source()) {
Will Drewry4dd0e662010-01-19 14:43:50 -080072 if (!env->FilterSyscallsBySource()) {
73 return false;
74 }
drewry@google.combd940e92009-12-07 19:13:27 +000075 } else if (opts->enforce_syscalls_benchmark()) {
Will Drewry4dd0e662010-01-19 14:43:50 -080076 if (!env->FilterSyscallsBenchmarkOnly()) {
77 return false;
78 }
drewry@google.combd940e92009-12-07 19:13:27 +000079 }
80
81 if (opts->use_capabilities()) {
82 // TODO(wad) use helpers to read caps from flags
Will Drewry4dd0e662010-01-19 14:43:50 -080083 if (!env->SanitizeCapabilities(0)) {
84 return false;
85 }
86 if (!env->SanitizeBoundingSet(0)) {
87 return false;
88 }
drewry@google.combd940e92009-12-07 19:13:27 +000089 }
90 return true;
91}
92
93} // namespace chromeos