apparmor-policy
Typed, lossless parser and serializer for the AppArmor policy language.
Overview
apparmor-policy is a typed, lossless parser and serializer for AppArmor policy text — a mechanism layer, so tools that read or write profiles don't each reimplement a broken half-parser.
Highlights:
- Complete grammar:
profile/hat/ sub-profile headers, file rules with the full AARE glob language (*,**,?,[...],{...},@{}variables), and typed rules forfile,capability,network,mount,pivot_root,link,change_profile,set rlimit,dbus,unix,mqueue,userns,io_uring,signal,ptraceandall. - Lossless and fault tolerant: variables stay
@{...}(never expanded), unknown rules degrade toRawRulenodes with their original text — nothing is dropped, and partially supported policies still parse. - Round-trippable:
parse(render(x)) == x, so a normalized policy can be written back out. - Precise errors: structural failures carry 1-based line/column plus the offending source line, for incremental-repair tooling.
- No policy decisions: this crate never evaluates rules against paths, reads kernel state, merges or loads profiles, or binds an async runtime — those are caller concerns.
Usage
Add to your Cargo.toml:
[]
= "0.1"
The crate is pure Rust with no dependencies and works on any platform (parsing is text-only).
Quick start
use parse_str;
let src = r#"
profile /usr/bin/foo flags=(complain) {
/etc/foo.conf r,
owner @{HOME}/** rw,
/bin/bash ix,
capability setuid,
network inet stream,
deny /proc/*/mem rw,
}
"#;
let profiles = parse_str.unwrap;
assert_eq!;
// AST -> text -> AST round trip.
let text = profiles.to_string;
assert_eq!;
Documentation
- The crate-level docs cover the API surface and the round-trip guarantee.
- Each public type and method carries a runnable doctest showing real usage.