Elly Jones | a44d22d | 2012-01-05 18:05:56 -0500 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 5 | #include <sys/mount.h> |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 6 | #include <unistd.h> |
| 7 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 8 | #include <base/command_line.h> |
| 9 | #include <base/logging.h> |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 10 | #include <chromeos/libminijail.h> |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 11 | #include <chromeos/process.h> |
| 12 | #include <chromeos/syslog_logging.h> |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 13 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 14 | #include "debug_daemon.h" |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 15 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 16 | namespace { |
| 17 | const char* kHelpers[] = { |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 18 | NULL, |
| 19 | }; |
| 20 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 21 | // @brief Enter a VFS namespace. |
| 22 | // |
| 23 | // We don't want anyone other than our descendants to see our tmpfs. |
| 24 | void enter_vfs_namespace() { |
| 25 | struct minijail* j = minijail_new(); |
| 26 | minijail_namespace_vfs(j); |
| 27 | minijail_enter(j); |
| 28 | minijail_destroy(j); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | // @brief Enter a minijail. |
| 32 | // |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 33 | // We are already in a vfs namespace so that our tmpfs is only visible to us and |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 34 | // our descendants, and we don't want to be root. Note that minijail_enter() |
| 35 | // exits the process if it can't succeed. |
| 36 | void enter_sandbox() { |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 37 | static const char* kDebugdUser = "debugd"; |
| 38 | static const char* kDebugdGroup = "debugd"; |
| 39 | struct minijail* j = minijail_new(); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 40 | minijail_change_user(j, kDebugdUser); |
| 41 | minijail_change_group(j, kDebugdGroup); |
| 42 | minijail_enter(j); |
| 43 | minijail_destroy(j); |
| 44 | } |
| 45 | |
| 46 | // @brief Sets up a tmpfs visible to this program and its descendants. |
| 47 | // |
| 48 | // The created tmpfs is mounted at /debugd. |
| 49 | void make_tmpfs() { |
| 50 | int r = mount("none", "/debugd", "tmpfs", MS_NODEV | MS_NOSUID | MS_NOEXEC, |
| 51 | NULL); |
| 52 | if (r < 0) |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 53 | PLOG(FATAL) << "mount() failed"; |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // @brief Launch all our helper programs. |
| 57 | void launch_helpers() { |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 58 | for (int i = 0; kHelpers[i]; ++i) { |
| 59 | chromeos::ProcessImpl p; |
| 60 | p.AddArg(kHelpers[i]); |
| 61 | p.Start(); |
| 62 | p.Release(); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 66 | // @brief Start the debugd DBus interface. |
| 67 | void start() { |
| 68 | DBus::BusDispatcher dispatcher; |
| 69 | DBus::default_dispatcher = &dispatcher; |
| 70 | DBus::Connection conn = DBus::Connection::SystemBus(); |
| 71 | debugd::DebugDaemon debugd(&conn, &dispatcher); |
| 72 | if (!debugd.Init()) |
| 73 | LOG(FATAL) << "debugd.Init() failed"; |
| 74 | debugd.Run(); |
| 75 | LOG(FATAL) << "debugd.Run() returned"; |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 76 | } |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 77 | }; // namespace |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 78 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 79 | int __attribute__((visibility("default"))) main(int argc, char* argv[]) { |
| 80 | CommandLine::Init(argc, argv); |
| 81 | chromeos::InitLog(chromeos::kLogToSyslog | chromeos::kLogToStderr); |
| 82 | enter_vfs_namespace(); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 83 | make_tmpfs(); |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 84 | enter_sandbox(); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 85 | launch_helpers(); |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 86 | start(); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 87 | return 0; |
| 88 | } |