Skip to contentSkip to conversation

bpf: Add BPF_MAP_TYPE_CRED_STORAGE support

PATCH In review David Windsor 3 participants 13 messages Sep 12, 2025 – Sep 16, 2025 2 messages not in the archive

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v2 cover bpf: Add BPF_MAP_TYPE_CRED_STORAGE support

This series adds BPF_MAP_TYPE_CRED_STORAGE, enabling BPF programs to associate data with credential structures (struct cred).

Like other local storage types (task, inode, sk), this provides automatic lifecycle management and is useful for LSM programs tracking credential state across LSM calls. Lifetime management is necessary for detecting credential leaks and enforcing time-based security policies.

The implementation uses kfuncs (bpf_cred_storage_get/delete) that return bpf_local_storage_data pointers, with map values accessible via the data field.

v2:
- fix kernel ci build error

David Windsor (2):
bpf: Add BPF_MAP_TYPE_CRED_STORAGE map type and kfuncs selftests/bpf: Add cred local storage tests

 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 +
 .../selftests/bpf/prog_tests/cred_storage.c   |  52 ++++++
 .../selftests/bpf/progs/cred_storage.c        |  87 +++++++++
 10 files changed, 367 insertions(+), 3 deletions(-)
 create mode 100644 kernel/bpf/bpf_cred_storage.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/cred_storage.c
 create mode 100644 tools/testing/selftests/bpf/progs/cred_storage.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 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
index 643809cc78c3..b0e2e5f2a2b8 100644
@@ -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
index fa78f49d4a9a..b8349e837158 100644
@@ -108,6 +108,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY_OF_MAPS, array_of_maps_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_HASH_OF_MAPS, htab_of_maps_map_ops)
 #ifdef CONFIG_BPF_LSM
 BPF_MAP_TYPE(BPF_MAP_TYPE_INODE_STORAGE, inode_storage_map_ops)
+BPF_MAP_TYPE(BPF_MAP_TYPE_CRED_STORAGE, cred_storage_map_ops)
 #endif
 BPF_MAP_TYPE(BPF_MAP_TYPE_TASK_STORAGE, task_storage_map_ops)
 #ifdef CONFIG_NET
include/uapi/linux/bpf.h +1 -0
index 233de8677382..8ce34453b907 100644
@@ -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
index f6cf8c2af5f7..7fd8746b2d51 100644
@@ -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
index 3f178a0f8eb1..f03811efe266 100644
@@ -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
index 9676965c0981..a1be27fe5f4c 100644
@@ -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
index db759025abe1..d42badc18eb6 100644
@@ -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
reviewer
#2 lore raw

Re: [PATCH 1/2] bpf: Add BPF_MAP_TYPE_CRED_STORAGE map type and kfuncs

Quoted 7 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 struct bpf_local_storage_data * and access map values through sdata->data.

Maybe I missed something, but I think you haven't addressed Alexei's question in v1: why this is needed and why hash map is not sufficient.

Other local storage types (task, inode, sk storage) may get a large number of entries in a system, and thus would benefit from object local storage. I don't think we expect too many creds in a system. hash map of a smallish size should be good in most cases, and be faster than cred local storage.

Did I get this right?

Thanks,
Song

The following are some quick feedbacks of the patch, but let's first address the question above.

This is hitting KASAN BUG in CI:

https://github.com/kernel-patches/bpf/actions/runs/17687566710/job/50275683479

(You may need to log in GitHub to see details).

[...]

Quoted 12 lines
kernel/bpf/bpf_cred_storage.c new file +12 -0
+
+__bpf_kfunc int bpf_cred_storage_delete(struct bpf_map *map, struct cred *cred)
+{
+       if (!cred)
+               return -EINVAL;
+
+       return cred_storage_delete(cred, map);
+}
+
+BTF_KFUNCS_START(bpf_cred_storage_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_cred_storage_delete, 0)
+BTF_ID_FLAGS(func, bpf_cred_storage_get, KF_RET_NULL)

