blob: 52142a3465dff508cea72e80ab0548204f390ce4 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2011 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
adlr@google.com3defe6a2009-12-04 20:57:17 +000016
Amin Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/cros/omaha_response_handler_action.h"
Darin Petkov73058b42010-10-06 16:32:19 -070018
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -070019#include <limits>
adlr@google.com3defe6a2009-12-04 20:57:17 +000020#include <string>
Darin Petkov73058b42010-10-06 16:32:19 -070021
22#include <base/logging.h>
Sen Jiang2703ef42017-03-16 13:36:21 -070023#include <base/strings/string_number_conversions.h>
Miriam Polzerad671982020-04-23 16:25:58 +020024#include <base/version.h>
Gilad Arnold1f847232014-04-07 12:07:49 -070025#include <policy/device_policy.h>
Darin Petkov73058b42010-10-06 16:32:19 -070026
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/constants.h"
28#include "update_engine/common/hardware_interface.h"
29#include "update_engine/common/prefs_interface.h"
30#include "update_engine/common/utils.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070031#include "update_engine/cros/connection_manager_interface.h"
32#include "update_engine/cros/omaha_request_params.h"
33#include "update_engine/cros/payload_state_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080034#include "update_engine/payload_consumer/delta_performer.h"
Aaron Wood23bd3392017-10-06 14:48:25 -070035#include "update_engine/update_manager/policy.h"
36#include "update_engine/update_manager/update_manager.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000037
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070038using chromeos_update_manager::kRollforwardInfinity;
Aaron Wood23bd3392017-10-06 14:48:25 -070039using chromeos_update_manager::Policy;
40using chromeos_update_manager::UpdateManager;
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -070041using std::numeric_limits;
adlr@google.com3defe6a2009-12-04 20:57:17 +000042using std::string;
43
44namespace chromeos_update_engine {
45
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080046OmahaResponseHandlerAction::OmahaResponseHandlerAction(
47 SystemState* system_state)
Gilad Arnold4dbd47e2013-07-22 05:39:26 -070048 : system_state_(system_state),
Amin Hassanid3f4bea2018-04-30 14:52:40 -070049 deadline_file_(constants::kOmahaResponseDeadlineFile) {}
Darin Petkovabc7bc02011-02-23 14:39:43 -080050
adlr@google.com3defe6a2009-12-04 20:57:17 +000051void OmahaResponseHandlerAction::PerformAction() {
52 CHECK(HasInputObject());
53 ScopedActionCompleter completer(processor_, this);
Darin Petkov6a5b3222010-07-13 14:55:28 -070054 const OmahaResponse& response = GetInputObject();
adlr@google.com3defe6a2009-12-04 20:57:17 +000055 if (!response.update_exists) {
56 LOG(INFO) << "There are no updates. Aborting.";
Sen Jiang3978ddd2018-03-22 18:05:44 -070057 completer.set_code(ErrorCode::kNoUpdate);
adlr@google.com3defe6a2009-12-04 20:57:17 +000058 return;
59 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080060
61 // All decisions as to which URL should be used have already been done. So,
Jay Srinivasan53173b92013-05-17 17:13:01 -070062 // make the current URL as the download URL.
63 string current_url = system_state_->payload_state()->GetCurrentUrl();
64 if (current_url.empty()) {
65 // This shouldn't happen as we should always supply the HTTPS backup URL.
66 // Handling this anyway, just in case.
67 LOG(ERROR) << "There are no suitable URLs in the response to use.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070068 completer.set_code(ErrorCode::kOmahaResponseInvalid);
Jay Srinivasan53173b92013-05-17 17:13:01 -070069 return;
70 }
71
Aaron Wood7f92e2b2017-08-28 14:51:21 -070072 // This is the url to the first package, not all packages.
Jae Hoon Kim694eeb02020-06-01 14:24:08 -070073 // (For updates): All |Action|s prior to this must pass in non-excluded URLs
74 // within the |OmahaResponse|, reference exlusion logic in
75 // |OmahaRequestAction| and keep the enforcement of exclusions for updates.
Jay Srinivasan53173b92013-05-17 17:13:01 -070076 install_plan_.download_url = current_url;
Chris Sosafb1020e2013-07-29 17:27:33 -070077 install_plan_.version = response.version;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080078
Gilad Arnold74b5f552014-10-07 08:17:16 -070079 OmahaRequestParams* const params = system_state_->request_params();
80 PayloadStateInterface* const payload_state = system_state_->payload_state();
David Zeuthen8f191b22013-08-06 12:27:50 -070081
82 // If we're using p2p to download and there is a local peer, use it.
Gilad Arnold74b5f552014-10-07 08:17:16 -070083 if (payload_state->GetUsingP2PForDownloading() &&
84 !payload_state->GetP2PUrl().empty()) {
David Zeuthen8f191b22013-08-06 12:27:50 -070085 LOG(INFO) << "Replacing URL " << install_plan_.download_url
Gilad Arnold74b5f552014-10-07 08:17:16 -070086 << " with local URL " << payload_state->GetP2PUrl()
David Zeuthen8f191b22013-08-06 12:27:50 -070087 << " since p2p is enabled.";
Gilad Arnold74b5f552014-10-07 08:17:16 -070088 install_plan_.download_url = payload_state->GetP2PUrl();
89 payload_state->SetUsingP2PForDownloading(true);
David Zeuthen8f191b22013-08-06 12:27:50 -070090 }
91
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080092 // Fill up the other properties based on the response.
Sen Jiang0affc2c2017-02-10 15:55:05 -080093 string update_check_response_hash;
94 for (const auto& package : response.packages) {
95 brillo::Blob raw_hash;
96 if (!base::HexStringToBytes(package.hash, &raw_hash)) {
97 LOG(ERROR) << "Failed to convert payload hash from hex string to bytes: "
98 << package.hash;
99 completer.set_code(ErrorCode::kOmahaResponseInvalid);
100 return;
101 }
102 install_plan_.payloads.push_back(
Jae Hoon Kim8da11e22019-12-23 11:26:17 -0800103 {.payload_urls = package.payload_urls,
104 .size = package.size,
Sen Jiang0affc2c2017-02-10 15:55:05 -0800105 .metadata_size = package.metadata_size,
106 .metadata_signature = package.metadata_signature,
Sen Jiangcdd52062017-05-18 15:33:10 -0700107 .hash = raw_hash,
108 .type = package.is_delta ? InstallPayloadType::kDelta
Vyshu Khota4c5413d2020-11-04 16:17:25 -0800109 : InstallPayloadType::kFull,
110 .fp = package.fp,
111 .app_id = package.app_id});
Sen Jiang0affc2c2017-02-10 15:55:05 -0800112 update_check_response_hash += package.hash + ":";
Sen Jiang2703ef42017-03-16 13:36:21 -0700113 }
David Zeuthene7f89172013-10-31 10:21:04 -0700114 install_plan_.public_key_rsa = response.public_key_rsa;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800115 install_plan_.hash_checks_mandatory = AreHashChecksMandatory(response);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800116 install_plan_.is_resume = DeltaPerformer::CanResumeUpdate(
117 system_state_->prefs(), update_check_response_hash);
Chris Sosabe45bef2013-04-09 18:25:12 -0700118 if (install_plan_.is_resume) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700119 payload_state->UpdateResumed();
Chris Sosabe45bef2013-04-09 18:25:12 -0700120 } else {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700121 payload_state->UpdateRestarted();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800122 LOG_IF(WARNING,
123 !DeltaPerformer::ResetUpdateProgress(system_state_->prefs(), false))
Darin Petkov0406e402010-10-06 21:33:11 -0700124 << "Unable to reset the update progress.";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800125 LOG_IF(WARNING,
126 !system_state_->prefs()->SetString(kPrefsUpdateCheckResponseHash,
127 update_check_response_hash))
Darin Petkov0406e402010-10-06 21:33:11 -0700128 << "Unable to save the update check response hash.";
129 }
130
Xiaochu Liu88d90382018-08-29 16:09:11 -0700131 if (params->is_install()) {
132 install_plan_.target_slot = system_state_->boot_control()->GetCurrentSlot();
133 install_plan_.source_slot = BootControlInterface::kInvalidSlot;
134 } else {
135 install_plan_.source_slot = system_state_->boot_control()->GetCurrentSlot();
136 install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0;
137 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000138
Alex Deymo85616652015-10-15 18:48:31 -0700139 // The Omaha response doesn't include the channel name for this image, so we
140 // use the download_channel we used during the request to tag the target slot.
141 // This will be used in the next boot to know the channel the image was
142 // downloaded from.
143 string current_channel_key =
144 kPrefsChannelOnSlotPrefix + std::to_string(install_plan_.target_slot);
145 system_state_->prefs()->SetString(current_channel_key,
146 params->download_channel());
147
Marton Hunyady199152d2018-05-07 19:08:48 +0200148 // Checking whether device is able to boot up the returned rollback image.
149 if (response.is_rollback) {
150 if (!params->rollback_allowed()) {
151 LOG(ERROR) << "Received rollback image but rollback is not allowed.";
152 completer.set_code(ErrorCode::kOmahaResponseInvalid);
153 return;
154 }
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700155
156 // Calculate the values on the version values on current device.
Marton Hunyady199152d2018-05-07 19:08:48 +0200157 auto min_kernel_key_version = static_cast<uint32_t>(
158 system_state_->hardware()->GetMinKernelKeyVersion());
159 auto min_firmware_key_version = static_cast<uint32_t>(
160 system_state_->hardware()->GetMinFirmwareKeyVersion());
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700161
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700162 uint32_t kernel_key_version =
163 static_cast<uint32_t>(response.rollback_key_version.kernel_key) << 16 |
164 static_cast<uint32_t>(response.rollback_key_version.kernel);
165 uint32_t firmware_key_version =
166 static_cast<uint32_t>(response.rollback_key_version.firmware_key)
167 << 16 |
168 static_cast<uint32_t>(response.rollback_key_version.firmware);
169
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700170 LOG(INFO) << "Rollback image versions:"
171 << " device_kernel_key_version=" << min_kernel_key_version
172 << " image_kernel_key_version=" << kernel_key_version
173 << " device_firmware_key_version=" << min_firmware_key_version
174 << " image_firmware_key_version=" << firmware_key_version;
175
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700176 // Don't attempt a rollback if the versions are incompatible or the
177 // target image does not specify the version information.
178 if (kernel_key_version == numeric_limits<uint32_t>::max() ||
179 firmware_key_version == numeric_limits<uint32_t>::max() ||
180 kernel_key_version < min_kernel_key_version ||
181 firmware_key_version < min_firmware_key_version) {
Marton Hunyady199152d2018-05-07 19:08:48 +0200182 LOG(ERROR) << "Device won't be able to boot up the rollback image.";
183 completer.set_code(ErrorCode::kRollbackNotPossible);
184 return;
185 }
186 install_plan_.is_rollback = true;
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800187 install_plan_.rollback_data_save_requested =
188 params->rollback_data_save_requested();
Marton Hunyady199152d2018-05-07 19:08:48 +0200189 }
190
Miriam Polzerad671982020-04-23 16:25:58 +0200191 // Powerwash if either the response requires it or the parameters indicated
Miriam Polzeraff72002020-08-27 08:20:39 +0200192 // powerwash (usually because there was a channel downgrade) and we are
193 // downgrading the version. Enterprise rollback, indicated by
194 // |response.is_rollback| is dealt with separately above.
Miriam Polzerad671982020-04-23 16:25:58 +0200195 if (response.powerwash_required) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700196 install_plan_.powerwash_required = true;
Miriam Polzeraff72002020-08-27 08:20:39 +0200197 } else if (params->ShouldPowerwash() && !response.is_rollback) {
Miriam Polzerad671982020-04-23 16:25:58 +0200198 base::Version new_version(response.version);
199 base::Version current_version(params->app_version());
200
201 if (!new_version.IsValid()) {
202 LOG(WARNING) << "Not powerwashing,"
203 << " the update's version number is unreadable."
204 << " Update's version number: " << response.version;
205 } else if (!current_version.IsValid()) {
206 LOG(WARNING) << "Not powerwashing,"
207 << " the current version number is unreadable."
208 << " Current version number: " << params->app_version();
209 } else if (new_version < current_version) {
210 install_plan_.powerwash_required = true;
Miriam Polzeraff72002020-08-27 08:20:39 +0200211 // Always try to preserve enrollment and wifi data for enrolled devices.
212 install_plan_.rollback_data_save_requested =
213 system_state_ && system_state_->device_policy() &&
214 system_state_->device_policy()->IsEnterpriseEnrolled();
Miriam Polzerad671982020-04-23 16:25:58 +0200215 }
216 }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700217
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700218 TEST_AND_RETURN(HasOutputPipe());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000219 if (HasOutputPipe())
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700220 SetOutputObject(install_plan_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000221 LOG(INFO) << "Using this install plan:";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700222 install_plan_.Dump();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700223
Darin Petkov6c118642010-10-21 12:06:30 -0700224 // Send the deadline data (if any) to Chrome through a file. This is a pretty
225 // hacky solution but should be OK for now.
226 //
Alex Vakulenko072359c2014-07-18 11:41:07 -0700227 // TODO(petkov): Re-architect this to avoid communication through a
Chris Sosabe45bef2013-04-09 18:25:12 -0700228 // file. Ideally, we would include this information in D-Bus's GetStatus
Darin Petkov6c118642010-10-21 12:06:30 -0700229 // method and UpdateStatus signal. A potential issue is that update_engine may
230 // be unresponsive during an update download.
Alex Deymo6dd160a2015-10-09 18:45:14 -0700231 if (!deadline_file_.empty()) {
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200232 if (payload_state->GetRollbackHappened()) {
233 // Don't do forced update if rollback has happened since the last update
234 // check where policy was present.
235 LOG(INFO) << "Not forcing update because a rollback happened.";
236 utils::WriteFile(deadline_file_.c_str(), nullptr, 0);
237 } else {
238 utils::WriteFile(deadline_file_.c_str(),
239 response.deadline.data(),
240 response.deadline.size());
241 }
Alex Deymo6dd160a2015-10-09 18:45:14 -0700242 chmod(deadline_file_.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
243 }
Darin Petkov6c118642010-10-21 12:06:30 -0700244
Aaron Wood23bd3392017-10-06 14:48:25 -0700245 // Check the generated install-plan with the Policy to confirm that
246 // it can be applied at this time (or at all).
247 UpdateManager* const update_manager = system_state_->update_manager();
248 CHECK(update_manager);
249 auto ec = ErrorCode::kSuccess;
250 update_manager->PolicyRequest(
251 &Policy::UpdateCanBeApplied, &ec, &install_plan_);
252 completer.set_code(ec);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700253
254 const auto allowed_milestones = params->rollback_allowed_milestones();
255 if (allowed_milestones > 0) {
256 auto max_firmware_rollforward = numeric_limits<uint32_t>::max();
257 auto max_kernel_rollforward = numeric_limits<uint32_t>::max();
258
259 // Determine the version to update the max rollforward verified boot
260 // value.
261 OmahaResponse::RollbackKeyVersion version =
262 response.past_rollback_key_version;
263
264 // Determine the max rollforward values to be set in the TPM.
265 max_firmware_rollforward = static_cast<uint32_t>(version.firmware_key)
266 << 16 |
267 static_cast<uint32_t>(version.firmware);
268 max_kernel_rollforward = static_cast<uint32_t>(version.kernel_key) << 16 |
269 static_cast<uint32_t>(version.kernel);
270
271 // In the case that the value is 0xffffffff, log a warning because the
272 // device should not be installing a rollback image without having version
273 // information.
274 if (max_firmware_rollforward == numeric_limits<uint32_t>::max() ||
275 max_kernel_rollforward == numeric_limits<uint32_t>::max()) {
276 LOG(WARNING)
277 << "Max rollforward values were not sent in rollback response: "
278 << " max_kernel_rollforward=" << max_kernel_rollforward
279 << " max_firmware_rollforward=" << max_firmware_rollforward
280 << " rollback_allowed_milestones="
281 << params->rollback_allowed_milestones();
282 } else {
283 LOG(INFO) << "Setting the max rollforward values: "
284 << " max_kernel_rollforward=" << max_kernel_rollforward
285 << " max_firmware_rollforward=" << max_firmware_rollforward
286 << " rollback_allowed_milestones="
287 << params->rollback_allowed_milestones();
288 system_state_->hardware()->SetMaxKernelKeyRollforward(
289 max_kernel_rollforward);
290 // TODO(crbug/783998): Set max firmware rollforward when implemented.
291 }
292 } else {
293 LOG(INFO) << "Rollback is not allowed. Setting max rollforward values"
294 << " to infinity";
295 // When rollback is not allowed, explicitly set the max roll forward to
296 // infinity.
297 system_state_->hardware()->SetMaxKernelKeyRollforward(kRollforwardInfinity);
298 // TODO(crbug/783998): Set max firmware rollforward when implemented.
299 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000300}
301
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800302bool OmahaResponseHandlerAction::AreHashChecksMandatory(
303 const OmahaResponse& response) {
David Pursell907b4fa2015-01-27 10:27:38 -0800304 // We sometimes need to waive the hash checks in order to download from
305 // sources that don't provide hashes, such as dev server.
306 // At this point UpdateAttempter::IsAnyUpdateSourceAllowed() has already been
307 // checked, so an unofficial update URL won't get this far unless it's OK to
308 // use without a hash. Additionally, we want to always waive hash checks on
309 // unofficial builds (i.e. dev/test images).
310 // The end result is this:
311 // * Base image:
312 // - Official URLs require a hash.
313 // - Unofficial URLs only get this far if the IsAnyUpdateSourceAllowed()
314 // devmode/debugd checks pass, in which case the hash is waived.
315 // * Dev/test image:
316 // - Any URL is allowed through with no hash checking.
317 if (!system_state_->request_params()->IsUpdateUrlOfficial() ||
318 !system_state_->hardware()->IsOfficialBuild()) {
David Pursell02c18642014-11-06 11:26:11 -0800319 // Still do a hash check if a public key is included.
David Zeuthene7f89172013-10-31 10:21:04 -0700320 if (!response.public_key_rsa.empty()) {
David Zeuthenbc27aac2013-11-26 11:17:48 -0800321 // The autoupdate_CatchBadSignatures test checks for this string
322 // in log-files. Keep in sync.
David Zeuthene7f89172013-10-31 10:21:04 -0700323 LOG(INFO) << "Mandating payload hash checks since Omaha Response "
324 << "for unofficial build includes public RSA key.";
325 return true;
326 } else {
David Pursell02c18642014-11-06 11:26:11 -0800327 LOG(INFO) << "Waiving payload hash checks for unofficial update URL.";
David Zeuthene7f89172013-10-31 10:21:04 -0700328 return false;
329 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800330 }
331
Sen Jiang81c705b2018-10-04 14:15:05 -0700332 LOG(INFO) << "Mandating hash checks for official URL on official build.";
333 return true;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800334}
335
adlr@google.com3defe6a2009-12-04 20:57:17 +0000336} // namespace chromeos_update_engine