blob: 4625037bec2300a336ca0e2f944647fbea9a5da0 [file] [log] [blame]
Darin Petkov7ed561b2011-10-04 02:59:03 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/omaha_response_handler_action.h"
Darin Petkov73058b42010-10-06 16:32:19 -07006
adlr@google.com3defe6a2009-12-04 20:57:17 +00007#include <string>
Darin Petkov73058b42010-10-06 16:32:19 -07008
9#include <base/logging.h>
Jay Srinivasan738fdf32012-12-07 17:40:54 -080010#include "base/string_util.h"
Darin Petkov73058b42010-10-06 16:32:19 -070011
Jay Srinivasand29695d2013-04-08 15:08:05 -070012#include "update_engine/constants.h"
Darin Petkov0406e402010-10-06 21:33:11 -070013#include "update_engine/delta_performer.h"
Alex Deymo42432912013-07-12 20:21:15 -070014#include "update_engine/hardware_interface.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080015#include "update_engine/payload_state_interface.h"
Darin Petkov73058b42010-10-06 16:32:19 -070016#include "update_engine/prefs_interface.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000017#include "update_engine/utils.h"
18
19using std::string;
20
21namespace chromeos_update_engine {
22
Darin Petkov6c118642010-10-21 12:06:30 -070023const char OmahaResponseHandlerAction::kDeadlineFile[] =
24 "/tmp/update-check-response-deadline";
25
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080026OmahaResponseHandlerAction::OmahaResponseHandlerAction(
27 SystemState* system_state)
28 : system_state_(system_state),
Darin Petkovabc7bc02011-02-23 14:39:43 -080029 got_no_update_response_(false),
Gilad Arnold4dbd47e2013-07-22 05:39:26 -070030 key_path_(DeltaPerformer::kUpdatePayloadPublicKeyPath),
31 deadline_file_(kDeadlineFile) {}
32
33OmahaResponseHandlerAction::OmahaResponseHandlerAction(
34 SystemState* system_state, const string& deadline_file)
35 : system_state_(system_state),
36 got_no_update_response_(false),
37 key_path_(DeltaPerformer::kUpdatePayloadPublicKeyPath),
38 deadline_file_(deadline_file) {}
Darin Petkovabc7bc02011-02-23 14:39:43 -080039
adlr@google.com3defe6a2009-12-04 20:57:17 +000040void OmahaResponseHandlerAction::PerformAction() {
41 CHECK(HasInputObject());
42 ScopedActionCompleter completer(processor_, this);
Darin Petkov6a5b3222010-07-13 14:55:28 -070043 const OmahaResponse& response = GetInputObject();
adlr@google.com3defe6a2009-12-04 20:57:17 +000044 if (!response.update_exists) {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080045 got_no_update_response_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000046 LOG(INFO) << "There are no updates. Aborting.";
47 return;
48 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080049
Chris Sosaaa18e162013-06-20 13:20:30 -070050 // Note: policy decision to not update to a version we rolled back from.
51 string rollback_version =
52 system_state_->payload_state()->GetRollbackVersion();
Chris Sosab3dcdb32013-09-04 15:22:12 -070053 if(!rollback_version.empty()) {
54 LOG(INFO) << "Detected previous rollback from version " << rollback_version;
55 if(rollback_version == response.version) {
56 LOG(INFO) << "Received version that we rolled back from. Aborting.";
57 return;
58 }
Chris Sosaaa18e162013-06-20 13:20:30 -070059 }
60
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080061 // 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.";
68 completer.set_code(kErrorCodeOmahaResponseInvalid);
69 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
David Zeuthen8f191b22013-08-06 12:27:50 -070075 OmahaRequestParams* params = system_state_->request_params();
76
77 // If we're using p2p to download and there is a local peer, use it.
78 if (params->use_p2p_for_downloading() && !params->p2p_url().empty()) {
79 LOG(INFO) << "Replacing URL " << install_plan_.download_url
80 << " with local URL " << params->p2p_url()
81 << " since p2p is enabled.";
82 install_plan_.download_url = params->p2p_url();
David Zeuthenbb8bdc72013-09-03 13:43:48 -070083 system_state_->payload_state()->SetUsingP2PForDownloading(true);
David Zeuthen8f191b22013-08-06 12:27:50 -070084 }
85
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080086 // Fill up the other properties based on the response.
Jay Srinivasan51dcf262012-09-13 17:24:32 -070087 install_plan_.payload_size = response.size;
88 install_plan_.payload_hash = response.hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -070089 install_plan_.metadata_size = response.metadata_size;
90 install_plan_.metadata_signature = response.metadata_signature;
Jay Srinivasan738fdf32012-12-07 17:40:54 -080091 install_plan_.hash_checks_mandatory = AreHashChecksMandatory(response);
Darin Petkov0406e402010-10-06 21:33:11 -070092 install_plan_.is_resume =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080093 DeltaPerformer::CanResumeUpdate(system_state_->prefs(), response.hash);
Chris Sosabe45bef2013-04-09 18:25:12 -070094 if (install_plan_.is_resume) {
95 system_state_->payload_state()->UpdateResumed();
96 } else {
Jay Srinivasan19409b72013-04-12 19:23:36 -070097 system_state_->payload_state()->UpdateRestarted();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080098 LOG_IF(WARNING, !DeltaPerformer::ResetUpdateProgress(
99 system_state_->prefs(), false))
Darin Petkov0406e402010-10-06 21:33:11 -0700100 << "Unable to reset the update progress.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800101 LOG_IF(WARNING, !system_state_->prefs()->SetString(
102 kPrefsUpdateCheckResponseHash, response.hash))
Darin Petkov0406e402010-10-06 21:33:11 -0700103 << "Unable to save the update check response hash.";
104 }
Gilad Arnold21504f02013-05-24 08:51:22 -0700105 install_plan_.is_full_update = !response.is_delta_payload;
Darin Petkov0406e402010-10-06 21:33:11 -0700106
Chris Sosad317e402013-06-12 13:47:09 -0700107 TEST_AND_RETURN(utils::GetInstallDev(
Alex Deymo42432912013-07-12 20:21:15 -0700108 (!boot_device_.empty() ? boot_device_ :
109 system_state_->hardware()->BootDevice()),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700110 &install_plan_.install_path));
111 install_plan_.kernel_install_path =
J. Richard Barnette30842932013-10-28 15:04:23 -0700112 utils::KernelDeviceOfBootDevice(install_plan_.install_path);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000113
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700114 if (params->to_more_stable_channel() && params->is_powerwash_allowed())
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700115 install_plan_.powerwash_required = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700116
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700117
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700118 TEST_AND_RETURN(HasOutputPipe());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000119 if (HasOutputPipe())
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700120 SetOutputObject(install_plan_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000121 LOG(INFO) << "Using this install plan:";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700122 install_plan_.Dump();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700123
Darin Petkov6c118642010-10-21 12:06:30 -0700124 // Send the deadline data (if any) to Chrome through a file. This is a pretty
125 // hacky solution but should be OK for now.
126 //
127 // TODO(petkov): Rearchitect this to avoid communication through a
Chris Sosabe45bef2013-04-09 18:25:12 -0700128 // file. Ideally, we would include this information in D-Bus's GetStatus
Darin Petkov6c118642010-10-21 12:06:30 -0700129 // method and UpdateStatus signal. A potential issue is that update_engine may
130 // be unresponsive during an update download.
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700131 utils::WriteFile(deadline_file_.c_str(),
Darin Petkov6c118642010-10-21 12:06:30 -0700132 response.deadline.data(),
133 response.deadline.size());
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700134 chmod(deadline_file_.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
Darin Petkov6c118642010-10-21 12:06:30 -0700135
David Zeuthena99981f2013-04-29 13:42:47 -0700136 completer.set_code(kErrorCodeSuccess);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000137}
138
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800139bool OmahaResponseHandlerAction::AreHashChecksMandatory(
140 const OmahaResponse& response) {
141 // All our internal testing uses dev server which doesn't generate metadata
142 // signatures yet. So, in order not to break image_to_live or other AU tools,
143 // we should waive the hash checks for those cases. Since all internal
144 // testing is done using a dev_image or test_image, we can use that as a
145 // criteria for waiving. This criteria reduces the attack surface as
146 // opposed to waiving the checks when we're in dev mode, because we do want
147 // to enforce the hash checks when our end customers run in dev mode if they
148 // are using an official build, so that they are protected more.
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700149 if (!system_state_->hardware()->IsOfficialBuild()) {
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800150 LOG(INFO) << "Waiving payload hash checks for unofficial builds";
151 return false;
152 }
153
David Zeuthen8f191b22013-08-06 12:27:50 -0700154 // If we're using p2p, |install_plan_.download_url| may contain a
155 // HTTP URL even if |response.payload_urls| contain only HTTPS URLs.
156 if (!StartsWithASCII(install_plan_.download_url, "https://", false)) {
157 LOG(INFO) << "Mandating hash checks since download_url is not HTTPS.";
158 return true;
159 }
160
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800161 // TODO(jaysri): VALIDATION: For official builds, we currently waive hash
162 // checks for HTTPS until we have rolled out at least once and are confident
163 // nothing breaks. chromium-os:37082 tracks turning this on for HTTPS
164 // eventually.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800165
166 // Even if there's a single non-HTTPS URL, make the hash checks as
167 // mandatory because we could be downloading the payload from any URL later
168 // on. It's really hard to do book-keeping based on each byte being
169 // downloaded to see whether we only used HTTPS throughout.
170 for (size_t i = 0; i < response.payload_urls.size(); i++) {
171 if (!StartsWithASCII(response.payload_urls[i], "https://", false)) {
172 LOG(INFO) << "Mandating payload hash checks since Omaha response "
173 << "contains non-HTTPS URL(s)";
174 return true;
175 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800176 }
177
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800178 LOG(INFO) << "Waiving payload hash checks since Omaha response "
179 << "only has HTTPS URL(s)";
180 return false;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800181}
182
adlr@google.com3defe6a2009-12-04 20:57:17 +0000183} // namespace chromeos_update_engine