1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/guest_os/public/guest_os_wayland_server.h"
#include <algorithm>
#include <memory>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "chrome/browser/ash/borealis/borealis_security_delegate.h"
#include "chrome/browser/ash/crostini/crostini_security_delegate.h"
#include "chrome/browser/ash/guest_os/guest_os_security_delegate.h"
#include "chrome/browser/ash/guest_os/public/guest_os_service.h"
#include "chrome/browser/ash/guest_os/public/guest_os_service_factory.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chromeos/ash/components/dbus/vm_wl/wl.pb.h"
#include "components/exo/server/wayland_server_controller.h"
#include "components/exo/server/wayland_server_handle.h"
namespace guest_os {
GuestOsWaylandServer::ScopedServer::ScopedServer(
std::unique_ptr<exo::WaylandServerHandle> handle,
base::WeakPtr<GuestOsSecurityDelegate> security_delegate)
: handle_(std::move(handle)), security_delegate_(security_delegate) {}
GuestOsWaylandServer::ScopedServer::~ScopedServer() = default;
// static
void GuestOsWaylandServer::ListenOnSocket(
const vm_tools::wl::ListenOnSocketRequest& request,
base::ScopedFD socket_fd,
base::OnceCallback<void(std::optional<std::string>)> response_callback) {
Profile* profile = ProfileManager::GetPrimaryUserProfile();
if (!profile || ash::ProfileHelper::GetUserIdHashFromProfile(profile) !=
request.desc().owner_id()) {
std::move(response_callback).Run({"Invalid owner_id"});
return;
}
GuestOsServiceFactory::GetForProfile(profile)->WaylandServer()->Listen(
std::move(socket_fd), request.desc().type(), request.desc().name(),
std::move(response_callback));
}
// static
void GuestOsWaylandServer::CloseSocket(
const vm_tools::wl::CloseSocketRequest& request,
base::OnceCallback<void(std::optional<std::string>)> response_callback) {
Profile* profile = ProfileManager::GetPrimaryUserProfile();
if (!profile || ash::ProfileHelper::GetUserIdHashFromProfile(profile) !=
request.desc().owner_id()) {
std::move(response_callback).Run({"Invalid owner_id"});
return;
}
GuestOsServiceFactory::GetForProfile(profile)->WaylandServer()->Close(
request.desc().type(), request.desc().name(),
std::move(response_callback));
}
GuestOsWaylandServer::GuestOsWaylandServer(Profile* profile)
: profile_(profile) {
// Cleanup is best-effort, so don't bother if for some reason we
// can't get a handle to the service (like tests).
if (auto* concierge = ash::ConciergeClient::Get(); concierge) {
concierge->AddObserver(this);
}
}
GuestOsWaylandServer::~GuestOsWaylandServer() {
// ConciergeClient may be destroyed prior to GuestOsWaylandServer in tests.
// Therefore we do this instead of ScopedObservation.
if (auto* concierge = ash::ConciergeClient::Get(); concierge) {
concierge->RemoveObserver(this);
}
}
// Returns a weak handle to the security delegate for the VM with the given
// |name| and |type|, if one exists, and nullptr otherwise.
base::WeakPtr<GuestOsSecurityDelegate> GuestOsWaylandServer::GetDelegate(
vm_tools::apps::VmType type,
const std::string& name) const {
auto type_iter = servers_.find(type);
if (type_iter == servers_.end()) {
return nullptr;
}
auto name_iter = type_iter->second.find(name);
if (name_iter == type_iter->second.end()) {
return nullptr;
}
return name_iter->second->security_delegate();
}
void GuestOsWaylandServer::Listen(base::ScopedFD fd,
vm_tools::apps::VmType type,
const std::string& name,
ResponseCallback callback) {
if (servers_[type].erase(name) > 0) {
LOG(WARNING) << "Re-binding wayland server for " << name << "(type=" << type
<< ") while in-use";
}
switch (type) {
case vm_tools::apps::TERMINA:
crostini::CrostiniSecurityDelegate::Build(
profile_, name,
base::BindOnce(&GuestOsWaylandServer::OnSecurityDelegateCreated,
weak_factory_.GetWeakPtr(), std::move(fd), type, name,
std::move(callback)));
return;
case vm_tools::apps::BOREALIS:
borealis::BorealisSecurityDelegate::Build(
profile_, name,
base::BindOnce(&GuestOsWaylandServer::OnSecurityDelegateCreated,
weak_factory_.GetWeakPtr(), std::move(fd), type, name,
std::move(callback)));
return;
default:
// For all other VMs, provide the minimal capability-set.
OnSecurityDelegateCreated(
std::move(fd), type, name, std::move(callback),
std::make_unique<GuestOsSecurityDelegate>(name));
return;
}
}
void GuestOsWaylandServer::Close(vm_tools::apps::VmType type,
const std::string& name,
ResponseCallback callback) {
if (servers_[type].erase(name) == 0) {
LOG(WARNING) << "Trying to close non-existent server for " << name
<< "(type=" << type << ")";
}
std::move(callback).Run(std::nullopt);
}
void GuestOsWaylandServer::OnSecurityDelegateCreated(
base::ScopedFD fd,
vm_tools::apps::VmType type,
std::string name,
ResponseCallback callback,
std::unique_ptr<GuestOsSecurityDelegate> delegate) {
if (!delegate) {
std::move(callback).Run({"Failed to get security privileges"});
return;
}
GuestOsSecurityDelegate::MakeServerWithFd(
std::move(delegate), std::move(fd),
base::BindOnce(&GuestOsWaylandServer::OnServerCreated,
weak_factory_.GetWeakPtr(), type, std::move(name),
std::move(callback)));
}
void GuestOsWaylandServer::OnServerCreated(
vm_tools::apps::VmType type,
std::string name,
ResponseCallback callback,
base::WeakPtr<GuestOsSecurityDelegate> delegate,
std::unique_ptr<exo::WaylandServerHandle> handle) {
if (!handle) {
std::move(callback).Run({"Failed to create wayland server"});
return;
}
servers_[type].insert_or_assign(
std::move(name),
std::make_unique<ScopedServer>(std::move(handle), delegate));
std::move(callback).Run(std::nullopt);
}
void GuestOsWaylandServer::ConciergeServiceStarted() {
// Do nothing.
}
void GuestOsWaylandServer::ConciergeServiceStopped() {
servers_.clear();
}
} // namespace guest_os
|