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

init: optionally load the system SELinux policy #400

Open
wants to merge 69 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 59 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
69d80f4
init: optionally load the system SELinux policy
WavyEbuilder Oct 15, 2024
0a5f876
mark selinux_transition as static
WavyEbuilder Oct 19, 2024
c465b81
BUILD_MESON: add selinux option documentation
WavyEbuilder Oct 19, 2024
6063686
init: selinux: add --disable-selinux
WavyEbuilder Oct 19, 2024
57b94a2
doc: manpages: mention --disable-selinux flag
WavyEbuilder Oct 19, 2024
6910b6d
init: selinux: fix header guards
WavyEbuilder Oct 19, 2024
67c3d8b
build: docs: add SUPPORT_SELINUX info
WavyEbuilder Oct 23, 2024
55b82d8
init: rename --disable-selinux option to --disable-selinux-policy
WavyEbuilder Oct 23, 2024
15e2f6e
selinux: add explanatory comments for selinux related functions
WavyEbuilder Oct 26, 2024
ca63b57
configure: update help text for selinux options
WavyEbuilder Oct 26, 2024
159ffac
printVersion: add selinux information to output
WavyEbuilder Oct 26, 2024
0e959a7
selinux: update comments
WavyEbuilder Oct 27, 2024
a40f43a
meson.build: clean up selinux related options
WavyEbuilder Oct 27, 2024
7d88201
meson.build: rename libselinux dependency to libselinux_dep
WavyEbuilder Oct 27, 2024
157a78a
meson: refractor mconfig logic for selinux
WavyEbuilder Oct 27, 2024
01640d8
configure: update --enable-selinux help text
WavyEbuilder Oct 31, 2024
a8ecd7d
doc: manpages: update --disable-selinux flag to --disable-selinux-policy
WavyEbuilder Oct 31, 2024
10c8198
dinit.cc: fix line wrapping for comments per CODE-STYLE
WavyEbuilder Oct 31, 2024
e4b5b3e
dinit.cc: selinux_transition: clarify comment regarding the log
WavyEbuilder Oct 31, 2024
02b93a8
selinux_transition: clean up comment per CODE-STYLE
WavyEbuilder Nov 2, 2024
66be73d
selinux_transition: log error and return early for permissive
WavyEbuilder Nov 2, 2024
e06e054
selinux_transition: check that getcon_raw(3) doesn't return nullptr
WavyEbuilder Nov 8, 2024
4bf712a
selinux_transition: clarify kernel context in comment
WavyEbuilder Nov 8, 2024
eadc90c
selinux_transition: be more specific in comment
WavyEbuilder Nov 8, 2024
86a9f0c
selinux_transition: fix line wrapping for comments
WavyEbuilder Nov 8, 2024
8737eef
selinux_transition: correct check for getcon_raw(3) return value
WavyEbuilder Nov 11, 2024
f093426
selinux_transition: don't exit if we are unable to transition
WavyEbuilder Nov 11, 2024
e40b38e
selinux_transition: always error exit if we fail to load the policy
WavyEbuilder Nov 11, 2024
a6af309
selinux_transition: update comments to reflect the current control flow
WavyEbuilder Nov 11, 2024
25eb167
selinux_transition: fix grammar in comment
WavyEbuilder Nov 11, 2024
d90b013
selinux_transition: improve wording for comment
WavyEbuilder Nov 11, 2024
1f2f7cf
selinux_transition: document the mounting of /sys
WavyEbuilder Nov 11, 2024
ab15586
selinux_transition: reformat if statement per CODE-STYLE
WavyEbuilder Nov 11, 2024
90b789b
selinux_transition: clarify policy choice for inital domain in comment
WavyEbuilder Nov 11, 2024
c0cef53
selinux_transition: reword comment about pitfalls of getcon_raw(3)
WavyEbuilder Dec 18, 2024
2a6af9d
selinux_transition: cleanroom rewrite of getcon_raw(3) comment
WavyEbuilder Dec 18, 2024
ef2c41f
selinux_transition: use correct variable naming in comments
WavyEbuilder Dec 18, 2024
8f1ac95
selinux_transition: add attribution to getcon_raw(3) comment
WavyEbuilder Dec 18, 2024
64ec986
selinux: new documentation
WavyEbuilder Dec 18, 2024
266cc8c
selinux: correct flowchart in documentation
WavyEbuilder Dec 18, 2024
cec70a6
selinux_transition: fix formatting by placing opening { on a new line
WavyEbuilder Dec 18, 2024
4774421
selinux: document flag to disable policy loading
WavyEbuilder Dec 18, 2024
38fdef4
selinux: document mounting of /sys
WavyEbuilder Dec 18, 2024
b7ef63a
Merge branch 'master' into master
WavyEbuilder Dec 18, 2024
7d5ac36
meson.build: fix dependencies for libselinux_dep
WavyEbuilder Dec 18, 2024
c502a07
dinit.cc: add missing #endif directive for SUPPORT_SELINUX #ifdef
WavyEbuilder Dec 18, 2024
6f3f5cd
meson.build: remove old cgroup-related changes
WavyEbuilder Dec 18, 2024
4a5d7c1
meson.build: force SUPPORT_SELINUX mconfig variable for support-selinux
WavyEbuilder Dec 18, 2024
350fed8
configure: remove deprecated arguments
WavyEbuilder Dec 18, 2024
376c1cc
configure: fix to align with current upstream/master
WavyEbuilder Dec 18, 2024
5d143f4
feature_count: bump on SUPPORT_SELINUX
WavyEbuilder Dec 18, 2024
cc8bcb7
selinux_transition: add base for mounting /proc
WavyEbuilder Dec 18, 2024
653ecf4
selinux_transition: don't use type inference for errno_str
WavyEbuilder Dec 18, 2024
46ac998
selinux_transition: remove TODO for relabeling /proc
WavyEbuilder Jan 10, 2025
1a79e2f
doc: add SELinux related support to manpages
WavyEbuilder Jan 10, 2025
46331bf
doc: update SELinux flowchart to include mounting of /proc
WavyEbuilder Jan 10, 2025
26c1855
configure: sync to latest in davmac314/dinit
WavyEbuilder Jan 10, 2025
c2cf2f9
dinit: fix formatting in manpages
WavyEbuilder Jan 17, 2025
f360aff
manpages: fix typo
WavyEbuilder Jan 17, 2025
9260c20
docs: SELinux: refer to Dinit the project with a capital D
WavyEbuilder Jan 31, 2025
0f114a6
docs: SELinux: link to SELinux notebook
WavyEbuilder Jan 31, 2025
c5b6aae
docs: SELinux: link to build documentation
WavyEbuilder Jan 31, 2025
6be8175
selinux_transition: update comment wording to note relevant manpage
WavyEbuilder Jan 31, 2025
a3062d7
selinux_transition: mention SELinux in log output
WavyEbuilder Jan 31, 2025
3b674b3
selinux_transition: fix formatting
WavyEbuilder Jan 31, 2025
7e52501
selinux_transition: only bail early if enforcing mode requested
WavyEbuilder Jan 31, 2025
ed2a166
selinux_transition: stop attempting to create /proc if it doesn't exist
WavyEbuilder Feb 1, 2025
7a843a0
selinux_transition: clarify mounting behaviour in comment
WavyEbuilder Feb 1, 2025
622790e
[EXPERIMENTAL] dinit: use the log for selinux error messages
WavyEbuilder Feb 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ DEFAULT_STOP_TIMEOUT=XXX
this, its process group is sent a SIGKILL signal which should cause it to terminate immediately.
The default if unspecified is 10 seconds. (The value can be overridden for individual services
via the service description).
SUPPORT_SELINUX=1|0
Whether to build support for loading the system SELinux policy at boot.
SUPPORT_CGROUPS=1|0
Whether to include support for cgroups (Linux only).
SUPPORT_CAPABILITIES=1|0
Expand Down
4 changes: 4 additions & 0 deletions BUILD_MESON
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ Custom options:
build-shutdown : Whether to build the shutdown/reboot/halt utilities.
Available values : enabled, disabled, auto
Default value : auto

support-selinux : Enable SELinux support.
Available values : enabled, disabled, auto
Default value : auto


Running the test suite
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ The following people (in alphabetical order) have contributed:
* Oliver Amann - Code, testing, documentation
* Locria Cyber - Code, documentation
* q66 - Code, testing, documentation.
* Rahul Sandhu - Code
3 changes: 2 additions & 1 deletion build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ includes/mconfig.h: ../mconfig tools/mconfig-gen.cc version.conf
$(if $(SUPPORT_IOPRIO),SUPPORT_IOPRIO=$(SUPPORT_IOPRIO),) \
$(if $(SUPPORT_OOM_ADJ),SUPPORT_OOM_ADJ=$(SUPPORT_OOM_ADJ),) \
$(if $(USE_UTMPX),USE_UTMPX=$(USE_UTMPX),) \
$(if $(USE_INITGROUPS),USE_INITGROUPS=$(USE_INITGROUPS),) > includes/mconfig.h
$(if $(USE_INITGROUPS),USE_INITGROUPS=$(USE_INITGROUPS),) \
$(if $(SUPPORT_SELINUX),SUPPORT_SELINUX=$(SUPPORT_SELINUX),) > includes/mconfig.h

clean:
rm -f includes/mconfig.h
Expand Down
1 change: 1 addition & 0 deletions build/mconfig.mesontemplate
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#mesondefine USE_UTMPX
#mesondefine USE_INITGROUPS
#mesondefine SUPPORT_CGROUPS
#mesondefine SUPPORT_SELINUX
#mesondefine SUPPORT_CAPABILITIES
#mesondefine SUPPORT_IOPRIO
#mesondefine SUPPORT_OOM_ADJ
Expand Down
3 changes: 3 additions & 0 deletions build/tools/mconfig-gen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ int main(int argc, char **argv)
if (vars.find("DEFAULT_AUTO_RESTART") != vars.end()) {
cout << "#define DEFAULT_AUTO_RESTART " << vars["DEFAULT_AUTO_RESTART"] << "\n";
}
if (vars.find("SUPPORT_SELINUX") != vars.end()) {
cout << "#define SUPPORT_SELINUX " << vars["SUPPORT_SELINUX"] << "\n";
}

cout << "\n// Constants\n";
cout << "\nconstexpr static char DINIT_VERSION[] = " << stringify(vars["VERSION"]) << ";\n";
Expand Down
10 changes: 10 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ Optional options:
--enable-initgroups Enable initialization of supplementary groups for run-as
[Enabled]
--disable-initgroups Disable initialization of supplementary groups for run-as
--enable-selinux Enable SELinux support [Disabled]
--disable-selinux Disable SELinux support
--enable-auto-restart Enable auto-restart for services by default (Deprecated;
use --default-auto-restart=...)
--disable-auto-restart Disable auto-restart for services by default (Deprecated;
Expand Down Expand Up @@ -283,6 +285,7 @@ for var in PREFIX \
SUPPORT_OOM_ADJ \
USE_UTMPX \
USE_INITGROUPS \
SUPPORT_SELINUX \
SYSCONTROLSOCKET \
STRIPOPTS
do
Expand Down Expand Up @@ -324,6 +327,8 @@ for arg in "$@"; do
--disable-initgroups|--enable-initgroups=no) USE_INITGROUPS=0 ;;
--enable-auto-restart|--enable-auto-restart=yes) DEFAULT_AUTO_RESTART=ALWAYS ;; # Deprecated
--disable-auto-restart|--enable-auto-restart=no) DEFAULT_AUTO_RESTART=NEVER ;; # Deprecated
--enable-selinux|--enable-selinux=yes) SUPPORT_SELINUX=1 ;;
--disable-selinux|--enable-selinux=no) SUPPORT_SELINUX=0 ;;
--enable-strip|--enable-strip=yes) STRIPOPTS="-s" ;;
--disable-strip|--enable-strip=no) STRIPOPTS="" ;;
--default-auto-restart=never) DEFAULT_AUTO_RESTART=NEVER ;;
Expand Down Expand Up @@ -355,6 +360,7 @@ done
: "${DEFAULT_START_TIMEOUT:="60"}"
: "${DEFAULT_STOP_TIMEOUT:="10"}"
: "${USE_INITGROUPS:="1"}"
: "${SUPPORT_SELINUX:="0"}"
if [ "$PLATFORM" = "Linux" ]; then
: "${BUILD_SHUTDOWN:="yes"}"
: "${SUPPORT_CGROUPS:="1"}"
Expand Down Expand Up @@ -477,6 +483,9 @@ fi
if [ "$AUTO_LDFLAGS_BASE" = true ] && [ "$PLATFORM" = FreeBSD ]; then
try_ld_argument LDFLAGS_BASE -lrt
fi
if [ "$AUTO_LDFLAGS_BASE" = true ] && [ "$SUPPORT_SELINUX" = "1" ]; then
try_ld_argument LDFLAGS_BASE -lselinux
fi
if [ "$SUPPORT_CAPABILITIES" != 0 ]; then
if [ "$AUTO_LDFLAGS_LIBCAP" = true ]; then
try_ld_argument LDFLAGS_LIBCAP -lcap
Expand Down Expand Up @@ -587,6 +596,7 @@ LDFLAGS_LIBCAP=$LDFLAGS_LIBCAP
# Feature settings
SUPPORT_CGROUPS=$SUPPORT_CGROUPS
USE_INITGROUPS=$USE_INITGROUPS
SUPPORT_SELINUX=$SUPPORT_SELINUX
SUPPORT_CAPABILITIES=$SUPPORT_CAPABILITIES
SUPPORT_IOPRIO=$SUPPORT_IOPRIO
SUPPORT_OOM_ADJ=$SUPPORT_OOM_ADJ
Expand Down
46 changes: 46 additions & 0 deletions doc/linux/SELINUX.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Dinit SELinux Awareness

