Skip to contentSkip to conversation

Generalizing bpf_local_storage

PATCH Superseded KP Singh 4 participants 15 messages Jun 17, 2020 – Jul 6, 2020

Subsystems (11)

v2 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

KP Singh

author
#0 lore raw

v2 cover Generalizing bpf_local_storage

From: KP Singh <[email protected]>

# 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 (4):
bpf: Generalize 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

 .../bpf_local_storage.h}                      |  15 +-
 include/linux/bpf_lsm.h                       |  21 +
 include/linux/bpf_types.h                     |   1 +
 include/net/sock.h                            |   4 +-
 include/uapi/linux/bpf.h                      |  56 +-
 kernel/bpf/Makefile                           |   1 +
 .../bpf/bpf_local_storage.c                   | 933 ++++++++++++------
 kernel/bpf/bpf_lsm.c                          |  20 +-
 kernel/bpf/cgroup.c                           |   2 +-
 kernel/bpf/syscall.c                          |   3 +-
 kernel/bpf/verifier.c                         |  10 +
 net/bpf/test_run.c                            |   2 +-
 net/core/Makefile                             |   1 -
 net/core/filter.c                             |   2 +-
 net/core/sock.c                               |   2 +-
 net/ipv4/bpf_tcp_ca.c                         |   2 +-
 net/ipv4/inet_diag.c                          |   2 +-
 security/bpf/hooks.c                          |   7 +
 tools/bpf/bpftool/map.c                       |   1 +
 tools/include/uapi/linux/bpf.h                |  56 +-
 tools/lib/bpf/libbpf_probes.c                 |   5 +-
 .../bpf/prog_tests/test_local_storage.c       |  60 ++
 .../selftests/bpf/progs/local_storage.c       | 137 +++
 23 files changed, 1038 insertions(+), 305 deletions(-)
 rename include/{net/bpf_sk_storage.h => linux/bpf_local_storage.h} (72%)
 rename net/core/bpf_sk_storage.c => kernel/bpf/bpf_local_storage.c (50%)
 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.27.0.111.gc72c7da667-goog
author
#1 lore raw

PATCH v2 1/4 bpf: Generalize bpf_sk_storage

From: 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.

bpf_sk_storage is updated to be bpf_local_storage with a union that contains a pointer to the owner object. The type of the bpf_local_storage can be determined using the newly added bpf_local_storage_type enum.

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.

---
 .../bpf_local_storage.h}                      |   6 +-
 include/net/sock.h                            |   4 +-
 include/uapi/linux/bpf.h                      |  12 +-
 kernel/bpf/Makefile                           |   1 +
 .../bpf/bpf_local_storage.c                   | 652 ++++++++++--------
 kernel/bpf/cgroup.c                           |   2 +-
 net/bpf/test_run.c                            |   2 +-
 net/core/Makefile                             |   1 -
 net/core/filter.c                             |   2 +-
 net/core/sock.c                               |   2 +-
 net/ipv4/bpf_tcp_ca.c                         |   2 +-
 net/ipv4/inet_diag.c                          |   2 +-
 tools/include/uapi/linux/bpf.h                |  12 +-
 13 files changed, 405 insertions(+), 295 deletions(-)
 rename include/{net/bpf_sk_storage.h => linux/bpf_local_storage.h} (93%)
 rename net/core/bpf_sk_storage.c => kernel/bpf/bpf_local_storage.c (60%)
include/linux/bpf_local_storage.h renamed from include/net/bpf_sk_storage.h +3 -3
similarity index 93%
rename from include/net/bpf_sk_storage.h
rename to include/linux/bpf_local_storage.h
index 5036c94c0503..85524f18cd91 100644
@@ -1,7 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 /* Copyright (c) 2019 Facebook */
-#ifndef _BPF_SK_STORAGE_H
-#define _BPF_SK_STORAGE_H
+#ifndef _BPF_LOCAL_STORAGE_H
+#define _BPF_LOCAL_STORAGE_H
 
 struct sock;
 
@@ -47,4 +47,4 @@ static inline int bpf_sk_storage_diag_put(struct bpf_sk_storage_diag *diag,
 }
 #endif
 
-#endif /* _BPF_SK_STORAGE_H */
+#endif /* _BPF_LOCAL_STORAGE_H */
include/net/sock.h +2 -2
index c53cc42b5ab9..105e08a3e994 100644
@@ -245,7 +245,7 @@ struct sock_common {
 	/* public: */
 };
 
-struct bpf_sk_storage;
+struct bpf_local_storage;
 
 /**
   *	struct sock - network layer representation of sockets
@@ -516,7 +516,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;
 };
include/uapi/linux/bpf.h +8 -4
index 19684813faae..6218677daaa2 100644
@@ -2798,10 +2798,10 @@ union bpf_attr {
  *		"type". The bpf-local-storage "type" (i.e. the *map*) is
  *		searched against all bpf-local-storages residing at *sk*.
  *
- *		An optional *flags* (**BPF_SK_STORAGE_GET_F_CREATE**) can be
+ *		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_SK_STORAGE_GET_F_CREATE** to specify
+ *		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
@@ -3498,9 +3498,13 @@ enum {
 	BPF_F_SYSCTL_BASE_NAME		= (1ULL << 0),
 };
 
-/* BPF_FUNC_sk_storage_get flags */
+/* BPF_FUNC_<local>_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. */
kernel/bpf/Makefile +1 -0
index 1131a921e1a6..0acb8f8a6042 100644
@@ -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 renamed from net/core/bpf_sk_storage.c +377 -275

1,120 lines not on this page · raw message

kernel/bpf/cgroup.c +1 -1
index 4d76f16524cc..0c54fa7d2e51 100644
@@ -14,8 +14,8 @@
 #include <linux/string.h>
 #include <linux/bpf.h>
 #include <linux/bpf-cgroup.h>
+#include <linux/bpf_local_storage.h>
 #include <net/sock.h>
-#include <net/bpf_sk_storage.h>
 
 #include "../cgroup/cgroup-internal.h"
 
net/bpf/test_run.c +1 -1
index bfd4ccd80847..b1547efcc842 100644
@@ -7,7 +7,7 @@
 #include <linux/etherdevice.h>
 #include <linux/filter.h>
 #include <linux/sched/signal.h>
-#include <net/bpf_sk_storage.h>
+#include <linux/bpf_local_storage.h>
 #include <net/sock.h>
 #include <net/tcp.h>
 #include <linux/error-injection.h>
net/core/Makefile +0 -1
index 3e2c378e5f31..9ddc2f3ecd97 100644
@@ -34,4 +34,3 @@ obj-$(CONFIG_HWBM) += hwbm.o
 obj-$(CONFIG_NET_DEVLINK) += devlink.o
 obj-$(CONFIG_GRO_CELLS) += gro_cells.o
 obj-$(CONFIG_FAILOVER) += failover.o
-obj-$(CONFIG_BPF_SYSCALL) += bpf_sk_storage.o
net/core/filter.c +1 -1
index 73395384afe2..f60b0ba8b529 100644
@@ -72,7 +72,7 @@
 #include <net/seg6_local.h>
 #include <net/lwtunnel.h>
 #include <net/ipv6_stubs.h>
