Generalizing bpf_local_storage
Subsystems (10)
v9 cover Generalizing bpf_local_storage
From: KP Singh <[email protected]>
# v8 -> v9
- Fixed reference count logic for files for inode maps.
- Other fixes suggested by Martin
- Rebase
# v7 -> v8
- Fixed an issue with BTF IDs for helpers and added bpf_<>_storage_delete to selftests to catch this issue.
- Update comments about refcounts and grabbed a refcount to the open file for userspace inode helpers.
- Rebase.
# v6 -> v7
- Updated the series to use Martin's POC patch:
https://lore.kernel.org/bpf/[email protected]/
I added a Co-developed-by: tag, but would need Martin's Signoff (was not sure of the procedure here).
- Rebase.
# v5 -> v6
- Fixed a build warning.
- Rebase.
# v4 -> v5
- Split non-functional changes into separate commits.
- Updated the cache macros to be simpler.
- Fixed some bugs noticed by Martin.
- Updated the userspace map functions to use an fd for lookups, updates and deletes.
- Rebase.
# v3 -> v4
- Fixed a missing include to bpf_sk_storage.h in bpf_sk_storage.c
- Fixed some functions that were not marked as static which led to W=1 compilation warnings.
# v2 -> v3
* Restructured the code as per Martin's suggestions:
- Common functionality in bpf_local_storage.c
- bpf_sk_storage functionality remains in net/bpf_sk_storage.
- bpf_inode_storage is kept separate as it is enabled only with CONFIG_BPF_LSM.
* A separate cache for inode and sk storage with macros to define it.
* Use the ops style approach as suggested by Martin instead of the enum + switch style.
* Added the inode map to bpftool bash completion and docs.
* Rebase and indentation fixes.
# v1 -> v2
* Use the security blob pointer instead of dedicated member in struct inode.
* Better code re-use as suggested by Alexei.
* Dropped the inode count arithmetic as pointed out by Alexei.
* Minor bug fixes and rebase.
bpf_sk_storage can already be used by some BPF program types to annotate socket objects. These annotations are managed with the life-cycle of the object (i.e. freed when the object is freed) which makes BPF programs much simpler and less prone to errors and leaks.
This patch series:
* Generalizes the bpf_sk_storage infrastructure to allow easy implementation of local storage for other objects
* Implements local storage for inodes
* Makes both bpf_{sk, inode}_storage available to LSM programs.
Local storage is safe to use in LSM programs as the attachment sites are limited and the owning object won't be freed, however, this is not the case for tracing. Usage in tracing is expected to follow a white-list based approach similar to the d_path helper
(https://lore.kernel.org/bpf/[email protected]).
Access to local storage would allow LSM programs to implement stateful detections like detecting the unlink of a running executable from the examples shared as a part of the KRSI series
https://lore.kernel.org/bpf/[email protected]/
and
https://github.com/sinkap/linux-krsi/blob/patch/v1/examples/samples/bpf/lsm_detect_exec_unlink.c
KP Singh (7):
bpf: Renames in preparation for bpf_local_storage
bpf: Generalize caching for sk_storage.
bpf: Generalize bpf_sk_storage
bpf: Split bpf_local_storage to bpf_sk_storage
bpf: Implement bpf_local_storage for inodes
bpf: Allow local storage to be used from LSM programs
bpf: Add selftests for local_storage
include/linux/bpf.h | 8 + include/linux/bpf_local_storage.h | 163 ++++ include/linux/bpf_lsm.h | 29 + include/linux/bpf_types.h | 3 + include/net/bpf_sk_storage.h | 14 + include/net/sock.h | 4 +- include/uapi/linux/bpf.h | 53 +- kernel/bpf/Makefile | 2 + kernel/bpf/bpf_inode_storage.c | 265 ++++++ kernel/bpf/bpf_local_storage.c | 600 +++++++++++++ kernel/bpf/bpf_lsm.c | 21 +- kernel/bpf/syscall.c | 3 +- kernel/bpf/verifier.c | 10 + net/core/bpf_sk_storage.c | 830 +++--------------- security/bpf/hooks.c | 7 + .../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 | 53 +- tools/lib/bpf/libbpf_probes.c | 5 +- .../bpf/prog_tests/test_local_storage.c | 60 ++ .../selftests/bpf/progs/local_storage.c | 140 +++ 22 files changed, 1566 insertions(+), 712 deletions(-) create mode 100644 include/linux/bpf_local_storage.h create mode 100644 kernel/bpf/bpf_inode_storage.c create mode 100644 kernel/bpf/bpf_local_storage.c create mode 100644 tools/testing/selftests/bpf/prog_tests/test_local_storage.c create mode 100644 tools/testing/selftests/bpf/progs/local_storage.c
Signature · 1 line
2.28.0.297.g1956fa8f8d-goog
PATCH v9 1/7 bpf: Renames in preparation for bpf_local_storage
From: KP Singh <[email protected]>
A purely mechanical change to split the renaming from the actual generalization.
Flags/consts:
SK_STORAGE_CREATE_FLAG_MASK BPF_LOCAL_STORAGE_CREATE_FLAG_MASK
BPF_SK_STORAGE_CACHE_SIZE BPF_LOCAL_STORAGE_CACHE_SIZE
MAX_VALUE_SIZE BPF_LOCAL_STORAGE_MAX_VALUE_SIZE
Structs:
bucket bpf_local_storage_map_bucket
bpf_sk_storage_map bpf_local_storage_map
bpf_sk_storage_data bpf_local_storage_data
bpf_sk_storage_elem bpf_local_storage_elem
bpf_sk_storage bpf_local_storage
The "sk" member in bpf_local_storage is also updated to "owner" in preparation for changing the type to void * in a subsequent patch.
Functions:
selem_linked_to_sk selem_linked_to_storage
selem_alloc bpf_selem_alloc
__selem_unlink_sk bpf_selem_unlink_storage_nolock
__selem_link_sk bpf_selem_link_storage_nolock
selem_unlink_sk __bpf_selem_unlink_storage
sk_storage_update bpf_local_storage_update
__sk_storage_lookup bpf_local_storage_lookup
bpf_sk_storage_map_free bpf_local_storage_map_free
bpf_sk_storage_map_alloc bpf_local_storage_map_alloc
bpf_sk_storage_map_alloc_check bpf_local_storage_map_alloc_check
bpf_sk_storage_map_check_btf bpf_local_storage_map_check_btf
--- include/net/sock.h | 4 +- net/core/bpf_sk_storage.c | 488 +++++++++++++++++++------------------- 2 files changed, 252 insertions(+), 240 deletions(-)
include/net/sock.h +2 -2
@@ -246,7 +246,7 @@ struct sock_common {/* public: */ }; -struct bpf_sk_storage ; +struct bpf_local_storage ;/** * struct sock - network layer representation of sockets
@@ -517,7 +517,7 @@ struct sock {void (*sk_destruct)(struct sock *sk);struct sock_reuseport __rcu *sk_reuseport_cb;#ifdef CONFIG_BPF_SYSCALL -struct bpf_sk_storage __rcu *sk_bpf_storage; +struct bpf_local_storage __rcu *sk_bpf_storage;#endif struct rcu_head sk_rcu; };
net/core/bpf_sk_storage.c +250 -238
1,024 lines not on this page · raw message
Signature · 1 line
2.28.0.297.g1956fa8f8d-goog
PATCH v9 2/7 bpf: Generalize caching for sk_storage.
In reply to KP SinghFrom: KP Singh <[email protected]>
Provide the a ability to define local storage caches on a per-object type basis. The caches and caching indices for different objects should not be inter-mixed as suggested in:
https://lore.kernel.org/bpf/[email protected]/
"Caching a sk-storage at idx=0 of a sk should not stop an inode-storage to be cached at the same idx of a inode."
--- include/net/bpf_sk_storage.h | 19 +++++++++++++++++++ net/core/bpf_sk_storage.c | 31 +++++++++++++++---------------- 2 files changed, 34 insertions(+), 16 deletions(-)
include/net/bpf_sk_storage.h +19 -0
@@ -3,6 +3,9 @@#ifndef _BPF_SK_STORAGE_H#define _BPF_SK_STORAGE_H +#include <linux/types.h> +#include <linux/spinlock.h> +struct sock ;void bpf_sk_storage_free (struct sock *sk);
@@ -15,6 +18,22 @@ struct sk_buff;struct nlattr ;struct sock ; +#define BPF_LOCAL_STORAGE_CACHE_SIZE 16 + +struct bpf_local_storage_cache { +spinlock_t idx_lock; +u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE ]; +}; + +#define DEFINE_BPF_STORAGE_CACHE (name) \ +static struct bpf_local_storage_cache name = { \ + .idx_lock = __SPIN_LOCK_UNLOCKED(name.idx_lock), \ +} + +u16 bpf_local_storage_cache_idx_get (struct bpf_local_storage_cache *cache); +void bpf_local_storage_cache_idx_free (struct bpf_local_storage_cache *cache, +u16 idx); +#ifdef CONFIG_BPF_SYSCALL int bpf_sk_storage_clone (const struct sock *sk,struct sock *newsk); struct bpf_sk_storage_diag *
net/core/bpf_sk_storage.c +15 -16
@@ -14,6 +14,8 @@#define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE) +DEFINE_BPF_STORAGE_CACHE (sk_cache); +struct bpf_local_storage_map_bucket {struct hlist_head list;raw_spinlock_t lock;
@@ -78,10 +80,6 @@ struct bpf_local_storage_elem {#define SELEM (_SDATA) \ container_of((_SDATA), struct bpf_local_storage_elem, sdata)#define SDATA (_SELEM) (&(_SELEM)->sdata) -#define BPF_LOCAL_STORAGE_CACHE_SIZE 16 - -static DEFINE_SPINLOCK (cache_idx_lock); -static u64 cache_idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE ];struct bpf_local_storage {struct bpf_local_storage_data __rcu *cache[BPF_LOCAL_STORAGE_CACHE_SIZE ];
@@ -521,16 +519,16 @@ static int sk_storage_delete(struct sock *sk, struct bpf_map *map)return 0 ; } -static u16 cache_idx_get (void ) +u16 bpf_local_storage_cache_idx_get (struct bpf_local_storage_cache *cache) {u64 min_usage =U64_MAX ;u16 i, res =0 ; -spin_lock (&cache_idx_lock); +spin_lock (&cache->idx_lock);for (i =0 ; i <BPF_LOCAL_STORAGE_CACHE_SIZE ; i++) { -if (cache_idx_usage_counts[i] < min_usage) { - min_usage = cache_idx_usage_counts[i]; +if (cache->idx_usage_counts[i] < min_usage) { + min_usage = cache->idx_usage_counts[i]; res = i;/* Found a free cache_idx */
@@ -538,18 +536,19 @@ static u16 cache_idx_get(void)break ; } } - cache_idx_usage_counts[res]++; + cache->idx_usage_counts[res]++; -spin_unlock (&cache_idx_lock); +spin_unlock (&cache->idx_lock);return res; } -static void cache_idx_free (u16 idx) +void bpf_local_storage_cache_idx_free (struct bpf_local_storage_cache *cache, +u16 idx) { -spin_lock (&cache_idx_lock); - cache_idx_usage_counts[idx]--; -spin_unlock (&cache_idx_lock); +spin_lock (&cache->idx_lock); + cache->idx_usage_counts[idx]--; +spin_unlock (&cache->idx_lock); }/* Called by __sk_destruct() & bpf_sk_storage_clone() */
@@ -601,7 +600,7 @@ static void bpf_local_storage_map_free(struct bpf_map *map) smap = (struct bpf_local_storage_map *)map; -cache_idx_free (smap->cache_idx); +bpf_local_storage_cache_idx_free (&sk_cache, smap->cache_idx);/* Note that this map might be concurrently cloned from * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone
@@ -718,7 +717,7 @@ static struct bpf_map *bpf_local_storage_map_alloc(union bpf_attr *attr) smap->elem_size =sizeof (struct bpf_local_storage_elem ) + attr->value_size; - smap->cache_idx =cache_idx_get (); + smap->cache_idx =bpf_local_storage_cache_idx_get (&sk_cache);return &smap->map; }
Signature · 1 line
2.28.0.297.g1956fa8f8d-goog
PATCH v9 3/7 bpf: Generalize bpf_sk_storage
In reply to KP SinghFrom: KP Singh <[email protected]>
Refactor the functionality in bpf_sk_storage.c so that concept of storage linked to kernel objects can be extended to other objects like inode, task_struct etc.
Each new local storage will still be a separate map and provide its own set of helpers. This allows for future object specific extensions and still share a lot of the underlying implementation.
This includes the changes suggested by Martin in:
https://lore.kernel.org/bpf/[email protected]/
adding new map operations to support bpf_local_storage maps:
* storages for different kernel objects to optionally have different memory charging strategy (map_local_storage_charge, map_local_storage_uncharge)
* Functionality to extract the storage pointer from a pointer to the owning object (map_owner_storage_ptr)
--- include/linux/bpf.h | 8 ++ include/net/bpf_sk_storage.h | 52 +++++++ include/uapi/linux/bpf.h | 8 +- net/core/bpf_sk_storage.c | 238 +++++++++++++++++++++------------ tools/include/uapi/linux/bpf.h | 8 +- 5 files changed, 228 insertions(+), 86 deletions(-)
include/linux/bpf.h +8 -0
@@ -34,6 +34,8 @@ struct btf_type;struct exception_table_entry ;struct seq_operations ;struct bpf_iter_aux_info ; +struct bpf_local_storage ; +struct bpf_local_storage_map ;extern struct idr btf_idr;extern spinlock_t btf_idr_lock;
@@ -104,6 +106,12 @@ struct bpf_map_ops {__poll_t (*map_poll)(struct bpf_map *map,struct file *filp,struct poll_table_struct *pts); +/* Functions called by bpf_local_storage maps */ +int (*map_local_storage_charge)(struct bpf_local_storage_map *smap, +void *owner,u32 size); +void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap, +void *owner,u32 size); +struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(void *owner);/* BTF name and id of struct allocated by map_alloc */ const char *const map_btf_name;int *map_btf_id;
include/net/bpf_sk_storage.h +52 -0
72 lines not on this page · raw message
include/uapi/linux/bpf.h +6 -2
@@ -3648,9 +3648,13 @@ enum { BPF_F_SYSCTL_BASE_NAME = (1ULL << 0), }; -/* BPF_FUNC_sk_storage_get flags */ +/* BPF_FUNC_<kernel_obj>_storage_get flags */ enum { -BPF_SK_STORAGE_GET_F_CREATE = (1ULL <<0 ), +BPF_LOCAL_STORAGE_GET_F_CREATE = (1ULL <<0 ), +/* BPF_SK_STORAGE_GET_F_CREATE is only kept for backward compatibility +* and BPF_LOCAL_STORAGE_GET_F_CREATE must be used instead. +*/ +BPF_SK_STORAGE_GET_F_CREATE =BPF_LOCAL_STORAGE_GET_F_CREATE , };/* BPF_FUNC_read_branch_records flags. */
net/core/bpf_sk_storage.c +156 -82
469 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +6 -2
@@ -3648,9 +3648,13 @@ enum { BPF_F_SYSCTL_BASE_NAME = (1ULL << 0), }; -/* BPF_FUNC_sk_storage_get flags */ +/* BPF_FUNC_<kernel_obj>_storage_get flags */ enum { -BPF_SK_STORAGE_GET_F_CREATE = (1ULL <<0 ), +BPF_LOCAL_STORAGE_GET_F_CREATE = (1ULL <<0 ), +/* BPF_SK_STORAGE_GET_F_CREATE is only kept for backward compatibility +* and BPF_LOCAL_STORAGE_GET_F_CREATE must be used instead. +*/ +BPF_SK_STORAGE_GET_F_CREATE =BPF_LOCAL_STORAGE_GET_F_CREATE , };/* BPF_FUNC_read_branch_records flags. */
Signature · 1 line
2.28.0.297.g1956fa8f8d-goog
Re: [PATCH bpf-next v9 3/7] bpf: Generalize bpf_sk_storage
Quoted 16 lines · KP Singh
From: KP Singh <[email protected]>
Refactor the functionality in
bpf_sk_storage.cso that concept of storage linked to kernel objects can be extended to other objects like inode,task_structetc.Each new local storage will still be a separate map and provide its own set of helpers. This allows for future object specific extensions and still share a lot of the underlying implementation.
This includes the changes suggested by Martin in:
https://lore.kernel.org/bpf/[email protected]/
adding new map operations to support
bpf_local_storagemaps:
Quoted 8 lines
* storages for different kernel objects to optionally have different memory charging strategy (
map_local_storage_charge,map_local_storage_uncharge)
* Functionality to extract the storage pointer from a pointer to the owning object (map_owner_storage_ptr)Co-developed-by Martin KaFai Lau <[email protected]>Signed-off-by KP Singh <[email protected]>
PATCH v9 4/7 bpf: Split bpf_local_storage to bpf_sk_storage
In reply to KP SinghFrom: KP Singh <[email protected]>
A purely mechanical change:
bpf_sk_storage.c = bpf_sk_storage.c + bpf_local_storage.c bpf_sk_storage.h = bpf_sk_storage.h + bpf_local_storage.h
--- include/linux/bpf_local_storage.h | 163 ++++++++ include/net/bpf_sk_storage.h | 61 +-- kernel/bpf/Makefile | 1 + kernel/bpf/bpf_local_storage.c | 600 ++++++++++++++++++++++++++ net/core/bpf_sk_storage.c | 672 +----------------------------- 5 files changed, 766 insertions(+), 731 deletions(-) create mode 100644 include/linux/bpf_local_storage.h create mode 100644 kernel/bpf/bpf_local_storage.c
include/linux/bpf_local_storage.h new file +163 -0
@@ -0,0 +1,163 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (c) 2019 Facebook +* Copyright 2020 Google LLC. +*/ + +#ifndef _BPF_LOCAL_STORAGE_H +#define _BPF_LOCAL_STORAGE_H + +#include <linux/bpf.h> +#include <linux/rculist.h> +#include <linux/list.h> +#include <linux/hash.h> +#include <linux/types.h> +#include <uapi/linux/btf.h> + +#define BPF_LOCAL_STORAGE_CACHE_SIZE 16 + +struct bpf_local_storage_map_bucket { +struct hlist_head list; +raw_spinlock_t lock; +}; + +/* Thp map is not the primary owner of a bpf_local_storage_elem. +* Instead, the container object (eg. sk->sk_bpf_storage) is. +* +* The map (bpf_local_storage_map) is for two purposes +* 1. Define the size of the "local storage". It is +* the map's value_size. +* +* 2. Maintain a list to keep track of all elems such +* that they can be cleaned up during the map destruction. +* +* When a bpf local storage is being looked up for a +* particular object, the "bpf_map" pointer is actually used +* as the "key" to search in the list of elem in +* the respective bpf_local_storage owned by the object. +* +* e.g. sk->sk_bpf_storage is the mini-map with the "bpf_map" pointer +* as the searching key. +*/ +struct bpf_local_storage_map { +struct bpf_map map; +/* Lookup elem does not require accessing the map. +* +* Updating/Deleting requires a bucket lock to +* link/unlink the elem from the map. Having +* multiple buckets to improve contention. +*/ +struct bpf_local_storage_map_bucket *buckets; +u32 bucket_log; +u16 elem_size; +u16 cache_idx; +}; + +struct bpf_local_storage_data { +/* smap is used as the searching key when looking up +* from the object's bpf_local_storage. +* +* Put it in the same cacheline as the data to minimize +* the number of cachelines access during the cache hit case. +*/ +struct bpf_local_storage_map __rcu *smap; +u8 data[]__aligned(8) ; +}; + +/* Linked to bpf_local_storage and bpf_local_storage_map */ +struct bpf_local_storage_elem { +struct hlist_node map_node;/* Linked to bpf_local_storage_map */ +struct hlist_node snode;/* Linked to bpf_local_storage */ +struct bpf_local_storage __rcu *local_storage; +struct rcu_head rcu; +/* 8 bytes hole */ +/* The data is stored in aother cacheline to minimize +* the number of cachelines access during a cache hit. +*/ +struct bpf_local_storage_data sdata____cacheline_aligned ; +}; + +struct bpf_local_storage { +struct bpf_local_storage_data __rcu *cache[BPF_LOCAL_STORAGE_CACHE_SIZE ]; +struct hlist_head list;/* List of bpf_local_storage_elem */ +void *owner;/* The object that owns the above "list" of +* bpf_local_storage_elem. +*/ +struct rcu_head rcu; +raw_spinlock_t lock;/* Protect adding/removing from the "list" */ +}; + +/* U16_MAX is much more than enough for sk local storage +* considering a tcp_sock is ~2k. +*/ +#define BPF_LOCAL_STORAGE_MAX_VALUE_SIZE \ + min_t(u32, \ + (KMALLOC_MAX_SIZE - MAX_BPF_STACK - \ + sizeof(struct bpf_local_storage_elem)), \ + (U16_MAX - sizeof(struct bpf_local_storage_elem))) + +#define SELEM (_SDATA) \ + container_of((_SDATA), struct bpf_local_storage_elem, sdata) +#define SDATA (_SELEM) (&(_SELEM)->sdata) + +#define BPF_LOCAL_STORAGE_CACHE_SIZE 16 + +struct bpf_local_storage_cache { +spinlock_t idx_lock; +u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE ]; +}; + +#define DEFINE_BPF_STORAGE_CACHE (name) \ +static struct bpf_local_storage_cache name = { \ + .idx_lock = __SPIN_LOCK_UNLOCKED(name.idx_lock), \ +} + +u16 bpf_local_storage_cache_idx_get (struct bpf_local_storage_cache *cache); +void bpf_local_storage_cache_idx_free (struct bpf_local_storage_cache *cache, +u16 idx); + +/* Helper functions for bpf_local_storage */ +int bpf_local_storage_map_alloc_check (union bpf_attr *attr); + +struct bpf_local_storage_map *bpf_local_storage_map_alloc (union bpf_attr *attr); + +struct bpf_local_storage_data * +bpf_local_storage_lookup (struct bpf_local_storage *local_storage, +struct bpf_local_storage_map *smap, +bool cacheit_lockit); + +void bpf_local_storage_map_free (struct bpf_local_storage_map *smap); + +int bpf_local_storage_map_check_btf (const struct bpf_map *map, +const struct btf *btf, +const struct btf_type *key_type, +const struct btf_type *value_type); + +void bpf_selem_link_storage_nolock (struct bpf_local_storage *local_storage, +struct bpf_local_storage_elem *selem); + +bool bpf_selem_unlink_storage_nolock (struct bpf_local_storage *local_storage, +struct bpf_local_storage_elem *selem, +bool uncharge_omem); + +void bpf_selem_unlink (struct bpf_local_storage_elem *selem); + +void bpf_selem_link_map (struct bpf_local_storage_map *smap, +struct bpf_local_storage_elem *selem); + +void bpf_selem_unlink_map (struct bpf_local_storage_elem *selem); + +struct bpf_local_storage_elem * +bpf_selem_alloc (struct bpf_local_storage_map *smap,void *owner,void *value, +bool charge_mem); + +int +bpf_local_storage_alloc (void *owner, +struct bpf_local_storage_map *smap, +struct bpf_local_storage_elem *first_selem); + +struct bpf_local_storage_data * +bpf_local_storage_update (void *owner,struct bpf_local_storage_map *smap, +void *value,u64 map_flags); + +#endif /* _BPF_LOCAL_STORAGE_H */
include/net/bpf_sk_storage.h +1 -60
73 lines not on this page · raw message
kernel/bpf/Makefile +1 -0
@@ -12,6 +12,7 @@ obj-$(CONFIG_BPF_JIT) += dispatcher.o ifeq ($(CONFIG_NET),y) obj-$(CONFIG_BPF_SYSCALL) += devmap.o obj-$(CONFIG_BPF_SYSCALL) += cpumap.o +obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o obj-$(CONFIG_BPF_SYSCALL) += offload.o obj-$(CONFIG_BPF_SYSCALL) += net_namespace.o endif
kernel/bpf/bpf_local_storage.c new file +600 -0
600 lines not on this page · raw message
net/core/bpf_sk_storage.c +1 -671
721 lines not on this page · raw message
Signature · 1 line
2.28.0.297.g1956fa8f8d-goog
Re: [PATCH bpf-next v9 4/7] bpf: Split bpf_local_storage to bpf_sk_storage
Quoted 8 lines · KP Singh
From: KP Singh <[email protected]>
A purely mechanical change:
bpf_sk_storage.c=bpf_sk_storage.c+bpf_local_storage.cbpf_sk_storage.h=bpf_sk_storage.h+bpf_local_storage.hSigned-off-by KP Singh <[email protected]>
PATCH v9 5/7 bpf: Implement bpf_local_storage for inodes
In reply to KP SinghFrom: KP Singh <[email protected]>
Similar to bpf_local_storage for sockets, add local storage for inodes. The life-cycle of storage is managed with the life-cycle of the inode. i.e. the storage is destroyed along with the owning inode.
The BPF LSM allocates an __rcu pointer to the bpf_local_storage in the security blob which are now stackable and can co-exist with other LSMs.
--- include/linux/bpf_lsm.h | 29 ++ include/linux/bpf_types.h | 3 + include/uapi/linux/bpf.h | 38 +++ kernel/bpf/Makefile | 1 + kernel/bpf/bpf_inode_storage.c | 265 ++++++++++++++++++ kernel/bpf/syscall.c | 3 +- kernel/bpf/verifier.c | 10 + security/bpf/hooks.c | 7 + .../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 | 38 +++ tools/lib/bpf/libbpf_probes.c | 5 +- 13 files changed, 401 insertions(+), 6 deletions(-) create mode 100644 kernel/bpf/bpf_inode_storage.c
include/linux/bpf_lsm.h +29 -0
@@ -17,9 +17,28 @@#include <linux/lsm_hook_defs.h> #undef LSM_HOOK +struct bpf_storage_blob { +struct bpf_local_storage __rcu *storage; +}; + +extern struct lsm_blob_sizes bpf_lsm_blob_sizes; +int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog,const struct bpf_prog *prog); +static inline struct bpf_storage_blob *bpf_inode ( +const struct inode *inode) +{ +if (unlikely (!inode->i_security)) +return NULL ; + +return inode->i_security + bpf_lsm_blob_sizes.lbs_inode; +} + +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); +#else /* !CONFIG_BPF_LSM */ static inline int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
@@ -28,6 +47,16 @@ static inline int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,return -EOPNOTSUPP ; } +static inline struct bpf_storage_blob *bpf_inode ( +const struct inode *inode) +{ +return NULL ; +} + +static inline void bpf_inode_storage_free (struct inode *inode) +{ +} +#endif /* CONFIG_BPF_LSM */ #endif /* _LINUX_BPF_LSM_H */
include/linux/bpf_types.h +3 -0
@@ -107,6 +107,9 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_SK_STORAGE, sk_storage_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKMAP, sock_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKHASH, sock_hash_ops)#endif +#ifdef CONFIG_BPF_LSM +BPF_MAP_TYPE(BPF_MAP_TYPE_INODE_STORAGE, inode_storage_map_ops) +#endif BPF_MAP_TYPE(BPF_MAP_TYPE_CPUMAP, cpu_map_ops)#if defined(CONFIG_XDP_SOCKETS )BPF_MAP_TYPE (BPF_MAP_TYPE_XSKMAP , xsk_map_ops)
include/uapi/linux/bpf.h +38 -0
@@ -155,6 +155,7 @@ enum bpf_map_type {BPF_MAP_TYPE_DEVMAP_HASH ,BPF_MAP_TYPE_STRUCT_OPS ,BPF_MAP_TYPE_RINGBUF , + BPF_MAP_TYPE_INODE_STORAGE, };/* Note that tracing related programs such as
@@ -3395,6 +3396,41 @@ union bpf_attr {* A non-negative value equal to or less than *size* on success, * or a negative error in case of failure. * +* void *bpf_inode_storage_get(struct bpf_map *map, void *inode, void *value, u64 flags) +* Description +* Get a bpf_local_storage from an *inode*. +* +* Logically, it could be thought of as getting the value from +* a *map* with *inode* as the **key**. From this +* perspective, the usage is not much different from +* **bpf_map_lookup_elem**\ (*map*, **&**\ *inode*) except this +* helper enforces the key must be an inode and the map must also +* be a **BPF_MAP_TYPE_INODE_STORAGE**. +* +* Underneath, the value is stored locally at *inode* 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 *inode*. +* +* 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_inode_storage_delete(struct bpf_map *map, void *inode) +* Description +* Delete a bpf_local_storage from an *inode*. +* Return +* 0 on success. +* +* **-ENOENT** if the bpf_local_storage cannot be found. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -3539,6 +3575,8 @@ union bpf_attr {FN (skc_to_tcp_request_sock), \FN (skc_to_udp6_sock), \FN (get_task_stack), \ +FN (inode_storage_get), \ +FN (inode_storage_delete), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/Makefile +1 -0
@@ -5,6 +5,7 @@ CFLAGS_core.o += $(call cc-disable-warning, override-init) obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o 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_LSM} += bpf_inode_storage.o obj-$(CONFIG_BPF_SYSCALL) += disasm.o obj-$(CONFIG_BPF_JIT) += trampoline.o obj-$(CONFIG_BPF_SYSCALL) += btf.o
kernel/bpf/bpf_inode_storage.c new file +265 -0
265 lines not on this page · raw message
kernel/bpf/syscall.c +2 -1
@@ -769,7 +769,8 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf,if (map->map_type !=BPF_MAP_TYPE_HASH && map->map_type !=BPF_MAP_TYPE_ARRAY && map->map_type !=BPF_MAP_TYPE_CGROUP_STORAGE && - map->map_type !=BPF_MAP_TYPE_SK_STORAGE ) + map->map_type !=BPF_MAP_TYPE_SK_STORAGE && + map->map_type !=BPF_MAP_TYPE_INODE_STORAGE )return -ENOTSUPP ;if (map->spin_lock_off +sizeof (struct bpf_spin_lock ) > map->value_size) {
kernel/bpf/verifier.c +10 -0
@@ -4311,6 +4311,11 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, func_id != BPF_FUNC_sk_storage_delete) goto error;break ; +case BPF_MAP_TYPE_INODE_STORAGE : +if (func_id != BPF_FUNC_inode_storage_get && + func_id != BPF_FUNC_inode_storage_delete) + goto error; +break ;default :break ; }
@@ -4384,6 +4389,11 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,if (map->map_type !=BPF_MAP_TYPE_SK_STORAGE ) goto error;break ; +case BPF_FUNC_inode_storage_get: +case BPF_FUNC_inode_storage_delete: +if (map->map_type !=BPF_MAP_TYPE_INODE_STORAGE ) + goto error; +break ;default :break ; }
security/bpf/hooks.c +7 -0
@@ -3,6 +3,7 @@/* * Copyright (C) 2020 Google LLC. */ +#include <linux/bpf_local_storage.h> #include <linux/lsm_hooks.h> #include <linux/bpf_lsm.h>
@@ -11,6 +12,7 @@ static struct security_hook_list bpf_lsm_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(NAME, bpf_lsm_##NAME),#include <linux/lsm_hook_defs.h> #undef LSM_HOOK + LSM_HOOK_INIT(inode_free_security, bpf_inode_storage_free), };static int __init bpf_lsm_init (void )
@@ -20,7 +22,12 @@ static int __init bpf_lsm_init(void)return 0 ; } +struct lsm_blob_sizes bpf_lsm_blob_sizes __lsm_ro_after_init = { + .lbs_inode =sizeof (struct bpf_storage_blob ), +}; +DEFINE_LSM (bpf) = { .name ="bpf" , .init = bpf_lsm_init, + .blobs = &bpf_lsm_blob_sizes };
tools/bpf/bpftool/Documentation/bpftool-map.rst +1 -1
@@ -49,7 +49,7 @@ MAP COMMANDS | | **lru_percpu_hash** | **lpm_trie** | **array_of_maps** | **hash_of_maps** | | **devmap** | **devmap_hash** | **sockmap** | **cpumap** | **xskmap** | **sockhash** | | **cgroup_storage** | **reuseport_sockarray** | **percpu_cgroup_storage** -| | **queue** | **stack** | **sk_storage** | **struct_ops** | **ringbuf** } +| | **queue** | **stack** | **sk_storage** | **struct_ops** | **ringbuf** | **inode_storage** } DESCRIPTION ===========
tools/bpf/bpftool/bash-completion/bpftool +2 -1
@@ -704,7 +704,8 @@ _bpftool() lru_percpu_hash lpm_trie array_of_maps \ hash_of_maps devmap devmap_hash sockmap cpumap \ xskmap sockhash cgroup_storage reuseport_sockarray \ - percpu_cgroup_storage queue stack' -- \ + percpu_cgroup_storage queue stack sk_storage \ + struct_ops inode_storage' -- \ "$cur" ) ) return 0 ;;
tools/bpf/bpftool/map.c +2 -1
@@ -50,6 +50,7 @@ const char * const map_type_name[] = { [BPF_MAP_TYPE_SK_STORAGE] = "sk_storage", [BPF_MAP_TYPE_STRUCT_OPS] = "struct_ops", [BPF_MAP_TYPE_RINGBUF] = "ringbuf", + [BPF_MAP_TYPE_INODE_STORAGE] = "inode_storage", };const size_t map_type_name_size =ARRAY_SIZE (map_type_name);
@@ -1442,7 +1443,7 @@ static int do_help(int argc, char **argv)" lru_percpu_hash | lpm_trie | array_of_maps | hash_of_maps |\n" " devmap | devmap_hash | sockmap | cpumap | xskmap | sockhash |\n" " cgroup_storage | reuseport_sockarray | percpu_cgroup_storage |\n" -" queue | stack | sk_storage | struct_ops | ringbuf }\n" +" queue | stack | sk_storage | struct_ops | ringbuf | inode_storage }\n" " " HELP_SPEC_OPTIONS "\n" "" , bin_name, argv[-2]);
tools/include/uapi/linux/bpf.h +38 -0
@@ -155,6 +155,7 @@ enum bpf_map_type {BPF_MAP_TYPE_DEVMAP_HASH ,BPF_MAP_TYPE_STRUCT_OPS ,BPF_MAP_TYPE_RINGBUF , + BPF_MAP_TYPE_INODE_STORAGE, };/* Note that tracing related programs such as
@@ -3395,6 +3396,41 @@ union bpf_attr {* A non-negative value equal to or less than *size* on success, * or a negative error in case of failure. * +* void *bpf_inode_storage_get(struct bpf_map *map, void *inode, void *value, u64 flags) +* Description +* Get a bpf_local_storage from an *inode*. +* +* Logically, it could be thought of as getting the value from +* a *map* with *inode* as the **key**. From this +* perspective, the usage is not much different from +* **bpf_map_lookup_elem**\ (*map*, **&**\ *inode*) except this +* helper enforces the key must be an inode and the map must also +* be a **BPF_MAP_TYPE_INODE_STORAGE**. +* +* Underneath, the value is stored locally at *inode* 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 *inode*. +* +* 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_inode_storage_delete(struct bpf_map *map, void *inode) +* Description +* Delete a bpf_local_storage from an *inode*. +* Return +* 0 on success. +* +* **-ENOENT** if the bpf_local_storage cannot be found. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -3539,6 +3575,8 @@ union bpf_attr {FN (skc_to_tcp_request_sock), \FN (skc_to_udp6_sock), \FN (get_task_stack), \ +FN (inode_storage_get), \ +FN (inode_storage_delete), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
tools/lib/bpf/libbpf_probes.c +3 -2
@@ -170,7 +170,7 @@ int libbpf__load_raw_btf(const char *raw_types, size_t types_len,return btf_fd; } -static int load_sk_storage_btf (void ) +static int load_local_storage_btf (void ) {const char strs[] ="\0bpf_spin_lock\0val\0cnt\0l" ;/* struct bpf_spin_lock {
@@ -229,12 +229,13 @@ bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex) key_size =0 ;break ;case BPF_MAP_TYPE_SK_STORAGE : +case BPF_MAP_TYPE_INODE_STORAGE : btf_key_type_id =1 ; btf_value_type_id =3 ; value_size =8 ; max_entries =0 ; map_flags =BPF_F_NO_PREALLOC ; - btf_fd =load_sk_storage_btf (); + btf_fd =load_local_storage_btf ();if (btf_fd <0 )return false;break ;
Signature · 1 line
2.28.0.297.g1956fa8f8d-goog
Re: [PATCH bpf-next v9 5/7] bpf: Implement bpf_local_storage for inodes
Quoted 9 lines · KP Singh
From: KP Singh <[email protected]>
Similar to
bpf_local_storagefor sockets, add local storage for inodes. The life-cycle of storage is managed with the life-cycle of the inode. i.e. the storage is destroyed along with the owning inode.The BPF LSM allocates an
__rcupointer to thebpf_local_storagein the security blob which are now stackable and can co-exist with other LSMs.
[ ... ]
Quoted 27 lines
kernel/bpf/bpf_inode_storage.c new file +21
-0@@ -0,0 +1,265 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (c) 2019 Facebook +* Copyright 2020 Google LLC. +*/ + +#include <linux/rculist.h> +#include <linux/list.h> +#include <linux/hash.h> +#include <linux/types.h> +#include <linux/spinlock.h> +#include <linux/bpf.h> +#include <linux/bpf_local_storage.h> +#include <net/sock.h> +#include <uapi/linux/sock_diag.h> +#include <uapi/linux/btf.h> +#include <linux/bpf_lsm.h> +#include <linux/btf_ids.h> +#include <linux/fdtable.h> + +DEFINE_BPF_STORAGE_CACHE (inode_cache);
kernel/bpf/bpf_inode_storage.c line 31
Quoted 10 lines
kernel/bpf/bpf_inode_storage.c new file +10
-0+ +static struct bpf_local_storage __rcu ** +inode_storage_ptr (void *owner) +{ +struct inode *inode = owner; +struct bpf_storage_blob *bsb; + + bsb =bpf_inode (inode); +if (!bsb) +return NULL ;
just noticed this one. NULL could be returned here. When will it happen?
Quoted 4 lines
kernel/bpf/bpf_inode_storage.c new file +4
-0+return &bsb->storage; +} + +static struct bpf_local_storage_data *inode_storage_lookup(struct inode *inode,
kernel/bpf/bpf_inode_storage.c line 45
Quoted 10 lines
kernel/bpf/bpf_inode_storage.c new file +10
-0+ struct bpf_map *map, + bool cacheit_lockit) +{ +struct bpf_local_storage *inode_storage; +struct bpf_local_storage_map *smap; +struct bpf_storage_blob *bsb; + + bsb =bpf_inode (inode); +if (!bsb) +return NULL ;
lookup is fine since NULL is checked here.
Quoted 9 lines
kernel/bpf/bpf_inode_storage.c new file +9
-0+ + inode_storage =rcu_dereference (bsb->storage); +if (!inode_storage) +return NULL ; + + smap = (struct bpf_local_storage_map *)map; +return bpf_local_storage_lookup (inode_storage, smap, cacheit_lockit); +} +
[ ... ]
kernel/bpf/bpf_inode_storage.c line 131
Quoted 13 lines
kernel/bpf/bpf_inode_storage.c new file +13
-0+static int bpf_fd_inode_storage_update_elem (struct bpf_map *map,void *key, +void *value,u64 map_flags) +{ +struct bpf_local_storage_data *sdata; +struct file *f; +int fd; + + fd = *(int *)key; + f =fget_raw (fd); +if (!f) +return -EBADF ; + + sdata = bpf_local_storage_update(f->f_inode,
This will be an issue. bpf_local_storage_update() will not check NULL returned by inode_storage_ptr(). It should be checked here in the inode code path first before calling the bpf_local_storage_update() since this case is specific to inode local storage.
Same for the other bpf_local_storage_update() cases.
Quoted 6 lines
kernel/bpf/bpf_inode_storage.c new file +6
-0+ (struct bpf_local_storage_map *)map, + value, map_flags); +fput (f); +return PTR_ERR_OR_ZERO (sdata); +} +
[ ... ]
Quoted 32 lines
kernel/bpf/bpf_inode_storage.c new file +26
-0+BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode, + void *, value, u64, flags) +{ +struct bpf_local_storage_data *sdata; + +if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE )) +return (unsigned long )NULL ; + + sdata =inode_storage_lookup (inode, map, true); +if (sdata) +return (unsigned long )sdata->data; + +/* This helper must only called from where the inode is gurranteed +* to have a refcount and cannot be freed. +*/ +if (flags &BPF_LOCAL_STORAGE_GET_F_CREATE ) { + sdata =bpf_local_storage_update ( + inode, (struct bpf_local_storage_map *)map, value, +BPF_NOEXIST ); +return IS_ERR (sdata) ? (unsigned long )NULL : + (unsigned long )sdata->data; + } + +return (unsigned long )NULL ; +} +security/bpf/hooks.c +0
-0
security/bpf/hooks.c line 6
Quoted 4 lines
security/bpf/hooks.c +1
-0@@ -3,6 +3,7 @@/* * Copyright (C) 2020 Google LLC. */ +#include <linux/bpf_local_storage.h>
Is it needed?
Quoted 24 lines
security/bpf/hooks.c +6
-0#include <linux/lsm_hooks.h> #include <linux/bpf_lsm.h> @@ -11,6 +12,7 @@ static struct security_hook_list bpf_lsm_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(NAME, bpf_lsm_##NAME),#include <linux/lsm_hook_defs.h> #undef LSM_HOOK + LSM_HOOK_INIT(inode_free_security, bpf_inode_storage_free), };static int __init bpf_lsm_init (void )@@ -20,7 +22,12 @@ static int __init bpf_lsm_init(void)return 0 ; } +struct lsm_blob_sizes bpf_lsm_blob_sizes __lsm_ro_after_init = { + .lbs_inode =sizeof (struct bpf_storage_blob ), +}; +DEFINE_LSM (bpf) = { .name ="bpf" , .init = bpf_lsm_init, + .blobs = &bpf_lsm_blob_sizes };
Re: [PATCH bpf-next v9 5/7] bpf: Implement bpf_local_storage for inodes
Quoted 17 lines · Martin KaFai Lau
Quoted 9 lines · KP Singh
From: KP Singh <[email protected]>
Similar to
bpf_local_storagefor sockets, add local storage for inodes. The life-cycle of storage is managed with the life-cycle of the inode. i.e. the storage is destroyed along with the owning inode.The BPF LSM allocates an
__rcupointer to thebpf_local_storagein the security blob which are now stackable and can co-exist with other LSMs.[ ... ]
Quoted 5 lines
kernel/bpf/bpf_inode_storage.c new file +0
-0
[...]
Quoted 5 lines
Quoted 5 lines
kernel/bpf/bpf_inode_storage.c new file +5
-0+ +DEFINE_BPF_STORAGE_CACHE (inode_cache); + +static struct bpf_local_storage __rcu ** +inode_storage_ptr (void *owner)
Quoted 8 lines
kernel/bpf/bpf_inode_storage.c
Quoted 7 lines
kernel/bpf/bpf_inode_storage.c new file +7
-0+{ +struct inode *inode = owner; +struct bpf_storage_blob *bsb; + + bsb =bpf_inode (inode); +if (!bsb) +return NULL ;just noticed this one. NULL could be returned here. When will it happen?
This can happen if CONFIG_BPF_LSM is enabled but "bpf" is not in the list of active LSMs.
Quoted 8 lines
Quoted 8 lines
kernel/bpf/bpf_inode_storage.c new file +7
-0+return &bsb->storage; +} + +static struct bpf_local_storage_data *inode_storage_lookup (struct inode *inode, +struct bpf_map *map, +bool cacheit_lockit) +{
[...]
Quoted 4 lines
path first before calling the
bpf_local_storage_update()since this case is specific to inode local storage.Same for the other
bpf_local_storage_update()cases.
If you're okay with this I can send a new series with the following updates.
kernel/bpf/bpf_inode_storage.c +9 -1
@@ -125,7 +125,7 @@ static int bpf_fd_inode_storage_update_elem(struct bpf_map *map, void *key, fd = *(int *)key; f =fget_raw (fd); -if (!f) +if (!f || !inode_storage_ptr (f->f_inode))return -EBADF ; sdata = bpf_local_storage_update(f->f_inode,
@@ -171,6 +171,14 @@ BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE ))return (unsigned long )NULL ; +/* explicitly check that the inode_storage_ptr is not +* NULL as inode_storage_lookup returns NULL in this case and +* and bpf_local_storage_update expects the owner to have a +* valid storage pointer. +*/ +if (!inode_storage_ptr (inode)) +return (unsigned long )NULL ; + sdata =inode_storage_lookup (inode, map, true);if (sdata)return (unsigned long )sdata->data;
Quoted 8 lines
Quoted 7 lines
kernel/bpf/bpf_inode_storage.c new file +6
-0+ (struct bpf_local_storage_map *)map, + value, map_flags); +fput (f); +return PTR_ERR_OR_ZERO (sdata); +} +
[...]
Quoted 4 lines
Quoted 3 lines
kernel/bpf/bpf_inode_storage.c new file +3
-0+return (unsigned long )NULL ; +} +
Quoted 10 lines
security/bpf/hooks.c line 6
Quoted 9 lines
security/bpf/hooks.c +1
-0@@ -3,6 +3,7 @@/* * Copyright (C) 2020 Google LLC. */ +#include <linux/bpf_local_storage.h> Is it needed?
No. Removed. Thanks!
Quoted 6 lines
Quoted 6 lines
security/bpf/hooks.c +0
-0#include <linux/lsm_hooks.h> #include <linux/bpf_lsm.h> @@ -11,6 +12,7 @@ static struct security_hook_list bpf_lsm_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(NAME, bpf_lsm_##NAME),
[...]
Quoted 2 lines
Quoted 2 lines
security/bpf/hooks.c +1
-0+ .blobs = &bpf_lsm_blob_sizes };
Re: [PATCH bpf-next v9 5/7] bpf: Implement bpf_local_storage for inodes
Quoted 72 lines · KP Singh
Quoted 17 lines · Martin KaFai Lau
Quoted 9 lines · KP Singh
From: KP Singh <[email protected]>
Similar to
bpf_local_storagefor sockets, add local storage for inodes. The life-cycle of storage is managed with the life-cycle of the inode. i.e. the storage is destroyed along with the owning inode.The BPF LSM allocates an
__rcupointer to thebpf_local_storagein the security blob which are now stackable and can co-exist with other LSMs.[ ... ]
Quoted 5 lines
kernel/bpf/bpf_inode_storage.c new file +0
-0[...]
Quoted 5 lines
Quoted 5 lines
kernel/bpf/bpf_inode_storage.c new file +5
-0+ +DEFINE_BPF_STORAGE_CACHE (inode_cache); + +static struct bpf_local_storage __rcu ** +inode_storage_ptr (void *owner)Quoted 8 lines
kernel/bpf/bpf_inode_storage.c
Quoted 7 lines
kernel/bpf/bpf_inode_storage.c new file +7
-0+{ +struct inode *inode = owner; +struct bpf_storage_blob *bsb; + + bsb =bpf_inode (inode); +if (!bsb) +return NULL ;just noticed this one. NULL could be returned here. When will it happen?
This can happen if
CONFIG_BPF_LSMis enabled but "bpf" is not in the list of active LSMs.Quoted 8 lines
Quoted 8 lines
kernel/bpf/bpf_inode_storage.c new file +7
-0+return &bsb->storage; +} + +static struct bpf_local_storage_data *inode_storage_lookup (struct inode *inode, +struct bpf_map *map, +bool cacheit_lockit) +{[...]
Quoted 4 lines
path first before calling the
bpf_local_storage_update()since this case is specific to inode local storage.Same for the other
bpf_local_storage_update()cases.If you're okay with this I can send a new series with the following updates.
kernel/bpf/bpf_inode_storage.c +1
-1@@ -125,7 +125,7 @@ static int bpf_fd_inode_storage_update_elem(struct bpf_map *map, void *key, fd = *(int *)key; f =fget_raw (fd); -if (!f) +if (!f || !inode_storage_ptr (f->f_inode))return -EBADF ; sdata = bpf_local_storage_update(f->f_inode,
kernel/bpf/bpf_inode_storage.c BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode, · line 180
Quoted 10 lines
kernel/bpf/bpf_inode_storage.c +7
-0@@ -171,6 +171,14 @@ BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE ))return (unsigned long )NULL ; +/* explicitly check that the inode_storage_ptr is not +* NULL as inode_storage_lookup returns NULL in this case and +* and bpf_local_storage_update expects the owner to have a +* valid storage pointer. +*/ +if (!inode_storage_ptr (inode)) +return (unsigned long )NULL ;
LGTM
PATCH v9 6/7 bpf: Allow local storage to be used from LSM programs
In reply to KP SinghFrom: KP Singh <[email protected]>
Adds support for both bpf_{sk, inode}_storage_{get, delete} to be used in LSM programs. These helpers are not used for tracing programs (currently) as their usage is tied to the life-cycle of the object and should only be used where the owning object won't be freed (when the owning object is passed as an argument to the LSM hook). Thus, they are safer to use in LSM hooks than tracing. Usage of local storage in tracing programs will probably follow a per function based whitelist approach.
Since the UAPI helper signature for bpf_sk_storage expect a bpf_sock, it, leads to a compilation warning for LSM programs, it's also updated to accept a void * pointer instead.
--- include/net/bpf_sk_storage.h | 2 ++ include/uapi/linux/bpf.h | 7 +++++-- kernel/bpf/bpf_lsm.c | 21 ++++++++++++++++++++- net/core/bpf_sk_storage.c | 25 +++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 7 +++++-- 5 files changed, 57 insertions(+), 5 deletions(-)
include/net/bpf_sk_storage.h +2 -0
@@ -20,6 +20,8 @@ void bpf_sk_storage_free(struct sock *sk);extern const struct bpf_func_proto bpf_sk_storage_get_proto;extern const struct bpf_func_proto bpf_sk_storage_delete_proto; +extern const struct bpf_func_proto sk_storage_get_btf_proto; +extern const struct bpf_func_proto sk_storage_delete_btf_proto;struct bpf_local_storage_elem ;struct bpf_sk_storage_diag ;
include/uapi/linux/bpf.h +5 -2
@@ -2808,7 +2808,7 @@ union bpf_attr {* * **-ERANGE** if resulting value was out of range. * -* void *bpf_sk_storage_get(struct bpf_map *map, struct bpf_sock *sk, void *value, u64 flags) +* void *bpf_sk_storage_get(struct bpf_map *map, void *sk, void *value, u64 flags) * Description * Get a bpf-local-storage from a *sk*. *
@@ -2824,6 +2824,9 @@ union bpf_attr {* "type". The bpf-local-storage "type" (i.e. the *map*) is * searched against all bpf-local-storages residing at *sk*. * +* *sk* is a kernel **struct sock** pointer for LSM program. +* *sk* is a **struct bpf_sock** pointer for other program types. +* * An optional *flags* (**BPF_SK_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
@@ -2836,7 +2839,7 @@ union bpf_attr {* **NULL** if not found or there was an error in adding * a new bpf-local-storage. * -* long bpf_sk_storage_delete(struct bpf_map *map, struct bpf_sock *sk) +* long bpf_sk_storage_delete(struct bpf_map *map, void *sk) * Description * Delete a bpf-local-storage from a *sk*. * Return
kernel/bpf/bpf_lsm.c +20 -1
@@ -11,6 +11,8 @@#include <linux/bpf_lsm.h> #include <linux/kallsyms.h> #include <linux/bpf_verifier.h> +#include <net/bpf_sk_storage.h> +#include <linux/bpf_local_storage.h> /* For every LSM hook that allows attachment of BPF programs, declare a nop * function where a BPF program can be attached.
@@ -45,10 +47,27 @@ int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,return 0 ; } +static const struct bpf_func_proto * +bpf_lsm_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) +{ +switch (func_id) { +case BPF_FUNC_inode_storage_get: +return &bpf_inode_storage_get_proto; +case BPF_FUNC_inode_storage_delete: +return &bpf_inode_storage_delete_proto; +case BPF_FUNC_sk_storage_get: +return &sk_storage_get_btf_proto; +case BPF_FUNC_sk_storage_delete: +return &sk_storage_delete_btf_proto; +default : +return tracing_prog_func_proto (func_id, prog); + } +} +const struct bpf_prog_ops lsm_prog_ops = { };const struct bpf_verifier_ops lsm_verifier_ops = { - .get_func_proto = tracing_prog_func_proto, + .get_func_proto = bpf_lsm_func_proto, .is_valid_access = btf_ctx_access, };
net/core/bpf_sk_storage.c +25 -0
@@ -12,6 +12,7 @@#include <net/sock.h> #include <uapi/linux/sock_diag.h> #include <uapi/linux/btf.h> +#include <linux/btf_ids.h> DEFINE_BPF_STORAGE_CACHE (sk_cache);
@@ -377,6 +378,30 @@ const struct bpf_func_proto bpf_sk_storage_delete_proto = { .arg2_type = ARG_PTR_TO_SOCKET, }; +BTF_ID_LIST (sk_storage_btf_ids) +BTF_ID_UNUSED +BTF_ID(struct, sock) + +const struct bpf_func_proto sk_storage_get_btf_proto = { + .func = bpf_sk_storage_get, + .gpl_only = false, + .ret_type =RET_PTR_TO_MAP_VALUE_OR_NULL , + .arg1_type =ARG_CONST_MAP_PTR , + .arg2_type =ARG_PTR_TO_BTF_ID , + .arg3_type =ARG_PTR_TO_MAP_VALUE_OR_NULL , + .arg4_type =ARG_ANYTHING , + .btf_id = sk_storage_btf_ids, +}; + +const struct bpf_func_proto sk_storage_delete_btf_proto = { + .func = bpf_sk_storage_delete, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_CONST_MAP_PTR , + .arg2_type =ARG_PTR_TO_BTF_ID , + .btf_id = sk_storage_btf_ids, +}; +struct bpf_sk_storage_diag {u32 nr_maps;struct bpf_map *maps[];
tools/include/uapi/linux/bpf.h +5 -2
@@ -2808,7 +2808,7 @@ union bpf_attr {* * **-ERANGE** if resulting value was out of range. * -* void *bpf_sk_storage_get(struct bpf_map *map, struct bpf_sock *sk, void *value, u64 flags) +* void *bpf_sk_storage_get(struct bpf_map *map, void *sk, void *value, u64 flags) * Description * Get a bpf-local-storage from a *sk*. *
@@ -2824,6 +2824,9 @@ union bpf_attr {* "type". The bpf-local-storage "type" (i.e. the *map*) is * searched against all bpf-local-storages residing at *sk*. * +* *sk* is a kernel **struct sock** pointer for LSM program. +* *sk* is a **struct bpf_sock** pointer for other program types. +* * An optional *flags* (**BPF_SK_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
@@ -2836,7 +2839,7 @@ union bpf_attr {* **NULL** if not found or there was an error in adding * a new bpf-local-storage. * -* long bpf_sk_storage_delete(struct bpf_map *map, struct bpf_sock *sk) +* long bpf_sk_storage_delete(struct bpf_map *map, void *sk) * Description * Delete a bpf-local-storage from a *sk*. * Return
Signature · 1 line
2.28.0.297.g1956fa8f8d-goog
PATCH v9 7/7 bpf: Add selftests for local_storage
In reply to KP SinghFrom: KP Singh <[email protected]>
inode_local_storage:
* Hook to the file_open and inode_unlink LSM hooks.
* Create and unlink a temporary file.
* Store some information in the inode's bpf_local_storage during file_open.
* Verify that this information exists when the file is unlinked.
sk_local_storage:
* Hook to the socket_post_create and socket_bind LSM hooks.
* Open and bind a socket and set the sk_storage in the socket_post_create hook using the start_server helper.
* Verify if the information is set in the socket_bind hook.
--- .../bpf/prog_tests/test_local_storage.c | 60 ++++++++ .../selftests/bpf/progs/local_storage.c | 140 ++++++++++++++++++ 2 files changed, 200 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_local_storage.c create mode 100644 tools/testing/selftests/bpf/progs/local_storage.c
tools/testing/selftests/bpf/prog_tests/test_local_storage.c new file +60 -0
@@ -0,0 +1,60 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright (C) 2020 Google LLC. +*/ + +#include <test_progs.h> +#include <linux/limits.h> + +#include "local_storage.skel.h" +#include "network_helpers.h" + +int create_and_unlink_file (void ) +{ +char fname[PATH_MAX ] ="/tmp/fileXXXXXX" ; +int fd; + + fd =mkstemp (fname); +if (fd <0 ) +return fd; + +close (fd); +unlink (fname); +return 0 ; +} + +void test_test_local_storage (void ) +{ +struct local_storage *skel =NULL ; +int err, duration =0 , serv_sk =-1 ; + + skel =local_storage__open_and_load (); +if (CHECK (!skel,"skel_load" ,"lsm skeleton failed\n" )) + goto close_prog; + + err =local_storage__attach (skel); +if (CHECK (err,"attach" ,"lsm attach failed: %d\n" , err)) + goto close_prog; + + skel->bss->monitored_pid =getpid (); + + err =create_and_unlink_file (); +if (CHECK (err <0 ,"exec_cmd" ,"err %d errno %d\n" , err, errno)) + goto close_prog; + +CHECK (skel->data->inode_storage_result !=0 ,"inode_storage_result" , +"inode_local_storage not set\n" ); + + serv_sk =start_server (AF_INET6 ,SOCK_STREAM ,NULL ,0 ,0 ); +if (CHECK (serv_sk <0 ,"start_server" ,"failed to start server\n" )) + goto close_prog; + +CHECK (skel->data->sk_storage_result !=0 ,"sk_storage_result" , +"sk_local_storage not set\n" ); + +close (serv_sk); + +close_prog: +local_storage__destroy (skel); +}
tools/testing/selftests/bpf/progs/local_storage.c new file +140 -0
140 lines not on this page · raw message
Signature · 1 line
2.28.0.297.g1956fa8f8d-goog