Skip to contentSkip to conversation

bpf: use bpf_capable() instead of CAP_SYS_ADMIN for blinding decision

RFC PATCH Superseded Yauheni Kaliuta 4 participants 6 messages Aug 31, 2022 – Sep 16, 2022

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

RFC PATCH 1/1 bpf: use bpf_capable() instead of CAP_SYS_ADMIN for blinding decision

I'm wodering about the cap check against CAP_SYS_ADMIN. Is it intentional to provide more security or oversight in commit 2c78ee898d8f ("bpf: Implement CAP_BPF")?

---
 include/linux/filter.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/filter.h +1 -1
index a5f21dc3c432..3de96b1a736b 100644
@@ -1100,7 +1100,7 @@ static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)
 		return false;
 	if (!bpf_jit_harden)
 		return false;
-	if (bpf_jit_harden == 1 && capable(CAP_SYS_ADMIN))
+	if (bpf_jit_harden == 1 && bpf_capable())
 		return false;
 
 	return true;
Signature · 1 line
2.34.1
#1 lore raw

RFC PATCH v2 1/1 bpf: use bpf_capable() instead of CAP_SYS_ADMIN for blinding decision

The capability check can cause SELinux denial.

For example, in ptp4l, setsockopt() with the SO_ATTACH_FILTER option raises sk_attach_filter() to run a bpf program. SELinux hooks into capable() calls and performs an additional check if the task's SELinux domain has permission to "use" the given capability. ptp4l_t already has CAP_BPF granted by SELinux, so if the function used bpf_capable() as most BPF code does, there would be no change needed in selinux-policy.

---

v2: put the reasoning in the commit message

---
 include/linux/filter.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/filter.h +1 -1
index a5f21dc3c432..3de96b1a736b 100644
@@ -1100,7 +1100,7 @@ static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)
 		return false;
 	if (!bpf_jit_harden)
 		return false;
-	if (bpf_jit_harden == 1 && capable(CAP_SYS_ADMIN))
+	if (bpf_jit_harden == 1 && bpf_capable())
 		return false;
 
 	return true;
Signature · 1 line
2.34.1
maintainer
#2 lore raw
Quoted 9 lines · Yauheni Kaliuta

The capability check can cause SELinux denial.

For example, in ptp4l, setsockopt() with the SO_ATTACH_FILTER option raises sk_attach_filter() to run a bpf program. SELinux hooks into capable() calls and performs an additional check if the task's SELinux domain has permission to "use" the given capability. ptp4l_t already has CAP_BPF granted by SELinux, so if the function used bpf_capable() as most BPF code does, there would be no change needed in selinux-policy.

The selinux mentions probably aren't really necessary. The more concise way to say it is that bpf_jit_blinding_enabled() should be permitted with CAP_BPF, that full CAP_SYS_ADMIN is not needed. (Assuming that that is the case)

Quoted 24 lines

---

v2: put the reasoning in the commit message

---
 include/linux/filter.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/filter.h +1 -1
index a5f21dc3c432..3de96b1a736b 100644
@@ -1100,7 +1100,7 @@ static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)
 		return false;
 	if (!bpf_jit_harden)
 		return false;
-	if (bpf_jit_harden == 1 && capable(CAP_SYS_ADMIN))
+	if (bpf_jit_harden == 1 && bpf_capable())
 		return false;
 
 	return true;
Signature · 1 line
2.34.1
maintainer
#3 lore raw
Quoted 25 lines · Serge E. Hallyn
Quoted 9 lines · Yauheni Kaliuta

The capability check can cause SELinux denial.

For example, in ptp4l, setsockopt() with the SO_ATTACH_FILTER option raises sk_attach_filter() to run a bpf program. SELinux hooks into capable() calls and performs an additional check if the task's SELinux domain has permission to "use" the given capability. ptp4l_t already has CAP_BPF granted by SELinux, so if the function used bpf_capable() as most BPF code does, there would be no change needed in selinux-policy.

The selinux mentions probably aren't really necessary. The more concise way to say it is that bpf_jit_blinding_enabled() should be permitted with CAP_BPF, that full CAP_SYS_ADMIN is not needed. (Assuming that that is the case)

Quoted 9 lines

---

v2: put the reasoning in the commit message

---
  include/linux/filter.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 10 lines

include/linux/filter.h static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog) · line 1103

Quoted 10 lines
include/linux/filter.h +1 -1
index a5f21dc3c432..3de96b1a736b 100644
@@ -1100,7 +1100,7 @@ static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)
  		return false;
  	if (!bpf_jit_harden)
  		return false;
-	if (bpf_jit_harden == 1 && capable(CAP_SYS_ADMIN))
+	if (bpf_jit_harden == 1 && bpf_capable())

I think lowering this requirement is fine here. These days given unpriv eBPF is disabled by default, I see the main users for constant blinding coming from unpriv in particular via cBPF -> eBPF migration (e.g. old-style socket filters).

Quoted 3 lines

include/linux/filter.h static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog) · line 1106

Quoted 3 lines
include/linux/filter.h +0 -0
  		return false;
 
  	return true;

Please also update Documentation/admin-guide/sysctl/net.rst to clarify cap details.

Thanks,
Daniel

#4 lore raw

PATCH 1/1 bpf: use bpf_capable() instead of CAP_SYS_ADMIN for blinding decision

In reply to Yauheni Kaliuta

The full CAP_SYS_ADMIN requirement for blining looks too strict nowadays. These days given unpriv eBPF is disabled by default, the main users for constant blinding coming from unpriv in particular via cBPF -> eBPF migration (e.g. old-style socket filters).

Discussion: https://lore.kernel.org/bpf/[email protected]/

---
 Documentation/admin-guide/sysctl/net.rst | 3 +++
 include/linux/filter.h                   | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
Documentation/admin-guide/sysctl/net.rst +3 -0
index 805f2281e000..ff1e5b5acd28 100644
@@ -101,6 +101,9 @@ Values:
 	- 1 - enable JIT hardening for unprivileged users only
 	- 2 - enable JIT hardening for all users
 
+where "privileged user" in this context means a process having
+CAP_BPF or CAP_SYS_ADMIN in the root user name space.
+
 bpf_jit_kallsyms
 ----------------
 
include/linux/filter.h +1 -1
index 527ae1d64e27..75335432fcbc 100644
@@ -1099,7 +1099,7 @@ static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)
 		return false;
 	if (!bpf_jit_harden)
 		return false;
-	if (bpf_jit_harden == 1 && capable(CAP_SYS_ADMIN))
+	if (bpf_jit_harden == 1 && bpf_capable())
 		return false;
 
 	return true;
Signature · 1 line
2.34.1
#5 lore raw
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 8 lines · On Mon, 5 Sep 2022 12:01:49 +0300 you wrote:

The full CAP_SYS_ADMIN requirement for blining looks too strict nowadays. These days given unpriv eBPF is disabled by default, the main users for constant blinding coming from unpriv in particular via cBPF -> eBPF migration (e.g. old-style socket filters).

Discussion: https://lore.kernel.org/bpf/[email protected]/

[...]

Here is the summary with links:
- [bpf-next] bpf: use bpf_capable() instead of CAP_SYS_ADMIN for blinding decision

    https://git.kernel.org/bpf/bpf-next/c/bfeb7e399bac

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html