Skip to contentSkip to conversation

libselinux: allow a '*' wildcard in file_contexts.subs_dist/.subs

PATCH Superseded Stephen Smalley 1 participant 1 message Sep 3 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 libselinux: allow a '*' wildcard in file_contexts.subs_dist/.subs

systemd-sysext and systemd-confext stage extension trees under {/etc,/run, /var/lib,/usr/lib,/usr/local/lib}/extensions/<name>/ and {/var/lib,/run,/usr/lib}/confexts/<name>/ and merge them onto /usr, /opt, and /etc via overlayfs. overlayfs presents the underlying layer's on-disk security.selinux xattrs, so the staging tree needs the same labels the merged paths would have. setfiles -r <root> can already do this per extension, but callers that don't know they are writing into a staging tree (rpm scriptlets, tmpfiles) run a plain restorecon and get the wrong label.

The natural place to express this mapping is file_contexts.subs_dist, but substitution sources are a literal prefix match and cannot express the <name> component.

Allow a '*' as a whole-component wildcard in the subtitution sources. Each '*' matches exactly one non-empty component of the input. When the pattern ends in '*' the substitution only applies when the input has a further '/' after the matched component so any entry such as:

    /var/lib/extensions/* /

remaps the contents of each named directory but leaves the directory itself and any sibling regular files unchanged. Multiple '*' components are supported for layouts such as /var/lib/flatpak/app/*/*/active/files.

Entries without a '*' keep the existing strncmp() fast path; wildcard entries record the literal prefix length so the same prefix comparison short-circuits before the component walk. A '*' that is not a whole component is rejected at load.

The systemd-specific paths belong in policy (file_contexts.subs_dist); this only provides the mechanism for matching them.

---
 libselinux/man/man5/selabel_file.5 |  19 +++++-
 libselinux/src/label_file.c        | 106 ++++++++++++++++++++++++-----
 libselinux/src/label_file.h        |   2 +
 3 files changed, 106 insertions(+), 21 deletions(-)
libselinux/man/man5/selabel_file.5 +16 -3
index baba7776..679a3f0e 100644
@@ -188,6 +188,13 @@ A path that matches an entry in one or more of the file contexts policy configur
 .I subs_pathname
 .RS
 The path that will be aliased (considered equivalent) with pathname by the look up process.
+A path component consisting of a single \fB*\fR matches exactly one
+non-empty component of the input path.
+When the last component of \fIsubs_pathname\fR is \fB*\fR the
+substitution only applies when the input has a further \fB/\fR after
+the matched component, so an entry such as \fI/var/lib/extensions/*\fR
+remaps the contents of each named directory but not the directory itself
+or a sibling regular file.
 .RE
 .RE
 .sp
@@ -195,11 +202,13 @@ Example:
 .RS
 # ./contexts/files/file_contexts.subs
 .br
-# pathname  subs_pathname
+# pathname	subs_pathname
 .br
-/myweb      /var/www
+/myweb			/var/www
 .br
-/myspool    /var/spool/mail
+/myspool		/var/spool/mail
+.br
+/var/lib/extensions/*	/
 .sp
 Using the above example, when \fBselabel_lookup\fR(3) is passed a path of
 \fI/myweb/index.html\fR the function will substitute the \fI/myweb\fR
@@ -208,6 +217,10 @@ component with \fI/var/www\fR, therefore the path used is:
 .RS
 .I /var/www/index.html
 .RE
+.sp
+and a path of \fI/var/lib/extensions/example/usr/lib/foo.so\fR is looked
+up as \fI/usr/lib/foo.so\fR, while \fI/var/lib/extensions/example\fR and
+\fI/var/lib/extensions/example.raw\fR are left unchanged.
 .RE
 .
 .SH "NOTES"
libselinux/src/label_file.c +88 -18

140 lines not on this page · raw message

libselinux/src/label_file.h +2 -0
index acfe2e0d..a42e2aef 100644
@@ -79,6 +79,8 @@ struct selabel_sub {
 	char *dst; /* substituted path prefix */
 	uint32_t slen; /* length of source path prefix */
 	uint32_t dlen; /* length of substituted path prefix */
+	uint32_t plen; /* prefix up to first wildcard, == slen if none */
+	bool has_wild; /* src contains one or more '*' whole-component wildcards */
 };
 
 /* A regular expression file security context specification */
Signature · 1 line
2.55.0