blob: 5b4410c8eb6030397f8b5ad119efb5fdeb8af257 [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>
Alex Vakulenko75039d72014-03-25 12:36:28 -070010#include <base/strings/string_util.h>
Gilad Arnold1f847232014-04-07 12:07:49 -070011#include <policy/device_policy.h>
Darin Petkov73058b42010-10-06 16:32:19 -070012
Chris Sosa77f79e82014-06-02 18:16:24 -070013#include "update_engine/connection_manager.h"
Jay Srinivasand29695d2013-04-08 15:08:05 -070014#include "update_engine/constants.h"
Darin Petkov0406e402010-10-06 21:33:11 -070015#include "update_engine/delta_performer.h"
Alex Deymo42432912013-07-12 20:21:15 -070016#include "update_engine/hardware_interface.h"
Gilad Arnold1f847232014-04-07 12:07:49 -070017#include "update_engine/omaha_request_params.h"
Jay Srinivasan55f50c22013-01-10 19:24:35 -080018#include "update_engine/payload_state_interface.h"
Darin Petkov73058b42010-10-06 16:32:19 -070019#include "update_engine/prefs_interface.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000020#include "update_engine/utils.h"
21
22using std::string;
23
24namespace chromeos_update_engine {
25
Darin Petkov6c118642010-10-21 12:06:30 -070026const char OmahaResponseHandlerAction::kDeadlineFile[] =
27 "/tmp/update-check-response-deadline";
28
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080029OmahaResponseHandlerAction::OmahaResponseHandlerAction(
30 SystemState* system_state)
31 : system_state_(system_state),
Darin Petkovabc7bc02011-02-23 14:39:43 -080032 got_no_update_response_(false),
Gilad Arnold4dbd47e2013-07-22 05:39:26 -070033 key_path_(DeltaPerformer::kUpdatePayloadPublicKeyPath),
34 deadline_file_(kDeadlineFile) {}
35
36OmahaResponseHandlerAction::OmahaResponseHandlerAction(
37 SystemState* system_state, const string& deadline_file)
38 : system_state_(system_state),
39 got_no_update_response_(false),
40 key_path_(DeltaPerformer::kUpdatePayloadPublicKeyPath),
41 deadline_file_(deadline_file) {}
Darin Petkovabc7bc02011-02-23 14:39:43 -080042
adlr@google.com3defe6a2009-12-04 20:57:17 +000043void OmahaResponseHandlerAction::PerformAction() {
44 CHECK(HasInputObject());
45 ScopedActionCompleter completer(processor_, this);
Darin Petkov6a5b3222010-07-13 14:55:28 -070046 const OmahaResponse& response = GetInputObject();
adlr@google.com3defe6a2009-12-04 20:57:17 +000047 if (!response.update_exists) {
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080048 got_no_update_response_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000049 LOG(INFO) << "There are no updates. Aborting.";
50 return;
51 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080052
53 // All decisions as to which URL should be used have already been done. So,
Jay Srinivasan53173b92013-05-17 17:13:01 -070054 // make the current URL as the download URL.
55 string current_url = system_state_->payload_state()->GetCurrentUrl();
56 if (current_url.empty()) {
57 // This shouldn't happen as we should always supply the HTTPS backup URL.
58 // Handling this anyway, just in case.
59 LOG(ERROR) << "There are no suitable URLs in the response to use.";
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070060 completer.set_code(ErrorCode::kOmahaResponseInvalid);
Jay Srinivasan53173b92013-05-17 17:13:01 -070061 return;
62 }
63
64 install_plan_.download_url = current_url;
Chris Sosafb1020e2013-07-29 17:27:33 -070065 install_plan_.version = response.version;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080066
David Zeuthen8f191b22013-08-06 12:27:50 -070067 OmahaRequestParams* params = system_state_->request_params();
68
69 // If we're using p2p to download and there is a local peer, use it.
70 if (params->use_p2p_for_downloading() && !params->p2p_url().empty()) {
71 LOG(INFO) << "Replacing URL " << install_plan_.download_url
72 << " with local URL " << params->p2p_url()
73 << " since p2p is enabled.";
74 install_plan_.download_url = params->p2p_url();
David Zeuthenbb8bdc72013-09-03 13:43:48 -070075 system_state_->payload_state()->SetUsingP2PForDownloading(true);
David Zeuthen8f191b22013-08-06 12:27:50 -070076 }
77
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080078 // Fill up the other properties based on the response.
Jay Srinivasan51dcf262012-09-13 17:24:32 -070079 install_plan_.payload_size = response.size;
80 install_plan_.payload_hash = response.hash;
Jay Srinivasanf4318702012-09-24 11:56:24 -070081 install_plan_.metadata_size = response.metadata_size;
82 install_plan_.metadata_signature = response.metadata_signature;
David Zeuthene7f89172013-10-31 10:21:04 -070083 install_plan_.public_key_rsa = response.public_key_rsa;
Jay Srinivasan738fdf32012-12-07 17:40:54 -080084 install_plan_.hash_checks_mandatory = AreHashChecksMandatory(response);
Darin Petkov0406e402010-10-06 21:33:11 -070085 install_plan_.is_resume =
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080086 DeltaPerformer::CanResumeUpdate(system_state_->prefs(), response.hash);
Chris Sosabe45bef2013-04-09 18:25:12 -070087 if (install_plan_.is_resume) {
88 system_state_->payload_state()->UpdateResumed();
89 } else {
Jay Srinivasan19409b72013-04-12 19:23:36 -070090 system_state_->payload_state()->UpdateRestarted();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080091 LOG_IF(WARNING, !DeltaPerformer::ResetUpdateProgress(
92 system_state_->prefs(), false))
Darin Petkov0406e402010-10-06 21:33:11 -070093 << "Unable to reset the update progress.";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080094 LOG_IF(WARNING, !system_state_->prefs()->SetString(
95 kPrefsUpdateCheckResponseHash, response.hash))
Darin Petkov0406e402010-10-06 21:33:11 -070096 << "Unable to save the update check response hash.";
97 }
Gilad Arnold21504f02013-05-24 08:51:22 -070098 install_plan_.is_full_update = !response.is_delta_payload;
Darin Petkov0406e402010-10-06 21:33:11 -070099
Chris Sosad317e402013-06-12 13:47:09 -0700100 TEST_AND_RETURN(utils::GetInstallDev(
Alex Deymo42432912013-07-12 20:21:15 -0700101 (!boot_device_.empty() ? boot_device_ :
102 system_state_->hardware()->BootDevice()),
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700103 &install_plan_.install_path));
104 install_plan_.kernel_install_path =
J. Richard Barnette30842932013-10-28 15:04:23 -0700105 utils::KernelDeviceOfBootDevice(install_plan_.install_path);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000106
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700107 if (params->to_more_stable_channel() && params->is_powerwash_allowed())
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700108 install_plan_.powerwash_required = true;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700109
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700110
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700111 TEST_AND_RETURN(HasOutputPipe());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000112 if (HasOutputPipe())
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700113 SetOutputObject(install_plan_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000114 LOG(INFO) << "Using this install plan:";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700115 install_plan_.Dump();
Darin Petkov6a5b3222010-07-13 14:55:28 -0700116
Darin Petkov6c118642010-10-21 12:06:30 -0700117 // Send the deadline data (if any) to Chrome through a file. This is a pretty
118 // hacky solution but should be OK for now.
119 //
120 // TODO(petkov): Rearchitect this to avoid communication through a
Chris Sosabe45bef2013-04-09 18:25:12 -0700121 // file. Ideally, we would include this information in D-Bus's GetStatus
Darin Petkov6c118642010-10-21 12:06:30 -0700122 // method and UpdateStatus signal. A potential issue is that update_engine may
123 // be unresponsive during an update download.
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700124 utils::WriteFile(deadline_file_.c_str(),
Darin Petkov6c118642010-10-21 12:06:30 -0700125 response.deadline.data(),
126 response.deadline.size());
Gilad Arnold4dbd47e2013-07-22 05:39:26 -0700127 chmod(deadline_file_.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
Darin Petkov6c118642010-10-21 12:06:30 -0700128
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700129 completer.set_code(ErrorCode::kSuccess);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000130}
131
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800132bool OmahaResponseHandlerAction::AreHashChecksMandatory(
133 const OmahaResponse& response) {
David Zeuthene7f89172013-10-31 10:21:04 -0700134 // All our internal testing uses dev server which doesn't generate
135 // metadata signatures by default. So, in order not to break
136 // image_to_live or other AU tools, we should waive the hash checks
137 // for those cases, except if the response indicates that the
138 // payload is signed.
139 //
140 // Since all internal testing is done using a dev_image or
141 // test_image, we can use that as a criteria for waiving. This
142 // criteria reduces the attack surface as opposed to waiving the
143 // checks when we're in dev mode, because we do want to enforce the
144 // hash checks when our end customers run in dev mode if they are
145 // using an official build, so that they are protected more.
J. Richard Barnette056b0ab2013-10-29 15:24:56 -0700146 if (!system_state_->hardware()->IsOfficialBuild()) {
David Zeuthene7f89172013-10-31 10:21:04 -0700147 if (!response.public_key_rsa.empty()) {
David Zeuthenbc27aac2013-11-26 11:17:48 -0800148 // The autoupdate_CatchBadSignatures test checks for this string
149 // in log-files. Keep in sync.
David Zeuthene7f89172013-10-31 10:21:04 -0700150 LOG(INFO) << "Mandating payload hash checks since Omaha Response "
151 << "for unofficial build includes public RSA key.";
152 return true;
153 } else {
154 LOG(INFO) << "Waiving payload hash checks for unofficial builds";
155 return false;
156 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800157 }
158
David Zeuthen8f191b22013-08-06 12:27:50 -0700159 // If we're using p2p, |install_plan_.download_url| may contain a
160 // HTTP URL even if |response.payload_urls| contain only HTTPS URLs.
161 if (!StartsWithASCII(install_plan_.download_url, "https://", false)) {
162 LOG(INFO) << "Mandating hash checks since download_url is not HTTPS.";
163 return true;
164 }
165
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800166 // TODO(jaysri): VALIDATION: For official builds, we currently waive hash
167 // checks for HTTPS until we have rolled out at least once and are confident
168 // nothing breaks. chromium-os:37082 tracks turning this on for HTTPS
169 // eventually.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800170
171 // Even if there's a single non-HTTPS URL, make the hash checks as
172 // mandatory because we could be downloading the payload from any URL later
173 // on. It's really hard to do book-keeping based on each byte being
174 // downloaded to see whether we only used HTTPS throughout.
175 for (size_t i = 0; i < response.payload_urls.size(); i++) {
176 if (!StartsWithASCII(response.payload_urls[i], "https://", false)) {
177 LOG(INFO) << "Mandating payload hash checks since Omaha response "
178 << "contains non-HTTPS URL(s)";
179 return true;
180 }
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800181 }
182
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800183 LOG(INFO) << "Waiving payload hash checks since Omaha response "
184 << "only has HTTPS URL(s)";
185 return false;
Jay Srinivasan738fdf32012-12-07 17:40:54 -0800186}
187
adlr@google.com3defe6a2009-12-04 20:57:17 +0000188} // namespace chromeos_update_engine