system_update_committer/metadata/
policy.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use super::configuration::Configuration;
use super::errors::{
    BootManagerError, BootManagerResultExt, PolicyError, VerifyError, VerifyErrors, VerifySource,
};
use crate::config::{Config as ComponentConfig, Mode};
use fidl_fuchsia_paver as paver;
use tracing::{info, warn};
use zx::Status;

/// After gathering state from the BootManager, the PolicyEngine can answer whether we
/// should verify and commit.
#[derive(Debug)]
pub struct PolicyEngine(State);

#[derive(Debug)]
enum State {
    // If no verification or committing is necessary, i.e. if any of:
    //   * ABR is not supported
    //   * the current config is Recovery
    //   * the current config status is Healthy
    NoOp,
    Active {
        current_config: Configuration,
        // None if the value is erroneously missing from QueryConfigurationStatusAndBootAttempts.
        boot_attempts: Option<u8>,
    },
}

impl PolicyEngine {
    /// Gathers system state from the BootManager.
    pub async fn build(boot_manager: &paver::BootManagerProxy) -> Result<Self, PolicyError> {
        let current_config = match boot_manager
            .query_current_configuration()
            .await
            .into_boot_manager_result("query_current_configuration")
        {
            Err(BootManagerError::Fidl {
                error: fidl::Error::ClientChannelClosed { status: Status::NOT_SUPPORTED, .. },
                ..
            }) => {
                info!("ABR not supported: skipping health verification and boot metadata updates");
                return Ok(Self(State::NoOp));
            }
            Err(e) => return Err(PolicyError::Build(e)),
            Ok(paver::Configuration::Recovery) => {
                info!("System in recovery: skipping health verification and boot metadata updates");
                return Ok(Self(State::NoOp));
            }
            Ok(paver::Configuration::A) => Configuration::A,
            Ok(paver::Configuration::B) => Configuration::B,
        };

        let status_and_boot_attempts = boot_manager
            .query_configuration_status_and_boot_attempts((&current_config).into())
            .await
            .into_boot_manager_result("query_configuration_status")
            .map_err(PolicyError::Build)?;
        match status_and_boot_attempts
            .status
            .ok_or(PolicyError::Build(BootManagerError::StatusNotSet))?
        {
            paver::ConfigurationStatus::Healthy => {
                return Ok(Self(State::NoOp));
            }
            paver::ConfigurationStatus::Pending => {}
            paver::ConfigurationStatus::Unbootable => {
                return Err(PolicyError::CurrentConfigurationUnbootable((&current_config).into()));
            }
        };

        let boot_attempts = status_and_boot_attempts.boot_attempts;
        if boot_attempts.is_none() {
            warn!("Current config status is pending but boot attempts was not set");
        }

        Ok(Self(State::Active { current_config, boot_attempts }))
    }

    /// Determines if we should verify and commit.
    /// * If we should (e.g. if the system is pending commit), return
    ///   `Some((slot_to_act_on, boot_attempts))`.
    /// * If we shouldn't (e.g. if the system is already committed), return `None`.
    pub fn should_verify_and_commit(&self) -> Option<(&Configuration, Option<u8>)> {
        match &self.0 {
            State::Active { current_config, boot_attempts } => {
                Some((current_config, *boot_attempts))
            }
            State::NoOp => None,
        }
    }

