Skip to contentSkip to conversation

bpf: Allow CGROUP_SKB eBPF program to access sk_buff

PATCH Applied Chenbo Feng 4 participants 15 messages Jun 1, 2017 – Jun 7, 2017 2 messages not in the archive

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

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
index 339c8a1..94a9bc9 100644
@@ -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
#1 lore raw

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
index 265a0d8..59da103 100644
@@ -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
Acked-by on v2 2/2
#2 lore raw

Re: [PATCH net-next v2 2/2] bpf: Remove the capability check for cgroup skb eBPF program

Quoted 8 lines · Chenbo Feng

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.

Quoted 5 lines

Change since v1:
Change the code style in order to be compliant with checkpatch.pl preference

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?

A message that is not in the archive (sent off-list or not yet received): CAMOXUJkHsj8c6Yc8FSvJsFt3vPcf-UKV0PPVWY8ewcZuA2vUwA@mail.gmail.com

#4 lore raw

Re: [PATCH net-next v2 2/2] bpf: Remove the capability check for cgroup skb eBPF program

Quoted 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_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.

Quoted 5 lines

Change since v1:
Change the code style in order to be compliant with checkpatch.pl preference

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_ADMIN but not CAP_SYS_ADMIN it can still load and attach eBPF programs to cgroup.

got it. Thanks

maintainer
#5 lore raw

Re: [PATCH net-next v2 2/2] bpf: Remove the capability check for cgroup skb eBPF program

In reply to Alexei Starovoitov
Quoted 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_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

Quoted 8 lines
Quoted 1 line

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().

#6 lore raw

Re: [PATCH net-next v2 2/2] bpf: Remove the capability check for cgroup skb eBPF program

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_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.

Quoted 5 lines

Change since v1:
Change the code style in order to be compliant with checkpatch.pl preference

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?

maintainer
#7 lore raw

Re: [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_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.

Quoted 5 lines

Change since v1:
Change the code style in order to be compliant with checkpatch.pl preference

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_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?

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

maintainer Applied
#8 lore raw

Re: [PATCH net-next v2 2/2] bpf: Remove the capability check for cgroup skb eBPF program

In reply to Chenbo Feng

From: 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_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.

Quoted 5 lines

Change since v1:
Change the code style in order to be compliant with checkpatch.pl preference

Applied.

Acked-by on v2 1/2
#9 lore raw
In reply to Chenbo Feng
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_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.

maintainer Applied
#10 lore raw
In reply to Chenbo Feng

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_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.

Applied.

maintainer
#11 lore raw
In reply to Chenbo Feng
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_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.

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
index 339c8a1..94a9bc9 100644
@@ -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;
maintainer
#12 lore raw
Quoted 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_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.

Quoted 7 lines
Quoted 1 line

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?

See my other email [1], this one is also problematic wrt SKF_LL_OFF.

[1] http://patchwork.ozlabs.org/patch/771946/

A message that is not in the archive (sent off-list or not yet received): CAMOXUJ=mUKvMMFnjfOUHuGms+p2fE+NkwEcORdV9eLBsFwyREQ@mail.gmail.com

maintainer
#14 lore raw

From: 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_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.

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 8 lines
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_bytes helper 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.

maintainer
#15 lore raw
Quoted 31 lines · David Miller

From: Chenbo Feng <[email protected]>
Date: Tue, 6 Jun 2017 13:24:11 -0700

Quoted 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_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.

Quoted 9 lines
Quoted 2 lines

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?

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_bytes helper 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/

maintainer
#16 lore raw

From: 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 -0700

Quoted 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_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.

Quoted 9 lines
Quoted 2 lines

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 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_bytes helper 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!