Skip to contentSkip to conversation

selinux: enable per-file labeling for functionfs

PATCH Applied Neill Kapron 3 participants 5 messages Aug 28, 2025 – Sep 5, 2025 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 selinux: enable per-file labeling for functionfs

This patch adds support for genfscon per-file labeling of functionfs files as well as support for userspace to apply labels after new functionfs endpoints are created.

This allows for separate labels and therefore access control on a per-endpoint basis. An example use case would be for the default endpoint EP0 used as a restricted control endpoint, and additional usb endpoints to be used by other more permissive domains.

It should be noted that if there are multiple functionfs mounts on a system, genfs file labels will apply to all mounts, and therefore will not likely be as useful as the userspace relabeling portion of this patch - the addition to selinux_is_genfs_special_handling().

This patch introduces the functionfs_seclabel policycap to maintain existing functionfs genfscon behavior unless explicitly enabled.

Changes since v1:
- Add functionfs_seclabel policycap
- Move new functionality to the end of existing lists

Changes since v2:
- Sending as separate patches

---
 security/selinux/hooks.c                   | 8 ++++++--
 security/selinux/include/policycap.h       | 1 +
 security/selinux/include/policycap_names.h | 1 +
 security/selinux/include/security.h        | 6 ++++++
 4 files changed, 14 insertions(+), 2 deletions(-)
security/selinux/hooks.c +6 -2
index e474cd7398ef..333bb6cba25e 100644
@@ -476,7 +476,9 @@ static int selinux_is_genfs_special_handling(struct super_block *sb)
 		!strcmp(sb->s_type->name, "rootfs") ||
 		(selinux_policycap_cgroupseclabel() &&
 		 (!strcmp(sb->s_type->name, "cgroup") ||
-		  !strcmp(sb->s_type->name, "cgroup2")));
+		  !strcmp(sb->s_type->name, "cgroup2"))) ||
+		(selinux_policycap_functionfs_seclabel() &&
+		 !strcmp(sb->s_type->name, "functionfs"));
 }
 
 static int selinux_is_sblabel_mnt(struct super_block *sb)
@@ -741,7 +743,9 @@ static int selinux_set_mnt_opts(struct super_block *sb,
 	    !strcmp(sb->s_type->name, "binder") ||
 	    !strcmp(sb->s_type->name, "bpf") ||
 	    !strcmp(sb->s_type->name, "pstore") ||
-	    !strcmp(sb->s_type->name, "securityfs"))
+	    !strcmp(sb->s_type->name, "securityfs") ||
+	    (selinux_policycap_functionfs_seclabel() &&
+	     strcmp(sb->s_type->name, "functionfs")))
 		sbsec->flags |= SE_SBGENFS;
 
 	if (!strcmp(sb->s_type->name, "sysfs") ||
security/selinux/include/policycap.h +1 -0
index 7405154e6c42..135a969f873c 100644
@@ -17,6 +17,7 @@ enum {
 	POLICYDB_CAP_NETLINK_XPERM,
 	POLICYDB_CAP_NETIF_WILDCARD,
 	POLICYDB_CAP_GENFS_SECLABEL_WILDCARD,
+	POLICYDB_CAP_FUNCTIONFS_SECLABEL,
 	__POLICYDB_CAP_MAX
 };
 #define POLICYDB_CAP_MAX (__POLICYDB_CAP_MAX - 1)
security/selinux/include/policycap_names.h +1 -0
index d8962fcf2ff9..ff8882887651 100644
@@ -20,6 +20,7 @@ const char *const selinux_policycap_names[__POLICYDB_CAP_MAX] = {
 	"netlink_xperm",
 	"netif_wildcard",
 	"genfs_seclabel_wildcard",
+	"functionfs_seclabel",
 };
 /* clang-format on */
 
security/selinux/include/security.h +6 -0
index 7f19972f7922..0f954a40d3fc 100644
@@ -203,6 +203,12 @@ static inline bool selinux_policycap_netlink_xperm(void)
 		selinux_state.policycap[POLICYDB_CAP_NETLINK_XPERM]);
 }
 
