From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932789AbcJZHFm (ORCPT ); Wed, 26 Oct 2016 03:05:42 -0400 Received: from smtp-sh.infomaniak.ch ([128.65.195.4]:40800 "EHLO smtp-sh.infomaniak.ch" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753369AbcJZG61 (ORCPT ); Wed, 26 Oct 2016 02:58:27 -0400 From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= To: linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Alexei Starovoitov , Andy Lutomirski , Daniel Borkmann , Daniel Mack , David Drysdale , "David S . Miller" , "Eric W . Biederman" , James Morris , Jann Horn , Kees Cook , Paul Moore , Sargun Dhillon , "Serge E . Hallyn" , Tejun Heo , Thomas Graf , Will Drewry , kernel-hardening@lists.openwall.com, linux-api@vger.kernel.org, linux-security-module@vger.kernel.org, netdev@vger.kernel.org, cgroups@vger.kernel.org Subject: [RFC v4 13/18] bpf/cgroup: Replace struct bpf_prog with struct bpf_object Date: Wed, 26 Oct 2016 08:56:49 +0200 Message-Id: <20161026065654.19166-14-mic@digikod.net> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20161026065654.19166-1-mic@digikod.net> References: <20161026065654.19166-1-mic@digikod.net> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Antivirus: Dr.Web (R) for Unix mail servers drweb plugin ver.6.0.2.8 X-Antivirus-Code: 0x100000 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This allows CONFIG_CGROUP_BPF to manage different type of pointers instead of only eBPF programs. This will be useful for the next commits to support Landlock with cgroups. Changes since v3: * do not use an union of pointers but a struct (suggested by Kees Cook) Signed-off-by: Mickaël Salaün Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: Daniel Mack Cc: David S. Miller Cc: Tejun Heo Link: https://lkml.kernel.org/r/CAGXu5j+F80XCPSVL0VxAAoTiYk5D1NKKC3jyAU=Z0Gi7L9S0aw@mail.gmail.com --- include/linux/bpf-cgroup.h | 8 ++++++-- kernel/bpf/cgroup.c | 35 ++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h index fc076de74ab9..2f608e5d94e9 100644 --- a/include/linux/bpf-cgroup.h +++ b/include/linux/bpf-cgroup.h @@ -14,14 +14,18 @@ struct sk_buff; extern struct static_key_false cgroup_bpf_enabled_key; #define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key) +struct bpf_object { + struct bpf_prog *prog; +}; + struct cgroup_bpf { /* * Store two sets of bpf_prog pointers, one for programs that are * pinned directly to this cgroup, and one for those that are effective * when this cgroup is accessed. */ - struct bpf_prog *prog[MAX_BPF_ATTACH_TYPE]; - struct bpf_prog *effective[MAX_BPF_ATTACH_TYPE]; + struct bpf_object pinned[MAX_BPF_ATTACH_TYPE]; + struct bpf_object effective[MAX_BPF_ATTACH_TYPE]; }; void cgroup_bpf_put(struct cgroup *cgrp); diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index a0ab43f264b0..75482cd92d56 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -20,18 +20,18 @@ DEFINE_STATIC_KEY_FALSE(cgroup_bpf_enabled_key); EXPORT_SYMBOL(cgroup_bpf_enabled_key); /** - * cgroup_bpf_put() - put references of all bpf programs + * cgroup_bpf_put() - put references of all bpf objects * @cgrp: the cgroup to modify */ void cgroup_bpf_put(struct cgroup *cgrp) { unsigned int type; - for (type = 0; type < ARRAY_SIZE(cgrp->bpf.prog); type++) { - struct bpf_prog *prog = cgrp->bpf.prog[type]; + for (type = 0; type < ARRAY_SIZE(cgrp->bpf.pinned); type++) { + struct bpf_object pinned = cgrp->bpf.pinned[type]; - if (prog) { - bpf_prog_put(prog); + if (pinned.prog) { + bpf_prog_put(pinned.prog); static_branch_dec(&cgroup_bpf_enabled_key); } } @@ -47,11 +47,12 @@ void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent) unsigned int type; for (type = 0; type < ARRAY_SIZE(cgrp->bpf.effective); type++) { - struct bpf_prog *e; + struct bpf_prog *prog; - e = rcu_dereference_protected(parent->bpf.effective[type], - lockdep_is_held(&cgroup_mutex)); - rcu_assign_pointer(cgrp->bpf.effective[type], e); + prog = rcu_dereference_protected( + parent->bpf.effective[type].prog, + lockdep_is_held(&cgroup_mutex)); + rcu_assign_pointer(cgrp->bpf.effective[type].prog, prog); } } @@ -87,13 +88,13 @@ void __cgroup_bpf_update(struct cgroup *cgrp, struct bpf_prog *prog, enum bpf_attach_type type) { - struct bpf_prog *old_prog, *effective; + struct bpf_prog *old_prog = NULL, *effective_prog; struct cgroup_subsys_state *pos; - old_prog = xchg(cgrp->bpf.prog + type, prog); + old_prog = xchg(&cgrp->bpf.pinned[type].prog, prog); - effective = (!prog && parent) ? - rcu_dereference_protected(parent->bpf.effective[type], + effective_prog = (!prog && parent) ? + rcu_dereference_protected(parent->bpf.effective[type].prog, lockdep_is_held(&cgroup_mutex)) : prog; @@ -101,11 +102,11 @@ void __cgroup_bpf_update(struct cgroup *cgrp, struct cgroup *desc = container_of(pos, struct cgroup, self); /* skip the subtree if the descendant has its own program */ - if (desc->bpf.prog[type] && desc != cgrp) + if (desc->bpf.pinned[type].prog && desc != cgrp) pos = css_rightmost_descendant(pos); else - rcu_assign_pointer(desc->bpf.effective[type], - effective); + rcu_assign_pointer(desc->bpf.effective[type].prog, + effective_prog); } if (prog) @@ -151,7 +152,7 @@ int __cgroup_bpf_run_filter(struct sock *sk, rcu_read_lock(); - prog = rcu_dereference(cgrp->bpf.effective[type]); + prog = rcu_dereference(cgrp->bpf.effective[type].prog); if (prog) { unsigned int offset = skb->data - skb_network_header(skb); -- 2.9.3 From mboxrd@z Thu Jan 1 00:00:00 1970 Reply-To: kernel-hardening@lists.openwall.com From: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= Date: Wed, 26 Oct 2016 08:56:49 +0200 Message-Id: <20161026065654.19166-14-mic@digikod.net> In-Reply-To: <20161026065654.19166-1-mic@digikod.net> References: <20161026065654.19166-1-mic@digikod.net> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: [kernel-hardening] [RFC v4 13/18] bpf/cgroup: Replace struct bpf_prog with struct bpf_object To: linux-kernel@vger.kernel.org Cc: =?UTF-8?q?Micka=C3=ABl=20Sala=C3=BCn?= , Alexei Starovoitov , Andy Lutomirski , Daniel Borkmann , Daniel Mack , David Drysdale , "David S . Miller" , "Eric W . Biederman" , James Morris , Jann Horn , Kees Cook , Paul Moore , Sargun Dhillon , "Serge E . Hallyn" , Tejun Heo , Thomas Graf , Will Drewry , kernel-hardening@lists.openwall.com, linux-api@vger.kernel.org, linux-security-module@vger.kernel.org, netdev@vger.kernel.org, cgroups@vger.kernel.org List-ID: This allows CONFIG_CGROUP_BPF to manage different type of pointers instead of only eBPF programs. This will be useful for the next commits to support Landlock with cgroups. Changes since v3: * do not use an union of pointers but a struct (suggested by Kees Cook) Signed-off-by: Mickaël Salaün Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: Daniel Mack Cc: David S. Miller Cc: Tejun Heo Link: https://lkml.kernel.org/r/CAGXu5j+F80XCPSVL0VxAAoTiYk5D1NKKC3jyAU=Z0Gi7L9S0aw@mail.gmail.com --- include/linux/bpf-cgroup.h | 8 ++++++-- kernel/bpf/cgroup.c | 35 ++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h index fc076de74ab9..2f608e5d94e9 100644 --- a/include/linux/bpf-cgroup.h +++ b/include/linux/bpf-cgroup.h @@ -14,14 +14,18 @@ struct sk_buff; extern struct static_key_false cgroup_bpf_enabled_key; #define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key) +struct bpf_object { + struct bpf_prog *prog; +}; + struct cgroup_bpf { /* * Store two sets of bpf_prog pointers, one for programs that are * pinned directly to this cgroup, and one for those that are effective * when this cgroup is accessed. */ - struct bpf_prog *prog[MAX_BPF_ATTACH_TYPE]; - struct bpf_prog *effective[MAX_BPF_ATTACH_TYPE]; + struct bpf_object pinned[MAX_BPF_ATTACH_TYPE]; + struct bpf_object effective[MAX_BPF_ATTACH_TYPE]; }; void cgroup_bpf_put(struct cgroup *cgrp); diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index a0ab43f264b0..75482cd92d56 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -20,18 +20,18 @@ DEFINE_STATIC_KEY_FALSE(cgroup_bpf_enabled_key); EXPORT_SYMBOL(cgroup_bpf_enabled_key); /** - * cgroup_bpf_put() - put references of all bpf programs + * cgroup_bpf_put() - put references of all bpf objects * @cgrp: the cgroup to modify */ void cgroup_bpf_put(struct cgroup *cgrp) { unsigned int type; - for (type = 0; type < ARRAY_SIZE(cgrp->bpf.prog); type++) { - struct bpf_prog *prog = cgrp->bpf.prog[type]; + for (type = 0; type < ARRAY_SIZE(cgrp->bpf.pinned); type++) { + struct bpf_object pinned = cgrp->bpf.pinned[type]; - if (prog) { - bpf_prog_put(prog); + if (pinned.prog) { + bpf_prog_put(pinned.prog); static_branch_dec(&cgroup_bpf_enabled_key); } } @@ -47,11 +47,12 @@ void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent) unsigned int type; for (type = 0; type < ARRAY_SIZE(cgrp->bpf.effective); type++) { - struct bpf_prog *e; + struct bpf_prog *prog; - e = rcu_dereference_protected(parent->bpf.effective[type], - lockdep_is_held(&cgroup_mutex)); - rcu_assign_pointer(cgrp->bpf.effective[type], e); + prog = rcu_dereference_protected( + parent->bpf.effective[type].prog, + lockdep_is_held(&cgroup_mutex)); + rcu_assign_pointer(cgrp->bpf.effective[type].prog, prog); } } @@ -87,13 +88,13 @@ void __cgroup_bpf_update(struct cgroup *cgrp, struct bpf_prog *prog, enum bpf_attach_type type) { - struct bpf_prog *old_prog, *effective; + struct bpf_prog *old_prog = NULL, *effective_prog; struct cgroup_subsys_state *pos; - old_prog = xchg(cgrp->bpf.prog + type, prog); + old_prog = xchg(&cgrp->bpf.pinned[type].prog, prog); - effective = (!prog && parent) ? - rcu_dereference_protected(parent->bpf.effective[type], + effective_prog = (!prog && parent) ? + rcu_dereference_protected(parent->bpf.effective[type].prog, lockdep_is_held(&cgroup_mutex)) : prog; @@ -101,11 +102,11 @@ void __cgroup_bpf_update(struct cgroup *cgrp, struct cgroup *desc = container_of(pos, struct cgroup, self); /* skip the subtree if the descendant has its own program */ - if (desc->bpf.prog[type] && desc != cgrp) + if (desc->bpf.pinned[type].prog && desc != cgrp) pos = css_rightmost_descendant(pos); else - rcu_assign_pointer(desc->bpf.effective[type], - effective); + rcu_assign_pointer(desc->bpf.effective[type].prog, + effective_prog); } if (prog) @@ -151,7 +152,7 @@ int __cgroup_bpf_run_filter(struct sock *sk, rcu_read_lock(); - prog = rcu_dereference(cgrp->bpf.effective[type]); + prog = rcu_dereference(cgrp->bpf.effective[type].prog); if (prog) { unsigned int offset = skb->data - skb_network_header(skb); -- 2.9.3