Skip to content

Commit f97dd2e

Browse files
committed
workaround selinux issues with osbuild
We have a few issues right now where files in our images don't have any selinux context (i.e. end up unlabeled_t). Here we workaround the hidden mountpoints issue [1] with a patch to OSBuild to hardcode some chcon calls. We workaround the "bunch of files under /sysroot are unlabeled" issue [2] by backported a proposed upstream change to the org.osbuild.selinux stage [3] and then using it to explicitly set the context on the root of the tree to `root_t`. We also add a fix [4] for another issue where '/boot/coreos/platforms.json' would end up with the wrong label. [1] coreos/fedora-coreos-tracker#1771 [2] coreos/fedora-coreos-tracker#1772 [3] osbuild/osbuild#1889 [4] osbuild/osbuild#1888
1 parent dc99d2e commit f97dd2e

8 files changed

+169
-1
lines changed

build.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ patch_osbuild() {
173173
mv /usr/bin/osbuild-mpp /usr/lib/osbuild/tools/
174174

175175
# Now all the software is under the /usr/lib/osbuild dir and we can patch
176-
patch -d /usr/lib/osbuild -p1 < /usr/lib/coreos-assembler/0001-stages-dmverity-make-device-objects-more-generic.patch
176+
cat /usr/lib/coreos-assembler/0001-stages-dmverity-make-device-objects-more-generic.patch \
177+
/usr/lib/coreos-assembler/0001-stages-coreos.platform-use-shutil.copy.patch \
178+
/usr/lib/coreos-assembler/0001-stages-selinux-don-t-require-file_contexts-if-labels.patch \
179+
/usr/lib/coreos-assembler/0001-hacks-for-coreos-selinux-issues.patch \
180+
| patch -d /usr/lib/osbuild -p1
177181

178182
# And then move the files back; supermin appliance creation will need it back
179183
# in the places delivered by the RPM.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
From 9faf7e2566cd9460ac51ff508c192bdc839ad2ef Mon Sep 17 00:00:00 2001
2+
From: Dusty Mabe <[email protected]>
3+
Date: Tue, 17 Sep 2024 12:27:37 -0400
4+
Subject: [PATCH 3/3] hacks for coreos selinux issues
5+
6+
context in https://github.com/coreos/fedora-coreos-tracker/issues/1771#issuecomment-2348607969
7+
---
8+
osbuild/mounts.py | 13 ++++++++++++-
9+
1 file changed, 12 insertions(+), 1 deletion(-)
10+
11+
diff --git a/osbuild/mounts.py b/osbuild/mounts.py
12+
index 42b556ba..9b6c0804 100644
13+
--- a/osbuild/mounts.py
14+
+++ b/osbuild/mounts.py
15+
@@ -178,7 +178,12 @@ class FileSystemMountService(MountService):
16+
17+
options = self.translate_options(options)
18+
19+
- os.makedirs(mountpoint, exist_ok=True)
20+
+ if not os.path.exists(mountpoint):
21+
+ os.makedirs(mountpoint)
22+
+ # Tactical fix for https://github.com/coreos/fedora-coreos-tracker/issues/1771
23+
+ if target == '/boot' or target == "/boot/efi":
24+
+ subprocess.run(["chcon", "-v", "-t", 'boot_t', mountpoint], check=True)
25+
+
26+
self.mountpoint = mountpoint
27+
28+
print(f"mounting {source} -> {mountpoint}")
29+
@@ -198,6 +203,12 @@ class FileSystemMountService(MountService):
30+
msg = e.stdout.strip()
31+
raise RuntimeError(f"{msg} (code: {code})") from e
32+
33+
+ # Tactical fix for https://github.com/coreos/fedora-coreos-tracker/issues/1771
34+
+ # After the mount, let's make sure the lost+found directory has the right label
35+
+ lostfounddir = os.path.join(mountpoint, 'lost+found')
36+
+ if os.path.exists(lostfounddir):
37+
+ subprocess.run(["chcon", "-v", "-t", 'lost_found_t', lostfounddir], check=True)
38+
+
39+
self.check = True
40+
return mountpoint
41+
42+
--
43+
2.46.0
44+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
From 6b48c91e26efb448b2f2121b4179a1b79e15ce6d Mon Sep 17 00:00:00 2001
2+
From: Dusty Mabe <[email protected]>
3+
Date: Tue, 17 Sep 2024 12:18:45 -0400
4+
Subject: [PATCH 1/3] stages/coreos.platform: use shutil.copy
5+
6+
Switch from shutil.copy2 so that we don't copy over the
7+
SELinux labels from the source file.
8+
---
9+
stages/org.osbuild.coreos.platform | 6 ++++--
10+
1 file changed, 4 insertions(+), 2 deletions(-)
11+
12+
diff --git a/stages/org.osbuild.coreos.platform b/stages/org.osbuild.coreos.platform
13+
index a88951cc..7e66c26c 100755
14+
--- a/stages/org.osbuild.coreos.platform
15+
+++ b/stages/org.osbuild.coreos.platform
16+
@@ -52,8 +52,10 @@ def main(paths, options):
17+
json_grub_args, json_kargs = None, None
18+
if os.path.exists(platforms_source_path):
19+
os.makedirs(os.path.dirname(platforms_dest_path), mode=0o755, exist_ok=True)
20+
- # Copy platforms.json to the boot partition
21+
- shutil.copy2(platforms_source_path, platforms_dest_path)
22+
+ # Copy platforms.json to the boot partition. Use shutil.copy here and not
23+
+ # shutil.copy2 because we don't want the selinux labels from the source
24+
+ # to be copied over, but rather the defaults for the destination.
25+
+ shutil.copy(platforms_source_path, platforms_dest_path)
26+
json_grub_args, json_kargs = process_platforms_json(platforms_dest_path, platform)
27+
if json_kargs:
28+
kernel_arguments.extend(json_kargs)
29+
--
30+
2.46.0
31+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
From 281b0795fb4cc43ea05039627ebb5ff7130d70e9 Mon Sep 17 00:00:00 2001
2+
From: Dusty Mabe <[email protected]>
3+
Date: Tue, 17 Sep 2024 12:22:16 -0400
4+
Subject: [PATCH 2/3] stages/selinux: don't require file_contexts if labels
5+
passed
6+
7+
With the labels option the user is specifying the exact context
8+
they want to set on the path so it's not necessary to supply a
9+
context here. This can be also useful in the case where you want
10+
to set some labels and you haven't yet populated the tree yet.
11+
---
12+
stages/org.osbuild.selinux | 11 +++++++----
13+
stages/org.osbuild.selinux.meta.json | 13 +++++++++++--
14+
2 files changed, 18 insertions(+), 6 deletions(-)
15+
16+
diff --git a/stages/org.osbuild.selinux b/stages/org.osbuild.selinux
17+
index bb45298d..563d827b 100755
18+
--- a/stages/org.osbuild.selinux
19+
+++ b/stages/org.osbuild.selinux
20+
@@ -8,11 +8,14 @@ from osbuild.util import selinux
21+
22+
23+
def main(tree, options):
24+
- file_contexts = os.path.join(f"{tree}", options["file_contexts"])
25+
+ file_contexts = options.get("file_contexts")
26+
exclude_paths = options.get("exclude_paths")
27+
- if exclude_paths:
28+
- exclude_paths = [os.path.join(tree, p.lstrip("/")) for p in exclude_paths]
29+
- selinux.setfiles(file_contexts, os.fspath(tree), "", exclude_paths=exclude_paths)
30+
+
31+
+ if file_contexts:
32+
+ file_contexts = os.path.join(f"{tree}", options["file_contexts"])
33+
+ if exclude_paths:
34+
+ exclude_paths = [os.path.join(tree, p.lstrip("/")) for p in exclude_paths]
35+
+ selinux.setfiles(file_contexts, os.fspath(tree), "", exclude_paths=exclude_paths)
36+
37+
labels = options.get("labels", {})
38+
for path, label in labels.items():
39+
diff --git a/stages/org.osbuild.selinux.meta.json b/stages/org.osbuild.selinux.meta.json
40+
index ea1bb3ef..151839e5 100644
41+
--- a/stages/org.osbuild.selinux.meta.json
42+
+++ b/stages/org.osbuild.selinux.meta.json
43+
@@ -20,8 +20,17 @@
44+
"schema_2": {
45+
"options": {
46+
"additionalProperties": false,
47+
- "required": [
48+
- "file_contexts"
49+
+ "oneOf": [
50+
+ {
51+
+ "required": [
52+
+ "file_contexts"
53+
+ ]
54+
+ },
55+
+ {
56+
+ "required": [
57+
+ "labels"
58+
+ ]
59+
+ }
60+
],
61+
"properties": {
62+
"file_contexts": {
63+
--
64+
2.46.0
65+

src/osbuild-manifests/coreos.osbuild.aarch64.mpp.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ pipelines:
139139
mpp-format-string: '{buildroot}'
140140
source-epoch: 1659397331
141141
stages:
142+
# Set the context of the root of the tree so that we avoid unlabeled_t files.
143+
# https://github.com/coreos/fedora-coreos-tracker/issues/1772
144+
- type: org.osbuild.selinux
145+
options:
146+
labels:
147+
/: system_u:object_r:root_t:s0
142148
- type: org.osbuild.ostree.init-fs
143149
- type: org.osbuild.ostree.os-init
144150
options:

src/osbuild-manifests/coreos.osbuild.ppc64le.mpp.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ pipelines:
141141
mpp-format-string: '{buildroot}'
142142
source-epoch: 1659397331
143143
stages:
144+
# Set the context of the root of the tree so that we avoid unlabeled_t files.
145+
# https://github.com/coreos/fedora-coreos-tracker/issues/1772
146+
- type: org.osbuild.selinux
147+
options:
148+
labels:
149+
/: system_u:object_r:root_t:s0
144150
- type: org.osbuild.ostree.init-fs
145151
- type: org.osbuild.ostree.os-init
146152
options:

src/osbuild-manifests/coreos.osbuild.s390x.mpp.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ pipelines:
169169
mpp-format-string: '{buildroot}'
170170
source-epoch: 1659397331
171171
stages:
172+
# Set the context of the root of the tree so that we avoid unlabeled_t files.
173+
# https://github.com/coreos/fedora-coreos-tracker/issues/1772
174+
- type: org.osbuild.selinux
175+
options:
176+
labels:
177+
/: system_u:object_r:root_t:s0
172178
- type: org.osbuild.ostree.init-fs
173179
- type: org.osbuild.ostree.os-init
174180
options:

src/osbuild-manifests/coreos.osbuild.x86_64.mpp.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ pipelines:
141141
mpp-format-string: '{buildroot}'
142142
source-epoch: 1659397331
143143
stages:
144+
# Set the context of the root of the tree so that we avoid unlabeled_t files.
145+
# https://github.com/coreos/fedora-coreos-tracker/issues/1772
146+
- type: org.osbuild.selinux
147+
options:
148+
labels:
149+
/: system_u:object_r:root_t:s0
144150
- type: org.osbuild.ostree.init-fs
145151
- type: org.osbuild.ostree.os-init
146152
options:

0 commit comments

Comments
 (0)