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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
|
// 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.
#include "chrome/browser/component_updater/recovery_component_installer.h"
#include <stdint.h>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/json/json_file_value_serializer.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/process/process.h"
#include "base/strings/string_util.h"
#include "base/task/thread_pool.h"
#include "base/values.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/component_updater/component_updater_paths.h"
#include "components/component_updater/component_updater_service.h"
#include "components/component_updater/pref_names.h"
#include "components/crx_file/crx_verifier.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/update_client/update_client.h"
#include "components/update_client/utils.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "crypto/sha2.h"
#if BUILDFLAG(IS_MAC)
#include "base/mac/authorization_util.h"
#include "base/mac/scoped_authorizationref.h"
#endif
namespace component_updater {
using ::content::BrowserThread;
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
namespace {
// CRX hash. The extension id is: npdjjkjlcidkjlamlmmdelcjbcpdjocm.
const uint8_t kRecoverySha2Hash[] = {
0xdf, 0x39, 0x9a, 0x9b, 0x28, 0x3a, 0x9b, 0x0c, 0xbc, 0xc3, 0x4b,
0x29, 0x12, 0xf3, 0x9e, 0x2c, 0x19, 0x7a, 0x71, 0x4b, 0x0a, 0x7c,
0x80, 0x1c, 0xf6, 0x29, 0x7c, 0x0a, 0x5f, 0xea, 0x67, 0xb7};
static_assert(std::size(kRecoverySha2Hash) == crypto::kSHA256Length,
"Wrong hash length");
// File name of the recovery binary on different platforms.
const base::FilePath::CharType kRecoveryFileName[] =
#if BUILDFLAG(IS_WIN)
FILE_PATH_LITERAL("ChromeRecovery.exe");
#else // BUILDFLAG(IS_LINUX), BUILDFLAG(IS_MAC), etc.
FILE_PATH_LITERAL("ChromeRecovery");
#endif
const char kRecoveryManifestName[] = "ChromeRecovery";
// ChromeRecovery process exit codes.
enum ChromeRecoveryExitCode {
EXIT_CODE_RECOVERY_SUCCEEDED = 0,
EXIT_CODE_RECOVERY_SKIPPED = 1,
EXIT_CODE_ELEVATION_NEEDED = 2,
};
// Checks if elevated recovery simulation switch was present on the command
// line. This is for testing purpose.
bool SimulatingElevatedRecovery() {
return base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kSimulateElevatedRecovery);
}
std::vector<std::string> GetRecoveryInstallArguments(
const base::Value::Dict& manifest,
bool is_deferred_run,
const base::Version& version) {
std::vector<std::string> arguments;
// Add a flag for re-attempted install with elevated privilege so that the
// recovery executable can report back accordingly.
if (is_deferred_run) {
arguments.push_back("/deferredrun");
}
if (const std::string* recovery_args =
manifest.FindString("x-recovery-args")) {
if (base::IsStringASCII(*recovery_args)) {
arguments.push_back(*recovery_args);
}
}
if (const std::string* recovery_add_version =
manifest.FindString("x-recovery-add-version")) {
if (*recovery_add_version == "yes") {
arguments.push_back("/version");
arguments.push_back(version.GetString());
}
}
return arguments;
}
base::CommandLine BuildRecoveryInstallCommandLine(
const base::FilePath& command,
const base::Value::Dict& manifest,
bool is_deferred_run,
const base::Version& version) {
base::CommandLine command_line(command);
const auto arguments =
GetRecoveryInstallArguments(manifest, is_deferred_run, version);
for (const auto& arg : arguments) {
command_line.AppendArg(arg);
}
return command_line;
}
base::Value::Dict ReadManifest(const base::FilePath& manifest) {
JSONFileValueDeserializer deserializer(manifest);
std::string error;
return std::move(*deserializer.Deserialize(nullptr, &error)).TakeDict();
}
void WaitForElevatedInstallToComplete(base::Process process) {
const base::TimeDelta kMaxWaitTime = base::Seconds(600);
process.WaitForExitWithTimeout(kMaxWaitTime, nullptr);
}
void DoElevatedInstallRecoveryComponent(const base::FilePath& path) {
const base::FilePath main_file = path.Append(kRecoveryFileName);
const base::FilePath manifest_file =
path.Append(FILE_PATH_LITERAL("manifest.json"));
if (!base::PathExists(main_file) || !base::PathExists(manifest_file)) {
return;
}
base::Value::Dict manifest(ReadManifest(manifest_file));
const std::string* name = manifest.FindString("name");
if (!name || *name != kRecoveryManifestName) {
return;
}
std::string proposed_version;
if (const std::string* ptr = manifest.FindString("version")) {
if (base::IsStringASCII(*ptr)) {
proposed_version = *ptr;
}
}
const base::Version version(proposed_version);
if (!version.IsValid()) {
return;
}
const bool is_deferred_run = true;
#if BUILDFLAG(IS_WIN)
const auto cmdline = BuildRecoveryInstallCommandLine(
main_file, manifest, is_deferred_run, version);
base::LaunchOptions options;
options.start_hidden = true;
options.elevated = true;
base::Process process = base::LaunchProcess(cmdline, options);
#elif BUILDFLAG(IS_MAC)
base::mac::ScopedAuthorizationRef authRef =
base::mac::AuthorizationCreateToRunAsRoot(nullptr);
if (!authRef.get()) {
return;
}
const auto arguments =
GetRecoveryInstallArguments(manifest, is_deferred_run, version);
// Convert the arguments memory layout to the format required by
// ExecuteWithPrivilegesAndGetPID(): an array of string pointers
// that ends with a null pointer.
std::vector<const char*> raw_string_args;
for (const auto& arg : arguments) {
raw_string_args.push_back(arg.c_str());
}
raw_string_args.push_back(nullptr);
pid_t pid = -1;
const OSStatus status = base::mac::ExecuteWithPrivilegesAndGetPID(
authRef.get(), main_file.value().c_str(), kAuthorizationFlagDefaults,
raw_string_args.data(), nullptr, &pid);
if (status != errAuthorizationSuccess) {
return;
}
// The child process must print its PID in the first line of its STDOUT. See
// https://cs.chromium.org/chromium/src/base/mac/authorization_util.h?l=8
// for more details. When |pid| cannot be determined, we are not able to
// get process exit code, thus bail out early.
if (pid < 0) {
return;
}
base::Process process = base::Process::Open(pid);
#endif
// This task joins a process, hence .WithBaseSyncPrimitives().
base::ThreadPool::PostTask(
FROM_HERE,
{base::WithBaseSyncPrimitives(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&WaitForElevatedInstallToComplete, std::move(process)));
}
void ElevatedInstallRecoveryComponent(const base::FilePath& installer_path) {
base::ThreadPool::PostTask(
FROM_HERE,
{base::MayBlock(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&DoElevatedInstallRecoveryComponent, installer_path));
}
} // namespace
// Component installer that is responsible to repair the chrome installation
// or repair the Google update installation. This is a last resort safety
// mechanism.
// For user Chrome, recovery component just installs silently. For machine
// Chrome, elevation may be needed. If that happens, the installer will set
// preference flag prefs::kRecoveryComponentNeedsElevation to request that.
// There is a global error service monitors this flag and will pop up
// bubble if the flag is set to true.
// See chrome/browser/recovery/recovery_install_global_error.cc for details.
class RecoveryComponentInstaller : public update_client::CrxInstaller {
public:
RecoveryComponentInstaller(const base::Version& version, PrefService* prefs);
// ComponentInstaller implementation:
void OnUpdateError(int error) override;
void Install(const base::FilePath& unpack_path,
const std::string& public_key,
std::unique_ptr<InstallParams> install_params,
ProgressCallback progress_callback,
Callback callback) override;
std::optional<base::FilePath> GetInstalledFile(
const std::string& file) override;
bool Uninstall() override;
private:
~RecoveryComponentInstaller() override = default;
bool DoInstall(const base::FilePath& unpack_path);
bool RunInstallCommand(const base::CommandLine& cmdline,
const base::FilePath& installer_folder) const;
base::Version current_version_;
raw_ptr<PrefService> prefs_;
};
void SimulateElevatedRecoveryHelper(PrefService* prefs) {
prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, true);
}
void RecoveryRegisterHelper(ComponentUpdateService* cus, PrefService* prefs) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
base::Version version(prefs->GetString(prefs::kRecoveryComponentVersion));
if (!version.IsValid()) {
VLOG(2) << "Recovery component version is not valid.";
}
std::vector<uint8_t> public_key_hash;
public_key_hash.assign(std::begin(kRecoverySha2Hash),
std::end(kRecoverySha2Hash));
if (!cus->RegisterComponent(ComponentRegistration(
update_client::GetCrxIdFromPublicKeyHash(public_key_hash), "recovery",
public_key_hash, version, /*fingerprint=*/{},
/*installer_attributes=*/{}, /*action_handler=*/nullptr,
new RecoveryComponentInstaller(version, prefs),
/*requires_network_encryption=*/false,
/*supports_group_policy_enable_component_updates=*/true,
/*allow_cached_copies=*/true,
/*allow_updates_on_metered_connection=*/true,
/*allow_updates=*/true))) {
VLOG(2) << "Recovery component registration failed.";
}
}
void RecoveryUpdateVersionHelper(const base::Version& version,
PrefService* prefs) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
prefs->SetString(prefs::kRecoveryComponentVersion, version.GetString());
}
void SetPrefsForElevatedRecoveryInstall(const base::FilePath& unpack_path,
PrefService* prefs) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
prefs->SetFilePath(prefs::kRecoveryComponentUnpackPath, unpack_path);
prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, true);
}
RecoveryComponentInstaller::RecoveryComponentInstaller(
const base::Version& version,
PrefService* prefs)
: current_version_(version), prefs_(prefs) {}
void RecoveryComponentInstaller::OnUpdateError(int error) {
VLOG(2) << "Recovery component update error: " << error;
}
void WaitForInstallToComplete(base::Process process,
const base::FilePath& installer_folder,
PrefService* prefs) {
int installer_exit_code = 0;
const base::TimeDelta kMaxWaitTime = base::Seconds(600);
if (process.WaitForExitWithTimeout(kMaxWaitTime, &installer_exit_code)) {
if (installer_exit_code == EXIT_CODE_ELEVATION_NEEDED) {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&SetPrefsForElevatedRecoveryInstall,
installer_folder, prefs));
}
}
}
bool RecoveryComponentInstaller::RunInstallCommand(
const base::CommandLine& cmdline,
const base::FilePath& installer_folder) const {
base::LaunchOptions options;
#if BUILDFLAG(IS_WIN)
options.start_hidden = true;
#endif
base::Process process = base::LaunchProcess(cmdline, options);
if (!process.IsValid()) {
return false;
}
// Let worker pool thread wait for us so we don't block Chrome shutdown.
// This task joins a process, hence .WithBaseSyncPrimitives().
base::ThreadPool::PostTask(
FROM_HERE,
{base::WithBaseSyncPrimitives(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
base::BindOnce(&WaitForInstallToComplete, std::move(process),
installer_folder, prefs_));
// Returns true regardless of install result since from updater service
// perspective the install is done, even we may need to do elevated
// install later.
return true;
}
#if BUILDFLAG(IS_POSIX)
// Sets the POSIX executable permissions on a file
bool SetPosixExecutablePermission(const base::FilePath& path) {
int permissions = 0;
if (!base::GetPosixFilePermissions(path, &permissions)) {
return false;
}
const int kExecutableMask = base::FILE_PERMISSION_EXECUTE_BY_USER |
base::FILE_PERMISSION_EXECUTE_BY_GROUP |
base::FILE_PERMISSION_EXECUTE_BY_OTHERS;
if ((permissions & kExecutableMask) == kExecutableMask) {
return true; // No need to update
}
return base::SetPosixFilePermissions(path, permissions | kExecutableMask);
}
#endif // BUILDFLAG(IS_POSIX)
void RecoveryComponentInstaller::Install(
const base::FilePath& unpack_path,
const std::string& /*public_key*/,
std::unique_ptr<InstallParams> /*install_params*/,
ProgressCallback /*progress_callback*/,
update_client::CrxInstaller::Callback callback) {
auto result = update_client::InstallFunctionWrapper(
base::BindOnce(&RecoveryComponentInstaller::DoInstall,
base::Unretained(this), std::cref(unpack_path)));
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), result));
}
bool RecoveryComponentInstaller::DoInstall(const base::FilePath& unpack_path) {
std::optional<base::Value::Dict> manifest =
update_client::ReadManifest(unpack_path);
if (!manifest.has_value()) {
return false;
}
const std::string* name = manifest->FindString("name");
if (!name || *name != kRecoveryManifestName) {
return false;
}
const std::string* proposed_version = manifest->FindString("version");
if (!proposed_version || !base::IsStringASCII(*proposed_version)) {
return false;
}
base::Version version(*proposed_version);
if (!version.IsValid()) {
return false;
}
if (current_version_.CompareTo(version) >= 0) {
return false;
}
// Passed the basic tests. Copy the installation to a permanent directory.
base::FilePath path;
if (!base::PathService::Get(DIR_RECOVERY_BASE, &path)) {
return false;
}
if (!base::PathExists(path) && !base::CreateDirectory(path)) {
return false;
}
path = path.AppendASCII(version.GetString());
if (base::PathExists(path) && !base::DeletePathRecursively(path)) {
return false;
}
if (!base::Move(unpack_path, path)) {
DVLOG(1) << "Recovery component move failed.";
return false;
}
base::FilePath main_file = path.Append(kRecoveryFileName);
if (!base::PathExists(main_file)) {
return false;
}
#if BUILDFLAG(IS_POSIX)
// The current version of the CRX unzipping does not restore
// correctly the executable flags/permissions. See https://crbug.com/555011
if (!SetPosixExecutablePermission(main_file)) {
DVLOG(1) << "Recovery component failed to set the executable "
"permission on the file: "
<< main_file.value();
return false;
}
#endif
// Run the recovery component.
const bool is_deferred_run = false;
const auto cmdline = BuildRecoveryInstallCommandLine(
main_file, manifest.value(), is_deferred_run, current_version_);
if (!RunInstallCommand(cmdline, path)) {
return false;
}
current_version_ = version;
if (prefs_) {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&RecoveryUpdateVersionHelper, version, prefs_));
}
return true;
}
std::optional<base::FilePath> RecoveryComponentInstaller::GetInstalledFile(
const std::string& file) {
return std::nullopt;
}
bool RecoveryComponentInstaller::Uninstall() {
return false;
}
#endif // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
void RegisterRecoveryComponent(ComponentUpdateService* cus,
PrefService* prefs) {
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
if (SimulatingElevatedRecovery()) {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(&SimulateElevatedRecoveryHelper, prefs));
}
// We delay execute the registration because we are not required in
// the critical path during browser startup.
content::GetUIThreadTaskRunner({})->PostDelayedTask(
FROM_HERE, base::BindOnce(&RecoveryRegisterHelper, cus, prefs),
base::Seconds(6));
#endif
#endif
}
void RegisterPrefsForRecoveryComponent(PrefRegistrySimple* registry) {
registry->RegisterStringPref(prefs::kRecoveryComponentVersion, "0.0.0.0");
registry->RegisterFilePathPref(prefs::kRecoveryComponentUnpackPath,
base::FilePath());
registry->RegisterBooleanPref(prefs::kRecoveryComponentNeedsElevation, false);
}
void AcceptedElevatedRecoveryInstall(PrefService* prefs) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
ElevatedInstallRecoveryComponent(
prefs->GetFilePath(prefs::kRecoveryComponentUnpackPath));
#endif
#endif
prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, false);
}
void DeclinedElevatedRecoveryInstall(PrefService* prefs) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
prefs->SetBoolean(prefs::kRecoveryComponentNeedsElevation, false);
}
} // namespace component_updater
|