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