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 | |
Nicole Anderson-Au | a21e72b | 2021-03-04 21:35:26 +0000 | [diff] [blame] | 55 | minijail_remount_mode(j.get(), MS_SLAVE); |
mhasank | 545ccff | 2020-04-01 12:10:54 -0700 | [diff] [blame] | 56 | |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame] | 57 | if (minijail_mount_with_data(j.get(), "tmpfs", "/run", "tmpfs", |
| 58 | MS_NOSUID | MS_NOEXEC | MS_NODEV, nullptr)) { |
| 59 | LOG(FATAL) << "minijail_mount_with_data(\"/run\") failed"; |
| 60 | } |
mhasank | 545ccff | 2020-04-01 12:10:54 -0700 | [diff] [blame] | 61 | |
Nicole Anderson-Au | a21e72b | 2021-03-04 21:35:26 +0000 | [diff] [blame] | 62 | if (minijail_mount(j.get(), "/run/daemon-store/debugd", |
| 63 | "/run/daemon-store/debugd", "none", |
mhasank | 545ccff | 2020-04-01 12:10:54 -0700 | [diff] [blame] | 64 | MS_BIND | MS_REC) != 0) { |
Nicole Anderson-Au | a21e72b | 2021-03-04 21:35:26 +0000 | [diff] [blame] | 65 | LOG(FATAL) << "minijail_mount(\"/run/daemon-store/debugd\") failed"; |
mhasank | 545ccff | 2020-04-01 12:10:54 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // Mount /run/dbus to be able to communicate with D-Bus. |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame] | 69 | if (minijail_bind(j.get(), "/run/dbus", "/run/dbus", 0)) |
| 70 | LOG(FATAL) << "minijail_bind(\"/run/dbus\") failed"; |
| 71 | |
David Valleau | e694deb | 2018-07-30 17:19:11 -0700 | [diff] [blame] | 72 | // 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] | 73 | minijail_mount_tmp(j.get()); |
Mike Frysinger | 97000d8 | 2018-06-05 11:36:12 -0400 | [diff] [blame] | 74 | // In case we start before cups, make sure the path exists. |
| 75 | mkdir("/run/cups", 0755); |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame] | 76 | if (minijail_bind(j.get(), "/run/cups", "/run/cups", 0)) |
| 77 | LOG(FATAL) << "minijail_bind(\"/run/cups\") failed"; |
David Valleau | e694deb | 2018-07-30 17:19:11 -0700 | [diff] [blame] | 78 | // In case we start before upstart-socket-bridge, make sure the path exists. |
| 79 | mkdir("/run/ippusb", 0755); |
| 80 | // Mount /run/ippusb to be able to communicate with CUPS. |
| 81 | if (minijail_bind(j.get(), "/run/ippusb", "/run/ippusb", 0)) |
| 82 | LOG(FATAL) << "minijail_bind(\"/run/ippusb\") failed"; |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame] | 83 | |
David Valleau | 3339ca2 | 2019-03-19 13:33:24 -0700 | [diff] [blame] | 84 | // In case we start before avahi-daemon, make sure the path exists. |
| 85 | mkdir("/var/run/avahi-daemon", 0755); |
| 86 | // Mount /run/avahi-daemon in order to perform mdns name resolution. |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 87 | if (minijail_bind(j.get(), "/run/avahi-daemon", "/run/avahi-daemon", 0)) |
David Valleau | 3339ca2 | 2019-03-19 13:33:24 -0700 | [diff] [blame] | 88 | LOG(FATAL) << "minijail_bind(\"/run/avahi-daemon\") failed"; |
| 89 | |
Mike Frysinger | 65884e6 | 2018-07-09 13:41:26 -0400 | [diff] [blame] | 90 | // Since shill provides network resolution settings, bind mount it. |
| 91 | // In case we start before shill, make sure the path exists. |
| 92 | mkdir("/run/shill", 0755); |
| 93 | if (minijail_bind(j.get(), "/run/shill", "/run/shill", 0)) |
| 94 | LOG(FATAL) << "minijail_bind(\"/run/shill\") failed"; |
| 95 | |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame] | 96 | // Mount /dev to be able to inspect devices. |
| 97 | if (minijail_mount_with_data(j.get(), "/dev", "/dev", "bind", |
| 98 | MS_BIND | MS_REC, nullptr)) { |
| 99 | LOG(FATAL) << "minijail_mount_with_data(\"/dev\") failed"; |
| 100 | } |
| 101 | |
| 102 | // Mount /sys to access some logs. |
| 103 | if (minijail_mount_with_data(j.get(), "/sys", "/sys", "bind", |
| 104 | MS_BIND | MS_REC, nullptr)) { |
| 105 | LOG(FATAL) << "minijail_mount_with_data(\"/sys\") failed"; |
| 106 | } |
| 107 | |
Jesse Schettler | e127764 | 2020-02-27 12:23:18 -0700 | [diff] [blame] | 108 | // Mount /run/chromeos-config/v1 to access chromeos-config. |
| 109 | if (minijail_bind(j.get(), "/run/chromeos-config/v1", |
| 110 | "/run/chromeos-config/v1", 0)) { |
| 111 | LOG(FATAL) << "minijail_bind(\"/run/chromeos-config/v1\") failed"; |
| 112 | } |
| 113 | |
Andrey Pronin | 0f029d8 | 2018-07-11 15:25:26 -0700 | [diff] [blame] | 114 | if (USE_TPM) { |
| 115 | // For TPM 1.2 only: Enable utilities that communicate with TPM via tcsd - |
| 116 | // mount directory containing tcsd socket. |
| 117 | mkdir(kTcsdDir, 0755); |
| 118 | if (minijail_bind(j.get(), kTcsdDir, kTcsdDir, 0)) { |
| 119 | LOG(FATAL) << "minijail_bind(\"" << kTcsdDir << "\") failed"; |
| 120 | } |
| 121 | } |
| 122 | |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame] | 123 | minijail_enter(j.get()); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 126 | class Daemon : public brillo::DBusServiceDaemon { |
| 127 | public: |
Eric Caruso | 5153585 | 2017-07-11 14:13:20 -0700 | [diff] [blame] | 128 | Daemon() : DBusServiceDaemon(debugd::kDebugdServiceName) {} |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 129 | Daemon(const Daemon&) = delete; |
| 130 | Daemon& operator=(const Daemon&) = delete; |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 131 | |
| 132 | protected: |
| 133 | void RegisterDBusObjectsAsync( |
Eric Caruso | b298bb4 | 2017-05-09 10:53:30 -0700 | [diff] [blame] | 134 | brillo::dbus_utils::AsyncEventSequencer* sequencer) override { |
Eric Caruso | 5153585 | 2017-07-11 14:13:20 -0700 | [diff] [blame] | 135 | adaptor_.reset(new debugd::DebugdDBusAdaptor(bus_)); |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 136 | adaptor_->RegisterAsync( |
| 137 | sequencer->GetHandler("RegisterAsync() failed.", true)); |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | private: |
| 141 | std::unique_ptr<debugd::DebugdDBusAdaptor> adaptor_; |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 142 | }; |
Ben Chan | af12586 | 2017-02-08 23:11:18 -0800 | [diff] [blame] | 143 | |
| 144 | } // namespace |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 145 | |
Ben Chan | c572aff | 2018-04-10 09:20:27 -0700 | [diff] [blame] | 146 | int main(int argc, char* argv[]) { |
Mike Frysinger | 0597cdd | 2020-03-18 05:32:21 -0400 | [diff] [blame] | 147 | brillo::FlagHelper::Init(argc, argv, "CrOS debug daemon"); |
Mike Frysinger | b2f7ac5 | 2018-05-17 02:36:23 -0400 | [diff] [blame] | 148 | brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderrIfTty); |
Mike Frysinger | 0597cdd | 2020-03-18 05:32:21 -0400 | [diff] [blame] | 149 | if (argc != 1) { |
| 150 | LOG(ERROR) << "debugd takes no arguments"; |
| 151 | return 1; |
| 152 | } |
| 153 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 154 | enter_vfs_namespace(); |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 155 | Daemon().Run(); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 156 | return 0; |
| 157 | } |