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 super::extensible_bitmap::{MAP_NODE_BITS, MAX_BITMAP_ITEMS};
7use super::metadata::{
8    CONFIG_HANDLE_UNKNOWN_MASK, CONFIG_MLS_FLAG, POLICYDB_SIGNATURE, POLICYDB_STRING_MAX_LENGTH,
9    POLICYDB_VERSION_MAX, POLICYDB_VERSION_MIN, SELINUX_MAGIC,
10};
11use super::symbols::{ClassDefault, ClassDefaultRange};
12
13use bstr::BString;
14use thiserror::Error;
15
16/// Structured errors that may be encountered parsing a binary policy.
17#[derive(Clone, Debug, Error, PartialEq)]
18pub enum ParseError {
19    #[error("expected MLS-enabled flag ({CONFIG_MLS_FLAG:#032b}), but found {found_config:#032b}")]
20    ConfigMissingMlsFlag { found_config: u32 },
21    #[error(
22        "expected handle-unknown config at most 1 bit set (mask {CONFIG_HANDLE_UNKNOWN_MASK:#032b}), but found {masked_bits:#032b}"
23    )]
24    InvalidHandleUnknownConfigurationBits { masked_bits: u32 },
25    #[error("expected end of policy, but found {num_bytes} additional bytes")]
26    TrailingBytes { num_bytes: usize },
27    #[error("expected data item of type {type_name} ({type_size} bytes), but found {num_bytes}")]
28    MissingData { type_name: &'static str, type_size: usize, num_bytes: usize },
29    #[error("required parsing routine not implemented")]
30    NotImplemented,
31}
32
33/// Structured errors that may be encountered validating a binary policy.
34#[derive(Debug, Error, PartialEq)]
35pub enum ValidateError {
36    #[error("expected selinux magic value {SELINUX_MAGIC:#x}, but found {found_magic:#x}")]
37    InvalidMagic { found_magic: u32 },
38    #[error(
39        "expected signature length in range [0, {POLICYDB_STRING_MAX_LENGTH}], but found {found_length}"
40    )]
41    InvalidSignatureLength { found_length: u32 },
42    #[error("expected signature {POLICYDB_SIGNATURE:?}, but found {:?}", bstr::BStr::new(found_signature.as_slice()))]
43    InvalidSignature { found_signature: Vec<u8> },
44    #[error(
45        "expected policy version in range [{POLICYDB_VERSION_MIN}, {POLICYDB_VERSION_MAX}], but found {found_policy_version}"
46    )]
47    InvalidPolicyVersion { found_policy_version: u32 },
48    #[error(
49        "expected extensible bitmap item size to be exactly {MAP_NODE_BITS}, but found {found_size}"
50    )]
51    InvalidExtensibleBitmapItemSize { found_size: u32 },
52    #[error(
53        "expected extensible bitmap item high bit to be multiple of {found_size}, but found {found_high_bit}"
54    )]
55    MisalignedExtensibleBitmapHighBit { found_size: u32, found_high_bit: u32 },
56    #[error(
57        "expected extensible bitmap item high bit to be at most items_count + items_size = {found_count} + {found_size}, but found {found_high_bit}"
58    )]
59    InvalidExtensibleBitmapHighBit { found_size: u32, found_high_bit: u32, found_count: u32 },
60    #[error(
61        "expected extensible bitmap item count to be in range [0, {MAX_BITMAP_ITEMS}], but found {found_count}"
62    )]
63    InvalidExtensibleBitmapCount { found_count: u32 },
64    #[error("found extensible bitmap item count = 0, but high count != 0")]
65    ExtensibleBitmapNonZeroHighBitAndZeroCount,
66    #[error(
67        "expected extensible bitmap item start bit to be multiple of item size {found_size}, but found {found_start_bit}"
68    )]
69    MisalignedExtensibleBitmapItemStartBit { found_start_bit: u32, found_size: u32 },
70    #[error(
71        "expected extensible bitmap items to be in sorted order, but found item starting at {found_start_bit} after item that ends at {min_start}"
72    )]
73    OutOfOrderExtensibleBitmapItems { found_start_bit: u32, min_start: u32 },
74    #[error(
75        "expected extensible bitmap items to refer to bits in range [0, {found_high_bit}), but found item that ends at {found_items_end}"
76    )]
77    ExtensibleBitmapItemOverflow { found_items_end: u32, found_high_bit: u32 },
78    #[error(
79        "expected class default binary value to be one of {}, {}, or {}, but found {value}",
80        ClassDefault::DEFAULT_UNSPECIFIED,
81        ClassDefault::DEFAULT_SOURCE,
82        ClassDefault::DEFAULT_TARGET
83    )]
84    InvalidClassDefault { value: u32 },
85    #[error(
86        "expected class default binary value to be one of {:?}, but found {value}",
87        [ClassDefaultRange::DEFAULT_UNSPECIFIED,
88        ClassDefaultRange::DEFAULT_SOURCE_LOW,
89        ClassDefaultRange::DEFAULT_SOURCE_HIGH,
90        ClassDefaultRange::DEFAULT_SOURCE_LOW_HIGH,
91        ClassDefaultRange::DEFAULT_TARGET_LOW,
92        ClassDefaultRange::DEFAULT_TARGET_HIGH,
93        ClassDefaultRange::DEFAULT_TARGET_LOW_HIGH,
94        ClassDefaultRange::DEFAULT_UNKNOWN_USED_VALUE]
95    )]
96    InvalidClassDefaultRange { value: u32 },
97    #[error("missing initial SID {initial_sid:?}")]
98    MissingInitialSid { initial_sid: crate::InitialSid },
99    #[error(
100        "invalid SELinux fs_use type; expected one of {:?}, but found {value}",
101        [FsUseType::Xattr as u32,
102        FsUseType::Trans as u32,
103        FsUseType::Task as u32]
104    )]
105    InvalidFsUseType { value: u32 },
106    #[error("non-optional Id field is zero")]
107    NonOptionalIdIsZero,
108    #[error("required validation routine not implemented")]
109    NotImplemented,
110    #[error("undefined {kind} Id value {id}")]
111    UnknownId { kind: &'static str, id: String },
112    #[error("invalid MLS range: {low}-{high}")]
113    InvalidMlsRange { low: BString, high: BString },
114    #[error("invalid extended permissions type: {type_}")]
115    InvalidExtendedPermissionsType { type_: u8 },
116}