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