Skip to contentSkip to conversation

bpf: Introduce BPF namespace

RFC PATCH In review Yafang Shao 8 participants 53 messages Mar 26, 2023 – Apr 7, 2023

Subsystems (13)
Kselftest BPF MM core Tracing +9 more subsystems
lore
Share & tools
lore
#0 lore raw

cover bpf: Introduce BPF namespace

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Without CAP_SYS_ADMIN, the only way to get the information of a bpf object which is not created by the process itself is with SCM_RIGHTS, that requires each processes which created bpf object has to implement a unix domain socket to share the fd of a bpf object between different processes, that is really trivial and troublesome.

Hence we need a better mechanism to get bpf object info without CAP_SYS_ADMIN.

BPF namespace is introduced in this patchset with an attempt to remove the CAP_SYS_ADMIN requirement. The user can create bpf map, prog and link in a specific bpf namespace, then these bpf objects will not be visible to the users in a different bpf namespace. But these bpf objects are visible to its parent bpf namespace, so the sys admin can still iterate and inspect them.

BPF namespace is similar to PID namespace, and the bpf objects are similar to tasks, so BPF namespace is very easy to understand. These patchset only implements BPF namespace for bpf map, prog and link. In the future we may extend it to other bpf objects like btf, bpffs and etc. For example, we can allow some of the BTF objects to be used in non-init bpf namespace, then the container user can only trace the processes running in his container, but can't get the information of tasks running in other containers.

A simple example is introduced into selftests/bpf on how to use the bpf namespace.

Putting bpf map, prog and link into bpf namespace is the first step. Let's start with it.

[1]. https://lore.kernel.org/bpf/[email protected]/

Yafang Shao (13):
fork: New clone3 flag for BPF namespace
proc_ns: Extend the field type in struct proc_ns_operations to long
bpf: Implement bpf namespace
bpf: No need to check if id is 0
bpf: Make bpf objects id have the same alloc and free pattern
bpf: Helpers to alloc and free object id in bpf namespace
bpf: Add bpf helper to get bpf object id
bpf: Alloc and free bpf_map id in bpf namespace
bpf: Alloc and free bpf_prog id in bpf namespace
bpf: Alloc and free bpf_link id in bpf namespace
bpf: Allow iterating bpf objects with CAP_BPF in bpf namespace
bpf: Use bpf_idr_lock array instead
selftests/bpf: Add selftest for bpf namespace

 fs/proc/namespaces.c                      |   4 +
 include/linux/bpf.h                       |   9 +-
 include/linux/bpf_namespace.h             |  88 ++++++++++
 include/linux/nsproxy.h                   |   4 +
 include/linux/proc_ns.h                   |   3 +-
 include/linux/user_namespace.h            |   1 +
 include/uapi/linux/bpf.h                  |   7 +
 include/uapi/linux/sched.h                |   1 +
 kernel/bpf/Makefile                       |   1 +
 kernel/bpf/bpf_namespace.c                | 283 ++++++++++++++++++++++++++++++
 kernel/bpf/offload.c                      |  16 +-
 kernel/bpf/syscall.c                      | 262 ++++++++++-----------------
 kernel/bpf/task_iter.c                    |  12 ++
 kernel/fork.c                             |   5 +-
 kernel/nsproxy.c                          |  19 +-
 kernel/trace/bpf_trace.c                  |   2 +
 kernel/ucount.c                           |   1 +
 tools/bpf/bpftool/skeleton/pid_iter.bpf.c |  13 +-
 tools/include/uapi/linux/bpf.h            |   7 +
 tools/testing/selftests/bpf/Makefile      |   3 +-
 tools/testing/selftests/bpf/test_bpfns.c  |  76 ++++++++
 21 files changed, 637 insertions(+), 180 deletions(-)
 create mode 100644 include/linux/bpf_namespace.h
 create mode 100644 kernel/bpf/bpf_namespace.c
 create mode 100644 tools/testing/selftests/bpf/test_bpfns.c
Signature · 1 line
1.8.3.1
#1 lore raw

RFC PATCH 1/13 fork: New clone3 flag for BPF namespace

A new clone3 flag CLONE_NEWBPF is introduced to create a new BPF namespace.

---
 include/uapi/linux/sched.h | 1 +
 kernel/fork.c              | 5 +++--
 2 files changed, 4 insertions(+), 2 deletions(-)
include/uapi/linux/sched.h +1 -0
index 3bac0a8..ace31df 100644
@@ -36,6 +36,7 @@
 /* Flags for the clone3() syscall. */
 #define CLONE_CLEAR_SIGHAND 0x100000000ULL /* Clear any signal handler and reset to SIG_DFL. */
 #define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup given the right permissions. */
+#define CLONE_NEWBPF		0x400000000ULL /* New BPF namespace */
 
 /*
  * cloning flags intersect with CSIGNAL so can be used with unshare and clone3
kernel/fork.c +3 -2
index f68954d..db0abd4 100644
@@ -2929,7 +2929,8 @@ static bool clone3_args_valid(struct kernel_clone_args *kargs)
 {
 	/* Verify that no unknown flags are passed along. */
 	if (kargs->flags &
-	    ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP))
+	    ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | CLONE_INTO_CGROUP |
+		  CLONE_NEWBPF))
 		return false;
 
 	/*
@@ -3080,7 +3081,7 @@ static int check_unshare_flags(unsigned long unshare_flags)
 				CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
 				CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET|
 				CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP|
-				CLONE_NEWTIME))
+				CLONE_NEWTIME|CLONE_NEWBPF))
 		return -EINVAL;
 	/*
 	 * Not implemented, but pretend it works if there is nothing
Signature · 1 line
1.8.3.1
#2 lore raw

RFC PATCH 2/13 proc_ns: Extend the field type in struct proc_ns_operations to long

In reply to Yafang Shao

In struct proc_ns_operations, the field 'type' is the new namespace clone flag. As the newly introduced CLONE_NEWBPF is more than 32bit, we need also extend this field from int to long to adapt to this change.

---
 include/linux/proc_ns.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/proc_ns.h +1 -1
index 75807ec..555c257 100644
@@ -16,7 +16,7 @@
 struct proc_ns_operations {
 	const char *name;
 	const char *real_ns_name;
-	int type;
+	long type;
 	struct ns_common *(*get)(struct task_struct *task);
 	void (*put)(struct ns_common *ns);
 	int (*install)(struct nsset *nsset, struct ns_common *ns);
Signature · 1 line
1.8.3.1
#3 lore raw

RFC PATCH 3/13 bpf: Implement bpf namespace

In reply to Yafang Shao

It is similar with pid namespace. When we create a new bpf object in a child BPF namespace, it will alloc the id in current BPF namespace and its parent BPF namespace. The hierarchy as follows,

init_bpf_ns : level = 0
/ \
child_a child_b : level = 1
/ \
child_b_a child_b_b : level = 2

When we create a bpf object in child_bb, it will allocate IDs for this object in child_bb, child_b and the init_bpf_ns.

We will allocate the id for bpf_map, bpf_prog and bpf_link in bpf namespace.

---
 fs/proc/namespaces.c           |   4 +
 include/linux/bpf_namespace.h  |  46 +++++++++
 include/linux/nsproxy.h        |   4 +
 include/linux/proc_ns.h        |   1 +
 include/linux/user_namespace.h |   1 +
 kernel/bpf/Makefile            |   1 +
 kernel/bpf/bpf_namespace.c     | 219 +++++++++++++++++++++++++++++++++++++++++
 kernel/nsproxy.c               |  19 +++-
 kernel/ucount.c                |   1 +
 9 files changed, 294 insertions(+), 2 deletions(-)
 create mode 100644 include/linux/bpf_namespace.h
 create mode 100644 kernel/bpf/bpf_namespace.c
fs/proc/namespaces.c +4 -0
index 8e159fc..1a36757 100644
@@ -9,6 +9,7 @@
 #include <linux/ipc_namespace.h>
 #include <linux/pid_namespace.h>
 #include <linux/user_namespace.h>
+#include <linux/bpf_namespace.h>
 #include "internal.h"
 
 
@@ -37,6 +38,9 @@
 	&timens_operations,
 	&timens_for_children_operations,
 #endif
+#ifdef CONFIG_BPF
+	&bpfns_operations,
+#endif
 };
 
 static const char *proc_ns_get_link(struct dentry *dentry,
include/linux/bpf_namespace.h new file +46 -0

46 lines not on this page · raw message

include/linux/nsproxy.h +4 -0
index fee881c..d24ab6b 100644
@@ -10,6 +10,9 @@
 struct ipc_namespace;
 struct pid_namespace;
 struct cgroup_namespace;
+#ifdef CONFIG_BPF
+struct bpf_namespace;
+#endif
 struct fs_struct;
 
 /*
@@ -38,6 +41,7 @@ struct nsproxy {
 	struct time_namespace *time_ns;
 	struct time_namespace *time_ns_for_children;
 	struct cgroup_namespace *cgroup_ns;
+	struct bpf_namespace *bpf_ns;
 };
 extern struct nsproxy init_nsproxy;
 
include/linux/proc_ns.h +1 -0
index 555c257..c10ce2c 100644
@@ -46,6 +46,7 @@ enum {
 	PROC_PID_INIT_INO	= 0xEFFFFFFCU,
 	PROC_CGROUP_INIT_INO	= 0xEFFFFFFBU,
 	PROC_TIME_INIT_INO	= 0xEFFFFFFAU,
+	PROC_BPF_INIT_INO	= 0xEFFFFFF9U,
 };
 
 #ifdef CONFIG_PROC_FS
include/linux/user_namespace.h +1 -0
index 45f09be..93eb618 100644
@@ -54,6 +54,7 @@ enum ucount_type {
 	UCOUNT_FANOTIFY_GROUPS,
 	UCOUNT_FANOTIFY_MARKS,
 #endif
+	UCOUNT_BPF_NAMESPACES,
 	UCOUNT_COUNTS,
 };
 
kernel/bpf/Makefile +1 -0
index 0224261..828aef0 100644
@@ -44,3 +44,4 @@ obj-$(CONFIG_BPF_PRELOAD) += preload/
 obj-$(CONFIG_BPF_SYSCALL) += relo_core.o
 $(obj)/relo_core.o: $(srctree)/tools/lib/bpf/relo_core.c FORCE
 	$(call if_changed_rule,cc_o_c)
+obj-$(CONFIG_BPF_SYSCALL) += bpf_namespace.o
kernel/bpf/bpf_namespace.c new file +219 -0

219 lines not on this page · raw message

kernel/nsproxy.c +17 -2
index a487ff2..6a6fa70 100644
@@ -19,6 +19,7 @@
 #include <net/net_namespace.h>
 #include <linux/ipc_namespace.h>
 #include <linux/time_namespace.h>
+#include <linux/bpf_namespace.h>
 #include <linux/fs_struct.h>
 #include <linux/proc_fs.h>
 #include <linux/proc_ns.h>
@@ -26,6 +27,7 @@
 #include <linux/syscalls.h>
 #include <linux/cgroup.h>
 #include <linux/perf_event.h>
+#include <linux/bpf_namespace.h>
 
 static struct kmem_cache *nsproxy_cachep;
 
@@ -47,6 +49,9 @@ struct nsproxy init_nsproxy = {
 	.time_ns		= &init_time_ns,
 	.time_ns_for_children	= &init_time_ns,
 #endif
+#ifdef CONFIG_BPF
+	.bpf_ns		= &init_bpf_ns,
+#endif
 };
 
 static inline struct nsproxy *create_nsproxy(void)
@@ -121,8 +126,16 @@ static struct nsproxy *create_new_namespaces(unsigned long flags,
 	}
 	new_nsp->time_ns = get_time_ns(tsk->nsproxy->time_ns);
 
+	new_nsp->bpf_ns = copy_bpfns(flags, user_ns, tsk->nsproxy->bpf_ns);
+	if (IS_ERR(new_nsp->bpf_ns)) {
+		err = PTR_ERR(new_nsp->bpf_ns);
+		goto out_bpf;
+	}
 	return new_nsp;
 
+out_bpf:
+	put_time_ns(new_nsp->time_ns);
+	put_time_ns(new_nsp->time_ns_for_children);
 out_time:
 	put_net(new_nsp->net_ns);
 out_net:
@@ -156,7 +169,7 @@ int copy_namespaces(unsigned long flags, struct task_struct *tsk)
 
 	if (likely(!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
 			      CLONE_NEWPID | CLONE_NEWNET |
-			      CLONE_NEWCGROUP | CLONE_NEWTIME)))) {
+			      CLONE_NEWCGROUP | CLONE_NEWTIME | CLONE_NEWBPF)))) {
 		if ((flags & CLONE_VM) ||
 		    likely(old_ns->time_ns_for_children == old_ns->time_ns)) {
 			get_nsproxy(old_ns);
@@ -203,6 +216,8 @@ void free_nsproxy(struct nsproxy *ns)
 		put_time_ns(ns->time_ns_for_children);
 	put_cgroup_ns(ns->cgroup_ns);
 	put_net(ns->net_ns);
+	if (ns->bpf_ns)
+		put_bpfns(ns->bpf_ns);
 	kmem_cache_free(nsproxy_cachep, ns);
 }
 
@@ -218,7 +233,7 @@ int unshare_nsproxy_namespaces(unsigned long unshare_flags,
 
 	if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
 			       CLONE_NEWNET | CLONE_NEWPID | CLONE_NEWCGROUP |
-			       CLONE_NEWTIME)))
+			       CLONE_NEWTIME | CLONE_NEWBPF)))
 		return 0;
 
 	user_ns = new_cred ? new_cred->user_ns : current_user_ns();
kernel/ucount.c +1 -0
index ee8e57f..97e0ae3 100644
@@ -87,6 +87,7 @@ static int set_permissions(struct ctl_table_header *head,
 	UCOUNT_ENTRY("max_fanotify_groups"),
 	UCOUNT_ENTRY("max_fanotify_marks"),
 #endif
+	UCOUNT_ENTRY("max_bpf_namespaces"),
 	{ }
 };
 #endif /* CONFIG_SYSCTL */
Signature · 1 line
1.8.3.1
#4 lore raw

Re: [RFC PATCH bpf-next 03/13] bpf: Implement bpf namespace

reproduce (this is a W=1 build): # https://github.com/intel-lab-lkp/linux/commit/97c1724462aa56ab051d35da4a23086e98466e40 git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch…

Hi Yafang,