Dinit has support for basic SELinux awareness. This document is intended to
outline the extent and inner workings of dinit's SELinux awareness. The reader
WavyEbuilder marked this conversation as resolved.
Show resolved Hide resolved
is assumed to be knowledgeable about the basics of SELinux and dinit.
Copy link
Owner

Choose a reason for hiding this comment

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

Where is suitable information/documentation about SELinux available?

Copy link
Author

Choose a reason for hiding this comment

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

I have linked to it in 0f114a6, and I haven't wrapped the raw line at 100 characters as to me that seems unnatural for rendered markdown (which still wraps at 100 here), however I'm happy to change that if you wish


Dinit needs to be built with SELinux support to enable any of the features that
WavyEbuilder marked this conversation as resolved.
Show resolved Hide resolved
are mentioned in this document.

## Loading the system SELinux policy
When booted as the system init system, dinit by default will attempt to load the
system's SELinux policy and transition itself to a context specified by that policy
if not already done so in earlier boot (e.g. by an initramfs). This behaviour may be
disabled by passing dinit the `--disable-selinux-policy` flag. As dinit will always
be PID1 in this senario, this can be done by appending the flag to the kernel cmdline.

If not already mounted in earlier boot (e.g. by an initramfs), dinit will mount `/sys`,
and selinuxfs (typically `/sys/fs/selinux`) during the call to `selinux_init_load_policy(3)`.

