1use 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#[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#[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(
53 "expected extensible bitmap item size to be exactly {MAP_NODE_BITS}, but found {found_size}"
54 )]
55 InvalidExtensibleBitmapItemSize { found_size: u32 },
56 #[error(
57 "expected extensible bitmap item high bit to be multiple of {found_size}, but found {found_high_bit}"
58 )]
59 MisalignedExtensibleBitmapHighBit { found_size: u32, found_high_bit: u32 },
60 #[error(
61 "expected extensible bitmap item high bit to be at most items_count + items_size = {found_count} + {found_size}, but found {found_high_bit}"
62 )]
63 InvalidExtensibleBitmapHighBit { found_size: u32, found_high_bit: u32, found_count: u32 },
64 #[error(
65 "expected extensible bitmap item count to be in range [0, {MAX_BITMAP_ITEMS}], but found {found_count}"
66 )]
67 InvalidExtensibleBitmapCount { found_count: u32 },
68 #[error("found extensible bitmap item count = 0, but high count != 0")]
69 ExtensibleBitmapNonZeroHighBitAndZeroCount,
70 #[error(
71 "expected extensible bitmap item start bit to be multiple of item size {found_size}, but found {found_start_bit}"
72 )]
73 MisalignedExtensibleBitmapItemStartBit { found_start_bit: u32, found_size: u32 },
74 #[error(
75 "expected extensible bitmap items to be in sorted order, but found item starting at {found_start_bit} after item that ends at {min_start}"
76 )]
77 OutOfOrderExtensibleBitmapItems { found_start_bit: u32, min_start: u32 },
78 #[error(
79 "expected extensible bitmap items to refer to bits in range [0, {found_high_bit}), but found item that ends at {found_items_end}"
80 )]
81 ExtensibleBitmapItemOverflow { found_items_end: u32, found_high_bit: u32 },
82 #[error(
83 "expected class default binary value to be one of {}, {}, or {}, but found {value}",
84 ClassDefault::DEFAULT_UNSPECIFIED,
85 ClassDefault::DEFAULT_SOURCE,
86 ClassDefault::DEFAULT_TARGET
87 )]
88 InvalidClassDefault { value: u32 },
89 #[error(
90 "expected class default binary value to be one of {:?}, but found {value}",
91 [ClassDefaultRange::DEFAULT_UNSPECIFIED,
92 ClassDefaultRange::DEFAULT_SOURCE_LOW,
93 ClassDefaultRange::DEFAULT_SOURCE_HIGH,
94 ClassDefaultRange::DEFAULT_SOURCE_LOW_HIGH,
95 ClassDefaultRange::DEFAULT_TARGET_LOW,
96 ClassDefaultRange::DEFAULT_TARGET_HIGH,
97 ClassDefaultRange::DEFAULT_TARGET_LOW_HIGH,
98 ClassDefaultRange::DEFAULT_UNKNOWN_USED_VALUE]
99 )]
100 InvalidClassDefaultRange { value: u32 },
101 #[error("missing initial SID {initial_sid:?}")]
102 MissingInitialSid { initial_sid: crate::InitialSid },
103 #[error(
104 "invalid SELinux fs_use type; expected one of {:?}, but found {value}",
105 [FsUseType::Xattr as u32,
106 FsUseType::Trans as u32,
107 FsUseType::Task as u32]
108 )]
109 InvalidFsUseType { value: u32 },
110 #[error("non-optional Id field is zero")]
111 NonOptionalIdIsZero,
112 #[error("required validation routine not implemented")]
113 NotImplemented,
114 #[error("undefined {kind} Id value {id}")]
115 UnknownId { kind: &'static str, id: String },
116 #[error("invalid MLS range: {low}-{high}")]
117 InvalidMlsRange { low: BString, high: BString },
118 #[error("invalid extended permissions type: {type_}")]
119 InvalidExtendedPermissionsType { type_: u8 },
120}