blob: f5ab37c828ceabb625cb0152e34297f25c682d6b [file] [log] [blame]
Yi Chou9d24b462020-12-04 01:12:57 +08001// Copyright 2021 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6#include <utility>
7
8#include <base/callback.h>
9#include <base/files/file_util.h>
10#include <base/files/file.h>
Yi Chou9d24b462020-12-04 01:12:57 +080011#include <base/logging.h>
12#include <base/posix/eintr_wrapper.h>
Yi Chou9d24b462020-12-04 01:12:57 +080013#include <fcntl.h>
Yi Choudee22a52020-12-07 15:06:22 +080014#include <libminijail.h>
Yi Chou9d24b462020-12-04 01:12:57 +080015#include <linux/vtpm_proxy.h>
Yi Choudee22a52020-12-07 15:06:22 +080016#include <scoped_minijail.h>
17#include <signal.h>
Yi Chou9d24b462020-12-04 01:12:57 +080018#include <sys/ioctl.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <sysexits.h>
Yi Chou9d24b462020-12-04 01:12:57 +080022#include <unistd.h>
23
24#include "tpm2-simulator/simulator.h"
25
26namespace {
Yi Choudee22a52020-12-07 15:06:22 +080027constexpr char kSimulatorUser[] = "tpm2-simulator";
28constexpr char kSimulatorGroup[] = "tpm2-simulator";
29constexpr char kSimulatorSeccompPath[] =
30 "/usr/share/policy/tpm2-simulator.policy";
Yi Chou9d24b462020-12-04 01:12:57 +080031constexpr char kVtpmxPath[] = "/dev/vtpmx";
Yi Choudee22a52020-12-07 15:06:22 +080032constexpr char kDevTpmPathPrefix[] = "/dev/tpm";
Yi Chouf964bab2020-12-10 11:50:28 +080033constexpr size_t kMaxCommandSize = 4096;
Yi Chou9d24b462020-12-04 01:12:57 +080034constexpr size_t kHeaderSize = 10;
Yi Chou9d24b462020-12-04 01:12:57 +080035
Yi Choudee22a52020-12-07 15:06:22 +080036base::ScopedFD RegisterVTPM(base::FilePath* tpm_path) {
Yi Chou9d24b462020-12-04 01:12:57 +080037 struct vtpm_proxy_new_dev new_dev = {};
38 new_dev.flags = VTPM_PROXY_FLAG_TPM2;
39 base::ScopedFD vtpmx_fd(HANDLE_EINTR(open(kVtpmxPath, O_RDWR | O_CLOEXEC)));
40 if (!vtpmx_fd.is_valid()) {
41 return vtpmx_fd;
42 }
43 if (ioctl(vtpmx_fd.get(), VTPM_PROXY_IOC_NEW_DEV, &new_dev) < 0) {
44 PLOG(ERROR) << "Create vTPM failed.";
45 // return an invalid FD.
46 return {};
47 }
Yi Choudee22a52020-12-07 15:06:22 +080048 *tpm_path =
49 base::FilePath(kDevTpmPathPrefix + std::to_string(new_dev.tpm_num));
Yi Chou9d24b462020-12-04 01:12:57 +080050 LOG(INFO) << "Create TPM at: /dev/tpm" << new_dev.tpm_num;
51 return base::ScopedFD(new_dev.fd);
52}
53
Yi Choudee22a52020-12-07 15:06:22 +080054void InitMinijailSandbox() {
55 ScopedMinijail j(minijail_new());
56 minijail_no_new_privs(j.get());
57 minijail_log_seccomp_filter_failures(j.get());
58 minijail_parse_seccomp_filters(j.get(), kSimulatorSeccompPath);
59 minijail_use_seccomp_filter(j.get());
60 minijail_change_user(j.get(), kSimulatorUser);
61 minijail_change_group(j.get(), kSimulatorGroup);
62 minijail_inherit_usergroups(j.get());
63 minijail_enter(j.get());
64}
65
Yi Chou9d24b462020-12-04 01:12:57 +080066} // namespace
67
68namespace tpm2_simulator {
69
Yi Chouf964bab2020-12-10 11:50:28 +080070SimulatorDaemon::SimulatorDaemon(TpmExecutor* tpm_executor)
71 : tpm_executor_(tpm_executor) {}
72
Yi Chou9d24b462020-12-04 01:12:57 +080073int SimulatorDaemon::OnInit() {
Yi Chouf964bab2020-12-10 11:50:28 +080074 CHECK(tpm_executor_);
Yi Chou9d24b462020-12-04 01:12:57 +080075 int exit_code = Daemon::OnInit();
76 if (exit_code != EX_OK)
77 return exit_code;
Yi Chouf964bab2020-12-10 11:50:28 +080078 tpm_executor_->InitializeVTPM();
Yi Choudee22a52020-12-07 15:06:22 +080079 base::FilePath tpm_path;
80 command_fd_ = RegisterVTPM(&tpm_path);
Yi Chou9d24b462020-12-04 01:12:57 +080081 if (!command_fd_.is_valid()) {
82 LOG(ERROR) << "Failed to register vTPM";
83 return EX_OSERR;
84 }
85 command_fd_watcher_ = base::FileDescriptorWatcher::WatchReadable(
86 command_fd_.get(),
87 base::BindRepeating(&SimulatorDaemon::OnCommand, base::Unretained(this)));
Yi Choudee22a52020-12-07 15:06:22 +080088 tpm_watcher_.reset(new base::FilePathWatcher);
89 tpm_watcher_->Watch(
90 tpm_path, false,
91 base::Bind(&SimulatorDaemon::OnTpmPathChange, base::Unretained(this)));
Yi Chou9d24b462020-12-04 01:12:57 +080092 return EX_OK;
93}
94
95void SimulatorDaemon::OnCommand() {
Yi Chouf964bab2020-12-10 11:50:28 +080096 CHECK(tpm_executor_);
Yi Chou9d24b462020-12-04 01:12:57 +080097 char buffer[kMaxCommandSize];
98 do {
99 std::string request;
100 remain_request_.swap(request);
101
102 // Read request header.
103 while (kHeaderSize > request.size()) {
104 ssize_t size =
105 HANDLE_EINTR(read(command_fd_.get(), buffer, kMaxCommandSize));
106 CHECK_GE(size, 0);
107 request.append(buffer, size);
108 }
109
Yi Chouf964bab2020-12-10 11:50:28 +0800110 const uint32_t command_size = tpm_executor_->GetCommandSize(request);
Yi Chou9d24b462020-12-04 01:12:57 +0800111
112 // Read request body.
113 while (command_size > request.size()) {
114 ssize_t size =
115 HANDLE_EINTR(read(command_fd_.get(), buffer, kMaxCommandSize));
116 CHECK_GE(size, 0);
117 request.append(buffer, size);
118 }
119
120 // Trim request.
121 if (command_size < request.size()) {
122 remain_request_ = request.substr(command_size);
123 request.resize(command_size);
124 }
125
126 // Run command.
Yi Chouf964bab2020-12-10 11:50:28 +0800127 std::string response = tpm_executor_->RunCommand(request);
Yi Chou9d24b462020-12-04 01:12:57 +0800128
129 // Write response.
130 if (!base::WriteFileDescriptor(command_fd_.get(), response.c_str(),
131 response.size())) {
132 PLOG(ERROR) << "WriteFileDescriptor failed.";
133 }
134 } while (!remain_request_.empty());
135}
136
Yi Choudee22a52020-12-07 15:06:22 +0800137void SimulatorDaemon::OnTpmPathChange(const base::FilePath& path, bool error) {
138 if (error) {
139 LOG(ERROR) << "Got error while hearing about change to " << path.value();
140 return;
141 }
142 if (!initialized_ && base::PathExists(path)) {
143 LOG(INFO) << "vTPM initialized: " << path.value();
144 tpm_watcher_.reset();
145 initialized_ = true;
146 if (sigstop_on_initialized_) {
147 // Raise the SIGSTOP, so upstart would know the initialization process had
148 // been finished.
149 raise(SIGSTOP);
150 }
151 // Initialize the minijail.
152 InitMinijailSandbox();
153 }
154}
155
Yi Chou9d24b462020-12-04 01:12:57 +0800156} // namespace tpm2_simulator