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
|
// Copyright 2019 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/ash/policy/handlers/tpm_auto_update_mode_policy_handler.h"
#include <utility>
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "base/values.h"
#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
#include "chrome/browser/ash/tpm/tpm_firmware_update.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/common/pref_names.h"
#include "chromeos/ash/components/dbus/session_manager/session_manager_client.h"
#include "chromeos/ash/components/settings/cros_settings_names.h"
#include "chromeos/ash/components/settings/cros_settings_provider.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/user_manager/user_manager.h"
namespace policy {
namespace {
// Timeout for the TPM firmware update availability check.
const base::TimeDelta kFirmwareAvailabilityCheckerTimeout = base::Seconds(20);
const base::TimeDelta kTPMUpdatePlannedNotificationWaitTime = base::Days(1);
// Reads the value of the the device setting key
// TPMFirmwareUpdateSettings.AutoUpdateMode from a trusted store. If the value
// is temporarily untrusted |callback| will be invoked later when trusted values
// are available and AutoUpdateMode::kNever will be returned. This value is set
// via the device policy TPMFirmwareUpdateSettings.
AutoUpdateMode GetTPMAutoUpdateModeSetting(
const ash::CrosSettings* cros_settings,
const base::RepeatingClosure callback) {
if (!g_browser_process->platform_part()
->browser_policy_connector_ash()
->IsDeviceEnterpriseManaged()) {
return AutoUpdateMode::kNever;
}
ash::CrosSettingsProvider::TrustedStatus status =
cros_settings->PrepareTrustedValues(callback);
if (status != ash::CrosSettingsProvider::TRUSTED)
return AutoUpdateMode::kNever;
const base::Value* tpm_settings =
cros_settings->GetPref(ash::kTPMFirmwareUpdateSettings);
if (!tpm_settings)
return AutoUpdateMode::kNever;
std::optional<int> auto_update_mode = tpm_settings->GetDict().FindInt(
ash::tpm_firmware_update::kSettingsKeyAutoUpdateMode);
// Policy not set.
if (!auto_update_mode || *auto_update_mode == 0) {
return AutoUpdateMode::kNever;
}
// Verify that the value is within range.
if (*auto_update_mode < static_cast<int>(AutoUpdateMode::kNever) ||
*auto_update_mode > static_cast<int>(AutoUpdateMode::kEnrollment)) {
NOTREACHED() << "Invalid value for device policy key "
"TPMFirmwareUpdateSettings.AutoUpdateMode";
}
return static_cast<AutoUpdateMode>(*auto_update_mode);
}
} // namespace
TPMAutoUpdateModePolicyHandler::TPMAutoUpdateModePolicyHandler(
ash::CrosSettings* cros_settings,
PrefService* local_state)
: cros_settings_(cros_settings), local_state_(local_state) {
DCHECK(local_state_);
policy_subscription_ = cros_settings_->AddSettingsObserver(
ash::kTPMFirmwareUpdateSettings,
base::BindRepeating(&TPMAutoUpdateModePolicyHandler::OnPolicyChanged,
weak_factory_.GetWeakPtr()));
update_checker_callback_ =
base::BindRepeating(&TPMAutoUpdateModePolicyHandler::CheckForUpdate,
weak_factory_.GetWeakPtr());
show_notification_callback_ =
base::BindRepeating(&ash::ShowAutoUpdateNotification);
notification_timer_ = std::make_unique<base::OneShotTimer>();
// Fire it once so we're sure we get an invocation on startup.
OnPolicyChanged();
}
TPMAutoUpdateModePolicyHandler::~TPMAutoUpdateModePolicyHandler() = default;
void TPMAutoUpdateModePolicyHandler::OnPolicyChanged() {
AutoUpdateMode auto_update_mode = GetTPMAutoUpdateModeSetting(
cros_settings_,
base::BindRepeating(&TPMAutoUpdateModePolicyHandler::OnPolicyChanged,
weak_factory_.GetWeakPtr()));
if (auto_update_mode == AutoUpdateMode::kNever ||
auto_update_mode == AutoUpdateMode::kEnrollment) {
if (notification_timer_->IsRunning())
notification_timer_->Stop();
return;
}
if (auto_update_mode == AutoUpdateMode::kUserAcknowledgment) {
if (!WasTPMUpdateOnNextRebootNotificationShown()) {
update_checker_callback_.Run(base::BindOnce(
&TPMAutoUpdateModePolicyHandler::ShowTPMAutoUpdateNotification,
weak_factory_.GetWeakPtr()));
return;
}
}
update_checker_callback_.Run(base::BindOnce(&OnUpdateAvailableCheckResult));
}
void TPMAutoUpdateModePolicyHandler::CheckForUpdate(
base::OnceCallback<void(bool)> callback) {
ash::tpm_firmware_update::UpdateAvailable(
std::move(callback), kFirmwareAvailabilityCheckerTimeout);
}
// static
void TPMAutoUpdateModePolicyHandler::OnUpdateAvailableCheckResult(
bool update_available) {
if (!update_available)
return;
ash::SessionManagerClient::Get()->StartTPMFirmwareUpdate("preserve_stateful");
}
void TPMAutoUpdateModePolicyHandler::SetUpdateCheckerCallbackForTesting(
const UpdateCheckerCallback& callback) {
update_checker_callback_ = callback;
}
void TPMAutoUpdateModePolicyHandler::SetShowNotificationCallbackForTesting(
const ShowNotificationCallback& callback) {
show_notification_callback_ = callback;
}
void TPMAutoUpdateModePolicyHandler::UpdateOnEnrollmentIfNeeded() {
AutoUpdateMode auto_update_mode = GetTPMAutoUpdateModeSetting(
cros_settings_,
base::BindRepeating(
&TPMAutoUpdateModePolicyHandler::UpdateOnEnrollmentIfNeeded,
weak_factory_.GetWeakPtr()));
// If the TPM is set to update with user acknowlegment, always update on
// enrollment. The AutoUpdateMode.UserAcknowledgment flow is meant to give a
// warning to the user to backup their data. During enrollment there is no
// user data on the device.
if (auto_update_mode == AutoUpdateMode::kEnrollment ||
auto_update_mode == AutoUpdateMode::kUserAcknowledgment) {
update_checker_callback_.Run(base::BindOnce(&OnUpdateAvailableCheckResult));
}
}
void TPMAutoUpdateModePolicyHandler::ShowTPMAutoUpdateNotificationIfNeeded() {
AutoUpdateMode auto_update_mode = GetTPMAutoUpdateModeSetting(
cros_settings_,
base::BindRepeating(&TPMAutoUpdateModePolicyHandler::OnPolicyChanged,
weak_factory_.GetWeakPtr()));
if (auto_update_mode != AutoUpdateMode::kUserAcknowledgment)
return;
update_checker_callback_.Run(base::BindOnce(
&TPMAutoUpdateModePolicyHandler::ShowTPMAutoUpdateNotification,
weak_factory_.GetWeakPtr()));
}
void TPMAutoUpdateModePolicyHandler::ShowTPMAutoUpdateNotification(
bool update_available) {
if (!update_available)
return;
if (!user_manager::UserManager::IsInitialized())
return;
const user_manager::UserManager* user_manager =
user_manager::UserManager::Get();
if (!user_manager->IsUserLoggedIn() ||
user_manager->IsLoggedInAsAnyKioskApp()) {
return;
}
base::Time notification_shown =
local_state_->GetTime(prefs::kTPMUpdatePlannedNotificationShownTime);
if (notification_shown == base::Time::Min()) {
notification_shown = base::Time::Now();
local_state_->SetTime(prefs::kTPMUpdatePlannedNotificationShownTime,
notification_shown);
show_notification_callback_.Run(
ash::TpmAutoUpdateUserNotification::kPlanned);
}
// Show update on next reboot notification after
// |kTPMUpdatePlannedNotificationWaitTime|.
base::Time show_reboot_notification_time =
notification_shown + kTPMUpdatePlannedNotificationWaitTime;
if (show_reboot_notification_time <= base::Time::Now()) {
if (notification_timer_->IsRunning())
notification_timer_->Stop();
ShowTPMUpdateOnNextRebootNotification();
return;
}
// Set a timer that will display the update on next reboot notification after
// 24 hours.
if (notification_timer_->IsRunning())
return;
notification_timer_->Start(
FROM_HERE, show_reboot_notification_time - base::Time::Now(), this,
&TPMAutoUpdateModePolicyHandler::ShowTPMUpdateOnNextRebootNotification);
}
bool TPMAutoUpdateModePolicyHandler::
WasTPMUpdateOnNextRebootNotificationShown() {
return local_state_->GetBoolean(
prefs::kTPMUpdateOnNextRebootNotificationShown);
}
// static
void TPMAutoUpdateModePolicyHandler::RegisterPrefs(
PrefRegistrySimple* registry) {
registry->RegisterTimePref(prefs::kTPMUpdatePlannedNotificationShownTime,
base::Time::Min());
registry->RegisterBooleanPref(prefs::kTPMUpdateOnNextRebootNotificationShown,
false);
}
void TPMAutoUpdateModePolicyHandler::ShowTPMUpdateOnNextRebootNotification() {
local_state_->SetBoolean(prefs::kTPMUpdateOnNextRebootNotificationShown,
true);
show_notification_callback_.Run(
ash::TpmAutoUpdateUserNotification::kOnNextReboot);
}
void TPMAutoUpdateModePolicyHandler::SetNotificationTimerForTesting(
std::unique_ptr<base::OneShotTimer> timer) {
notification_timer_ = std::move(timer);
}
} // namespace policy
|