blob: 67e6afad490b23f1be27498cbf23a77ad5c66d76 [file] [log] [blame]
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -05001// Copyright (c) 2012 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
Alex Vakulenko262be3f2014-07-30 15:25:50 -07005#include "debugd/src/sandboxed_process.h"
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -05006
Ben Chan297c3c22013-07-17 17:34:12 -07007#include <base/strings/stringprintf.h>
8
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -05009namespace debugd {
10
Elly Fong-Jones215b5622013-03-20 14:32:18 -040011const char *SandboxedProcess::kDefaultUser = "debugd";
12const char *SandboxedProcess::kDefaultGroup = "debugd";
13
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050014SandboxedProcess::SandboxedProcess()
Ben Chan78f89532014-08-29 09:35:09 -070015 : sandboxing_(true), user_(kDefaultUser), group_(kDefaultGroup) {}
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050016
Ben Chan297c3c22013-07-17 17:34:12 -070017// static
18bool SandboxedProcess::GetHelperPath(const std::string& relative_path,
19 std::string* full_path) {
20 // This environment variable controls the root directory for debugd helpers,
21 // which lets people develop helpers even when verified boot is on.
22 const char* helpers_dir = getenv("DEBUGD_HELPERS");
23 std::string path = base::StringPrintf(
24 "%s/%s",
25 helpers_dir ? helpers_dir : "/usr/libexec/debugd/helpers",
26 relative_path.c_str());
27
28 if (path.length() > PATH_MAX)
29 return false;
30
31 *full_path = path;
32 return true;
33}
34
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050035bool SandboxedProcess::Init() {
36 const char *kMiniJail = "/sbin/minijail0";
37 if (sandboxing_) {
38 if (user_.empty() || group_.empty())
39 return false;
40 AddArg(kMiniJail);
Elly Fong-Jonese56a8f62013-01-23 15:50:21 -050041 if (user_ != "root") {
42 AddArg("-u");
43 AddArg(user_);
44 }
45 if (group_ != "root") {
46 AddArg("-g");
47 AddArg(group_);
48 }
Elly Fong-Jonesec8d7622013-01-22 11:35:22 -050049 AddArg("--");
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050050 }
51 return true;
52}
53
54void SandboxedProcess::DisableSandbox() {
55 sandboxing_ = false;
56}
57
58void SandboxedProcess::SandboxAs(const std::string& user,
59 const std::string& group) {
60 sandboxing_ = true;
61 user_ = user;
62 group_ = group;
63}
64
Ben Chana0011d82014-05-13 00:19:29 -070065} // namespace debugd