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

add missing SELinux directives #1031

Merged
merged 1 commit into from
Jul 5, 2024
Merged

Conversation

kbknapp
Copy link
Contributor

@kbknapp kbknapp commented Jul 4, 2024

Description

Fixes #1030

On a Fedora 40 system where /nix is a ZFS dataset there were several missing SELinux directives.

Checklist
  • Formatted with cargo fmt
  • Built with nix build
  • Ran flake checks with nix flake check
  • Added or updated relevant tests (leave unchecked if not applicable)
  • Added or updated relevant documentation (leave unchecked if not applicable)
  • Linked to related issues (leave unchecked if not applicable)

Copy link
Member

@grahamc grahamc left a comment

Choose a reason for hiding this comment

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

I'd like to recompile the policy file and make sure I get the same output. done. I have a ping out to a buddy, hoping to get a bit of secondary feedback on this. If I don't hear back soon let's go ahead with it.

Comment on lines 10 to 16
} No newline at end of file
type default_t;
type init_t;
class lnk_file read;
}

allow init_t default_t:lnk_file read;
Copy link
Member

@grahamc grahamc Jul 5, 2024

Choose a reason for hiding this comment

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

I'm surely no expert with SELinux, so this might be an obvious question but ... did this policy do anything before? Why did we have it? Yes. The relevant pieces are in the associated .fc file.

@grahamc grahamc merged commit 3f3d26b into DeterminateSystems:main Jul 5, 2024
16 checks passed
@NeilHanlon
Copy link

putting a public note here so I remember -- giving init_t full access to read default_t:lnk_file is probably not the "most correct" solution, as it is overly broad, despite being the solution recommended by audit2allow. I will try and spend some time on this next week (as I also use detsys nix on Fedora :) )

@kbknapp
Copy link
Contributor Author

kbknapp commented Jul 8, 2024

Yeah there is almost certainly a more targeted policy that could be used, in fact I see a few places where the default_t domain is used and I'm guessing something like a nix_t domain would be more appropriate?

$  ls -lZ /nix/var/nix/profiles/default 
lrwxrwxrwx - root system_u:object_r:bin_t:s0     31 Dec  1969 bin -> /nix/store/00dc6iygj63mjcmixki1vwg7m1jbdi9p-nix-2.21.2/bin
dr-xr-xr-x - root system_u:object_r:etc_t:s0     31 Dec  1969 etc
lrwxrwxrwx - root system_u:object_r:lib_t:s0     31 Dec  1969 lib -> /nix/store/00dc6iygj63mjcmixki1vwg7m1jbdi9p-nix-2.21.2/lib
lrwxrwxrwx - root system_u:object_r:default_t:s0 31 Dec  1969 libexec -> /nix/store/00dc6iygj63mjcmixki1vwg7m1jbdi9p-nix-2.21.2/libexec
lrwxrwxrwx - root system_u:object_r:default_t:s0 31 Dec  1969 manifest.nix -> /nix/store/3681kssclgak8rcvk9a0jhbpfcmm3sjq-env-manifest.nix
lrwxrwxrwx - root system_u:object_r:usr_t:s0     31 Dec  1969 share -> /nix/store/00dc6iygj63mjcmixki1vwg7m1jbdi9p-nix-2.21.2/share

Back to the original issue, I'm not sure exactly why that symlink was labeled default_t in the first place, because now that everything installed it's labeled systemd_unit_file_t at both ends:

$ ls -lZ /etc/systemd/system/nix-daemon.service                                                      
lrwxrwxrwx - root unconfined_u:object_r:systemd_unit_file_t:s0  2 Jul 08:37 /etc/systemd/system/nix-daemon.service -> /nix/var/nix/profiles/default/lib/systemd/system/nix-daemon.service
$ ls -lZ /nix/var/nix/profiles/default/lib/systemd/system/nix-daemon.service                                                
.r--r--r-- 430 root system_u:object_r:systemd_unit_file_t:s0 31 Dec  1969 /nix/var/nix/profiles/default/lib/systemd/system/nix-daemon.service

@emilazy
Copy link

emilazy commented Jul 8, 2024

Back to the original issue, I'm not sure exactly why that symlink was labeled default_t in the first place, because now that everything installed it's labeled systemd_unit_file_t at both ends:

The Determinate Systems installer turns automatic store optimization on. SELinux labels files, not paths. So when two identical files get hard‐linked together, labels can get messed up. nix-store --optimise is basically a request to corrupt your store on an SELinux system, and auto-optimise-store is even worse. Sorry, I kept meaning to properly report this and never got around to it, but I also ran into this problem all the time and narrowed it down to store optimization, and I just came across this PR, so I figured I should at least post this.

@NeilHanlon
Copy link

That's very helpful to know, thanks @emilazy !

@kbknapp
Copy link
Contributor Author

kbknapp commented Jul 8, 2024

It sounds like the optimize process either just needs to learn to restorecon, or whatever is scheduling the optimize could add that after? I'm assuming this is far easier said than done, of course 😄

@emilazy
Copy link

emilazy commented Jul 8, 2024

I believe that restorecon isn’t enough; the problem is that you can have “the same file” (because of hard‐links) with two possible labels (e.g. because one version is in the bin directory of a derivation and another isn’t). I didn’t narrow down a minimal reproduction case but fiddling around led me to the conclusion that the current store optimization process just isn’t fit for purpose in the presence of SELinux.

@kbknapp
Copy link
Contributor Author

kbknapp commented Jul 8, 2024

Ah ok, that makes sense thanks! Yeah I don't know of a way out that conundrum since from what I understand; SELinux just labels based off "last matched" when it comes to multiple links point to the same inode. There is an option to hard error (i.e. setfiles -E) in such a case, but that doesn't help anything here where by design nix expects such conflicts to occur on optimize.

@emilazy
Copy link

emilazy commented Jul 8, 2024

I think the installer should just turn off auto-optimise-store on SELinux systems, and in an ideal world trying to do a manual optimization would flag up a big red warning. You could do fancier stuff like checking that the files would get the same label before linking, but it really doesn’t feel worth it to me. (What if you change the policy afterwards?)

@cole-h cole-h added this to the 0.20.1 milestone Jul 8, 2024
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 error when /nix is a ZFS dataset
5 participants