The following flowchart provides an overview of the process of loading the policy:
```mermaid
flowchart TD
A[Start] --> B{"Is dinit running as the init system (PID1)?"}
B -->|Yes| C{Have we been requested to not load the SELinux policy?}
B -->|No| D[Continue rest of dinit initialization]
C -->|Yes| D
C -->|No| E[Is the SELinux policy already loaded?]
E -->|Yes| D
E --> |No| F{Is /proc mounted?}
F --> |Yes| J
F --> |No| G[Attempt to mount /proc]
G --> H{Could we successfully mount /proc?}
H --> |Yes| J
H -->|No| I[Error exit early]
J[Attempt to load the SELinux policy]
J --> K{Did the SELinux policy load succeed?}
K -->|Yes| L[Attempt to calculate our new context and transition]
K -->|No| I
L --> M{Did we successfully transition?}
M -->|Yes| O{Did we mount /proc?}
M -->|No| N[Log an error to stderr]
N --> O
O -->|Yes| P[Unmount /proc]
O -->|No| D
P --> D
```
14 changes: 14 additions & 0 deletions doc/manpages/dinit.8.m4
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ If service description settings contain relative cgroup paths, they will be reso
this path.
This option is only available if \fBdinit\fR is built with cgroups support.
.TP
\fB\-\-disable\-selinux\-policy\fR
Disable loading of the system SELinux policy.
This option is only available if \fBdinit\fR is built with SELinux support.
.TP
\fB\-\-help\fR
Display brief help text and then exit.
.TP
Expand Down Expand Up @@ -298,6 +302,16 @@ There are several ways to work around this.
Service names following the \fB\-\-container\fR (\fB\-o\fR) or \fB\-\-system\-mgr\fR (\fB\-m\fR) options are not ignored.
Also, the \fB\-\-service\fR (\fB\-t\fR) option can be used to force a service name to be recognised regardless of operating mode.
.\"
.SH SELINUX SUPPORT
.LP
When running as PID 1 on a SELinux enabled machine, \fBdinit\fR will by default load the system's SELinux policy.
This behaviour can be disabled by passing \fB\-\-disable\-selinux\-policy\fR to dinit through the kernel cmdline.
.LP
When loading the SELinux policy, dinit will automatically mount a few special filesystems needed to successfully load the policy.
\fBsysfs\fR will be mounted at \fB/sys\fR, and \fBselinuxfs\fR will be mounted at \fB/sys/fs/selinux\fR.
\fBdinit\fR will not unmount either.
\fBprocfs\fR will also be mounted at \fB/proc\fR, but \fBdinit\fR will unmount it when done with it.
.\"
.SH FILES
.\"
.TP
Expand Down
3 changes: 3 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ support_ioprio = get_option('support-ioprio')
support_oom_adj = get_option('support-oom-adj')
use_utmpx = get_option('use-utmpx')
use_initgroups = get_option('use-initgroups')
support_selinux = get_option('support-selinux')
default_auto_restart = get_option('default-auto-restart')
default_start_timeout = get_option('default-start-timeout').to_string()
default_stop_timeout = get_option('default-stop-timeout').to_string()
Expand All @@ -61,6 +62,7 @@ endif

