Skip to content

Commit 3c43343

Browse files
committed
selinux stuff
Signed-off-by: Krzesimir Nowak <[email protected]>
1 parent 05e0339 commit 3c43343

File tree

7 files changed

+120
-47
lines changed

7 files changed

+120
-47
lines changed

build_library/board_options.sh

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,6 @@ ARCH=$(get_board_arch ${BOARD})
1414
# What cross-build are we targeting?
1515
. "${BOARD_ROOT}/etc/portage/make.conf" || die
1616

17-
# check if any of the given use flags are enabled for a pkg
18-
pkg_use_enabled() {
19-
local pkg="${1}"; shift
20-
21-
# for every flag argument, turn it into a regexp that matches it as
22-
# either '+${flag}' or '(+${flag})'
23-
local -a grep_args=()
24-
local flag
25-
for flag; do
26-
grep_args+=( -e '^(\?+'"${flag}"')\?$' )
27-
done
28-
local -i rv=0
29-
30-
equery-"${BOARD}" --quiet uses --forced-masked "${pkg}" | grep --quiet "${grep_args[@]}" || rv=$?
31-
return ${rv}
32-
}
33-
3417
# Usage: pkg_version [installed|binary|ebuild] some-pkg/name
3518
# Prints: some-pkg/name-1.2.3
3619
# Note: returns 0 even if the package was not found.

build_library/build_image_util.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fi
1919
BUILD_DIR="${FLAGS_output_root}/${BOARD}/${IMAGE_SUBDIR}"
2020
OUTSIDE_OUTPUT_DIR="../build/images/${BOARD}/${IMAGE_SUBDIR}"
2121

22+
source "${BUILD_LIBRARY_DIR}/pkg_util.sh" || exit 1
2223
source "${BUILD_LIBRARY_DIR}/reports_util.sh" || exit 1
2324
source "${BUILD_LIBRARY_DIR}/sbsign_util.sh" || exit 1
2425

@@ -683,7 +684,7 @@ EOF
683684
fi
684685

685686
# Build the selinux policy
686-
if pkg_use_enabled coreos-base/coreos selinux; then
687+
if is_selinux_enabled "${BOARD}"; then
687688
info "Building selinux mcs policy"
688689
sudo chroot "${root_fs_dir}" bash -s <<'EOF'
689690
cd /usr/share/selinux/mcs
@@ -723,7 +724,7 @@ EOF
723724
# SELinux: Label the root filesystem for using 'file_contexts'.
724725
# The labeling has to be done before moving /etc to /usr/share/flatcar/etc to prevent wrong labels for these files and as
725726
# the relabeling on boot would cause upcopies in the overlay.
726-
if pkg_use_enabled coreos-base/coreos selinux; then
727+
if is_selinux_enabled "${BOARD}"; then
727728
# -D - set or update any directory SHA1 digests
728729
# -E - treat conflicting specifications as errors
729730
# -F - force reset of context to match file_context

build_library/pkg_util.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (c) 2025 The Flatcar Maintainers. All rights reserved.
2+
# Use of this source code is governed by the Apache 2.0 license.
3+
4+
# check if any of the given use flags are enabled for a pkg
5+
pkg_use_enabled() {
6+
local board=${1}; shift
7+
local pkg=${1}; shift
8+
9+
# for every flag argument, turn it into a regexp that matches it as
10+
# either '+${flag}' or '(+${flag})'
11+
local -a grep_args=()
12+
local flag
13+
for flag; do
14+
grep_args+=( -e '^(\?+'"${flag}"')\?$' )
15+
done
16+
local -i rv=0
17+
local equery='equery'
18+
if [[ -n ${board} ]]; then
19+
equery+="-${board}"
20+
fi
21+
22+
"${equery}" --quiet uses --forced-masked "${pkg}" | grep --quiet "${grep_args[@]}" || rv=$?
23+
return ${rv}
24+
}
25+
26+
is_selinux_enabled() {
27+
local board=${1}; shift
28+
29+
pkg_use_enabled "${board}" coreos-base/coreos selinux
30+
}

build_library/prod_image_util.sh

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Use of this source code is governed by a BSD-style license that can be
44
# found in the LICENSE file.
55