    /// Filters out any failed verifications if the config says to ignore them.
    pub fn apply_config(
        res: Result<(), VerifyErrors>,
        config: &ComponentConfig,
    ) -> Result<(), VerifyErrors> {
        match res {
            Ok(()) => Ok(()),
            Err(VerifyErrors::VerifyErrors(v)) => {
                // For any existing verification errors,
                let errors: Vec<_> = v
                    .into_iter()
                    .filter(|VerifyError::VerifyError(source, _, _)| {
                        // filter out the ones which config says to ignore.
                        match source {
                            VerifySource::Blobfs => config.blobfs() != &Mode::Ignore,
                            VerifySource::Netstack => config.netstack() != &Mode::Ignore,
                        }
                    })
                    .collect();

                // If there are any remaining verification errors, pass them on.
                if errors.is_empty() {
                    Ok(())
                } else {
                    Err(VerifyErrors::VerifyErrors(errors))
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::metadata::errors::VerifyFailureReason;
    use assert_matches::assert_matches;
    use mock_paver::{hooks as mphooks, MockPaverServiceBuilder, PaverEvent};
    use std::sync::Arc;
    use {fidl_fuchsia_update_verify as verify, fuchsia_async as fasync};

    /// Test we should NOT verify and commit when when the device is in recovery.
    #[fasync::run_singlethreaded(test)]
    async fn test_skip_when_device_in_recovery() {
        let paver = Arc::new(
            MockPaverServiceBuilder::new()
                .current_config(paver::Configuration::Recovery)
                .insert_hook(mphooks::config_status_and_boot_attempts(|_| {
                    Ok((paver::ConfigurationStatus::Healthy, None))
                }))
                .build(),
        );
        let engine = PolicyEngine::build(&paver.spawn_boot_manager_service()).await.unwrap();

        assert_eq!(engine.should_verify_and_commit(), None);

        assert_eq!(paver.take_events(), vec![PaverEvent::QueryCurrentConfiguration]);
    }

    /// Test we should NOT verify and commit when the device does not support ABR.
    #[fasync::run_singlethreaded(test)]
    async fn test_skip_when_device_does_not_support_abr() {
        let paver = Arc::new(
            MockPaverServiceBuilder::new()
                .boot_manager_close_with_epitaph(Status::NOT_SUPPORTED)
                .build(),
        );
        let engine = PolicyEngine::build(&paver.spawn_boot_manager_service()).await.unwrap();

        assert_eq!(engine.should_verify_and_commit(), None);

        assert_eq!(paver.take_events(), vec![]);
    }

    /// Helper fn to verify we should NOT verify and commit when current is healthy.
    async fn test_skip_when_current_is_healthy(current_config: &Configuration) {
        let paver = Arc::new(
            MockPaverServiceBuilder::new()
                .current_config(current_config.into())
                .insert_hook(mphooks::config_status_and_boot_attempts(|_| {
                    Ok((paver::ConfigurationStatus::Healthy, None))
                }))
                .build(),
        );
        let engine = PolicyEngine::build(&paver.spawn_boot_manager_service()).await.unwrap();

        assert_eq!(engine.should_verify_and_commit(), None);

        assert_eq!(
            paver.take_events(),
            vec![
                PaverEvent::QueryCurrentConfiguration,
                PaverEvent::QueryConfigurationStatusAndBootAttempts {
                    configuration: current_config.into()
                },
            ]
        );
    }

    #[fasync::run_singlethreaded(test)]
    async fn test_skip_when_current_is_healthy_a() {
        test_skip_when_current_is_healthy(&Configuration::A).await
    }

    #[fasync::run_singlethreaded(test)]
    async fn test_skip_when_current_is_healthy_b() {
        test_skip_when_current_is_healthy(&Configuration::B).await
    }

    /// Helper fn to verify we should verify and commit when current is pending.
    async fn test_verify_and_commit_when_current_is_pending(current_config: &Configuration) {
        let paver = Arc::new(
            MockPaverServiceBuilder::new()
                .current_config(current_config.into())
                .insert_hook(mphooks::config_status_and_boot_attempts(|_| {
                    Ok((paver::ConfigurationStatus::Pending, Some(1)))
                }))
                .build(),
        );
        let engine = PolicyEngine::build(&paver.spawn_boot_manager_service()).await.unwrap();

        assert_eq!(engine.should_verify_and_commit(), Some((current_config, Some(1))));

        assert_eq!(
            paver.take_events(),
            vec![
                PaverEvent::QueryCurrentConfiguration,
                PaverEvent::QueryConfigurationStatusAndBootAttempts {
                    configuration: current_config.into()
                },
            ]
        );
    }

    #[fasync::run_singlethreaded(test)]
    async fn test_verify_and_commit_when_current_is_pending_a() {
        test_verify_and_commit_when_current_is_pending(&Configuration::A).await
    }

    #[fasync::run_singlethreaded(test)]
    async fn test_verify_and_commit_when_current_is_pending_b() {
        test_verify_and_commit_when_current_is_pending(&Configuration::B).await
    }

    /// Helper fn to verify an error is returned if current is unbootable.
    async fn test_returns_error_when_current_unbootable(current_config: &Configuration) {
        let paver = Arc::new(
            MockPaverServiceBuilder::new()
                .current_config(current_config.into())
                .insert_hook(mphooks::config_status_and_boot_attempts(|_| {
                    Ok((paver::ConfigurationStatus::Unbootable, None))
                }))
                .build(),
        );

        assert_matches!(
            PolicyEngine::build(&paver.spawn_boot_manager_service()).await,
            Err(PolicyError::CurrentConfigurationUnbootable(cc)) if cc == current_config.into()
        );

        assert_eq!(
            paver.take_events(),
            vec![
                PaverEvent::QueryCurrentConfiguration,
                PaverEvent::QueryConfigurationStatusAndBootAttempts {
                    configuration: current_config.into()
                },
            ]
        );
    }

    #[fasync::run_singlethreaded(test)]
    async fn test_returns_error_when_current_unbootable_a() {
        test_returns_error_when_current_unbootable(&Configuration::A).await
    }

    #[fasync::run_singlethreaded(test)]
    async fn test_returns_error_when_current_unbootable_b() {
        test_returns_error_when_current_unbootable(&Configuration::B).await
    }

    /// Test the build fn fails on a standard paver error.
    #[fasync::run_singlethreaded(test)]
    async fn test_build_fails_when_paver_fails() {
        let paver = Arc::new(
            MockPaverServiceBuilder::new()
                .insert_hook(mphooks::return_error(|e| match e {
                    PaverEvent::QueryCurrentConfiguration { .. } => Status::OUT_OF_RANGE,
                    _ => Status::OK,
                }))
                .build(),
        );

        assert_matches!(
            PolicyEngine::build(&paver.spawn_boot_manager_service()).await,
            Err(PolicyError::Build(BootManagerError::Status {
                method_name: "query_current_configuration",
                status: Status::OUT_OF_RANGE
            }))
        );

        assert_eq!(paver.take_events(), vec![PaverEvent::QueryCurrentConfiguration]);
    }

    fn test_blobfs_verify_errors(config: ComponentConfig, expect_err: bool) {
        let duration = std::time::Duration::from_secs(1);
        let timeout_err = Err(VerifyErrors::VerifyErrors(vec![VerifyError::VerifyError(
            VerifySource::Blobfs,
            VerifyFailureReason::Timeout,
            duration,
        )]));
        let verify_err = Err(VerifyErrors::VerifyErrors(vec![VerifyError::VerifyError(
            VerifySource::Blobfs,
            VerifyFailureReason::Verify(verify::VerifyError::Internal),
            duration,
        )]));
        let fidl_err = Err(VerifyErrors::VerifyErrors(vec![VerifyError::VerifyError(
            VerifySource::Blobfs,
            VerifyFailureReason::Fidl(fidl::Error::OutOfRange),
            duration,
        )]));

        assert_eq!(PolicyEngine::apply_config(timeout_err, &config).is_err(), expect_err);
        assert_eq!(PolicyEngine::apply_config(verify_err, &config).is_err(), expect_err);
        assert_eq!(PolicyEngine::apply_config(fidl_err, &config).is_err(), expect_err);
    }

    /// Blobfs errors should be ignored if the config says so.
    #[test]
    fn test_blobfs_errors_ignored() {
        test_blobfs_verify_errors(ComponentConfig::builder().blobfs(Mode::Ignore).build(), false);
    }

    #[test]
    fn test_errors_all_ignored() {
        let duration = std::time::Duration::from_secs(1);
        let ve1 =
            VerifyError::VerifyError(VerifySource::Blobfs, VerifyFailureReason::Timeout, duration);
        let ve2 = VerifyError::VerifyError(
            VerifySource::Blobfs,
            VerifyFailureReason::Verify(verify::VerifyError::Internal),
            duration,
        );

        let config = ComponentConfig::builder().blobfs(Mode::Ignore).build();

        // TODO(https://fxbug.dev/42156562): When there are multiple VerifySource
        // types, test heterogeneous VerifyErrors lists.
        assert_matches!(
            PolicyEngine::apply_config(Err(VerifyErrors::VerifyErrors(vec![ve1, ve2])), &config),
            Ok(())
        );
    }

    #[test]
    fn test_errors_none_ignored() {
        let duration = std::time::Duration::from_secs(1);
        let ve1 =
            VerifyError::VerifyError(VerifySource::Blobfs, VerifyFailureReason::Timeout, duration);
        let ve2 = VerifyError::VerifyError(
            VerifySource::Blobfs,
            VerifyFailureReason::Verify(verify::VerifyError::Internal),
            duration,
        );

        let config = ComponentConfig::builder().blobfs(Mode::RebootOnFailure).build();

        let filtered_errors = assert_matches!(
            PolicyEngine::apply_config(Err(VerifyErrors::VerifyErrors(vec![ve1, ve2])), &config),
            Err(VerifyErrors::VerifyErrors(s)) => s);

        assert_matches!(
            &filtered_errors[..],
            [
                VerifyError::VerifyError(VerifySource::Blobfs, VerifyFailureReason::Timeout, _),
                VerifyError::VerifyError(
                    VerifySource::Blobfs,
                    VerifyFailureReason::Verify(verify::VerifyError::Internal),
                    _
                )
            ]
        );
    }

    /// Blobfs errors should NOT be ignored if the config says to reboot on failure.
    #[test]
    fn test_blobfs_errors_reboot_on_failure() {
        test_blobfs_verify_errors(
            ComponentConfig::builder().blobfs(Mode::RebootOnFailure).build(),
            true,
        );
    }
}