kernel/bpf: Add BPF_MAP_TYPE_CRED_STORAGE map type and kfuncs
Subsystems (7)
PATCH 1/2 kernel/bpf: Add BPF_MAP_TYPE_CRED_STORAGE map type and kfuncs
All other bpf local storage is obtained using helpers which benefit from RET_PTR_TO_MAP_VALUE_OR_NULL, so can return void * pointers directly to map values. kfuncs don't have that, so return struct bpf_local_storage_data * and access map values through sdata->data.
--- include/linux/bpf_lsm.h | 35 +++++++ include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 1 + kernel/bpf/Makefile | 1 + kernel/bpf/bpf_cred_storage.c | 175 ++++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 10 +- kernel/cred.c | 7 ++ security/bpf/hooks.c | 1 + 8 files changed, 228 insertions(+), 3 deletions(-) create mode 100644 kernel/bpf/bpf_cred_storage.c
include/linux/bpf_lsm.h +35 -0
@@ -40,10 +40,27 @@ static inline struct bpf_storage_blob *bpf_inode(return inode->i_security + bpf_lsm_blob_sizes.lbs_inode; } +static inline struct bpf_storage_blob *bpf_cred ( +const struct cred *cred) +{ +if (unlikely (!cred->security)) +return NULL ; + +return cred->security + bpf_lsm_blob_sizes.lbs_cred; +} +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); +void bpf_cred_storage_free (struct cred *cred); +struct bpf_local_storage_data *bpf_cred_storage_get (struct bpf_map *map, +struct cred *cred, +void *init, +int init__sz, +u64 flags); +int bpf_cred_storage_delete (struct bpf_map *map,struct cred *cred); +void bpf_lsm_find_cgroup_shim (const struct bpf_prog *prog,bpf_func_t *bpf_func); int bpf_lsm_get_retval_range(const struct bpf_prog *prog,
@@ -81,6 +98,24 @@ static inline void bpf_inode_storage_free(struct inode *inode) { } +static inline void bpf_cred_storage_free (struct cred *cred) +{ +} + +static inline struct bpf_local_storage_data *bpf_cred_storage_get (struct bpf_map *map, +struct cred *cred, +void *init, +int init__sz, +u64 flags) +{ +return NULL ; +} + +static inline int bpf_cred_storage_delete (struct bpf_map *map,struct cred *cred) +{ +return -EOPNOTSUPP ; +} +static inline void bpf_lsm_find_cgroup_shim (const struct bpf_prog *prog,bpf_func_t *bpf_func) {
include/linux/bpf_types.h +1 -0
@@ -110,6 +110,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_HASH_OF_MAPS, htab_of_maps_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_INODE_STORAGE, inode_storage_map_ops)#endif BPF_MAP_TYPE(BPF_MAP_TYPE_TASK_STORAGE, task_storage_map_ops) +BPF_MAP_TYPE(BPF_MAP_TYPE_CRED_STORAGE, cred_storage_map_ops)#ifdef CONFIG_NET BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP, dev_map_ops)BPF_MAP_TYPE (BPF_MAP_TYPE_DEVMAP_HASH , dev_map_hash_ops)
include/uapi/linux/bpf.h +1 -0
@@ -1026,6 +1026,7 @@ enum bpf_map_type {BPF_MAP_TYPE_USER_RINGBUF ,BPF_MAP_TYPE_CGRP_STORAGE ,BPF_MAP_TYPE_ARENA , +BPF_MAP_TYPE_CRED_STORAGE , __MAX_BPF_MAP_TYPE };
kernel/bpf/Makefile +1 -0
@@ -12,6 +12,7 @@ obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list 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_cred_storage.o obj-$(CONFIG_BPF_SYSCALL) += disasm.o mprog.o obj-$(CONFIG_BPF_JIT) += trampoline.o obj-$(CONFIG_BPF_SYSCALL) += btf.o memalloc.o rqspinlock.o stream.o
kernel/bpf/bpf_cred_storage.c new file +175 -0
175 lines not on this page · raw message
kernel/bpf/syscall.c +7 -3
@@ -1262,7 +1262,8 @@ static int map_check_btf(struct bpf_map *map, struct bpf_token *token, 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_CGRP_STORAGE) { + map->map_type !=BPF_MAP_TYPE_CGRP_STORAGE && + map->map_type != BPF_MAP_TYPE_CRED_STORAGE) { ret = -EOPNOTSUPP; goto free_map_tab; }
@@ -1289,13 +1290,15 @@ static int map_check_btf(struct bpf_map *map, struct bpf_token *token, 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_CGRP_STORAGE) { + map->map_type !=BPF_MAP_TYPE_CGRP_STORAGE && + map->map_type != BPF_MAP_TYPE_CRED_STORAGE) { ret = -EOPNOTSUPP; goto free_map_tab; }break ;case BPF_UPTR : -if (map->map_type !=BPF_MAP_TYPE_TASK_STORAGE ) { +if (map->map_type !=BPF_MAP_TYPE_TASK_STORAGE && + map->map_type !=BPF_MAP_TYPE_CRED_STORAGE ) { ret = -EOPNOTSUPP ; goto free_map_tab; }
@@ -1449,6 +1452,7 @@ static int map_create(union bpf_attr *attr, bool kernel)case BPF_MAP_TYPE_SK_STORAGE :case BPF_MAP_TYPE_INODE_STORAGE :case BPF_MAP_TYPE_TASK_STORAGE : +case BPF_MAP_TYPE_CRED_STORAGE :case BPF_MAP_TYPE_CGRP_STORAGE :case BPF_MAP_TYPE_BLOOM_FILTER :case BPF_MAP_TYPE_LPM_TRIE :
kernel/cred.c +7 -0
@@ -38,6 +38,10 @@ static struct kmem_cache *cred_jar;/* init to 2 - one for init_task, one to ensure it is never freed */ static struct group_info init_groups = { .usage =REFCOUNT_INIT (2 ) }; +#ifdef CONFIG_BPF_LSM +#include <linux/bpf_lsm.h> +#endif +/* * The initial credentials for the initial task */
@@ -76,6 +80,9 @@ static void put_cred_rcu(struct rcu_head *rcu) cred, atomic_long_read(&cred->usage));security_cred_free (cred); +#ifdef CONFIG_BPF_LSM +bpf_cred_storage_free (cred); +#endif key_put (cred->session_keyring);key_put (cred->process_keyring);key_put (cred->thread_keyring);
security/bpf/hooks.c +1 -0
@@ -30,6 +30,7 @@ static int __init bpf_lsm_init(void)struct lsm_blob_sizes bpf_lsm_blob_sizes__ro_after_init = { .lbs_inode =sizeof (struct bpf_storage_blob ), + .lbs_cred =sizeof (struct bpf_storage_blob ), };DEFINE_LSM (bpf) = {
Signature · 1 line
2.43.0
PATCH 2/2 selftests/bpf: Add cred local storage tests
--- .../selftests/bpf/prog_tests/cred_storage.c | 52 +++++++++++ .../selftests/bpf/progs/cred_storage.c | 87 +++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/cred_storage.c create mode 100644 tools/testing/selftests/bpf/progs/cred_storage.c
tools/testing/selftests/bpf/prog_tests/cred_storage.c new file +52 -0
@@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include <unistd.h> +#include <sys/wait.h> + +#include "cred_storage.skel.h" + +static void test_cred_lifecycle (void ) +{ +struct cred_storage *skel; +pid_t child; +int status, err; + + skel =cred_storage__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_load" )) +return ; + + err =cred_storage__attach (skel); +if (!ASSERT_OK (err,"attach" )) + goto cleanup; + + skel->data->cred_storage_result =-1 ; + + skel->bss->monitored_pid =getpid (); + + child =fork (); +if (child ==0 ) { +/* forces cred_prepare with new credentials */ +exit (0 ); + }else if (child >0 ) { +waitpid (child, &status,0 ); + +/* give time for cred_free hook to run */ +usleep (10000 ); + +/* verify that the dummy value was stored and persisted */ +ASSERT_EQ (skel->data->cred_storage_result,0 , +"cred_storage_dummy_value" ); + }else { +ASSERT_TRUE (false,"fork failed" ); + } + +cleanup: +cred_storage__destroy (skel); +} + +void test_cred_storage (void ) +{ +if (test__start_subtest ("lifecycle" )) +test_cred_lifecycle (); +}
tools/testing/selftests/bpf/progs/cred_storage.c new file +87 -0
87 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 5 lines · David Windsor
All other bpf local storage is obtained using helpers which benefit from
RET_PTR_TO_MAP_VALUE_OR_NULL, so can return void * pointers directly to map values. kfuncs don't have that, so return structbpf_local_storage_data* and access map values throughsdata->data.
The commit log tells nothing about motivation for such "cred local storage". Technically it's doable, but sorry not going to.
cred is not something that needs fast access and automatic lifetime management. Use hash map with 'struct cred *' as a key.
pw-bot: cr
Quoted 1 line
cred is not something that needs fast access and automatic lifetime
management.
Use case is for eg KRSI to be able to track credential leakage. Lifetime management absolutely helps there. Also, the use case is "whatever the bpf-lsm maintainers had in mind in their presentation," right?
Also, this entire presentation says otherwise:
https://lpc.events/event/18/contributions/1940/attachments/1438/3389/kfuncs%20for%20BPF%20LSM%20Use%20Cases.v4.pdf
From that:
"Still missing storage for following types:
struct file
struct cred
struct ipc
struct msg_msg
struct superblock"
Quoted 1 line
Technically it's doable, but sorry not going to.
Sweet I'll do it
Quoted 14 lines · Alexei Starovoitov
Quoted 5 lines · David Windsor
All other bpf local storage is obtained using helpers which benefit from
RET_PTR_TO_MAP_VALUE_OR_NULL, so can return void * pointers directly to map values. kfuncs don't have that, so return structbpf_local_storage_data* and access map values throughsdata->data.The commit log tells nothing about motivation for such "cred local storage". Technically it's doable, but sorry not going to.
cred is not something that needs fast access and automatic lifetime management. Use hash map with 'struct cred *' as a key.pw-bot: cr
kernel test robot
build botkernel test robot noticed the following build errors:
Hi David,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/net]
[also build test ERROR on bpf-next/master bpf/master linus/master v6.17-rc4] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/David-Windsor/selftests-bpf-Add-cred-local-storage-tests/20250904-015935 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git net patch link: https://lore.kernel.org/r/20250903175841.232537-1-dwindsor%40gmail.com patch subject: [PATCH 1/2] kernel/bpf: Add BPF_MAP_TYPE_CRED_STORAGE map type and kfuncs config: nios2-randconfig-001-20250904 (https://download.01.org/0day-ci/archive/20250904/[email protected]/config) compiler: nios2-linux-gcc (GCC) 11.5.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250904/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> nios2-linux-ld: kernel/bpf/syscall.o:(.rodata+0x734): undefined reference to `cred_storage_map_ops'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki