use crate::ast::*;
pub(crate) fn render_policy(p: &Policy) -> String {
let mut out = String::new();
for item in &p.items {
render_policy_item(item, &mut out, 0);
out.push('\n');
}
out
}
fn render_policy_item(item: &PolicyItem, out: &mut String, indent: usize) {
match item {
PolicyItem::Variable(v) => render_variable(v, out, indent),
PolicyItem::Alias(a) => render_alias(a, out, indent),
PolicyItem::Include(i) => render_include(i, out, indent),
PolicyItem::Abi(a) => render_abi(a, out, indent),
PolicyItem::Profile(p) => render_profile(p, out, indent),
PolicyItem::Raw(r) => {
push_indent(out, indent);
out.push_str(&r.text);
out.push('\n');
}
}
}
fn render_variable(v: &VariableAssignment, out: &mut String, indent: usize) {
push_indent(out, indent);
out.push_str("@{");
out.push_str(&v.name);
out.push('}');
if v.append {
out.push_str(" +=");
} else {
out.push_str(" =");
}
for value in &v.values {
out.push(' ');
out.push_str("e_if_needed(value));
}
out.push_str(",\n");
}
fn render_alias(a: &AliasRule, out: &mut String, indent: usize) {
push_indent(out, indent);
out.push_str("alias ");
out.push_str("e_if_needed(&a.oldname));
out_strarrow(out);
out.push_str("e_if_needed(&a.newname));
out.push_str(",\n");
}
fn render_include(i: &IncludeRule, out: &mut String, indent: usize) {
push_indent(out, indent);
out.push_str("#include");
if i.if_exists {
out.push_str(" if exists");
}
out.push(' ');
if i.magic {
out.push('<');
out.push_str(&i.path);
out.push('>');
} else {
out.push_str("e_if_needed(&i.path));
}
out.push('\n');
}
fn render_abi(a: &AbiRule, out: &mut String, indent: usize) {
push_indent(out, indent);
out.push_str("abi ");
if a.magic {
out.push('<');
out.push_str(&a.path);
out.push('>');
} else {
out.push_str("e_if_needed(&a.path));
}
out.push_str(",\n");
}
fn render_profile(p: &Profile, out: &mut String, indent: usize) {
push_indent(out, indent);
out.push_str("profile ");
let name_str = quote_if_needed(&p.name);
out.push_str(&name_str);
if let Some(att) = &p.attachment {
let att_str = quote_if_needed(&att.render());
if att_str != name_str {
out.push(' ');
out.push_str(&att_str);
}
}
render_flags(&p.flags, out);
out.push_str(" {\n");
for v in &p.variables {
render_variable(v, out, indent + 2);
}
for i in &p.includes {
render_include(i, out, indent + 2);
}
for a in &p.aliases {
render_alias(a, out, indent + 2);
}
for r in &p.rules {
render_rule(r, out, indent + 2);
}
for h in &p.hats {
render_hat(h, out, indent + 2);
}
push_indent(out, indent);
out.push_str("}\n");
}
pub(crate) fn render_profile_only(p: &Profile, out: &mut String) {
render_profile(p, out, 0);
}
fn render_hat(h: &Hat, out: &mut String, indent: usize) {
for v in &h.variables {
render_variable(v, out, indent);
}
for i in &h.includes {
render_include(i, out, indent);
}
push_indent(out, indent);
if h.is_subprofile {
out.push_str("profile ");
} else {
out.push_str("hat ");
}
let name_str = quote_if_needed(&h.name);
out.push_str(&name_str);
if let Some(att) = &h.attachment {
let att_str = quote_if_needed(&att.render());
if att_str != name_str {
out.push(' ');
out.push_str(&att_str);
}
}
render_flags(&h.flags, out);
out.push_str(" {\n");
for a in &h.aliases {
render_alias(a, out, indent + 2);
}
for r in &h.rules {
render_rule(r, out, indent + 2);
}
for sub in &h.hats {
render_hat(sub, out, indent + 2);
}
push_indent(out, indent);
out.push_str("}\n");
}
fn render_flags(flags: &[Flag], out: &mut String) {
if flags.is_empty() {
return;
}
out.push_str(" flags=(");
for (i, f) in flags.iter().enumerate() {
if i > 0 {
out.push_str(", ");
}
out.push_str(&f.name);
if let Some(v) = &f.value {
out.push('=');
out.push_str(v);
}
}
out.push(')');
}
fn render_rule(r: &Rule, out: &mut String, indent: usize) {
push_indent(out, indent);
match r {
Rule::File(f) => render_file_rule(f, out),
Rule::Capability(c) => {
render_qualifiers(&c.qualifiers, out);
out.push_str("capability");
for cap in &c.capabilities {
out.push(' ');
out.push_str(cap);
}
out.push_str(",\n");
}
Rule::Network(n) => render_network_rule(n, out),
Rule::Mount(m) => render_mount_rule(m, out),
Rule::PivotRoot(p) => render_pivot_root_rule(p, out),
Rule::Link(l) => render_link_rule(l, out),
Rule::ChangeProfile(c) => render_change_profile_rule(c, out),
Rule::Rlimit(rl) => render_rlimit_rule(rl, out),
Rule::Dbus(d) => render_dbus_rule(d, out),
Rule::Unix(u) => render_unix_rule(u, out),
Rule::Mqueue(m) => render_mqueue_rule(m, out),
Rule::Userns(u) => {
render_qualifiers(&u.qualifiers, out);
out.push_str("userns");
for a in &u.access {
out.push(' ');
out.push_str(a);
}
out.push_str(",\n");
}
Rule::IoUring(i) => render_io_uring_rule(i, out),
Rule::Signal(s) => render_signal_rule(s, out),
Rule::Ptrace(p) => render_ptrace_rule(p, out),
Rule::All(a) => {
render_qualifiers(&a.qualifiers, out);
out.push_str("all,\n");
}
Rule::Raw(raw) => {
out.push_str(&raw.text);
out.push_str(",\n");
}
}
}
fn render_file_rule(f: &FileRule, out: &mut String) {
render_qualifiers(&f.qualifiers, out);
if f.owner {
out.push_str("owner ");
}
if let Some(g) = &f.group {
out.push_str("group=");
out.push_str(g);
out.push(' ');
}
if let Some(p) = &f.path {
out.push_str("e_if_needed(&p.render()));
out.push(' ');
} else {
out.push_str("file");
if !f.access.is_empty() {
out.push(' ');
}
}
out.push_str(&render_access(&f.access));
if let Some(t) = &f.exec_target {
out_strarrow(out);
out.push_str("e_if_needed(&t.render()));
}
out.push_str(",\n");
}
fn render_access(access: &[FileAccess]) -> String {
let mut s = String::new();
for a in access {
match a {
FileAccess::Read => s.push('r'),
FileAccess::Write => s.push('w'),
FileAccess::Append => s.push('a'),
FileAccess::Link => s.push('l'),
FileAccess::Lock => s.push('k'),
FileAccess::MemoryMap => s.push('m'),
FileAccess::Exec(t) => s.push_str(exec_str(t)),
}
}
s
}
fn exec_str(t: &ExecTransition) -> &'static str {
match t {
ExecTransition::Inherit => "ix",
ExecTransition::Unconstrained => "ux",
ExecTransition::UnconstrainedSafe => "Ux",
ExecTransition::Profile => "px",
ExecTransition::ProfileSafe => "Px",
ExecTransition::Child => "cx",
ExecTransition::ChildSafe => "Cx",
ExecTransition::ProfileInherit => "pix",
ExecTransition::ProfileInheritSafe => "Pix",
ExecTransition::ChildInherit => "cix",
ExecTransition::ChildInheritSafe => "Cix",
ExecTransition::ProfileUnconstrained => "pux",
ExecTransition::ProfileUnconstrainedSafe => "PUx",
ExecTransition::ChildUnconstrained => "cux",
ExecTransition::ChildUnconstrainedSafe => "CUx",
ExecTransition::BareX => "x",
}
}
fn render_network_rule(n: &NetworkRule, out: &mut String) {
render_qualifiers(&n.qualifiers, out);
out.push_str("network");
for a in &n.access {
out.push(' ');
out.push_str(&network_access_str(a));
}
if let Some(d) = &n.domain {
out.push(' ');
out.push_str(d);
}
if let Some(t) = &n.network_type {
out.push(' ');
out.push_str(t);
}
if let Some(p) = &n.protocol {
out.push(' ');
out.push_str(p);
}
for (k, v) in &n.conditions {
out.push(' ');
out.push_str(k);
out.push('=');
out.push_str(v);
}
out.push_str(",\n");
}
fn network_access_str(a: &NetworkAccess) -> String {
match a {
NetworkAccess::Create => "create".to_string(),
NetworkAccess::Bind => "bind".to_string(),
NetworkAccess::Listen => "listen".to_string(),
NetworkAccess::Accept => "accept".to_string(),
NetworkAccess::Connect => "connect".to_string(),
NetworkAccess::Shutdown => "shutdown".to_string(),
NetworkAccess::GetAttr => "getattr".to_string(),
NetworkAccess::SetAttr => "setattr".to_string(),
NetworkAccess::GetOpt => "getopt".to_string(),
NetworkAccess::SetOpt => "setopt".to_string(),
NetworkAccess::Send => "send".to_string(),
NetworkAccess::Receive => "receive".to_string(),
NetworkAccess::Read => "r".to_string(),
NetworkAccess::Write => "w".to_string(),
NetworkAccess::ReadWrite => "rw".to_string(),
NetworkAccess::Other(s) => s.clone(),
}
}
fn render_mount_rule(m: &MountRule, out: &mut String) {
render_qualifiers(&m.qualifiers, out);
match m.operation {
MountOp::Mount => out.push_str("mount"),
MountOp::Remount => out.push_str("remount"),
MountOp::Umount => out.push_str("umount"),
}
if let Some(f) = &m.fstype {
render_value_condition(f, out);
}
if let Some(o) = &m.options {
render_value_condition(o, out);
}
if let Some(s) = &m.source {
out.push(' ');
out.push_str("e_if_needed(&s.render()));
}
if let Some(mp) = &m.mount_point {
out.push(' ');
out.push_str("e_if_needed(&mp.render()));
}
if let Some(t) = &m.target {
out_strarrow(out);
out.push_str(&t.name);
}
out.push_str(",\n");
}
fn render_value_condition(c: &ValueCondition, out: &mut String) {
out.push(' ');
out.push_str(&c.name);
if c.in_list {
out.push_str(" in (");
for (i, v) in c.values.iter().enumerate() {
if i > 0 {
out.push_str(", ");
}
out.push_str(v);
}
out.push(')');
} else {
out.push('=');
out.push_str(&c.values.first().cloned().unwrap_or_default());
}
}
fn render_pivot_root_rule(p: &PivotRootRule, out: &mut String) {
render_qualifiers(&p.qualifiers, out);
out.push_str("pivot_root");
if let Some(o) = &p.old_root {
out.push_str(" oldroot=");
out.push_str("e_if_needed(&o.render()));
}
if let Some(n) = &p.new_root {
out.push(' ');
out.push_str("e_if_needed(&n.render()));
}
if let Some(t) = &p.target {
out_strarrow(out);
out.push_str(&t.name);
}
out.push_str(",\n");
}
fn render_link_rule(l: &LinkRule, out: &mut String) {
render_qualifiers(&l.qualifiers, out);
if l.owner {
out.push_str("owner ");
}
out.push_str("link");
if l.subset {
out.push_str(" subset");
}
out.push(' ');
out.push_str("e_if_needed(&l.oldname.render()));
out_strarrow(out);
out.push_str("e_if_needed(&l.newname.render()));
out.push_str(",\n");
}
fn render_change_profile_rule(c: &ChangeProfileRule, out: &mut String) {
render_qualifiers(&c.qualifiers, out);
out.push_str("change_profile");
match c.safe {
Some(true) => out.push_str(" safe"),
Some(false) => out.push_str(" unsafe"),
None => {}
}
out.push(' ');
out.push_str("e_if_needed(&c.executable.render()));
if let Some(t) = &c.target {
out_strarrow(out);
out.push_str(&t.name);
}
out.push_str(",\n");
}
fn render_rlimit_rule(r: &RlimitRule, out: &mut String) {
render_qualifiers(&r.qualifiers, out);
out.push_str("set rlimit ");
out.push_str(&r.rlimit);
if let Some(op) = r.op {
match op {
RelOp::LessEqual => out.push_str(" <="),
RelOp::Less => out.push_str(" <"),
}
}
if let Some(v) = &r.value {
out.push(' ');
out.push_str(&v.number.to_string());
if let Some(u) = &v.unit {
out.push_str(u);
}
}
out.push_str(",\n");
}
fn render_dbus_rule(d: &DbusRule, out: &mut String) {
render_qualifiers(&d.qualifiers, out);
out.push_str("dbus");
for a in &d.access {
out.push(' ');
out.push_str(a);
}
if let Some(b) = &d.bus {
out.push_str(" bus=");
out.push_str(b);
}
if let Some(p) = &d.path {
out.push_str(" path=");
out.push_str("e_if_needed(&p.render()));
}
if let Some(i) = &d.interface {
out.push_str(" interface=");
out.push_str(i);
}
if let Some(m) = &d.member {
out.push_str(" member=");
out.push_str(m);
}
if let Some(p) = &d.peer {
render_peer(p, out);
}
if let Some(l) = &d.label {
out.push_str(" label=");
out.push_str("e_if_needed(&l.render()));
}
if let Some(n) = &d.name {
out.push_str(" name=");
out.push_str(n);
}
out.push_str(",\n");
}
fn render_peer(p: &PeerCondition, out: &mut String) {
out.push_str(" peer=(");
let mut first = true;
if let Some(n) = &p.name {
out.push_str("name=");
out.push_str(n);
first = false;
}
if let Some(l) = &p.label {
if !first {
out.push_str(", ");
}
out.push_str("label=");
out.push_str(l);
first = false;
}
if let Some(a) = &p.addr {
if !first {
out.push_str(", ");
}
out.push_str("addr=");
out.push_str(&a.render());
}
out.push(')');
}
fn render_unix_rule(u: &UnixRule, out: &mut String) {
render_qualifiers(&u.qualifiers, out);
out.push_str("unix");
for a in &u.access {
out.push(' ');
out.push_str(a);
}
if let Some(t) = &u.type_cond {
out.push_str(" type=");
out.push_str(t);
}
if let Some(p) = &u.protocol {
out.push_str(" protocol=");
out.push_str(p);
}
if let Some(a) = &u.addr {
out.push_str(" addr=");
out.push_str("e_if_needed(&a.render()));
}
if let Some(l) = &u.label {
out.push_str(" label=");
out.push_str("e_if_needed(&l.render()));
}
if let Some(a) = &u.attr {
out.push_str(" attr=");
out.push_str("e_if_needed(&a.render()));
}
if let Some(o) = &u.opt {
out.push_str(" opt=");
out.push_str("e_if_needed(&o.render()));
}
if let Some(p) = &u.peer {
render_peer(p, out);
}
out.push_str(",\n");
}
fn render_mqueue_rule(m: &MqueueRule, out: &mut String) {
render_qualifiers(&m.qualifiers, out);
out.push_str("mqueue");
for a in &m.access {
out.push(' ');
out.push_str(a);
}
if let Some(t) = &m.mq_type {
out.push_str(" type=");
out.push_str(t);
}
if let Some(l) = &m.label {
out.push_str(" label=");
out.push_str("e_if_needed(&l.render()));
}
if let Some(n) = &m.name {
out.push_str(" name=");
out.push_str("e_if_needed(&n.render()));
}
out.push_str(",\n");
}
fn render_io_uring_rule(i: &IoUringRule, out: &mut String) {
render_qualifiers(&i.qualifiers, out);
out.push_str("io_uring");
for a in &i.access {
out.push(' ');
out.push_str(a);
}
if let Some(l) = &i.label {
out.push_str(" label=");
out.push_str("e_if_needed(&l.render()));
}
out.push_str(",\n");
}
fn render_signal_rule(s: &SignalRule, out: &mut String) {
render_qualifiers(&s.qualifiers, out);
out.push_str("signal");
for a in &s.access {
out.push(' ');
out.push_str(a);
}
if let Some(set) = &s.set {
out.push_str(" set=(");
for (i, v) in set.iter().enumerate() {
if i > 0 {
out.push_str(", ");
}
out.push_str(v);
}
out.push(')');
}
if let Some(p) = &s.peer {
out.push_str(" peer=");
out.push_str("e_if_needed(&p.render()));
}
out.push_str(",\n");
}
fn render_ptrace_rule(p: &PtraceRule, out: &mut String) {
render_qualifiers(&p.qualifiers, out);
out.push_str("ptrace");
for a in &p.access {
out.push(' ');
out.push_str(a);
}
if let Some(peer) = &p.peer {
out.push_str(" peer=");
out.push_str("e_if_needed(&peer.render()));
}
out.push_str(",\n");
}
fn render_qualifiers(q: &Qualifiers, out: &mut String) {
if let Some(p) = q.priority {
out.push_str("priority=");
out.push_str(&p.to_string());
out.push(' ');
}
if q.audit {
out.push_str("audit ");
}
if q.deny {
out.push_str("deny ");
}
if q.allow {
out.push_str("allow ");
}
}
fn out_strarrow(out: &mut String) {
out.push_str(" -> ");
}
fn push_indent(out: &mut String, indent: usize) {
for _ in 0..indent {
out.push(' ');
}
}
fn quote_if_needed(s: &str) -> String {
if s.contains(char::is_whitespace) {
let mut out = String::with_capacity(s.len() + 2);
out.push('"');
for c in s.chars() {
if c == '"' || c == '\\' {
out.push('\\');
}
out.push(c);
}
out.push('"');
out
} else {
s.to_string()
}
}