-#include <net/bpf_sk_storage.h>
+#include <linux/bpf_local_storage.h>
 
 /**
  *	sk_filter_trim_cap - run a packet through a socket filter
net/core/sock.c +1 -1
index 6c4acf1f0220..68693484ff77 100644
@@ -130,8 +130,8 @@
 #include <linux/sock_diag.h>
 
 #include <linux/filter.h>
+#include <linux/bpf_local_storage.h>
 #include <net/sock_reuseport.h>
-#include <net/bpf_sk_storage.h>
 
 #include <trace/events/sock.h>
 
net/ipv4/bpf_tcp_ca.c +1 -1
index e3939f76b024..075abf1c6d7e 100644
@@ -7,7 +7,7 @@
 #include <linux/btf.h>
 #include <linux/filter.h>
 #include <net/tcp.h>
-#include <net/bpf_sk_storage.h>
+#include <linux/bpf_local_storage.h>
 
 static u32 optional_ops[] = {
 	offsetof(struct tcp_congestion_ops, init),
net/ipv4/inet_diag.c +1 -1
index 125f4f8a36b4..ce22867cce5d 100644
@@ -23,7 +23,7 @@
 #include <net/inet_hashtables.h>
 #include <net/inet_timewait_sock.h>
 #include <net/inet6_hashtables.h>
-#include <net/bpf_sk_storage.h>
+#include <linux/bpf_local_storage.h>
 #include <net/netlink.h>
 
 #include <linux/inet.h>
tools/include/uapi/linux/bpf.h +8 -4
index 19684813faae..6218677daaa2 100644
@@ -2798,10 +2798,10 @@ union bpf_attr {
  *		"type". The bpf-local-storage "type" (i.e. the *map*) is
  *		searched against all bpf-local-storages residing at *sk*.
  *
- *		An optional *flags* (**BPF_SK_STORAGE_GET_F_CREATE**) can be
+ *		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_SK_STORAGE_GET_F_CREATE** to specify
+ *		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
@@ -3498,9 +3498,13 @@ enum {
 	BPF_F_SYSCTL_BASE_NAME		= (1ULL << 0),
 };
 
-/* BPF_FUNC_sk_storage_get flags */
+/* BPF_FUNC_<local>_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.27.0.111.gc72c7da667-goog
#2 lore raw

Re: [PATCH bpf-next v2 1/4] bpf: Generalize bpf_sk_storage

Quoted 6 lines · KP Singh

From: 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.

Quoted 8 lines

bpf_sk_storage is updated to be bpf_local_storage with a union that contains a pointer to the owner object. The type of the bpf_local_storage can be determined using the newly added bpf_local_storage_type enum.

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.

Thanks for taking up this effort to refactor sk_local_storage.

I took a quick look. I have some comments and would like to explore some thoughts.

kernel/bpf/bpf_local_storage.c line 5

Quoted 8 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +3 -0
@@ -1,19 +1,22 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2019 Facebook  */
+#include "linux/bpf.h"
+#include "asm-generic/bug.h"
+#include "linux/err.h"

"<" ">"

kernel/bpf/bpf_local_storage.c line 17

Quoted 13 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +1 -1
 #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 <net/bpf_sk_storage.h>
+#include <linux/bpf_local_storage.h>
 #include <net/sock.h>
 #include <uapi/linux/sock_diag.h>
 #include <uapi/linux/btf.h>
 
 static atomic_t cache_idx;

inode local storage and sk local storage probably need a separate cache_idx. An improvement on picking cache_idx has just been landed also.

[ ... ]

Quoted 13 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +10 -0
+struct bpf_local_storage {
+	struct bpf_local_storage_data __rcu *cache[BPF_STORAGE_CACHE_SIZE];
+	struct hlist_head list;		/* List of bpf_local_storage_elem */
+	/* The object that owns the the above "list" of
+	 * bpf_local_storage_elem
+	 */
+	union {
+		struct sock *sk;
+	};
 	struct rcu_head rcu;
 	raw_spinlock_t lock;	/* Protect adding/removing from the "list" */
+	enum bpf_local_storage_type stype;
 };

[ ... ]

Quoted 12 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +12 -0
+static struct bpf_local_storage_elem *sk_selem_alloc(
+	struct bpf_local_storage_map *smap, struct sock *sk, void *value,
+	bool charge_omem)
+{
+	struct bpf_local_storage_elem *selem;
+
+	if (charge_omem && omem_charge(sk, smap->elem_size))
+		return NULL;
+
+	selem = selem_alloc(smap, value);
+	if (selem)
+		return selem;

kernel/bpf/bpf_local_storage.c static struct bpf_sk_storage_elem *selem_alloc(struct bpf_sk_storage_map *smap, · line 169

Quoted 10 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +3 -1
+
 	if (charge_omem)
 		atomic_sub(smap->elem_size, &sk->sk_omem_alloc);
 
 	return NULL;
 }
 
-/* sk_storage->lock must be held and selem->sk_storage == sk_storage.
+static void __unlink_local_storage(struct bpf_local_storage *local_storage,
+				   bool uncharge_omem)

Nit. indent is off. There are a few more cases like this.

kernel/bpf/bpf_local_storage.c static struct bpf_sk_storage_elem *selem_alloc(struct bpf_sk_storage_map *smap, · line 173

Quoted 4 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +4 -0
+{
+	struct sock *sk;
+
+	switch (local_storage->stype) {

Does it need a new bpf_local_storage_type? Is map_type as good?

Instead of adding any new member (e.g. stype) to "struct bpf_local_storage", can the smap pointer be directly used here instead?

For example in __unlink_local_storage() here, it should have a hold to the selem which then has a hold to smap.

kernel/bpf/bpf_local_storage.c static struct bpf_sk_storage_elem *selem_alloc(struct bpf_sk_storage_map *smap, · line 184

Quoted 11 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +11 -0
+	case BPF_LOCAL_STORAGE_SK:
+		sk = local_storage->sk;
+		if (uncharge_omem)
+			atomic_sub(sizeof(struct bpf_local_storage),
+				   &sk->sk_omem_alloc);
+
+		/* After this RCU_INIT, sk may be freed and cannot be used */
+		RCU_INIT_POINTER(sk->sk_bpf_storage, NULL);
+		local_storage->sk = NULL;
+		break;
+	}

Another thought on the stype switch cases.

Instead of having multiple switches on stype in bpf_local_storage.c which may not be scalable soon if we are planning to support a few more kernel objects, have you considered putting them into its own "ops". May be a few new ops can be added to bpf_map_ops to do local storage unlink/update/alloc...etc.

Quoted 10 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +7 -0
+}
+
+/* local_storage->lock must be held and selem->local_storage == local_storage.
  * The caller must ensure selem->smap is still valid to be
  * dereferenced for its smap->elem_size and smap->cache_idx.
+ *
+ * uncharge_omem is only relevant when:
+ *
+ *	local_storage->stype == BPF_LOCAL_STORAGE_SK
  */

[ ... ]

