selinux: implement bpf_token_cmd and bpf_token_capable hooks
PATCH 1/1 selinux: implement bpf_token_cmd and bpf_token_capable hooks
From: Daniel Durning <[email protected]>
Implement bpf_token_cmd and bpf_token_capable hooks for SELinux. For both hooks we check against the token SID, to support delegation. We could add a further check based on process SID when the token is first created.
--- security/selinux/hooks.c | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)
security/selinux/hooks.c +48 -0
@@ -7154,6 +7154,52 @@ 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 = token->security; +u32 sid = bpfsec->sid; +int ret; + +switch (cmd) { +case BPF_MAP_CREATE : + ret =avc_has_perm (sid, sid,SECCLASS_BPF ,BPF__MAP_CREATE , +NULL ); +break ; +case BPF_PROG_LOAD : + ret =avc_has_perm (sid, sid,SECCLASS_BPF ,BPF__PROG_LOAD , +NULL ); +break ; +default : + ret =0 ; +break ; + } + +return ret; +} + +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 = {
@@ -7525,6 +7571,8 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {LSM_HOOK_INIT (bpf_map_free, selinux_bpf_map_free),LSM_HOOK_INIT (bpf_prog_free, selinux_bpf_prog_free),LSM_HOOK_INIT (bpf_token_free, selinux_bpf_token_free), +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
Signature · 1 line
2.50.1
Quoted 7 lines · On Fri, Aug 1, 2025 at 11:49 AM <[email protected]> wrote:
From: Daniel Durning <[email protected]>
Implement
bpf_token_cmdandbpf_token_capablehooks for SELinux. For both hooks we check against the token SID, to support delegation. We could add a further check based on process SID when the token is first created.
I agree with the approach - it is consistent with how tokens are used. I suppose we could perform a process-based check in selinux_bpf_token_create() if it was generic, or add one to each of these hooks if we want a different permission for different cmd or cap values.
We may need to wrap these checks with a new policy capability (see security/selinux/include/policycap.h and related code) to avoid breaking compatibility on existing systems using BPF tokens, if any.
Quoted 2 lines
Signed-off-by Daniel Durning <[email protected]>
Quoted 73 lines
--- security/selinux/hooks.c | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)security/selinux/hooks.c +48
-1@@ -7154,6 +7154,52 @@ 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 = token->security; +u32 sid = bpfsec->sid; +int ret; + +switch (cmd) { +case BPF_MAP_CREATE : + ret =avc_has_perm (sid, sid,SECCLASS_BPF ,BPF__MAP_CREATE , +NULL ); +break ; +case BPF_PROG_LOAD : + ret =avc_has_perm (sid, sid,SECCLASS_BPF ,BPF__PROG_LOAD , +NULL ); +break ; +default : + ret =0 ; +break ; + } + +return ret; +} + +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 = {@@ -7525,6 +7571,8 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {LSM_HOOK_INIT (bpf_map_free, selinux_bpf_map_free),LSM_HOOK_INIT (bpf_prog_free, selinux_bpf_prog_free),LSM_HOOK_INIT (bpf_token_free, selinux_bpf_token_free), +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 --2.50.1
Stephen Smalley
maintainerQuoted 10 lines · Stephen Smalley
Quoted 7 lines · On Fri, Aug 1, 2025 at 11:49 AM <[email protected]> wrote:
From: Daniel Durning <[email protected]>
Implement
bpf_token_cmdandbpf_token_capablehooks for SELinux. For both hooks we check against the token SID, to support delegation. We could add a further check based on process SID when the token is first created.
Quoted 5 lines
I agree with the approach - it is consistent with how tokens are used. I suppose we could perform a process-based check in
selinux_bpf_token_create()if it was generic, or add one to each of these hooks if we want a different permission for different cmd or cap values.
Ah, actually that doesn't look viable at least for the token capable checks, since those look like they can occur from outside of process context if I am reading the code correctly.
Quoted 84 lines
We may need to wrap these checks with a new policy capability (see
security/selinux/include/policycap.hand related code) to avoid breaking compatibility on existing systems using BPF tokens, if any.Quoted 2 lines
Signed-off-by Daniel Durning <[email protected]>Reviewed-by Stephen Smalley <[email protected]>Tested-by Stephen Smalley <[email protected]>Quoted 73 lines
--- security/selinux/hooks.c | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)security/selinux/hooks.c +48
-1@@ -7154,6 +7154,52 @@ 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 = token->security; +u32 sid = bpfsec->sid; +int ret; + +switch (cmd) { +case BPF_MAP_CREATE : + ret =avc_has_perm (sid, sid,SECCLASS_BPF ,BPF__MAP_CREATE , +NULL ); +break ; +case BPF_PROG_LOAD : + ret =avc_has_perm (sid, sid,SECCLASS_BPF ,BPF__PROG_LOAD , +NULL ); +break ; +default : + ret =0 ; +break ; + } + +return ret; +} + +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 = {@@ -7525,6 +7571,8 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {LSM_HOOK_INIT (bpf_map_free, selinux_bpf_map_free),LSM_HOOK_INIT (bpf_prog_free, selinux_bpf_prog_free),LSM_HOOK_INIT (bpf_token_free, selinux_bpf_token_free), +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 --2.50.1
Paul Moore
maintainerQuoted 18 lines · Stephen Smalley
Quoted 10 lines · Stephen Smalley
Quoted 7 lines · On Fri, Aug 1, 2025 at 11:49 AM <[email protected]> wrote:
From: Daniel Durning <[email protected]>
Implement
bpf_token_cmdandbpf_token_capablehooks for SELinux. For both hooks we check against the token SID, to support delegation. We could add a further check based on process SID when the token is first created.Quoted 5 lines
I agree with the approach - it is consistent with how tokens are used. I suppose we could perform a process-based check in
selinux_bpf_token_create()if it was generic, or add one to each of these hooks if we want a different permission for different cmd or cap values.
Quoted 4 lines
Ah, actually that doesn't look viable at least for the token capable checks, since those look like they can occur from outside of process context if I am reading the code correctly.
Quoted 1 line
Quoted 1 line
Eric Suen has also been working on a SELinux BPF token implementation too, and I believe he is planning to post it upstream for review soon.
I do expect we'll need to have a discussion about the access controls, especially the labels involved, as BPF tokens are quite bit different from many of the other SELinux access controls.
--
paul-moore.com
Stephen Smalley
maintainerQuoted 26 lines · Paul Moore
Quoted 18 lines · Stephen Smalley
Quoted 10 lines · Stephen Smalley
Quoted 7 lines · On Fri, Aug 1, 2025 at 11:49 AM <[email protected]> wrote:
From: Daniel Durning <[email protected]>
Implement
bpf_token_cmdandbpf_token_capablehooks for SELinux. For both hooks we check against the token SID, to support delegation. We could add a further check based on process SID when the token is first created.Quoted 5 lines
I agree with the approach - it is consistent with how tokens are used. I suppose we could perform a process-based check in
selinux_bpf_token_create()if it was generic, or add one to each of these hooks if we want a different permission for different cmd or cap values.Quoted 4 lines
Ah, actually that doesn't look viable at least for the token capable checks, since those look like they can occur from outside of process context if I am reading the code correctly.
Quoted 1 line
Quoted 1 line
Quoted 6 lines
Eric Suen has also been working on a SELinux BPF token implementation too, and I believe he is planning to post it upstream for review soon.
I do expect we'll need to have a discussion about the access controls, especially the labels involved, as BPF tokens are quite bit different from many of the other SELinux access controls.
Eric - note that Daniel also posted a patch for the selinux-testsuite to exercise these hooks and checks based on the Linux kernel self-test for bpf tokens, see
https://lore.kernel.org/selinux/CAEjxPJ7DBDnZEFvgpe58K4B+4kZdOqUGMHvGC2vKt-4Zget=Hg@mail.gmail.com/T/#t
Paul - it would be good to avoid such duplication of effort in the future, maybe we should be tracking such things in the GitHub project?
Quoted 6 lines
--
paul-moore.com
Stephen Smalley
maintainerQuoted 16 lines · Stephen Smalley
Quoted 7 lines · On Fri, Aug 1, 2025 at 11:49 AM <[email protected]> wrote:
From: Daniel Durning <[email protected]>
Implement
bpf_token_cmdandbpf_token_capablehooks for SELinux. For both hooks we check against the token SID, to support delegation. We could add a further check based on process SID when the token is first created.I agree with the approach - it is consistent with how tokens are used. I suppose we could perform a process-based check in
selinux_bpf_token_create()if it was generic, or add one to each of these hooks if we want a different permission for different cmd or cap values.
Quoted 3 lines
We may need to wrap these checks with a new policy capability (see
security/selinux/include/policycap.hand related code) to avoid breaking compatibility on existing systems using BPF tokens, if any.
I have added some notes with links to examples of how to add a new SELinux policy capability to the SELinux kernel Getting Started guide at:
https://github.com/SELinuxProject/selinux-kernel/wiki/Getting-Started#adding-a-new-selinux-policy-capability
Quoted 80 lines
Quoted 3 lines
Signed-off-by Daniel Durning <[email protected]>Reviewed-by Stephen Smalley <[email protected]>Tested-by Stephen Smalley <[email protected]>Quoted 73 lines
--- security/selinux/hooks.c | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)security/selinux/hooks.c +48
-1@@ -7154,6 +7154,52 @@ 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 = token->security; +u32 sid = bpfsec->sid; +int ret; + +switch (cmd) { +case BPF_MAP_CREATE : + ret =avc_has_perm (sid, sid,SECCLASS_BPF ,BPF__MAP_CREATE , +NULL ); +break ; +case BPF_PROG_LOAD : + ret =avc_has_perm (sid, sid,SECCLASS_BPF ,BPF__PROG_LOAD , +NULL ); +break ; +default : + ret =0 ; +break ; + } + +return ret; +} + +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 = {@@ -7525,6 +7571,8 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {LSM_HOOK_INIT (bpf_map_free, selinux_bpf_map_free),LSM_HOOK_INIT (bpf_prog_free, selinux_bpf_prog_free),LSM_HOOK_INIT (bpf_token_free, selinux_bpf_token_free), +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 --2.50.1
Paul Moore
maintainerQuoted 5 lines · Stephen Smalley
Eric - note that Daniel also posted a patch for the selinux-testsuite to exercise these hooks and checks based on the Linux kernel self-test for bpf tokens, see
https://lore.kernel.org/selinux/CAEjxPJ7DBDnZEFvgpe58K4B+4kZdOqUGMHvGC2vKt-4Zget=Hg@mail.gmail.com/T/#t
FWIW, I believe Eric has some basic tests too, although I will admit to losing track of that aspect, as we have had several months of setbacks lately due to package building, email, etc.
Quoted 2 lines
Paul - it would be good to avoid such duplication of effort in the future, maybe we should be tracking such things in the GitHub project?
Yes, it's unfortunate when we see duplicated work, but thankfully it happens very rarely in our case. We can track things on GitHub, but with development happening largely on the mailing list I'm skeptical about how successful that will end up being. Our GH related efforts have been very mixed thus far. Another option might simply be to tell people to announce a development effort on the mailing list, although I can see that having problems too.
If there are some positives, it may be that both Daniel and Eric's work are still in the early stages, so there is likely room for the two of them to cooperate together on a solution. Daniel, Eric, what do you think about that?
Signature · 1 line
paul-moore.com
Quoted 24 lines · Paul Moore
Quoted 5 lines · Stephen Smalley
Eric - note that Daniel also posted a patch for the selinux-testsuite to exercise these hooks and checks based on the Linux kernel self-test for bpf tokens, see
https://lore.kernel.org/selinux/CAEjxPJ7DBDnZEFvgpe58K4B+4kZdOqUGMHvGC2vKt-4Zget=Hg@mail.gmail.com/T/#tFWIW, I believe Eric has some basic tests too, although I will admit to losing track of that aspect, as we have had several months of setbacks lately due to package building, email, etc.
Quoted 2 lines
Paul - it would be good to avoid such duplication of effort in the future, maybe we should be tracking such things in the GitHub project?
Yes, it's unfortunate when we see duplicated work, but thankfully it happens very rarely in our case. We can track things on GitHub, but with development happening largely on the mailing list I'm skeptical about how successful that will end up being. Our GH related efforts have been very mixed thus far. Another option might simply be to tell people to announce a development effort on the mailing list, although I can see that having problems too.
Quoted 4 lines
If there are some positives, it may be that both Daniel and Eric's work are still in the early stages, so there is likely room for the two of them to cooperate together on a solution. Daniel, Eric, what do you think about that?
I would be happy to work with Eric on a solution. Looking forward to seeing his patch once it gets posted.
Quoted 21 lines · Daniel Durning
Quoted 20 lines · Paul Moore
Quoted 4 lines · Stephen Smalley
Eric - note that Daniel also posted a patch for the selinux-testsuite to exercise these hooks and checks based on the Linux kernel self-test for bpf tokens, see
https://lore.kernel.org/selinux/CAEjxPJ7DBDnZEFvgpe58K4B+4kZdOqUGMHvGC2vKt-4Zget=Hg@mail.gmail.com/T/#tFWIW, I believe Eric has some basic tests too, although I will admit to losing track of that aspect, as we have had several months of setbacks lately due to package building, email, etc.
Quoted 2 lines
Paul - it would be good to avoid such duplication of effort in the future, maybe we should be tracking such things in the GitHub project?
Yes, it's unfortunate when we see duplicated work, but thankfully it happens very rarely in our case. We can track things on GitHub, but with development happening largely on the mailing list I'm skeptical about how successful that will end up being. Our GH related efforts have been very mixed thus far. Another option might simply be to tell people to announce a development effort on the mailing list, although I can see that having problems too.
Quoted 6 lines
Quoted 4 lines
If there are some positives, it may be that both Daniel and Eric's work are still in the early stages, so there is likely room for the two of them to cooperate together on a solution. Daniel, Eric, what do you think about that?
I would be happy to work with Eric on a solution. Looking forward to seeing his patch once it gets posted.
Thanks a lot, Daniel. I was actually hesitant to send out my changes since you
already have test changes prepared, and I didn’t want to step on your toes.
Really appreciate your openness to collaborate.
I'll send out my patch soon and make sure to reference your test work in the
description.
Quoted 32 lines · Eric Suen
Quoted 22 lines · Daniel Durning
Quoted 21 lines · Paul Moore
Quoted 5 lines · Stephen Smalley
Eric - note that Daniel also posted a patch for the selinux-testsuite to exercise these hooks and checks based on the Linux kernel self-test for bpf tokens, see
https://lore.kernel.org/selinux/CAEjxPJ7DBDnZEFvgpe58K4B+4kZdOqUGMHvGC2vKt-4Zget=Hg@mail.gmail.com/T/#tFWIW, I believe Eric has some basic tests too, although I will admit to losing track of that aspect, as we have had several months of setbacks lately due to package building, email, etc.
Quoted 2 lines
Paul - it would be good to avoid such duplication of effort in the future, maybe we should be tracking such things in the GitHub project?
Yes, it's unfortunate when we see duplicated work, but thankfully it happens very rarely in our case. We can track things on GitHub, but with development happening largely on the mailing list I'm skeptical about how successful that will end up being. Our GH related efforts have been very mixed thus far. Another option might simply be to tell people to announce a development effort on the mailing list, although I can see that having problems too.
Quoted 6 lines
Quoted 4 lines
If there are some positives, it may be that both Daniel and Eric's work are still in the early stages, so there is likely room for the two of them to cooperate together on a solution. Daniel, Eric, what do you think about that?
I would be happy to work with Eric on a solution. Looking forward to seeing his patch once it gets posted.
Thanks a lot, Daniel. I was actually hesitant to send out my changes since you
Quoted 9 lines
already have test changes prepared, and I didn’t want to step on your toes.
Really appreciate your openness to collaborate.
I'll send out my patch soon and make sure to reference your test work in the
description.
Daniel - I just posted my patch. It's available here: https://lore.kernel.org/selinux/[email protected]/
Over the next couple of days, I'll build and run your test changes against it to check for any unexpected issues.