|
| 1 | +From beacc9e146cb3c104ff69a06e439e76656dc8df3 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 2/3] 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 | + targets: |
| 13 | + - tree:///path/to/dir |
| 14 | + - mount://root/path/to/dir |
| 15 | + devices: |
| 16 | + disk: ... |
| 17 | + mounts: |
| 18 | + - name: root |
| 19 | + source: disk |
| 20 | + target: / |
| 21 | +``` |
| 22 | +--- |
| 23 | + stages/org.osbuild.selinux | 52 ++++++++++++++++------------ |
| 24 | + stages/org.osbuild.selinux.meta.json | 19 ++++++++++ |
| 25 | + 2 files changed, 49 insertions(+), 22 deletions(-) |
| 26 | + |
| 27 | +diff --git a/stages/org.osbuild.selinux b/stages/org.osbuild.selinux |
| 28 | +index 563d827b..da1ad39a 100755 |
| 29 | +--- a/stages/org.osbuild.selinux |
| 30 | ++++ b/stages/org.osbuild.selinux |
| 31 | +@@ -4,36 +4,44 @@ import pathlib |
| 32 | + import sys |
| 33 | + |
| 34 | + import osbuild.api |
| 35 | +-from osbuild.util import selinux |
| 36 | ++from osbuild.util import parsing, selinux |
| 37 | + |
| 38 | + |
| 39 | +-def main(tree, options): |
| 40 | ++def main(args, options): |
| 41 | + file_contexts = options.get("file_contexts") |
| 42 | + exclude_paths = options.get("exclude_paths") |
| 43 | + |
| 44 | +- if file_contexts: |
| 45 | +- file_contexts = os.path.join(f"{tree}", options["file_contexts"]) |
| 46 | +- if exclude_paths: |
| 47 | +- exclude_paths = [os.path.join(tree, p.lstrip("/")) for p in exclude_paths] |
| 48 | +- selinux.setfiles(file_contexts, os.fspath(tree), "", exclude_paths=exclude_paths) |
| 49 | ++ # Get the path where the tree is |
| 50 | ++ tree = args["tree"] |
| 51 | + |
| 52 | +- labels = options.get("labels", {}) |
| 53 | +- for path, label in labels.items(): |
| 54 | +- fullpath = os.path.join(tree, path.lstrip("/")) |
| 55 | +- selinux.setfilecon(fullpath, label) |
| 56 | ++ # Set labels on each target |
| 57 | ++ for target in options.get("targets", ["tree:///"]): |
| 58 | ++ root, target = parsing.parse_location_ext(target, args) |
| 59 | ++ target = target.lstrip("/") |
| 60 | + |
| 61 | +- if options.get("force_autorelabel", False): |
| 62 | +- stamp = pathlib.Path(tree, ".autorelabel") |
| 63 | +- # Creating just empty /.autorelabel resets only the type of files. |
| 64 | +- # To ensure that the full context is reset, we write "-F" into the file. |
| 65 | +- # This mimics the behavior of `fixfiles -F boot`. The "-F" option is |
| 66 | +- # then passed to `selinux-autorelabel` script [0]. |
| 67 | +- # Note that this is missing from the selinux(8) and selinux_config(5) man-pages |
| 68 | +- # [0] https://src.fedoraproject.org/rpms/policycoreutils/blob/rawhide/f/selinux-autorelabel#_54 |
| 69 | +- stamp.write_text("-F", encoding="utf-8") |
| 70 | ++ if file_contexts: |
| 71 | ++ file_contexts = os.path.join(f"{tree}", options["file_contexts"]) |
| 72 | ++ if exclude_paths: |
| 73 | ++ exclude_paths = [os.path.join(root, target, p.lstrip("/")) for p in exclude_paths] |
| 74 | ++ selinux.setfiles(file_contexts, os.fspath(root), target, exclude_paths=exclude_paths) |
| 75 | ++ |
| 76 | ++ labels = options.get("labels", {}) |
| 77 | ++ for path, label in labels.items(): |
| 78 | ++ fullpath = os.path.join(root, path.lstrip("/")) |
| 79 | ++ selinux.setfilecon(fullpath, label) |
| 80 | ++ |
| 81 | ++ if options.get("force_autorelabel", False): |
| 82 | ++ stamp = pathlib.Path(root, ".autorelabel") |
| 83 | ++ # Creating just empty /.autorelabel resets only the type of files. |
| 84 | ++ # To ensure that the full context is reset, we write "-F" into the file. |
| 85 | ++ # This mimics the behavior of `fixfiles -F boot`. The "-F" option is |
| 86 | ++ # then passed to `selinux-autorelabel` script [0]. |
| 87 | ++ # Note that this is missing from the selinux(8) and selinux_config(5) man-pages |
| 88 | ++ # [0] https://src.fedoraproject.org/rpms/policycoreutils/blob/rawhide/f/selinux-autorelabel#_54 |
| 89 | ++ stamp.write_text("-F", encoding="utf-8") |
| 90 | + |
| 91 | + |
| 92 | + if __name__ == '__main__': |
| 93 | +- args = osbuild.api.arguments() |
| 94 | +- r = main(args["tree"], args["options"]) |
| 95 | ++ _args = osbuild.api.arguments() |
| 96 | ++ r = main(_args, _args["options"]) |
| 97 | + sys.exit(r) |
| 98 | +diff --git a/stages/org.osbuild.selinux.meta.json b/stages/org.osbuild.selinux.meta.json |
| 99 | +index 30dbddae..3476df9d 100644 |
| 100 | +--- a/stages/org.osbuild.selinux.meta.json |
| 101 | ++++ b/stages/org.osbuild.selinux.meta.json |
| 102 | +@@ -33,6 +33,25 @@ |
| 103 | + } |
| 104 | + ], |
| 105 | + "properties": { |
| 106 | ++ "targets": { |
| 107 | ++ "description": "Array of target paths to operate on", |
| 108 | ++ "type": "array", |
| 109 | ++ "additionalItems": false, |
| 110 | ++ "items": { |
| 111 | ++ "oneOf": [ |
| 112 | ++ { |
| 113 | ++ "type": "string", |
| 114 | ++ "description": "Target path, if a mount", |
| 115 | ++ "pattern": "^mount://.+" |
| 116 | ++ }, |
| 117 | ++ { |
| 118 | ++ "type": "string", |
| 119 | ++ "description": "Target path, if a tree", |
| 120 | ++ "pattern": "^tree://.+" |
| 121 | ++ } |
| 122 | ++ ] |
| 123 | ++ } |
| 124 | ++ }, |
| 125 | + "file_contexts": { |
| 126 | + "type": "string", |
| 127 | + "description": "Path to the active SELinux policy's `file_contexts`" |
| 128 | +-- |
| 129 | +2.47.0 |
| 130 | + |
0 commit comments