Quoted 58 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +11 -11
@@ -845,7 +947,7 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk)
 BPF_CALL_4(bpf_sk_storage_get, struct bpf_map *, map, struct sock *, sk,
 	   void *, value, u64, flags)
 {
-	struct bpf_sk_storage_data *sdata;
+	struct bpf_local_storage_data *sdata;
 
 	if (flags > BPF_SK_STORAGE_GET_F_CREATE)
 		return (unsigned long)NULL;
@@ -854,14 +956,14 @@ BPF_CALL_4(bpf_sk_storage_get, struct bpf_map *, map, struct sock *, sk,
 	if (sdata)
 		return (unsigned long)sdata->data;
 
-	if (flags == BPF_SK_STORAGE_GET_F_CREATE &&
+	if (flags == BPF_LOCAL_STORAGE_GET_F_CREATE &&
 	    /* Cannot add new elem to a going away sk.
 	     * Otherwise, the new elem may become a leak
 	     * (and also other memory issues during map
 	     *  destruction).
 	     */
 	    refcount_inc_not_zero(&sk->sk_refcnt)) {
-		sdata = sk_storage_update(sk, map, value, BPF_NOEXIST);
+		sdata = local_storage_update(sk, map, value, BPF_NOEXIST);
 		/* sk must be a fullsock (guaranteed by verifier),
 		 * so sock_gen_put() is unnecessary.
 		 */
@@ -887,14 +989,14 @@ BPF_CALL_2(bpf_sk_storage_delete, struct bpf_map *, map, struct sock *, sk)
 }
 
 const struct bpf_map_ops sk_storage_map_ops = {
-	.map_alloc_check = bpf_sk_storage_map_alloc_check,
-	.map_alloc = bpf_sk_storage_map_alloc,
-	.map_free = bpf_sk_storage_map_free,
+	.map_alloc_check = bpf_local_storage_map_alloc_check,
+	.map_alloc = bpf_local_storage_map_alloc,
+	.map_free = bpf_local_storage_map_free,
 	.map_get_next_key = notsupp_get_next_key,
-	.map_lookup_elem = bpf_fd_sk_storage_lookup_elem,
-	.map_update_elem = bpf_fd_sk_storage_update_elem,
-	.map_delete_elem = bpf_fd_sk_storage_delete_elem,
-	.map_check_btf = bpf_sk_storage_map_check_btf,
+	.map_lookup_elem = bpf_sk_storage_lookup_elem,
+	.map_update_elem = bpf_sk_storage_update_elem,
+	.map_delete_elem = bpf_sk_storage_delete_elem,
+	.map_check_btf = bpf_local_storage_map_check_btf,
 };
 
 const struct bpf_func_proto bpf_sk_storage_get_proto = {
@@ -975,7 +1077,7 @@ bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs)
 	u32 nr_maps = 0;
 	int rem, err;
 
-	/* bpf_sk_storage_map is currently limited to CAP_SYS_ADMIN as
+	/* bpf_local_storage_map is currently limited to CAP_SYS_ADMIN as
 	 * the map_alloc_check() side also does.
 	 */
 	if (!bpf_capable())

kernel/bpf/bpf_local_storage.c bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs) · line 1128

Quoted 2 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +0 -0
@@ -1025,10 +1127,10 @@ bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs)
 }
 EXPORT_SYMBOL_GPL(bpf_sk_storage_diag_alloc);

Would it be cleaner to leave bpf_sk specific function, map_ops, and func_proto in net/core/bpf_sk_storage.c?

There is a test in map_tests/sk_storage_map.c, in case you may not notice.

author
#3 lore raw

Re: [PATCH bpf-next v2 1/4] bpf: Generalize bpf_sk_storage

Thanks for your feedback! Apologies it took some time for me to incorporate this into another revision.

Quoted 42 lines · Martin KaFai Lau
Quoted 6 lines · KP Singh

From: 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.

Quoted 8 lines

bpf_sk_storage is updated to be bpf_local_storage with a union that contains a pointer to the owner object. The type of the bpf_local_storage can be determined using the newly added bpf_local_storage_type enum.

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.

Thanks for taking up this effort to refactor sk_local_storage.

I took a quick look. I have some comments and would like to explore some thoughts.

kernel/bpf/bpf_local_storage.c line 5

Quoted 8 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +3 -0
@@ -1,19 +1,22 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2019 Facebook  */
+#include "linux/bpf.h"
+#include "asm-generic/bug.h"
+#include "linux/err.h"

"<" ">"

Quoted 12 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +1 -1
 #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 <net/bpf_sk_storage.h>
+#include <linux/bpf_local_storage.h>
 #include <net/sock.h>
 #include <uapi/linux/sock_diag.h>
 #include <uapi/linux/btf.h>
 
Quoted 4 lines

kernel/bpf/bpf_local_storage.c line 17

Quoted 1 line
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +0 -0
 static atomic_t cache_idx;

inode local storage and sk local storage probably need a separate cache_idx. An improvement on picking cache_idx has just been landed also.

I see, thanks! I rebased and I now see that cache_idx is now a:

  static u64 cache_idx_usage_counts[BPF_STORAGE_CACHE_SIZE];

which tracks the free cache slots rather than using a single atomic cache_idx. I guess all types of local storage can share this now right?

Quoted 6 lines

[ ... ]

Quoted 3 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +2 -0
+struct bpf_local_storage {
+	struct bpf_local_storage_data __rcu *cache[BPF_STORAGE_CACHE_SIZE];
 	return NULL;

[...]

Quoted 6 lines

kernel/bpf/bpf_local_storage.c static struct bpf_sk_storage_elem *selem_alloc(struct bpf_sk_storage_map *smap, · line 169

Quoted 5 lines

}

kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +2 -1
-/* sk_storage->lock must be held and selem->sk_storage == sk_storage.
+static void __unlink_local_storage(struct bpf_local_storage *local_storage,
+				   bool uncharge_omem)

Nit. indent is off. There are a few more cases like this.

Thanks, will fix this. (note to self: don't trust the editor's clang-format blindly).

Quoted 7 lines

kernel/bpf/bpf_local_storage.c static struct bpf_sk_storage_elem *selem_alloc(struct bpf_sk_storage_map *smap, · line 173

Quoted 5 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +4 -0
+{
+	struct sock *sk;
+
+	switch (local_storage->stype) {

Does it need a new bpf_local_storage_type? Is map_type as good?

Quoted 6 lines

Instead of adding any new member (e.g. stype) to "struct bpf_local_storage", can the smap pointer be directly used here instead?

For example in __unlink_local_storage() here, it should have a hold to the selem which then has a hold to smap.

Good point, Updated to using the map->map_type.

Quoted 14 lines

kernel/bpf/bpf_local_storage.c static struct bpf_sk_storage_elem *selem_alloc(struct bpf_sk_storage_map *smap, · line 184

Quoted 12 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +11 -0
+	case BPF_LOCAL_STORAGE_SK:
+		sk = local_storage->sk;
+		if (uncharge_omem)
+			atomic_sub(sizeof(struct bpf_local_storage),
+				   &sk->sk_omem_alloc);
+
+		/* After this RCU_INIT, sk may be freed and cannot be used */
+		RCU_INIT_POINTER(sk->sk_bpf_storage, NULL);
+		local_storage->sk = NULL;
+		break;
+	}

