bpf: cgroup_sock lsm flavor
Subsystems (10)
cover bpf: cgroup_sock lsm flavor
This is an RFC proposal for a recent discussion about default socket policy [0]. The series implements new lsm flavor for attaching lsm-like programs to existing lsm hooks that operate on 'struct socket' The actual requirement is that the first argument is of type 'struct socket'. Later on we can add support 'struct sock' based hooks without any user-visible changes.
For demonstration purposes only two hooks are included (can be extended to more later). Also, for demonstration purposes, writes to sock->sk_priority are exposed to lsm hooks (can cover more bpf_sock fields later).
The intended workflow is:
The users load lsm_cgroup_sock tracepoint into the system. This installs generic fmod_ret trampoline that runs __cgroup_bpf_run_lsm_sock.
After that, bpf_prog_attach should be called to activate this program for the particular cgroup. This interface uses exiting cgroup_bpf functionality and should support all existing inheritance flags.
I'd like to get a generic feedback whether I'm going into the right direction or not. The thing I'm not sure about is the way I'm abusing jit generation (maybe fmod_ret should be automagically installed instead?).
For non-socket specific hooks, we can add a similar BPF_LSM_CGROUP attach point that looks at current->cgroup instead of socket->cgroup.
[0] https://lore.kernel.org/bpf/[email protected]/
Stanislav Fomichev (4):
bpf: cgroup_sock lsm flavor
bpf: allow writing to sock->sk_priority from lsm progtype
libbpf: add lsm_cgoup_sock type
selftest: lsm_cgroup_sock sample usage
arch/x86/net/bpf_jit_comp.c | 27 +++++-- include/linux/bpf-cgroup-defs.h | 4 + include/linux/bpf.h | 2 + include/uapi/linux/bpf.h | 1 + kernel/bpf/bpf_lsm.c | 49 +++++++++++ kernel/bpf/btf.c | 10 +++ kernel/bpf/cgroup.c | 43 +++++++++- kernel/bpf/syscall.c | 6 +- kernel/bpf/trampoline.c | 1 + kernel/bpf/verifier.c | 4 +- tools/include/uapi/linux/bpf.h | 1 + tools/lib/bpf/libbpf.c | 2 + .../bpf/prog_tests/lsm_cgroup_sock.c | 81 +++++++++++++++++++ .../selftests/bpf/progs/lsm_cgroup_sock.c | 55 +++++++++++++ 14 files changed, 273 insertions(+), 13 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_cgroup_sock.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_cgroup_sock.c
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
RFC 1/4 bpf: cgroup_sock lsm flavor
Allow per-cgroup lsm attachment of a subset of hooks that operate on the 'struct sock'
Expected usage:
1. attach raw tracepoint hook with expected_attach=BPF_LSM_CGROUP_SOCK
2. this causes fmod_ret trampoline that invokes __cgroup_bpf_run_lsm_sock
3. __cgroup_bpf_run_lsm_sock relies on existing cgroup_bpf->effective array which is extended to include new slots for lsm hooks
4. attach same program to the cgroup_fd
Current limitation:
- abusing x86 jit, not generic
- no proper error handling (detach tracepoint first will probably cause problems)
- 2 hooks for now for demonstration purposes
- lsm specific, maybe can be extended fentry/fexit/fmod_ret
--- arch/x86/net/bpf_jit_comp.c | 27 +++++++++++++++------ include/linux/bpf-cgroup-defs.h | 4 +++ include/linux/bpf.h | 2 ++ include/uapi/linux/bpf.h | 1 + kernel/bpf/btf.c | 10 ++++++++ kernel/bpf/cgroup.c | 43 ++++++++++++++++++++++++++++++--- kernel/bpf/syscall.c | 6 ++++- kernel/bpf/trampoline.c | 1 + kernel/bpf/verifier.c | 1 + tools/include/uapi/linux/bpf.h | 1 + 10 files changed, 84 insertions(+), 12 deletions(-)
arch/x86/net/bpf_jit_comp.c +19 -8
@@ -1742,6 +1742,8 @@ static void restore_regs(const struct btf_func_model *m, u8 **prog, int nr_args, -(stack_size - i * 8)); } +extern int __cgroup_bpf_run_lsm_sock (u64 *,const struct bpf_prog *); +static int invoke_bpf_prog (const struct btf_func_model *m,u8 **pprog,struct bpf_prog *p,int stack_size,bool save_ret) {
@@ -1767,14 +1769,23 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,/* arg1: lea rdi, [rbp - stack_size] */ EMIT4 (0x48 ,0x8D ,0x7D , -stack_size); -/* arg2: progs[i]->insnsi for interpreter */ -if (!p->jited) -emit_mov_imm64 (&prog,BPF_REG_2 , - (long ) p->insnsi >>32 , - (u32 ) (long ) p->insnsi); -/* call JITed bpf program or interpreter */ -if (emit_call (&prog, p->bpf_func, prog)) -return -EINVAL ; + +if (p->expected_attach_type ==BPF_LSM_CGROUP_SOCK ) { +/* arg2: progs[i] */ +emit_mov_imm64 (&prog,BPF_REG_2 , (long ) p >>32 , (u32 ) (long ) p); +if (emit_call (&prog, __cgroup_bpf_run_lsm_sock, prog)) +return -EINVAL ; + }else { +/* arg2: progs[i]->insnsi for interpreter */ +if (!p->jited) +emit_mov_imm64 (&prog,BPF_REG_2 , + (long ) p->insnsi >>32 , + (u32 ) (long ) p->insnsi); + +/* call JITed bpf program or interpreter */ +if (emit_call (&prog, p->bpf_func, prog)) +return -EINVAL ; + }/* * BPF_TRAMP_MODIFY_RETURN trampolines can modify the return
include/linux/bpf-cgroup-defs.h +4 -0
@@ -10,6 +10,8 @@struct bpf_prog_array ; +#define CGROUP_LSM_SOCK_NUM 2 +enum cgroup_bpf_attach_type {CGROUP_BPF_ATTACH_TYPE_INVALID =-1 ,CGROUP_INET_INGRESS =0 ,
@@ -35,6 +37,8 @@ enum cgroup_bpf_attach_type {CGROUP_INET4_GETSOCKNAME ,CGROUP_INET6_GETSOCKNAME ,CGROUP_INET_SOCK_RELEASE , +CGROUP_LSM_SOCK_START , +CGROUP_LSM_SOCK_END =CGROUP_LSM_SOCK_START +CGROUP_LSM_SOCK_NUM , MAX_CGROUP_BPF_ATTACH_TYPE };
include/linux/bpf.h +2 -0
@@ -975,6 +975,7 @@ struct bpf_prog_aux {u64 load_time;/* ns since boottime */ u32 verified_insns;struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE ]; +int cgroup_atype;/* enum cgroup_bpf_attach_type */ char name[BPF_OBJ_NAME_LEN ];#ifdef CONFIG_SECURITY void *security;
@@ -2367,6 +2368,7 @@ void *bpf_arch_text_copy(void *dst, void *src, size_t len);struct btf_id_set ;bool btf_id_set_contains (const struct btf_id_set *set,u32 id); +int btf_id_set_index (const struct btf_id_set *set,u32 id);#define MAX_BPRINTF_VARARGS 12
include/uapi/linux/bpf.h +1 -0
@@ -997,6 +997,7 @@ enum bpf_attach_type {BPF_SK_REUSEPORT_SELECT ,BPF_SK_REUSEPORT_SELECT_OR_MIGRATE ,BPF_PERF_EVENT , +BPF_LSM_CGROUP_SOCK , __MAX_BPF_ATTACH_TYPE };
kernel/bpf/btf.c +10 -0
@@ -4928,6 +4928,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,if (arg == nr_args) {switch (prog->expected_attach_type) { +case BPF_LSM_CGROUP_SOCK :case BPF_LSM_MAC :case BPF_TRACE_FEXIT :/* When LSM programs are attached to void LSM hooks
@@ -6338,6 +6339,15 @@ static int btf_id_cmp_func(const void *a, const void *b)return *pa - *pb; } +int btf_id_set_index (const struct btf_id_set *set,u32 id) +{ +const u32 *p; + p =bsearch (&id, set->ids, set->cnt,sizeof (u32), btf_id_cmp_func); +if (!p) +return -1 ; +return p - set->ids; +} +bool btf_id_set_contains (const struct btf_id_set *set,u32 id) {return bsearch (&id, set->ids, set->cnt,sizeof (u32), btf_id_cmp_func) !=NULL ;
kernel/bpf/cgroup.c +40 -3
67 lines not on this page · raw message
kernel/bpf/syscall.c +5 -1
@@ -2724,7 +2724,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, }break ;case BPF_PROG_TYPE_LSM : -if (prog->expected_attach_type !=BPF_LSM_MAC ) { +if (prog->expected_attach_type !=BPF_LSM_MAC && + prog->expected_attach_type !=BPF_LSM_CGROUP_SOCK ) { err = -EINVAL ; goto out_put_prog; }
@@ -3184,6 +3185,8 @@ attach_type_to_prog_type(enum bpf_attach_type attach_type)return BPF_PROG_TYPE_SK_LOOKUP ;case BPF_XDP :return BPF_PROG_TYPE_XDP ; +case BPF_LSM_CGROUP_SOCK : +return BPF_PROG_TYPE_LSM ;default :return BPF_PROG_TYPE_UNSPEC ; }
@@ -3237,6 +3240,7 @@ static int bpf_prog_attach(const union bpf_attr *attr)case BPF_PROG_TYPE_CGROUP_SOCKOPT :case BPF_PROG_TYPE_CGROUP_SYSCTL :case BPF_PROG_TYPE_SOCK_OPS : +case BPF_PROG_TYPE_LSM : ret =cgroup_bpf_prog_attach (attr, ptype, prog);break ;default :
kernel/bpf/trampoline.c +1 -0
@@ -406,6 +406,7 @@ static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog)return BPF_TRAMP_MODIFY_RETURN ;case BPF_TRACE_FEXIT :return BPF_TRAMP_FEXIT ; +case BPF_LSM_CGROUP_SOCK :case BPF_LSM_MAC :if (!prog->aux->attach_func_proto->type)/* The function returns void, we cannot modify its
kernel/bpf/verifier.c +1 -0
@@ -14105,6 +14105,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, fallthrough;case BPF_MODIFY_RETURN :case BPF_LSM_MAC : +case BPF_LSM_CGROUP_SOCK :case BPF_TRACE_FENTRY :case BPF_TRACE_FEXIT :if (!btf_type_is_func (t)) {
tools/include/uapi/linux/bpf.h +1 -0
@@ -997,6 +997,7 @@ enum bpf_attach_type {BPF_SK_REUSEPORT_SELECT ,BPF_SK_REUSEPORT_SELECT_OR_MIGRATE ,BPF_PERF_EVENT , +BPF_LSM_CGROUP_SOCK , __MAX_BPF_ATTACH_TYPE };
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
RFC 2/4 bpf: allow writing to sock->sk_priority from lsm progtype
In reply to Stanislav FomichevOpen up enough fields for selftests. Will be extended in the real patch series to match bpf_sock fields.
--- kernel/bpf/bpf_lsm.c | 49 +++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 3 ++- 2 files changed, 51 insertions(+), 1 deletion(-)
kernel/bpf/bpf_lsm.c +49 -0
@@ -222,7 +222,56 @@ bool bpf_lsm_is_sleepable_hook(u32 btf_id)const struct bpf_prog_ops lsm_prog_ops = { }; +static int lsm_btf_struct_access (struct bpf_verifier_log *log, +const struct btf *btf, +const struct btf_type *t,int off, +int size,enum bpf_access_type atype, +u32 *next_btf_id, +enum bpf_type_flag *flag) +{ +const struct btf_type *sock_type; +struct btf *btf_vmlinux; +s32 type_id; +size_t end; + +if (atype ==BPF_READ ) +return btf_struct_access (log, btf, t, off, size, atype, next_btf_id, + flag); + + btf_vmlinux =bpf_get_btf_vmlinux (); + + type_id =btf_find_by_name_kind (btf_vmlinux,"sock" ,BTF_KIND_STRUCT ); +if (type_id <0 ) +return -EINVAL ; + + sock_type =btf_type_by_id (btf_vmlinux, type_id); + +if (t != sock_type) { +bpf_log (log,"only 'struct sock' writes are supported\n" ); +return -EACCES ; + } + +switch (off) { + case bpf_ctx_range(struct sock, sk_priority): + end = offsetofend(struct sock, sk_priority); +break ; +default : +bpf_log (log,"no write support to 'struct sock' at off %d\n" , off); +return -EACCES ; + } + +if (off + size > end) { +bpf_log (log, +"write access at off %d with size %d beyond the member of 'struct sock' ended at %zu\n" , + off, size, end); +return -EACCES ; + } + +return NOT_INIT ; +} +const struct bpf_verifier_ops lsm_verifier_ops = { .get_func_proto = bpf_lsm_func_proto, .is_valid_access = btf_ctx_access, + .btf_struct_access = lsm_btf_struct_access, };
kernel/bpf/verifier.c +2 -1
@@ -12801,7 +12801,8 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env) insn->code =BPF_LDX |BPF_PROBE_MEM |BPF_SIZE ((insn)->code); env->prog->aux->num_exentries++; - }else if (resolve_prog_type (env->prog) !=BPF_PROG_TYPE_STRUCT_OPS ) { + }else if (resolve_prog_type (env->prog) !=BPF_PROG_TYPE_STRUCT_OPS && +resolve_prog_type (env->prog) !=BPF_PROG_TYPE_LSM ) {verbose (env,"Writes through BTF pointers are not allowed\n" );return -EINVAL ; }
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
RFC 3/4 libbpf: add lsm_cgoup_sock type
In reply to Stanislav Fomichevlsm+expected_attach_type=BPF_LSM_CGROUP_SOCK
--- tools/lib/bpf/libbpf.c | 2 ++ 1 file changed, 2 insertions(+)
tools/lib/bpf/libbpf.c +2 -0
@@ -8615,6 +8615,7 @@ static const struct bpf_sec_def section_defs[] = {SEC_DEF ("freplace/" ,EXT ,0 ,SEC_ATTACH_BTF , attach_trace),SEC_DEF ("lsm/" ,LSM ,BPF_LSM_MAC ,SEC_ATTACH_BTF , attach_lsm),SEC_DEF ("lsm.s/" ,LSM ,BPF_LSM_MAC ,SEC_ATTACH_BTF |SEC_SLEEPABLE , attach_lsm), +SEC_DEF ("lsm_cgroup_sock/" ,LSM ,BPF_LSM_CGROUP_SOCK ,SEC_ATTACH_BTF , attach_lsm),SEC_DEF ("iter/" ,TRACING ,BPF_TRACE_ITER ,SEC_ATTACH_BTF , attach_iter),SEC_DEF ("iter.s/" ,TRACING ,BPF_TRACE_ITER ,SEC_ATTACH_BTF |SEC_SLEEPABLE , attach_iter), SEC_DEF("syscall", SYSCALL, 0, SEC_SLEEPABLE),
@@ -8930,6 +8931,7 @@ void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type, *kind =BTF_KIND_TYPEDEF ;break ;case BPF_LSM_MAC : +case BPF_LSM_CGROUP_SOCK : *prefix =BTF_LSM_PREFIX ; *kind =BTF_KIND_FUNC ;break ;
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
RFC 4/4 selftest: lsm_cgroup_sock sample usage
In reply to Stanislav FomichevImplement two things I'd like to get from this:
1. apply default sk_priority policy
2. permit TX-only AF_PACKET socket
--- .../bpf/prog_tests/lsm_cgroup_sock.c | 81 +++++++++++++++++++ .../selftests/bpf/progs/lsm_cgroup_sock.c | 55 +++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_cgroup_sock.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_cgroup_sock.c
tools/testing/selftests/bpf/prog_tests/lsm_cgroup_sock.c new file +81 -0
@@ -0,0 +1,81 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <sys/types.h> +#include <sys/socket.h> +#include <test_progs.h> + +#include "lsm_cgroup_sock.skel.h" + +void test_lsm_cgroup_sock (void ) +{ +int post_create_prog_fd =-1 , bind_prog_fd =-1 ; +struct lsm_cgroup_sock *skel =NULL ; +int cgroup_fd, err, fd, prio; +socklen_t socklen; + + + cgroup_fd =test__join_cgroup ("/sock_policy" ); +if (!ASSERT_GE (cgroup_fd,0 ,"join_cgroup" )) + goto close_skel; + + skel =lsm_cgroup_sock__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_and_load" )) + goto close_cgroup; + + err =lsm_cgroup_sock__attach (skel); +if (!ASSERT_OK (err,"attach" )) + goto close_cgroup; + + post_create_prog_fd =bpf_program__fd (skel->progs.socket_post_create); + bind_prog_fd =bpf_program__fd (skel->progs.socket_bind); + + err =bpf_prog_attach (post_create_prog_fd, cgroup_fd,BPF_LSM_CGROUP_SOCK ,0 ); +if (!ASSERT_OK (err,"attach post_create_prog_fd" )) + goto close_cgroup; + + err =bpf_prog_attach (bind_prog_fd, cgroup_fd,BPF_LSM_CGROUP_SOCK ,0 ); +if (!ASSERT_OK (err,"attach bind_prog_fd" )) + goto detach_cgroup; + +/* AF_INET6 gets default policy (sk_priority). +*/ + + fd =socket (AF_INET6 ,SOCK_STREAM ,0 ); +if (!ASSERT_GE (fd,0 ,"socket(SOCK_STREAM)" )) + goto detach_cgroup; + + prio =0 ; + socklen =sizeof (prio); +ASSERT_GE (getsockopt (fd,SOL_SOCKET ,SO_PRIORITY , &prio, &socklen),0 ,"getsockopt" ); +ASSERT_EQ (prio,123 ,"sk_priority" ); + +close (fd); + +/* TX-only AF_PACKET is allowed. +*/ + +ASSERT_LT (socket (AF_PACKET ,SOCK_RAW ,htons (ETH_P_ALL )),0 ,"socket(AF_PACKET, ..., ETH_P_ALL)" ); + + fd =socket (AF_PACKET ,SOCK_RAW ,0 ); +ASSERT_GE (fd,0 ,"socket(AF_PACKET, ..., 0)" ); + +/* TX-only AF_PACKET can not be rebound. +*/ + +struct sockaddr_ll sa = { + .sll_family =AF_PACKET , + .sll_protocol =htons (ETH_P_ALL ), + }; +ASSERT_LT (bind (fd, (struct sockaddr *)&sa,sizeof (sa)),0 ,"bind(ETH_P_ALL)" ); + +close (fd); + +detach_cgroup: +bpf_prog_detach2 (post_create_prog_fd, cgroup_fd,BPF_LSM_CGROUP_SOCK ); +bpf_prog_detach2 (bind_prog_fd, cgroup_fd,BPF_LSM_CGROUP_SOCK ); + +close_cgroup: +close (cgroup_fd); +close_skel: +lsm_cgroup_sock__destroy (skel); +}
tools/testing/selftests/bpf/progs/lsm_cgroup_sock.c new file +55 -0
55 lines not on this page · raw message
Signature · 1 line
2.35.1.265.g69c8d7142f-goog
kernel test robot
build botreproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/054de9533f9ed11c320c7d1411504b95d80be131 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags…
Hi Stanislav,
[FYI, it's a private test report for your RFC patch.] [auto build test WARNING on bpf-next/master]
url: https://github.com/0day-ci/linux/commits/Stanislav-Fomichev/bpf-cgroup_sock-lsm-flavor/20220216-081444 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: x86_64-allyesconfig (https://download.01.org/0day-ci/archive/20220216/202202161116.E0tzCXNk-lkp(a)intel.com/config) compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/054de9533f9ed11c320c7d1411504b95d80be131
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Stanislav-Fomichev/bpf-cgroup_sock-lsm-flavor/20220216-081444 git checkout 054de9533f9ed11c320c7d1411504b95d80be131
# save the config file to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash kernel/bpf/
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> kernel/bpf/cgroup.c:1115:5: warning: no previous prototype for '__cgroup_bpf_run_lsm_sock' [-Wmissing-prototypes]
1115 | int __cgroup_bpf_run_lsm_sock(u64 *regs, const struct bpf_prog *prog)
| ^~~~~~~~~~~~~~~~~~~~~~~~~
vim +/__cgroup_bpf_run_lsm_sock +1115 kernel/bpf/cgroup.c
1114
> 1115 int __cgroup_bpf_run_lsm_sock(u64 *regs, const struct bpf_prog *prog)
1116 {
1117 struct socket *sock = (void *)regs[BPF_REG_0];
1118 struct cgroup *cgrp;
1119 struct sock *sk;
1120
1121 sk = sock->sk;
1122 if (!sk)
1123 return 0;
1124
1125 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
1126
1127 return BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[prog->aux->cgroup_atype],
1128 regs, bpf_prog_run, 0);
1129 }
1130
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Quoted 19 lines · Stanislav Fomichev
{
arch/x86/net/bpf_jit_comp.c +6
-8@@ -1767,14 +1769,23 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,/* arg1: lea rdi, [rbp - stack_size] */ EMIT4 (0x48 ,0x8D ,0x7D , -stack_size); -/* arg2: progs[i]->insnsi for interpreter */ -if (!p->jited) -emit_mov_imm64 (&prog,BPF_REG_2 , - (long ) p->insnsi >>32 , - (u32 ) (long ) p->insnsi); -/* call JITed bpf program or interpreter */ -if (emit_call (&prog, p->bpf_func, prog)) -return -EINVAL ; + +if (p->expected_attach_type ==BPF_LSM_CGROUP_SOCK ) { +/* arg2: progs[i] */ +emit_mov_imm64 (&prog,BPF_REG_2 , (long ) p >>32 , (u32 ) (long ) p); +if (emit_call (&prog, __cgroup_bpf_run_lsm_sock, prog)) +return -EINVAL ;
arch/x86/net/bpf_jit_comp.c static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog, · line 1787
Quoted 10 lines
arch/x86/net/bpf_jit_comp.c +10
-0+ }else { +/* arg2: progs[i]->insnsi for interpreter */ +if (!p->jited) +emit_mov_imm64 (&prog,BPF_REG_2 , + (long ) p->insnsi >>32 , + (u32 ) (long ) p->insnsi); + +/* call JITed bpf program or interpreter */ +if (emit_call (&prog, p->bpf_func, prog)) +return -EINVAL ;
Overall I think it's a workable solution.
As far as mechanism I think it would be better
to allocate single dummy bpf_prog and use normal fmod_ret registration mechanism instead of hacking arch trampoline bits. Set dummy_bpf_prog->bpf_func = __cgroup_bpf_run_lsm_sock; and keep as dummy_bpf_prog->jited = false;
From p->insnsi pointer in arg2 it's easy to go back to struct bpf_prog. Such dummy prog might even be statically defined like dummy_bpf_prog. Or allocated dynamically once.
It can be added as fmod_ret to multiple trampolines.
Just gut the func_model check.
As far as api the attach should probably be to a cgroup+lsm_hook pair. link_create.target_fd will be cgroup_fd.
At prog load time attach_btf_id should probably be one
of existing bpf_lsm_* hooks.
Feels wrong to duplicate the whole set into lsm_cgroup_sock set.
It's fine to have prog->expected_attach_type == BPF_LSM_CGROUP_SOCK to disambiguate. Will we probably only have two:BPF_LSM_CGROUP_SOCK and BPF_LSM_CGROUP_TASK ?
Quoted 5 lines
kernel/bpf/cgroup.c +5
-0+int __cgroup_bpf_run_lsm_sock (u64 *regs,const struct bpf_prog *prog) +{ +struct socket *sock = (void *)regs[BPF_REG_0 ]; +struct cgroup *cgrp; +struct sock *sk;
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_skb(struct sock *sk, · line 1129
Quoted 10 lines
kernel/bpf/cgroup.c +10
-0+ + sk = sock->sk; +if (!sk) +return 0 ; + + cgrp =sock_cgroup_ptr (&sk->sk_cgrp_data); + +return BPF_PROG_RUN_ARRAY_CG (cgrp->bpf.effective[prog->aux->cgroup_atype], + regs, bpf_prog_run,0 ); +}
Would it be fast enough?
We went through a bunch of optimization for normal cgroup and ended with:
if (cgroup_bpf_enabled(CGROUP_INET_INGRESS) &&
cgroup_bpf_sock_enabled(sk, CGROUP_INET_INGRESS))Here the trampoline code plus call into __cgroup_bpf_run_lsm_sock will be there for all cgroups.
Since cgroup specific check will be inside BPF_PROG_RUN_ARRAY_CG. I suspect it's ok, since the link_create will be for few specific lsm hooks which are typically not in the fast path.
Unlike traditional cgroup hook like ingress that is hot.
For BPF_LSM_CGROUP_TASK it will take cgroup from current instead of sock, right? Args access should magically work. 'regs' above should be fine for all lsm hooks.
The typical prog:
+SEC("lsm_cgroup_sock/socket_post_create")
+int BPF_PROG(socket_post_create, struct socket *sock, int family,
+ int type, int protocol, int kern)
looks good too.
Feel natural.
I guess they can be sleepable too?
Quoted 35 lines · Alexei Starovoitov
Review static int invoke_bpf_prog(const struct
Quoted 2 lines · Stanislav Fomichev
{
(quoted diff) +0
-0@@ -1767,14 +1769,23 @@ static int invoke_bpf_prog(const struct
btf_func_model*m, u8 **pprog,Quoted 17 lines
arch/x86/net/bpf_jit_comp.c +6
-8/* arg1: lea rdi, [rbp - stack_size] */ EMIT4 (0x48 ,0x8D ,0x7D , -stack_size); -/* arg2: progs[i]->insnsi for interpreter */ -if (!p->jited) -emit_mov_imm64 (&prog,BPF_REG_2 , - (long ) p->insnsi >>32 , - (u32 ) (long ) p->insnsi); -/* call JITed bpf program or interpreter */ -if (emit_call (&prog, p->bpf_func, prog)) -return -EINVAL ; + +if (p->expected_attach_type ==BPF_LSM_CGROUP_SOCK ) { +/* arg2: progs[i] */ +emit_mov_imm64 (&prog,BPF_REG_2 , (long ) p >>32 , (u32 ) (long ) p); +if (emit_call (&prog, __cgroup_bpf_run_lsm_sock, prog)) +return -EINVAL ;arch/x86/net/bpf_jit_comp.c static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog, · line 1787
Quoted 10 lines
arch/x86/net/bpf_jit_comp.c +10
-0+ }else { +/* arg2: progs[i]->insnsi for interpreter */ +if (!p->jited) +emit_mov_imm64 (&prog,BPF_REG_2 , + (long ) p->insnsi >>32 , + (u32 ) (long ) p->insnsi); + +/* call JITed bpf program or interpreter */ +if (emit_call (&prog, p->bpf_func, prog)) +return -EINVAL ;Overall I think it's a workable solution.
As far as mechanism I think it would be better to allocate single dummybpf_progand use normalfmod_ret
Quoted 8 lines
registration mechanism instead of hacking arch trampoline bits. Set
dummy_bpf_prog->bpf_func=__cgroup_bpf_run_lsm_sock; and keep asdummy_bpf_prog->jited= false;
Fromp->insnsipointer in arg2 it's easy to go back to structbpf_prog.
Such dummy prog might even be statically defined likedummy_bpf_prog. Or allocated dynamically once.
It can be added asfmod_retto multiple trampolines.
Just gut thefunc_modelcheck.
Oooh, that's much cleaner, thanks!
Quoted 5 lines
As far as api the attach should probably be to a cgroup+lsm_hook pair. link_create.target_fd will be
cgroup_fd.
At prog load timeattach_btf_idshould probably be one
of existing bpf_lsm_* hooks.
Feels wrong to duplicate the whole set intolsm_cgroup_sockset.
lsm_cgroup_sock is there to further limit which particular lsm hooks BPF_LSM_CGROUP_SOCK can use. I guess I can maybe look at BTF's first argument to verify that it's 'struct socket'? Let me try to see whether it's a good alternative..
Quoted 3 lines
It's fine to have
prog->expected_attach_type==BPF_LSM_CGROUP_SOCKto disambiguate. Will we probably only have two:BPF_LSM_CGROUP_SOCKandBPF_LSM_CGROUP_TASK?
I hope so. Unless objects other than socket and task can have cgroup association.
Quoted 19 lines
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_skb(struct sock *sk,
Quoted 13 lines
kernel/bpf/cgroup.c +13
-0+int __cgroup_bpf_run_lsm_sock (u64 *regs,const struct bpf_prog *prog) +{ +struct socket *sock = (void *)regs[BPF_REG_0 ]; +struct cgroup *cgrp; +struct sock *sk; + + sk = sock->sk; +if (!sk) +return 0 ; + + cgrp =sock_cgroup_ptr (&sk->sk_cgrp_data); + +return BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[prog->aux->cgroup_atype],
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_skb(struct sock *sk, · line 1129
Quoted 2 lines
kernel/bpf/cgroup.c +2
-0+ regs, bpf_prog_run, 0); +}Would it be fast enough?
We went through a bunch of optimization for normal cgroup and ended with:
Quoted 8 lines
if (cgroup_bpf_enabled(CGROUP_INET_INGRESS) && cgroup_bpf_sock_enabled(sk, CGROUP_INET_INGRESS))Here the trampoline code plus call into
__cgroup_bpf_run_lsm_sockwill be there for all cgroups.
Since cgroup specific check will be insideBPF_PROG_RUN_ARRAY_CG. I suspect it's ok, since thelink_createwill be for few specific lsm hooks which are typically not in the fast path.
Unlike traditional cgroup hook like ingress that is hot.
Right, cgroup_bpf_enabled() is not needed because lsm is by definition off/unattached by default. Seems like we can add cgroup_bpf_sock_enabled() to __cgroup_bpf_run_lsm_sock.
Quoted 1 line
For
BPF_LSM_CGROUP_TASKit will take cgroup from current instead of sock, right?
Right. Seems like the only difference is where we get the cgroup pointer
from: current vs sock->cgroup. Although, I'm a bit unsure whether to allow hooks that are clearly sock-cgroup-based to use BPF_LSM_CGROUP_TASK. For example, should we allow BPF_LSM_CGROUP_TASK to attach to that socket_post_create? I'd prohibit that at least initially to avoid some subtle 'why sometimes my
programs trigger on the wrong cgroup' types of issues.
Quoted 10 lines
Args access should magically work. 'regs' above should be fine for all lsm hooks.
The typical prog:
(quoted diff) +3
-0+SEC("lsm_cgroup_sock/socket_post_create") +int BPF_PROG(socket_post_create, struct socket *sock, int family, + int type, int protocol, int kern)looks good too.
Feel natural.
I guess they can be sleepable too?
Haven't gone into the sleepable world, but I don't see any reason why there couldn't be a sleepable variation.
Thank you for a review!
Quoted 10 lines · On Thu, Feb 17, 2022 at 8:21 AM <[email protected]> wrote:
Quoted 5 lines
As far as api the attach should probably be to a cgroup+lsm_hook pair. link_create.target_fd will be
cgroup_fd.
At prog load timeattach_btf_idshould probably be one
of existing bpf_lsm_* hooks.
Feels wrong to duplicate the whole set intolsm_cgroup_sockset.
lsm_cgroup_sockis there to further limit which particular lsm hooksBPF_LSM_CGROUP_SOCKcan use. I guess I can maybe look at BTF's first argument to verify that it's 'struct socket'? Let me try to see whether it's a good alternative..
That's a great idea.
We probably would need 2 flavors of __cgroup_bpf_run_lsm_sock wrapper. One for 'struct socket *' and another for 'struct sock *'.
In lsm hooks they come as the first argument and BTF will tell us what it is. There are exceptions like socket_create hook
which would have to use current->cgroup.
So ideally we can have a single attach_type BPF_LSM_CGROUP
and use proper wrapper socket/sock/current depending on BTF of the lsm hook.
Quoted 12 lines
Quoted 11 lines
Would it be fast enough?
We went through a bunch of optimization for normal cgroup and ended with:if (cgroup_bpf_enabled(CGROUP_INET_INGRESS) && cgroup_bpf_sock_enabled(sk, CGROUP_INET_INGRESS))Here the trampoline code plus call into
__cgroup_bpf_run_lsm_sockwill be there for all cgroups.
Since cgroup specific check will be insideBPF_PROG_RUN_ARRAY_CG. I suspect it's ok, since thelink_createwill be for few specific lsm hooks
which are typically not in the fast path.
Unlike traditional cgroup hook like ingress that is hot.
Quoted 4 lines
Right,
cgroup_bpf_enabled()is not needed because lsm is by definition off/unattached by default. Seems like we can addcgroup_bpf_sock_enabled()to__cgroup_bpf_run_lsm_sock.
I guess we can, but that feels redundant.
If we attach the wrapper to a particular lsm hook then cgroup_bpf_sock is surely enabled.
Quoted 4 lines
Quoted 3 lines
For
BPF_LSM_CGROUP_TASKit will take cgroup from current instead of sock, right?
Quoted 8 lines
Right. Seems like the only difference is where we get the cgroup pointer
from: current vssock->cgroup. Although, I'm a bit unsure whether to allow hooks that are clearly sock-cgroup-based to useBPF_LSM_CGROUP_TASK. For example, should we allowBPF_LSM_CGROUP_TASKto attach to thatsocket_post_create? I'd prohibit that at
least initially to avoid some subtle 'why sometimes my
programs trigger on the wrong cgroup' types of issues.
Agree. With a single BPF_LSM_CGROUP attach type will get correct behavior automatically.
Looking forward to non rfc patches. Exciting feature!