blob: 5741a2bdb1aabc2f2cbbe3a743d83898766fe358 [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
17#include "update_engine/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>
Gilad Arnold1f847232014-04-07 12:07:49 -070024#include <policy/device_policy.h>
Darin Petkov73058b42010-10-06 16:32:19 -070025
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/common/constants.h"
27#include "update_engine/common/hardware_interface.h"
28#include "update_engine/common/prefs_interface.h"
29#include "update_engine/common/utils.h"
Alex Deymof6ee0162015-07-31 12:35:22 -070030#include "update_engine/connection_manager_interface.h"
Gilad Arnold1f847232014-04-07 12:07:49 -070031#include "update_engine/omaha_request_params.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080032#include "update_engine/payload_consumer/delta_performer.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080033#include "update_engine/payload_state_interface.h"
Aaron Wood23bd3392017-10-06 14:48:25 -070034#include "update_engine/update_manager/policy.h"
35#include "update_engine/update_manager/update_manager.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000036
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -070037using chromeos_update_manager::kRollforwardInfinity;
Aaron Wood23bd3392017-10-06 14:48:25 -070038using chromeos_update_manager::Policy;
39using chromeos_update_manager::UpdateManager;
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -070040using std::numeric_limits;
adlr@google.com3defe6a2009-12-04 20:57:17 +000041using std::string;
42
43namespace chromeos_update_engine {
44
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080045OmahaResponseHandlerAction::OmahaResponseHandlerAction(
46 SystemState* system_state)
Gilad Arnold4dbd47e2013-07-22 05:39:26 -070047 : system_state_(system_state),
Amin Hassanid3f4bea2018-04-30 14:52:40 -070048 deadline_file_(constants::kOmahaResponseDeadlineFile) {}
Darin Petkovabc7bc02011-02-23 14:39:43 -080049
adlr@google.com3defe6a2009-12-04 20:57:17 +000050void OmahaResponseHandlerAction::PerformAction() {
51 CHECK(HasInputObject());
52 ScopedActionCompleter completer(processor_, this);
Darin Petkov6a5b3222010-07-13 14:55:28 -070053 const OmahaResponse& response = GetInputObject();
adlr@google.com3defe6a2009-12-04 20:57:17 +000054 if (!response.update_exists) {
55 LOG(INFO) << "There are no updates. Aborting.";
Sen Jiang3978ddd2018-03-22 18:05:44 -070056 completer.set_code(ErrorCode::kNoUpdate);
adlr@google.com3defe6a2009-12-04 20:57:17 +000057 return;
58 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080059
60 // All decisions as to which URL should be used have already been done. So,
Jay Srinivasan53173b92013-05-17 17:13:01 -070061 // make the current URL as the download URL.
62 string current_url = system_state_->payload_state()->GetCurrentUrl();
63 if (current_url.empty()) {
64 // This shouldn't happen as we should always supply the HTTPS backup URL.
65 // Handling this anyway, just in case.
66 LOG(ERROR) << "There are no suitable URLs in the response to use.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070067 completer.set_code(ErrorCode::kOmahaResponseInvalid);
Jay Srinivasan53173b92013-05-17 17:13:01 -070068 return;
69 }
70
Aaron Wood7f92e2b2017-08-28 14:51:21 -070071 // This is the url to the first package, not all packages.
Jay Srinivasan53173b92013-05-17 17:13:01 -070072 install_plan_.download_url = current_url;
Chris Sosafb1020e2013-07-29 17:27:33 -070073 install_plan_.version = response.version;
Aaron Wood7dcdedf2017-09-06 17:17:41 -070074 install_plan_.system_version = response.system_version;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080075
Gilad Arnold74b5f552014-10-07 08:17:16 -070076 OmahaRequestParams* const params = system_state_->request_params();
77 PayloadStateInterface* const payload_state = system_state_->payload_state();
David Zeuthen8f191b22013-08-06 12:27:50 -070078
79 // If we're using p2p to download and there is a local peer, use it.
Gilad Arnold74b5f552014-10-07 08:17:16 -070080 if (payload_state->GetUsingP2PForDownloading() &&
81 !payload_state->GetP2PUrl().empty()) {
David Zeuthen8f191b22013-08-06 12:27:50 -070082 LOG(INFO) << "Replacing URL " << install_plan_.download_url
Gilad Arnold74b5f552014-10-07 08:17:16 -070083 << " with local URL " << payload_state->GetP2PUrl()
David Zeuthen8f191b22013-08-06 12:27:50 -070084 << " since p2p is enabled.";
Gilad Arnold74b5f552014-10-07 08:17:16 -070085 install_plan_.download_url = payload_state->GetP2PUrl();
86 payload_state->SetUsingP2PForDownloading(true);
David Zeuthen8f191b22013-08-06 12:27:50 -070087 }
88
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080089 // Fill up the other properties based on the response.
Sen Jiang0affc2c2017-02-10 15:55:05 -080090 string update_check_response_hash;
91 for (const auto& package : response.packages) {
92 brillo::Blob raw_hash;
93 if (!base::HexStringToBytes(package.hash, &raw_hash)) {
94 LOG(ERROR) << "Failed to convert payload hash from hex string to bytes: "
95 << package.hash;
96 completer.set_code(ErrorCode::kOmahaResponseInvalid);
97 return;
98 }
99 install_plan_.payloads.push_back(
100 {.size = package.size,
101 .metadata_size = package.metadata_size,
102 .metadata_signature = package.metadata_signature,
Sen Jiangcdd52062017-05-18 15:33:10 -0700103 .hash = raw_hash,
104 .type = package.is_delta ? InstallPayloadType::kDelta
105 : InstallPayloadType::kFull});
Sen Jiang0affc2c2017-02-10 15:55:05 -0800106 update_check_response_hash += package.hash + ":";
Sen Jiang2703ef42017-03-16 13:36:21 -0700107 }
David Zeuthene7f89172013-10-31 10:21:04 -0700108 install_plan_.public_key_rsa = response.public_key_rsa;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800109 install_plan_.hash_checks_mandatory = AreHashChecksMandatory(response);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800110 install_plan_.is_resume = DeltaPerformer::CanResumeUpdate(
111 system_state_->prefs(), update_check_response_hash);
Chris Sosabe45bef2013-04-09 18:25:12 -0700112 if (install_plan_.is_resume) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700113 payload_state->UpdateResumed();
Chris Sosabe45bef2013-04-09 18:25:12 -0700114 } else {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700115 payload_state->UpdateRestarted();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800116 LOG_IF(WARNING,
117 !DeltaPerformer::ResetUpdateProgress(system_state_->prefs(), false))
Darin Petkov0406e402010-10-06 21:33:11 -0700118 << "Unable to reset the update progress.";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800119 LOG_IF(WARNING,
120 !system_state_->prefs()->SetString(kPrefsUpdateCheckResponseHash,
121 update_check_response_hash))
Darin Petkov0406e402010-10-06 21:33:11 -0700122 << "Unable to save the update check response hash.";
123 }
124
Xiaochu Liu88d90382018-08-29 16:09:11 -0700125 if (params->is_install()) {
126 install_plan_.target_slot = system_state_->boot_control()->GetCurrentSlot();
127 install_plan_.source_slot = BootControlInterface::kInvalidSlot;
128 } else {
129 install_plan_.source_slot = system_state_->boot_control()->GetCurrentSlot();
130 install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0;
131 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000132
Alex Deymo85616652015-10-15 18:48:31 -0700133 // The Omaha response doesn't include the channel name for this image, so we
134 // use the download_channel we used during the request to tag the target slot.
135 // This will be used in the next boot to know the channel the image was
136 // downloaded from.
137 string current_channel_key =
138 kPrefsChannelOnSlotPrefix + std::to_string(install_plan_.target_slot);
139 system_state_->prefs()->SetString(current_channel_key,
140 params->download_channel());
141
Marton Hunyady199152d2018-05-07 19:08:48 +0200142 // Checking whether device is able to boot up the returned rollback image.
143 if (response.is_rollback) {
144 if (!params->rollback_allowed()) {
145 LOG(ERROR) << "Received rollback image but rollback is not allowed.";
146 completer.set_code(ErrorCode::kOmahaResponseInvalid);
147 return;
148 }
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700149
150 // Calculate the values on the version values on current device.
Marton Hunyady199152d2018-05-07 19:08:48 +0200151 auto min_kernel_key_version = static_cast<uint32_t>(
152 system_state_->hardware()->GetMinKernelKeyVersion());
153 auto min_firmware_key_version = static_cast<uint32_t>(
154 system_state_->hardware()->GetMinFirmwareKeyVersion());
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700155
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700156 uint32_t kernel_key_version =
157 static_cast<uint32_t>(response.rollback_key_version.kernel_key) << 16 |
158 static_cast<uint32_t>(response.rollback_key_version.kernel);
159 uint32_t firmware_key_version =
160 static_cast<uint32_t>(response.rollback_key_version.firmware_key)
161 << 16 |
162 static_cast<uint32_t>(response.rollback_key_version.firmware);
163
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700164 LOG(INFO) << "Rollback image versions:"
165 << " device_kernel_key_version=" << min_kernel_key_version
166 << " image_kernel_key_version=" << kernel_key_version
167 << " device_firmware_key_version=" << min_firmware_key_version
168 << " image_firmware_key_version=" << firmware_key_version;
169
Zentaro Kavanagh0ff621c2018-07-13 13:06:56 -0700170 // Don't attempt a rollback if the versions are incompatible or the
171 // target image does not specify the version information.
172 if (kernel_key_version == numeric_limits<uint32_t>::max() ||
173 firmware_key_version == numeric_limits<uint32_t>::max() ||
174 kernel_key_version < min_kernel_key_version ||
175 firmware_key_version < min_firmware_key_version) {
Marton Hunyady199152d2018-05-07 19:08:48 +0200176 LOG(ERROR) << "Device won't be able to boot up the rollback image.";
177 completer.set_code(ErrorCode::kRollbackNotPossible);
178 return;
179 }
180 install_plan_.is_rollback = true;
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -0800181 install_plan_.rollback_data_save_requested =
182 params->rollback_data_save_requested();
Marton Hunyady199152d2018-05-07 19:08:48 +0200183 }
184
Sen Jiangfe284402018-03-21 14:03:50 -0700185 if (response.powerwash_required || params->ShouldPowerwash())
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700186 install_plan_.powerwash_required = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700187
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700188 TEST_AND_RETURN(HasOutputPipe());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000189 if (HasOutputPipe())
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700190 SetOutputObject(install_plan_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000191 LOG(INFO) << "Using this install plan:";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700192 install_plan_.Dump();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700193
Darin Petkov6c118642010-10-21 12:06:30 -0700194 // Send the deadline data (if any) to Chrome through a file. This is a pretty
195 // hacky solution but should be OK for now.
196 //
Alex Vakulenko072359c2014-07-18 11:41:07 -0700197 // TODO(petkov): Re-architect this to avoid communication through a
Chris Sosabe45bef2013-04-09 18:25:12 -0700198 // file. Ideally, we would include this information in D-Bus's GetStatus
Darin Petkov6c118642010-10-21 12:06:30 -0700199 // method and UpdateStatus signal. A potential issue is that update_engine may
200 // be unresponsive during an update download.
Alex Deymo6dd160a2015-10-09 18:45:14 -0700201 if (!deadline_file_.empty()) {
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200202 if (payload_state->GetRollbackHappened()) {
203 // Don't do forced update if rollback has happened since the last update
204 // check where policy was present.
205 LOG(INFO) << "Not forcing update because a rollback happened.";
206 utils::WriteFile(deadline_file_.c_str(), nullptr, 0);
207 } else {
208 utils::WriteFile(deadline_file_.c_str(),
209 response.deadline.data(),
210 response.deadline.size());
211 }
Alex Deymo6dd160a2015-10-09 18:45:14 -0700212 chmod(deadline_file_.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
213 }
Darin Petkov6c118642010-10-21 12:06:30 -0700214
Aaron Wood23bd3392017-10-06 14:48:25 -0700215 // Check the generated install-plan with the Policy to confirm that
216 // it can be applied at this time (or at all).
217 UpdateManager* const update_manager = system_state_->update_manager();
218 CHECK(update_manager);
219 auto ec = ErrorCode::kSuccess;
220 update_manager->PolicyRequest(
221 &Policy::UpdateCanBeApplied, &ec, &install_plan_);
222 completer.set_code(ec);
Zentaro Kavanagh0ef9a2f2018-07-02 12:05:07 -0700223
224 const auto allowed_milestones = params->rollback_allowed_milestones();
225 if (allowed_milestones > 0) {
226 auto max_firmware_rollforward = numeric_limits<uint32_t>::max();
227 auto max_kernel_rollforward = numeric_limits<uint32_t>::max();
228
229 // Determine the version to update the max rollforward verified boot
230 // value.
231 OmahaResponse::RollbackKeyVersion version =
232 response.past_rollback_key_version;
233
234 // Determine the max rollforward values to be set in the TPM.
235 max_firmware_rollforward = static_cast<uint32_t>(version.firmware_key)
236 << 16 |
237 static_cast<uint32_t>(version.firmware);
238 max_kernel_rollforward = static_cast<uint32_t>(version.kernel_key) << 16 |
239 static_cast<uint32_t>(version.kernel);
240
241 // In the case that the value is 0xffffffff, log a warning because the
242 // device should not be installing a rollback image without having version
243 // information.
244 if (max_firmware_rollforward == numeric_limits<uint32_t>::max() ||
245 max_kernel_rollforward == numeric_limits<uint32_t>::max()) {
246 LOG(WARNING)
247 << "Max rollforward values were not sent in rollback response: "
248 << " max_kernel_rollforward=" << max_kernel_rollforward
249 << " max_firmware_rollforward=" << max_firmware_rollforward
250 << " rollback_allowed_milestones="
251 << params->rollback_allowed_milestones();
252 } else {
253 LOG(INFO) << "Setting the max rollforward values: "
254 << " max_kernel_rollforward=" << max_kernel_rollforward
255 << " max_firmware_rollforward=" << max_firmware_rollforward
256 << " rollback_allowed_milestones="
257 << params->rollback_allowed_milestones();
258 system_state_->hardware()->SetMaxKernelKeyRollforward(
259 max_kernel_rollforward);
260 // TODO(crbug/783998): Set max firmware rollforward when implemented.
261 }
262 } else {
263 LOG(INFO) << "Rollback is not allowed. Setting max rollforward values"
264 << " to infinity";
265 // When rollback is not allowed, explicitly set the max roll forward to
266 // infinity.
267 system_state_->hardware()->SetMaxKernelKeyRollforward(kRollforwardInfinity);
268 // TODO(crbug/783998): Set max firmware rollforward when implemented.
269 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000270}
271
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800272bool OmahaResponseHandlerAction::AreHashChecksMandatory(
273 const OmahaResponse& response) {
David Pursell907b4fa2015-01-27 10:27:38 -0800274 // We sometimes need to waive the hash checks in order to download from
275 // sources that don't provide hashes, such as dev server.
276 // At this point UpdateAttempter::IsAnyUpdateSourceAllowed() has already been
277 // checked, so an unofficial update URL won't get this far unless it's OK to
278 // use without a hash. Additionally, we want to always waive hash checks on
279 // unofficial builds (i.e. dev/test images).
280 // The end result is this:
281 // * Base image:
282 // - Official URLs require a hash.
283 // - Unofficial URLs only get this far if the IsAnyUpdateSourceAllowed()
284 // devmode/debugd checks pass, in which case the hash is waived.
285 // * Dev/test image:
286 // - Any URL is allowed through with no hash checking.
287 if (!system_state_->request_params()->IsUpdateUrlOfficial() ||
288 !system_state_->hardware()->IsOfficialBuild()) {
David Pursell02c18642014-11-06 11:26:11 -0800289 // Still do a hash check if a public key is included.
David Zeuthene7f89172013-10-31 10:21:04 -0700290 if (!response.public_key_rsa.empty()) {
David Zeuthenbc27aac2013-11-26 11:17:48 -0800291 // The autoupdate_CatchBadSignatures test checks for this string
292 // in log-files. Keep in sync.
David Zeuthene7f89172013-10-31 10:21:04 -0700293 LOG(INFO) << "Mandating payload hash checks since Omaha Response "
294 << "for unofficial build includes public RSA key.";
295 return true;
296 } else {
David Pursell02c18642014-11-06 11:26:11 -0800297 LOG(INFO) << "Waiving payload hash checks for unofficial update URL.";
David Zeuthene7f89172013-10-31 10:21:04 -0700298 return false;
299 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800300 }
301
Sen Jiang81c705b2018-10-04 14:15:05 -0700302 LOG(INFO) << "Mandating hash checks for official URL on official build.";
303 return true;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800304}
305
adlr@google.com3defe6a2009-12-04 20:57:17 +0000306} // namespace chromeos_update_engine