|
| 1 | +From e3454fe7de62f675fcfc3092689803416a457984 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Nikita Dubrovskii < [email protected]> |
| 3 | +Date: Thu, 17 Oct 2024 12:57:00 +0200 |
| 4 | +Subject: [PATCH 3/4] org.osbuild.selinux: support operating on mounts |
| 5 | + |
| 6 | +This adds support for specifying paths to operate on, |
| 7 | +rather than just the root of the target: |
| 8 | +``` |
| 9 | +- type: org.osbuild.selinux |
| 10 | + options: |
| 11 | + file_contexts: etc/selinux/targeted/contexts/files/file_contexts |
| 12 | + target: mount://root/path/to/dir |
| 13 | + mounts: |
| 14 | + - name: root |
| 15 | + source: disk |
| 16 | + target: / |
| 17 | +``` |
| 18 | + |
| 19 | +or |
| 20 | + |
| 21 | +``` |
| 22 | +- type: org.osbuild.selinux |
| 23 | + options: |
| 24 | + labels: |
| 25 | + mount://root/path/to/file: system_u:object_r:boot_t:s0 |
| 26 | + mount://root/path/to/other/file: system_u:object_r:var_t:s0 |
| 27 | + mounts: |
| 28 | + - name: root |
| 29 | + source: disk |
| 30 | + target: / |
| 31 | + |
| 32 | +``` |
| 33 | +--- |
| 34 | + stages/org.osbuild.selinux | 21 ++++++++++++--------- |
| 35 | + stages/org.osbuild.selinux.meta.json | 17 ++++++++++++++++- |
| 36 | + 2 files changed, 28 insertions(+), 10 deletions(-) |
| 37 | + |
| 38 | +diff --git a/stages/org.osbuild.selinux b/stages/org.osbuild.selinux |
| 39 | +index 563d827b..40487599 100755 |
| 40 | +--- a/stages/org.osbuild.selinux |
| 41 | ++++ b/stages/org.osbuild.selinux |
| 42 | +@@ -4,26 +4,30 @@ import pathlib |
| 43 | + import sys |
| 44 | + |
| 45 | + import osbuild.api |
| 46 | +-from osbuild.util import selinux |
| 47 | ++from osbuild.util import parsing, selinux |
| 48 | + |
| 49 | + |
| 50 | +-def main(tree, options): |
| 51 | ++def main(args): |
| 52 | ++ # Get the path where the tree is |
| 53 | ++ options = args["options"] |
| 54 | + file_contexts = options.get("file_contexts") |
| 55 | + exclude_paths = options.get("exclude_paths") |
| 56 | ++ target = options.get("target", "tree:///") |
| 57 | ++ root, target = parsing.parse_location_into_parts(target, args) |
| 58 | + |
| 59 | + if file_contexts: |
| 60 | +- file_contexts = os.path.join(f"{tree}", options["file_contexts"]) |
| 61 | ++ file_contexts = os.path.join(args["tree"], options["file_contexts"]) |
| 62 | + if exclude_paths: |
| 63 | +- exclude_paths = [os.path.join(tree, p.lstrip("/")) for p in exclude_paths] |
| 64 | +- selinux.setfiles(file_contexts, os.fspath(tree), "", exclude_paths=exclude_paths) |
| 65 | ++ exclude_paths = [os.path.normpath(f"{root}/{target}/{p}") for p in exclude_paths] |
| 66 | ++ selinux.setfiles(file_contexts, os.path.normpath(root), target, exclude_paths=exclude_paths) |
| 67 | + |
| 68 | + labels = options.get("labels", {}) |
| 69 | + for path, label in labels.items(): |
| 70 | +- fullpath = os.path.join(tree, path.lstrip("/")) |
| 71 | ++ fullpath = parsing.parse_location(path, args) |
| 72 | + selinux.setfilecon(fullpath, label) |
| 73 | + |
| 74 | + if options.get("force_autorelabel", False): |
| 75 | +- stamp = pathlib.Path(tree, ".autorelabel") |
| 76 | ++ stamp = pathlib.Path(root, ".autorelabel") |
| 77 | + # Creating just empty /.autorelabel resets only the type of files. |
| 78 | + # To ensure that the full context is reset, we write "-F" into the file. |
| 79 | + # This mimics the behavior of `fixfiles -F boot`. The "-F" option is |
| 80 | +@@ -34,6 +38,5 @@ def main(tree, options): |
| 81 | + |
| 82 | + |
| 83 | + if __name__ == '__main__': |
| 84 | +- args = osbuild.api.arguments() |
| 85 | +- r = main(args["tree"], args["options"]) |
| 86 | ++ r = main(osbuild.api.arguments()) |
| 87 | + sys.exit(r) |
| 88 | +diff --git a/stages/org.osbuild.selinux.meta.json b/stages/org.osbuild.selinux.meta.json |
| 89 | +index 30dbddae..87b13e59 100644 |
| 90 | +--- a/stages/org.osbuild.selinux.meta.json |
| 91 | ++++ b/stages/org.osbuild.selinux.meta.json |
| 92 | +@@ -33,6 +33,21 @@ |
| 93 | + } |
| 94 | + ], |
| 95 | + "properties": { |
| 96 | ++ "target": { |
| 97 | ++ "oneOf": [ |
| 98 | ++ { |
| 99 | ++ "type": "string", |
| 100 | ++ "description": "Target path, if a mount", |
| 101 | ++ "pattern": "^mount://.+" |
| 102 | ++ }, |
| 103 | ++ { |
| 104 | ++ "type": "string", |
| 105 | ++ "description": "Target path, if a tree", |
| 106 | ++ "pattern": "^tree://.+" |
| 107 | ++ } |
| 108 | ++ ], |
| 109 | ++ "default": "tree:///" |
| 110 | ++ }, |
| 111 | + "file_contexts": { |
| 112 | + "type": "string", |
| 113 | + "description": "Path to the active SELinux policy's `file_contexts`" |
| 114 | +@@ -53,7 +68,7 @@ |
| 115 | + }, |
| 116 | + "force_autorelabel": { |
| 117 | + "type": "boolean", |
| 118 | +- "description": "Do not use. Forces auto-relabelling on first boot.", |
| 119 | ++ "description": "Do not use. Forces auto-relabelling on first boot. Affects target's root or tree:/// by default", |
| 120 | + "default": false |
| 121 | + } |
| 122 | + } |
| 123 | +-- |
| 124 | +2.47.0 |
| 125 | + |
0 commit comments