#![feature(format_args_nl)] use io::Cursor; use std::fmt::Write; use std::io; use std::io::{BufRead, BufReader}; use std::pin::Pin; pub use base; use base::libc::{O_CLOEXEC, O_RDONLY}; use base::{error, BufReadExt, FsPath, LoggedResult, Utf8CStr}; use crate::ffi::sepolicy; mod rules; mod statement; #[cxx::bridge] mod ffi { struct Xperm { low: u16, high: u16, reset: bool, } unsafe extern "C++" { #[namespace = "rust"] #[cxx_name = "Utf8CStr"] type Utf8CStrRef<'a> = base::ffi::Utf8CStrRef<'a>; include!("include/sepolicy.hpp"); type sepolicy; fn allow(self: Pin<&mut sepolicy>, s: Vec<&str>, t: Vec<&str>, c: Vec<&str>, p: Vec<&str>); fn deny(self: Pin<&mut sepolicy>, s: Vec<&str>, t: Vec<&str>, c: Vec<&str>, p: Vec<&str>); fn auditallow( self: Pin<&mut sepolicy>, s: Vec<&str>, t: Vec<&str>, c: Vec<&str>, p: Vec<&str>, ); fn dontaudit( self: Pin<&mut sepolicy>, s: Vec<&str>, t: Vec<&str>, c: Vec<&str>, p: Vec<&str>, ); fn allowxperm( self: Pin<&mut sepolicy>, s: Vec<&str>, t: Vec<&str>, c: Vec<&str>, p: Vec, ); fn auditallowxperm( self: Pin<&mut sepolicy>, s: Vec<&str>, t: Vec<&str>, c: Vec<&str>, p: Vec, ); fn dontauditxperm( self: Pin<&mut sepolicy>, s: Vec<&str>, t: Vec<&str>, c: Vec<&str>, p: Vec, ); fn permissive(self: Pin<&mut sepolicy>, t: Vec<&str>); fn enforce(self: Pin<&mut sepolicy>, t: Vec<&str>); fn typeattribute(self: Pin<&mut sepolicy>, t: Vec<&str>, a: Vec<&str>); #[cxx_name = "type"] fn type_(self: Pin<&mut sepolicy>, t: &str, a: Vec<&str>); fn attribute(self: Pin<&mut sepolicy>, t: &str); fn type_transition( self: Pin<&mut sepolicy>, s: &str, t: &str, c: &str, d: &str, o: Vec<&str>, ); fn type_change(self: Pin<&mut sepolicy>, s: &str, t: &str, c: &str, d: &str); fn type_member(self: Pin<&mut sepolicy>, s: &str, t: &str, c: &str, d: &str); fn genfscon(self: Pin<&mut sepolicy>, s: &str, t: &str, c: &str); fn strip_dontaudit(self: Pin<&mut sepolicy>); } #[namespace = "rust"] extern "Rust" { fn load_rules(sepol: Pin<&mut sepolicy>, rules: &[u8]); fn load_rule_file(sepol: Pin<&mut sepolicy>, filename: Utf8CStrRef); fn parse_statement(sepol: Pin<&mut sepolicy>, statement: Utf8CStrRef); fn magisk_rules(sepol: Pin<&mut sepolicy>); fn xperm_to_string(perm: &Xperm) -> String; } } trait SepolicyExt { fn load_rules(self: Pin<&mut Self>, rules: &[u8]); fn load_rule_file(self: Pin<&mut Self>, filename: &Utf8CStr); fn load_rules_from_reader(self: Pin<&mut Self>, reader: &mut T); fn parse_statement(self: Pin<&mut Self>, statement: &str); } impl SepolicyExt for sepolicy { fn load_rules(self: Pin<&mut sepolicy>, rules: &[u8]) { let mut cursor = Cursor::new(rules); self.load_rules_from_reader(&mut cursor); } fn load_rule_file(self: Pin<&mut sepolicy>, filename: &Utf8CStr) { fn inner(sepol: Pin<&mut sepolicy>, filename: &Utf8CStr) -> LoggedResult<()> { let file = FsPath::from(filename).open(O_RDONLY | O_CLOEXEC)?; let mut reader = BufReader::new(file); sepol.load_rules_from_reader(&mut reader); Ok(()) } inner(self, filename).ok(); } fn load_rules_from_reader(mut self: Pin<&mut sepolicy>, reader: &mut T) { reader.foreach_lines(|line| { let line = line.trim(); if line.is_empty() { return true; } self.as_mut().parse_statement(line); true }); } fn parse_statement(self: Pin<&mut Self>, statement: &str) { if statement::parse_statement(self, statement).is_err() { error!("sepolicy rule syntax error: {statement}"); } } } fn load_rule_file(sepol: Pin<&mut sepolicy>, filename: &Utf8CStr) { sepol.load_rule_file(filename); } fn load_rules(sepol: Pin<&mut sepolicy>, rules: &[u8]) { sepol.load_rules(rules); } fn parse_statement(sepol: Pin<&mut sepolicy>, statement: &Utf8CStr) { sepol.parse_statement(statement.as_str()); } trait SepolicyMagisk { fn magisk_rules(self: Pin<&mut Self>); } fn magisk_rules(sepol: Pin<&mut sepolicy>) { sepol.magisk_rules(); } fn xperm_to_string(perm: &ffi::Xperm) -> String { let mut s = String::new(); if perm.reset { s.push('~'); } s.write_fmt(format_args!("{{ {:#06x}-{:#06x} }}", perm.low, perm.high)) .ok(); s }