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 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    #[error(
32        "policy is of {observed} bytes, but this implementation only supports policies of up to {limit} bytes"
33    )]
34    UnsupportedlyLarge { observed: usize, limit: usize },
35}
36
37/// Structured errors that may be encountered validating a binary policy.
38#[derive(Debug, Error, PartialEq)]
39pub enum ValidateError {
40    #[error("expected selinux magic value {SELINUX_MAGIC:#x}, but found {found_magic:#x}")]
41    InvalidMagic { found_magic: u32 },
42    #[error(
43        "expected signature length in range [0, {POLICYDB_STRING_MAX_LENGTH}], but found {found_length}"
44    )]
45    InvalidSignatureLength { found_length: u32 },
46    #[error("expected signature {POLICYDB_SIGNATURE:?}, but found {:?}", bstr::BStr::new(found_signature.as_slice()))]
47    InvalidSignature { found_signature: Vec<u8> },
48    #[error(
49        "expected policy version in range [{POLICYDB_VERSION_MIN}, {POLICYDB_VERSION_MAX}], but found {found_policy_version}"
50    )]
51    InvalidPolicyVersion { found_policy_version: u32 },
52    #[error("expected extensible bitmap items to have at least one bit set")]
53    InvalidExtensibleBitmapItem,
54    #[error(
55        "expected extensible bitmap item size to be exactly {MAP_NODE_BITS}, but found {found_size}"
56    )]
57    InvalidExtensibleBitmapItemSize { found_size: u32 },
58    #[error(
59        "expected extensible bitmap item high bit to be multiple of {found_size}, but found {found_high_bit}"
60    )]
61    MisalignedExtensibleBitmapHighBit { found_size: u32, found_high_bit: u32 },
62    #[error(
63        "expected extensible bitmap item high bit to be at most items_count + items_size = {found_count} + {found_size}, but found {found_high_bit}"
64    )]
65    InvalidExtensibleBitmapHighBit { found_size: u32, found_high_bit: u32, found_count: u32 },
66    #[error(
67        "expected extensible bitmap item count to be in range [0, {MAX_BITMAP_ITEMS}], but found {found_count}"
68    )]
69    InvalidExtensibleBitmapCount { found_count: u32 },
70    #[error("found extensible bitmap item count = 0, but high count != 0")]
71    ExtensibleBitmapNonZeroHighBitAndZeroCount,
72    #[error(
73        "expected extensible bitmap item start bit to be multiple of item size {found_size}, but found {found_start_bit}"
74    )]
75    MisalignedExtensibleBitmapItemStartBit { found_start_bit: u32, found_size: u32 },
76    #[error(
77        "expected extensible bitmap items to be in sorted order, but found item starting at {found_start_bit} after item that ends at {min_start}"
78    )]
79    OutOfOrderExtensibleBitmapItems { found_start_bit: u32, min_start: u32 },
80    #[error(
81        "expected extensible bitmap items to refer to bits in range [0, {found_high_bit}), but found item that ends at {found_items_end}"
82    )]
83    ExtensibleBitmapItemOverflow { found_items_end: u32, found_high_bit: u32 },
84    #[error(
85        "expected class default binary value to be one of {}, {}, or {}, but found {value}",
86        ClassDefault::DEFAULT_UNSPECIFIED,
87        ClassDefault::DEFAULT_SOURCE,
88        ClassDefault::DEFAULT_TARGET
89    )]
90    InvalidClassDefault { value: u32 },
91    #[error(
92        "expected class default binary value to be one of {:?}, but found {value}",
93        [ClassDefaultRange::DEFAULT_UNSPECIFIED,
94        ClassDefaultRange::DEFAULT_SOURCE_LOW,
95        ClassDefaultRange::DEFAULT_SOURCE_HIGH,
96        ClassDefaultRange::DEFAULT_SOURCE_LOW_HIGH,
97        ClassDefaultRange::DEFAULT_TARGET_LOW,
98        ClassDefaultRange::DEFAULT_TARGET_HIGH,
99        ClassDefaultRange::DEFAULT_TARGET_LOW_HIGH,
100        ClassDefaultRange::DEFAULT_UNKNOWN_USED_VALUE]
101    )]
102    InvalidClassDefaultRange { value: u32 },
103    #[error("paths not ordered lexicographicaly")]
104    InvalidGenFsPathOrdering,
105    #[error("missing initial SID {initial_sid:?}")]
106    MissingInitialSid { initial_sid: crate::InitialSid },
107    #[error(
108        "invalid SELinux fs_use type; expected one of {:?}, but found {value}",
109        [FsUseType::Xattr as u32,
110        FsUseType::Trans as u32,
111        FsUseType::Task as u32]
112    )]
113    InvalidFsUseType { value: u32 },
114    #[error("non-optional Id field is zero")]
115    NonOptionalIdIsZero,
116    #[error("required validation routine not implemented")]
117    NotImplemented,
118    #[error("undefined {kind} Id value {id}")]
119    UnknownId { kind: &'static str, id: String },
120    #[error("invalid MLS range: {low}-{high}")]
121    InvalidMlsRange { low: BString, high: BString },
122    #[error("invalid extended permissions type: {type_}")]
123    InvalidExtendedPermissionsType { type_: u8 },
124}