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> |
Alex Vakulenko | e769653 | 2015-10-16 16:27:29 -0700 | [diff] [blame] | 12 | #include <brillo/syslog_logging.h> |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 13 | #include <chromeos/dbus/service_constants.h> |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 14 | #include <chromeos/libminijail.h> |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame^] | 15 | #include <chromeos/scoped_minijail.h> |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 16 | |
Eric Caruso | 63c9743 | 2017-04-27 13:23:28 -0700 | [diff] [blame] | 17 | #include "debugd/src/debugd_dbus_adaptor.h" |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 18 | |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 19 | namespace { |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 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() { |
Luis Hector Chavez | ce837b8 | 2018-05-09 18:08:38 -0700 | [diff] [blame^] | 25 | ScopedMinijail j(minijail_new()); |
| 26 | |
| 27 | // Create a minimalistic mount namespace with just the bare minimum required. |
| 28 | minijail_namespace_vfs(j.get()); |
| 29 | if (minijail_enter_pivot_root(j.get(), "/var/empty")) |
| 30 | LOG(FATAL) << "minijail_enter_pivot_root() failed"; |
| 31 | if (minijail_bind(j.get(), "/", "/", 0)) |
| 32 | LOG(FATAL) << "minijail_bind(\"/\") failed"; |
| 33 | if (minijail_mount_with_data(j.get(), "none", "/proc", "proc", |
| 34 | MS_NOSUID | MS_NOEXEC | MS_NODEV, nullptr)) { |
| 35 | LOG(FATAL) << "minijail_mount_with_data(\"/proc\") failed"; |
| 36 | } |
| 37 | if (minijail_bind(j.get(), "/var", "/var", 1)) |
| 38 | LOG(FATAL) << "minijail_bind(\"/var\") failed"; |
| 39 | |
| 40 | // Mount /run/dbus to be able to communicate with D-Bus. |
| 41 | if (minijail_mount_with_data(j.get(), "tmpfs", "/run", "tmpfs", |
| 42 | MS_NOSUID | MS_NOEXEC | MS_NODEV, nullptr)) { |
| 43 | LOG(FATAL) << "minijail_mount_with_data(\"/run\") failed"; |
| 44 | } |
| 45 | if (minijail_bind(j.get(), "/run/dbus", "/run/dbus", 0)) |
| 46 | LOG(FATAL) << "minijail_bind(\"/run/dbus\") failed"; |
| 47 | |
| 48 | // Mount /tmp and /run/cups to be able to communicate with CUPS. |
| 49 | if (minijail_bind(j.get(), "/tmp", "/tmp", 1)) |
| 50 | LOG(FATAL) << "minijail_bind(\"/tmp\") failed"; |
| 51 | if (minijail_bind(j.get(), "/run/cups", "/run/cups", 0)) |
| 52 | LOG(FATAL) << "minijail_bind(\"/run/cups\") failed"; |
| 53 | |
| 54 | // Mount /dev to be able to inspect devices. |
| 55 | if (minijail_mount_with_data(j.get(), "/dev", "/dev", "bind", |
| 56 | MS_BIND | MS_REC, nullptr)) { |
| 57 | LOG(FATAL) << "minijail_mount_with_data(\"/dev\") failed"; |
| 58 | } |
| 59 | |
| 60 | // Mount /sys to access some logs. |
| 61 | if (minijail_mount_with_data(j.get(), "/sys", "/sys", "bind", |
| 62 | MS_BIND | MS_REC, nullptr)) { |
| 63 | LOG(FATAL) << "minijail_mount_with_data(\"/sys\") failed"; |
| 64 | } |
| 65 | |
| 66 | minijail_enter(j.get()); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 69 | class Daemon : public brillo::DBusServiceDaemon { |
| 70 | public: |
Eric Caruso | 5153585 | 2017-07-11 14:13:20 -0700 | [diff] [blame] | 71 | Daemon() : DBusServiceDaemon(debugd::kDebugdServiceName) {} |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 72 | |
| 73 | protected: |
| 74 | void RegisterDBusObjectsAsync( |
Eric Caruso | b298bb4 | 2017-05-09 10:53:30 -0700 | [diff] [blame] | 75 | brillo::dbus_utils::AsyncEventSequencer* sequencer) override { |
Eric Caruso | 5153585 | 2017-07-11 14:13:20 -0700 | [diff] [blame] | 76 | adaptor_.reset(new debugd::DebugdDBusAdaptor(bus_)); |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 77 | adaptor_->RegisterAsync(sequencer->GetHandler( |
| 78 | "RegisterAsync() failed.", true)); |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | std::unique_ptr<debugd::DebugdDBusAdaptor> adaptor_; |
| 83 | |
| 84 | DISALLOW_COPY_AND_ASSIGN(Daemon); |
| 85 | }; |
Ben Chan | af12586 | 2017-02-08 23:11:18 -0800 | [diff] [blame] | 86 | |
| 87 | } // namespace |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 88 | |
Ben Chan | c572aff | 2018-04-10 09:20:27 -0700 | [diff] [blame] | 89 | int main(int argc, char* argv[]) { |
Alex Vakulenko | 274c74a | 2015-04-02 14:31:10 -0700 | [diff] [blame] | 90 | base::CommandLine::Init(argc, argv); |
Mike Frysinger | b2f7ac5 | 2018-05-17 02:36:23 -0400 | [diff] [blame] | 91 | brillo::InitLog(brillo::kLogToSyslog | brillo::kLogToStderrIfTty); |
Elly Jones | e7cb5b3 | 2011-12-01 14:18:32 -0500 | [diff] [blame] | 92 | enter_vfs_namespace(); |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 93 | Daemon().Run(); |
Elly Jones | 9aa5eca | 2011-11-04 14:48:13 -0400 | [diff] [blame] | 94 | return 0; |
| 95 | } |