1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! Parser and AST for the [AppArmor policy language](https://apparmor.net/):
//! 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`](std::fmt::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
//!
//! - [`glob`] — AARE file-glob parsing (the subtlest part of the language).
//! - [`ast`] — the AST types.
//! - [`error`] — [`ParseError`] and [`Error`].
//!
//! 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.
pub use ;
use ;
use Path;
/// Parse AppArmor policy text into a lossless [`Policy`].
///
/// The parse is *fault tolerant*: unknown preamble items and rules are kept
/// as `Raw` nodes (see [`ast::RawItem`] / [`ast::RawRule`]) rather than
/// failing, so a partially supported policy never loses text. Structural
/// errors (unbalanced braces, unterminated strings, a missing profile body)
/// return a [`ParseError`] with a precise position.
///
/// # Examples
///
/// ```
/// use apparmor_policy::parse_policy;
///
/// let policy = parse_policy("profile foo {\n capability net_raw,\n}\n").unwrap();
/// assert_eq!(policy.items.len(), 1);
/// assert_eq!(policy.to_string().contains("net_raw"), true);
/// ```
/// Parse AppArmor policy text and return just the [`Profile`]s it contains.
///
/// This is a convenience over [`parse_policy`] for the common case where the
/// preamble (variables, aliases, includes, abi) is of no interest. Variables
/// and includes that belong to a profile (including the immediately
/// preceding preamble) are still carried by that profile. Preamble-only
/// items with no following profile are dropped; use [`parse_policy`] for a
/// fully lossless parse.
///
/// # Examples
///
/// ```
/// use apparmor_policy::parse_str;
///
/// let profiles = parse_str("profile /usr/bin/foo {\n /etc/passwd r,\n}\n").unwrap();
/// assert_eq!(profiles.len(), 1);
/// assert_eq!(profiles[0].name, "/usr/bin/foo");
/// ```
/// Read an AppArmor policy file from disk and parse it.
///
/// Returns [`Error::Io`] if the file cannot be read, or
/// [`Error::Parse`] if it is structurally invalid.
///
/// # Examples
///
/// ```no_run
/// use apparmor_policy::parse_file;
///
/// let policy = parse_file("/etc/apparmor.d/usr.bin.foo").unwrap();
/// assert!(!policy.items.is_empty());
/// ```
/// Render a policy back to canonical AppArmor text.
///
/// Equivalent to the [`Display`](std::fmt::Display) impl on [`Policy`];
/// provided as a free function for discoverability.
///
/// # Examples
///
/// ```
/// use apparmor_policy::{parse_policy, render};
///
/// let policy = parse_policy("profile foo {\n capability setuid,\n}\n").unwrap();
/// let text = render(&policy);
/// assert!(text.contains("capability setuid"));
/// ```