blob: 7499229bd4157516913f8a3e95ed5d00f6fc9509 [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
5#include "sandboxed_process.h"
6
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()
Elly Fong-Jones215b5622013-03-20 14:32:18 -040015 : sandboxing_(true), user_(kDefaultUser), group_(kDefaultGroup) { }
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050016SandboxedProcess::~SandboxedProcess() { }
17
Ben Chan297c3c22013-07-17 17:34:12 -070018// static
19bool SandboxedProcess::GetHelperPath(const std::string& relative_path,
20 std::string* full_path) {
21 // This environment variable controls the root directory for debugd helpers,
22 // which lets people develop helpers even when verified boot is on.
23 const char* helpers_dir = getenv("DEBUGD_HELPERS");
24 std::string path = base::StringPrintf(
25 "%s/%s",
26 helpers_dir ? helpers_dir : "/usr/libexec/debugd/helpers",
27 relative_path.c_str());
28
29 if (path.length() > PATH_MAX)
30 return false;
31
32 *full_path = path;
33 return true;
34}
35
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050036bool SandboxedProcess::Init() {
37 const char *kMiniJail = "/sbin/minijail0";
38 if (sandboxing_) {
39 if (user_.empty() || group_.empty())
40 return false;
41 AddArg(kMiniJail);
Elly Fong-Jonese56a8f62013-01-23 15:50:21 -050042 if (user_ != "root") {
43 AddArg("-u");
44 AddArg(user_);
45 }
46 if (group_ != "root") {
47 AddArg("-g");
48 AddArg(group_);
49 }
Elly Fong-Jonesec8d7622013-01-22 11:35:22 -050050 AddArg("--");
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050051 }
52 return true;
53}
54
55void SandboxedProcess::DisableSandbox() {
56 sandboxing_ = false;
57}
58
59void SandboxedProcess::SandboxAs(const std::string& user,
60 const std::string& group) {
61 sandboxing_ = true;
62 user_ = user;
63 group_ = group;
64}
65
66}; // namespace debugd