Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 1 | // 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 Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 7 | #include <base/strings/stringprintf.h> |
| 8 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 9 | namespace debugd { |
| 10 | |
Elly Fong-Jones | 215b562 | 2013-03-20 14:32:18 -0400 | [diff] [blame] | 11 | const char *SandboxedProcess::kDefaultUser = "debugd"; |
| 12 | const char *SandboxedProcess::kDefaultGroup = "debugd"; |
| 13 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 14 | SandboxedProcess::SandboxedProcess() |
Elly Fong-Jones | 215b562 | 2013-03-20 14:32:18 -0400 | [diff] [blame] | 15 | : sandboxing_(true), user_(kDefaultUser), group_(kDefaultGroup) { } |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 16 | SandboxedProcess::~SandboxedProcess() { } |
| 17 | |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 18 | // static |
| 19 | bool 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-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 36 | bool 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-Jones | e56a8f6 | 2013-01-23 15:50:21 -0500 | [diff] [blame] | 42 | if (user_ != "root") { |
| 43 | AddArg("-u"); |
| 44 | AddArg(user_); |
| 45 | } |
| 46 | if (group_ != "root") { |
| 47 | AddArg("-g"); |
| 48 | AddArg(group_); |
| 49 | } |
Elly Fong-Jones | ec8d762 | 2013-01-22 11:35:22 -0500 | [diff] [blame] | 50 | AddArg("--"); |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | void SandboxedProcess::DisableSandbox() { |
| 56 | sandboxing_ = false; |
| 57 | } |
| 58 | |
| 59 | void 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 |