blob: 7fdaf9a197eeb096938ddf3c1637bb510d9c41d1 [file] [log] [blame]
Elly Jonesa44d22d2012-01-05 18:05:56 -05001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Elly Jones9aa5eca2011-11-04 14:48:13 -04002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Elly Jones9aa5eca2011-11-04 14:48:13 -04005#include <sys/mount.h>
Elly Jonesd31aee22012-07-02 11:09:10 -04006#include <sys/stat.h>
Elly Jones9aa5eca2011-11-04 14:48:13 -04007#include <unistd.h>
8
Elly Jonese7cb5b32011-12-01 14:18:32 -05009#include <base/command_line.h>
10#include <base/logging.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070011#include <brillo/daemons/dbus_daemon.h>
Mike Frysinger0597cdd2020-03-18 05:32:21 -040012#include <brillo/flag_helper.h>
Alex Vakulenkoe7696532015-10-16 16:27:29 -070013#include <brillo/syslog_logging.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070014#include <chromeos/dbus/service_constants.h>
Elly Jones9aa5eca2011-11-04 14:48:13 -040015#include <chromeos/libminijail.h>
Luis Hector Chavezce837b82018-05-09 18:08:38 -070016#include <chromeos/scoped_minijail.h>
Elly Jones9aa5eca2011-11-04 14:48:13 -040017
Eric Caruso63c97432017-04-27 13:23:28 -070018#include "debugd/src/debugd_dbus_adaptor.h"
Elly Jones9aa5eca2011-11-04 14:48:13 -040019
Elly Jonese7cb5b32011-12-01 14:18:32 -050020namespace {
Elly Jones9aa5eca2011-11-04 14:48:13 -040021
Andrey Pronin0f029d82018-07-11 15:25:26 -070022// For TPM 1.2 only: Directory to mount for access to tcsd socket.
23constexpr char kTcsdDir[] = "/run/tcsd";
24
Elly Jonese7cb5b32011-12-01 14:18:32 -050025// @brief Enter a VFS namespace.
26//
27// We don't want anyone other than our descendants to see our tmpfs.
28void enter_vfs_namespace() {
Luis Hector Chavezce837b82018-05-09 18:08:38 -070029 ScopedMinijail j(minijail_new());
30
31 // Create a minimalistic mount namespace with just the bare minimum required.
32 minijail_namespace_vfs(j.get());
Allen Webb2b6e35d2019-02-21 10:06:31 -080033 if (minijail_enter_pivot_root(j.get(), "/mnt/empty"))
Luis Hector Chavezce837b82018-05-09 18:08:38 -070034 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 Frysinger25cdbe72018-08-23 01:54:00 -040044 // 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
mhasank545ccff2020-04-01 12:10:54 -070055 minijail_remount_mode(j.get(), MS_SLAVE);
56
Luis Hector Chavezce837b82018-05-09 18:08:38 -070057 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 }
mhasank545ccff2020-04-01 12:10:54 -070061
62 if (minijail_mount(j.get(), "/run/daemon-store/debugd",
63 "/run/daemon-store/debugd", "none",
64 MS_BIND | MS_REC) != 0) {
65 LOG(FATAL) << "minijail_mount(\"/run/daemon-store/debugd\") failed";
66 }
67
68 // Mount /run/dbus to be able to communicate with D-Bus.
Luis Hector Chavezce837b82018-05-09 18:08:38 -070069 if (minijail_bind(j.get(), "/run/dbus", "/run/dbus", 0))
70 LOG(FATAL) << "minijail_bind(\"/run/dbus\") failed";
71
David Valleaue694deb2018-07-30 17:19:11 -070072 // Mount /tmp, /run/cups, and /run/ippusb to be able to communicate with CUPS.
Piotr Pawliczek39752f32018-10-16 14:42:28 -070073 minijail_mount_tmp(j.get());
Mike Frysinger97000d82018-06-05 11:36:12 -040074 // In case we start before cups, make sure the path exists.
75 mkdir("/run/cups", 0755);
Luis Hector Chavezce837b82018-05-09 18:08:38 -070076 if (minijail_bind(j.get(), "/run/cups", "/run/cups", 0))
77 LOG(FATAL) << "minijail_bind(\"/run/cups\") failed";
David Valleaue694deb2018-07-30 17:19:11 -070078 // 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 Chavezce837b82018-05-09 18:08:38 -070083
David Valleau3339ca22019-03-19 13:33:24 -070084 // 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 Hughesd6c2d392020-08-24 18:12:11 -070087 if (minijail_bind(j.get(), "/run/avahi-daemon", "/run/avahi-daemon", 0))
David Valleau3339ca22019-03-19 13:33:24 -070088 LOG(FATAL) << "minijail_bind(\"/run/avahi-daemon\") failed";
89
Mike Frysinger65884e62018-07-09 13:41:26 -040090 // 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
Chris Morinb7a32eb2019-02-19 20:06:21 -080096 // Mount /run/systemd/journal to be able to log to journald.
Chris Morinb7a32eb2019-02-19 20:06:21 -080097 if (minijail_bind(j.get(), "/run/systemd/journal", "/run/systemd/journal", 0))
98 LOG(FATAL) << "minijail_bind(\"/run/systemd/journal\") failed";
99
Luis Hector Chavezce837b82018-05-09 18:08:38 -0700100 // Mount /dev to be able to inspect devices.
101 if (minijail_mount_with_data(j.get(), "/dev", "/dev", "bind",
102 MS_BIND | MS_REC, nullptr)) {
103 LOG(FATAL) << "minijail_mount_with_data(\"/dev\") failed";
104 }
105
106 // Mount /sys to access some logs.
107 if (minijail_mount_with_data(j.get(), "/sys", "/sys", "bind",
108 MS_BIND | MS_REC, nullptr)) {
109 LOG(FATAL) << "minijail_mount_with_data(\"/sys\") failed";
110 }
111
Jesse Schettlere1277642020-02-27 12:23:18 -0700112 // Mount /run/chromeos-config/v1 to access chromeos-config.
113 if (minijail_bind(j.get(), "/run/chromeos-config/v1",
114 "/run/chromeos-config/v1", 0)) {
115 LOG(FATAL) << "minijail_bind(\"/run/chromeos-config/v1\") failed";
116 }
117
Andrey Pronin0f029d82018-07-11 15:25:26 -0700118 if (USE_TPM) {
119 // For TPM 1.2 only: Enable utilities that communicate with TPM via tcsd -
120 // mount directory containing tcsd socket.
121 mkdir(kTcsdDir, 0755);
122 if (minijail_bind(j.get(), kTcsdDir, kTcsdDir, 0)) {
123 LOG(FATAL) << "minijail_bind(\"" << kTcsdDir << "\") failed";
124 }
125 }
126
Luis Hector Chavezce837b82018-05-09 18:08:38 -0700127 minijail_enter(j.get());
Elly Jones9aa5eca2011-11-04 14:48:13 -0400128}
129
Eric Carusocc7106c2017-04-27 14:22:42 -0700130class Daemon : public brillo::DBusServiceDaemon {
131 public:
Eric Caruso51535852017-07-11 14:13:20 -0700132 Daemon() : DBusServiceDaemon(debugd::kDebugdServiceName) {}
Qijiang Fan6bc59e12020-11-11 02:51:06 +0900133 Daemon(const Daemon&) = delete;
134 Daemon& operator=(const Daemon&) = delete;
Eric Carusocc7106c2017-04-27 14:22:42 -0700135
136 protected:
137 void RegisterDBusObjectsAsync(
Eric Carusob298bb42017-05-09 10:53:30 -0700138 brillo::dbus_utils::AsyncEventSequencer* sequencer) override {
Eric Caruso51535852017-07-11 14:13:20 -0700139 adaptor_.reset(new debugd::DebugdDBusAdaptor(bus_));
Tom Hughesd6c2d392020-08-24 18:12:11 -0700140 adaptor_->RegisterAsync(
141 sequencer->GetHandler("RegisterAsync() failed.", true));
Eric Carusocc7106c2017-04-27 14:22:42 -0700142 }
143
144 private:
145 std::unique_ptr<debugd::DebugdDBusAdaptor> adaptor_;
Eric Carusocc7106c2017-04-27 14:22:42 -0700146};
Ben Chanaf125862017-02-08 23:11:18 -0800147
148} // namespace
Elly Jones9aa5eca2011-11-04 14:48:13 -0400149
Ben Chanc572aff2018-04-10 09:20:27 -0700150int main(int argc, char* argv[]) {
Mike Frysinger0597cdd2020-03-18 05:32:21 -0400151 brillo::FlagHelper::Init(argc, argv, "CrOS debug daemon");
Mike Frysingerb2f7ac52018-05-17 02:36:23 -0400152 brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderrIfTty);
Mike Frysinger0597cdd2020-03-18 05:32:21 -0400153 if (argc != 1) {
154 LOG(ERROR) << "debugd takes no arguments";
155 return 1;
156 }
157
Elly Jonese7cb5b32011-12-01 14:18:32 -0500158 enter_vfs_namespace();
Eric Carusocc7106c2017-04-27 14:22:42 -0700159 Daemon().Run();
Elly Jones9aa5eca2011-11-04 14:48:13 -0400160 return 0;
161}