Another thought on the stype switch cases.

Quoted 4 lines

Instead of having multiple switches on stype in bpf_local_storage.c which may not be scalable soon if we are planning to support a few more kernel objects, have you considered putting them into its own "ops". May be a few new ops can be added to bpf_map_ops to do local storage unlink/update/alloc...etc.

Good idea, I was able to refactor this with the following ops:

        /* Functions called by bpf_local_storage maps */
	void (*map_local_storage_unlink)(struct bpf_local_storage *local_storage,
                                         bool uncharge_omem);
	struct bpf_local_storage_elem *(*map_selem_alloc)(
		struct bpf_local_storage_map *smap, void *owner, void *value,
		bool charge_omem);
	struct bpf_local_storage_data *(*map_local_storage_update)(
		void  *owner, struct bpf_map *map, void *value, u64 flags);
	int (*map_local_storage_alloc)(void *owner,
				       struct bpf_local_storage_map *smap,
				       struct bpf_local_storage_elem *elem);

Let me know if you have any particular thoughts/suggestions about this.

Quoted 8 lines
Quoted 8 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +5 -0
+}
+
+/* local_storage->lock must be held and selem->local_storage == local_storage.
  * The caller must ensure selem->smap is still valid to be
  * dereferenced for its smap->elem_size and smap->cache_idx.
+ *
+ * uncharge_omem is only relevant when:

[...]

Quoted 9 lines

kernel/bpf/bpf_local_storage.c bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs) · line 1128

Quoted 7 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +1 -0
+	/* bpf_local_storage_map is currently limited to CAP_SYS_ADMIN as
 	 * the map_alloc_check() side also does.
 	 */
 	if (!bpf_capable())
@@ -1025,10 +1127,10 @@ bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs)
 }
 EXPORT_SYMBOL_GPL(bpf_sk_storage_diag_alloc);

Would it be cleaner to leave bpf_sk specific function, map_ops, and func_proto in net/core/bpf_sk_storage.c?

Sure, I can also keep the sk_clone code their as well for now.

Quoted 2 lines

There is a test in map_tests/sk_storage_map.c, in case you may not notice.

I will try to make it generic as a part of this series. If it takes too much time, I will send a separate patch for testing inode_storage_map and till then we have some assurance with test_local_storage in test_progs.

- KP

#4 lore raw

Re: [PATCH bpf-next v2 1/4] bpf: Generalize bpf_sk_storage

Quoted 51 lines · KP Singh

Thanks for your feedback! Apologies it took some time for me to incorporate this into another revision.

Quoted 42 lines · Martin KaFai Lau
Quoted 6 lines · KP Singh

From: 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.

Quoted 8 lines

bpf_sk_storage is updated to be bpf_local_storage with a union that contains a pointer to the owner object. The type of the bpf_local_storage can be determined using the newly added bpf_local_storage_type enum.

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.

Thanks for taking up this effort to refactor sk_local_storage.

I took a quick look. I have some comments and would like to explore some thoughts.

kernel/bpf/bpf_local_storage.c line 5

Quoted 8 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +3 -0
@@ -1,19 +1,22 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2019 Facebook  */
+#include "linux/bpf.h"
+#include "asm-generic/bug.h"
+#include "linux/err.h"

"<" ">"

Quoted 12 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +1 -1
 #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 <net/bpf_sk_storage.h>
+#include <linux/bpf_local_storage.h>
 #include <net/sock.h>
 #include <uapi/linux/sock_diag.h>
 #include <uapi/linux/btf.h>
 
Quoted 4 lines

kernel/bpf/bpf_local_storage.c line 17

Quoted 1 line
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +0 -0
 static atomic_t cache_idx;

inode local storage and sk local storage probably need a separate cache_idx. An improvement on picking cache_idx has just been landed also.

Quoted 7 lines

I see, thanks! I rebased and I now see that cache_idx is now a:

  static u64 cache_idx_usage_counts[BPF_STORAGE_CACHE_SIZE];

which tracks the free cache slots rather than using a single atomic cache_idx. I guess all types of local storage can share this now right?

I believe they have to be separated. A sk-storage will not be cached/stored in inode. 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.

Quoted 69 lines
Quoted 7 lines

[ ... ]

Quoted 3 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +2 -0
+struct bpf_local_storage {
+	struct bpf_local_storage_data __rcu *cache[BPF_STORAGE_CACHE_SIZE];
 	return NULL;

[...]

Quoted 6 lines

kernel/bpf/bpf_local_storage.c static struct bpf_sk_storage_elem *selem_alloc(struct bpf_sk_storage_map *smap, · line 169

Quoted 5 lines

}

kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +2 -1
-/* sk_storage->lock must be held and selem->sk_storage == sk_storage.
+static void __unlink_local_storage(struct bpf_local_storage *local_storage,
+				   bool uncharge_omem)

Nit. indent is off. There are a few more cases like this.

Thanks, will fix this. (note to self: don't trust the editor's clang-format blindly).

Quoted 7 lines

kernel/bpf/bpf_local_storage.c static struct bpf_sk_storage_elem *selem_alloc(struct bpf_sk_storage_map *smap, · line 173

Quoted 5 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +4 -0
+{
+	struct sock *sk;
+
+	switch (local_storage->stype) {

Does it need a new bpf_local_storage_type? Is map_type as good?

Quoted 6 lines

Instead of adding any new member (e.g. stype) to "struct bpf_local_storage", can the smap pointer be directly used here instead?

For example in __unlink_local_storage() here, it should have a hold to the selem which then has a hold to smap.

Good point, Updated to using the map->map_type.

Quoted 14 lines

kernel/bpf/bpf_local_storage.c static struct bpf_sk_storage_elem *selem_alloc(struct bpf_sk_storage_map *smap, · line 184

Quoted 12 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +11 -0
+	case BPF_LOCAL_STORAGE_SK:
+		sk = local_storage->sk;
+		if (uncharge_omem)
+			atomic_sub(sizeof(struct bpf_local_storage),
+				   &sk->sk_omem_alloc);
+
+		/* After this RCU_INIT, sk may be freed and cannot be used */
+		RCU_INIT_POINTER(sk->sk_bpf_storage, NULL);
+		local_storage->sk = NULL;
+		break;
+	}

Another thought on the stype switch cases.

Quoted 4 lines

Instead of having multiple switches on stype in bpf_local_storage.c which may not be scalable soon if we are planning to support a few more kernel objects, have you considered putting them into its own "ops". May be a few new ops can be added to bpf_map_ops to do local storage unlink/update/alloc...etc.

Good idea, I was able to refactor this with the following ops:

        /* Functions called by bpf_local_storage maps */
	void (*map_local_storage_unlink)(struct bpf_local_storage *local_storage,
                                         bool uncharge_omem);
	struct bpf_local_storage_elem *(*map_selem_alloc)(
		struct bpf_local_storage_map *smap, void *owner, void *value,
		bool charge_omem);
	struct bpf_local_storage_data *(*map_local_storage_update)(
		void  *owner, struct bpf_map *map, void *value, u64 flags);
	int (*map_local_storage_alloc)(void *owner,
				       struct bpf_local_storage_map *smap,
				       struct bpf_local_storage_elem *elem);