kernel/bpf/bpf_cred_storage.c line 165

Quoted 10 lines
kernel/bpf/bpf_cred_storage.c new file +10 -0
+BTF_KFUNCS_END(bpf_cred_storage_kfunc_ids)
+
+static const struct btf_kfunc_id_set bpf_cred_storage_kfunc_set = {
+       .owner = THIS_MODULE,
+       .set   = &bpf_cred_storage_kfunc_ids,
+};
+
+static int __init bpf_cred_storage_init(void)
+{
+       int err;

We need an empty line after the declaration. scripts/checkpatch.pl should warn this. Please fix other warnings from checkpatch.pl.

Quoted 2 lines
kernel/bpf/bpf_cred_storage.c new file +2 -0
+       err = register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, &bpf_cred_storage_kfunc_set);
+       if (err) {

[...]

kernel/cred.c static struct kmem_cache *cred_jar; · line 43

Quoted 11 lines
kernel/cred.c +3 -0
index 9676965c0981..a1be27fe5f4c 100644
@@ -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

We defined a dummy version of bpf_cred_storage_free in bpf_lsm.h, so the ifdef here is not needed.

kernel/cred.c static void put_cred_rcu(struct rcu_head *rcu) · line 85

Quoted 11 lines
kernel/cred.c +4 -0
+
 /*
  * 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

Ditto.

Quoted 3 lines
kernel/cred.c +0 -0
        key_put(cred->session_keyring);
        key_put(cred->process_keyring);
        key_put(cred->thread_keyring);

[...]

#3 lore raw

Re: [PATCH 1/2] bpf: Add BPF_MAP_TYPE_CRED_STORAGE map type and kfuncs

Quoted 19 lines · Song Liu
Quoted 7 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 struct bpf_local_storage_data * and access map values through sdata->data.

Maybe I missed something, but I think you haven't addressed Alexei's question in v1: why this is needed and why hash map is not sufficient.

Other local storage types (task, inode, sk storage) may get a large number of entries in a system, and thus would benefit from object local storage. I don't think we expect too many creds in a system. hash map of a smallish size should be good in most cases, and be faster than cred local storage.

Quoted 5 lines

Did I get this right?

Thanks,
Song

Yes I think I addressed in the cover letter of -v2:

"Like other local storage types (task, inode, sk), this provides automatic lifecycle management and is useful for LSM programs tracking credential state across LSM calls. Lifetime management is necessary for detecting credential leaks and enforcing time-based security policies."

You're right it's faster and there aren't many creds, but I feel like in this case, it'll be a nightmare to manual cleanup with hashmaps. I think the correctness we get with lifetime management is worth it in this case, but could be convinced otherwise. Many cred usage patterns are short lived and a hash map could quickly become stale...

Quoted 76 lines

The following are some quick feedbacks of the patch, but let's first address the question above.

This is hitting KASAN BUG in CI:

https://github.com/kernel-patches/bpf/actions/runs/17687566710/job/50275683479

(You may need to log in GitHub to see details).

[...]

Quoted 12 lines
kernel/bpf/bpf_cred_storage.c new file +12 -0
+
+__bpf_kfunc int bpf_cred_storage_delete(struct bpf_map *map, struct cred *cred)
+{
+       if (!cred)
+               return -EINVAL;
+
+       return cred_storage_delete(cred, map);
+}
+
+BTF_KFUNCS_START(bpf_cred_storage_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_cred_storage_delete, 0)
+BTF_ID_FLAGS(func, bpf_cred_storage_get, KF_RET_NULL)

kernel/bpf/bpf_cred_storage.c line 165

Quoted 10 lines
kernel/bpf/bpf_cred_storage.c new file +10 -0
+BTF_KFUNCS_END(bpf_cred_storage_kfunc_ids)
+
+static const struct btf_kfunc_id_set bpf_cred_storage_kfunc_set = {
+       .owner = THIS_MODULE,
+       .set   = &bpf_cred_storage_kfunc_ids,
+};
+
+static int __init bpf_cred_storage_init(void)
+{
+       int err;

We need an empty line after the declaration. scripts/checkpatch.pl should warn this. Please fix other warnings from checkpatch.pl.

Quoted 2 lines
kernel/bpf/bpf_cred_storage.c new file +2 -0
+       err = register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, &bpf_cred_storage_kfunc_set);
+       if (err) {

[...]

kernel/cred.c static struct kmem_cache *cred_jar; · line 43

Quoted 11 lines
kernel/cred.c +3 -0
index 9676965c0981..a1be27fe5f4c 100644
@@ -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

We defined a dummy version of bpf_cred_storage_free in bpf_lsm.h, so the ifdef here is not needed.

kernel/cred.c static void put_cred_rcu(struct rcu_head *rcu) · line 85

Quoted 11 lines
kernel/cred.c +4 -0
+
 /*
  * 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

Ditto.

Quoted 3 lines
kernel/cred.c +0 -0
        key_put(cred->session_keyring);
        key_put(cred->process_keyring);
        key_put(cred->thread_keyring);

[...]

reviewer
#4 lore raw

Re: [PATCH 1/2] bpf: Add BPF_MAP_TYPE_CRED_STORAGE map type and kfuncs

On Fri, Sep 12, 2025 at 5:27 PM David Windsor <[email protected]> wrote:
[...]

Quoted 23 lines
Quoted 10 lines

Maybe I missed something, but I think you haven't addressed Alexei's question in v1: why this is needed and why hash map is not sufficient.

Other local storage types (task, inode, sk storage) may get a large number of entries in a system, and thus would benefit from object local storage. I don't think we expect too many creds in a system. hash map of a smallish size should be good in most cases, and be faster than cred local storage.

Quoted 5 lines

Did I get this right?

Thanks,
Song

Yes I think I addressed in the cover letter of -v2:

"Like other local storage types (task, inode, sk), this provides automatic lifecycle management and is useful for LSM programs tracking credential state across LSM calls. Lifetime management is necessary for detecting credential leaks and enforcing time-based security policies."

Quoted 5 lines

You're right it's faster and there aren't many creds, but I feel like in this case, it'll be a nightmare to manual cleanup with hashmaps. I think the correctness we get with lifetime management is worth it in this case, but could be convinced otherwise. Many cred usage patterns are short lived and a hash map could quickly become stale...

We can clean up the hashmap in hook cred_free, no? The following check in security_cred_free() seems problematic:

        if (unlikely(cred->security == NULL))
                return;

But as far as I can tell, it is not really useful, and can be removed. With this removed, hash map will work just as well. Did I miss something?

Thanks,
Song

A message that is not in the archive (sent off-list or not yet received): CAEXv5_g2xMwSXGJ=X1FEiA8_YQnSXKwHFW3Cv5Ki5wwLkhAfuA@mail.gmail.com

#6 lore raw

Re: [PATCH 1/2] bpf: Add BPF_MAP_TYPE_CRED_STORAGE map type and kfuncs

Quoted 51 lines · David Windsor
Quoted 35 lines · Song Liu

On Fri, Sep 12, 2025 at 5:27 PM David Windsor <[email protected]> wrote:
[...]

Quoted 23 lines
Quoted 10 lines

Maybe I missed something, but I think you haven't addressed Alexei's question in v1: why this is needed and why hash map is not sufficient.

Other local storage types (task, inode, sk storage) may get a large number of entries in a system, and thus would benefit from object local storage. I don't think we expect too many creds in a system. hash map of a smallish size should be good in most cases, and be faster than cred local storage.

Quoted 5 lines

Did I get this right?

Thanks,
Song

Yes I think I addressed in the cover letter of -v2:

"Like other local storage types (task, inode, sk), this provides automatic lifecycle management and is useful for LSM programs tracking credential state across LSM calls. Lifetime management is necessary for detecting credential leaks and enforcing time-based security policies."

Quoted 5 lines

You're right it's faster and there aren't many creds, but I feel like in this case, it'll be a nightmare to manual cleanup with hashmaps. I think the correctness we get with lifetime management is worth it in this case, but could be convinced otherwise. Many cred usage patterns are short lived and a hash map could quickly become stale...

We can clean up the hashmap in hook cred_free, no? The following check in security_cred_free() seems problematic:

Quoted 6 lines
        if (unlikely(cred->security == NULL))
                return;

But as far as I can tell, it is not really useful, and can be removed. With this removed, hash map will work just as well. Did I miss something?

No I think actually this is easier.

I will prepare a patch for the race in cleanup I stumbled on earlier which is still there and could affect other users.

Quoted 7 lines

That said, is there any use case for local storage for these structs:

- struct file
- struct msg_msg
- struct ipc

I can off the top of my head think of some security use cases for these but not sure if hashmaps are needed, perhaps struct file

Sorry, no. This is not a copy paste territory. The existing local storage maps were added because performance was critical for those use cases,
but we made a few mistakes. There is a performance cliff that has to be fixed before we adopt it to other kernel objects.
Please use hash map and consider wrapping rhashtable as a new bpf map type if fixed max_entries is problematic.

pw-bot: cr

#7 lore raw

Re: [PATCH 1/2] bpf: Add BPF_MAP_TYPE_CRED_STORAGE map type and kfuncs

Quoted 55 lines · Alexei Starovoitov
Quoted 53 lines · David Windsor
Quoted 35 lines · Song Liu

On Fri, Sep 12, 2025 at 5:27 PM David Windsor <[email protected]> wrote:
[...]

Quoted 23 lines
Quoted 10 lines

Maybe I missed something, but I think you haven't addressed Alexei's question in v1: why this is needed and why hash map is not sufficient.

Other local storage types (task, inode, sk storage) may get a large number of entries in a system, and thus would benefit from object local storage. I don't think we expect too many creds in a system. hash map of a smallish size should be good in most cases, and be faster than cred local storage.

Quoted 5 lines

Did I get this right?

Thanks,
Song

Yes I think I addressed in the cover letter of -v2:

"Like other local storage types (task, inode, sk), this provides automatic lifecycle management and is useful for LSM programs tracking credential state across LSM calls. Lifetime management is necessary for detecting credential leaks and enforcing time-based security policies."

Quoted 5 lines

You're right it's faster and there aren't many creds, but I feel like in this case, it'll be a nightmare to manual cleanup with hashmaps. I think the correctness we get with lifetime management is worth it in this case, but could be convinced otherwise. Many cred usage patterns are short lived and a hash map could quickly become stale...

We can clean up the hashmap in hook cred_free, no? The following check in security_cred_free() seems problematic:

Quoted 6 lines
        if (unlikely(cred->security == NULL))
                return;

But as far as I can tell, it is not really useful, and can be removed. With this removed, hash map will work just as well. Did I miss something?

No I think actually this is easier.

I will prepare a patch for the race in cleanup I stumbled on earlier which is still there and could affect other users.

That said, is there any use case for local storage for these structs:

Quoted 7 lines
Quoted 5 lines

- struct file
- struct msg_msg
- struct ipc

I can off the top of my head think of some security use cases for these but not sure if hashmaps are needed, perhaps struct file

Sorry, no. This is not a copy paste territory.

no i get it's not copy/paste but I have the series for struct file ready for submission, with selftests. this is also a performance critical use case and there will be numerous struct file on edge servers.

Quoted 5 lines

The existing local storage maps were added because performance was critical for those use cases, but we made a few mistakes. There is a performance cliff that has to be fixed before we adopt it to other kernel objects.

ahh wasn't aware of this.

Quoted 3 lines

Please use hash map and consider wrapping rhashtable as a new bpf map type if fixed max_entries is problematic.

makes sense thanks

Quoted 1 line

pw-bot: cr

#8 lore raw

Re: [PATCH 1/2] bpf: Add BPF_MAP_TYPE_CRED_STORAGE map type and kfuncs

Quoted 77 lines · David Windsor
Quoted 55 lines · Alexei Starovoitov
Quoted 53 lines · David Windsor
Quoted 35 lines · Song Liu

On Fri, Sep 12, 2025 at 5:27 PM David Windsor <[email protected]> wrote:
[...]

Quoted 23 lines
Quoted 10 lines

Maybe I missed something, but I think you haven't addressed Alexei's question in v1: why this is needed and why hash map is not sufficient.

Other local storage types (task, inode, sk storage) may get a large number of entries in a system, and thus would benefit from object local storage. I don't think we expect too many creds in a system. hash map of a smallish size should be good in most cases, and be faster than cred local storage.

Quoted 5 lines

Did I get this right?

Thanks,
Song

Yes I think I addressed in the cover letter of -v2:

"Like other local storage types (task, inode, sk), this provides automatic lifecycle management and is useful for LSM programs tracking credential state across LSM calls. Lifetime management is necessary for detecting credential leaks and enforcing time-based security policies."

Quoted 5 lines

You're right it's faster and there aren't many creds, but I feel like in this case, it'll be a nightmare to manual cleanup with hashmaps. I think the correctness we get with lifetime management is worth it in this case, but could be convinced otherwise. Many cred usage patterns are short lived and a hash map could quickly become stale...

We can clean up the hashmap in hook cred_free, no? The following check in security_cred_free() seems problematic:

Quoted 6 lines
        if (unlikely(cred->security == NULL))
                return;

But as far as I can tell, it is not really useful, and can be removed. With this removed, hash map will work just as well. Did I miss something?

No I think actually this is easier.

I will prepare a patch for the race in cleanup I stumbled on earlier which is still there and could affect other users.

That said, is there any use case for local storage for these structs:

Quoted 7 lines
Quoted 5 lines

- struct file
- struct msg_msg
- struct ipc

I can off the top of my head think of some security use cases for these but not sure if hashmaps are needed, perhaps struct file

Sorry, no. This is not a copy paste territory.

no i get it's not copy/paste but I have the series for struct file ready for submission, with selftests. this is also a performance critical use case and there will be numerous struct file on edge servers.

Quoted 5 lines

The existing local storage maps were added because performance was critical for those use cases, but we made a few mistakes. There is a performance cliff that has to be fixed before we adopt it to other kernel objects.

Quoted 8 lines

ahh wasn't aware of this.

Quoted 3 lines

Please use hash map and consider wrapping rhashtable as a new bpf map type if fixed max_entries is problematic.

makes sense thanks

Hi,

Thinking about this more, hashmaps are still problematic for this case.

Meaning, placing a hook on security_cred_free alone for garbage collection / end-of-life processing isn't enough - we still have to deal with prepare/commit_creds. This flow works by having prepare_creds clone an existing cred object, then commit_creds works by swapping old creds with new one atomically, then later freeing the original cred. If we are not very careful there will be a period of time during which both cred objects could be valid, and I think this is worth the feature alone.

Also, the main reason we want local storage for these structs is that LSMs use them. Every classic LSM (SELinux, Smack, AppArmor, TOMOYO, Landlock, Yama) has a cred blob, all of them have file blobs, ipc blobs, superblock blobs, etc. struct cred is the basis for subject identities in at least SELinux, probably others I'm sure.

With respect to performance issues, correct thing to do is still build out these local storage types for in-kernel LSMs but then fix the performance issue.

kp do you have any thoughts on this?

Quoted 1 line
Quoted 1 line

pw-bot: cr

reviewer
#9 lore raw

Re: [PATCH 1/2] bpf: Add BPF_MAP_TYPE_CRED_STORAGE map type and kfuncs

On Tue, Sep 16, 2025 at 8:25 AM David Windsor <[email protected]> wrote:
[...]

Quoted 8 lines
Quoted 3 lines

makes sense thanks

Hi,

Thinking about this more, hashmaps are still problematic for this case.

Quoted 8 lines

Meaning, placing a hook on security_cred_free alone for garbage collection / end-of-life processing isn't enough - we still have to deal with prepare/commit_creds. This flow works by having prepare_creds clone an existing cred object, then commit_creds works by swapping old creds with new one atomically, then later freeing the original cred. If we are not very careful there will be a period of time during which both cred objects could be valid, and I think this is worth the feature alone.

With cred local storage, we still need to deal with prepare/commit creds, right? cred local storage only makes sure the storage is allocated and freed. The BPF LSM programs still need to initiate the data properly based on the policy. IOW, whether we have cred local storage or not, it is necessary to handle all the paths that alloc/free the cred. Did I miss something here?

Thanks,
Song

#10 lore raw

Re: [PATCH 1/2] bpf: Add BPF_MAP_TYPE_CRED_STORAGE map type and kfuncs

Quoted 20 lines · Song Liu

On Tue, Sep 16, 2025 at 8:25 AM David Windsor <[email protected]> wrote:
[...]

Quoted 8 lines
Quoted 3 lines

makes sense thanks

Hi,

Thinking about this more, hashmaps are still problematic for this case.

Quoted 8 lines

Meaning, placing a hook on security_cred_free alone for garbage collection / end-of-life processing isn't enough - we still have to deal with prepare/commit_creds. This flow works by having prepare_creds clone an existing cred object, then commit_creds works by swapping old creds with new one atomically, then later freeing the original cred. If we are not very careful there will be a period of time during which both cred objects could be valid, and I think this is worth the feature alone.

Quoted 7 lines

With cred local storage, we still need to deal with prepare/commit creds, right? cred local storage only makes sure the storage is allocated and freed. The BPF LSM programs still need to initiate the data properly based on the policy. IOW, whether we have cred local storage or not, it is necessary to handle all the paths that alloc/free the cred. Did I miss something here?

Yes each LSM will have to do whatever it feels it should. Some will initialize their blob's data with one type of data, some another, depends on the LSM's use case. We're just here to provide the storage
- bpf cannot use the "classic" LSM storage blob.

I was referring to the fact that if we use a hashmap to track state on a per-cred basis there may be a period of time when it could be come stale during the state change from commit -> prepare_creds.

Thanks,
David

Quoted 2 lines

Thanks,
Song

reviewer
#11 lore raw

Re: [PATCH 1/2] bpf: Add BPF_MAP_TYPE_CRED_STORAGE map type and kfuncs

Quoted 30 lines · David Windsor
Quoted 20 lines · Song Liu

On Tue, Sep 16, 2025 at 8:25 AM David Windsor <[email protected]> wrote:
[...]

Quoted 8 lines
Quoted 3 lines

makes sense thanks

Hi,

Thinking about this more, hashmaps are still problematic for this case.

Quoted 8 lines

Meaning, placing a hook on security_cred_free alone for garbage collection / end-of-life processing isn't enough - we still have to deal with prepare/commit_creds. This flow works by having prepare_creds clone an existing cred object, then commit_creds works by swapping old creds with new one atomically, then later freeing the original cred. If we are not very careful there will be a period of time during which both cred objects could be valid, and I think this is worth the feature alone.

Quoted 7 lines

With cred local storage, we still need to deal with prepare/commit creds, right? cred local storage only makes sure the storage is allocated and freed. The BPF LSM programs still need to initiate the data properly based on the policy. IOW, whether we have cred local storage or not, it is necessary to handle all the paths that alloc/free the cred. Did I miss something here?

Quoted 8 lines

Yes each LSM will have to do whatever it feels it should. Some will initialize their blob's data with one type of data, some another, depends on the LSM's use case. We're just here to provide the storage
- bpf cannot use the "classic" LSM storage blob.

I was referring to the fact that if we use a hashmap to track state on a per-cred basis there may be a period of time when it could be come stale during the state change from commit -> prepare_creds.

I still don't see how cred local storage will make a difference here. If the cred is stale, the data attached to it is also stale. As long as we free the attached data together with the cred, it should just work, no?

Song

A message that is not in the archive (sent off-list or not yet received): CAEXv5_h=DoexdK4ZtFGS1Ya3NSM146qxxKyLhO9R736TS7=idg@mail.gmail.com

reviewer
#13 lore raw

Re: [PATCH 1/2] bpf: Add BPF_MAP_TYPE_CRED_STORAGE map type and kfuncs

Quoted 62 lines · David Windsor
Quoted 37 lines · Song Liu
Quoted 35 lines · David Windsor
Quoted 20 lines · Song Liu

On Tue, Sep 16, 2025 at 8:25 AM David Windsor <[email protected]> wrote:
[...]

Quoted 8 lines
Quoted 3 lines

makes sense thanks

Hi,

Thinking about this more, hashmaps are still problematic for this case.

Quoted 8 lines

Meaning, placing a hook on security_cred_free alone for garbage collection / end-of-life processing isn't enough - we still have to deal with prepare/commit_creds. This flow works by having prepare_creds clone an existing cred object, then commit_creds works by swapping old creds with new one atomically, then later freeing the original cred. If we are not very careful there will be a period of time during which both cred objects could be valid, and I think this is worth the feature alone.

Quoted 7 lines

With cred local storage, we still need to deal with prepare/commit creds, right? cred local storage only makes sure the storage is allocated and freed. The BPF LSM programs still need to initiate the data properly based on the policy. IOW, whether we have cred local storage or not, it is necessary to handle all the paths that alloc/free the cred. Did I miss something here?

Yes each LSM will have to do whatever it feels it should. Some will initialize their blob's data with one type of data, some another, depends on the LSM's use case. We're just here to provide the storage
- bpf cannot use the "classic" LSM storage blob.

Quoted 8 lines
Quoted 3 lines

I was referring to the fact that if we use a hashmap to track state on a per-cred basis there may be a period of time when it could be come stale during the state change from commit -> prepare_creds.

I still don't see how cred local storage will make a difference here. If the cred is stale, the data attached to it is also stale. As long as we free the attached data together with the cred, it should just work, no?

If we use local storage, the cred being stale will still be possible but its attached object will at least be consistent.

In this case, a cred object has been replaced with a new instance, but under RCU readers may still legally see the old pointer for some time.

If we're tracking state in a side map keyed by the pointer and you’ve already removed the old entry (after copying state to the new one), those readers will get a miss even though the old object is still valid:

CPU0 (commit_creds)        CPU1 (BPF hook)         Hashmap
-------------------------  ----------------------  ----------------------------
task->cred = old_pointer                           entry[old_pointer] = blob_old

rcu_assign_pointer(new_pointer)

Quoted 9 lines

still sees old_pointer lookup(old_pointer) -> blob_old

map_update(new_pointer, blob_old) entry[new_pointer] = blob_old
map_delete(old_pointer) del entry[old_pointer]

still sees old_pointer lookup(old_pointer) -> NULL (this is a failure)

This is a problem because we are calling map_delete(old_pointer) at commit_creds time. If we instead calls map_delete(old_pointer) at security_cred_free, it will be the same as cred local storage.

Does this make sense?

Thanks,
Song

#14 lore raw

PATCH v2 2/2 selftests/bpf: Add cred local storage tests

In reply to David Windsor

Add test coverage for the new BPF_MAP_TYPE_CRED_STORAGE map type. The test verifies that credential storage can be created, accessed, and persists across credential lifecycle events.

---
 .../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
new file mode 100644
index 000000000000..1a99f6453a0f
@@ -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