blob: 646c3f5c4e2625aa8ac0d6dc953152c45764b392 [file] [log] [blame]
Alex Deymoc705cc82014-02-19 11:15:00 -08001// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo63784a52014-05-28 10:46:14 -07005#include "update_engine/update_manager/chromeos_policy.h"
Alex Deymo0d11c602014-04-23 20:12:20 -07006
Gilad Arnolde1218812014-05-07 12:21:36 -07007#include <algorithm>
Gilad Arnold0adbc942014-05-12 10:35:43 -07008#include <set>
Alex Deymoc705cc82014-02-19 11:15:00 -08009#include <string>
10
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070011#include <base/logging.h>
12#include <base/time/time.h>
13
Alex Deymo63784a52014-05-28 10:46:14 -070014#include "update_engine/update_manager/device_policy_provider.h"
15#include "update_engine/update_manager/policy_utils.h"
16#include "update_engine/update_manager/shill_provider.h"
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070017
Alex Deymo0d11c602014-04-23 20:12:20 -070018using base::Time;
19using base::TimeDelta;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070020using std::min;
Gilad Arnold0adbc942014-05-12 10:35:43 -070021using std::set;
Alex Deymoc705cc82014-02-19 11:15:00 -080022using std::string;
23
Alex Deymo63784a52014-05-28 10:46:14 -070024namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080025
Alex Deymo0d11c602014-04-23 20:12:20 -070026EvalStatus ChromeOSPolicy::UpdateCheckAllowed(
27 EvaluationContext* ec, State* state, string* error,
28 UpdateCheckParams* result) const {
29 Time next_update_check;
30 if (NextUpdateCheckTime(ec, state, error, &next_update_check) !=
31 EvalStatus::kSucceeded) {
32 return EvalStatus::kFailed;
33 }
34
35 if (!ec->IsTimeGreaterThan(next_update_check))
36 return EvalStatus::kAskMeAgainLater;
37
38 // It is time to check for an update.
39 result->updates_enabled = true;
Alex Deymoe636c3c2014-03-11 19:02:08 -070040 return EvalStatus::kSucceeded;
Alex Deymoc705cc82014-02-19 11:15:00 -080041}
42
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070043EvalStatus ChromeOSPolicy::UpdateCanStart(
44 EvaluationContext* ec,
45 State* state,
46 string* error,
47 UpdateCanStartResult* result,
48 const bool interactive,
49 const UpdateState& update_state) const {
50 // Set the default return values.
51 result->update_can_start = true;
52 result->http_allowed = true;
53 result->p2p_allowed = false;
54 result->target_channel.clear();
55 result->cannot_start_reason = UpdateCannotStartReason::kUndefined;
56 result->scatter_wait_period = kZeroInterval;
57 result->scatter_check_threshold = 0;
58
59 // Make sure that we're not due for an update check.
60 UpdateCheckParams check_result;
61 EvalStatus check_status = UpdateCheckAllowed(ec, state, error, &check_result);
62 if (check_status == EvalStatus::kFailed)
63 return EvalStatus::kFailed;
64 if (check_status == EvalStatus::kSucceeded &&
65 check_result.updates_enabled == true) {
66 result->update_can_start = false;
67 result->cannot_start_reason = UpdateCannotStartReason::kCheckDue;
68 return EvalStatus::kSucceeded;
69 }
70
71 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
Gilad Arnold76a11f62014-05-20 09:02:12 -070072 SystemProvider* const system_provider = state->system_provider();
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070073
74 const bool* device_policy_is_loaded_p = ec->GetValue(
75 dp_provider->var_device_policy_is_loaded());
76 if (device_policy_is_loaded_p && *device_policy_is_loaded_p) {
77 // Ensure that update is enabled.
78 const bool* update_disabled_p = ec->GetValue(
79 dp_provider->var_update_disabled());
80 if (update_disabled_p && *update_disabled_p) {
81 result->update_can_start = false;
82 result->cannot_start_reason = UpdateCannotStartReason::kDisabledByPolicy;
83 return EvalStatus::kAskMeAgainLater;
84 }
85
Gilad Arnold76a11f62014-05-20 09:02:12 -070086 // Check whether scattering applies to this update attempt. We should not be
87 // scattering if this is an interactive update check, or if OOBE is enabled
88 // but not completed.
89 //
90 // Note: current code further suppresses scattering if a "deadline"
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070091 // attribute is found in the Omaha response. However, it appears that the
Gilad Arnold76a11f62014-05-20 09:02:12 -070092 // presence of this attribute is merely indicative of an OOBE update, during
93 // which we suppress scattering anyway.
94 bool scattering_applies = false;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070095 if (!interactive) {
Gilad Arnold76a11f62014-05-20 09:02:12 -070096 const bool* is_oobe_enabled_p = ec->GetValue(
97 state->config_provider()->var_is_oobe_enabled());
98 if (is_oobe_enabled_p && !(*is_oobe_enabled_p)) {
99 scattering_applies = true;
100 } else {
101 const bool* is_oobe_complete_p = ec->GetValue(
102 system_provider->var_is_oobe_complete());
103 scattering_applies = (is_oobe_complete_p && *is_oobe_complete_p);
104 }
105 }
106
107 // Compute scattering values.
108 if (scattering_applies) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700109 UpdateScatteringResult scatter_result;
110 EvalStatus scattering_status = UpdateScattering(
111 ec, state, error, &scatter_result, update_state);
112 if (scattering_status != EvalStatus::kSucceeded ||
113 scatter_result.is_scattering) {
114 if (scattering_status != EvalStatus::kFailed) {
115 result->update_can_start = false;
116 result->cannot_start_reason = UpdateCannotStartReason::kScattering;
117 result->scatter_wait_period = scatter_result.wait_period;
118 result->scatter_check_threshold = scatter_result.check_threshold;
119 }
120 return scattering_status;
121 }
122 }
123
124 // Determine whether HTTP downloads are forbidden by policy. This only
125 // applies to official system builds; otherwise, HTTP is always enabled.
126 const bool* is_official_build_p = ec->GetValue(
Gilad Arnold76a11f62014-05-20 09:02:12 -0700127 system_provider->var_is_official_build());
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700128 if (is_official_build_p && *is_official_build_p) {
129 const bool* policy_http_downloads_enabled_p = ec->GetValue(
130 dp_provider->var_http_downloads_enabled());
131 result->http_allowed =
132 !policy_http_downloads_enabled_p || *policy_http_downloads_enabled_p;
133 }
134
135 // Determine whether use of P2P is allowed by policy.
136 const bool* policy_au_p2p_enabled_p = ec->GetValue(
137 dp_provider->var_au_p2p_enabled());
138 result->p2p_allowed = policy_au_p2p_enabled_p && *policy_au_p2p_enabled_p;
139
140 // Determine whether a target channel is dictated by policy.
141 const bool* release_channel_delegated_p = ec->GetValue(
142 dp_provider->var_release_channel_delegated());
143 if (release_channel_delegated_p && !(*release_channel_delegated_p)) {
144 const string* release_channel_p = ec->GetValue(
145 dp_provider->var_release_channel());
146 if (release_channel_p)
147 result->target_channel = *release_channel_p;
148 }
149 }
150
151 // Enable P2P, if so mandated by the updater configuration.
152 if (!result->p2p_allowed) {
153 const bool* updater_p2p_enabled_p = ec->GetValue(
154 state->updater_provider()->var_p2p_enabled());
155 result->p2p_allowed = updater_p2p_enabled_p && *updater_p2p_enabled_p;
156 }
157
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700158 return EvalStatus::kSucceeded;
159}
160
Gilad Arnolda8262e22014-06-02 13:54:27 -0700161// TODO(garnold) Logic in this method is based on
162// ConnectionManager::IsUpdateAllowedOver(); be sure to deprecate the latter.
163//
164// TODO(garnold) The current logic generally treats the list of allowed
165// connections coming from the device policy as a whitelist, meaning that it
166// can only be used for enabling connections, but not disable them. Further,
167// certain connection types (like Bluetooth) cannot be enabled even by policy.
168// In effect, the only thing that device policy can change is to enable
169// updates over a cellular network (disabled by default). We may want to
170// revisit this semantics, allowing greater flexibility in defining specific
171// permissions over all types of networks.
172EvalStatus ChromeOSPolicy::UpdateCurrentConnectionAllowed(
173 EvaluationContext* ec,
174 State* state,
175 string* error,
176 bool* result) const {
177 // Get the current connection type.
178 ShillProvider* const shill_provider = state->shill_provider();
179 const ConnectionType* conn_type_p = ec->GetValue(
180 shill_provider->var_conn_type());
181 POLICY_CHECK_VALUE_AND_FAIL(conn_type_p, error);
182 ConnectionType conn_type = *conn_type_p;
183
184 // If we're tethering, treat it as a cellular connection.
185 if (conn_type != ConnectionType::kCellular) {
186 const ConnectionTethering* conn_tethering_p = ec->GetValue(
187 shill_provider->var_conn_tethering());
188 POLICY_CHECK_VALUE_AND_FAIL(conn_tethering_p, error);
189 if (*conn_tethering_p == ConnectionTethering::kConfirmed)
190 conn_type = ConnectionType::kCellular;
191 }
192
193 // By default, we allow updates for all connection types, with exceptions as
194 // noted below. This also determines whether a device policy can override the
195 // default.
196 *result = true;
197 bool device_policy_can_override = false;
198 switch (conn_type) {
199 case ConnectionType::kBluetooth:
200 *result = false;
201 break;
202
203 case ConnectionType::kCellular:
204 *result = false;
205 device_policy_can_override = true;
206 break;
207
208 case ConnectionType::kUnknown:
209 if (error)
210 *error = "Unknown connection type";
211 return EvalStatus::kFailed;
212
213 default:
214 break; // Nothing to do.
215 }
216
217 // If update is allowed, we're done.
218 if (*result)
219 return EvalStatus::kSucceeded;
220
221 // Check whether the device policy specifically allows this connection.
222 bool user_settings_can_override = false;
223 if (device_policy_can_override) {
224 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
225 const bool* device_policy_is_loaded_p = ec->GetValue(
226 dp_provider->var_device_policy_is_loaded());
227 if (device_policy_is_loaded_p && *device_policy_is_loaded_p) {
228 const set<ConnectionType>* allowed_conn_types_p = ec->GetValue(
229 dp_provider->var_allowed_connection_types_for_update());
230 if (allowed_conn_types_p) {
231 if (allowed_conn_types_p->count(conn_type)) {
232 *result = true;
233 return EvalStatus::kSucceeded;
234 }
235 } else {
236 user_settings_can_override = true;
237 }
238 }
239 }
240
241 // Local user settings can allow updates iff a policy was loaded but no
242 // allowed connections were specified in it. In all other cases, we either
243 // stick with the default or use the values determined by the policy.
244 if (user_settings_can_override) {
245 const bool* update_over_cellular_allowed_p = ec->GetValue(
246 state->updater_provider()->var_cellular_enabled());
247 if (update_over_cellular_allowed_p && *update_over_cellular_allowed_p)
248 *result = true;
249 }
250
251 return EvalStatus::kSucceeded;
252}
253
Alex Deymo0d11c602014-04-23 20:12:20 -0700254EvalStatus ChromeOSPolicy::NextUpdateCheckTime(EvaluationContext* ec,
255 State* state, string* error,
256 Time* next_update_check) const {
257 // Don't check for updates too often. We limit the update checks to once every
258 // some interval. The interval is kTimeoutInitialInterval the first time and
259 // kTimeoutPeriodicInterval for the subsequent update checks. If the update
260 // check fails, we increase the interval between the update checks
261 // exponentially until kTimeoutMaxBackoffInterval. Finally, to avoid having
262 // many chromebooks running update checks at the exact same time, we add some
263 // fuzz to the interval.
264 const Time* updater_started_time =
265 ec->GetValue(state->updater_provider()->var_updater_started_time());
266 POLICY_CHECK_VALUE_AND_FAIL(updater_started_time, error);
267
268 const base::Time* last_checked_time =
269 ec->GetValue(state->updater_provider()->var_last_checked_time());
270
271 const uint64_t* seed = ec->GetValue(state->random_provider()->var_seed());
272 POLICY_CHECK_VALUE_AND_FAIL(seed, error);
273
274 PRNG prng(*seed);
275
276 if (!last_checked_time || *last_checked_time < *updater_started_time) {
277 // First attempt.
278 *next_update_check = *updater_started_time + FuzzedInterval(
279 &prng, kTimeoutInitialInterval, kTimeoutRegularFuzz);
280 return EvalStatus::kSucceeded;
281 }
282 // Check for previous failed attempts to implement the exponential backoff.
283 const unsigned int* consecutive_failed_update_checks = ec->GetValue(
284 state->updater_provider()->var_consecutive_failed_update_checks());
285 POLICY_CHECK_VALUE_AND_FAIL(consecutive_failed_update_checks, error);
286
287 int interval = kTimeoutInitialInterval;
288 for (unsigned int i = 0; i < *consecutive_failed_update_checks; ++i) {
289 interval *= 2;
290 if (interval > kTimeoutMaxBackoffInterval) {
291 interval = kTimeoutMaxBackoffInterval;
292 break;
293 }
294 }
295
296 *next_update_check = *last_checked_time + FuzzedInterval(
297 &prng, interval, kTimeoutRegularFuzz);
298 return EvalStatus::kSucceeded;
299}
300
301TimeDelta ChromeOSPolicy::FuzzedInterval(PRNG* prng, int interval, int fuzz) {
Gilad Arnolde1218812014-05-07 12:21:36 -0700302 DCHECK_GE(interval, 0);
303 DCHECK_GE(fuzz, 0);
Alex Deymo0d11c602014-04-23 20:12:20 -0700304 int half_fuzz = fuzz / 2;
Alex Deymo0d11c602014-04-23 20:12:20 -0700305 // This guarantees the output interval is non negative.
Gilad Arnolde1218812014-05-07 12:21:36 -0700306 int interval_min = std::max(interval - half_fuzz, 0);
307 int interval_max = interval + half_fuzz;
308 return TimeDelta::FromSeconds(prng->RandMinMax(interval_min, interval_max));
Alex Deymo0d11c602014-04-23 20:12:20 -0700309}
310
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700311EvalStatus ChromeOSPolicy::UpdateScattering(
312 EvaluationContext* ec,
313 State* state,
314 string* error,
315 UpdateScatteringResult* result,
316 const UpdateState& update_state) const {
317 // Preconditions. These stem from the postconditions and usage contract.
318 DCHECK(update_state.scatter_wait_period >= kZeroInterval);
319 DCHECK_GE(update_state.scatter_check_threshold, 0);
320
321 // Set default result values.
322 result->is_scattering = false;
323 result->wait_period = kZeroInterval;
324 result->check_threshold = 0;
325
326 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
327
328 // Ensure that a device policy is loaded.
329 const bool* device_policy_is_loaded_p = ec->GetValue(
330 dp_provider->var_device_policy_is_loaded());
331 if (!(device_policy_is_loaded_p && *device_policy_is_loaded_p))
332 return EvalStatus::kSucceeded;
333
334 // Is scattering enabled by policy?
335 const TimeDelta* scatter_factor_p = ec->GetValue(
336 dp_provider->var_scatter_factor());
337 if (!scatter_factor_p || *scatter_factor_p == kZeroInterval)
338 return EvalStatus::kSucceeded;
339
340 // Obtain a pseudo-random number generator.
341 const uint64_t* seed = ec->GetValue(state->random_provider()->var_seed());
342 POLICY_CHECK_VALUE_AND_FAIL(seed, error);
343 PRNG prng(*seed);
344
345 // Step 1: Maintain the scattering wait period.
346 //
347 // If no wait period was previously determined, or it no longer fits in the
348 // scatter factor, then generate a new one. Otherwise, keep the one we have.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700349 TimeDelta wait_period = update_state.scatter_wait_period;
350 if (wait_period == kZeroInterval || wait_period > *scatter_factor_p) {
351 wait_period = TimeDelta::FromSeconds(
352 prng.RandMinMax(1, scatter_factor_p->InSeconds()));
353 }
354
355 // If we surpass the wait period or the max scatter period associated with
356 // the update, then no wait is needed.
357 Time wait_expires = (update_state.first_seen +
358 min(wait_period, update_state.scatter_wait_period_max));
359 if (ec->IsTimeGreaterThan(wait_expires))
360 wait_period = kZeroInterval;
361
362 // Step 2: Maintain the update check threshold count.
363 //
364 // If an update check threshold is not specified then generate a new
365 // one.
366 int check_threshold = update_state.scatter_check_threshold;
367 if (check_threshold == 0) {
368 check_threshold = prng.RandMinMax(
369 update_state.scatter_check_threshold_min,
370 update_state.scatter_check_threshold_max);
371 }
372
373 // If the update check threshold is not within allowed range then nullify it.
374 // TODO(garnold) This is compliant with current logic found in
375 // OmahaRequestAction::IsUpdateCheckCountBasedWaitingSatisfied(). We may want
376 // to change it so that it behaves similarly to the wait period case, namely
377 // if the current value exceeds the maximum, we set a new one within range.
378 if (check_threshold > update_state.scatter_check_threshold_max)
379 check_threshold = 0;
380
381 // If the update check threshold is non-zero and satisfied, then nullify it.
382 if (check_threshold > 0 && update_state.num_checks >= check_threshold)
383 check_threshold = 0;
384
385 bool is_scattering = (wait_period != kZeroInterval || check_threshold);
386 EvalStatus ret = EvalStatus::kSucceeded;
387 if (is_scattering && wait_period == update_state.scatter_wait_period &&
388 check_threshold == update_state.scatter_check_threshold)
389 ret = EvalStatus::kAskMeAgainLater;
390 result->is_scattering = is_scattering;
391 result->wait_period = wait_period;
392 result->check_threshold = check_threshold;
393 return ret;
394}
395
Alex Deymo63784a52014-05-28 10:46:14 -0700396} // namespace chromeos_update_manager