From: Maciej Żenczykowski <maze@google.com>
Date: Sat, 20 Jun 2020 21:26:16 +0000 (-0700)
Subject: bpf: Restore behaviour of CAP_SYS_ADMIN allowing the loading of networking bpf programs
X-Git-Tag: baikal/mips/sdk5.9~13212^2~55^2~14
X-Git-Url: https://git.baikalelectronics.ru/sdk/?a=commitdiff_plain;h=db0adb1198b91de4367710c7c80b17ce710b1f3d;p=kernel.git

bpf: Restore behaviour of CAP_SYS_ADMIN allowing the loading of networking bpf programs

This is a fix for a regression in commit b38450704486 ("bpf: Implement CAP_BPF").
Before the above commit it was possible to load network bpf programs
with just the CAP_SYS_ADMIN privilege.

The Android bpfloader happens to run in such a configuration (it has
SYS_ADMIN but not NET_ADMIN) and creates maps and loads bpf programs
for later use by Android's netd (which has NET_ADMIN but not SYS_ADMIN).

Fixes: b38450704486 ("bpf: Implement CAP_BPF")
Reported-by: John Stultz <john.stultz@linaro.org>
Signed-off-by: Maciej Żenczykowski <maze@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Tested-by: John Stultz <john.stultz@linaro.org>
Link: https://lore.kernel.org/bpf/20200620212616.93894-1-zenczykowski@gmail.com
---

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 8da159936bab1..7d946435587de 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -2121,7 +2121,7 @@ static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
 	    !bpf_capable())
 		return -EPERM;
 
-	if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN))
+	if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
 		return -EPERM;
 	if (is_perfmon_prog_type(type) && !perfmon_capable())
 		return -EPERM;