[FYI, it's a private test report for your RFC patch.] [auto build test ERROR on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/fork-New-clone3-flag-for-BPF-namespace/20230326-172511
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20230326092208.13613-4-laoar.shao%40gmail.com
patch subject: [RFC PATCH bpf-next 03/13] bpf: Implement bpf namespace
config: i386-defconfig (https://download.01.org/0day-ci/archive/20230326/[email protected]/config)
compiler: gcc-11 (Debian 11.3.0-8) 11.3.0

reproduce (this is a W=1 build):
# https://github.com/intel-lab-lkp/linux/commit/97c1724462aa56ab051d35da4a23086e98466e40
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Yafang-Shao/fork-New-clone3-flag-for-BPF-namespace/20230326-172511 git checkout 97c1724462aa56ab051d35da4a23086e98466e40
# save the config file
mkdir build_dir && cp config build_dir/.config
make W=1 O=build_dir ARCH=i386 olddefconfig
make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

ld: kernel/nsproxy.o: in function `create_new_namespaces':

>> nsproxy.c:(.text+0x164): undefined reference to `copy_bpfns'
   ld: kernel/nsproxy.o: in function `free_nsproxy':
>> nsproxy.c:(.text+0x551): undefined reference to `put_bpfns'
>> ld: kernel/nsproxy.o:(.data+0x24): undefined reference to `init_bpf_ns'
>> ld: fs/proc/namespaces.o:(.data+0x24): undefined reference to `bpfns_operations'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#5 lore raw

Re: [RFC PATCH bpf-next 03/13] bpf: Implement bpf namespace

In reply to Yafang Shao
reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross #…

Hi Yafang,

[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/fork-New-clone3-flag-for-BPF-namespace/20230326-172511
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20230326092208.13613-4-laoar.shao%40gmail.com
patch subject: [RFC PATCH bpf-next 03/13] bpf: Implement bpf namespace
config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20230326/[email protected]/config)
compiler: sh4-linux-gcc (GCC) 12.1.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/97c1724462aa56ab051d35da4a23086e98466e40
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Yafang-Shao/fork-New-clone3-flag-for-BPF-namespace/20230326-172511 git checkout 97c1724462aa56ab051d35da4a23086e98466e40
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=sh olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=sh SHELL=/bin/bash kernel/bpf/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

In file included from include/linux/sched.h:10,

                    from include/linux/sched/mm.h:7,
                    from include/linux/xarray.h:19,
                    from include/linux/list_lru.h:14,
                    from include/linux/fs.h:13,
                    from include/uapi/linux/aio_abi.h:31,
                    from include/linux/syscalls.h:77,
                    from kernel/bpf/bpf_namespace.c:3:
>> include/uapi/linux/sched.h:39:33: warning: overflow in conversion from 'long long unsigned int' to 'long int' changes value from '17179869184' to '0' [-Woverflow]
      39 | #define CLONE_NEWBPF            0x400000000ULL /* New BPF namespace */
         |                                 ^~~~~~~~~~~~~~

kernel/bpf/bpf_namespace.c:29:17: note: in expansion of macro 'CLONE_NEWBPF'
29 | .type = CLONE_NEWBPF,
| ^~~~~~~~~~~~

vim +39 include/uapi/linux/sched.h

607ca46e97a1b6 David Howells     2012-10-13  35  
b612e5df4587c9 Christian Brauner 2019-10-14  36  /* Flags for the clone3() syscall. */
b612e5df4587c9 Christian Brauner 2019-10-14  37  #define CLONE_CLEAR_SIGHAND 0x100000000ULL /* Clear any signal handler and reset to SIG_DFL. */
ef2c41cf38a755 Christian Brauner 2020-02-05  38  #define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup given the right permissions. */

7d97a87979269a Yafang Shao 2023-03-26 @39 #define CLONE_NEWBPF 0x400000000ULL /* New BPF namespace */
b612e5df4587c9 Christian Brauner 2019-10-14 40

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#6 lore raw

Re: [RFC PATCH bpf-next 03/13] bpf: Implement bpf namespace

In reply to Yafang Shao
reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross #…

Hi Yafang,

[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/fork-New-clone3-flag-for-BPF-namespace/20230326-172511
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20230326092208.13613-4-laoar.shao%40gmail.com
patch subject: [RFC PATCH bpf-next 03/13] bpf: Implement bpf namespace
config: i386-randconfig-a013 (https://download.01.org/0day-ci/archive/20230326/[email protected]/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/97c1724462aa56ab051d35da4a23086e98466e40
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Yafang-Shao/fork-New-clone3-flag-for-BPF-namespace/20230326-172511 git checkout 97c1724462aa56ab051d35da4a23086e98466e40
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash kernel/bpf/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> kernel/bpf/bpf_namespace.c:29:10: warning: implicit conversion from 'unsigned long long' to 'long' changes value from 17179869184 to 0 [-Wconstant-conversion]
           .type = CLONE_NEWBPF,
                   ^~~~~~~~~~~~
   include/uapi/linux/sched.h:39:23: note: expanded from macro 'CLONE_NEWBPF'
   #define CLONE_NEWBPF            0x400000000ULL /* New BPF namespace */
                                   ^~~~~~~~~~~~~~

1 warning generated.

vim +29 kernel/bpf/bpf_namespace.c

    26	
    27	struct proc_ns_operations bpfns_operations = {
    28		.name = "bpf",
  > 29		.type = CLONE_NEWBPF,
    30		.get  = bpfns_get,
    31		.put  = bpfns_put,
    32		.install = bpfns_install,
    33	};
    34	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#7 lore raw

Re: [RFC PATCH bpf-next 03/13] bpf: Implement bpf namespace

In reply to Yafang Shao
reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross #…

Hi Yafang,

[FYI, it's a private test report for your RFC patch.] [auto build test ERROR on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Yafang-Shao/fork-New-clone3-flag-for-BPF-namespace/20230326-172511
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
patch link:    https://lore.kernel.org/r/20230326092208.13613-4-laoar.shao%40gmail.com
patch subject: [RFC PATCH bpf-next 03/13] bpf: Implement bpf namespace
config: openrisc-randconfig-r004-20230326 (https://download.01.org/0day-ci/archive/20230326/[email protected]/config)
compiler: or1k-linux-gcc (GCC) 12.1.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/97c1724462aa56ab051d35da4a23086e98466e40
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Yafang-Shao/fork-New-clone3-flag-for-BPF-namespace/20230326-172511 git checkout 97c1724462aa56ab051d35da4a23086e98466e40
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=openrisc olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=openrisc SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

or1k-linux-ld: or1k-linux-ld: DWARF error: could not find abbrev number 31
kernel/nsproxy.o: in function `create_new_namespaces':
nsproxy.c:(.text+0x460): undefined reference to `copy_bpfns'
nsproxy.c:(.text+0x460): relocation truncated to fit: R_OR1K_INSN_REL_26 against undefined symbol `copy_bpfns'
or1k-linux-ld: kernel/nsproxy.o: in function `free_nsproxy':
nsproxy.c:(.text+0x740): undefined reference to `put_bpfns'
nsproxy.c:(.text+0x740): relocation truncated to fit: R_OR1K_INSN_REL_26 against undefined symbol `put_bpfns'
or1k-linux-ld: kernel/nsproxy.o:(.data+0x24): undefined reference to `init_bpf_ns'
or1k-linux-ld: or1k-linux-ld: DWARF error: could not find abbrev number 99

>> fs/proc/namespaces.o:(.data+0x14): undefined reference to `bpfns_operations'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#8 lore raw

RFC PATCH 4/13 bpf: No need to check if id is 0

In reply to Yafang Shao

idr_alloc_cyclic() will return -ENOSPC if there's no available IDs, so don't need to check if the id is less than the start number.

---
 kernel/bpf/syscall.c | 7 -------
 1 file changed, 7 deletions(-)
kernel/bpf/syscall.c +0 -7
index e18ac7f..f3664f2 100644
@@ -387,9 +387,6 @@ static int bpf_map_alloc_id(struct bpf_map *map)
 	spin_unlock_bh(&map_idr_lock);
 	idr_preload_end();
 
-	if (WARN_ON_ONCE(!id))
-		return -ENOSPC;
-
 	return id > 0 ? 0 : id;
 }
 
@@ -2032,10 +2029,6 @@ static int bpf_prog_alloc_id(struct bpf_prog *prog)
 	spin_unlock_bh(&prog_idr_lock);
 	idr_preload_end();
 
-	/* id is in [1, INT_MAX) */
-	if (WARN_ON_ONCE(!id))
-		return -ENOSPC;
-
 	return id > 0 ? 0 : id;
 }
 
Signature · 1 line
1.8.3.1
#9 lore raw

RFC PATCH 5/13 bpf: Make bpf objects id have the same alloc and free pattern

In reply to Yafang Shao

Make them have the same patthern, then we can use the generic helpers instead.

---
 kernel/bpf/offload.c | 15 +++++++++++--
 kernel/bpf/syscall.c | 62 ++++++++++++++++++++++++----------------------------
 2 files changed, 41 insertions(+), 36 deletions(-)
kernel/bpf/offload.c +13 -2
index d9c9f45..aec70e0 100644
@@ -134,9 +134,20 @@ static int bpf_map_offload_ndo(struct bpf_offloaded_map *offmap,
 
 static void __bpf_map_offload_destroy(struct bpf_offloaded_map *offmap)
 {
+	struct bpf_map *map = &offmap->map;
+
 	WARN_ON(bpf_map_offload_ndo(offmap, BPF_OFFLOAD_MAP_FREE));
-	/* Make sure BPF_MAP_GET_NEXT_ID can't find this dead map */
-	bpf_map_free_id(&offmap->map);
+	/* Make sure BPF_MAP_GET_NEXT_ID can't find this dead map.
+	 *
+	 * Offloaded maps are removed from the IDR store when their device
+	 * disappears - even if someone holds an fd to them they are unusable,
+	 * the memory is gone, all ops will fail; they are simply waiting for
+	 * refcnt to drop to be freed.
+	 */
+	if (map->id) {
+		bpf_map_free_id(map);
+		map->id = 0;
+	}
 	list_del_init(&offmap->offloads);
 	offmap->netdev = NULL;
 }
kernel/bpf/syscall.c +28 -34

141 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#10 lore raw

RFC PATCH 6/13 bpf: Helpers to alloc and free object id in bpf namespace

In reply to Yafang Shao

Introduce generic helpers to alloc bpf_{map,prog,link} in bpf namespace.

---
 include/linux/bpf_namespace.h | 36 ++++++++++++++++++
 kernel/bpf/bpf_namespace.c    | 86 +++++++++++++++++++++++++++++++++++++++++++
 kernel/bpf/syscall.c          |  6 +--
 3 files changed, 125 insertions(+), 3 deletions(-)
include/linux/bpf_namespace.h +36 -0
index 06aa51f..50bd68c 100644
@@ -38,9 +38,45 @@ struct bpf_namespace {
 
 extern struct bpf_namespace init_bpf_ns;
 extern struct proc_ns_operations bpfns_operations;
+extern spinlock_t map_idr_lock;
+extern spinlock_t prog_idr_lock;
+extern spinlock_t link_idr_lock;
 
 struct bpf_namespace *copy_bpfns(unsigned long flags,
 								struct user_namespace *user_ns,
 								struct bpf_namespace *old_ns);
 void put_bpfns(struct bpf_namespace *ns);
+struct bpf_obj_id *bpf_alloc_obj_id(struct bpf_namespace *ns,
+									void *obj, int type);
+void bpf_free_obj_id(struct bpf_obj_id *obj_id, int type);
+
+/*
+ * The helpers to get the bpf_id's id seen from different namespaces
+ *
+ * bpf_id_nr()    : global id, i.e. the id seen from the init namespace;
+ * bpf_id_vnr()   : virtual id, i.e. the id seen from the pid namespace of
+ *                  current.
+ * bpf_id_nr_ns() : id seen from the ns specified.
+ *
+ * see also task_xid_nr() etc in include/linux/sched.h
+ */
+static inline int bpf_obj_id_nr(struct bpf_obj_id *obj_id)
+{
+	if (obj_id)
+		return obj_id->numbers[0].nr;
+	return 0;
+}
+
+static inline int bpf_obj_id_nr_ns(struct bpf_obj_id *obj_id,
+								   struct bpf_namespace *ns)
+{
+	if (obj_id && ns->level <= obj_id->level)
+		return obj_id->numbers[ns->level].nr;
+	return 0;
+}
+
+static inline int bpf_obj_id_vnr(struct bpf_obj_id *obj_id)
+{
+	return bpf_obj_id_nr_ns(obj_id, current->nsproxy->bpf_ns);
+}
 #endif /* _LINUX_BPF_ID_NS_H */
kernel/bpf/bpf_namespace.c +86 -0

89 lines not on this page · raw message

kernel/bpf/syscall.c +3 -3
index ee1297d..f24e550 100644
@@ -48,11 +48,11 @@
 
 DEFINE_PER_CPU(int, bpf_prog_active);
 static DEFINE_IDR(prog_idr);
-static DEFINE_SPINLOCK(prog_idr_lock);
+DEFINE_SPINLOCK(prog_idr_lock);
 static DEFINE_IDR(map_idr);
-static DEFINE_SPINLOCK(map_idr_lock);
+DEFINE_SPINLOCK(map_idr_lock);
 static DEFINE_IDR(link_idr);
-static DEFINE_SPINLOCK(link_idr_lock);
+DEFINE_SPINLOCK(link_idr_lock);
 
 int sysctl_unprivileged_bpf_disabled __read_mostly =
 	IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
Signature · 1 line
1.8.3.1
#11 lore raw

RFC PATCH 7/13 bpf: Add bpf helper to get bpf object id

In reply to Yafang Shao

A new bpf helper is introduced to get bpf object id in a tracing bpf prog.

---
 include/linux/bpf.h            |  1 +
 include/uapi/linux/bpf.h       |  7 +++++++
 kernel/bpf/task_iter.c         | 12 ++++++++++++
 kernel/trace/bpf_trace.c       |  2 ++
 tools/include/uapi/linux/bpf.h |  7 +++++++
 5 files changed, 29 insertions(+)
include/linux/bpf.h +1 -0
index 2d8f3f6..c94034a 100644
@@ -2867,6 +2867,7 @@ static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
 extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto;
 extern const struct bpf_func_proto bpf_cgrp_storage_get_proto;
 extern const struct bpf_func_proto bpf_cgrp_storage_delete_proto;
+extern const struct bpf_func_proto bpf_find_obj_id_proto;
 
 const struct bpf_func_proto *tracing_prog_func_proto(
   enum bpf_func_id func_id, const struct bpf_prog *prog);
include/uapi/linux/bpf.h +7 -0
index e3d3b51..3009877 100644
@@ -5540,6 +5540,12 @@ struct bpf_stack_build_id {
  *		0 on success.
  *
  *		**-ENOENT** if the bpf_local_storage cannot be found.
+ *
+ * int bpf_find_obj_id(void *obj_id)
+ *	Description
+ *		Get bpf object id in current bpf namespace.
+ *	Return
+ *		bpf object id is returned on success.
  */
 #define ___BPF_FUNC_MAPPER(FN, ctx...)			\
 	FN(unspec, 0, ##ctx)				\
@@ -5754,6 +5760,7 @@ struct bpf_stack_build_id {
 	FN(user_ringbuf_drain, 209, ##ctx)		\
 	FN(cgrp_storage_get, 210, ##ctx)		\
 	FN(cgrp_storage_delete, 211, ##ctx)		\
+	FN(find_obj_id, 212, ##ctx)		\
 	/* */
 
 /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
kernel/bpf/task_iter.c +12 -0
index c4ab9d6..a551743 100644
@@ -8,6 +8,7 @@
 #include <linux/fdtable.h>
 #include <linux/filter.h>
 #include <linux/btf_ids.h>
+#include <linux/bpf_namespace.h>
 #include "mmap_unlock_work.h"
 
 static const char * const iter_task_type_names[] = {
@@ -823,6 +824,17 @@ static void bpf_iter_task_show_fdinfo(const struct bpf_iter_aux_info *aux, struc
 	.arg5_type	= ARG_ANYTHING,
 };
 
+BPF_CALL_1(bpf_find_obj_id, void *, obj_id)
+{
+	return bpf_obj_id_vnr(obj_id);
+}
+
+const struct bpf_func_proto bpf_find_obj_id_proto = {
+	.func		= bpf_find_obj_id,
+	.ret_type	= RET_INTEGER,
+	.arg1_type	= ARG_ANYTHING,
+};
+
 DEFINE_PER_CPU(struct mmap_unlock_irq_work, mmap_unlock_work);
 
 static void do_mmap_read_unlock(struct irq_work *entry)
kernel/trace/bpf_trace.c +2 -0
index bcf91bc..977bb61 100644
@@ -1510,6 +1510,8 @@ static int __init bpf_key_sig_kfuncs_init(void)
 		return &bpf_find_vma_proto;
 	case BPF_FUNC_trace_vprintk:
 		return bpf_get_trace_vprintk_proto();
+	case BPF_FUNC_find_obj_id:
+		return &bpf_find_obj_id_proto;
 	default:
 		return bpf_base_func_proto(func_id);
 	}
tools/include/uapi/linux/bpf.h +7 -0
index d6c5a02..8beacad 100644
@@ -5540,6 +5540,12 @@ struct bpf_stack_build_id {
  *		0 on success.
  *
  *		**-ENOENT** if the bpf_local_storage cannot be found.
+ *
+ * int bpf_find_obj_id(void *obj_id)
+ *	Description
+ *		Get bpf object id in current bpf namespace.
+ *	Return
+ *		bpf object id is returned on success.
  */
 #define ___BPF_FUNC_MAPPER(FN, ctx...)			\
 	FN(unspec, 0, ##ctx)				\
@@ -5754,6 +5760,7 @@ struct bpf_stack_build_id {
 	FN(user_ringbuf_drain, 209, ##ctx)		\
 	FN(cgrp_storage_get, 210, ##ctx)		\
 	FN(cgrp_storage_delete, 211, ##ctx)		\
+	FN(find_obj_id, 212, ##ctx)				\
 	/* */
 
 /* backwards-compatibility macros for users of __BPF_FUNC_MAPPER that don't
Signature · 1 line
1.8.3.1
#12 lore raw

RFC PATCH 8/13 bpf: Alloc and free bpf_map id in bpf namespace

In reply to Yafang Shao

We only expose the bpf map id under current bpf namespace to user. The map->id is still the id in the init bpf namespace.

The result as follows,

Run bpftool in a new bpf namespace

$ bpftool map show
4: array  name kprobe_b.rodata  flags 0x80
        key 4B  value 37B  max_entries 1  memlock 360B
        btf_id 159  frozen
        pids kprobe(20490)
5: array  name kprobe_b.data  flags 0x400
        key 4B  value 4B  max_entries 1  memlock 8192B
        btf_id 159
        pids kprobe(20490)
$ bpftool prog show
91: kprobe  name kretprobe_run  tag 0de47cc241a2b1b3  gpl
        loaded_at 2023-03-20T22:19:16+0800  uid 0
        xlated 56B  jited 39B  memlock 4096B  map_ids 4
        btf_id 159
        pids kprobe(20490)
93: kprobe  name kprobe_run  tag bf163b23cd3b174d  gpl
        loaded_at 2023-03-20T22:19:16+0800  uid 0
        xlated 48B  jited 35B  memlock 4096B  map_ids 4
        btf_id 159
        pids kprobe(20490)

At the same time, run bpftool in init bpf namespace,

$ bpftool map show
48: array  name kprobe_b.rodata  flags 0x80
        key 4B  value 37B  max_entries 1  memlock 360B
        btf_id 159  frozen
        pids kprobe(20490)
49: array  name kprobe_b.data  flags 0x400
        key 4B  value 4B  max_entries 1  memlock 8192B
        btf_id 159
        pids kprobe(20490)
$ bpftool prog show
91: kprobe  name kretprobe_run  tag 0de47cc241a2b1b3  gpl
        loaded_at 2023-03-20T22:19:16+0800  uid 0
        xlated 56B  jited 39B  memlock 4096B  map_ids 48
        btf_id 159
        pids kprobe(20490)
93: kprobe  name kprobe_run  tag bf163b23cd3b174d  gpl
        loaded_at 2023-03-20T22:19:16+0800  uid 0
        xlated 48B  jited 35B  memlock 4096B  map_ids 48
        btf_id 159
        pids kprobe(20490)

In init bpf namespace, bpftool can also show other bpf maps, but the bpftool running in the new bpf namespace can't.

---
 include/linux/bpf.h                       |  3 +-
 kernel/bpf/bpf_namespace.c                |  1 +
 kernel/bpf/offload.c                      |  3 +-
 kernel/bpf/syscall.c                      | 58 ++++++++++---------------------
 tools/bpf/bpftool/skeleton/pid_iter.bpf.c |  7 +++-
 5 files changed, 30 insertions(+), 42 deletions(-)
include/linux/bpf.h +2 -1
index c94034a..2a1f19c 100644
@@ -29,6 +29,7 @@
 #include <linux/rcupdate_trace.h>
 #include <linux/static_call.h>
 #include <linux/memcontrol.h>
+#include <linux/bpf_namespace.h>
 
 struct bpf_verifier_env;
 struct bpf_verifier_log;
@@ -279,6 +280,7 @@ struct bpf_map {
 	} owner;
 	bool bypass_spec_v1;
 	bool frozen; /* write-once; write-protected by freeze_mutex */
+	struct bpf_obj_id *obj_id;
 };
 
 static inline const char *btf_field_type_name(enum btf_field_type type)
@@ -1939,7 +1941,6 @@ struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
 void bpf_prog_put(struct bpf_prog *prog);
 
 void bpf_prog_free_id(struct bpf_prog *prog);
-void bpf_map_free_id(struct bpf_map *map);
 
 struct btf_field *btf_record_find(const struct btf_record *rec,
 				  u32 offset, u32 field_mask);
kernel/bpf/bpf_namespace.c +1 -0
index 1e98d1d..6a6ef70 100644
@@ -11,6 +11,7 @@
 #include <linux/bpf_namespace.h>
 
 #define MAX_BPF_NS_LEVEL 32
+DEFINE_SPINLOCK(map_idr_lock);
 static struct kmem_cache *bpfns_cachep;
 static struct kmem_cache *obj_id_cache[MAX_PID_NS_LEVEL];
 static struct ns_common *bpfns_get(struct task_struct *task);
kernel/bpf/offload.c +2 -1
index aec70e0..7a90ebe 100644
@@ -25,6 +25,7 @@
 #include <linux/rhashtable.h>
 #include <linux/rtnetlink.h>
 #include <linux/rwsem.h>
+#include <linux/bpf_namespace.h>
 
 /* Protects offdevs, members of bpf_offload_netdev and offload members
  * of all progs.
@@ -145,7 +146,7 @@ static void __bpf_map_offload_destroy(struct bpf_offloaded_map *offmap)
 	 * refcnt to drop to be freed.
 	 */
 	if (map->id) {
-		bpf_map_free_id(map);
+		bpf_free_obj_id(map->obj_id, MAP_OBJ_ID);
 		map->id = 0;
 	}
 	list_del_init(&offmap->offloads);
kernel/bpf/syscall.c +19 -39

160 lines not on this page · raw message

tools/bpf/bpftool/skeleton/pid_iter.bpf.c +6 -1
index eb05ea5..a71aef7 100644
@@ -24,11 +24,14 @@ enum bpf_obj_type {
 
 static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
 {
+	void *obj_id;
+
 	switch (type) {
 	case BPF_OBJ_PROG:
 		return BPF_CORE_READ((struct bpf_prog *)ent, aux, id);
 	case BPF_OBJ_MAP:
-		return BPF_CORE_READ((struct bpf_map *)ent, id);
+		obj_id = BPF_CORE_READ((struct bpf_map *)ent, obj_id);
+		break;
 	case BPF_OBJ_BTF:
 		return BPF_CORE_READ((struct btf *)ent, id);
 	case BPF_OBJ_LINK:
@@ -36,6 +39,8 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
 	default:
 		return 0;
 	}
+
+	return bpf_find_obj_id(obj_id);
 }
 
 /* could be used only with BPF_LINK_TYPE_PERF_EVENT links */
Signature · 1 line
1.8.3.1
#13 lore raw

Re: [RFC PATCH bpf-next 08/13] bpf: Alloc and free bpf_map id in bpf namespace

Quoted 10 lines · Yafang Shao

We only expose the bpf map id under current bpf namespace to user. The map->id is still the id in the init bpf namespace.

The result as follows,

Run bpftool in a new bpf namespace

$ bpftool map show
4: array  name kprobe_b.rodata  flags 0x80
        key 4B  value 37B  max_entries 1  memlock 360B
        btf_id 159  frozen

The btf_id is identical for all the different objects in this example output; surely that can't be right? Copy-paste error? Same thing in the other patches...

-Toke

#14 lore raw

Re: [RFC PATCH bpf-next 08/13] bpf: Alloc and free bpf_map id in bpf namespace

Quoted 14 lines · Toke Høiland-Jørgensen
Quoted 10 lines · Yafang Shao

We only expose the bpf map id under current bpf namespace to user. The map->id is still the id in the init bpf namespace.

The result as follows,

Run bpftool in a new bpf namespace

$ bpftool map show
4: array  name kprobe_b.rodata  flags 0x80
        key 4B  value 37B  max_entries 1  memlock 360B
        btf_id 159  frozen
Quoted 4 lines

The btf_id is identical for all the different objects in this example output; surely that can't be right? Copy-paste error? Same thing in the other patches...

The bpf progs {"kretprobe_run","kprobe_run"} and the bpf maps {"kprobe_b.rodata","kprobe_b.data"} belong to the same bpf program. So the btf_id of them are always the same. For example, below is the result when I rerun it on my test server,

$ bpftool btf show
...
943: name <anon>  size 1086B  prog_ids 48824,48822  map_ids 43712,43711
        pids kprobe(3599801)
...
$ bpftool map show
43711: array  name kprobe_b.rodata  flags 0x80
        key 4B  value 37B  max_entries 1  memlock 360B
        btf_id 943  frozen
        pids kprobe(3599801)
43712: array  name kprobe_b.data  flags 0x400
        key 4B  value 4B  max_entries 1  memlock 8192B
        btf_id 943
        pids kprobe(3599801)
$ bpftool prog show
48822: kprobe  name kretprobe_run  tag 0de47cc241a2b1b3  gpl
        loaded_at 2023-03-27T10:35:01+0800  uid 0
        xlated 112B  jited 78B  memlock 4096B  map_ids 43711
        btf_id 943
        pids kprobe(3599801)
48824: kprobe  name kprobe_run  tag bf163b23cd3b174d  gpl
        loaded_at 2023-03-27T10:35:01+0800  uid 0
        xlated 104B  jited 75B  memlock 4096B  map_ids 43711
        btf_id 943
        pids kprobe(3599801)

The btf_id hasn't been added into the bpf namespace, so the btf id in init bpf namespace and child bpf namespace are the same value.

Signature · 2 lines
Regards
Yafang
#15 lore raw

RFC PATCH 9/13 bpf: Alloc and free bpf_prog id in bpf namespace

In reply to Yafang Shao

Similar to bpf map, We only expose the bpf map id under current bpf namespace to user. The prog->aux->id is still the id in the init bpf namespace.
The id of used_maps is also the id in current bpf namespace.

The result as follows,

Run bpftool in current namespace,

$ bpftool map show
4: array  name kprobe_b.rodata  flags 0x80
        key 4B  value 37B  max_entries 1  memlock 360B
        btf_id 96  frozen
        pids kprobe(8790)
5: array  name kprobe_b.data  flags 0x400
        key 4B  value 4B  max_entries 1  memlock 8192B
        btf_id 96
        pids kprobe(8790)
$ bpftool prog show
7: kprobe  name kretprobe_run  tag 0de47cc241a2b1b3  gpl
        loaded_at 2023-03-21T10:20:58+0800  uid 0
        xlated 56B  jited 39B  memlock 4096B  map_ids 4
        btf_id 96
9: kprobe  name kprobe_run  tag bf163b23cd3b174d  gpl
        loaded_at 2023-03-21T10:20:58+0800  uid 0
        xlated 48B  jited 35B  memlock 4096B  map_ids 4
        btf_id 96

At the same time, run bpftool in init bpf namespace.

$ bpftool map show
18: array  name kprobe_b.rodata  flags 0x80
        key 4B  value 37B  max_entries 1  memlock 360B
        btf_id 96  frozen
        pids kprobe(8790)
19: array  name kprobe_b.data  flags 0x400
        key 4B  value 4B  max_entries 1  memlock 8192B
        btf_id 96
        pids kprobe(8790)
$ bpftool prog show
29: kprobe  name kretprobe_run  tag 0de47cc241a2b1b3  gpl
        loaded_at 2023-03-21T10:20:58+0800  uid 0
        xlated 56B  jited 39B  memlock 4096B  map_ids 18
        btf_id 96
        pids kprobe(8790)
31: kprobe  name kprobe_run  tag bf163b23cd3b174d  gpl
        loaded_at 2023-03-21T10:20:58+0800  uid 0
        xlated 48B  jited 35B  memlock 4096B  map_ids 18
        btf_id 96
        pids kprobe(8790)

In init bpf namespace, bpftool can also show other bpf progs, but the bpftool running in the new bpf namespace can't.

---
 include/linux/bpf.h                       |  3 +-
 kernel/bpf/bpf_namespace.c                |  1 +
 kernel/bpf/syscall.c                      | 56 ++++++++++---------------------
 tools/bpf/bpftool/skeleton/pid_iter.bpf.c |  3 +-
 4 files changed, 22 insertions(+), 41 deletions(-)
include/linux/bpf.h +1 -2
index 2a1f19c..16f2a01 100644
@@ -1416,6 +1416,7 @@ struct bpf_prog_aux {
 		struct work_struct work;
 		struct rcu_head	rcu;
 	};
+	struct bpf_obj_id *obj_id;
 };
 
 struct bpf_prog {
@@ -1940,8 +1941,6 @@ struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
 void bpf_prog_put(struct bpf_prog *prog);
 
-void bpf_prog_free_id(struct bpf_prog *prog);
-
 struct btf_field *btf_record_find(const struct btf_record *rec,
 				  u32 offset, u32 field_mask);
 void btf_record_free(struct btf_record *rec);
kernel/bpf/bpf_namespace.c +1 -0
index 6a6ef70..8c70945 100644
@@ -12,6 +12,7 @@
 
 #define MAX_BPF_NS_LEVEL 32
 DEFINE_SPINLOCK(map_idr_lock);
+DEFINE_SPINLOCK(prog_idr_lock);
 static struct kmem_cache *bpfns_cachep;
 static struct kmem_cache *obj_id_cache[MAX_PID_NS_LEVEL];
 static struct ns_common *bpfns_get(struct task_struct *task);
kernel/bpf/syscall.c +18 -38

149 lines not on this page · raw message

tools/bpf/bpftool/skeleton/pid_iter.bpf.c +2 -1
index a71aef7..1fd8ceb 100644
@@ -28,7 +28,8 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
 
 	switch (type) {
 	case BPF_OBJ_PROG:
-		return BPF_CORE_READ((struct bpf_prog *)ent, aux, id);
+		obj_id = BPF_CORE_READ((struct bpf_prog *)ent, aux, obj_id);
+		break;
 	case BPF_OBJ_MAP:
 		obj_id = BPF_CORE_READ((struct bpf_map *)ent, obj_id);
 		break;
Signature · 1 line
1.8.3.1
#16 lore raw

RFC PATCH 10/13 bpf: Alloc and free bpf_link id in bpf namespace

In reply to Yafang Shao

Similar to bpf map, We only expose the bpf link id under current bpf namespace to user. The link->id is still the id in the init bpf namespace.

The result as follows,

Run bpftool in a new bpf namespace,

$ bpftool map show
4: array  name kprobe_b.rodata  flags 0x80
        key 4B  value 37B  max_entries 1  memlock 360B
        btf_id 79  frozen
        pids kprobe(8322)
5: array  name kprobe_b.data  flags 0x400
        key 4B  value 4B  max_entries 1  memlock 8192B
        btf_id 79
        pids kprobe(8322)
$ bpftool prog show
7: kprobe  name kretprobe_run  tag 0de47cc241a2b1b3  gpl
        loaded_at 2023-03-21T13:54:34+0800  uid 0
        xlated 56B  jited 39B  memlock 4096B  map_ids 4
        btf_id 79
        pids kprobe(8322)
9: kprobe  name kprobe_run  tag bf163b23cd3b174d  gpl
        loaded_at 2023-03-21T13:54:34+0800  uid 0
        xlated 48B  jited 35B  memlock 4096B  map_ids 4
        btf_id 79
        pids kprobe(8322)
$ bpftool link show
1: perf_event  prog 9
        bpf_cookie 0
        pids kprobe(8322)
2: perf_event  prog 7
        bpf_cookie 0
        pids kprobe(8322)

At the same time, run bpftool in the init bpf namespace,

$ bpftool map show
8: array  name kprobe_b.rodata  flags 0x80
        key 4B  value 37B  max_entries 1  memlock 360B
        btf_id 79  frozen
        pids kprobe(8322)
9: array  name kprobe_b.data  flags 0x400
        key 4B  value 4B  max_entries 1  memlock 8192B
        btf_id 79
        pids kprobe(8322)
$ bpftool prog show
15: kprobe  name kretprobe_run  tag 0de47cc241a2b1b3  gpl
        loaded_at 2023-03-21T13:54:34+0800  uid 0
        xlated 56B  jited 39B  memlock 4096B  map_ids 8
        btf_id 79
        pids kprobe(8322)
17: kprobe  name kprobe_run  tag bf163b23cd3b174d  gpl
        loaded_at 2023-03-21T13:54:34+0800  uid 0
        xlated 48B  jited 35B  memlock 4096B  map_ids 8
        btf_id 79
        pids kprobe(8322)
$ bpftool link show
2: perf_event  prog 17
        bpf_cookie 0
        pids kprobe(8322)
3: perf_event  prog 15
        bpf_cookie 0
        pids kprobe(8322)

The bpftool running in the init bpf namespace can also show other bpf links, but the bpftool in the new bpf namespace can only show the links in its current bpf namespace.

---
 include/linux/bpf.h                       |  2 ++
 kernel/bpf/bpf_namespace.c                |  1 +
 kernel/bpf/syscall.c                      | 55 +++++++++++--------------------
 tools/bpf/bpftool/skeleton/pid_iter.bpf.c |  3 +-
 4 files changed, 24 insertions(+), 37 deletions(-)
include/linux/bpf.h +2 -0
index 16f2a01..efa14ac 100644
@@ -1468,6 +1468,7 @@ struct bpf_link {
 	const struct bpf_link_ops *ops;
 	struct bpf_prog *prog;
 	struct work_struct work;
+	struct bpf_obj_id *obj_id;
 };
 
 struct bpf_link_ops {
@@ -1506,6 +1507,7 @@ struct bpf_link_primer {
 	struct file *file;
 	int fd;
 	u32 id;
+	struct bpf_obj_id *obj_id;
 };
 
 struct bpf_struct_ops_value;
kernel/bpf/bpf_namespace.c +1 -0
index 8c70945..c7d62ef 100644
@@ -13,6 +13,7 @@
 #define MAX_BPF_NS_LEVEL 32
 DEFINE_SPINLOCK(map_idr_lock);
 DEFINE_SPINLOCK(prog_idr_lock);
+DEFINE_SPINLOCK(link_idr_lock);
 static struct kmem_cache *bpfns_cachep;
 static struct kmem_cache *obj_id_cache[MAX_PID_NS_LEVEL];
 static struct ns_common *bpfns_get(struct task_struct *task);
kernel/bpf/syscall.c +19 -36

158 lines not on this page · raw message

tools/bpf/bpftool/skeleton/pid_iter.bpf.c +2 -1
index 1fd8ceb..e2237ad 100644
@@ -36,7 +36,8 @@ static __always_inline __u32 get_obj_id(void *ent, enum bpf_obj_type type)
 	case BPF_OBJ_BTF:
 		return BPF_CORE_READ((struct btf *)ent, id);
 	case BPF_OBJ_LINK:
-		return BPF_CORE_READ((struct bpf_link *)ent, id);
+		obj_id = BPF_CORE_READ((struct bpf_link *)ent, obj_id);
+		break;
 	default:
 		return 0;
 	}
Signature · 1 line
1.8.3.1
#17 lore raw

RFC PATCH 11/13 bpf: Allow iterating bpf objects with CAP_BPF in bpf namespace

In reply to Yafang Shao

CAP_SYS_ADMIN is not required to iterate bpf objects if a user is in a non-init bpf namespace. The user can iterate bpf maps, progs, and links in his bpf namespace but can't iterate the bpf objects in different bpf namespace.

---
 include/linux/bpf_namespace.h |  8 ++++++++
 kernel/bpf/syscall.c          | 10 +++++-----
 2 files changed, 13 insertions(+), 5 deletions(-)
include/linux/bpf_namespace.h +8 -0
index 50bd68c..f484791 100644
@@ -5,6 +5,7 @@
 #include <linux/idr.h>
 #include <linux/ns_common.h>
 #include <linux/user_namespace.h>
+#include <linux/capability.h>
 
 struct ubpf_obj_id {
 	int nr;
@@ -79,4 +80,11 @@ static inline int bpf_obj_id_vnr(struct bpf_obj_id *obj_id)
 {
 	return bpf_obj_id_nr_ns(obj_id, current->nsproxy->bpf_ns);
 }
+
+static inline bool bpfns_capable(void)
+{
+	if (current->nsproxy->bpf_ns != &init_bpf_ns && capable(CAP_BPF))
+		return true;
+	return false;
+}
 #endif /* _LINUX_BPF_ID_NS_H */
kernel/bpf/syscall.c +5 -5
index 855d5f7..8a72694 100644
@@ -3628,7 +3628,7 @@ static int bpf_obj_get_next_id(const union bpf_attr *attr,
 	if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
 		return -EINVAL;
 
-	if (!capable(CAP_SYS_ADMIN))
+	if (!capable(CAP_SYS_ADMIN) && !bpfns_capable())
 		return -EPERM;
 
 	next_id++;
@@ -3712,7 +3712,7 @@ static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
 	if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
 		return -EINVAL;
 
-	if (!capable(CAP_SYS_ADMIN))
+	if (!capable(CAP_SYS_ADMIN) && !bpfns_capable())
 		return -EPERM;
 
 	prog = bpf_prog_by_id(id);
@@ -3740,7 +3740,7 @@ static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
 	    attr->open_flags & ~BPF_OBJ_FLAG_MASK)
 		return -EINVAL;
 
-	if (!capable(CAP_SYS_ADMIN))
+	if (!capable(CAP_SYS_ADMIN) && !bpfns_capable())
 		return -EPERM;
 
 	f_flags = bpf_get_file_flag(attr->open_flags);
@@ -4386,7 +4386,7 @@ static int bpf_task_fd_query(const union bpf_attr *attr,
 	if (CHECK_ATTR(BPF_TASK_FD_QUERY))
 		return -EINVAL;
 
-	if (!capable(CAP_SYS_ADMIN))
+	if (!capable(CAP_SYS_ADMIN) && !bpfns_capable())
 		return -EPERM;
 
 	if (attr->task_fd_query.flags != 0)
@@ -4781,7 +4781,7 @@ static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
 	if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
 		return -EINVAL;
 
-	if (!capable(CAP_SYS_ADMIN))
+	if (!capable(CAP_SYS_ADMIN) && !bpfns_capable())
 		return -EPERM;
 
 	link = bpf_link_by_id(id);
Signature · 1 line
1.8.3.1
#18 lore raw

RFC PATCH 12/13 bpf: Use bpf_idr_lock array instead

In reply to Yafang Shao

Use an array instead, that will make the code more clear. It is a cleanup.

---
 include/linux/bpf_namespace.h |  4 +---
 kernel/bpf/bpf_namespace.c    | 37 ++++++-------------------------------
 kernel/bpf/syscall.c          | 42 +++++++++++++++++++++---------------------
 3 files changed, 28 insertions(+), 55 deletions(-)
include/linux/bpf_namespace.h +1 -3
index f484791..4d58986 100644
@@ -39,9 +39,7 @@ struct bpf_namespace {
 
 extern struct bpf_namespace init_bpf_ns;
 extern struct proc_ns_operations bpfns_operations;
-extern spinlock_t map_idr_lock;
-extern spinlock_t prog_idr_lock;
-extern spinlock_t link_idr_lock;
+extern spinlock_t bpf_idr_lock[OBJ_ID_NUM];
 
 struct bpf_namespace *copy_bpfns(unsigned long flags,
 								struct user_namespace *user_ns,
kernel/bpf/bpf_namespace.c +6 -31
index c7d62ef..51c240f 100644
@@ -11,9 +11,7 @@
 #include <linux/bpf_namespace.h>
 
 #define MAX_BPF_NS_LEVEL 32
-DEFINE_SPINLOCK(map_idr_lock);
-DEFINE_SPINLOCK(prog_idr_lock);
-DEFINE_SPINLOCK(link_idr_lock);
+spinlock_t bpf_idr_lock[OBJ_ID_NUM];
 static struct kmem_cache *bpfns_cachep;
 static struct kmem_cache *obj_id_cache[MAX_PID_NS_LEVEL];
 static struct ns_common *bpfns_get(struct task_struct *task);
@@ -208,8 +206,10 @@ static void __init bpfns_idr_init(void)
 
 	init_bpf_ns.obj_id_cachep =
 		KMEM_CACHE(pid, SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT);
-	for (i = 0; i < OBJ_ID_NUM; i++)
+	for (i = 0; i < OBJ_ID_NUM; i++) {
 		idr_init(&init_bpf_ns.idr[i]);
+		spin_lock_init(&bpf_idr_lock[i]);
+	}
 }
 
 static __init int bpf_namespaces_init(void)
@@ -231,24 +231,11 @@ struct bpf_obj_id *bpf_alloc_obj_id(struct bpf_namespace *ns,
 	int id;
 	int i;
 
-	switch (type) {
-	case MAP_OBJ_ID:
-		idr_lock = &map_idr_lock;
-		break;
-	case PROG_OBJ_ID:
-		idr_lock = &prog_idr_lock;
-		break;
-	case LINK_OBJ_ID:
-		idr_lock = &link_idr_lock;
-		break;
-	default:
-		return ERR_PTR(-EINVAL);
-	}
-
 	obj_id = kmem_cache_alloc(ns->obj_id_cachep, GFP_KERNEL);
 	if (!obj_id)
 		return ERR_PTR(-ENOMEM);
 
+	idr_lock = &bpf_idr_lock[type];
 	obj_id->level = ns->level;
 	for (i = ns->level; i >= 0; i--) {
 		idr_preload(GFP_KERNEL);
@@ -283,19 +270,7 @@ void bpf_free_obj_id(struct bpf_obj_id *obj_id, int type)
 	unsigned long flags;
 	int i;
 
-	switch (type) {
-	case MAP_OBJ_ID:
-		idr_lock = &map_idr_lock;
-		break;
-	case PROG_OBJ_ID:
-		idr_lock = &prog_idr_lock;
-		break;
-	case LINK_OBJ_ID:
-		idr_lock = &link_idr_lock;
-		break;
-	default:
-		return;
-	}
+	idr_lock = &bpf_idr_lock[type];
 	/* Note that the level-0 should be freed at last */
 	for (i = obj_id->level; i >= 0; i--) {
 		spin_lock_irqsave(idr_lock, flags);
kernel/bpf/syscall.c +21 -21

154 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#19 lore raw

RFC PATCH 13/13 selftests/bpf: Add selftest for bpf namespace

In reply to Yafang Shao

A simple test case is added for the newly introduced bpf namespcae.

---
 tools/testing/selftests/bpf/Makefile     |  3 +-
 tools/testing/selftests/bpf/test_bpfns.c | 76 ++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/test_bpfns.c
tools/testing/selftests/bpf/Makefile +2 -1
index 4a8ef11..55f0aeb 100644
@@ -40,7 +40,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
 	test_sock test_sockmap get_cgroup_id_user \
 	test_cgroup_storage \
 	test_tcpnotify_user test_sysctl \
-	test_progs-no_alu32
+	test_progs-no_alu32 test_bpfns
 
 # Also test bpf-gcc, if present
 ifneq ($(BPF_GCC),)
@@ -255,6 +255,7 @@ $(OUTPUT)/flow_dissector_load: $(TESTING_HELPERS)
 $(OUTPUT)/test_maps: $(TESTING_HELPERS)
 $(OUTPUT)/test_verifier: $(TESTING_HELPERS) $(CAP_HELPERS) $(UNPRIV_HELPERS)
 $(OUTPUT)/xsk.o: $(BPFOBJ)
+$(OUTPUT)/test_bpfns: $(TESTING_HELPERS)
 
 BPFTOOL ?= $(DEFAULT_BPFTOOL)
 $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)    \
tools/testing/selftests/bpf/test_bpfns.c new file +76 -0

76 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#20 lore raw
In reply to Yafang Shao
Quoted 27 lines · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Without CAP_SYS_ADMIN, the only way to get the information of a bpf object which is not created by the process itself is with SCM_RIGHTS, that requires each processes which created bpf object has to implement a unix domain socket to share the fd of a bpf object between different processes, that is really trivial and troublesome.

Hence we need a better mechanism to get bpf object info without CAP_SYS_ADMIN.

BPF namespace is introduced in this patchset with an attempt to remove the CAP_SYS_ADMIN requirement. The user can create bpf map, prog and link in a specific bpf namespace, then these bpf objects will not be visible to the users in a different bpf namespace. But these bpf objects are visible to its parent bpf namespace, so the sys admin can still iterate and inspect them.

Quoted 4 lines

BPF namespace is similar to PID namespace, and the bpf objects are similar to tasks, so BPF namespace is very easy to understand. These patchset only implements BPF namespace for bpf map, prog and link. In the future we may extend it to other bpf objects like btf, bpffs and etc.

May? I think we should cover all of the existing BPF objects from the beginning here, or we may miss important interactions that will invalidate the whole idea. In particular, I'm a little worried about the interaction between namespaces and bpffs; what happens if you're in a bpf namespace and you try to read a BPF object from a bpffs that belongs to a different namespace? Does the operation fail? Is the object hidden entirely? Something else?

-Toke

#21 lore raw
Quoted 30 lines · Toke Høiland-Jørgensen
Quoted 27 lines · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Without CAP_SYS_ADMIN, the only way to get the information of a bpf object which is not created by the process itself is with SCM_RIGHTS, that requires each processes which created bpf object has to implement a unix domain socket to share the fd of a bpf object between different processes, that is really trivial and troublesome.

Hence we need a better mechanism to get bpf object info without CAP_SYS_ADMIN.

BPF namespace is introduced in this patchset with an attempt to remove the CAP_SYS_ADMIN requirement. The user can create bpf map, prog and link in a specific bpf namespace, then these bpf objects will not be visible to the users in a different bpf namespace. But these bpf objects are visible to its parent bpf namespace, so the sys admin can still iterate and inspect them.

Quoted 8 lines
Quoted 4 lines

BPF namespace is similar to PID namespace, and the bpf objects are similar to tasks, so BPF namespace is very easy to understand. These patchset only implements BPF namespace for bpf map, prog and link. In the future we may extend it to other bpf objects like btf, bpffs and etc.

May? I think we should cover all of the existing BPF objects from the beginning here, or we may miss important interactions that will invalidate the whole idea.

This patchset is intended to address iterating bpf IDs and converting IDs to FDs. To be more specific, it covers BPF_{PROG,MAP,LINK}_GET_NEXT_ID and BPF_{PROG,MAP,LINK}_GET_FD_BY_ID. It should also include BPF_BTF_GET_NEXT_ID and BPF_BTF_GET_FD_BY_ID, but I don't implement it because I find we can do more wrt BTF, for example, if we can expose a small amount of BTFs in the vmlinux to non-root bpf namespace.
But, yes, I should implement BTF ID in this patchset.

Quoted 6 lines

In particular, I'm a little worried about the
interaction between namespaces and bpffs; what happens if you're in a bpf namespace and you try to read a BPF object from a bpffs that belongs to a different namespace? Does the operation fail? Is the object hidden entirely? Something else?

bpffs is a different topic and it can be implemented in later patchsets. bpffs has its own specific problem even without the bpf namespace.
1. The user can always get the information of a bpf object through its corresponding pinned file.
In our practice, different container users have different bpffs, and we allow the container user to bind-mount its bpffs only, so others' bpffs are invisible.
To make it better with the bpf namespace, I think we can fail the operation if the pinned file doesn't belong to its bpf namespace. That said, we will add pinned bpf files into the bpf namespace in the next step.

2. The user can always iterate bpf objects through progs.debug and maps.debug progs.debug and maps.debug are debugging purposes only. So I think we can handle it later.

Signature · 2 lines
Regards
Yafang
#22 lore raw
Quoted 40 lines · Yafang Shao
Quoted 30 lines · Toke Høiland-Jørgensen
Quoted 27 lines · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Without CAP_SYS_ADMIN, the only way to get the information of a bpf object which is not created by the process itself is with SCM_RIGHTS, that requires each processes which created bpf object has to implement a unix domain socket to share the fd of a bpf object between different processes, that is really trivial and troublesome.

Hence we need a better mechanism to get bpf object info without CAP_SYS_ADMIN.

BPF namespace is introduced in this patchset with an attempt to remove the CAP_SYS_ADMIN requirement. The user can create bpf map, prog and link in a specific bpf namespace, then these bpf objects will not be visible to the users in a different bpf namespace. But these bpf objects are visible to its parent bpf namespace, so the sys admin can still iterate and inspect them.

Quoted 8 lines
Quoted 4 lines

BPF namespace is similar to PID namespace, and the bpf objects are similar to tasks, so BPF namespace is very easy to understand. These patchset only implements BPF namespace for bpf map, prog and link. In the future we may extend it to other bpf objects like btf, bpffs and etc.

May? I think we should cover all of the existing BPF objects from the beginning here, or we may miss important interactions that will invalidate the whole idea.

Quoted 8 lines

This patchset is intended to address iterating bpf IDs and converting IDs to FDs. To be more specific, it covers BPF_{PROG,MAP,LINK}_GET_NEXT_ID and BPF_{PROG,MAP,LINK}_GET_FD_BY_ID. It should also include BPF_BTF_GET_NEXT_ID and BPF_BTF_GET_FD_BY_ID, but I don't implement it because I find we can do more wrt BTF, for example, if we can expose a small amount of BTFs in the vmlinux to non-root bpf namespace.
But, yes, I should implement BTF ID in this patchset.

Right, as you can see by my comment on that patch, not including the btf id is a tad confusing, so yeah, better include that.

Quoted 19 lines
Quoted 6 lines

In particular, I'm a little worried about the
interaction between namespaces and bpffs; what happens if you're in a bpf namespace and you try to read a BPF object from a bpffs that belongs to a different namespace? Does the operation fail? Is the object hidden entirely? Something else?

bpffs is a different topic and it can be implemented in later patchsets. bpffs has its own specific problem even without the bpf namespace.
1. The user can always get the information of a bpf object through its corresponding pinned file.
In our practice, different container users have different bpffs, and we allow the container user to bind-mount its bpffs only, so others' bpffs are invisible.
To make it better with the bpf namespace, I think we can fail the operation if the pinned file doesn't belong to its bpf namespace. That said, we will add pinned bpf files into the bpf namespace in the next step.

Quoted 3 lines

2. The user can always iterate bpf objects through progs.debug and maps.debug progs.debug and maps.debug are debugging purposes only. So I think we can handle it later.

Well, I disagree. Working out these issues with bpffs is an important aspect to get a consistent API, and handwaving it away risks merging something that will turn out to not be workable further down the line at which point we can't change it.

-Toke

#23 lore raw
Quoted 74 lines · Toke Høiland-Jørgensen
Quoted 40 lines · Yafang Shao
Quoted 30 lines · Toke Høiland-Jørgensen
Quoted 27 lines · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Without CAP_SYS_ADMIN, the only way to get the information of a bpf object which is not created by the process itself is with SCM_RIGHTS, that requires each processes which created bpf object has to implement a unix domain socket to share the fd of a bpf object between different processes, that is really trivial and troublesome.

Hence we need a better mechanism to get bpf object info without CAP_SYS_ADMIN.

BPF namespace is introduced in this patchset with an attempt to remove the CAP_SYS_ADMIN requirement. The user can create bpf map, prog and link in a specific bpf namespace, then these bpf objects will not be visible to the users in a different bpf namespace. But these bpf objects are visible to its parent bpf namespace, so the sys admin can still iterate and inspect them.

Quoted 8 lines
Quoted 4 lines

BPF namespace is similar to PID namespace, and the bpf objects are similar to tasks, so BPF namespace is very easy to understand. These patchset only implements BPF namespace for bpf map, prog and link. In the future we may extend it to other bpf objects like btf, bpffs and etc.

May? I think we should cover all of the existing BPF objects from the beginning here, or we may miss important interactions that will invalidate the whole idea.

Quoted 8 lines

This patchset is intended to address iterating bpf IDs and converting IDs to FDs. To be more specific, it covers BPF_{PROG,MAP,LINK}_GET_NEXT_ID and BPF_{PROG,MAP,LINK}_GET_FD_BY_ID. It should also include BPF_BTF_GET_NEXT_ID and BPF_BTF_GET_FD_BY_ID, but I don't implement it because I find we can do more wrt BTF, for example, if we can expose a small amount of BTFs in the vmlinux to non-root bpf namespace.
But, yes, I should implement BTF ID in this patchset.

Right, as you can see by my comment on that patch, not including the btf id is a tad confusing, so yeah, better include that.

Quoted 19 lines
Quoted 6 lines

In particular, I'm a little worried about the
interaction between namespaces and bpffs; what happens if you're in a bpf namespace and you try to read a BPF object from a bpffs that belongs to a different namespace? Does the operation fail? Is the object hidden entirely? Something else?

bpffs is a different topic and it can be implemented in later patchsets. bpffs has its own specific problem even without the bpf namespace.
1. The user can always get the information of a bpf object through its corresponding pinned file.
In our practice, different container users have different bpffs, and we allow the container user to bind-mount its bpffs only, so others' bpffs are invisible.
To make it better with the bpf namespace, I think we can fail the operation if the pinned file doesn't belong to its bpf namespace. That said, we will add pinned bpf files into the bpf namespace in the next step.

Quoted 9 lines
Quoted 3 lines

2. The user can always iterate bpf objects through progs.debug and maps.debug progs.debug and maps.debug are debugging purposes only. So I think we can handle it later.

Well, I disagree. Working out these issues with bpffs is an important aspect to get a consistent API, and handwaving it away risks merging something that will turn out to not be workable further down the line at which point we can't change it.

Sure, I will include bpffs in the next version.

Signature · 2 lines
Regards
Yafang
#24 lore raw
In reply to Yafang Shao
Quoted 19 lines · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Without CAP_SYS_ADMIN, the only way to get the information of a bpf object which is not created by the process itself is with SCM_RIGHTS, that requires each processes which created bpf object has to implement a unix domain socket to share the fd of a bpf object between different processes, that is really trivial and troublesome.

Hence we need a better mechanism to get bpf object info without CAP_SYS_ADMIN.

[..]

Quoted 6 lines

BPF namespace is introduced in this patchset with an attempt to remove the CAP_SYS_ADMIN requirement. The user can create bpf map, prog and link in a specific bpf namespace, then these bpf objects will not be visible to the users in a different bpf namespace. But these bpf objects are visible to its parent bpf namespace, so the sys admin can still iterate and inspect them.

Does it essentially mean unpriv bpf? Can I, as a non-root, create a new bpf namespace and start loading/attaching progs?
Maybe add a paragraph about now vs whatever you're proposing. Otherwise it's not very clear what's the security story. (haven't looked at the whole series, so maybe it's answered somewhere else?)

Quoted 62 lines

BPF namespace is similar to PID namespace, and the bpf objects are similar to tasks, so BPF namespace is very easy to understand. These patchset only implements BPF namespace for bpf map, prog and link. In the future we may extend it to other bpf objects like btf, bpffs and etc. For example, we can allow some of the BTF objects to be used in non-init bpf namespace, then the container user can only trace the processes running in his container, but can't get the information of tasks running in other containers.

A simple example is introduced into selftests/bpf on how to use the bpf namespace.

Putting bpf map, prog and link into bpf namespace is the first step. Let's start with it.

[1]. https://lore.kernel.org/bpf/[email protected]/

Yafang Shao (13):
fork: New clone3 flag for BPF namespace
proc_ns: Extend the field type in struct proc_ns_operations to long
bpf: Implement bpf namespace
bpf: No need to check if id is 0
bpf: Make bpf objects id have the same alloc and free pattern
bpf: Helpers to alloc and free object id in bpf namespace
bpf: Add bpf helper to get bpf object id
bpf: Alloc and free bpf_map id in bpf namespace
bpf: Alloc and free bpf_prog id in bpf namespace
bpf: Alloc and free bpf_link id in bpf namespace
bpf: Allow iterating bpf objects with CAP_BPF in bpf namespace
bpf: Use bpf_idr_lock array instead
selftests/bpf: Add selftest for bpf namespace

  fs/proc/namespaces.c                      |   4 +
  include/linux/bpf.h                       |   9 +-
  include/linux/bpf_namespace.h             |  88 ++++++++++
  include/linux/nsproxy.h                   |   4 +
  include/linux/proc_ns.h                   |   3 +-
  include/linux/user_namespace.h            |   1 +
  include/uapi/linux/bpf.h                  |   7 +
  include/uapi/linux/sched.h                |   1 +
  kernel/bpf/Makefile                       |   1 +
  kernel/bpf/bpf_namespace.c                | 283  
++++++++++++++++++++++++++++++
  kernel/bpf/offload.c                      |  16 +-
  kernel/bpf/syscall.c                      | 262  
++++++++++-----------------
  kernel/bpf/task_iter.c                    |  12 ++
  kernel/fork.c                             |   5 +-
  kernel/nsproxy.c                          |  19 +-
  kernel/trace/bpf_trace.c                  |   2 +
  kernel/ucount.c                           |   1 +
  tools/bpf/bpftool/skeleton/pid_iter.bpf.c |  13 +-
  tools/include/uapi/linux/bpf.h            |   7 +
  tools/testing/selftests/bpf/Makefile      |   3 +-
  tools/testing/selftests/bpf/test_bpfns.c  |  76 ++++++++
  21 files changed, 637 insertions(+), 180 deletions(-)

create mode 100644 include/linux/bpf_namespace.h create mode 100644 kernel/bpf/bpf_namespace.c create mode 100644 tools/testing/selftests/bpf/test_bpfns.c

--
1.8.3.1

#25 lore raw
Quoted 24 lines · Stanislav Fomichev
Quoted 19 lines · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Without CAP_SYS_ADMIN, the only way to get the information of a bpf object which is not created by the process itself is with SCM_RIGHTS, that requires each processes which created bpf object has to implement a unix domain socket to share the fd of a bpf object between different processes, that is really trivial and troublesome.

Hence we need a better mechanism to get bpf object info without CAP_SYS_ADMIN.

[..]

Quoted 8 lines
Quoted 6 lines

BPF namespace is introduced in this patchset with an attempt to remove the CAP_SYS_ADMIN requirement. The user can create bpf map, prog and link in a specific bpf namespace, then these bpf objects will not be visible to the users in a different bpf namespace. But these bpf objects are visible to its parent bpf namespace, so the sys admin can still iterate and inspect them.

Does it essentially mean unpriv bpf?

Right. With CAP_BPF and some other CAPs enabled.

Quoted 2 lines

Can I, as a non-root, create
a new bpf namespace and start loading/attaching progs?

No, you can't create a new bpf namespace as a non-root, see also copy_namespaces().
In the container environment, new namespaces are always created by containered, which is started by root.

Quoted 1 line

Maybe add a paragraph about now vs whatever you're proposing.

What I'm proposing in this patchset is to put bpf objects (map, prog, link, and btf) into the bpf namespace. Next step I will put bpffs into the bpf namespace as well.
That said, I'm trying to put all the objects created in bpf into the bpf namespace. Below is a simple paragraph to illustrate it.

Regarding the unpriv user with CAP_BPF enabled,
Now | Future
------------------------------------------------------------------------
Iterate his BPF IDs | N | Y |
Iterate others' BPF IDs | N | N |
Convert his BPF IDs to FDs | N | Y |

Convert others' BPF IDs to FDs            | N   |  N  |
Get others' object info from pinned file  | Y(*) | N  |

------------------------------------------------------------------------

(*) It can be improved by,
1). Different containers has different bpffs
2). Setting file permission
That's not perfect, for example, if one single user has two bpf
instances, but we don't want them to inspect each other.

Quoted 3 lines

Otherwise it's not very clear what's the security story. (haven't looked at the whole series, so maybe it's answered somewhere else?)

Signature · 2 lines
Regards
Yafang
#26 lore raw
Quoted 41 lines · Yafang Shao
Quoted 3 lines · Stanislav Fomichev
Quoted 1 line · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert

IDs

Quoted 2 lines
Quoted 2 lines

to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program,

but

Quoted 5 lines
Quoted 5 lines

also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs

in a

Quoted 4 lines
Quoted 4 lines

generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Without CAP_SYS_ADMIN, the only way to get the information of a bpf

object

Quoted 2 lines
Quoted 2 lines

which is not created by the process itself is with SCM_RIGHTS, that requires each processes which created bpf object has to implement a

unix

Quoted 8 lines
Quoted 5 lines

domain socket to share the fd of a bpf object between different processes, that is really trivial and troublesome.

Hence we need a better mechanism to get bpf object info without CAP_SYS_ADMIN.

[..]

Quoted 8 lines
Quoted 6 lines

BPF namespace is introduced in this patchset with an attempt to remove the CAP_SYS_ADMIN requirement. The user can create bpf map, prog and link in a specific bpf namespace, then these bpf objects will not be visible to the users in a different bpf namespace. But these bpf objects are visible to its parent bpf namespace, so the sys admin can still iterate and inspect them.

Does it essentially mean unpriv bpf?

Right. With CAP_BPF and some other CAPs enabled.

Quoted 7 lines
Quoted 2 lines

Can I, as a non-root, create
a new bpf namespace and start loading/attaching progs?

No, you can't create a new bpf namespace as a non-root, see also copy_namespaces().
In the container environment, new namespaces are always created by containered, which is started by root.

Are you talking about "if (!ns_capable(user_ns, CAP_SYS_ADMIN))" part from copy_namespaces? Isn't it trivially bypassed with a new user namespace?

IIUC, I can create a new user namespace which gives me CAP_SYS_ADMIN in this particular user-ns. Then I can go on and create a new bpf namespace (with CAP_BPF) and go wild? I won't see anything from the other namespaces, but I'll be able to load/attach bpf programs?

Quoted 16 lines
Quoted 1 line

Maybe add a paragraph about now vs whatever you're proposing.

What I'm proposing in this patchset is to put bpf objects (map, prog, link, and btf) into the bpf namespace. Next step I will put bpffs into the bpf namespace as well.
That said, I'm trying to put all the objects created in bpf into the bpf namespace. Below is a simple paragraph to illustrate it.

Regarding the unpriv user with CAP_BPF enabled,
Now | Future
------------------------------------------------------------------------
Iterate his BPF IDs | N | Y |
Iterate others' BPF IDs | N | N |
Convert his BPF IDs to FDs | N | Y |

Convert others' BPF IDs to FDs            | N   |  N  |
Get others' object info from pinned file  | Y(*) | N  |
Quoted 7 lines

------------------------------------------------------------------------

(*) It can be improved by,
1). Different containers has different bpffs
2). Setting file permission
That's not perfect, for example, if one single user has two bpf
instances, but we don't want them to inspect each other.

I think the question here is what happens to the existing capable(CAP_BPF) checks? Do they become ns_capable(CAP_BPF) eventually?

And if not, I don't think it integrates well with the user namespaces?

Quoted 9 lines
Quoted 2 lines

Otherwise it's not very clear what's the security story. (haven't looked at the whole series, so maybe it's answered somewhere

else?)

Quoted 1 line

--
Regards
Yafang

#27 lore raw
Quoted 51 lines · Stanislav Fomichev
Quoted 41 lines · Yafang Shao
Quoted 3 lines · Stanislav Fomichev
Quoted 1 line · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert

IDs

Quoted 2 lines
Quoted 2 lines

to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program,

but

Quoted 5 lines
Quoted 5 lines

also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs

in a

Quoted 4 lines
Quoted 4 lines

generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Without CAP_SYS_ADMIN, the only way to get the information of a bpf

object

Quoted 2 lines
Quoted 2 lines

which is not created by the process itself is with SCM_RIGHTS, that requires each processes which created bpf object has to implement a

unix

Quoted 8 lines
Quoted 5 lines

domain socket to share the fd of a bpf object between different processes, that is really trivial and troublesome.

Hence we need a better mechanism to get bpf object info without CAP_SYS_ADMIN.

[..]

Quoted 8 lines
Quoted 6 lines

BPF namespace is introduced in this patchset with an attempt to remove the CAP_SYS_ADMIN requirement. The user can create bpf map, prog and link in a specific bpf namespace, then these bpf objects will not be visible to the users in a different bpf namespace. But these bpf objects are visible to its parent bpf namespace, so the sys admin can still iterate and inspect them.

Does it essentially mean unpriv bpf?

Right. With CAP_BPF and some other CAPs enabled.

Quoted 7 lines
Quoted 2 lines

Can I, as a non-root, create
a new bpf namespace and start loading/attaching progs?

No, you can't create a new bpf namespace as a non-root, see also copy_namespaces().
In the container environment, new namespaces are always created by containered, which is started by root.

Quoted 9 lines

Are you talking about "if (!ns_capable(user_ns, CAP_SYS_ADMIN))" part from copy_namespaces? Isn't it trivially bypassed with a new user namespace?

IIUC, I can create a new user namespace which gives me CAP_SYS_ADMIN in this particular user-ns. Then I can go on and create a new bpf namespace (with CAP_BPF) and go wild? I won't see anything from the other namespaces, but I'll be able to load/attach bpf programs?

I don't think so. If you create a new userspace, and give the process the CAP_BPF or CAP_SYS_ADMIN in this new user namespace but not the initial namespace, you can't do that. Because currently only CAP_BPF or CAP_SYS_ADMIN in the init user namespace can load/attach bpf programs.

Quoted 18 lines
Quoted 17 lines
Quoted 1 line

Maybe add a paragraph about now vs whatever you're proposing.

What I'm proposing in this patchset is to put bpf objects (map, prog, link, and btf) into the bpf namespace. Next step I will put bpffs into the bpf namespace as well.
That said, I'm trying to put all the objects created in bpf into the bpf namespace. Below is a simple paragraph to illustrate it.

Regarding the unpriv user with CAP_BPF enabled,
Now | Future
------------------------------------------------------------------------
Iterate his BPF IDs | N | Y |
Iterate others' BPF IDs | N | N |
Convert his BPF IDs to FDs | N | Y |

Convert others' BPF IDs to FDs            | N   |  N  |
Get others' object info from pinned file  | Y(*) | N  |

------------------------------------------------------------------------

Quoted 9 lines
Quoted 5 lines

(*) It can be improved by,
1). Different containers has different bpffs
2). Setting file permission
That's not perfect, for example, if one single user has two bpf
instances, but we don't want them to inspect each other.

I think the question here is what happens to the existing capable(CAP_BPF) checks? Do they become ns_capable(CAP_BPF) eventually?

They won't become ns_capable(CAP_BPF). If it becomes ns_capable(CAP_BPF), it will really go wild then.

Quoted 2 lines

And if not, I don't think it integrates well with the user namespaces?

IIUC, it is the CAP_BPF which doesn't integrate with the user namespaces, right?

Signature · 2 lines
Regards
Yafang
#28 lore raw
Quoted 97 lines · Yafang Shao
Quoted 51 lines · Stanislav Fomichev
Quoted 41 lines · Yafang Shao
Quoted 3 lines · Stanislav Fomichev
Quoted 1 line · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert

IDs

Quoted 2 lines
Quoted 2 lines

to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program,

but

Quoted 5 lines
Quoted 5 lines

also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs

in a

Quoted 4 lines
Quoted 4 lines

generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Without CAP_SYS_ADMIN, the only way to get the information of a bpf

object

Quoted 2 lines
Quoted 2 lines

which is not created by the process itself is with SCM_RIGHTS, that requires each processes which created bpf object has to implement a

unix

Quoted 8 lines
Quoted 5 lines

domain socket to share the fd of a bpf object between different processes, that is really trivial and troublesome.

Hence we need a better mechanism to get bpf object info without CAP_SYS_ADMIN.

[..]

Quoted 8 lines
Quoted 6 lines

BPF namespace is introduced in this patchset with an attempt to remove the CAP_SYS_ADMIN requirement. The user can create bpf map, prog and link in a specific bpf namespace, then these bpf objects will not be visible to the users in a different bpf namespace. But these bpf objects are visible to its parent bpf namespace, so the sys admin can still iterate and inspect them.

Does it essentially mean unpriv bpf?

Right. With CAP_BPF and some other CAPs enabled.

Quoted 7 lines
Quoted 2 lines

Can I, as a non-root, create
a new bpf namespace and start loading/attaching progs?

No, you can't create a new bpf namespace as a non-root, see also copy_namespaces().
In the container environment, new namespaces are always created by containered, which is started by root.

Quoted 9 lines

Are you talking about "if (!ns_capable(user_ns, CAP_SYS_ADMIN))" part from copy_namespaces? Isn't it trivially bypassed with a new user namespace?

IIUC, I can create a new user namespace which gives me CAP_SYS_ADMIN in this particular user-ns. Then I can go on and create a new bpf namespace (with CAP_BPF) and go wild? I won't see anything from the other namespaces, but I'll be able to load/attach bpf programs?

I don't think so. If you create a new userspace, and give the process the CAP_BPF or CAP_SYS_ADMIN in this new user namespace but not the initial namespace, you can't do that. Because currently only CAP_BPF or CAP_SYS_ADMIN in the init user namespace can load/attach bpf programs.

Quoted 18 lines
Quoted 17 lines
Quoted 1 line

Maybe add a paragraph about now vs whatever you're proposing.

What I'm proposing in this patchset is to put bpf objects (map, prog, link, and btf) into the bpf namespace. Next step I will put bpffs into the bpf namespace as well.
That said, I'm trying to put all the objects created in bpf into the bpf namespace. Below is a simple paragraph to illustrate it.

Regarding the unpriv user with CAP_BPF enabled,
Now | Future
------------------------------------------------------------------------
Iterate his BPF IDs | N | Y |
Iterate others' BPF IDs | N | N |
Convert his BPF IDs to FDs | N | Y |

Convert others' BPF IDs to FDs            | N   |  N  |
Get others' object info from pinned file  | Y(*) | N  |

------------------------------------------------------------------------

Quoted 9 lines
Quoted 5 lines

(*) It can be improved by,
1). Different containers has different bpffs
2). Setting file permission
That's not perfect, for example, if one single user has two bpf
instances, but we don't want them to inspect each other.

I think the question here is what happens to the existing capable(CAP_BPF) checks? Do they become ns_capable(CAP_BPF) eventually?

Quoted 8 lines

They won't become ns_capable(CAP_BPF). If it becomes ns_capable(CAP_BPF), it will really go wild then.

Quoted 2 lines

And if not, I don't think it integrates well with the user namespaces?

IIUC, it is the CAP_BPF which doesn't integrate with the user namespaces, right?

Yeah. And it's probably fine if we don't, I just wanted to see some explanation on the long-term plan.
If the purpose is to have a bpf namespace and use it for pure isolation purposes, let's state it clearly in the cover letter. Otherwise it's not clear whether it's only about isolation or potentially allowing CAP_BPF in user namespaces.
Maybe clone(CLONE_NEWBPF|CLONE_NEWUSER) should return an explicit error? (or maybe it already does, haven't looked at the patches)

One other question I have is: should init bpf namespace see everything? Otherwise it might be hard to chase down the namespaces that loaded some BPF program...

Quoted 3 lines

--
Regards
Yafang

#29 lore raw
Quoted 105 lines · Stanislav Fomichev
Quoted 103 lines · Yafang Shao
Quoted 51 lines · Stanislav Fomichev
Quoted 41 lines · Yafang Shao
Quoted 3 lines · Stanislav Fomichev
Quoted 1 line · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert

IDs

Quoted 2 lines
Quoted 2 lines

to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program,

but

Quoted 5 lines
Quoted 5 lines

also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs

in a

Quoted 4 lines
Quoted 4 lines

generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Without CAP_SYS_ADMIN, the only way to get the information of a bpf

object

Quoted 2 lines
Quoted 2 lines

which is not created by the process itself is with SCM_RIGHTS, that requires each processes which created bpf object has to implement a

unix

Quoted 8 lines
Quoted 5 lines

domain socket to share the fd of a bpf object between different processes, that is really trivial and troublesome.

Hence we need a better mechanism to get bpf object info without CAP_SYS_ADMIN.

[..]

Quoted 8 lines
Quoted 6 lines

BPF namespace is introduced in this patchset with an attempt to remove the CAP_SYS_ADMIN requirement. The user can create bpf map, prog and link in a specific bpf namespace, then these bpf objects will not be visible to the users in a different bpf namespace. But these bpf objects are visible to its parent bpf namespace, so the sys admin can still iterate and inspect them.

Does it essentially mean unpriv bpf?

Right. With CAP_BPF and some other CAPs enabled.

Quoted 7 lines
Quoted 2 lines

Can I, as a non-root, create
a new bpf namespace and start loading/attaching progs?

No, you can't create a new bpf namespace as a non-root, see also copy_namespaces().
In the container environment, new namespaces are always created by containered, which is started by root.

Quoted 9 lines

Are you talking about "if (!ns_capable(user_ns, CAP_SYS_ADMIN))" part from copy_namespaces? Isn't it trivially bypassed with a new user namespace?

IIUC, I can create a new user namespace which gives me CAP_SYS_ADMIN in this particular user-ns. Then I can go on and create a new bpf namespace (with CAP_BPF) and go wild? I won't see anything from the other namespaces, but I'll be able to load/attach bpf programs?

I don't think so. If you create a new userspace, and give the process the CAP_BPF or CAP_SYS_ADMIN in this new user namespace but not the initial namespace, you can't do that. Because currently only CAP_BPF or CAP_SYS_ADMIN in the init user namespace can load/attach bpf programs.

Quoted 18 lines
Quoted 17 lines
Quoted 1 line

Maybe add a paragraph about now vs whatever you're proposing.

What I'm proposing in this patchset is to put bpf objects (map, prog, link, and btf) into the bpf namespace. Next step I will put bpffs into the bpf namespace as well.
That said, I'm trying to put all the objects created in bpf into the bpf namespace. Below is a simple paragraph to illustrate it.

Regarding the unpriv user with CAP_BPF enabled,
Now | Future
------------------------------------------------------------------------
Iterate his BPF IDs | N | Y |
Iterate others' BPF IDs | N | N |
Convert his BPF IDs to FDs | N | Y |

Convert others' BPF IDs to FDs            | N   |  N  |
Get others' object info from pinned file  | Y(*) | N  |

------------------------------------------------------------------------

Quoted 9 lines
Quoted 5 lines

(*) It can be improved by,
1). Different containers has different bpffs
2). Setting file permission
That's not perfect, for example, if one single user has two bpf
instances, but we don't want them to inspect each other.

I think the question here is what happens to the existing capable(CAP_BPF) checks? Do they become ns_capable(CAP_BPF) eventually?

They won't become ns_capable(CAP_BPF). If it becomes ns_capable(CAP_BPF), it will really go wild then.

Quoted 2 lines

And if not, I don't think it integrates well with the user namespaces?

Quoted 7 lines
Quoted 2 lines

IIUC, it is the CAP_BPF which doesn't integrate with the user namespaces, right?

Yeah. And it's probably fine if we don't, I just wanted to see some explanation on the long-term plan.
If the purpose is to have a bpf namespace and use it for pure isolation purposes, let's state it clearly in the cover letter.

Will add it.

Quoted 5 lines

Otherwise it's not clear whether it's only about isolation or potentially allowing CAP_BPF in user namespaces.
Maybe clone(CLONE_NEWBPF|CLONE_NEWUSER) should return an explicit error? (or maybe it already does, haven't looked at the patches)

Good suggestion. It should return an error. I will change it in the next version.

Quoted 3 lines

One other question I have is: should init bpf namespace see everything? Otherwise it might be hard to chase down the namespaces that loaded some BPF program...

Right, the init bpf namespace will see everything.

Signature · 2 lines
Regards
Yafang
maintainer
#30 lore raw
In reply to Yafang Shao
Quoted 11 lines · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Agreed that it is important to enable tools like bpftool without CAP_SYS_ADMIN. However, I am not sure whether we need a new namespace for this. Can we reuse some existing namespace for this?

If we do need a new namespace, maybe we should share some effort with tracer namespace proposal [1]?

Thanks,
Song

[1] https://lpc.events/event/16/contributions/1237/

#31 lore raw
Quoted 14 lines · Song Liu
Quoted 11 lines · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Quoted 4 lines

Agreed that it is important to enable tools like bpftool without CAP_SYS_ADMIN. However, I am not sure whether we need a new namespace for this. Can we reuse some existing namespace for this?

It seems we can't.

Quoted 3 lines

If we do need a new namespace, maybe we should share some effort with tracer namespace proposal [1]?

Thanks for your information. I will learn the tracer namespace first and try to analyze how to cooperate with it.

Quoted 4 lines

Thanks,
Song

[1] https://lpc.events/event/16/contributions/1237/

Signature · 2 lines
Regards
Yafang
#32 lore raw
Quoted 15 lines · Yafang Shao
Quoted 14 lines · Song Liu
Quoted 11 lines · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Quoted 6 lines
Quoted 4 lines

Agreed that it is important to enable tools like bpftool without CAP_SYS_ADMIN. However, I am not sure whether we need a new namespace for this. Can we reuse some existing namespace for this?

It seems we can't.

Yafang,

It's a Nack.

The only thing you've been trying to "solve" with bpf namespace is to allow 'bpftool prog show' iterate progs in the "namespace" without CAP_SYS_ADMIN. The concept of bpf namespace is not even close to be thought through. Others pointed out the gaps in the design. Like bpffs. There are plenty. Please do not send patches like this in the future.
You need to start with describing the problem you want to solve, then propose _several_ solutions, describe their pros and cons, solicit feedback, present at the conferences (like LSFMMBPF or LPC), and when the community agrees that 1. problem is worth solving,
2. the solution makes sense, only then work on patches.

"In container environment, some users want to run bpf programs in their containers." is something Song brought up at LSFMMBPF a year ago.
At that meeting most of the folks agreed that there is a need to run bpf in containers and make sure that the effect of bpf prog is limited to a container. This new namespace that creates virtual IDs for progs and maps doesn't come close in solving this task.

#33 lore raw
Quoted 24 lines · Alexei Starovoitov
Quoted 15 lines · Yafang Shao
Quoted 14 lines · Song Liu
Quoted 11 lines · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Quoted 6 lines
Quoted 4 lines

Agreed that it is important to enable tools like bpftool without CAP_SYS_ADMIN. However, I am not sure whether we need a new namespace for this. Can we reuse some existing namespace for this?

It seems we can't.

Quoted 7 lines

Yafang,

It's a Nack.

The only thing you've been trying to "solve" with bpf namespace is to allow 'bpftool prog show' iterate progs in the "namespace" without CAP_SYS_ADMIN. The concept of bpf namespace is not even close to be thought through.

Right, it is more likely a PoC in its current state.

Quoted 2 lines

Others pointed out the gaps in the design. Like bpffs. There are plenty. Please do not send patches like this in the future.

The reason I sent it with an early state is that I want to get some early feedback from the community ahead of the LSF/MM/BPF workshop, then I can improve it based on these feedbacks and present it more specifically at the workshop. Then the discussion will be more effective.

Quoted 6 lines

You need to start with describing the problem you want to solve, then propose _several_ solutions, describe their pros and cons, solicit feedback, present at the conferences (like LSFMMBPF or LPC), and when the community agrees that 1. problem is worth solving,
2. the solution makes sense, only then work on patches.

I would like to give a short discussion on the BPF namespace if everything goes fine.

Quoted 6 lines

"In container environment, some users want to run bpf programs in their containers." is something Song brought up at LSFMMBPF a year ago.
At that meeting most of the folks agreed that there is a need to run bpf in containers and make sure that the effect of bpf prog is limited to a container. This new namespace that creates virtual IDs for progs and maps doesn't come close in solving this task.

Currently in our production environment, all the containers running bpf programs are privileged, that is risky. So actually the goal of the BPF namespace is to make them (or part of them) non-privileged. But some of the abilities of these bpf programs will be lost in this procedure, like the debug-bility with bpftool, so we need to fix it. Agree with you that this goal is far from making bpf programs safely running in a container environment.

Signature · 2 lines
Regards
Yafang
#34 lore raw
Quoted 52 lines · Yafang Shao
Quoted 24 lines · Alexei Starovoitov
Quoted 15 lines · Yafang Shao
Quoted 14 lines · Song Liu
Quoted 11 lines · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Quoted 6 lines
Quoted 4 lines

Agreed that it is important to enable tools like bpftool without CAP_SYS_ADMIN. However, I am not sure whether we need a new namespace for this. Can we reuse some existing namespace for this?

It seems we can't.

Quoted 7 lines

Yafang,

It's a Nack.

The only thing you've been trying to "solve" with bpf namespace is to allow 'bpftool prog show' iterate progs in the "namespace" without CAP_SYS_ADMIN. The concept of bpf namespace is not even close to be thought through.

Right, it is more likely a PoC in its current state.

Quoted 2 lines

Others pointed out the gaps in the design. Like bpffs. There are plenty. Please do not send patches like this in the future.

The reason I sent it with an early state is that I want to get some early feedback from the community ahead of the LSF/MM/BPF workshop, then I can improve it based on these feedbacks and present it more specifically at the workshop. Then the discussion will be more effective.

Quoted 6 lines

You need to start with describing the problem you want to solve, then propose _several_ solutions, describe their pros and cons, solicit feedback, present at the conferences (like LSFMMBPF or LPC), and when the community agrees that 1. problem is worth solving,
2. the solution makes sense, only then work on patches.

Quoted 2 lines

I would like to give a short discussion on the BPF namespace if everything goes fine.

Not in this shape of BPF namespace as done in this patch set. We've talked about BPF namespace in the past. This is not it.

Quoted 7 lines
Quoted 6 lines

"In container environment, some users want to run bpf programs in their containers." is something Song brought up at LSFMMBPF a year ago.
At that meeting most of the folks agreed that there is a need to run bpf in containers and make sure that the effect of bpf prog is limited to a container. This new namespace that creates virtual IDs for progs and maps doesn't come close in solving this task.

Quoted 7 lines

Currently in our production environment, all the containers running bpf programs are privileged, that is risky. So actually the goal of the BPF namespace is to make them (or part of them) non-privileged. But some of the abilities of these bpf programs will be lost in this procedure, like the debug-bility with bpftool, so we need to fix it. Agree with you that this goal is far from making bpf programs safely running in a container environment.

I disagree that allowing admin to run bpftool without sudo is a task worth solving. The visibility of bpf progs in a container is a different task. Without doing any kernel changes we can add a flag to bpftool to let 'bpftool prog show' list progs that were loaded by processes in the same cgroup. bpftool already does prog->pid mapping with bpf iterators.
It can filter by cgroup just as well.

#35 lore raw
Quoted 75 lines · Alexei Starovoitov
Quoted 52 lines · Yafang Shao
Quoted 24 lines · Alexei Starovoitov
Quoted 15 lines · Yafang Shao
Quoted 14 lines · Song Liu
Quoted 11 lines · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Quoted 6 lines
Quoted 4 lines

Agreed that it is important to enable tools like bpftool without CAP_SYS_ADMIN. However, I am not sure whether we need a new namespace for this. Can we reuse some existing namespace for this?

It seems we can't.

Quoted 7 lines

Yafang,

It's a Nack.

The only thing you've been trying to "solve" with bpf namespace is to allow 'bpftool prog show' iterate progs in the "namespace" without CAP_SYS_ADMIN. The concept of bpf namespace is not even close to be thought through.

Right, it is more likely a PoC in its current state.

Quoted 2 lines

Others pointed out the gaps in the design. Like bpffs. There are plenty. Please do not send patches like this in the future.

The reason I sent it with an early state is that I want to get some early feedback from the community ahead of the LSF/MM/BPF workshop, then I can improve it based on these feedbacks and present it more specifically at the workshop. Then the discussion will be more effective.

Quoted 6 lines

You need to start with describing the problem you want to solve, then propose _several_ solutions, describe their pros and cons, solicit feedback, present at the conferences (like LSFMMBPF or LPC), and when the community agrees that 1. problem is worth solving,
2. the solution makes sense, only then work on patches.

Quoted 2 lines

I would like to give a short discussion on the BPF namespace if everything goes fine.

Not in this shape of BPF namespace as done in this patch set. We've talked about BPF namespace in the past. This is not it.

Quoted 7 lines
Quoted 6 lines

"In container environment, some users want to run bpf programs in their containers." is something Song brought up at LSFMMBPF a year ago.
At that meeting most of the folks agreed that there is a need to run bpf in containers and make sure that the effect of bpf prog is limited to a container. This new namespace that creates virtual IDs for progs and maps doesn't come close in solving this task.

Quoted 7 lines

Currently in our production environment, all the containers running bpf programs are privileged, that is risky. So actually the goal of the BPF namespace is to make them (or part of them) non-privileged. But some of the abilities of these bpf programs will be lost in this procedure, like the debug-bility with bpftool, so we need to fix it. Agree with you that this goal is far from making bpf programs safely running in a container environment.

Quoted 6 lines

I disagree that allowing admin to run bpftool without sudo is a task worth solving. The visibility of bpf progs in a container is a different task. Without doing any kernel changes we can add a flag to bpftool to let 'bpftool prog show' list progs that were loaded by processes in the same cgroup. bpftool already does prog->pid mapping with bpf iterators.
It can filter by cgroup just as well.

IIUC, at least we need bellow change in the kernel,

kernel/bpf/syscall.c +0 -3
@@ -3705,9 +3705,6 @@ static int bpf_obj_get_next_id(const union bpf_attr *attr,
        if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
                return -EINVAL;
 
-       if (!capable(CAP_SYS_ADMIN))
-               return -EPERM;
-
        next_id++;
        spin_lock_bh(lock);
        if (!idr_get_next(idr, &next_id))

Because the container doesn't have CAP_SYS_ADMIN enabled, while they only have CAP_BPF and other required CAPs.

Another possible solution is that we run an agent in the host, and the user in the container who wants to get the bpf objects info in his container should send a request to this agent via unix domain socket. That is what we are doing now in our production environment. That said, each container has to run a client to get the bpf object fd. There are some downsides,
- It can't handle pinned bpf programs
For pinned programs, the user can get them from the pinned files
directly, so he can use bpftool in his case, only with some complaints.
- If the user attached the bpf prog, and then removed the pinned file, but didn't detach it.
That happened. But this error case can't be handled.
- There may be other corner cases that it can't fit.

There's a solution to improve it, but we also need to change the kernel. That is, we can use the wasted space btf->name.

kernel/bpf/btf.c +0 -0
index b7e5a55..59d73a3 100644
@@ -5542,6 +5542,8 @@ static struct btf *btf_parse(bpfptr_t btf_data,

u32 btf_data_size,

                err = -ENOMEM;
                goto errout;
        }
+       snprintf(btf->name, sizeof(btf->name), "%s-%d-%d", current->comm,
+                        current->pid, cgroup_id(task_cgroup(p, cpu_cgrp_id)));
        env->btf = btf;

        data = kvmalloc(btf_data_size, GFP_KERNEL | __GFP_NOWARN);
Signature · 2 lines
Regards
Yafang
#36 lore raw
Quoted 77 lines · Yafang Shao
Quoted 75 lines · Alexei Starovoitov
Quoted 52 lines · Yafang Shao
Quoted 24 lines · Alexei Starovoitov
Quoted 15 lines · Yafang Shao
Quoted 14 lines · Song Liu
Quoted 11 lines · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Quoted 6 lines
Quoted 4 lines

Agreed that it is important to enable tools like bpftool without CAP_SYS_ADMIN. However, I am not sure whether we need a new namespace for this. Can we reuse some existing namespace for this?

It seems we can't.

Quoted 7 lines

Yafang,

It's a Nack.

The only thing you've been trying to "solve" with bpf namespace is to allow 'bpftool prog show' iterate progs in the "namespace" without CAP_SYS_ADMIN. The concept of bpf namespace is not even close to be thought through.

Right, it is more likely a PoC in its current state.

Quoted 2 lines

Others pointed out the gaps in the design. Like bpffs. There are plenty. Please do not send patches like this in the future.

The reason I sent it with an early state is that I want to get some early feedback from the community ahead of the LSF/MM/BPF workshop, then I can improve it based on these feedbacks and present it more specifically at the workshop. Then the discussion will be more effective.

Quoted 6 lines

You need to start with describing the problem you want to solve, then propose _several_ solutions, describe their pros and cons, solicit feedback, present at the conferences (like LSFMMBPF or LPC), and when the community agrees that 1. problem is worth solving,
2. the solution makes sense, only then work on patches.

Quoted 2 lines

I would like to give a short discussion on the BPF namespace if everything goes fine.

Not in this shape of BPF namespace as done in this patch set. We've talked about BPF namespace in the past. This is not it.

Quoted 14 lines
Quoted 6 lines

"In container environment, some users want to run bpf programs in their containers." is something Song brought up at LSFMMBPF a year ago.
At that meeting most of the folks agreed that there is a need to run bpf in containers and make sure that the effect of bpf prog is limited to a container. This new namespace that creates virtual IDs for progs and maps doesn't come close in solving this task.

Currently in our production environment, all the containers running bpf programs are privileged, that is risky. So actually the goal of the BPF namespace is to make them (or part of them) non-privileged. But some of the abilities of these bpf programs will be lost in this procedure, like the debug-bility with bpftool, so we need to fix it. Agree with you that this goal is far from making bpf programs safely running in a container environment.

Quoted 8 lines
Quoted 6 lines

I disagree that allowing admin to run bpftool without sudo is a task worth solving. The visibility of bpf progs in a container is a different task. Without doing any kernel changes we can add a flag to bpftool to let 'bpftool prog show' list progs that were loaded by processes in the same cgroup. bpftool already does prog->pid mapping with bpf iterators.
It can filter by cgroup just as well.

IIUC, at least we need bellow change in the kernel,

No. The user should just 'sudo bpftool ...' instead.

Quoted 13 lines
kernel/bpf/syscall.c +0 -3
@@ -3705,9 +3705,6 @@ static int bpf_obj_get_next_id(const union bpf_attr *attr,
        if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
                return -EINVAL;
 
-       if (!capable(CAP_SYS_ADMIN))
-               return -EPERM;
-
        next_id++;
        spin_lock_bh(lock);
        if (!idr_get_next(idr, &next_id))
Quoted 8 lines

Because the container doesn't have CAP_SYS_ADMIN enabled, while they only have CAP_BPF and other required CAPs.

Another possible solution is that we run an agent in the host, and the user in the container who wants to get the bpf objects info in his container should send a request to this agent via unix domain socket. That is what we are doing now in our production environment. That said, each container has to run a client to get the bpf object fd.

None of such hacks are necessary. People that debug bpf setups with bpftool can always sudo.

Quoted 18 lines

There are some downsides,
- It can't handle pinned bpf programs
For pinned programs, the user can get them from the pinned files
directly, so he can use bpftool in his case, only with some complaints.
- If the user attached the bpf prog, and then removed the pinned file, but didn't detach it.
That happened. But this error case can't be handled.
- There may be other corner cases that it can't fit.

There's a solution to improve it, but we also need to change the kernel. That is, we can use the wasted space btf->name.

kernel/bpf/btf.c +0 -0
index b7e5a55..59d73a3 100644

kernel/bpf/btf.c static struct btf *btf_parse(bpfptr_t btf_data, · line 5547

Quoted 6 lines
kernel/bpf/btf.c +2 -0
@@ -5542,6 +5542,8 @@ static struct btf *btf_parse(bpfptr_t btf_data,
 u32 btf_data_size,
                err = -ENOMEM;
                goto errout;
        }
+       snprintf(btf->name, sizeof(btf->name), "%s-%d-%d", current->comm,
+                        current->pid, cgroup_id(task_cgroup(p, cpu_cgrp_id)));

Unnecessary.
comm, pid, cgroup can be printed by bpftool without changing the kernel.

#37 lore raw
Quoted 86 lines · Alexei Starovoitov
Quoted 84 lines · Yafang Shao
Quoted 81 lines · Alexei Starovoitov
Quoted 52 lines · Yafang Shao
Quoted 24 lines · Alexei Starovoitov
Quoted 15 lines · Yafang Shao
Quoted 14 lines · Song Liu
Quoted 11 lines · Yafang Shao

Currently only CAP_SYS_ADMIN can iterate BPF object IDs and convert IDs to FDs, that's intended for BPF's security model[1]. Not only does it prevent non-privilidged users from getting other users' bpf program, but also it prevents the user from iterating his own bpf objects.

In container environment, some users want to run bpf programs in their containers. These users can run their bpf programs under CAP_BPF and some other specific CAPs, but they can't inspect their bpf programs in a generic way. For example, the bpftool can't be used as it requires CAP_SYS_ADMIN. That is very inconvenient.

Quoted 6 lines
Quoted 4 lines

Agreed that it is important to enable tools like bpftool without CAP_SYS_ADMIN. However, I am not sure whether we need a new namespace for this. Can we reuse some existing namespace for this?

It seems we can't.

Quoted 7 lines

Yafang,

It's a Nack.

The only thing you've been trying to "solve" with bpf namespace is to allow 'bpftool prog show' iterate progs in the "namespace" without CAP_SYS_ADMIN. The concept of bpf namespace is not even close to be thought through.

Right, it is more likely a PoC in its current state.

Quoted 2 lines

Others pointed out the gaps in the design. Like bpffs. There are plenty. Please do not send patches like this in the future.

The reason I sent it with an early state is that I want to get some early feedback from the community ahead of the LSF/MM/BPF workshop, then I can improve it based on these feedbacks and present it more specifically at the workshop. Then the discussion will be more effective.

Quoted 6 lines

You need to start with describing the problem you want to solve, then propose _several_ solutions, describe their pros and cons, solicit feedback, present at the conferences (like LSFMMBPF or LPC), and when the community agrees that 1. problem is worth solving,
2. the solution makes sense, only then work on patches.

Quoted 2 lines

I would like to give a short discussion on the BPF namespace if everything goes fine.

Not in this shape of BPF namespace as done in this patch set. We've talked about BPF namespace in the past. This is not it.

Quoted 7 lines
Quoted 6 lines

"In container environment, some users want to run bpf programs in their containers." is something Song brought up at LSFMMBPF a year ago.
At that meeting most of the folks agreed that there is a need to run bpf in containers and make sure that the effect of bpf prog is limited to a container. This new namespace that creates virtual IDs for progs and maps doesn't come close in solving this task.

Quoted 7 lines

Currently in our production environment, all the containers running bpf programs are privileged, that is risky. So actually the goal of the BPF namespace is to make them (or part of them) non-privileged. But some of the abilities of these bpf programs will be lost in this procedure, like the debug-bility with bpftool, so we need to fix it. Agree with you that this goal is far from making bpf programs safely running in a container environment.

I disagree that allowing admin to run bpftool without sudo is a task worth solving. The visibility of bpf progs in a container is a different task. Without doing any kernel changes we can add a flag to bpftool to let 'bpftool prog show' list progs that were loaded by processes in the same cgroup. bpftool already does prog->pid mapping with bpf iterators.
It can filter by cgroup just as well.

Quoted 4 lines
Quoted 1 line

IIUC, at least we need bellow change in the kernel,

No. The user should just 'sudo bpftool ...' instead.

It seems that I didn't describe the issue clearly.
The container doesn't have CAP_SYS_ADMIN, but the CAP_SYS_ADMIN is required to run bpftool, so the bpftool running in the container can't get the ID of bpf objects or convert IDs to FDs. Is there something that I missed ?

Quoted 38 lines
Quoted 13 lines
kernel/bpf/syscall.c +0 -3
@@ -3705,9 +3705,6 @@ static int bpf_obj_get_next_id(const union bpf_attr *attr,
        if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
                return -EINVAL;
 
-       if (!capable(CAP_SYS_ADMIN))
-               return -EPERM;
-
        next_id++;
        spin_lock_bh(lock);
        if (!idr_get_next(idr, &next_id))
Quoted 8 lines

Because the container doesn't have CAP_SYS_ADMIN enabled, while they only have CAP_BPF and other required CAPs.

Another possible solution is that we run an agent in the host, and the user in the container who wants to get the bpf objects info in his container should send a request to this agent via unix domain socket. That is what we are doing now in our production environment. That said, each container has to run a client to get the bpf object fd.

None of such hacks are necessary. People that debug bpf setups with bpftool can always sudo.

Quoted 13 lines

There are some downsides,
- It can't handle pinned bpf programs
For pinned programs, the user can get them from the pinned files
directly, so he can use bpftool in his case, only with some complaints.
- If the user attached the bpf prog, and then removed the pinned file, but didn't detach it.
That happened. But this error case can't be handled.
- There may be other corner cases that it can't fit.

There's a solution to improve it, but we also need to change the kernel. That is, we can use the wasted space btf->name.

Quoted 14 lines

kernel/bpf/btf.c static struct btf *btf_parse(bpfptr_t btf_data, · line 5547

Quoted 11 lines
kernel/bpf/btf.c +2 -0
index b7e5a55..59d73a3 100644
@@ -5542,6 +5542,8 @@ static struct btf *btf_parse(bpfptr_t btf_data,
 u32 btf_data_size,
                err = -ENOMEM;
                goto errout;
        }
+       snprintf(btf->name, sizeof(btf->name), "%s-%d-%d", current->comm,
+                        current->pid, cgroup_id(task_cgroup(p, cpu_cgrp_id)));

Unnecessary.
comm, pid, cgroup can be printed by bpftool without changing the kernel.

Some questions,
- What if the process exits after attaching the bpf prog and the prog is not auto-detachable?
For example, the reuserport bpf prog is not auto-detachable. After
pins the reuserport bpf prog, a task can attach it through the pinned bpf file, but if the task forgets to detach it and the pinned file is removed, then it seems there's no way to figure out which task or cgroup this prog belongs to...
- Could you pls. explain in detail how to get comm, pid, or cgroup from a pinned bpffs file?

Signature · 2 lines
Regards
Yafang
#38 lore raw
Quoted 6 lines · Yafang Shao

It seems that I didn't describe the issue clearly.
The container doesn't have CAP_SYS_ADMIN, but the CAP_SYS_ADMIN is required to run bpftool, so the bpftool running in the container can't get the ID of bpf objects or convert IDs to FDs. Is there something that I missed ?

Nothing. This is by design. bpftool needs sudo. That's all.

Quoted 54 lines
Quoted 39 lines
Quoted 14 lines
kernel/bpf/syscall.c +0 -3
@@ -3705,9 +3705,6 @@ static int bpf_obj_get_next_id(const union bpf_attr *attr,
        if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
                return -EINVAL;
 
-       if (!capable(CAP_SYS_ADMIN))
-               return -EPERM;
-
        next_id++;
        spin_lock_bh(lock);
        if (!idr_get_next(idr, &next_id))
Quoted 8 lines

Because the container doesn't have CAP_SYS_ADMIN enabled, while they only have CAP_BPF and other required CAPs.

Another possible solution is that we run an agent in the host, and the user in the container who wants to get the bpf objects info in his container should send a request to this agent via unix domain socket. That is what we are doing now in our production environment. That said, each container has to run a client to get the bpf object fd.

None of such hacks are necessary. People that debug bpf setups with bpftool can always sudo.

Quoted 13 lines

There are some downsides,
- It can't handle pinned bpf programs
For pinned programs, the user can get them from the pinned files
directly, so he can use bpftool in his case, only with some complaints.
- If the user attached the bpf prog, and then removed the pinned file, but didn't detach it.
That happened. But this error case can't be handled.
- There may be other corner cases that it can't fit.

There's a solution to improve it, but we also need to change the kernel. That is, we can use the wasted space btf->name.

Quoted 14 lines

kernel/bpf/btf.c static struct btf *btf_parse(bpfptr_t btf_data, · line 5547

Quoted 11 lines
kernel/bpf/btf.c +2 -0
index b7e5a55..59d73a3 100644
@@ -5542,6 +5542,8 @@ static struct btf *btf_parse(bpfptr_t btf_data,
 u32 btf_data_size,
                err = -ENOMEM;
                goto errout;
        }
+       snprintf(btf->name, sizeof(btf->name), "%s-%d-%d", current->comm,
+                        current->pid, cgroup_id(task_cgroup(p, cpu_cgrp_id)));

Unnecessary.
comm, pid, cgroup can be printed by bpftool without changing the kernel.

Quoted 8 lines

Some questions,
- What if the process exits after attaching the bpf prog and the prog is not auto-detachable?
For example, the reuserport bpf prog is not auto-detachable. After
pins the reuserport bpf prog, a task can attach it through the pinned bpf file, but if the task forgets to detach it and the pinned file is removed, then it seems there's no way to figure out which task or cgroup this prog belongs to...

you're saying that there is a bpf prog in the kernel without corresponding user space ? Meaning no user space process has an FD that points to this prog or FD to a map that this prog is using? In such a case this is truly kernel bpf prog. It doesn't belong to cgroup.

Quoted 2 lines

- Could you pls. explain in detail how to get comm, pid, or cgroup from a pinned bpffs file?

pinned bpf prog and no user space holds FD to it?
It's not part of any cgroup. Nothing to print.

#39 lore raw
Quoted 11 lines · Alexei Starovoitov
Quoted 6 lines · Yafang Shao

It seems that I didn't describe the issue clearly.
The container doesn't have CAP_SYS_ADMIN, but the CAP_SYS_ADMIN is required to run bpftool, so the bpftool running in the container can't get the ID of bpf objects or convert IDs to FDs. Is there something that I missed ?

Nothing. This is by design. bpftool needs sudo. That's all.

Hmm, what I'm trying to do is make bpftool run without sudo.

Quoted 64 lines
Quoted 55 lines
Quoted 40 lines
Quoted 15 lines
kernel/bpf/syscall.c +0 -3
@@ -3705,9 +3705,6 @@ static int bpf_obj_get_next_id(const union bpf_attr *attr,
        if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
                return -EINVAL;
 
-       if (!capable(CAP_SYS_ADMIN))
-               return -EPERM;
-
        next_id++;
        spin_lock_bh(lock);
        if (!idr_get_next(idr, &next_id))
Quoted 8 lines

Because the container doesn't have CAP_SYS_ADMIN enabled, while they only have CAP_BPF and other required CAPs.

Another possible solution is that we run an agent in the host, and the user in the container who wants to get the bpf objects info in his container should send a request to this agent via unix domain socket. That is what we are doing now in our production environment. That said, each container has to run a client to get the bpf object fd.

None of such hacks are necessary. People that debug bpf setups with bpftool can always sudo.

Quoted 13 lines

There are some downsides,
- It can't handle pinned bpf programs
For pinned programs, the user can get them from the pinned files
directly, so he can use bpftool in his case, only with some complaints.
- If the user attached the bpf prog, and then removed the pinned file, but didn't detach it.
That happened. But this error case can't be handled.
- There may be other corner cases that it can't fit.

There's a solution to improve it, but we also need to change the kernel. That is, we can use the wasted space btf->name.

Quoted 14 lines

kernel/bpf/btf.c static struct btf *btf_parse(bpfptr_t btf_data, · line 5547

Quoted 11 lines
kernel/bpf/btf.c +2 -0
index b7e5a55..59d73a3 100644
@@ -5542,6 +5542,8 @@ static struct btf *btf_parse(bpfptr_t btf_data,
 u32 btf_data_size,
                err = -ENOMEM;
                goto errout;
        }
+       snprintf(btf->name, sizeof(btf->name), "%s-%d-%d", current->comm,
+                        current->pid, cgroup_id(task_cgroup(p, cpu_cgrp_id)));

Unnecessary.
comm, pid, cgroup can be printed by bpftool without changing the kernel.

Quoted 8 lines

Some questions,
- What if the process exits after attaching the bpf prog and the prog is not auto-detachable?
For example, the reuserport bpf prog is not auto-detachable. After
pins the reuserport bpf prog, a task can attach it through the pinned bpf file, but if the task forgets to detach it and the pinned file is removed, then it seems there's no way to figure out which task or cgroup this prog belongs to...

Quoted 2 lines

you're saying that there is a bpf prog in the kernel without corresponding user space ?

No, it is corresponding to user space. For example, it may be corresponding to a socket fd, or a cgroup fd.

Quoted 4 lines

Meaning no user space process has an FD
that points to this prog or FD to a map that this prog is using? In such a case this is truly kernel bpf prog. It doesn't belong to cgroup.

Even if it is kernel bpf prog, it is created by a process. The user needs to know which one created it.

Quoted 5 lines
Quoted 2 lines

- Could you pls. explain in detail how to get comm, pid, or cgroup from a pinned bpffs file?

pinned bpf prog and no user space holds FD to it?
It's not part of any cgroup. Nothing to print.

As I explained above, even if it holds nothing, the user needs to know the information from it. For example, if it is expected, which one created it?

Signature · 2 lines
Regards
Yafang
#40 lore raw
Quoted 12 lines · Yafang Shao
Quoted 9 lines · Alexei Starovoitov
Quoted 6 lines · Yafang Shao

It seems that I didn't describe the issue clearly.
The container doesn't have CAP_SYS_ADMIN, but the CAP_SYS_ADMIN is required to run bpftool, so the bpftool running in the container can't get the ID of bpf objects or convert IDs to FDs. Is there something that I missed ?

Quoted 4 lines
Quoted 2 lines

Nothing. This is by design. bpftool needs sudo. That's all.

Hmm, what I'm trying to do is make bpftool run without sudo.

This is not a task that is worth solving.

Quoted 15 lines
Quoted 11 lines
Quoted 8 lines

Some questions,
- What if the process exits after attaching the bpf prog and the prog is not auto-detachable?
For example, the reuserport bpf prog is not auto-detachable. After
pins the reuserport bpf prog, a task can attach it through the pinned bpf file, but if the task forgets to detach it and the pinned file is removed, then it seems there's no way to figure out which task or cgroup this prog belongs to...

you're saying that there is a bpf prog in the kernel without corresponding user space ?

No, it is corresponding to user space. For example, it may be corresponding to a socket fd, or a cgroup fd.

Quoted 7 lines
Quoted 4 lines

Meaning no user space process has an FD
that points to this prog or FD to a map that this prog is using? In such a case this is truly kernel bpf prog. It doesn't belong to cgroup.

Even if it is kernel bpf prog, it is created by a process. The user needs to know which one created it.

In some situations it's certainly interesting to know which process loaded a particular program.
In many other situations it's irrelevant.
For example, the process that loaded a prog could have been moved to a different cgroup.
If you want to track the loading you need to install bpf_lsm that monitors prog_load hook and collect that info.
It's not the job of the kernel to do it.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

- Could you pls. explain in detail how to get comm, pid, or cgroup from a pinned bpffs file?

pinned bpf prog and no user space holds FD to it?
It's not part of any cgroup. Nothing to print.

As I explained above, even if it holds nothing, the user needs to know the information from it. For example, if it is expected, which one created it?

See the answer above. The kernel has enough hooks already to provide this information to user space. No kernel changes necessary.

#41 lore raw
Quoted 14 lines · Alexei Starovoitov
Quoted 12 lines · Yafang Shao
Quoted 9 lines · Alexei Starovoitov
Quoted 6 lines · Yafang Shao

It seems that I didn't describe the issue clearly.
The container doesn't have CAP_SYS_ADMIN, but the CAP_SYS_ADMIN is required to run bpftool, so the bpftool running in the container can't get the ID of bpf objects or convert IDs to FDs. Is there something that I missed ?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Nothing. This is by design. bpftool needs sudo. That's all.

Hmm, what I'm trying to do is make bpftool run without sudo.

This is not a task that is worth solving.

Then the container with CAP_BPF enabled can't even iterate its bpf progs ...

Quoted 23 lines
Quoted 15 lines
Quoted 11 lines
Quoted 8 lines

Some questions,
- What if the process exits after attaching the bpf prog and the prog is not auto-detachable?
For example, the reuserport bpf prog is not auto-detachable. After
pins the reuserport bpf prog, a task can attach it through the pinned bpf file, but if the task forgets to detach it and the pinned file is removed, then it seems there's no way to figure out which task or cgroup this prog belongs to...

you're saying that there is a bpf prog in the kernel without corresponding user space ?

No, it is corresponding to user space. For example, it may be corresponding to a socket fd, or a cgroup fd.

Quoted 7 lines
Quoted 4 lines

Meaning no user space process has an FD
that points to this prog or FD to a map that this prog is using? In such a case this is truly kernel bpf prog. It doesn't belong to cgroup.

Even if it is kernel bpf prog, it is created by a process. The user needs to know which one created it.

Quoted 9 lines

In some situations it's certainly interesting to know which process loaded a particular program.
In many other situations it's irrelevant.
For example, the process that loaded a prog could have been moved to a different cgroup.
If you want to track the loading you need to install bpf_lsm that monitors prog_load hook and collect that info.
It's not the job of the kernel to do it.

Agreed with you that we can add lots of hooks to track every detail of the operations.
But it is not free. More hooks, more overhead.
If we can change the kernel to make it lightweight, why not...

Quoted 12 lines
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

- Could you pls. explain in detail how to get comm, pid, or cgroup from a pinned bpffs file?

pinned bpf prog and no user space holds FD to it?
It's not part of any cgroup. Nothing to print.

As I explained above, even if it holds nothing, the user needs to know the information from it. For example, if it is expected, which one created it?

See the answer above. The kernel has enough hooks already to provide this information to user space. No kernel changes necessary.

Signature · 2 lines
Regards
Yafang
#42 lore raw
Quoted 20 lines · Yafang Shao
Quoted 17 lines · Alexei Starovoitov
Quoted 15 lines · Yafang Shao
Quoted 11 lines · Alexei Starovoitov
Quoted 6 lines · Yafang Shao

It seems that I didn't describe the issue clearly.
The container doesn't have CAP_SYS_ADMIN, but the CAP_SYS_ADMIN is required to run bpftool, so the bpftool running in the container can't get the ID of bpf objects or convert IDs to FDs. Is there something that I missed ?

Nothing. This is by design. bpftool needs sudo. That's all.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Hmm, what I'm trying to do is make bpftool run without sudo.

This is not a task that is worth solving.

Then the container with CAP_BPF enabled can't even iterate its bpf progs ...

I'll leave the BPF namespace discussion aside (I agree that it needs way more thought).

I am a bit surprised that we require CAP_SYS_ADMIN for GET_NEXT_ID operations. GET_FD_BY_ID is definitely CAP_SYS_ADMIN, as they allow you to take over someone else's link and stuff like this. But just iterating IDs seems like a pretty innocent functionality, so maybe we should remove CAP_SYS_ADMIN for GET_NEXT_ID?

By itself GET_NEXT_ID is relatively useless without capabilities, but we've been floating the idea of providing GET_INFO_BY_ID (not by FD) for a while now, and that seems useful in itself, as it would indeed help tools like bpftool to get *some* information even without privileges. Whether those GET_INFO_BY_ID operations should return same full bpf_{prog,map,link,btf}_info or some trimmed down version of them would be up to discussion, but I think getting some info without creating an FD seems useful in itself.

Would it be worth discussing and solving this separately from namespacing issues?

Quoted 57 lines
Quoted 24 lines
Quoted 16 lines
Quoted 10 lines
Quoted 9 lines

Some questions,
- What if the process exits after attaching the bpf prog and the prog is not auto-detachable?
For example, the reuserport bpf prog is not auto-detachable. After
pins the reuserport bpf prog, a task can attach it through the pinned bpf file, but if the task forgets to detach it and the pinned file is removed, then it seems there's no way to figure out which task or cgroup this prog belongs to...

Quoted 2 lines

you're saying that there is a bpf prog in the kernel without corresponding user space ?

No, it is corresponding to user space. For example, it may be corresponding to a socket fd, or a cgroup fd.

Quoted 7 lines
Quoted 4 lines

Meaning no user space process has an FD
that points to this prog or FD to a map that this prog is using? In such a case this is truly kernel bpf prog. It doesn't belong to cgroup.

Even if it is kernel bpf prog, it is created by a process. The user needs to know which one created it.

Quoted 9 lines

In some situations it's certainly interesting to know which process loaded a particular program.
In many other situations it's irrelevant.
For example, the process that loaded a prog could have been moved to a different cgroup.
If you want to track the loading you need to install bpf_lsm that monitors prog_load hook and collect that info.
It's not the job of the kernel to do it.

Agreed with you that we can add lots of hooks to track every detail of the operations.
But it is not free. More hooks, more overhead.
If we can change the kernel to make it lightweight, why not...

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

- Could you pls. explain in detail how to get comm, pid, or cgroup from a pinned bpffs file?

pinned bpf prog and no user space holds FD to it?
It's not part of any cgroup. Nothing to print.

Quoted 6 lines
Quoted 3 lines

As I explained above, even if it holds nothing, the user needs to know the information from it. For example, if it is expected, which one created it?

See the answer above. The kernel has enough hooks already to provide this information to user space. No kernel changes necessary.

--
Regards
Yafang

#43 lore raw
Quoted 46 lines · Andrii Nakryiko
Quoted 20 lines · Yafang Shao
Quoted 17 lines · Alexei Starovoitov
Quoted 15 lines · Yafang Shao
Quoted 11 lines · Alexei Starovoitov
Quoted 6 lines · Yafang Shao

It seems that I didn't describe the issue clearly.
The container doesn't have CAP_SYS_ADMIN, but the CAP_SYS_ADMIN is required to run bpftool, so the bpftool running in the container can't get the ID of bpf objects or convert IDs to FDs. Is there something that I missed ?

Nothing. This is by design. bpftool needs sudo. That's all.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Hmm, what I'm trying to do is make bpftool run without sudo.

This is not a task that is worth solving.

Then the container with CAP_BPF enabled can't even iterate its bpf progs ...

I'll leave the BPF namespace discussion aside (I agree that it needs way more thought).

I am a bit surprised that we require CAP_SYS_ADMIN for GET_NEXT_ID operations. GET_FD_BY_ID is definitely CAP_SYS_ADMIN, as they allow you to take over someone else's link and stuff like this. But just iterating IDs seems like a pretty innocent functionality, so maybe we should remove CAP_SYS_ADMIN for GET_NEXT_ID?

By itself GET_NEXT_ID is relatively useless without capabilities, but we've been floating the idea of providing GET_INFO_BY_ID (not by FD) for a while now, and that seems useful in itself, as it would indeed help tools like bpftool to get *some* information even without privileges. Whether those GET_INFO_BY_ID operations should return same full bpf_{prog,map,link,btf}_info or some trimmed down version of them would be up to discussion, but I think getting some info without creating an FD seems useful in itself.

Quoted 2 lines

Would it be worth discussing and solving this separately from namespacing issues?

Iteration of IDs itself is fine. The set of IDs is not security sensitive, but GET_NEXT_BY_ID has to be carefully restricted.
It returns xlated, jited, BTF, line info, etc
and with all the restrictions it would need something like CAP_SYS_PTRACE and CAP_PERFMON to be useful.
And with that we're not far from CAP_SYS_ADMIN.
Why bother then?

#44 lore raw
Quoted 51 lines · Alexei Starovoitov
Quoted 46 lines · Andrii Nakryiko
Quoted 20 lines · Yafang Shao
Quoted 17 lines · Alexei Starovoitov
Quoted 15 lines · Yafang Shao
Quoted 11 lines · Alexei Starovoitov
Quoted 6 lines · Yafang Shao

It seems that I didn't describe the issue clearly.
The container doesn't have CAP_SYS_ADMIN, but the CAP_SYS_ADMIN is required to run bpftool, so the bpftool running in the container can't get the ID of bpf objects or convert IDs to FDs. Is there something that I missed ?

Nothing. This is by design. bpftool needs sudo. That's all.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Hmm, what I'm trying to do is make bpftool run without sudo.

This is not a task that is worth solving.

Then the container with CAP_BPF enabled can't even iterate its bpf progs ...

I'll leave the BPF namespace discussion aside (I agree that it needs way more thought).

I am a bit surprised that we require CAP_SYS_ADMIN for GET_NEXT_ID operations. GET_FD_BY_ID is definitely CAP_SYS_ADMIN, as they allow you to take over someone else's link and stuff like this. But just iterating IDs seems like a pretty innocent functionality, so maybe we should remove CAP_SYS_ADMIN for GET_NEXT_ID?

By itself GET_NEXT_ID is relatively useless without capabilities, but we've been floating the idea of providing GET_INFO_BY_ID (not by FD) for a while now, and that seems useful in itself, as it would indeed help tools like bpftool to get *some* information even without privileges. Whether those GET_INFO_BY_ID operations should return same full bpf_{prog,map,link,btf}_info or some trimmed down version of them would be up to discussion, but I think getting some info without creating an FD seems useful in itself.

Quoted 2 lines

Would it be worth discussing and solving this separately from namespacing issues?

Quoted 7 lines

Iteration of IDs itself is fine. The set of IDs is not security sensitive, but GET_NEXT_BY_ID has to be carefully restricted.
It returns xlated, jited, BTF, line info, etc
and with all the restrictions it would need something like CAP_SYS_PTRACE and CAP_PERFMON to be useful.
And with that we're not far from CAP_SYS_ADMIN.
Why bother then?

Not sure if I get your point clearly. I think the reason we introduce CAP_BPF is that we don't want the user to enable CAP_SYS_ADMIN. Enabling specific CAPs instead of CAP_SYS_ADMIN should be our alignment goal, right?
If so, then it is worth doing. As Andrii suggested, a trimmed down version is also helpful and should be acceptable. Agreed with you that we have lots of things to do, but if we don't try to move it forward, it will always be as-is.

Signature · 2 lines
Regards
Yafang
#45 lore raw
Quoted 62 lines · Yafang Shao
Quoted 51 lines · Alexei Starovoitov
Quoted 46 lines · Andrii Nakryiko
Quoted 20 lines · Yafang Shao
Quoted 17 lines · Alexei Starovoitov
Quoted 15 lines · Yafang Shao
Quoted 11 lines · Alexei Starovoitov
Quoted 6 lines · Yafang Shao

It seems that I didn't describe the issue clearly.
The container doesn't have CAP_SYS_ADMIN, but the CAP_SYS_ADMIN is required to run bpftool, so the bpftool running in the container can't get the ID of bpf objects or convert IDs to FDs. Is there something that I missed ?

Nothing. This is by design. bpftool needs sudo. That's all.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Hmm, what I'm trying to do is make bpftool run without sudo.

This is not a task that is worth solving.

Then the container with CAP_BPF enabled can't even iterate its bpf progs ...

I'll leave the BPF namespace discussion aside (I agree that it needs way more thought).

I am a bit surprised that we require CAP_SYS_ADMIN for GET_NEXT_ID operations. GET_FD_BY_ID is definitely CAP_SYS_ADMIN, as they allow you to take over someone else's link and stuff like this. But just iterating IDs seems like a pretty innocent functionality, so maybe we should remove CAP_SYS_ADMIN for GET_NEXT_ID?

By itself GET_NEXT_ID is relatively useless without capabilities, but we've been floating the idea of providing GET_INFO_BY_ID (not by FD) for a while now, and that seems useful in itself, as it would indeed help tools like bpftool to get *some* information even without privileges. Whether those GET_INFO_BY_ID operations should return same full bpf_{prog,map,link,btf}_info or some trimmed down version of them would be up to discussion, but I think getting some info without creating an FD seems useful in itself.

Quoted 2 lines

Would it be worth discussing and solving this separately from namespacing issues?

Quoted 7 lines

Iteration of IDs itself is fine. The set of IDs is not security sensitive, but GET_NEXT_BY_ID has to be carefully restricted.
It returns xlated, jited, BTF, line info, etc
and with all the restrictions it would need something like CAP_SYS_PTRACE and CAP_PERFMON to be useful.
And with that we're not far from CAP_SYS_ADMIN.
Why bother then?

Quoted 4 lines

Not sure if I get your point clearly. I think the reason we introduce CAP_BPF is that we don't want the user to enable CAP_SYS_ADMIN. Enabling specific CAPs instead of CAP_SYS_ADMIN should be our alignment goal, right?

We want users to switch to CAP_BPF (potentially with CAP_PERFMON) from full CAP_SYS_ADMIN to reduce attack surface of production workloads. bpftool is a tool for humans to do introspection and debugging. It will stay root only.

Quoted 2 lines

If so, then it is worth doing. As Andrii suggested, a trimmed down version is also helpful and should be acceptable.

It's not helpful. It's actively misleading.

#46 lore raw
In reply to Alexei Starovoitov
Quoted 51 lines · Alexei Starovoitov
Quoted 46 lines · Andrii Nakryiko
Quoted 20 lines · Yafang Shao
Quoted 17 lines · Alexei Starovoitov
Quoted 15 lines · Yafang Shao
Quoted 11 lines · Alexei Starovoitov
Quoted 6 lines · Yafang Shao

It seems that I didn't describe the issue clearly.
The container doesn't have CAP_SYS_ADMIN, but the CAP_SYS_ADMIN is required to run bpftool, so the bpftool running in the container can't get the ID of bpf objects or convert IDs to FDs. Is there something that I missed ?

Nothing. This is by design. bpftool needs sudo. That's all.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Hmm, what I'm trying to do is make bpftool run without sudo.

This is not a task that is worth solving.

Then the container with CAP_BPF enabled can't even iterate its bpf progs ...

I'll leave the BPF namespace discussion aside (I agree that it needs way more thought).

I am a bit surprised that we require CAP_SYS_ADMIN for GET_NEXT_ID operations. GET_FD_BY_ID is definitely CAP_SYS_ADMIN, as they allow you to take over someone else's link and stuff like this. But just iterating IDs seems like a pretty innocent functionality, so maybe we should remove CAP_SYS_ADMIN for GET_NEXT_ID?

By itself GET_NEXT_ID is relatively useless without capabilities, but we've been floating the idea of providing GET_INFO_BY_ID (not by FD) for a while now, and that seems useful in itself, as it would indeed help tools like bpftool to get *some* information even without privileges. Whether those GET_INFO_BY_ID operations should return same full bpf_{prog,map,link,btf}_info or some trimmed down version of them would be up to discussion, but I think getting some info without creating an FD seems useful in itself.

Quoted 2 lines

Would it be worth discussing and solving this separately from namespacing issues?

Quoted 7 lines

Iteration of IDs itself is fine. The set of IDs is not security sensitive, but GET_NEXT_BY_ID has to be carefully restricted.
It returns xlated, jited, BTF, line info, etc
and with all the restrictions it would need something like CAP_SYS_PTRACE and CAP_PERFMON to be useful.
And with that we're not far from CAP_SYS_ADMIN.
Why bother then?

You probably meant that GET_INFO_BY_ID should be carefully restricted? So yeah, that's what I said that this would have to be discussed further. I agree that returning func/line info, program dump, etc is probably a privileged part. But there is plenty of useful info besides that (e.g., prog name, insns cnt, run stats, etc) that would be useful for unpriv applications to monitor their own apps that they opened from BPF FS, or just some observability daemons.

There is a lot of useful information in bpf_map_info and bpf_link_info that's way less privileged. I think bpf_link_info is good as is. Same for bpf_map_info.

Either way, I'm not insisting, just something that seems pretty simple to add and useful in some scenarios. We can reuse existing code and types for GET_INFO_BY_FD and just zero-out (or prevent filling out) those privileged fields you mentioned. Anyway, something to put on the backburner, perhaps.

#47 lore raw
Quoted 62 lines · Andrii Nakryiko
Quoted 51 lines · Alexei Starovoitov
Quoted 46 lines · Andrii Nakryiko
Quoted 20 lines · Yafang Shao
Quoted 17 lines · Alexei Starovoitov
Quoted 15 lines · Yafang Shao
Quoted 11 lines · Alexei Starovoitov
Quoted 6 lines · Yafang Shao

It seems that I didn't describe the issue clearly.
The container doesn't have CAP_SYS_ADMIN, but the CAP_SYS_ADMIN is required to run bpftool, so the bpftool running in the container can't get the ID of bpf objects or convert IDs to FDs. Is there something that I missed ?

Nothing. This is by design. bpftool needs sudo. That's all.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Hmm, what I'm trying to do is make bpftool run without sudo.

This is not a task that is worth solving.

Then the container with CAP_BPF enabled can't even iterate its bpf progs ...

I'll leave the BPF namespace discussion aside (I agree that it needs way more thought).

I am a bit surprised that we require CAP_SYS_ADMIN for GET_NEXT_ID operations. GET_FD_BY_ID is definitely CAP_SYS_ADMIN, as they allow you to take over someone else's link and stuff like this. But just iterating IDs seems like a pretty innocent functionality, so maybe we should remove CAP_SYS_ADMIN for GET_NEXT_ID?

By itself GET_NEXT_ID is relatively useless without capabilities, but we've been floating the idea of providing GET_INFO_BY_ID (not by FD) for a while now, and that seems useful in itself, as it would indeed help tools like bpftool to get *some* information even without privileges. Whether those GET_INFO_BY_ID operations should return same full bpf_{prog,map,link,btf}_info or some trimmed down version of them would be up to discussion, but I think getting some info without creating an FD seems useful in itself.

Quoted 2 lines

Would it be worth discussing and solving this separately from namespacing issues?

Quoted 7 lines

Iteration of IDs itself is fine. The set of IDs is not security sensitive, but GET_NEXT_BY_ID has to be carefully restricted.
It returns xlated, jited, BTF, line info, etc
and with all the restrictions it would need something like CAP_SYS_PTRACE and CAP_PERFMON to be useful.
And with that we're not far from CAP_SYS_ADMIN.
Why bother then?

Quoted 1 line

You probably meant that GET_INFO_BY_ID should be carefully restricted?

yes.

Quoted 11 lines

So yeah, that's what I said that this would have to be discussed further. I agree that returning func/line info, program dump, etc is probably a privileged part. But there is plenty of useful info besides that (e.g., prog name, insns cnt, run stats, etc) that would be useful for unpriv applications to monitor their own apps that they opened from BPF FS, or just some observability daemons.

There is a lot of useful information in bpf_map_info and bpf_link_info that's way less privileged. I think bpf_link_info is good as is. Same for bpf_map_info.

Quoted 5 lines

Either way, I'm not insisting, just something that seems pretty simple to add and useful in some scenarios. We can reuse existing code and types for GET_INFO_BY_FD and just zero-out (or prevent filling out) those privileged fields you mentioned. Anyway, something to put on the backburner, perhaps.

Sorry, but I only see negatives. It's an extra code in the kernel that has to be carefully reviewed when initially submitted and then every patch that touches get_info_by_id would have to go through a microscope every time to avoid introducing a security issue. And for what? So that CAP_BPF application can read prog name and run stats?

#48 lore raw
Quoted 86 lines · Alexei Starovoitov
Quoted 62 lines · Andrii Nakryiko
Quoted 51 lines · Alexei Starovoitov
Quoted 46 lines · Andrii Nakryiko
Quoted 20 lines · Yafang Shao
Quoted 17 lines · Alexei Starovoitov
Quoted 15 lines · Yafang Shao
Quoted 11 lines · Alexei Starovoitov
Quoted 6 lines · Yafang Shao

It seems that I didn't describe the issue clearly.
The container doesn't have CAP_SYS_ADMIN, but the CAP_SYS_ADMIN is required to run bpftool, so the bpftool running in the container can't get the ID of bpf objects or convert IDs to FDs. Is there something that I missed ?

Nothing. This is by design. bpftool needs sudo. That's all.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Hmm, what I'm trying to do is make bpftool run without sudo.

This is not a task that is worth solving.

Then the container with CAP_BPF enabled can't even iterate its bpf progs ...

I'll leave the BPF namespace discussion aside (I agree that it needs way more thought).

I am a bit surprised that we require CAP_SYS_ADMIN for GET_NEXT_ID operations. GET_FD_BY_ID is definitely CAP_SYS_ADMIN, as they allow you to take over someone else's link and stuff like this. But just iterating IDs seems like a pretty innocent functionality, so maybe we should remove CAP_SYS_ADMIN for GET_NEXT_ID?

By itself GET_NEXT_ID is relatively useless without capabilities, but we've been floating the idea of providing GET_INFO_BY_ID (not by FD) for a while now, and that seems useful in itself, as it would indeed help tools like bpftool to get *some* information even without privileges. Whether those GET_INFO_BY_ID operations should return same full bpf_{prog,map,link,btf}_info or some trimmed down version of them would be up to discussion, but I think getting some info without creating an FD seems useful in itself.

Quoted 2 lines

Would it be worth discussing and solving this separately from namespacing issues?

Quoted 7 lines

Iteration of IDs itself is fine. The set of IDs is not security sensitive, but GET_NEXT_BY_ID has to be carefully restricted.
It returns xlated, jited, BTF, line info, etc
and with all the restrictions it would need something like CAP_SYS_PTRACE and CAP_PERFMON to be useful.
And with that we're not far from CAP_SYS_ADMIN.
Why bother then?

Quoted 1 line

You probably meant that GET_INFO_BY_ID should be carefully restricted?

yes.

Quoted 11 lines

So yeah, that's what I said that this would have to be discussed further. I agree that returning func/line info, program dump, etc is probably a privileged part. But there is plenty of useful info besides that (e.g., prog name, insns cnt, run stats, etc) that would be useful for unpriv applications to monitor their own apps that they opened from BPF FS, or just some observability daemons.

There is a lot of useful information in bpf_map_info and bpf_link_info that's way less privileged. I think bpf_link_info is good as is. Same for bpf_map_info.

Quoted 5 lines

Either way, I'm not insisting, just something that seems pretty simple to add and useful in some scenarios. We can reuse existing code and types for GET_INFO_BY_FD and just zero-out (or prevent filling out) those privileged fields you mentioned. Anyway, something to put on the backburner, perhaps.

Quoted 5 lines

Sorry, but I only see negatives. It's an extra code in the kernel that has to be carefully reviewed when initially submitted and then every patch that touches get_info_by_id would have to go through a microscope every time to avoid introducing a security issue. And for what? So that CAP_BPF application can read prog name and run stats?

Per my experience, observability is a very important part for a project. If the user can't observe the object directly created by it, he will worry about or even mistrust it.
However I don't insist on it either if you think we shouldn't do it.

Signature · 2 lines
Regards
Yafang
#49 lore raw
Quoted 95 lines · Yafang Shao
Quoted 86 lines · Alexei Starovoitov
Quoted 62 lines · Andrii Nakryiko
Quoted 51 lines · Alexei Starovoitov
Quoted 46 lines · Andrii Nakryiko
Quoted 20 lines · Yafang Shao
Quoted 17 lines · Alexei Starovoitov
Quoted 15 lines · Yafang Shao
Quoted 11 lines · Alexei Starovoitov
Quoted 6 lines · Yafang Shao

It seems that I didn't describe the issue clearly.
The container doesn't have CAP_SYS_ADMIN, but the CAP_SYS_ADMIN is required to run bpftool, so the bpftool running in the container can't get the ID of bpf objects or convert IDs to FDs. Is there something that I missed ?

Nothing. This is by design. bpftool needs sudo. That's all.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Hmm, what I'm trying to do is make bpftool run without sudo.

This is not a task that is worth solving.

Then the container with CAP_BPF enabled can't even iterate its bpf progs ...

I'll leave the BPF namespace discussion aside (I agree that it needs way more thought).

I am a bit surprised that we require CAP_SYS_ADMIN for GET_NEXT_ID operations. GET_FD_BY_ID is definitely CAP_SYS_ADMIN, as they allow you to take over someone else's link and stuff like this. But just iterating IDs seems like a pretty innocent functionality, so maybe we should remove CAP_SYS_ADMIN for GET_NEXT_ID?

By itself GET_NEXT_ID is relatively useless without capabilities, but we've been floating the idea of providing GET_INFO_BY_ID (not by FD) for a while now, and that seems useful in itself, as it would indeed help tools like bpftool to get *some* information even without privileges. Whether those GET_INFO_BY_ID operations should return same full bpf_{prog,map,link,btf}_info or some trimmed down version of them would be up to discussion, but I think getting some info without creating an FD seems useful in itself.

Quoted 2 lines

Would it be worth discussing and solving this separately from namespacing issues?

Quoted 7 lines

Iteration of IDs itself is fine. The set of IDs is not security sensitive, but GET_NEXT_BY_ID has to be carefully restricted.
It returns xlated, jited, BTF, line info, etc
and with all the restrictions it would need something like CAP_SYS_PTRACE and CAP_PERFMON to be useful.
And with that we're not far from CAP_SYS_ADMIN.
Why bother then?

Quoted 1 line

You probably meant that GET_INFO_BY_ID should be carefully restricted?

yes.

Quoted 11 lines

So yeah, that's what I said that this would have to be discussed further. I agree that returning func/line info, program dump, etc is probably a privileged part. But there is plenty of useful info besides that (e.g., prog name, insns cnt, run stats, etc) that would be useful for unpriv applications to monitor their own apps that they opened from BPF FS, or just some observability daemons.

There is a lot of useful information in bpf_map_info and bpf_link_info that's way less privileged. I think bpf_link_info is good as is. Same for bpf_map_info.

Quoted 5 lines

Either way, I'm not insisting, just something that seems pretty simple to add and useful in some scenarios. We can reuse existing code and types for GET_INFO_BY_FD and just zero-out (or prevent filling out) those privileged fields you mentioned. Anyway, something to put on the backburner, perhaps.

Quoted 5 lines

Sorry, but I only see negatives. It's an extra code in the kernel that has to be carefully reviewed when initially submitted and then every patch that touches get_info_by_id would have to go through a microscope every time to avoid introducing a security issue. And for what? So that CAP_BPF application can read prog name and run stats?

Quoted 3 lines

Per my experience, observability is a very important part for a project. If the user can't observe the object directly created by it, he will worry about or even mistrust it.

The user can observe the objects just fine. That's what get_info_by_fd is for. But the kernel will not report JITed instructions to unpriv user who just loaded a prog and a sole owner of it.
By your definition such a user should not trust the kernel. So be it.

#50 lore raw
Quoted 97 lines · Alexei Starovoitov
Quoted 95 lines · Yafang Shao
Quoted 91 lines · Alexei Starovoitov
Quoted 62 lines · Andrii Nakryiko
Quoted 51 lines · Alexei Starovoitov
Quoted 46 lines · Andrii Nakryiko
Quoted 20 lines · Yafang Shao
Quoted 17 lines · Alexei Starovoitov
Quoted 15 lines · Yafang Shao
Quoted 11 lines · Alexei Starovoitov
Quoted 6 lines · Yafang Shao

It seems that I didn't describe the issue clearly.
The container doesn't have CAP_SYS_ADMIN, but the CAP_SYS_ADMIN is required to run bpftool, so the bpftool running in the container can't get the ID of bpf objects or convert IDs to FDs. Is there something that I missed ?

Nothing. This is by design. bpftool needs sudo. That's all.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Hmm, what I'm trying to do is make bpftool run without sudo.

This is not a task that is worth solving.

Then the container with CAP_BPF enabled can't even iterate its bpf progs ...

I'll leave the BPF namespace discussion aside (I agree that it needs way more thought).

I am a bit surprised that we require CAP_SYS_ADMIN for GET_NEXT_ID operations. GET_FD_BY_ID is definitely CAP_SYS_ADMIN, as they allow you to take over someone else's link and stuff like this. But just iterating IDs seems like a pretty innocent functionality, so maybe we should remove CAP_SYS_ADMIN for GET_NEXT_ID?

By itself GET_NEXT_ID is relatively useless without capabilities, but we've been floating the idea of providing GET_INFO_BY_ID (not by FD) for a while now, and that seems useful in itself, as it would indeed help tools like bpftool to get *some* information even without privileges. Whether those GET_INFO_BY_ID operations should return same full bpf_{prog,map,link,btf}_info or some trimmed down version of them would be up to discussion, but I think getting some info without creating an FD seems useful in itself.

Quoted 2 lines

Would it be worth discussing and solving this separately from namespacing issues?

Quoted 7 lines

Iteration of IDs itself is fine. The set of IDs is not security sensitive, but GET_NEXT_BY_ID has to be carefully restricted.
It returns xlated, jited, BTF, line info, etc
and with all the restrictions it would need something like CAP_SYS_PTRACE and CAP_PERFMON to be useful.
And with that we're not far from CAP_SYS_ADMIN.
Why bother then?

Quoted 1 line

You probably meant that GET_INFO_BY_ID should be carefully restricted?

yes.

Quoted 11 lines

So yeah, that's what I said that this would have to be discussed further. I agree that returning func/line info, program dump, etc is probably a privileged part. But there is plenty of useful info besides that (e.g., prog name, insns cnt, run stats, etc) that would be useful for unpriv applications to monitor their own apps that they opened from BPF FS, or just some observability daemons.

There is a lot of useful information in bpf_map_info and bpf_link_info that's way less privileged. I think bpf_link_info is good as is. Same for bpf_map_info.

Quoted 5 lines

Either way, I'm not insisting, just something that seems pretty simple to add and useful in some scenarios. We can reuse existing code and types for GET_INFO_BY_FD and just zero-out (or prevent filling out) those privileged fields you mentioned. Anyway, something to put on the backburner, perhaps.

Sorry, but I only see negatives. It's an extra code in the kernel that has to be carefully reviewed when initially submitted and then every patch that touches get_info_by_id would have to go through a microscope every time to avoid introducing a security issue. And for what? So that CAP_BPF application can read prog name and run stats?

Quoted 7 lines
Quoted 3 lines

Per my experience, observability is a very important part for a project. If the user can't observe the object directly created by it, he will worry about or even mistrust it.

The user can observe the objects just fine. That's what get_info_by_fd is for. But the kernel will not report JITed instructions to unpriv user who just loaded a prog and a sole owner of it.

There's no UAPI to create the JITed instructions directly per my understanding. The JITed instructions are created by the kernel. While they're really UAPI to create a map, prog, and link.

Quoted 1 line

By your definition such a user should not trust the kernel. So be it.

Signature · 2 lines
Regards
Yafang
#51 lore raw
In reply to Yafang Shao
Quoted 1 line · Yafang Shao

<...>

Quoted 8 lines

BPF namespace is introduced in this patchset with an attempt to remove the CAP_SYS_ADMIN requirement. The user can create bpf map, prog and link in a specific bpf namespace, then these bpf objects will not be visible to the users in a different bpf namespace. But these bpf objects are visible to its parent bpf namespace, so the sys admin can still iterate and inspect them.

Quoted 9 lines

BPF namespace is similar to PID namespace, and the bpf objects are similar to tasks, so BPF namespace is very easy to understand. These patchset only implements BPF namespace for bpf map, prog and link. In the future we may extend it to other bpf objects like btf, bpffs and etc. For example, we can allow some of the BTF objects to be used in non-init bpf namespace, then the container user can only trace the processes running in his container, but can't get the information of tasks running in other containers.

Hi Yafang,

Thanks for putting effort toward enabling BPF for container users!

However, I think the cover letter can be improved. It's unclear to me what exactly is BPF namespace, what exactly it tries to achieve and what is its behavior. If you look at the manpage of pid namespace [1], cgroup namespace[2], and namespace[3], they all have a very precise definition, their goals and explain the intended behaviors well.

I felt you intended the BPF namespace to provide isolation of object ids. That is, different views of the bpf object ids for different processes. This is like the PID namespace. But somehow, you also attach CAPs on top of that. That, I think, is not a namespace's job.

Well, I could be wrong, but would appreciate you adding more details as follow-up.

Hao

[1] https://man7.org/linux/man-pages/man7/pid_namespaces.7.html
[2] https://man7.org/linux/man-pages/man7/cgroup_namespaces.7.html
[3] https://man7.org/linux/man-pages/man7/namespaces.7.html

#52 lore raw
Quoted 24 lines · Hao Luo
Quoted 1 line · Yafang Shao

<...>

Quoted 8 lines

BPF namespace is introduced in this patchset with an attempt to remove the CAP_SYS_ADMIN requirement. The user can create bpf map, prog and link in a specific bpf namespace, then these bpf objects will not be visible to the users in a different bpf namespace. But these bpf objects are visible to its parent bpf namespace, so the sys admin can still iterate and inspect them.

Quoted 9 lines

BPF namespace is similar to PID namespace, and the bpf objects are similar to tasks, so BPF namespace is very easy to understand. These patchset only implements BPF namespace for bpf map, prog and link. In the future we may extend it to other bpf objects like btf, bpffs and etc. For example, we can allow some of the BTF objects to be used in non-init bpf namespace, then the container user can only trace the processes running in his container, but can't get the information of tasks running in other containers.

Hi Yafang,

Quoted 8 lines

Thanks for putting effort toward enabling BPF for container users!

However, I think the cover letter can be improved. It's unclear to me what exactly is BPF namespace, what exactly it tries to achieve and what is its behavior. If you look at the manpage of pid namespace [1], cgroup namespace[2], and namespace[3], they all have a very precise definition, their goals and explain the intended behaviors well.

Thanks for your suggestion. The covetter should be improved. I will read the man pages of these namespaces and improve it as you suggested.

Quoted 5 lines

I felt you intended the BPF namespace to provide isolation of object ids. That is, different views of the bpf object ids for different processes. This is like the PID namespace. But somehow, you also attach CAPs on top of that. That, I think, is not a namespace's job.

Agree with you that it should be independent of CAPs.
After the bpf namespace is introduced, actually we don't need the CAPs when the user iterates IDs or converts IDs to FDs in his bpf namespace (except in the init bpf namespace), because these are all readonly operations and the user can only read the bpf objects created by himself. While the CAPs should be required when the user wants to write something, e.g. creating a map, loading a prog. They are really different things. I will change it in the next version.

Quoted 8 lines

Well, I could be wrong, but would appreciate you adding more details as follow-up.

Hao

[1] https://man7.org/linux/man-pages/man7/pid_namespaces.7.html
[2] https://man7.org/linux/man-pages/man7/cgroup_namespaces.7.html
[3] https://man7.org/linux/man-pages/man7/namespaces.7.html

Signature · 2 lines
Regards
Yafang