Skip to contentSkip to conversation

SELinux: Add support for BPF token access control

PATCH Superseded Eric Suen 3 participants 9 messages Oct 8, 2025 – Oct 21, 2025 SELinux security module

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Eric Suen

author
#0 lore raw

PATCH v5 1/1 SELinux: Add support for BPF token access control

BPF token support was introduced to allow a privileged process to delegate limited BPF functionality—such as map creation and program loading—to an unprivileged process:
https://lore.kernel.org/linux-security-module/[email protected]/

This patch adds SELinux support for controlling BPF token access. With this change, SELinux policies can now enforce constraints on BPF token usage based on both the delegating (privileged) process and the recipient (unprivileged) process.

Supported operations currently include:
- map_create
- prog_load

High-level workflow:
1. An unprivileged process creates a VFS context via fsopen() and obtains a file descriptor.
2. This descriptor is passed to a privileged process, which configures BPF token delegation options and mounts a BPF filesystem.
3. SELinux records the creator_sid of the privileged process during mount setup.
4. The unprivileged process then uses this BPF fs mount to create a token and attach it to subsequent BPF syscalls.
5. During verification of map_create and prog_load, SELinux uses creator_sid and the current SID to check policy permissions via:
avc_has_perm(creator_sid, current_sid, SECCLASS_BPF,
BPF__MAP_CREATE, NULL);

The implementation introduces two new permissions:
- map_create_as
- prog_load_as

At token creation time, SELinux verifies that the current process has the appropriate *_as permission (depending on the allowed_cmds value in the bpf_token) to act on behalf of the creator_sid.

Example SELinux policy:
allow test_bpf_t self:bpf {
map_create map_read map_write prog_load prog_run map_create_as prog_load_as
};

Additionally, a new policy capability bpf_token_perms is added to ensure backward compatibility. If disabled, previous behavior ((checks based on current process SID)) is preserved.

---
Changes in v2:
- Fixed bug in selinux_bpffs_creator_sid(u32 fd) where it retrieved creator_sid from wrong file descriptor
- Removed unnecessary checks for null, per review comments from the first patch

Changes in v3:
- Removed check for 'sid == SECSID_NULL' in selinux_bpf_token_create and allow it to fall through to the permission checks which will fail as access denied to unlabeled_t

Changes in v4:
- Added initialization of creator_sid in selinux_sb_alloc_security
- Enabled handling of creator_sid in selinux_cmp_sb_context and selinux_sb_clone_mnt_opts
- Minor updates based on review comments

Changes in v5:
- Moved to dev-staging branch instead of main
- Added implementation of selinux_bpf_token_capable which is originally from Daniel's patch
https://lore.kernel.org/selinux/[email protected]/

 security/selinux/hooks.c                   | 116 ++++++++++++++++++++-
 security/selinux/include/classmap.h        |   2 +-
 security/selinux/include/objsec.h          |   2 +
 security/selinux/include/policycap.h       |   1 +
 security/selinux/include/policycap_names.h |   1 +
 security/selinux/include/security.h        |   6 ++
 6 files changed, 125 insertions(+), 3 deletions(-)
security/selinux/hooks.c +114 -2
index 2b685f9dd61d..ba2fcd0a7d8a 100644
@@ -736,6 +736,8 @@ static int selinux_set_mnt_opts(struct super_block *sb,
 		goto out;
 	}
 
+	sbsec->creator_sid = current_sid();
+
 	if (strcmp(sb->s_type->name, "proc") == 0)
 		sbsec->flags |= SE_SBPROC | SE_SBGENFS;
 
@@ -907,6 +909,8 @@ static int selinux_cmp_sb_context(const struct super_block *oldsb,
 		if (oldroot->sid != newroot->sid)
 			goto mismatch;
 	}
+	if (old->creator_sid != new->creator_sid)
+		goto mismatch;
 	return 0;
 mismatch:
 	pr_warn("SELinux: mount invalid.  Same superblock, "
@@ -966,6 +970,7 @@ static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
 	newsbsec->sid = oldsbsec->sid;
 	newsbsec->def_sid = oldsbsec->def_sid;
 	newsbsec->behavior = oldsbsec->behavior;
+	newsbsec->creator_sid = oldsbsec->creator_sid;
 
 	if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
 		!(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
@@ -2585,6 +2590,7 @@ static int selinux_sb_alloc_security(struct super_block *sb)
 	sbsec->sid = SECINITSID_UNLABELED;
 	sbsec->def_sid = SECINITSID_FILE;
 	sbsec->mntpoint_sid = SECINITSID_UNLABELED;
+	sbsec->creator_sid = SECINITSID_UNLABELED;
 
 	return 0;
 }
@@ -7037,6 +7043,9 @@ static int selinux_bpf(int cmd, union bpf_attr *attr,
 	u32 sid = current_sid();
 	int ret;
 
+	if (selinux_policycap_bpf_token_perms())
+		return 0;
+
 	switch (cmd) {
 	case BPF_MAP_CREATE:
 		ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
@@ -7118,10 +7127,29 @@ static int selinux_bpf_prog(struct bpf_prog *prog)
 			    BPF__PROG_RUN, NULL);
 }
 
+static u32 selinux_bpffs_creator_sid(u32 fd)
+{
+	struct path path;
+	struct super_block *sb;
+	struct superblock_security_struct *sbsec;
+
+	CLASS(fd, f)(fd);
+
+	if (fd_empty(f))
+		return SECSID_NULL;
+
+	path = fd_file(f)->f_path;
+	sb = path.dentry->d_sb;
+	sbsec = selinux_superblock(sb);
+
+	return sbsec->creator_sid;
+}
+
 static int selinux_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
 				  struct bpf_token *token, bool kernel)
 {
 	struct bpf_security_struct *bpfsec;
+	u32 ssid;
 
 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
 	if (!bpfsec)
@@ -7130,7 +7158,12 @@ static int selinux_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
 	bpfsec->sid = current_sid();
 	map->security = bpfsec;
 
-	return 0;
+	if (!token)
+		ssid = bpfsec->sid;
+	else
+		ssid = selinux_bpffs_creator_sid(attr->map_token_fd);
+
+	return avc_has_perm(ssid, bpfsec->sid, SECCLASS_BPF, BPF__MAP_CREATE, NULL);
 }
 
 static void selinux_bpf_map_free(struct bpf_map *map)
@@ -7145,6 +7178,7 @@ static int selinux_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
 				 struct bpf_token *token, bool kernel)
 {
 	struct bpf_security_struct *bpfsec;
+	u32 ssid;
 
 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
 	if (!bpfsec)
@@ -7153,7 +7187,12 @@ static int selinux_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
 	bpfsec->sid = current_sid();
 	prog->aux->security = bpfsec;
 
-	return 0;
+	if (!token)
+		ssid = bpfsec->sid;
+	else
+		ssid = selinux_bpffs_creator_sid(attr->prog_token_fd);
+
+	return avc_has_perm(ssid, bpfsec->sid, SECCLASS_BPF, BPF__PROG_LOAD, NULL);
 }
 
 static void selinux_bpf_prog_free(struct bpf_prog *prog)
