blob: 6cf1fd1001f56f96a0cc7720a7c93af6144e10e3 [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
Alex Deymo0d11c602014-04-23 20:12:20 -0700161EvalStatus ChromeOSPolicy::NextUpdateCheckTime(EvaluationContext* ec,
162 State* state, string* error,
163 Time* next_update_check) const {
164 // Don't check for updates too often. We limit the update checks to once every
165 // some interval. The interval is kTimeoutInitialInterval the first time and
166 // kTimeoutPeriodicInterval for the subsequent update checks. If the update
167 // check fails, we increase the interval between the update checks
168 // exponentially until kTimeoutMaxBackoffInterval. Finally, to avoid having
169 // many chromebooks running update checks at the exact same time, we add some
170 // fuzz to the interval.
171 const Time* updater_started_time =
172 ec->GetValue(state->updater_provider()->var_updater_started_time());
173 POLICY_CHECK_VALUE_AND_FAIL(updater_started_time, error);
174
175 const base::Time* last_checked_time =
176 ec->GetValue(state->updater_provider()->var_last_checked_time());
177
178 const uint64_t* seed = ec->GetValue(state->random_provider()->var_seed());
179 POLICY_CHECK_VALUE_AND_FAIL(seed, error);
180
181 PRNG prng(*seed);
182
183 if (!last_checked_time || *last_checked_time < *updater_started_time) {
184 // First attempt.
185 *next_update_check = *updater_started_time + FuzzedInterval(
186 &prng, kTimeoutInitialInterval, kTimeoutRegularFuzz);
187 return EvalStatus::kSucceeded;
188 }
189 // Check for previous failed attempts to implement the exponential backoff.
190 const unsigned int* consecutive_failed_update_checks = ec->GetValue(
191 state->updater_provider()->var_consecutive_failed_update_checks());
192 POLICY_CHECK_VALUE_AND_FAIL(consecutive_failed_update_checks, error);
193
194 int interval = kTimeoutInitialInterval;
195 for (unsigned int i = 0; i < *consecutive_failed_update_checks; ++i) {
196 interval *= 2;
197 if (interval > kTimeoutMaxBackoffInterval) {
198 interval = kTimeoutMaxBackoffInterval;
199 break;
200 }
201 }
202
203 *next_update_check = *last_checked_time + FuzzedInterval(
204 &prng, interval, kTimeoutRegularFuzz);
205 return EvalStatus::kSucceeded;
206}
207
208TimeDelta ChromeOSPolicy::FuzzedInterval(PRNG* prng, int interval, int fuzz) {
Gilad Arnolde1218812014-05-07 12:21:36 -0700209 DCHECK_GE(interval, 0);
210 DCHECK_GE(fuzz, 0);
Alex Deymo0d11c602014-04-23 20:12:20 -0700211 int half_fuzz = fuzz / 2;
Alex Deymo0d11c602014-04-23 20:12:20 -0700212 // This guarantees the output interval is non negative.
Gilad Arnolde1218812014-05-07 12:21:36 -0700213 int interval_min = std::max(interval - half_fuzz, 0);
214 int interval_max = interval + half_fuzz;
215 return TimeDelta::FromSeconds(prng->RandMinMax(interval_min, interval_max));
Alex Deymo0d11c602014-04-23 20:12:20 -0700216}
217
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700218EvalStatus ChromeOSPolicy::UpdateScattering(
219 EvaluationContext* ec,
220 State* state,
221 string* error,
222 UpdateScatteringResult* result,
223 const UpdateState& update_state) const {
224 // Preconditions. These stem from the postconditions and usage contract.
225 DCHECK(update_state.scatter_wait_period >= kZeroInterval);
226 DCHECK_GE(update_state.scatter_check_threshold, 0);
227
228 // Set default result values.
229 result->is_scattering = false;
230 result->wait_period = kZeroInterval;
231 result->check_threshold = 0;
232
233 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
234
235 // Ensure that a device policy is loaded.
236 const bool* device_policy_is_loaded_p = ec->GetValue(
237 dp_provider->var_device_policy_is_loaded());
238 if (!(device_policy_is_loaded_p && *device_policy_is_loaded_p))
239 return EvalStatus::kSucceeded;
240
241 // Is scattering enabled by policy?
242 const TimeDelta* scatter_factor_p = ec->GetValue(
243 dp_provider->var_scatter_factor());
244 if (!scatter_factor_p || *scatter_factor_p == kZeroInterval)
245 return EvalStatus::kSucceeded;
246
247 // Obtain a pseudo-random number generator.
248 const uint64_t* seed = ec->GetValue(state->random_provider()->var_seed());
249 POLICY_CHECK_VALUE_AND_FAIL(seed, error);
250 PRNG prng(*seed);
251
252 // Step 1: Maintain the scattering wait period.
253 //
254 // If no wait period was previously determined, or it no longer fits in the
255 // scatter factor, then generate a new one. Otherwise, keep the one we have.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700256 TimeDelta wait_period = update_state.scatter_wait_period;
257 if (wait_period == kZeroInterval || wait_period > *scatter_factor_p) {
258 wait_period = TimeDelta::FromSeconds(
259 prng.RandMinMax(1, scatter_factor_p->InSeconds()));
260 }
261
262 // If we surpass the wait period or the max scatter period associated with
263 // the update, then no wait is needed.
264 Time wait_expires = (update_state.first_seen +
265 min(wait_period, update_state.scatter_wait_period_max));
266 if (ec->IsTimeGreaterThan(wait_expires))
267 wait_period = kZeroInterval;
268
269 // Step 2: Maintain the update check threshold count.
270 //
271 // If an update check threshold is not specified then generate a new
272 // one.
273 int check_threshold = update_state.scatter_check_threshold;
274 if (check_threshold == 0) {
275 check_threshold = prng.RandMinMax(
276 update_state.scatter_check_threshold_min,
277 update_state.scatter_check_threshold_max);
278 }
279
280 // If the update check threshold is not within allowed range then nullify it.
281 // TODO(garnold) This is compliant with current logic found in
282 // OmahaRequestAction::IsUpdateCheckCountBasedWaitingSatisfied(). We may want
283 // to change it so that it behaves similarly to the wait period case, namely
284 // if the current value exceeds the maximum, we set a new one within range.
285 if (check_threshold > update_state.scatter_check_threshold_max)
286 check_threshold = 0;
287
288 // If the update check threshold is non-zero and satisfied, then nullify it.
289 if (check_threshold > 0 && update_state.num_checks >= check_threshold)
290 check_threshold = 0;
291
292 bool is_scattering = (wait_period != kZeroInterval || check_threshold);
293 EvalStatus ret = EvalStatus::kSucceeded;
294 if (is_scattering && wait_period == update_state.scatter_wait_period &&
295 check_threshold == update_state.scatter_check_threshold)
296 ret = EvalStatus::kAskMeAgainLater;
297 result->is_scattering = is_scattering;
298 result->wait_period = wait_period;
299 result->check_threshold = check_threshold;
300 return ret;
301}
302
Gilad Arnold0adbc942014-05-12 10:35:43 -0700303// TODO(garnold) Logic in this method is based on
304// ConnectionManager::IsUpdateAllowedOver(); be sure to deprecate the latter.
305//
306// TODO(garnold) The current logic generally treats the list of allowed
307// connections coming from the device policy as a whitelist, meaning that it
308// can only be used for enabling connections, but not disable them. Further,
309// certain connection types (like Bluetooth) cannot be enabled even by policy.
310// In effect, the only thing that device policy can change is to enable
311// updates over a cellular network (disabled by default). We may want to
312// revisit this semantics, allowing greater flexibility in defining specific
313// permissions over all types of networks.
314EvalStatus ChromeOSPolicy::UpdateCurrentConnectionAllowed(
315 EvaluationContext* ec,
316 State* state,
317 string* error,
318 bool* result) const {
319 // Get the current connection type.
320 ShillProvider* const shill_provider = state->shill_provider();
321 const ConnectionType* conn_type_p = ec->GetValue(
322 shill_provider->var_conn_type());
323 POLICY_CHECK_VALUE_AND_FAIL(conn_type_p, error);
324 ConnectionType conn_type = *conn_type_p;
325
326 // If we're tethering, treat it as a cellular connection.
327 if (conn_type != ConnectionType::kCellular) {
328 const ConnectionTethering* conn_tethering_p = ec->GetValue(
329 shill_provider->var_conn_tethering());
330 POLICY_CHECK_VALUE_AND_FAIL(conn_tethering_p, error);
331 if (*conn_tethering_p == ConnectionTethering::kConfirmed)
332 conn_type = ConnectionType::kCellular;
333 }
334
335 // By default, we allow updates for all connection types, with exceptions as
336 // noted below. This also determines whether a device policy can override the
337 // default.
338 *result = true;
339 bool device_policy_can_override = false;
340 switch (conn_type) {
341 case ConnectionType::kBluetooth:
342 *result = false;
343 break;
344
345 case ConnectionType::kCellular:
346 *result = false;
347 device_policy_can_override = true;
348 break;
349
350 case ConnectionType::kUnknown:
351 if (error)
352 *error = "Unknown connection type";
353 return EvalStatus::kFailed;
354
355 default:
356 break; // Nothing to do.
357 }
358
359 // If update is allowed, we're done.
360 if (*result)
361 return EvalStatus::kSucceeded;
362
363 // Check whether the device policy specifically allows this connection.
364 bool user_settings_can_override = false;
365 if (device_policy_can_override) {
366 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
367 const bool* device_policy_is_loaded_p = ec->GetValue(
368 dp_provider->var_device_policy_is_loaded());
369 if (device_policy_is_loaded_p && *device_policy_is_loaded_p) {
370 const set<ConnectionType>* allowed_conn_types_p = ec->GetValue(
371 dp_provider->var_allowed_connection_types_for_update());
372 if (allowed_conn_types_p) {
373 if (allowed_conn_types_p->count(conn_type)) {
374 *result = true;
375 return EvalStatus::kSucceeded;
376 }
377 } else {
378 user_settings_can_override = true;
379 }
380 }
381 }
382
383 // Local user settings can allow updates iff a policy was loaded but no
384 // allowed connections were specified in it. In all other cases, we either
385 // stick with the default or use the values determined by the policy.
386 if (user_settings_can_override) {
387 const bool* update_over_cellular_allowed_p = ec->GetValue(
388 state->updater_provider()->var_cellular_enabled());
389 if (update_over_cellular_allowed_p && *update_over_cellular_allowed_p)
390 *result = true;
391 }
392
393 return EvalStatus::kSucceeded;
394}
395
Alex Deymo63784a52014-05-28 10:46:14 -0700396} // namespace chromeos_update_manager