selinux: fix BPF token permission checks
Paul Moore
maintainerPATCH 1/1 selinux: fix BPF token permission checks
Avoid multiple lookups of the bpffs creator SID using the token's file descriptor when the same information can be found via the resolved path/dentry (in selinux_bpf_token_create()) or the token itself (in selinux_bpf_map_create() and selinux_bpf_prog_load()). Not only does this simplify the code, it avoids potential TOCTOU issues if the user changes the token file descriptor passed into the kernel.
--- security/selinux/hooks.c | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-)
security/selinux/hooks.c +10 -26
@@ -7267,24 +7267,6 @@ 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) {
@@ -7297,7 +7279,7 @@ static int selinux_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,if (!token) ssid = bpfsec->sid;else - ssid =selinux_bpffs_creator_sid (attr->map_token_fd); + ssid =selinux_bpf_token_security (token)->grantor_sid;return avc_has_perm (ssid, bpfsec->sid,SECCLASS_BPF ,BPF__MAP_CREATE ,NULL );
@@ -7315,7 +7297,7 @@ static int selinux_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,if (!token) ssid = bpfsec->sid;else - ssid =selinux_bpffs_creator_sid (attr->prog_token_fd); + ssid =selinux_bpf_token_security (token)->grantor_sid;return avc_has_perm (ssid, bpfsec->sid,SECCLASS_BPF ,BPF__PROG_LOAD ,NULL );
@@ -7329,12 +7311,14 @@ static int selinux_bpf_token_create(struct bpf_token *token, const struct path *path) {struct bpf_security_struct *bpfsec; -u32 sid =selinux_bpffs_creator_sid (attr->token_create.bpffs_fd); +struct superblock_security_struct *sbsec;int err; + sbsec =selinux_superblock (path->dentry->d_sb); + bpfsec =selinux_bpf_token_security (token); bpfsec->sid =current_sid (); - bpfsec->grantor_sid = sid; + bpfsec->grantor_sid = sbsec->creator_sid; bpfsec->perms =0 ;/**
@@ -7343,15 +7327,15 @@ static int selinux_bpf_token_create(struct bpf_token *token,* 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 ); + err =avc_has_perm (bpfsec->sid, bpfsec->grantor_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 ); + err =avc_has_perm (bpfsec->sid, bpfsec->grantor_sid, +SECCLASS_BPF ,BPF__PROG_LOAD_AS ,NULL );if (err)return err; bpfsec->perms |=BPF__PROG_LOAD ;
Signature · 1 line
2.55.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
Avoid multiple lookups of the bpffs creator SID using the token's file descriptor when the same information can be found via the resolved path/dentry (in
selinux_bpf_token_create()) or the token itself (inselinux_bpf_map_create()andselinux_bpf_prog_load()). Not only does this simplify the code, it avoids potential TOCTOU issues if the user changes the token file descriptor passed into the kernel.Fixes 5473a722f782 ("selinux: add support for BPF token access control")Signed-off-by Paul Moore <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 11 lines · Paul Moore
Avoid multiple lookups of the bpffs creator SID using the token's file descriptor when the same information can be found via the resolved path/dentry (in
selinux_bpf_token_create()) or the token itself (inselinux_bpf_map_create()andselinux_bpf_prog_load()). Not only does this simplify the code, it avoids potential TOCTOU issues if the user changes the token file descriptor passed into the kernel.Fixes 5473a722f782 ("selinux: add support for BPF token access control")Signed-off-by Paul Moore <[email protected]>
Paul Moore
maintainerQuoted 9 lines · Stephen Smalley
Quoted 8 lines · Paul Moore
Avoid multiple lookups of the bpffs creator SID using the token's file descriptor when the same information can be found via the resolved path/dentry (in
selinux_bpf_token_create()) or the token itself (inselinux_bpf_map_create()andselinux_bpf_prog_load()). Not only does this simplify the code, it avoids potential TOCTOU issues if the user changes the token file descriptor passed into the kernel.
Quoted 6 lines
Quoted 3 lines
Fixes 5473a722f782 ("selinux: add support for BPF token access control")Signed-off-by Paul Moore <[email protected]>Reviewed-by Stephen Smalley <[email protected]>Tested-by Stephen Smalley <[email protected]>
Thanks.
Given that v7.3-rc1 is due in a few days I'm going to merge this into selinux/dev-staging for now and will move it to selinux/stable-7.3 once v7.3-rc1 is released.
Signature · 1 line
paul-moore.com
Paul Moore
maintainerQuoted 15 lines · Paul Moore
Quoted 13 lines · Stephen Smalley
Quoted 11 lines · Paul Moore
Avoid multiple lookups of the bpffs creator SID using the token's file descriptor when the same information can be found via the resolved path/dentry (in
selinux_bpf_token_create()) or the token itself (inselinux_bpf_map_create()andselinux_bpf_prog_load()). Not only does this simplify the code, it avoids potential TOCTOU issues if the user changes the token file descriptor passed into the kernel.Fixes 5473a722f782 ("selinux: add support for BPF token access control")Signed-off-by Paul Moore <[email protected]>
Quoted 8 lines
Quoted 2 lines
Reviewed-by Stephen Smalley <[email protected]>Tested-by Stephen Smalley <[email protected]>Thanks.
Given that v7.3-rc1 is due in a few days I'm going to merge this into selinux/dev-staging for now and will move it to selinux/stable-7.3 once v7.3-rc1 is released.
This is now in selinux/stable-7.3 and will be sent up to Linus later this week assuming no problems are found in linux-next.
Signature · 1 line
paul-moore.com