lix/src/libstore
Maximilian Bosch 045ee37438 libstore/local-derivation-goal: prohibit creating setuid/setgid binaries
With Linux kernel >=6.6 & glibc 2.39 a `fchmodat2(2)` is available that
isn't filtered away by the libseccomp sandbox.

Being able to use this to bypass that restriction has surprising results
for some builds such as lxc[1]:

> With kernel ≥6.6 and glibc 2.39, lxc's install phase uses fchmodat2,
> which slips through 9b88e52846/src/libstore/build/local-derivation-goal.cc (L1650-L1663).
> The fixupPhase then uses fchmodat, which fails.
> With older kernel or glibc, setting the suid bit fails in the
> install phase, which is not treated as fatal, and then the
> fixup phase does not try to set it again.

Please note that there are still ways to bypass this sandbox[2] and this is
mostly a fix for the breaking builds.

This change works by creating a syscall filter for the `fchmodat2`
syscall (number 452 on most systems). The problem is that glibc 2.39
is needed to have the correct syscall number available via
`__NR_fchmodat2` / `__SNR_fchmodat2`, but this flake is still on
nixpkgs 23.11. To have this change everywhere and not dependent on the
glibc this package is built against, I added a header
"fchmodat2-compat.hh" that sets the syscall number based on the
architecture. On most platforms its 452 according to glibc with a few
exceptions:

    $ rg --pcre2 'define __NR_fchmodat2 (?!452)'
    sysdeps/unix/sysv/linux/x86_64/x32/arch-syscall.h
    58:#define __NR_fchmodat2 1073742276

    sysdeps/unix/sysv/linux/mips/mips64/n32/arch-syscall.h
    67:#define __NR_fchmodat2 6452

    sysdeps/unix/sysv/linux/mips/mips64/n64/arch-syscall.h
    62:#define __NR_fchmodat2 5452

    sysdeps/unix/sysv/linux/mips/mips32/arch-syscall.h
    70:#define __NR_fchmodat2 4452

    sysdeps/unix/sysv/linux/alpha/arch-syscall.h
    59:#define __NR_fchmodat2 562

I added a small regression-test to the setuid integration-test that
attempts to set the suid bit on a file using the fchmodat2 syscall.
I confirmed that the test fails without the change in
local-derivation-goal.

Additionally, we require libseccomp 2.5.5 or greater now: as it turns
out, libseccomp maintains an internal syscall table and
validates each rule against it. This means that when using libseccomp
2.5.4 or older, one may pass `452` as syscall number against it, but
since it doesn't exist in the internal structure, `libseccomp` will refuse
to create a filter for that. This happens with nixpkgs-23.11, i.e. on
stable NixOS and when building Lix against the project's flake.

To work around that

* a backport of libseccomp 2.5.5 on upstream nixpkgs has been
  scheduled[3].

* the package now uses libseccomp 2.5.5 on its own already. This is to
  provide a quick fix since the correct fix for 23.11 is still a staging cycle
  away.

We still need the compat header though since `SCMP_SYS(fchmodat2)`
internally transforms this into `__SNR_fchmodat2` which points to
`__NR_fchmodat2` from glibc 2.39, so it wouldn't build on glibc 2.38.
The updated syscall table from libseccomp 2.5.5 is NOT used for that
step, but used later, so we need both, our compat header and their
syscall table 🤷

Relevant PRs in CppNix:

* https://github.com/NixOS/nix/pull/10591
* https://github.com/NixOS/nix/pull/10501

[1] https://github.com/NixOS/nixpkgs/issues/300635#issuecomment-2031073804
[2] https://github.com/NixOS/nixpkgs/issues/300635#issuecomment-2030844251
[3] https://github.com/NixOS/nixpkgs/pull/306070

