Skip to content

Commit 72af9cb

Browse files
committed
Load the SELinux policy after switch_root. This fixes
the bootup process with recent kernels, as it was getting stuck on Permission Denied errors due to the early SELinux policy load. Signed-off-by: Guido Trentalancia <[email protected]> --- .github/labeler.yml | 4 - modules.d/98selinux/module-setup.sh | 17 ------- modules.d/98selinux/selinux-loadpolicy.sh | 70 ------------------------------ modules.d/99base/init.sh | 61 ++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 91 deletions(-)
1 parent 5d2bda4 commit 72af9cb

File tree

4 files changed

+61
-91
lines changed

4 files changed

+61
-91
lines changed

.github/labeler.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,6 @@ pollcdrom:
498498
- changed-files:
499499
- any-glob-to-any-file: 'modules.d/98pollcdrom/*'
500500

501-
selinux:
502-
- changed-files:
503-
- any-glob-to-any-file: 'modules.d/98selinux/*'
504-
505501
syslog:
506502
- changed-files:
507503
- any-glob-to-any-file: 'modules.d/98syslog/*'

modules.d/98selinux/module-setup.sh

Lines changed: 0 additions & 17 deletions
This file was deleted.

modules.d/98selinux/selinux-loadpolicy.sh

Lines changed: 0 additions & 70 deletions
This file was deleted.

modules.d/99base/init.sh

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Copyright 2008-2010, Red Hat, Inc.
66
# Harald Hoyer <[email protected]>
77
# Jeremy Katz <[email protected]>
8+
# Copyright 2024 Guido Trentalancia <[email protected]>
89

910
export -p > /tmp/export.orig
1011

@@ -397,3 +398,63 @@ else
397398
emergency_shell
398399
}
399400
fi
401+
402+
# If SELinux is disabled exit now
403+
getarg "selinux=0" > /dev/null && return 0
404+
405+
SELINUX="enforcing"
406+
# shellcheck disable=SC1090
407+
[ -e "/etc/selinux/config" ] && . "/etc/selinux/config"
408+
409+
# Check whether SELinux is in permissive mode
410+
permissive=0
411+
412+
if getarg "enforcing=0" > /dev/null || [ "$SELINUX" = "permissive" ]; then
413+
permissive=1
414+
fi
415+
416+
# Finally load the SELinux policy and perform relabeling if needed
417+
if [ -x "/sbin/load_policy" ] || [ -x "/usr/sbin/load_policy" ]; then
418+
local ret=0
419+
local out
420+
info "Loading SELinux policy"
421+
422+
if [ -x "/sbin/load_policy" ]; then
423+
out=$(LANG=C /sbin/load_policy -i 2>&1)
424+
ret=$?
425+
info "$out"
426+
else
427+
out=$(LANG=C /usr/sbin/load_policy -i 2>&1)
428+
ret=$?
429+
info "$out"
430+
fi
431+
umount /sys/fs/selinux
432+
433+
if [ "$SELINUX" = "disabled" ]; then
434+
return 0
435+
fi
436+
437+
if [ $ret -eq 0 ] || [ $ret -eq 2 ]; then
438+
# If machine requires a relabel, force to permissive mode
439+
[ -e "/.autorelabel" ] && LANG=C /usr/sbin/setenforce 0
440+
mount --rbind /dev "/dev"
441+
LANG=C /sbin/restorecon -R /dev
442+
umount -R "/dev"
443+
return 0
444+
fi
445+
446+
warn "Initial SELinux policy load failed."
447+
if [ $ret -eq 3 ] || [ $permissive -eq 0 ]; then
448+
warn "Machine in enforcing mode."
449+
warn "Not continuing"
450+
emergency_shell -n selinux
451+
exit 1
452+
fi
453+
return 0
454+
elif [ $permissive -eq 0 ] && [ "$SELINUX" != "disabled" ]; then
455+
warn "Machine in enforcing mode and cannot execute load_policy."
456+
warn "To disable selinux, add selinux=0 to the kernel command line."
457+
warn "Not continuing"
458+
emergency_shell -n selinux
459+
exit 1
460+
fi

0 commit comments

Comments
 (0)