bpf: cgroup_sock lsm flavor
Subsystems (8)
PATCH v2 5/7 libbpf: add lsm_cgoup_sock type
In reply to Stanislav Fomichevlsm_cgroup/ is the prefix for BPF_LSM_CGROUP.
--- tools/lib/bpf/libbpf.c | 2 ++ 1 file changed, 2 insertions(+)
tools/lib/bpf/libbpf.c +2 -0
@@ -8667,6 +8667,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/" ,LSM ,BPF_LSM_CGROUP ,SEC_ATTACH_BTF ),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),
@@ -9088,6 +9089,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 : *prefix =BTF_LSM_PREFIX ; *kind =BTF_KIND_FUNC ;break ;
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
v2 cover bpf: cgroup_sock lsm flavor
This series implements new lsm flavor for attaching per-cgroup programs to existing lsm hooks. The cgroup is taken out of 'current', unless the first argument of the hook is 'struct socket'. In this case, the cgroup association is taken out of socket. The attachment looks like a regular per-cgroup attachment: we add new BPF_LSM_CGROUP attach type which, together with attach_btf_id, signals per-cgroup lsm. Behind the scenes, we allocate trampoline shim program and attach to lsm. This program looks up cgroup from current/socket and runs cgroup's effective prog array. The rest of the per-cgroup BPF stays the same: hierarchy, local storage, retval conventions (return 1 == success).
Current limitations:
* haven't considered sleepable bpf; can be extended later on
* not sure the verifier does the right thing with null checks; see latest selftest for details
* total of 10 (global) per-cgroup LSM attach points; this bloats bpf_cgroup a bit
v2:
- addressed build bot failures
Stanislav Fomichev (7):
bpf: add bpf_func_t and trampoline helpers
bpf: per-cgroup lsm flavor
bpf: minimize number of allocated lsm slots per program
bpf: allow writing to a subset of sock fields from lsm progtype
libbpf: add lsm_cgoup_sock type
selftests/bpf: lsm_cgroup functional test selftests/bpf: verify lsm_cgroup struct sock access
include/linux/bpf-cgroup-defs.h | 8 + include/linux/bpf.h | 24 +- include/linux/bpf_lsm.h | 8 + include/uapi/linux/bpf.h | 1 + kernel/bpf/bpf_lsm.c | 147 ++++++++++++ kernel/bpf/btf.c | 11 + kernel/bpf/cgroup.c | 209 ++++++++++++++++-- kernel/bpf/syscall.c | 10 + kernel/bpf/trampoline.c | 205 ++++++++++++++--- kernel/bpf/verifier.c | 4 +- tools/include/uapi/linux/bpf.h | 1 + tools/lib/bpf/libbpf.c | 2 + .../selftests/bpf/prog_tests/lsm_cgroup.c | 158 +++++++++++++ .../testing/selftests/bpf/progs/lsm_cgroup.c | 94 ++++++++ tools/testing/selftests/bpf/test_verifier.c | 54 ++++- .../selftests/bpf/verifier/lsm_cgroup.c | 34 +++ 16 files changed, 912 insertions(+), 58 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_cgroup.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_cgroup.c create mode 100644 tools/testing/selftests/bpf/verifier/lsm_cgroup.c
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
PATCH v2 7/7 selftests/bpf: verify lsm_cgroup struct sock access
sk_priority & sk_mark are writable, the rest is readonly.
Add new ldx_offset fixups to lookup the offset of struct field. Allow using test.kfunc regardless of prog_type.
One interesting thing here is that the verifier doesn't really force me to add NULL checks anywhere :-/
--- tools/testing/selftests/bpf/test_verifier.c | 54 ++++++++++++++++++- .../selftests/bpf/verifier/lsm_cgroup.c | 34 ++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/verifier/lsm_cgroup.c
tools/testing/selftests/bpf/test_verifier.c +53 -1
@@ -75,6 +75,12 @@ struct kfunc_btf_id_pair {int insn_idx; }; +struct ldx_offset { +const char *strct; +const char *field; +int insn_idx; +}; +struct bpf_test {const char *descr;struct bpf_insn insns[MAX_INSNS ];
@@ -102,6 +108,7 @@ struct bpf_test {int fixup_map_ringbuf[MAX_FIXUPS ];int fixup_map_timer[MAX_FIXUPS ];struct kfunc_btf_id_pair fixup_kfunc_btf_id[MAX_FIXUPS ]; +struct ldx_offset fixup_ldx[MAX_FIXUPS ];/* Expected verifier log output for result REJECT or VERBOSE_ACCEPT. * Can be a tab-separated sequence of expected strings. An empty string * means no log verification.
@@ -755,6 +762,7 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,int *fixup_map_ringbuf = test->fixup_map_ringbuf;int *fixup_map_timer = test->fixup_map_timer;struct kfunc_btf_id_pair *fixup_kfunc_btf_id = test->fixup_kfunc_btf_id; +struct ldx_offset *fixup_ldx = test->fixup_ldx;if (test->fill_helper) { test->fill_insns =calloc (MAX_TEST_INSNS ,sizeof (struct bpf_insn ));
@@ -967,6 +975,50 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type, fixup_kfunc_btf_id++; }while (fixup_kfunc_btf_id->kfunc); } + +if (fixup_ldx->strct) { +const struct btf_member *memb; +const struct btf_type *tp; +const char *name; +struct btf *btf; +int btf_id; +int off; +int i; + + btf =btf__load_vmlinux_btf (); + +do { + off =-1 ; +if (!btf) + goto next_ldx; + + btf_id =btf__find_by_name_kind (btf, + fixup_ldx->strct, +BTF_KIND_STRUCT ); +if (btf_id <0 ) + goto next_ldx; + + tp =btf__type_by_id (btf, btf_id); + memb =btf_members (tp); + +for (i =0 ; i <btf_vlen (tp); i++) { + name =btf__name_by_offset (btf, + memb->name_off); +if (strcmp (fixup_ldx->field, name) ==0 ) { + off = memb->offset /8 ; +break ; + } + memb++; + } + +next_ldx: + prog[fixup_ldx->insn_idx].off = off; + fixup_ldx++; + + }while (fixup_ldx->strct); + +btf__free (btf); + } }struct libcap {
@@ -1131,7 +1183,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv, opts.log_level =4 ; opts.prog_flags = pflags; -if (prog_type ==BPF_PROG_TYPE_TRACING && test->kfunc) { +if (test->kfunc) {int attach_btf_id; attach_btf_id = libbpf_find_vmlinux_btf_id(test->kfunc,
tools/testing/selftests/bpf/verifier/lsm_cgroup.c new file +34 -0
@@ -0,0 +1,34 @@ +#define SK_WRITABLE_FIELD (tp, field, size, res) \ +{ \ + .descr = field, \ + .insns = { \ +/* r1 = *(u64 *)(r1 + 0) */ \ +BPF_LDX_MEM (BPF_DW ,BPF_REG_1 ,BPF_REG_1 ,0 ), \ +/* r1 = *(u64 *)(r1 + offsetof(struct socket, sk)) */ \ +BPF_LDX_MEM (BPF_DW ,BPF_REG_1 ,BPF_REG_1 ,0 ), \ +/* r2 = *(u64 *)(r1 + offsetof(struct sock, <field>)) */ \ +BPF_LDX_MEM (size,BPF_REG_2 ,BPF_REG_1 ,0 ), \ +/* *(u64 *)(r1 + offsetof(struct sock, <field>)) = r2 */ \ +BPF_STX_MEM (size,BPF_REG_1 ,BPF_REG_2 ,0 ), \ +BPF_MOV64_IMM (BPF_REG_0 ,1 ), \ + BPF_EXIT_INSN(), \ + }, \ + .result = res, \ + .errstr = res ? "no write support to 'struct sock' at off" : "", \ + .prog_type = BPF_PROG_TYPE_LSM, \ + .expected_attach_type = BPF_LSM_CGROUP, \ + .kfunc = "socket_post_create", \ + .fixup_ldx = { \ + { "socket", "sk", 1 }, \ + { tp, field, 2 }, \ + { tp, field, 3 }, \ + }, \ +} + +SK_WRITABLE_FIELD ("sock_common" ,"skc_family" ,BPF_H ,REJECT ), +SK_WRITABLE_FIELD ("sock" ,"sk_sndtimeo" ,BPF_DW ,REJECT ), +SK_WRITABLE_FIELD ("sock" ,"sk_priority" ,BPF_W ,ACCEPT ), +SK_WRITABLE_FIELD ("sock" ,"sk_mark" ,BPF_W ,ACCEPT ), +SK_WRITABLE_FIELD ("sock" ,"sk_pacing_rate" ,BPF_DW ,REJECT ), + +#undef SK_WRITABLE_FIELD
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
PATCH v2 6/7 selftests/bpf: lsm_cgroup functional test
In reply to Stanislav FomichevFunctional test that exercises the following:
1. apply default sk_priority policy
2. permit TX-only AF_PACKET socket
3. cgroup attach/detach/replace
4. reusing trampoline shim
--- .../selftests/bpf/prog_tests/lsm_cgroup.c | 158 ++++++++++++++++++ .../testing/selftests/bpf/progs/lsm_cgroup.c | 94 +++++++++++ 2 files changed, 252 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_cgroup.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_cgroup.c
tools/testing/selftests/bpf/prog_tests/lsm_cgroup.c new file +158 -0
@@ -0,0 +1,158 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <sys/types.h> +#include <sys/socket.h> +#include <test_progs.h> + +#include "lsm_cgroup.skel.h" +#include "cgroup_helpers.h" + +void test_lsm_cgroup (void ) +{ +DECLARE_LIBBPF_OPTS (bpf_prog_attach_opts, attach_opts); +DECLARE_LIBBPF_OPTS (bpf_link_update_opts, update_opts); +int cgroup_fd, cgroup_fd2, err, fd, prio; +struct lsm_cgroup *skel =NULL ; +int post_create_prog_fd2 =-1 ; +int post_create_prog_fd =-1 ; +int bind_link_fd2 =-1 ; +int bind_prog_fd2 =-1 ; +int alloc_prog_fd =-1 ; +int bind_prog_fd =-1 ; +int bind_link_fd =-1 ; +socklen_t socklen; + + cgroup_fd =test__join_cgroup ("/sock_policy" ); +if (!ASSERT_GE (cgroup_fd,0 ,"join_cgroup" )) + goto close_skel; + + cgroup_fd2 =create_and_get_cgroup ("/sock_policy2" ); +if (!ASSERT_GE (cgroup_fd2,0 ,"create second cgroup" )) + goto close_skel; + + skel =lsm_cgroup__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_and_load" )) + goto close_cgroup; + + post_create_prog_fd =bpf_program__fd (skel->progs.socket_post_create); + post_create_prog_fd2 =bpf_program__fd (skel->progs.socket_post_create2); + bind_prog_fd =bpf_program__fd (skel->progs.socket_bind); + bind_prog_fd2 =bpf_program__fd (skel->progs.socket_bind2); + alloc_prog_fd =bpf_program__fd (skel->progs.socket_alloc); + + err =bpf_prog_attach (alloc_prog_fd, cgroup_fd,BPF_LSM_CGROUP ,0 ); +if (!ASSERT_OK (err,"attach alloc_prog_fd" )) + goto detach_cgroup; + +/* Make sure replacing works. +*/ + + err =bpf_prog_attach (post_create_prog_fd, cgroup_fd, +BPF_LSM_CGROUP ,0 ); +if (!ASSERT_OK (err,"attach post_create_prog_fd" )) + goto close_cgroup; + + attach_opts.replace_prog_fd = post_create_prog_fd; + err =bpf_prog_attach_opts (post_create_prog_fd2, cgroup_fd, +BPF_LSM_CGROUP , &attach_opts); +if (!ASSERT_OK (err,"prog replace post_create_prog_fd" )) + goto detach_cgroup; + +/* Try the same attach/replace via link API. +*/ + + bind_link_fd =bpf_link_create (bind_prog_fd, cgroup_fd, +BPF_LSM_CGROUP ,NULL ); +if (!ASSERT_GE (bind_link_fd,0 ,"link create bind_prog_fd" )) + goto detach_cgroup; + + update_opts.old_prog_fd = bind_prog_fd; + update_opts.flags =BPF_F_REPLACE ; + + err =bpf_link_update (bind_link_fd, bind_prog_fd2, &update_opts); +if (!ASSERT_OK (err,"link update bind_prog_fd" )) + goto detach_cgroup; + +/* Attach another instance of bind program to another cgroup. +* This should trigger the reuse of the trampoline shim (two +* programs attaching to the same btf_id). +*/ + + bind_link_fd2 =bpf_link_create (bind_prog_fd2, cgroup_fd2, +BPF_LSM_CGROUP ,NULL ); +if (!ASSERT_GE (bind_link_fd2,0 ,"link create bind_prog_fd2" )) + goto detach_cgroup; + +/* AF_UNIX is prohibited. +*/ + + fd =socket (AF_UNIX ,SOCK_STREAM ,0 ); +ASSERT_LT (fd,0 ,"socket(AF_UNIX)" ); + +/* 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); + +/* Make sure other cgroup doesn't trigger the programs. +*/ + +if (!ASSERT_OK (join_cgroup ("" ),"join root cgroup" )) + goto detach_cgroup; + + 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,0 ,"sk_priority" ); + +close (fd); + +detach_cgroup: +ASSERT_GE (bpf_prog_detach2 (post_create_prog_fd2, cgroup_fd, +BPF_LSM_CGROUP ),0 ,"detach_create" ); +close (bind_link_fd); +/* Don't close bind_link_fd2, exercise cgroup release cleanup. */ +ASSERT_GE (bpf_prog_detach2 (alloc_prog_fd, cgroup_fd, +BPF_LSM_CGROUP ),0 ,"detach_alloc" ); + +close_cgroup: +close (cgroup_fd); +close_skel: +lsm_cgroup__destroy (skel); +}
tools/testing/selftests/bpf/progs/lsm_cgroup.c new file +94 -0
94 lines not on this page · raw message
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
PATCH v2 1/7 bpf: add bpf_func_t and trampoline helpers
In reply to Stanislav FomichevI'll be adding lsm cgroup specific helpers that grab trampoline mutex.
No functional changes.
--- include/linux/bpf.h | 11 ++++--- kernel/bpf/trampoline.c | 63 +++++++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 34 deletions(-)
include/linux/bpf.h +5 -6
@@ -52,6 +52,8 @@ typedef u64 (*bpf_callback_t)(u64, u64, u64, u64, u64);typedef int (*bpf_iter_init_seq_priv_t )(void *private_data,struct bpf_iter_aux_info *aux);typedef void (*bpf_iter_fini_seq_priv_t )(void *private_data); +typedef unsigned int (*bpf_func_t )(const void *, +const struct bpf_insn *);struct bpf_iter_seq_info {const struct seq_operations *seq_ops;bpf_iter_init_seq_priv_t init_seq_private;
@@ -798,8 +800,7 @@ struct bpf_dispatcher { static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(const void *ctx,const struct bpf_insn *insnsi, -unsigned int (*bpf_func)(const void *, -const struct bpf_insn *)) +bpf_func_t bpf_func) {return bpf_func (ctx, insnsi); }
@@ -827,8 +828,7 @@ int arch_prepare_bpf_dispatcher(void *image, s64 *funcs, int num_funcs); noinline __nocfi unsigned int bpf_dispatcher_##name##_func( \const void *ctx, \const struct bpf_insn *insnsi, \ -unsigned int (*bpf_func)(const void *, \ -const struct bpf_insn *)) \ +bpf_func_t bpf_func) \ { \return bpf_func (ctx, insnsi); \ } \
@@ -839,8 +839,7 @@ int arch_prepare_bpf_dispatcher(void *image, s64 *funcs, int num_funcs); unsigned int bpf_dispatcher_##name##_func( \ const void *ctx, \ const struct bpf_insn *insnsi, \ - unsigned int (*bpf_func)(const void *, \ - const struct bpf_insn *)); \ + bpf_func_t bpf_func); \ extern struct bpf_dispatcher bpf_dispatcher_##name;#define BPF_DISPATCHER_FUNC (name) bpf_dispatcher_##name##_func#define BPF_DISPATCHER_PTR (name) (&bpf_dispatcher_##name)
kernel/bpf/trampoline.c +35 -28
107 lines not on this page · raw message
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
PATCH v2 4/7 bpf: allow writing to a subset of sock fields from lsm progtype
In reply to Stanislav FomichevFor now, allow only the obvious ones, like sk_priority and sk_mark.
--- kernel/bpf/bpf_lsm.c | 58 +++++++++++++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 3 ++- 2 files changed, 60 insertions(+), 1 deletion(-)
kernel/bpf/bpf_lsm.c +58 -0
@@ -332,7 +332,65 @@ 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 (); +if (!btf_vmlinux) { +bpf_log (log,"no vmlinux btf\n" ); +return -EOPNOTSUPP ; + } + + type_id =btf_find_by_name_kind (btf_vmlinux,"sock" ,BTF_KIND_STRUCT ); +if (type_id <0 ) { +bpf_log (log,"'struct sock' not found in vmlinux btf\n" ); +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 ; + case bpf_ctx_range(struct sock, sk_mark): + end = offsetofend(struct sock, sk_mark); +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
@@ -12843,7 +12843,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.1094.g7c7d902a7c-goog
PATCH v2 2/7 bpf: per-cgroup lsm flavor
In reply to Stanislav FomichevAllow attaching to lsm hooks in the cgroup context.
Attaching to per-cgroup LSM works exactly like attaching to other per-cgroup hooks. New BPF_LSM_CGROUP is added to trigger new mode; the actual lsm hook we attach to is signaled via existing attach_btf_id.
For the hooks that have 'struct socket' as its first argument, we use the cgroup associated with that socket. For the rest, we use 'current' cgroup (this is all on default hierarchy == v2 only). Note that for the hooks that work on 'struct sock' we still take the cgroup from 'current' because most of the time, the 'sock' argument is not properly initialized.
Behind the scenes, we allocate a shim program that is attached to the trampoline and runs cgroup effective BPF programs array. This shim has some rudimentary ref counting and can be shared between several programs attaching to the same per-cgroup lsm hook.
Note that this patch bloats cgroup size because we add 211 cgroup_bpf_attach_type(s) for simplicity sake. This will be addressed in the subsequent patch.
--- include/linux/bpf-cgroup-defs.h | 6 ++ include/linux/bpf.h | 13 +++ include/linux/bpf_lsm.h | 14 ++++ include/uapi/linux/bpf.h | 1 + kernel/bpf/bpf_lsm.c | 92 ++++++++++++++++++++ kernel/bpf/btf.c | 11 +++ kernel/bpf/cgroup.c | 116 ++++++++++++++++++++++--- kernel/bpf/syscall.c | 10 +++ kernel/bpf/trampoline.c | 144 ++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 1 + tools/include/uapi/linux/bpf.h | 1 + 11 files changed, 397 insertions(+), 12 deletions(-)
include/linux/bpf-cgroup-defs.h +6 -0
@@ -10,6 +10,8 @@struct bpf_prog_array ; +#define CGROUP_LSM_NUM 211/* will be addressed in the next patch */ +enum cgroup_bpf_attach_type {CGROUP_BPF_ATTACH_TYPE_INVALID =-1 ,CGROUP_INET_INGRESS =0 ,
@@ -35,6 +37,10 @@ enum cgroup_bpf_attach_type {CGROUP_INET4_GETSOCKNAME ,CGROUP_INET6_GETSOCKNAME ,CGROUP_INET_SOCK_RELEASE , +#ifdef CONFIG_BPF_LSM +CGROUP_LSM_START , +CGROUP_LSM_END =CGROUP_LSM_START +CGROUP_LSM_NUM -1 , +#endif MAX_CGROUP_BPF_ATTACH_TYPE };
include/linux/bpf.h +13 -0
@@ -807,6 +807,9 @@ static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(#ifdef CONFIG_BPF_JIT int bpf_trampoline_link_prog (struct bpf_prog *prog,struct bpf_trampoline *tr);int bpf_trampoline_unlink_prog (struct bpf_prog *prog,struct bpf_trampoline *tr); +int bpf_trampoline_link_cgroup_shim (struct bpf_prog *prog, +struct bpf_attach_target_info *tgt_info); +void bpf_trampoline_unlink_cgroup_shim (struct bpf_prog *prog);struct bpf_trampoline *bpf_trampoline_get (u64 key,struct bpf_attach_target_info *tgt_info);void bpf_trampoline_put (struct bpf_trampoline *tr);
@@ -865,6 +868,14 @@ static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog, {return -ENOTSUPP ; } +static inline int bpf_trampoline_link_cgroup_shim (struct bpf_prog *prog, +struct bpf_attach_target_info *tgt_info) +{ +return -EOPNOTSUPP ; +} +static inline void bpf_trampoline_unlink_cgroup_shim (struct bpf_prog *prog) +{ +}static inline struct bpf_trampoline *bpf_trampoline_get (u64 key,struct bpf_attach_target_info *tgt_info) {
@@ -980,6 +991,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;
@@ -2383,6 +2395,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/linux/bpf_lsm.h +14 -0
@@ -42,6 +42,9 @@ extern const struct bpf_func_proto bpf_inode_storage_get_proto;extern const struct bpf_func_proto bpf_inode_storage_delete_proto;void bpf_inode_storage_free (struct inode *inode); +int bpf_lsm_find_cgroup_shim (const struct bpf_prog *prog,bpf_func_t *bpf_func); +int bpf_lsm_hook_idx (u32 btf_id); +#else /* !CONFIG_BPF_LSM */ static inline bool bpf_lsm_is_sleepable_hook (u32 btf_id)
@@ -65,6 +68,17 @@ static inline void bpf_inode_storage_free(struct inode *inode) { } +static inline int bpf_lsm_find_cgroup_shim (const struct bpf_prog *prog, +bpf_func_t *bpf_func) +{ +return -ENOENT ; +} + +static inline int bpf_lsm_hook_idx (u32 btf_id) +{ +return -EINVAL ; +} +#endif /* CONFIG_BPF_LSM */ #endif /* _LINUX_BPF_LSM_H */
include/uapi/linux/bpf.h +1 -0
@@ -998,6 +998,7 @@ enum bpf_attach_type {BPF_SK_REUSEPORT_SELECT_OR_MIGRATE ,BPF_PERF_EVENT ,BPF_TRACE_KPROBE_MULTI , +BPF_LSM_CGROUP , __MAX_BPF_ATTACH_TYPE };
kernel/bpf/bpf_lsm.c +92 -0
98 lines not on this page · raw message
kernel/bpf/btf.c +11 -0
@@ -4971,6 +4971,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 :case BPF_LSM_MAC :case BPF_TRACE_FEXIT :/* When LSM programs are attached to void LSM hooks
@@ -6396,6 +6397,16 @@ 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 +104 -12
196 lines not on this page · raw message
kernel/bpf/syscall.c +10 -0
@@ -3139,6 +3139,11 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,return prog->enforce_expected_attach_type && prog->expected_attach_type != attach_type ? -EINVAL :0 ; +case BPF_PROG_TYPE_LSM : +if (prog->expected_attach_type !=BPF_LSM_CGROUP ) +return -EINVAL ; +return 0 ; +default :return 0 ; }
@@ -3194,6 +3199,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 : +return BPF_PROG_TYPE_LSM ;default :return BPF_PROG_TYPE_UNSPEC ; }
@@ -3247,6 +3254,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 :
@@ -3284,6 +3292,7 @@ static int bpf_prog_detach(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 :return cgroup_bpf_prog_detach (attr, ptype);default :return -EINVAL ;
@@ -4317,6 +4326,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)case BPF_PROG_TYPE_CGROUP_DEVICE :case BPF_PROG_TYPE_CGROUP_SYSCTL :case BPF_PROG_TYPE_CGROUP_SOCKOPT : +case BPF_PROG_TYPE_LSM : ret =cgroup_bpf_link_attach (attr, prog);break ;case BPF_PROG_TYPE_TRACING :
kernel/bpf/trampoline.c +144 -0
162 lines not on this page · raw message
kernel/bpf/verifier.c +1 -0
@@ -14168,6 +14168,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, fallthrough;case BPF_MODIFY_RETURN :case BPF_LSM_MAC : +case BPF_LSM_CGROUP :case BPF_TRACE_FENTRY :case BPF_TRACE_FEXIT :if (!btf_type_is_func (t)) {
tools/include/uapi/linux/bpf.h +1 -0
@@ -998,6 +998,7 @@ enum bpf_attach_type {BPF_SK_REUSEPORT_SELECT_OR_MIGRATE ,BPF_PERF_EVENT ,BPF_TRACE_KPROBE_MULTI , +BPF_LSM_CGROUP , __MAX_BPF_ATTACH_TYPE };
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
PATCH v2 3/7 bpf: minimize number of allocated lsm slots per program
In reply to Stanislav FomichevPrevious patch adds 1:1 mapping between all 211 LSM hooks and bpf_cgroup program array. Instead of reserving a slot per possible hook, reserve 10 slots per cgroup for lsm programs. Those slots are dynamically allocated on demand and reclaimed. This still adds some bloat to the cgroup and brings us back to roughly pre-cgroup_bpf_attach_type times.
It should be possible to eventually extend this idea to all hooks if the memory consumption is unacceptable and shrink overall effective programs array.
--- include/linux/bpf-cgroup-defs.h | 4 +- include/linux/bpf_lsm.h | 6 --- kernel/bpf/bpf_lsm.c | 9 ++-- kernel/bpf/cgroup.c | 95 ++++++++++++++++++++++++++++----- 4 files changed, 89 insertions(+), 25 deletions(-)
include/linux/bpf-cgroup-defs.h +3 -1
@@ -10,7 +10,9 @@struct bpf_prog_array ; -#define CGROUP_LSM_NUM 211/* will be addressed in the next patch */ +/* Maximum number of concurrently attachable per-cgroup LSM hooks. +*/ +#define CGROUP_LSM_NUM 10enum cgroup_bpf_attach_type {CGROUP_BPF_ATTACH_TYPE_INVALID =-1 ,
include/linux/bpf_lsm.h +0 -6
@@ -43,7 +43,6 @@ extern const struct bpf_func_proto bpf_inode_storage_delete_proto;void bpf_inode_storage_free (struct inode *inode);int bpf_lsm_find_cgroup_shim (const struct bpf_prog *prog,bpf_func_t *bpf_func); -int bpf_lsm_hook_idx (u32 btf_id);#else /* !CONFIG_BPF_LSM */
@@ -74,11 +73,6 @@ static inline int bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog,return -ENOENT ; } -static inline int bpf_lsm_hook_idx (u32 btf_id) -{ -return -EINVAL ; -} -#endif /* CONFIG_BPF_LSM */ #endif /* _LINUX_BPF_LSM_H */
kernel/bpf/bpf_lsm.c +3 -6
@@ -57,10 +57,12 @@ static unsigned int __cgroup_bpf_run_lsm_socket(const void *ctx,if (unlikely (!sk))return 0 ; +rcu_read_lock ();/* See bpf_lsm_attach_type_get(). */ cgrp =sock_cgroup_ptr (&sk->sk_cgrp_data);if (likely (cgrp)) ret =BPF_PROG_RUN_ARRAY_CG (cgrp->bpf.effective[prog->aux->cgroup_atype], ctx, bpf_prog_run,0 ); +rcu_read_unlock ();return ret; }
@@ -77,7 +79,7 @@ static unsigned int __cgroup_bpf_run_lsm_current(const void *ctx,/*prog = container_of(insn, struct bpf_prog, insnsi);*/ prog = (const struct bpf_prog *)((void *)insn - offsetof(struct bpf_prog , insnsi)); -rcu_read_lock (); +rcu_read_lock ();/* See bpf_lsm_attach_type_get(). */ cgrp =task_dfl_cgroup (current);if (likely (cgrp)) ret = BPF_PROG_RUN_ARRAY_CG(cgrp->bpf.effective[prog->aux->cgroup_atype],
@@ -122,11 +124,6 @@ int bpf_lsm_find_cgroup_shim(const struct bpf_prog *prog,return 0 ; } -int bpf_lsm_hook_idx (u32 btf_id) -{ -return btf_id_set_index (&bpf_lsm_hooks, btf_id); -} -int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog,const struct bpf_prog *prog) {
kernel/bpf/cgroup.c +83 -12
144 lines not on this page · raw message
Signature · 1 line
2.35.1.1094.g7c7d902a7c-goog
kernel test robot
build botRe: [PATCH bpf-next v2 2/7] bpf: per-cgroup lsm flavor
In reply to Stanislav FomichevThank 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/intel-lab-lkp/linux/commits/Stanislav-Fomichev/bpf-cgroup_sock-lsm-flavor/20220406-162419 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master config: powerpc-buildonly-randconfig-r003-20220406 (https://download.01.org/0day-ci/archive/20220407/[email protected]/config) compiler: powerpc-linux-gcc (GCC) 11.2.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/b416dabea64e9ab8418ffb26990f2d303b968f2d
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Stanislav-Fomichev/bpf-cgroup_sock-lsm-flavor/20220406-162419 git checkout b416dabea64e9ab8418ffb26990f2d303b968f2d
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=powerpc SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
powerpc-linux-ld: kernel/bpf/trampoline.o: in function `bpf_trampoline_compute_key':
>> include/linux/bpf_verifier.h:540: undefined reference to `btf_obj_id' >> powerpc-linux-ld: include/linux/bpf_verifier.h:540: undefined reference to `btf_obj_id'
vim +540 include/linux/bpf_verifier.h
ab3f0063c48c26c Jakub Kicinski 2017-11-03 521
be80a1d3f9dbe5a Daniel Borkmann 2022-01-10 522 int check_ptr_off_reg(struct bpf_verifier_env *env,
51c39bb1d5d105a Alexei Starovoitov 2020-01-09 523 const struct bpf_reg_state *reg, int regno);
25b35dd28138f61 Kumar Kartikeya Dwivedi 2022-03-05 524 int check_func_arg_reg_off(struct bpf_verifier_env *env,
25b35dd28138f61 Kumar Kartikeya Dwivedi 2022-03-05 525 const struct bpf_reg_state *reg, int regno,
24d5bb806c7e2c0 Kumar Kartikeya Dwivedi 2022-03-05 526 enum bpf_arg_type arg_type,
24d5bb806c7e2c0 Kumar Kartikeya Dwivedi 2022-03-05 527 bool is_release_func);
d583691c47dc042 Kumar Kartikeya Dwivedi 2022-01-14 528 int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
d583691c47dc042 Kumar Kartikeya Dwivedi 2022-01-14 529 u32 regno);
e5069b9c23b3857 Dmitrii Banshchikov 2021-02-13 530 int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
e5069b9c23b3857 Dmitrii Banshchikov 2021-02-13 531 u32 regno, u32 mem_size);
51c39bb1d5d105a Alexei Starovoitov 2020-01-09 532
f7b12b6fea00988 Toke Høiland-Jørgensen 2020-09-25 533 /* this lives here instead of in bpf.h because it needs to dereference tgt_prog */
f7b12b6fea00988 Toke Høiland-Jørgensen 2020-09-25 534 static inline u64 bpf_trampoline_compute_key(const struct bpf_prog *tgt_prog,
22dc4a0f5ed11b6 Andrii Nakryiko 2020-12-03 535 struct btf *btf, u32 btf_id)
f7b12b6fea00988 Toke Høiland-Jørgensen 2020-09-25 536 {
22dc4a0f5ed11b6 Andrii Nakryiko 2020-12-03 537 if (tgt_prog)
22dc4a0f5ed11b6 Andrii Nakryiko 2020-12-03 538 return ((u64)tgt_prog->aux->id << 32) | btf_id;
22dc4a0f5ed11b6 Andrii Nakryiko 2020-12-03 539 else
22dc4a0f5ed11b6 Andrii Nakryiko 2020-12-03 @540 return ((u64)btf_obj_id(btf) << 32) | 0x80000000 | btf_id;
f7b12b6fea00988 Toke Høiland-Jørgensen 2020-09-25 541 }
f7b12b6fea00988 Toke Høiland-Jørgensen 2020-09-25 542 Signature · 2 lines
0-DAY CI Kernel Test Service https://01.org/lkp
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 2/7] bpf: per-cgroup lsm flavor
In reply to Stanislav FomichevQuoted 220 lines · Stanislav Fomichev
Allow attaching to lsm hooks in the cgroup context.
Attaching to per-cgroup LSM works exactly like attaching to other per-cgroup hooks. New
BPF_LSM_CGROUPis added to trigger new mode; the actual lsm hook we attach to is signaled via existingattach_btf_id.For the hooks that have 'struct socket' as its first argument, we use the cgroup associated with that socket. For the rest, we use 'current' cgroup (this is all on default hierarchy == v2 only). Note that for the hooks that work on 'struct sock' we still take the cgroup from 'current' because most of the time, the 'sock' argument is not properly initialized.
Behind the scenes, we allocate a shim program that is attached to the trampoline and runs cgroup effective BPF programs array. This shim has some rudimentary ref counting and can be shared between several programs attaching to the same per-cgroup lsm hook.
Note that this patch bloats cgroup size because we add 211
cgroup_bpf_attach_type(s)for simplicity sake. This will be addressed in the subsequent patch.Signed-off-by Stanislav Fomichev <[email protected]>--- include/linux/bpf-cgroup-defs.h | 6 ++ include/linux/bpf.h | 13 +++ include/linux/bpf_lsm.h | 14 ++++ include/uapi/linux/bpf.h | 1 + kernel/bpf/bpf_lsm.c | 92 ++++++++++++++++++++ kernel/bpf/btf.c | 11 +++ kernel/bpf/cgroup.c | 116 ++++++++++++++++++++++--- kernel/bpf/syscall.c | 10 +++ kernel/bpf/trampoline.c | 144 ++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 1 + tools/include/uapi/linux/bpf.h | 1 + 11 files changed, 397 insertions(+), 12 deletions(-)include/linux/bpf-cgroup-defs.h +6
-0@@ -10,6 +10,8 @@struct bpf_prog_array ; +#define CGROUP_LSM_NUM 211/* will be addressed in the next patch */ +enum cgroup_bpf_attach_type {CGROUP_BPF_ATTACH_TYPE_INVALID =-1 ,CGROUP_INET_INGRESS =0 ,@@ -35,6 +37,10 @@ enum cgroup_bpf_attach_type {CGROUP_INET4_GETSOCKNAME ,CGROUP_INET6_GETSOCKNAME ,CGROUP_INET_SOCK_RELEASE , +#ifdef CONFIG_BPF_LSM +CGROUP_LSM_START , +CGROUP_LSM_END =CGROUP_LSM_START +CGROUP_LSM_NUM -1 , +#endif MAX_CGROUP_BPF_ATTACH_TYPE };include/linux/bpf.h +13
-0@@ -807,6 +807,9 @@ static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(#ifdef CONFIG_BPF_JIT int bpf_trampoline_link_prog (struct bpf_prog *prog,struct bpf_trampoline *tr);int bpf_trampoline_unlink_prog (struct bpf_prog *prog,struct bpf_trampoline *tr); +int bpf_trampoline_link_cgroup_shim (struct bpf_prog *prog, +struct bpf_attach_target_info *tgt_info); +void bpf_trampoline_unlink_cgroup_shim (struct bpf_prog *prog);struct bpf_trampoline *bpf_trampoline_get (u64 key,struct bpf_attach_target_info *tgt_info);void bpf_trampoline_put (struct bpf_trampoline *tr);@@ -865,6 +868,14 @@ static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog, {return -ENOTSUPP ; } +static inline int bpf_trampoline_link_cgroup_shim (struct bpf_prog *prog, +struct bpf_attach_target_info *tgt_info) +{ +return -EOPNOTSUPP ; +} +static inline void bpf_trampoline_unlink_cgroup_shim (struct bpf_prog *prog) +{ +}static inline struct bpf_trampoline *bpf_trampoline_get (u64 key,struct bpf_attach_target_info *tgt_info) {@@ -980,6 +991,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;@@ -2383,6 +2395,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 12include/linux/bpf_lsm.h +14
-0@@ -42,6 +42,9 @@ extern const struct bpf_func_proto bpf_inode_storage_get_proto;extern const struct bpf_func_proto bpf_inode_storage_delete_proto;void bpf_inode_storage_free (struct inode *inode); +int bpf_lsm_find_cgroup_shim (const struct bpf_prog *prog,bpf_func_t *bpf_func); +int bpf_lsm_hook_idx (u32 btf_id); +#else /* !CONFIG_BPF_LSM */ static inline bool bpf_lsm_is_sleepable_hook (u32 btf_id)@@ -65,6 +68,17 @@ static inline void bpf_inode_storage_free(struct inode *inode) { } +static inline int bpf_lsm_find_cgroup_shim (const struct bpf_prog *prog, +bpf_func_t *bpf_func) +{ +return -ENOENT ; +} + +static inline int bpf_lsm_hook_idx (u32 btf_id) +{ +return -EINVAL ; +} +#endif /* CONFIG_BPF_LSM */ #endif /* _LINUX_BPF_LSM_H */ include/uapi/linux/bpf.h +1
-0@@ -998,6 +998,7 @@ enum bpf_attach_type {BPF_SK_REUSEPORT_SELECT_OR_MIGRATE ,BPF_PERF_EVENT ,BPF_TRACE_KPROBE_MULTI , +BPF_LSM_CGROUP , __MAX_BPF_ATTACH_TYPE };kernel/bpf/bpf_lsm.c +61
-0@@ -35,6 +35,98 @@ BTF_SET_START(bpf_lsm_hooks)#undef LSM_HOOKBTF_SET_END (bpf_lsm_hooks) +static unsigned int __cgroup_bpf_run_lsm_socket(const void *ctx, +const struct bpf_insn *insn) +{ +const struct bpf_prog *prog; +struct socket *sock; +struct cgroup *cgrp; +struct sock *sk; +int ret =0 ; +u64 *regs; + + regs = (u64 *)ctx; + sock = (void *)(unsigned long )regs[BPF_REG_0 ]; +/*prog = container_of(insn, struct bpf_prog, insnsi);*/ + prog = (const struct bpf_prog *)((void *)insn - offsetof(struct bpf_prog , insnsi)); + +if (unlikely (!sock)) +return 0 ; + + sk = sock->sk; +if (unlikely (!sk)) +return 0 ; + + cgrp =sock_cgroup_ptr (&sk->sk_cgrp_data); +if (likely (cgrp)) + ret =BPF_PROG_RUN_ARRAY_CG (cgrp->bpf.effective[prog->aux->cgroup_atype], + ctx, bpf_prog_run,0 ); +return ret; +} + +static unsigned int __cgroup_bpf_run_lsm_current (const void *ctx, +const struct bpf_insn *insn) +{ +const struct bpf_prog *prog; +struct cgroup *cgrp; +int ret =0 ; + +if (unlikely (!current)) +return 0 ; + +/*prog = container_of(insn, struct bpf_prog, insnsi);*/ + prog = (const struct bpf_prog *)((void *)insn - offsetof(struct bpf_prog , insnsi)); + +rcu_read_lock (); + cgrp =task_dfl_cgroup (current); +if (likely (cgrp)) + ret =BPF_PROG_RUN_ARRAY_CG (cgrp->bpf.effective[prog->aux->cgroup_atype], + ctx, bpf_prog_run,0 ); +rcu_read_unlock (); +return ret; +} + +int bpf_lsm_find_cgroup_shim (const struct bpf_prog *prog, +bpf_func_t *bpf_func) +{ +const struct btf_type *first_arg_type; +const struct btf_type *sock_type; +const struct btf *btf_vmlinux; +const struct btf_param *args; +s32 type_id; + + if (!prog->aux->attach_func_proto ||
kernel/bpf/bpf_lsm.c BTF_SET_START(bpf_lsm_hooks) · line 108
Quoted 10 lines
kernel/bpf/bpf_lsm.c +10
-0+ !btf_type_is_func_proto(prog->aux->attach_func_proto)) +return -EINVAL ; + +if (btf_type_vlen (prog->aux->attach_func_proto) <1 ) +return -EINVAL ; + + args = (const struct btf_param *)(prog->aux->attach_func_proto +1 ); + + btf_vmlinux =bpf_get_btf_vmlinux (); +if (!btf_vmlinux)
We should use IS_ERR_OR_NULL to check the result, e.g. see:7ada3787e91c ("bpf: Check for NULL return from bpf_get_btf_vmlinux").
Quoted 1 line
[...]
--
Kartikeya
Re: [PATCH bpf-next v2 2/7] bpf: per-cgroup lsm flavor
Quoted 227 lines · Kumar Kartikeya Dwivedi
Quoted 225 lines · Stanislav Fomichev
Allow attaching to lsm hooks in the cgroup context.
Attaching to per-cgroup LSM works exactly like attaching to other per-cgroup hooks. New
BPF_LSM_CGROUPis added to trigger new mode; the actual lsm hook we attach to is signaled via existingattach_btf_id.For the hooks that have 'struct socket' as its first argument, we use the cgroup associated with that socket. For the rest, we use 'current' cgroup (this is all on default hierarchy == v2 only). Note that for the hooks that work on 'struct sock' we still take the cgroup from 'current' because most of the time, the 'sock' argument is not properly initialized.
Behind the scenes, we allocate a shim program that is attached to the trampoline and runs cgroup effective BPF programs array. This shim has some rudimentary ref counting and can be shared between several programs attaching to the same per-cgroup lsm hook.
Note that this patch bloats cgroup size because we add 211
cgroup_bpf_attach_type(s)for simplicity sake. This will be addressed in the subsequent patch.Signed-off-by Stanislav Fomichev <[email protected]>--- include/linux/bpf-cgroup-defs.h | 6 ++ include/linux/bpf.h | 13 +++ include/linux/bpf_lsm.h | 14 ++++ include/uapi/linux/bpf.h | 1 + kernel/bpf/bpf_lsm.c | 92 ++++++++++++++++++++ kernel/bpf/btf.c | 11 +++ kernel/bpf/cgroup.c | 116 ++++++++++++++++++++++--- kernel/bpf/syscall.c | 10 +++ kernel/bpf/trampoline.c | 144 ++++++++++++++++++++++++++++++++ kernel/bpf/verifier.c | 1 + tools/include/uapi/linux/bpf.h | 1 + 11 files changed, 397 insertions(+), 12 deletions(-)include/linux/bpf-cgroup-defs.h +6
-0@@ -10,6 +10,8 @@struct bpf_prog_array ; +#define CGROUP_LSM_NUM 211/* will be addressed in the next patch */ +enum cgroup_bpf_attach_type {CGROUP_BPF_ATTACH_TYPE_INVALID =-1 ,CGROUP_INET_INGRESS =0 ,@@ -35,6 +37,10 @@ enum cgroup_bpf_attach_type {CGROUP_INET4_GETSOCKNAME ,CGROUP_INET6_GETSOCKNAME ,CGROUP_INET_SOCK_RELEASE , +#ifdef CONFIG_BPF_LSM +CGROUP_LSM_START , +CGROUP_LSM_END =CGROUP_LSM_START +CGROUP_LSM_NUM -1 , +#endif MAX_CGROUP_BPF_ATTACH_TYPE };include/linux/bpf.h +13
-0@@ -807,6 +807,9 @@ static __always_inline __nocfi unsigned int bpf_dispatcher_nop_func(#ifdef CONFIG_BPF_JIT int bpf_trampoline_link_prog (struct bpf_prog *prog,struct bpf_trampoline *tr);int bpf_trampoline_unlink_prog (struct bpf_prog *prog,struct bpf_trampoline *tr); +int bpf_trampoline_link_cgroup_shim (struct bpf_prog *prog, +struct bpf_attach_target_info *tgt_info); +void bpf_trampoline_unlink_cgroup_shim (struct bpf_prog *prog);struct bpf_trampoline *bpf_trampoline_get (u64 key,struct bpf_attach_target_info *tgt_info);void bpf_trampoline_put (struct bpf_trampoline *tr);@@ -865,6 +868,14 @@ static inline int bpf_trampoline_unlink_prog(struct bpf_prog *prog, {return -ENOTSUPP ; } +static inline int bpf_trampoline_link_cgroup_shim (struct bpf_prog *prog, +struct bpf_attach_target_info *tgt_info) +{ +return -EOPNOTSUPP ; +} +static inline void bpf_trampoline_unlink_cgroup_shim (struct bpf_prog *prog) +{ +}static inline struct bpf_trampoline *bpf_trampoline_get (u64 key,struct bpf_attach_target_info *tgt_info) {@@ -980,6 +991,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;@@ -2383,6 +2395,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 12include/linux/bpf_lsm.h +14
-0@@ -42,6 +42,9 @@ extern const struct bpf_func_proto bpf_inode_storage_get_proto;extern const struct bpf_func_proto bpf_inode_storage_delete_proto;void bpf_inode_storage_free (struct inode *inode); +int bpf_lsm_find_cgroup_shim (const struct bpf_prog *prog,bpf_func_t *bpf_func); +int bpf_lsm_hook_idx (u32 btf_id); +#else /* !CONFIG_BPF_LSM */ static inline bool bpf_lsm_is_sleepable_hook (u32 btf_id)@@ -65,6 +68,17 @@ static inline void bpf_inode_storage_free(struct inode *inode) { } +static inline int bpf_lsm_find_cgroup_shim (const struct bpf_prog *prog, +bpf_func_t *bpf_func) +{ +return -ENOENT ; +} + +static inline int bpf_lsm_hook_idx (u32 btf_id) +{ +return -EINVAL ; +} +#endif /* CONFIG_BPF_LSM */ #endif /* _LINUX_BPF_LSM_H */ include/uapi/linux/bpf.h +1
-0@@ -998,6 +998,7 @@ enum bpf_attach_type {BPF_SK_REUSEPORT_SELECT_OR_MIGRATE ,BPF_PERF_EVENT ,BPF_TRACE_KPROBE_MULTI , +BPF_LSM_CGROUP , __MAX_BPF_ATTACH_TYPE };kernel/bpf/bpf_lsm.c +66
-0@@ -35,6 +35,98 @@ BTF_SET_START(bpf_lsm_hooks)#undef LSM_HOOKBTF_SET_END (bpf_lsm_hooks) +static unsigned int __cgroup_bpf_run_lsm_socket(const void *ctx, +const struct bpf_insn *insn) +{ +const struct bpf_prog *prog; +struct socket *sock; +struct cgroup *cgrp; +struct sock *sk; +int ret =0 ; +u64 *regs; + + regs = (u64 *)ctx; + sock = (void *)(unsigned long )regs[BPF_REG_0 ]; +/*prog = container_of(insn, struct bpf_prog, insnsi);*/ + prog = (const struct bpf_prog *)((void *)insn - offsetof(struct bpf_prog , insnsi)); + +if (unlikely (!sock)) +return 0 ; + + sk = sock->sk; +if (unlikely (!sk)) +return 0 ; + + cgrp =sock_cgroup_ptr (&sk->sk_cgrp_data); +if (likely (cgrp)) + ret =BPF_PROG_RUN_ARRAY_CG (cgrp->bpf.effective[prog->aux->cgroup_atype], + ctx, bpf_prog_run,0 ); +return ret; +} + +static unsigned int __cgroup_bpf_run_lsm_current (const void *ctx, +const struct bpf_insn *insn) +{ +const struct bpf_prog *prog; +struct cgroup *cgrp; +int ret =0 ; + +if (unlikely (!current)) +return 0 ; + +/*prog = container_of(insn, struct bpf_prog, insnsi);*/ + prog = (const struct bpf_prog *)((void *)insn - offsetof(struct bpf_prog , insnsi)); + +rcu_read_lock (); + cgrp =task_dfl_cgroup (current); +if (likely (cgrp)) + ret =BPF_PROG_RUN_ARRAY_CG (cgrp->bpf.effective[prog->aux->cgroup_atype], + ctx, bpf_prog_run,0 ); +rcu_read_unlock (); +return ret; +} + +int bpf_lsm_find_cgroup_shim (const struct bpf_prog *prog, +bpf_func_t *bpf_func) +{ +const struct btf_type *first_arg_type; +const struct btf_type *sock_type; +const struct btf *btf_vmlinux; +const struct btf_param *args; +s32 type_id; + +if (!prog->aux->attach_func_proto || + !btf_type_is_func_proto (prog->aux->attach_func_proto)) +return -EINVAL ; + +if (btf_type_vlen (prog->aux->attach_func_proto) <1 ) +return -EINVAL ;
Quoted 8 lines
kernel/bpf/bpf_lsm.c BTF_SET_START(bpf_lsm_hooks) · line 108
Quoted 5 lines
kernel/bpf/bpf_lsm.c +5
-0+ + args = (const struct btf_param *)(prog->aux->attach_func_proto +1 ); + + btf_vmlinux =bpf_get_btf_vmlinux (); +if (!btf_vmlinux)We should use
IS_ERR_OR_NULLto check the result, e.g. see:7ada3787e91c("bpf: Check for NULL return frombpf_get_btf_vmlinux").
Oh, thanks, will do!