(cherry picked from commit ba6804518772e6afb403dd55478365d4b863c854)
Change-Id: I6921ab5a363188c6bff617750d00bb517276b7fe
2024-05-03 16:29:06 +02:00
..
build libstore/local-derivation-goal: prohibit creating setuid/setgid binaries 2024-05-03 16:29:06 +02:00
builtins filetransfer: remove decompress request parameter 2024-04-25 01:33:08 +02:00
linux libstore/local-derivation-goal: prohibit creating setuid/setgid binaries 2024-05-03 16:29:06 +02:00
platform libstore: Create platform LocalStore subclasses 2024-04-23 16:17:05 +00:00
binary-cache-store.cc libutil: make AutoCloseFD a better resource 2024-03-18 15:42:52 -06:00
binary-cache-store.hh Merge pull request #6258 from obsidiansystems/gcc-bug-ergonomics 2024-03-04 05:24:33 +01:00
build-result.cc Merge pull request #9094 from obsidiansystems/test-proto 2024-03-04 04:36:23 +01:00
build-result.hh Merge pull request #9094 from obsidiansystems/test-proto 2024-03-04 04:36:23 +01:00
builtins.hh Ensure all headers have #pragma once and are in API docs 2023-03-31 23:19:44 -04:00
ca-specific-schema.sql ca: add sqlite index on RealisationsRefs(realisationReference) 2022-04-21 10:06:39 +02:00
common-protocol-impl.hh Merge pull request #9099 from obsidiansystems/common-proto 2024-03-04 04:36:58 +01:00
common-protocol.cc Merge pull request #9099 from obsidiansystems/common-proto 2024-03-04 04:36:58 +01:00
common-protocol.hh Merge pull request #9099 from obsidiansystems/common-proto 2024-03-04 04:36:58 +01:00
content-address.cc Fix various clang-tidy lints 2024-03-29 20:26:38 -07:00
content-address.hh Fix various clang-tidy lints 2024-03-29 20:26:38 -07:00
crypto.cc Make sodium a required dependency 2021-01-06 17:56:53 +01:00
crypto.hh Finish converting existing comments for internal API docs (#8146) 2023-04-07 13:55:28 +00:00
daemon.cc libstore: un-inline copyNAR expansions 2024-03-24 15:24:02 +01:00
daemon.hh Add Store::isTrustedClient() 2023-04-06 19:59:57 -04:00
derivations.cc HOT SALE: 15% off your build times! 2024-03-27 03:52:57 +00:00
derivations.hh Merge pull request #9563 from obsidiansystems/tryResolve-evalStore 2024-03-04 07:11:25 +01:00
derived-path-map.cc Revert "Adapt scheduler to work with dynamic derivations" 2023-10-02 15:05:23 +00:00
derived-path-map.hh pragma once and ///@file everything missing it 2024-04-08 15:40:12 -07:00
derived-path.cc Merge pull request #9582 from pennae/misc-opts 2024-03-04 07:32:31 +01:00
derived-path.hh Introduce OutputName and OutputNameView type aliases 2023-08-25 09:55:07 -04:00
downstream-placeholder.cc Introduce OutputName and OutputNameView type aliases 2023-08-25 09:55:07 -04:00
downstream-placeholder.hh Introduce OutputName and OutputNameView type aliases 2023-08-25 09:55:07 -04:00
dummy-store.cc Push getFSAccessor unsupported(...) down Store class hierarchy 2023-04-13 13:39:44 -04:00
dummy-store.md Support per-store Markdown documentation 2023-03-21 14:03:40 +01:00
export-import.cc libstore: un-inline copyNAR expansions 2024-03-24 15:24:02 +01:00
filetransfer.cc filetransfer: drop errorSink 2024-04-25 01:33:22 +02:00
filetransfer.hh filetransfer: remove decompress request parameter 2024-04-25 01:33:08 +02:00
fs-accessor.hh Finish converting existing comments for internal API docs (#8146) 2023-04-07 13:55:28 +00:00
gc-store.hh libstore: Create platform LocalStore subclasses 2024-04-23 16:17:05 +00:00
gc.cc libstore: Create platform LocalStore subclasses 2024-04-23 16:17:05 +00:00
globals.cc libutil: remove exception handling workingness check 2024-03-19 06:09:42 -06:00
globals.hh libstore: fix glossary link in documentation 2024-04-11 02:34:45 +02:00
http-binary-cache-store.cc Delete hasPrefix and hasSuffix from the codebase 2024-03-17 20:17:19 -07:00
http-binary-cache-store.md Support per-store Markdown documentation 2023-03-21 14:03:40 +01:00
indirect-root-store.hh Clean up store hierarchy with IndirectRootStore 2023-07-24 09:19:44 -04:00
legacy-ssh-store.cc Revert "libstore: remove one Resource::good flag" 2024-04-05 20:13:02 +00:00
legacy-ssh-store.md Support per-store Markdown documentation 2023-03-21 14:03:40 +01:00
length-prefixed-protocol-helper.hh Merge pull request #9099 from obsidiansystems/common-proto 2024-03-04 04:36:58 +01:00
local-binary-cache-store.cc Delete hasPrefix and hasSuffix from the codebase 2024-03-17 20:17:19 -07:00
local-binary-cache-store.md Support per-store Markdown documentation 2023-03-21 14:03:40 +01:00
local-fs-store.cc getBuildLog: factor out resolving derivations 2022-12-15 15:58:54 -05:00
local-fs-store.hh Merge pull request #6258 from obsidiansystems/gcc-bug-ergonomics 2024-03-04 05:24:33 +01:00
local-store.cc libstore: Create platform LocalStore subclasses 2024-04-23 16:17:05 +00:00
local-store.hh libstore: Create platform LocalStore subclasses 2024-04-23 16:17:05 +00:00
local-store.md Tweaks 2023-03-23 15:32:59 +01:00
local.mk libstore: Create platform LocalStore subclasses 2024-04-23 16:17:05 +00:00
lock.cc libutil: make AutoCloseFD a better resource 2024-03-18 15:42:52 -06:00
lock.hh Finish converting existing comments for internal API docs (#8146) 2023-04-07 13:55:28 +00:00
log-store.cc Move the getBuildLog implementation to its own implementation file 2023-01-13 11:05:44 +01:00
log-store.hh Finish converting existing comments for internal API docs (#8146) 2023-04-07 13:55:28 +00:00
machines.cc Delete hasPrefix and hasSuffix from the codebase 2024-03-17 20:17:19 -07:00
machines.hh Merge pull request #9841 from obsidiansystems/float-speed-factor 2024-03-04 08:40:38 +01:00
make-content-addressed.cc Merge pull request #8650 from obsidiansystems/content-address-simpler 2023-07-21 13:46:53 +02:00
make-content-addressed.hh makeContentAddressed: Add single path helper 2023-06-30 18:22:47 +02:00
meson.build Merge "libstore: Create platform LocalStore subclasses" into main 2024-04-24 15:35:32 +00:00
misc.cc Merge pull request #9589 from obsidiansystems/floating-content-addressing-derivations-eval-store 2024-03-04 07:15:08 +01:00
names.cc return string_views from forceString* 2022-01-27 17:15:43 +01:00
names.hh Ensure all headers have #pragma once and are in API docs 2023-03-31 23:19:44 -04:00
nar-accessor.cc Add explicit case statements where -Wswitch-enum would report them 2023-04-03 18:17:32 +02:00
nar-accessor.hh Finish converting existing comments for internal API docs (#8146) 2023-04-07 13:55:28 +00:00
nar-info-disk-cache.cc Use "raw pattern" for content address types 2023-03-30 17:12:49 -04:00
nar-info-disk-cache.hh Finish converting existing comments for internal API docs (#8146) 2023-04-07 13:55:28 +00:00
nar-info.cc Make "NAR info file is corrupt" messages more informative 2023-04-18 14:10:49 +02:00
nar-info.hh Merge remote-tracking branch 'upstream/master' into path-info 2023-04-07 20:39:04 -04:00
nix-store.pc.in Fix building with GCC 9 2023-02-10 18:38:57 +01:00
optimise-store.cc util.hh: split out signals stuff 2024-03-11 00:52:09 -07:00
outputs-spec.cc Merge pull request #9172 from tfc/bad-moves 2024-03-04 04:41:52 +01:00
outputs-spec.hh Introduce OutputName and OutputNameView type aliases 2023-08-25 09:55:07 -04:00
parsed-derivations.cc Fix exportReferencesGraph when given store subpath 2024-04-21 10:27:32 +00:00
parsed-derivations.hh Ensure all headers have #pragma once and are in API docs 2023-03-31 23:19:44 -04:00
path-info.cc Merge pull request #6223 from obsidiansystems/worker-proto-with-version 2024-03-04 04:59:31 +01:00
path-info.hh Merge pull request #9238 from tfc/small-improvements2 2024-03-04 05:18:05 +01:00
path-references.cc Make RewritingSink accept a map of rewrites 2023-05-24 14:11:50 +02:00
path-references.hh pragma once and ///@file everything missing it 2024-04-08 15:40:12 -07:00
path-regex.hh StorePath: reject names starting with '.' 2023-10-08 12:21:18 +00:00
path-with-outputs.cc Fixing #7479 2023-08-18 11:44:00 -04:00
path-with-outputs.hh Make the Derived Path family of types inductive for dynamic derivations 2023-08-10 00:08:32 -04:00
path.cc don't throw an exception for the trivial case of isStorePath()... 2024-04-14 21:08:07 +00:00
path.hh Ensure all headers have #pragma once and are in API docs 2023-03-31 23:19:44 -04:00
pathlocks.cc libutil: make AutoCloseFD a better resource 2024-03-18 15:42:52 -06:00
pathlocks.hh Finish converting existing comments for internal API docs (#8146) 2023-04-07 13:55:28 +00:00
platform.cc libstore: Create platform LocalStore subclasses 2024-04-23 16:17:05 +00:00
profiles.cc Merge pull request #9041 from trofi/profiles-sign 2024-03-04 04:36:10 +01:00
profiles.hh Clean up a few things related to profiles (#8526) 2023-06-19 04:04:59 +00:00
realisation.cc libstore: also pass unwanted outputs to the post-build-hook 2023-05-08 12:58:59 +02:00
realisation.hh Introduce OutputName and OutputNameView type aliases 2023-08-25 09:55:07 -04:00
remote-fs-accessor.cc libutil: make AutoCloseFD a better resource 2024-03-18 15:42:52 -06:00
remote-fs-accessor.hh Ensure all headers have #pragma once and are in API docs 2023-03-31 23:19:44 -04:00
remote-store-connection.hh pragma once and ///@file everything missing it 2024-04-08 15:40:12 -07:00
remote-store.cc Revert "libutil: remove Pool::Handle::bad" 2024-04-05 20:13:02 +00:00
remote-store.hh libutil: remove Pool::flushBad 2024-03-31 00:07:09 +00:00
s3-binary-cache-store.cc Delete hasPrefix and hasSuffix from the codebase 2024-03-17 20:17:19 -07:00
s3-binary-cache-store.hh Ensure all headers have #pragma once and are in API docs 2023-03-31 23:19:44 -04:00
s3-binary-cache-store.md Support per-store Markdown documentation 2023-03-21 14:03:40 +01:00
s3.hh Ensure all headers have #pragma once and are in API docs 2023-03-31 23:19:44 -04:00
schema.sql schema.sql: add comment about hash being in base16 2022-06-01 14:59:57 +02:00
serve-protocol-impl.hh Merge pull request #9137 from obsidiansystems/serve-protocol 2024-03-04 04:37:05 +01:00
serve-protocol.cc Merge pull request #9560 from obsidiansystems/serve-proto-unkeyed-valid-path-info-serializer 2024-03-07 12:37:33 +01:00
serve-protocol.hh Merge pull request #9560 from obsidiansystems/serve-proto-unkeyed-valid-path-info-serializer 2024-03-07 12:37:33 +01:00
sqlite.cc util.hh: split out signals stuff 2024-03-11 00:52:09 -07:00
sqlite.hh Merge pull request #9925 from 9999years/fmt-cleanup 2024-03-09 07:00:13 -07:00
ssh-store-config.hh Merge pull request #6258 from obsidiansystems/gcc-bug-ergonomics 2024-03-04 05:24:33 +01:00
ssh-store.cc Merge pull request #6258 from obsidiansystems/gcc-bug-ergonomics 2024-03-04 05:24:33 +01:00
ssh-store.md Support per-store Markdown documentation 2023-03-21 14:03:40 +01:00
ssh.cc libutil: make AutoCloseFD a better resource 2024-03-18 15:42:52 -06:00
ssh.hh Fix ControlMaster behaviour 2023-05-16 18:50:09 +04:00
store-api.cc libstore: Create platform LocalStore subclasses 2024-04-23 16:17:05 +00:00
store-api.hh Make things that can throw not noexcept anymore 2024-03-29 20:26:38 -07:00
store-cast.hh Ensure all headers have #pragma once and are in API docs 2023-03-31 23:19:44 -04:00
uds-remote-store.cc Clean up store hierarchy with IndirectRootStore 2023-07-24 09:19:44 -04:00
uds-remote-store.hh Clean up store hierarchy with IndirectRootStore 2023-07-24 09:19:44 -04:00
uds-remote-store.md Support per-store Markdown documentation 2023-03-21 14:03:40 +01:00
worker-protocol-impl.hh Merge pull request #9099 from obsidiansystems/common-proto 2024-03-04 04:36:58 +01:00
worker-protocol.cc Merge pull request #6223 from obsidiansystems/worker-proto-with-version 2024-03-04 04:59:31 +01:00
worker-protocol.hh Merge pull request #6223 from obsidiansystems/worker-proto-with-version 2024-03-04 04:59:31 +01:00