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