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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
// Copyright 2012 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/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif
#include "chrome/browser/extensions/api/messaging/native_message_process_host.h"
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <utility>
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/process/kill.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/api/messaging/native_messaging_host_manifest.h"
#include "chrome/browser/extensions/api/messaging/native_messaging_launch_from_native.h"
#include "chrome/browser/extensions/api/messaging/native_process_launcher.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/features/feature.h"
#include "net/base/file_stream.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "url/gurl.h"
namespace {
// Maximum message size in bytes for messages received from Native Messaging
// hosts. Message size is limited mainly to prevent Chrome from crashing when
// native application misbehaves (e.g. starts writing garbage to the pipe).
const size_t kMaximumNativeMessageSize = 1024 * 1024;
// Message header contains 4-byte integer size of the message.
const size_t kMessageHeaderSize = 4;
// Size of the buffer to be allocated for each read.
const size_t kReadBufferSize = 4096;
base::FilePath GetProfilePathIfEnabled(
Profile* profile,
const extensions::ExtensionId& extension_id,
const std::string& host_id) {
return extensions::ExtensionSupportsConnectionFromNativeApp(
extension_id, host_id, profile, /* log_errors = */ false)
? profile->GetPath()
: base::FilePath();
}
} // namespace
namespace extensions {
NativeMessageProcessHost::NativeMessageProcessHost(
const ExtensionId& source_extension_id,
const std::string& native_host_name,
std::unique_ptr<NativeProcessLauncher> launcher)
: client_(nullptr),
source_extension_id_(source_extension_id),
native_host_name_(native_host_name),
launcher_(std::move(launcher)),
closed_(false),
#if BUILDFLAG(IS_POSIX)
read_file_(-1),
#endif
read_pending_(false),
write_pending_(false) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
task_runner_ = content::GetIOThreadTaskRunner({});
}
NativeMessageProcessHost::~NativeMessageProcessHost() {
DCHECK(task_runner_->BelongsToCurrentThread());
if (process_.IsValid()) {
// Kill the host process if necessary to make sure we don't leave zombies.
// TODO(crbug.com/41367359): On OSX EnsureProcessTerminated() may
// block, so we have to post a task on the blocking pool.
#if BUILDFLAG(IS_MAC)
base::ThreadPool::PostTask(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
base::BindOnce(&base::EnsureProcessTerminated, std::move(process_)));
#else
base::EnsureProcessTerminated(std::move(process_));
#endif
}
}
// static
std::unique_ptr<NativeMessageHost> NativeMessageHost::Create(
content::BrowserContext* browser_context,
gfx::NativeView native_view,
const ExtensionId& source_extension_id,
const std::string& native_host_name,
bool allow_user_level,
std::string* error_message) {
return NativeMessageProcessHost::CreateWithLauncher(
source_extension_id, native_host_name,
NativeProcessLauncher::CreateDefault(
allow_user_level, native_view,
GetProfilePathIfEnabled(Profile::FromBrowserContext(browser_context),
source_extension_id, native_host_name),
/* require_native_initiated_connections = */ false,
/* connect_id = */ "", /* error_arg = */ "",
Profile::FromBrowserContext(browser_context)));
}
// static
std::unique_ptr<NativeMessageHost> NativeMessageProcessHost::CreateWithLauncher(
const ExtensionId& source_extension_id,
const std::string& native_host_name,
std::unique_ptr<NativeProcessLauncher> launcher) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
std::unique_ptr<NativeMessageHost> process(new NativeMessageProcessHost(
source_extension_id, native_host_name, std::move(launcher)));
return process;
}
void NativeMessageProcessHost::LaunchHostProcess() {
DCHECK(task_runner_->BelongsToCurrentThread());
GURL origin(std::string(kExtensionScheme) + "://" + source_extension_id_);
launcher_->Launch(
origin, native_host_name_,
base::BindOnce(&NativeMessageProcessHost::OnHostProcessLaunched,
weak_factory_.GetWeakPtr()));
}
void NativeMessageProcessHost::OnHostProcessLaunched(
NativeProcessLauncher::LaunchResult result,
base::Process process,
base::PlatformFile read_file,
std::unique_ptr<net::FileStream> read_stream,
std::unique_ptr<net::FileStream> write_stream) {
DCHECK(task_runner_->BelongsToCurrentThread());
switch (result) {
case NativeProcessLauncher::RESULT_INVALID_NAME:
Close(kInvalidNameError);
return;
case NativeProcessLauncher::RESULT_NOT_FOUND:
Close(kNotFoundError);
return;
case NativeProcessLauncher::RESULT_FORBIDDEN:
Close(kForbiddenError);
return;
case NativeProcessLauncher::RESULT_FAILED_TO_START:
Close(kFailedToStartError);
return;
case NativeProcessLauncher::RESULT_SUCCESS:
break;
}
process_ = std::move(process);
#if BUILDFLAG(IS_POSIX)
// |read_stream| owns |read_file|, yet the underlying file descript is needed
// for FileDescriptorWatcher.
read_file_ = read_file;
#endif
read_stream_ = std::move(read_stream);
write_stream_ = std::move(write_stream);
WaitRead();
DoWrite();
}
void NativeMessageProcessHost::OnMessage(const std::string& json) {
DCHECK(task_runner_->BelongsToCurrentThread());
if (closed_)
return;
// Allocate new buffer for the message.
scoped_refptr<net::IOBufferWithSize> buffer =
base::MakeRefCounted<net::IOBufferWithSize>(json.size() +
kMessageHeaderSize);
// Copy size and content of the message to the buffer.
static_assert(sizeof(uint32_t) == kMessageHeaderSize,
"kMessageHeaderSize is incorrect");
const uint32_t message_size = base::checked_cast<uint32_t>(json.size());
memcpy(buffer->data(), reinterpret_cast<const char*>(&message_size),
kMessageHeaderSize);
buffer->span()
.subspan(kMessageHeaderSize)
.copy_from_nonoverlapping(base::as_byte_span(json));
// Push new message to the write queue.
write_queue_.push(buffer);
// Send() may be called before the host process is started. In that case the
// message will be written when OnHostProcessLaunched() is called. If it's
// already started then write the message now.
if (write_stream_)
DoWrite();
}
void NativeMessageProcessHost::Start(Client* client) {
DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK(!client_);
client_ = client;
// It's safe to use base::Unretained() here because NativeMessagePort always
// deletes us on the IO thread.
task_runner_->PostTask(
FROM_HERE, base::BindOnce(&NativeMessageProcessHost::LaunchHostProcess,
weak_factory_.GetWeakPtr()));
}
scoped_refptr<base::SingleThreadTaskRunner>
NativeMessageProcessHost::task_runner() const {
return task_runner_;
}
void NativeMessageProcessHost::WaitRead() {
if (closed_)
return;
DCHECK(!read_pending_);
// On POSIX FileStream::Read() uses blocking thread pool, so it's better to
// wait for the file to become readable before calling DoRead(). Otherwise it
// would always be consuming one thread in the thread pool. On Windows
// FileStream uses overlapped IO, so that optimization isn't necessary there.
#if BUILDFLAG(IS_POSIX)
if (!read_controller_) {
read_controller_ = base::FileDescriptorWatcher::WatchReadable(
read_file_, base::BindRepeating(&NativeMessageProcessHost::DoRead,
base::Unretained(this)));
}
#else // BUILDFLAG(IS_POSIX)
DoRead();
#endif // BUILDFLAG(IS_POSIX)
}
void NativeMessageProcessHost::DoRead() {
DCHECK(task_runner_->BelongsToCurrentThread());
while (!closed_ && !read_pending_) {
read_buffer_ = base::MakeRefCounted<net::IOBufferWithSize>(kReadBufferSize);
int result =
read_stream_->Read(read_buffer_.get(), kReadBufferSize,
base::BindOnce(&NativeMessageProcessHost::OnRead,
weak_factory_.GetWeakPtr()));
HandleReadResult(result);
}
}
void NativeMessageProcessHost::OnRead(int result) {
DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK(read_pending_);
read_pending_ = false;
HandleReadResult(result);
WaitRead();
}
void NativeMessageProcessHost::HandleReadResult(int result) {
DCHECK(task_runner_->BelongsToCurrentThread());
if (closed_)
return;
if (result > 0) {
ProcessIncomingData(read_buffer_->data(), result);
} else if (result == net::ERR_IO_PENDING) {
read_pending_ = true;
} else if (result == 0 || result == net::ERR_CONNECTION_RESET) {
// On Windows we get net::ERR_CONNECTION_RESET for a broken pipe, while on
// Posix read() returns 0 in that case.
Close(kNativeHostExited);
} else {
Close(kHostInputOutputError);
}
}
void NativeMessageProcessHost::ProcessIncomingData(
const char* data, int data_size) {
DCHECK(task_runner_->BelongsToCurrentThread());
incoming_data_.append(data, data_size);
while (true) {
if (incoming_data_.size() < kMessageHeaderSize)
return;
size_t message_size =
*reinterpret_cast<const uint32_t*>(incoming_data_.data());
if (message_size > kMaximumNativeMessageSize) {
LOG(ERROR) << "Native Messaging host tried sending a message that is "
<< message_size << " bytes long.";
Close(kHostInputOutputError);
return;
}
if (incoming_data_.size() < message_size + kMessageHeaderSize)
return;
client_->PostMessageFromNativeHost(
incoming_data_.substr(kMessageHeaderSize, message_size));
incoming_data_.erase(0, kMessageHeaderSize + message_size);
}
}
void NativeMessageProcessHost::DoWrite() {
DCHECK(task_runner_->BelongsToCurrentThread());
while (!write_pending_ && !closed_) {
if (!current_write_buffer_.get() ||
!current_write_buffer_->BytesRemaining()) {
if (write_queue_.empty())
return;
scoped_refptr<net::IOBufferWithSize> buffer =
std::move(write_queue_.front());
int buffer_size = buffer->size();
current_write_buffer_ = base::MakeRefCounted<net::DrainableIOBuffer>(
std::move(buffer), buffer_size);
write_queue_.pop();
}
int result = write_stream_->Write(
current_write_buffer_.get(), current_write_buffer_->BytesRemaining(),
base::BindOnce(&NativeMessageProcessHost::OnWritten,
weak_factory_.GetWeakPtr()));
HandleWriteResult(result);
}
}
void NativeMessageProcessHost::HandleWriteResult(int result) {
DCHECK(task_runner_->BelongsToCurrentThread());
if (result <= 0) {
if (result == net::ERR_IO_PENDING) {
write_pending_ = true;
} else {
LOG(ERROR) << "Error when writing to Native Messaging host: " << result;
Close(kHostInputOutputError);
}
return;
}
current_write_buffer_->DidConsume(result);
}
void NativeMessageProcessHost::OnWritten(int result) {
DCHECK(task_runner_->BelongsToCurrentThread());
DCHECK(write_pending_);
write_pending_ = false;
HandleWriteResult(result);
DoWrite();
}
void NativeMessageProcessHost::Close(const std::string& error_message) {
DCHECK(task_runner_->BelongsToCurrentThread());
if (!closed_) {
closed_ = true;
#if BUILDFLAG(IS_POSIX)
read_controller_.reset();
#endif
read_stream_.reset();
write_stream_.reset();
client_->CloseChannel(error_message);
}
}
} // namespace extensions
|