bpf: add access control for map
Subsystems (4)
PATCH 1/1 bpf: add access control for map
From: Menglong Dong <[email protected]>
Hello,
I have a idea about the access control of eBPF map, could you help to see if it works?
For now, only users with the CAP_SYS_ADMIN or CAP_BPF have the right to access the data in eBPF maps. So I'm thinking, are there any way to control the access to the maps, just like what we do to files? Therefore, we can decide who have the right to read the map and who can write.
I think it is useful in some case. For example, I made a eBPF-based network statistics program, and the information is stored in an array map. And I want all users can read the information in the map, without changing the capacity of them. As the information is iunsensitive, normal users can read it. This make publish-consume mode possible, the eBPF program is publisher and the user space program is consumer.
So this aim can be achieve, if we can control the access of maps as a file. There are many ways I thought, and I choosed one to implement:
While pining the map, add the inode that is created to a list on the map. root can change the permission of the inode through the pin path. Therefore, we can try to find the inode corresponding to current user namespace in the list, and check whether user have permission to read or write.
The steps can be:
1. create the map with BPF_F_UMODE flags, which imply that enable access control in this map.
2. load and pin the map on /sys/fs/bpf/xxx.
3. change the umode of /sys/fs/bpf/xxx with 'chmod 744 /sys/fs/bpf/xxx', therefor all user can read the map.
I'm not sure if there is already way to achieve this aim, this is just a idea and the code is totally not ok (it will panic when unpin the map, seems the usage of my RCU lock is wrong)
--- include/linux/bpf.h | 7 ++++ include/uapi/linux/bpf.h | 1 + kernel/bpf/arraymap.c | 2 +- kernel/bpf/inode.c | 60 +++++++++++++++++++++++++++++++--- kernel/bpf/syscall.c | 69 +++++++++++++++++++++++++++++++++++++--- 5 files changed, 130 insertions(+), 9 deletions(-)
include/linux/bpf.h +7 -0
@@ -190,6 +190,11 @@ struct bpf_map_off_arr {u8 field_sz[BPF_MAP_OFF_ARR_MAX ]; }; +struct bpf_map_inode { +struct list_head list; +struct inode *inode; +}; +struct bpf_map {/* The first two cachelines with read-mostly members of which some * are also accessed in fast-path (e.g. ops, max_entries).
@@ -205,6 +210,7 @@ struct bpf_map {u32 max_entries;u64 map_extra;/* any per-map-type extra fields */ u32 map_flags; +struct list_head inode_list;int spin_lock_off;/* >=0 valid offset, <0 error */ struct bpf_map_value_off *kptr_off_tab;int timer_off;/* >=0 valid offset, <0 error */
@@ -2345,5 +2351,6 @@ bool btf_id_set_contains(const struct btf_id_set *set, u32 id);int bpf_bprintf_prepare (char *fmt,u32 fmt_size,const u64 *raw_args,u32 **bin_buf,u32 num_args);void bpf_bprintf_cleanup (void ); +int bpf_map_permission (struct bpf_map *map,int flags);#endif /* _LINUX_BPF_H */
include/uapi/linux/bpf.h +1 -0
@@ -1225,6 +1225,7 @@ enum {/* Create a map that is suitable to be an inner map with dynamic max entries */ BPF_F_INNER_MAP = (1U <<12 ), + BPF_F_UMODE = (1U << 13), };/* Flags for BPF_PROG_QUERY. */
kernel/bpf/arraymap.c +1 -1
@@ -17,7 +17,7 @@#define ARRAY_CREATE_FLAG_MASK \ (BPF_F_NUMA_NODE | BPF_F_MMAPABLE | BPF_F_ACCESS_MASK | \ - BPF_F_PRESERVE_ELEMS | BPF_F_INNER_MAP) + BPF_F_PRESERVE_ELEMS | BPF_F_INNER_MAP | BPF_F_UMODE)static void bpf_array_free_percpu (struct bpf_array *array) {
kernel/bpf/inode.c +56 -4
94 lines not on this page · raw message
kernel/bpf/syscall.c +65 -4
116 lines not on this page · raw message
Signature · 1 line
2.36.1
Quoted 11 lines · On Mon, May 16, 2022 at 8:44 PM <[email protected]> wrote:
From: Menglong Dong <[email protected]>
Hello,
I have a idea about the access control of eBPF map, could you help to see if it works?
For now, only users with the
CAP_SYS_ADMINorCAP_BPFhave the right to access the data in eBPF maps. So I'm thinking, are there any way to control the access to the maps, just like what we do to files?
The bpf objects pinned in bpffs should always be accessible as files regardless of sysctl or cap-s.
Quoted 9 lines
Therefore, we can decide who have the right to read the map and who can write.
I think it is useful in some case. For example, I made a eBPF-based network statistics program, and the information is stored in an array map. And I want all users can read the information in the map, without changing the capacity of them. As the information is iunsensitive, normal users can read it. This make publish-consume mode possible, the eBPF program is publisher and the user space program is consumer.
Right. It is a choice of the bpf prog which data expose in the map.
Quoted 9 lines
So this aim can be achieve, if we can control the access of maps as a file. There are many ways I thought, and I choosed one to implement:
While pining the map, add the inode that is created to a list on the map. root can change the permission of the inode through the pin path. Therefore, we can try to find the inode corresponding to current user namespace in the list, and check whether user have permission to read or write.
Quoted 7 lines
The steps can be:
1. create the map with
BPF_F_UMODEflags, which imply that enable access control in this map.
2. load and pin the map on/sys/fs/bpf/xxx.
3. change the umode of/sys/fs/bpf/xxxwith 'chmod 744/sys/fs/bpf/xxx', therefor all user can read the map.
This behavior should be available by default. Only sysctl was preventing it. It's being fixed by the following patch. Please take a look at:
https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
Does it solve your use case?
kernel/bpf/inode.c int bpf_obj_get_user(const char __user *pathname, int flags) · line 561
Quoted 7 lines
kernel/bpf/inode.c +2
-1@@ -542,14 +557,26 @@ int bpf_obj_get_user(const char __user *pathname, int flags)if (IS_ERR (raw))return PTR_ERR (raw); -if (type ==BPF_TYPE_PROG ) +if (type !=BPF_TYPE_MAP && !bpf_capable ()) +return -EPERM ;
obj_get already implements normal ACL style access to files. Let's not fragment this security model with extra cap checks.
kernel/bpf/inode.c int bpf_obj_get_user(const char __user *pathname, int flags) · line 571
Quoted 11 lines
kernel/bpf/inode.c +9
-1+ +switch (type) { +case BPF_TYPE_PROG : ret =bpf_prog_new_fd (raw); - else if (type == BPF_TYPE_MAP) +break ; +case BPF_TYPE_MAP : +if (bpf_map_permission (raw, f_flags)) { +bpf_any_put (raw, type); +return -EPERM ; + }
bpf_obj_do_get() already does such check.
Quoted 13 lines
kernel/bpf/syscall.c +13
-0+int bpf_map_permission (struct bpf_map *map,int flags) +{ +struct bpf_map_inode *map_inode; +struct user_namespace *ns; + +if (capable (CAP_SYS_ADMIN )) +return 0 ; + +if (!(map->map_flags &BPF_F_UMODE )) +return -1 ; + +rcu_read_lock (); +list_for_each_entry_rcu (map_inode, &map->inode_list, list) {
kernel/bpf/syscall.c static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) · line 3745
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+ ns = map_inode->inode->i_sb->s_user_ns; +if (ns ==current_user_ns ()) + goto found; + } +rcu_read_unlock (); +return -1 ; +found: +rcu_read_unlock (); +return inode_permission (ns, map_inode->inode,ACC_MODE (flags)); +}
See path_permission() in bpf_obj_do_get().
kernel/bpf/syscall.c static int bpf_map_get_fd_by_id(const union bpf_attr *attr) · line 3725
Quoted 8 lines
static int
bpf_map_get_fd_by_id(const union bpf_attr *attr)kernel/bpf/syscall.c +0
-3@@ -3720,9 +3757,6 @@ static int bpf_map_get_fd_by_id(const union bpf_attr *attr) attr->open_flags & ~BPF_OBJ_FLAG_MASK)return -EINVAL ; -if (!capable (CAP_SYS_ADMIN )) -return -EPERM ; -
This part we cannot relax.
What you're trying to do is to bypass path checks by pointing at a map with its ID only.
That contradicts to your official goal in the cover letter.
bpf_map_get_fd_by_id() has to stay cap_sys_admin only. Exactly for the reason that bpf subsystem has file ACL style. fd_by_id is a debug interface used by tools like bpftool and root admin that needs to see the system as a whole. Normal tasks/processes need to use bpffs and pin files with correct permissions to pass maps from one process to another. Or use FD passing kernel facilities.
Quoted 46 lines · Alexei Starovoitov
Quoted 11 lines · On Mon, May 16, 2022 at 8:44 PM <[email protected]> wrote:
From: Menglong Dong <[email protected]>
Hello,
I have a idea about the access control of eBPF map, could you help to see if it works?
For now, only users with the
CAP_SYS_ADMINorCAP_BPFhave the right to access the data in eBPF maps. So I'm thinking, are there any way to control the access to the maps, just like what we do to files?The bpf objects pinned in bpffs should always be accessible as files regardless of sysctl or cap-s.
Quoted 9 lines
Therefore, we can decide who have the right to read the map and who can write.
I think it is useful in some case. For example, I made a eBPF-based network statistics program, and the information is stored in an array map. And I want all users can read the information in the map, without changing the capacity of them. As the information is iunsensitive, normal users can read it. This make publish-consume mode possible, the eBPF program is publisher and the user space program is consumer.
Right. It is a choice of the bpf prog which data expose in the map.
Quoted 9 lines
So this aim can be achieve, if we can control the access of maps as a file. There are many ways I thought, and I choosed one to implement:
While pining the map, add the inode that is created to a list on the map. root can change the permission of the inode through the pin path. Therefore, we can try to find the inode corresponding to current user namespace in the list, and check whether user have permission to read or write.
Quoted 7 lines
The steps can be:
1. create the map with
BPF_F_UMODEflags, which imply that enable access control in this map.
2. load and pin the map on/sys/fs/bpf/xxx.
3. change the umode of/sys/fs/bpf/xxxwith 'chmod 744/sys/fs/bpf/xxx', therefor all user can read the map.
Quoted 6 lines
This behavior should be available by default. Only sysctl was preventing it. It's being fixed by the following patch. Please take a look at:
https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/Does it solve your use case?
Yeah, it seems this patch gives another way: give users all access to bpf commands (except map_create and prog_load). Therefore, users that have the access to the pin path have corresponding r/w of the eBPF object. This patch can cover my case.
However, this seems to give users too much permission (or the access check is not enough?) I have do a test:
1. load a ebpf program of type cgroup and pin it on /sys/fs/bpf/post_bind as root.
2. give users access to read /sys/fs/bpf/post_bind
3. Now, all users can attach or detach the eBPF program to /sys/fs/cgroup/, who have only read access to the ebpf and the cgroup.
I think there are many such cases. Is this fine?
Quoted 5 lines
Quoted 5 lines
kernel/bpf/inode.c +0
-0@@ -542,14 +557,26 @@ int bpf_obj_get_user(const char __user *pathname, int flags)if (IS_ERR (raw))return PTR_ERR (raw);
Quoted 7 lines
kernel/bpf/inode.c int bpf_obj_get_user(const char __user *pathname, int flags) · line 561
Quoted 3 lines
kernel/bpf/inode.c +2
-1-if (type ==BPF_TYPE_PROG ) +if (type !=BPF_TYPE_MAP && !bpf_capable ()) +return -EPERM ;
obj_getalready implements normal ACL style access to files. Let's not fragment this security model with extra cap checks.
Yeah, my way is too rough.
Quoted 12 lines
kernel/bpf/inode.c int bpf_obj_get_user(const char __user *pathname, int flags) · line 571
Quoted 11 lines
kernel/bpf/inode.c +9
-1+ +switch (type) { +case BPF_TYPE_PROG : ret =bpf_prog_new_fd (raw); - else if (type == BPF_TYPE_MAP) +break ; +case BPF_TYPE_MAP : +if (bpf_map_permission (raw, f_flags)) { +bpf_any_put (raw, type); +return -EPERM ; + }
Quoted 2 lines
bpf_obj_do_get()already does such check.
With the patch you mentioned above, now bpf_obj_do_get() can do this job, as normal users can also get there too.
Quoted 40 lines
Quoted 13 lines
kernel/bpf/syscall.c +13
-0+int bpf_map_permission (struct bpf_map *map,int flags) +{ +struct bpf_map_inode *map_inode; +struct user_namespace *ns; + +if (capable (CAP_SYS_ADMIN )) +return 0 ; + +if (!(map->map_flags &BPF_F_UMODE )) +return -1 ; + +rcu_read_lock (); +list_for_each_entry_rcu (map_inode, &map->inode_list, list) {kernel/bpf/syscall.c static int bpf_prog_get_fd_by_id(const union bpf_attr *attr) · line 3745
Quoted 10 lines
kernel/bpf/syscall.c +10
-0+ ns = map_inode->inode->i_sb->s_user_ns; +if (ns ==current_user_ns ()) + goto found; + } +rcu_read_unlock (); +return -1 ; +found: +rcu_read_unlock (); +return inode_permission (ns, map_inode->inode,ACC_MODE (flags)); +}See
path_permission()inbpf_obj_do_get().kernel/bpf/syscall.c static int bpf_map_get_fd_by_id(const union bpf_attr *attr) · line 3725
Quoted 8 lines
static int
bpf_map_get_fd_by_id(const union bpf_attr *attr)kernel/bpf/syscall.c +0
-3@@ -3720,9 +3757,6 @@ static int bpf_map_get_fd_by_id(const union bpf_attr *attr) attr->open_flags & ~BPF_OBJ_FLAG_MASK)return -EINVAL ; -if (!capable (CAP_SYS_ADMIN )) -return -EPERM ; -This part we cannot relax.
What you're trying to do is to bypass path checks by pointing at a map with its ID only.
That contradicts to your official goal in the cover letter.
Quoted 7 lines
bpf_map_get_fd_by_id()has to staycap_sys_adminonly. Exactly for the reason that bpf subsystem has file ACL style.fd_by_idis a debug interface used by tools like bpftool and root admin that needs to see the system as a whole. Normal tasks/processes need to use bpffs and pin files with correct permissions to pass maps from one process to another. Or use FD passing kernel facilities.
Yeah, this part is not necessary for me either. Without this part, the current code already can do what I wanted.
Thanks
Menglong Dong