Skip to main content

selinux/policy/
error.rs

1// Copyright 2023 The Fuchsia 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
5use super::arrays::FsUseType;
6use crate::new_policy::{ClassDefault, ClassDefaultRange};
7
8use bstr::BString;
9use thiserror::Error;
10
11/// Structured errors that may be encountered parsing a binary policy.
12#[derive(Clone, Debug, Error, PartialEq)]
13pub enum ParseError {
14    #[error("expected end of policy, but found {num_bytes} additional bytes")]
15    TrailingBytes { num_bytes: usize },
16    #[error("expected data item of type {type_name} ({type_size} bytes), but found {num_bytes}")]
17    MissingData { type_name: &'static str, type_size: usize, num_bytes: usize },
18    #[error(
19        "policy is of {observed} bytes, but this implementation only supports policies of up to {limit} bytes"
20    )]
21    UnsupportedlyLarge { observed: usize, limit: usize },
22    #[error("invalid ID value: {value}")]
23    InvalidId { value: u32 },
24}
25
26/// Structured errors that may be encountered validating a binary policy.
27#[derive(Debug, Error, PartialEq)]
28pub enum ValidateError {
29    #[error(
30        "expected class default binary value to be one of {}, {}, or {}, but found {value}",
31        ClassDefault::Unspecified as u32,
32        ClassDefault::Source as u32,
33        ClassDefault::Target as u32
34    )]
35    InvalidClassDefault { value: u32 },
36    #[error(
37        "expected class default binary value to be one of {:?}, but found {value}",
38        [ClassDefaultRange::Unspecified as u32,
39        ClassDefaultRange::SourceLow as u32,
40        ClassDefaultRange::SourceHigh as u32,
41        ClassDefaultRange::SourceLowHigh as u32,
42        ClassDefaultRange::TargetLow as u32,
43        ClassDefaultRange::TargetHigh as u32,
44        ClassDefaultRange::TargetLowHigh as u32,
45        ClassDefaultRange::UnknownUsedValue as u32]
46    )]
47    InvalidClassDefaultRange { value: u32 },
48    #[error("paths not ordered lexicographicaly")]
49    InvalidGenFsPathOrdering,
50    #[error("missing initial SID {initial_sid:?}")]
51    MissingInitialSid { initial_sid: crate::InitialSid },
52    #[error(
53        "invalid SELinux fs_use type; expected one of {:?}, but found {value}",
54        [FsUseType::Xattr as u32,
55        FsUseType::Trans as u32,
56        FsUseType::Task as u32]
57    )]
58    InvalidFsUseType { value: u32 },
59    #[error("non-optional Id field is zero")]
60    NonOptionalIdIsZero,
61    #[error("undefined {kind} Id value {id}")]
62    UnknownId { kind: &'static str, id: String },
63    #[error("invalid MLS range: {low}-{high}")]
64    InvalidMlsRange { low: BString, high: BString },
65}