@@ -7164,10 +7203,15 @@ static void selinux_bpf_prog_free(struct bpf_prog *prog)
 	kfree(bpfsec);
 }
 
+#define bpf_token_cmd(T, C) \
+	((T)->allowed_cmds & (1ULL << (C)))
+
 static int selinux_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
 				    const struct path *path)
 {
 	struct bpf_security_struct *bpfsec;
+	u32 sid = selinux_bpffs_creator_sid(attr->token_create.bpffs_fd);
+	int err;
 
 	bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
 	if (!bpfsec)
@@ -7176,6 +7220,29 @@ static int selinux_bpf_token_create(struct bpf_token *token, union bpf_attr *att
 	bpfsec->sid = current_sid();
 	token->security = bpfsec;
 
+	bpfsec->perms = 0;
+
+	/**
+	 * 'token->allowed_cmds' is a bit mask of allowed commands
+	 * Convert the BPF command enum to a bitmask representing its position in the
+	 * allowed_cmds bitmap.
+	 */
+	if (bpf_token_cmd(token, BPF_MAP_CREATE)) {
+		err = avc_has_perm(bpfsec->sid, sid, SECCLASS_BPF, BPF__MAP_CREATE_AS, NULL);
+		if (err)
+			return err;
+
+		bpfsec->perms |= BPF__MAP_CREATE;
+	}
+
+	if (bpf_token_cmd(token, BPF_PROG_LOAD)) {
+		err = avc_has_perm(bpfsec->sid, sid, SECCLASS_BPF, BPF__PROG_LOAD_AS, NULL);
+		if (err)
+			return err;
+
+		bpfsec->perms |= BPF__PROG_LOAD;
+	}
+
 	return 0;
 }
 
@@ -7186,6 +7253,49 @@ static void selinux_bpf_token_free(struct bpf_token *token)
 	token->security = NULL;
 	kfree(bpfsec);
 }
