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