6+
source "${BUILD_LIBRARY_DIR}/pkg_util.sh" || exit 1
7+
68
# Lookup the current version of a binary package, downloading it if needed.
79
# Usage: get_binary_pkg some-pkg/name
810
# Prints: some-pkg/name-1.2.3
@@ -214,6 +216,14 @@ create_prod_tar() {
214216
create_prod_sysexts() {
215217
local image_name="$1"
216218
local image_sysext_base="${image_name%.bin}_sysext.squashfs"
219+
local -a extra_args
220+
221+
local selinux=''
222+
if is_selinux_enabled "${BOARD}"; then
223+
selinux=x
224+
fi
225+
226+
local sysext
217227
for sysext in "${EXTRA_SYSEXTS[@]}"; do
218228
local name pkgs useflags arches
219229
IFS="|" read -r name pkgs useflags arches <<< "$sysext"
@@ -222,9 +232,13 @@ create_prod_sysexts() {
222232
local arch_array=(${arches//,/ })
223233
local useflags_array=(${useflags//,/ })
224234

235+
extra_args=()
225236
local mangle_script="${BUILD_LIBRARY_DIR}/sysext_mangle_${name}"
226-
if [[ ! -x "${mangle_script}" ]]; then
227-
mangle_script=
237+
if [[ -x "${mangle_script}" ]]; then
238+
extra_args+=( --manglefs_script="${mangle_script}" )
239+
fi
240+
if [[ -n ${selinux} ]]; then
241+
extra_args+=( --selinux )
228242
fi
229243

230244
if [[ -n "$arches" ]]; then
@@ -240,8 +254,8 @@ create_prod_sysexts() {
240254
fi
241255

242256
sudo rm -f "${BUILD_DIR}/${name}.raw" \
243-
"${BUILD_DIR}/flatcar-test-update-${name}.gz" \
244-
"${BUILD_DIR}/${name}_*"
257+
"${BUILD_DIR}/flatcar-test-update-${name}.gz" \
258+
"${BUILD_DIR}/${name}_*"
245259
# we use -E to pass the USE flags, but also MODULES_SIGN variables
246260
#
247261
# The --install_root_basename="${name}-extra-sysext-rootfs" flag
@@ -252,8 +266,8 @@ create_prod_sysexts() {
252266
--squashfs_base="${BUILD_DIR}/${image_sysext_base}" \
253267
--image_builddir="${BUILD_DIR}" \
254268
--install_root_basename="${name}-extra-sysext-rootfs" \
255-
${mangle_script:+--manglefs_script=${mangle_script}} \
256269
--forbidden_packages='sec-policy/selinux-.*;selinux policy packages must be in base image' \
270+
"${extra_args[@]}" \
257271
"${name}" "${pkg_array[@]}"
258272
delta_generator \
259273
-private_key "/usr/share/update_engine/update-payload-key.key.pem" \

build_library/sysext_prod_builder

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ assert_inside_chroot
1616
switch_to_strict_mode
1717

1818
. "${BUILD_LIBRARY_DIR}/build_image_util.sh" || exit 1
19+
. "${BUILD_LIBRARY_DIR}/pkg_util.sh" || exit 1
1920

2021
# Create a sysext from a package and install it to the OS image.
2122
# Conventions:
@@ -54,6 +55,13 @@ create_prod_sysext() {
5455
msg="${msg}, FS mangle script 'sysext_mangle_${name}'"
5556
fi
5657

58+
if is_selinux_enabled "${BOARD}"; then
59+
build_sysext_opts+=(
60+
--selinux
61+
--forbidden_packages='sec-policy/selinux-.*;selinux policy packages must be in base image'
62+
)
63+
fi
64+
5765
info "${msg}."
5866

5967
# Pass the build ID extracted from root FS to build_sysext. This prevents common.sh
@@ -63,13 +71,20 @@ create_prod_sysext() {
6371
# The --install_root_basename="${name}-base-sysext-rootfs" flag is
6472
# important - it sets the name of a rootfs directory, which is used
6573
# to determine the package target in coreos/base/profile.bashrc
74+
echo sudo "FLATCAR_BUILD_ID=$FLATCAR_BUILD_ID" "${SCRIPTS_DIR}/build_sysext" \
75+
--board="${BOARD}" \
76+
--image_builddir="${workdir}/sysext-build" \
77+
--squashfs_base="${base_sysext}" \
78+
--generate_pkginfo \
79+
--install_root_basename="${name}-base-sysext-rootfs" \
80+
"${build_sysext_opts[@]}" \
81+
"${name}" "${grp_pkg[@]}"
6682
sudo "FLATCAR_BUILD_ID=$FLATCAR_BUILD_ID" "${SCRIPTS_DIR}/build_sysext" \
6783
--board="${BOARD}" \
6884
--image_builddir="${workdir}/sysext-build" \
6985
--squashfs_base="${base_sysext}" \
7086
--generate_pkginfo \
7187
--install_root_basename="${name}-base-sysext-rootfs" \
72-
--forbidden_packages='sec-policy/selinux-.*;selinux policy packages must be in base image' \
7388
"${build_sysext_opts[@]}" \
7489
"${name}" "${grp_pkg[@]}"
7590

build_library/vm_image_util.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# Format options. Each variable uses the form IMG_<type>_<opt>.
66
# Default values use the format IMG_DEFAULT_<opt>.
77

8+
. "${BUILD_LIBRARY_DIR}/pkg_util.sh" || exit 1
9+
810
VALID_IMG_TYPES=(
911
akamai
1012
ami
@@ -582,6 +584,9 @@ install_oem_sysext() {
582584
--install_root_basename="${VM_IMG_TYPE}-oem-sysext-rootfs"
583585
--forbidden_packages='sec-policy/selinux-.*;selinux policy packages must be in base image' \
584586
)
587+
if is_selinux_enabled "${BOARD}"; then
588+
build_sysext_flags+=( --selinux )
589+
fi
585590
local overlay_path mangle_fs
586591
overlay_path=$(portageq get_repo_path / coreos-overlay)
587592
mangle_fs="${overlay_path}/${metapkg}/files/manglefs.sh"

build_sysext

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ DEFINE_string install_root_basename "${default_install_root_basename}" \
4545
"Name of a root directory where packages will be installed. ${default_install_root_basename@Q} by default."
4646
DEFINE_string forbidden_packages "" \
4747
"Comma-separated list of pairs describing packages that are forbidden in the sysext. Every pair consist of regexp and message, separated with semicolon. The regexp is for matching a package name (<category>/<name>-<version>::<repo>), and message is printed if the regexp matched a package name. Be careful to not include commas in the regexp or message."
48+
DEFINE_boolean selinux "${FLAGS_FALSE}" \
49+
"Relabel the files in sysext using policies installed in the base squashfs image."
4850

4951
FLAGS_HELP="USAGE: build_sysext [flags] <sysext_name> <binary_package> [<binary_package> ...]
5052
@@ -154,6 +156,8 @@ cleanup() {
154156
"${THE_INSTALL_ROOT}"
155157
"${BUILD_DIR}/workdir"
156158
"${BUILD_DIR}/img-rootfs"
159+
"${BUILD_DIR}/selinux-root"
160+
"${BUILD_DIR}/selinux-root-workdir"
157161
)
158162
umount "${dirs[@]}" 2>/dev/null || true
159163
rm -rf "${dirs[@]}" || true
@@ -253,6 +257,13 @@ export SOURCE_DATE_EPOCH=$(stat -c '%Y' "${BUILD_DIR}/fs-root/usr/lib/os-release
253257
# Unmount in order to get rid of the overlay, but keep fs-root for
254258
# now, so we can use selinux file contexts.
255259
umount "${THE_INSTALL_ROOT}"
260+
if [[ ${FLAGS_selinux} = "${FLAGS_TRUE}" ]]; then
261+
mkdir "${BUILD_DIR}/selinux-root"
262+
mkdir "${BUILD_DIR}/selinux-root-workdir"
263+
mount -t overlay overlay -o lowerdir="${BUILD_DIR}/fs-root${pkginfo_lowerdirs}",upperdir="${BUILD_DIR}/selinux-root",workdir="${BUILD_DIR}/selinux-root-workdir" "${BUILD_DIR}/selinux-root"
264+
else
265+
umount "${BUILD_DIR}/fs-root"
266+
fi
256267

257268
if [[ "$FLAGS_generate_pkginfo" = "${FLAGS_TRUE}" ]] ; then
258269
info " Creating pkginfo squashfs '${BUILD_DIR}/${SYSEXTNAME}_pkginfo.raw'"
@@ -266,27 +277,29 @@ info "Writing ${SYSEXTNAME}_packages.txt"
266277
ROOT="${THE_INSTALL_ROOT}" PORTAGE_CONFIGROOT="${THE_INSTALL_ROOT}" \
267278
equery --no-color list --format '$cpv::$repo' '*' > "${BUILD_DIR}/${SYSEXTNAME}_packages.txt"
268279

269-
# Check if there are forbidden packages
270-
mapfile -t pairs <<<"${FLAGS_forbidden_packages//,/$'\n'}"
271-
declare -A re_msg_pairs=()
272-
for pair in "${pairs[@]}"; do
273-
re=${pair%%;*}
274-
msg=${pair#.;}
275-
re_msg_pairs["${re}"]="${msg}"
276-
done
280+
if [[ -n ${FLAGS_forbidden_packages} ]]; then
281+
# Check if there are forbidden packages
282+
mapfile -t pairs <<<"${FLAGS_forbidden_packages//,/$'\n'}"
283+
declare -A re_msg_pairs=()
284+
for pair in "${pairs[@]}"; do
285+
re=${pair%%;*}
286+
msg=${pair#.;}
287+
re_msg_pairs["${re}"]="${msg}"
288+
done
277289

278-
mapfile -t pkgs <"${BUILD_DIR}/${SYSEXTNAME}_packages.txt"
279-
has_forbidden_pkg=
280-
for pkg in "${pkgs[@]}"; do
281-
for re in "${!re_msg_pairs[@]}"; do
282-
if [[ ${pkg} =~ ${re} ]]; then
283-
has_forbidden_pkg=x
284-
error "Forbidden package ${pkg}: ${msg}"
285-
fi
290+
mapfile -t pkgs <"${BUILD_DIR}/${SYSEXTNAME}_packages.txt"
291+
has_forbidden_pkg=
292+
for pkg in "${pkgs[@]}"; do
293+
for re in "${!re_msg_pairs[@]}"; do
294+
if [[ ${pkg} =~ ${re} ]]; then
295+
has_forbidden_pkg=x
296+
error "Forbidden package ${pkg}: ${msg}"
297+
fi
298+
done
286299
done
287-
done
288-
if [[ -n ${has_forbidden_pkg} ]]; then
289-
die "Forbidden packages encountered"
300+
if [[ -n ${has_forbidden_pkg} ]]; then
301+
die "Forbidden packages encountered"
302+
fi
290303
fi
291304

292305
if [[ "${FLAGS_strip_binaries}" = "${FLAGS_TRUE}" ]]; then
@@ -337,9 +350,21 @@ if [[ -n "${invalid_files}" ]]; then
337350
die "Invalid file ownership: ${invalid_files}"
338351
fi
339352

340-
info "Relabeling sysext contents"
341-
setfiles -D -E -F -r "${THE_INSTALL_ROOT}" -v -T 0 "${BUILD_DIR}/fs-root/usr/share/flatcar/etc/selinux/mcs/contexts/files/file_contexts" "${THE_INSTALL_ROOT}"
342-
umount "${BUILD_DIR}/fs-root"
353+
if [[ ${FLAGS_selinux} = "${FLAGS_TRUE}" ]]; then
354+
info "Build temporary selinux modules"
355+
chroot "${BUILD_DIR}/selinux-root" bash -s <<'EOF'
356+
cd /usr/share/selinux/mcs
357+
set -x
358+
semodule -s mcs -n -i *.pp
359+
EOF
360+
361+
info "Relabeling sysext contents"
362+
spec_file="${BUILD_DIR}/selinux-root/etc/selinux/mcs/contexts/files/file_contexts"
363+
setfiles -D -E -F -r "${THE_INSTALL_ROOT}" -v -T 0 "${spec_file}" "${THE_INSTALL_ROOT}"
364+
ls -laRZ "${THE_INSTALL_ROOT}"
365+
umount "${BUILD_DIR}/selinux-root"
366+
umount "${BUILD_DIR}/fs-root"
367+
fi
343368

344369
info "Creating squashfs image"
345370
mksquashfs "${THE_INSTALL_ROOT}" "${BUILD_DIR}/${SYSEXTNAME}.raw" \

0 commit comments

Comments
 (0)