Skip to contentSkip to conversation

bpf: add bpf_jit_limit knob to restrict unpriv allocations

PATCH Applied Daniel Borkmann 2 participants 2 messages Oct 22, 2018 – Oct 26, 2018

Subsystems (5)
lore
Share & tools
lore

Daniel Borkmann

author maintainer
#0 lore raw

PATCH 1/1 bpf: add bpf_jit_limit knob to restrict unpriv allocations

Rick reported that the BPF JIT could potentially fill the entire module space with BPF programs from unprivileged users which would prevent later attempts to load normal kernel modules or privileged BPF programs, for example. If JIT was enabled but unsuccessful to generate the image, then before commit 290af86629b2 ("bpf: introduce BPF_JIT_ALWAYS_ON config") we would always fall back to the BPF interpreter. Nowadays in the case where the CONFIG_BPF_JIT_ALWAYS_ON could be set, then the load will abort with a failure since the BPF interpreter was compiled out.

Add a global limit and enforce it for unprivileged users such that in case of BPF interpreter compiled out we fail once the limit has been reached or we fall back to BPF interpreter earlier w/o using module mem if latter was compiled in. In a next step, fair share among unprivileged users can be resolved in particular for the case where we would fail hard once limit is reached.

Fixes 290af86629b2 ("bpf: introduce BPF_JIT_ALWAYS_ON config")
Fixes 0a14842f5a3c ("net: filter: Just In Time compiler for x86-64")
Cc Eric Dumazet <[email protected]>
Cc Jann Horn <[email protected]>
Cc Kees Cook <[email protected]>

---
Hi Rick, I've reworked the original patch into something much simpler which is only focussing on the actual main issue we want to resolve right now as a first step to make some forward progress, that is, limiting usage on the JIT for unprivileged users. Tested the below on x86 and arm64. (Trimmed down massive Cc list as well a bit and Cc'ed people related to commits referenced and netdev where BPF patches are usually discussed.) Thanks a lot!

 Documentation/sysctl/net.txt |  8 ++++++++
 include/linux/filter.h       |  1 +
 kernel/bpf/core.c            | 49 +++++++++++++++++++++++++++++++++++++++++---
 net/core/sysctl_net_core.c   | 10 +++++++--
 4 files changed, 63 insertions(+), 5 deletions(-)
Documentation/sysctl/net.txt +8 -0
index 9ecde51..2793d4e 100644
@@ -92,6 +92,14 @@ Values :
 	0 - disable JIT kallsyms export (default value)
 	1 - enable JIT kallsyms export for privileged users only
 
+bpf_jit_limit
+-------------
+
+This enforces a global limit for memory allocations to the BPF JIT
+compiler in order to reject unprivileged JIT requests once it has
+been surpassed. bpf_jit_limit contains the value of the global limit
+in bytes.
+
 dev_weight
 --------------
 
include/linux/filter.h +1 -0
index 91b4c93..de629b7 100644
@@ -854,6 +854,7 @@ bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
 extern int bpf_jit_enable;
 extern int bpf_jit_harden;
 extern int bpf_jit_kallsyms;
+extern int bpf_jit_limit;
 
 typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
 
kernel/bpf/core.c +46 -3

90 lines not on this page · raw message

net/core/sysctl_net_core.c +8 -2
index b1a2c5e..37b4667 100644
@@ -279,7 +279,6 @@ static int proc_dointvec_minmax_bpf_enable(struct ctl_table *table, int write,
 	return ret;
 }
 
-# ifdef CONFIG_HAVE_EBPF_JIT
 static int
 proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write,
 				    void __user *buffer, size_t *lenp,
@@ -290,7 +289,6 @@ proc_dointvec_minmax_bpf_restricted(struct ctl_table *table, int write,
 
 	return proc_dointvec_minmax(table, write, buffer, lenp, ppos);
 }
-# endif
 #endif
 
 static struct ctl_table net_core_table[] = {
@@ -397,6 +395,14 @@ static struct ctl_table net_core_table[] = {
 		.extra2		= &one,
 	},
 # endif
+	{
+		.procname	= "bpf_jit_limit",
+		.data		= &bpf_jit_limit,
+		.maxlen		= sizeof(int),
+		.mode		= 0600,
+		.proc_handler	= proc_dointvec_minmax_bpf_restricted,
+		.extra1		= &one,
+	},
 #endif
 	{
 		.procname	= "netdev_tstamp_prequeue",
Signature · 1 line
2.9.5
#1 lore raw
Quoted 25 lines · Daniel Borkmann

Rick reported that the BPF JIT could potentially fill the entire module space with BPF programs from unprivileged users which would prevent later attempts to load normal kernel modules or privileged BPF programs, for example. If JIT was enabled but unsuccessful to generate the image, then before commit 290af86629b2 ("bpf: introduce BPF_JIT_ALWAYS_ON config") we would always fall back to the BPF interpreter. Nowadays in the case where the CONFIG_BPF_JIT_ALWAYS_ON could be set, then the load will abort with a failure since the BPF interpreter was compiled out.

Add a global limit and enforce it for unprivileged users such that in case of BPF interpreter compiled out we fail once the limit has been reached or we fall back to BPF interpreter earlier w/o using module mem if latter was compiled in. In a next step, fair share among unprivileged users can be resolved in particular for the case where we would fail hard once limit is reached.

Fixes 290af86629b2 ("bpf: introduce BPF_JIT_ALWAYS_ON config")
Fixes 0a14842f5a3c ("net: filter: Just In Time compiler for x86-64")
Cc Eric Dumazet <[email protected]>
Cc Jann Horn <[email protected]>
Cc Kees Cook <[email protected]>
Quoted 8 lines

---
Hi Rick, I've reworked the original patch into something much simpler which is only focussing on the actual main issue we want to resolve right now as a first step to make some forward progress, that is, limiting usage on the JIT for unprivileged users. Tested the below on x86 and arm64. (Trimmed down massive Cc list as well a bit and Cc'ed people related to commits referenced and netdev where BPF patches are usually discussed.) Thanks a lot!

Applied to bpf tree. Thanks Daniel.