Quoted 2 lines

Let me know if you have any particular thoughts/suggestions about this.

Make sense to me.

It seems the fast-path's lookup can all be done within __local_storage_lookup(). No indirect call is needed, so should be good.

Quoted 22 lines
Quoted 9 lines
Quoted 9 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +5 -0
+}
+
+/* local_storage->lock must be held and selem->local_storage == local_storage.
  * The caller must ensure selem->smap is still valid to be
  * dereferenced for its smap->elem_size and smap->cache_idx.
+ *
+ * uncharge_omem is only relevant when:

[...]

Quoted 9 lines

kernel/bpf/bpf_local_storage.c bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs) · line 1128

Quoted 7 lines
kernel/bpf/bpf_local_storage.c renamed from net/core/bpf_sk_storage.c +1 -0
+	/* bpf_local_storage_map is currently limited to CAP_SYS_ADMIN as
 	 * the map_alloc_check() side also does.
 	 */
 	if (!bpf_capable())
@@ -1025,10 +1127,10 @@ bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs)
 }
 EXPORT_SYMBOL_GPL(bpf_sk_storage_diag_alloc);

Would it be cleaner to leave bpf_sk specific function, map_ops, and func_proto in net/core/bpf_sk_storage.c?

Quoted 1 line

Sure, I can also keep the sk_clone code their as well for now.

Just came to my mind. For easier review purpose, may be first do the refactoring/renaming within bpf_sk_storage.c first and then create another patch to move the common parts to a new file bpf_local_storage.c.

Not sure if it will be indeed easier to read the diff in practice. I probably should be able to follow it either way.

Quoted 8 lines
Quoted 3 lines

There is a test in map_tests/sk_storage_map.c, in case you may not notice.

I will try to make it generic as a part of this series. If it takes too much time, I will send a separate patch for testing inode_storage_map and till then we have some assurance with test_local_storage in test_progs.

Sure. no problem. It is mostly for you to test sk_storage to ensure things ;) Also give some ideas on what racing conditions have
been considered in the sk_storage test and may be the inode storage test want to stress similar code path.

author
#5 lore raw

Re: [PATCH bpf-next v2 1/4] bpf: Generalize bpf_sk_storage

Quoted 3 lines · Martin KaFai Lau
Quoted 1 line · KP Singh
Quoted 1 line
Quoted 1 line

[...]

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

static atomic_t cache_idx;

inode local storage and sk local storage probably need a separate cache_idx. An improvement on picking cache_idx has just been landed also.

I see, thanks! I rebased and I now see that cache_idx is now a:

Quoted 8 lines
Quoted 5 lines
  static u64 cache_idx_usage_counts[BPF_STORAGE_CACHE_SIZE];

which tracks the free cache slots rather than using a single atomic cache_idx. I guess all types of local storage can share this now right?

I believe they have to be separated. A sk-storage will not be cached/stored in inode. 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.

Ah yes, I see.

I came up with some macros to solve this. Let me know what you think:
(this is on top of the refactoring I did, so some function names may seem new, but it should, hopefully, convey the general idea).

