Expand description
Parser and AST for the AppArmor policy language: profiles, hats, file globs, variables, aliases, and the full rule grammar.
This crate is a mechanism layer (see the project’s DESIGN.md): it
solves the correctness problem of turning AppArmor policy text into a
typed, lossless AST and back — a problem every tool that reads or writes
profiles (policy linters, aa-logprof-style repair tools, LXD-style
containers, security tooling) would otherwise reimplement, usually
incorrectly.
§What you get
- Complete grammar:
profile/hat/ sub-profile headers, file rules with the full AARE glob language (*,**,?,[...],{...},@{}variables), and the typed rule set:file,capability,network,mount,pivot_root,link,change_profile,set rlimit,dbus,unix,mqueue,userns,io_uring,signal,ptraceandall. - Lossless AST: variables are kept as
@{...}references (never expanded — expansion is the caller’s job), and unknown rules degrade toast::RawRulenodes carrying their original text, so no information is ever dropped. - Round-trippable: a
Policyrenders back to canonical policy text via itsDisplayimpl, withparse(render(ast)) == ast. - Precise errors: structural failures carry 1-based line/column plus the offending source line, for incremental-repair tooling.
§Example
use apparmor_policy::parse_str;
let src = r#"
#include <tunables/global>
profile /usr/bin/foo flags=(complain) {
#include <abstractions/base>
/etc/foo.conf r,
owner @{HOME}/** rw,
/bin/bash ix,
capability setuid,
network inet stream,
deny /proc/*/mem rw,
}
"#;
let profiles = parse_str(src).unwrap();
assert_eq!(profiles[0].name, "/usr/bin/foo");
assert_eq!(profiles[0].rules.len(), 6);
// Round trip: AST -> text -> AST is the identity.
let text = profiles[0].to_string();
let again = parse_str(&text).unwrap();
assert_eq!(again, profiles);§Module map
glob— AARE file-glob parsing (the subtlest part of the language).ast— the AST types.error—ParseErrorandError.
The lexer and grammar are internal modules.
§Scope and stopping point
This crate parses and serializes policy text. It deliberately does not:
evaluate rules against paths (the kernel does that), read
/sys/kernel/security/apparmor state, merge or generate profiles, load
profiles into the kernel, or bind an async runtime. Those are policy /
runtime-layer concerns; constraining them here would force every caller
into one choice.
Re-exports§
pub use error::Error;pub use error::ParseError;
Modules§
- ast
- The AppArmor policy AST (abstract syntax tree).
- error
- Error types for parsing and validating AppArmor policy text.
- glob
- Parsing and rendering of AppArmor file-path globs (AARE).
Functions§
- parse_
file - Read an AppArmor policy file from disk and parse it.
- parse_
policy - Parse AppArmor policy text into a lossless
Policy. - parse_
str - Parse AppArmor policy text and return just the
Profiles it contains. - render
- Render a policy back to canonical AppArmor text.