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
|
// 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.
#include "chrome/updater/usage_stats_permissions.h"
#include <algorithm>
#include <cwchar>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "base/logging.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/registry.h"
#include "base/win/windows_types.h"
#include "chrome/updater/app/app_utils.h"
#include "chrome/updater/external_constants.h"
#include "chrome/updater/persisted_data.h"
#include "chrome/updater/updater_scope.h"
#include "chrome/updater/util/win_util.h"
#include "chrome/updater/win/win_constants.h"
namespace updater {
namespace {
std::vector<std::wstring> GetClientStatePathsForScope(UpdaterScope scope) {
std::vector<std::wstring> paths = {CLIENT_STATE_KEY};
if (IsSystemInstall(scope)) {
paths.push_back(CLIENT_STATE_MEDIUM_KEY);
}
return paths;
}
std::vector<std::string> GetInstalledAppIds(
HKEY hive,
const std::vector<std::wstring>& key_paths) {
std::vector<std::string> app_ids;
for (const std::wstring& path : key_paths) {
for (base::win::RegistryKeyIterator it(hive, path.c_str(), KEY_WOW64_32KEY);
it.Valid(); ++it) {
std::string app_id;
if (!base::WideToUTF8(it.Name(), wcslen(it.Name()), &app_id)) {
VLOG(1) << "AppId " << it.Name() << " from registry is not UTF-8";
continue;
}
app_ids.push_back(app_id);
}
}
return app_ids;
}
bool AppAllowsUsageStats(HKEY hive,
const std::vector<std::wstring>& key_paths,
const std::string& app_id) {
return std::ranges::any_of(key_paths, [&](std::wstring key_path) {
DWORD usagestats = 0;
base::win::RegKey key;
return key.Open(hive,
base::StrCat({key_path, base::UTF8ToWide(app_id)}).c_str(),
Wow6432(KEY_READ)) == ERROR_SUCCESS &&
key.ReadValueDW(L"usagestats", &usagestats) == ERROR_SUCCESS &&
usagestats == 1;
});
}
} // namespace
bool AnyAppEnablesUsageStats(HKEY hive,
const std::vector<std::wstring>& key_paths) {
bool allowed = std::ranges::any_of(
GetInstalledAppIds(hive, key_paths), [&](const std::string& app_id) {
if (!IsUpdaterOrCompanionApp(app_id) &&
AppAllowsUsageStats(hive, key_paths, app_id)) {
VLOG(2) << "usage stats enabled by app " << app_id;
return true;
}
return false;
});
if (!allowed) {
VLOG(2) << "no app enables usage stats";
}
return allowed;
}
bool RemoteEventLoggingAllowed(
HKEY hive,
const std::vector<std::wstring>& key_paths,
const std::vector<std::string>& installed_app_ids,
std::optional<EventLoggingPermissionProvider>
event_logging_permission_provider) {
if (!event_logging_permission_provider) {
VLOG(2) << "Event logging disabled by absence of permission provider";
return false;
}
bool manages_additional_apps =
std::ranges::any_of(installed_app_ids, [&](const std::string& app_id) {
return !IsUpdaterOrCompanionApp(app_id) &&
!base::EqualsCaseInsensitiveASCII(
app_id, event_logging_permission_provider->app_id);
});
if (manages_additional_apps) {
VLOG(2) << "Event logging disabled by presence of other apps";
return false;
}
bool allowed = AppAllowsUsageStats(hive, key_paths,
event_logging_permission_provider->app_id);
VLOG_IF(2, !allowed) << "Event logging disabled; app "
<< event_logging_permission_provider->app_id
<< " does not enable usage stats";
return allowed;
}
bool AnyAppEnablesUsageStats(UpdaterScope scope) {
return AnyAppEnablesUsageStats(UpdaterScopeToHKeyRoot(scope),
GetClientStatePathsForScope(scope));
}
bool RemoteEventLoggingAllowed(
UpdaterScope scope,
const std::vector<std::string>& installed_app_ids,
std::optional<EventLoggingPermissionProvider>
event_logging_permission_provider) {
return RemoteEventLoggingAllowed(
UpdaterScopeToHKeyRoot(scope), GetClientStatePathsForScope(scope),
installed_app_ids, std::move(event_logging_permission_provider));
}
} // namespace updater
|