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 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 5 | #include "debugd/src/sandboxed_process.h" |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 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() |
Ben Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 15 | : sandboxing_(true), user_(kDefaultUser), group_(kDefaultGroup) {} |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 16 | |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 17 | // static |
| 18 | bool 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-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 35 | bool 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-Jones | e56a8f6 | 2013-01-23 15:50:21 -0500 | [diff] [blame] | 41 | if (user_ != "root") { |
| 42 | AddArg("-u"); |
| 43 | AddArg(user_); |
| 44 | } |
| 45 | if (group_ != "root") { |
| 46 | AddArg("-g"); |
| 47 | AddArg(group_); |
| 48 | } |
Elly Fong-Jones | ec8d762 | 2013-01-22 11:35:22 -0500 | [diff] [blame] | 49 | AddArg("--"); |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 50 | } |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | void SandboxedProcess::DisableSandbox() { |
| 55 | sandboxing_ = false; |
| 56 | } |
| 57 | |
| 58 | void SandboxedProcess::SandboxAs(const std::string& user, |
| 59 | const std::string& group) { |
| 60 | sandboxing_ = true; |
| 61 | user_ = user; |
| 62 | group_ = group; |
| 63 | } |
| 64 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 65 | } // namespace debugd |