blob: f4d962eda0c90bd746fba8ff9b3441c2558a119a [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
adlr@google.com3defe6a2009-12-04 20:57:17 +000019#include <string>
Darin Petkov73058b42010-10-06 16:32:19 -070020
21#include <base/logging.h>
Sen Jiang2703ef42017-03-16 13:36:21 -070022#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070023#include <base/strings/string_util.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"
adlr@google.com3defe6a2009-12-04 20:57:17 +000034
35using std::string;
36
37namespace chromeos_update_engine {
38
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080039OmahaResponseHandlerAction::OmahaResponseHandlerAction(
40 SystemState* system_state)
Alex Deymo6dd160a2015-10-09 18:45:14 -070041 : OmahaResponseHandlerAction(system_state,
42 constants::kOmahaResponseDeadlineFile) {}
Gilad Arnold4dbd47e2013-07-22 05:39:26 -070043
44OmahaResponseHandlerAction::OmahaResponseHandlerAction(
45 SystemState* system_state, const string& deadline_file)
46 : system_state_(system_state),
47 got_no_update_response_(false),
Alex Deymoe6fc8e12015-09-28 14:02:17 -070048 key_path_(constants::kUpdatePayloadPublicKeyPath),
Gilad Arnold4dbd47e2013-07-22 05:39:26 -070049 deadline_file_(deadline_file) {}
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) {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080056 got_no_update_response_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000057 LOG(INFO) << "There are no updates. Aborting.";
58 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
72 install_plan_.download_url = current_url;
Chris Sosafb1020e2013-07-29 17:27:33 -070073 install_plan_.version = response.version;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080074
Gilad Arnold74b5f552014-10-07 08:17:16 -070075 OmahaRequestParams* const params = system_state_->request_params();
76 PayloadStateInterface* const payload_state = system_state_->payload_state();
David Zeuthen8f191b22013-08-06 12:27:50 -070077
78 // If we're using p2p to download and there is a local peer, use it.
Gilad Arnold74b5f552014-10-07 08:17:16 -070079 if (payload_state->GetUsingP2PForDownloading() &&
80 !payload_state->GetP2PUrl().empty()) {
David Zeuthen8f191b22013-08-06 12:27:50 -070081 LOG(INFO) << "Replacing URL " << install_plan_.download_url
Gilad Arnold74b5f552014-10-07 08:17:16 -070082 << " with local URL " << payload_state->GetP2PUrl()
David Zeuthen8f191b22013-08-06 12:27:50 -070083 << " since p2p is enabled.";
Gilad Arnold74b5f552014-10-07 08:17:16 -070084 install_plan_.download_url = payload_state->GetP2PUrl();
85 payload_state->SetUsingP2PForDownloading(true);
David Zeuthen8f191b22013-08-06 12:27:50 -070086 }
87
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080088 // Fill up the other properties based on the response.
Sen Jiang0affc2c2017-02-10 15:55:05 -080089 string update_check_response_hash;
90 for (const auto& package : response.packages) {
91 brillo::Blob raw_hash;
92 if (!base::HexStringToBytes(package.hash, &raw_hash)) {
93 LOG(ERROR) << "Failed to convert payload hash from hex string to bytes: "
94 << package.hash;
95 completer.set_code(ErrorCode::kOmahaResponseInvalid);
96 return;
97 }
98 install_plan_.payloads.push_back(
99 {.size = package.size,
100 .metadata_size = package.metadata_size,
101 .metadata_signature = package.metadata_signature,
102 .hash = raw_hash});
103 update_check_response_hash += package.hash + ":";
Sen Jiang2703ef42017-03-16 13:36:21 -0700104 }
David Zeuthene7f89172013-10-31 10:21:04 -0700105 install_plan_.public_key_rsa = response.public_key_rsa;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800106 install_plan_.hash_checks_mandatory = AreHashChecksMandatory(response);
Sen Jiang0affc2c2017-02-10 15:55:05 -0800107 install_plan_.is_resume = DeltaPerformer::CanResumeUpdate(
108 system_state_->prefs(), update_check_response_hash);
Chris Sosabe45bef2013-04-09 18:25:12 -0700109 if (install_plan_.is_resume) {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700110 payload_state->UpdateResumed();
Chris Sosabe45bef2013-04-09 18:25:12 -0700111 } else {
Gilad Arnold74b5f552014-10-07 08:17:16 -0700112 payload_state->UpdateRestarted();
Sen Jiang0affc2c2017-02-10 15:55:05 -0800113 LOG_IF(WARNING,
114 !DeltaPerformer::ResetUpdateProgress(system_state_->prefs(), false))
Darin Petkov0406e402010-10-06 21:33:11 -0700115 << "Unable to reset the update progress.";
Sen Jiang0affc2c2017-02-10 15:55:05 -0800116 LOG_IF(WARNING,
117 !system_state_->prefs()->SetString(kPrefsUpdateCheckResponseHash,
118 update_check_response_hash))
Darin Petkov0406e402010-10-06 21:33:11 -0700119 << "Unable to save the update check response hash.";
120 }
Alex Deymo64d98782016-02-05 18:03:48 -0800121 install_plan_.payload_type = response.is_delta_payload
122 ? InstallPayloadType::kDelta
123 : InstallPayloadType::kFull;
Darin Petkov0406e402010-10-06 21:33:11 -0700124
Alex Deymo763e7db2015-08-27 21:08:08 -0700125 install_plan_.source_slot = system_state_->boot_control()->GetCurrentSlot();
126 install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000127
Alex Deymo85616652015-10-15 18:48:31 -0700128 // The Omaha response doesn't include the channel name for this image, so we
129 // use the download_channel we used during the request to tag the target slot.
130 // This will be used in the next boot to know the channel the image was
131 // downloaded from.
132 string current_channel_key =
133 kPrefsChannelOnSlotPrefix + std::to_string(install_plan_.target_slot);
134 system_state_->prefs()->SetString(current_channel_key,
135 params->download_channel());
136
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700137 if (params->to_more_stable_channel() && params->is_powerwash_allowed())
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700138 install_plan_.powerwash_required = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700139
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700140 TEST_AND_RETURN(HasOutputPipe());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000141 if (HasOutputPipe())
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700142 SetOutputObject(install_plan_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000143 LOG(INFO) << "Using this install plan:";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700144 install_plan_.Dump();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700145
Darin Petkov6c118642010-10-21 12:06:30 -0700146 // Send the deadline data (if any) to Chrome through a file. This is a pretty
147 // hacky solution but should be OK for now.
148 //
Alex Vakulenko072359c2014-07-18 11:41:07 -0700149 // TODO(petkov): Re-architect this to avoid communication through a
Chris Sosabe45bef2013-04-09 18:25:12 -0700150 // file. Ideally, we would include this information in D-Bus's GetStatus
Darin Petkov6c118642010-10-21 12:06:30 -0700151 // method and UpdateStatus signal. A potential issue is that update_engine may
152 // be unresponsive during an update download.
Alex Deymo6dd160a2015-10-09 18:45:14 -0700153 if (!deadline_file_.empty()) {
154 utils::WriteFile(deadline_file_.c_str(),
155 response.deadline.data(),
156 response.deadline.size());
157 chmod(deadline_file_.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
158 }
Darin Petkov6c118642010-10-21 12:06:30 -0700159
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700160 completer.set_code(ErrorCode::kSuccess);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000161}
162
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800163bool OmahaResponseHandlerAction::AreHashChecksMandatory(
164 const OmahaResponse& response) {
David Pursell907b4fa2015-01-27 10:27:38 -0800165 // We sometimes need to waive the hash checks in order to download from
166 // sources that don't provide hashes, such as dev server.
167 // At this point UpdateAttempter::IsAnyUpdateSourceAllowed() has already been
168 // checked, so an unofficial update URL won't get this far unless it's OK to
169 // use without a hash. Additionally, we want to always waive hash checks on
170 // unofficial builds (i.e. dev/test images).
171 // The end result is this:
172 // * Base image:
173 // - Official URLs require a hash.
174 // - Unofficial URLs only get this far if the IsAnyUpdateSourceAllowed()
175 // devmode/debugd checks pass, in which case the hash is waived.
176 // * Dev/test image:
177 // - Any URL is allowed through with no hash checking.
178 if (!system_state_->request_params()->IsUpdateUrlOfficial() ||
179 !system_state_->hardware()->IsOfficialBuild()) {
David Pursell02c18642014-11-06 11:26:11 -0800180 // Still do a hash check if a public key is included.
David Zeuthene7f89172013-10-31 10:21:04 -0700181 if (!response.public_key_rsa.empty()) {
David Zeuthenbc27aac2013-11-26 11:17:48 -0800182 // The autoupdate_CatchBadSignatures test checks for this string
183 // in log-files. Keep in sync.
David Zeuthene7f89172013-10-31 10:21:04 -0700184 LOG(INFO) << "Mandating payload hash checks since Omaha Response "
185 << "for unofficial build includes public RSA key.";
186 return true;
187 } else {
David Pursell02c18642014-11-06 11:26:11 -0800188 LOG(INFO) << "Waiving payload hash checks for unofficial update URL.";
David Zeuthene7f89172013-10-31 10:21:04 -0700189 return false;
190 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800191 }
192
David Zeuthen8f191b22013-08-06 12:27:50 -0700193 // If we're using p2p, |install_plan_.download_url| may contain a
194 // HTTP URL even if |response.payload_urls| contain only HTTPS URLs.
Alex Vakulenko0103c362016-01-20 07:56:15 -0800195 if (!base::StartsWith(install_plan_.download_url, "https://",
196 base::CompareCase::INSENSITIVE_ASCII)) {
David Zeuthen8f191b22013-08-06 12:27:50 -0700197 LOG(INFO) << "Mandating hash checks since download_url is not HTTPS.";
198 return true;
199 }
200
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800201 // TODO(jaysri): VALIDATION: For official builds, we currently waive hash
202 // checks for HTTPS until we have rolled out at least once and are confident
203 // nothing breaks. chromium-os:37082 tracks turning this on for HTTPS
204 // eventually.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800205
206 // Even if there's a single non-HTTPS URL, make the hash checks as
207 // mandatory because we could be downloading the payload from any URL later
208 // on. It's really hard to do book-keeping based on each byte being
209 // downloaded to see whether we only used HTTPS throughout.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800210 for (const auto& package : response.packages) {
211 for (const string& payload_url : package.payload_urls) {
212 if (!base::StartsWith(
213 payload_url, "https://", base::CompareCase::INSENSITIVE_ASCII)) {
214 LOG(INFO) << "Mandating payload hash checks since Omaha response "
215 << "contains non-HTTPS URL(s)";
216 return true;
217 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800218 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800219 }
220
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800221 LOG(INFO) << "Waiving payload hash checks since Omaha response "
222 << "only has HTTPS URL(s)";
223 return false;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800224}
225
adlr@google.com3defe6a2009-12-04 20:57:17 +0000226} // namespace chromeos_update_engine