bpf: Allow CGROUP_SKB eBPF program to access sk_buff
Subsystems (4)
PATCH v2 1/2 bpf: Allow CGROUP_SKB eBPF program to access sk_buff
From: Chenbo Feng <[email protected]>
This allows cgroup eBPF program to classify packet based on their protocol or other detail information. Currently program need CAP_NET_ADMIN privilege to attach a cgroup eBPF program, and A process with CAP_NET_ADMIN can already see all packets on the system, for example, by creating an iptables rules that causes the packet to be passed to userspace via NFLOG.
--- kernel/bpf/verifier.c | 1 + 1 file changed, 1 insertion(+)
kernel/bpf/verifier.c +1 -0
@@ -2419,6 +2419,7 @@ static bool may_access_skb(enum bpf_prog_type type)case BPF_PROG_TYPE_SOCKET_FILTER :case BPF_PROG_TYPE_SCHED_CLS :case BPF_PROG_TYPE_SCHED_ACT : +case BPF_PROG_TYPE_CGROUP_SKB :return true;default :return false;
Signature · 1 line
2.7.4
PATCH v2 2/2 bpf: Remove the capability check for cgroup skb eBPF program
From: Chenbo Feng <[email protected]>
Currently loading a cgroup skb eBPF program require a CAP_SYS_ADMIN capability while attaching the program to a cgroup only requires the user have CAP_NET_ADMIN privilege. We can escape the capability check when load the program just like socket filter program to make the capability requirement consistent.
Change since v1:
Change the code style in order to be compliant with checkpatch.pl preference
--- kernel/bpf/syscall.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/bpf/syscall.c +3 -1
@@ -815,7 +815,9 @@ static int bpf_prog_load(union bpf_attr *attr) attr->kern_version != LINUX_VERSION_CODE)return -EINVAL ; -if (type !=BPF_PROG_TYPE_SOCKET_FILTER && !capable (CAP_SYS_ADMIN )) +if (type !=BPF_PROG_TYPE_SOCKET_FILTER && + type !=BPF_PROG_TYPE_CGROUP_SKB && + !capable (CAP_SYS_ADMIN ))return -EPERM ;/* plain bpf_prog allocation */
Signature · 1 line
2.7.4
Quoted 10 lines · Chenbo Feng
From: Chenbo Feng <[email protected]>
This allows cgroup eBPF program to classify packet based on their protocol or other detail information. Currently program need
CAP_NET_ADMINprivilege to attach a cgroup eBPF program, and A process withCAP_NET_ADMINcan already see all packets on the system, for example, by creating an iptables rules that causes the packet to be passed to userspace via NFLOG.Signed-off-by Chenbo Feng <[email protected]>
Re: [PATCH net-next v2 2/2] bpf: Remove the capability check for cgroup skb eBPF program
In reply to Chenbo FengQuoted 8 lines · Chenbo Feng
From: Chenbo Feng <[email protected]>
Currently loading a cgroup skb eBPF program require a
CAP_SYS_ADMINcapability while attaching the program to a cgroup only requires the user haveCAP_NET_ADMINprivilege. We can escape the capability check when load the program just like socket filter program to make the capability requirement consistent.
Quoted 5 lines
Change since v1:
Change the code style in order to be compliant with checkpatch.pl preferenceSigned-off-by Chenbo Feng <[email protected]>
as far as I can see they're indeed the same as socket filters, so
but I don't quite understand how it helps, since as you said attaching such unpriv fd to cgroup still requires root. Do you have more patches to follow?
Re: [PATCH net-next v2 2/2] bpf: Remove the capability check for cgroup skb eBPF program
In reply to a message not in the archiveQuoted 21 lines · Chenbo Feng
Quoted 18 lines · Alexei Starovoitov
Quoted 8 lines · Chenbo Feng
From: Chenbo Feng <[email protected]>
Currently loading a cgroup skb eBPF program require a
CAP_SYS_ADMINcapability while attaching the program to a cgroup only requires the user haveCAP_NET_ADMINprivilege. We can escape the capability check when load the program just like socket filter program to make the capability requirement consistent.Quoted 5 lines
Change since v1:
Change the code style in order to be compliant with checkpatch.pl preferenceSigned-off-by Chenbo Feng <[email protected]>as far as I can see they're indeed the same as socket filters, so
Quoted 8 lines
Quoted 5 lines
but I don't quite understand how it helps, since as you said attaching such unpriv fd to cgroup still requires root. Do you have more patches to follow?
Actually not, the purpose of this patch is only to make sure if a program
have
CAP_NET_ADMINbut notCAP_SYS_ADMINit can still load and attach eBPF programs to cgroup.
got it. Thanks
From: Chenbo Feng <[email protected]>
Date: Wed, 31 May 2017 18:15:59 -0700
Quoted 10 lines
From: Chenbo Feng <[email protected]>
This allows cgroup eBPF program to classify packet based on their protocol or other detail information. Currently program need
CAP_NET_ADMINprivilege to attach a cgroup eBPF program, and A process withCAP_NET_ADMINcan already see all packets on the system, for example, by creating an iptables rules that causes the packet to be passed to userspace via NFLOG.Signed-off-by Chenbo Feng <[email protected]>
Applied.
Re: [PATCH net-next v2 2/2] bpf: Remove the capability check for cgroup skb eBPF program
In reply to Chenbo FengFrom: Chenbo Feng <[email protected]>
Date: Wed, 31 May 2017 18:16:00 -0700
Quoted 8 lines
From: Chenbo Feng <[email protected]>
Currently loading a cgroup skb eBPF program require a
CAP_SYS_ADMINcapability while attaching the program to a cgroup only requires the user haveCAP_NET_ADMINprivilege. We can escape the capability check when load the program just like socket filter program to make the capability requirement consistent.
Quoted 5 lines
Change since v1:
Change the code style in order to be compliant with checkpatch.pl preferenceSigned-off-by Chenbo Feng <[email protected]>
Applied.
Daniel Borkmann
maintainerQuoted 10 lines · Chenbo Feng
From: Chenbo Feng <[email protected]>
This allows cgroup eBPF program to classify packet based on their protocol or other detail information. Currently program need
CAP_NET_ADMINprivilege to attach a cgroup eBPF program, and A process withCAP_NET_ADMINcan already see all packets on the system, for example, by creating an iptables rules that causes the packet to be passed to userspace via NFLOG.Signed-off-by Chenbo Feng <[email protected]>
Sorry, but I am puzzled what above change log has to do with the below diff?! Back then we decided not to add BPF_PROG_TYPE_CGROUP_SKB to may_access_skb(), since one can already use bpf_skb_load_bytes() helper to access pkt data, which is a much more flexible interface. Mind to elaborate why you cannot use bpf_skb_load_bytes() instead?
Quoted 17 lines
--- kernel/bpf/verifier.c | 1 + 1 file changed, 1 insertion(+)kernel/bpf/verifier.c +1
-0@@ -2419,6 +2419,7 @@ static bool may_access_skb(enum bpf_prog_type type)case BPF_PROG_TYPE_SOCKET_FILTER :case BPF_PROG_TYPE_SCHED_CLS :case BPF_PROG_TYPE_SCHED_ACT : +case BPF_PROG_TYPE_CGROUP_SKB :return true;default :return false;
Daniel Borkmann
maintainerQuoted 10 lines · Daniel Borkmann
Quoted 9 lines · Chenbo Feng
From: Chenbo Feng <[email protected]>
This allows cgroup eBPF program to classify packet based on their protocol or other detail information. Currently program need
CAP_NET_ADMINprivilege to attach a cgroup eBPF program, and A process withCAP_NET_ADMINcan already see all packets on the system, for example, by creating an iptables rules that causes the packet to be passed to userspace via NFLOG.
Quoted 7 lines
Quoted 1 line
Signed-off-by Chenbo Feng <[email protected]>Sorry, but I am puzzled what above change log has to do with the below diff?! Back then we decided not to add
BPF_PROG_TYPE_CGROUP_SKBtomay_access_skb(), since one can already usebpf_skb_load_bytes()helper to access pkt data, which is a much more flexible interface. Mind to elaborate why you cannot usebpf_skb_load_bytes()instead?
See my other email [1], this one is also problematic wrt SKF_LL_OFF.
Daniel Borkmann
maintainerRe: [PATCH net-next v2 2/2] bpf: Remove the capability check for cgroup skb eBPF program
In reply to Alexei StarovoitovQuoted 13 lines · Alexei Starovoitov
Quoted 12 lines · Chenbo Feng
From: Chenbo Feng <[email protected]>
Currently loading a cgroup skb eBPF program require a
CAP_SYS_ADMINcapability while attaching the program to a cgroup only requires the user haveCAP_NET_ADMINprivilege. We can escape the capability check when load the program just like socket filter program to make the capability requirement consistent.Change since v1:
Change the code style in order to be compliant with checkpatch.pl preference
Quoted 8 lines
Quoted 1 line
Signed-off-by Chenbo Feng <[email protected]>as far as I can see they're indeed the same as socket filters, so
but I don't quite understand how it helps, since as you said attaching such unpriv fd to cgroup still requires root. Do you have more patches to follow?
Hmm, when we relax this from capable(CAP_SYS_ADMIN) to unprivileged, then we must at least also zero out the not-yet-initialized memory for the mac header for egress case in __cgroup_bpf_run_filter_skb().
David Miller
maintainerFrom: Chenbo Feng <[email protected]>
Date: Tue, 6 Jun 2017 13:24:11 -0700
Quoted 26 lines
Quoted 23 lines · Daniel Borkmann
Quoted 20 lines · Daniel Borkmann
Quoted 11 lines · Chenbo Feng
From: Chenbo Feng <[email protected]>
This allows cgroup eBPF program to classify packet based on their protocol or other detail information. Currently program need
CAP_NET_ADMINprivilege to attach a cgroup eBPF program, and A process withCAP_NET_ADMINcan already see all packets on the system, for example, by creating an iptables rules that causes the packet to be passed to userspace via NFLOG.Signed-off-by Chenbo Feng <[email protected]>Sorry, but I am puzzled what above change log has to do with the below diff?! Back then we decided not to add
BPF_PROG_TYPE_CGROUP_SKBtomay_access_skb(), since one can already usebpf_skb_load_bytes()helper to access pkt data, which is a much more flexible interface. Mind to elaborate why you cannot usebpf_skb_load_bytes()instead?
Quoted 8 lines
Quoted 3 lines
See my other email [1], this one is also problematic wrt
SKF_LL_OFF.Oh sorry I just find out the
bpf_skb_load_byteshelper already can achieve the goal. There is no point to add my patch then. Thanks you for pointing it out and fixing it.
If something now needs to be reverted, you need to send that revert to me.
Thanks.
Daniel Borkmann
maintainerQuoted 31 lines · David Miller
From: Chenbo Feng <[email protected]>
Date: Tue, 6 Jun 2017 13:24:11 -0700Quoted 28 lines
Quoted 25 lines · Daniel Borkmann
Quoted 11 lines · Daniel Borkmann
Quoted 9 lines · Chenbo Feng
From: Chenbo Feng <[email protected]>
This allows cgroup eBPF program to classify packet based on their protocol or other detail information. Currently program need
CAP_NET_ADMINprivilege to attach a cgroup eBPF program, and A process withCAP_NET_ADMINcan already see all packets on the system, for example, by creating an iptables rules that causes the packet to be passed to userspace via NFLOG.Quoted 9 lines
Quoted 2 lines
Signed-off-by Chenbo Feng <[email protected]>Sorry, but I am puzzled what above change log has to do with the below diff?! Back then we decided not to add
BPF_PROG_TYPE_CGROUP_SKBtomay_access_skb(), since one can already usebpf_skb_load_bytes()helper to access pkt data, which is a much more flexible interface. Mind to elaborate why you cannot usebpf_skb_load_bytes()instead?See my other email [1], this one is also problematic wrt
SKF_LL_OFF.
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
[1] http://patchwork.ozlabs.org/patch/771946/Oh sorry I just find out the
bpf_skb_load_byteshelper already can achieve the goal. There is no point to add my patch then. Thanks you for pointing it out and fixing it.If something now needs to be reverted, you need to send that revert to me.
It's sitting here: http://patchwork.ozlabs.org/patch/771946/
David Miller
maintainerFrom: Daniel Borkmann <[email protected]>
Date: Tue, 06 Jun 2017 22:27:15 +0200
Quoted 41 lines · David Miller
Quoted 40 lines
From: Chenbo Feng <[email protected]>
Date: Tue, 6 Jun 2017 13:24:11 -0700Quoted 36 lines
Quoted 23 lines · Daniel Borkmann
Quoted 11 lines · Daniel Borkmann
Quoted 9 lines · Chenbo Feng
From: Chenbo Feng <[email protected]>
This allows cgroup eBPF program to classify packet based on their protocol or other detail information. Currently program need
CAP_NET_ADMINprivilege to attach a cgroup eBPF program, and A process withCAP_NET_ADMINcan already see all packets on the system, for example, by creating an iptables rules that causes the packet to be passed to userspace via NFLOG.Quoted 9 lines
Quoted 2 lines
Signed-off-by Chenbo Feng <[email protected]>Sorry, but I am puzzled what above change log has to do with the below diff?! Back then we decided not to add
BPF_PROG_TYPE_CGROUP_SKBtomay_access_skb(), since one can already usebpf_skb_load_bytes()helper to access pkt data, which is a much more flexible interface. Mind to elaborate why you cannot usebpf_skb_load_bytes()instead?Quoted 3 lines
See my other email [1], this one is also problematic wrt
SKF_LL_OFF.[1] http://patchwork.ozlabs.org/patch/771946/Oh sorry I just find out the
bpf_skb_load_byteshelper already can achieve
the goal. There is no point to add my patch then. Thanks you for pointing
it out and fixing it.
Quoted 4 lines
Quoted 2 lines
If something now needs to be reverted, you need to send that revert to me.
It's sitting here: http://patchwork.ozlabs.org/patch/771946/
I see that now, applied to net-next, thanks!
Re: [PATCH net-next v2 2/2] bpf: Remove the capability check for cgroup skb eBPF program
In reply to Daniel BorkmannQuoted 19 lines · Daniel Borkmann
Quoted 18 lines · Alexei Starovoitov
Quoted 8 lines · Chenbo Feng
From: Chenbo Feng <[email protected]>
Currently loading a cgroup skb eBPF program require a
CAP_SYS_ADMINcapability while attaching the program to a cgroup only requires the user haveCAP_NET_ADMINprivilege. We can escape the capability check when load the program just like socket filter program to make the capability requirement consistent.Quoted 5 lines
Change since v1:
Change the code style in order to be compliant with checkpatch.pl preferenceSigned-off-by Chenbo Feng <[email protected]>as far as I can see they're indeed the same as socket filters, so
Quoted 8 lines
Quoted 3 lines
but I don't quite understand how it helps, since as you said attaching such unpriv fd to cgroup still requires root. Do you have more patches to follow?
Hmm, when we relax this from
capable(CAP_SYS_ADMIN)to unprivileged, then we must at least also zero out the not-yet-initialized memory for the mac header for egress case in__cgroup_bpf_run_filter_skb().
Do you mean something like:
if (type == BPF_CGROUP_INET_EGRESS) {
offset = skb_network_header(skb) - skb_mac_header(skb);
memset(skb_mac_header(skb), 0, offset)
}
And could you explain more on why we need to do this if we remove the CAP_SYS_ADMIN check? I thought we still cannot directly access the sk_buff without using bpf_skb_load_bytes helper and we still need a CAP_NET_ADMIN in order to attach and run the program on egress side right?
Daniel Borkmann
maintainerRe: [PATCH net-next v2 2/2] bpf: Remove the capability check for cgroup skb eBPF program
Quoted 28 lines · Chenbo Feng
Quoted 19 lines · Daniel Borkmann
Quoted 18 lines · Alexei Starovoitov
Quoted 8 lines · Chenbo Feng
From: Chenbo Feng <[email protected]>
Currently loading a cgroup skb eBPF program require a
CAP_SYS_ADMINcapability while attaching the program to a cgroup only requires the user haveCAP_NET_ADMINprivilege. We can escape the capability check when load the program just like socket filter program to make the capability requirement consistent.Quoted 5 lines
Change since v1:
Change the code style in order to be compliant with checkpatch.pl preferenceSigned-off-by Chenbo Feng <[email protected]>as far as I can see they're indeed the same as socket filters, so
Quoted 7 lines
Quoted 3 lines
but I don't quite understand how it helps, since as you said attaching such unpriv fd to cgroup still requires root. Do you have more patches to follow?
Hmm, when we relax this from
capable(CAP_SYS_ADMIN)to unprivileged, then we must at least also zero out the not-yet-initialized memory for the mac header for egress case in__cgroup_bpf_run_filter_skb().
Quoted 7 lines
Do you mean something like:
if (type ==
BPF_CGROUP_INET_EGRESS) {offset =
skb_network_header(skb)-skb_mac_header(skb);
memset(skb_mac_header(skb), 0, offset)
That won't work, reason is the same as per 92046578ac88 ("bpf: cgroup skb progs cannot access ld_abs/ind"), meaning that mac header is not yet set at that point in time, but more below.
Quoted 6 lines
}
And could you explain more on why we need to do this if we remove the
CAP_SYS_ADMINcheck? I thought we still cannot directly access thesk_buffwithout usingbpf_skb_load_byteshelper and we still need aCAP_NET_ADMINin order to attach and run the program on egress side right?
Ok, forget this comment of mine. The __cgroup_bpf_run_filter_skb() does __skb_push()/__skb_pull(), but for egress case the offset is always 0, so we don't start at mac header but at network header instead, meaning memset() is not needed.
Thanks,
Daniel