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