Skip to content

add apparmor support in parser #1067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ default = []
# on the system you are building sudo for (e.g. Debian, Fedora, but not Arch)
pam-login = []

# this enables the setting of APPARMOR_PROFILE in /etc/sudoers; this is disabled
# by default and is only used on Ubuntu
apparmor = []

# enable detailed logging (use for development only) to /tmp
# this will compromise the security of sudo-rs somewhat
dev = []
Expand Down
19 changes: 17 additions & 2 deletions src/sudoers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ pub struct Tag {
pub(super) authenticate: Authenticate,
pub(super) cwd: Option<ChDir>,
pub(super) env: EnvironmentControl,
#[cfg(feature = "apparmor")]
pub(super) apparmor_profile: Option<String>,
}

impl Tag {
Expand Down Expand Up @@ -418,11 +420,24 @@ impl Parse for MetaOrTag {
"ROLE" | "TYPE" => unrecoverable!(
pos = start_pos,
stream,
"role based access control is not yet supported by sudo-rs"
"SELinux role based access control is not yet supported by sudo-rs"
),

#[cfg(feature = "apparmor")]
"APPARMOR_PROFILE" => {
expect_syntax('=', stream)?;
let StringParameter(profile) = expect_nonterminal(stream)?;
Box::new(move |tag| tag.apparmor_profile = Some(profile.clone()))
}

"ALL" => return make(MetaOrTag(All)),
alias => return make(MetaOrTag(Alias(alias.to_string()))),
alias => {
if is_syntax('=', stream)? {
unrecoverable!(pos = start_pos, stream, "unsupported modifier '{}'", alias);
} else {
return make(MetaOrTag(Alias(alias.to_string())));
}
}
};

make(MetaOrTag(Only(result)))
Expand Down
5 changes: 5 additions & 0 deletions src/sudoers/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub struct Authentication {
pub allowed_attempts: u16,
pub prior_validity: Duration,
pub pwfeedback: bool,
#[cfg(feature = "apparmor")]
#[expect(dead_code)] // TODO: this attribute should be removed
pub apparmor_profile: Option<String>,
}

impl super::Settings {
Expand All @@ -45,6 +48,8 @@ impl super::Settings {
} else {
AuthenticatingUser::InvokingUser
},
#[cfg(feature = "apparmor")]
apparmor_profile: tag.apparmor_profile.clone(),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/sudoers/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ fn permission_test() {
pass!(["a\\,b ALL=ALL"], "a,b" => request! { root, root }, "server"; "/bin/foo");
pass!(["\"a,b\" ALL=ALL"], "a,b" => request! { root, root }, "server"; "/bin/foo");
pass!(["\"a\\b\" ALL=ALL"], "a\\b" => request! { root, root }, "server"; "/bin/foo");

// apparmor
#[cfg(feature = "apparmor")]
pass!(["ALL ALL=(ALL:ALL) APPARMOR_PROFILE=unconfined ALL"], "user" => root(), "server"; "/bin/bar" => [apparmor_profile: Some("unconfined".to_string())]);
}

#[test]
Expand Down
Loading