include/linux/bpf_local_storage.h +23 -2
index 3067774cc640..1dc2e6d72091 100644
@@ -79,6 +79,26 @@ struct bpf_local_storage_elem {
 #define SDATA(_SELEM) (&(_SELEM)->sdata)
 #define BPF_STORAGE_CACHE_SIZE	16
 
+u16 bpf_ls_cache_idx_get(spinlock_t *cache_idx_lock,
+			   u64 *cache_idx_usage_count);
+
+void bpf_ls_cache_idx_free(spinlock_t *cache_idx_lock,
+			   u64 *cache_idx_usage_counts, u16 idx);
+
+#define DEFINE_BPF_STORAGE_CACHE(type)					\
+static DEFINE_SPINLOCK(cache_idx_lock_##type);				\
+static u64 cache_idx_usage_counts_##type[BPF_STORAGE_CACHE_SIZE];	\
+static u16 cache_idx_get_##type(void)					\
+{									\
+	return bpf_ls_cache_idx_get(&cache_idx_lock_##type,		\
+				    cache_idx_usage_counts_##type);	\
+}									\
+static void cache_idx_free_##type(u16 idx)				\
+{									\
+	return bpf_ls_cache_idx_free(&cache_idx_lock_##type,		\
+				     cache_idx_usage_counts_##type,	\
+				     idx);				\
+}
 
 /* U16_MAX is much more than enough for sk local storage
  * considering a tcp_sock is ~2k.
@@ -105,13 +125,14 @@ struct bpf_local_storage {
 
 /* Helper functions for bpf_local_storage */
 int bpf_local_storage_map_alloc_check(union bpf_attr *attr);
-struct bpf_map *bpf_local_storage_map_alloc(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_map *map);
+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,
kernel/bpf/bpf_local_storage.c +37 -22

126 lines not on this page · raw message

net/core/bpf_sk_storage.c +25 -2
index 0ec44e819bfe..add0340e9ad3 100644
@@ -396,11 +396,34 @@ static int notsupp_get_next_key(struct bpf_map *map, void *key,
 	return -ENOTSUPP;
 }
 
+DEFINE_BPF_STORAGE_CACHE(sk);
+
+struct bpf_map *sk_storage_map_alloc(union bpf_attr *attr)
+{
+	struct bpf_local_storage_map *smap;
+
+	smap = bpf_local_storage_map_alloc(attr);
+	if (IS_ERR(smap))
+		return ERR_CAST(smap);
+
+	smap->cache_idx = cache_idx_get_sk();
+	return &smap->map;
+}
+
+void sk_storage_map_free(struct bpf_map *map)
+{
+	struct bpf_local_storage_map *smap;
+
+	smap = (struct bpf_local_storage_map *)map;
+	cache_idx_free_sk(smap->cache_idx);
+	bpf_local_storage_map_free(smap);
+}
+
 static int sk_storage_map_btf_id;
 const struct bpf_map_ops sk_storage_map_ops = {
 	.map_alloc_check = bpf_local_storage_map_alloc_check,
-	.map_alloc = bpf_local_storage_map_alloc,
-	.map_free = bpf_local_storage_map_free,
+	.map_alloc = sk_storage_map_alloc,
+	.map_free = sk_storage_map_free,
 	.map_get_next_key = notsupp_get_next_key,
 	.map_lookup_elem = bpf_sk_storage_lookup_elem,
 	.map_update_elem = bpf_sk_storage_update_elem,
Quoted 1 line

[...]

Quoted 9 lines
Quoted 2 lines

Sure, I can also keep the sk_clone code their as well for now.

Just came to my mind. For easier review purpose, may be first do the refactoring/renaming within bpf_sk_storage.c first and then create another patch to move the common parts to a new file bpf_local_storage.c.

Not sure if it will be indeed easier to read the diff in practice. I probably should be able to follow it either way.

Since I already refactored it. I will send the next version with the refactoring and split done as a part of the Generalize bpf_sk_storage patch.
If it becomes too hard to review, please let me know and I can split it. :)

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

There is a test in map_tests/sk_storage_map.c, in case you may not notice.

Quoted 8 lines
Quoted 4 lines

I will try to make it generic as a part of this series. If it takes too much time, I will send a separate patch for testing inode_storage_map and till then we have some assurance with test_local_storage in test_progs.

Sure. no problem. It is mostly for you to test sk_storage to ensure things ;) Also give some ideas on what racing conditions have
been considered in the sk_storage test and may be the inode storage test want to stress similar code path.

Thanks! I ran test_maps and it passes. I will send a separate patch that generalizes the sk_storage_map.c.

- KP

#6 lore raw

Re: [PATCH bpf-next v2 1/4] bpf: Generalize bpf_sk_storage

Quoted 49 lines · KP Singh
Quoted 3 lines · Martin KaFai Lau
Quoted 1 line · KP Singh
Quoted 1 line
Quoted 1 line

[...]

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

static atomic_t cache_idx;

inode local storage and sk local storage probably need a separate cache_idx. An improvement on picking cache_idx has just been landed also.

I see, thanks! I rebased and I now see that cache_idx is now a:

Quoted 8 lines
Quoted 5 lines
  static u64 cache_idx_usage_counts[BPF_STORAGE_CACHE_SIZE];

which tracks the free cache slots rather than using a single atomic cache_idx. I guess all types of local storage can share this now right?

I believe they have to be separated. A sk-storage will not be cached/stored in inode. 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.

Ah yes, I see.

I came up with some macros to solve this. Let me know what you think:
(this is on top of the refactoring I did, so some function names may seem new, but it should, hopefully, convey the general idea).

include/linux/bpf_local_storage.h +10 -0
index 3067774cc640..1dc2e6d72091 100644
@@ -79,6 +79,26 @@ struct bpf_local_storage_elem {
 #define SDATA(_SELEM) (&(_SELEM)->sdata)
 #define BPF_STORAGE_CACHE_SIZE	16
 
+u16 bpf_ls_cache_idx_get(spinlock_t *cache_idx_lock,
+			   u64 *cache_idx_usage_count);
+
+void bpf_ls_cache_idx_free(spinlock_t *cache_idx_lock,
+			   u64 *cache_idx_usage_counts, u16 idx);
+
+#define DEFINE_BPF_STORAGE_CACHE(type)					\
+static DEFINE_SPINLOCK(cache_idx_lock_##type);				\
+static u64 cache_idx_usage_counts_##type[BPF_STORAGE_CACHE_SIZE];	\
+static u16 cache_idx_get_##type(void)					\

include/linux/bpf_local_storage.h struct bpf_local_storage_elem { · line 101

Quoted 10 lines
include/linux/bpf_local_storage.h +10 -0
+{									\
+	return bpf_ls_cache_idx_get(&cache_idx_lock_##type,		\
+				    cache_idx_usage_counts_##type);	\
+}									\
+static void cache_idx_free_##type(u16 idx)				\
+{									\
+	return bpf_ls_cache_idx_free(&cache_idx_lock_##type,		\
+				     cache_idx_usage_counts_##type,	\
+				     idx);				\
+}

Sorry for the late reply. I missed this email.

The above looks reasonable.

author
#7 lore raw

PATCH v2 2/4 bpf: Implement bpf_local_storage for inodes

In reply to KP Singh

From: 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_local_storage.h |   7 +
 include/linux/bpf_lsm.h           |  21 +++
 include/linux/bpf_types.h         |   1 +
 include/uapi/linux/bpf.h          |  40 ++++-
 kernel/bpf/bpf_local_storage.c    | 261 +++++++++++++++++++++++++++++-
 kernel/bpf/syscall.c              |   3 +-
 kernel/bpf/verifier.c             |  10 ++
 security/bpf/hooks.c              |   7 +
 tools/bpf/bpftool/map.c           |   1 +
 tools/include/uapi/linux/bpf.h    |  40 ++++-
 tools/lib/bpf/libbpf_probes.c     |   5 +-
 11 files changed, 390 insertions(+), 6 deletions(-)
include/linux/bpf_local_storage.h +7 -0
index 85524f18cd91..7488487b1099 100644
@@ -4,11 +4,14 @@
 #define _BPF_LOCAL_STORAGE_H
 
 struct sock;
+struct inode;
 
 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 bpf_inode_storage_get_proto;
+extern const struct bpf_func_proto bpf_inode_storage_delete_proto;
 
 struct bpf_sk_storage_diag;
 struct sk_buff;
@@ -16,6 +19,7 @@ struct nlattr;
 struct sock;
 
 #ifdef CONFIG_BPF_SYSCALL
+void bpf_inode_storage_free(struct inode *inode);
 int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk);
 struct bpf_sk_storage_diag *
 bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs);
@@ -35,6 +39,9 @@ bpf_sk_storage_diag_alloc(const struct nlattr *nla)
 {
 	return NULL;
 }
+static inline void void bpf_inode_storage_free(struct inode *inode)
+{
+}
 static inline void bpf_sk_storage_diag_free(struct bpf_sk_storage_diag *diag)
 {
 }
include/linux/bpf_lsm.h +21 -0
index af74712af585..8efd7562e3de 100644
@@ -17,9 +17,24 @@
 #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;
+}
+
 #else /* !CONFIG_BPF_LSM */
 
 static inline int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
@@ -28,6 +43,12 @@ static inline int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
 	return -EOPNOTSUPP;
 }
 
+static inline struct bpf_storage_blob *bpf_inode_storage(
+	const struct inode *inode)
+{
+	return NULL;
+}
+
 #endif /* CONFIG_BPF_LSM */
 
 #endif /* _LINUX_BPF_LSM_H */
include/linux/bpf_types.h +1 -0
index a18ae82a298a..881e7954c956 100644
@@ -101,6 +101,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_HASH_OF_MAPS, htab_of_maps_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP, dev_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP_HASH, dev_map_hash_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_SK_STORAGE, sk_storage_map_ops)
+BPF_MAP_TYPE(BPF_MAP_TYPE_INODE_STORAGE, inode_storage_map_ops)
 #if defined(CONFIG_BPF_STREAM_PARSER)
 BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKMAP, sock_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKHASH, sock_hash_ops)
include/uapi/linux/bpf.h +39 -1
index 6218677daaa2..679a50562e72 100644
@@ -148,6 +148,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
@@ -3252,6 +3253,41 @@ union bpf_attr {
  * 		case of **BPF_CSUM_LEVEL_QUERY**, the current skb->csum_level
  * 		is returned or the error code -EACCES in case the skb is not
  * 		subject to CHECKSUM_UNNECESSARY.
+ * 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),			\
@@ -3389,7 +3425,9 @@ union bpf_attr {
 	FN(ringbuf_submit),		\
 	FN(ringbuf_discard),		\
 	FN(ringbuf_query),		\
-	FN(csum_level),
+	FN(csum_level),			\
+	FN(inode_storage_get),		\
+	FN(inode_storage_delete),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
kernel/bpf/bpf_local_storage.c +260 -1

399 lines not on this page · raw message

kernel/bpf/syscall.c +2 -1
index 8da159936bab..a685ec4c7b99 100644
@@ -768,7 +768,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
index 34cde841ab68..52936d24271d 100644
@@ -4090,6 +4090,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;
 	}
