From 3049fd5d5e1c90def7b03929b4f39bff8f18fc62 Mon Sep 17 00:00:00 2001 From: James Le Cuirot Date: Mon, 16 Nov 2026 15:18:17 +0000 Subject: [PATCH] SELinux: Relabel the confext and sysexts correctly We have been relabelling /etc (for the confext) already, but this was broken in CI and other systems where SELinux was enabled on the host, even when it was permissive. We were previously not relabelling sysexts at all, but they were still picking up the container_file_t label from Docker. systemd-repart was silently dropping our labels because they couldn't be applied to its staging directory within Docker's overlay. The staging directory needs to be on a bind mount instead. Signed-off-by: James Le Cuirot --- .github/workflows/portage-stable-packages-list | 1 + build_library/build_image_util.sh | 11 +++++++---- build_sysext | 15 +++++++++++++-- .../bugfixes/2026-11-17-extensions-selinux.md | 1 + ...0.0.1-r321.ebuild => coreos-0.0.1-r322.ebuild} | 0 .../coreos-base/coreos/coreos-0.0.1.ebuild | 1 + 6 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 changelog/bugfixes/2026-11-17-extensions-selinux.md rename sdk_container/src/third_party/coreos-overlay/coreos-base/coreos/{coreos-0.0.1-r321.ebuild => coreos-0.0.1-r322.ebuild} (100%) diff --git a/.github/workflows/portage-stable-packages-list b/.github/workflows/portage-stable-packages-list index 0949ca56b16..585579004ba 100644 --- a/.github/workflows/portage-stable-packages-list +++ b/.github/workflows/portage-stable-packages-list @@ -592,6 +592,7 @@ sec-policy/selinux-base sec-policy/selinux-base-policy sec-policy/selinux-container sec-policy/selinux-dbus +sec-policy/selinux-docker sec-policy/selinux-policykit sec-policy/selinux-sssd sec-policy/selinux-unconfined diff --git a/build_library/build_image_util.sh b/build_library/build_image_util.sh index b7795ade839..2db0c0250e2 100755 --- a/build_library/build_image_util.sh +++ b/build_library/build_image_util.sh @@ -675,10 +675,10 @@ EOF local real_root_fs_dir real_root_fs_dir=$(realpath "${root_fs_dir}") # TODO: Breaks the system: - # sudo setfiles -Dv -r "${real_root_fs_dir}" "${real_root_fs_dir}"/etc/selinux/mcs/contexts/files/file_contexts "${real_root_fs_dir}" - # sudo setfiles -Dv -r "${real_root_fs_dir}" "${real_root_fs_dir}"/etc/selinux/mcs/contexts/files/file_contexts "${real_root_fs_dir}"/usr + # sudo setfiles -Dv -r "${real_root_fs_dir}" -c "${real_root_fs_dir}"/etc/selinux/mcs/policy/policy.* "${real_root_fs_dir}"/etc/selinux/mcs/contexts/files/file_contexts "${real_root_fs_dir}" + # sudo setfiles -Dv -r "${real_root_fs_dir}" -c "${real_root_fs_dir}"/etc/selinux/mcs/policy/policy.* "${real_root_fs_dir}"/etc/selinux/mcs/contexts/files/file_contexts "${real_root_fs_dir}"/usr # For now we only try it with /etc - sudo setfiles -Dv -r "${real_root_fs_dir}" "${real_root_fs_dir}"/etc/selinux/mcs/contexts/files/file_contexts "${real_root_fs_dir}"/etc + sudo setfiles -Dv -r "${real_root_fs_dir}" -c "${real_root_fs_dir}"/etc/selinux/mcs/policy/policy.* "${real_root_fs_dir}"/etc/selinux/mcs/contexts/files/file_contexts "${real_root_fs_dir}"/etc fi # Temporary hack: set group ownership of /etc/{g,}shadow to the @@ -719,7 +719,10 @@ EOF sudo cp -a "${root_fs_dir}/etc" "${root_fs_dir}/usr/lib/confexts/00-flatcar-default/etc" sudo mkdir -p "${root_fs_dir}/usr/lib/confexts/00-flatcar-default/etc/extension-release.d/" echo ID=_any | sudo tee "${root_fs_dir}/usr/lib/confexts/00-flatcar-default/etc/extension-release.d/extension-release.00-flatcar-default" > /dev/null - sudo systemd-repart \ + # systemd-repart stages in /var/tmp by default. Inside the SDK, this is within + # Docker's overlay, which may silently fail to accept SELinux labelling + # changes. We must therefore repoint TMPDIR to a bind-mounted directory. + sudo TMPDIR="${BUILD_DIR}" systemd-repart \ --private-key="${SYSEXT_SIGNING_KEY_DIR}/sysexts.key" \ --certificate="${SYSEXT_SIGNING_KEY_DIR}/sysexts.crt" \ --make-ddi=confext \ diff --git a/build_sysext b/build_sysext index 6ecbdb5f500..c8fd2a2a367 100755 --- a/build_sysext +++ b/build_sysext @@ -244,7 +244,6 @@ export SOURCE_DATE_EPOCH=$(stat -c '%Y' "${BUILD_DIR}/fs-root/usr/lib/os-release # Unmount in order to get rid of the overlay umount "${BUILD_DIR}/${FLAGS_install_root_basename}" -umount "${BUILD_DIR}/fs-root" if [[ "$FLAGS_generate_pkginfo" = "${FLAGS_TRUE}" ]] ; then info " Creating pkginfo squashfs '${BUILD_DIR}/${SYSEXT_ID}_pkginfo.raw'" @@ -305,6 +304,15 @@ EOF info "Removing opaque directory markers to always merge all contents" find "${BUILD_DIR}/${FLAGS_install_root_basename}" -xdev -type d -exec sh -c 'if [ "$(attr -R -q -g overlay.opaque {} 2>/dev/null)" = y ]; then attr -R -r overlay.opaque {}; fi' \; +if pkg_use_enabled coreos-base/coreos selinux; then + # setfiles from version 3.11 requires the passed path to have no symlinks + real_install_root=$(realpath "${BUILD_DIR}/${FLAGS_install_root_basename}") + real_fs_root=$(realpath "${BUILD_DIR}/fs-root") + setfiles -Dv -r "${real_install_root}" -c "${real_fs_root}"/etc/selinux/mcs/policy/policy.* "${real_fs_root}/etc/selinux/mcs/contexts/files/file_contexts" "${real_install_root}/usr" +fi + +umount "${BUILD_DIR}/fs-root" + info "Checking for invalid file ownership" invalid_files=$(find "${BUILD_DIR}/${FLAGS_install_root_basename}" -user sdk -or -group sdk) if [[ -n "${invalid_files}" ]]; then @@ -330,7 +338,10 @@ else info "Building sysext without compression (built-in sysexts)" fi -systemd-repart \ +# systemd-repart stages in /var/tmp by default. Inside the SDK, this is within +# Docker's overlay, which may silently fail to accept SELinux labelling changes. +# We must therefore repoint TMPDIR to a bind-mounted directory. +TMPDIR="${BUILD_DIR}" systemd-repart \ --sector-size=4096 \ --private-key="${SYSEXT_SIGNING_KEY_DIR}/sysexts.key" \ --certificate="${SYSEXT_SIGNING_KEY_DIR}/sysexts.crt" \ diff --git a/changelog/bugfixes/2026-11-17-extensions-selinux.md b/changelog/bugfixes/2026-11-17-extensions-selinux.md new file mode 100644 index 00000000000..23b5fa325b6 --- /dev/null +++ b/changelog/bugfixes/2026-11-17-extensions-selinux.md @@ -0,0 +1 @@ +- Fixed SELinux labelling of the confexts and system dependent sysexts. These were previously labelled entirely with `container_file_t`. ([scripts#4385](https://github.com/flatcar/scripts/pull/4385)) diff --git a/sdk_container/src/third_party/coreos-overlay/coreos-base/coreos/coreos-0.0.1-r321.ebuild b/sdk_container/src/third_party/coreos-overlay/coreos-base/coreos/coreos-0.0.1-r322.ebuild similarity index 100% rename from sdk_container/src/third_party/coreos-overlay/coreos-base/coreos/coreos-0.0.1-r321.ebuild rename to sdk_container/src/third_party/coreos-overlay/coreos-base/coreos/coreos-0.0.1-r322.ebuild diff --git a/sdk_container/src/third_party/coreos-overlay/coreos-base/coreos/coreos-0.0.1.ebuild b/sdk_container/src/third_party/coreos-overlay/coreos-base/coreos/coreos-0.0.1.ebuild index b6e64960492..e58e45a82d0 100644 --- a/sdk_container/src/third_party/coreos-overlay/coreos-base/coreos/coreos-0.0.1.ebuild +++ b/sdk_container/src/third_party/coreos-overlay/coreos-base/coreos/coreos-0.0.1.ebuild @@ -148,6 +148,7 @@ RDEPEND="${RDEPEND} sec-policy/selinux-base-policy sec-policy/selinux-container sec-policy/selinux-dbus + sec-policy/selinux-docker sec-policy/selinux-policykit sec-policy/selinux-unconfined sys-apps/acl