## Dependencies
libcap_dep = dependency('libcap', required: support_capabilities)
libselinux_dep = dependency('libselinux', version : '>= 2.1.9', required : support_selinux)

## Prepare mconfig.h
mconfig_data.set_quoted('DINIT_VERSION', version)
Expand All @@ -71,6 +73,7 @@ mconfig_data.set('DEFAULT_AUTO_RESTART', default_auto_restart)
mconfig_data.set('DEFAULT_START_TIMEOUT', default_start_timeout)
mconfig_data.set('DEFAULT_STOP_TIMEOUT', default_stop_timeout)
mconfig_data.set10('USE_INITGROUPS', use_initgroups)
mconfig_data.set10('SUPPORT_SELINUX', libselinux_dep.found() or support_selinux.enabled())
mconfig_data.set10('SUPPORT_CGROUPS', support_cgroups.auto() and platform == 'linux' or support_cgroups.enabled())
mconfig_data.set10('SUPPORT_CAPABILITIES', libcap_dep.found() and not support_capabilities.disabled())
mconfig_data.set10('SUPPORT_IOPRIO', support_ioprio.auto() and platform == 'linux' or support_ioprio.enabled())
Expand Down
6 changes: 6 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,9 @@ option(
value : 'auto',
description : 'Building shutdown/reboot/soft-reboot/halt or not.'
)
option(
'support-selinux',
type : 'feature',
value : 'auto',
description : 'SELinux support'
)
Loading