@@ -4163,6 +4168,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
index 32d32d485451..35f9b19259e5 100644
@@ -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/map.c +1 -0
index c5fac8068ba1..e8fbafb3e87b 100644
@@ -49,6 +49,7 @@ const char * const map_type_name[] = {
 	[BPF_MAP_TYPE_STACK]			= "stack",
 	[BPF_MAP_TYPE_SK_STORAGE]		= "sk_storage",
 	[BPF_MAP_TYPE_STRUCT_OPS]		= "struct_ops",
+	[BPF_MAP_TYPE_INODE_STORAGE]		= "inode_storage",
 };
 
 const size_t map_type_name_size = ARRAY_SIZE(map_type_name);
tools/include/uapi/linux/bpf.h +39 -1
index 6218677daaa2..679a50562e72 100644
@@ -148,6 +148,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
@@ -3252,6 +3253,41 @@ union bpf_attr {
  * 		case of **BPF_CSUM_LEVEL_QUERY**, the current skb->csum_level
  * 		is returned or the error code -EACCES in case the skb is not
  * 		subject to CHECKSUM_UNNECESSARY.
+ * 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),			\
@@ -3389,7 +3425,9 @@ union bpf_attr {
 	FN(ringbuf_submit),		\
 	FN(ringbuf_discard),		\
 	FN(ringbuf_query),		\
-	FN(csum_level),
+	FN(csum_level),			\
+	FN(inode_storage_get),		\
+	FN(inode_storage_delete),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
tools/lib/bpf/libbpf_probes.c +3 -2
index 10cd8d1891f5..b859558ce290 100644
@@ -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.27.0.111.gc72c7da667-goog
#8 lore raw

Re: [PATCH bpf-next v2 2/4] bpf: Implement bpf_local_storage for inodes

On Wed, Jun 17, 2020 at 10:29:39PM +0200, KP Singh wrote:
[ ... ]

Quoted 30 lines
include/linux/bpf_lsm.h +15 -0
index af74712af585..8efd7562e3de 100644
@@ -17,9 +17,24 @@
 #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;
+}
+
 #else /* !CONFIG_BPF_LSM */
 
 static inline int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,

include/linux/bpf_lsm.h static inline int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog, · line 46

Quoted 4 lines
include/linux/bpf_lsm.h +1 -0
@@ -28,6 +43,12 @@ static inline int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
 	return -EOPNOTSUPP;
 }
 
+static inline struct bpf_storage_blob *bpf_inode_storage(

This does not seem to match the newly added "bpf_inode()" above for the "CONFIG_BPF_LSM" case.

A typo? May be a good idea to test compiling with !CONFIG_BPF_LSM.

Quoted 13 lines
include/linux/bpf_lsm.h +5 -0
+	const struct inode *inode)
+{
+	return NULL;
+}
+
 #endif /* CONFIG_BPF_LSM */
 
 #endif /* _LINUX_BPF_LSM_H */
include/linux/bpf_types.h +0 -0
index a18ae82a298a..881e7954c956 100644

include/linux/bpf_types.h BPF_MAP_TYPE(BPF_MAP_TYPE_HASH_OF_MAPS, htab_of_maps_map_ops) · line 104

Quoted 4 lines
include/linux/bpf_types.h +1 -0
@@ -101,6 +101,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_HASH_OF_MAPS, htab_of_maps_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP, dev_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP_HASH, dev_map_hash_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_SK_STORAGE, sk_storage_map_ops)
+BPF_MAP_TYPE(BPF_MAP_TYPE_INODE_STORAGE, inode_storage_map_ops)

sk_storage is under CONFIG_NET.

inode_storage should be CONFIG_BPF_LSM?

Quoted 3 lines
include/linux/bpf_types.h +0 -0
 #if defined(CONFIG_BPF_STREAM_PARSER)
 BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKMAP, sock_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKHASH, sock_hash_ops)
author
#9 lore raw

Re: [PATCH bpf-next v2 2/4] bpf: Implement bpf_local_storage for inodes

Quoted 36 lines · Martin KaFai Lau

On Wed, Jun 17, 2020 at 10:29:39PM +0200, KP Singh wrote:
[ ... ]

Quoted 33 lines
include/linux/bpf_lsm.h +15 -0
index af74712af585..8efd7562e3de 100644
@@ -17,9 +17,24 @@
 #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;
+}
+
 #else /* !CONFIG_BPF_LSM */
 
 static inline int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
@@ -28,6 +43,12 @@ static inline int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
 	return -EOPNOTSUPP;
 }
 
Quoted 5 lines

include/linux/bpf_lsm.h static inline int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog, · line 46

Quoted 1 line
include/linux/bpf_lsm.h +1 -0
+static inline struct bpf_storage_blob *bpf_inode_storage(

This does not seem to match the newly added "bpf_inode()" above for the "CONFIG_BPF_LSM" case.

A typo? May be a good idea to test compiling with !CONFIG_BPF_LSM.

Sorry about that, yeah it was a last minute lazy rename. Will compile test the series with !CONFIG_BPF_LSM and !CONFIG_NET. Thanks.

Quoted 20 lines
Quoted 14 lines
include/linux/bpf_lsm.h +5 -0
+	const struct inode *inode)
+{
+	return NULL;
+}
+
 #endif /* CONFIG_BPF_LSM */
 
 #endif /* _LINUX_BPF_LSM_H */
include/linux/bpf_types.h +0 -0
index a18ae82a298a..881e7954c956 100644

include/linux/bpf_types.h BPF_MAP_TYPE(BPF_MAP_TYPE_HASH_OF_MAPS, htab_of_maps_map_ops) · line 104

Quoted 4 lines
include/linux/bpf_types.h +1 -0
@@ -101,6 +101,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_HASH_OF_MAPS, htab_of_maps_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP, dev_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_DEVMAP_HASH, dev_map_hash_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_SK_STORAGE, sk_storage_map_ops)
+BPF_MAP_TYPE(BPF_MAP_TYPE_INODE_STORAGE, inode_storage_map_ops)

sk_storage is under CONFIG_NET.

Quoted 1 line

inode_storage should be CONFIG_BPF_LSM?

Thanks, updated.

- KP

Quoted 4 lines
Quoted 4 lines
include/linux/bpf_types.h +0 -0
 #if defined(CONFIG_BPF_STREAM_PARSER)
 BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKMAP, sock_map_ops)
 BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKHASH, sock_hash_ops)
#10 lore raw

Re: [PATCH bpf-next v2 2/4] bpf: Implement bpf_local_storage for inodes

In reply to KP Singh

2020-06-17 22:29 UTC+0200 ~ KP Singh <[email protected]>

Quoted 16 lines

From: 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.

tools/bpf/bpftool/map.c +0 -0
index c5fac8068ba1..e8fbafb3e87b 100644

