Skip to content
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

nixos/apparmor: policy activation tristate and policy path support #356796

Merged
merged 5 commits into from
Dec 17, 2024

Conversation

LordGrimmauld
Copy link
Contributor

@LordGrimmauld LordGrimmauld commented Nov 17, 2024

AppArmor profiles are files written to /etc/apparmor.d/<name>. Previously, profiles were defined with a string and written to a store entry, then linked into the etc path. However, this makes little sense if one wants to use profiles that are already files in the nix store, e.g. when using rules from roddhjav-apparmor-rules (which is absolutely broken currently, but that is a different topic; PR for that coming eventually). This change in the module exposes an option security.apparmor.policies.<name>.path with which a path to a profile can be directly symlinked into the apparmor directory, without first reading and rewriting that file into nix store again. This should boost nixos eval times on systems using prebuilt apparmor rules and makes prebuilding/caching apparmor rulesets via nix possible.

AppArmor profiles always fall within a tristate of enable, disable, complain. It makes no sense to have two boolean options instead of a neat enum config for the tristate. Therefore, i added that tristate, removing the two bools in the process.

The apparmor nixos test breaks often. While testing this PR, i noticed the test failing on master/nixos-unstable too. Therefore, i included a fix to the nixos test.

Yes, the two removed bools are a breaking change. However, the path option addition is inert, it does not break definitions of apparmor policies defined via string.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: documentation This PR adds or changes documentation 8.has: changelog 8.has: module (update) This PR changes an existing module in `nixos/` labels Nov 17, 2024
@LordGrimmauld LordGrimmauld changed the title Apparmor module pr nixos/apparmor: profile activation tristate and profile path support Nov 17, 2024
@LordGrimmauld LordGrimmauld changed the title nixos/apparmor: profile activation tristate and profile path support nixos/apparmor: policy activation tristate and policy path support Nov 17, 2024
@mweinelt mweinelt requested a review from ajs124 November 17, 2024 21:39
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10 labels Nov 18, 2024
@LordGrimmauld LordGrimmauld requested a review from ju1m November 18, 2024 09:08
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/apparmor-default-profiles/16780/8

Copy link
Member

@symphorien symphorien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think you could test your options in the nixos test?

nixos/modules/security/apparmor.nix Outdated Show resolved Hide resolved
nixos/modules/security/apparmor.nix Outdated Show resolved Hide resolved
nixos/modules/security/apparmor.nix Outdated Show resolved Hide resolved
nixos/modules/security/apparmor.nix Outdated Show resolved Hide resolved
@LordGrimmauld
Copy link
Contributor Author

Thank you for the feedback! I'll take a look at more explicit nixos tests also testing the new logic. Your points about wording of docs/descriptions is entirely fair.

@symphorien
Copy link
Member

Thank you for the improved nixos test. Do you think you could add a profile in enforce mode for a third tool (say, hexdump) and check that it really gets permission denied/crashes when trying to access files beyond what its profile allows ? That would be a good integration test that apparmor works.

@LordGrimmauld
Copy link
Contributor Author

Alright. I had a go at completely refactoring the apparmor nixos tests.
Things done:

  • nixfmt on apparmor test
  • move apparmor test to nixos/tests/apparmor directory
  • expected profile contents are now generated in its own file to make the test file less confusing and hard to maintain
  • enforce/complain is now being tested via diff of expected against aa-status
  • path is now tested against diff+file checking symlink target of /etc/static/apparmor.d/<name>
  • profile is now checked by diff of /etc/static/apparmor.d/<name> against original string added in nix config
  • test still successfully passes

@LordGrimmauld
Copy link
Contributor Author

fair point in seeing something crash! Will add a test case for hello succeeding and hexdump failing.

@LordGrimmauld
Copy link
Contributor Author

Alright, i managed to add a failing test case for hexdump and a succeeding testcase for hello.

