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> |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 11 | #include <brillo/daemons/dbus_daemon.h> |
Mike Frysinger | 0597cdd | 2020-03-18 05:32:21 -0400 | [diff] [blame^] | 12 | #include <brillo/flag_helper.h> |
Alex Vakulenko | e769653 | 2015-10-16 16:27:29 -0700 | [diff] [blame] | 13 | #include <brillo/syslog_logging.h> |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 14 | #include <chromeos/dbus/service_constants.h> |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 15 | #include <chromeos/libminijail.h> |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame] | 16 | #include <chromeos/scoped_minijail.h> |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 17 | |
Eric Caruso | 63c9743 | 2017-04-27 13:23:28 -0700 | [diff] [blame] | 18 | #include "debugd/src/debugd_dbus_adaptor.h" |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 19 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 20 | namespace { |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 21 | |
Andrey Pronin | 0f029d8 | 2018-07-11 15:25:26 -0700 | [diff] [blame] | 22 | // For TPM 1.2 only: Directory to mount for access to tcsd socket. |
| 23 | constexpr char kTcsdDir[] = "/run/tcsd"; |
| 24 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 25 | // @brief Enter a VFS namespace. |
| 26 | // |
| 27 | // We don't want anyone other than our descendants to see our tmpfs. |
| 28 | void enter_vfs_namespace() { |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame] | 29 | ScopedMinijail j(minijail_new()); |
| 30 | |
| 31 | // Create a minimalistic mount namespace with just the bare minimum required. |
| 32 | minijail_namespace_vfs(j.get()); |
Allen Webb | 2b6e35d | 2019-02-21 10:06:31 -0800 | [diff] [blame] | 33 | if (minijail_enter_pivot_root(j.get(), "/mnt/empty")) |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame] | 34 | LOG(FATAL) << "minijail_enter_pivot_root() failed"; |
| 35 | if (minijail_bind(j.get(), "/", "/", 0)) |
| 36 | LOG(FATAL) << "minijail_bind(\"/\") failed"; |
| 37 | if (minijail_mount_with_data(j.get(), "none", "/proc", "proc", |
| 38 | MS_NOSUID | MS_NOEXEC | MS_NODEV, nullptr)) { |
| 39 | LOG(FATAL) << "minijail_mount_with_data(\"/proc\") failed"; |
| 40 | } |
| 41 | if (minijail_bind(j.get(), "/var", "/var", 1)) |
| 42 | LOG(FATAL) << "minijail_bind(\"/var\") failed"; |
| 43 | |
Mike Frysinger | 25cdbe7 | 2018-08-23 01:54:00 -0400 | [diff] [blame] | 44 | // Hack a path for vpd until it can migrate to /var. |
| 45 | // https://crbug.com/876838 |
| 46 | if (minijail_mount_with_data(j.get(), "tmpfs", "/mnt", "tmpfs", |
| 47 | MS_NOSUID | MS_NOEXEC | MS_NODEV, |
| 48 | "mode=0755,size=10M")) { |
| 49 | LOG(FATAL) << "minijail_mount_with_data(\"/mnt\") failed"; |
| 50 | } |
| 51 | const char kVpdPath[] = "/mnt/stateful_partition/unencrypted/cache/vpd"; |
| 52 | if (minijail_bind(j.get(), kVpdPath, kVpdPath, 1)) |
| 53 | LOG(FATAL) << "minijail_bind(\"" << kVpdPath << "\") failed"; |
| 54 | |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame] | 55 | // Mount /run/dbus to be able to communicate with D-Bus. |
| 56 | if (minijail_mount_with_data(j.get(), "tmpfs", "/run", "tmpfs", |
| 57 | MS_NOSUID | MS_NOEXEC | MS_NODEV, nullptr)) { |
| 58 | LOG(FATAL) << "minijail_mount_with_data(\"/run\") failed"; |
| 59 | } |
| 60 | if (minijail_bind(j.get(), "/run/dbus", "/run/dbus", 0)) |
| 61 | LOG(FATAL) << "minijail_bind(\"/run/dbus\") failed"; |
| 62 | |
David Valleau | e694deb | 2018-07-30 17:19:11 -0700 | [diff] [blame] | 63 | // Mount /tmp, /run/cups, and /run/ippusb to be able to communicate with CUPS. |
Piotr Pawliczek | 39752f3 | 2018-10-16 14:42:28 -0700 | [diff] [blame] | 64 | minijail_mount_tmp(j.get()); |
Mike Frysinger | 97000d8 | 2018-06-05 11:36:12 -0400 | [diff] [blame] | 65 | // In case we start before cups, make sure the path exists. |
| 66 | mkdir("/run/cups", 0755); |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame] | 67 | if (minijail_bind(j.get(), "/run/cups", "/run/cups", 0)) |
| 68 | LOG(FATAL) << "minijail_bind(\"/run/cups\") failed"; |
David Valleau | e694deb | 2018-07-30 17:19:11 -0700 | [diff] [blame] | 69 | // In case we start before upstart-socket-bridge, make sure the path exists. |
| 70 | mkdir("/run/ippusb", 0755); |
| 71 | // Mount /run/ippusb to be able to communicate with CUPS. |
| 72 | if (minijail_bind(j.get(), "/run/ippusb", "/run/ippusb", 0)) |
| 73 | LOG(FATAL) << "minijail_bind(\"/run/ippusb\") failed"; |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame] | 74 | |
David Valleau | 3339ca2 | 2019-03-19 13:33:24 -0700 | [diff] [blame] | 75 | // In case we start before avahi-daemon, make sure the path exists. |
| 76 | mkdir("/var/run/avahi-daemon", 0755); |
| 77 | // Mount /run/avahi-daemon in order to perform mdns name resolution. |
| 78 | if (minijail_bind(j.get(), "/run/avahi-daemon", "/run/avahi-daemon", |
| 79 | 0)) |
| 80 | LOG(FATAL) << "minijail_bind(\"/run/avahi-daemon\") failed"; |
| 81 | |
Mike Frysinger | 65884e6 | 2018-07-09 13:41:26 -0400 | [diff] [blame] | 82 | // Since shill provides network resolution settings, bind mount it. |
| 83 | // In case we start before shill, make sure the path exists. |
| 84 | mkdir("/run/shill", 0755); |
| 85 | if (minijail_bind(j.get(), "/run/shill", "/run/shill", 0)) |
| 86 | LOG(FATAL) << "minijail_bind(\"/run/shill\") failed"; |
| 87 | |
Kansho Nishida | dedbf83 | 2019-08-02 18:31:59 +0900 | [diff] [blame] | 88 | // TODO(kanso): Drop this mount once log_tool.cc no longer cats bugreport |
| 89 | // directly (used by Android N and older). |
Luis Hector Chavez | 2a2dc1f | 2018-06-08 15:36:37 -0700 | [diff] [blame] | 90 | // Mount /run/arc/bugreport to be able to collect ARC bug reports. |
| 91 | // In case we start before ARC, make sure the path exists. |
| 92 | mkdir("/run/arc", 0755); |
| 93 | mkdir("/run/arc/bugreport", 0755); |
| 94 | if (minijail_bind(j.get(), "/run/arc/bugreport", "/run/arc/bugreport", 0)) |
| 95 | LOG(FATAL) << "minijail_bind(\"/run/arc/bugreport\") failed"; |
| 96 | |
Junichi Uekawa | f8a0c9e | 2018-06-29 14:36:22 +0900 | [diff] [blame] | 97 | // Mount /run/containers to be able to collect container stats. |
| 98 | mkdir("/run/containers", 0755); |
| 99 | if (minijail_bind(j.get(), "/run/containers", "/run/containers", 0)) |
| 100 | LOG(FATAL) << "minijail_bind(\"/run/containers\") failed"; |
| 101 | |
Chris Morin | b7a32eb | 2019-02-19 20:06:21 -0800 | [diff] [blame] | 102 | // Mount /run/systemd/journal to be able to log to journald. |
Chris Morin | b7a32eb | 2019-02-19 20:06:21 -0800 | [diff] [blame] | 103 | if (minijail_bind(j.get(), "/run/systemd/journal", "/run/systemd/journal", 0)) |
| 104 | LOG(FATAL) << "minijail_bind(\"/run/systemd/journal\") failed"; |
| 105 | |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame] | 106 | // Mount /dev to be able to inspect devices. |
| 107 | if (minijail_mount_with_data(j.get(), "/dev", "/dev", "bind", |
| 108 | MS_BIND | MS_REC, nullptr)) { |
| 109 | LOG(FATAL) << "minijail_mount_with_data(\"/dev\") failed"; |
| 110 | } |
| 111 | |
| 112 | // Mount /sys to access some logs. |
| 113 | if (minijail_mount_with_data(j.get(), "/sys", "/sys", "bind", |
| 114 | MS_BIND | MS_REC, nullptr)) { |
| 115 | LOG(FATAL) << "minijail_mount_with_data(\"/sys\") failed"; |
| 116 | } |
| 117 | |
Jesse Schettler | e127764 | 2020-02-27 12:23:18 -0700 | [diff] [blame] | 118 | // Mount /run/chromeos-config/v1 to access chromeos-config. |
| 119 | if (minijail_bind(j.get(), "/run/chromeos-config/v1", |
| 120 | "/run/chromeos-config/v1", 0)) { |
| 121 | LOG(FATAL) << "minijail_bind(\"/run/chromeos-config/v1\") failed"; |
| 122 | } |
| 123 | |
Andrey Pronin | 0f029d8 | 2018-07-11 15:25:26 -0700 | [diff] [blame] | 124 | if (USE_TPM) { |
| 125 | // For TPM 1.2 only: Enable utilities that communicate with TPM via tcsd - |
| 126 | // mount directory containing tcsd socket. |
| 127 | mkdir(kTcsdDir, 0755); |
| 128 | if (minijail_bind(j.get(), kTcsdDir, kTcsdDir, 0)) { |
| 129 | LOG(FATAL) << "minijail_bind(\"" << kTcsdDir << "\") failed"; |
| 130 | } |
| 131 | } |
| 132 | |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame] | 133 | minijail_enter(j.get()); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 136 | class Daemon : public brillo::DBusServiceDaemon { |
| 137 | public: |
Eric Caruso | 5153585 | 2017-07-11 14:13:20 -0700 | [diff] [blame] | 138 | Daemon() : DBusServiceDaemon(debugd::kDebugdServiceName) {} |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 139 | |
| 140 | protected: |
| 141 | void RegisterDBusObjectsAsync( |
Eric Caruso | b298bb4 | 2017-05-09 10:53:30 -0700 | [diff] [blame] | 142 | brillo::dbus_utils::AsyncEventSequencer* sequencer) override { |
Eric Caruso | 5153585 | 2017-07-11 14:13:20 -0700 | [diff] [blame] | 143 | adaptor_.reset(new debugd::DebugdDBusAdaptor(bus_)); |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 144 | adaptor_->RegisterAsync(sequencer->GetHandler( |
| 145 | "RegisterAsync() failed.", true)); |
| 146 | } |
| 147 | |
| 148 | private: |
| 149 | std::unique_ptr<debugd::DebugdDBusAdaptor> adaptor_; |
| 150 | |
| 151 | DISALLOW_COPY_AND_ASSIGN(Daemon); |
| 152 | }; |
Ben Chan | af12586 | 2017-02-08 23:11:18 -0800 | [diff] [blame] | 153 | |
| 154 | } // namespace |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 155 | |
Ben Chan | c572aff | 2018-04-10 09:20:27 -0700 | [diff] [blame] | 156 | int main(int argc, char* argv[]) { |
Mike Frysinger | 0597cdd | 2020-03-18 05:32:21 -0400 | [diff] [blame^] | 157 | brillo::FlagHelper::Init(argc, argv, "CrOS debug daemon"); |
Mike Frysinger | b2f7ac5 | 2018-05-17 02:36:23 -0400 | [diff] [blame] | 158 | brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderrIfTty); |
Mike Frysinger | 0597cdd | 2020-03-18 05:32:21 -0400 | [diff] [blame^] | 159 | if (argc != 1) { |
| 160 | LOG(ERROR) << "debugd takes no arguments"; |
| 161 | return 1; |
| 162 | } |
| 163 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 164 | enter_vfs_namespace(); |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 165 | Daemon().Run(); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 166 | return 0; |
| 167 | } |