| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 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.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 |  | 
|  | 17 | #include "update_engine/omaha_response_handler_action.h" | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 18 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 19 | #include <string> | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 20 |  | 
|  | 21 | #include <base/logging.h> | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 22 | #include <base/strings/string_util.h> | 
| Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 23 | #include <policy/device_policy.h> | 
| Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 24 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 25 | #include "update_engine/common/constants.h" | 
|  | 26 | #include "update_engine/common/hardware_interface.h" | 
|  | 27 | #include "update_engine/common/prefs_interface.h" | 
|  | 28 | #include "update_engine/common/utils.h" | 
| Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 29 | #include "update_engine/connection_manager_interface.h" | 
| Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 30 | #include "update_engine/omaha_request_params.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 31 | #include "update_engine/payload_consumer/delta_performer.h" | 
| Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 32 | #include "update_engine/payload_state_interface.h" | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 33 |  | 
|  | 34 | using std::string; | 
|  | 35 |  | 
|  | 36 | namespace chromeos_update_engine { | 
|  | 37 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 38 | OmahaResponseHandlerAction::OmahaResponseHandlerAction( | 
|  | 39 | SystemState* system_state) | 
| Alex Deymo | 6dd160a | 2015-10-09 18:45:14 -0700 | [diff] [blame] | 40 | : OmahaResponseHandlerAction(system_state, | 
|  | 41 | constants::kOmahaResponseDeadlineFile) {} | 
| Gilad Arnold | 4dbd47e | 2013-07-22 05:39:26 -0700 | [diff] [blame] | 42 |  | 
|  | 43 | OmahaResponseHandlerAction::OmahaResponseHandlerAction( | 
|  | 44 | SystemState* system_state, const string& deadline_file) | 
|  | 45 | : system_state_(system_state), | 
|  | 46 | got_no_update_response_(false), | 
| Alex Deymo | e6fc8e1 | 2015-09-28 14:02:17 -0700 | [diff] [blame] | 47 | key_path_(constants::kUpdatePayloadPublicKeyPath), | 
| Gilad Arnold | 4dbd47e | 2013-07-22 05:39:26 -0700 | [diff] [blame] | 48 | deadline_file_(deadline_file) {} | 
| Darin Petkov | abc7bc0 | 2011-02-23 14:39:43 -0800 | [diff] [blame] | 49 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 50 | void OmahaResponseHandlerAction::PerformAction() { | 
|  | 51 | CHECK(HasInputObject()); | 
|  | 52 | ScopedActionCompleter completer(processor_, this); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 53 | const OmahaResponse& response = GetInputObject(); | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 54 | if (!response.update_exists) { | 
| Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 55 | got_no_update_response_ = true; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 56 | LOG(INFO) << "There are no updates. Aborting."; | 
|  | 57 | return; | 
|  | 58 | } | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 59 |  | 
|  | 60 | // All decisions as to which URL should be used have already been done. So, | 
| Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 61 | // 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 Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 67 | completer.set_code(ErrorCode::kOmahaResponseInvalid); | 
| Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 68 | return; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | install_plan_.download_url = current_url; | 
| Chris Sosa | fb1020e | 2013-07-29 17:27:33 -0700 | [diff] [blame] | 72 | install_plan_.version = response.version; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 73 |  | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 74 | OmahaRequestParams* const params = system_state_->request_params(); | 
|  | 75 | PayloadStateInterface* const payload_state = system_state_->payload_state(); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 76 |  | 
|  | 77 | // If we're using p2p to download and there is a local peer, use it. | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 78 | if (payload_state->GetUsingP2PForDownloading() && | 
|  | 79 | !payload_state->GetP2PUrl().empty()) { | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 80 | LOG(INFO) << "Replacing URL " << install_plan_.download_url | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 81 | << " with local URL " << payload_state->GetP2PUrl() | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 82 | << " since p2p is enabled."; | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 83 | install_plan_.download_url = payload_state->GetP2PUrl(); | 
|  | 84 | payload_state->SetUsingP2PForDownloading(true); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 87 | // Fill up the other properties based on the response. | 
| Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 88 | install_plan_.payload_size = response.size; | 
|  | 89 | install_plan_.payload_hash = response.hash; | 
| Jay Srinivasan | f431870 | 2012-09-24 11:56:24 -0700 | [diff] [blame] | 90 | install_plan_.metadata_size = response.metadata_size; | 
|  | 91 | install_plan_.metadata_signature = response.metadata_signature; | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 92 | install_plan_.public_key_rsa = response.public_key_rsa; | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 93 | install_plan_.hash_checks_mandatory = AreHashChecksMandatory(response); | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 94 | install_plan_.is_resume = | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 95 | DeltaPerformer::CanResumeUpdate(system_state_->prefs(), response.hash); | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 96 | if (install_plan_.is_resume) { | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 97 | payload_state->UpdateResumed(); | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 98 | } else { | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 99 | payload_state->UpdateRestarted(); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 100 | LOG_IF(WARNING, !DeltaPerformer::ResetUpdateProgress( | 
|  | 101 | system_state_->prefs(), false)) | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 102 | << "Unable to reset the update progress."; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 103 | LOG_IF(WARNING, !system_state_->prefs()->SetString( | 
|  | 104 | kPrefsUpdateCheckResponseHash, response.hash)) | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 105 | << "Unable to save the update check response hash."; | 
|  | 106 | } | 
| Alex Deymo | 64d9878 | 2016-02-05 18:03:48 -0800 | [diff] [blame] | 107 | install_plan_.payload_type = response.is_delta_payload | 
|  | 108 | ? InstallPayloadType::kDelta | 
|  | 109 | : InstallPayloadType::kFull; | 
| Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame] | 110 |  | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 111 | install_plan_.source_slot = system_state_->boot_control()->GetCurrentSlot(); | 
|  | 112 | install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 113 |  | 
| Alex Deymo | 8561665 | 2015-10-15 18:48:31 -0700 | [diff] [blame] | 114 | // The Omaha response doesn't include the channel name for this image, so we | 
|  | 115 | // use the download_channel we used during the request to tag the target slot. | 
|  | 116 | // This will be used in the next boot to know the channel the image was | 
|  | 117 | // downloaded from. | 
|  | 118 | string current_channel_key = | 
|  | 119 | kPrefsChannelOnSlotPrefix + std::to_string(install_plan_.target_slot); | 
|  | 120 | system_state_->prefs()->SetString(current_channel_key, | 
|  | 121 | params->download_channel()); | 
|  | 122 |  | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 123 | if (params->to_more_stable_channel() && params->is_powerwash_allowed()) | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 124 | install_plan_.powerwash_required = true; | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 125 |  | 
| Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 126 | TEST_AND_RETURN(HasOutputPipe()); | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 127 | if (HasOutputPipe()) | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 128 | SetOutputObject(install_plan_); | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 129 | LOG(INFO) << "Using this install plan:"; | 
| Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 130 | install_plan_.Dump(); | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 131 |  | 
| Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 132 | // Send the deadline data (if any) to Chrome through a file. This is a pretty | 
|  | 133 | // hacky solution but should be OK for now. | 
|  | 134 | // | 
| Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 135 | // TODO(petkov): Re-architect this to avoid communication through a | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 136 | // file. Ideally, we would include this information in D-Bus's GetStatus | 
| Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 137 | // method and UpdateStatus signal. A potential issue is that update_engine may | 
|  | 138 | // be unresponsive during an update download. | 
| Alex Deymo | 6dd160a | 2015-10-09 18:45:14 -0700 | [diff] [blame] | 139 | if (!deadline_file_.empty()) { | 
|  | 140 | utils::WriteFile(deadline_file_.c_str(), | 
|  | 141 | response.deadline.data(), | 
|  | 142 | response.deadline.size()); | 
|  | 143 | chmod(deadline_file_.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | 
|  | 144 | } | 
| Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 145 |  | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 146 | completer.set_code(ErrorCode::kSuccess); | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 147 | } | 
|  | 148 |  | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 149 | bool OmahaResponseHandlerAction::AreHashChecksMandatory( | 
|  | 150 | const OmahaResponse& response) { | 
| David Pursell | 907b4fa | 2015-01-27 10:27:38 -0800 | [diff] [blame] | 151 | // We sometimes need to waive the hash checks in order to download from | 
|  | 152 | // sources that don't provide hashes, such as dev server. | 
|  | 153 | // At this point UpdateAttempter::IsAnyUpdateSourceAllowed() has already been | 
|  | 154 | // checked, so an unofficial update URL won't get this far unless it's OK to | 
|  | 155 | // use without a hash. Additionally, we want to always waive hash checks on | 
|  | 156 | // unofficial builds (i.e. dev/test images). | 
|  | 157 | // The end result is this: | 
|  | 158 | //  * Base image: | 
|  | 159 | //    - Official URLs require a hash. | 
|  | 160 | //    - Unofficial URLs only get this far if the IsAnyUpdateSourceAllowed() | 
|  | 161 | //      devmode/debugd checks pass, in which case the hash is waived. | 
|  | 162 | //  * Dev/test image: | 
|  | 163 | //    - Any URL is allowed through with no hash checking. | 
|  | 164 | if (!system_state_->request_params()->IsUpdateUrlOfficial() || | 
|  | 165 | !system_state_->hardware()->IsOfficialBuild()) { | 
| David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 166 | // Still do a hash check if a public key is included. | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 167 | if (!response.public_key_rsa.empty()) { | 
| David Zeuthen | bc27aac | 2013-11-26 11:17:48 -0800 | [diff] [blame] | 168 | // The autoupdate_CatchBadSignatures test checks for this string | 
|  | 169 | // in log-files. Keep in sync. | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 170 | LOG(INFO) << "Mandating payload hash checks since Omaha Response " | 
|  | 171 | << "for unofficial build includes public RSA key."; | 
|  | 172 | return true; | 
|  | 173 | } else { | 
| David Pursell | 02c1864 | 2014-11-06 11:26:11 -0800 | [diff] [blame] | 174 | LOG(INFO) << "Waiving payload hash checks for unofficial update URL."; | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 175 | return false; | 
|  | 176 | } | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 177 | } | 
|  | 178 |  | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 179 | // If we're using p2p, |install_plan_.download_url| may contain a | 
|  | 180 | // HTTP URL even if |response.payload_urls| contain only HTTPS URLs. | 
| Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 181 | if (!base::StartsWith(install_plan_.download_url, "https://", | 
|  | 182 | base::CompareCase::INSENSITIVE_ASCII)) { | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 183 | LOG(INFO) << "Mandating hash checks since download_url is not HTTPS."; | 
|  | 184 | return true; | 
|  | 185 | } | 
|  | 186 |  | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 187 | // TODO(jaysri): VALIDATION: For official builds, we currently waive hash | 
|  | 188 | // checks for HTTPS until we have rolled out at least once and are confident | 
|  | 189 | // nothing breaks. chromium-os:37082 tracks turning this on for HTTPS | 
|  | 190 | // eventually. | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 191 |  | 
|  | 192 | // Even if there's a single non-HTTPS URL, make the hash checks as | 
|  | 193 | // mandatory because we could be downloading the payload from any URL later | 
|  | 194 | // on. It's really hard to do book-keeping based on each byte being | 
|  | 195 | // downloaded to see whether we only used HTTPS throughout. | 
|  | 196 | for (size_t i = 0; i < response.payload_urls.size(); i++) { | 
| Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 197 | if (!base::StartsWith(response.payload_urls[i], "https://", | 
|  | 198 | base::CompareCase::INSENSITIVE_ASCII)) { | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 199 | LOG(INFO) << "Mandating payload hash checks since Omaha response " | 
|  | 200 | << "contains non-HTTPS URL(s)"; | 
|  | 201 | return true; | 
|  | 202 | } | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 203 | } | 
|  | 204 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 205 | LOG(INFO) << "Waiving payload hash checks since Omaha response " | 
|  | 206 | << "only has HTTPS URL(s)"; | 
|  | 207 | return false; | 
| Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 210 | }  // namespace chromeos_update_engine |