nixos/modules/security/apparmor.nix Outdated Show resolved Hide resolved
nixos/modules/security/apparmor.nix Outdated Show resolved Hide resolved
@getchoo getchoo added the 2.status: merge conflict This PR has merge conflicts with the target branch label Dec 8, 2024
@LordGrimmauld
Copy link
Contributor Author

LordGrimmauld commented Dec 8, 2024

Since assertions don't really exist in submodules, we can instead create these assertions from the top-level context by mapping over each policy (we can also simplify this assertion with an XOR operator)

@getchoo you are missing one relevant detail here: The current assert runs in the apply function. This is relevant, because querrying the policy path from outside will always return a path (returned by apply), either taking the path defined in path or writing the policy to store and returning that path.
This is relevant, because this means you can't easily check for null/undefined of <policy>.path outside.

The alternative is dropping the apply function and inlining it wherever the actual path is required. I did not do this because the previous module using apply. I also figured it'd break peoples configs less hard if behavior stayed roughly similar (previously, you could query the policy path by using policy.profile to read).

I am willing to change the assert to a module-global assert instead of specifically in the submodule. I did want to confirm though, because your requested changes alone wouldn't work.

nixos/modules/security/apparmor.nix Outdated Show resolved Hide resolved
nixos/modules/security/apparmor.nix Outdated Show resolved Hide resolved
@LordGrimmauld LordGrimmauld removed the 2.status: merge conflict This PR has merge conflicts with the target branch label Dec 9, 2024
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/apparmor-on-nixos-roadmap/57217/1

@ofborg ofborg bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Dec 10, 2024
@LordGrimmauld
Copy link
Contributor Author

Alright, my new ram arrived, i ran nixpkgs-review (which this passed) and fixed the merge conflicts (again). I believe this is ready.

@LordGrimmauld LordGrimmauld removed the 2.status: merge conflict This PR has merge conflicts with the target branch label Dec 11, 2024
Copy link
Member

@getchoo getchoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for all of your work here!

@getchoo
Copy link
Member

getchoo commented Dec 12, 2024

@ofborg test apparmor

@LordGrimmauld
Copy link
Contributor Author

I am confused, why are ofborg eval and eval-lib-tests failing on gnome circle things? this PR doesn't even touch those... But at least the apparmor nixos test passes, and the new github actions to replace ofborg pass too

@mweinelt
Copy link
Member

mweinelt commented Dec 12, 2024

They were probably failing on the base commit at the time that ofborg merged your changes against master to test eval.

@wegank wegank added the 12.approvals: 1 This PR was reviewed and approved by one reputable person label Dec 12, 2024
@LordGrimmauld
Copy link
Contributor Author

i fixed merge conflicts (again).
I also noticed the test being flaky because hexdump wouldn't match the profile properly, fixed by matching against /nix/store/*/bin/hexdump for now.

@ofborg ofborg bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Dec 16, 2024
@wegank wegank removed the 12.approvals: 1 This PR was reviewed and approved by one reputable person label Dec 16, 2024
- nixfmt on apparmor test
- move apparmor test to nixos/tests/apparmor directory
- expected profile contents are now generated in its own file to make the test file less confusing and hard to maintain
- enforce/complain is now being tested via diff of expected against aa-status
- path is now tested against diff+file checking symlink target of /etc/static/apparmor.d/<name>
- profile is now checked by diff of /etc/static/apparmor.d/<name> against original string added in nix config
- test still successfully passes
- added test for confined hello to succeed
- added test for confined hexdump on denied path to fail
@mweinelt mweinelt removed the 2.status: merge conflict This PR has merge conflicts with the target branch label Dec 16, 2024
@getchoo
Copy link
Member

getchoo commented Dec 17, 2024

@ofborg test apparmor
And feel free to ping me in a couple days if there are no other reviews :)

@symphorien symphorien merged commit 03040e0 into NixOS:master Dec 17, 2024
41 of 42 checks passed
@symphorien
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: changelog 8.has: documentation This PR adds or changes documentation 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants