]> git.ipfire.org Git - thirdparty/linux.git/commit
selinux: support wildcard match in genfscon
authorTakaya Saeki <takayas@chromium.org>
Tue, 18 Mar 2025 08:31:39 +0000 (08:31 +0000)
committerPaul Moore <paul@paul-moore.com>
Fri, 11 Apr 2025 20:36:34 +0000 (16:36 -0400)
commit8716451a4e57cc82f3656d7a71b67d3b5831ef3f
tree841f0158aafeeabe0a4d32756ffcf6ff310e88fd
parent4926c3fd83d50987685a0c1131bd60b252a3d541
selinux: support wildcard match in genfscon

Currently, genfscon only supports string prefix match to label files.
Thus, labeling numerous dynamic sysfs entries requires many specific
path rules. For example, labeling device paths such as
`/sys/devices/pci0000:00/0000:00:03.1/<...>/0000:04:00.1/wakeup`
requires listing all specific PCI paths, which is challenging to
maintain. While user-space restorecon can handle these paths with
regular expression rules, relabeling thousands of paths under sysfs
after it is mounted is inefficient compared to using genfscon.

This commit adds wildcard matching to genfscon to make rules more
efficient and expressive. This new behavior is enabled by
genfs_seclabel_wildcard capability. With this capability, genfscon does
wildcard matching instead of prefix matching. When multiple wildcard
rules match against a path, then the longest rule (determined by the
length of the rule string) will be applied. If multiple rules of the
same length match, the first matching rule encountered in the given
genfscon policy will be applied. Users are encouraged to write longer,
more explicit path rules to avoid relying on this behavior.

This change resulted in nice real-world performance improvements. For
example, boot times on test Android devices were reduced by 15%. This
improvement is due to the elimination of the "restorecon -R /sys" step
during boot, which takes more than two seconds in the worst case.

Signed-off-by: Takaya Saeki <takayas@chromium.org>
Signed-off-by: Paul Moore <paul@paul-moore.com>
security/selinux/include/policycap.h
security/selinux/include/policycap_names.h
security/selinux/ss/services.c