Skip to main content

Crate apparmor_policy

Crate apparmor_policy 

Source
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, ptrace and all.
  • Lossless AST: variables are kept as @{...} references (never expanded — expansion is the caller’s job), and unknown rules degrade to ast::RawRule nodes carrying their original text, so no information is ever dropped.
  • Round-trippable: a Policy renders back to canonical policy text via its Display impl, with parse(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

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.