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
|
// Copyright 2021 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/updater/mac/install_from_archive.h"
#import <Cocoa/Cocoa.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <algorithm>
#include <map>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/numerics/checked_math.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/process/process.h"
#include "base/strings/strcat.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/time/time.h"
#include "base/version.h"
#include "chrome/updater/branded_constants.h"
#include "chrome/updater/constants.h"
#include "chrome/updater/updater_branding.h"
#include "chrome/updater/updater_scope.h"
#include "chrome/updater/util/mac_util.h"
#include "chrome/updater/util/util.h"
namespace updater {
namespace {
bool RunHDIUtil(const std::vector<std::string>& args,
std::string* command_output) {
base::FilePath hdiutil_path("/usr/bin/hdiutil");
if (!base::PathExists(hdiutil_path)) {
VLOG(1) << "hdiutil path (" << hdiutil_path << ") does not exist.";
return false;
}
base::CommandLine command(hdiutil_path);
for (const auto& arg : args) {
command.AppendArg(arg);
}
std::string output;
bool result = base::GetAppOutput(command, &output);
if (!result) {
VLOG(1) << "hdiutil failed.";
}
if (command_output) {
*command_output = output;
}
return result;
}
bool MountDMG(const base::FilePath& dmg_path, std::string* mount_point) {
if (!base::PathExists(dmg_path)) {
VLOG(1) << "The DMG file path (" << dmg_path << ") does not exist.";
return false;
}
std::string command_output;
std::vector<std::string> args{"attach", dmg_path.value(), "-plist",
"-nobrowse", "-readonly"};
if (!RunHDIUtil(args, &command_output)) {
VLOG(1) << "Mounting DMG (" << dmg_path
<< ") failed. Output: " << command_output;
return false;
}
@autoreleasepool {
NSDictionary* plist = nil;
@try {
plist = [base::SysUTF8ToNSString(command_output) propertyList];
} @catch (NSException*) {
// `[NSString propertyList]` throws an NSParseErrorException if bad data.
VLOG(1) << "Unable to parse command output: [" << command_output << "]";
return false;
}
// Look for the mountpoint.
NSArray* system_entities = [plist objectForKey:@"system-entities"];
NSString* dmg_mount_point = nil;
for (NSDictionary* entry in system_entities) {
NSString* entry_mount_point = entry[@"mount-point"];
if ([entry_mount_point length]) {
dmg_mount_point = [entry_mount_point stringByStandardizingPath];
break;
}
}
if (mount_point) {
*mount_point = base::SysNSStringToUTF8(dmg_mount_point);
}
}
return true;
}
bool UnmountDMG(const base::FilePath& mounted_dmg_path) {
if (!base::PathExists(mounted_dmg_path)) {
VLOG(1) << "The mounted DMG path (" << mounted_dmg_path
<< ") does not exist.";
return false;
}
std::vector<std::string> args{"detach", mounted_dmg_path.value(), "-force"};
if (!RunHDIUtil(args, nullptr)) {
VLOG(1) << "Unmounting DMG (" << mounted_dmg_path << ") failed.";
return false;
}
return true;
}
bool IsInstallScriptExecutable(const base::FilePath& script_path) {
int permissions = 0;
if (!base::GetPosixFilePermissions(script_path, &permissions)) {
return false;
}
static constexpr int kExecutableMask = base::FILE_PERMISSION_EXECUTE_BY_USER;
return (permissions & kExecutableMask) == kExecutableMask;
}
int RunExecutable(const base::FilePath& existence_checker_path,
const std::string& ap,
const std::string& arguments,
std::optional<base::FilePath> installer_data_file,
UpdaterScope scope,
const base::Version& pv,
bool usage_stats_enabled,
base::TimeDelta timeout,
const base::FilePath& unpacked_path) {
if (!base::PathExists(unpacked_path)) {
VLOG(1) << "File path (" << unpacked_path << ") does not exist.";
return static_cast<int>(InstallErrors::kMountedDmgPathDoesNotExist);
}
int run_executables = 0;
for (const char* executable : {
".preinstall",
".keystone_preinstall",
".install",
".keystone_install",
".postinstall",
".keystone_postinstall",
}) {
base::FilePath executable_file_path = unpacked_path.Append(executable);
if (!base::PathExists(executable_file_path)) {
continue;
}
if (!IsInstallScriptExecutable(executable_file_path)) {
VLOG(1) << "Executable file path (" << executable_file_path
<< ") is not executable";
return static_cast<int>(InstallErrors::kExecutablePathNotExecutable);
}
base::CommandLine command(executable_file_path);
command.AppendArgPath(unpacked_path);
command.AppendArgPath(existence_checker_path);
command.AppendArg(pv.GetString());
// Provide a small PATH to provide a predictable execution environment,
// including ksadmin on the PATH. If updating this logic, please keep the
// install script test in sync with the behavior here.
// LINT.IfChange(InstallerEnvPath)
std::string env_path = "/bin:/usr/bin";
std::optional<base::FilePath> ksadmin_path =
GetKSAdminPath(GetUpdaterScope());
if (ksadmin_path) {
env_path = base::StrCat({env_path, ":", ksadmin_path->DirName().value()});
}
// LINT.ThenChange(/chrome/installer/mac/keystone_install_test.sh:InstallerEnvPath)
base::ScopedFD read_fd, write_fd;
{
int pipefds[2] = {};
if (pipe(pipefds) != 0) {
VPLOG(1) << "pipe";
return static_cast<int>(InstallErrors::kExecutablePipeFailed);
}
read_fd.reset(pipefds[0]);
write_fd.reset(pipefds[1]);
}
base::LaunchOptions options;
options.fds_to_remap.emplace_back(write_fd.get(), STDOUT_FILENO);
options.fds_to_remap.emplace_back(write_fd.get(), STDERR_FILENO);
options.current_directory = unpacked_path;
options.clear_environment = true;
options.environment = {
{"KS_TICKET_AP", ap},
{"KS_TICKET_SERVER_URL", UPDATE_CHECK_URL},
{"KS_TICKET_XC_PATH", existence_checker_path.value()},
{"PATH", env_path},
{"PREVIOUS_VERSION", pv.GetString()},
{"SERVER_ARGS", arguments},
{"UPDATE_IS_MACHINE", IsSystemInstall(scope) ? "1" : "0"},
{"UNPACK_DIR", unpacked_path.value()},
{kUsageStatsEnabled,
usage_stats_enabled ? kUsageStatsEnabledValueEnabled : "0"},
};
if (installer_data_file) {
options.environment.emplace(base::ToUpperASCII(kInstallerDataSwitch),
installer_data_file->value());
}
int exit_code = 0;
VLOG(1) << "Running " << command.GetCommandLineString();
const base::Process proc = base::LaunchProcess(command, options);
if (!proc.IsValid()) {
return static_cast<int>(InstallErrors::kExecutableWaitForExitFailed);
}
// Close write_fd to generate EOF in the read loop below.
write_fd.reset();
std::string output;
base::Time deadline = base::Time::Now() + timeout;
static constexpr size_t kBufferSize = 1024;
base::CheckedNumeric<size_t> total_bytes_read = 0;
ssize_t read_this_pass = 0;
do {
struct pollfd fds[1] = {{.fd = read_fd.get(), .events = POLLIN}};
int timeout_remaining_ms =
static_cast<int>((deadline - base::Time::Now()).InMilliseconds());
if (timeout_remaining_ms < 0 || poll(fds, 1, timeout_remaining_ms) != 1) {
break;
}
base::CheckedNumeric<size_t> new_size =
base::CheckedNumeric<size_t>(output.size()) +
base::CheckedNumeric<size_t>(kBufferSize);
if (!new_size.IsValid() || !total_bytes_read.IsValid()) {
// Ignore the rest of the output.
break;
}
output.resize(new_size.ValueOrDie());
read_this_pass = HANDLE_EINTR(read(
read_fd.get(), &output[total_bytes_read.ValueOrDie()], kBufferSize));
if (read_this_pass >= 0) {
total_bytes_read += base::CheckedNumeric<size_t>(read_this_pass);
if (!total_bytes_read.IsValid()) {
// Ignore the rest of the output.
break;
}
output.resize(total_bytes_read.ValueOrDie());
}
} while (read_this_pass > 0);
VLOG(1) << "Output from " << executable << ": " << output;
if (!proc.WaitForExitWithTimeout(
std::max(deadline - base::Time::Now(), base::TimeDelta()),
&exit_code)) {
return static_cast<int>(InstallErrors::kExecutableWaitForExitFailed);
}
if (exit_code != 0) {
return exit_code;
}
++run_executables;
}
return run_executables > 0
? 0
: static_cast<int>(InstallErrors::kExecutableFilePathDoesNotExist);
}
void CopyDMGContents(const base::FilePath& dmg_path,
const base::FilePath& destination_path) {
base::FileEnumerator(
dmg_path, false,
base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES)
.ForEach([&destination_path](const base::FilePath& path) {
base::File::Info file_info;
if (!base::GetFileInfo(path, &file_info)) {
VLOG(0) << "Couldn't get file info for: " << path.value();
return;
}
if (base::IsLink(path)) {
VLOG(0) << "File is symbolic link: " << path.value();
return;
}
if (file_info.is_directory) {
if (!base::CopyDirectory(path, destination_path, true)) {
VLOG(0) << "Couldn't copy directory for: " << path.value() << " to "
<< destination_path.value();
return;
}
} else {
if (!base::CopyFile(path, destination_path.Append(path.BaseName()))) {
VLOG(0) << "Couldn't copy file for: " << path.value() << " to "
<< destination_path.value();
return;
}
}
});
}
// Mounts the DMG specified by `dmg_file_path`. The install executable located
// at "/.install" in the mounted volume is executed, and then the DMG is
// un-mounted. Returns an error code if mounting the DMG or executing the
// executable failed.
int InstallFromDMG(const base::FilePath& dmg_file_path,
base::OnceCallback<int(const base::FilePath&)> install) {
std::string mount_point;
if (!MountDMG(dmg_file_path, &mount_point)) {
return static_cast<int>(InstallErrors::kFailMountDmg);
}
if (mount_point.empty()) {
VLOG(1) << "No mount point.";
return static_cast<int>(InstallErrors::kNoMountPoint);
}
const base::FilePath mounted_dmg_path = base::FilePath(mount_point);
const int result = std::move(install).Run(mounted_dmg_path);
// After running the executable, before unmount, copy the contents of the DMG
// into the cache folder. This will allow for differentials.
CopyDMGContents(mounted_dmg_path, dmg_file_path.DirName());
if (!UnmountDMG(mounted_dmg_path)) {
VLOG(1) << "Could not unmount the DMG: " << mounted_dmg_path;
}
// Delete the DMG from the cached folder after we are done.
if (!base::DeleteFile(dmg_file_path)) {
VPLOG(1) << "Couldn't remove the DMG.";
}
return result;
}
// Installs by running the install scripts in the specified directory.
int InstallFromDir(const base::FilePath& dir,
base::OnceCallback<int(const base::FilePath&)> install) {
// Update permissions on files in the directory.
if (!SetFilePermissionsRecursive(dir)) {
return static_cast<int>(InstallErrors::kCouldNotConfirmAppPermissions);
}
return std::move(install).Run(dir);
}
// Installs with a path to the app specified by the `app_file_path`. The install
// executable located at "/.install" next to the .app is executed. This function
// is important for the differential installs, as applying the differential
// creates a .app file within the caching folder.
int InstallFromApp(const base::FilePath& app_file_path,
base::OnceCallback<int(const base::FilePath&)> install) {
if (!base::PathExists(app_file_path) ||
app_file_path.FinalExtension() != ".app") {
VLOG(1) << "Path to the app does not exist!";
return static_cast<int>(InstallErrors::kNotSupportedInstallerType);
}
// Need to make sure that the app at the path being installed has the correect
// permissions.
if (!SetFilePermissionsRecursive(app_file_path)) {
return static_cast<int>(InstallErrors::kCouldNotConfirmAppPermissions);
}
return std::move(install).Run(app_file_path.DirName());
}
} // namespace
int InstallFromArchive(const base::FilePath& file_path,
const base::FilePath& existence_checker_path,
const std::string& ap,
UpdaterScope scope,
const base::Version& pv,
const std::string& arguments,
std::optional<base::FilePath> installer_data_file,
const bool usage_stats_enabled,
base::TimeDelta timeout) {
const std::map<std::string,
int (*)(const base::FilePath&,
base::OnceCallback<int(const base::FilePath&)>)>
handlers = {
{".dmg", &InstallFromDMG},
{".app", &InstallFromApp},
{"", &InstallFromDir},
};
auto handler = handlers.find(file_path.Extension());
if (handler == handlers.end()) {
VLOG(0) << "Install failed: no handler for " << file_path.Extension();
return static_cast<int>(InstallErrors::kNotSupportedInstallerType);
}
return handler->second(
file_path, base::BindOnce(&RunExecutable, existence_checker_path, ap,
arguments, installer_data_file, scope, pv,
usage_stats_enabled, timeout));
}
} // namespace updater
|