Implement file local storage
Subsystems (8)
Kumar Kartikeya Dwivedi
maintainercover Implement file local storage
This series implements a file local storage map for eBPF LSM programs. This allows to tie lifetime of data in the map to an open file description (in POSIX parlance). Like other local storage map types, lifetime of data is tied to the struct file instance.
The main purpose is a general purpose map keyed by fd where the open file underlying the fd (struct file *) serves as the key into the map. It is possible to use struct file * from kernelspace, but sharing update access with userspace means userspace has no way except kcmp-aring with another known fd with a key. This is pretty wasteful.
It can also be used to treat the map as a set of files that have been added to it, such that multiples sets can be looked up for matching purposes in O(1) instead of O(n) using kcmp(2) from userspace (for same struct file *).
There are multiple other usecases served by this map. One of the motivating ones is the ability to now implement a Capsicum [0] style capability based sandbox using eBPF LSM, but the actual mechanism is much more generic and allows applications to enforce rights of their own per open file that they delegate to other users by conventional fd-passing on UNIX (dup/fork/SCM_RIGHTS).
The implementation is exactly the same as bpf_inode_storage, except some modifications to use struct file * as the key instead of struct inode *.
[0]: https://www.usenix.org/legacy/event/sec10/tech/full_papers/Watson.pdf
Kumar Kartikeya Dwivedi (5):
bpf: Implement file local storage
tools: sync bpf.h header
libbpf: Add bpf_probe_map_type support for file local storage
tools: bpf: update bpftool for file_storage map
tools: testing: Add selftest for file local storage map
include/linux/bpf_lsm.h | 21 ++ include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 39 +++ kernel/bpf/Makefile | 2 +- kernel/bpf/bpf_file_storage.c | 244 ++++++++++++++++++ kernel/bpf/bpf_lsm.c | 4 + kernel/bpf/syscall.c | 3 +- kernel/bpf/verifier.c | 10 + security/bpf/hooks.c | 2 + .../bpf/bpftool/Documentation/bpftool-map.rst | 2 +- tools/bpf/bpftool/bash-completion/bpftool | 3 +- tools/bpf/bpftool/map.c | 3 +- tools/include/uapi/linux/bpf.h | 39 +++ tools/lib/bpf/libbpf_probes.c | 1 + .../bpf/prog_tests/test_local_storage.c | 51 ++++ .../selftests/bpf/progs/local_storage.c | 23 ++ 16 files changed, 443 insertions(+), 5 deletions(-) create mode 100644 kernel/bpf/bpf_file_storage.c
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 1/5 bpf: Implement file local storage
This map is useful in general to tie data associated with a open file (not fd) from eBPF programs, such that data is released when the file goes away (e.g. checkpoint/restore usecase).
Another usecase is implementing Capsicum [0] style capability sandbox in userspace using eBPF LSM, enforcing rights at the file level using this mechanism.
The implementation is exactly similar to bpf_inode_storage, with some minor changes where required.
[0]: https://www.usenix.org/legacy/event/sec10/tech/full_papers/Watson.pdf
--- include/linux/bpf_lsm.h | 21 +++ include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 39 ++++++ kernel/bpf/Makefile | 2 +- kernel/bpf/bpf_file_storage.c | 244 ++++++++++++++++++++++++++++++++++ kernel/bpf/bpf_lsm.c | 4 + kernel/bpf/syscall.c | 3 +- kernel/bpf/verifier.c | 10 ++ security/bpf/hooks.c | 2 + 9 files changed, 324 insertions(+), 2 deletions(-) create mode 100644 kernel/bpf/bpf_file_storage.c
include/linux/bpf_lsm.h +21 -0
@@ -42,6 +42,18 @@ extern const struct bpf_func_proto bpf_inode_storage_get_proto;extern const struct bpf_func_proto bpf_inode_storage_delete_proto;void bpf_inode_storage_free (struct inode *inode); +static inline struct bpf_storage_blob *bpf_file (const struct file *file) +{ +if (unlikely (!file->f_security)) +return NULL ; + +return file->f_security + bpf_lsm_blob_sizes.lbs_file; +} + +extern const struct bpf_func_proto bpf_file_storage_get_proto; +extern const struct bpf_func_proto bpf_file_storage_delete_proto; +void bpf_file_storage_free (struct file *file); +#else /* !CONFIG_BPF_LSM */ static inline bool bpf_lsm_is_sleepable_hook (u32 btf_id)
@@ -65,6 +77,15 @@ static inline void bpf_inode_storage_free(struct inode *inode) { } +static inline struct bpf_storage_blob *bpf_file (const struct file *file) +{ +return NULL ; +} + +static inline void bpf_file_storage_free (struct file *file) +{ +} +#endif /* CONFIG_BPF_LSM */ #endif /* _LINUX_BPF_LSM_H */
include/linux/bpf_types.h +1 -0
@@ -107,6 +107,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP_HASH, dev_map_hash_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_SK_STORAGE, sk_storage_map_ops)#ifdef CONFIG_BPF_LSM BPF_MAP_TYPE(BPF_MAP_TYPE_INODE_STORAGE, inode_storage_map_ops) +BPF_MAP_TYPE(BPF_MAP_TYPE_FILE_STORAGE, file_storage_map_ops)#endif BPF_MAP_TYPE(BPF_MAP_TYPE_TASK_STORAGE, task_storage_map_ops)BPF_MAP_TYPE (BPF_MAP_TYPE_CPUMAP , cpu_map_ops)
include/uapi/linux/bpf.h +39 -0
@@ -906,6 +906,7 @@ enum bpf_map_type {BPF_MAP_TYPE_RINGBUF ,BPF_MAP_TYPE_INODE_STORAGE ,BPF_MAP_TYPE_TASK_STORAGE , + BPF_MAP_TYPE_FILE_STORAGE, };/* Note that tracing related programs such as
@@ -4871,6 +4872,42 @@ union bpf_attr {* Return * Value specified by user at BPF link creation/attachment time * or 0, if it was not specified. +* +* void *bpf_file_storage_get(struct bpf_map *map, void *file, void *value, u64 flags) +* Description +* Get a bpf_local_storage from a *file*. +* +* Logically, it could be thought of as getting the value from +* a *map* with *file* as the **key**. From this +* perspective, the usage is not much different from +* **bpf_map_lookup_elem**\ (*map*, **&**\ *file*) except this +* helper enforces the key must be an file and the map must also +* be a **BPF_MAP_TYPE_FILE_STORAGE**. +* +* Underneath, the value is stored locally at *file* instead of +* the *map*. The *map* is used as the bpf-local-storage +* "type". The bpf-local-storage "type" (i.e. the *map*) is +* searched against all bpf_local_storage residing at *file*. +* +* An optional *flags* (**BPF_LOCAL_STORAGE_GET_F_CREATE**) can be +* used such that a new bpf_local_storage will be +* created if one does not exist. *value* can be used +* together with **BPF_LOCAL_STORAGE_GET_F_CREATE** to specify +* the initial value of a bpf_local_storage. If *value* is +* **NULL**, the new bpf_local_storage will be zero initialized. +* Return +* A bpf_local_storage pointer is returned on success. +* +* **NULL** if not found or there was an error in adding +* a new bpf_local_storage. +* +* int bpf_file_storage_delete(struct bpf_map *map, void *file) +* Description +* Delete a bpf_local_storage from a *file*. +* Return +* 0 on success. +* +* **-ENOENT** if the bpf_local_storage cannot be found. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5048,6 +5085,8 @@ union bpf_attr {FN (timer_cancel), \FN (get_func_ip), \FN (get_attach_cookie), \ +FN (file_storage_get), \ +FN (file_storage_delete), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/Makefile +1 -1
@@ -10,7 +10,7 @@ obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_i obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o -obj-${CONFIG_BPF_LSM} += bpf_inode_storage.o +obj-${CONFIG_BPF_LSM} += bpf_inode_storage.o bpf_file_storage.o obj-$(CONFIG_BPF_SYSCALL) += disasm.o obj-$(CONFIG_BPF_JIT) += trampoline.o obj-$(CONFIG_BPF_SYSCALL) += btf.o
kernel/bpf/bpf_file_storage.c new file +244 -0
244 lines not on this page · raw message
kernel/bpf/bpf_lsm.c +4 -0
@@ -121,6 +121,10 @@ bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)return &bpf_bprm_opts_set_proto;case BPF_FUNC_ima_inode_hash:return prog->aux->sleepable ? &bpf_ima_inode_hash_proto :NULL ; +case BPF_FUNC_file_storage_get: +return &bpf_file_storage_get_proto; +case BPF_FUNC_file_storage_delete: +return &bpf_file_storage_delete_proto;default :return tracing_prog_func_proto (func_id, prog); }
kernel/bpf/syscall.c +2 -1
@@ -783,7 +783,8 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf, map->map_type !=BPF_MAP_TYPE_CGROUP_STORAGE && map->map_type !=BPF_MAP_TYPE_SK_STORAGE && map->map_type !=BPF_MAP_TYPE_INODE_STORAGE && - map->map_type != BPF_MAP_TYPE_TASK_STORAGE) + map->map_type !=BPF_MAP_TYPE_TASK_STORAGE && + map->map_type != BPF_MAP_TYPE_FILE_STORAGE) return -ENOTSUPP;if (map->spin_lock_off +sizeof (struct bpf_spin_lock ) > map->value_size) {
kernel/bpf/verifier.c +10 -0
@@ -5390,6 +5390,11 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, func_id != BPF_FUNC_task_storage_delete) goto error;break ; +case BPF_MAP_TYPE_FILE_STORAGE : +if (func_id != BPF_FUNC_file_storage_get && + func_id != BPF_FUNC_file_storage_delete) + goto error; +break ;default :break ; }
@@ -5473,6 +5478,11 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,if (map->map_type !=BPF_MAP_TYPE_TASK_STORAGE ) goto error;break ; +case BPF_FUNC_file_storage_get: +case BPF_FUNC_file_storage_delete: +if (map->map_type !=BPF_MAP_TYPE_FILE_STORAGE ) + goto error; +break ;default :break ; }
security/bpf/hooks.c +2 -0
@@ -13,6 +13,7 @@ static struct security_hook_list bpf_lsm_hooks[] __lsm_ro_after_init = {#undef LSM_HOOKLSM_HOOK_INIT (inode_free_security, bpf_inode_storage_free),LSM_HOOK_INIT (task_free, bpf_task_storage_free), + LSM_HOOK_INIT(file_free_security, bpf_file_storage_free), };static int __init bpf_lsm_init (void )
@@ -25,6 +26,7 @@ static int __init bpf_lsm_init(void) struct lsm_blob_sizes bpf_lsm_blob_sizes __lsm_ro_after_init = { .lbs_inode =sizeof (struct bpf_storage_blob ), .lbs_task =sizeof (struct bpf_storage_blob ), + .lbs_file =sizeof (struct bpf_storage_blob ), };DEFINE_LSM (bpf) = {
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 2/5 tools: sync bpf.h header
In reply to Kumar Kartikeya DwivediUpdate the bpf.h UAPI header with changes made from file local storage additions.
--- tools/include/uapi/linux/bpf.h | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
tools/include/uapi/linux/bpf.h +39 -0
@@ -906,6 +906,7 @@ enum bpf_map_type {BPF_MAP_TYPE_RINGBUF ,BPF_MAP_TYPE_INODE_STORAGE ,BPF_MAP_TYPE_TASK_STORAGE , + BPF_MAP_TYPE_FILE_STORAGE, };/* Note that tracing related programs such as
@@ -4871,6 +4872,42 @@ union bpf_attr {* Return * Value specified by user at BPF link creation/attachment time * or 0, if it was not specified. +* +* void *bpf_file_storage_get(struct bpf_map *map, void *file, void *value, u64 flags) +* Description +* Get a bpf_local_storage from a *file*. +* +* Logically, it could be thought of as getting the value from +* a *map* with *file* as the **key**. From this +* perspective, the usage is not much different from +* **bpf_map_lookup_elem**\ (*map*, **&**\ *file*) except this +* helper enforces the key must be an file and the map must also +* be a **BPF_MAP_TYPE_FILE_STORAGE**. +* +* Underneath, the value is stored locally at *file* instead of +* the *map*. The *map* is used as the bpf-local-storage +* "type". The bpf-local-storage "type" (i.e. the *map*) is +* searched against all bpf_local_storage residing at *file*. +* +* An optional *flags* (**BPF_LOCAL_STORAGE_GET_F_CREATE**) can be +* used such that a new bpf_local_storage will be +* created if one does not exist. *value* can be used +* together with **BPF_LOCAL_STORAGE_GET_F_CREATE** to specify +* the initial value of a bpf_local_storage. If *value* is +* **NULL**, the new bpf_local_storage will be zero initialized. +* Return +* A bpf_local_storage pointer is returned on success. +* +* **NULL** if not found or there was an error in adding +* a new bpf_local_storage. +* +* int bpf_file_storage_delete(struct bpf_map *map, void *file) +* Description +* Delete a bpf_local_storage from a *file*. +* Return +* 0 on success. +* +* **-ENOENT** if the bpf_local_storage cannot be found. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -5048,6 +5085,8 @@ union bpf_attr {FN (timer_cancel), \FN (get_func_ip), \FN (get_attach_cookie), \ +FN (file_storage_get), \ +FN (file_storage_delete), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 3/5 libbpf: Add bpf_probe_map_type support for file local storage
In reply to Kumar Kartikeya Dwivedi--- tools/lib/bpf/libbpf_probes.c | 1 + 1 file changed, 1 insertion(+)
tools/lib/bpf/libbpf_probes.c +1 -0
@@ -233,6 +233,7 @@ bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex)case BPF_MAP_TYPE_SK_STORAGE :case BPF_MAP_TYPE_INODE_STORAGE :case BPF_MAP_TYPE_TASK_STORAGE : +case BPF_MAP_TYPE_FILE_STORAGE : btf_key_type_id =1 ; btf_value_type_id =3 ; value_size =8 ;
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 4/5 tools: bpf: update bpftool for file_storage map
In reply to Kumar Kartikeya DwivediThis updates bpftool to recognise the new file local storage map type.
--- tools/bpf/bpftool/Documentation/bpftool-map.rst | 2 +- tools/bpf/bpftool/bash-completion/bpftool | 3 ++- tools/bpf/bpftool/map.c | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-map.rst +1 -1
@@ -52,7 +52,7 @@ MAP COMMANDS | | **devmap** | **devmap_hash** | **sockmap** | **cpumap** | **xskmap** | **sockhash** | | **cgroup_storage** | **reuseport_sockarray** | **percpu_cgroup_storage** | | **queue** | **stack** | **sk_storage** | **struct_ops** | **ringbuf** | **inode_storage** - | **task_storage** } + | **task_storage** | **file_storage** } DESCRIPTION ===========
tools/bpf/bpftool/bash-completion/bpftool +2 -1
@@ -710,7 +710,8 @@ _bpftool() hash_of_maps devmap devmap_hash sockmap cpumap \ xskmap sockhash cgroup_storage reuseport_sockarray \ percpu_cgroup_storage queue stack sk_storage \ - struct_ops inode_storage task_storage ringbuf' + struct_ops inode_storage task_storage ringbuf \ + file_storage' COMPREPLY=( $( compgen -W "$BPFTOOL_MAP_CREATE_TYPES" -- "$cur" ) ) return 0 ;;
tools/bpf/bpftool/map.c +2 -1
@@ -52,6 +52,7 @@ const char * const map_type_name[] = { [BPF_MAP_TYPE_RINGBUF] = "ringbuf", [BPF_MAP_TYPE_INODE_STORAGE] = "inode_storage", [BPF_MAP_TYPE_TASK_STORAGE] = "task_storage", + [BPF_MAP_TYPE_FILE_STORAGE] = "file_storage", };const size_t map_type_name_size =ARRAY_SIZE (map_type_name);
@@ -1466,7 +1467,7 @@ static int do_help(int argc, char **argv)" devmap | devmap_hash | sockmap | cpumap | xskmap | sockhash |\n" " cgroup_storage | reuseport_sockarray | percpu_cgroup_storage |\n" " queue | stack | sk_storage | struct_ops | ringbuf | inode_storage |\n" -" task_storage }\n" +" task_storage | file_storage }\n" " " HELP_SPEC_OPTIONS " |\n" " {-f|--bpffs} | {-n|--nomount} }\n" "",
Signature · 1 line
2.33.0
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 5/5 tools: testing: Add selftest for file local storage map
In reply to Kumar Kartikeya DwivediThis adds a test case for verifying that file local storage map works as intended.
--- .../bpf/prog_tests/test_local_storage.c | 51 +++++++++++++++++++ .../selftests/bpf/progs/local_storage.c | 23 +++++++++ 2 files changed, 74 insertions(+)
tools/testing/selftests/bpf/prog_tests/test_local_storage.c +51 -0
@@ -24,6 +24,7 @@ static inline int sys_pidfd_open(pid_t pid, unsigned int flags)static unsigned int duration;#define TEST_STORAGE_VALUE 0xbeefdead +#define DUMMY_STORAGE_VALUE 0xdeadbeefstruct storage {void *inode;
@@ -111,6 +112,51 @@ static bool check_syscall_operations(int map_fd, int obj_fd)return true; } +int test_file_local_storage (struct bpf_map *map) +{ +struct storage ls; +int fd, ret; + + fd =open ("/dev/null" ,O_RDONLY ); +if (!ASSERT_GE (fd,0 ,"open(/dev/null)" )) +return -errno; + + ret =bpf_map_lookup_elem (bpf_map__fd (map), &fd, &ls); +if (!ASSERT_OK (ret,"bpf_map_lookup_elem for file local storage" )) + goto end; + +ASSERT_EQ (ls.value,DUMMY_STORAGE_VALUE ,"file local value match" ); + + ret =bpf_map_delete_elem (bpf_map__fd (map), &fd); +if (!ASSERT_OK (ret,"bpf_map_delete_elem for file local storage" )) + goto end; + + ret =bpf_map_lookup_elem (bpf_map__fd (map), &fd, &ls); +if (!ASSERT_EQ (ret, -ENOENT ,"bpf_map_lookup_elem should fail" )) + goto end; + +memset (&ls,0 ,sizeof (ls)); + ls.value =DUMMY_STORAGE_VALUE ; + ret =bpf_map_update_elem (bpf_map__fd (map), &fd, &ls,BPF_NOEXIST ); +if (!ASSERT_OK (ret,"bpf_map_update_elem for file local storage" )) + goto end; + + ret =bpf_map_lookup_elem (bpf_map__fd (map), &fd, &ls); +if (!ASSERT_OK (ret,"bpf_map_lookup_elem for file local storage" )) + goto end; + +close (fd); + + ret =bpf_map_lookup_elem (bpf_map__fd (map), &fd, &ls); +if (!ASSERT_EQ (ret, -EBADF ,"bpf_map_lookup_elem should fail" )) +return -EINVAL ; + +return 0 ; +end: +close (fd); +return ret; +} +void test_test_local_storage (void ) {char tmp_dir_path[] ="/tmp/local_storageXXXXXX" ;
@@ -167,6 +213,11 @@ void test_test_local_storage(void)/* Set the process being monitored to be the current process */ skel->bss->monitored_pid =getpid (); +/* Test file local storage */ + err =test_file_local_storage (skel->maps.file_storage_map); +if (!ASSERT_OK (err,"test_file_local_storage" )) + goto close_prog_rmdir; +/* Move copy_of_rm to a new location so that it triggers the * inode_rename LSM hook with a new_dentry that has a NULL inode ptr. */
tools/testing/selftests/bpf/progs/local_storage.c +23 -0
@@ -44,6 +44,13 @@ struct { __type(value, struct local_storage); } task_storage_map SEC(".maps"); +struct { + __uint(type, BPF_MAP_TYPE_FILE_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, struct local_storage); +} file_storage_map SEC(".maps"); + SEC("lsm/inode_unlink")int BPF_PROG (unlink_hook ,struct inode *dir,struct dentry *victim) {
@@ -181,3 +188,19 @@ void BPF_PROG(exec, struct linux_binprm *bprm) storage->value =DUMMY_STORAGE_VALUE ;bpf_spin_unlock (&storage->lock); } + +SEC("lsm/file_open") +int BPF_PROG (file_open ,struct file *file) +{ +__u32 pid =bpf_get_current_pid_tgid () >>32 ; +struct local_storage *storage; + +if (pid != monitored_pid) +return 0 ; + + storage =bpf_file_storage_get (&file_storage_map, file,0 , +BPF_LOCAL_STORAGE_GET_F_CREATE ); +if (storage) + storage->value =DUMMY_STORAGE_VALUE ; +return 0 ; +}
Signature · 1 line
2.33.0