+static inline bool selinux_policycap_functionfs_seclabel(void)
+{
+	return READ_ONCE(
+		selinux_state.policycap[POLICYDB_CAP_FUNCTIONFS_SECLABEL]);
+}
+
 struct selinux_policy_convert_data;
 
 struct selinux_load_state {
Signature · 1 line
2.51.0.318.gd7df087d1a-goog
maintainer Acked-by on v3 1/1
#1 lore raw
Quoted 15 lines · Neill Kapron

This patch adds support for genfscon per-file labeling of functionfs files as well as support for userspace to apply labels after new functionfs endpoints are created.

This allows for separate labels and therefore access control on a per-endpoint basis. An example use case would be for the default endpoint EP0 used as a restricted control endpoint, and additional usb endpoints to be used by other more permissive domains.

It should be noted that if there are multiple functionfs mounts on a system, genfs file labels will apply to all mounts, and therefore will not likely be as useful as the userspace relabeling portion of this patch - the addition to selinux_is_genfs_special_handling().

Quoted 4 lines

This patch introduces the functionfs_seclabel policycap to maintain existing functionfs genfscon behavior unless explicitly enabled.

As before, don't rely on the policy capability bit remaining stable until Paul merges this patch.
Also, not worth re-spinning IMHO but the changelog below normally goes after the "---" before
the diffstat so that it doesn't get included in the commit message since no one cares about
the in-submission changes once the patch is merged.

Quoted 84 lines

Changes since v1:
- Add functionfs_seclabel policycap
- Move new functionality to the end of existing lists

Changes since v2:
- Sending as separate patches

---
 security/selinux/hooks.c                   | 8 ++++++--
 security/selinux/include/policycap.h       | 1 +
 security/selinux/include/policycap_names.h | 1 +
 security/selinux/include/security.h        | 6 ++++++
 4 files changed, 14 insertions(+), 2 deletions(-)
security/selinux/hooks.c +6 -2
index e474cd7398ef..333bb6cba25e 100644
@@ -476,7 +476,9 @@ static int selinux_is_genfs_special_handling(struct super_block *sb)
                !strcmp(sb->s_type->name, "rootfs") ||
                (selinux_policycap_cgroupseclabel() &&
                 (!strcmp(sb->s_type->name, "cgroup") ||
-                 !strcmp(sb->s_type->name, "cgroup2")));
+                 !strcmp(sb->s_type->name, "cgroup2"))) ||
+               (selinux_policycap_functionfs_seclabel() &&
+                !strcmp(sb->s_type->name, "functionfs"));
 }
 
 static int selinux_is_sblabel_mnt(struct super_block *sb)
@@ -741,7 +743,9 @@ static int selinux_set_mnt_opts(struct super_block *sb,
            !strcmp(sb->s_type->name, "binder") ||
            !strcmp(sb->s_type->name, "bpf") ||
            !strcmp(sb->s_type->name, "pstore") ||
-           !strcmp(sb->s_type->name, "securityfs"))
+           !strcmp(sb->s_type->name, "securityfs") ||
+           (selinux_policycap_functionfs_seclabel() &&
+            strcmp(sb->s_type->name, "functionfs")))
                sbsec->flags |= SE_SBGENFS;
 
        if (!strcmp(sb->s_type->name, "sysfs") ||
security/selinux/include/policycap.h +1 -0
index 7405154e6c42..135a969f873c 100644
@@ -17,6 +17,7 @@ enum {
        POLICYDB_CAP_NETLINK_XPERM,
        POLICYDB_CAP_NETIF_WILDCARD,
        POLICYDB_CAP_GENFS_SECLABEL_WILDCARD,
+       POLICYDB_CAP_FUNCTIONFS_SECLABEL,
        __POLICYDB_CAP_MAX
 };
 #define POLICYDB_CAP_MAX (__POLICYDB_CAP_MAX - 1)
security/selinux/include/policycap_names.h +1 -0
index d8962fcf2ff9..ff8882887651 100644
@@ -20,6 +20,7 @@ const char *const selinux_policycap_names[__POLICYDB_CAP_MAX] = {
        "netlink_xperm",
        "netif_wildcard",
        "genfs_seclabel_wildcard",
+       "functionfs_seclabel",
 };
 /* clang-format on */
 
security/selinux/include/security.h +6 -1
index 7f19972f7922..0f954a40d3fc 100644
@@ -203,6 +203,12 @@ static inline bool selinux_policycap_netlink_xperm(void)
                selinux_state.policycap[POLICYDB_CAP_NETLINK_XPERM]);
 }
 
+static inline bool selinux_policycap_functionfs_seclabel(void)
+{
+       return READ_ONCE(
+               selinux_state.policycap[POLICYDB_CAP_FUNCTIONFS_SECLABEL]);
+}
+
 struct selinux_policy_convert_data;
 
 struct selinux_load_state {
--

2.51.0.318.gd7df087d1a-goog

#2 lore raw
Quoted 7 lines · Stephen Smalley

As before, don't rely on the policy capability bit remaining stable until Paul merges this patch.
Also, not worth re-spinning IMHO but the changelog below normally goes after the "---" before
the diffstat so that it doesn't get included in the commit message since no one cares about
the in-submission changes once the patch is merged.

Thanks, I appreciate your guidance on this!

Neill

maintainer Applied
#3 lore raw
In reply to Neill Kapron
Quoted 27 lines · On Aug 28, 2025 Neill Kapron <[email protected]> wrote:

This patch adds support for genfscon per-file labeling of functionfs files as well as support for userspace to apply labels after new functionfs endpoints are created.

This allows for separate labels and therefore access control on a per-endpoint basis. An example use case would be for the default endpoint EP0 used as a restricted control endpoint, and additional usb endpoints to be used by other more permissive domains.

It should be noted that if there are multiple functionfs mounts on a system, genfs file labels will apply to all mounts, and therefore will not likely be as useful as the userspace relabeling portion of this patch - the addition to selinux_is_genfs_special_handling().

This patch introduces the functionfs_seclabel policycap to maintain existing functionfs genfscon behavior unless explicitly enabled.

Changes since v1:
- Add functionfs_seclabel policycap
- Move new functionality to the end of existing lists

Changes since v2:
- Sending as separate patches

Quoted 7 lines
---
 security/selinux/hooks.c                   | 8 ++++++--
 security/selinux/include/policycap.h       | 1 +
 security/selinux/include/policycap_names.h | 1 +
 security/selinux/include/security.h        | 6 ++++++
 4 files changed, 14 insertions(+), 2 deletions(-)

Merged into selinux/dev, thanks!

--
paul-moore.com