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

sandbox/apparmor: add GenerateAAREExclusionPatterns #11567

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

anonymouse64
Copy link
Member

I just couldn't resist the l33tcodeness of the problem of generating these rules with a helper so I fell into a hole and wrote this complex monster of a helper function.

Hope this helps! 😄

I only changed the docker-support interface to use this new helper and left TODO's elsewhere in the codebase that it could be used for future followups in the interest of time. Also happy to drop the docker-support changes to land this more quickly if reviewing it is too complex but I think it's also useful to see here how it's used in a prototypical manner.


This function is generic (and complex) enough to be able to handle all of the
overlapping and wildcard behavior we need in docker-support, and it could also
serve to replace numerous other places in the codebase where we need this sort
of complex behavior. It is a generalization of the existing
aareExclusionPatterns helper, though it's actually unclear if this exact
implementation will currently be able to serve the use case from that helper
directly or if more options/adjustments are needed to enable that use case as
well.

To keep the diff smaller, this patch does not actually change any of the
profiles/interfaces, just TODO's are left for where to use it.

Note that the generated rules are slightly more condensed in terms of number of
rules but significantly more verbose in terms of alternations, not sharing more
of repeated substrings between alternations inside the patterns. This was done
explicitly to keep the generating code simpler and easier to understand, but it
may prove to have performance effects, either detrimental or benevolent but
that should be measured before deciding to make the generation code even more
complex than it already is.

This function is generic (and complex) enough to be able to handle all of the
overlapping and wildcard behavior we need in docker-support, and it could also
serve to replace numerous other places in the codebase where we need this sort
of complex behavior. It is a generalization of the existing
aareExclusionPatterns helper, though it's actually unclear if this exact
implementation will currently be able to serve the use case from that helper
directly or if more options/adjustments are needed to enable that use case as
well.

To keep the diff smaller, this patch does not actually change any of the
profiles/interfaces, just TODO's are left for where to use it.

Note that the generated rules are slightly more condensed in terms of number of
rules but significantly more verbose in terms of alternations, not sharing more
of repeated substrings between alternations inside the patterns. This was done
explicitly to keep the generating code simpler and easier to understand, but it
may prove to have performance effects, either detrimental or benevolent but
that should be measured before deciding to make the generation code even more
complex than it already is.

Signed-off-by: Ian Johnson <[email protected]>
@anonymouse64 anonymouse64 added the Needs security review Can only be merged once security gave a :+1: label Mar 24, 2022
@codecov-commenter
Copy link

codecov-commenter commented Mar 24, 2022

Codecov Report

Merging #11567 (ad1936c) into master (1e7ef66) will increase coverage by 0.02%.
The diff coverage is 92.69%.

@@            Coverage Diff             @@
##           master   #11567      +/-   ##
==========================================
+ Coverage   78.00%   78.03%   +0.02%     
==========================================
  Files         939      939              
  Lines      109526   110129     +603     
==========================================
+ Hits        85441    85938     +497     
- Misses      18796    18869      +73     
- Partials     5289     5322      +33     
Flag Coverage Δ
unittests 78.03% <92.69%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
interfaces/builtin/home.go 100.00% <ø> (ø)
interfaces/builtin/system_backup.go 100.00% <ø> (ø)
interfaces/builtin/utils.go 96.77% <ø> (ø)
interfaces/builtin/docker_support.go 83.17% <78.94%> (-5.51%) ⬇️
sandbox/apparmor/apparmor.go 95.82% <95.73%> (-0.12%) ⬇️
overlord/devicestate/handlers_install.go 66.46% <0.00%> (-1.05%) ⬇️
overlord/devicestate/devicemgr.go 78.64% <0.00%> (-0.01%) ⬇️
interfaces/builtin/network_manager.go 78.94% <0.00%> (ø)
interfaces/policy/helpers.go 98.32% <0.00%> (+0.04%) ⬆️
... and 4 more

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

Copy link
Contributor

@mardy mardy left a comment

Choose a reason for hiding this comment

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

Just WOW :-)

I wish the long function could be split into a couple of smaller ones, but I don't see an obvious way to do it (without having to pass a lot of parameters...).
There's a little (but dangerous!) bug which would break the pattern when you have capital letters or numbers, but other than that this looks great!

@@ -129,6 +129,9 @@
# LD_PRELOAD to be set to a library which is in a directory configured by
# ld.so.conf, but access to those locations is mediated by this profile
# (which requires rules for specific locations).
# TODO: use GenerateAAREExclusionPatterns for this, though the first
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIU this apparmor profile is not generated at run-time, so I don't think we can use GenerateAAREExclusionPatterns unless we somehow hook it up in the Makefile.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes but it would still be nice to have some way to use the Go code which does this to generate it instead like perhaps as a go generate thing, hence the TODO

sandbox/apparmor/apparmor.go Show resolved Hide resolved
Comment on lines +320 to +322
if len(stillPresentPatterns) == 1 {
// there is only one group left from here and we can do the simple
// generation with this pattern for the rest of the rules
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should be able to call GenerateAAREExclusionPatternsSingle here, if we add the common exclude pattern computed so far to the opts.Prefix (of course, we need to pass a copy of the opts variable, not to modify our own one)

Copy link
Member Author

Choose a reason for hiding this comment

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

hmm so the issue with trying to implement this is that the loop for generateAAREExclusionPatternsSingle starts at 1 to skip the "/" in the normal exclude pattern case, however to re-use that here, we would need to start at 0 instead. I considered adding an option to not skip the first character / allow non-absolute filepaths but doing so would require more tests so I think I will just leave a TODO about this optimization instead

@bboozzoo bboozzoo self-requested a review March 24, 2022 06:48
@pedronis pedronis added the Needs Samuele review Needs a review from Samuele before it can land label Mar 24, 2022
These were not meant to be exported, only the fully generic one is meant to be
exported.

Signed-off-by: Ian Johnson <[email protected]>
…lude patt

Thanks to Alberto for spotting this :-)

Signed-off-by: Ian Johnson <[email protected]>
@anonymouse64
Copy link
Member Author

(closed and reopened to see if that kicks github action into doing something ... )

Copy link
Contributor

@mardy mardy left a comment

Choose a reason for hiding this comment

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

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Samuele review Needs a review from Samuele before it can land Needs security review Can only be merged once security gave a :+1:
Projects
None yet
4 participants