bpf: Allow bpf_get_netns_cookie in BPF_PROG_TYPE_CGROUP_SOCKOPT
Subsystems (5)
cover bpf: Allow bpf_get_netns_cookie in BPF_PROG_TYPE_CGROUP_SOCKOPT
We'd like to be able to identify netns from setsockopt hooks to be able to do the enforcement of some options only in the "initial" netns (to give users the ability to create clear/isolated sandboxes if needed without any enforcement by doing unshare(net)).
Stanislav Fomichev (2):
bpf: Allow bpf_get_netns_cookie in BPF_PROG_TYPE_CGROUP_SOCKOPT selftests/bpf: verify bpf_get_netns_cookie in
BPF_PROG_TYPE_CGROUP_SOCKOPT
kernel/bpf/cgroup.c | 17 +++++++++++++++ tools/testing/selftests/bpf/verifier/ctx.c | 25 ++++++++++++++++++++++ 2 files changed, 42 insertions(+)
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
PATCH 1/2 bpf: Allow bpf_get_netns_cookie in BPF_PROG_TYPE_CGROUP_SOCKOPT
This is similar to existing BPF_PROG_TYPE_CGROUP_SOCK and BPF_PROG_TYPE_CGROUP_SOCK_ADDR.
--- kernel/bpf/cgroup.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
kernel/bpf/cgroup.c +17 -0
@@ -1846,10 +1846,27 @@ const struct bpf_verifier_ops cg_sysctl_verifier_ops = {const struct bpf_prog_ops cg_sysctl_prog_ops = { }; +BPF_CALL_1(bpf_get_netns_cookie_sockopt, struct bpf_sockopt_kern *, ctx) +{ +struct sock *sk = ctx ? ctx->sk :NULL ; +const struct net *net = sk ?sock_net (sk) : &init_net; + +return net->net_cookie; +} + +static const struct bpf_func_proto bpf_get_netns_cookie_sockopt_proto = { + .func = bpf_get_netns_cookie_sockopt, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX_OR_NULL , +}; +static const struct bpf_func_proto *cg_sockopt_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) {switch (func_id) { +case BPF_FUNC_get_netns_cookie: +return &bpf_get_netns_cookie_sockopt_proto;#ifdef CONFIG_NET case BPF_FUNC_sk_storage_get:return &bpf_sk_storage_get_proto;
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
PATCH 2/2 selftests/bpf: verify bpf_get_netns_cookie in BPF_PROG_TYPE_CGROUP_SOCKOPT
In reply to Stanislav FomichevAdd verifier ctx test to call bpf_get_netns_cookie from cgroup/setsockopt.
#269/p pass ctx or null check, 1: ctx Did not run the program (not supported) OK #270/p pass ctx or null check, 2: null Did not run the program (not supported) OK #271/p pass ctx or null check, 3: 1 OK
#272/p pass ctx or null check, 4: ctx - const OK
#273/p pass ctx or null check, 5: null (connect) Did not run the program (not supported) OK #274/p pass ctx or null check, 6: null (bind) Did not run the program (not supported) OK #275/p pass ctx or null check, 7: ctx (bind) Did not run the program (not supported) OK #276/p pass ctx or null check, 8: null (bind) OK
#277/p pass ctx or null check, 9: ctx (cgroup/setsockopt) Did not run the program (not supported) OK #278/p pass ctx or null check, 10: null (cgroup/setsockopt) Did not run the program (not supported) OK
--- tools/testing/selftests/bpf/verifier/ctx.c | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+)
tools/testing/selftests/bpf/verifier/ctx.c +25 -0
@@ -195,3 +195,28 @@ .result = REJECT, .errstr = "R1 type=inv expected=ctx", }, +{ + "pass ctx or null check, 9: ctx (cgroup/setsockopt)", + .insns = { +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , + BPF_FUNC_get_netns_cookie), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .prog_type =BPF_PROG_TYPE_CGROUP_SOCKOPT , + .expected_attach_type = BPF_CGROUP_SETSOCKOPT, + .result = ACCEPT, +}, +{ +"pass ctx or null check, 10: null (cgroup/setsockopt)" , + .insns = { +BPF_MOV64_IMM (BPF_REG_1 ,0 ), +BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , + BPF_FUNC_get_netns_cookie), +BPF_MOV64_IMM (BPF_REG_0 ,0 ), + BPF_EXIT_INSN(), + }, + .prog_type =BPF_PROG_TYPE_CGROUP_SOCKOPT , + .expected_attach_type = BPF_CGROUP_SETSOCKOPT, + .result = ACCEPT, +},
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
kernel test robot
build botThank you for the patch! Yet something to improve:
Hi Stanislav,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on bpf-next/master]
url: https://github.com/0day-ci/linux/commits/Stanislav-Fomichev/bpf-Allow-bpf_get_netns_cookie-in-BPF_PROG_TYPE_CGROUP_SOCKOPT/20210812-062850 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: riscv-randconfig-r026-20210811 (attached as .config) compiler: riscv32-linux-gcc (GCC) 10.3.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/0day-ci/linux/commit/192e9ea84066a6ef07e9d1ce72e7f597b920ada2
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Stanislav-Fomichev/bpf-Allow-bpf_get_netns_cookie-in-BPF_PROG_TYPE_CGROUP_SOCKOPT/20210812-062850 git checkout 192e9ea84066a6ef07e9d1ce72e7f597b920ada2
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-10.3.0 make.cross O=build_dir ARCH=riscv SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
riscv32-linux-ld: kernel/bpf/cgroup.o: in function `.L31':
>> cgroup.c:(.text+0x240): undefined reference to `init_net' >> riscv32-linux-ld: cgroup.c:(.text+0x244): undefined reference to `init_net'
riscv32-linux-ld: cgroup.c:(.text+0x248): undefined reference to `init_net'
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org