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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include <utility>
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "base/win/access_control_list.h"
#include "base/win/security_descriptor.h"
#include "base/win/sid.h"
#include "remoting/base/crash/breakpad_utils.h"
#include "remoting/base/crash/crash_reporting_breakpad.h"
#include "remoting/base/logging.h"
#include "remoting/base/version.h"
#include "third_party/breakpad/breakpad/src/client/windows/crash_generation/crash_generation_server.h"
namespace remoting {
namespace {
using base::win::SecurityDescriptor;
using base::win::Sid;
using base::win::SecurityAccessMode::kGrant;
using base::win::WellKnownSid::kLocalSystem;
// Passed as a flag in the named pipe DACL entry to indicate no inheritance.
constexpr bool kNoInheritance = false;
class BreakpadServer {
public:
BreakpadServer();
BreakpadServer(const BreakpadServer&) = delete;
BreakpadServer& operator=(const BreakpadServer&) = delete;
~BreakpadServer();
static BreakpadServer& GetInstance();
void set_client_connected_time(base::Time client_connected_time) {
client_connected_time_ = client_connected_time;
}
base::Time get_client_connected_time() { return client_connected_time_; }
private:
base::Time client_connected_time_;
std::unique_ptr<google_breakpad::CrashGenerationServer> crash_server_;
};
void OnClientConnectedCallback(void* context,
const google_breakpad::ClientInfo* client_info) {
HOST_LOG << "OOP Crash client connected";
BreakpadServer::GetInstance().set_client_connected_time(
base::Time::NowFromSystemTime());
}
void OnClientDumpRequestCallback(void* context,
const google_breakpad::ClientInfo* client_info,
const std::wstring* file_path) {
if (!file_path) {
LOG(ERROR) << "OnClientDumpRequestCallback called with invalid file_path";
return;
}
base::FilePath dump_file(*file_path);
if (!GetMinidumpDirectoryPath().IsParent(dump_file)) {
LOG(ERROR) << "Minidump written to an unexpected location: " << dump_file;
return;
}
base::Value::Dict metadata;
// Use the crash server version since we update all host components in the
// same package and we've seen problems trying to use the metadata provided
// by the client process, see crbug.com/350725178.
metadata.Set(kBreakpadProductVersionKey, REMOTING_VERSION_STRING);
// We only have one OOP client so we can use the value of the 'last connected'
// client to determine an approximate uptime for that process.
auto process_start_time =
BreakpadServer::GetInstance().get_client_connected_time();
auto process_uptime = base::Time::NowFromSystemTime() - process_start_time;
metadata.Set(kBreakpadProcessUptimeKey,
base::NumberToString(process_uptime.InMilliseconds()));
WriteMetadataForMinidump(dump_file, std::move(metadata));
}
BreakpadServer::BreakpadServer() {
base::win::SecurityDescriptor sd;
sd.set_owner(Sid(kLocalSystem));
sd.set_group(Sid(kLocalSystem));
// Configure the named pipe to prevent non-SYSTEM access unless a handle is
// created by the server and provided over IPC or STDIO.
if (!sd.SetDaclEntry(kLocalSystem, kGrant, FILE_ALL_ACCESS, kNoInheritance)) {
LOG(ERROR) << "Failed to set named pipe security attributes, skipping "
<< "out-of-process crash handler initialization.";
return;
}
SECURITY_DESCRIPTOR security_descriptor;
sd.ToAbsolute(security_descriptor);
SECURITY_ATTRIBUTES security_attributes = {0};
security_attributes.nLength = sizeof(security_attributes);
security_attributes.lpSecurityDescriptor = &security_descriptor;
security_attributes.bInheritHandle = false;
crash_server_ = std::make_unique<google_breakpad::CrashGenerationServer>(
kCrashServerPipeName, &security_attributes,
/*connect_callback=*/OnClientConnectedCallback,
/*connect_context=*/nullptr,
/*dump_callback=*/OnClientDumpRequestCallback,
/*dump_context=*/nullptr,
/*exit_callback=*/nullptr,
/*exit_context=*/nullptr,
/*upload_request_callback=*/nullptr,
/*upload_context=*/nullptr,
/*generate_dumps=*/true, &GetMinidumpDirectoryPath().value());
crash_server_->Start();
}
BreakpadServer::~BreakpadServer() = default;
// static
BreakpadServer& BreakpadServer::GetInstance() {
static base::NoDestructor<BreakpadServer> instance;
return *instance;
}
} // namespace
void InitializeOopCrashServer() {
// Touch the object to make sure it is initialized.
BreakpadServer::GetInstance();
}
} // namespace remoting
|