Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 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 | // |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 16 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 17 | #include "update_engine/update_manager/chromeos_policy.h" |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 18 | |
Gilad Arnold | e121881 | 2014-05-07 12:21:36 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 20 | #include <set> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 21 | #include <string> |
Amin Hassani | 186ff6a | 2018-02-27 11:06:03 -0800 | [diff] [blame] | 22 | #include <vector> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 23 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 24 | #include <base/logging.h> |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 25 | #include <base/strings/string_util.h> |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 26 | #include <base/time/time.h> |
| 27 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 28 | #include "update_engine/common/error_code.h" |
Alex Deymo | e88e9fe | 2016-02-03 16:38:00 -0800 | [diff] [blame] | 29 | #include "update_engine/common/error_code_utils.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 30 | #include "update_engine/common/utils.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 31 | #include "update_engine/update_manager/device_policy_provider.h" |
Amin Hassani | 186ff6a | 2018-02-27 11:06:03 -0800 | [diff] [blame] | 32 | #include "update_engine/update_manager/enough_slots_ab_updates_policy_impl.h" |
| 33 | #include "update_engine/update_manager/enterprise_device_policy_impl.h" |
| 34 | #include "update_engine/update_manager/interactive_update_policy_impl.h" |
| 35 | #include "update_engine/update_manager/official_build_check_policy_impl.h" |
| 36 | #include "update_engine/update_manager/out_of_box_experience_policy_impl.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 37 | #include "update_engine/update_manager/policy_utils.h" |
| 38 | #include "update_engine/update_manager/shill_provider.h" |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 39 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 40 | using base::Time; |
| 41 | using base::TimeDelta; |
Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 42 | using chromeos_update_engine::ConnectionTethering; |
| 43 | using chromeos_update_engine::ConnectionType; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 44 | using chromeos_update_engine::ErrorCode; |
Aaron Wood | 23bd339 | 2017-10-06 14:48:25 -0700 | [diff] [blame] | 45 | using chromeos_update_engine::InstallPlan; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 46 | using std::get; |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 47 | using std::min; |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 48 | using std::set; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 49 | using std::string; |
Amin Hassani | 186ff6a | 2018-02-27 11:06:03 -0800 | [diff] [blame] | 50 | using std::vector; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 51 | |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 52 | namespace { |
| 53 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 54 | // Examines |err_code| and decides whether the URL index needs to be advanced, |
| 55 | // the error count for the URL incremented, or none of the above. In the first |
| 56 | // case, returns true; in the second case, increments |*url_num_error_p| and |
| 57 | // returns false; otherwise just returns false. |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 58 | // |
| 59 | // TODO(garnold) Adapted from PayloadState::UpdateFailed() (to be retired). |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 60 | bool HandleErrorCode(ErrorCode err_code, int* url_num_error_p) { |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 61 | err_code = chromeos_update_engine::utils::GetBaseErrorCode(err_code); |
| 62 | switch (err_code) { |
| 63 | // Errors which are good indicators of a problem with a particular URL or |
| 64 | // the protocol used in the URL or entities in the communication channel |
| 65 | // (e.g. proxies). We should try the next available URL in the next update |
| 66 | // check to quickly recover from these errors. |
| 67 | case ErrorCode::kPayloadHashMismatchError: |
| 68 | case ErrorCode::kPayloadSizeMismatchError: |
| 69 | case ErrorCode::kDownloadPayloadVerificationError: |
| 70 | case ErrorCode::kDownloadPayloadPubKeyVerificationError: |
| 71 | case ErrorCode::kSignedDeltaPayloadExpectedError: |
| 72 | case ErrorCode::kDownloadInvalidMetadataMagicString: |
| 73 | case ErrorCode::kDownloadSignatureMissingInManifest: |
| 74 | case ErrorCode::kDownloadManifestParseError: |
| 75 | case ErrorCode::kDownloadMetadataSignatureError: |
| 76 | case ErrorCode::kDownloadMetadataSignatureVerificationError: |
| 77 | case ErrorCode::kDownloadMetadataSignatureMismatch: |
| 78 | case ErrorCode::kDownloadOperationHashVerificationError: |
| 79 | case ErrorCode::kDownloadOperationExecutionError: |
| 80 | case ErrorCode::kDownloadOperationHashMismatch: |
| 81 | case ErrorCode::kDownloadInvalidMetadataSize: |
| 82 | case ErrorCode::kDownloadInvalidMetadataSignature: |
| 83 | case ErrorCode::kDownloadOperationHashMissingError: |
| 84 | case ErrorCode::kDownloadMetadataSignatureMissingError: |
| 85 | case ErrorCode::kPayloadMismatchedType: |
| 86 | case ErrorCode::kUnsupportedMajorPayloadVersion: |
| 87 | case ErrorCode::kUnsupportedMinorPayloadVersion: |
| 88 | LOG(INFO) << "Advancing download URL due to error " |
Alex Deymo | e88e9fe | 2016-02-03 16:38:00 -0800 | [diff] [blame] | 89 | << chromeos_update_engine::utils::ErrorCodeToString(err_code) |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 90 | << " (" << static_cast<int>(err_code) << ")"; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 91 | return true; |
| 92 | |
| 93 | // Errors which seem to be just transient network/communication related |
| 94 | // failures and do not indicate any inherent problem with the URL itself. |
| 95 | // So, we should keep the current URL but just increment the |
| 96 | // failure count to give it more chances. This way, while we maximize our |
| 97 | // chances of downloading from the URLs that appear earlier in the response |
| 98 | // (because download from a local server URL that appears earlier in a |
| 99 | // response is preferable than downloading from the next URL which could be |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 100 | // an Internet URL and thus could be more expensive). |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 101 | case ErrorCode::kError: |
| 102 | case ErrorCode::kDownloadTransferError: |
| 103 | case ErrorCode::kDownloadWriteError: |
| 104 | case ErrorCode::kDownloadStateInitializationError: |
Gilad Arnold | 684219d | 2014-07-07 14:54:57 -0700 | [diff] [blame] | 105 | case ErrorCode::kOmahaErrorInHTTPResponse: // Aggregate for HTTP errors. |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 106 | LOG(INFO) << "Incrementing URL failure count due to error " |
Alex Deymo | e88e9fe | 2016-02-03 16:38:00 -0800 | [diff] [blame] | 107 | << chromeos_update_engine::utils::ErrorCodeToString(err_code) |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 108 | << " (" << static_cast<int>(err_code) << ")"; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 109 | *url_num_error_p += 1; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 110 | return false; |
| 111 | |
| 112 | // Errors which are not specific to a URL and hence shouldn't result in |
| 113 | // the URL being penalized. This can happen in two cases: |
| 114 | // 1. We haven't started downloading anything: These errors don't cost us |
| 115 | // anything in terms of actual payload bytes, so we should just do the |
| 116 | // regular retries at the next update check. |
| 117 | // 2. We have successfully downloaded the payload: In this case, the |
| 118 | // payload attempt number would have been incremented and would take care |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 119 | // of the back-off at the next update check. |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 120 | // In either case, there's no need to update URL index or failure count. |
| 121 | case ErrorCode::kOmahaRequestError: |
| 122 | case ErrorCode::kOmahaResponseHandlerError: |
| 123 | case ErrorCode::kPostinstallRunnerError: |
| 124 | case ErrorCode::kFilesystemCopierError: |
| 125 | case ErrorCode::kInstallDeviceOpenError: |
| 126 | case ErrorCode::kKernelDeviceOpenError: |
| 127 | case ErrorCode::kDownloadNewPartitionInfoError: |
| 128 | case ErrorCode::kNewRootfsVerificationError: |
| 129 | case ErrorCode::kNewKernelVerificationError: |
| 130 | case ErrorCode::kPostinstallBootedFromFirmwareB: |
| 131 | case ErrorCode::kPostinstallFirmwareRONotUpdatable: |
| 132 | case ErrorCode::kOmahaRequestEmptyResponseError: |
| 133 | case ErrorCode::kOmahaRequestXMLParseError: |
| 134 | case ErrorCode::kOmahaResponseInvalid: |
| 135 | case ErrorCode::kOmahaUpdateIgnoredPerPolicy: |
| 136 | case ErrorCode::kOmahaUpdateDeferredPerPolicy: |
Kevin Cernekee | 2494e28 | 2016-03-29 18:03:53 -0700 | [diff] [blame] | 137 | case ErrorCode::kNonCriticalUpdateInOOBE: |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 138 | case ErrorCode::kOmahaUpdateDeferredForBackoff: |
| 139 | case ErrorCode::kPostinstallPowerwashError: |
| 140 | case ErrorCode::kUpdateCanceledByChannelChange: |
David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 141 | case ErrorCode::kOmahaRequestXMLHasEntityDecl: |
Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 142 | case ErrorCode::kFilesystemVerifierError: |
Alex Deymo | 1f19dcc | 2016-02-03 09:22:17 -0800 | [diff] [blame] | 143 | case ErrorCode::kUserCanceled: |
Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 144 | case ErrorCode::kOmahaUpdateIgnoredOverCellular: |
Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 145 | case ErrorCode::kUpdatedButNotActive: |
Sen Jiang | 3978ddd | 2018-03-22 18:05:44 -0700 | [diff] [blame] | 146 | case ErrorCode::kNoUpdate: |
Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 147 | case ErrorCode::kRollbackNotPossible: |
Amin Hassani | 80f4d4c | 2018-05-16 13:34:00 -0700 | [diff] [blame^] | 148 | case ErrorCode::kFirstActiveOmahaPingSentPersistenceError: |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 149 | LOG(INFO) << "Not changing URL index or failure count due to error " |
Alex Deymo | e88e9fe | 2016-02-03 16:38:00 -0800 | [diff] [blame] | 150 | << chromeos_update_engine::utils::ErrorCodeToString(err_code) |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 151 | << " (" << static_cast<int>(err_code) << ")"; |
| 152 | return false; |
| 153 | |
| 154 | case ErrorCode::kSuccess: // success code |
| 155 | case ErrorCode::kUmaReportedMax: // not an error code |
| 156 | case ErrorCode::kOmahaRequestHTTPResponseBase: // aggregated already |
Alex Deymo | 1f19dcc | 2016-02-03 09:22:17 -0800 | [diff] [blame] | 157 | case ErrorCode::kDevModeFlag: // not an error code |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 158 | case ErrorCode::kResumedFlag: // not an error code |
| 159 | case ErrorCode::kTestImageFlag: // not an error code |
| 160 | case ErrorCode::kTestOmahaUrlFlag: // not an error code |
| 161 | case ErrorCode::kSpecialFlags: // not an error code |
| 162 | // These shouldn't happen. Enumerating these explicitly here so that we |
| 163 | // can let the compiler warn about new error codes that are added to |
| 164 | // action_processor.h but not added here. |
| 165 | LOG(WARNING) << "Unexpected error " |
Alex Deymo | e88e9fe | 2016-02-03 16:38:00 -0800 | [diff] [blame] | 166 | << chromeos_update_engine::utils::ErrorCodeToString(err_code) |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 167 | << " (" << static_cast<int>(err_code) << ")"; |
| 168 | // Note: Not adding a default here so as to let the compiler warn us of |
| 169 | // any new enums that were added in the .h but not listed in this switch. |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 174 | // Checks whether |url| can be used under given download restrictions. |
| 175 | bool IsUrlUsable(const string& url, bool http_allowed) { |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 176 | return http_allowed || |
| 177 | !base::StartsWith(url, "http://", |
| 178 | base::CompareCase::INSENSITIVE_ASCII); |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | } // namespace |
| 182 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 183 | namespace chromeos_update_manager { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 184 | |
Amin Hassani | 186ff6a | 2018-02-27 11:06:03 -0800 | [diff] [blame] | 185 | const NextUpdateCheckPolicyConstants |
| 186 | ChromeOSPolicy::kNextUpdateCheckPolicyConstants = { |
| 187 | .timeout_initial_interval = 7 * 60, |
| 188 | .timeout_periodic_interval = 45 * 60, |
| 189 | .timeout_max_backoff_interval = 4 * 60 * 60, |
| 190 | .timeout_regular_fuzz = 10 * 60, |
| 191 | .attempt_backoff_max_interval_in_days = 16, |
| 192 | .attempt_backoff_fuzz_in_hours = 12, |
| 193 | }; |
Alex Deymo | 14e7dde | 2015-10-20 14:46:33 -0700 | [diff] [blame] | 194 | |
Gilad Arnold | 349ac83 | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 195 | const int ChromeOSPolicy::kMaxP2PAttempts = 10; |
| 196 | const int ChromeOSPolicy::kMaxP2PAttemptsPeriodInSeconds = 5 * 24 * 60 * 60; |
Gilad Arnold | a2e8eaa | 2014-09-24 13:12:33 -0700 | [diff] [blame] | 197 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 198 | EvalStatus ChromeOSPolicy::UpdateCheckAllowed( |
| 199 | EvaluationContext* ec, State* state, string* error, |
| 200 | UpdateCheckParams* result) const { |
Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 201 | // Set the default return values. |
| 202 | result->updates_enabled = true; |
| 203 | result->target_channel.clear(); |
Gilad Arnold | d4b3032 | 2014-07-21 15:35:27 -0700 | [diff] [blame] | 204 | result->target_version_prefix.clear(); |
Marton Hunyady | ba51c3f | 2018-04-25 15:18:10 +0200 | [diff] [blame] | 205 | result->rollback_allowed = false; |
Marton Hunyady | 0e0e354 | 2018-02-21 18:51:39 +0100 | [diff] [blame] | 206 | result->rollback_allowed_milestones = -1; |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 207 | result->is_interactive = false; |
Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 208 | |
Amin Hassani | 186ff6a | 2018-02-27 11:06:03 -0800 | [diff] [blame] | 209 | EnoughSlotsAbUpdatesPolicyImpl enough_slots_ab_updates_policy; |
| 210 | EnterpriseDevicePolicyImpl enterprise_device_policy; |
| 211 | OnlyUpdateOfficialBuildsPolicyImpl only_update_official_builds_policy; |
| 212 | InteractiveUpdatePolicyImpl interactive_update_policy; |
| 213 | OobePolicyImpl oobe_policy; |
| 214 | NextUpdateCheckTimePolicyImpl next_update_check_time_policy( |
| 215 | kNextUpdateCheckPolicyConstants); |
Gilad Arnold | a1eabcd | 2014-07-09 15:42:40 -0700 | [diff] [blame] | 216 | |
Amin Hassani | 186ff6a | 2018-02-27 11:06:03 -0800 | [diff] [blame] | 217 | vector<Policy const*> policies_to_consult = { |
| 218 | // Do not perform any updates if there are not enough slots to do A/B |
| 219 | // updates. |
| 220 | &enough_slots_ab_updates_policy, |
| 221 | |
| 222 | // Check to see if Enterprise-managed (has DevicePolicy) and/or |
| 223 | // Kiosk-mode. If so, then defer to those settings. |
| 224 | &enterprise_device_policy, |
| 225 | |
| 226 | // Check to see if an interactive update was requested. |
| 227 | &interactive_update_policy, |
| 228 | |
| 229 | // Unofficial builds should not perform periodic update checks. |
| 230 | &only_update_official_builds_policy, |
| 231 | |
| 232 | // If OOBE is enabled, wait until it is completed. |
| 233 | &oobe_policy, |
| 234 | |
| 235 | // Ensure that periodic update checks are timed properly. |
| 236 | &next_update_check_time_policy, |
| 237 | }; |
| 238 | |
| 239 | // Now that the list of policy implementations, and the order to consult them, |
| 240 | // has been setup, consult the policies. If none of the policies make a |
| 241 | // definitive decisions about whether or not to check for updates, then allow |
| 242 | // the update check to happen. |
| 243 | EvalStatus status = ConsultPolicies(policies_to_consult, |
| 244 | &Policy::UpdateCheckAllowed, |
| 245 | ec, |
| 246 | state, |
| 247 | error, |
| 248 | result); |
| 249 | if (EvalStatus::kContinue != status) { |
| 250 | return status; |
| 251 | } else { |
| 252 | // It is time to check for an update. |
| 253 | LOG(INFO) << "Allowing update check."; |
Gilad Arnold | bfc44f7 | 2014-07-09 14:41:39 -0700 | [diff] [blame] | 254 | return EvalStatus::kSucceeded; |
| 255 | } |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Aaron Wood | 23bd339 | 2017-10-06 14:48:25 -0700 | [diff] [blame] | 258 | EvalStatus ChromeOSPolicy::UpdateCanBeApplied(EvaluationContext* ec, |
| 259 | State* state, |
| 260 | std::string* error, |
| 261 | ErrorCode* result, |
| 262 | InstallPlan* install_plan) const { |
| 263 | *result = ErrorCode::kSuccess; |
| 264 | return EvalStatus::kSucceeded; |
| 265 | } |
| 266 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 267 | EvalStatus ChromeOSPolicy::UpdateCanStart( |
| 268 | EvaluationContext* ec, |
| 269 | State* state, |
| 270 | string* error, |
Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 271 | UpdateDownloadParams* result, |
Gilad Arnold | d78caf9 | 2014-09-24 09:28:14 -0700 | [diff] [blame] | 272 | const UpdateState update_state) const { |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 273 | // Set the default return values. Note that we set persisted values (backoff, |
| 274 | // scattering) to the same values presented in the update state. The reason is |
| 275 | // that preemptive returns, such as the case where an update check is due, |
| 276 | // should not clear off the said values; rather, it is the deliberate |
| 277 | // inference of new values that should cause them to be reset. |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 278 | result->update_can_start = false; |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 279 | result->cannot_start_reason = UpdateCannotStartReason::kUndefined; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 280 | result->download_url_idx = -1; |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 281 | result->download_url_allowed = true; |
| 282 | result->download_url_num_errors = 0; |
Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 283 | result->p2p_downloading_allowed = false; |
| 284 | result->p2p_sharing_allowed = false; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 285 | result->do_increment_failures = false; |
| 286 | result->backoff_expiry = update_state.backoff_expiry; |
| 287 | result->scatter_wait_period = update_state.scatter_wait_period; |
| 288 | result->scatter_check_threshold = update_state.scatter_check_threshold; |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 289 | |
| 290 | // Make sure that we're not due for an update check. |
| 291 | UpdateCheckParams check_result; |
| 292 | EvalStatus check_status = UpdateCheckAllowed(ec, state, error, &check_result); |
| 293 | if (check_status == EvalStatus::kFailed) |
| 294 | return EvalStatus::kFailed; |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 295 | bool is_check_due = (check_status == EvalStatus::kSucceeded && |
| 296 | check_result.updates_enabled == true); |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 297 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 298 | // Check whether backoff applies, and if not then which URL can be used for |
| 299 | // downloading. These require scanning the download error log, and so they are |
| 300 | // done together. |
| 301 | UpdateBackoffAndDownloadUrlResult backoff_url_result; |
| 302 | EvalStatus backoff_url_status = UpdateBackoffAndDownloadUrl( |
| 303 | ec, state, error, &backoff_url_result, update_state); |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 304 | if (backoff_url_status == EvalStatus::kFailed) |
| 305 | return EvalStatus::kFailed; |
| 306 | result->download_url_idx = backoff_url_result.url_idx; |
| 307 | result->download_url_num_errors = backoff_url_result.url_num_errors; |
| 308 | result->do_increment_failures = backoff_url_result.do_increment_failures; |
| 309 | result->backoff_expiry = backoff_url_result.backoff_expiry; |
| 310 | bool is_backoff_active = |
| 311 | (backoff_url_status == EvalStatus::kAskMeAgainLater) || |
| 312 | !backoff_url_result.backoff_expiry.is_null(); |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 313 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 314 | DevicePolicyProvider* const dp_provider = state->device_policy_provider(); |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 315 | bool is_scattering_active = false; |
| 316 | EvalStatus scattering_status = EvalStatus::kSucceeded; |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 317 | |
| 318 | const bool* device_policy_is_loaded_p = ec->GetValue( |
| 319 | dp_provider->var_device_policy_is_loaded()); |
| 320 | if (device_policy_is_loaded_p && *device_policy_is_loaded_p) { |
Gilad Arnold | 76a11f6 | 2014-05-20 09:02:12 -0700 | [diff] [blame] | 321 | // Check whether scattering applies to this update attempt. We should not be |
| 322 | // scattering if this is an interactive update check, or if OOBE is enabled |
| 323 | // but not completed. |
| 324 | // |
| 325 | // Note: current code further suppresses scattering if a "deadline" |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 326 | // attribute is found in the Omaha response. However, it appears that the |
Gilad Arnold | 76a11f6 | 2014-05-20 09:02:12 -0700 | [diff] [blame] | 327 | // presence of this attribute is merely indicative of an OOBE update, during |
| 328 | // which we suppress scattering anyway. |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 329 | bool is_scattering_applicable = false; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 330 | result->scatter_wait_period = kZeroInterval; |
| 331 | result->scatter_check_threshold = 0; |
| 332 | if (!update_state.is_interactive) { |
Gilad Arnold | 76a11f6 | 2014-05-20 09:02:12 -0700 | [diff] [blame] | 333 | const bool* is_oobe_enabled_p = ec->GetValue( |
| 334 | state->config_provider()->var_is_oobe_enabled()); |
| 335 | if (is_oobe_enabled_p && !(*is_oobe_enabled_p)) { |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 336 | is_scattering_applicable = true; |
Gilad Arnold | 76a11f6 | 2014-05-20 09:02:12 -0700 | [diff] [blame] | 337 | } else { |
| 338 | const bool* is_oobe_complete_p = ec->GetValue( |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 339 | state->system_provider()->var_is_oobe_complete()); |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 340 | is_scattering_applicable = (is_oobe_complete_p && *is_oobe_complete_p); |
Gilad Arnold | 76a11f6 | 2014-05-20 09:02:12 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | // Compute scattering values. |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 345 | if (is_scattering_applicable) { |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 346 | UpdateScatteringResult scatter_result; |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 347 | scattering_status = UpdateScattering(ec, state, error, &scatter_result, |
| 348 | update_state); |
| 349 | if (scattering_status == EvalStatus::kFailed) { |
| 350 | return EvalStatus::kFailed; |
| 351 | } else { |
| 352 | result->scatter_wait_period = scatter_result.wait_period; |
| 353 | result->scatter_check_threshold = scatter_result.check_threshold; |
| 354 | if (scattering_status == EvalStatus::kAskMeAgainLater || |
| 355 | scatter_result.is_scattering) |
| 356 | is_scattering_active = true; |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 359 | } |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 360 | |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 361 | // Find out whether P2P is globally enabled. |
| 362 | bool p2p_enabled; |
| 363 | EvalStatus p2p_enabled_status = P2PEnabled(ec, state, error, &p2p_enabled); |
| 364 | if (p2p_enabled_status != EvalStatus::kSucceeded) |
| 365 | return EvalStatus::kFailed; |
| 366 | |
| 367 | // Is P2P is enabled, consider allowing it for downloading and/or sharing. |
| 368 | if (p2p_enabled) { |
| 369 | // Sharing via P2P is allowed if not disabled by Omaha. |
| 370 | if (update_state.p2p_sharing_disabled) { |
| 371 | LOG(INFO) << "Blocked P2P sharing because it is disabled by Omaha."; |
Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 372 | } else { |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 373 | result->p2p_sharing_allowed = true; |
Gilad Arnold | ef8d087 | 2014-10-03 14:14:06 -0700 | [diff] [blame] | 374 | } |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 375 | |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 376 | // Downloading via P2P is allowed if not disabled by Omaha, an update is not |
| 377 | // interactive, and other limits haven't been reached. |
| 378 | if (update_state.p2p_downloading_disabled) { |
| 379 | LOG(INFO) << "Blocked P2P downloading because it is disabled by Omaha."; |
| 380 | } else if (update_state.is_interactive) { |
| 381 | LOG(INFO) << "Blocked P2P downloading because update is interactive."; |
Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 382 | } else if (update_state.p2p_num_attempts >= kMaxP2PAttempts) { |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 383 | LOG(INFO) << "Blocked P2P downloading as it was attempted too many " |
| 384 | "times."; |
Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 385 | } else if (!update_state.p2p_first_attempted.is_null() && |
| 386 | ec->IsWallclockTimeGreaterThan( |
| 387 | update_state.p2p_first_attempted + |
| 388 | TimeDelta::FromSeconds(kMaxP2PAttemptsPeriodInSeconds))) { |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 389 | LOG(INFO) << "Blocked P2P downloading as its usage timespan exceeds " |
| 390 | "limit."; |
Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 391 | } else { |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 392 | // P2P download is allowed; if backoff or scattering are active, be sure |
| 393 | // to suppress them, yet prevent any download URL from being used. |
Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 394 | result->p2p_downloading_allowed = true; |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 395 | if (is_backoff_active || is_scattering_active) { |
| 396 | is_backoff_active = is_scattering_active = false; |
| 397 | result->download_url_allowed = false; |
| 398 | } |
Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 399 | } |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 402 | // Check for various deterrents. |
| 403 | if (is_check_due) { |
| 404 | result->cannot_start_reason = UpdateCannotStartReason::kCheckDue; |
| 405 | return EvalStatus::kSucceeded; |
| 406 | } |
| 407 | if (is_backoff_active) { |
| 408 | result->cannot_start_reason = UpdateCannotStartReason::kBackoff; |
| 409 | return backoff_url_status; |
| 410 | } |
| 411 | if (is_scattering_active) { |
| 412 | result->cannot_start_reason = UpdateCannotStartReason::kScattering; |
| 413 | return scattering_status; |
| 414 | } |
Gilad Arnold | b2f9919 | 2014-10-07 13:01:52 -0700 | [diff] [blame] | 415 | if (result->download_url_idx < 0 && !result->p2p_downloading_allowed) { |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 416 | result->cannot_start_reason = UpdateCannotStartReason::kCannotDownload; |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 417 | return EvalStatus::kSucceeded; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Gilad Arnold | 14a9e70 | 2014-10-08 08:09:09 -0700 | [diff] [blame] | 420 | // Update is good to go. |
| 421 | result->update_can_start = true; |
Gilad Arnold | af2f6ae | 2014-04-28 14:14:52 -0700 | [diff] [blame] | 422 | return EvalStatus::kSucceeded; |
| 423 | } |
| 424 | |
Gilad Arnold | a8262e2 | 2014-06-02 13:54:27 -0700 | [diff] [blame] | 425 | // TODO(garnold) Logic in this method is based on |
| 426 | // ConnectionManager::IsUpdateAllowedOver(); be sure to deprecate the latter. |
| 427 | // |
| 428 | // TODO(garnold) The current logic generally treats the list of allowed |
| 429 | // connections coming from the device policy as a whitelist, meaning that it |
| 430 | // can only be used for enabling connections, but not disable them. Further, |
| 431 | // certain connection types (like Bluetooth) cannot be enabled even by policy. |
| 432 | // In effect, the only thing that device policy can change is to enable |
| 433 | // updates over a cellular network (disabled by default). We may want to |
| 434 | // revisit this semantics, allowing greater flexibility in defining specific |
| 435 | // permissions over all types of networks. |
Gilad Arnold | 684219d | 2014-07-07 14:54:57 -0700 | [diff] [blame] | 436 | EvalStatus ChromeOSPolicy::UpdateDownloadAllowed( |
Gilad Arnold | a8262e2 | 2014-06-02 13:54:27 -0700 | [diff] [blame] | 437 | EvaluationContext* ec, |
| 438 | State* state, |
| 439 | string* error, |
| 440 | bool* result) const { |
| 441 | // Get the current connection type. |
| 442 | ShillProvider* const shill_provider = state->shill_provider(); |
| 443 | const ConnectionType* conn_type_p = ec->GetValue( |
| 444 | shill_provider->var_conn_type()); |
| 445 | POLICY_CHECK_VALUE_AND_FAIL(conn_type_p, error); |
| 446 | ConnectionType conn_type = *conn_type_p; |
| 447 | |
| 448 | // If we're tethering, treat it as a cellular connection. |
| 449 | if (conn_type != ConnectionType::kCellular) { |
| 450 | const ConnectionTethering* conn_tethering_p = ec->GetValue( |
| 451 | shill_provider->var_conn_tethering()); |
| 452 | POLICY_CHECK_VALUE_AND_FAIL(conn_tethering_p, error); |
| 453 | if (*conn_tethering_p == ConnectionTethering::kConfirmed) |
| 454 | conn_type = ConnectionType::kCellular; |
| 455 | } |
| 456 | |
| 457 | // By default, we allow updates for all connection types, with exceptions as |
| 458 | // noted below. This also determines whether a device policy can override the |
| 459 | // default. |
| 460 | *result = true; |
| 461 | bool device_policy_can_override = false; |
| 462 | switch (conn_type) { |
| 463 | case ConnectionType::kBluetooth: |
| 464 | *result = false; |
| 465 | break; |
| 466 | |
| 467 | case ConnectionType::kCellular: |
| 468 | *result = false; |
| 469 | device_policy_can_override = true; |
| 470 | break; |
| 471 | |
| 472 | case ConnectionType::kUnknown: |
| 473 | if (error) |
| 474 | *error = "Unknown connection type"; |
| 475 | return EvalStatus::kFailed; |
| 476 | |
| 477 | default: |
| 478 | break; // Nothing to do. |
| 479 | } |
| 480 | |
| 481 | // If update is allowed, we're done. |
| 482 | if (*result) |
| 483 | return EvalStatus::kSucceeded; |
| 484 | |
| 485 | // Check whether the device policy specifically allows this connection. |
Gilad Arnold | a8262e2 | 2014-06-02 13:54:27 -0700 | [diff] [blame] | 486 | if (device_policy_can_override) { |
| 487 | DevicePolicyProvider* const dp_provider = state->device_policy_provider(); |
| 488 | const bool* device_policy_is_loaded_p = ec->GetValue( |
| 489 | dp_provider->var_device_policy_is_loaded()); |
| 490 | if (device_policy_is_loaded_p && *device_policy_is_loaded_p) { |
| 491 | const set<ConnectionType>* allowed_conn_types_p = ec->GetValue( |
| 492 | dp_provider->var_allowed_connection_types_for_update()); |
| 493 | if (allowed_conn_types_p) { |
| 494 | if (allowed_conn_types_p->count(conn_type)) { |
| 495 | *result = true; |
| 496 | return EvalStatus::kSucceeded; |
| 497 | } |
Gilad Arnold | 28d6be6 | 2014-06-30 14:04:04 -0700 | [diff] [blame] | 498 | } else if (conn_type == ConnectionType::kCellular) { |
| 499 | // Local user settings can allow updates over cellular iff a policy was |
| 500 | // loaded but no allowed connections were specified in it. |
| 501 | const bool* update_over_cellular_allowed_p = ec->GetValue( |
| 502 | state->updater_provider()->var_cellular_enabled()); |
| 503 | if (update_over_cellular_allowed_p && *update_over_cellular_allowed_p) |
| 504 | *result = true; |
Gilad Arnold | a8262e2 | 2014-06-02 13:54:27 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
Gilad Arnold | 28d6be6 | 2014-06-30 14:04:04 -0700 | [diff] [blame] | 509 | return (*result ? EvalStatus::kSucceeded : EvalStatus::kAskMeAgainLater); |
Gilad Arnold | a8262e2 | 2014-06-02 13:54:27 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 512 | EvalStatus ChromeOSPolicy::P2PEnabled(EvaluationContext* ec, |
| 513 | State* state, |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 514 | string* error, |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 515 | bool* result) const { |
| 516 | bool enabled = false; |
| 517 | |
| 518 | // Determine whether use of P2P is allowed by policy. Even if P2P is not |
| 519 | // explicitly allowed, we allow it if the device is enterprise enrolled (that |
| 520 | // is, missing or empty owner string). |
| 521 | DevicePolicyProvider* const dp_provider = state->device_policy_provider(); |
| 522 | const bool* device_policy_is_loaded_p = ec->GetValue( |
| 523 | dp_provider->var_device_policy_is_loaded()); |
| 524 | if (device_policy_is_loaded_p && *device_policy_is_loaded_p) { |
| 525 | const bool* policy_au_p2p_enabled_p = ec->GetValue( |
| 526 | dp_provider->var_au_p2p_enabled()); |
| 527 | if (policy_au_p2p_enabled_p) { |
| 528 | enabled = *policy_au_p2p_enabled_p; |
| 529 | } else { |
| 530 | const string* policy_owner_p = ec->GetValue(dp_provider->var_owner()); |
| 531 | if (!policy_owner_p || policy_owner_p->empty()) |
| 532 | enabled = true; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | // Enable P2P, if so mandated by the updater configuration. This is additive |
| 537 | // to whether or not P2P is enabled by device policy. |
| 538 | if (!enabled) { |
| 539 | const bool* updater_p2p_enabled_p = ec->GetValue( |
| 540 | state->updater_provider()->var_p2p_enabled()); |
| 541 | enabled = updater_p2p_enabled_p && *updater_p2p_enabled_p; |
| 542 | } |
| 543 | |
| 544 | *result = enabled; |
| 545 | return EvalStatus::kSucceeded; |
| 546 | } |
| 547 | |
| 548 | EvalStatus ChromeOSPolicy::P2PEnabledChanged(EvaluationContext* ec, |
| 549 | State* state, |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 550 | string* error, |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 551 | bool* result, |
| 552 | bool prev_result) const { |
| 553 | EvalStatus status = P2PEnabled(ec, state, error, result); |
| 554 | if (status == EvalStatus::kSucceeded && *result == prev_result) |
| 555 | return EvalStatus::kAskMeAgainLater; |
| 556 | return status; |
| 557 | } |
| 558 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 559 | EvalStatus ChromeOSPolicy::UpdateBackoffAndDownloadUrl( |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 560 | EvaluationContext* ec, State* state, string* error, |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 561 | UpdateBackoffAndDownloadUrlResult* result, |
| 562 | const UpdateState& update_state) const { |
| 563 | // Sanity checks. |
| 564 | DCHECK_GE(update_state.download_errors_max, 0); |
| 565 | |
| 566 | // Set default result values. |
| 567 | result->do_increment_failures = false; |
| 568 | result->backoff_expiry = update_state.backoff_expiry; |
| 569 | result->url_idx = -1; |
| 570 | result->url_num_errors = 0; |
| 571 | |
| 572 | const bool* is_official_build_p = ec->GetValue( |
| 573 | state->system_provider()->var_is_official_build()); |
| 574 | bool is_official_build = (is_official_build_p ? *is_official_build_p : true); |
| 575 | |
| 576 | // Check whether backoff is enabled. |
| 577 | bool may_backoff = false; |
| 578 | if (update_state.is_backoff_disabled) { |
| 579 | LOG(INFO) << "Backoff disabled by Omaha."; |
| 580 | } else if (update_state.is_interactive) { |
| 581 | LOG(INFO) << "No backoff for interactive updates."; |
| 582 | } else if (update_state.is_delta_payload) { |
| 583 | LOG(INFO) << "No backoff for delta payloads."; |
| 584 | } else if (!is_official_build) { |
| 585 | LOG(INFO) << "No backoff for unofficial builds."; |
| 586 | } else { |
| 587 | may_backoff = true; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 590 | // If previous backoff still in effect, block. |
| 591 | if (may_backoff && !update_state.backoff_expiry.is_null() && |
| 592 | !ec->IsWallclockTimeGreaterThan(update_state.backoff_expiry)) { |
| 593 | LOG(INFO) << "Previous backoff has not expired, waiting."; |
| 594 | return EvalStatus::kAskMeAgainLater; |
| 595 | } |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 596 | |
| 597 | // Determine whether HTTP downloads are forbidden by policy. This only |
| 598 | // applies to official system builds; otherwise, HTTP is always enabled. |
| 599 | bool http_allowed = true; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 600 | if (is_official_build) { |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 601 | DevicePolicyProvider* const dp_provider = state->device_policy_provider(); |
| 602 | const bool* device_policy_is_loaded_p = ec->GetValue( |
| 603 | dp_provider->var_device_policy_is_loaded()); |
| 604 | if (device_policy_is_loaded_p && *device_policy_is_loaded_p) { |
| 605 | const bool* policy_http_downloads_enabled_p = ec->GetValue( |
| 606 | dp_provider->var_http_downloads_enabled()); |
| 607 | http_allowed = (!policy_http_downloads_enabled_p || |
| 608 | *policy_http_downloads_enabled_p); |
| 609 | } |
| 610 | } |
| 611 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 612 | int url_idx = update_state.last_download_url_idx; |
| 613 | if (url_idx < 0) |
| 614 | url_idx = -1; |
| 615 | bool do_advance_url = false; |
| 616 | bool is_failure_occurred = false; |
| 617 | Time err_time; |
| 618 | |
| 619 | // Scan the relevant part of the download error log, tracking which URLs are |
| 620 | // being used, and accounting the number of errors for each URL. Note that |
| 621 | // this process may not traverse all errors provided, as it may decide to bail |
| 622 | // out midway depending on the particular errors exhibited, the number of |
| 623 | // failures allowed, etc. When this ends, |url_idx| will point to the last URL |
| 624 | // used (-1 if starting fresh), |do_advance_url| will determine whether the |
| 625 | // URL needs to be advanced, and |err_time| the point in time when the last |
| 626 | // reported error occurred. Additionally, if the error log indicates that an |
| 627 | // update attempt has failed (abnormal), then |is_failure_occurred| will be |
| 628 | // set to true. |
| 629 | const int num_urls = update_state.download_urls.size(); |
| 630 | int prev_url_idx = -1; |
| 631 | int url_num_errors = update_state.last_download_url_num_errors; |
| 632 | Time prev_err_time; |
| 633 | bool is_first = true; |
| 634 | for (const auto& err_tuple : update_state.download_errors) { |
| 635 | // Do some sanity checks. |
| 636 | int used_url_idx = get<0>(err_tuple); |
| 637 | if (is_first && url_idx >= 0 && used_url_idx != url_idx) { |
| 638 | LOG(WARNING) << "First URL in error log (" << used_url_idx |
| 639 | << ") not as expected (" << url_idx << ")"; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 640 | } |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 641 | is_first = false; |
| 642 | url_idx = used_url_idx; |
| 643 | if (url_idx < 0 || url_idx >= num_urls) { |
| 644 | LOG(ERROR) << "Download error log contains an invalid URL index (" |
| 645 | << url_idx << ")"; |
| 646 | return EvalStatus::kFailed; |
| 647 | } |
| 648 | err_time = get<2>(err_tuple); |
| 649 | if (!(prev_err_time.is_null() || err_time >= prev_err_time)) { |
| 650 | // TODO(garnold) Monotonicity cannot really be assumed when dealing with |
| 651 | // wallclock-based timestamps. However, we're making a simplifying |
| 652 | // assumption so as to keep the policy implementation straightforward, for |
| 653 | // now. In general, we should convert all timestamp handling in the |
| 654 | // UpdateManager to use monotonic time (instead of wallclock), including |
| 655 | // the computation of various expiration times (backoff, scattering, etc). |
| 656 | // The client will do whatever conversions necessary when |
| 657 | // persisting/retrieving these values across reboots. See chromium:408794. |
| 658 | LOG(ERROR) << "Download error timestamps not monotonically increasing."; |
| 659 | return EvalStatus::kFailed; |
| 660 | } |
| 661 | prev_err_time = err_time; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 662 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 663 | // Ignore errors that happened before the last known failed attempt. |
| 664 | if (!update_state.failures_last_updated.is_null() && |
| 665 | err_time <= update_state.failures_last_updated) |
| 666 | continue; |
| 667 | |
| 668 | if (prev_url_idx >= 0) { |
| 669 | if (url_idx < prev_url_idx) { |
| 670 | LOG(ERROR) << "The URLs in the download error log have wrapped around (" |
| 671 | << prev_url_idx << "->" << url_idx |
| 672 | << "). This should not have happened and means that there's " |
| 673 | "a bug. To be conservative, we record a failed attempt " |
| 674 | "(invalidating the rest of the error log) and resume " |
| 675 | "download from the first usable URL."; |
| 676 | url_idx = -1; |
| 677 | is_failure_occurred = true; |
| 678 | break; |
| 679 | } |
| 680 | |
| 681 | if (url_idx > prev_url_idx) { |
| 682 | url_num_errors = 0; |
| 683 | do_advance_url = false; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | if (HandleErrorCode(get<1>(err_tuple), &url_num_errors) || |
| 688 | url_num_errors > update_state.download_errors_max) |
| 689 | do_advance_url = true; |
| 690 | |
| 691 | prev_url_idx = url_idx; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 694 | // If required, advance to the next usable URL. If the URLs wraparound, we |
| 695 | // mark an update attempt failure. Also be sure to set the download error |
| 696 | // count to zero. |
| 697 | if (url_idx < 0 || do_advance_url) { |
| 698 | url_num_errors = 0; |
| 699 | int start_url_idx = -1; |
| 700 | do { |
| 701 | if (++url_idx == num_urls) { |
| 702 | url_idx = 0; |
| 703 | // We only mark failure if an actual advancing of a URL was required. |
| 704 | if (do_advance_url) |
| 705 | is_failure_occurred = true; |
| 706 | } |
| 707 | |
| 708 | if (start_url_idx < 0) |
| 709 | start_url_idx = url_idx; |
| 710 | else if (url_idx == start_url_idx) |
| 711 | url_idx = -1; // No usable URL. |
| 712 | } while (url_idx >= 0 && |
| 713 | !IsUrlUsable(update_state.download_urls[url_idx], http_allowed)); |
| 714 | } |
| 715 | |
| 716 | // If we have a download URL but a failure was observed, compute a new backoff |
| 717 | // expiry (if allowed). The backoff period is generally 2 ^ (num_failures - 1) |
| 718 | // days, bounded by the size of int and kAttemptBackoffMaxIntervalInDays, and |
| 719 | // fuzzed by kAttemptBackoffFuzzInHours hours. Backoff expiry is computed from |
| 720 | // the latest recorded time of error. |
| 721 | Time backoff_expiry; |
| 722 | if (url_idx >= 0 && is_failure_occurred && may_backoff) { |
| 723 | CHECK(!err_time.is_null()) |
| 724 | << "We must have an error timestamp if a failure occurred!"; |
| 725 | const uint64_t* seed = ec->GetValue(state->random_provider()->var_seed()); |
| 726 | POLICY_CHECK_VALUE_AND_FAIL(seed, error); |
| 727 | PRNG prng(*seed); |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 728 | int exp = min(update_state.num_failures, |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 729 | static_cast<int>(sizeof(int)) * 8 - 2); |
Amin Hassani | 186ff6a | 2018-02-27 11:06:03 -0800 | [diff] [blame] | 730 | TimeDelta backoff_interval = TimeDelta::FromDays(min( |
| 731 | 1 << exp, |
| 732 | kNextUpdateCheckPolicyConstants.attempt_backoff_max_interval_in_days)); |
| 733 | TimeDelta backoff_fuzz = TimeDelta::FromHours( |
| 734 | kNextUpdateCheckPolicyConstants.attempt_backoff_fuzz_in_hours); |
| 735 | TimeDelta wait_period = NextUpdateCheckTimePolicyImpl::FuzzedInterval( |
| 736 | &prng, backoff_interval.InSeconds(), backoff_fuzz.InSeconds()); |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 737 | backoff_expiry = err_time + wait_period; |
| 738 | |
| 739 | // If the newly computed backoff already expired, nullify it. |
| 740 | if (ec->IsWallclockTimeGreaterThan(backoff_expiry)) |
| 741 | backoff_expiry = Time(); |
| 742 | } |
| 743 | |
| 744 | result->do_increment_failures = is_failure_occurred; |
| 745 | result->backoff_expiry = backoff_expiry; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 746 | result->url_idx = url_idx; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 747 | result->url_num_errors = url_num_errors; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 748 | return EvalStatus::kSucceeded; |
| 749 | } |
| 750 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 751 | EvalStatus ChromeOSPolicy::UpdateScattering( |
| 752 | EvaluationContext* ec, |
| 753 | State* state, |
| 754 | string* error, |
| 755 | UpdateScatteringResult* result, |
| 756 | const UpdateState& update_state) const { |
| 757 | // Preconditions. These stem from the postconditions and usage contract. |
| 758 | DCHECK(update_state.scatter_wait_period >= kZeroInterval); |
| 759 | DCHECK_GE(update_state.scatter_check_threshold, 0); |
| 760 | |
| 761 | // Set default result values. |
| 762 | result->is_scattering = false; |
| 763 | result->wait_period = kZeroInterval; |
| 764 | result->check_threshold = 0; |
| 765 | |
| 766 | DevicePolicyProvider* const dp_provider = state->device_policy_provider(); |
| 767 | |
| 768 | // Ensure that a device policy is loaded. |
| 769 | const bool* device_policy_is_loaded_p = ec->GetValue( |
| 770 | dp_provider->var_device_policy_is_loaded()); |
| 771 | if (!(device_policy_is_loaded_p && *device_policy_is_loaded_p)) |
| 772 | return EvalStatus::kSucceeded; |
| 773 | |
| 774 | // Is scattering enabled by policy? |
| 775 | const TimeDelta* scatter_factor_p = ec->GetValue( |
| 776 | dp_provider->var_scatter_factor()); |
| 777 | if (!scatter_factor_p || *scatter_factor_p == kZeroInterval) |
| 778 | return EvalStatus::kSucceeded; |
| 779 | |
| 780 | // Obtain a pseudo-random number generator. |
| 781 | const uint64_t* seed = ec->GetValue(state->random_provider()->var_seed()); |
| 782 | POLICY_CHECK_VALUE_AND_FAIL(seed, error); |
| 783 | PRNG prng(*seed); |
| 784 | |
| 785 | // Step 1: Maintain the scattering wait period. |
| 786 | // |
| 787 | // If no wait period was previously determined, or it no longer fits in the |
| 788 | // scatter factor, then generate a new one. Otherwise, keep the one we have. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 789 | TimeDelta wait_period = update_state.scatter_wait_period; |
| 790 | if (wait_period == kZeroInterval || wait_period > *scatter_factor_p) { |
| 791 | wait_period = TimeDelta::FromSeconds( |
| 792 | prng.RandMinMax(1, scatter_factor_p->InSeconds())); |
| 793 | } |
| 794 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 795 | // If we surpassed the wait period or the max scatter period associated with |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 796 | // the update, then no wait is needed. |
| 797 | Time wait_expires = (update_state.first_seen + |
| 798 | min(wait_period, update_state.scatter_wait_period_max)); |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 799 | if (ec->IsWallclockTimeGreaterThan(wait_expires)) |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 800 | wait_period = kZeroInterval; |
| 801 | |
| 802 | // Step 2: Maintain the update check threshold count. |
| 803 | // |
| 804 | // If an update check threshold is not specified then generate a new |
| 805 | // one. |
| 806 | int check_threshold = update_state.scatter_check_threshold; |
| 807 | if (check_threshold == 0) { |
| 808 | check_threshold = prng.RandMinMax( |
| 809 | update_state.scatter_check_threshold_min, |
| 810 | update_state.scatter_check_threshold_max); |
| 811 | } |
| 812 | |
| 813 | // If the update check threshold is not within allowed range then nullify it. |
| 814 | // TODO(garnold) This is compliant with current logic found in |
| 815 | // OmahaRequestAction::IsUpdateCheckCountBasedWaitingSatisfied(). We may want |
| 816 | // to change it so that it behaves similarly to the wait period case, namely |
| 817 | // if the current value exceeds the maximum, we set a new one within range. |
| 818 | if (check_threshold > update_state.scatter_check_threshold_max) |
| 819 | check_threshold = 0; |
| 820 | |
| 821 | // If the update check threshold is non-zero and satisfied, then nullify it. |
| 822 | if (check_threshold > 0 && update_state.num_checks >= check_threshold) |
| 823 | check_threshold = 0; |
| 824 | |
| 825 | bool is_scattering = (wait_period != kZeroInterval || check_threshold); |
| 826 | EvalStatus ret = EvalStatus::kSucceeded; |
| 827 | if (is_scattering && wait_period == update_state.scatter_wait_period && |
| 828 | check_threshold == update_state.scatter_check_threshold) |
| 829 | ret = EvalStatus::kAskMeAgainLater; |
| 830 | result->is_scattering = is_scattering; |
| 831 | result->wait_period = wait_period; |
| 832 | result->check_threshold = check_threshold; |
| 833 | return ret; |
| 834 | } |
| 835 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 836 | } // namespace chromeos_update_manager |