tools/bpf/bpftool/map.c const char * const map_type_name[] = { · line 55

Quoted 7 lines
tools/bpf/bpftool/map.c +1 -0
@@ -49,6 +49,7 @@ const char * const map_type_name[] = {
 	[BPF_MAP_TYPE_STACK]			= "stack",
 	[BPF_MAP_TYPE_SK_STORAGE]		= "sk_storage",
 	[BPF_MAP_TYPE_STRUCT_OPS]		= "struct_ops",
+	[BPF_MAP_TYPE_INODE_STORAGE]		= "inode_storage",
 };
 
 const size_t map_type_name_size = ARRAY_SIZE(map_type_name);

Thanks for the update on bpftool map types, could you also change the relevant help message, man page and bash completion please? (See below.)

Best regards,
Quentin

------

tools/bpf/bpftool/Documentation/bpftool-map.rst +1 -1
index 31101643e57c..a9cd15ed7187 100644
@@ -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** }
+|		| **queue** | **stack** | **sk_storage** | **struct_ops** | **inode_storage** }
 
 DESCRIPTION
 ===========
tools/bpf/bpftool/bash-completion/bpftool +2 -1
index 25b25aca1112..34cadc081a78 100644
@@ -688,7 +688,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 +1 -1
index c5fac8068ba1..f1b48a97b378 100644
@@ -1590,7 +1590,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 }\n"
+		"                 queue | stack | sk_storage | struct_ops | inode_storage }\n"
 		"       " HELP_SPEC_OPTIONS "\n"
 		"",
 		bin_name, argv[-2]);
author
#11 lore raw

PATCH v2 3/4 bpf: Allow local storage to be used from LSM programs

In reply to KP Singh

From: 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/linux/bpf_local_storage.h |  2 ++
 include/uapi/linux/bpf.h          |  4 ++--
 kernel/bpf/bpf_local_storage.c    | 22 ++++++++++++++++++++++
 kernel/bpf/bpf_lsm.c              | 20 +++++++++++++++++++-
 tools/include/uapi/linux/bpf.h    |  4 ++--
 5 files changed, 47 insertions(+), 5 deletions(-)
include/linux/bpf_local_storage.h +2 -0
index 7488487b1099..cf43ed0b6c3b 100644
@@ -10,6 +10,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;
 extern const struct bpf_func_proto bpf_inode_storage_get_proto;
 extern const struct bpf_func_proto bpf_inode_storage_delete_proto;
 
include/uapi/linux/bpf.h +2 -2
index 679a50562e72..6867e24f8c77 100644
@@ -2783,7 +2783,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*.
  *
@@ -2811,7 +2811,7 @@ union bpf_attr {
  *		**NULL** if not found or there was an error in adding
  *		a new bpf-local-storage.
  *
- * int bpf_sk_storage_delete(struct bpf_map *map, struct bpf_sock *sk)
+ * int bpf_sk_storage_delete(struct bpf_map *map, void *sk)
  *	Description
  *		Delete a bpf-local-storage from a *sk*.
  *	Return
kernel/bpf/bpf_local_storage.c +22 -0
index 572ed711c3fd..c944fc0cc81d 100644
@@ -1276,6 +1276,28 @@ const struct bpf_func_proto bpf_inode_storage_delete_proto = {
 	.btf_id		= bpf_inode_storage_delete_btf_ids,
 };
 
+static int sk_storage_get_btf_ids[4];
+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_get_btf_ids,
+};
+
+static int sk_storage_delete_btf_ids[2];
+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_delete_btf_ids,
+};
+
 struct bpf_sk_storage_diag {
 	u32 nr_maps;
 	struct bpf_map *maps[];
kernel/bpf/bpf_lsm.c +19 -1
index fb278144e9fd..e97dbbf56285 100644
@@ -11,6 +11,7 @@
 #include <linux/bpf_lsm.h>
 #include <linux/kallsyms.h>
 #include <linux/bpf_verifier.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 +46,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,
 };
tools/include/uapi/linux/bpf.h +2 -2
index 679a50562e72..6867e24f8c77 100644
@@ -2783,7 +2783,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*.
  *
@@ -2811,7 +2811,7 @@ union bpf_attr {
  *		**NULL** if not found or there was an error in adding
  *		a new bpf-local-storage.
  *
- * int bpf_sk_storage_delete(struct bpf_map *map, struct bpf_sock *sk)
+ * int bpf_sk_storage_delete(struct bpf_map *map, void *sk)
  *	Description
  *		Delete a bpf-local-storage from a *sk*.
  *	Return
Signature · 1 line
2.27.0.111.gc72c7da667-goog
author
#12 lore raw

PATCH v2 4/4 bpf: Add selftests for local_storage

In reply to KP Singh

From: 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       | 137 ++++++++++++++++++
 2 files changed, 197 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
new file mode 100644
index 000000000000..ee4e27473c1d
@@ -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->bss->inode_storage_result, "inode_storage_result",
+	      "inode_local_storage not set");
+
+	serv_sk = start_server(AF_INET6, SOCK_STREAM);
+	if (CHECK(serv_sk < 0, "start_server", "failed to start server\n"))
+		goto close_prog;
+
+	CHECK(!skel->bss->sk_storage_result, "sk_storage_result",
+	      "sk_local_storage not set");
+
+	close(serv_sk);
+
+close_prog:
+	local_storage__destroy(skel);
+}
tools/testing/selftests/bpf/progs/local_storage.c new file +137 -0

137 lines not on this page · raw message

Signature · 1 line
2.27.0.111.gc72c7da667-goog
Acked-by on v2 4/4
#13 lore raw

Re: [PATCH bpf-next v2 4/4] bpf: Add selftests for local_storage

Quoted 26 lines · KP Singh

From: 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       | 137 ++++++++++++++++++
 2 files changed, 197 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

[...]

Quoted 10 lines
tools/testing/selftests/bpf/progs/local_storage.c new file +4 -0
new file mode 100644
index 000000000000..38954e6a1edc
@@ -0,0 +1,137 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2020 Google LLC.

tools/testing/selftests/bpf/progs/local_storage.c line 14

Quoted 10 lines
tools/testing/selftests/bpf/progs/local_storage.c new file +10 -0
+ */
+
+#include <errno.h>
+#include <linux/bpf.h>
+#include <stdbool.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+__u32 _version SEC("version") = 1;

version is anachronism, please drop it.

Otherwise, LGTM.

[...]

author
#14 lore raw

Re: [PATCH bpf-next v2 4/4] bpf: Add selftests for local_storage

Quoted 44 lines · Andrii Nakryiko
Quoted 26 lines · KP Singh

From: 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       | 137 ++++++++++++++++++
 2 files changed, 197 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

[...]

Quoted 14 lines
tools/testing/selftests/bpf/progs/local_storage.c new file +8 -0
new file mode 100644
index 000000000000..38954e6a1edc
@@ -0,0 +1,137 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright 2020 Google LLC.
+ */
+
+#include <errno.h>
+#include <linux/bpf.h>
Quoted 8 lines

tools/testing/selftests/bpf/progs/local_storage.c line 14

Quoted 6 lines
tools/testing/selftests/bpf/progs/local_storage.c new file +6 -0
+#include <stdbool.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+__u32 _version SEC("version") = 1;

version is anachronism, please drop it.

Removed.

Quoted 2 lines

Otherwise, LGTM.

Thanks

- KP

Quoted 4 lines

[...]