Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 2 | // 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 Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 6 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 7 | #include <string> |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 8 | |
| 9 | #include <base/logging.h> |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 10 | #include "base/string_util.h" |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 11 | |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 12 | #include "update_engine/delta_performer.h" |
Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame^] | 13 | #include "update_engine/payload_state_interface.h" |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 14 | #include "update_engine/prefs_interface.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 15 | #include "update_engine/utils.h" |
| 16 | |
| 17 | using std::string; |
| 18 | |
| 19 | namespace chromeos_update_engine { |
| 20 | |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 21 | const char OmahaResponseHandlerAction::kDeadlineFile[] = |
| 22 | "/tmp/update-check-response-deadline"; |
| 23 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 24 | OmahaResponseHandlerAction::OmahaResponseHandlerAction( |
| 25 | SystemState* system_state) |
| 26 | : system_state_(system_state), |
Darin Petkov | abc7bc0 | 2011-02-23 14:39:43 -0800 | [diff] [blame] | 27 | got_no_update_response_(false), |
| 28 | key_path_(DeltaPerformer::kUpdatePayloadPublicKeyPath) {} |
| 29 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 30 | void OmahaResponseHandlerAction::PerformAction() { |
| 31 | CHECK(HasInputObject()); |
| 32 | ScopedActionCompleter completer(processor_, this); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 33 | const OmahaResponse& response = GetInputObject(); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 34 | if (!response.update_exists) { |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 35 | got_no_update_response_ = true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 36 | LOG(INFO) << "There are no updates. Aborting."; |
| 37 | return; |
| 38 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 39 | |
| 40 | // All decisions as to which URL should be used have already been done. So, |
| 41 | // make the download URL as the payload URL at the current url index. |
| 42 | uint32_t url_index = system_state_->payload_state()->GetUrlIndex(); |
| 43 | LOG(INFO) << "Using Url" << url_index << " as the download url this time"; |
| 44 | CHECK(url_index < response.payload_urls.size()); |
| 45 | install_plan_.download_url = response.payload_urls[url_index]; |
| 46 | |
| 47 | // Fill up the other properties based on the response. |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 48 | install_plan_.payload_size = response.size; |
| 49 | install_plan_.payload_hash = response.hash; |
Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 50 | install_plan_.metadata_size = response.metadata_size; |
| 51 | install_plan_.metadata_signature = response.metadata_signature; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 52 | install_plan_.hash_checks_mandatory = AreHashChecksMandatory(response); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 53 | install_plan_.is_resume = |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 54 | DeltaPerformer::CanResumeUpdate(system_state_->prefs(), response.hash); |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 55 | if (!install_plan_.is_resume) { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 56 | LOG_IF(WARNING, !DeltaPerformer::ResetUpdateProgress( |
| 57 | system_state_->prefs(), false)) |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 58 | << "Unable to reset the update progress."; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 59 | LOG_IF(WARNING, !system_state_->prefs()->SetString( |
| 60 | kPrefsUpdateCheckResponseHash, response.hash)) |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 61 | << "Unable to save the update check response hash."; |
| 62 | } |
| 63 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 64 | TEST_AND_RETURN(GetInstallDev( |
| 65 | (!boot_device_.empty() ? boot_device_ : utils::BootDevice()), |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 66 | &install_plan_.install_path)); |
| 67 | install_plan_.kernel_install_path = |
| 68 | utils::BootKernelDevice(install_plan_.install_path); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 69 | |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 70 | TEST_AND_RETURN(HasOutputPipe()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 71 | if (HasOutputPipe()) |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 72 | SetOutputObject(install_plan_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 73 | LOG(INFO) << "Using this install plan:"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 74 | install_plan_.Dump(); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 75 | |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 76 | // Send the deadline data (if any) to Chrome through a file. This is a pretty |
| 77 | // hacky solution but should be OK for now. |
| 78 | // |
| 79 | // TODO(petkov): Rearchitect this to avoid communication through a |
| 80 | // file. Ideallly, we would include this information in D-Bus's GetStatus |
| 81 | // method and UpdateStatus signal. A potential issue is that update_engine may |
| 82 | // be unresponsive during an update download. |
| 83 | utils::WriteFile(kDeadlineFile, |
| 84 | response.deadline.data(), |
| 85 | response.deadline.size()); |
| 86 | chmod(kDeadlineFile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
| 87 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 88 | completer.set_code(kActionCodeSuccess); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool OmahaResponseHandlerAction::GetInstallDev(const std::string& boot_dev, |
| 92 | std::string* install_dev) { |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 93 | TEST_AND_RETURN_FALSE(utils::StringHasPrefix(boot_dev, "/dev/")); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 94 | string ret(boot_dev); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 95 | string::reverse_iterator it = ret.rbegin(); // last character in string |
| 96 | // Right now, we just switch '3' and '5' partition numbers. |
| 97 | TEST_AND_RETURN_FALSE((*it == '3') || (*it == '5')); |
| 98 | *it = (*it == '3') ? '5' : '3'; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 99 | *install_dev = ret; |
| 100 | return true; |
| 101 | } |
| 102 | |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 103 | bool OmahaResponseHandlerAction::AreHashChecksMandatory( |
| 104 | const OmahaResponse& response) { |
| 105 | // All our internal testing uses dev server which doesn't generate metadata |
| 106 | // signatures yet. So, in order not to break image_to_live or other AU tools, |
| 107 | // we should waive the hash checks for those cases. Since all internal |
| 108 | // testing is done using a dev_image or test_image, we can use that as a |
| 109 | // criteria for waiving. This criteria reduces the attack surface as |
| 110 | // opposed to waiving the checks when we're in dev mode, because we do want |
| 111 | // to enforce the hash checks when our end customers run in dev mode if they |
| 112 | // are using an official build, so that they are protected more. |
| 113 | if (!utils::IsOfficialBuild()) { |
| 114 | LOG(INFO) << "Waiving payload hash checks for unofficial builds"; |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | // TODO(jaysri): VALIDATION: For official builds, we currently waive hash |
| 119 | // checks for HTTPS until we have rolled out at least once and are confident |
| 120 | // nothing breaks. chromium-os:37082 tracks turning this on for HTTPS |
| 121 | // eventually. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 122 | |
| 123 | // Even if there's a single non-HTTPS URL, make the hash checks as |
| 124 | // mandatory because we could be downloading the payload from any URL later |
| 125 | // on. It's really hard to do book-keeping based on each byte being |
| 126 | // downloaded to see whether we only used HTTPS throughout. |
| 127 | for (size_t i = 0; i < response.payload_urls.size(); i++) { |
| 128 | if (!StartsWithASCII(response.payload_urls[i], "https://", false)) { |
| 129 | LOG(INFO) << "Mandating payload hash checks since Omaha response " |
| 130 | << "contains non-HTTPS URL(s)"; |
| 131 | return true; |
| 132 | } |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 135 | LOG(INFO) << "Waiving payload hash checks since Omaha response " |
| 136 | << "only has HTTPS URL(s)"; |
| 137 | return false; |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 138 | } |
| 139 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 140 | } // namespace chromeos_update_engine |