+
+static int selinux_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd)
+{
+	struct bpf_security_struct *bpfsec;
+
+	bpfsec = token->security;
+	switch (cmd) {
+	case BPF_MAP_CREATE:
+		if (!(bpfsec->perms & BPF__MAP_CREATE))
+			return -EACCES;
+		break;
+	case BPF_PROG_LOAD:
+		if (!(bpfsec->perms & BPF__PROG_LOAD))
+			return -EACCES;
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+
+static int selinux_bpf_token_capable(const struct bpf_token *token, int cap)
+{
+	u16 sclass;
+	struct bpf_security_struct *bpfsec = token->security;
+	u32 sid = bpfsec->sid;
+	u32 av = CAP_TO_MASK(cap);
+
+	switch (CAP_TO_INDEX(cap)) {
+	case 0:
+		sclass = SECCLASS_CAP_USERNS;
+		break;
+	case 1:
+		sclass = SECCLASS_CAP2_USERNS;
+		break;
+	default:
+		pr_err("SELinux:  out of range capability %d\n", cap);
+		return -EINVAL;
+	}
+
+	return avc_has_perm(sid, sid, sclass, av, NULL);
+}
 #endif
 
 struct lsm_blob_sizes selinux_blob_sizes __ro_after_init = {
@@ -7619,6 +7729,8 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(bpf_map_create, selinux_bpf_map_create),
 	LSM_HOOK_INIT(bpf_prog_load, selinux_bpf_prog_load),
 	LSM_HOOK_INIT(bpf_token_create, selinux_bpf_token_create),
+	LSM_HOOK_INIT(bpf_token_cmd, selinux_bpf_token_cmd),
+	LSM_HOOK_INIT(bpf_token_capable, selinux_bpf_token_capable),
 #endif
 #ifdef CONFIG_PERF_EVENTS
 	LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc),
security/selinux/include/classmap.h +1 -1
index 3ec85142771f..90cb61b16425 100644
@@ -171,7 +171,7 @@ const struct security_class_mapping secclass_map[] = {
 	{ "infiniband_endport", { "manage_subnet", NULL } },
 	{ "bpf",
 	  { "map_create", "map_read", "map_write", "prog_load", "prog_run",
-	    NULL } },
+	    "map_create_as", "prog_load_as", NULL } },
 	{ "xdp_socket", { COMMON_SOCK_PERMS, NULL } },
 	{ "mctp_socket", { COMMON_SOCK_PERMS, NULL } },
 	{ "perf_event",
security/selinux/include/objsec.h +2 -0
index 1d7ac59015a1..3b41f75cdac9 100644
@@ -87,6 +87,7 @@ struct superblock_security_struct {
 	u32 sid; /* SID of file system superblock */
 	u32 def_sid; /* default SID for labeling */
 	u32 mntpoint_sid; /* SECURITY_FS_USE_MNTPOINT context for files */
+	u32 creator_sid; /* SID of privileged process and is used to verify bpf operations */
 	unsigned short behavior; /* labeling behavior */
 	unsigned short flags; /* which mount options were specified */
 	struct mutex lock;
@@ -164,6 +165,7 @@ struct pkey_security_struct {
 
 struct bpf_security_struct {
 	u32 sid; /* SID of bpf obj creator */
+	u32 perms; /* allowed AV permissions, e.g. BPF__MAP_CREATE, for requested bpf commands */
 };
 
 struct perf_event_security_struct {
security/selinux/include/policycap.h +1 -0
index 231d02227e59..dbf39358ae6a 100644
@@ -19,6 +19,7 @@ enum {
 	POLICYDB_CAP_GENFS_SECLABEL_WILDCARD,
 	POLICYDB_CAP_FUNCTIONFS_SECLABEL,
 	POLICYDB_CAP_MEMFD_CLASS,
+	POLICYDB_CAP_BPF_TOKEN_PERMS,
 	__POLICYDB_CAP_MAX
 };
 #define POLICYDB_CAP_MAX (__POLICYDB_CAP_MAX - 1)
security/selinux/include/policycap_names.h +1 -0
index 454dab37bda3..6e2b808e12e8 100644
@@ -22,6 +22,7 @@ const char *const selinux_policycap_names[__POLICYDB_CAP_MAX] = {
 	"genfs_seclabel_wildcard",
 	"functionfs_seclabel",
 	"memfd_class",
+	"bpf_token_perms",
 };
 /* clang-format on */
 
security/selinux/include/security.h +6 -0
index 5d1dad8058b1..d1f16d7f684d 100644
@@ -214,6 +214,12 @@ static inline bool selinux_policycap_memfd_class(void)
 	return READ_ONCE(selinux_state.policycap[POLICYDB_CAP_MEMFD_CLASS]);
 }
 
+static inline bool selinux_policycap_bpf_token_perms(void)
+{
+	return READ_ONCE(
+		selinux_state.policycap[POLICYDB_CAP_BPF_TOKEN_PERMS]);
+}
+
 struct selinux_policy_convert_data;
 
 struct selinux_load_state {
Signature · 1 line
2.50.1
maintainer Reviewed-by Tested-by on v5 1/1
#1 lore raw
Quoted 43 lines · Eric Suen

BPF token support was introduced to allow a privileged process to delegate limited BPF functionality—such as map creation and program loading—to an unprivileged process:
https://lore.kernel.org/linux-security-module/[email protected]/

This patch adds SELinux support for controlling BPF token access. With this change, SELinux policies can now enforce constraints on BPF token usage based on both the delegating (privileged) process and the recipient (unprivileged) process.

Supported operations currently include:
- map_create
- prog_load

High-level workflow:
1. An unprivileged process creates a VFS context via fsopen() and obtains a file descriptor.
2. This descriptor is passed to a privileged process, which configures BPF token delegation options and mounts a BPF filesystem.
3. SELinux records the creator_sid of the privileged process during mount setup.
4. The unprivileged process then uses this BPF fs mount to create a token and attach it to subsequent BPF syscalls.
5. During verification of map_create and prog_load, SELinux uses creator_sid and the current SID to check policy permissions via:
avc_has_perm(creator_sid, current_sid, SECCLASS_BPF,
BPF__MAP_CREATE, NULL);

The implementation introduces two new permissions:
- map_create_as
- prog_load_as

At token creation time, SELinux verifies that the current process has the appropriate *_as permission (depending on the allowed_cmds value in the bpf_token) to act on behalf of the creator_sid.

Example SELinux policy:
allow test_bpf_t self:bpf {
map_create map_read map_write prog_load prog_run map_create_as prog_load_as
};

Quoted 5 lines

Additionally, a new policy capability bpf_token_perms is added to ensure backward compatibility. If disabled, previous behavior ((checks based on current process SID)) is preserved.

Should probably credit Daniel Durning for the bpf_token_capable() implementation in the patch description, not just the changelog (which won't be included when the patch is applied). Otherwise,

Quoted 328 lines

---
Changes in v2:
- Fixed bug in selinux_bpffs_creator_sid(u32 fd) where it retrieved creator_sid from wrong file descriptor
- Removed unnecessary checks for null, per review comments from the first patch

Changes in v3:
- Removed check for 'sid == SECSID_NULL' in selinux_bpf_token_create and allow it to fall through to the permission checks which will fail as access denied to unlabeled_t

Changes in v4:
- Added initialization of creator_sid in selinux_sb_alloc_security
- Enabled handling of creator_sid in selinux_cmp_sb_context and selinux_sb_clone_mnt_opts
- Minor updates based on review comments

Changes in v5:
- Moved to dev-staging branch instead of main
- Added implementation of selinux_bpf_token_capable which is originally from Daniel's patch
https://lore.kernel.org/selinux/[email protected]/

 security/selinux/hooks.c                   | 116 ++++++++++++++++++++-
 security/selinux/include/classmap.h        |   2 +-
 security/selinux/include/objsec.h          |   2 +
 security/selinux/include/policycap.h       |   1 +
 security/selinux/include/policycap_names.h |   1 +
 security/selinux/include/security.h        |   6 ++
 6 files changed, 125 insertions(+), 3 deletions(-)
security/selinux/hooks.c +114 -2
index 2b685f9dd61d..ba2fcd0a7d8a 100644
@@ -736,6 +736,8 @@ static int selinux_set_mnt_opts(struct super_block *sb,
                goto out;
        }
 
+       sbsec->creator_sid = current_sid();
+
        if (strcmp(sb->s_type->name, "proc") == 0)
                sbsec->flags |= SE_SBPROC | SE_SBGENFS;
 
@@ -907,6 +909,8 @@ static int selinux_cmp_sb_context(const struct super_block *oldsb,
                if (oldroot->sid != newroot->sid)
                        goto mismatch;
        }
+       if (old->creator_sid != new->creator_sid)
+               goto mismatch;
        return 0;
 mismatch:
        pr_warn("SELinux: mount invalid.  Same superblock, "
@@ -966,6 +970,7 @@ static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
        newsbsec->sid = oldsbsec->sid;
        newsbsec->def_sid = oldsbsec->def_sid;
        newsbsec->behavior = oldsbsec->behavior;
+       newsbsec->creator_sid = oldsbsec->creator_sid;
 
        if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
                !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
@@ -2585,6 +2590,7 @@ static int selinux_sb_alloc_security(struct super_block *sb)
        sbsec->sid = SECINITSID_UNLABELED;
        sbsec->def_sid = SECINITSID_FILE;
        sbsec->mntpoint_sid = SECINITSID_UNLABELED;
+       sbsec->creator_sid = SECINITSID_UNLABELED;
 
        return 0;
 }
@@ -7037,6 +7043,9 @@ static int selinux_bpf(int cmd, union bpf_attr *attr,
        u32 sid = current_sid();
        int ret;
 
+       if (selinux_policycap_bpf_token_perms())
+               return 0;
+
        switch (cmd) {
        case BPF_MAP_CREATE:
                ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
@@ -7118,10 +7127,29 @@ static int selinux_bpf_prog(struct bpf_prog *prog)
                            BPF__PROG_RUN, NULL);
 }
 
+static u32 selinux_bpffs_creator_sid(u32 fd)
+{
+       struct path path;
+       struct super_block *sb;
+       struct superblock_security_struct *sbsec;
+
+       CLASS(fd, f)(fd);
+
+       if (fd_empty(f))
+               return SECSID_NULL;
+
+       path = fd_file(f)->f_path;
+       sb = path.dentry->d_sb;
+       sbsec = selinux_superblock(sb);
+
+       return sbsec->creator_sid;
+}
+
 static int selinux_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
                                  struct bpf_token *token, bool kernel)
 {
        struct bpf_security_struct *bpfsec;
+       u32 ssid;
 
        bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
        if (!bpfsec)
@@ -7130,7 +7158,12 @@ static int selinux_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
        bpfsec->sid = current_sid();
        map->security = bpfsec;
 
-       return 0;
+       if (!token)
+               ssid = bpfsec->sid;
+       else
+               ssid = selinux_bpffs_creator_sid(attr->map_token_fd);
+
+       return avc_has_perm(ssid, bpfsec->sid, SECCLASS_BPF, BPF__MAP_CREATE, NULL);
 }
 
 static void selinux_bpf_map_free(struct bpf_map *map)
@@ -7145,6 +7178,7 @@ static int selinux_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
                                 struct bpf_token *token, bool kernel)
 {
        struct bpf_security_struct *bpfsec;
+       u32 ssid;
 
        bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
        if (!bpfsec)
@@ -7153,7 +7187,12 @@ static int selinux_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
        bpfsec->sid = current_sid();
        prog->aux->security = bpfsec;
 
-       return 0;
+       if (!token)
+               ssid = bpfsec->sid;
+       else
+               ssid = selinux_bpffs_creator_sid(attr->prog_token_fd);
+
+       return avc_has_perm(ssid, bpfsec->sid, SECCLASS_BPF, BPF__PROG_LOAD, NULL);
 }
 
 static void selinux_bpf_prog_free(struct bpf_prog *prog)
@@ -7164,10 +7203,15 @@ static void selinux_bpf_prog_free(struct bpf_prog *prog)
        kfree(bpfsec);
 }
 
+#define bpf_token_cmd(T, C) \
+       ((T)->allowed_cmds & (1ULL << (C)))
+
 static int selinux_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
                                    const struct path *path)
 {
        struct bpf_security_struct *bpfsec;
+       u32 sid = selinux_bpffs_creator_sid(attr->token_create.bpffs_fd);
+       int err;
 
        bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
        if (!bpfsec)
@@ -7176,6 +7220,29 @@ static int selinux_bpf_token_create(struct bpf_token *token, union bpf_attr *att
        bpfsec->sid = current_sid();
        token->security = bpfsec;
 
+       bpfsec->perms = 0;
+
+       /**
+        * 'token->allowed_cmds' is a bit mask of allowed commands
+        * Convert the BPF command enum to a bitmask representing its position in the
+        * allowed_cmds bitmap.
+        */
+       if (bpf_token_cmd(token, BPF_MAP_CREATE)) {
+               err = avc_has_perm(bpfsec->sid, sid, SECCLASS_BPF, BPF__MAP_CREATE_AS, NULL);
+               if (err)
+                       return err;
+
+               bpfsec->perms |= BPF__MAP_CREATE;
+       }
+
+       if (bpf_token_cmd(token, BPF_PROG_LOAD)) {
+               err = avc_has_perm(bpfsec->sid, sid, SECCLASS_BPF, BPF__PROG_LOAD_AS, NULL);
+               if (err)
+                       return err;
+
+               bpfsec->perms |= BPF__PROG_LOAD;
+       }
+
        return 0;
 }
 
@@ -7186,6 +7253,49 @@ static void selinux_bpf_token_free(struct bpf_token *token)
        token->security = NULL;
        kfree(bpfsec);
 }
+
+static int selinux_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd)
+{
+       struct bpf_security_struct *bpfsec;
+
+       bpfsec = token->security;
+       switch (cmd) {
+       case BPF_MAP_CREATE:
+               if (!(bpfsec->perms & BPF__MAP_CREATE))
+                       return -EACCES;
+               break;
+       case BPF_PROG_LOAD:
+               if (!(bpfsec->perms & BPF__PROG_LOAD))
+                       return -EACCES;
+               break;
+       default:
+               break;
+       }
+
+       return 0;
+}
+
+static int selinux_bpf_token_capable(const struct bpf_token *token, int cap)
+{
+       u16 sclass;
+       struct bpf_security_struct *bpfsec = token->security;
+       u32 sid = bpfsec->sid;
+       u32 av = CAP_TO_MASK(cap);
+
+       switch (CAP_TO_INDEX(cap)) {
+       case 0:
+               sclass = SECCLASS_CAP_USERNS;
+               break;
+       case 1:
+               sclass = SECCLASS_CAP2_USERNS;
+               break;
+       default:
+               pr_err("SELinux:  out of range capability %d\n", cap);
+               return -EINVAL;
+       }
+
+       return avc_has_perm(sid, sid, sclass, av, NULL);
+}
 #endif
 
 struct lsm_blob_sizes selinux_blob_sizes __ro_after_init = {
@@ -7619,6 +7729,8 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {
        LSM_HOOK_INIT(bpf_map_create, selinux_bpf_map_create),
        LSM_HOOK_INIT(bpf_prog_load, selinux_bpf_prog_load),
        LSM_HOOK_INIT(bpf_token_create, selinux_bpf_token_create),
+       LSM_HOOK_INIT(bpf_token_cmd, selinux_bpf_token_cmd),
+       LSM_HOOK_INIT(bpf_token_capable, selinux_bpf_token_capable),
 #endif
 #ifdef CONFIG_PERF_EVENTS
        LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc),
security/selinux/include/classmap.h +1 -1
index 3ec85142771f..90cb61b16425 100644
@@ -171,7 +171,7 @@ const struct security_class_mapping secclass_map[] = {
        { "infiniband_endport", { "manage_subnet", NULL } },
        { "bpf",
          { "map_create", "map_read", "map_write", "prog_load", "prog_run",
-           NULL } },
+           "map_create_as", "prog_load_as", NULL } },
        { "xdp_socket", { COMMON_SOCK_PERMS, NULL } },
        { "mctp_socket", { COMMON_SOCK_PERMS, NULL } },
        { "perf_event",
security/selinux/include/objsec.h +2 -0
index 1d7ac59015a1..3b41f75cdac9 100644
@@ -87,6 +87,7 @@ struct superblock_security_struct {
        u32 sid; /* SID of file system superblock */
        u32 def_sid; /* default SID for labeling */
        u32 mntpoint_sid; /* SECURITY_FS_USE_MNTPOINT context for files */
+       u32 creator_sid; /* SID of privileged process and is used to verify bpf operations */
        unsigned short behavior; /* labeling behavior */
        unsigned short flags; /* which mount options were specified */
        struct mutex lock;
@@ -164,6 +165,7 @@ struct pkey_security_struct {
 
 struct bpf_security_struct {
        u32 sid; /* SID of bpf obj creator */
+       u32 perms; /* allowed AV permissions, e.g. BPF__MAP_CREATE, for requested bpf commands */
 };
 
 struct perf_event_security_struct {
security/selinux/include/policycap.h +1 -0
index 231d02227e59..dbf39358ae6a 100644
@@ -19,6 +19,7 @@ enum {
        POLICYDB_CAP_GENFS_SECLABEL_WILDCARD,
        POLICYDB_CAP_FUNCTIONFS_SECLABEL,
        POLICYDB_CAP_MEMFD_CLASS,
+       POLICYDB_CAP_BPF_TOKEN_PERMS,
        __POLICYDB_CAP_MAX
 };
 #define POLICYDB_CAP_MAX (__POLICYDB_CAP_MAX - 1)
security/selinux/include/policycap_names.h +1 -0
index 454dab37bda3..6e2b808e12e8 100644
@@ -22,6 +22,7 @@ const char *const selinux_policycap_names[__POLICYDB_CAP_MAX] = {
        "genfs_seclabel_wildcard",
        "functionfs_seclabel",
        "memfd_class",
+       "bpf_token_perms",
 };
 /* clang-format on */
 
security/selinux/include/security.h +6 -1
index 5d1dad8058b1..d1f16d7f684d 100644
@@ -214,6 +214,12 @@ static inline bool selinux_policycap_memfd_class(void)
        return READ_ONCE(selinux_state.policycap[POLICYDB_CAP_MEMFD_CLASS]);
 }
 
+static inline bool selinux_policycap_bpf_token_perms(void)
+{
+       return READ_ONCE(
+               selinux_state.policycap[POLICYDB_CAP_BPF_TOKEN_PERMS]);
+}
+
 struct selinux_policy_convert_data;
 
 struct selinux_load_state {
--

2.50.1

maintainer
#2 lore raw
In reply to Eric Suen
Quoted 74 lines · On Oct 8, 2025 Eric Suen <[email protected]> wrote:

BPF token support was introduced to allow a privileged process to delegate limited BPF functionality—such as map creation and program loading—to an unprivileged process:
https://lore.kernel.org/linux-security-module/[email protected]/

This patch adds SELinux support for controlling BPF token access. With this change, SELinux policies can now enforce constraints on BPF token usage based on both the delegating (privileged) process and the recipient (unprivileged) process.

Supported operations currently include:
- map_create
- prog_load

High-level workflow:
1. An unprivileged process creates a VFS context via fsopen() and obtains a file descriptor.
2. This descriptor is passed to a privileged process, which configures BPF token delegation options and mounts a BPF filesystem.
3. SELinux records the creator_sid of the privileged process during mount setup.
4. The unprivileged process then uses this BPF fs mount to create a token and attach it to subsequent BPF syscalls.
5. During verification of map_create and prog_load, SELinux uses creator_sid and the current SID to check policy permissions via:
avc_has_perm(creator_sid, current_sid, SECCLASS_BPF,
BPF__MAP_CREATE, NULL);

The implementation introduces two new permissions:
- map_create_as
- prog_load_as

At token creation time, SELinux verifies that the current process has the appropriate *_as permission (depending on the allowed_cmds value in the bpf_token) to act on behalf of the creator_sid.

Example SELinux policy:
allow test_bpf_t self:bpf {
map_create map_read map_write prog_load prog_run map_create_as prog_load_as
};

Additionally, a new policy capability bpf_token_perms is added to ensure backward compatibility. If disabled, previous behavior ((checks based on current process SID)) is preserved.

---
Changes in v2:
- Fixed bug in selinux_bpffs_creator_sid(u32 fd) where it retrieved creator_sid from wrong file descriptor
- Removed unnecessary checks for null, per review comments from the first patch

Changes in v3:
- Removed check for 'sid == SECSID_NULL' in selinux_bpf_token_create and allow it to fall through to the permission checks which will fail as access denied to unlabeled_t

Changes in v4:
- Added initialization of creator_sid in selinux_sb_alloc_security
- Enabled handling of creator_sid in selinux_cmp_sb_context and selinux_sb_clone_mnt_opts
- Minor updates based on review comments

Changes in v5:
- Moved to dev-staging branch instead of main
- Added implementation of selinux_bpf_token_capable which is originally from Daniel's patch
https://lore.kernel.org/selinux/[email protected]/

Quoted 7 lines
 security/selinux/hooks.c                   | 116 ++++++++++++++++++++-
 security/selinux/include/classmap.h        |   2 +-
 security/selinux/include/objsec.h          |   2 +
 security/selinux/include/policycap.h       |   1 +
 security/selinux/include/policycap_names.h |   1 +
 security/selinux/include/security.h        |   6 ++
 6 files changed, 125 insertions(+), 3 deletions(-)

FYI, I had to apply a surprising amount of merge fixups due to the allocator changes that went up to Linus during the merge window; I've never seen git put put patch hunks in such odd places before ...

I fixed up several long lines too (lines > 80 chars).

I also gave credit to Daniel in the commit description with the following text:

Credit to Daniel Durning <[email protected]> for his work on the selinux_bpf_token_capable() implementation.

... but I have some questions about the BPF token capability hook, see below.

Quoted 4 lines
security/selinux/hooks.c +4 -0
+static int selinux_bpf_token_capable(const struct bpf_token *token, int cap)
+{
+	u16 sclass;
+	struct bpf_security_struct *bpfsec = token->security;

security/selinux/hooks.c static void selinux_bpf_token_free(struct bpf_token *token) · line 7291

Quoted 10 lines
security/selinux/hooks.c +10 -0
+	u32 sid = bpfsec->sid;
+	u32 av = CAP_TO_MASK(cap);
+
+	switch (CAP_TO_INDEX(cap)) {
+	case 0:
+		sclass = SECCLASS_CAP_USERNS;
+		break;
+	case 1:
+		sclass = SECCLASS_CAP2_USERNS;
+		break;

This assumes that this call will always be made in a non-init userns, I'd rather see us handle arbitrary user namespaces, see bpf_token_capable() for hints.

This is especially important if we consider that the capability being checked doesn't necessarily belong to the current task, but rather the token, which was very likely created in a userns closer to the init ns, more on this below.

security/selinux/hooks.c static void selinux_bpf_token_free(struct bpf_token *token) · line 7298

Quoted 7 lines
security/selinux/hooks.c +7 -0
+	default:
+		pr_err("SELinux:  out of range capability %d\n", cap);
+		return -EINVAL;
+	}
+
+	return avc_has_perm(sid, sid, sclass, av, NULL);
+}

This is surely a copy-n-paste from selinux_capable(), which is a very sensible thing to do, however I do wonder if the special nature of the BPF tokens warrants a change in the subj/obj.

For a normal capability permission check, it makes sense for the subject and object to be the same as the process/subject isn't operating on anything other than itself, it's simply attempting to assert a capability that it has been assigned.

However, I don't believe it is quite as simple for the BPF token capability check. In this case the current process isn't asking if it can assert a capability assigned to itself, it is asking if it can assert a capability assigned to the token. Due to this I'm wondering if we should change the subject to the current task, leaving the object as the token:

  selinux_bpf_token_capable(...)
  {
    struct bpf_security_struct *bpfsec = token->security
    bool initns = (token->userns == &init_user_ns);
    u32 av = CAP_TO_MASK(cap);
    u16 sclass;
    switch (CAP_TO_INDEX(cap)) {
    case 0:
        sclass = initns ? SECCLASS_CAPABILITY : SECCCLASS_CAP_USERNS;
        break;
    ...
    }

    return avc_has_perm(current_sid(), bpfsec->sid, sclass, av, NULL);

}

Thoughts?

--
paul-moore.com

maintainer
#3 lore raw
Quoted 108 lines · Paul Moore
Quoted 74 lines · On Oct 8, 2025 Eric Suen <[email protected]> wrote:

BPF token support was introduced to allow a privileged process to delegate limited BPF functionality—such as map creation and program loading—to an unprivileged process:
https://lore.kernel.org/linux-security-module/[email protected]/

This patch adds SELinux support for controlling BPF token access. With this change, SELinux policies can now enforce constraints on BPF token usage based on both the delegating (privileged) process and the recipient (unprivileged) process.

Supported operations currently include:
- map_create
- prog_load

High-level workflow:
1. An unprivileged process creates a VFS context via fsopen() and obtains a file descriptor.
2. This descriptor is passed to a privileged process, which configures BPF token delegation options and mounts a BPF filesystem.
3. SELinux records the creator_sid of the privileged process during mount setup.
4. The unprivileged process then uses this BPF fs mount to create a token and attach it to subsequent BPF syscalls.
5. During verification of map_create and prog_load, SELinux uses creator_sid and the current SID to check policy permissions via:
avc_has_perm(creator_sid, current_sid, SECCLASS_BPF,
BPF__MAP_CREATE, NULL);

The implementation introduces two new permissions:
- map_create_as
- prog_load_as

At token creation time, SELinux verifies that the current process has the appropriate *_as permission (depending on the allowed_cmds value in the bpf_token) to act on behalf of the creator_sid.

Example SELinux policy:
allow test_bpf_t self:bpf {
map_create map_read map_write prog_load prog_run map_create_as prog_load_as
};

Additionally, a new policy capability bpf_token_perms is added to ensure backward compatibility. If disabled, previous behavior ((checks based on current process SID)) is preserved.

---
Changes in v2:
- Fixed bug in selinux_bpffs_creator_sid(u32 fd) where it retrieved creator_sid from wrong file descriptor
- Removed unnecessary checks for null, per review comments from the first patch

Changes in v3:
- Removed check for 'sid == SECSID_NULL' in selinux_bpf_token_create and allow it to fall through to the permission checks which will fail as access denied to unlabeled_t

Changes in v4:
- Added initialization of creator_sid in selinux_sb_alloc_security
- Enabled handling of creator_sid in selinux_cmp_sb_context and selinux_sb_clone_mnt_opts
- Minor updates based on review comments

Changes in v5:
- Moved to dev-staging branch instead of main
- Added implementation of selinux_bpf_token_capable which is originally from Daniel's patch
https://lore.kernel.org/selinux/[email protected]/

Quoted 7 lines
 security/selinux/hooks.c                   | 116 ++++++++++++++++++++-
 security/selinux/include/classmap.h        |   2 +-
 security/selinux/include/objsec.h          |   2 +
 security/selinux/include/policycap.h       |   1 +
 security/selinux/include/policycap_names.h |   1 +
 security/selinux/include/security.h        |   6 ++
 6 files changed, 125 insertions(+), 3 deletions(-)

FYI, I had to apply a surprising amount of merge fixups due to the allocator changes that went up to Linus during the merge window; I've never seen git put put patch hunks in such odd places before ...

I fixed up several long lines too (lines > 80 chars).

I also gave credit to Daniel in the commit description with the following text:

Credit to Daniel Durning <[email protected]> for his work on the selinux_bpf_token_capable() implementation.

... but I have some questions about the BPF token capability hook, see below.

Quoted 10 lines
security/selinux/hooks.c +10 -0
+static int selinux_bpf_token_capable(const struct bpf_token *token, int cap)
+{
+     u16 sclass;
+     struct bpf_security_struct *bpfsec = token->security;
+     u32 sid = bpfsec->sid;
+     u32 av = CAP_TO_MASK(cap);
+
+     switch (CAP_TO_INDEX(cap)) {
+     case 0:
+             sclass = SECCLASS_CAP_USERNS;
Quoted 8 lines

security/selinux/hooks.c static void selinux_bpf_token_free(struct bpf_token *token) · line 7291

Quoted 4 lines
security/selinux/hooks.c +4 -0
+             break;
+     case 1:
+             sclass = SECCLASS_CAP2_USERNS;
+             break;

This assumes that this call will always be made in a non-init userns, I'd rather see us handle arbitrary user namespaces, see bpf_token_capable() for hints.

Is that possible? bpf_token_create() explicitly disallows creating a BPF token in the init_user_ns.

Quoted 36 lines

This is especially important if we consider that the capability being checked doesn't necessarily belong to the current task, but rather the token, which was very likely created in a userns closer to the init ns, more on this below.

security/selinux/hooks.c static void selinux_bpf_token_free(struct bpf_token *token) · line 7298

Quoted 7 lines
security/selinux/hooks.c +7 -0
+     default:
+             pr_err("SELinux:  out of range capability %d\n", cap);
+             return -EINVAL;
+     }
+
+     return avc_has_perm(sid, sid, sclass, av, NULL);
+}

This is surely a copy-n-paste from selinux_capable(), which is a very sensible thing to do, however I do wonder if the special nature of the BPF tokens warrants a change in the subj/obj.

For a normal capability permission check, it makes sense for the subject and object to be the same as the process/subject isn't operating on anything other than itself, it's simply attempting to assert a capability that it has been assigned.

However, I don't believe it is quite as simple for the BPF token capability check. In this case the current process isn't asking if it can assert a capability assigned to itself, it is asking if it can assert a capability assigned to the token. Due to this I'm wondering if we should change the subject to the current task, leaving the object as the token:

  selinux_bpf_token_capable(...)
  {
    struct bpf_security_struct *bpfsec = token->security
    bool initns = (token->userns == &init_user_ns);
    u32 av = CAP_TO_MASK(cap);
    u16 sclass;
Quoted 8 lines
    switch (CAP_TO_INDEX(cap)) {
    case 0:
        sclass = initns ? SECCLASS_CAPABILITY : SECCCLASS_CAP_USERNS;
        break;
    ...
    }

    return avc_has_perm(current_sid(), bpfsec->sid, sclass, av, NULL);

My understanding, which could be wrong, is that this hook is called outside of process context (or at least outside of the context of either process involved - token creator or token user) since it can be called during any eBPF program and hence we can only use information from the token, not the current process, for our permission checks. We could perhaps pre-compute the capability access vectors at token creation time and cache them for later checking instead.

Quoted 6 lines
  }

Thoughts?

--
paul-moore.com

maintainer
#4 lore raw
Quoted 6 lines · Stephen Smalley
Quoted 5 lines · Paul Moore
Quoted 4 lines · On Oct 8, 2025 Eric Suen <[email protected]> wrote:

BPF token support was introduced to allow a privileged process to delegate limited BPF functionality—such as map creation and program loading—to an unprivileged process ...

...

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
security/selinux/hooks.c +10 -0
+static int selinux_bpf_token_capable(const struct bpf_token *token, int cap)
+{
+     u16 sclass;
+     struct bpf_security_struct *bpfsec = token->security;
+     u32 sid = bpfsec->sid;
+     u32 av = CAP_TO_MASK(cap);
+
+     switch (CAP_TO_INDEX(cap)) {
+     case 0:
+             sclass = SECCLASS_CAP_USERNS;
Quoted 8 lines

security/selinux/hooks.c static void selinux_bpf_token_free(struct bpf_token *token) · line 7291

Quoted 4 lines
security/selinux/hooks.c +4 -0
+             break;
+     case 1:
+             sclass = SECCLASS_CAP2_USERNS;
+             break;

This assumes that this call will always be made in a non-init userns, I'd rather see us handle arbitrary user namespaces, see bpf_token_capable() for hints.

Quoted 2 lines

Is that possible? bpf_token_create() explicitly disallows creating a BPF token in the init_user_ns.

Yes, you are correct, bpf_token_create() blocks creating a token in the init userns, but based on the return codes and comments in the function I worry that this is simply because the BPF devs can't think of a good reason to allow it at the moment, and not because it is something that they want to intentionally block. If we don't have code to handle both the init and non-init cases we might miss a change in bpf_token_create() that could cause us to use the wrong object class in the future.

The change to support both types of user namespaces is relatively small so I'd rather us include it now and have one less bug waiting for us in the future.

Quoted 28 lines
Quoted 19 lines

For a normal capability permission check, it makes sense for the subject and object to be the same as the process/subject isn't operating on anything other than itself, it's simply attempting to assert a capability that it has been assigned.

However, I don't believe it is quite as simple for the BPF token capability check. In this case the current process isn't asking if it can assert a capability assigned to itself, it is asking if it can assert a capability assigned to the token. Due to this I'm wondering if we should change the subject to the current task, leaving the object as the token:

  selinux_bpf_token_capable(...)
  {
    struct bpf_security_struct *bpfsec = token->security
    bool initns = (token->userns == &init_user_ns);
    u32 av = CAP_TO_MASK(cap);
    u16 sclass;
Quoted 8 lines
    switch (CAP_TO_INDEX(cap)) {
    case 0:
        sclass = initns ? SECCLASS_CAPABILITY : SECCCLASS_CAP_USERNS;
        break;
    ...
    }

    return avc_has_perm(current_sid(), bpfsec->sid, sclass, av, NULL);
Quoted 7 lines

My understanding, which could be wrong, is that this hook is called outside of process context (or at least outside of the context of either process involved - token creator or token user) since it can be called during any eBPF program and hence we can only use information from the token, not the current process, for our permission checks. We could perhaps pre-compute the capability access vectors at token creation time and cache them for later checking instead.

If you take a close look at bpf_token_capable() and you follow the bpf_ns_capable() call down to ns_capable_common() you will see that it calls into security_capable() with the current task's creds. I haven't chased all of the callers of bpf_token_capable(), but if there is a case where bpf_token_capable() is being called outside of the current task's process context we have a larger problem that needs to be addressed.

Aside from the calling context, does the subj/obj change sound reasonable? While I think it makes sense, I like getting a sanity check from others on things like this as the impact is significant.

Signature · 1 line
paul-moore.com
maintainer
#5 lore raw
Quoted 91 lines · Paul Moore
Quoted 6 lines · Stephen Smalley
Quoted 5 lines · Paul Moore
Quoted 4 lines · On Oct 8, 2025 Eric Suen <[email protected]> wrote:

BPF token support was introduced to allow a privileged process to delegate limited BPF functionality—such as map creation and program loading—to an unprivileged process ...

...

Quoted 19 lines
Quoted 10 lines
Quoted 10 lines
security/selinux/hooks.c +10 -0
+static int selinux_bpf_token_capable(const struct bpf_token *token, int cap)
+{
+     u16 sclass;
+     struct bpf_security_struct *bpfsec = token->security;
+     u32 sid = bpfsec->sid;
+     u32 av = CAP_TO_MASK(cap);
+
+     switch (CAP_TO_INDEX(cap)) {
+     case 0:
+             sclass = SECCLASS_CAP_USERNS;
Quoted 8 lines

security/selinux/hooks.c static void selinux_bpf_token_free(struct bpf_token *token) · line 7291

Quoted 4 lines
security/selinux/hooks.c +4 -0
+             break;
+     case 1:
+             sclass = SECCLASS_CAP2_USERNS;
+             break;

This assumes that this call will always be made in a non-init userns, I'd rather see us handle arbitrary user namespaces, see bpf_token_capable() for hints.

Quoted 2 lines

Is that possible? bpf_token_create() explicitly disallows creating a BPF token in the init_user_ns.

Yes, you are correct, bpf_token_create() blocks creating a token in the init userns, but based on the return codes and comments in the function I worry that this is simply because the BPF devs can't think of a good reason to allow it at the moment, and not because it is something that they want to intentionally block. If we don't have code to handle both the init and non-init cases we might miss a change in bpf_token_create() that could cause us to use the wrong object class in the future.

The change to support both types of user namespaces is relatively small so I'd rather us include it now and have one less bug waiting for us in the future.

Quoted 28 lines
Quoted 19 lines

For a normal capability permission check, it makes sense for the subject and object to be the same as the process/subject isn't operating on anything other than itself, it's simply attempting to assert a capability that it has been assigned.

However, I don't believe it is quite as simple for the BPF token capability check. In this case the current process isn't asking if it can assert a capability assigned to itself, it is asking if it can assert a capability assigned to the token. Due to this I'm wondering if we should change the subject to the current task, leaving the object as the token:

  selinux_bpf_token_capable(...)
  {
    struct bpf_security_struct *bpfsec = token->security
    bool initns = (token->userns == &init_user_ns);
    u32 av = CAP_TO_MASK(cap);
    u16 sclass;
Quoted 8 lines
    switch (CAP_TO_INDEX(cap)) {
    case 0:
        sclass = initns ? SECCLASS_CAPABILITY : SECCCLASS_CAP_USERNS;
        break;
    ...
    }

    return avc_has_perm(current_sid(), bpfsec->sid, sclass, av, NULL);
Quoted 7 lines

My understanding, which could be wrong, is that this hook is called outside of process context (or at least outside of the context of either process involved - token creator or token user) since it can be called during any eBPF program and hence we can only use information from the token, not the current process, for our permission checks. We could perhaps pre-compute the capability access vectors at token creation time and cache them for later checking instead.

If you take a close look at bpf_token_capable() and you follow the bpf_ns_capable() call down to ns_capable_common() you will see that it calls into security_capable() with the current task's creds. I haven't chased all of the callers of bpf_token_capable(), but if there is a case where bpf_token_capable() is being called outside of the current task's process context we have a larger problem that needs to be addressed.

Quoted 3 lines

Aside from the calling context, does the subj/obj change sound reasonable? While I think it makes sense, I like getting a sanity check from others on things like this as the impact is significant.

I suppose the only residual question is whether the bpffs creator sid should factor into these checks too as it does for the map_create/prog_load ones. Daniel's patch didn't have that construct and hence didn't consider it. If not, then I suppose the proposed subj/obj change is reasonable albeit a bit strange looking to those who are used to capability checks always being task-self checks.

maintainer
#6 lore raw
Quoted 9 lines · Stephen Smalley
Quoted 8 lines · Paul Moore
Quoted 6 lines · Stephen Smalley
Quoted 5 lines · Paul Moore
Quoted 4 lines · On Oct 8, 2025 Eric Suen <[email protected]> wrote:

BPF token support was introduced to allow a privileged process to delegate limited BPF functionality—such as map creation and program loading—to an unprivileged process ...

...

Quoted 44 lines
Quoted 44 lines
Quoted 28 lines
Quoted 19 lines

For a normal capability permission check, it makes sense for the subject and object to be the same as the process/subject isn't operating on anything other than itself, it's simply attempting to assert a capability that it has been assigned.

However, I don't believe it is quite as simple for the BPF token capability check. In this case the current process isn't asking if it can assert a capability assigned to itself, it is asking if it can assert a capability assigned to the token. Due to this I'm wondering if we should change the subject to the current task, leaving the object as the token:

  selinux_bpf_token_capable(...)
  {
    struct bpf_security_struct *bpfsec = token->security
    bool initns = (token->userns == &init_user_ns);
    u32 av = CAP_TO_MASK(cap);
    u16 sclass;
Quoted 8 lines
    switch (CAP_TO_INDEX(cap)) {
    case 0:
        sclass = initns ? SECCLASS_CAPABILITY : SECCCLASS_CAP_USERNS;
        break;
    ...
    }

    return avc_has_perm(current_sid(), bpfsec->sid, sclass, av, NULL);
Quoted 7 lines

My understanding, which could be wrong, is that this hook is called outside of process context (or at least outside of the context of either process involved - token creator or token user) since it can be called during any eBPF program and hence we can only use information from the token, not the current process, for our permission checks. We could perhaps pre-compute the capability access vectors at token creation time and cache them for later checking instead.

If you take a close look at bpf_token_capable() and you follow the bpf_ns_capable() call down to ns_capable_common() you will see that it calls into security_capable() with the current task's creds. I haven't chased all of the callers of bpf_token_capable(), but if there is a case where bpf_token_capable() is being called outside of the current task's process context we have a larger problem that needs to be addressed.

Quoted 8 lines
Quoted 3 lines

Aside from the calling context, does the subj/obj change sound reasonable? While I think it makes sense, I like getting a sanity check from others on things like this as the impact is significant.

I suppose the only residual question is whether the bpffs creator sid should factor into these checks too as it does for the map_create/prog_load ones. Daniel's patch didn't have that construct and hence didn't consider it.

Yes, good point, I was mistakenly thinking that the token's label was from the creator SID, but it isn't as the child namespace creates the token. I think we probably need a grantor_sid field in the bpf_security_struct that is set to the superblock's creator_sid in selinux_bpf_token_create(); the grantor_sid would then be used as the object in the selinux_bpf_token_capable() check.

Quoted 3 lines

If not, then I suppose the proposed
subj/obj change is reasonable albeit a bit strange looking to those who are used to capability checks always being task-self checks.

Yes, I agree it would be a bit unusual, but I'm more concerned with the permission check properly reflecting what is being requested by the kernel. I also tend to think that the bpf token capability check looking a bit unusual may be a positive thing as it will allow policy devs/admins to better distinguish between "normal" capability checks and token-based capability checks.

Signature · 1 line
paul-moore.com
author
#7 lore raw
Quoted 71 lines · Paul Moore
Quoted 8 lines · Stephen Smalley
Quoted 7 lines · Paul Moore
Quoted 5 lines · Stephen Smalley
Quoted 4 lines · Paul Moore
Quoted 3 lines · On Oct 8, 2025 Eric Suen <[email protected]> wrote:

BPF token support was introduced to allow a privileged process to delegate limited BPF functionality—such as map creation and program loading—to an unprivileged process ...

...

Quoted 42 lines
Quoted 42 lines
Quoted 26 lines
Quoted 26 lines

For a normal capability permission check, it makes sense for the subject and object to be the same as the process/subject isn't operating on anything other than itself, it's simply attempting to assert a capability that it has been assigned.

However, I don't believe it is quite as simple for the BPF token capability check. In this case the current process isn't asking if it can assert a capability assigned to itself, it is asking if it can assert a capability assigned to the token. Due to this I'm wondering if we should change the subject to the current task, leaving the object as the token:

   selinux_bpf_token_capable(...)
   {
     struct bpf_security_struct *bpfsec = token->security
     bool initns = (token->userns == &init_user_ns);
     u32 av = CAP_TO_MASK(cap);
     u16 sclass;
     switch (CAP_TO_INDEX(cap)) {
     case 0:
         sclass = initns ? SECCLASS_CAPABILITY : SECCCLASS_CAP_USERNS;
         break;
     ...
     }
Quoted 8 lines
Quoted 1 line
     return avc_has_perm(current_sid(), bpfsec->sid, sclass, av, NULL);

My understanding, which could be wrong, is that this hook is called outside of process context (or at least outside of the context of either process involved - token creator or token user) since it can be called during any eBPF program and hence we can only use information from the token, not the current process, for our permission checks. We could perhaps pre-compute the capability access vectors at token creation time and cache them for later checking instead.

If you take a close look at bpf_token_capable() and you follow the bpf_ns_capable() call down to ns_capable_common() you will see that it calls into security_capable() with the current task's creds. I haven't chased all of the callers of bpf_token_capable(), but if there is a case where bpf_token_capable() is being called outside of the current task's process context we have a larger problem that needs to be addressed.

Quoted 7 lines
Quoted 3 lines

Aside from the calling context, does the subj/obj change sound reasonable? While I think it makes sense, I like getting a sanity check from others on things like this as the impact is significant.

I suppose the only residual question is whether the bpffs creator sid should factor into these checks too as it does for the map_create/prog_load ones. Daniel's patch didn't have that construct and hence didn't consider it.

Yes, good point, I was mistakenly thinking that the token's label was from the creator SID, but it isn't as the child namespace creates the token. I think we probably need a grantor_sid field in the bpf_security_struct that is set to the superblock's creator_sid in selinux_bpf_token_create(); the grantor_sid would then be used as the object in the selinux_bpf_token_capable() check.

Quoted 3 lines

If not, then I suppose the proposed
subj/obj change is reasonable albeit a bit strange looking to those who are used to capability checks always being task-self checks.

Quoted 7 lines

Yes, I agree it would be a bit unusual, but I'm more concerned with the permission check properly reflecting what is being requested by the kernel. I also tend to think that the bpf token capability check looking a bit unusual may be a positive thing as it will allow policy devs/admins to better distinguish between "normal" capability checks and token-based capability checks.

Learned a lot from this great discussion. I will look more into it and send out a new patch.

Paul - to help minimize merge fixups next time, should I base my next revision on the latest dev-staging branch? Not sure if this branch will have all the changes from the merge window.

maintainer
#8 lore raw
Quoted 4 lines · Eric Suen

Paul - to help minimize merge fixups next time, should I base my next revision on the latest dev-staging branch? Not sure if this branch will have all the changes from the merge window.

My policy is generally that patches based either on Linus' tree or the selinux/dev are okay, if any merge fuzzing is needed I'll take care of that. I think the guidance you received to use selinux/dev-staging was simply due to the state of different trees during that particular point in time of development cycle.

For the next revision I would recommend basing your patch on the current selinux/dev branch.

Signature · 1 line
paul-moore.com