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

selinux relabel support #30

Merged
merged 6 commits into from
Nov 11, 2024
Merged

selinux relabel support #30

merged 6 commits into from
Nov 11, 2024

Conversation

allisonkarlitskaya
Copy link
Collaborator

@allisonkarlitskaya allisonkarlitskaya commented Nov 7, 2024

There are some outstanding items here:

  • figure out which policy to check. It's hardcoded to targeted right now
  • provide a nice fallback if the policy isn't present. It's currently fatal.
  • figure out a nicer way to handle linking to libselinux, possibly making it a conditional feature? don't link libselinux: do it ourselves
  • figure out a better way to handle mutability of hardlinks in the filesystem tree
  • properly label our /run/systemd/volatile-root hack symlink to avoid this: [ 3.055118] audit: type=1400 audit(1731016644.835:3): avc: denied { read } for pid=447 comm="systemd-gpt-aut" name="volatile-root" dev="tmpfs" ino=679 scontext=system_u:system_r:systemd_gpt_generator_t:s0 tcontext=system_u:object_r:init_var_run_t:s0 tclass=lnk_file permissive=1
  • drop enforcing=0

Closes #29

@travier
Copy link
Member

travier commented Nov 8, 2024

As far as I know, the SELinux policy type is taken from /etc/selinux/config:

$ grep "^SELINUXTYPE=" /etc/selinux/config`
SELINUXTYPE=targeted

Update our workflow to actually fail if clippy/fmt are unhappy.

Signed-off-by: Allison Karlitskaya <[email protected]>
We can also use fakeroot to make sure we see the correct selinux
contexts on the created filesystem, but xattrs aren't saved in
fakeroot's "save file", so we need to do this in a single running
session: move all of those operations to a new script and run the entire
script under one fakeroot invocation.

Signed-off-by: Allison Karlitskaya <[email protected]>
Rc<> doesn't want us to mutate the inner value when it has multiple
outstanding refs (as we have for hardlinks).  That's going to be a
problem for us soon when we want to visit the tree, adding selinux
xattrs.

Use RefCell<> to gain interior mutability on the xattr field.  This is
terribly ad-hoc, but it solves the problem at hand in the least
intrusive way possible.

Signed-off-by: Allison Karlitskaya <[email protected]>
We're about to start using this API with a lot more constant strings (to
fish out the selinux policy), so let's make it a bit less awful to use
in that case.

Signed-off-by: Allison Karlitskaya <[email protected]>
This is a fairly straight-forward operation on the in-memory filesystem
tree.

The actual work done by libselinux here is fairly trivial, and with the
use of the excellent `regex-automata` crate we can mostly do it
ourselves: we just need to consult a list of regexps for each file and
whichever one matches first decides the label.  We can build a DFA to do
all of the "real work" for us in a single step.  There's even probably
more room for improvement: if we figure out a way to deal with cache
invalidation (and path substitutions), we could completely avoid path
building and just pass around state identifiers, but let's keep things
simple for the time being.

It's worth noting that `regex-automata` is fairly complete, but doesn't
support some of the more advanced features of PCRE.  Nobody apparently
uses these features when writing SELinux policy, though, as the
labelling on the booted container is 100% correct (as reported by
`restorecon`).

Still: doing the work ourselves means that we don't need to take a
dependency on an external library which might change its behaviour in
the future.  The labelling sits on our critical "must be deterministic"
path, so this is quite nice.

Closes #29

Signed-off-by: Allison Karlitskaya <[email protected]>
We can now run the container with SELinux enabled and enforcing, with
only one very minor issue: our `/run/systemd/volatile-root` hack is
causing an AVC rejection with the GPT scanner.  We can put it into
permissive mode until we find a better solution.

Signed-off-by: Allison Karlitskaya <[email protected]>
@allisonkarlitskaya allisonkarlitskaya changed the title WIP: selinux support selinux relabel support Nov 10, 2024
@allisonkarlitskaya allisonkarlitskaya merged commit 0eb996a into main Nov 11, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SELinux support for oci create-image
2 participants