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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
// 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.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "mojo/core/ipcz_driver/driver.h"
#include <cstddef>
#include <cstdint>
#include <tuple>
#include <utility>
#include "base/containers/span.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/rand_util.h"
#include "mojo/core/ipcz_driver/object.h"
#include "mojo/core/ipcz_driver/shared_buffer.h"
#include "mojo/core/ipcz_driver/shared_buffer_mapping.h"
#include "mojo/core/ipcz_driver/transmissible_platform_handle.h"
#include "mojo/core/ipcz_driver/transport.h"
#include "mojo/public/cpp/platform/platform_handle.h"
#include "third_party/ipcz/include/ipcz/ipcz.h"
namespace mojo::core::ipcz_driver {
namespace {
IpczResult IPCZ_API Close(IpczDriverHandle handle,
uint32_t flags,
const void* options) {
scoped_refptr<ObjectBase> object = ObjectBase::TakeFromHandle(handle);
if (!object) {
return IPCZ_RESULT_INVALID_ARGUMENT;
}
object->Close();
return IPCZ_RESULT_OK;
}
IpczResult IPCZ_API Serialize(IpczDriverHandle handle,
IpczDriverHandle transport_handle,
uint32_t flags,
const void* options,
volatile void* data,
size_t* num_bytes,
IpczDriverHandle* handles,
size_t* num_handles) {
ObjectBase* object = ObjectBase::FromHandle(handle);
Transport* transport = Transport::FromHandle(transport_handle);
if (!object || !object->IsSerializable()) {
return IPCZ_RESULT_INVALID_ARGUMENT;
}
if (!transport) {
return IPCZ_RESULT_ABORTED;
}
// TODO(crbug.com/40270656): Propagate the volatile qualifier on
// `data`.
const IpczResult result = transport->SerializeObject(
*object, const_cast<void*>(data), num_bytes, handles, num_handles);
if (result != IPCZ_RESULT_OK) {
return result;
}
// On success we consume the object reference owned by the input handle.
std::ignore = ObjectBase::TakeFromHandle(handle);
return IPCZ_RESULT_OK;
}
IpczResult IPCZ_API Deserialize(const volatile void* data,
size_t num_bytes,
const IpczDriverHandle* handles,
size_t num_handles,
IpczDriverHandle transport_handle,
uint32_t flags,
const void* options,
IpczDriverHandle* driver_handle) {
Transport* transport = Transport::FromHandle(transport_handle);
if (!transport || !driver_handle) {
return IPCZ_RESULT_INVALID_ARGUMENT;
}
// TODO(crbug.com/40270656): Propagate the volatile qualifier on
// `data`.
scoped_refptr<ObjectBase> object;
const IpczResult result = transport->DeserializeObject(
base::span(static_cast<const uint8_t*>(const_cast<const void*>(data)),
num_bytes),
base::span(handles, num_handles), object);
if (result != IPCZ_RESULT_OK) {
return result;
}
*driver_handle = ObjectBase::ReleaseAsHandle(std::move(object));
return IPCZ_RESULT_OK;
}
IpczResult IPCZ_API
CreateTransports(IpczDriverHandle existing_transport_from_here_to_a,
IpczDriverHandle existing_transport_from_here_to_b,
uint32_t flags,
const void* options,
IpczDriverHandle* new_transport_from_a_to_b,
IpczDriverHandle* new_transport_from_b_to_a) {
// For two existing transports from the calling node (one to a node A and
// another to a node B), this creates a new transport that will be used to
// connect A and B directly to each other.
//
// The output `new_transport_from_a_to_b` is created to be sent to node A via
// `existing_transport_from_here_to_a`, while the output
// `new_transport_from_b_to_a` will be sent to node B via
// `existing_transport_from_here_to_b`.
//
// This function does not actually send the transports though: it only creates
// them and configures them with appropriate relative levels of trust in each
// other.
Transport* our_transport_to_a =
Transport::FromHandle(existing_transport_from_here_to_a);
Transport* our_transport_to_b =
Transport::FromHandle(existing_transport_from_here_to_b);
if (!our_transport_to_a || !our_transport_to_b) {
return IPCZ_RESULT_INVALID_ARGUMENT;
}
const Transport::EndpointType node_a_type =
our_transport_to_a->destination_type();
const Transport::EndpointType node_b_type =
our_transport_to_b->destination_type();
auto [transport_from_a_to_b, transport_from_b_to_a] =
Transport::CreatePair(node_a_type, node_b_type);
if (node_a_type == Transport::EndpointType::kBroker) {
// If node A is a broker, give its new endpoint a handle to node B's process
// when possible.
transport_from_a_to_b->set_remote_process(
our_transport_to_b->remote_process().Duplicate());
}
// A can trust B if we trust B. Note that this is only true if A also trusts
// us, which A must validate when accepting this transport from us. See
// Transport::Deserialize() for that validation.
transport_from_a_to_b->set_is_peer_trusted(
our_transport_to_b->is_peer_trusted());
// If A can assume it's trusted by us then it can also assume it's trusted by
// B, because we will tell B to do so.
transport_from_a_to_b->set_is_trusted_by_peer(
our_transport_to_a->is_trusted_by_peer());
if (node_b_type == Transport::EndpointType::kBroker) {
// If node B is a broker, give its new endpoint a handle to node A's process
// when possible.
transport_from_b_to_a->set_remote_process(
our_transport_to_a->remote_process().Duplicate());
}
// Similar to above: B can trust A if we trust A; and if B can assume it's
// trusted by us then it can assume it's trusted by A too.
transport_from_b_to_a->set_is_peer_trusted(
our_transport_to_a->is_peer_trusted());
transport_from_b_to_a->set_is_trusted_by_peer(
our_transport_to_b->is_trusted_by_peer());
*new_transport_from_a_to_b =
ObjectBase::ReleaseAsHandle(std::move(transport_from_a_to_b));
*new_transport_from_b_to_a =
ObjectBase::ReleaseAsHandle(std::move(transport_from_b_to_a));
return IPCZ_RESULT_OK;
}
IpczResult IPCZ_API
ActivateTransport(IpczDriverHandle transport_handle,
IpczHandle listener,
IpczTransportActivityHandler activity_handler,
uint32_t flags,
const void* options) {
Transport* transport = Transport::FromHandle(transport_handle);
if (!transport) {
return IPCZ_RESULT_INVALID_ARGUMENT;
}
transport->Activate(listener, activity_handler);
return IPCZ_RESULT_OK;
}
IpczResult IPCZ_API DeactivateTransport(IpczDriverHandle transport_handle,
uint32_t flags,
const void* options) {
Transport* transport = Transport::FromHandle(transport_handle);
if (!transport) {
return IPCZ_RESULT_INVALID_ARGUMENT;
}
transport->Deactivate();
return IPCZ_RESULT_OK;
}
IpczResult IPCZ_API Transmit(IpczDriverHandle transport_handle,
const void* data,
size_t num_bytes,
const IpczDriverHandle* handles,
size_t num_handles,
uint32_t flags,
const void* options) {
Transport* transport = Transport::FromHandle(transport_handle);
if (!transport) {
return IPCZ_RESULT_INVALID_ARGUMENT;
}
transport->Transmit(base::span(static_cast<const uint8_t*>(data), num_bytes),
base::span(handles, num_handles));
return IPCZ_RESULT_OK;
}
IpczResult IPCZ_API
ReportBadTransportActivity(IpczDriverHandle transport_handle,
uintptr_t context,
uint32_t flags,
const void* options) {
Transport* transport = Transport::FromHandle(transport_handle);
if (!transport) {
return IPCZ_RESULT_INVALID_ARGUMENT;
}
std::unique_ptr<std::string> error_message(
reinterpret_cast<std::string*>(context));
transport->ReportBadActivity(*error_message);
return IPCZ_RESULT_OK;
}
IpczResult IPCZ_API AllocateSharedMemory(size_t num_bytes,
uint32_t flags,
const void* options,
IpczDriverHandle* driver_memory) {
auto region = base::UnsafeSharedMemoryRegion::Create(num_bytes);
*driver_memory = SharedBuffer::ReleaseAsHandle(
SharedBuffer::MakeForRegion(std::move(region)));
return IPCZ_RESULT_OK;
}
IpczResult IPCZ_API GetSharedMemoryInfo(IpczDriverHandle driver_memory,
uint32_t flags,
const void* options,
IpczSharedMemoryInfo* info) {
SharedBuffer* buffer = SharedBuffer::FromHandle(driver_memory);
if (!buffer || !info || info->size < sizeof(*info)) {
return IPCZ_RESULT_INVALID_ARGUMENT;
}
info->region_num_bytes = buffer->region().GetSize();
return IPCZ_RESULT_OK;
}
IpczResult IPCZ_API DuplicateSharedMemory(IpczDriverHandle driver_memory,
uint32_t flags,
const void* options,
IpczDriverHandle* new_driver_memory) {
SharedBuffer* buffer = SharedBuffer::FromHandle(driver_memory);
if (!buffer || !new_driver_memory) {
return IPCZ_RESULT_INVALID_ARGUMENT;
}
base::UnsafeSharedMemoryRegion new_region =
base::UnsafeSharedMemoryRegion::Deserialize(buffer->region().Duplicate());
if (!new_region.IsValid()) {
return IPCZ_RESULT_RESOURCE_EXHAUSTED;
}
*new_driver_memory = SharedBuffer::ReleaseAsHandle(
SharedBuffer::MakeForRegion(std::move(new_region)));
return IPCZ_RESULT_OK;
}
IpczResult IPCZ_API MapSharedMemory(IpczDriverHandle driver_memory,
uint32_t flags,
const void* options,
volatile void** address,
IpczDriverHandle* driver_mapping) {
SharedBuffer* buffer = SharedBuffer::FromHandle(driver_memory);
if (!buffer || !driver_mapping) {
return IPCZ_RESULT_INVALID_ARGUMENT;
}
scoped_refptr<SharedBufferMapping> mapping =
SharedBufferMapping::Create(buffer->region());
if (!mapping) {
return IPCZ_RESULT_RESOURCE_EXHAUSTED;
}
*address = mapping->memory();
*driver_mapping = SharedBufferMapping::ReleaseAsHandle(std::move(mapping));
return IPCZ_RESULT_OK;
}
IpczResult IPCZ_API GenerateRandomBytes(size_t num_bytes,
uint32_t flags,
const void* options,
void* buffer) {
if (!buffer || !num_bytes) {
return IPCZ_RESULT_INVALID_ARGUMENT;
}
base::RandBytes(
// SAFETY: This requires the caller to provide a valid pointer/size pair.
// The function API is a C API so can't use a span.
UNSAFE_BUFFERS(base::span(static_cast<uint8_t*>(buffer), num_bytes)));
return IPCZ_RESULT_OK;
}
} // namespace
const IpczDriver kDriver = {
sizeof(kDriver),
Close,
Serialize,
Deserialize,
CreateTransports,
ActivateTransport,
DeactivateTransport,
Transmit,
ReportBadTransportActivity,
AllocateSharedMemory,
GetSharedMemoryInfo,
DuplicateSharedMemory,
MapSharedMemory,
GenerateRandomBytes,
};
} // namespace mojo::core::ipcz_driver
|